agppcgas.pas 8.3 KB

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