agllvm.pas 47 KB

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