agppcgas.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 o.ref^.offset>0 then
  152. hs:=hs+'+'+tostr(o.ref^.offset)
  153. else
  154. if o.ref^.offset<0 then
  155. hs:=hs+tostr(o.ref^.offset);
  156. getopstr_jmp:=hs;
  157. end;
  158. top_none:
  159. getopstr_jmp:='';
  160. else
  161. internalerror(2002070603);
  162. end;
  163. end;
  164. function getopstr(const o:toper) : string;
  165. var
  166. hs : string;
  167. begin
  168. case o.typ of
  169. top_reg:
  170. getopstr:=gas_regname(o.reg);
  171. top_const:
  172. getopstr:=tostr(longint(o.val));
  173. top_ref:
  174. if o.ref^.refaddr=addr_full then
  175. begin
  176. hs:=o.ref^.symbol.name;
  177. if o.ref^.offset>0 then
  178. hs:=hs+'+'+tostr(o.ref^.offset)
  179. else
  180. if o.ref^.offset<0 then
  181. hs:=hs+tostr(o.ref^.offset);
  182. getopstr:=hs;
  183. end
  184. else
  185. getopstr:=getreferencestring(o.ref^);
  186. else
  187. internalerror(2002070604);
  188. end;
  189. end;
  190. function branchmode(o: tasmop): topstr;
  191. var tempstr: topstr;
  192. begin
  193. tempstr := '';
  194. case o of
  195. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  196. A_BCLR,A_BCLRL: tempstr := 'lr';
  197. end;
  198. case o of
  199. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  200. end;
  201. case o of
  202. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  203. end;
  204. branchmode := tempstr;
  205. end;
  206. function cond2str(op: tasmop; c: tasmcond): string;
  207. { note: no checking is performed whether the given combination of }
  208. { conditions is valid }
  209. var
  210. tempstr: string;
  211. begin
  212. tempstr:=#9;
  213. case c.simple of
  214. false:
  215. begin
  216. cond2str := tempstr+gas_op2str[op];
  217. case c.dirhint of
  218. DH_None:;
  219. DH_Minus:
  220. cond2str:=cond2str+'-';
  221. DH_Plus:
  222. cond2str:=cond2str+'+';
  223. else
  224. internalerror(2003112901);
  225. end;
  226. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  227. end;
  228. true:
  229. if (op >= A_B) and (op <= A_BCLRL) then
  230. case c.cond of
  231. { unconditional branch }
  232. C_NONE:
  233. cond2str := tempstr+gas_op2str[op];
  234. { bdnzt etc }
  235. else
  236. begin
  237. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  238. branchmode(op);
  239. case c.dirhint of
  240. DH_None:
  241. tempstr:=tempstr+#9;
  242. DH_Minus:
  243. tempstr:=tempstr+('-'+#9);
  244. DH_Plus:
  245. tempstr:=tempstr+('+'+#9);
  246. else
  247. internalerror(2003112901);
  248. end;
  249. case c.cond of
  250. C_LT..C_NU:
  251. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  252. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  253. cond2str := tempstr+tostr(c.crbit);
  254. else
  255. cond2str := tempstr;
  256. end;
  257. end;
  258. end
  259. { we have a trap instruction }
  260. else
  261. begin
  262. internalerror(2002070601);
  263. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  264. { case tempstr := 'tw';}
  265. end;
  266. end;
  267. end;
  268. {****************************************************************************}
  269. { PowerPC Instruction Writer }
  270. {****************************************************************************}
  271. Procedure TPPCInstrWriter.WriteInstruction(hp : tai);
  272. var op: TAsmOp;
  273. s: string;
  274. i: byte;
  275. sep: string[3];
  276. begin
  277. op:=taicpu(hp).opcode;
  278. if is_calljmp(op) then
  279. begin
  280. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  281. case op of
  282. A_B,A_BA,A_BL,A_BLA:
  283. s:=#9+gas_op2str[op]+#9;
  284. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  285. s:=#9+gas_op2str[op]
  286. else
  287. begin
  288. s:=cond2str(op,taicpu(hp).condition);
  289. if (s[length(s)] <> #9) and
  290. (taicpu(hp).ops>0) then
  291. s := s + ',';
  292. end;
  293. end;
  294. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  295. begin
  296. { first write the current contents of s, because the symbol }
  297. { may be 255 characters }
  298. owner.asmwrite(s);
  299. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  300. end;
  301. end
  302. else
  303. { process operands }
  304. begin
  305. s:=#9+gas_op2str[op];
  306. if taicpu(hp).ops<>0 then
  307. begin
  308. {
  309. if not is_calljmp(op) then
  310. sep:=','
  311. else
  312. }
  313. sep:=#9;
  314. for i:=0 to taicpu(hp).ops-1 do
  315. begin
  316. // debug code
  317. // writeln(s);
  318. // writeln(taicpu(hp).fileinfo.line);
  319. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  320. sep:=',';
  321. end;
  322. end;
  323. end;
  324. owner.AsmWriteLn(s);
  325. end;
  326. {****************************************************************************}
  327. { GNU PPC Assembler writer }
  328. {****************************************************************************}
  329. constructor TPPCGNUAssembler.create(smart: boolean);
  330. begin
  331. inherited create(smart);
  332. InstrWriter := TPPCInstrWriter.create(self);
  333. end;
  334. procedure TPPCGNUAssembler.WriteExtraHeader;
  335. var
  336. i : longint;
  337. begin
  338. for i:=0 to 31 do
  339. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  340. for i:=0 to 31 do
  341. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  342. end;
  343. {****************************************************************************}
  344. { GNU/Apple PPC Assembler writer }
  345. {****************************************************************************}
  346. constructor TPPCAppleGNUAssembler.create(smart: boolean);
  347. begin
  348. inherited create(smart);
  349. InstrWriter := TPPCInstrWriter.create(self);
  350. end;
  351. function TPPCAppleGNUAssembler.MakeCmdLine: TCmdStr;
  352. begin
  353. result := inherited MakeCmdLine;
  354. {$ifdef cpu64bitaddr}
  355. Replace(result,'$ARCH','ppc64')
  356. {$else cpu64bitaddr}
  357. case current_settings.cputype of
  358. cpu_PPC7400:
  359. Replace(result,'$ARCH','ppc7400');
  360. cpu_PPC970:
  361. Replace(result,'$ARCH','ppc970');
  362. else
  363. Replace(result,'$ARCH','ppc')
  364. end;
  365. {$endif cpu64bitaddr}
  366. end;
  367. {*****************************************************************************
  368. Initialize
  369. *****************************************************************************}
  370. const
  371. as_ppc_gas_info : tasminfo =
  372. (
  373. id : as_gas;
  374. idtxt : 'AS';
  375. asmbin : 'as';
  376. {$ifdef cpu64bitaddr}
  377. asmcmd : '-a64 -o $OBJ $ASM';
  378. {$else cpu64bitaddr}
  379. asmcmd: '-o $OBJ $ASM';
  380. {$endif cpu64bitaddr}
  381. supported_target : system_any;
  382. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  383. labelprefix : '.L';
  384. comment : '# ';
  385. );
  386. as_ppc_gas_darwin_powerpc_info : tasminfo =
  387. (
  388. id : as_darwin;
  389. idtxt : 'AS-Darwin';
  390. asmbin : 'as';
  391. asmcmd : '-o $OBJ $ASM -arch $ARCH';
  392. supported_target : system_any;
  393. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  394. labelprefix : 'L';
  395. comment : '# ';
  396. );
  397. begin
  398. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  399. RegisterAssembler(as_ppc_gas_darwin_powerpc_info,TPPCAppleGNUAssembler);
  400. end.