agllvm.pas 48 KB

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