agppcgas.pas 20 KB

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