agppcgas.pas 8.3 KB

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