agcpugas.pas 10 KB

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