agcpugas.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. result:=ref.symbol.name;
  100. else
  101. internalerror(2015022302);
  102. end
  103. end
  104. else
  105. begin
  106. result:='['+gas_regname(ref.base);
  107. if ref.addressmode=AM_POSTINDEXED then
  108. result:=result+']';
  109. if ref.index<>NR_NO then
  110. begin
  111. if (ref.offset<>0) or
  112. assigned(ref.symbol) then
  113. internalerror(2014121504);
  114. result:=result+', '+gas_regname(ref.index);
  115. case ref.shiftmode of
  116. SM_None: ;
  117. SM_LSL,
  118. SM_UXTW, SM_UXTX, SM_SXTW, SM_SXTX:
  119. begin
  120. result:=result+', '+gas_shiftmode2str[ref.shiftmode];
  121. if (ref.shiftmode=SM_LSL) or
  122. (ref.shiftimm<>0) then
  123. result:=result+' #'+tostr(ref.shiftimm);
  124. end
  125. else
  126. internalerror(2014121505);
  127. end;
  128. end
  129. else
  130. begin
  131. if assigned(ref.symbol) then
  132. begin
  133. case ref.refaddr of
  134. addr_gotpageoffset,
  135. addr_pageoffset:
  136. begin
  137. if target_info.system in systems_darwin then
  138. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  139. else
  140. result:=result+', '+linux_addrpage2str[ref.refaddr]+ref.symbol.name
  141. end
  142. else
  143. { todo: not yet generated/don't know syntax }
  144. internalerror(2014121506);
  145. end;
  146. end
  147. else
  148. begin
  149. if ref.refaddr<>addr_no then
  150. internalerror(2014121506);
  151. if (ref.offset<>0) then
  152. result:=result+', #'+tostr(ref.offset);
  153. end;
  154. end;
  155. case ref.addressmode of
  156. AM_OFFSET:
  157. result:=result+']';
  158. AM_PREINDEXED:
  159. result:=result+']!';
  160. else
  161. ;
  162. end;
  163. end;
  164. end;
  165. function getopstr(asminfo: pasminfo; hp: taicpu; opnr: longint; const o: toper): string;
  166. begin
  167. case o.typ of
  168. top_reg:
  169. { we cannot yet represent "umov w0, v4.s[0]" or "ins v4.d[0], x1",
  170. so for now we use "s4" or "d4" instead -> translate here }
  171. if ((hp.opcode=A_INS) or
  172. (hp.opcode=A_UMOV)) and
  173. (getregtype(hp.oper[opnr]^.reg)=R_MMREGISTER) then
  174. begin
  175. case getsubreg(hp.oper[opnr]^.reg) of
  176. R_SUBMMS:
  177. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.S[0]';
  178. R_SUBMMD:
  179. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.D[0]';
  180. else
  181. internalerror(2014122907);
  182. end;
  183. end
  184. else
  185. getopstr:=gas_regname(o.reg);
  186. top_shifterop:
  187. begin
  188. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  189. if o.shifterop^.shiftimm<>0 then
  190. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  191. end;
  192. top_const:
  193. if o.val>=0 then
  194. getopstr:='#'+tostr(o.val)
  195. else
  196. getopstr:='#0x'+hexStr(o.val,16);
  197. top_conditioncode:
  198. getopstr:=cond2str[o.cc];
  199. top_ref:
  200. if is_calljmp(hp.opcode) then
  201. begin
  202. if o.ref^.refaddr<>addr_full then
  203. internalerror(2014122220);
  204. if not assigned(o.ref^.symbol) or
  205. assigned(o.ref^.relsymbol) or
  206. (o.ref^.base<>NR_NO) or
  207. (o.ref^.index<>NR_NO) or
  208. (o.ref^.offset<>0) then
  209. internalerror(2014122221);
  210. getopstr:=o.ref^.symbol.name;
  211. end
  212. else
  213. getopstr:=getreferencestring(asminfo,o.ref^);
  214. top_realconst:
  215. begin
  216. str(o.val_real,Result);
  217. Result:='#'+Result;
  218. end
  219. else
  220. internalerror(2014121507);
  221. end;
  222. end;
  223. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  224. var
  225. op: TAsmOp;
  226. s: string;
  227. i: byte;
  228. sep: string[3];
  229. begin
  230. op:=taicpu(hp).opcode;
  231. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  232. if taicpu(hp).condition<>C_NONE then
  233. s:=s+'.'+cond2str[taicpu(hp).condition];
  234. if taicpu(hp).ops<>0 then
  235. begin
  236. sep:=#9;
  237. for i:=0 to taicpu(hp).ops-1 do
  238. begin
  239. // debug code
  240. // writeln(s);
  241. // writeln(taicpu(hp).fileinfo.line);
  242. s:=s+sep+getopstr(owner.asminfo,taicpu(hp),i,taicpu(hp).oper[i]^);
  243. sep:=',';
  244. end;
  245. end;
  246. owner.writer.AsmWriteLn(s);
  247. end;
  248. const
  249. as_aarch64_gas_info : tasminfo =
  250. (
  251. id : as_gas;
  252. idtxt : 'AS';
  253. asmbin : 'as';
  254. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  255. supported_targets : [system_aarch64_linux,system_aarch64_android];
  256. flags : [af_needar,af_smartlink_sections];
  257. labelprefix : '.L';
  258. comment : '// ';
  259. dollarsign: '$';
  260. );
  261. as_aarch64_clang_darwin_info : tasminfo =
  262. (
  263. id : as_clang;
  264. idtxt : 'CLANG';
  265. asmbin : 'clang';
  266. asmcmd : '-c -o $OBJ $EXTRAOPT -arch arm64 $DARWINVERSION -x assembler $ASM';
  267. supported_targets : [system_aarch64_darwin];
  268. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  269. labelprefix : 'L';
  270. comment : '# ';
  271. dollarsign: '$';
  272. );
  273. begin
  274. RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
  275. RegisterAssembler(as_aarch64_clang_darwin_info,TAArch64AppleAssembler);
  276. end.