agllvm.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. {
  2. Copyright (c) 1998-2013 by the Free Pascal team
  3. This unit implements the generic part of the LLVM IR writer
  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. unit agllvm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,globals,systems,
  22. aasmbase,aasmtai,aasmdata,
  23. assemble,
  24. aasmllvm;
  25. type
  26. TLLVMInstrWriter = class;
  27. TLLVMModuleInlineAssemblyDecorator = class(IExternalAssemblerOutputFileDecorator)
  28. function LineFilter(const s: AnsiString): AnsiString;
  29. function LinePrefix: AnsiString;
  30. function LinePostfix: AnsiString;
  31. function LineEnding(const deflineending: ShortString): ShortString;
  32. end;
  33. TLLVMFunctionInlineAssemblyDecorator = class(IExternalAssemblerOutputFileDecorator)
  34. function LineFilter(const s: AnsiString): AnsiString;
  35. function LinePrefix: AnsiString;
  36. function LinePostfix: AnsiString;
  37. function LineEnding(const deflineending: ShortString): ShortString;
  38. end;
  39. TLLVMAssember=class(texternalassembler)
  40. protected
  41. ffuncinlasmdecorator: TLLVMFunctionInlineAssemblyDecorator;
  42. fdecllevel: longint;
  43. procedure WriteExtraHeader;virtual;
  44. procedure WriteExtraFooter;virtual;
  45. procedure WriteInstruction(hp: tai);
  46. procedure WriteLlvmInstruction(hp: tai);
  47. procedure WriteDirectiveName(dir: TAsmDirective); virtual;
  48. procedure WriteRealConst(hp: tai_realconst; do_line: boolean);
  49. procedure WriteOrdConst(hp: tai_const);
  50. procedure WriteTai(const replaceforbidden: boolean; const do_line: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
  51. public
  52. constructor CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean); override;
  53. function MakeCmdLine: TCmdStr; override;
  54. procedure WriteTree(p:TAsmList);override;
  55. procedure WriteAsmList;override;
  56. procedure WriteFunctionInlineAsmList(list: tasmlist);
  57. destructor destroy; override;
  58. protected
  59. InstrWriter: TLLVMInstrWriter;
  60. end;
  61. {# This is the base class for writing instructions.
  62. The WriteInstruction() method must be overridden
  63. to write a single instruction to the assembler
  64. file.
  65. }
  66. TLLVMInstrWriter = class
  67. constructor create(_owner: TLLVMAssember);
  68. procedure WriteInstruction(hp : tai);
  69. protected
  70. owner: TLLVMAssember;
  71. fstr: TSymStr;
  72. function getopcodestr(hp: taillvm): TSymStr;
  73. function getopstr(const o:toper; refwithalign: boolean) : TSymStr;
  74. procedure WriteAsmRegisterAllocationClobbers(list: tasmlist);
  75. end;
  76. implementation
  77. uses
  78. SysUtils,
  79. cutils,cclasses,cfileutl,
  80. fmodule,verbose,
  81. objcasm,
  82. aasmcnst,symconst,symdef,symtable,
  83. llvmbase,itllvm,llvmdef,
  84. cgbase,cgutils,cpubase,llvminfo;
  85. const
  86. line_length = 70;
  87. type
  88. {$ifdef cpuextended}
  89. t80bitarray = array[0..9] of byte;
  90. {$endif cpuextended}
  91. t64bitarray = array[0..7] of byte;
  92. t32bitarray = array[0..3] of byte;
  93. {****************************************************************************}
  94. { Support routines }
  95. {****************************************************************************}
  96. function single2str(d : single) : string;
  97. var
  98. hs : string;
  99. begin
  100. str(d,hs);
  101. { replace space with + }
  102. if hs[1]=' ' then
  103. hs[1]:='+';
  104. single2str:=hs
  105. end;
  106. function double2str(d : double) : string;
  107. var
  108. hs : string;
  109. begin
  110. str(d,hs);
  111. { replace space with + }
  112. if hs[1]=' ' then
  113. hs[1]:='+';
  114. double2str:=hs
  115. end;
  116. function extended2str(e : extended) : string;
  117. var
  118. hs : string;
  119. begin
  120. str(e,hs);
  121. { replace space with + }
  122. if hs[1]=' ' then
  123. hs[1]:='+';
  124. extended2str:=hs
  125. end;
  126. {****************************************************************************}
  127. { Decorator for module-level inline assembly }
  128. {****************************************************************************}
  129. function TLLVMModuleInlineAssemblyDecorator.LineFilter(const s: AnsiString): AnsiString;
  130. var
  131. i: longint;
  132. begin
  133. result:='';
  134. for i:=1 to length(s) do
  135. begin
  136. case s[i] of
  137. #0..#31,
  138. #127..#255,
  139. '"','\':
  140. result:=result+
  141. '\'+
  142. chr((ord(s[i]) shr 4)+ord('0'))+
  143. chr((ord(s[i]) and $f)+ord('0'));
  144. else
  145. result:=result+s[i];
  146. end;
  147. end;
  148. end;
  149. function TLLVMModuleInlineAssemblyDecorator.LinePrefix: AnsiString;
  150. begin
  151. result:='module asm "';
  152. end;
  153. function TLLVMModuleInlineAssemblyDecorator.LinePostfix: AnsiString;
  154. begin
  155. result:='"';
  156. end;
  157. function TLLVMModuleInlineAssemblyDecorator.LineEnding(const deflineending: ShortString): ShortString;
  158. begin
  159. result:=deflineending
  160. end;
  161. {****************************************************************************}
  162. { Decorator for function-level inline assembly }
  163. {****************************************************************************}
  164. function TLLVMFunctionInlineAssemblyDecorator.LineFilter(const s: AnsiString): AnsiString;
  165. var
  166. i: longint;
  167. begin
  168. result:='';
  169. for i:=1 to length(s) do
  170. begin
  171. case s[i] of
  172. { escape dollars }
  173. '$':
  174. result:=result+'$$';
  175. { ^ is used as placeholder for a single dollar (reference to
  176. argument to the inline assembly) }
  177. '^':
  178. result:=result+'$';
  179. #0..#31,
  180. #127..#255,
  181. '"','\':
  182. result:=result+
  183. '\'+
  184. chr((ord(s[i]) shr 4)+ord('0'))+
  185. chr((ord(s[i]) and $f)+ord('0'));
  186. else
  187. result:=result+s[i];
  188. end;
  189. end;
  190. end;
  191. function TLLVMFunctionInlineAssemblyDecorator.LinePrefix: AnsiString;
  192. begin
  193. result:='';
  194. end;
  195. function TLLVMFunctionInlineAssemblyDecorator.LinePostfix: AnsiString;
  196. begin
  197. result:='';
  198. end;
  199. function TLLVMFunctionInlineAssemblyDecorator.LineEnding(const deflineending: ShortString): ShortString;
  200. begin
  201. result:='\0A';
  202. end;
  203. {****************************************************************************}
  204. { LLVM Instruction writer }
  205. {****************************************************************************}
  206. function getregisterstring(reg: tregister): ansistring;
  207. begin
  208. if getregtype(reg)=R_TEMPREGISTER then
  209. result:='%tmp.'
  210. else
  211. result:='%reg.'+tostr(byte(getregtype(reg)))+'_';
  212. result:=result+tostr(getsupreg(reg));
  213. end;
  214. function getreferencealignstring(var ref: treference) : ansistring;
  215. begin
  216. result:=', align '+tostr(ref.alignment);
  217. end;
  218. function getreferencestring(var ref : treference; withalign: boolean) : ansistring;
  219. begin
  220. result:='';
  221. if assigned(ref.relsymbol) or
  222. (assigned(ref.symbol) and
  223. (ref.base<>NR_NO)) or
  224. (ref.index<>NR_NO) or
  225. (ref.offset<>0) then
  226. begin
  227. result:=' **(error ref: ';
  228. if assigned(ref.symbol) then
  229. result:=result+'sym='+ref.symbol.name+', ';
  230. if assigned(ref.relsymbol) then
  231. result:=result+'sym='+ref.relsymbol.name+', ';
  232. if ref.base=NR_NO then
  233. result:=result+'base=NR_NO, ';
  234. if ref.index<>NR_NO then
  235. result:=result+'index<>NR_NO, ';
  236. if ref.offset<>0 then
  237. result:=result+'offset='+tostr(ref.offset);
  238. result:=result+')**';
  239. internalerror(2013060225);
  240. end;
  241. if ref.base<>NR_NO then
  242. result:=result+getregisterstring(ref.base)
  243. else if assigned(ref.symbol) then
  244. result:=result+LlvmAsmSymName(ref.symbol)
  245. else
  246. result:=result+'null';
  247. if withalign then
  248. result:=result+getreferencealignstring(ref);
  249. end;
  250. function getparas(const paras: tfplist): ansistring;
  251. var
  252. i: longint;
  253. para: pllvmcallpara;
  254. begin
  255. result:='(';
  256. for i:=0 to paras.count-1 do
  257. begin
  258. if i<>0 then
  259. result:=result+', ';
  260. para:=pllvmcallpara(paras[i]);
  261. result:=result+llvmencodetypename(para^.def);
  262. if para^.valueext<>lve_none then
  263. result:=result+llvmvalueextension2str[para^.valueext];
  264. if para^.byval then
  265. result:=result+' byval';
  266. if para^.sret then
  267. result:=result+' sret';
  268. case para^.loc of
  269. LOC_REGISTER,
  270. LOC_FPUREGISTER,
  271. LOC_MMREGISTER:
  272. result:=result+' '+getregisterstring(para^.reg);
  273. LOC_CONSTANT:
  274. result:=result+' '+tostr(int64(para^.value));
  275. { empty records }
  276. LOC_VOID:
  277. result:=result+' undef';
  278. else
  279. internalerror(2014010801);
  280. end;
  281. end;
  282. result:=result+')';
  283. end;
  284. function llvmdoubletostr(const d: double): TSymStr;
  285. type
  286. tdoubleval = record
  287. case byte of
  288. 1: (d: double);
  289. 2: (i: int64);
  290. end;
  291. begin
  292. { "When using the hexadecimal form, constants of types half,
  293. float, and double are represented using the 16-digit form shown
  294. above (which matches the IEEE754 representation for double)"
  295. And always in big endian form (sign bit leftmost)
  296. }
  297. result:='0x'+hexstr(tdoubleval(d).i,16);
  298. end;
  299. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  300. function llvmextendedtostr(const e: extended): TSymStr;
  301. var
  302. extendedval: record
  303. case byte of
  304. 1: (e: extended);
  305. 2: (r: packed record
  306. {$ifdef FPC_LITTLE_ENDIAN}
  307. l: int64;
  308. h: word;
  309. {$else FPC_LITTLE_ENDIAN}
  310. h: int64;
  311. l: word;
  312. {$endif FPC_LITTLE_ENDIAN}
  313. end;
  314. );
  315. end;
  316. begin
  317. extendedval.e:=e;
  318. { hex format is always big endian in llvm }
  319. result:='0xK'+hexstr(extendedval.r.h,sizeof(extendedval.r.h)*2)+
  320. hexstr(extendedval.r.l,sizeof(extendedval.r.l)*2);
  321. end;
  322. {$endif cpuextended}
  323. function TLLVMInstrWriter.getopstr(const o:toper; refwithalign: boolean) : TSymStr;
  324. var
  325. hs : ansistring;
  326. hp: tai;
  327. tmpinline: cardinal;
  328. tmpasmblock: boolean;
  329. begin
  330. case o.typ of
  331. top_reg:
  332. getopstr:=getregisterstring(o.reg);
  333. top_const:
  334. getopstr:=tostr(int64(o.val));
  335. top_ref:
  336. if o.ref^.refaddr=addr_full then
  337. begin
  338. getopstr:='';
  339. if assigned(o.ref^.symbol) then
  340. getopstr:=LlvmAsmSymName(o.ref^.symbol)
  341. else
  342. getopstr:='null';
  343. if o.ref^.offset<>0 then
  344. internalerror(2013060223);
  345. end
  346. else
  347. getopstr:=getreferencestring(o.ref^,refwithalign);
  348. top_def:
  349. begin
  350. getopstr:=llvmencodetypename(o.def);
  351. end;
  352. top_cond:
  353. begin
  354. getopstr:=llvm_cond2str[o.cond];
  355. end;
  356. top_fpcond:
  357. begin
  358. getopstr:=llvm_fpcond2str[o.fpcond];
  359. end;
  360. top_single,
  361. top_double:
  362. begin
  363. { "When using the hexadecimal form, constants of types half,
  364. float, and double are represented using the 16-digit form shown
  365. above (which matches the IEEE754 representation for double)"
  366. And always in big endian form (sign bit leftmost)
  367. }
  368. if o.typ=top_double then
  369. result:=llvmdoubletostr(o.dval)
  370. else
  371. result:=llvmdoubletostr(o.sval)
  372. end;
  373. top_para:
  374. begin
  375. result:=getparas(o.paras);
  376. end;
  377. top_tai:
  378. begin
  379. if assigned(o.ai) then
  380. begin
  381. tmpinline:=1;
  382. tmpasmblock:=false;
  383. hp:=o.ai;
  384. owner.writer.AsmWrite(fstr);
  385. fstr:='';
  386. owner.WriteTai(false,false,tmpinline,tmpasmblock,hp);
  387. end;
  388. result:='';
  389. end;
  390. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  391. top_extended80:
  392. begin
  393. result:=llvmextendedtostr(o.eval);
  394. end;
  395. {$endif cpuextended}
  396. top_undef:
  397. result:='undef'
  398. else
  399. internalerror(2013060227);
  400. end;
  401. end;
  402. procedure TLLVMInstrWriter.WriteAsmRegisterAllocationClobbers(list: tasmlist);
  403. var
  404. hp: tai;
  405. begin
  406. hp:=tai(list.first);
  407. while assigned(hp) do
  408. begin
  409. if (hp.typ=ait_regalloc) and
  410. (tai_regalloc(hp).ratype=ra_alloc) then
  411. begin
  412. owner.writer.AsmWrite(',~{');
  413. owner.writer.AsmWrite(std_regname(tai_regalloc(hp).reg));
  414. owner.writer.AsmWrite('}');
  415. end;
  416. hp:=tai(hp.next);
  417. end;
  418. end;
  419. procedure TLLVMInstrWriter.WriteInstruction(hp: tai);
  420. var
  421. op: tllvmop;
  422. tmpstr,
  423. sep: TSymStr;
  424. i, opstart: longint;
  425. nested: boolean;
  426. opdone,
  427. done: boolean;
  428. begin
  429. op:=taillvm(hp).llvmopcode;
  430. { we write everything immediately rather than adding it into a string,
  431. because operands may contain other tai that will also write things out
  432. (and their output must come after everything that was processed in this
  433. instruction, such as its opcode or previous operands) }
  434. if owner.fdecllevel=0 then
  435. owner.writer.AsmWrite(#9);
  436. sep:=' ';
  437. opdone:=false;
  438. done:=false;
  439. opstart:=0;
  440. nested:=false;
  441. case op of
  442. la_type:
  443. begin
  444. owner.writer.AsmWrite(llvmtypeidentifier(taillvm(hp).oper[0]^.def));
  445. owner.writer.AsmWrite(' = type ');
  446. owner.writer.AsmWrite(llvmencodetypedecl(taillvm(hp).oper[0]^.def));
  447. done:=true;
  448. end;
  449. la_asmblock:
  450. begin
  451. owner.writer.AsmWrite('call void asm sideeffect "');
  452. owner.WriteFunctionInlineAsmList(taillvm(hp).oper[0]^.asmlist);
  453. owner.writer.AsmWrite('","');
  454. { we pass all accessed local variables as in/out address parameters,
  455. since we don't analyze the assembly code to determine what exactly
  456. happens to them; this is also compatible with the regular code
  457. generators, which always place local place local variables
  458. accessed from assembly code in memory }
  459. for i:=0 to taillvm(hp).oper[1]^.paras.Count-1 do
  460. begin
  461. owner.writer.AsmWrite('=*m,');
  462. end;
  463. owner.writer.AsmWrite('~{memory},~{fpsr},~{flags}');
  464. WriteAsmRegisterAllocationClobbers(taillvm(hp).oper[0]^.asmlist);
  465. owner.writer.AsmWrite('"');
  466. owner.writer.AsmWrite(getparas(taillvm(hp).oper[1]^.paras));
  467. done:=true;
  468. end;
  469. la_load,
  470. la_getelementptr:
  471. begin
  472. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  473. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  474. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
  475. else
  476. nested:=true;
  477. opstart:=1;
  478. if llvmflag_load_getelptr_type in llvmversion_properties[current_settings.llvmversion] then
  479. begin
  480. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  481. opdone:=true;
  482. if nested then
  483. owner.writer.AsmWrite(' (')
  484. else
  485. owner.writer.AsmWrite(' ');
  486. { can't just dereference the type, because it may be an
  487. implicit pointer type such as a class -> resort to string
  488. manipulation... Not very clean :( }
  489. tmpstr:=llvmencodetypename(taillvm(hp).spilling_get_reg_type(0));
  490. if op=la_getelementptr then
  491. begin
  492. if tmpstr[length(tmpstr)]<>'*' then
  493. begin
  494. writeln(tmpstr);
  495. internalerror(2016071101);
  496. end
  497. else
  498. setlength(tmpstr,length(tmpstr)-1);
  499. end;
  500. owner.writer.AsmWrite(tmpstr);
  501. owner.writer.AsmWrite(',');
  502. end
  503. end;
  504. la_ret, la_br, la_switch, la_indirectbr,
  505. la_resume,
  506. la_unreachable,
  507. la_store,
  508. la_fence,
  509. la_cmpxchg,
  510. la_atomicrmw,
  511. la_catch,
  512. la_filter:
  513. begin
  514. { instructions that never have a result }
  515. end;
  516. la_call,
  517. la_invoke:
  518. begin
  519. if taillvm(hp).oper[1]^.reg<>NR_NO then
  520. owner.writer.AsmWrite(getregisterstring(taillvm(hp).oper[1]^.reg)+' = ');
  521. opstart:=2;
  522. if llvmflag_call_no_ptr in llvmversion_properties[current_settings.llvmversion] then
  523. begin
  524. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  525. opdone:=true;
  526. tmpstr:=llvmencodetypename(taillvm(hp).oper[2]^.def);
  527. if tmpstr[length(tmpstr)]<>'*' then
  528. begin
  529. writeln(tmpstr);
  530. internalerror(2016071102);
  531. end
  532. else
  533. setlength(tmpstr,length(tmpstr)-1);
  534. owner.writer.AsmWrite(tmpstr);
  535. opstart:=3;
  536. end;
  537. end;
  538. la_blockaddress:
  539. begin
  540. owner.writer.AsmWrite('i8* blockaddress(');
  541. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false));
  542. { getopstr would add a "label" qualifier, which blockaddress does
  543. not want }
  544. owner.writer.AsmWrite(',%');
  545. with taillvm(hp).oper[1]^ do
  546. begin
  547. if (typ<>top_ref) or
  548. (ref^.refaddr<>addr_full) then
  549. internalerror(2016112001);
  550. owner.writer.AsmWrite(ref^.symbol.name);
  551. end;
  552. nested:=true;
  553. done:=true;
  554. end;
  555. la_alloca:
  556. begin
  557. owner.writer.AsmWrite(getreferencestring(taillvm(hp).oper[0]^.ref^,false)+' = ');
  558. sep:=' ';
  559. opstart:=1;
  560. end;
  561. la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
  562. la_fptoui, la_fptosi, la_uitofp, la_sitofp,
  563. la_ptrtoint, la_inttoptr,
  564. la_bitcast:
  565. begin
  566. { destination can be empty in case of nested constructs, or
  567. data initialisers }
  568. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  569. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  570. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
  571. else
  572. nested:=true;
  573. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  574. if not nested then
  575. owner.writer.AsmWrite(' ')
  576. else
  577. owner.writer.AsmWrite(' (');
  578. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[1]^,false));
  579. { if there's a tai operand, its def is used instead of an
  580. explicit def operand }
  581. if taillvm(hp).ops=4 then
  582. begin
  583. owner.writer.AsmWrite(' ');
  584. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[2]^,false));
  585. opstart:=3;
  586. end
  587. else
  588. opstart:=2;
  589. owner.writer.AsmWrite(' to ');
  590. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[opstart]^,false));
  591. done:=true;
  592. end
  593. else
  594. begin
  595. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  596. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  597. begin
  598. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,true)+' = ');
  599. end
  600. else
  601. nested:=true;
  602. sep:=' ';
  603. opstart:=1
  604. end;
  605. end;
  606. { process operands }
  607. if not done then
  608. begin
  609. if not opdone then
  610. begin
  611. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  612. if nested then
  613. owner.writer.AsmWrite(' (');
  614. end;
  615. if taillvm(hp).ops<>0 then
  616. begin
  617. for i:=opstart to taillvm(hp).ops-1 do
  618. begin
  619. owner.writer.AsmWrite(sep);
  620. { special invoke interjections: "to label X unwind label Y" }
  621. if (op=la_invoke) then
  622. case i of
  623. 5: owner.writer.AsmWrite('to ');
  624. 6: owner.writer.AsmWrite('unwind ');
  625. end;
  626. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[i]^,op in [la_load,la_store]));
  627. if (taillvm(hp).oper[i]^.typ in [top_def,top_cond,top_fpcond]) or
  628. (op in [la_call,la_invoke,la_landingpad,la_catch,la_filter]) then
  629. sep :=' '
  630. else
  631. sep:=', ';
  632. end;
  633. end;
  634. end;
  635. if op=la_alloca then
  636. owner.writer.AsmWrite(getreferencealignstring(taillvm(hp).oper[0]^.ref^));
  637. if nested then
  638. owner.writer.AsmWrite(')')
  639. else if owner.fdecllevel=0 then
  640. owner.writer.AsmLn;
  641. end;
  642. function TLLVMInstrWriter.getopcodestr(hp: taillvm): TSymStr;
  643. begin
  644. result:=llvm_op2str[hp.llvmopcode];
  645. case hp.llvmopcode of
  646. la_load:
  647. begin
  648. if vol_read in hp.oper[2]^.ref^.volatility then
  649. result:=result+' volatile';
  650. end;
  651. la_store:
  652. begin
  653. if vol_write in hp.oper[3]^.ref^.volatility then
  654. result:=result+' volatile';
  655. end;
  656. end;
  657. end;
  658. {****************************************************************************}
  659. { LLVM Assembler writer }
  660. {****************************************************************************}
  661. destructor TLLVMAssember.Destroy;
  662. begin
  663. InstrWriter.free;
  664. ffuncinlasmdecorator.free;
  665. inherited destroy;
  666. end;
  667. function TLLVMAssember.MakeCmdLine: TCmdStr;
  668. var
  669. optstr: TCmdStr;
  670. begin
  671. result := inherited MakeCmdLine;
  672. { standard optimization flags for llc -- todo: this needs to be split
  673. into a call to opt and one to llc }
  674. if cs_opt_level3 in current_settings.optimizerswitches then
  675. optstr:='-O3'
  676. else if cs_opt_level2 in current_settings.optimizerswitches then
  677. optstr:='-O2'
  678. else if cs_opt_level1 in current_settings.optimizerswitches then
  679. optstr:='-O1'
  680. else
  681. optstr:='-O0';
  682. { stack frame elimination }
  683. if not(cs_opt_stackframe in current_settings.optimizerswitches) then
  684. optstr:=optstr+' -disable-fp-elim';
  685. { fast math }
  686. if cs_opt_fastmath in current_settings.optimizerswitches then
  687. optstr:=optstr+' -enable-unsafe-fp-math -enable-fp-mad -fp-contract=fast';
  688. { smart linking }
  689. if cs_create_smart in current_settings.moduleswitches then
  690. optstr:=optstr+' -data-sections -function-sections';
  691. { pic }
  692. if cs_create_pic in current_settings.moduleswitches then
  693. optstr:=optstr+' -relocation-model=pic'
  694. else if not(target_info.system in systems_darwin) then
  695. optstr:=optstr+' -relocation-model=static'
  696. else
  697. optstr:=optstr+' -relocation-model=dynamic-no-pic';
  698. { our stack alignment is non-standard on some targets. The following
  699. parameter is however ignored on some targets by llvm, so it may not
  700. be enough }
  701. optstr:=optstr+' -stack-alignment='+tostr(target_info.stackalign*8);
  702. { force object output instead of textual assembler code }
  703. optstr:=optstr+' -filetype=obj';
  704. replace(result,'$OPT',optstr);
  705. end;
  706. procedure TLLVMAssember.WriteTree(p:TAsmList);
  707. var
  708. hp : tai;
  709. InlineLevel : cardinal;
  710. asmblock: boolean;
  711. do_line : boolean;
  712. replaceforbidden: boolean;
  713. begin
  714. if not assigned(p) then
  715. exit;
  716. replaceforbidden:=asminfo^.dollarsign<>'$';
  717. InlineLevel:=0;
  718. asmblock:=false;
  719. { lineinfo is only needed for al_procedures (PFV) }
  720. do_line:=(cs_asm_source in current_settings.globalswitches) or
  721. ((cs_lineinfo in current_settings.moduleswitches)
  722. and (p=current_asmdata.asmlists[al_procedures]));
  723. hp:=tai(p.first);
  724. while assigned(hp) do
  725. begin
  726. prefetch(pointer(hp.next)^);
  727. if not(hp.typ in SkipLineInfo) then
  728. begin
  729. current_filepos:=tailineinfo(hp).fileinfo;
  730. { no line info for inlined code }
  731. if do_line and (inlinelevel=0) then
  732. WriteSourceLine(hp as tailineinfo);
  733. end;
  734. WriteTai(replaceforbidden, do_line, InlineLevel, asmblock, hp);
  735. hp:=tai(hp.next);
  736. end;
  737. end;
  738. procedure TLLVMAssember.WriteExtraHeader;
  739. begin
  740. writer.AsmWrite('target datalayout = "');
  741. writer.AsmWrite(target_info.llvmdatalayout);
  742. writer.AsmWriteln('"');
  743. writer.AsmWrite('target triple = "');
  744. writer.AsmWrite(llvm_target_name);
  745. writer.AsmWriteln('"');
  746. end;
  747. procedure TLLVMAssember.WriteExtraFooter;
  748. begin
  749. end;
  750. procedure TLLVMAssember.WriteInstruction(hp: tai);
  751. begin
  752. end;
  753. procedure TLLVMAssember.WriteLlvmInstruction(hp: tai);
  754. begin
  755. InstrWriter.WriteInstruction(hp);
  756. end;
  757. procedure TLLVMAssember.WriteRealConst(hp: tai_realconst; do_line: boolean);
  758. begin
  759. if fdecllevel=0 then
  760. begin
  761. case tai_realconst(hp).realtyp of
  762. aitrealconst_s32bit:
  763. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  764. aitrealconst_s64bit:
  765. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  766. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  767. { can't write full 80 bit floating point constants yet on non-x86 }
  768. aitrealconst_s80bit:
  769. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  770. {$endif cpuextended}
  771. aitrealconst_s64comp:
  772. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  773. else
  774. internalerror(2014050604);
  775. end;
  776. internalerror(2016120202);
  777. end;
  778. case hp.realtyp of
  779. aitrealconst_s32bit:
  780. writer.AsmWrite(llvmdoubletostr(hp.value.s32val));
  781. aitrealconst_s64bit:
  782. writer.AsmWriteln(llvmdoubletostr(hp.value.s64val));
  783. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  784. aitrealconst_s80bit:
  785. writer.AsmWrite(llvmextendedtostr(hp.value.s80val));
  786. {$endif defined(cpuextended)}
  787. aitrealconst_s64comp:
  788. { handled as int64 most of the time in llvm }
  789. writer.AsmWrite(tostr(round(hp.value.s64compval)));
  790. else
  791. internalerror(2014062401);
  792. end;
  793. end;
  794. procedure TLLVMAssember.WriteOrdConst(hp: tai_const);
  795. var
  796. consttyp: taiconst_type;
  797. begin
  798. if fdecllevel=0 then
  799. internalerror(2016120203);
  800. consttyp:=hp.consttype;
  801. case consttyp of
  802. aitconst_got,
  803. aitconst_gotoff_symbol,
  804. aitconst_uleb128bit,
  805. aitconst_sleb128bit,
  806. aitconst_rva_symbol,
  807. aitconst_secrel32_symbol,
  808. aitconst_darwin_dwarf_delta32,
  809. aitconst_darwin_dwarf_delta64,
  810. aitconst_half16bit,
  811. aitconst_gs:
  812. internalerror(2014052901);
  813. aitconst_128bit,
  814. aitconst_64bit,
  815. aitconst_32bit,
  816. aitconst_16bit,
  817. aitconst_8bit,
  818. aitconst_16bit_unaligned,
  819. aitconst_32bit_unaligned,
  820. aitconst_64bit_unaligned:
  821. begin
  822. if fdecllevel=0 then
  823. writer.AsmWrite(asminfo^.comment);
  824. { can't have compile-time differences between symbols; these are
  825. normally for PIC, but llvm takes care of that for us }
  826. if assigned(hp.endsym) then
  827. internalerror(2014052902);
  828. if assigned(hp.sym) then
  829. begin
  830. writer.AsmWrite(LlvmAsmSymName(hp.sym));
  831. { can't have offsets }
  832. if hp.value<>0 then
  833. if fdecllevel<>0 then
  834. internalerror(2014052903)
  835. else
  836. writer.AsmWrite(' -- symbol offset: ' + tostr(hp.value));
  837. end
  838. else if hp.value=0 then
  839. writer.AsmWrite('zeroinitializer')
  840. else
  841. writer.AsmWrite(tostr(hp.value));
  842. {
  843. // activate in case of debugging IE 2016120203
  844. if fdecllevel=0 then
  845. writer.AsmLn;
  846. }
  847. end;
  848. else
  849. internalerror(200704251);
  850. end;
  851. end;
  852. procedure TLLVMAssember.WriteTai(const replaceforbidden: boolean; const do_line: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
  853. procedure WriteLinkageVibilityFlags(bind: TAsmSymBind);
  854. begin
  855. case bind of
  856. AB_EXTERNAL,
  857. AB_EXTERNAL_INDIRECT:
  858. writer.AsmWrite(' external');
  859. AB_COMMON:
  860. writer.AsmWrite(' common');
  861. AB_LOCAL:
  862. writer.AsmWrite(' internal');
  863. AB_GLOBAL,
  864. AB_INDIRECT:
  865. ;
  866. AB_WEAK_EXTERNAL:
  867. writer.AsmWrite(' extern_weak');
  868. AB_PRIVATE_EXTERN:
  869. begin
  870. if not(llvmflag_linker_private in llvmversion_properties[current_settings.llvmversion]) then
  871. writer.AsmWrite(' hidden')
  872. else
  873. writer.AsmWrite(' linker_private');
  874. end
  875. else
  876. internalerror(2014020104);
  877. end;
  878. end;
  879. procedure WriteFunctionFlags(pd: tprocdef);
  880. begin
  881. if (pos('FPC_SETJMP',upper(pd.mangledname))<>0) or
  882. (pd.mangledname=(target_info.cprefix+'setjmp')) then
  883. writer.AsmWrite(' returns_twice');
  884. if po_inline in pd.procoptions then
  885. writer.AsmWrite(' inlinehint');
  886. { ensure that functions that happen to have the same name as a
  887. standard C library function, but which are implemented in Pascal,
  888. are not considered to have the same semantics as the C function with
  889. the same name }
  890. if not(po_external in pd.procoptions) then
  891. writer.AsmWrite(' nobuiltin');
  892. if po_noreturn in pd.procoptions then
  893. writer.AsmWrite(' noreturn');
  894. end;
  895. procedure WriteTypedConstData(hp: tai_abstracttypedconst);
  896. var
  897. p: tai_abstracttypedconst;
  898. pval: tai;
  899. defstr: TSymStr;
  900. first, gotstring: boolean;
  901. begin
  902. defstr:=llvmencodetypename(hp.def);
  903. { write the struct, array or simple type }
  904. case hp.adetyp of
  905. tck_record:
  906. begin
  907. writer.AsmWrite(defstr);
  908. writer.AsmWrite(' <{');
  909. first:=true;
  910. for p in tai_aggregatetypedconst(hp) do
  911. begin
  912. if not first then
  913. writer.AsmWrite(', ')
  914. else
  915. first:=false;
  916. WriteTypedConstData(p);
  917. end;
  918. writer.AsmWrite('}>');
  919. end;
  920. tck_array:
  921. begin
  922. writer.AsmWrite(defstr);
  923. first:=true;
  924. gotstring:=false;
  925. for p in tai_aggregatetypedconst(hp) do
  926. begin
  927. if not first then
  928. writer.AsmWrite(',')
  929. else
  930. begin
  931. writer.AsmWrite(' ');
  932. if (tai_abstracttypedconst(p).adetyp=tck_simple) and
  933. (tai_simpletypedconst(p).val.typ=ait_string) then
  934. begin
  935. gotstring:=true;
  936. end
  937. else
  938. begin
  939. writer.AsmWrite('[');
  940. end;
  941. first:=false;
  942. end;
  943. { cannot concat strings and other things }
  944. if gotstring and
  945. ((tai_abstracttypedconst(p).adetyp<>tck_simple) or
  946. (tai_simpletypedconst(p).val.typ<>ait_string)) then
  947. internalerror(2014062701);
  948. WriteTypedConstData(p);
  949. end;
  950. if not gotstring then
  951. writer.AsmWrite(']');
  952. end;
  953. tck_simple:
  954. begin
  955. pval:=tai_simpletypedconst(hp).val;
  956. if pval.typ<>ait_string then
  957. begin
  958. writer.AsmWrite(defstr);
  959. writer.AsmWrite(' ');
  960. end;
  961. WriteTai(replaceforbidden,do_line,InlineLevel,asmblock,pval);
  962. end;
  963. end;
  964. end;
  965. var
  966. hp2: tai;
  967. s: string;
  968. sstr: TSymStr;
  969. i: longint;
  970. ch: ansichar;
  971. begin
  972. case hp.typ of
  973. ait_comment :
  974. begin
  975. writer.AsmWrite(asminfo^.comment);
  976. writer.AsmWritePChar(tai_comment(hp).str);
  977. if fdecllevel<>0 then
  978. internalerror(2015090601);
  979. writer.AsmLn;
  980. end;
  981. ait_regalloc :
  982. begin
  983. if (cs_asm_regalloc in current_settings.globalswitches) then
  984. begin
  985. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  986. repeat
  987. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  988. if (hp.next=nil) or
  989. (tai(hp.next).typ<>ait_regalloc) or
  990. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  991. break;
  992. hp:=tai(hp.next);
  993. writer.AsmWrite(',');
  994. until false;
  995. writer.AsmWrite(' ');
  996. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  997. end;
  998. end;
  999. ait_tempalloc :
  1000. begin
  1001. if (cs_asm_tempalloc in current_settings.globalswitches) then
  1002. WriteTempalloc(tai_tempalloc(hp));
  1003. end;
  1004. ait_align,
  1005. ait_section :
  1006. begin
  1007. { ignore, specified as part of declarations -- don't write
  1008. comment, because could appear in the middle of an aggregate
  1009. constant definition }
  1010. end;
  1011. ait_datablock :
  1012. begin
  1013. writer.AsmWrite(asminfo^.comment);
  1014. writer.AsmWriteln('datablock');
  1015. end;
  1016. ait_const:
  1017. begin
  1018. WriteOrdConst(tai_const(hp));
  1019. end;
  1020. ait_realconst :
  1021. begin
  1022. WriteRealConst(tai_realconst(hp), do_line);
  1023. end;
  1024. ait_string :
  1025. begin
  1026. if fdecllevel=0 then
  1027. internalerror(2016120201);
  1028. writer.AsmWrite('c"');
  1029. for i:=1 to tai_string(hp).len do
  1030. begin
  1031. ch:=tai_string(hp).str[i-1];
  1032. case ch of
  1033. #0, {This can't be done by range, because a bug in FPC}
  1034. #1..#31,
  1035. #128..#255,
  1036. '"',
  1037. '\' : s:='\'+hexStr(ord(ch),2);
  1038. else
  1039. s:=ch;
  1040. end;
  1041. writer.AsmWrite(s);
  1042. end;
  1043. writer.AsmWrite('"');
  1044. end;
  1045. ait_label :
  1046. begin
  1047. if not asmblock and
  1048. (tai_label(hp).labsym.is_used) then
  1049. begin
  1050. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1051. begin
  1052. { should be emitted as part of the variable/function def }
  1053. internalerror(2013010703);
  1054. end;
  1055. if tai_label(hp).labsym.bind in [AB_GLOBAL, AB_PRIVATE_EXTERN] then
  1056. begin
  1057. { should be emitted as part of the variable/function def }
  1058. //internalerror(2013010704);
  1059. writer.AsmWriteln(asminfo^.comment+'global/privateextern label: '+tai_label(hp).labsym.name);
  1060. end;
  1061. if replaceforbidden then
  1062. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1063. else
  1064. writer.AsmWrite(tai_label(hp).labsym.name);
  1065. writer.AsmWriteLn(':');
  1066. end;
  1067. end;
  1068. ait_symbol :
  1069. begin
  1070. if fdecllevel=0 then
  1071. writer.AsmWrite(asminfo^.comment);
  1072. writer.AsmWriteln(LlvmAsmSymName(tai_symbol(hp).sym));
  1073. { todo }
  1074. if tai_symbol(hp).has_value then
  1075. internalerror(2014062402);
  1076. end;
  1077. ait_llvmdecl:
  1078. begin
  1079. if taillvmdecl(hp).def.typ=procdef then
  1080. begin
  1081. if not(ldf_definition in taillvmdecl(hp).flags) then
  1082. begin
  1083. writer.AsmWrite('declare');
  1084. writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), taillvmdecl(hp).namesym.name, lpd_decl));
  1085. WriteFunctionFlags(tprocdef(taillvmdecl(hp).def));
  1086. writer.AsmLn;
  1087. end
  1088. else
  1089. begin
  1090. writer.AsmWrite('define');
  1091. if ldf_weak in taillvmdecl(hp).flags then
  1092. writer.AsmWrite(' weak');
  1093. WriteLinkageVibilityFlags(taillvmdecl(hp).namesym.bind);
  1094. writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), '', lpd_def));
  1095. WriteFunctionFlags(tprocdef(taillvmdecl(hp).def));
  1096. if assigned(tprocdef(taillvmdecl(hp).def).personality) then
  1097. begin
  1098. writer.AsmWrite(' personality i8* bitcast (');
  1099. writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def).personality, '', lpd_procvar));
  1100. writer.AsmWrite('* ');
  1101. writer.AsmWrite(llvmmangledname(tprocdef(taillvmdecl(hp).def).personality.mangledname));
  1102. writer.AsmWrite(' to i8*)');
  1103. end;
  1104. writer.AsmWriteln(' {');
  1105. end;
  1106. end
  1107. else
  1108. begin
  1109. writer.AsmWrite(LlvmAsmSymName(taillvmdecl(hp).namesym));
  1110. writer.AsmWrite(' =');
  1111. if ldf_weak in taillvmdecl(hp).flags then
  1112. writer.AsmWrite(' weak');
  1113. if ldf_appending in taillvmdecl(hp).flags then
  1114. writer.AsmWrite(' appending');
  1115. WriteLinkageVibilityFlags(taillvmdecl(hp).namesym.bind);
  1116. writer.AsmWrite(' ');
  1117. if (ldf_tls in taillvmdecl(hp).flags) then
  1118. writer.AsmWrite('thread_local ');
  1119. if ldf_unnamed_addr in taillvmdecl(hp).flags then
  1120. writer.AsmWrite('unnamed_addr ');
  1121. if taillvmdecl(hp).sec in [sec_rodata,sec_rodata_norel] then
  1122. writer.AsmWrite('constant ')
  1123. else
  1124. writer.AsmWrite('global ');
  1125. if not assigned(taillvmdecl(hp).initdata) then
  1126. begin
  1127. writer.AsmWrite(llvmencodetypename(taillvmdecl(hp).def));
  1128. if not(taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL,AB_EXTERNAL_INDIRECT]) then
  1129. writer.AsmWrite(' zeroinitializer');
  1130. end
  1131. else
  1132. begin
  1133. inc(fdecllevel);
  1134. { can't have an external symbol with initialisation data }
  1135. if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
  1136. internalerror(2014052905);
  1137. { bitcast initialisation data to the type of the constant }
  1138. { write initialisation data }
  1139. hp2:=tai(taillvmdecl(hp).initdata.first);
  1140. while assigned(hp2) do
  1141. begin
  1142. WriteTai(replaceforbidden,do_line,InlineLevel,asmblock,hp2);
  1143. hp2:=tai(hp2.next);
  1144. end;
  1145. dec(fdecllevel);
  1146. end;
  1147. { custom section name? }
  1148. case taillvmdecl(hp).sec of
  1149. sec_user:
  1150. begin
  1151. writer.AsmWrite(', section "');
  1152. writer.AsmWrite(taillvmdecl(hp).secname);
  1153. writer.AsmWrite('"');
  1154. end;
  1155. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1156. begin
  1157. writer.AsmWrite(', section "');
  1158. writer.AsmWrite(objc_section_name(taillvmdecl(hp).sec));
  1159. writer.AsmWrite('"');
  1160. end;
  1161. end;
  1162. { sections whose name starts with 'llvm.' are for LLVM
  1163. internal use and don't have an alignment }
  1164. if pos('llvm.',taillvmdecl(hp).secname)<>1 then
  1165. begin
  1166. { alignment }
  1167. writer.AsmWrite(', align ');
  1168. writer.AsmWriteln(tostr(taillvmdecl(hp).alignment));
  1169. end
  1170. else
  1171. writer.AsmLn;
  1172. end;
  1173. end;
  1174. ait_llvmalias:
  1175. begin
  1176. writer.AsmWrite(LlvmAsmSymName(taillvmalias(hp).newsym));
  1177. writer.AsmWrite(' = alias ');
  1178. WriteLinkageVibilityFlags(taillvmalias(hp).bind);
  1179. if taillvmalias(hp).def.typ=procdef then
  1180. sstr:=llvmencodeproctype(tabstractprocdef(taillvmalias(hp).def), '', lpd_alias)
  1181. else
  1182. sstr:=llvmencodetypename(taillvmalias(hp).def);
  1183. writer.AsmWrite(sstr);
  1184. if llvmflag_alias_double_type in llvmversion_properties[current_settings.llvmversion] then
  1185. begin
  1186. writer.AsmWrite(', ');
  1187. writer.AsmWrite(sstr);
  1188. end;
  1189. writer.AsmWrite('* ');
  1190. writer.AsmWriteln(LlvmAsmSymName(taillvmalias(hp).oldsym));
  1191. end;
  1192. ait_symbolpair:
  1193. begin
  1194. { should be emitted as part of the symbol def }
  1195. internalerror(2013010708);
  1196. end;
  1197. ait_symbol_end :
  1198. begin
  1199. if tai_symbol_end(hp).sym.typ=AT_FUNCTION then
  1200. writer.AsmWriteln('}')
  1201. else
  1202. writer.AsmWriteln('; ait_symbol_end error, should not be generated');
  1203. // internalerror(2013010711);
  1204. end;
  1205. ait_instruction :
  1206. begin
  1207. WriteInstruction(hp);
  1208. end;
  1209. ait_llvmins:
  1210. begin
  1211. WriteLlvmInstruction(hp);
  1212. end;
  1213. ait_stab :
  1214. begin
  1215. internalerror(2013010712);
  1216. end;
  1217. ait_force_line,
  1218. ait_function_name :
  1219. ;
  1220. ait_cutobject :
  1221. begin
  1222. end;
  1223. ait_marker :
  1224. case
  1225. tai_marker(hp).kind of
  1226. mark_NoLineInfoStart:
  1227. inc(InlineLevel);
  1228. mark_NoLineInfoEnd:
  1229. dec(InlineLevel);
  1230. { these cannot be nested }
  1231. mark_AsmBlockStart:
  1232. asmblock:=true;
  1233. mark_AsmBlockEnd:
  1234. asmblock:=false;
  1235. end;
  1236. ait_directive :
  1237. begin
  1238. { CPU directive is commented out for the LLVM }
  1239. if tai_directive(hp).directive=asd_cpu then
  1240. writer.AsmWrite(asminfo^.comment);
  1241. WriteDirectiveName(tai_directive(hp).directive);
  1242. if tai_directive(hp).name <>'' then
  1243. writer.AsmWrite(tai_directive(hp).name);
  1244. if fdecllevel<>0 then
  1245. internalerror(2015090602);
  1246. writer.AsmLn;
  1247. end;
  1248. ait_seh_directive :
  1249. begin
  1250. internalerror(2013010713);
  1251. end;
  1252. ait_varloc:
  1253. begin
  1254. if tai_varloc(hp).newlocationhi<>NR_NO then
  1255. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1256. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  1257. else
  1258. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1259. std_regname(tai_varloc(hp).newlocation)));
  1260. if fdecllevel<>0 then
  1261. internalerror(2015090603);
  1262. writer.AsmLn;
  1263. end;
  1264. ait_typedconst:
  1265. begin
  1266. WriteTypedConstData(tai_abstracttypedconst(hp));
  1267. end
  1268. else
  1269. internalerror(2006012201);
  1270. end;
  1271. end;
  1272. constructor TLLVMAssember.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  1273. begin
  1274. inherited;
  1275. InstrWriter:=TLLVMInstrWriter.create(self);
  1276. end;
  1277. procedure TLLVMAssember.WriteDirectiveName(dir: TAsmDirective);
  1278. begin
  1279. writer.AsmWrite('.'+directivestr[dir]+' ');
  1280. end;
  1281. procedure TLLVMAssember.WriteAsmList;
  1282. var
  1283. hal : tasmlisttype;
  1284. i: longint;
  1285. a: TExternalAssembler;
  1286. decorator: TLLVMModuleInlineAssemblyDecorator;
  1287. begin
  1288. WriteExtraHeader;
  1289. for hal:=low(TasmlistType) to high(TasmlistType) do
  1290. begin
  1291. if not assigned(current_asmdata.asmlists[hal]) or
  1292. current_asmdata.asmlists[hal].Empty then
  1293. continue;
  1294. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1295. if hal<>al_pure_assembler then
  1296. writetree(current_asmdata.asmlists[hal])
  1297. else
  1298. begin
  1299. { write routines using the target-specific external assembler
  1300. writer, filtered using the LLVM module-level assembly
  1301. decorator }
  1302. decorator:=TLLVMModuleInlineAssemblyDecorator.Create;
  1303. writer.decorator:=decorator;
  1304. a:=GetExternalGnuAssemblerWithAsmInfoWriter(asminfo,writer);
  1305. a.WriteTree(current_asmdata.asmlists[hal]);
  1306. writer.decorator:=nil;
  1307. decorator.free;
  1308. a.free;
  1309. end;
  1310. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1311. end;
  1312. writer.AsmLn;
  1313. end;
  1314. procedure TLLVMAssember.WriteFunctionInlineAsmList(list: tasmlist);
  1315. var
  1316. a: TExternalAssembler;
  1317. begin
  1318. if not assigned(ffuncinlasmdecorator) then
  1319. ffuncinlasmdecorator:=TLLVMFunctionInlineAssemblyDecorator.create;
  1320. if assigned(writer.decorator) then
  1321. internalerror(2016110201);
  1322. writer.decorator:=ffuncinlasmdecorator;
  1323. a:=GetExternalGnuAssemblerWithAsmInfoWriter(asminfo,writer);
  1324. a.WriteTree(list);
  1325. a.free;
  1326. writer.decorator:=nil;
  1327. end;
  1328. {****************************************************************************}
  1329. { LLVM Instruction Writer }
  1330. {****************************************************************************}
  1331. constructor TLLVMInstrWriter.create(_owner: TLLVMAssember);
  1332. begin
  1333. inherited create;
  1334. owner := _owner;
  1335. end;
  1336. const
  1337. as_llvm_info : tasminfo =
  1338. (
  1339. id : as_llvm;
  1340. idtxt : 'LLVM-AS';
  1341. asmbin : 'llc';
  1342. asmcmd: '$OPT -o $OBJ $ASM';
  1343. supported_targets : [system_x86_64_linux,system_x86_64_darwin,system_powerpc64_darwin];
  1344. flags : [af_smartlink_sections];
  1345. labelprefix : 'L';
  1346. comment : '; ';
  1347. dollarsign: '$';
  1348. );
  1349. begin
  1350. RegisterAssembler(as_llvm_info,TLLVMAssember);
  1351. end.