nllvmtcon.pas 18 KB

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