nllvmtcon.pas 12 KB

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