nllvmtcon.pas 17 KB

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