agcpugas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {
  2. Copyright (c) 2003,2014 by Florian Klaempfl and Jonas Maebe
  3. This unit implements an asm for AArch64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the GNU Assembler writer for AArch64
  18. }
  19. unit agcpugas;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,systems,
  24. aasmtai,
  25. assemble,aggas,
  26. cpubase,cpuinfo;
  27. type
  28. TAArch64InstrWriter=class(TCPUInstrWriter)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. TAArch64Assembler=class(TGNUassembler)
  32. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  33. end;
  34. TAArch64AppleAssembler=class(TAppleGNUassembler)
  35. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  36. end;
  37. const
  38. gas_shiftmode2str : array[tshiftmode] of string[4] = (
  39. '','lsl','lsr','asr','ror',
  40. 'uxtb','uxth','uxtw','uxtx',
  41. 'sxtb','sxth','sxtw','sxtx');
  42. const
  43. cputype_to_gas_march : array[tcputype] of string = (
  44. '', // cpu_none
  45. 'armv8'
  46. );
  47. implementation
  48. uses
  49. cutils,globals,verbose,
  50. aasmcpu,
  51. itcpugas,
  52. cgbase,cgutils;
  53. {****************************************************************************}
  54. { AArch64 Assembler writer }
  55. {****************************************************************************}
  56. constructor TAArch64Assembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  57. begin
  58. inherited;
  59. InstrWriter := TAArch64InstrWriter.create(self);
  60. end;
  61. {****************************************************************************}
  62. { Apple AArch64 Assembler writer }
  63. {****************************************************************************}
  64. constructor TAArch64AppleAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  65. begin
  66. inherited;
  67. InstrWriter := TAArch64InstrWriter.create(self);
  68. end;
  69. {****************************************************************************}
  70. { Helper routines for Instruction Writer }
  71. {****************************************************************************}
  72. function getreferencestring(asminfo: pasminfo; var ref : treference) : string;
  73. const
  74. darwin_addrpage2str: array[addr_page..addr_gotpageoffset] of string[11] =
  75. ('@PAGE','@PAGEOFF','@GOTPAGE','@GOTPAGEOFF');
  76. linux_addrpage2str: array[addr_page..addr_gotpageoffset] of string[10] =
  77. ('',':lo12:',':got:',':got_lo12:');
  78. begin
  79. if ref.base=NR_NO then
  80. begin
  81. case ref.refaddr of
  82. addr_gotpage,
  83. addr_page,
  84. addr_gotpageoffset,
  85. addr_pageoffset:
  86. begin
  87. if not assigned(ref.symbol) or
  88. (ref.base<>NR_NO) or
  89. (ref.index<>NR_NO) or
  90. (ref.shiftmode<>SM_None) or
  91. (ref.offset<>0) then
  92. internalerror(2014121501);
  93. if target_info.system in systems_darwin then
  94. result:=ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  95. else
  96. result:=linux_addrpage2str[ref.refaddr]+ref.symbol.name
  97. end;
  98. addr_pic,
  99. { for locals replaced by temp symbols on LLVM }
  100. addr_no:
  101. result:=ref.symbol.name;
  102. else
  103. internalerror(2015022302);
  104. end
  105. end
  106. else
  107. begin
  108. result:='['+gas_regname(ref.base);
  109. if ref.addressmode=AM_POSTINDEXED then
  110. result:=result+']';
  111. if ref.index<>NR_NO then
  112. begin
  113. if (ref.offset<>0) or
  114. assigned(ref.symbol) then
  115. internalerror(2014121504);
  116. result:=result+', '+gas_regname(ref.index);
  117. case ref.shiftmode of
  118. SM_None: ;
  119. SM_LSL,
  120. SM_UXTW, SM_UXTX, SM_SXTW, SM_SXTX:
  121. begin
  122. result:=result+', '+gas_shiftmode2str[ref.shiftmode];
  123. if (ref.shiftmode=SM_LSL) or
  124. (ref.shiftimm<>0) then
  125. result:=result+' #'+tostr(ref.shiftimm);
  126. end
  127. else
  128. internalerror(2014121505);
  129. end;
  130. end
  131. else
  132. begin
  133. if assigned(ref.symbol) then
  134. begin
  135. case ref.refaddr of
  136. addr_gotpageoffset,
  137. addr_pageoffset:
  138. begin
  139. if target_info.system in systems_darwin then
  140. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  141. else
  142. result:=result+', '+linux_addrpage2str[ref.refaddr]+ref.symbol.name
  143. end
  144. else
  145. { todo: not yet generated/don't know syntax }
  146. internalerror(2014121506);
  147. end;
  148. end
  149. else
  150. begin
  151. if ref.refaddr<>addr_no then
  152. internalerror(2014121506);
  153. if (ref.offset<>0) then
  154. result:=result+', #'+tostr(ref.offset);
  155. end;
  156. end;
  157. case ref.addressmode of
  158. AM_OFFSET:
  159. result:=result+']';
  160. AM_PREINDEXED:
  161. result:=result+']!';
  162. else
  163. ;
  164. end;
  165. end;
  166. end;
  167. function getopstr(asminfo: pasminfo; hp: taicpu; opnr: longint; const o: toper): string;
  168. begin
  169. case o.typ of
  170. top_reg:
  171. { we cannot yet represent "umov w0, v4.s[0]" or "ins v4.d[0], x1",
  172. so for now we use "s4" or "d4" instead -> translate here }
  173. if ((hp.opcode=A_INS) or
  174. (hp.opcode=A_UMOV)) and
  175. (getregtype(hp.oper[opnr]^.reg)=R_MMREGISTER) then
  176. begin
  177. case getsubreg(hp.oper[opnr]^.reg) of
  178. R_SUBMMS:
  179. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.S[0]';
  180. R_SUBMMD:
  181. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.D[0]';
  182. else
  183. internalerror(2014122907);
  184. end;
  185. end
  186. else
  187. getopstr:=gas_regname(o.reg);
  188. top_shifterop:
  189. begin
  190. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  191. if o.shifterop^.shiftimm<>0 then
  192. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  193. end;
  194. top_const:
  195. if o.val>=0 then
  196. getopstr:='#'+tostr(o.val)
  197. else
  198. getopstr:='#0x'+hexStr(o.val,16);
  199. top_conditioncode:
  200. getopstr:=cond2str[o.cc];
  201. top_ref:
  202. if is_calljmp(hp.opcode) then
  203. begin
  204. if o.ref^.refaddr<>addr_full then
  205. internalerror(2014122220);
  206. if not assigned(o.ref^.symbol) or
  207. assigned(o.ref^.relsymbol) or
  208. (o.ref^.base<>NR_NO) or
  209. (o.ref^.index<>NR_NO) or
  210. (o.ref^.offset<>0) then
  211. internalerror(2014122221);
  212. getopstr:=o.ref^.symbol.name;
  213. end
  214. else
  215. getopstr:=getreferencestring(asminfo,o.ref^);
  216. top_realconst:
  217. begin
  218. str(o.val_real,Result);
  219. Result:='#'+Result;
  220. end
  221. else
  222. internalerror(2014121507);
  223. end;
  224. end;
  225. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  226. var
  227. op: TAsmOp;
  228. s: string;
  229. i: byte;
  230. sep: string[3];
  231. begin
  232. op:=taicpu(hp).opcode;
  233. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  234. if taicpu(hp).condition<>C_NONE then
  235. s:=s+'.'+cond2str[taicpu(hp).condition];
  236. if taicpu(hp).ops<>0 then
  237. begin
  238. sep:=#9;
  239. for i:=0 to taicpu(hp).ops-1 do
  240. begin
  241. // debug code
  242. // writeln(s);
  243. // writeln(taicpu(hp).fileinfo.line);
  244. s:=s+sep+getopstr(owner.asminfo,taicpu(hp),i,taicpu(hp).oper[i]^);
  245. sep:=',';
  246. end;
  247. end;
  248. owner.writer.AsmWriteLn(s);
  249. end;
  250. const
  251. as_aarch64_gas_info : tasminfo =
  252. (
  253. id : as_gas;
  254. idtxt : 'AS';
  255. asmbin : 'as';
  256. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  257. supported_targets : [system_aarch64_linux,system_aarch64_android];
  258. flags : [af_needar,af_smartlink_sections];
  259. labelprefix : '.L';
  260. comment : '// ';
  261. dollarsign: '$';
  262. );
  263. as_aarch64_clang_darwin_info : tasminfo =
  264. (
  265. id : as_clang;
  266. idtxt : 'CLANG';
  267. asmbin : 'clang';
  268. asmcmd : '-c -o $OBJ $EXTRAOPT -arch arm64 $DARWINVERSION -x assembler $ASM';
  269. supported_targets : [system_aarch64_darwin];
  270. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  271. labelprefix : 'L';
  272. comment : '# ';
  273. dollarsign: '$';
  274. );
  275. begin
  276. RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
  277. RegisterAssembler(as_aarch64_clang_darwin_info,TAArch64AppleAssembler);
  278. end.