agppcgas.pas 9.2 KB

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