2
0

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