agcpugas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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',
  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. end;
  161. end;
  162. end;
  163. function getopstr(asminfo: pasminfo; hp: taicpu; opnr: longint; const o: toper): string;
  164. begin
  165. case o.typ of
  166. top_reg:
  167. { we cannot yet represent "umov w0, v4.s[0]" or "ins v4.d[0], x1",
  168. so for now we use "s4" or "d4" instead -> translate here }
  169. if ((hp.opcode=A_INS) or
  170. (hp.opcode=A_UMOV)) and
  171. (getregtype(hp.oper[opnr]^.reg)=R_MMREGISTER) then
  172. begin
  173. case getsubreg(hp.oper[opnr]^.reg) of
  174. R_SUBMMS:
  175. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.S[0]';
  176. R_SUBMMD:
  177. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.D[0]';
  178. else
  179. internalerror(2014122907);
  180. end;
  181. end
  182. else
  183. getopstr:=gas_regname(o.reg);
  184. top_shifterop:
  185. begin
  186. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  187. if o.shifterop^.shiftimm<>0 then
  188. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  189. end;
  190. top_const:
  191. if o.val>=0 then
  192. getopstr:='#'+tostr(o.val)
  193. else
  194. getopstr:='#0x'+hexStr(o.val,16);
  195. top_conditioncode:
  196. getopstr:=cond2str[o.cc];
  197. top_ref:
  198. if is_calljmp(hp.opcode) then
  199. begin
  200. if o.ref^.refaddr<>addr_full then
  201. internalerror(2014122220);
  202. if not assigned(o.ref^.symbol) or
  203. assigned(o.ref^.relsymbol) or
  204. (o.ref^.base<>NR_NO) or
  205. (o.ref^.index<>NR_NO) or
  206. (o.ref^.offset<>0) then
  207. internalerror(2014122221);
  208. getopstr:=o.ref^.symbol.name;
  209. end
  210. else
  211. getopstr:=getreferencestring(asminfo,o.ref^);
  212. else
  213. internalerror(2014121507);
  214. end;
  215. end;
  216. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  217. var
  218. op: TAsmOp;
  219. s: string;
  220. i: byte;
  221. sep: string[3];
  222. begin
  223. op:=taicpu(hp).opcode;
  224. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  225. if taicpu(hp).condition<>C_NONE then
  226. s:=s+'.'+cond2str[taicpu(hp).condition];
  227. if taicpu(hp).ops<>0 then
  228. begin
  229. sep:=#9;
  230. for i:=0 to taicpu(hp).ops-1 do
  231. begin
  232. // debug code
  233. // writeln(s);
  234. // writeln(taicpu(hp).fileinfo.line);
  235. s:=s+sep+getopstr(owner.asminfo,taicpu(hp),i,taicpu(hp).oper[i]^);
  236. sep:=',';
  237. end;
  238. end;
  239. owner.writer.AsmWriteLn(s);
  240. end;
  241. const
  242. as_aarch64_gas_info : tasminfo =
  243. (
  244. id : as_gas;
  245. idtxt : 'AS';
  246. asmbin : 'as';
  247. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  248. supported_targets : [system_aarch64_linux,system_aarch64_android];
  249. flags : [af_needar,af_smartlink_sections];
  250. labelprefix : '.L';
  251. comment : '// ';
  252. dollarsign: '$';
  253. );
  254. as_aarch64_clang_darwin_info : tasminfo =
  255. (
  256. id : as_clang_asdarwin;
  257. idtxt : 'CLANG';
  258. asmbin : 'clang';
  259. asmcmd : '-x assembler -c -target $TRIPLET -o $OBJ $EXTRAOPT -x assembler $ASM';
  260. supported_targets : [system_aarch64_ios,system_aarch64_darwin];
  261. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_llvm];
  262. labelprefix : 'L';
  263. comment : '# ';
  264. dollarsign: '$';
  265. );
  266. begin
  267. RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
  268. RegisterAssembler(as_aarch64_clang_darwin_info,TAArch64AppleAssembler);
  269. end.