agllvm.pas 36 KB

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