agcpugas.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. TAArch64AppleAssembler=class(TAppleGNUassembler)
  32. constructor create(smart: boolean); override;
  33. function MakeCmdLine: TCmdStr; override;
  34. end;
  35. const
  36. gas_shiftmode2str : array[tshiftmode] of string[4] = (
  37. '','lsl','lsr','asr',
  38. 'uxtb','uxth','uxtw','uxtx',
  39. 'sxtb','sxth','sxtw','sxtx');
  40. const
  41. cputype_to_gas_march : array[tcputype] of string = (
  42. '', // cpu_none
  43. 'armv8'
  44. );
  45. implementation
  46. uses
  47. cutils,globals,verbose,
  48. systems,
  49. assemble,
  50. aasmcpu,
  51. itcpugas,
  52. cgbase,cgutils;
  53. {****************************************************************************}
  54. { Apple AArch64 Assembler writer }
  55. {****************************************************************************}
  56. constructor TAArch64AppleAssembler.create(smart: boolean);
  57. begin
  58. inherited create(smart);
  59. InstrWriter := TAArch64InstrWriter.create(self);
  60. end;
  61. function TAArch64AppleAssembler.MakeCmdLine: TCmdStr;
  62. begin
  63. { 'as' calls through to clang for aarch64, and that one only supports
  64. reading from standard input in case "-" is specified as input file
  65. (in which case you also have to specify the language via -x) }
  66. result:=inherited;
  67. {$ifdef hasunix}
  68. if DoPipe then
  69. result:=result+' -x assembler -'
  70. {$endif}
  71. end;
  72. {****************************************************************************}
  73. { Helper routines for Instruction Writer }
  74. {****************************************************************************}
  75. function getreferencestring(var ref : treference) : string;
  76. const
  77. darwin_addrpage2str: array[addr_page..addr_gotpageoffset] of string[11] =
  78. ('@PAGE','@PAGEOFF','@GOTPAGE','@GOTPAGEOFF');
  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_asm.id=as_darwin then
  95. result:=ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  96. else
  97. { todo }
  98. internalerror(2014121502);
  99. end
  100. else
  101. internalerror(2015022301);
  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_asm.id=as_darwin then
  138. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  139. else
  140. { todo }
  141. internalerror(2014122510);
  142. end
  143. else
  144. { todo: not yet generated/don't know syntax }
  145. internalerror(2014121506);
  146. end;
  147. end
  148. else
  149. begin
  150. if ref.refaddr<>addr_no then
  151. internalerror(2014121506);
  152. if (ref.offset<>0) then
  153. result:=result+', #'+tostr(ref.offset);
  154. end;
  155. end;
  156. case ref.addressmode of
  157. AM_OFFSET:
  158. result:=result+']';
  159. AM_PREINDEXED:
  160. result:=result+']!';
  161. end;
  162. end;
  163. end;
  164. function getopstr(hp: taicpu; opnr: longint; const o: toper): string;
  165. begin
  166. case o.typ of
  167. top_reg:
  168. { we cannot yet represent "umov w0, v4.s[0]" or "ins v4.d[0], x1",
  169. so for now we use "s4" or "d4" instead -> translate here }
  170. if ((hp.opcode=A_INS) or
  171. (hp.opcode=A_UMOV)) and
  172. (getregtype(hp.oper[opnr]^.reg)=R_MMREGISTER) then
  173. begin
  174. case getsubreg(hp.oper[opnr]^.reg) of
  175. R_SUBMMS:
  176. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.S[0]';
  177. R_SUBMMD:
  178. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.D[0]';
  179. else
  180. internalerror(2014122907);
  181. end;
  182. end
  183. else
  184. getopstr:=gas_regname(o.reg);
  185. top_shifterop:
  186. begin
  187. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  188. if o.shifterop^.shiftimm<>0 then
  189. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  190. end;
  191. top_const:
  192. if o.val>=0 then
  193. getopstr:='#'+tostr(o.val)
  194. else
  195. getopstr:='#0x'+hexStr(o.val,16);
  196. top_conditioncode:
  197. getopstr:=cond2str[o.cc];
  198. top_ref:
  199. if is_calljmp(hp.opcode) then
  200. begin
  201. if o.ref^.refaddr<>addr_full then
  202. internalerror(2014122220);
  203. if not assigned(o.ref^.symbol) or
  204. assigned(o.ref^.relsymbol) or
  205. (o.ref^.base<>NR_NO) or
  206. (o.ref^.index<>NR_NO) or
  207. (o.ref^.offset<>0) then
  208. internalerror(2014122221);
  209. getopstr:=o.ref^.symbol.name;
  210. end
  211. else
  212. getopstr:=getreferencestring(o.ref^);
  213. else
  214. internalerror(2014121507);
  215. end;
  216. end;
  217. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  218. var
  219. op: TAsmOp;
  220. s: string;
  221. i: byte;
  222. sep: string[3];
  223. begin
  224. op:=taicpu(hp).opcode;
  225. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  226. if taicpu(hp).condition<>C_NONE then
  227. s:=s+'.'+cond2str[taicpu(hp).condition];
  228. if taicpu(hp).ops<>0 then
  229. begin
  230. sep:=#9;
  231. for i:=0 to taicpu(hp).ops-1 do
  232. begin
  233. // debug code
  234. // writeln(s);
  235. // writeln(taicpu(hp).fileinfo.line);
  236. s:=s+sep+getopstr(taicpu(hp),i,taicpu(hp).oper[i]^);
  237. sep:=',';
  238. end;
  239. end;
  240. owner.AsmWriteLn(s);
  241. end;
  242. const
  243. as_aarch64_gas_darwin_info : tasminfo =
  244. (
  245. id : as_darwin;
  246. idtxt : 'AS-Darwin';
  247. asmbin : 'as';
  248. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch arm64';
  249. supported_targets : [system_aarch64_darwin];
  250. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  251. labelprefix : 'L';
  252. comment : '# ';
  253. dollarsign: '$';
  254. );
  255. begin
  256. RegisterAssembler(as_aarch64_gas_darwin_info,TAArch64AppleAssembler);
  257. end.