nllvmtcon.pas 11 KB

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