agppcgas.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit the GAS asm writers for PowerPC/PowerPC64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {****************************************************************************}
  18. { Helper routines for Instruction Writer }
  19. {****************************************************************************}
  20. unit agppcgas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmbase,
  25. aasmtai,aasmdata,
  26. aggas,
  27. cpubase,cgutils,
  28. globtype;
  29. type
  30. TPPCInstrWriter=class(TCPUInstrWriter)
  31. procedure WriteInstruction(hp : tai);override;
  32. end;
  33. TPPCGNUAssembler=class(TGNUassembler)
  34. constructor create(smart: boolean); override;
  35. procedure WriteExtraHeader; override;
  36. end;
  37. TPPCAppleGNUAssembler=class(TAppleGNUassembler)
  38. constructor create(smart: boolean); override;
  39. function MakeCmdLine: TCmdStr; override;
  40. end;
  41. TPPCAIXAssembler=class(TPPCGNUAssembler)
  42. constructor create(smart: boolean); override;
  43. protected
  44. function sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string; override;
  45. procedure WriteExtraHeader; override;
  46. procedure WriteExtraFooter; override;
  47. procedure WriteDirectiveName(dir: TAsmDirective); override;
  48. end;
  49. topstr = string[4];
  50. function getreferencestring(var ref : treference) : string;
  51. function getopstr_jmp(const o:toper) : string;
  52. function getopstr(const o:toper) : string;
  53. function branchmode(o: tasmop): topstr;
  54. function cond2str(op: tasmop; c: tasmcond): string;
  55. implementation
  56. uses
  57. cutils,globals,verbose,
  58. cgbase,systems,
  59. assemble,
  60. itcpugas,cpuinfo,
  61. aasmcpu;
  62. {$ifdef cpu64bitaddr}
  63. const
  64. refaddr2str: array[trefaddr] of string[9] = ('', '', '', '', '@l', '@h', '@higher', '@highest', '@ha', '@highera', '@highesta');
  65. verbose_refaddrs = [addr_low, addr_high, addr_higher, addr_highest, addr_higha, addr_highera, addr_highesta];
  66. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','','','lo16', 'hi16', '@err', '@err', 'ha16', '@err', '@err');
  67. {$else cpu64bitaddr}
  68. const
  69. refaddr2str: array[trefaddr] of string[3] = ('','','','','@l','@h','@ha');
  70. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','','','lo16','hi16','ha16');
  71. verbose_refaddrs = [addr_low,addr_high,addr_higha];
  72. {$endif cpu64bitaddr}
  73. function getreferencestring(var ref : treference) : string;
  74. var
  75. s : string;
  76. begin
  77. with ref do
  78. begin
  79. if ((offset < -32768) or (offset > 32767)) and
  80. (refaddr = addr_no) then
  81. internalerror(2006052501);
  82. case refaddr of
  83. addr_no:
  84. s := '';
  85. addr_pic_no_got:
  86. begin
  87. { used for TOC-based loads }
  88. if (base<>NR_RTOC) or
  89. (index<>NR_NO) or
  90. (offset<>0) or
  91. not assigned(symbol) then
  92. internalerror(2011122701);
  93. if target_asm.dollarsign<>'$' then
  94. getreferencestring:=ReplaceForbiddenAsmSymbolChars(symbol.name)+'('+gas_regname(NR_RTOC)+')'
  95. else
  96. getreferencestring:=symbol.name+'('+gas_regname(NR_RTOC)+')';
  97. exit;
  98. end
  99. else
  100. begin
  101. if target_info.system in [system_powerpc_darwin,system_powerpc64_darwin] then
  102. s := refaddr2str_darwin[refaddr]
  103. else
  104. s :='';
  105. s := s+'(';
  106. if assigned(symbol) then
  107. begin
  108. if target_asm.dollarsign<>'$' then
  109. begin
  110. s:=s+ReplaceForbiddenAsmSymbolChars(symbol.name);
  111. if assigned(relsymbol) then
  112. s:=s+'-'+ReplaceForbiddenAsmSymbolChars(relsymbol.name)
  113. end
  114. else
  115. begin
  116. s:=s+symbol.name;
  117. if assigned(relsymbol) then
  118. s:=s+'-'+relsymbol.name;
  119. end;
  120. end;
  121. end;
  122. end;
  123. if offset<0 then
  124. s:=s+tostr(offset)
  125. else
  126. if (offset>0) then
  127. begin
  128. if assigned(symbol) then
  129. s:=s+'+'+tostr(offset)
  130. else
  131. s:=s+tostr(offset);
  132. end;
  133. if not(refaddr in [addr_no,addr_pic_no_got]) then
  134. begin
  135. s := s+')';
  136. if (refaddr in verbose_refaddrs) and
  137. not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin]) then
  138. s := s+refaddr2str[refaddr];
  139. end;
  140. {$ifdef cpu64bitaddr}
  141. if (refaddr=addr_pic) and
  142. (target_info.system=system_powerpc64_linux) then
  143. s := s + '@got';
  144. {$endif cpu64bitaddr}
  145. if (index=NR_NO) then
  146. begin
  147. if offset=0 then
  148. begin
  149. if not (assigned(symbol)) then
  150. s:=s+'0';
  151. end;
  152. if (base<>NR_NO) then
  153. s:=s+'('+gas_regname(base)+')'
  154. else if not assigned(symbol) and
  155. not(refaddr in verbose_refaddrs) then
  156. s:=s+'(0)';
  157. end
  158. else if (index<>NR_NO) and (base<>NR_NO) then
  159. begin
  160. if (offset=0) then
  161. s:=s+gas_regname(base)+','+gas_regname(index)
  162. else
  163. internalerror(2006052502);
  164. end;
  165. end;
  166. getreferencestring:=s;
  167. end;
  168. function getopstr_jmp(const o:toper) : string;
  169. var
  170. hs : string;
  171. begin
  172. case o.typ of
  173. top_reg :
  174. getopstr_jmp:=gas_regname(o.reg);
  175. { no top_ref jumping for powerpc }
  176. top_const :
  177. getopstr_jmp:=tostr(o.val);
  178. top_ref :
  179. begin
  180. if o.ref^.refaddr<>addr_full then
  181. internalerror(200402267);
  182. hs:=o.ref^.symbol.name;
  183. if target_asm.dollarsign<>'$' then
  184. hs:=ReplaceForbiddenAsmSymbolChars(hs);
  185. if o.ref^.offset>0 then
  186. hs:=hs+'+'+tostr(o.ref^.offset)
  187. else
  188. if o.ref^.offset<0 then
  189. hs:=hs+tostr(o.ref^.offset);
  190. getopstr_jmp:=hs;
  191. end;
  192. top_none:
  193. getopstr_jmp:='';
  194. else
  195. internalerror(2002070603);
  196. end;
  197. end;
  198. function getopstr(const o:toper) : string;
  199. var
  200. hs : string;
  201. begin
  202. case o.typ of
  203. top_reg:
  204. getopstr:=gas_regname(o.reg);
  205. top_const:
  206. getopstr:=tostr(longint(o.val));
  207. top_ref:
  208. if o.ref^.refaddr=addr_full then
  209. begin
  210. hs:=o.ref^.symbol.name;
  211. if target_asm.dollarsign<>'$' then
  212. hs:=ReplaceForbiddenAsmSymbolChars(hs);
  213. if o.ref^.offset>0 then
  214. hs:=hs+'+'+tostr(o.ref^.offset)
  215. else
  216. if o.ref^.offset<0 then
  217. hs:=hs+tostr(o.ref^.offset);
  218. getopstr:=hs;
  219. end
  220. else
  221. getopstr:=getreferencestring(o.ref^);
  222. else
  223. internalerror(2002070604);
  224. end;
  225. end;
  226. function branchmode(o: tasmop): topstr;
  227. var tempstr: topstr;
  228. begin
  229. tempstr := '';
  230. case o of
  231. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  232. A_BCLR,A_BCLRL: tempstr := 'lr';
  233. end;
  234. case o of
  235. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  236. end;
  237. case o of
  238. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  239. end;
  240. branchmode := tempstr;
  241. end;
  242. function cond2str(op: tasmop; c: tasmcond): string;
  243. { note: no checking is performed whether the given combination of }
  244. { conditions is valid }
  245. var
  246. tempstr: string;
  247. begin
  248. tempstr:=#9;
  249. case c.simple of
  250. false:
  251. begin
  252. cond2str := tempstr+gas_op2str[op];
  253. case c.dirhint of
  254. DH_None:;
  255. DH_Minus:
  256. cond2str:=cond2str+'-';
  257. DH_Plus:
  258. cond2str:=cond2str+'+';
  259. else
  260. internalerror(2003112901);
  261. end;
  262. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  263. end;
  264. true:
  265. if (op >= A_B) and (op <= A_BCLRL) then
  266. case c.cond of
  267. { unconditional branch }
  268. C_NONE:
  269. cond2str := tempstr+gas_op2str[op];
  270. { bdnzt etc }
  271. else
  272. begin
  273. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  274. branchmode(op);
  275. case c.dirhint of
  276. DH_None:
  277. tempstr:=tempstr+#9;
  278. DH_Minus:
  279. tempstr:=tempstr+('-'+#9);
  280. DH_Plus:
  281. tempstr:=tempstr+('+'+#9);
  282. else
  283. internalerror(2003112901);
  284. end;
  285. case c.cond of
  286. C_LT..C_NU:
  287. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  288. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  289. cond2str := tempstr+tostr(c.crbit);
  290. else
  291. cond2str := tempstr;
  292. end;
  293. end;
  294. end
  295. { we have a trap instruction }
  296. else
  297. begin
  298. internalerror(2002070601);
  299. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  300. { case tempstr := 'tw';}
  301. end;
  302. end;
  303. end;
  304. {****************************************************************************}
  305. { PowerPC Instruction Writer }
  306. {****************************************************************************}
  307. Procedure TPPCInstrWriter.WriteInstruction(hp : tai);
  308. var op: TAsmOp;
  309. s: string;
  310. i: byte;
  311. sep: string[3];
  312. begin
  313. op:=taicpu(hp).opcode;
  314. if is_calljmp(op) then
  315. begin
  316. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  317. case op of
  318. A_B,A_BA,A_BL,A_BLA:
  319. s:=#9+gas_op2str[op]+#9;
  320. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  321. s:=#9+gas_op2str[op]
  322. else
  323. begin
  324. s:=cond2str(op,taicpu(hp).condition);
  325. if (s[length(s)] <> #9) and
  326. (taicpu(hp).ops>0) then
  327. s := s + ',';
  328. end;
  329. end;
  330. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  331. begin
  332. { first write the current contents of s, because the symbol }
  333. { may be 255 characters }
  334. owner.asmwrite(s);
  335. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  336. end;
  337. end
  338. else
  339. { process operands }
  340. begin
  341. s:=#9+gas_op2str[op];
  342. if taicpu(hp).ops<>0 then
  343. begin
  344. {
  345. if not is_calljmp(op) then
  346. sep:=','
  347. else
  348. }
  349. sep:=#9;
  350. for i:=0 to taicpu(hp).ops-1 do
  351. begin
  352. // debug code
  353. // writeln(s);
  354. // writeln(taicpu(hp).fileinfo.line);
  355. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  356. sep:=',';
  357. end;
  358. end;
  359. end;
  360. owner.AsmWriteLn(s);
  361. end;
  362. {****************************************************************************}
  363. { GNU PPC Assembler writer }
  364. {****************************************************************************}
  365. constructor TPPCGNUAssembler.create(smart: boolean);
  366. begin
  367. inherited create(smart);
  368. InstrWriter := TPPCInstrWriter.create(self);
  369. end;
  370. procedure TPPCGNUAssembler.WriteExtraHeader;
  371. var
  372. i : longint;
  373. begin
  374. for i:=0 to 31 do
  375. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  376. for i:=0 to 31 do
  377. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  378. end;
  379. {****************************************************************************}
  380. { GNU/Apple PPC Assembler writer }
  381. {****************************************************************************}
  382. constructor TPPCAppleGNUAssembler.create(smart: boolean);
  383. begin
  384. inherited create(smart);
  385. InstrWriter := TPPCInstrWriter.create(self);
  386. end;
  387. function TPPCAppleGNUAssembler.MakeCmdLine: TCmdStr;
  388. begin
  389. result := inherited MakeCmdLine;
  390. {$ifdef cpu64bitaddr}
  391. Replace(result,'$ARCH','ppc64')
  392. {$else cpu64bitaddr}
  393. case current_settings.cputype of
  394. cpu_PPC7400:
  395. Replace(result,'$ARCH','ppc7400');
  396. cpu_PPC970:
  397. Replace(result,'$ARCH','ppc970');
  398. else
  399. Replace(result,'$ARCH','ppc')
  400. end;
  401. {$endif cpu64bitaddr}
  402. end;
  403. {****************************************************************************}
  404. { AIX PPC Assembler writer }
  405. {****************************************************************************}
  406. constructor TPPCAIXAssembler.create(smart: boolean);
  407. begin
  408. inherited create(smart);
  409. InstrWriter := TPPCInstrWriter.create(self);
  410. end;
  411. procedure TPPCAIXAssembler.WriteExtraHeader;
  412. var
  413. i: longint;
  414. begin
  415. inherited WriteExtraHeader;
  416. { map cr registers to plain numbers }
  417. for i:=0 to 7 do
  418. AsmWriteln(#9'.set'#9'cr'+tostr(i)+','+tostr(i));
  419. { make sure we always have a code and toc section, the linker expects
  420. that }
  421. AsmWriteln(#9'.csect .text[PR]');
  422. { set _text_s, to be used by footer below }
  423. AsmWriteln(#9'_text_s:');
  424. AsmWriteln(#9'.toc');
  425. end;
  426. procedure TPPCAIXAssembler.WriteExtraFooter;
  427. begin
  428. inherited WriteExtraFooter;
  429. { link between data and text section }
  430. AsmWriteln(#9'.csect .data[RW],4');
  431. {$ifdef cpu64bitaddr}
  432. AsmWriteln('text_pos:'#9'.llong _text_s')
  433. {$else cpu64bitaddr}
  434. AsmWriteln('text_pos:'#9'.long _text_s')
  435. {$endif cpu64bitaddr}
  436. end;
  437. procedure TPPCAIXAssembler.WriteDirectiveName(dir: TAsmDirective);
  438. begin
  439. case dir of
  440. asd_reference:
  441. AsmWrite('.ref ');
  442. asd_weak_reference,
  443. asd_weak_definition:
  444. AsmWrite('.weak ');
  445. else
  446. inherited WriteDirectiveName(dir);
  447. end;
  448. end;
  449. function TPPCAIXAssembler.sectionname(atype: TAsmSectiontype; const aname: string; aorder: TAsmSectionOrder): string;
  450. begin
  451. case atype of
  452. sec_code:
  453. result:='.csect .text[PR]';
  454. sec_data,
  455. sec_rodata,
  456. { don't use .bss[BS], causes relocation problems }
  457. sec_bss:
  458. result:='.csect .data[RW]';
  459. sec_rodata_norel:
  460. result:='.csect .text[RO]';
  461. sec_fpc:
  462. result:='.csect .fpc[RO]';
  463. sec_toc:
  464. result:='.toc';
  465. { automatically placed in the right section }
  466. sec_stab,
  467. sec_stabstr:
  468. result:='';
  469. else
  470. internalerror(2011122601);
  471. end;
  472. end;
  473. {*****************************************************************************
  474. Initialize
  475. *****************************************************************************}
  476. const
  477. as_ppc_gas_info : tasminfo =
  478. (
  479. id : as_gas;
  480. idtxt : 'AS';
  481. asmbin : 'as';
  482. {$ifdef cpu64bitaddr}
  483. asmcmd : '-a64 -o $OBJ $EXTRAOPT $ASM';
  484. {$else cpu64bitaddr}
  485. asmcmd: '-o $OBJ $EXTRAOPT $ASM';
  486. {$endif cpu64bitaddr}
  487. supported_targets : [system_powerpc_linux,system_powerpc_netbsd,system_powerpc_openbsd,system_powerpc_MorphOS,system_powerpc_Amiga,system_powerpc64_linux,system_powerpc_embedded,system_powerpc64_embedded];
  488. flags : [af_needar,af_smartlink_sections];
  489. labelprefix : '.L';
  490. comment : '# ';
  491. dollarsign: '$';
  492. );
  493. as_ppc_gas_darwin_powerpc_info : tasminfo =
  494. (
  495. id : as_darwin;
  496. idtxt : 'AS-Darwin';
  497. asmbin : 'as';
  498. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch $ARCH';
  499. supported_targets : [system_powerpc_darwin,system_powerpc64_darwin];
  500. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  501. labelprefix : 'L';
  502. comment : '# ';
  503. dollarsign : '$';
  504. );
  505. as_ppc_aix_powerpc_info : tasminfo =
  506. (
  507. id : as_powerpc_xcoff;
  508. idtxt : 'AS-AIX';
  509. asmbin : 'as';
  510. { -u: allow using symbols before they are defined (when using native
  511. AIX assembler, ignore by GNU assembler)
  512. -mpwr5: we actually support Power3 and higher, but the AIX assembler
  513. has no parameter to select that one (only -mpwr3 and -mpwr5) }
  514. {$ifdef cpu64bitaddr}
  515. asmcmd : '-a64 -u -o $OBJ $EXTRAOPT $ASM -mpwr5';
  516. {$else cpu64bitaddr}
  517. asmcmd : '-u -o $OBJ $EXTRAOPT $ASM -mpwr5';
  518. {$endif cpu64bitaddr}
  519. supported_targets : [system_powerpc_aix,system_powerpc64_aix];
  520. flags : [af_needar,af_smartlink_sections,af_stabs_use_function_absolute_addresses];
  521. labelprefix : 'L';
  522. comment : '# ';
  523. dollarsign : '.'
  524. );
  525. as_ppc_gas_aix_powerpc_info : tasminfo =
  526. (
  527. id : as_gas_powerpc_xcoff;
  528. idtxt : 'GAS';
  529. asmbin : 'gas';
  530. { -u: allow using symbols before they are defined (when using native
  531. AIX assembler, ignore by GNU assembler)
  532. -mpwr5: we actually support Power3 and higher, but the AIX assembler
  533. has no parameter to select that one (only -mpwr3 and -mpwr5) }
  534. {$ifdef cpu64bitaddr}
  535. asmcmd : '-a64 -u -o $OBJ $EXTRAOPT $ASM -mpwr5';
  536. {$else cpu64bitaddr}
  537. asmcmd : '-a32 -u -o $OBJ $EXTRAOPT $ASM -mpwr5';
  538. {$endif cpu64bitaddr}
  539. supported_targets : [system_powerpc_aix,system_powerpc64_aix];
  540. flags : [af_needar,af_smartlink_sections,af_stabs_use_function_absolute_addresses];
  541. labelprefix : 'L';
  542. comment : '# ';
  543. dollarsign : '.'
  544. );
  545. begin
  546. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  547. RegisterAssembler(as_ppc_gas_darwin_powerpc_info,TPPCAppleGNUAssembler);
  548. RegisterAssembler(as_ppc_aix_powerpc_info,TPPCAIXAssembler);
  549. RegisterAssembler(as_ppc_gas_aix_powerpc_info,TPPCAIXAssembler);
  550. end.