agppcgas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements an asm for the PowerPC
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. { This unit implements the GNU Assembler writer for the PowerPC
  19. }
  20. unit agppcgas;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. aasmtai,
  25. aggas,
  26. cpubase;
  27. type
  28. PPPCGNUAssembler=^TPPCGNUAssembler;
  29. TPPCGNUAssembler=class(TGNUassembler)
  30. procedure WriteExtraHeader;override;
  31. procedure WriteInstruction(hp : tai);override;
  32. end;
  33. implementation
  34. uses
  35. cutils,globals,verbose,
  36. cgbase,cgutils,systems,
  37. assemble,
  38. itcpugas,
  39. aasmcpu;
  40. procedure TPPCGNUAssembler.WriteExtraHeader;
  41. var
  42. i : longint;
  43. begin
  44. if (target_info.system <> system_powerpc_darwin) then
  45. begin
  46. for i:=0 to 31 do
  47. AsmWriteln(#9'.set'#9'r'+tostr(i)+','+tostr(i));
  48. for i:=0 to 31 do
  49. AsmWriteln(#9'.set'#9'f'+tostr(i)+','+tostr(i));
  50. end;
  51. end;
  52. const
  53. as_ppc_gas_info : tasminfo =
  54. (
  55. id : as_gas;
  56. idtxt : 'AS';
  57. asmbin : 'as';
  58. asmcmd : '-o $OBJ $ASM';
  59. supported_target : system_any;
  60. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  61. labelprefix : '.L';
  62. comment : '# ';
  63. );
  64. as_ppc_gas_darwin_info : tasminfo =
  65. (
  66. id : as_darwin;
  67. idtxt : 'AS-Darwin';
  68. asmbin : 'as';
  69. asmcmd : '-o $OBJ $ASM';
  70. supported_target : system_any;
  71. flags : [af_allowdirect,af_needar,af_smartlink_sections];
  72. labelprefix : 'L';
  73. comment : '# ';
  74. );
  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 assigned(symbol) then
  123. begin
  124. if target_info.system <> system_powerpc_darwin then
  125. s:=s+'+0'
  126. end
  127. else
  128. s:=s+'0';
  129. end;
  130. s:=s+'('+gas_regname(base)+')';
  131. end
  132. else if (index<>NR_NO) and (base<>NR_NO) then
  133. begin
  134. if (offset=0) then
  135. s:=s+gas_regname(base)+','+gas_regname(index)
  136. else
  137. internalerror(19992);
  138. end;
  139. end;
  140. getreferencestring:=s;
  141. end;
  142. function getopstr_jmp(const o:toper) : string;
  143. var
  144. hs : string;
  145. begin
  146. case o.typ of
  147. top_reg :
  148. getopstr_jmp:=gas_regname(o.reg);
  149. { no top_ref jumping for powerpc }
  150. top_const :
  151. getopstr_jmp:=tostr(o.val);
  152. top_ref :
  153. begin
  154. if o.ref^.refaddr<>addr_full then
  155. internalerror(200402262);
  156. hs:=o.ref^.symbol.name;
  157. if o.ref^.offset>0 then
  158. hs:=hs+'+'+tostr(o.ref^.offset)
  159. else
  160. if o.ref^.offset<0 then
  161. hs:=hs+tostr(o.ref^.offset);
  162. getopstr_jmp:=hs;
  163. end;
  164. top_none:
  165. getopstr_jmp:='';
  166. else
  167. internalerror(2002070603);
  168. end;
  169. end;
  170. function getopstr(const o:toper) : string;
  171. var
  172. hs : string;
  173. begin
  174. case o.typ of
  175. top_reg:
  176. getopstr:=gas_regname(o.reg);
  177. top_const:
  178. getopstr:=tostr(longint(o.val));
  179. top_ref:
  180. if o.ref^.refaddr=addr_full then
  181. begin
  182. hs:=o.ref^.symbol.name;
  183. if o.ref^.offset>0 then
  184. hs:=hs+'+'+tostr(o.ref^.offset)
  185. else
  186. if o.ref^.offset<0 then
  187. hs:=hs+tostr(o.ref^.offset);
  188. getopstr:=hs;
  189. end
  190. else
  191. getopstr:=getreferencestring(o.ref^);
  192. else
  193. internalerror(2002070604);
  194. end;
  195. end;
  196. function branchmode(o: tasmop): string[4];
  197. var tempstr: string[4];
  198. begin
  199. tempstr := '';
  200. case o of
  201. A_BCCTR,A_BCCTRL: tempstr := 'ctr';
  202. A_BCLR,A_BCLRL: tempstr := 'lr';
  203. end;
  204. case o of
  205. A_BL,A_BLA,A_BCL,A_BCLA,A_BCCTRL,A_BCLRL: tempstr := tempstr+'l';
  206. end;
  207. case o of
  208. A_BA,A_BLA,A_BCA,A_BCLA: tempstr:=tempstr+'a';
  209. end;
  210. branchmode := tempstr;
  211. end;
  212. function cond2str(op: tasmop; c: tasmcond): string;
  213. { note: no checking is performed whether the given combination of }
  214. { conditions is valid }
  215. var
  216. tempstr: string;
  217. begin
  218. tempstr:=#9;
  219. case c.simple of
  220. false:
  221. begin
  222. cond2str := tempstr+gas_op2str[op];
  223. case c.dirhint of
  224. DH_None:;
  225. DH_Minus:
  226. cond2str:=cond2str+'-';
  227. DH_Plus:
  228. cond2str:=cond2str+'+';
  229. else
  230. internalerror(2003112901);
  231. end;
  232. cond2str:=cond2str+#9+tostr(c.bo)+','+tostr(c.bi);
  233. end;
  234. true:
  235. if (op >= A_B) and (op <= A_BCLRL) then
  236. case c.cond of
  237. { unconditional branch }
  238. C_NONE:
  239. cond2str := tempstr+gas_op2str[op];
  240. { bdnzt etc }
  241. else
  242. begin
  243. tempstr := tempstr+'b'+asmcondflag2str[c.cond]+
  244. branchmode(op);
  245. case c.dirhint of
  246. DH_None:
  247. tempstr:=tempstr+#9;
  248. DH_Minus:
  249. tempstr:=tempstr+('-'+#9);
  250. DH_Plus:
  251. tempstr:=tempstr+('+'+#9);
  252. else
  253. internalerror(2003112901);
  254. end;
  255. case c.cond of
  256. C_LT..C_NU:
  257. cond2str := tempstr+gas_regname(newreg(R_SPECIALREGISTER,c.cr,R_SUBWHOLE));
  258. C_T,C_F,C_DNZT,C_DNZF,C_DZT,C_DZF:
  259. cond2str := tempstr+tostr(c.crbit);
  260. else
  261. cond2str := tempstr;
  262. end;
  263. end;
  264. end
  265. { we have a trap instruction }
  266. else
  267. begin
  268. internalerror(2002070601);
  269. { not yet implemented !!!!!!!!!!!!!!!!!!!!! }
  270. { case tempstr := 'tw';}
  271. end;
  272. end;
  273. end;
  274. Procedure TPPCGNUAssembler.WriteInstruction(hp : tai);
  275. var op: TAsmOp;
  276. s: string;
  277. i: byte;
  278. sep: string[3];
  279. begin
  280. op:=taicpu(hp).opcode;
  281. if is_calljmp(op) then
  282. begin
  283. { direct BO/BI in op[0] and op[1] not supported, put them in condition! }
  284. case op of
  285. A_B,A_BA,A_BL,A_BLA:
  286. s:=#9+gas_op2str[op]+#9;
  287. A_BCTR,A_BCTRL,A_BLR,A_BLRL:
  288. s:=#9+gas_op2str[op]
  289. else
  290. begin
  291. s:=cond2str(op,taicpu(hp).condition);
  292. if (s[length(s)] <> #9) and
  293. (taicpu(hp).ops>0) then
  294. s := s + ',';
  295. end;
  296. end;
  297. if (taicpu(hp).ops>0) and (taicpu(hp).oper[0]^.typ<>top_none) then
  298. begin
  299. { first write the current contents of s, because the symbol }
  300. { may be 255 characters }
  301. asmwrite(s);
  302. s:=getopstr_jmp(taicpu(hp).oper[0]^);
  303. end;
  304. end
  305. else
  306. { process operands }
  307. begin
  308. s:=#9+gas_op2str[op];
  309. if taicpu(hp).ops<>0 then
  310. begin
  311. {
  312. if not is_calljmp(op) then
  313. sep:=','
  314. else
  315. }
  316. sep:=#9;
  317. for i:=0 to taicpu(hp).ops-1 do
  318. begin
  319. // debug code
  320. // writeln(s);
  321. // writeln(taicpu(hp).fileinfo.line);
  322. s:=s+sep+getopstr(taicpu(hp).oper[i]^);
  323. sep:=',';
  324. end;
  325. end;
  326. end;
  327. AsmWriteLn(s);
  328. end;
  329. begin
  330. RegisterAssembler(as_ppc_gas_info,TPPCGNUAssembler);
  331. RegisterAssembler(as_ppc_gas_darwin_info,TPPCGNUAssembler);
  332. end.
  333. {
  334. $Log$
  335. Revision 1.46 2004-12-28 02:25:44 olle
  336. * fixed compilation for PowerPC
  337. Revision 1.45 2004/10/31 21:45:03 peter
  338. * generic tlocation
  339. * move tlocation to cgutils
  340. Revision 1.44 2004/06/20 08:55:31 florian
  341. * logs truncated
  342. Revision 1.43 2004/06/17 16:55:46 peter
  343. * powerpc compiles again
  344. Revision 1.42 2004/03/02 17:32:12 florian
  345. * make cycle fixed
  346. + pic support for darwin
  347. + support of importing vars from shared libs on darwin implemented
  348. Revision 1.41 2004/02/27 10:21:05 florian
  349. * top_symbol killed
  350. + refaddr to treference added
  351. + refsymbol to treference added
  352. * top_local stuff moved to an extra record to save memory
  353. + aint introduced
  354. * tppufile.get/putint64/aint implemented
  355. Revision 1.40 2004/01/04 21:17:51 jonas
  356. + added log message for last commit
  357. Revision 1.39 2004/01/04 21:12:47 jonas
  358. + as_darwin assembler type (labels start with L)
  359. * never generate register->number mappings for Darwin
  360. * always use real register names for Darwin
  361. }