agppcgas.pas 11 KB

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