agllvm.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. procedure WriteExtraHeader;virtual;
  30. procedure WriteExtraFooter;virtual;
  31. procedure WriteInstruction(hp: tai);
  32. procedure WriteLlvmInstruction(hp: tai);
  33. // procedure WriteWeakSymbolDef(s: tasmsymbol); virtual;
  34. procedure WriteDirectiveName(dir: TAsmDirective); virtual;
  35. procedure WriteWeakSymbolDef(s: tasmsymbol);
  36. public
  37. constructor create(smart: boolean); override;
  38. function MakeCmdLine: TCmdStr; override;
  39. procedure WriteTree(p:TAsmList);override;
  40. procedure WriteAsmList;override;
  41. destructor destroy; override;
  42. protected
  43. InstrWriter: TLLVMInstrWriter;
  44. end;
  45. {# This is the base class for writing instructions.
  46. The WriteInstruction() method must be overridden
  47. to write a single instruction to the assembler
  48. file.
  49. }
  50. TLLVMInstrWriter = class
  51. constructor create(_owner: TLLVMAssember);
  52. procedure WriteInstruction(hp : tai);
  53. protected
  54. owner: TLLVMAssember;
  55. end;
  56. implementation
  57. uses
  58. SysUtils,
  59. cutils,cfileutl,systems,
  60. fmodule,verbose,
  61. llvmbase,aasmllvm,itllvm,llvmdef,
  62. cgbase,cgutils,cpubase;
  63. const
  64. line_length = 70;
  65. var
  66. symendcount : longint;
  67. type
  68. {$ifdef cpuextended}
  69. t80bitarray = array[0..9] of byte;
  70. {$endif cpuextended}
  71. t64bitarray = array[0..7] of byte;
  72. t32bitarray = array[0..3] of byte;
  73. {****************************************************************************}
  74. { Support routines }
  75. {****************************************************************************}
  76. function single2str(d : single) : string;
  77. var
  78. hs : string;
  79. begin
  80. str(d,hs);
  81. { replace space with + }
  82. if hs[1]=' ' then
  83. hs[1]:='+';
  84. single2str:=hs
  85. end;
  86. function double2str(d : double) : string;
  87. var
  88. hs : string;
  89. begin
  90. str(d,hs);
  91. { replace space with + }
  92. if hs[1]=' ' then
  93. hs[1]:='+';
  94. double2str:=hs
  95. end;
  96. function extended2str(e : extended) : string;
  97. var
  98. hs : string;
  99. begin
  100. str(e,hs);
  101. { replace space with + }
  102. if hs[1]=' ' then
  103. hs[1]:='+';
  104. extended2str:=hs
  105. end;
  106. {****************************************************************************}
  107. { LLVM Instruction writer }
  108. {****************************************************************************}
  109. function getregisterstring(reg: tregister): ansistring;
  110. begin
  111. if getregtype(reg)=R_TEMPREGISTER then
  112. result:='%tmp.'
  113. else
  114. result:='%reg.'+tostr(byte(getregtype(reg)))+'_';
  115. result:=result+tostr(getsupreg(reg));
  116. end;
  117. function getreferencealignstring(var ref: treference) : ansistring;
  118. begin
  119. result:=', align '+tostr(ref.alignment);
  120. end;
  121. function getreferencestring(var ref : treference; withalign: boolean) : ansistring;
  122. begin
  123. result:='';
  124. if assigned(ref.relsymbol) or
  125. (assigned(ref.symbol) =
  126. (ref.base<>NR_NO)) or
  127. (ref.index<>NR_NO) or
  128. (ref.offset<>0) then
  129. begin
  130. result:=' **(error ref: ';
  131. if assigned(ref.symbol) then
  132. result:=result+'sym='+ref.symbol.name+', ';
  133. if assigned(ref.relsymbol) then
  134. result:=result+'sym='+ref.relsymbol.name+', ';
  135. if ref.base=NR_NO then
  136. result:=result+'base=NR_NO, ';
  137. if ref.index<>NR_NO then
  138. result:=result+'index<>NR_NO, ';
  139. if ref.offset<>0 then
  140. result:=result+'offset='+tostr(ref.offset);
  141. result:=result+')**'
  142. // internalerror(2013060225);
  143. end;
  144. if ref.base<>NR_NO then
  145. result:=result+getregisterstring(ref.base)
  146. else
  147. result:=result+ref.symbol.name;
  148. if withalign then
  149. result:=result+getreferencealignstring(ref);
  150. end;
  151. function getparas(const o: toper): ansistring;
  152. var
  153. i: longint;
  154. para: pllvmcallpara;
  155. begin
  156. result:='(';
  157. for i:=0 to o.paras.count-1 do
  158. begin
  159. if i<>0 then
  160. result:=result+', ';
  161. para:=pllvmcallpara(o.paras[i]);
  162. result:=result+llvmencodetype(para^.def);
  163. case para^.loc of
  164. LOC_REGISTER,
  165. LOC_FPUREGISTER,
  166. LOC_MMREGISTER:
  167. result:=result+' '+getregisterstring(para^.reg);
  168. else
  169. internalerror(2014010801);
  170. end;
  171. end;
  172. result:=result+')';
  173. end;
  174. function getopstr(const o:toper; refwithalign: boolean) : ansistring;
  175. var
  176. hs : ansistring;
  177. doubleval: record
  178. case byte of
  179. 1: (d: double);
  180. 2: (i: int64);
  181. end;
  182. {$ifdef cpuextended}
  183. extendedval: record
  184. case byte of
  185. 1: (e: extended);
  186. 2: (r: record
  187. {$ifdef FPC_LITTLE_ENDIAN}
  188. l: int64;
  189. h: word;
  190. {$else FPC_LITTLE_ENDIAN}
  191. h: int64;
  192. l: word;
  193. {$endif FPC_LITTLE_ENDIAN}
  194. end;
  195. );
  196. end;
  197. {$endif cpuextended}
  198. begin
  199. case o.typ of
  200. top_reg:
  201. getopstr:=getregisterstring(o.reg);
  202. top_const:
  203. getopstr:=tostr(longint(o.val));
  204. top_ref:
  205. if o.ref^.refaddr=addr_full then
  206. begin
  207. getopstr:='';
  208. if o.ref^.symbol.typ=AT_LABEL then
  209. getopstr:='label %';
  210. hs:=o.ref^.symbol.name;
  211. if o.ref^.offset<>0 then
  212. internalerror(2013060223);
  213. getopstr:=getopstr+hs;
  214. end
  215. else
  216. getopstr:=getreferencestring(o.ref^,refwithalign);
  217. top_def:
  218. begin
  219. getopstr:=llvmencodetype(o.def);
  220. end;
  221. top_cond:
  222. begin
  223. getopstr:=llvm_cond2str[o.cond];
  224. end;
  225. top_fpcond:
  226. begin
  227. getopstr:=llvm_fpcond2str[o.fpcond];
  228. end;
  229. top_double:
  230. begin
  231. { "When using the hexadecimal form, constants of types half,
  232. float, and double are represented using the 16-digit form shown
  233. above (which matches the IEEE754 representation for double)"
  234. And always in big endian form (sign bit leftmost)
  235. }
  236. doubleval.d:=o.dval;
  237. result:='0x'+hexstr(doubleval.i,16);
  238. end;
  239. top_para:
  240. begin
  241. result:=getparas(o);
  242. end;
  243. {$ifdef cpuextended}
  244. top_extended80:
  245. begin
  246. { hex format is always big endian in llvm }
  247. extendedval.e:=o.eval;
  248. result:='0xK'+hexstr(extendedval.r.h,sizeof(extendedval.r.h)*2)+hexstr(extendedval.r.l,sizeof(extendedval.r.l)*2);
  249. end;
  250. {$endif cpuextended}
  251. else
  252. internalerror(2013060227);
  253. end;
  254. end;
  255. procedure TLlvmInstrWriter.WriteInstruction(hp: tai);
  256. var
  257. op: tllvmop;
  258. s: string;
  259. i, opstart: byte;
  260. sep: string[3];
  261. done: boolean;
  262. begin
  263. op:=taillvm(hp).llvmopcode;
  264. s:=#9;
  265. sep:=' ';
  266. done:=false;
  267. opstart:=0;
  268. case op of
  269. la_ret, la_br, la_switch, la_indirectbr,
  270. la_invoke, la_resume,
  271. la_unreachable,
  272. la_store,
  273. la_fence,
  274. la_cmpxchg,
  275. la_atomicrmw:
  276. begin
  277. { instructions that never have a result }
  278. end;
  279. la_call:
  280. begin
  281. if taillvm(hp).oper[0]^.reg<>NR_NO then
  282. s:=s+getregisterstring(taillvm(hp).oper[0]^.reg)+' = ';
  283. sep:=' ';
  284. opstart:=1;
  285. end;
  286. la_alloca:
  287. begin
  288. s:=s+getreferencestring(taillvm(hp).oper[0]^.ref^,false)+' = ';
  289. sep:=' ';
  290. opstart:=1;
  291. end;
  292. la_trunc, la_zext, la_sext, la_fptrunc, la_fpext,
  293. la_fptoui, la_fptosi, la_uitofp, la_sitofp,
  294. la_ptrtoint, la_inttoptr,
  295. la_bitcast:
  296. begin
  297. s:=s+getopstr(taillvm(hp).oper[0]^,false)+' = '+
  298. llvm_op2str[op]+' '+
  299. getopstr(taillvm(hp).oper[1]^,false)+' '+
  300. getopstr(taillvm(hp).oper[2]^,false)+' to '+
  301. getopstr(taillvm(hp).oper[3]^,false);
  302. done:=true;
  303. end
  304. else
  305. begin
  306. s:=s+getopstr(taillvm(hp).oper[0]^,true)+' = ';
  307. sep:=' ';
  308. opstart:=1
  309. end;
  310. end;
  311. { process operands }
  312. if not done then
  313. begin
  314. s:=s+llvm_op2str[op];
  315. if taillvm(hp).ops<>0 then
  316. begin
  317. for i:=opstart to taillvm(hp).ops-1 do
  318. begin
  319. s:=s+sep+getopstr(taillvm(hp).oper[i]^,op in [la_load,la_store]);
  320. if (taillvm(hp).oper[i]^.typ in [top_def,top_cond,top_fpcond]) or
  321. (op=la_call) then
  322. sep :=' '
  323. else
  324. sep:=', ';
  325. end;
  326. end;
  327. end;
  328. if op=la_alloca then
  329. begin
  330. s:=s+getreferencealignstring(taillvm(hp).oper[0]^.ref^)
  331. end;
  332. owner.AsmWriteLn(s);
  333. end;
  334. {****************************************************************************}
  335. { LLVM Assembler writer }
  336. {****************************************************************************}
  337. destructor TLLVMAssember.Destroy;
  338. begin
  339. InstrWriter.free;
  340. inherited destroy;
  341. end;
  342. function TLLVMAssember.MakeCmdLine: TCmdStr;
  343. var
  344. optstr: TCmdStr;
  345. begin
  346. result := inherited MakeCmdLine;
  347. { standard optimization flags for llc -- todo: this needs to be split
  348. into a call to opt and one to llc }
  349. if cs_opt_level3 in current_settings.optimizerswitches then
  350. optstr:='-O3'
  351. else if cs_opt_level2 in current_settings.optimizerswitches then
  352. optstr:='-O2'
  353. else if cs_opt_level1 in current_settings.optimizerswitches then
  354. optstr:='-O1'
  355. else
  356. optstr:='-O0';
  357. { stack frame elimination }
  358. if not(cs_opt_stackframe in current_settings.optimizerswitches) then
  359. optstr:=optstr+' -disable-fp-elim';
  360. { fast math }
  361. if cs_opt_fastmath in current_settings.optimizerswitches then
  362. optstr:=optstr+' -enable-unsafe-fp-math -enable-fp-mad -fp-contract=fast';
  363. { smart linking }
  364. if cs_create_smart in current_settings.moduleswitches then
  365. optstr:=optstr+' -fdata-sections -fcode-sections';
  366. { pic }
  367. if cs_create_pic in current_settings.moduleswitches then
  368. optstr:=optstr+' -relocation-model=pic'
  369. else if not(target_info.system in systems_darwin) then
  370. optstr:=optstr+' -relocation-model=static'
  371. else
  372. optstr:=optstr+' -relocation-model=dynamic-no-pic';
  373. { our stack alignment is non-standard on some targets. The following
  374. parameter is however ignored on some targets by llvm, so it may not
  375. be enough }
  376. optstr:=optstr+' -stack-alignment='+tostr(target_info.stackalign*8);
  377. { force object output instead of textual assembler code }
  378. optstr:=optstr+' -filetype=obj';
  379. replace(result,'$OPT',optstr);
  380. end;
  381. procedure TLLVMAssember.WriteTree(p:TAsmList);
  382. var
  383. ch : char;
  384. lasthp,
  385. hp : tai;
  386. constdef : taiconst_type;
  387. s,t : string;
  388. i,pos,l : longint;
  389. InlineLevel : cardinal;
  390. last_align : longint;
  391. co : comp;
  392. sin : single;
  393. d : double;
  394. {$ifdef cpuextended}
  395. e : extended;
  396. {$endif cpuextended}
  397. do_line : boolean;
  398. sepChar : char;
  399. replaceforbidden: boolean;
  400. begin
  401. if not assigned(p) then
  402. exit;
  403. replaceforbidden:=target_asm.dollarsign<>'$';
  404. last_align := 2;
  405. InlineLevel:=0;
  406. { lineinfo is only needed for al_procedures (PFV) }
  407. do_line:=(cs_asm_source in current_settings.globalswitches) or
  408. ((cs_lineinfo in current_settings.moduleswitches)
  409. and (p=current_asmdata.asmlists[al_procedures]));
  410. lasthp:=nil;
  411. hp:=tai(p.first);
  412. while assigned(hp) do
  413. begin
  414. prefetch(pointer(hp.next)^);
  415. if not(hp.typ in SkipLineInfo) then
  416. begin
  417. current_filepos:=tailineinfo(hp).fileinfo;
  418. { no line info for inlined code }
  419. if do_line and (inlinelevel=0) then
  420. WriteSourceLine(hp as tailineinfo);
  421. end;
  422. case hp.typ of
  423. ait_comment :
  424. Begin
  425. AsmWrite(target_asm.comment);
  426. AsmWritePChar(tai_comment(hp).str);
  427. AsmLn;
  428. End;
  429. ait_regalloc :
  430. begin
  431. if (cs_asm_regalloc in current_settings.globalswitches) then
  432. begin
  433. AsmWrite(#9+target_asm.comment+'Register ');
  434. repeat
  435. AsmWrite(std_regname(Tai_regalloc(hp).reg));
  436. if (hp.next=nil) or
  437. (tai(hp.next).typ<>ait_regalloc) or
  438. (tai_regalloc(hp.next).ratype<>tai_regalloc(hp).ratype) then
  439. break;
  440. hp:=tai(hp.next);
  441. AsmWrite(',');
  442. until false;
  443. AsmWrite(' ');
  444. AsmWriteLn(regallocstr[tai_regalloc(hp).ratype]);
  445. end;
  446. end;
  447. ait_tempalloc :
  448. begin
  449. if (cs_asm_tempalloc in current_settings.globalswitches) then
  450. WriteTempalloc(tai_tempalloc(hp));
  451. end;
  452. ait_align :
  453. begin
  454. { has to be specified as part of the symbol declaration }
  455. AsmWriteln('; error: explicit aligns are forbidden');
  456. // internalerror(2013010714);
  457. end;
  458. ait_section :
  459. begin
  460. AsmWrite(target_asm.comment);
  461. AsmWriteln('section');
  462. end;
  463. ait_datablock :
  464. begin
  465. AsmWrite(target_asm.comment);
  466. AsmWriteln('datablock');
  467. end;
  468. ait_const:
  469. begin
  470. AsmWrite(target_asm.comment);
  471. AsmWriteln('const');
  472. end;
  473. { the "and defined(FPC_HAS_TYPE_EXTENDED)" isn't optimal but currently the only solution
  474. it prevents proper cross compilation to i386 though
  475. }
  476. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  477. ait_real_80bit :
  478. begin
  479. // if do_line then
  480. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  481. end;
  482. {$endif cpuextended}
  483. ait_real_32bit,
  484. ait_real_64bit:
  485. begin
  486. if hp.typ=ait_real_32bit then
  487. begin
  488. // if do_line then
  489. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  490. // d:=tai_real_32bit(hp).value
  491. end
  492. else
  493. begin
  494. // if do_line then
  495. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  496. // d:=tai_real_64bit(hp).value;
  497. end;
  498. end;
  499. ait_comp_64bit :
  500. begin
  501. // if do_line then
  502. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  503. end;
  504. ait_string :
  505. begin
  506. AsmWrite(target_asm.comment);
  507. AsmWriteln('string');
  508. end;
  509. ait_label :
  510. begin
  511. if (tai_label(hp).labsym.is_used) then
  512. begin
  513. if (tai_label(hp).labsym.bind=AB_PRIVATE_EXTERN) then
  514. begin
  515. { should be emitted as part of the variable/function def }
  516. internalerror(2013010703);
  517. end;
  518. if tai_label(hp).labsym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN] then
  519. begin
  520. { should be emitted as part of the variable/function def }
  521. internalerror(2013010704);
  522. end;
  523. if replaceforbidden then
  524. AsmWrite(ReplaceForbiddenAsmSymbolChars(tai_label(hp).labsym.name))
  525. else
  526. AsmWrite(tai_label(hp).labsym.name);
  527. AsmWriteLn(':');
  528. end;
  529. end;
  530. ait_symbol :
  531. begin
  532. { should be emitted as part of the variable/function def }
  533. asmwrite('; (ait_symbol error, should be part of variable/function def) :');
  534. asmwriteln(tai_symbol(hp).sym.name);
  535. // internalerror(2013010705);
  536. end;
  537. ait_llvmprocdef:
  538. begin
  539. asmwrite('define ');
  540. asmwrite(llvmencodeproctype(taillvmprocdef(hp).procdef,true,true));
  541. asmwriteln(' {');
  542. end;
  543. ait_llvmvarsym:
  544. begin
  545. asmwrite(taillvmvarsym(hp).varsym.mangledname);
  546. if not taillvmvarsym(hp).varsym.globalasmsym then
  547. asmwrite(' = internal global ')
  548. else
  549. asmwrite(' = global ');
  550. asmwrite(llvmencodetype(taillvmvarsym(hp).varsym.vardef));
  551. asmwrite(' zeroinitializer, align ');
  552. asmwriteln(tostr(taillvmvarsym(hp).varsym.vardef.alignment));
  553. end;
  554. ait_llvmalias:
  555. begin
  556. asmwrite(taillvmalias(hp).newsym.name);
  557. asmwrite(' = ');
  558. if taillvmalias(hp).linkage<>lll_default then
  559. begin
  560. str(taillvmalias(hp).linkage,s);
  561. asmwrite(copy(s,length('lll_'),255));
  562. asmwrite(' ');
  563. end;
  564. if taillvmalias(hp).vis<>llv_default then
  565. begin
  566. str(taillvmalias(hp).vis,s);
  567. asmwrite(copy(s,length('llv_'),255));
  568. asmwrite(' ');
  569. end;
  570. asmwrite(llvmencodetype(taillvmalias(hp).def));
  571. asmwrite(' ');
  572. asmwriteln(taillvmalias(hp).oldsym.name);
  573. end;
  574. {$ifdef arm}
  575. ait_thumb_func:
  576. begin
  577. { should be emitted as part of the function def }
  578. internalerror(2013010706);
  579. end;
  580. ait_thumb_set:
  581. begin
  582. { should be emitted as part of the symbol def }
  583. internalerror(2013010707);
  584. end;
  585. {$endif arm}
  586. ait_set:
  587. begin
  588. { should be emitted as part of the symbol def }
  589. internalerror(2013010708);
  590. end;
  591. ait_weak:
  592. begin
  593. { should be emitted as part of the symbol def }
  594. internalerror(2013010709);
  595. end;
  596. ait_symbol_end :
  597. begin
  598. if tai_symbol_end(hp).sym.typ=AT_FUNCTION then
  599. asmwriteln('}')
  600. else
  601. asmwriteln('; ait_symbol_end error, should not be generated');
  602. // internalerror(2013010711);
  603. end;
  604. ait_instruction :
  605. begin
  606. WriteInstruction(hp);
  607. end;
  608. ait_llvmins:
  609. begin
  610. WriteLlvmInstruction(hp);
  611. end;
  612. ait_stab :
  613. begin
  614. internalerror(2013010712);
  615. end;
  616. ait_force_line,
  617. ait_function_name :
  618. ;
  619. ait_cutobject :
  620. begin
  621. end;
  622. ait_marker :
  623. if tai_marker(hp).kind=mark_NoLineInfoStart then
  624. inc(InlineLevel)
  625. else if tai_marker(hp).kind=mark_NoLineInfoEnd then
  626. dec(InlineLevel);
  627. ait_directive :
  628. begin
  629. WriteDirectiveName(tai_directive(hp).directive);
  630. if tai_directive(hp).name <>'' then
  631. AsmWrite(tai_directive(hp).name);
  632. AsmLn;
  633. end;
  634. ait_seh_directive :
  635. begin
  636. internalerror(2013010713);
  637. end;
  638. ait_varloc:
  639. begin
  640. if tai_varloc(hp).newlocationhi<>NR_NO then
  641. AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  642. std_regname(tai_varloc(hp).newlocationhi)+':'+std_regname(tai_varloc(hp).newlocation)))
  643. else
  644. AsmWrite(strpnew('Var '+tai_varloc(hp).varsym.realname+' located in register '+
  645. std_regname(tai_varloc(hp).newlocation)));
  646. AsmLn;
  647. end;
  648. else
  649. internalerror(2006012201);
  650. end;
  651. lasthp:=hp;
  652. hp:=tai(hp.next);
  653. end;
  654. end;
  655. procedure TLLVMAssember.WriteExtraHeader;
  656. begin
  657. AsmWrite('target datalayout = "');
  658. AsmWrite(target_info.llvmdatalayout);
  659. AsmWriteln('"');
  660. AsmWrite('target triple = "');
  661. AsmWrite(llvm_target_name);
  662. AsmWriteln('"');
  663. end;
  664. procedure TLLVMAssember.WriteExtraFooter;
  665. begin
  666. end;
  667. procedure TLLVMAssember.WriteInstruction(hp: tai);
  668. begin
  669. end;
  670. procedure TLLVMAssember.WriteLlvmInstruction(hp: tai);
  671. begin
  672. InstrWriter.WriteInstruction(hp);
  673. end;
  674. procedure TLLVMAssember.WriteWeakSymbolDef(s: tasmsymbol);
  675. begin
  676. AsmWriteLn(#9'.weak '+s.name);
  677. end;
  678. constructor TLLVMAssember.create(smart: boolean);
  679. begin
  680. inherited create(smart);
  681. InstrWriter:=TLLVMInstrWriter.create(self);
  682. end;
  683. procedure TLLVMAssember.WriteDirectiveName(dir: TAsmDirective);
  684. begin
  685. AsmWrite('.'+directivestr[dir]+' ');
  686. end;
  687. procedure TLLVMAssember.WriteAsmList;
  688. var
  689. n : string;
  690. hal : tasmlisttype;
  691. i: longint;
  692. begin
  693. if current_module.mainsource<>'' then
  694. n:=ExtractFileName(current_module.mainsource)
  695. else
  696. n:=InputFileName;
  697. { gcc does not add it either for Darwin. Grep for
  698. TARGET_ASM_FILE_START_FILE_DIRECTIVE in gcc/config/*.h
  699. }
  700. if not(target_info.system in systems_darwin) then
  701. AsmWriteLn(#9'.file "'+FixFileName(n)+'"');
  702. WriteExtraHeader;
  703. AsmStartSize:=AsmSize;
  704. symendcount:=0;
  705. for hal:=low(TasmlistType) to high(TasmlistType) do
  706. begin
  707. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmlistTypeStr[hal]);
  708. writetree(current_asmdata.asmlists[hal]);
  709. AsmWriteLn(target_asm.comment+'End asmlist '+AsmlistTypeStr[hal]);
  710. end;
  711. { add weak symbol markers }
  712. for i:=0 to current_asmdata.asmsymboldict.count-1 do
  713. if (tasmsymbol(current_asmdata.asmsymboldict[i]).bind=AB_WEAK_EXTERNAL) then
  714. writeweaksymboldef(tasmsymbol(current_asmdata.asmsymboldict[i]));
  715. AsmLn;
  716. end;
  717. {****************************************************************************}
  718. { Abstract Instruction Writer }
  719. {****************************************************************************}
  720. constructor TLLVMInstrWriter.create(_owner: TLLVMAssember);
  721. begin
  722. inherited create;
  723. owner := _owner;
  724. end;
  725. const
  726. as_llvm_info : tasminfo =
  727. (
  728. id : as_llvm;
  729. idtxt : 'LLVM-AS';
  730. asmbin : 'llc';
  731. asmcmd: '$OPT -o $OBJ $ASM';
  732. supported_targets : [system_x86_64_linux,system_x86_64_darwin,system_powerpc64_darwin];
  733. flags : [af_smartlink_sections];
  734. labelprefix : 'L';
  735. comment : '; ';
  736. dollarsign: '$';
  737. );
  738. begin
  739. RegisterAssembler(as_llvm_info,TLLVMAssember);
  740. end.