agllvm.pas 36 KB

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