agppcgas.pas 9.3 KB

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