agppcgas.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asm for the PowerPC
  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. { This unit implements the GNU Assembler writer for the PowerPC
  18. }
  19. unit agppcgas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. aasmbase,
  24. aasmtai,aasmdata,
  25. aggas,
  26. cpubase,
  27. globtype;
  28. type
  29. TPPCGNUAssembler=class(TGNUassembler)
  30. constructor create(smart: boolean); override;
  31. procedure WriteExtraHeader;override;
  32. end;
  33. TPPCAppleGNUAssembler=class(TAppleGNUassembler)
  34. constructor create(smart: boolean); override;
  35. end;
  36. TPPCInstrWriter=class(TCPUInstrWriter)
  37. procedure WriteInstruction(hp : tai);override;
  38. end;
  39. implementation
  40. uses
  41. cutils,globals,verbose,
  42. cgbase,cgutils,systems,
  43. assemble,
  44. itcpugas,
  45. aasmcpu;
  46. {****************************************************************************}
  47. { GNU PPC Assembler writer }
  48. {****************************************************************************}
  49. constructor TPPCGNUAssembler.create(smart: boolean);
  50. begin
  51. inherited create(smart);
  52. InstrWriter := TPPCInstrWriter.create(self);
  53. end;
  54. procedure TPPCGNUAssembler.WriteExtraHeader;
  55. var
  56. i : longint;
  57. begin
  58. for i:=0 to 31 do
  59. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  60. for i:=0 to 31 do
  61. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  62. end;
  63. {****************************************************************************}
  64. { GNU/Apple PPC Assembler writer }
  65. {****************************************************************************}
  66. constructor TPPCAppleGNUAssembler.create(smart: boolean);
  67. begin
  68. inherited create(smart);
  69. InstrWriter := TPPCInstrWriter.create(self);
  70. end;
  71. {****************************************************************************}
  72. { Helper routines for Instruction Writer }
  73. {****************************************************************************}
  74. const
  75. refaddr2str: array[trefaddr] of string[3] = ('','','@ha','@l','');
  76. refaddr2str_darwin: array[trefaddr] of string[4] = ('','','ha16','lo16','');
  77. function getreferencestring(var ref : treference) : string;
  78. var
  79. s : string;
  80. begin
  81. with ref do
  82. begin
  83. if ((offset < -32768) or (offset > 32767)) and
  84. (refaddr = addr_no) then
  85. internalerror(19991);
  86. if (refaddr = addr_no) then
  87. s := ''
  88. else
  89. begin
  90. if target_info.system = system_powerpc_darwin then
  91. s := refaddr2str_darwin[refaddr]
  92. else
  93. s :='';
  94. s := s+'(';
  95. if assigned(symbol) then
  96. begin
  97. s:=s+symbol.name;
  98. if assigned(relsymbol) then
  99. s:=s+'-'+relsymbol.name;
  100. end;
  101. end;
  102. if offset<0 then
  103. s:=s+tostr(offset)
  104. else
  105. if (offset>0) then
  106. begin
  107. if assigned(symbol) then
  108. s:=s+'+'+tostr(offset)
  109. else
  110. s:=s+tostr(offset);
  111. end;
  112. if (refaddr in [addr_lo,addr_hi]) then
  113. begin
  114. s := s+')';
  115. if (target_info.system <> system_powerpc_darwin) then
  116. s := s+refaddr2str[refaddr];
  117. end;
  118. if (index=NR_NO) and (base<>NR_NO) then
  119. begin
  120. if offset=0 then
  121. begin
  122. if not (assigned(symbol)) then
  123. s:=s+'0';
  124. end;
  125. s:=s+'('+gas_regname(base)+')';
  126. end
  127. else if (index<>NR_NO) and (base<>NR_NO) then
  128. begin
  129. if (offset=0) then
  130. s:=s+gas_regname(base)+','+gas_regname(index)
  131. else
  132. internalerror(19992);
  133. end;
  134. end;
  135. getreferencestring:=s;
  136. end;
  137. function getopstr_jmp(const o:toper) : string;
  138. var
  139. hs : string;
  140. begin
  141. case o.typ of
  142. top_reg :
  143. getopstr_jmp:=gas_regname(o.reg);
  144. { no top_ref jumping for powerpc }
  145. top_const :
  146. getopstr_jmp:=tostr(o.val);
  147. top_ref :
  148. begin
  149. if o.ref^.refaddr<>addr_full then
  150. internalerror(200402262);
  151. hs:=o.ref^.symbol.name;
  152. if o.ref^.offset>0 then
  153. hs:=hs+'+'+tostr(o.ref^.offset)
  154. else
  155. if o.ref^.offset<0 then
  156. hs:=hs+tostr(o.ref^.offset);
  157. getopstr_jmp:=hs;
  158. end;
  159. top_none:
  160. getopstr_jmp:='';
  161. else
  162. internalerror(2002070603);
  163. end;
  164. end;
  165. function getopstr(const o:toper) : string;
  166. var
  167. hs : string;
  168. begin
  169. case o.typ of
  170. top_reg:
  171. getopstr:=gas_regname(o.reg);
  172. top_const:
  173. getopstr:=tostr(longint(o.val));
  174. top_ref:
  175. if o.ref^.refaddr=addr_full then
  176. begin
  177. hs:=o.ref^.symbol.name;
  178. if o.ref^.offset>0 then
  179. hs:=hs+'+'+tostr(o.ref^.offset)
  180. else
  181. if o.ref^.offset<0 then
  182. hs:=hs+tostr(o.ref^.offset);
  183. getopstr:=hs;
  184. end
  185. else
  186. getopstr:=getreferencestring(o.ref^);
  187. else
  188. internalerror(2002070604);
  189. end;
  190. end;
  191. function branchmode(o: tasmop): string[4];
  192. var tempstr: string[4];
  193. begin
  194. tempstr := '';
  195. case o of
  196. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  197. A_BCLR,A_BCLRL: tempstr := 'lr';
  198. end;
  199. case o of
  200. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  201. end;
  202. case o of
  203. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  204. end;
  205. branchmode := tempstr;
  206. end;
  207. function cond2str(op: tasmop; c: tasmcond): string;
  208. { note: no checking is performed whether the given combination of }
  209. { conditions is valid }
  210. var
  211. tempstr: string;
  212. begin
  213. tempstr:=#9;
  214. case c.simple of
  215. false:
  216. begin
  217. cond2str := tempstr+gas_op2str[op];
  218. case c.dirhint of
  219. DH_None:;
  220. DH_Minus:
  221. cond2str:=cond2str+'-';
  222. DH_Plus:
  223. cond2str:=cond2str+'+';
  224. else
  225. internalerror(2003112901);
  226. end;
  227. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  228. end;
  229. true:
  230. if (op >= A_B) and (op <= A_BCLRL) then
  231. case c.cond of
  232. { unconditional branch }
  233. C_NONE:
  234. cond2str := tempstr+gas_op2str[op];
  235. { bdnzt etc }
  236. else
  237. begin
  238. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  239. branchmode(op);
  240. case c.dirhint of
  241. DH_None:
  242. tempstr:=tempstr+#9;
  243. DH_Minus:
  244. tempstr:=tempstr+('-'+#9);
  245. DH_Plus:
  246. tempstr:=tempstr+('+'+#9);
  247. else
  248. internalerror(2003112901);
  249. end;
  250. case c.cond of
  251. C_LT..C_NU:
  252. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  253. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  254. cond2str := tempstr+tostr(c.crbit);
  255. else
  256. cond2str := tempstr;
  257. end;
  258. end;
  259. end
  260. { we have a trap instruction }
  261. else
  262. begin
  263. internalerror(2002070601);
  264. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  265. { case tempstr := 'tw';}
  266. end;
  267. end;
  268. end;
  269. {****************************************************************************}
  270. { PowerPC Instruction Writer }
  271. {****************************************************************************}
  272. Procedure TPPCInstrWriter.WriteInstruction(hp : tai);
  273. var op: TAsmOp;
  274. s: string;
  275. i: byte;
  276. sep: string[3];
  277. begin
  278. op:=taicpu(hp).opcode;
  279. if is_calljmp(op) then
  280. begin
  281. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  282. case op of
  283. A_B,A_BA,A_BL,A_BLA:
  284. s:=#9+gas_op2str[op]+#9;
  285. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  286. s:=#9+gas_op2str[op]
  287. else
  288. begin
  289. s:=cond2str(op,taicpu(hp).condition);
  290. if (s[length(s)] <> #9) and
  291. (taicpu(hp).ops>0) then
  292. s := s + ',';
  293. end;
  294. end;
  295. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  296. begin
  297. { first write the current contents of s, because the symbol }
  298. { may be 255 characters }
  299. owner.asmwrite(s);
  300. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  301. end;
  302. end
  303. else
  304. { process operands }
  305. begin
  306. s:=#9+gas_op2str[op];
  307. if taicpu(hp).ops<>0 then
  308. begin
  309. {
  310. if not is_calljmp(op) then
  311. sep:=','
  312. else
  313. }
  314. sep:=#9;
  315. for i:=0 to taicpu(hp).ops-1 do
  316. begin
  317. // debug code
  318. // writeln(s);
  319. // writeln(taicpu(hp).fileinfo.line);
  320. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  321. sep:=',';
  322. end;
  323. end;
  324. end;
  325. owner.AsmWriteLn(s);
  326. end;
  327. {*****************************************************************************
  328. Initialize
  329. *****************************************************************************}
  330. const
  331. as_ppc_gas_info : tasminfo =
  332. (
  333. id : as_gas;
  334. idtxt : 'AS';
  335. asmbin : 'as';
  336. asmcmd : '-o $OBJ $ASM';
  337. supported_target : system_any;
  338. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  339. labelprefix : '.L';
  340. comment : '# ';
  341. );
  342. as_ppc_gas_darwin_powerpc_info : tasminfo =
  343. (
  344. id : as_darwin;
  345. idtxt : 'AS-Darwin';
  346. asmbin : 'as';
  347. asmcmd : '-o $OBJ $ASM -arch ppc';
  348. supported_target : system_any;
  349. flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
  350. labelprefix : 'L';
  351. comment : '# ';
  352. );
  353. begin
  354. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  355. RegisterAssembler(as_ppc_gas_darwin_powerpc_info,TPPCAppleGNUAssembler);
  356. end.