agppcgas.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. topstr = string[4];
  42. function getreferencestring(var ref : treference) : string;
  43. function getopstr_jmp(const o:toper) : string;
  44. function getopstr(const o:toper) : string;
  45. function branchmode(o: tasmop): topstr;
  46. function cond2str(op: tasmop; c: tasmcond): string;
  47. implementation
  48. uses
  49. cutils,globals,verbose,
  50. cgbase,systems,
  51. assemble,
  52. itcpugas,cpuinfo,
  53. aasmcpu;
  54. {$ifdef cpu64bitaddr}
  55. const
  56. refaddr2str: array[trefaddr] of string[9] = ('', '', '', '', '@l', '@h', '@higher', '@highest', '@ha', '@highera', '@highesta');
  57. verbose_refaddrs = [addr_low, addr_high, addr_higher, addr_highest, addr_higha, addr_highera, addr_highesta];
  58. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','','','lo16', 'hi16', '@err', '@err', 'ha16', '@err', '@err');
  59. {$else cpu64bitaddr}
  60. const
  61. refaddr2str: array[trefaddr] of string[3] = ('','','','','@l','@h','@ha');
  62. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','','','lo16','hi16','ha16');
  63. verbose_refaddrs = [addr_low,addr_high,addr_higha];
  64. {$endif cpu64bitaddr}
  65. function getreferencestring(var ref : treference) : string;
  66. var
  67. s : string;
  68. begin
  69. with ref do
  70. begin
  71. if ((offset < -32768) or (offset > 32767)) and
  72. (refaddr = addr_no) then
  73. internalerror(2006052501);
  74. if (refaddr = addr_no) then
  75. s := ''
  76. else
  77. begin
  78. if target_info.system in [system_powerpc_darwin,system_powerpc64_darwin] then
  79. s := refaddr2str_darwin[refaddr]
  80. else
  81. s :='';
  82. s := s+'(';
  83. if assigned(symbol) then
  84. begin
  85. s:=s+symbol.name;
  86. if assigned(relsymbol) then
  87. s:=s+'-'+relsymbol.name;
  88. end;
  89. end;
  90. if offset<0 then
  91. s:=s+tostr(offset)
  92. else
  93. if (offset>0) then
  94. begin
  95. if assigned(symbol) then
  96. s:=s+'+'+tostr(offset)
  97. else
  98. s:=s+tostr(offset);
  99. end;
  100. if (refaddr in verbose_refaddrs) then
  101. begin
  102. s := s+')';
  103. if not(target_info.system in [system_powerpc_darwin,system_powerpc64_darwin]) then
  104. s := s+refaddr2str[refaddr];
  105. end;
  106. {$ifdef cpu64bitaddr}
  107. if (refaddr = addr_pic) then
  108. if (target_info.system <> system_powerpc64_linux) then
  109. s := s + ')'
  110. else
  111. s := s + ')@got';
  112. {$endif cpu64bitaddr}
  113. if (index=NR_NO) then
  114. begin
  115. if offset=0 then
  116. begin
  117. if not (assigned(symbol)) then
  118. s:=s+'0';
  119. end;
  120. if (base<>NR_NO) then
  121. s:=s+'('+gas_regname(base)+')'
  122. else if not assigned(symbol) and
  123. not(refaddr in verbose_refaddrs) then
  124. s:=s+'(0)';
  125. end
  126. else if (index<>NR_NO) and (base<>NR_NO) then
  127. begin
  128. if (offset=0) then
  129. s:=s+gas_regname(base)+','+gas_regname(index)
  130. else
  131. internalerror(2006052502);
  132. end;
  133. end;
  134. getreferencestring:=s;
  135. end;
  136. function getopstr_jmp(const o:toper) : string;
  137. var
  138. hs : string;
  139. begin
  140. case o.typ of
  141. top_reg :
  142. getopstr_jmp:=gas_regname(o.reg);
  143. { no top_ref jumping for powerpc }
  144. top_const :
  145. getopstr_jmp:=tostr(o.val);
  146. top_ref :
  147. begin
  148. if o.ref^.refaddr<>addr_full then
  149. internalerror(200402262);
  150. hs:=o.ref^.symbol.name;
  151. if target_asm.dollarsign<>'$' then
  152. hs:=ReplaceForbiddenAsmSymbolChars(hs);
  153. if o.ref^.offset>0 then
  154. hs:=hs+'+'+tostr(o.ref^.offset)
  155. else
  156. if o.ref^.offset<0 then
  157. hs:=hs+tostr(o.ref^.offset);
  158. getopstr_jmp:=hs;
  159. end;
  160. top_none:
  161. getopstr_jmp:='';
  162. else
  163. internalerror(2002070603);
  164. end;
  165. end;
  166. function getopstr(const o:toper) : string;
  167. var
  168. hs : string;
  169. begin
  170. case o.typ of
  171. top_reg:
  172. getopstr:=gas_regname(o.reg);
  173. top_const:
  174. getopstr:=tostr(longint(o.val));
  175. top_ref:
  176. if o.ref^.refaddr=addr_full then
  177. begin
  178. hs:=o.ref^.symbol.name;
  179. if target_asm.dollarsign<>'$' then
  180. hs:=ReplaceForbiddenAsmSymbolChars(hs);
  181. if o.ref^.offset>0 then
  182. hs:=hs+'+'+tostr(o.ref^.offset)
  183. else
  184. if o.ref^.offset<0 then
  185. hs:=hs+tostr(o.ref^.offset);
  186. getopstr:=hs;
  187. end
  188. else
  189. getopstr:=getreferencestring(o.ref^);
  190. else
  191. internalerror(2002070604);
  192. end;
  193. end;
  194. function branchmode(o: tasmop): topstr;
  195. var tempstr: topstr;
  196. begin
  197. tempstr := '';
  198. case o of
  199. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  200. A_BCLR,A_BCLRL: tempstr := 'lr';
  201. end;
  202. case o of
  203. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  204. end;
  205. case o of
  206. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  207. end;
  208. branchmode := tempstr;
  209. end;
  210. function cond2str(op: tasmop; c: tasmcond): string;
  211. { note: no checking is performed whether the given combination of }
  212. { conditions is valid }
  213. var
  214. tempstr: string;
  215. begin
  216. tempstr:=#9;
  217. case c.simple of
  218. false:
  219. begin
  220. cond2str := tempstr+gas_op2str[op];
  221. case c.dirhint of
  222. DH_None:;
  223. DH_Minus:
  224. cond2str:=cond2str+'-';
  225. DH_Plus:
  226. cond2str:=cond2str+'+';
  227. else
  228. internalerror(2003112901);
  229. end;
  230. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  231. end;
  232. true:
  233. if (op >= A_B) and (op <= A_BCLRL) then
  234. case c.cond of
  235. { unconditional branch }
  236. C_NONE:
  237. cond2str := tempstr+gas_op2str[op];
  238. { bdnzt etc }
  239. else
  240. begin
  241. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  242. branchmode(op);
  243. case c.dirhint of
  244. DH_None:
  245. tempstr:=tempstr+#9;
  246. DH_Minus:
  247. tempstr:=tempstr+('-'+#9);
  248. DH_Plus:
  249. tempstr:=tempstr+('+'+#9);
  250. else
  251. internalerror(2003112901);
  252. end;
  253. case c.cond of
  254. C_LT..C_NU:
  255. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  256. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  257. cond2str := tempstr+tostr(c.crbit);
  258. else
  259. cond2str := tempstr;
  260. end;
  261. end;
  262. end
  263. { we have a trap instruction }
  264. else
  265. begin
  266. internalerror(2002070601);
  267. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  268. { case tempstr := 'tw';}
  269. end;
  270. end;
  271. end;
  272. {****************************************************************************}
  273. { PowerPC Instruction Writer }
  274. {****************************************************************************}
  275. Procedure TPPCInstrWriter.WriteInstruction(hp : tai);
  276. var op: TAsmOp;
  277. s: string;
  278. i: byte;
  279. sep: string[3];
  280. begin
  281. op:=taicpu(hp).opcode;
  282. if is_calljmp(op) then
  283. begin
  284. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  285. case op of
  286. A_B,A_BA,A_BL,A_BLA:
  287. s:=#9+gas_op2str[op]+#9;
  288. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  289. s:=#9+gas_op2str[op]
  290. else
  291. begin
  292. s:=cond2str(op,taicpu(hp).condition);
  293. if (s[length(s)] <> #9) and
  294. (taicpu(hp).ops>0) then
  295. s := s + ',';
  296. end;
  297. end;
  298. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  299. begin
  300. { first write the current contents of s, because the symbol }
  301. { may be 255 characters }
  302. owner.asmwrite(s);
  303. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  304. end;
  305. end
  306. else
  307. { process operands }
  308. begin
  309. s:=#9+gas_op2str[op];
  310. if taicpu(hp).ops<>0 then
  311. begin
  312. {
  313. if not is_calljmp(op) then
  314. sep:=','
  315. else
  316. }
  317. sep:=#9;
  318. for i:=0 to taicpu(hp).ops-1 do
  319. begin
  320. // debug code
  321. // writeln(s);
  322. // writeln(taicpu(hp).fileinfo.line);
  323. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  324. sep:=',';
  325. end;
  326. end;
  327. end;
  328. owner.AsmWriteLn(s);
  329. end;
  330. {****************************************************************************}
  331. { GNU PPC Assembler writer }
  332. {****************************************************************************}
  333. constructor TPPCGNUAssembler.create(smart: boolean);
  334. begin
  335. inherited create(smart);
  336. InstrWriter := TPPCInstrWriter.create(self);
  337. end;
  338. procedure TPPCGNUAssembler.WriteExtraHeader;
  339. var
  340. i : longint;
  341. begin
  342. for i:=0 to 31 do
  343. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  344. for i:=0 to 31 do
  345. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  346. end;
  347. {****************************************************************************}
  348. { GNU/Apple PPC Assembler writer }
  349. {****************************************************************************}
  350. constructor TPPCAppleGNUAssembler.create(smart: boolean);
  351. begin
  352. inherited create(smart);
  353. InstrWriter := TPPCInstrWriter.create(self);
  354. end;
  355. function TPPCAppleGNUAssembler.MakeCmdLine: TCmdStr;
  356. begin
  357. result := inherited MakeCmdLine;
  358. {$ifdef cpu64bitaddr}
  359. Replace(result,'$ARCH','ppc64')
  360. {$else cpu64bitaddr}
  361. case current_settings.cputype of
  362. cpu_PPC7400:
  363. Replace(result,'$ARCH','ppc7400');
  364. cpu_PPC970:
  365. Replace(result,'$ARCH','ppc970');
  366. else
  367. Replace(result,'$ARCH','ppc')
  368. end;
  369. {$endif cpu64bitaddr}
  370. end;
  371. {*****************************************************************************
  372. Initialize
  373. *****************************************************************************}
  374. const
  375. as_ppc_gas_info : tasminfo =
  376. (
  377. id : as_gas;
  378. idtxt : 'AS';
  379. asmbin : 'as';
  380. {$ifdef cpu64bitaddr}
  381. asmcmd : '-a64 -o $OBJ $ASM';
  382. {$else cpu64bitaddr}
  383. asmcmd: '-o $OBJ $ASM';
  384. {$endif cpu64bitaddr}
  385. 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];
  386. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  387. labelprefix : '.L';
  388. comment : '# ';
  389. dollarsign: '$';
  390. );
  391. as_ppc_gas_darwin_powerpc_info : tasminfo =
  392. (
  393. id : as_darwin;
  394. idtxt : 'AS-Darwin';
  395. asmbin : 'as';
  396. asmcmd : '-o $OBJ $ASM -arch $ARCH';
  397. supported_targets : [system_powerpc_darwin,system_powerpc64_darwin];
  398. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  399. labelprefix : 'L';
  400. comment : '# ';
  401. dollarsign : '$';
  402. );
  403. begin
  404. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  405. RegisterAssembler(as_ppc_gas_darwin_powerpc_info,TPPCAppleGNUAssembler);
  406. end.