nllvmtcon.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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; procvar2procdef: boolean);
  44. function wrap_with_type(p: tai; def: tdef): tai;
  45. public
  46. constructor create; override;
  47. destructor destroy; override;
  48. procedure emit_tai(p: tai; def: tdef); override;
  49. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); override;
  50. procedure maybe_begin_aggregate(def: tdef); override;
  51. procedure maybe_end_aggregate(def: tdef); override;
  52. procedure queue_init(todef: tdef); override;
  53. procedure queue_vecn(def: tdef; const index: tconstexprint); override;
  54. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); override;
  55. procedure queue_typeconvn(fromdef, todef: tdef); override;
  56. procedure queue_emit_staticvar(vs: tstaticvarsym); override;
  57. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); override;
  58. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; override;
  59. end;
  60. implementation
  61. uses
  62. verbose,
  63. aasmdata,
  64. cpubase,llvmbase,
  65. symtable,llvmdef,defutil;
  66. procedure tllvmtai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; lab: boolean);
  67. var
  68. newasmlist: tasmlist;
  69. begin
  70. { todo }
  71. if section = sec_user then
  72. internalerror(2014052904);
  73. newasmlist:=tasmlist.create_without_marker;
  74. { llvm declaration with as initialisation data all the elements from the
  75. original asmlist }
  76. { TODO: propagate data/rodata different ("constant") }
  77. newasmlist.concat(taillvmdecl.create(sym,def,fasmlist,section));
  78. fasmlist:=newasmlist;
  79. end;
  80. procedure tllvmtai_typedconstbuilder.update_queued_tai(resdef: tdef; outerai, innerai: tai; newindex: longint);
  81. begin
  82. { the outer tai must always be a typed constant (possibly a wrapper
  83. around a taillvm or so), in order for result type information to be
  84. available }
  85. if outerai.typ<>ait_typedconst then
  86. internalerror(2014060401);
  87. { is the result of the outermost expression different from the type of
  88. this typed const? -> insert type conversion }
  89. if not assigned(fqueued_tai) and
  90. (resdef<>fqueued_def) and
  91. (llvmencodetype(resdef)<>llvmencodetype(fqueued_def)) then
  92. queue_typeconvn(resdef,fqueued_def);
  93. if assigned(fqueued_tai) then
  94. begin
  95. taillvm(flast_added_tai).loadtai(fqueued_tai_opidx,outerai);
  96. { already flushed? }
  97. if fqueued_tai_opidx=-1 then
  98. internalerror(2014062201);
  99. end
  100. else
  101. begin
  102. fqueued_tai:=outerai;
  103. fqueued_def:=resdef;
  104. end;
  105. fqueued_tai_opidx:=newindex;
  106. flast_added_tai:=innerai;
  107. end;
  108. procedure tllvmtai_typedconstbuilder.emit_tai_intern(p: tai; def: tdef; procvar2procdef: boolean);
  109. var
  110. ai: tai;
  111. stc: tai_abstracttypedconst;
  112. kind: ttypedconstkind;
  113. begin
  114. if assigned(fqueued_tai) then
  115. begin
  116. if not procvar2procdef then
  117. kind:=tck_simple
  118. else
  119. kind:=tck_simple_procvar2proc;
  120. { finalise the queued expression }
  121. ai:=tai_simpletypedconst.create(kind,def,p);
  122. { set the new index to -1, so we internalerror should we try to
  123. add anything further }
  124. update_queued_tai(def,ai,ai,-1);
  125. { and emit it }
  126. stc:=tai_abstracttypedconst(fqueued_tai);
  127. def:=fqueued_def;
  128. { ensure we don't try to emit this one again }
  129. fqueued_tai:=nil;
  130. end
  131. else
  132. stc:=tai_simpletypedconst.create(tck_simple,def,p);
  133. { these elements can be aggregates themselves, e.g. a shortstring can
  134. be emitted as a series of bytes and string data arrays }
  135. if not procvar2procdef then
  136. kind:=aggregate_kind(def)
  137. else
  138. kind:=tck_simple_procvar2proc;
  139. if not(kind in [tck_simple,tck_simple_procvar2proc]) 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. constructor tllvmtai_typedconstbuilder.create;
  155. begin
  156. inherited create;
  157. { constructed as needed }
  158. faggregates:=nil;
  159. end;
  160. destructor tllvmtai_typedconstbuilder.destroy;
  161. begin
  162. faggregates.free;
  163. inherited destroy;
  164. end;
  165. procedure tllvmtai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  166. begin
  167. emit_tai_intern(p,def,false);
  168. end;
  169. procedure tllvmtai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  170. begin
  171. emit_tai_intern(p,pvdef,true);
  172. end;
  173. procedure tllvmtai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  174. var
  175. agg: tai_aggregatetypedconst;
  176. tck: ttypedconstkind;
  177. begin
  178. tck:=aggregate_kind(def);
  179. if tck<>tck_simple then
  180. begin
  181. if not assigned(faggregates) then
  182. faggregates:=tfplist.create;
  183. agg:=tai_aggregatetypedconst.create(tck,def);
  184. { nested aggregate -> add to parent }
  185. if faggregates.count>0 then
  186. tai_aggregatetypedconst(faggregates[faggregates.count-1]).addvalue(agg)
  187. { otherwise add to asmlist }
  188. else
  189. fasmlist.concat(agg);
  190. { new top level aggregate, future data will be added to it }
  191. faggregates.add(agg);
  192. end;
  193. inherited;
  194. end;
  195. procedure tllvmtai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  196. begin
  197. if aggregate_kind(def)<>tck_simple then
  198. begin
  199. if not assigned(faggregates) or
  200. (faggregates.count=0) then
  201. internalerror(2014060101);
  202. tai_aggregatetypedconst(faggregates[faggregates.count-1]).finish;
  203. { already added to the asmlist if necessary }
  204. faggregates.count:=faggregates.count-1;
  205. end;
  206. inherited;
  207. end;
  208. procedure tllvmtai_typedconstbuilder.queue_init(todef: tdef);
  209. begin
  210. inherited;
  211. fqueued_tai:=nil;
  212. flast_added_tai:=nil;
  213. fqueued_tai_opidx:=-1;
  214. fqueued_def:=todef;
  215. end;
  216. procedure tllvmtai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  217. var
  218. ai: taillvm;
  219. aityped: tai;
  220. eledef: tdef;
  221. begin
  222. { update range checking info }
  223. inherited;
  224. ai:=taillvm.getelementptr_reg_tai_size_const(NR_NO,nil,ptrsinttype,index.svalue,true);
  225. case def.typ of
  226. arraydef:
  227. eledef:=tarraydef(def).elementdef;
  228. stringdef:
  229. case tstringdef(def).stringtype of
  230. st_shortstring,
  231. st_longstring,
  232. st_ansistring:
  233. eledef:=cansichartype;
  234. st_widestring,
  235. st_unicodestring:
  236. eledef:=cwidechartype;
  237. else
  238. internalerror(2014062202);
  239. end;
  240. else
  241. internalerror(2014062203);
  242. end;
  243. aityped:=wrap_with_type(ai,getpointerdef(eledef));
  244. update_queued_tai(getpointerdef(eledef),aityped,ai,1);
  245. end;
  246. procedure tllvmtai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  247. var
  248. getllvmfieldaddr,
  249. getpascalfieldaddr,
  250. getllvmfieldaddrtyped: tai;
  251. llvmfielddef: tdef;
  252. begin
  253. { update range checking info }
  254. inherited;
  255. llvmfielddef:=tabstractrecordsymtable(def.symtable).llvmst[vs.llvmfieldnr].def;
  256. { get the address of the llvm-struct field that corresponds to this
  257. Pascal field }
  258. getllvmfieldaddr:=taillvm.getelementptr_reg_tai_size_const(NR_NO,nil,s32inttype,vs.llvmfieldnr,true);
  259. { getelementptr doesn't contain its own resultdef, so encode it via a
  260. tai_simpletypedconst tai }
  261. getllvmfieldaddrtyped:=wrap_with_type(getllvmfieldaddr,getpointerdef(llvmfielddef));
  262. { if it doesn't match the requested field exactly (variant record),
  263. fixup the result }
  264. getpascalfieldaddr:=getllvmfieldaddrtyped;
  265. if (vs.offsetfromllvmfield<>0) or
  266. (llvmfielddef<>vs.vardef) then
  267. begin
  268. { offset of real field relative to llvm-struct field <> 0? }
  269. if vs.offsetfromllvmfield<>0 then
  270. begin
  271. { convert to a pointer to a 1-sized element }
  272. if llvmfielddef.size<>1 then
  273. begin
  274. getpascalfieldaddr:=taillvm.op_reg_tai_size(la_bitcast,NR_NO,getpascalfieldaddr,u8inttype);
  275. { update the current fielddef of the expression }
  276. llvmfielddef:=u8inttype;
  277. end;
  278. { add the offset }
  279. getpascalfieldaddr:=taillvm.getelementptr_reg_tai_size_const(NR_NO,getpascalfieldaddr,ptrsinttype,vs.offsetfromllvmfield,true);
  280. { ... and set the result type of the getelementptr }
  281. getpascalfieldaddr:=wrap_with_type(getpascalfieldaddr,getpointerdef(u8inttype));
  282. llvmfielddef:=u8inttype;
  283. end;
  284. { bitcast the data at the final offset to the right type }
  285. if llvmfielddef<>vs.vardef then
  286. getpascalfieldaddr:=wrap_with_type(taillvm.op_reg_tai_size(la_bitcast,NR_NO,getpascalfieldaddr,getpointerdef(vs.vardef)),getpointerdef(vs.vardef));
  287. end;
  288. update_queued_tai(getpointerdef(vs.vardef),getpascalfieldaddr,getllvmfieldaddr,1);
  289. end;
  290. procedure tllvmtai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  291. var
  292. ai: taillvm;
  293. typedai: tai;
  294. tmpintdef: tdef;
  295. op,
  296. firstop,
  297. secondop: tllvmop;
  298. begin
  299. inherited;
  300. op:=llvmconvop(fromdef,todef);
  301. case op of
  302. la_ptrtoint_to_x,
  303. la_x_to_inttoptr:
  304. begin
  305. { convert via an integer with the same size as "x" }
  306. if op=la_ptrtoint_to_x then
  307. begin
  308. tmpintdef:=cgsize_orddef(def_cgsize(todef));
  309. firstop:=la_ptrtoint;
  310. secondop:=la_bitcast
  311. end
  312. else
  313. begin
  314. tmpintdef:=cgsize_orddef(def_cgsize(fromdef));
  315. firstop:=la_bitcast;
  316. secondop:=la_inttoptr;
  317. end;
  318. { since we have to queue operations from outer to inner, first queue
  319. the conversion from the tempintdef to the todef }
  320. ai:=taillvm.op_reg_tai_size(secondop,NR_NO,nil,todef);
  321. typedai:=wrap_with_type(ai,todef);
  322. update_queued_tai(todef,typedai,ai,1);
  323. todef:=tmpintdef;
  324. op:=firstop
  325. end;
  326. end;
  327. ai:=taillvm.op_reg_tai_size(op,NR_NO,nil,todef);
  328. typedai:=wrap_with_type(ai,todef);
  329. update_queued_tai(todef,typedai,ai,1);
  330. end;
  331. procedure tllvmtai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  332. begin
  333. { we've already incorporated the offset via the inserted operations above,
  334. make sure it doesn't get emitted again as part of the tai_const for
  335. the tasmsymbol }
  336. fqueue_offset:=0;
  337. inherited;
  338. end;
  339. procedure tllvmtai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  340. begin
  341. { we've already incorporated the offset via the inserted operations above,
  342. make sure it doesn't get emitted again as part of the tai_const for
  343. the tasmsymbol }
  344. fqueue_offset:=0;
  345. inherited;
  346. end;
  347. class function tllvmtai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  348. begin
  349. { LLVM does not support labels in the middle of a declaration }
  350. result:=0;
  351. end;
  352. begin
  353. ctai_typedconstbuilder:=tllvmtai_typedconstbuilder;
  354. end.