agcpugas.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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_mm_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. unwindrec : longword;
  234. begin
  235. if not assigned(list) then
  236. exit;
  237. lastsym:=nil;
  238. tmplist:=nil;
  239. sehlist:=nil;
  240. lastsec:=nil;
  241. instrcount:=0;
  242. datacount:=0;
  243. unwinddata:=nil;
  244. inhandlerdata:=false;
  245. inprologue:=false;
  246. handlerdata:=nil;
  247. handlerdataidx:=0;
  248. handlerdatacount:=nil;
  249. handlerflags:=0;
  250. handlername:='';
  251. hp:=tai(list.first);
  252. while assigned(hp) do
  253. begin
  254. deleteai:=false;
  255. case hp.typ of
  256. ait_section:
  257. begin
  258. if assigned(sehlist) then
  259. begin
  260. if assigned(lastsec) and (tai_section(hp).name^=lastsec.name^) then
  261. begin
  262. { this section was only added due to the now removed SEH data }
  263. deleteai:=true;
  264. dec(list.section_count);
  265. end
  266. else
  267. internalerror(2020041214);
  268. end
  269. else
  270. begin
  271. lastsec:=tai_section(hp);
  272. { also reset the last encountered symbol }
  273. lastsym:=nil;
  274. end;
  275. if assigned(tmplist) then
  276. begin
  277. list.insertListBefore(hp,tmplist);
  278. tmplist.free;
  279. tmplist:=nil;
  280. end;
  281. end;
  282. ait_symbol:
  283. begin
  284. if tai_symbol(hp).sym.typ=AT_FUNCTION then
  285. lastsym:=tai_symbol(hp);
  286. end;
  287. ait_instruction:
  288. if assigned(sehlist) then
  289. inc(instrcount);
  290. ait_const:
  291. if assigned(sehlist) then
  292. inc(datacount,tai_const(hp).size);
  293. ait_seh_directive:
  294. begin
  295. if not assigned(sehlist) and (seh.kind<>ash_proc) then
  296. internalerror(2020041208);
  297. { most seh directives are removed }
  298. deleteai:=true;
  299. case seh.kind of
  300. ash_proc:
  301. begin
  302. if not assigned(lastsec) then
  303. internalerror(2020041203);
  304. datacount:=0;
  305. instrcount:=0;
  306. handlerflags:=0;
  307. handlername:='';
  308. sehlist:=tasmlist.create;
  309. inprologue:=true;
  310. end;
  311. ash_endproc:
  312. begin
  313. if not assigned(sehlist) then
  314. internalerror(2020041501);
  315. if assigned(tmplist) then
  316. internalerror(2020041302);
  317. if not assigned(lastsym) then
  318. internalerror(2020041303);
  319. if inprologue then
  320. cgmessage(asmw_e_missing_endprologue);
  321. unwinddata:=convert_unwinddata(sehlist);
  322. writebyte($E4);
  323. { fill up with NOPs }
  324. while unwinddata.size mod 4<>0 do
  325. writebyte($E3);
  326. { note: we can pass Nil here, because in case of a LLVM
  327. backend this whole code shouldn't be required
  328. anyway }
  329. xdatasym:=current_asmdata.DefineAsmSymbol('xdata_'+lastsym.sym.name,AB_LOCAL,AT_DATA,nil);
  330. tmplist:=tasmlist.create;
  331. new_section(tmplist,sec_pdata,lastsec.name^,0);
  332. tmplist.concat(tai_const.Create_rva_sym(lastsym.sym));
  333. tmplist.concat(tai_const.Create_rva_sym(xdatasym));
  334. new_section(tmplist,sec_rodata,xdatasym.name,0);
  335. tmplist.concat(tai_symbol.Create(xdatasym,0));
  336. tmplist.concat(tai_comment.Create(strpnew('instr: '+tostr(instrcount)+', data: '+tostr(datacount)+', unwind: '+tostr(unwinddata.size))));
  337. {$ifdef EXTDEBUG}
  338. comment(V_Debug,'got section: '+lastsec.name^);
  339. comment(V_Debug,'got instructions: '+tostr(instrcount));
  340. comment(V_Debug,'got data: '+tostr(datacount));
  341. comment(V_Debug,'got unwinddata: '+tostr(unwinddata.size));
  342. {$endif EXTDEBUG}
  343. if datacount mod 4<>0 then
  344. cgmessage(asmw_e_seh_invalid_data_size);
  345. totalcount:=datacount div 4+instrcount;
  346. { splitting to multiple pdata/xdata sections is not yet
  347. supported, so 1 MB is our limit for now }
  348. if totalcount>(1 shl 18) then
  349. comment(V_Error,'Function is larger than 1 MB which is not supported for SEH currently');
  350. unwindrec:=min(totalcount,(1 shl 18)-1);
  351. if handlerflags<>0 then
  352. unwindrec:=unwindrec or (1 shl 20);
  353. { currently we only have one epilog, so E needs to be
  354. set to 1 and epilog scope index needs to be 0, no
  355. matter if we require the extension for the unwinddata
  356. or not }
  357. unwindrec:=unwindrec or (1 shl 21);
  358. if unwinddata.size div 4<=31 then
  359. unwindrec:=unwindrec or ((unwinddata.size div 4) shl 27);
  360. { exception record headers }
  361. tmplist.concat(tai_const.Create_32bit(longint(unwindrec)));
  362. if cs_asm_source in init_settings.globalswitches then
  363. tmplist.concat(tai_comment.create(strpnew(hexstr(unwindrec,8))));
  364. if unwinddata.size div 4>31 then
  365. begin
  366. { once we're able to split a .pdata entry this can be
  367. removed as well }
  368. if unwinddata.size div 4>255 then
  369. comment(V_Error,'Too many unwind codes for SEH');
  370. unwindrec:=(unwinddata.size div 4) shl 16;
  371. tmplist.concat(tai_const.create_32bit(longint(unwindrec)));
  372. if cs_asm_source in init_settings.globalswitches then
  373. tmplist.concat(tai_comment.create(strpnew(hexstr(unwindrec,8))));
  374. end;
  375. { unwind codes }
  376. unwinddata.seek(0);
  377. while unwinddata.pos<unwinddata.size do
  378. begin
  379. unwinddata.read(unwindrec,sizeof(longword));
  380. tmplist.concat(tai_const.Create_32bit(longint(unwindrec)));
  381. if cs_asm_source in init_settings.globalswitches then
  382. tmplist.concat(tai_comment.create(strpnew(hexstr(unwindrec,8))));
  383. end;
  384. unwinddata.free;
  385. if handlerflags<>0 then
  386. begin
  387. tmplist.concat(tai_const.Create_rva_sym(current_asmdata.RefAsmSymbol(handlername,AT_FUNCTION,false)));
  388. if length(handlerdata)>0 then
  389. begin
  390. tmplist.concat(handlerdatacount);
  391. for handlerdataidx:=0 to high(handlerdata) do
  392. tmplist.concat(handlerdata[handlerdataidx]);
  393. end;
  394. end;
  395. handlerdata:=nil;
  396. sehlist.free;
  397. sehlist:=nil;
  398. end;
  399. ash_endprologue:
  400. inprologue:=false;
  401. ash_handler:
  402. begin
  403. handlername:=seh.data.name^;
  404. handlerflags:=seh.data.flags;
  405. end;
  406. ash_handlerdata:
  407. begin
  408. if handlername='' then
  409. cgmessage(asmw_e_handlerdata_no_handler);
  410. hpdata:=tai(hp.next);
  411. if not assigned(hpdata) or (hpdata.typ<>ait_const) or (tai_const(hpdata).consttype<>aitconst_32bit) then
  412. internalerror(2020041215);
  413. handlerdatacount:=hpdata;
  414. setlength(handlerdata,tai_const(hpdata).value*4);
  415. handlerdataidx:=0;
  416. hpnext:=tai(hpdata.next);
  417. list.remove(hpdata);
  418. hpdata:=hpnext;
  419. while (handlerdataidx<length(handlerdata)) and assigned(hpdata) do
  420. begin
  421. if (hpdata.typ<>ait_const) or not (tai_const(hpdata).consttype in [aitconst_32bit,aitconst_rva_symbol]) then
  422. internalerror(2020041212);
  423. handlerdata[handlerdataidx]:=hpdata;
  424. inc(handlerdataidx);
  425. hpnext:=tai(hpdata.next);
  426. list.remove(hpdata);
  427. hpdata:=hpnext;
  428. end;
  429. if handlerdataidx<length(handlerdata) then
  430. internalerror(2020041213);
  431. end;
  432. ash_stackalloc,
  433. ash_addfp,
  434. ash_setfp,
  435. ash_nop,
  436. ash_savefplr,
  437. ash_savefplr_x,
  438. ash_savereg,
  439. ash_savereg_x,
  440. ash_saveregp,
  441. ash_saveregp_x,
  442. ash_savefreg,
  443. ash_savefreg_x,
  444. ash_savefregp,
  445. ash_savefregp_x:
  446. begin
  447. if not assigned(sehlist) then
  448. internalerror(2020041504);
  449. if not inprologue then
  450. internalerror(2020041505);
  451. hpdata:=hp;
  452. hp:=tai(hp.previous);
  453. list.Remove(hpdata);
  454. sehlist.concat(hpdata);
  455. { don't delete this }
  456. deleteai:=false;
  457. end;
  458. else
  459. internalerror(2020041206);
  460. end;
  461. end;
  462. else
  463. { ignore }
  464. ;
  465. end;
  466. if deleteai then
  467. begin
  468. hpnext:=tai(hp.next);
  469. list.remove(hp);
  470. hp.free;
  471. hp:=hpnext;
  472. end
  473. else
  474. hp:=tai(hp.next);
  475. end;
  476. if assigned(sehlist) then
  477. internalerror(2020041205);
  478. if assigned(tmplist) then
  479. begin
  480. list.concatlist(tmplist);
  481. tmplist.free;
  482. end;
  483. end;
  484. function TAArch64ClangGASAssembler.sectionflags(secflags:TSectionFlags):string;
  485. begin
  486. Result:=inherited sectionflags(secflags);
  487. if (target_info.system=system_aarch64_win64) then
  488. begin
  489. { we require an explicit "r" if write is not allowed }
  490. if not (SF_W in secflags) then
  491. result:=result+'r';
  492. end;
  493. end;
  494. procedure TAArch64ClangGASAssembler.WriteAsmList;
  495. begin
  496. { clang does not support all the directives we need, so we need to
  497. manually transform them to pdata/xdata records }
  498. if target_info.system=system_aarch64_win64 then
  499. begin
  500. TransformSEHDirectives(current_asmdata.AsmLists[al_pure_assembler]);
  501. TransformSEHDirectives(current_asmdata.AsmLists[al_procedures]);
  502. end;
  503. inherited WriteAsmList;
  504. end;
  505. {****************************************************************************}
  506. { Helper routines for Instruction Writer }
  507. {****************************************************************************}
  508. function getreferencestring(asminfo: pasminfo; var ref : treference) : string;
  509. const
  510. darwin_addrpage2str: array[addr_page..addr_gotpageoffset] of string[11] =
  511. ('@PAGE','@PAGEOFF','@GOTPAGE','@GOTPAGEOFF');
  512. linux_addrpage2str: array[addr_page..addr_gotpageoffset] of string[10] =
  513. ('',':lo12:',':got:',':got_lo12:');
  514. begin
  515. if ref.base=NR_NO then
  516. begin
  517. case ref.refaddr of
  518. addr_gotpage,
  519. addr_page,
  520. addr_gotpageoffset,
  521. addr_pageoffset:
  522. begin
  523. if not assigned(ref.symbol) or
  524. (ref.base<>NR_NO) or
  525. (ref.index<>NR_NO) or
  526. (ref.shiftmode<>SM_None) or
  527. (ref.offset<>0) then
  528. internalerror(2014121501);
  529. if target_info.system in systems_darwin then
  530. result:=ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  531. else
  532. result:=linux_addrpage2str[ref.refaddr]+ref.symbol.name
  533. end;
  534. addr_pic,
  535. { for locals replaced by temp symbols on LLVM }
  536. addr_no:
  537. result:=ref.symbol.name;
  538. else
  539. internalerror(2015022302);
  540. end
  541. end
  542. else
  543. begin
  544. result:='['+gas_regname(ref.base);
  545. if ref.addressmode=AM_POSTINDEXED then
  546. result:=result+']';
  547. if ref.index<>NR_NO then
  548. begin
  549. if (ref.offset<>0) or
  550. assigned(ref.symbol) then
  551. internalerror(2014121504);
  552. result:=result+', '+gas_regname(ref.index);
  553. case ref.shiftmode of
  554. SM_None: ;
  555. SM_LSL,
  556. SM_UXTW, SM_UXTX, SM_SXTW, SM_SXTX:
  557. begin
  558. result:=result+', '+gas_shiftmode2str[ref.shiftmode];
  559. if (ref.shiftmode=SM_LSL) or
  560. (ref.shiftimm<>0) then
  561. result:=result+' #'+tostr(ref.shiftimm);
  562. end
  563. else
  564. internalerror(2014121505);
  565. end;
  566. end
  567. else
  568. begin
  569. if assigned(ref.symbol) then
  570. begin
  571. case ref.refaddr of
  572. addr_gotpageoffset,
  573. addr_pageoffset:
  574. begin
  575. if target_info.system in systems_darwin then
  576. result:=result+', '+ref.symbol.name+darwin_addrpage2str[ref.refaddr]
  577. else
  578. result:=result+', '+linux_addrpage2str[ref.refaddr]+ref.symbol.name
  579. end
  580. else
  581. { todo: not yet generated/don't know syntax }
  582. internalerror(2014121506);
  583. end;
  584. end
  585. else
  586. begin
  587. if ref.refaddr<>addr_no then
  588. internalerror(2014121502);
  589. if (ref.offset<>0) then
  590. result:=result+', #'+tostr(ref.offset);
  591. end;
  592. end;
  593. case ref.addressmode of
  594. AM_OFFSET:
  595. result:=result+']';
  596. AM_PREINDEXED:
  597. result:=result+']!';
  598. else
  599. ;
  600. end;
  601. end;
  602. end;
  603. function getopstr(asminfo: pasminfo; hp: taicpu; opnr: longint; const o: toper): string;
  604. var
  605. i: longint;
  606. reg: tregister;
  607. begin
  608. case o.typ of
  609. top_reg:
  610. getopstr:=gas_regname(o.reg);
  611. top_shifterop:
  612. begin
  613. getopstr:=gas_shiftmode2str[o.shifterop^.shiftmode];
  614. if o.shifterop^.shiftimm<>0 then
  615. getopstr:=getopstr+' #'+tostr(o.shifterop^.shiftimm)
  616. end;
  617. top_const:
  618. if o.val>=0 then
  619. getopstr:='#'+tostr(o.val)
  620. else
  621. getopstr:='#0x'+hexStr(o.val,16);
  622. top_conditioncode:
  623. getopstr:=cond2str[o.cc];
  624. top_ref:
  625. if is_calljmp(hp.opcode) then
  626. begin
  627. if o.ref^.refaddr<>addr_full then
  628. internalerror(2014122220);
  629. if not assigned(o.ref^.symbol) or
  630. assigned(o.ref^.relsymbol) or
  631. (o.ref^.base<>NR_NO) or
  632. (o.ref^.index<>NR_NO) or
  633. (o.ref^.offset<>0) then
  634. internalerror(2014122221);
  635. getopstr:=o.ref^.symbol.name;
  636. end
  637. else
  638. getopstr:=getreferencestring(asminfo,o.ref^);
  639. top_realconst:
  640. begin
  641. str(o.val_real,Result);
  642. Result:='#'+Result;
  643. end;
  644. top_regset:
  645. begin
  646. reg:=o.basereg;
  647. result:='{'+gas_regname(reg);
  648. for i:=1 to o.nregs-1 do
  649. begin
  650. setsupreg(reg,succ(getsupreg(reg)) mod 32);
  651. result:=result+', '+gas_regname(reg);
  652. end;
  653. result:=result+'}';
  654. if o.regsetindex<>255 then
  655. result:=result+'['+tostr(o.regsetindex)+']'
  656. end;
  657. top_indexedreg:
  658. begin
  659. result:=gas_regname(o.indexedreg)+'['+tostr(o.regindex)+']';
  660. end;
  661. else
  662. internalerror(2014121507);
  663. end;
  664. end;
  665. procedure TAArch64InstrWriter.WriteInstruction(hp : tai);
  666. var
  667. op: TAsmOp;
  668. s: string;
  669. i: byte;
  670. sep: string[3];
  671. begin
  672. op:=taicpu(hp).opcode;
  673. s:=#9+gas_op2str[op]+oppostfix2str[taicpu(hp).oppostfix];
  674. if taicpu(hp).condition<>C_NONE then
  675. s:=s+'.'+cond2str[taicpu(hp).condition];
  676. if taicpu(hp).ops<>0 then
  677. begin
  678. sep:=#9;
  679. for i:=0 to taicpu(hp).ops-1 do
  680. begin
  681. // debug code
  682. // writeln(s);
  683. // writeln(taicpu(hp).fileinfo.line);
  684. s:=s+sep+getopstr(owner.asminfo,taicpu(hp),i,taicpu(hp).oper[i]^);
  685. sep:=',';
  686. end;
  687. end;
  688. owner.writer.AsmWriteLn(s);
  689. end;
  690. const
  691. as_aarch64_gas_info : tasminfo =
  692. (
  693. id : as_gas;
  694. idtxt : 'AS';
  695. asmbin : 'as';
  696. asmcmd : '-o $OBJ $EXTRAOPT $ASM';
  697. supported_targets : [system_aarch64_linux,system_aarch64_android];
  698. flags : [af_needar,af_smartlink_sections];
  699. labelprefix : '.L';
  700. labelmaxlen : -1;
  701. comment : '// ';
  702. dollarsign: '$';
  703. );
  704. as_aarch64_clang_darwin_info : tasminfo =
  705. (
  706. id : as_clang_asdarwin;
  707. idtxt : 'CLANG';
  708. asmbin : 'clang';
  709. asmcmd : '-x assembler -c -target $TRIPLET -o $OBJ $EXTRAOPT -x assembler $ASM';
  710. supported_targets : [system_aarch64_ios,system_aarch64_darwin];
  711. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_llvm];
  712. labelprefix : 'L';
  713. labelmaxlen : -1;
  714. comment : '# ';
  715. dollarsign: '$';
  716. );
  717. as_aarch64_clang_gas_info : tasminfo =
  718. (
  719. id : as_clang_gas;
  720. idtxt : 'CLANG';
  721. asmbin : 'clang';
  722. asmcmd : '-x assembler -c -target $TRIPLET -o $OBJ $EXTRAOPT -x assembler $ASM';
  723. supported_targets : [system_aarch64_win64];
  724. flags : [af_needar,af_smartlink_sections,af_supports_dwarf,af_llvm];
  725. labelprefix : '.L';
  726. labelmaxlen : -1;
  727. comment : '// ';
  728. dollarsign: '$';
  729. );
  730. begin
  731. RegisterAssembler(as_aarch64_gas_info,TAArch64Assembler);
  732. RegisterAssembler(as_aarch64_clang_darwin_info,TAArch64AppleAssembler);
  733. RegisterAssembler(as_aarch64_clang_gas_info,TAArch64ClangGASAssembler);
  734. end.