agllvm.pas 60 KB

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