agppcgas.pas 11 KB

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