agllvm.pas 48 KB

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