agppcgas.pas 8.4 KB

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