agllvm.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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_invoke, la_resume,
  506. la_unreachable,
  507. la_store,
  508. la_fence,
  509. la_cmpxchg,
  510. la_atomicrmw:
  511. begin
  512. { instructions that never have a result }
  513. end;
  514. la_call:
  515. begin
  516. if taillvm(hp).oper[1]^.reg<>NR_NO then
  517. owner.writer.AsmWrite(getregisterstring(taillvm(hp).oper[1]^.reg)+' = ');
  518. opstart:=2;
  519. if llvmflag_call_no_ptr in llvmversion_properties[current_settings.llvmversion] then
  520. begin
  521. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  522. opdone:=true;
  523. tmpstr:=llvmencodetypename(taillvm(hp).oper[2]^.def);
  524. if tmpstr[length(tmpstr)]<>'*' then
  525. begin
  526. writeln(tmpstr);
  527. internalerror(2016071102);
  528. end
  529. else
  530. setlength(tmpstr,length(tmpstr)-1);
  531. owner.writer.AsmWrite(tmpstr);
  532. opstart:=3;
  533. end;
  534. end;
  535. la_blockaddress:
  536. begin
  537. owner.writer.AsmWrite('i8* blockaddress(');
  538. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false));
  539. { getopstr would add a "label" qualifier, which blockaddress does
  540. not want }
  541. owner.writer.AsmWrite(',%');
  542. with taillvm(hp).oper[1]^ do
  543. begin
  544. if (typ<>top_ref) or
  545. (ref^.refaddr<>addr_full) then
  546. internalerror(2016112001);
  547. owner.writer.AsmWrite(ref^.symbol.name);
  548. end;
  549. nested:=true;
  550. done:=true;
  551. end;
  552. la_alloca:
  553. begin
  554. owner.writer.AsmWrite(getreferencestring(taillvm(hp).oper[0]^.ref^,false)+' = ');
  555. sep:=' ';
  556. opstart:=1;
  557. end;
  558. la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
  559. la_fptoui, la_fptosi, la_uitofp, la_sitofp,
  560. la_ptrtoint, la_inttoptr,
  561. la_bitcast:
  562. begin
  563. { destination can be empty in case of nested constructs, or
  564. data initialisers }
  565. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  566. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  567. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
  568. else
  569. nested:=true;
  570. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  571. if not nested then
  572. owner.writer.AsmWrite(' ')
  573. else
  574. owner.writer.AsmWrite(' (');
  575. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[1]^,false));
  576. { if there's a tai operand, its def is used instead of an
  577. explicit def operand }
  578. if taillvm(hp).ops=4 then
  579. begin
  580. owner.writer.AsmWrite(' ');
  581. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[2]^,false));
  582. opstart:=3;
  583. end
  584. else
  585. opstart:=2;
  586. owner.writer.AsmWrite(' to ');
  587. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[opstart]^,false));
  588. done:=true;
  589. end
  590. else
  591. begin
  592. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  593. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  594. begin
  595. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[0]^,true)+' = ');
  596. end
  597. else
  598. nested:=true;
  599. sep:=' ';
  600. opstart:=1
  601. end;
  602. end;
  603. { process operands }
  604. if not done then
  605. begin
  606. if not opdone then
  607. begin
  608. owner.writer.AsmWrite(getopcodestr(taillvm(hp)));
  609. if nested then
  610. owner.writer.AsmWrite(' (');
  611. end;
  612. if taillvm(hp).ops<>0 then
  613. begin
  614. for i:=opstart to taillvm(hp).ops-1 do
  615. begin
  616. owner.writer.AsmWrite(sep);
  617. owner.writer.AsmWrite(getopstr(taillvm(hp).oper[i]^,op in [la_load,la_store]));
  618. if (taillvm(hp).oper[i]^.typ in [top_def,top_cond,top_fpcond]) or
  619. (op=la_call) then
  620. sep :=' '
  621. else
  622. sep:=', ';
  623. end;
  624. end;
  625. end;
  626. if op=la_alloca then
  627. owner.writer.AsmWrite(getreferencealignstring(taillvm(hp).oper[0]^.ref^));
  628. if nested then
  629. owner.writer.AsmWrite(')')
  630. else if owner.fdecllevel=0 then
  631. owner.writer.AsmLn;
  632. end;
  633. function TLLVMInstrWriter.getopcodestr(hp: taillvm): TSymStr;
  634. begin
  635. result:=llvm_op2str[hp.llvmopcode];
  636. case hp.llvmopcode of
  637. la_load:
  638. begin
  639. if vol_read in hp.oper[2]^.ref^.volatility then
  640. result:=result+' volatile';
  641. end;
  642. la_store:
  643. begin
  644. if vol_write in hp.oper[3]^.ref^.volatility then
  645. result:=result+' volatile';
  646. end;
  647. end;
  648. end;
  649. {****************************************************************************}
  650. { LLVM Assembler writer }
  651. {****************************************************************************}
  652. destructor TLLVMAssember.Destroy;
  653. begin
  654. InstrWriter.free;
  655. ffuncinlasmdecorator.free;
  656. inherited destroy;
  657. end;
  658. function TLLVMAssember.MakeCmdLine: TCmdStr;
  659. var
  660. optstr: TCmdStr;
  661. begin
  662. result := inherited MakeCmdLine;
  663. { standard optimization flags for llc -- todo: this needs to be split
  664. into a call to opt and one to llc }
  665. if cs_opt_level3 in current_settings.optimizerswitches then
  666. optstr:='-O3'
  667. else if cs_opt_level2 in current_settings.optimizerswitches then
  668. optstr:='-O2'
  669. else if cs_opt_level1 in current_settings.optimizerswitches then
  670. optstr:='-O1'
  671. else
  672. optstr:='-O0';
  673. { stack frame elimination }
  674. if not(cs_opt_stackframe in current_settings.optimizerswitches) then
  675. optstr:=optstr+' -disable-fp-elim';
  676. { fast math }
  677. if cs_opt_fastmath in current_settings.optimizerswitches then
  678. optstr:=optstr+' -enable-unsafe-fp-math -enable-fp-mad -fp-contract=fast';
  679. { smart linking }
  680. if cs_create_smart in current_settings.moduleswitches then
  681. optstr:=optstr+' -data-sections -function-sections';
  682. { pic }
  683. if cs_create_pic in current_settings.moduleswitches then
  684. optstr:=optstr+' -relocation-model=pic'
  685. else if not(target_info.system in systems_darwin) then
  686. optstr:=optstr+' -relocation-model=static'
  687. else
  688. optstr:=optstr+' -relocation-model=dynamic-no-pic';
  689. { our stack alignment is non-standard on some targets. The following
  690. parameter is however ignored on some targets by llvm, so it may not
  691. be enough }
  692. optstr:=optstr+' -stack-alignment='+tostr(target_info.stackalign*8);
  693. { force object output instead of textual assembler code }
  694. optstr:=optstr+' -filetype=obj';
  695. replace(result,'$OPT',optstr);
  696. end;
  697. procedure TLLVMAssember.WriteTree(p:TAsmList);
  698. var
  699. hp : tai;
  700. InlineLevel : cardinal;
  701. asmblock: boolean;
  702. do_line : boolean;
  703. replaceforbidden: boolean;
  704. begin
  705. if not assigned(p) then
  706. exit;
  707. replaceforbidden:=asminfo^.dollarsign<>'$';
  708. InlineLevel:=0;
  709. asmblock:=false;
  710. { lineinfo is only needed for al_procedures (PFV) }
  711. do_line:=(cs_asm_source in current_settings.globalswitches) or
  712. ((cs_lineinfo in current_settings.moduleswitches)
  713. and (p=current_asmdata.asmlists[al_procedures]));
  714. hp:=tai(p.first);
  715. while assigned(hp) do
  716. begin
  717. prefetch(pointer(hp.next)^);
  718. if not(hp.typ in SkipLineInfo) then
  719. begin
  720. current_filepos:=tailineinfo(hp).fileinfo;
  721. { no line info for inlined code }
  722. if do_line and (inlinelevel=0) then
  723. WriteSourceLine(hp as tailineinfo);
  724. end;
  725. WriteTai(replaceforbidden, do_line, InlineLevel, asmblock, hp);
  726. hp:=tai(hp.next);
  727. end;
  728. end;
  729. procedure TLLVMAssember.WriteExtraHeader;
  730. begin
  731. writer.AsmWrite('target datalayout = "');
  732. writer.AsmWrite(target_info.llvmdatalayout);
  733. writer.AsmWriteln('"');
  734. writer.AsmWrite('target triple = "');
  735. writer.AsmWrite(llvm_target_name);
  736. writer.AsmWriteln('"');
  737. end;
  738. procedure TLLVMAssember.WriteExtraFooter;
  739. begin
  740. end;
  741. procedure TLLVMAssember.WriteInstruction(hp: tai);
  742. begin
  743. end;
  744. procedure TLLVMAssember.WriteLlvmInstruction(hp: tai);
  745. begin
  746. InstrWriter.WriteInstruction(hp);
  747. end;
  748. procedure TLLVMAssember.WriteRealConst(hp: tai_realconst; do_line: boolean);
  749. begin
  750. if fdecllevel=0 then
  751. begin
  752. case tai_realconst(hp).realtyp of
  753. aitrealconst_s32bit:
  754. writer.AsmWriteLn(asminfo^.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  755. aitrealconst_s64bit:
  756. writer.AsmWriteLn(asminfo^.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  757. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  758. { can't write full 80 bit floating point constants yet on non-x86 }
  759. aitrealconst_s80bit:
  760. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  761. {$endif cpuextended}
  762. aitrealconst_s64comp:
  763. writer.AsmWriteLn(asminfo^.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  764. else
  765. internalerror(2014050604);
  766. end;
  767. internalerror(2016120202);
  768. end;
  769. case hp.realtyp of
  770. aitrealconst_s32bit:
  771. writer.AsmWrite(llvmdoubletostr(hp.value.s32val));
  772. aitrealconst_s64bit:
  773. writer.AsmWriteln(llvmdoubletostr(hp.value.s64val));
  774. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  775. aitrealconst_s80bit:
  776. writer.AsmWrite(llvmextendedtostr(hp.value.s80val));
  777. {$endif defined(cpuextended)}
  778. aitrealconst_s64comp:
  779. { handled as int64 most of the time in llvm }
  780. writer.AsmWrite(tostr(round(hp.value.s64compval)));
  781. else
  782. internalerror(2014062401);
  783. end;
  784. end;
  785. procedure TLLVMAssember.WriteOrdConst(hp: tai_const);
  786. var
  787. consttyp: taiconst_type;
  788. begin
  789. if fdecllevel=0 then
  790. internalerror(2016120203);
  791. consttyp:=hp.consttype;
  792. case consttyp of
  793. aitconst_got,
  794. aitconst_gotoff_symbol,
  795. aitconst_uleb128bit,
  796. aitconst_sleb128bit,
  797. aitconst_rva_symbol,
  798. aitconst_secrel32_symbol,
  799. aitconst_darwin_dwarf_delta32,
  800. aitconst_darwin_dwarf_delta64,
  801. aitconst_half16bit,
  802. aitconst_gs:
  803. internalerror(2014052901);
  804. aitconst_128bit,
  805. aitconst_64bit,
  806. aitconst_32bit,
  807. aitconst_16bit,
  808. aitconst_8bit,
  809. aitconst_16bit_unaligned,
  810. aitconst_32bit_unaligned,
  811. aitconst_64bit_unaligned:
  812. begin
  813. if fdecllevel=0 then
  814. writer.AsmWrite(asminfo^.comment);
  815. { can't have compile-time differences between symbols; these are
  816. normally for PIC, but llvm takes care of that for us }
  817. if assigned(hp.endsym) then
  818. internalerror(2014052902);
  819. if assigned(hp.sym) then
  820. begin
  821. writer.AsmWrite(LlvmAsmSymName(hp.sym));
  822. { can't have offsets }
  823. if hp.value<>0 then
  824. if fdecllevel<>0 then
  825. internalerror(2014052903)
  826. else
  827. writer.AsmWrite(' -- symbol offset: ' + tostr(hp.value));
  828. end
  829. else if hp.value=0 then
  830. writer.AsmWrite('zeroinitializer')
  831. else
  832. writer.AsmWrite(tostr(hp.value));
  833. {
  834. // activate in case of debugging IE 2016120203
  835. if fdecllevel=0 then
  836. writer.AsmLn;
  837. }
  838. end;
  839. else
  840. internalerror(200704251);
  841. end;
  842. end;
  843. procedure TLLVMAssember.WriteTai(const replaceforbidden: boolean; const do_line: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
  844. procedure WriteLinkageVibilityFlags(bind: TAsmSymBind);
  845. begin
  846. case bind of
  847. AB_EXTERNAL,
  848. AB_EXTERNAL_INDIRECT:
  849. writer.AsmWrite(' external');
  850. AB_COMMON:
  851. writer.AsmWrite(' common');
  852. AB_LOCAL:
  853. writer.AsmWrite(' internal');
  854. AB_GLOBAL,
  855. AB_INDIRECT:
  856. ;
  857. AB_WEAK_EXTERNAL:
  858. writer.AsmWrite(' extern_weak');
  859. AB_PRIVATE_EXTERN:
  860. begin
  861. if not(llvmflag_linker_private in llvmversion_properties[current_settings.llvmversion]) then
  862. writer.AsmWrite(' hidden')
  863. else
  864. writer.AsmWrite(' linker_private');
  865. end
  866. else
  867. internalerror(2014020104);
  868. end;
  869. end;
  870. procedure WriteFunctionFlags(pd: tprocdef);
  871. begin
  872. if (pos('FPC_SETJMP',upper(pd.mangledname))<>0) or
  873. (pd.mangledname=(target_info.cprefix+'setjmp')) then
  874. writer.AsmWrite(' returns_twice');
  875. if po_inline in pd.procoptions then
  876. writer.AsmWrite(' inlinehint');
  877. { ensure that functions that happen to have the same name as a
  878. standard C library function, but which are implemented in Pascal,
  879. are not considered to have the same semantics as the C function with
  880. the same name }
  881. if not(po_external in pd.procoptions) then
  882. writer.AsmWrite(' nobuiltin');
  883. if po_noreturn in pd.procoptions then
  884. writer.AsmWrite(' noreturn');
  885. end;
  886. procedure WriteTypedConstData(hp: tai_abstracttypedconst);
  887. var
  888. p: tai_abstracttypedconst;
  889. pval: tai;
  890. defstr: TSymStr;
  891. first, gotstring: boolean;
  892. begin
  893. defstr:=llvmencodetypename(hp.def);
  894. { write the struct, array or simple type }
  895. case hp.adetyp of
  896. tck_record:
  897. begin
  898. writer.AsmWrite(defstr);
  899. writer.AsmWrite(' <{');
  900. first:=true;
  901. for p in tai_aggregatetypedconst(hp) do
  902. begin
  903. if not first then
  904. writer.AsmWrite(', ')
  905. else
  906. first:=false;
  907. WriteTypedConstData(p);
  908. end;
  909. writer.AsmWrite('}>');
  910. end;
  911. tck_array:
  912. begin
  913. writer.AsmWrite(defstr);
  914. first:=true;
  915. gotstring:=false;
  916. for p in tai_aggregatetypedconst(hp) do
  917. begin
  918. if not first then
  919. writer.AsmWrite(',')
  920. else
  921. begin
  922. writer.AsmWrite(' ');
  923. if (tai_abstracttypedconst(p).adetyp=tck_simple) and
  924. (tai_simpletypedconst(p).val.typ=ait_string) then
  925. begin
  926. gotstring:=true;
  927. end
  928. else
  929. begin
  930. writer.AsmWrite('[');
  931. end;
  932. first:=false;
  933. end;
  934. { cannot concat strings and other things }
  935. if gotstring and
  936. ((tai_abstracttypedconst(p).adetyp<>tck_simple) or
  937. (tai_simpletypedconst(p).val.typ<>ait_string)) then
  938. internalerror(2014062701);
  939. WriteTypedConstData(p);
  940. end;
  941. if not gotstring then
  942. writer.AsmWrite(']');
  943. end;
  944. tck_simple:
  945. begin
  946. pval:=tai_simpletypedconst(hp).val;
  947. if pval.typ<>ait_string then
  948. begin
  949. writer.AsmWrite(defstr);
  950. writer.AsmWrite(' ');
  951. end;
  952. WriteTai(replaceforbidden,do_line,InlineLevel,asmblock,pval);
  953. end;
  954. end;
  955. end;
  956. var
  957. hp2: tai;
  958. s: string;
  959. sstr: TSymStr;
  960. i: longint;
  961. ch: ansichar;
  962. begin
  963. case hp.typ of
  964. ait_comment :
  965. begin
  966. writer.AsmWrite(asminfo^.comment);
  967. writer.AsmWritePChar(tai_comment(hp).str);
  968. if fdecllevel<>0 then
  969. internalerror(2015090601);
  970. writer.AsmLn;
  971. end;
  972. ait_regalloc :
  973. begin
  974. if (cs_asm_regalloc in current_settings.globalswitches) then
  975. begin
  976. writer.AsmWrite(#9+asminfo^.comment+'Register ');
  977. repeat
  978. writer.AsmWrite(std_regname(Tai_regalloc(hp).reg));
  979. if (hp.next=nil) or
  980. (tai(hp.next).typ<>ait_regalloc) or
  981. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  982. break;
  983. hp:=tai(hp.next);
  984. writer.AsmWrite(',');
  985. until false;
  986. writer.AsmWrite(' ');
  987. writer.AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  988. end;
  989. end;
  990. ait_tempalloc :
  991. begin
  992. if (cs_asm_tempalloc in current_settings.globalswitches) then
  993. WriteTempalloc(tai_tempalloc(hp));
  994. end;
  995. ait_align,
  996. ait_section :
  997. begin
  998. { ignore, specified as part of declarations -- don't write
  999. comment, because could appear in the middle of an aggregate
  1000. constant definition }
  1001. end;
  1002. ait_datablock :
  1003. begin
  1004. writer.AsmWrite(asminfo^.comment);
  1005. writer.AsmWriteln('datablock');
  1006. end;
  1007. ait_const:
  1008. begin
  1009. WriteOrdConst(tai_const(hp));
  1010. end;
  1011. ait_realconst :
  1012. begin
  1013. WriteRealConst(tai_realconst(hp), do_line);
  1014. end;
  1015. ait_string :
  1016. begin
  1017. if fdecllevel=0 then
  1018. internalerror(2016120201);
  1019. writer.AsmWrite('c"');
  1020. for i:=1 to tai_string(hp).len do
  1021. begin
  1022. ch:=tai_string(hp).str[i-1];
  1023. case ch of
  1024. #0, {This can't be done by range, because a bug in FPC}
  1025. #1..#31,
  1026. #128..#255,
  1027. '"',
  1028. '\' : s:='\'+hexStr(ord(ch),2);
  1029. else
  1030. s:=ch;
  1031. end;
  1032. writer.AsmWrite(s);
  1033. end;
  1034. writer.AsmWrite('"');
  1035. end;
  1036. ait_label :
  1037. begin
  1038. if not asmblock and
  1039. (tai_label(hp).labsym.is_used) then
  1040. begin
  1041. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  1042. begin
  1043. { should be emitted as part of the variable/function def }
  1044. internalerror(2013010703);
  1045. end;
  1046. if tai_label(hp).labsym.bind in [AB_GLOBAL, AB_PRIVATE_EXTERN] then
  1047. begin
  1048. { should be emitted as part of the variable/function def }
  1049. //internalerror(2013010704);
  1050. writer.AsmWriteln(asminfo^.comment+'global/privateextern label: '+tai_label(hp).labsym.name);
  1051. end;
  1052. if replaceforbidden then
  1053. writer.AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  1054. else
  1055. writer.AsmWrite(tai_label(hp).labsym.name);
  1056. writer.AsmWriteLn(':');
  1057. end;
  1058. end;
  1059. ait_symbol :
  1060. begin
  1061. if fdecllevel=0 then
  1062. writer.AsmWrite(asminfo^.comment);
  1063. writer.AsmWriteln(LlvmAsmSymName(tai_symbol(hp).sym));
  1064. { todo }
  1065. if tai_symbol(hp).has_value then
  1066. internalerror(2014062402);
  1067. end;
  1068. ait_llvmdecl:
  1069. begin
  1070. if taillvmdecl(hp).def.typ=procdef then
  1071. begin
  1072. if not(ldf_definition in taillvmdecl(hp).flags) then
  1073. begin
  1074. writer.AsmWrite('declare');
  1075. writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), taillvmdecl(hp).namesym.name, lpd_decl));
  1076. WriteFunctionFlags(tprocdef(taillvmdecl(hp).def));
  1077. writer.AsmLn;
  1078. end
  1079. else
  1080. begin
  1081. writer.AsmWrite('define');
  1082. if ldf_weak in taillvmdecl(hp).flags then
  1083. writer.AsmWrite(' weak');
  1084. WriteLinkageVibilityFlags(taillvmdecl(hp).namesym.bind);
  1085. writer.AsmWrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), '', lpd_def));
  1086. WriteFunctionFlags(tprocdef(taillvmdecl(hp).def));
  1087. writer.AsmWriteln(' {');
  1088. end;
  1089. end
  1090. else
  1091. begin
  1092. writer.AsmWrite(LlvmAsmSymName(taillvmdecl(hp).namesym));
  1093. writer.AsmWrite(' =');
  1094. if ldf_weak in taillvmdecl(hp).flags then
  1095. writer.AsmWrite(' weak');
  1096. if ldf_appending in taillvmdecl(hp).flags then
  1097. writer.AsmWrite(' appending');
  1098. WriteLinkageVibilityFlags(taillvmdecl(hp).namesym.bind);
  1099. writer.AsmWrite(' ');
  1100. if (ldf_tls in taillvmdecl(hp).flags) then
  1101. writer.AsmWrite('thread_local ');
  1102. if ldf_unnamed_addr in taillvmdecl(hp).flags then
  1103. writer.AsmWrite('unnamed_addr ');
  1104. if taillvmdecl(hp).sec in [sec_rodata,sec_rodata_norel] then
  1105. writer.AsmWrite('constant ')
  1106. else
  1107. writer.AsmWrite('global ');
  1108. if not assigned(taillvmdecl(hp).initdata) then
  1109. begin
  1110. writer.AsmWrite(llvmencodetypename(taillvmdecl(hp).def));
  1111. if not(taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL,AB_EXTERNAL_INDIRECT]) then
  1112. writer.AsmWrite(' zeroinitializer');
  1113. end
  1114. else
  1115. begin
  1116. inc(fdecllevel);
  1117. { can't have an external symbol with initialisation data }
  1118. if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
  1119. internalerror(2014052905);
  1120. { bitcast initialisation data to the type of the constant }
  1121. { write initialisation data }
  1122. hp2:=tai(taillvmdecl(hp).initdata.first);
  1123. while assigned(hp2) do
  1124. begin
  1125. WriteTai(replaceforbidden,do_line,InlineLevel,asmblock,hp2);
  1126. hp2:=tai(hp2.next);
  1127. end;
  1128. dec(fdecllevel);
  1129. end;
  1130. { custom section name? }
  1131. case taillvmdecl(hp).sec of
  1132. sec_user:
  1133. begin
  1134. writer.AsmWrite(', section "');
  1135. writer.AsmWrite(taillvmdecl(hp).secname);
  1136. writer.AsmWrite('"');
  1137. end;
  1138. low(TObjCAsmSectionType)..high(TObjCAsmSectionType):
  1139. begin
  1140. writer.AsmWrite(', section "');
  1141. writer.AsmWrite(objc_section_name(taillvmdecl(hp).sec));
  1142. writer.AsmWrite('"');
  1143. end;
  1144. end;
  1145. { sections whose name starts with 'llvm.' are for LLVM
  1146. internal use and don't have an alignment }
  1147. if pos('llvm.',taillvmdecl(hp).secname)<>1 then
  1148. begin
  1149. { alignment }
  1150. writer.AsmWrite(', align ');
  1151. writer.AsmWriteln(tostr(taillvmdecl(hp).alignment));
  1152. end
  1153. else
  1154. writer.AsmLn;
  1155. end;
  1156. end;
  1157. ait_llvmalias:
  1158. begin
  1159. writer.AsmWrite(LlvmAsmSymName(taillvmalias(hp).newsym));
  1160. writer.AsmWrite(' = alias ');
  1161. WriteLinkageVibilityFlags(taillvmalias(hp).bind);
  1162. if taillvmalias(hp).def.typ=procdef then
  1163. sstr:=llvmencodeproctype(tabstractprocdef(taillvmalias(hp).def), '', lpd_alias)
  1164. else
  1165. sstr:=llvmencodetypename(taillvmalias(hp).def);
  1166. writer.AsmWrite(sstr);
  1167. if llvmflag_alias_double_type in llvmversion_properties[current_settings.llvmversion] then
  1168. begin
  1169. writer.AsmWrite(', ');
  1170. writer.AsmWrite(sstr);
  1171. end;
  1172. writer.AsmWrite('* ');
  1173. writer.AsmWriteln(LlvmAsmSymName(taillvmalias(hp).oldsym));
  1174. end;
  1175. ait_symbolpair:
  1176. begin
  1177. { should be emitted as part of the symbol def }
  1178. internalerror(2013010708);
  1179. end;
  1180. ait_symbol_end :
  1181. begin
  1182. if tai_symbol_end(hp).sym.typ=AT_FUNCTION then
  1183. writer.AsmWriteln('}')
  1184. else
  1185. writer.AsmWriteln('; ait_symbol_end error, should not be generated');
  1186. // internalerror(2013010711);
  1187. end;
  1188. ait_instruction :
  1189. begin
  1190. WriteInstruction(hp);
  1191. end;
  1192. ait_llvmins:
  1193. begin
  1194. WriteLlvmInstruction(hp);
  1195. end;
  1196. ait_stab :
  1197. begin
  1198. internalerror(2013010712);
  1199. end;
  1200. ait_force_line,
  1201. ait_function_name :
  1202. ;
  1203. ait_cutobject :
  1204. begin
  1205. end;
  1206. ait_marker :
  1207. case
  1208. tai_marker(hp).kind of
  1209. mark_NoLineInfoStart:
  1210. inc(InlineLevel);
  1211. mark_NoLineInfoEnd:
  1212. dec(InlineLevel);
  1213. { these cannot be nested }
  1214. mark_AsmBlockStart:
  1215. asmblock:=true;
  1216. mark_AsmBlockEnd:
  1217. asmblock:=false;
  1218. end;
  1219. ait_directive :
  1220. begin
  1221. { CPU directive is commented out for the LLVM }
  1222. if tai_directive(hp).directive=asd_cpu then
  1223. writer.AsmWrite(asminfo^.comment);
  1224. WriteDirectiveName(tai_directive(hp).directive);
  1225. if tai_directive(hp).name <>'' then
  1226. writer.AsmWrite(tai_directive(hp).name);
  1227. if fdecllevel<>0 then
  1228. internalerror(2015090602);
  1229. writer.AsmLn;
  1230. end;
  1231. ait_seh_directive :
  1232. begin
  1233. internalerror(2013010713);
  1234. end;
  1235. ait_varloc:
  1236. begin
  1237. if tai_varloc(hp).newlocationhi<>NR_NO then
  1238. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1239. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  1240. else
  1241. writer.AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  1242. std_regname(tai_varloc(hp).newlocation)));
  1243. if fdecllevel<>0 then
  1244. internalerror(2015090603);
  1245. writer.AsmLn;
  1246. end;
  1247. ait_typedconst:
  1248. begin
  1249. WriteTypedConstData(tai_abstracttypedconst(hp));
  1250. end
  1251. else
  1252. internalerror(2006012201);
  1253. end;
  1254. end;
  1255. constructor TLLVMAssember.CreateWithWriter(info: pasminfo; wr: TExternalAssemblerOutputFile; freewriter, smart: boolean);
  1256. begin
  1257. inherited;
  1258. InstrWriter:=TLLVMInstrWriter.create(self);
  1259. end;
  1260. procedure TLLVMAssember.WriteDirectiveName(dir: TAsmDirective);
  1261. begin
  1262. writer.AsmWrite('.'+directivestr[dir]+' ');
  1263. end;
  1264. procedure TLLVMAssember.WriteAsmList;
  1265. var
  1266. hal : tasmlisttype;
  1267. i: longint;
  1268. a: TExternalAssembler;
  1269. decorator: TLLVMModuleInlineAssemblyDecorator;
  1270. begin
  1271. WriteExtraHeader;
  1272. for hal:=low(TasmlistType) to high(TasmlistType) do
  1273. begin
  1274. if not assigned(current_asmdata.asmlists[hal]) or
  1275. current_asmdata.asmlists[hal].Empty then
  1276. continue;
  1277. writer.AsmWriteLn(asminfo^.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  1278. if hal<>al_pure_assembler then
  1279. writetree(current_asmdata.asmlists[hal])
  1280. else
  1281. begin
  1282. { write routines using the target-specific external assembler
  1283. writer, filtered using the LLVM module-level assembly
  1284. decorator }
  1285. decorator:=TLLVMModuleInlineAssemblyDecorator.Create;
  1286. writer.decorator:=decorator;
  1287. a:=GetExternalGnuAssemblerWithAsmInfoWriter(asminfo,writer);
  1288. a.WriteTree(current_asmdata.asmlists[hal]);
  1289. writer.decorator:=nil;
  1290. decorator.free;
  1291. a.free;
  1292. end;
  1293. writer.AsmWriteLn(asminfo^.comment+'End asmlist '+AsmlistTypeStr[hal]);
  1294. end;
  1295. writer.AsmLn;
  1296. end;
  1297. procedure TLLVMAssember.WriteFunctionInlineAsmList(list: tasmlist);
  1298. var
  1299. a: TExternalAssembler;
  1300. begin
  1301. if not assigned(ffuncinlasmdecorator) then
  1302. ffuncinlasmdecorator:=TLLVMFunctionInlineAssemblyDecorator.create;
  1303. if assigned(writer.decorator) then
  1304. internalerror(2016110201);
  1305. writer.decorator:=ffuncinlasmdecorator;
  1306. a:=GetExternalGnuAssemblerWithAsmInfoWriter(asminfo,writer);
  1307. a.WriteTree(list);
  1308. a.free;
  1309. writer.decorator:=nil;
  1310. end;
  1311. {****************************************************************************}
  1312. { LLVM Instruction Writer }
  1313. {****************************************************************************}
  1314. constructor TLLVMInstrWriter.create(_owner: TLLVMAssember);
  1315. begin
  1316. inherited create;
  1317. owner := _owner;
  1318. end;
  1319. const
  1320. as_llvm_info : tasminfo =
  1321. (
  1322. id : as_llvm;
  1323. idtxt : 'LLVM-AS';
  1324. asmbin : 'llc';
  1325. asmcmd: '$OPT -o $OBJ $ASM';
  1326. supported_targets : [system_x86_64_linux,system_x86_64_darwin,system_powerpc64_darwin];
  1327. flags : [af_smartlink_sections];
  1328. labelprefix : 'L';
  1329. comment : '; ';
  1330. dollarsign: '$';
  1331. );
  1332. begin
  1333. RegisterAssembler(as_llvm_info,TLLVMAssember);
  1334. end.