nllvmtcon.pas 16 KB

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