agllvm.pas 55 KB

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