agcpugas.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. end;
  34. const
  35. gas_shiftmode2str : array[tshiftmode] of string[4] = (
  36. '','lsl','lsr','asr',
  37. 'uxtb','uxth','uxtw','uxtx',
  38. 'sxtb','sxth','sxtw','sxtx');
  39. const
  40. cputype_to_gas_march : array[tcputype] of string = (
  41. '', // cpu_none
  42. 'armv8'
  43. );
  44. implementation
  45. uses
  46. cutils,globals,verbose,
  47. systems,
  48. assemble,
  49. aasmcpu,
  50. itcpugas,
  51. cgbase,cgutils;
  52. {****************************************************************************}
  53. { Apple AArch64 Assembler writer }
  54. {****************************************************************************}
  55. constructor TAArch64AppleAssembler.create(smart: boolean);
  56. begin
  57. inherited create(smart);
  58. InstrWriter := TAArch64InstrWriter.create(self);
  59. end;
  60. {****************************************************************************}
  61. { Helper routines for Instruction Writer }
  62. {****************************************************************************}
  63. function getreferencestring(var ref : treference) : string;
  64. const
  65. darwin_addrpage2str: array[addr_page..addr_gotpageoffset] of string[11] =
  66. ('@PAGE','@PAGEOFF','@GOTPAGE','@GOTPAGEOFF');
  67. begin
  68. if ref.base=NR_NO then
  69. begin
  70. case ref.refaddr of
  71. addr_gotpage,
  72. addr_page,
  73. addr_gotpageoffset,
  74. addr_pageoffset:
  75. begin
  76. if not assigned(ref.symbol) or
  77. (ref.base<>NR_NO) or
  78. (ref.index<>NR_NO) or
  79. (ref.shiftmode<>SM_None) or
  80. (ref.offset<>0) then
  81. internalerror(2014121501);
  82. if target_asm.id=as_darwin then
  83. result:=ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  84. else
  85. { todo }
  86. internalerror(2014121502);
  87. end
  88. end
  89. end
  90. else
  91. begin
  92. result:='['+gas_regname(ref.base);
  93. if ref.addressmode=AM_POSTINDEXED then
  94. result:=result+']';
  95. if ref.index<>NR_NO then
  96. begin
  97. if (ref.offset<>0) or
  98. assigned(ref.symbol) then
  99. internalerror(2014121504);
  100. result:=result+', '+gas_regname(ref.index);
  101. case ref.shiftmode of
  102. SM_None: ;
  103. SM_LSL,
  104. SM_UXTW, SM_UXTX, SM_SXTW, SM_SXTX:
  105. begin
  106. result:=result+', '+gas_shiftmode2str[ref.shiftmode];
  107. if (ref.shiftmode=SM_LSL) or
  108. (ref.shiftimm<>0) then
  109. result:=result+' #'+tostr(ref.shiftimm);
  110. end
  111. else
  112. internalerror(2014121505);
  113. end;
  114. end
  115. else
  116. begin
  117. if assigned(ref.symbol) then
  118. begin
  119. case ref.refaddr of
  120. addr_gotpageoffset,
  121. addr_pageoffset:
  122. begin
  123. if target_asm.id=as_darwin then
  124. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  125. else
  126. { todo }
  127. internalerror(2014122510);
  128. end
  129. else
  130. { todo: not yet generated/don't know syntax }
  131. internalerror(2014121506);
  132. end;
  133. end
  134. else
  135. begin
  136. if ref.refaddr<>addr_no then
  137. internalerror(2014121506);
  138. if (ref.offset<>0) then
  139. result:=result+', #'+tostr(ref.offset);
  140. end;
  141. end;
  142. case ref.addressmode of
  143. AM_OFFSET:
  144. result:=result+']';
  145. AM_PREINDEXED:
  146. result:=result+']!';
  147. end;
  148. end;
  149. end;
  150. function getopstr(hp: taicpu; opnr: longint; const o: toper): string;
  151. begin
  152. case o.typ of
  153. top_reg:
  154. getopstr:=gas_regname(o.reg);
  155. top_shifterop:
  156. begin
  157. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  158. if o.shifterop^.shiftimm<>0 then
  159. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  160. end;
  161. top_const:
  162. if o.val>=0 then
  163. getopstr:='#'+tostr(o.val)
  164. else
  165. getopstr:='#0x'+hexStr(o.val,16);
  166. top_conditioncode:
  167. getopstr:=cond2str[o.cc];
  168. top_ref:
  169. if is_calljmp(hp.opcode) then
  170. begin
  171. if o.ref^.refaddr<>addr_full then
  172. internalerror(2014122220);
  173. if not assigned(o.ref^.symbol) or
  174. assigned(o.ref^.relsymbol) or
  175. (o.ref^.base<>NR_NO) or
  176. (o.ref^.index<>NR_NO) or
  177. (o.ref^.offset<>0) then
  178. internalerror(2014122221);
  179. getopstr:=o.ref^.symbol.name;
  180. end
  181. else
  182. getopstr:=getreferencestring(o.ref^);
  183. else
  184. internalerror(2014121507);
  185. end;
  186. end;
  187. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  188. var
  189. op: TAsmOp;
  190. s: string;
  191. i: byte;
  192. sep: string[3];
  193. begin
  194. op:=taicpu(hp).opcode;
  195. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  196. if taicpu(hp).condition<>C_NONE then
  197. s:=s+'.'+cond2str[taicpu(hp).condition];
  198. if taicpu(hp).ops<>0 then
  199. begin
  200. sep:=#9;
  201. for i:=0 to taicpu(hp).ops-1 do
  202. begin
  203. // debug code
  204. // writeln(s);
  205. // writeln(taicpu(hp).fileinfo.line);
  206. s:=s+sep+getopstr(taicpu(hp),i,taicpu(hp).oper[i]^);
  207. sep:=',';
  208. end;
  209. end;
  210. owner.AsmWriteLn(s);
  211. end;
  212. const
  213. as_aarch64_gas_darwin_info : tasminfo =
  214. (
  215. id : as_darwin;
  216. idtxt : 'AS-Darwin';
  217. asmbin : 'as';
  218. asmcmd : '-o $OBJ $EXTRAOPT $ASM -arch arm64';
  219. supported_targets : [system_aarch64_darwin];
  220. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_stabs_use_function_absolute_addresses];
  221. labelprefix : 'L';
  222. comment : '# ';
  223. dollarsign: '$';
  224. );
  225. begin
  226. RegisterAssembler(as_aarch64_gas_darwin_info,TAArch64AppleAssembler);
  227. end.