agcpugas.pas 32 KB

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