nllvmtcon.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. Generates code for typed constant declarations for the LLVM target
  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 nllvmtcon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,constexp,globtype,
  22. aasmbase,aasmtai,aasmcnst,aasmllvm,
  23. symconst,symtype,symdef,symsym,
  24. ngtcon;
  25. type
  26. tllvmaggregateinformation = class(taggregateinformation)
  27. private
  28. faggai: tai_aggregatetypedconst;
  29. fanonrecalignpos: longint;
  30. public
  31. constructor create(_def: tdef; _typ: ttypedconstkind); override;
  32. function prepare_next_field(nextfielddef: tdef): asizeint; override;
  33. property aggai: tai_aggregatetypedconst read faggai write faggai;
  34. property anonrecalignpos: longint read fanonrecalignpos write fanonrecalignpos;
  35. end;
  36. tllvmtai_typedconstbuilder = class(ttai_typedconstbuilder)
  37. protected type
  38. public
  39. { set the default value for caggregateinformation (= tllvmaggregateinformation) }
  40. class constructor classcreate;
  41. protected
  42. fqueued_tai,
  43. flast_added_tai: tai;
  44. fqueued_tai_opidx: longint;
  45. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); override;
  46. { outerai: the ai that should become fqueued_tai in case it's still nil,
  47. or that should be filled in the fqueued_tai_opidx of the current
  48. fqueued_tai if it's not nil
  49. innerai: the innermost ai (possibly an operand of outerai) in which
  50. newindex indicates which operand is empty and can be filled with the
  51. next queued tai }
  52. procedure update_queued_tai(resdef: tdef; outerai, innerai: tai; newindex: longint);
  53. function wrap_with_type(p: tai; def: tdef): tai;
  54. procedure do_emit_tai(p: tai; def: tdef); override;
  55. procedure mark_anon_aggregate_alignment; override;
  56. procedure insert_marked_aggregate_alignment(def: tdef); override;
  57. procedure maybe_emit_tail_padding(def: tdef); override;
  58. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); override;
  59. procedure end_aggregate_internal(def: tdef; anonymous: boolean); override;
  60. function get_internal_data_section_start_label: tasmlabel; override;
  61. function get_internal_data_section_internal_label: tasmlabel; override;
  62. procedure do_emit_extended_in_aggregate(p: tai);
  63. public
  64. destructor destroy; override;
  65. procedure emit_tai(p: tai; def: tdef); override;
  66. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); override;
  67. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef); override;
  68. procedure queue_init(todef: tdef); override;
  69. procedure queue_vecn(def: tdef; const index: tconstexprint); override;
  70. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); override;
  71. procedure queue_typeconvn(fromdef, todef: tdef); override;
  72. procedure queue_emit_staticvar(vs: tstaticvarsym); override;
  73. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); override;
  74. procedure queue_emit_ordconst(value: int64; def: tdef); override;
  75. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; override;
  76. end;
  77. implementation
  78. uses
  79. verbose,systems,
  80. aasmdata,
  81. cpubase,cpuinfo,llvmbase,
  82. symbase,symtable,llvmdef,defutil;
  83. { tllvmaggregateinformation }
  84. constructor tllvmaggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  85. begin
  86. inherited;
  87. fanonrecalignpos:=-1;
  88. end;
  89. function tllvmaggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  90. begin
  91. result:=inherited;
  92. { in case of C/ABI alignment, the padding gets added by LLVM }
  93. if tabstractrecordsymtable(tabstractrecorddef(def).symtable).usefieldalignment=C_alignment then
  94. result:=0;
  95. end;
  96. class constructor tllvmtai_typedconstbuilder.classcreate;
  97. begin
  98. caggregateinformation:=tllvmaggregateinformation;
  99. end;
  100. procedure tllvmtai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  101. var
  102. newasmlist: tasmlist;
  103. begin
  104. { todo }
  105. if section = sec_user then
  106. internalerror(2014052904);
  107. newasmlist:=tasmlist.create;
  108. { llvm declaration with as initialisation data all the elements from the
  109. original asmlist }
  110. newasmlist.concat(taillvmdecl.create(sym,def,fasmlist,section,alignment));
  111. fasmlist:=newasmlist;
  112. end;
  113. procedure tllvmtai_typedconstbuilder.update_queued_tai(resdef: tdef; outerai, innerai: tai; newindex: longint);
  114. begin
  115. { the outer tai must always be a typed constant (possibly a wrapper
  116. around a taillvm or so), in order for result type information to be
  117. available }
  118. if outerai.typ<>ait_typedconst then
  119. internalerror(2014060401);
  120. { is the result of the outermost expression different from the type of
  121. this typed const? -> insert type conversion }
  122. if not assigned(fqueued_tai) and
  123. (resdef<>fqueued_def) and
  124. (llvmencodetypename(resdef)<>llvmencodetypename(fqueued_def)) then
  125. queue_typeconvn(resdef,fqueued_def);
  126. if assigned(fqueued_tai) then
  127. begin
  128. taillvm(flast_added_tai).loadtai(fqueued_tai_opidx,outerai);
  129. { already flushed? }
  130. if fqueued_tai_opidx=-1 then
  131. internalerror(2014062201);
  132. end
  133. else
  134. begin
  135. fqueued_tai:=outerai;
  136. fqueued_def:=resdef;
  137. end;
  138. fqueued_tai_opidx:=newindex;
  139. flast_added_tai:=innerai;
  140. end;
  141. function tllvmtai_typedconstbuilder.wrap_with_type(p: tai; def: tdef): tai;
  142. begin
  143. result:=tai_simpletypedconst.create(tck_simple,def,p);
  144. end;
  145. destructor tllvmtai_typedconstbuilder.destroy;
  146. begin
  147. inherited destroy;
  148. end;
  149. procedure tllvmtai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  150. var
  151. arrdef: tdef;
  152. begin
  153. { inside an aggregate, an 80 bit floating point number must be
  154. emitted as an array of 10 bytes to prevent ABI alignment and
  155. padding to 16 bytes }
  156. if (def.typ=floatdef) and
  157. (tfloatdef(def).floattype=s80real) and
  158. assigned(curagginfo) then
  159. do_emit_extended_in_aggregate(p)
  160. else
  161. inherited;
  162. end;
  163. procedure tllvmtai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  164. var
  165. ai: tai;
  166. stc: tai_abstracttypedconst;
  167. kind: ttypedconstkind;
  168. info: tllvmaggregateinformation;
  169. begin
  170. if queue_is_active then
  171. begin
  172. kind:=tck_simple;
  173. { finalise the queued expression }
  174. ai:=tai_simpletypedconst.create(kind,def,p);
  175. { set the new index to -1, so we internalerror should we try to
  176. add anything further }
  177. update_queued_tai(def,ai,ai,-1);
  178. { and emit it }
  179. stc:=tai_abstracttypedconst(fqueued_tai);
  180. def:=fqueued_def;
  181. { ensure we don't try to emit this one again }
  182. fqueued_tai:=nil;
  183. end
  184. else
  185. stc:=tai_simpletypedconst.create(tck_simple,def,p);
  186. info:=tllvmaggregateinformation(curagginfo);
  187. { these elements can be aggregates themselves, e.g. a shortstring can
  188. be emitted as a series of bytes and string data arrays }
  189. kind:=aggregate_kind(def);
  190. if (kind<>tck_simple) then
  191. begin
  192. if not assigned(info) or
  193. (info.aggai.adetyp<>kind) then
  194. internalerror(2014052906);
  195. end;
  196. if assigned(info) then
  197. info.aggai.addvalue(stc)
  198. else
  199. inherited do_emit_tai(stc,def);
  200. end;
  201. procedure tllvmtai_typedconstbuilder.mark_anon_aggregate_alignment;
  202. var
  203. info: tllvmaggregateinformation;
  204. begin
  205. info:=tllvmaggregateinformation(curagginfo);
  206. info.anonrecalignpos:=info.aggai.valuecount;
  207. end;
  208. procedure tllvmtai_typedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  209. var
  210. info: tllvmaggregateinformation;
  211. fillbytes: asizeint;
  212. begin
  213. info:=tllvmaggregateinformation(curagginfo);
  214. if info.anonrecalignpos=-1 then
  215. internalerror(2014091501);
  216. fillbytes:=info.prepare_next_field(def);
  217. while fillbytes>0 do
  218. begin
  219. info.aggai.insertvaluebeforepos(tai_simpletypedconst.create(tck_simple,u8inttype,tai_const.create_8bit(0)),info.anonrecalignpos);
  220. dec(fillbytes);
  221. end;
  222. end;
  223. procedure tllvmtai_typedconstbuilder.maybe_emit_tail_padding(def: tdef);
  224. begin
  225. { in case of C/ABI alignment, the padding gets added by LLVM }
  226. if (is_record(def) or
  227. is_object(def)) and
  228. (tabstractrecordsymtable(tabstractrecorddef(def).symtable).usefieldalignment=C_alignment) then
  229. exit;
  230. inherited;
  231. end;
  232. procedure tllvmtai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  233. begin
  234. if not pvdef.is_addressonly then
  235. pvdef:=cprocvardef.getreusableprocaddr(pvdef);
  236. emit_tai(p,pvdef);
  237. end;
  238. procedure tllvmtai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  239. var
  240. srsym : tsym;
  241. srsymtable: tsymtable;
  242. strrecdef : trecorddef;
  243. offset: pint;
  244. field: tfieldvarsym;
  245. dataptrdef: tdef;
  246. begin
  247. { nil pointer? }
  248. if not assigned(ll.lab) then
  249. begin
  250. if ll.ofs<>0 then
  251. internalerror(2015030701);
  252. inherited;
  253. exit;
  254. end;
  255. { if the returned offset is <> 0, then the string data
  256. starts at that offset -> translate to a field for the
  257. high level code generator }
  258. if ll.ofs<>0 then
  259. begin
  260. { get the recorddef for this string constant }
  261. if not searchsym_type(ctai_typedconstbuilder.get_dynstring_rec_name(st,winlikewidestring,strlength),srsym,srsymtable) then
  262. internalerror(2014080406);
  263. strrecdef:=trecorddef(ttypesym(srsym).typedef);
  264. { offset in the record of the the string data }
  265. offset:=ctai_typedconstbuilder.get_string_symofs(st,winlikewidestring);
  266. { field corresponding to this offset }
  267. field:=trecordsymtable(strrecdef.symtable).findfieldbyoffset(offset);
  268. { pointerdef to the string data array }
  269. dataptrdef:=cpointerdef.getreusable(field.vardef);
  270. queue_init(charptrdef);
  271. queue_addrn(dataptrdef,charptrdef);
  272. queue_subscriptn(strrecdef,field);
  273. queue_emit_asmsym(ll.lab,strrecdef);
  274. end
  275. else
  276. { since llvm doesn't support labels in the middle of structs, this
  277. offset should never be 0 }
  278. internalerror(2014080506);
  279. end;
  280. procedure tllvmtai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  281. var
  282. agg: tai_aggregatetypedconst;
  283. tck: ttypedconstkind;
  284. curagg: tllvmaggregateinformation;
  285. begin
  286. tck:=aggregate_kind(def);
  287. if tck<>tck_simple then
  288. begin
  289. { create new typed const aggregate }
  290. agg:=tai_aggregatetypedconst.create(tck,def);
  291. { either add to the current typed const aggregate (if nested), or
  292. emit to the asmlist (if top level) }
  293. curagg:=tllvmaggregateinformation(curagginfo);
  294. if assigned(curagg) then
  295. curagg.aggai.addvalue(agg)
  296. else
  297. fasmlist.concat(agg);
  298. { create aggregate information for this new aggregate }
  299. inherited;
  300. { set new current typed const aggregate }
  301. tllvmaggregateinformation(curagginfo).aggai:=agg
  302. end
  303. else
  304. inherited;
  305. end;
  306. procedure tllvmtai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  307. var
  308. info: tllvmaggregateinformation;
  309. begin
  310. if aggregate_kind(def)<>tck_simple then
  311. begin
  312. info:=tllvmaggregateinformation(curagginfo);
  313. if not assigned(info) then
  314. internalerror(2014060101);
  315. info.aggai.finish;
  316. end;
  317. inherited;
  318. end;
  319. function tllvmtai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  320. begin
  321. { let llvm take care of everything by creating internal nameless
  322. constants }
  323. current_asmdata.getlocaldatalabel(result);
  324. end;
  325. function tllvmtai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  326. begin
  327. current_asmdata.getlocaldatalabel(result);
  328. end;
  329. procedure tllvmtai_typedconstbuilder.do_emit_extended_in_aggregate(p: tai);
  330. type
  331. p80realval =^t80realval;
  332. t80realval = packed record
  333. case byte of
  334. 0: (v: ts80real);
  335. 1: (a: array[0..9] of byte);
  336. end;
  337. var
  338. arrdef: tdef;
  339. i: longint;
  340. realval: p80realval;
  341. begin
  342. { emit as an array of 10 bytes }
  343. arrdef:=carraydef.getreusable(u8inttype,10);
  344. maybe_begin_aggregate(arrdef);
  345. if (p.typ<>ait_realconst) then
  346. internalerror(2015062401);
  347. realval:=p80realval(@tai_realconst(p).value.s80val);
  348. if target_info.endian=source_info.endian then
  349. for i:=0 to 9 do
  350. emit_tai(tai_const.Create_8bit(realval^.a[i]),u8inttype)
  351. else
  352. for i:=9 downto 0 do
  353. emit_tai(tai_const.Create_8bit(realval^.a[i]),u8inttype);
  354. maybe_end_aggregate(arrdef);
  355. { free the original constant, since we didn't emit it }
  356. p.free;
  357. end;
  358. procedure tllvmtai_typedconstbuilder.queue_init(todef: tdef);
  359. begin
  360. inherited;
  361. fqueued_tai:=nil;
  362. flast_added_tai:=nil;
  363. fqueued_tai_opidx:=-1;
  364. end;
  365. procedure tllvmtai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  366. var
  367. ai: taillvm;
  368. aityped: tai;
  369. eledef: tdef;
  370. begin
  371. { update range checking info }
  372. inherited;
  373. ai:=taillvm.getelementptr_reg_tai_size_const(NR_NO,nil,ptrsinttype,index.svalue,true);
  374. case def.typ of
  375. arraydef:
  376. eledef:=tarraydef(def).elementdef;
  377. stringdef:
  378. case tstringdef(def).stringtype of
  379. st_shortstring,
  380. st_longstring,
  381. st_ansistring:
  382. eledef:=cansichartype;
  383. st_widestring,
  384. st_unicodestring:
  385. eledef:=cwidechartype;
  386. else
  387. internalerror(2014062202);
  388. end;
  389. else
  390. internalerror(2014062203);
  391. end;
  392. aityped:=wrap_with_type(ai,cpointerdef.getreusable(eledef));
  393. update_queued_tai(cpointerdef.getreusable(eledef),aityped,ai,1);
  394. end;
  395. procedure tllvmtai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  396. var
  397. getllvmfieldaddr,
  398. getpascalfieldaddr,
  399. getllvmfieldaddrtyped: tai;
  400. llvmfielddef: tdef;
  401. begin
  402. { update range checking info }
  403. inherited;
  404. llvmfielddef:=tabstractrecordsymtable(def.symtable).llvmst[vs].def;
  405. { get the address of the llvm-struct field that corresponds to this
  406. Pascal field }
  407. getllvmfieldaddr:=taillvm.getelementptr_reg_tai_size_const(NR_NO,nil,s32inttype,vs.llvmfieldnr,true);
  408. { getelementptr doesn't contain its own resultdef, so encode it via a
  409. tai_simpletypedconst tai }
  410. getllvmfieldaddrtyped:=wrap_with_type(getllvmfieldaddr,cpointerdef.getreusable(llvmfielddef));
  411. { if it doesn't match the requested field exactly (variant record),
  412. fixup the result }
  413. getpascalfieldaddr:=getllvmfieldaddrtyped;
  414. if (vs.offsetfromllvmfield<>0) or
  415. (llvmfielddef<>vs.vardef) then
  416. begin
  417. { offset of real field relative to llvm-struct field <> 0? }
  418. if vs.offsetfromllvmfield<>0 then
  419. begin
  420. { convert to a pointer to a 1-sized element }
  421. if llvmfielddef.size<>1 then
  422. begin
  423. getpascalfieldaddr:=taillvm.op_reg_tai_size(la_bitcast,NR_NO,getpascalfieldaddr,u8inttype);
  424. { update the current fielddef of the expression }
  425. llvmfielddef:=u8inttype;
  426. end;
  427. { add the offset }
  428. getpascalfieldaddr:=taillvm.getelementptr_reg_tai_size_const(NR_NO,getpascalfieldaddr,ptrsinttype,vs.offsetfromllvmfield,true);
  429. { ... and set the result type of the getelementptr }
  430. getpascalfieldaddr:=wrap_with_type(getpascalfieldaddr,cpointerdef.getreusable(u8inttype));
  431. llvmfielddef:=u8inttype;
  432. end;
  433. { bitcast the data at the final offset to the right type }
  434. if llvmfielddef<>vs.vardef then
  435. getpascalfieldaddr:=wrap_with_type(taillvm.op_reg_tai_size(la_bitcast,NR_NO,getpascalfieldaddr,cpointerdef.getreusable(vs.vardef)),cpointerdef.getreusable(vs.vardef));
  436. end;
  437. update_queued_tai(cpointerdef.getreusable(vs.vardef),getpascalfieldaddr,getllvmfieldaddr,1);
  438. end;
  439. procedure tllvmtai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  440. var
  441. ai: taillvm;
  442. typedai: tai;
  443. tmpintdef: tdef;
  444. op,
  445. firstop,
  446. secondop: tllvmop;
  447. begin
  448. inherited;
  449. { special case: procdef -> procvardef/pointerdef: must take address of
  450. the procdef }
  451. if (fromdef.typ=procdef) and
  452. (todef.typ<>procdef) then
  453. fromdef:=cprocvardef.getreusableprocaddr(tprocdef(fromdef));
  454. op:=llvmconvop(fromdef,todef);
  455. case op of
  456. la_ptrtoint_to_x,
  457. la_x_to_inttoptr:
  458. begin
  459. { convert via an integer with the same size as "x" }
  460. if op=la_ptrtoint_to_x then
  461. begin
  462. tmpintdef:=cgsize_orddef(def_cgsize(todef));
  463. firstop:=la_ptrtoint;
  464. secondop:=la_bitcast
  465. end
  466. else
  467. begin
  468. tmpintdef:=cgsize_orddef(def_cgsize(fromdef));
  469. firstop:=la_bitcast;
  470. secondop:=la_inttoptr;
  471. end;
  472. { since we have to queue operations from outer to inner, first queue
  473. the conversion from the tempintdef to the todef }
  474. ai:=taillvm.op_reg_tai_size(secondop,NR_NO,nil,todef);
  475. typedai:=wrap_with_type(ai,todef);
  476. update_queued_tai(todef,typedai,ai,1);
  477. todef:=tmpintdef;
  478. op:=firstop
  479. end;
  480. end;
  481. ai:=taillvm.op_reg_tai_size(op,NR_NO,nil,todef);
  482. typedai:=wrap_with_type(ai,todef);
  483. update_queued_tai(todef,typedai,ai,1);
  484. end;
  485. procedure tllvmtai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  486. begin
  487. { we've already incorporated the offset via the inserted operations above,
  488. make sure it doesn't get emitted again as part of the tai_const for
  489. the tasmsymbol }
  490. fqueue_offset:=0;
  491. inherited;
  492. end;
  493. procedure tllvmtai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  494. begin
  495. { we've already incorporated the offset via the inserted operations above,
  496. make sure it doesn't get emitted again as part of the tai_const for
  497. the tasmsymbol }
  498. fqueue_offset:=0;
  499. inherited;
  500. end;
  501. procedure tllvmtai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  502. begin
  503. { no offset into an ordinal constant }
  504. if fqueue_offset<>0 then
  505. internalerror(2015030702);
  506. inherited;
  507. end;
  508. class function tllvmtai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  509. begin
  510. { LLVM does not support labels in the middle of a declaration }
  511. result:=get_string_header_size(typ,winlikewidestring);
  512. end;
  513. begin
  514. ctai_typedconstbuilder:=tllvmtai_typedconstbuilder;
  515. end.