agcpugas.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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,
  24. aasmtai,
  25. aggas,
  26. cpubase,cpuinfo;
  27. type
  28. TAArch64InstrWriter=class(TCPUInstrWriter)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. TAArch64Assembler=class(TGNUassembler)
  32. constructor create(smart: boolean); override;
  33. end;
  34. TAArch64AppleAssembler=class(TAppleGNUassembler)
  35. constructor create(smart: boolean); override;
  36. function MakeCmdLine: TCmdStr; override;
  37. end;
  38. const
  39. gas_shiftmode2str : array[tshiftmode] of string[4] = (
  40. '','lsl','lsr','asr',
  41. 'uxtb','uxth','uxtw','uxtx',
  42. 'sxtb','sxth','sxtw','sxtx');
  43. const
  44. cputype_to_gas_march : array[tcputype] of string = (
  45. '', // cpu_none
  46. 'armv8'
  47. );
  48. implementation
  49. uses
  50. cutils,globals,verbose,
  51. systems,
  52. assemble,
  53. aasmcpu,
  54. itcpugas,
  55. cgbase,cgutils;
  56. {****************************************************************************}
  57. { AArch64 Assembler writer }
  58. {****************************************************************************}
  59. constructor TAArch64Assembler.create(smart: boolean);
  60. begin
  61. inherited create(smart);
  62. InstrWriter := TAArch64InstrWriter.create(self);
  63. end;
  64. {****************************************************************************}
  65. { Apple AArch64 Assembler writer }
  66. {****************************************************************************}
  67. constructor TAArch64AppleAssembler.create(smart: boolean);
  68. begin
  69. inherited create(smart);
  70. InstrWriter := TAArch64InstrWriter.create(self);
  71. end;
  72. function TAArch64AppleAssembler.MakeCmdLine: TCmdStr;
  73. begin
  74. { 'as' calls through to clang for aarch64, and that one only supports
  75. reading from standard input in case "-" is specified as input file
  76. (in which case you also have to specify the language via -x) }
  77. result:=inherited;
  78. {$ifdef hasunix}
  79. if DoPipe then
  80. result:=result+' -x assembler -'
  81. {$endif}
  82. end;
  83. {****************************************************************************}
  84. { Helper routines for Instruction Writer }
  85. {****************************************************************************}
  86. function getreferencestring(var ref : treference) : string;
  87. const
  88. darwin_addrpage2str: array[addr_page..addr_gotpageoffset] of string[11] =
  89. ('@PAGE','@PAGEOFF','@GOTPAGE','@GOTPAGEOFF');
  90. linux_addrpage2str: array[addr_page..addr_gotpageoffset] of string[10] =
  91. ('',':lo12:',':got:',':got_lo12:');
  92. begin
  93. if ref.base=NR_NO then
  94. begin
  95. case ref.refaddr of
  96. addr_gotpage,
  97. addr_page,
  98. addr_gotpageoffset,
  99. addr_pageoffset:
  100. begin
  101. if not assigned(ref.symbol) or
  102. (ref.base<>NR_NO) or
  103. (ref.index<>NR_NO) or
  104. (ref.shiftmode<>SM_None) or
  105. (ref.offset<>0) then
  106. internalerror(2014121501);
  107. if target_asm.id=as_darwin then
  108. result:=ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  109. else
  110. result:=linux_addrpage2str[ref.refaddr]+ref.symbol.name
  111. end
  112. else
  113. internalerror(2015022301);
  114. end
  115. end
  116. else
  117. begin
  118. result:='['+gas_regname(ref.base);
  119. if ref.addressmode=AM_POSTINDEXED then
  120. result:=result+']';
  121. if ref.index<>NR_NO then
  122. begin
  123. if (ref.offset<>0) or
  124. assigned(ref.symbol) then
  125. internalerror(2014121504);
  126. result:=result+', '+gas_regname(ref.index);
  127. case ref.shiftmode of
  128. SM_None: ;
  129. SM_LSL,
  130. SM_UXTW, SM_UXTX, SM_SXTW, SM_SXTX:
  131. begin
  132. result:=result+', '+gas_shiftmode2str[ref.shiftmode];
  133. if (ref.shiftmode=SM_LSL) or
  134. (ref.shiftimm<>0) then
  135. result:=result+' #'+tostr(ref.shiftimm);
  136. end
  137. else
  138. internalerror(2014121505);
  139. end;
  140. end
  141. else
  142. begin
  143. if assigned(ref.symbol) then
  144. begin
  145. case ref.refaddr of
  146. addr_gotpageoffset,
  147. addr_pageoffset:
  148. begin
  149. if target_asm.id=as_darwin then
  150. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  151. else
  152. result:=result+', '+linux_addrpage2str[ref.refaddr]+ref.symbol.name
  153. end
  154. else
  155. { todo: not yet generated/don't know syntax }
  156. internalerror(2014121506);
  157. end;
  158. end
  159. else
  160. begin
  161. if ref.refaddr<>addr_no then
  162. internalerror(2014121506);
  163. if (ref.offset<>0) then
  164. result:=result+', #'+tostr(ref.offset);
  165. end;
  166. end;
  167. case ref.addressmode of
  168. AM_OFFSET:
  169. result:=result+']';
  170. AM_PREINDEXED:
  171. result:=result+']!';
  172. end;
  173. end;
  174. end;
  175. function getopstr(hp: taicpu; opnr: longint; const o: toper): string;
  176. begin
  177. case o.typ of
  178. top_reg:
  179. { we cannot yet represent "umov w0, v4.s[0]" or "ins v4.d[0], x1",
  180. so for now we use "s4" or "d4" instead -> translate here }
  181. if ((hp.opcode=A_INS) or
  182. (hp.opcode=A_UMOV)) and
  183. (getregtype(hp.oper[opnr]^.reg)=R_MMREGISTER) then
  184. begin
  185. case getsubreg(hp.oper[opnr]^.reg) of
  186. R_SUBMMS:
  187. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.S[0]';
  188. R_SUBMMD:
  189. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.D[0]';
  190. else
  191. internalerror(2014122907);
  192. end;
  193. end
  194. else
  195. getopstr:=gas_regname(o.reg);
  196. top_shifterop:
  197. begin
  198. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  199. if o.shifterop^.shiftimm<>0 then
  200. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  201. end;
  202. top_const:
  203. if o.val>=0 then
  204. getopstr:='#'+tostr(o.val)
  205. else
  206. getopstr:='#0x'+hexStr(o.val,16);
  207. top_conditioncode:
  208. getopstr:=cond2str[o.cc];
  209. top_ref:
  210. if is_calljmp(hp.opcode) then
  211. begin
  212. if o.ref^.refaddr<>addr_full then
  213. internalerror(2014122220);
  214. if not assigned(o.ref^.symbol) or
  215. assigned(o.ref^.relsymbol) or
  216. (o.ref^.base<>NR_NO) or
  217. (o.ref^.index<>NR_NO) or
  218. (o.ref^.offset<>0) then
  219. internalerror(2014122221);
  220. getopstr:=o.ref^.symbol.name;
  221. end
  222. else
  223. getopstr:=getreferencestring(o.ref^);
  224. else
  225. internalerror(2014121507);
  226. end;
  227. end;
  228. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  229. var
  230. op: TAsmOp;
  231. s: string;
  232. i: byte;
  233. sep: string[3];
  234. begin
  235. op:=taicpu(hp).opcode;
  236. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  237. if taicpu(hp).condition<>C_NONE then
  238. s:=s+'.'+cond2str[taicpu(hp).condition];
  239. if taicpu(hp).ops<>0 then
  240. begin
  241. sep:=#9;
  242. for i:=0 to taicpu(hp).ops-1 do
  243. begin
  244. // debug code
  245. // writeln(s);
  246. // writeln(taicpu(hp).fileinfo.line);
  247. s:=s+sep+getopstr(taicpu(hp),i,taicpu(hp).oper[i]^);
  248. sep:=',';
  249. end;
  250. end;
  251. owner.AsmWriteLn(s);
  252. end;
  253. const
  254. as_aarch64_gas_info : tasminfo =
  255. (
  256. id : as_gas;
  257. idtxt : 'AS';
  258. asmbin : 'as';
  259. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  260. supported_targets : [system_aarch64_linux];
  261. flags : [af_needar,af_smartlink_sections];
  262. labelprefix : '.L';
  263. comment : '// ';
  264. dollarsign: '$';
  265. );
  266. as_aarch64_gas_darwin_info : tasminfo =
  267. (
  268. id : as_darwin;
  269. idtxt : 'AS-Darwin';
  270. asmbin : 'as';
  271. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch arm64';
  272. supported_targets : [system_aarch64_darwin];
  273. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  274. labelprefix : 'L';
  275. comment : '# ';
  276. dollarsign: '$';
  277. );
  278. begin
  279. RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
  280. RegisterAssembler(as_aarch64_gas_darwin_info,TAArch64AppleAssembler);
  281. end.