agcpugas.pas 30 KB

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