agppcgas.pas 13 KB

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