agllvm.pas 50 KB

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