agppcgas.pas 11 KB

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