agllvm.pas 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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+ref.symbol.name;
  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. if o.ref^.symbol.typ=AT_LABEL then
  237. getopstr:='label %';
  238. hs:=o.ref^.symbol.name;
  239. if o.ref^.offset<>0 then
  240. internalerror(2013060223);
  241. getopstr:=getopstr+hs;
  242. end
  243. else
  244. getopstr:=getreferencestring(o.ref^,refwithalign);
  245. top_def:
  246. begin
  247. getopstr:=llvmencodetype(o.def);
  248. end;
  249. top_cond:
  250. begin
  251. getopstr:=llvm_cond2str[o.cond];
  252. end;
  253. top_fpcond:
  254. begin
  255. getopstr:=llvm_fpcond2str[o.fpcond];
  256. end;
  257. top_single,
  258. top_double:
  259. begin
  260. { "When using the hexadecimal form, constants of types half,
  261. float, and double are represented using the 16-digit form shown
  262. above (which matches the IEEE754 representation for double)"
  263. And always in big endian form (sign bit leftmost)
  264. }
  265. if o.typ=top_double then
  266. result:=llvmdoubletostr(o.dval)
  267. else
  268. result:=llvmdoubletostr(o.sval)
  269. end;
  270. top_para:
  271. begin
  272. result:=getparas(o);
  273. end;
  274. top_tai:
  275. begin
  276. tmpinline:=1;
  277. hp:=o.ai;
  278. owner.AsmWrite(fstr);
  279. fstr:='';
  280. owner.WriteTai(false,false,tmpinline,hp);
  281. result:='';
  282. end;
  283. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  284. top_extended80:
  285. begin
  286. result:=llvmextendedtostr(o.eval);
  287. end;
  288. {$endif cpuextended}
  289. else
  290. internalerror(2013060227);
  291. end;
  292. end;
  293. procedure TLLVMInstrWriter.WriteInstruction(hp: tai);
  294. var
  295. op: tllvmop;
  296. sep: TSymStr;
  297. i, opstart: byte;
  298. nested: boolean;
  299. done: boolean;
  300. begin
  301. op:=taillvm(hp).llvmopcode;
  302. { we write everything immediately rather than adding it into a string,
  303. because operands may contain other tai that will also write things out
  304. (and their output must come after everything that was processed in this
  305. instruction, such as its opcode or previous operands) }
  306. if owner.fdecllevel=0 then
  307. owner.AsmWrite(#9);
  308. sep:=' ';
  309. done:=false;
  310. opstart:=0;
  311. nested:=false;
  312. case op of
  313. la_ret, la_br, la_switch, la_indirectbr,
  314. la_invoke, la_resume,
  315. la_unreachable,
  316. la_store,
  317. la_fence,
  318. la_cmpxchg,
  319. la_atomicrmw:
  320. begin
  321. { instructions that never have a result }
  322. end;
  323. la_call:
  324. begin
  325. if taillvm(hp).oper[0]^.reg<>NR_NO then
  326. owner.AsmWrite(getregisterstring(taillvm(hp).oper[0]^.reg)+' = ');
  327. sep:=' ';
  328. opstart:=1;
  329. end;
  330. la_alloca:
  331. begin
  332. owner.AsmWrite(getreferencestring(taillvm(hp).oper[0]^.ref^,false)+' = ');
  333. sep:=' ';
  334. opstart:=1;
  335. end;
  336. la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
  337. la_fptoui, la_fptosi, la_uitofp, la_sitofp,
  338. la_ptrtoint, la_inttoptr,
  339. la_bitcast:
  340. begin
  341. { destination can be empty in case of nested constructs, or
  342. data initialisers }
  343. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  344. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  345. owner.AsmWrite(getopstr(taillvm(hp).oper[0]^,false)+' = ')
  346. else
  347. nested:=true;
  348. owner.AsmWrite(llvm_op2str[op]);
  349. if not nested then
  350. owner.AsmWrite(' ')
  351. else
  352. owner.AsmWrite(' (');
  353. owner.AsmWrite(getopstr(taillvm(hp).oper[1]^,false));
  354. owner.AsmWrite(' ');
  355. owner.AsmWrite(getopstr(taillvm(hp).oper[2]^,false));
  356. owner.AsmWrite(' to ');
  357. owner.AsmWrite(getopstr(taillvm(hp).oper[3]^,false));
  358. done:=true;
  359. end
  360. else
  361. begin
  362. if (taillvm(hp).oper[0]^.typ<>top_reg) or
  363. (taillvm(hp).oper[0]^.reg<>NR_NO) then
  364. begin
  365. owner.AsmWrite(getopstr(taillvm(hp).oper[0]^,true)+' = ');
  366. end
  367. else
  368. nested:=true;
  369. sep:=' ';
  370. opstart:=1
  371. end;
  372. end;
  373. { process operands }
  374. if not done then
  375. begin
  376. owner.AsmWrite(llvm_op2str[op]);
  377. if nested then
  378. owner.AsmWrite(' (');
  379. if taillvm(hp).ops<>0 then
  380. begin
  381. for i:=opstart to taillvm(hp).ops-1 do
  382. begin
  383. owner.AsmWrite(sep);
  384. owner.AsmWrite(getopstr(taillvm(hp).oper[i]^,op in [la_load,la_store]));
  385. if (taillvm(hp).oper[i]^.typ in [top_def,top_cond,top_fpcond]) or
  386. (op=la_call) then
  387. sep :=' '
  388. else
  389. sep:=', ';
  390. end;
  391. end;
  392. end;
  393. if op=la_alloca then
  394. owner.AsmWrite(getreferencealignstring(taillvm(hp).oper[0]^.ref^));
  395. if nested then
  396. owner.AsmWrite(')');
  397. owner.AsmLn;
  398. end;
  399. {****************************************************************************}
  400. { LLVM Assembler writer }
  401. {****************************************************************************}
  402. destructor TLLVMAssember.Destroy;
  403. begin
  404. InstrWriter.free;
  405. inherited destroy;
  406. end;
  407. function TLLVMAssember.MakeCmdLine: TCmdStr;
  408. var
  409. optstr: TCmdStr;
  410. begin
  411. result := inherited MakeCmdLine;
  412. { standard optimization flags for llc -- todo: this needs to be split
  413. into a call to opt and one to llc }
  414. if cs_opt_level3 in current_settings.optimizerswitches then
  415. optstr:='-O3'
  416. else if cs_opt_level2 in current_settings.optimizerswitches then
  417. optstr:='-O2'
  418. else if cs_opt_level1 in current_settings.optimizerswitches then
  419. optstr:='-O1'
  420. else
  421. optstr:='-O0';
  422. { stack frame elimination }
  423. if not(cs_opt_stackframe in current_settings.optimizerswitches) then
  424. optstr:=optstr+' -disable-fp-elim';
  425. { fast math }
  426. if cs_opt_fastmath in current_settings.optimizerswitches then
  427. optstr:=optstr+' -enable-unsafe-fp-math -enable-fp-mad -fp-contract=fast';
  428. { smart linking }
  429. if cs_create_smart in current_settings.moduleswitches then
  430. optstr:=optstr+' -fdata-sections -fcode-sections';
  431. { pic }
  432. if cs_create_pic in current_settings.moduleswitches then
  433. optstr:=optstr+' -relocation-model=pic'
  434. else if not(target_info.system in systems_darwin) then
  435. optstr:=optstr+' -relocation-model=static'
  436. else
  437. optstr:=optstr+' -relocation-model=dynamic-no-pic';
  438. { our stack alignment is non-standard on some targets. The following
  439. parameter is however ignored on some targets by llvm, so it may not
  440. be enough }
  441. optstr:=optstr+' -stack-alignment='+tostr(target_info.stackalign*8);
  442. { force object output instead of textual assembler code }
  443. optstr:=optstr+' -filetype=obj';
  444. replace(result,'$OPT',optstr);
  445. end;
  446. procedure TLLVMAssember.WriteTree(p:TAsmList);
  447. var
  448. hp : tai;
  449. InlineLevel : cardinal;
  450. do_line : boolean;
  451. replaceforbidden: boolean;
  452. begin
  453. if not assigned(p) then
  454. exit;
  455. replaceforbidden:=target_asm.dollarsign<>'$';
  456. InlineLevel:=0;
  457. { lineinfo is only needed for al_procedures (PFV) }
  458. do_line:=(cs_asm_source in current_settings.globalswitches) or
  459. ((cs_lineinfo in current_settings.moduleswitches)
  460. and (p=current_asmdata.asmlists[al_procedures]));
  461. hp:=tai(p.first);
  462. while assigned(hp) do
  463. begin
  464. prefetch(pointer(hp.next)^);
  465. if not(hp.typ in SkipLineInfo) then
  466. begin
  467. current_filepos:=tailineinfo(hp).fileinfo;
  468. { no line info for inlined code }
  469. if do_line and (inlinelevel=0) then
  470. WriteSourceLine(hp as tailineinfo);
  471. end;
  472. WriteTai(replaceforbidden, do_line, InlineLevel, hp);
  473. hp:=tai(hp.next);
  474. end;
  475. end;
  476. procedure TLLVMAssember.WriteExtraHeader;
  477. begin
  478. AsmWrite('target datalayout = "');
  479. AsmWrite(target_info.llvmdatalayout);
  480. AsmWriteln('"');
  481. AsmWrite('target triple = "');
  482. AsmWrite(llvm_target_name);
  483. AsmWriteln('"');
  484. end;
  485. procedure TLLVMAssember.WriteExtraFooter;
  486. begin
  487. end;
  488. procedure TLLVMAssember.WriteInstruction(hp: tai);
  489. begin
  490. end;
  491. procedure TLLVMAssember.WriteLlvmInstruction(hp: tai);
  492. begin
  493. InstrWriter.WriteInstruction(hp);
  494. end;
  495. procedure TLLVMAssember.WriteWeakSymbolDef(s: tasmsymbol);
  496. begin
  497. AsmWriteLn(#9'.weak '+s.name);
  498. end;
  499. procedure TLLVMAssember.WriteRealConst(hp: tai_realconst; do_line: boolean);
  500. begin
  501. if do_line and
  502. (fdecllevel=0) then
  503. begin
  504. case tai_realconst(hp).realtyp of
  505. aitrealconst_s32bit:
  506. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_realconst(hp).value.s32val));
  507. aitrealconst_s64bit:
  508. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_realconst(hp).value.s64val));
  509. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  510. { can't write full 80 bit floating point constants yet on non-x86 }
  511. aitrealconst_s80bit:
  512. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_realconst(hp).value.s80val));
  513. {$endif cpuextended}
  514. aitrealconst_s64comp:
  515. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_realconst(hp).value.s64compval));
  516. else
  517. internalerror(2014050604);
  518. end;
  519. end;
  520. case hp.realtyp of
  521. aitrealconst_s32bit:
  522. AsmWriteln(llvmdoubletostr(hp.value.s32val));
  523. aitrealconst_s64bit:
  524. AsmWriteln(llvmdoubletostr(hp.value.s64val));
  525. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  526. aitrealconst_s80bit:
  527. AsmWriteln(llvmextendedtostr(hp.value.s80val));
  528. {$endif defined(cpuextended)}
  529. aitrealconst_s64comp:
  530. { handled as int64 most of the time in llvm }
  531. AsmWriteln(tostr(round(hp.value.s64compval)));
  532. else
  533. internalerror(2014062401);
  534. end;
  535. end;
  536. procedure TLLVMAssember.WriteOrdConst(hp: tai_const);
  537. var
  538. consttyp: taiconst_type;
  539. begin
  540. if fdecllevel=0 then
  541. asmwrite(target_asm.comment+' const ');
  542. consttyp:=hp.consttype;
  543. case consttyp of
  544. aitconst_got,
  545. aitconst_gotoff_symbol,
  546. aitconst_uleb128bit,
  547. aitconst_sleb128bit,
  548. aitconst_rva_symbol,
  549. aitconst_secrel32_symbol,
  550. aitconst_darwin_dwarf_delta32,
  551. aitconst_darwin_dwarf_delta64,
  552. aitconst_half16bit:
  553. internalerror(2014052901);
  554. aitconst_128bit,
  555. aitconst_64bit,
  556. aitconst_32bit,
  557. aitconst_16bit,
  558. aitconst_8bit,
  559. aitconst_16bit_unaligned,
  560. aitconst_32bit_unaligned,
  561. aitconst_64bit_unaligned:
  562. begin
  563. if fdecllevel=0 then
  564. AsmWrite(target_asm.comment);
  565. { can't have compile-time differences between symbols; these are
  566. normally for PIC, but llvm takes care of that for us }
  567. if assigned(hp.endsym) then
  568. internalerror(2014052902);
  569. if assigned(hp.sym) then
  570. begin
  571. AsmWrite(hp.sym.name);
  572. { can't have offsets }
  573. if hp.value<>0 then
  574. if fdecllevel<>0 then
  575. internalerror(2014052903)
  576. else
  577. asmwrite(' -- symbol offset: ' + tostr(hp.value));
  578. end
  579. else
  580. AsmWrite(tostr(hp.value));
  581. AsmLn;
  582. end;
  583. else
  584. internalerror(200704251);
  585. end;
  586. end;
  587. procedure TLLVMAssember.WriteTai(const replaceforbidden: boolean; const do_line: boolean; var InlineLevel: cardinal; var hp: tai);
  588. procedure WriteTypedConstData(hp: tai_abstracttypedconst);
  589. var
  590. p: tai_abstracttypedconst;
  591. pval: tai;
  592. defstr: TSymStr;
  593. first, gotstring: boolean;
  594. begin
  595. { special case: tck_simple_procvar2proc; this means that we want the
  596. procdef of the procvardef, rather than both the procdef and the
  597. method/nestedfp/... pointers }
  598. if hp.adetyp<>tck_simple_procvar2proc then
  599. defstr:=llvmencodetype(hp.def)
  600. else
  601. defstr:=llvmencodeproctype(tabstractprocdef(hp.def),'',lpd_procvar);
  602. { write the struct, array or simple type }
  603. case hp.adetyp of
  604. tck_record:
  605. begin
  606. AsmWrite(defstr);
  607. AsmWrite(' ');
  608. AsmWrite('<{');
  609. first:=true;
  610. for p in tai_aggregatetypedconst(hp) do
  611. begin
  612. if not first then
  613. AsmWrite(', ')
  614. else
  615. first:=false;
  616. WriteTypedConstData(p);
  617. end;
  618. AsmWrite('}>');
  619. end;
  620. tck_array:
  621. begin
  622. AsmWrite(defstr);
  623. first:=true;
  624. gotstring:=false;
  625. for p in tai_aggregatetypedconst(hp) do
  626. begin
  627. if not first then
  628. AsmWrite(',')
  629. else
  630. begin
  631. AsmWrite(' ');
  632. if (tai_abstracttypedconst(p).adetyp=tck_simple) and
  633. (tai_simpletypedconst(p).val.typ=ait_string) then
  634. begin
  635. gotstring:=true;
  636. end
  637. else
  638. begin
  639. AsmWrite('[');
  640. end;
  641. first:=false;
  642. end;
  643. { cannot concat strings and other things }
  644. if gotstring and
  645. ((tai_abstracttypedconst(p).adetyp<>tck_simple) or
  646. (tai_simpletypedconst(p).val.typ<>ait_string)) then
  647. internalerror(2014062701);
  648. WriteTypedConstData(p);
  649. end;
  650. if not gotstring then
  651. AsmWrite(']');
  652. end;
  653. tck_simple,
  654. tck_simple_procvar2proc:
  655. begin
  656. pval:=tai_simpletypedconst(hp).val;
  657. if pval.typ<>ait_string then
  658. begin
  659. AsmWrite(defstr);
  660. AsmWrite(' ');
  661. end;
  662. WriteTai(replaceforbidden,do_line,InlineLevel,pval);
  663. end;
  664. end;
  665. end;
  666. var
  667. hp2: tai;
  668. s: string;
  669. i: longint;
  670. ch: ansichar;
  671. begin
  672. case hp.typ of
  673. ait_comment :
  674. begin
  675. AsmWrite(target_asm.comment);
  676. AsmWritePChar(tai_comment(hp).str);
  677. AsmLn;
  678. end;
  679. ait_regalloc :
  680. begin
  681. if (cs_asm_regalloc in current_settings.globalswitches) then
  682. begin
  683. AsmWrite(#9+target_asm.comment+'Register ');
  684. repeat
  685. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  686. if (hp.next=nil) or
  687. (tai(hp.next).typ<>ait_regalloc) or
  688. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  689. break;
  690. hp:=tai(hp.next);
  691. AsmWrite(',');
  692. until false;
  693. AsmWrite(' ');
  694. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  695. end;
  696. end;
  697. ait_tempalloc :
  698. begin
  699. if (cs_asm_tempalloc in current_settings.globalswitches) then
  700. WriteTempalloc(tai_tempalloc(hp));
  701. end;
  702. ait_align :
  703. begin
  704. { has to be specified as part of the symbol declaration }
  705. AsmWriteln('; error: explicit aligns are forbidden');
  706. // internalerror(2013010714);
  707. end;
  708. ait_section :
  709. begin
  710. AsmWrite(target_asm.comment);
  711. AsmWriteln('section');
  712. end;
  713. ait_datablock :
  714. begin
  715. AsmWrite(target_asm.comment);
  716. AsmWriteln('datablock');
  717. end;
  718. ait_const:
  719. begin
  720. WriteOrdConst(tai_const(hp));
  721. end;
  722. ait_realconst :
  723. begin
  724. WriteRealConst(tai_realconst(hp), do_line);
  725. end;
  726. ait_string :
  727. begin
  728. if fdecllevel=0 then
  729. AsmWrite(target_asm.comment);
  730. AsmWrite('c"');
  731. for i:=1 to tai_string(hp).len do
  732. begin
  733. ch:=tai_string(hp).str[i-1];
  734. case ch of
  735. #0, {This can't be done by range, because a bug in FPC}
  736. #1..#31,
  737. #128..#255,
  738. '"',
  739. '\' : s:='\'+tostr(ord(ch) shr 8)+tostr((ord(ch) and $f));
  740. else
  741. s:=ch;
  742. end;
  743. AsmWrite(s);
  744. end;
  745. AsmWriteLn('"');
  746. end;
  747. ait_label :
  748. begin
  749. if (tai_label(hp).labsym.is_used) then
  750. begin
  751. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  752. begin
  753. { should be emitted as part of the variable/function def }
  754. internalerror(2013010703);
  755. end;
  756. if tai_label(hp).labsym.bind in [AB_GLOBAL, AB_PRIVATE_EXTERN] then
  757. begin
  758. { should be emitted as part of the variable/function def }
  759. //internalerror(2013010704);
  760. AsmWriteln(target_asm.comment+'global/privateextern label: '+tai_label(hp).labsym.name);
  761. end;
  762. if replaceforbidden then
  763. AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  764. else
  765. AsmWrite(tai_label(hp).labsym.name);
  766. AsmWriteLn(':');
  767. end;
  768. end;
  769. ait_symbol :
  770. begin
  771. if fdecllevel=0 then
  772. AsmWrite(target_asm.comment);
  773. asmwriteln(tai_symbol(hp).sym.name);
  774. { todo }
  775. if tai_symbol(hp).has_value then
  776. internalerror(2014062402);
  777. end;
  778. ait_llvmdecl:
  779. begin
  780. if taillvmdecl(hp).def.typ=procdef then
  781. begin
  782. if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
  783. begin
  784. asmwrite('declare');
  785. asmwriteln(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), taillvmdecl(hp).namesym.name, lpd_decl));
  786. end
  787. else
  788. begin
  789. asmwrite('define');
  790. asmwrite(llvmencodeproctype(tprocdef(taillvmdecl(hp).def), '', lpd_decl));
  791. asmwriteln(' {');
  792. end;
  793. end
  794. else
  795. begin
  796. asmwrite(taillvmdecl(hp).namesym.name);
  797. case taillvmdecl(hp).namesym.bind of
  798. AB_EXTERNAL:
  799. asmwrite(' = external global ');
  800. AB_COMMON:
  801. asmwrite(' = common global ');
  802. AB_LOCAL:
  803. asmwrite(' = internal global ');
  804. AB_GLOBAL:
  805. asmwrite(' = global ');
  806. AB_WEAK_EXTERNAL:
  807. asmwrite(' = extern_weak global ');
  808. AB_PRIVATE_EXTERN:
  809. asmwrite('= linker_private global ');
  810. else
  811. internalerror(2014020104);
  812. end;
  813. if not assigned(taillvmdecl(hp).initdata) then
  814. begin
  815. asmwrite(llvmencodetype(taillvmdecl(hp).def));
  816. if not(taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL]) then
  817. asmwrite(' zeroinitializer');
  818. end
  819. else
  820. begin
  821. inc(fdecllevel);
  822. { can't have an external symbol with initialisation data }
  823. if taillvmdecl(hp).namesym.bind in [AB_EXTERNAL, AB_WEAK_EXTERNAL] then
  824. internalerror(2014052905);
  825. { bitcast initialisation data to the type of the constant }
  826. { write initialisation data }
  827. hp2:=tai(taillvmdecl(hp).initdata.first);
  828. while assigned(hp2) do
  829. begin
  830. WriteTai(replaceforbidden,do_line,InlineLevel,hp2);
  831. hp2:=tai(hp2.next);
  832. end;
  833. dec(fdecllevel);
  834. end;
  835. { alignment }
  836. asmwrite(', align ');
  837. asmwriteln(tostr(taillvmdecl(hp).def.alignment));
  838. end;
  839. end;
  840. ait_llvmalias:
  841. begin
  842. asmwrite('@'+taillvmalias(hp).newsym.name);
  843. asmwrite(' = alias ');
  844. if taillvmalias(hp).linkage<>lll_default then
  845. begin
  846. str(taillvmalias(hp).linkage, s);
  847. asmwrite(copy(s, length('lll_'), 255));
  848. asmwrite(' ');
  849. end
  850. else
  851. asmwrite('external ');
  852. if taillvmalias(hp).vis<>llv_default then
  853. begin
  854. str(taillvmalias(hp).vis, s);
  855. asmwrite(copy(s, length('llv_'), 255));
  856. asmwrite(' ');
  857. end;
  858. asmwrite(llvmencodeproctype(tabstractprocdef(taillvmalias(hp).def), '', lpd_alias));
  859. asmwrite('* ');
  860. asmwriteln(taillvmalias(hp).oldsym.name);
  861. end;
  862. {$ifdef arm}
  863. ait_thumb_func:
  864. begin
  865. { should be emitted as part of the function def }
  866. internalerror(2013010706);
  867. end;
  868. ait_thumb_set:
  869. begin
  870. { should be emitted as part of the symbol def }
  871. internalerror(2013010707);
  872. end;
  873. {$endif arm}
  874. ait_set:
  875. begin
  876. { should be emitted as part of the symbol def }
  877. internalerror(2013010708);
  878. end;
  879. ait_weak:
  880. begin
  881. { should be emitted as part of the symbol def }
  882. internalerror(2013010709);
  883. end;
  884. ait_symbol_end :
  885. begin
  886. if tai_symbol_end(hp).sym.typ=AT_FUNCTION then
  887. asmwriteln('}')
  888. else
  889. asmwriteln('; ait_symbol_end error, should not be generated');
  890. // internalerror(2013010711);
  891. end;
  892. ait_instruction :
  893. begin
  894. WriteInstruction(hp);
  895. end;
  896. ait_llvmins:
  897. begin
  898. WriteLlvmInstruction(hp);
  899. end;
  900. ait_stab :
  901. begin
  902. internalerror(2013010712);
  903. end;
  904. ait_force_line,
  905. ait_function_name :
  906. ;
  907. ait_cutobject :
  908. begin
  909. end;
  910. ait_marker :
  911. if tai_marker(hp).kind=mark_NoLineInfoStart then
  912. inc(InlineLevel)
  913. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  914. dec(InlineLevel);
  915. ait_directive :
  916. begin
  917. WriteDirectiveName(tai_directive(hp).directive);
  918. if tai_directive(hp).name <>'' then
  919. AsmWrite(tai_directive(hp).name);
  920. AsmLn;
  921. end;
  922. ait_seh_directive :
  923. begin
  924. internalerror(2013010713);
  925. end;
  926. ait_varloc:
  927. begin
  928. if tai_varloc(hp).newlocationhi<>NR_NO then
  929. AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  930. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  931. else
  932. AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  933. std_regname(tai_varloc(hp).newlocation)));
  934. AsmLn;
  935. end;
  936. ait_typedconst:
  937. begin
  938. WriteTypedConstData(tai_abstracttypedconst(hp));
  939. end
  940. else
  941. internalerror(2006012201);
  942. end;
  943. end;
  944. constructor TLLVMAssember.create(smart: boolean);
  945. begin
  946. inherited create(smart);
  947. InstrWriter:=TLLVMInstrWriter.create(self);
  948. end;
  949. procedure TLLVMAssember.AsmLn;
  950. begin
  951. { don't write newlines in the middle of declarations }
  952. if fdecllevel=0 then
  953. inherited AsmLn;
  954. end;
  955. procedure TLLVMAssember.WriteDirectiveName(dir: TAsmDirective);
  956. begin
  957. AsmWrite('.'+directivestr[dir]+' ');
  958. end;
  959. procedure TLLVMAssember.WriteAsmList;
  960. var
  961. n : string;
  962. hal : tasmlisttype;
  963. i: longint;
  964. begin
  965. if current_module.mainsource<>'' then
  966. n:=ExtractFileName(current_module.mainsource)
  967. else
  968. n:=InputFileName;
  969. { gcc does not add it either for Darwin. Grep for
  970. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  971. }
  972. if not(target_info.system in systems_darwin) then
  973. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  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. { add weak symbol markers }
  983. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  984. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  985. writeweaksymboldef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  986. AsmLn;
  987. end;
  988. {****************************************************************************}
  989. { Abstract Instruction Writer }
  990. {****************************************************************************}
  991. constructor TLLVMInstrWriter.create(_owner: TLLVMAssember);
  992. begin
  993. inherited create;
  994. owner := _owner;
  995. end;
  996. const
  997. as_llvm_info : tasminfo =
  998. (
  999. id : as_llvm;
  1000. idtxt : 'LLVM-AS';
  1001. asmbin : 'llc';
  1002. asmcmd: '$OPT -o $OBJ $ASM';
  1003. supported_targets : [system_x86_64_linux,system_x86_64_darwin,system_powerpc64_darwin];
  1004. flags : [af_smartlink_sections];
  1005. labelprefix : 'L';
  1006. comment : '; ';
  1007. dollarsign: '$';
  1008. );
  1009. begin
  1010. RegisterAssembler(as_llvm_info,TLLVMAssember);
  1011. end.