agllvm.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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,
  22. aasmbase,aasmtai,aasmdata,
  23. assemble;
  24. type
  25. TLLVMInstrWriter = class;
  26. { TLLVMAssember }
  27. TLLVMAssember=class(texternalassembler)
  28. protected
  29. fdecllevel: longint;
  30. procedure WriteExtraHeader;virtual;
  31. procedure WriteExtraFooter;virtual;
  32. procedure WriteInstruction(hp: tai);
  33. procedure WriteLlvmInstruction(hp: tai);
  34. procedure WriteDirectiveName(dir: TAsmDirective); virtual;
  35. procedure WriteRealConst(hp: tai_realconst; do_line: boolean);
  36. procedure WriteOrdConst(hp: tai_const);
  37. procedure WriteTai(const replaceforbidden: boolean; const do_line: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
  38. public
  39. constructor create(smart: boolean); override;
  40. procedure AsmLn; override;
  41. function MakeCmdLine: TCmdStr; override;
  42. procedure WriteTree(p:TAsmList);override;
  43. procedure WriteAsmList;override;
  44. destructor destroy; override;
  45. protected
  46. InstrWriter: TLLVMInstrWriter;
  47. end;
  48. {# This is the base class for writing instructions.
  49. The WriteInstruction() method must be overridden
  50. to write a single instruction to the assembler
  51. file.
  52. }
  53. TLLVMInstrWriter = class
  54. constructor create(_owner: TLLVMAssember);
  55. procedure WriteInstruction(hp : tai);
  56. protected
  57. owner: TLLVMAssember;
  58. fstr: TSymStr;
  59. function getopstr(const o:toper; refwithalign: boolean) : TSymStr;
  60. end;
  61. implementation
  62. uses
  63. SysUtils,
  64. cutils,cfileutl,systems,
  65. fmodule,verbose,
  66. aasmcnst,symconst,symdef,
  67. llvmbase,aasmllvm,itllvm,llvmdef,
  68. cgbase,cgutils,cpubase;
  69. const
  70. line_length = 70;
  71. type
  72. {$ifdef cpuextended}
  73. t80bitarray = array[0..9] of byte;
  74. {$endif cpuextended}
  75. t64bitarray = array[0..7] of byte;
  76. t32bitarray = array[0..3] of byte;
  77. {****************************************************************************}
  78. { Support routines }
  79. {****************************************************************************}
  80. function single2str(d : single) : string;
  81. var
  82. hs : string;
  83. begin
  84. str(d,hs);
  85. { replace space with + }
  86. if hs[1]=' ' then
  87. hs[1]:='+';
  88. single2str:=hs
  89. end;
  90. function double2str(d : double) : string;
  91. var
  92. hs : string;
  93. begin
  94. str(d,hs);
  95. { replace space with + }
  96. if hs[1]=' ' then
  97. hs[1]:='+';
  98. double2str:=hs
  99. end;
  100. function extended2str(e : extended) : string;
  101. var
  102. hs : string;
  103. begin
  104. str(e,hs);
  105. { replace space with + }
  106. if hs[1]=' ' then
  107. hs[1]:='+';
  108. extended2str:=hs
  109. end;
  110. {****************************************************************************}
  111. { LLVM Instruction writer }
  112. {****************************************************************************}
  113. function getregisterstring(reg: tregister): ansistring;
  114. begin
  115. if getregtype(reg)=R_TEMPREGISTER then
  116. result:='%tmp.'
  117. else
  118. result:='%reg.'+tostr(byte(getregtype(reg)))+'_';
  119. result:=result+tostr(getsupreg(reg));
  120. end;
  121. function getreferencealignstring(var ref: treference) : ansistring;
  122. begin
  123. result:=', align '+tostr(ref.alignment);
  124. end;
  125. function getreferencestring(var ref : treference; withalign: boolean) : ansistring;
  126. begin
  127. result:='';
  128. if assigned(ref.relsymbol) or
  129. (assigned(ref.symbol) =
  130. (ref.base<>NR_NO)) or
  131. (ref.index<>NR_NO) or
  132. (ref.offset<>0) then
  133. begin
  134. result:=' **(error ref: ';
  135. if assigned(ref.symbol) then
  136. result:=result+'sym='+ref.symbol.name+', ';
  137. if assigned(ref.relsymbol) then
  138. result:=result+'sym='+ref.relsymbol.name+', ';
  139. if ref.base=NR_NO then
  140. result:=result+'base=NR_NO, ';
  141. if ref.index<>NR_NO then
  142. result:=result+'index<>NR_NO, ';
  143. if ref.offset<>0 then
  144. result:=result+'offset='+tostr(ref.offset);
  145. result:=result+')**'
  146. // internalerror(2013060225);
  147. end;
  148. if ref.base<>NR_NO then
  149. result:=result+getregisterstring(ref.base)
  150. else
  151. result:=result+LlvmAsmSymName(ref.symbol);
  152. if withalign then
  153. result:=result+getreferencealignstring(ref);
  154. end;
  155. function getparas(const o: toper): ansistring;
  156. var
  157. i: longint;
  158. para: pllvmcallpara;
  159. begin
  160. result:='(';
  161. for i:=0 to o.paras.count-1 do
  162. begin
  163. if i<>0 then
  164. result:=result+', ';
  165. para:=pllvmcallpara(o.paras[i]);
  166. result:=result+llvmencodetypename(para^.def);
  167. if para^.valueext<>lve_none then
  168. result:=result+llvmvalueextension2str[para^.valueext];
  169. case para^.loc of
  170. LOC_REGISTER,
  171. LOC_FPUREGISTER,
  172. LOC_MMREGISTER:
  173. result:=result+' '+getregisterstring(para^.reg);
  174. else
  175. internalerror(2014010801);
  176. end;
  177. end;
  178. result:=result+')';
  179. end;
  180. function llvmdoubletostr(const d: double): TSymStr;
  181. type
  182. tdoubleval = record
  183. case byte of
  184. 1: (d: double);
  185. 2: (i: int64);
  186. end;
  187. begin
  188. { "When using the hexadecimal form, constants of types half,
  189. float, and double are represented using the 16-digit form shown
  190. above (which matches the IEEE754 representation for double)"
  191. And always in big endian form (sign bit leftmost)
  192. }
  193. result:='0x'+hexstr(tdoubleval(d).i,16);
  194. end;
  195. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  196. function llvmextendedtostr(const e: extended): TSymStr;
  197. var
  198. extendedval: record
  199. case byte of
  200. 1: (e: extended);
  201. 2: (r: packed record
  202. {$ifdef FPC_LITTLE_ENDIAN}
  203. l: int64;
  204. h: word;
  205. {$else FPC_LITTLE_ENDIAN}
  206. h: int64;
  207. l: word;
  208. {$endif FPC_LITTLE_ENDIAN}
  209. end;
  210. );
  211. end;
  212. begin
  213. extendedval.e:=e;
  214. { hex format is always big endian in llvm }
  215. result:='0xK'+hexstr(extendedval.r.h,sizeof(extendedval.r.h)*2)+
  216. hexstr(extendedval.r.l,sizeof(extendedval.r.l)*2);
  217. end;
  218. {$endif cpuextended}
  219. function TLLVMInstrWriter.getopstr(const o:toper; refwithalign: boolean) : TSymStr;
  220. var
  221. hs : ansistring;
  222. hp: tai;
  223. tmpinline: cardinal;
  224. tmpasmblock: boolean;
  225. begin
  226. case o.typ of
  227. top_reg:
  228. getopstr:=getregisterstring(o.reg);
  229. top_const:
  230. getopstr:=tostr(int64(o.val));
  231. top_ref:
  232. if o.ref^.refaddr=addr_full then
  233. begin
  234. getopstr:='';
  235. getopstr:=LlvmAsmSymName(o.ref^.symbol);
  236. if o.ref^.offset<>0 then
  237. internalerror(2013060223);
  238. end
  239. else
  240. getopstr:=getreferencestring(o.ref^,refwithalign);
  241. top_def:
  242. begin
  243. getopstr:=llvmencodetypename(o.def);
  244. end;
  245. top_cond:
  246. begin
  247. getopstr:=llvm_cond2str[o.cond];
  248. end;
  249. top_fpcond:
  250. begin
  251. getopstr:=llvm_fpcond2str[o.fpcond];
  252. end;
  253. top_single,
  254. top_double:
  255. begin
  256. { "When using the hexadecimal form, constants of types half,
  257. float, and double are represented using the 16-digit form shown
  258. above (which matches the IEEE754 representation for double)"
  259. And always in big endian form (sign bit leftmost)
  260. }
  261. if o.typ=top_double then
  262. result:=llvmdoubletostr(o.dval)
  263. else
  264. result:=llvmdoubletostr(o.sval)
  265. end;
  266. top_para:
  267. begin
  268. result:=getparas(o);
  269. end;
  270. top_tai:
  271. begin
  272. tmpinline:=1;
  273. tmpasmblock:=false;
  274. hp:=o.ai;
  275. owner.AsmWrite(fstr);
  276. fstr:='';
  277. owner.WriteTai(false,false,tmpinline,tmpasmblock,hp);
  278. result:='';
  279. end;
  280. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  281. top_extended80:
  282. begin
  283. result:=llvmextendedtostr(o.eval);
  284. end;
  285. {$endif cpuextended}
  286. top_undef:
  287. result:='undef'
  288. else
  289. internalerror(2013060227);
  290. end;
  291. end;
  292. procedure TLLVMInstrWriter.WriteInstruction(hp: tai);
  293. var
  294. op: tllvmop;
  295. sep: TSymStr;
  296. i, opstart: byte;
  297. nested: boolean;
  298. done: boolean;
  299. begin
  300. op:=taillvm(hp).llvmopcode;
  301. { we write everything immediately rather than adding it into a string,
  302. because operands may contain other tai that will also write things out
  303. (and their output must come after everything that was processed in this
  304. instruction, such as its opcode or previous operands) }
  305. if owner.fdecllevel=0 then
  306. owner.AsmWrite(#9);
  307. sep:=' ';
  308. done:=false;
  309. opstart:=0;
  310. nested:=false;
  311. case op of
  312. la_type:
  313. begin
  314. owner.asmwrite(llvmtypeidentifier(taillvm(hp).oper[0]^.def));
  315. owner.asmwrite(' = type ');
  316. owner.asmwrite(llvmencodetypedecl(taillvm(hp).oper[0]^.def));
  317. done:=true;
  318. end;
  319. la_ret, la_br, la_switch, la_indirectbr,
  320. la_invoke, la_resume,
  321. la_unreachable,
  322. la_store,
  323. la_fence,
  324. la_cmpxchg,
  325. la_atomicrmw:
  326. begin
  327. { instructions that never have a result }
  328. end;
  329. la_call:
  330. begin
  331. if taillvm(hp).oper[1]^.reg<>NR_NO then
  332. owner.AsmWrite(getregisterstring(taillvm(hp).oper[1]^.reg)+' = ');
  333. sep:=' ';
  334. opstart:=2;
  335. end;
  336. la_alloca:
  337. begin
  338. owner.AsmWrite(getreferencestring(taillvm(hp).oper[0]^.ref^,false)+' = ');
  339. sep:=' ';
  340. opstart:=1;
  341. end;
  342. la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
  343. la_fptoui, la_fptosi, la_uitofp, la_sitofp,
  344. la_ptrtoint, la_inttoptr,
  345. la_bitcast:
  346. begin
  347. { destination can be empty in case of nested constructs, or
  348. data initialisers }
  349. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  350. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  351. owner.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
  352. else
  353. nested:=true;
  354. owner.AsmWrite(llvm_op2str[op]);
  355. if not nested then
  356. owner.AsmWrite(' ')
  357. else
  358. owner.AsmWrite(' (');
  359. owner.AsmWrite(getopstr(taillvm(hp).oper[1]^,false));
  360. { if there's a tai operand, its def is used instead of an
  361. explicit def operand }
  362. if taillvm(hp).ops=4 then
  363. begin
  364. owner.AsmWrite(' ');
  365. owner.AsmWrite(getopstr(taillvm(hp).oper[2]^,false));
  366. opstart:=3;
  367. end
  368. else
  369. opstart:=2;
  370. owner.AsmWrite(' to ');
  371. owner.AsmWrite(getopstr(taillvm(hp).oper[opstart]^,false));
  372. done:=true;
  373. end
  374. else
  375. begin
  376. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  377. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  378. begin
  379. owner.AsmWrite(getopstr(taillvm(hp).oper[0]^,true)+' = ');
  380. end
  381. else
  382. nested:=true;
  383. sep:=' ';
  384. opstart:=1
  385. end;
  386. end;
  387. { process operands }
  388. if not done then
  389. begin
  390. owner.AsmWrite(llvm_op2str[op]);
  391. if nested then
  392. owner.AsmWrite(' (');
  393. if taillvm(hp).ops<>0 then
  394. begin
  395. for i:=opstart to taillvm(hp).ops-1 do
  396. begin
  397. owner.AsmWrite(sep);
  398. owner.AsmWrite(getopstr(taillvm(hp).oper[i]^,op in [la_load,la_store]));
  399. if (taillvm(hp).oper[i]^.typ in [top_def,top_cond,top_fpcond]) or
  400. (op=la_call) then
  401. sep :=' '
  402. else
  403. sep:=', ';
  404. end;
  405. end;
  406. end;
  407. if op=la_alloca then
  408. owner.AsmWrite(getreferencealignstring(taillvm(hp).oper[0]^.ref^));
  409. if nested then
  410. owner.AsmWrite(')')
  411. else
  412. owner.AsmLn;
  413. end;
  414. {****************************************************************************}
  415. { LLVM Assembler writer }
  416. {****************************************************************************}
  417. destructor TLLVMAssember.Destroy;
  418. begin
  419. InstrWriter.free;
  420. inherited destroy;
  421. end;
  422. function TLLVMAssember.MakeCmdLine: TCmdStr;
  423. var
  424. optstr: TCmdStr;
  425. begin
  426. result := inherited MakeCmdLine;
  427. { standard optimization flags for llc -- todo: this needs to be split
  428. into a call to opt and one to llc }
  429. if cs_opt_level3 in current_settings.optimizerswitches then
  430. optstr:='-O3'
  431. else if cs_opt_level2 in current_settings.optimizerswitches then
  432. optstr:='-O2'
  433. else if cs_opt_level1 in current_settings.optimizerswitches then
  434. optstr:='-O1'
  435. else
  436. optstr:='-O0';
  437. { stack frame elimination }
  438. if not(cs_opt_stackframe in current_settings.optimizerswitches) then
  439. optstr:=optstr+' -disable-fp-elim';
  440. { fast math }
  441. if cs_opt_fastmath in current_settings.optimizerswitches then
  442. optstr:=optstr+' -enable-unsafe-fp-math -enable-fp-mad -fp-contract=fast';
  443. { smart linking }
  444. if cs_create_smart in current_settings.moduleswitches then
  445. optstr:=optstr+' -fdata-sections -fcode-sections';
  446. { pic }
  447. if cs_create_pic in current_settings.moduleswitches then
  448. optstr:=optstr+' -relocation-model=pic'
  449. else if not(target_info.system in systems_darwin) then
  450. optstr:=optstr+' -relocation-model=static'
  451. else
  452. optstr:=optstr+' -relocation-model=dynamic-no-pic';
  453. { our stack alignment is non-standard on some targets. The following
  454. parameter is however ignored on some targets by llvm, so it may not
  455. be enough }
  456. optstr:=optstr+' -stack-alignment='+tostr(target_info.stackalign*8);
  457. { force object output instead of textual assembler code }
  458. optstr:=optstr+' -filetype=obj';
  459. replace(result,'$OPT',optstr);
  460. end;
  461. procedure TLLVMAssember.WriteTree(p:TAsmList);
  462. var
  463. hp : tai;
  464. InlineLevel : cardinal;
  465. asmblock: boolean;
  466. do_line : boolean;
  467. replaceforbidden: boolean;
  468. begin
  469. if not assigned(p) then
  470. exit;
  471. replaceforbidden:=target_asm.dollarsign<>'$';
  472. InlineLevel:=0;
  473. asmblock:=false;
  474. { lineinfo is only needed for al_procedures (PFV) }
  475. do_line:=(cs_asm_source in current_settings.globalswitches) or
  476. ((cs_lineinfo in current_settings.moduleswitches)
  477. and (p=current_asmdata.asmlists[al_procedures]));
  478. hp:=tai(p.first);
  479. while assigned(hp) do
  480. begin
  481. prefetch(pointer(hp.next)^);
  482. if not(hp.typ in SkipLineInfo) then
  483. begin
  484. current_filepos:=tailineinfo(hp).fileinfo;
  485. { no line info for inlined code }
  486. if do_line and (inlinelevel=0) then
  487. WriteSourceLine(hp as tailineinfo);
  488. end;
  489. WriteTai(replaceforbidden, do_line, InlineLevel, asmblock, hp);
  490. hp:=tai(hp.next);
  491. end;
  492. end;
  493. procedure TLLVMAssember.WriteExtraHeader;
  494. begin
  495. AsmWrite('target datalayout = "');
  496. AsmWrite(target_info.llvmdatalayout);
  497. AsmWriteln('"');
  498. AsmWrite('target triple = "');
  499. AsmWrite(llvm_target_name);
  500. AsmWriteln('"');
  501. end;
  502. procedure TLLVMAssember.WriteExtraFooter;
  503. begin
  504. end;
  505. procedure TLLVMAssember.WriteInstruction(hp: tai);
  506. begin
  507. end;
  508. procedure TLLVMAssember.WriteLlvmInstruction(hp: tai);
  509. begin
  510. InstrWriter.WriteInstruction(hp);
  511. end;
  512. procedure TLLVMAssember.WriteRealConst(hp: tai_realconst; do_line: boolean);
  513. begin
  514. if do_line and
  515. (fdecllevel=0) then
  516. begin
  517. case tai_realconst(hp).realtyp of
  518. aitrealconst_s32bit:
  519. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  520. aitrealconst_s64bit:
  521. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  522. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  523. { can't write full 80 bit floating point constants yet on non-x86 }
  524. aitrealconst_s80bit:
  525. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  526. {$endif cpuextended}
  527. aitrealconst_s64comp:
  528. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  529. else
  530. internalerror(2014050604);
  531. end;
  532. end;
  533. case hp.realtyp of
  534. aitrealconst_s32bit:
  535. AsmWriteln(llvmdoubletostr(hp.value.s32val));
  536. aitrealconst_s64bit:
  537. AsmWriteln(llvmdoubletostr(hp.value.s64val));
  538. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  539. aitrealconst_s80bit:
  540. AsmWriteln(llvmextendedtostr(hp.value.s80val));
  541. {$endif defined(cpuextended)}
  542. aitrealconst_s64comp:
  543. { handled as int64 most of the time in llvm }
  544. AsmWriteln(tostr(round(hp.value.s64compval)));
  545. else
  546. internalerror(2014062401);
  547. end;
  548. end;
  549. procedure TLLVMAssember.WriteOrdConst(hp: tai_const);
  550. var
  551. consttyp: taiconst_type;
  552. begin
  553. if fdecllevel=0 then
  554. asmwrite(target_asm.comment+' const ');
  555. consttyp:=hp.consttype;
  556. case consttyp of
  557. aitconst_got,
  558. aitconst_gotoff_symbol,
  559. aitconst_uleb128bit,
  560. aitconst_sleb128bit,
  561. aitconst_rva_symbol,
  562. aitconst_secrel32_symbol,
  563. aitconst_darwin_dwarf_delta32,
  564. aitconst_darwin_dwarf_delta64,
  565. aitconst_half16bit,
  566. aitconst_gs:
  567. internalerror(2014052901);
  568. aitconst_128bit,
  569. aitconst_64bit,
  570. aitconst_32bit,
  571. aitconst_16bit,
  572. aitconst_8bit,
  573. aitconst_16bit_unaligned,
  574. aitconst_32bit_unaligned,
  575. aitconst_64bit_unaligned:
  576. begin
  577. if fdecllevel=0 then
  578. AsmWrite(target_asm.comment);
  579. { can't have compile-time differences between symbols; these are
  580. normally for PIC, but llvm takes care of that for us }
  581. if assigned(hp.endsym) then
  582. internalerror(2014052902);
  583. if assigned(hp.sym) then
  584. begin
  585. AsmWrite(LlvmAsmSymName(hp.sym));
  586. { can't have offsets }
  587. if hp.value<>0 then
  588. if fdecllevel<>0 then
  589. internalerror(2014052903)
  590. else
  591. asmwrite(' -- symbol offset: ' + tostr(hp.value));
  592. end
  593. else if hp.value=0 then
  594. AsmWrite('zeroinitializer')
  595. else
  596. AsmWrite(tostr(hp.value));
  597. AsmLn;
  598. end;
  599. else
  600. internalerror(200704251);
  601. end;
  602. end;
  603. procedure TLLVMAssember.WriteTai(const replaceforbidden: boolean; const do_line: boolean; var InlineLevel: cardinal; var asmblock: boolean; var hp: tai);
  604. procedure WriteTypedConstData(hp: tai_abstracttypedconst);
  605. var
  606. p: tai_abstracttypedconst;
  607. pval: tai;
  608. defstr: TSymStr;
  609. first, gotstring: boolean;
  610. begin
  611. defstr:=llvmencodetypename(hp.def);
  612. { write the struct, array or simple type }
  613. case hp.adetyp of
  614. tck_record:
  615. begin
  616. AsmWrite(defstr);
  617. AsmWrite(' ');
  618. AsmWrite('<{');
  619. first:=true;
  620. for p in tai_aggregatetypedconst(hp) do
  621. begin
  622. if not first then
  623. AsmWrite(', ')
  624. else
  625. first:=false;
  626. WriteTypedConstData(p);
  627. end;
  628. AsmWrite('}>');
  629. end;
  630. tck_array:
  631. begin
  632. AsmWrite(defstr);
  633. first:=true;
  634. gotstring:=false;
  635. for p in tai_aggregatetypedconst(hp) do
  636. begin
  637. if not first then
  638. AsmWrite(',')
  639. else
  640. begin
  641. AsmWrite(' ');
  642. if (tai_abstracttypedconst(p).adetyp=tck_simple) and
  643. (tai_simpletypedconst(p).val.typ=ait_string) then
  644. begin
  645. gotstring:=true;
  646. end
  647. else
  648. begin
  649. AsmWrite('[');
  650. end;
  651. first:=false;
  652. end;
  653. { cannot concat strings and other things }
  654. if gotstring and
  655. ((tai_abstracttypedconst(p).adetyp<>tck_simple) or
  656. (tai_simpletypedconst(p).val.typ<>ait_string)) then
  657. internalerror(2014062701);
  658. WriteTypedConstData(p);
  659. end;
  660. if not gotstring then
  661. AsmWrite(']');
  662. end;
  663. tck_simple:
  664. begin
  665. pval:=tai_simpletypedconst(hp).val;
  666. if pval.typ<>ait_string then
  667. begin
  668. AsmWrite(defstr);
  669. AsmWrite(' ');
  670. end;
  671. WriteTai(replaceforbidden,do_line,InlineLevel,asmblock,pval);
  672. end;
  673. end;
  674. end;
  675. var
  676. hp2: tai;
  677. s: string;
  678. i: longint;
  679. ch: ansichar;
  680. begin
  681. case hp.typ of
  682. ait_comment :
  683. begin
  684. AsmWrite(target_asm.comment);
  685. AsmWritePChar(tai_comment(hp).str);
  686. AsmLn;
  687. end;
  688. ait_regalloc :
  689. begin
  690. if (cs_asm_regalloc in current_settings.globalswitches) then
  691. begin
  692. AsmWrite(#9+target_asm.comment+'Register ');
  693. repeat
  694. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  695. if (hp.next=nil) or
  696. (tai(hp.next).typ<>ait_regalloc) or
  697. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  698. break;
  699. hp:=tai(hp.next);
  700. AsmWrite(',');
  701. until false;
  702. AsmWrite(' ');
  703. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  704. end;
  705. end;
  706. ait_tempalloc :
  707. begin
  708. if (cs_asm_tempalloc in current_settings.globalswitches) then
  709. WriteTempalloc(tai_tempalloc(hp));
  710. end;
  711. ait_align,
  712. ait_section :
  713. begin
  714. { ignore, specified as part of declarations -- don't write
  715. comment, because could appear in the middle of an aggregate
  716. constant definition }
  717. end;
  718. ait_datablock :
  719. begin
  720. AsmWrite(target_asm.comment);
  721. AsmWriteln('datablock');
  722. end;
  723. ait_const:
  724. begin
  725. WriteOrdConst(tai_const(hp));
  726. end;
  727. ait_realconst :
  728. begin
  729. WriteRealConst(tai_realconst(hp), do_line);
  730. end;
  731. ait_string :
  732. begin
  733. if fdecllevel=0 then
  734. AsmWrite(target_asm.comment);
  735. AsmWrite('c"');
  736. for i:=1 to tai_string(hp).len do
  737. begin
  738. ch:=tai_string(hp).str[i-1];
  739. case ch of
  740. #0, {This can't be done by range, because a bug in FPC}
  741. #1..#31,
  742. #128..#255,
  743. '"',
  744. '\' : s:='\'+hexStr(ord(ch),2);
  745. else
  746. s:=ch;
  747. end;
  748. AsmWrite(s);
  749. end;
  750. AsmWriteLn('"');
  751. end;
  752. ait_label :
  753. begin
  754. if not asmblock and
  755. (tai_label(hp).labsym.is_used) then
  756. begin
  757. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  758. begin
  759. { should be emitted as part of the variable/function def }
  760. internalerror(2013010703);
  761. end;
  762. if tai_label(hp).labsym.bind in [AB_GLOBAL, AB_PRIVATE_EXTERN] then
  763. begin
  764. { should be emitted as part of the variable/function def }
  765. //internalerror(2013010704);
  766. AsmWriteln(target_asm.comment+'global/privateextern label: '+tai_label(hp).labsym.name);
  767. end;
  768. if replaceforbidden then
  769. AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  770. else
  771. AsmWrite(tai_label(hp).labsym.name);
  772. AsmWriteLn(':');
  773. end;
  774. end;
  775. ait_symbol :
  776. begin
  777. if fdecllevel=0 then
  778. AsmWrite(target_asm.comment);
  779. AsmWriteln(LlvmAsmSymName(tai_symbol(hp).sym));
  780. { todo }
  781. if tai_symbol(hp).has_value then
  782. internalerror(2014062402);
  783. end;
  784. ait_llvmdecl:
  785. begin
  786. if taillvmdecl(hp).def.typ=procdef then
  787. begin
  788. if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
  789. begin
  790. asmwrite('declare');
  791. asmwriteln(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), taillvmdecl(hp).namesym.name, lpd_decl));
  792. end
  793. else
  794. begin
  795. asmwrite('define');
  796. asmwrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), '', lpd_decl));
  797. asmwriteln(' {');
  798. end;
  799. end
  800. else
  801. begin
  802. asmwrite(LlvmAsmSymName(taillvmdecl(hp).namesym));
  803. case taillvmdecl(hp).namesym.bind of
  804. AB_EXTERNAL:
  805. asmwrite(' = external ');
  806. AB_COMMON:
  807. asmwrite(' = common ');
  808. AB_LOCAL:
  809. asmwrite(' = internal ');
  810. AB_GLOBAL:
  811. asmwrite(' = ');
  812. AB_WEAK_EXTERNAL:
  813. asmwrite(' = extern_weak ');
  814. AB_PRIVATE_EXTERN:
  815. asmwrite('= linker_private ');
  816. else
  817. internalerror(2014020104);
  818. end;
  819. if taillvmdecl(hp).tls then
  820. asmwrite('thread_local ');
  821. { todo: handle more different section types (mainly
  822. Objective-C }
  823. if taillvmdecl(hp).sec in [sec_rodata,sec_rodata_norel] then
  824. asmwrite('unnamed_addr constant ')
  825. else
  826. asmwrite('global ');
  827. if not assigned(taillvmdecl(hp).initdata) then
  828. begin
  829. asmwrite(llvmencodetypename(taillvmdecl(hp).def));
  830. if not(taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL]) then
  831. asmwrite(' zeroinitializer');
  832. end
  833. else
  834. begin
  835. inc(fdecllevel);
  836. { can't have an external symbol with initialisation data }
  837. if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
  838. internalerror(2014052905);
  839. { bitcast initialisation data to the type of the constant }
  840. { write initialisation data }
  841. hp2:=tai(taillvmdecl(hp).initdata.first);
  842. while assigned(hp2) do
  843. begin
  844. WriteTai(replaceforbidden,do_line,InlineLevel,asmblock,hp2);
  845. hp2:=tai(hp2.next);
  846. end;
  847. dec(fdecllevel);
  848. end;
  849. { alignment }
  850. asmwrite(', align ');
  851. asmwriteln(tostr(taillvmdecl(hp).alignment));
  852. end;
  853. end;
  854. ait_llvmalias:
  855. begin
  856. asmwrite(LlvmAsmSymName(taillvmalias(hp).newsym));
  857. asmwrite(' = alias ');
  858. if taillvmalias(hp).linkage<>lll_default then
  859. begin
  860. str(taillvmalias(hp).linkage, s);
  861. asmwrite(copy(s, length('lll_'), 255));
  862. asmwrite(' ');
  863. end
  864. else
  865. asmwrite('external ');
  866. if taillvmalias(hp).vis<>llv_default then
  867. begin
  868. str(taillvmalias(hp).vis, s);
  869. asmwrite(copy(s, length('llv_'), 255));
  870. asmwrite(' ');
  871. end;
  872. asmwrite(llvmencodeproctype(tabstractprocdef(taillvmalias(hp).def), '', lpd_alias));
  873. asmwrite('* ');
  874. asmwriteln(LlvmAsmSymName(taillvmalias(hp).oldsym));
  875. end;
  876. ait_symbolpair:
  877. begin
  878. { should be emitted as part of the symbol def }
  879. internalerror(2013010708);
  880. end;
  881. ait_weak:
  882. begin
  883. { should be emitted as part of the symbol def }
  884. internalerror(2013010709);
  885. end;
  886. ait_symbol_end :
  887. begin
  888. if tai_symbol_end(hp).sym.typ=AT_FUNCTION then
  889. asmwriteln('}')
  890. else
  891. asmwriteln('; ait_symbol_end error, should not be generated');
  892. // internalerror(2013010711);
  893. end;
  894. ait_instruction :
  895. begin
  896. WriteInstruction(hp);
  897. end;
  898. ait_llvmins:
  899. begin
  900. WriteLlvmInstruction(hp);
  901. end;
  902. ait_stab :
  903. begin
  904. internalerror(2013010712);
  905. end;
  906. ait_force_line,
  907. ait_function_name :
  908. ;
  909. ait_cutobject :
  910. begin
  911. end;
  912. ait_marker :
  913. case
  914. tai_marker(hp).kind of
  915. mark_NoLineInfoStart:
  916. inc(InlineLevel);
  917. mark_NoLineInfoEnd:
  918. dec(InlineLevel);
  919. { these cannot be nested }
  920. mark_AsmBlockStart:
  921. asmblock:=true;
  922. mark_AsmBlockEnd:
  923. asmblock:=false;
  924. end;
  925. ait_directive :
  926. begin
  927. WriteDirectiveName(tai_directive(hp).directive);
  928. if tai_directive(hp).name <>'' then
  929. AsmWrite(tai_directive(hp).name);
  930. AsmLn;
  931. end;
  932. ait_seh_directive :
  933. begin
  934. internalerror(2013010713);
  935. end;
  936. ait_varloc:
  937. begin
  938. if tai_varloc(hp).newlocationhi<>NR_NO then
  939. AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  940. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  941. else
  942. AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  943. std_regname(tai_varloc(hp).newlocation)));
  944. AsmLn;
  945. end;
  946. ait_typedconst:
  947. begin
  948. WriteTypedConstData(tai_abstracttypedconst(hp));
  949. end
  950. else
  951. internalerror(2006012201);
  952. end;
  953. end;
  954. constructor TLLVMAssember.create(smart: boolean);
  955. begin
  956. inherited create(smart);
  957. InstrWriter:=TLLVMInstrWriter.create(self);
  958. end;
  959. procedure TLLVMAssember.AsmLn;
  960. begin
  961. { don't write newlines in the middle of declarations }
  962. if fdecllevel=0 then
  963. inherited AsmLn;
  964. end;
  965. procedure TLLVMAssember.WriteDirectiveName(dir: TAsmDirective);
  966. begin
  967. AsmWrite('.'+directivestr[dir]+' ');
  968. end;
  969. procedure TLLVMAssember.WriteAsmList;
  970. var
  971. hal : tasmlisttype;
  972. i: longint;
  973. begin
  974. WriteExtraHeader;
  975. AsmStartSize:=AsmSize;
  976. for hal:=low(TasmlistType) to high(TasmlistType) do
  977. begin
  978. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  979. writetree(current_asmdata.asmlists[hal]);
  980. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  981. end;
  982. AsmLn;
  983. end;
  984. {****************************************************************************}
  985. { Abstract Instruction Writer }
  986. {****************************************************************************}
  987. constructor TLLVMInstrWriter.create(_owner: TLLVMAssember);
  988. begin
  989. inherited create;
  990. owner := _owner;
  991. end;
  992. const
  993. as_llvm_info : tasminfo =
  994. (
  995. id : as_llvm;
  996. idtxt : 'LLVM-AS';
  997. asmbin : 'llc';
  998. asmcmd: '$OPT -o $OBJ $ASM';
  999. supported_targets : [system_x86_64_linux,system_x86_64_darwin,system_powerpc64_darwin];
  1000. flags : [af_smartlink_sections];
  1001. labelprefix : 'L';
  1002. comment : '; ';
  1003. dollarsign: '$';
  1004. );
  1005. begin
  1006. RegisterAssembler(as_llvm_info,TLLVMAssember);
  1007. end.