agcpugas.pas 11 KB

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