agllvm.pas 33 KB

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