agcpugas.pas 30 KB

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