agllvm.pas 55 KB

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