agppcgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_info : tasminfo =
  69. (
  70. id : as_darwin;
  71. idtxt : 'AS-Darwin';
  72. asmbin : 'as';
  73. asmcmd : '-o $OBJ $ASM';
  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. atype := sec_code;
  87. sec_debug_frame:
  88. begin
  89. result := '.section __DWARFA,__debug_frame,coalesced,no_toc+strip_static_syms'#10'EH_frame'+tostr(debugframecount)+':';
  90. inc(debugframecount);
  91. exit;
  92. end;
  93. end;
  94. result := inherited sectionname(atype,aname);
  95. end;
  96. function getreferencestring(var ref : treference) : string;
  97. var
  98. s : string;
  99. begin
  100. with ref do
  101. begin
  102. if ((offset < -32768) or (offset > 32767)) and
  103. (refaddr = addr_no) then
  104. internalerror(19991);
  105. if (refaddr = addr_no) then
  106. s := ''
  107. else
  108. begin
  109. if target_info.system = system_powerpc_darwin then
  110. s := refaddr2str_darwin[refaddr]
  111. else
  112. s :='';
  113. s := s+'(';
  114. if assigned(symbol) then
  115. begin
  116. s:=s+symbol.name;
  117. if assigned(relsymbol) then
  118. s:=s+'-'+relsymbol.name;
  119. end;
  120. end;
  121. if offset<0 then
  122. s:=s+tostr(offset)
  123. else
  124. if (offset>0) then
  125. begin
  126. if assigned(symbol) then
  127. s:=s+'+'+tostr(offset)
  128. else
  129. s:=s+tostr(offset);
  130. end;
  131. if (refaddr in [addr_lo,addr_hi]) then
  132. begin
  133. s := s+')';
  134. if (target_info.system <> system_powerpc_darwin) then
  135. s := s+refaddr2str[refaddr];
  136. end;
  137. if (index=NR_NO) and (base<>NR_NO) then
  138. begin
  139. if offset=0 then
  140. begin
  141. if not (assigned(symbol)) then
  142. s:=s+'0';
  143. end;
  144. s:=s+'('+gas_regname(base)+')';
  145. end
  146. else if (index<>NR_NO) and (base<>NR_NO) then
  147. begin
  148. if (offset=0) then
  149. s:=s+gas_regname(base)+','+gas_regname(index)
  150. else
  151. internalerror(19992);
  152. end;
  153. end;
  154. getreferencestring:=s;
  155. end;
  156. function getopstr_jmp(const o:toper) : string;
  157. var
  158. hs : string;
  159. begin
  160. case o.typ of
  161. top_reg :
  162. getopstr_jmp:=gas_regname(o.reg);
  163. { no top_ref jumping for powerpc }
  164. top_const :
  165. getopstr_jmp:=tostr(o.val);
  166. top_ref :
  167. begin
  168. if o.ref^.refaddr<>addr_full then
  169. internalerror(200402262);
  170. hs:=o.ref^.symbol.name;
  171. if o.ref^.offset>0 then
  172. hs:=hs+'+'+tostr(o.ref^.offset)
  173. else
  174. if o.ref^.offset<0 then
  175. hs:=hs+tostr(o.ref^.offset);
  176. getopstr_jmp:=hs;
  177. end;
  178. top_none:
  179. getopstr_jmp:='';
  180. else
  181. internalerror(2002070603);
  182. end;
  183. end;
  184. function getopstr(const o:toper) : string;
  185. var
  186. hs : string;
  187. begin
  188. case o.typ of
  189. top_reg:
  190. getopstr:=gas_regname(o.reg);
  191. top_const:
  192. getopstr:=tostr(longint(o.val));
  193. top_ref:
  194. if o.ref^.refaddr=addr_full then
  195. begin
  196. hs:=o.ref^.symbol.name;
  197. if o.ref^.offset>0 then
  198. hs:=hs+'+'+tostr(o.ref^.offset)
  199. else
  200. if o.ref^.offset<0 then
  201. hs:=hs+tostr(o.ref^.offset);
  202. getopstr:=hs;
  203. end
  204. else
  205. getopstr:=getreferencestring(o.ref^);
  206. else
  207. internalerror(2002070604);
  208. end;
  209. end;
  210. function branchmode(o: tasmop): string[4];
  211. var tempstr: string[4];
  212. begin
  213. tempstr := '';
  214. case o of
  215. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  216. A_BCLR,A_BCLRL: tempstr := 'lr';
  217. end;
  218. case o of
  219. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  220. end;
  221. case o of
  222. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  223. end;
  224. branchmode := tempstr;
  225. end;
  226. function cond2str(op: tasmop; c: tasmcond): string;
  227. { note: no checking is performed whether the given combination of }
  228. { conditions is valid }
  229. var
  230. tempstr: string;
  231. begin
  232. tempstr:=#9;
  233. case c.simple of
  234. false:
  235. begin
  236. cond2str := tempstr+gas_op2str[op];
  237. case c.dirhint of
  238. DH_None:;
  239. DH_Minus:
  240. cond2str:=cond2str+'-';
  241. DH_Plus:
  242. cond2str:=cond2str+'+';
  243. else
  244. internalerror(2003112901);
  245. end;
  246. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  247. end;
  248. true:
  249. if (op >= A_B) and (op <= A_BCLRL) then
  250. case c.cond of
  251. { unconditional branch }
  252. C_NONE:
  253. cond2str := tempstr+gas_op2str[op];
  254. { bdnzt etc }
  255. else
  256. begin
  257. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  258. branchmode(op);
  259. case c.dirhint of
  260. DH_None:
  261. tempstr:=tempstr+#9;
  262. DH_Minus:
  263. tempstr:=tempstr+('-'+#9);
  264. DH_Plus:
  265. tempstr:=tempstr+('+'+#9);
  266. else
  267. internalerror(2003112901);
  268. end;
  269. case c.cond of
  270. C_LT..C_NU:
  271. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  272. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  273. cond2str := tempstr+tostr(c.crbit);
  274. else
  275. cond2str := tempstr;
  276. end;
  277. end;
  278. end
  279. { we have a trap instruction }
  280. else
  281. begin
  282. internalerror(2002070601);
  283. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  284. { case tempstr := 'tw';}
  285. end;
  286. end;
  287. end;
  288. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  289. var op: TAsmOp;
  290. s: string;
  291. i: byte;
  292. sep: string[3];
  293. begin
  294. op:=taicpu(hp).opcode;
  295. if is_calljmp(op) then
  296. begin
  297. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  298. case op of
  299. A_B,A_BA,A_BL,A_BLA:
  300. s:=#9+gas_op2str[op]+#9;
  301. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  302. s:=#9+gas_op2str[op]
  303. else
  304. begin
  305. s:=cond2str(op,taicpu(hp).condition);
  306. if (s[length(s)] <> #9) and
  307. (taicpu(hp).ops>0) then
  308. s := s + ',';
  309. end;
  310. end;
  311. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  312. begin
  313. { first write the current contents of s, because the symbol }
  314. { may be 255 characters }
  315. asmwrite(s);
  316. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  317. end;
  318. end
  319. else
  320. { process operands }
  321. begin
  322. s:=#9+gas_op2str[op];
  323. if taicpu(hp).ops<>0 then
  324. begin
  325. {
  326. if not is_calljmp(op) then
  327. sep:=','
  328. else
  329. }
  330. sep:=#9;
  331. for i:=0 to taicpu(hp).ops-1 do
  332. begin
  333. // debug code
  334. // writeln(s);
  335. // writeln(taicpu(hp).fileinfo.line);
  336. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  337. sep:=',';
  338. end;
  339. end;
  340. end;
  341. AsmWriteLn(s);
  342. end;
  343. begin
  344. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  345. RegisterAssembler(as_ppc_gas_darwin_info,TPPCGNUAssembler);
  346. end.