nllvmtcon.pas 12 KB

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