agllvm.pas 34 KB

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