agcpugas.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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,aasmdata,aasmbase,
  25. assemble,aggas,
  26. cpubase,cpuinfo;
  27. type
  28. TAArch64InstrWriter=class(TCPUInstrWriter)
  29. procedure WriteInstruction(hp : tai);override;
  30. end;
  31. TAArch64Assembler=class(TGNUassembler)
  32. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  33. end;
  34. TAArch64AppleAssembler=class(TAppleGNUassembler)
  35. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  36. end;
  37. TAArch64ClangGASAssembler=class(TGNUassembler)
  38. private
  39. function TargetStr:String;
  40. procedure TransformSEHDirectives(list:TAsmList);
  41. protected
  42. function sectionflags(secflags:TSectionFlags):string;override;
  43. public
  44. function MakeCmdLine:TCmdStr; override;
  45. procedure WriteAsmList; override;
  46. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  47. end;
  48. const
  49. gas_shiftmode2str : array[tshiftmode] of string[4] = (
  50. '','lsl','lsr','asr','ror',
  51. 'uxtb','uxth','uxtw','uxtx',
  52. 'sxtb','sxth','sxtw','sxtx');
  53. const
  54. cputype_to_gas_march : array[tcputype] of string = (
  55. '', // cpu_none
  56. 'armv8'
  57. );
  58. implementation
  59. uses
  60. cutils,cclasses,globals,verbose,
  61. aasmcpu,
  62. itcpugas,
  63. cgbase,cgutils;
  64. {****************************************************************************}
  65. { AArch64 Assembler writer }
  66. {****************************************************************************}
  67. constructor TAArch64Assembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  68. begin
  69. inherited;
  70. InstrWriter := TAArch64InstrWriter.create(self);
  71. end;
  72. {****************************************************************************}
  73. { Apple AArch64 Assembler writer }
  74. {****************************************************************************}
  75. constructor TAArch64AppleAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  76. begin
  77. inherited;
  78. InstrWriter := TAArch64InstrWriter.create(self);
  79. end;
  80. {****************************************************************************}
  81. { CLang AArch64 Assembler writer }
  82. {****************************************************************************}
  83. constructor TAArch64CLangGASAssembler.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  84. begin
  85. inherited;
  86. InstrWriter := TAArch64InstrWriter.create(self);
  87. end;
  88. function TAArch64ClangGASAssembler.TargetStr:String;
  89. begin
  90. case target_info.system of
  91. system_aarch64_win64:
  92. result:='aarch64-windows';
  93. else
  94. internalerror(2020032201);
  95. end;
  96. end;
  97. procedure TAArch64ClangGASAssembler.TransformSEHDirectives(list:TAsmList);
  98. function convert_unwinddata(list:tasmlist):tdynamicarray;
  99. procedure check_offset(ofs,max:dword);
  100. begin
  101. if ((ofs and $7)<>0) or (ofs>max) then
  102. internalerror(2020041210);
  103. end;
  104. procedure check_reg(reg:tregister;rt:TRegisterType;min:TSuperRegister);
  105. begin
  106. if (getregtype(reg)<>rt) or (getsupreg(reg)<min) then
  107. internalerror(2020041211);
  108. end;
  109. procedure writebyte(b:byte); inline;
  110. begin
  111. result.write(b,sizeof(b));
  112. end;
  113. procedure writeword(w:word);
  114. begin
  115. w:=NtoBE(w);
  116. result.write(w,sizeof(w));
  117. end;
  118. procedure writedword(dw:dword);
  119. begin
  120. dw:=NtoBE(dw);
  121. result.write(dw,sizeof(dw));
  122. end;
  123. const
  124. min_int_reg = 19;
  125. min_mm_reg = 8;
  126. var
  127. hp : tai;
  128. seh : tai_seh_directive absolute hp;
  129. begin
  130. result:=tdynamicarray.create(0);
  131. hp:=tai(list.last);
  132. while assigned(hp) do
  133. begin
  134. if hp.typ<>ait_seh_directive then
  135. internalerror(2020041502);
  136. case seh.kind of
  137. ash_stackalloc:
  138. begin
  139. if (seh.data.offset and $f)<>0 then
  140. internalerror(2020041207);
  141. if seh.data.offset<((1 shl 5)*16) then
  142. writebyte(byte(seh.data.offset shr 4))
  143. else if seh.data.offset<((1 shl 11)*16) then
  144. writeword($C000 or word(seh.data.offset shr 4))
  145. else if seh.data.offset<((1 shl 24)*16) then
  146. writedword($E0000000 or (seh.data.offset shr 4))
  147. else begin
  148. writeln(hexstr(seh.data.offset,8));
  149. internalerror(2020041209);
  150. end;
  151. end;
  152. ash_addfp:
  153. begin
  154. check_offset(seh.data.offset,(1 shl 7)*8);
  155. writeword($E200 or (seh.data.offset shr 3));
  156. end;
  157. ash_setfp:
  158. writebyte($E1);
  159. ash_nop:
  160. writebyte($E3);
  161. ash_savefplr:
  162. begin
  163. check_offset(seh.data.offset,504);
  164. writebyte($40 or (seh.data.offset shr 3));
  165. end;
  166. ash_savefplr_x:
  167. begin
  168. check_offset(seh.data.offset,512);
  169. writebyte($80 or (seh.data.offset shr 3)-1);
  170. end;
  171. ash_savereg:
  172. begin
  173. check_offset(seh.data.offset,504);
  174. check_reg(seh.data.reg,R_INTREGISTER,min_int_reg);
  175. writeword($C000 or ((getsupreg(seh.data.reg)-min_int_reg) shl 6) or (seh.data.offset shr 3));
  176. end;
  177. ash_savereg_x:
  178. begin
  179. check_offset(seh.data.offset,256);
  180. check_reg(seh.data.reg,R_INTREGISTER,min_int_reg);
  181. writeword($C400 or ((getsupreg(seh.data.reg)-min_int_reg) shl 5) or ((seh.data.offset shr 3)-1));
  182. end;
  183. ash_saveregp:
  184. begin
  185. check_offset(seh.data.offset,504);
  186. check_reg(seh.data.reg,R_INTREGISTER,min_int_reg);
  187. writeword($C800 or ((getsupreg(seh.data.reg)-min_int_reg) shl 6) or (seh.data.offset shr 3));
  188. end;
  189. ash_saveregp_x:
  190. begin
  191. check_offset(seh.data.offset,512);
  192. check_reg(seh.data.reg,R_INTREGISTER,min_int_reg);
  193. writeword($CC00 or ((getsupreg(seh.data.reg)-min_int_reg) shl 6) or ((seh.data.offset shr 3)-1));
  194. end;
  195. ash_savefreg:
  196. begin
  197. check_offset(seh.data.offset,504);
  198. check_reg(seh.data.reg,R_MMREGISTER,min_mm_reg);
  199. writeword($DC00 or ((getsupreg(seh.data.reg)-min_mm_reg) shl 6) or (seh.data.offset shr 3));
  200. end;
  201. ash_savefreg_x:
  202. begin
  203. check_offset(seh.data.offset,256);
  204. check_reg(seh.data.reg,R_MMREGISTER,min_mm_reg);
  205. writeword($CE00 or ((getsupreg(seh.data.reg)-min_mm_reg) shl 5) or ((seh.data.offset shr 3)-1));
  206. end;
  207. ash_savefregp:
  208. begin
  209. check_offset(seh.data.offset,504);
  210. check_reg(seh.data.reg,R_MMREGISTER,min_mm_reg);
  211. writeword($D800 or ((getsupreg(seh.data.reg)-min_mm_reg) shl 6) or (seh.data.offset shr 3));
  212. end;
  213. ash_savefregp_x:
  214. begin
  215. check_offset(seh.data.offset,512);
  216. check_reg(seh.data.reg,R_MMREGISTER,min_mm_reg);
  217. writeword($DA00 or ((getsupreg(seh.data.reg)-min_int_reg) shl 6) or ((seh.data.offset shr 3)-1));
  218. end;
  219. else
  220. internalerror(2020041503);
  221. end;
  222. hp:=tai(hp.previous);
  223. end;
  224. end;
  225. var
  226. unwinddata : tdynamicarray;
  227. procedure writebyte(b:byte);
  228. begin
  229. unwinddata.write(b,sizeof(b));
  230. end;
  231. var
  232. hp,hpnext,hpdata : tai;
  233. seh : tai_seh_directive absolute hp;
  234. lastsym : tai_symbol;
  235. lastsec : tai_section;
  236. inprologue,
  237. inhandlerdata,
  238. deleteai : boolean;
  239. totalcount,
  240. instrcount,
  241. datacount : sizeint;
  242. handlername : tsymstr;
  243. handlerflags : byte;
  244. handlerdata : array of tai;
  245. handlerdataidx : sizeint;
  246. handlerdatacount : tai;
  247. sehlist,
  248. tmplist : TAsmList;
  249. xdatasym : tasmsymbol;
  250. unwindread,
  251. unwindrec : longword;
  252. begin
  253. if not assigned(list) then
  254. exit;
  255. tmplist:=nil;
  256. sehlist:=nil;
  257. lastsec:=nil;
  258. instrcount:=0;
  259. datacount:=0;
  260. unwinddata:=nil;
  261. inhandlerdata:=false;
  262. inprologue:=false;
  263. handlerdata:=nil;
  264. handlerdataidx:=0;
  265. handlerdatacount:=nil;
  266. hp:=tai(list.first);
  267. while assigned(hp) do
  268. begin
  269. deleteai:=false;
  270. case hp.typ of
  271. ait_section:
  272. begin
  273. if assigned(sehlist) then
  274. begin
  275. if assigned(lastsec) and (tai_section(hp).name^=lastsec.name^) then
  276. begin
  277. { this section was only added due to the now removed SEH data }
  278. deleteai:=true;
  279. dec(list.section_count);
  280. end
  281. else
  282. internalerror(2020041214);
  283. end
  284. else
  285. lastsec:=tai_section(hp);
  286. if assigned(tmplist) then
  287. begin
  288. list.insertListBefore(hp,tmplist);
  289. tmplist.free;
  290. tmplist:=nil;
  291. end;
  292. end;
  293. ait_symbol:
  294. begin
  295. if tai_symbol(hp).is_global then
  296. lastsym:=tai_symbol(hp);
  297. end;
  298. ait_instruction:
  299. if assigned(sehlist) then
  300. inc(instrcount);
  301. ait_const:
  302. if assigned(sehlist) then
  303. inc(datacount,tai_const(hp).size);
  304. ait_seh_directive:
  305. begin
  306. if not assigned(sehlist) and (seh.kind<>ash_proc) then
  307. internalerror(2020041208);
  308. { most seh directives are removed }
  309. deleteai:=true;
  310. case seh.kind of
  311. ash_proc:
  312. begin
  313. if not assigned(lastsec) then
  314. internalerror(2020041203);
  315. datacount:=0;
  316. instrcount:=0;
  317. handlerflags:=0;
  318. handlername:='';
  319. sehlist:=tasmlist.create;
  320. inprologue:=true;
  321. end;
  322. ash_endproc:
  323. begin
  324. if not assigned(sehlist) then
  325. internalerror(2020041501);
  326. if assigned(tmplist) then
  327. internalerror(2020041302);
  328. if not assigned(lastsym) then
  329. internalerror(2020041303);
  330. if inprologue then
  331. cgmessage(asmw_e_missing_endprologue);
  332. unwinddata:=convert_unwinddata(sehlist);
  333. writebyte($E4);
  334. { fill up with NOPs }
  335. while unwinddata.size mod 4<>0 do
  336. writebyte($E3);
  337. { note: we can pass Nil here, because in case of a LLVM
  338. backend this whole code shouldn't be required
  339. anyway }
  340. xdatasym:=current_asmdata.DefineAsmSymbol('xdata_'+lastsec.name^,AB_LOCAL,AT_DATA,nil);
  341. tmplist:=tasmlist.create;
  342. new_section(tmplist,sec_pdata,lastsec.name^,0);
  343. tmplist.concat(tai_const.Create_rva_sym(lastsym.sym));
  344. tmplist.concat(tai_const.Create_rva_sym(xdatasym));
  345. new_section(tmplist,sec_rodata,xdatasym.name,0);
  346. tmplist.concat(tai_symbol.Create(xdatasym,0));
  347. tmplist.concat(tai_comment.Create(strpnew('instr: '+tostr(instrcount)+', data: '+tostr(datacount)+', unwind: '+tostr(unwinddata.size))));
  348. {$ifdef EXTDEBUG}
  349. comment(V_Debug,'got section: '+lastsec.name^);
  350. comment(V_Debug,'got instructions: '+tostr(instrcount));
  351. comment(V_Debug,'got data: '+tostr(datacount));
  352. comment(V_Debug,'got unwinddata: '+tostr(unwinddata.size));
  353. {$endif EXTDEBUG}
  354. if datacount mod 4<>0 then
  355. cgmessage(asmw_e_seh_invalid_data_size);
  356. totalcount:=datacount div 4+instrcount;
  357. { splitting to multiple pdata/xdata sections is not yet
  358. supported, so 1 MB is our limit for now }
  359. if totalcount>(1 shl 18) then
  360. comment(V_Error,'Function is larger than 1 MB which is not supported for SEH currently');
  361. unwindrec:=min(totalcount,(1 shl 18)-1);
  362. if handlerflags<>0 then
  363. unwindrec:=unwindrec or (1 shl 20);
  364. { currently we only have one epilog, so E needs to be
  365. set to 1 and epilog scope index needs to be 0, no
  366. matter if we require the extension for the unwinddata
  367. or not }
  368. unwindrec:=unwindrec or (1 shl 21);
  369. if unwinddata.size div 4<=31 then
  370. unwindrec:=unwindrec or ((unwinddata.size div 4) shl 27);
  371. { exception record headers }
  372. tmplist.concat(tai_const.Create_32bit(unwindrec));
  373. if cs_asm_source in init_settings.globalswitches then
  374. tmplist.concat(tai_comment.create(strpnew(hexstr(unwindrec,8))));
  375. if unwinddata.size div 4>31 then
  376. begin
  377. { once we're able to split a .pdata entry this can be
  378. removed as well }
  379. if unwinddata.size div 4>255 then
  380. comment(V_Error,'Too many unwind codes for SEH');
  381. unwindrec:=(unwinddata.size div 4) shl 16;
  382. tmplist.concat(tai_const.create_32bit(unwindrec));
  383. if cs_asm_source in init_settings.globalswitches then
  384. tmplist.concat(tai_comment.create(strpnew(hexstr(unwindrec,8))));
  385. end;
  386. { unwind codes }
  387. unwinddata.seek(0);
  388. while unwinddata.pos<unwinddata.size do
  389. begin
  390. unwinddata.read(unwindrec,sizeof(longword));
  391. tmplist.concat(tai_const.Create_32bit(unwindrec));
  392. if cs_asm_source in init_settings.globalswitches then
  393. tmplist.concat(tai_comment.create(strpnew(hexstr(unwindrec,8))));
  394. end;
  395. unwinddata.free;
  396. if handlerflags<>0 then
  397. begin
  398. tmplist.concat(tai_const.Create_rva_sym(current_asmdata.RefAsmSymbol(handlername,AT_FUNCTION,false)));
  399. if length(handlerdata)>0 then
  400. begin
  401. tmplist.concat(handlerdatacount);
  402. for handlerdataidx:=0 to high(handlerdata) do
  403. tmplist.concat(handlerdata[handlerdataidx]);
  404. end;
  405. end;
  406. handlerdata:=nil;
  407. sehlist.free;
  408. sehlist:=nil;
  409. end;
  410. ash_endprologue:
  411. inprologue:=false;
  412. ash_handler:
  413. begin
  414. handlername:=seh.data.name^;
  415. handlerflags:=seh.data.flags;
  416. end;
  417. ash_handlerdata:
  418. begin
  419. if handlername='' then
  420. cgmessage(asmw_e_handlerdata_no_handler);
  421. hpdata:=tai(hp.next);
  422. if not assigned(hpdata) or (hpdata.typ<>ait_const) or (tai_const(hpdata).consttype<>aitconst_32bit) then
  423. internalerror(2020041215);
  424. handlerdatacount:=hpdata;
  425. setlength(handlerdata,tai_const(hpdata).value*4);
  426. handlerdataidx:=0;
  427. hpnext:=tai(hpdata.next);
  428. list.remove(hpdata);
  429. hpdata:=hpnext;
  430. while (handlerdataidx<length(handlerdata)) and assigned(hpdata) do
  431. begin
  432. if (hpdata.typ<>ait_const) or not (tai_const(hpdata).consttype in [aitconst_32bit,aitconst_rva_symbol]) then
  433. internalerror(2020041212);
  434. handlerdata[handlerdataidx]:=hpdata;
  435. inc(handlerdataidx);
  436. hpnext:=tai(hpdata.next);
  437. list.remove(hpdata);
  438. hpdata:=hpnext;
  439. end;
  440. if handlerdataidx<length(handlerdata) then
  441. internalerror(2020041213);
  442. end;
  443. ash_stackalloc,
  444. ash_addfp,
  445. ash_setfp,
  446. ash_nop,
  447. ash_savefplr,
  448. ash_savefplr_x,
  449. ash_savereg,
  450. ash_savereg_x,
  451. ash_saveregp,
  452. ash_saveregp_x,
  453. ash_savefreg,
  454. ash_savefreg_x,
  455. ash_savefregp,
  456. ash_savefregp_x:
  457. begin
  458. if not assigned(sehlist) then
  459. internalerror(2020041504);
  460. if not inprologue then
  461. internalerror(2020041505);
  462. hpdata:=hp;
  463. hp:=tai(hp.previous);
  464. list.Remove(hpdata);
  465. sehlist.concat(hpdata);
  466. { don't delete this }
  467. deleteai:=false;
  468. end;
  469. else
  470. internalerror(2020041206);
  471. end;
  472. end;
  473. else
  474. { ignore }
  475. ;
  476. end;
  477. if deleteai then
  478. begin
  479. hpnext:=tai(hp.next);
  480. list.remove(hp);
  481. hp.free;
  482. hp:=hpnext;
  483. end
  484. else
  485. hp:=tai(hp.next);
  486. end;
  487. if assigned(sehlist) then
  488. internalerror(2020041205);
  489. if assigned(tmplist) then
  490. begin
  491. list.concatlist(tmplist);
  492. tmplist.free;
  493. end;
  494. end;
  495. function TAArch64ClangGASAssembler.sectionflags(secflags:TSectionFlags):string;
  496. begin
  497. Result:=inherited sectionflags(secflags);
  498. if (target_info.system=system_aarch64_win64) then
  499. begin
  500. { we require an explicit "r" if write is not allowed }
  501. if not (SF_W in secflags) then
  502. result:=result+'r';
  503. end;
  504. end;
  505. function TAArch64ClangGASAssembler.MakeCmdLine:TCmdStr;
  506. begin
  507. Result:=inherited MakeCmdLine;
  508. Replace(Result,'$TARGET',TargetStr);
  509. end;
  510. procedure TAArch64ClangGASAssembler.WriteAsmList;
  511. begin
  512. { clang does not support all the directives we need, so we need to
  513. manually transform them to pdata/xdata records }
  514. if target_info.system=system_aarch64_win64 then
  515. begin
  516. TransformSEHDirectives(current_asmdata.AsmLists[al_pure_assembler]);
  517. TransformSEHDirectives(current_asmdata.AsmLists[al_procedures]);
  518. end;
  519. inherited WriteAsmList;
  520. end;
  521. {****************************************************************************}
  522. { Helper routines for Instruction Writer }
  523. {****************************************************************************}
  524. function getreferencestring(asminfo: pasminfo; var ref : treference) : string;
  525. const
  526. darwin_addrpage2str: array[addr_page..addr_gotpageoffset] of string[11] =
  527. ('@PAGE','@PAGEOFF','@GOTPAGE','@GOTPAGEOFF');
  528. linux_addrpage2str: array[addr_page..addr_gotpageoffset] of string[10] =
  529. ('',':lo12:',':got:',':got_lo12:');
  530. begin
  531. if ref.base=NR_NO then
  532. begin
  533. case ref.refaddr of
  534. addr_gotpage,
  535. addr_page,
  536. addr_gotpageoffset,
  537. addr_pageoffset:
  538. begin
  539. if not assigned(ref.symbol) or
  540. (ref.base<>NR_NO) or
  541. (ref.index<>NR_NO) or
  542. (ref.shiftmode<>SM_None) or
  543. (ref.offset<>0) then
  544. internalerror(2014121501);
  545. if target_info.system in systems_darwin then
  546. result:=ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  547. else
  548. result:=linux_addrpage2str[ref.refaddr]+ref.symbol.name
  549. end;
  550. addr_pic,
  551. { for locals replaced by temp symbols on LLVM }
  552. addr_no:
  553. result:=ref.symbol.name;
  554. else
  555. internalerror(2015022302);
  556. end
  557. end
  558. else
  559. begin
  560. result:='['+gas_regname(ref.base);
  561. if ref.addressmode=AM_POSTINDEXED then
  562. result:=result+']';
  563. if ref.index<>NR_NO then
  564. begin
  565. if (ref.offset<>0) or
  566. assigned(ref.symbol) then
  567. internalerror(2014121504);
  568. result:=result+', '+gas_regname(ref.index);
  569. case ref.shiftmode of
  570. SM_None: ;
  571. SM_LSL,
  572. SM_UXTW, SM_UXTX, SM_SXTW, SM_SXTX:
  573. begin
  574. result:=result+', '+gas_shiftmode2str[ref.shiftmode];
  575. if (ref.shiftmode=SM_LSL) or
  576. (ref.shiftimm<>0) then
  577. result:=result+' #'+tostr(ref.shiftimm);
  578. end
  579. else
  580. internalerror(2014121505);
  581. end;
  582. end
  583. else
  584. begin
  585. if assigned(ref.symbol) then
  586. begin
  587. case ref.refaddr of
  588. addr_gotpageoffset,
  589. addr_pageoffset:
  590. begin
  591. if target_info.system in systems_darwin then
  592. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  593. else
  594. result:=result+', '+linux_addrpage2str[ref.refaddr]+ref.symbol.name
  595. end
  596. else
  597. { todo: not yet generated/don't know syntax }
  598. internalerror(2014121506);
  599. end;
  600. end
  601. else
  602. begin
  603. if ref.refaddr<>addr_no then
  604. internalerror(2014121506);
  605. if (ref.offset<>0) then
  606. result:=result+', #'+tostr(ref.offset);
  607. end;
  608. end;
  609. case ref.addressmode of
  610. AM_OFFSET:
  611. result:=result+']';
  612. AM_PREINDEXED:
  613. result:=result+']!';
  614. else
  615. ;
  616. end;
  617. end;
  618. end;
  619. function getopstr(asminfo: pasminfo; hp: taicpu; opnr: longint; const o: toper): string;
  620. begin
  621. case o.typ of
  622. top_reg:
  623. { we cannot yet represent "umov w0, v4.s[0]" or "ins v4.d[0], x1",
  624. so for now we use "s4" or "d4" instead -> translate here }
  625. if ((hp.opcode=A_INS) or
  626. (hp.opcode=A_UMOV)) and
  627. (getregtype(hp.oper[opnr]^.reg)=R_MMREGISTER) then
  628. begin
  629. case getsubreg(hp.oper[opnr]^.reg) of
  630. R_SUBMMS:
  631. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.S[0]';
  632. R_SUBMMD:
  633. getopstr:='v'+tostr(getsupreg(hp.oper[opnr]^.reg))+'.D[0]';
  634. else
  635. internalerror(2014122907);
  636. end;
  637. end
  638. else
  639. getopstr:=gas_regname(o.reg);
  640. top_shifterop:
  641. begin
  642. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  643. if o.shifterop^.shiftimm<>0 then
  644. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  645. end;
  646. top_const:
  647. if o.val>=0 then
  648. getopstr:='#'+tostr(o.val)
  649. else
  650. getopstr:='#0x'+hexStr(o.val,16);
  651. top_conditioncode:
  652. getopstr:=cond2str[o.cc];
  653. top_ref:
  654. if is_calljmp(hp.opcode) then
  655. begin
  656. if o.ref^.refaddr<>addr_full then
  657. internalerror(2014122220);
  658. if not assigned(o.ref^.symbol) or
  659. assigned(o.ref^.relsymbol) or
  660. (o.ref^.base<>NR_NO) or
  661. (o.ref^.index<>NR_NO) or
  662. (o.ref^.offset<>0) then
  663. internalerror(2014122221);
  664. getopstr:=o.ref^.symbol.name;
  665. end
  666. else
  667. getopstr:=getreferencestring(asminfo,o.ref^);
  668. top_realconst:
  669. begin
  670. str(o.val_real,Result);
  671. Result:='#'+Result;
  672. end
  673. else
  674. internalerror(2014121507);
  675. end;
  676. end;
  677. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  678. var
  679. op: TAsmOp;
  680. s: string;
  681. i: byte;
  682. sep: string[3];
  683. begin
  684. op:=taicpu(hp).opcode;
  685. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  686. if taicpu(hp).condition<>C_NONE then
  687. s:=s+'.'+cond2str[taicpu(hp).condition];
  688. if taicpu(hp).ops<>0 then
  689. begin
  690. sep:=#9;
  691. for i:=0 to taicpu(hp).ops-1 do
  692. begin
  693. // debug code
  694. // writeln(s);
  695. // writeln(taicpu(hp).fileinfo.line);
  696. s:=s+sep+getopstr(owner.asminfo,taicpu(hp),i,taicpu(hp).oper[i]^);
  697. sep:=',';
  698. end;
  699. end;
  700. owner.writer.AsmWriteLn(s);
  701. end;
  702. const
  703. as_aarch64_gas_info : tasminfo =
  704. (
  705. id : as_gas;
  706. idtxt : 'AS';
  707. asmbin : 'as';
  708. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  709. supported_targets : [system_aarch64_linux,system_aarch64_android];
  710. flags : [af_needar,af_smartlink_sections];
  711. labelprefix : '.L';
  712. labelmaxlen : -1;
  713. comment : '// ';
  714. dollarsign: '$';
  715. );
  716. as_aarch64_clang_darwin_info : tasminfo =
  717. (
  718. id : as_clang;
  719. idtxt : 'CLANG';
  720. asmbin : 'clang';
  721. asmcmd : '-c -o $OBJ $EXTRAOPT -arch arm64 $DARWINVERSION -x assembler $ASM';
  722. supported_targets : [system_aarch64_darwin];
  723. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  724. labelprefix : 'L';
  725. labelmaxlen : -1;
  726. comment : '# ';
  727. dollarsign: '$';
  728. );
  729. as_aarch64_clang_gas_info : tasminfo =
  730. (
  731. id : as_clang_gas;
  732. idtxt : 'CLANG';
  733. asmbin : 'clang';
  734. asmcmd : '-c -o $OBJ $EXTRAOPT -target $TARGET -x assembler $ASM';
  735. supported_targets : [system_aarch64_win64];
  736. flags : [af_needar,af_smartlink_sections,af_supports_dwarf];
  737. labelprefix : '.L';
  738. labelmaxlen : -1;
  739. comment : '// ';
  740. dollarsign: '$';
  741. );
  742. begin
  743. RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
  744. RegisterAssembler(as_aarch64_clang_darwin_info,TAArch64AppleAssembler);
  745. RegisterAssembler(as_aarch64_clang_gas_info,TAArch64ClangGASAssembler);
  746. end.