nllvmmem.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {
  2. Copyright (c) 2012 by Jonas Maebe
  3. Generate LLVM byetcode for in memory related nodes
  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 nllvmmem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cgutils,
  23. symtype,
  24. node,ncgnstmm, ncgmem;
  25. type
  26. tllvmloadparentfpnode = class(tcgnestloadparentfpnode)
  27. end;
  28. tllvmsubscriptnode = class(tcgsubscriptnode)
  29. protected
  30. function handle_platform_subscript: boolean; override;
  31. end;
  32. tllvmvecnode= class(tcgvecnode)
  33. private
  34. constarrayoffset: aint;
  35. arraytopointerconverted: boolean;
  36. public
  37. procedure pass_generate_code; override;
  38. procedure update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint); override;
  39. procedure update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l: aint); override;
  40. procedure update_reference_offset(var ref: treference; index, mulsize: aint); override;
  41. end;
  42. implementation
  43. uses
  44. verbose,cutils,
  45. aasmdata,aasmllvm,
  46. symtable,symconst,symdef,defutil,
  47. nmem,
  48. cpubase,llvmbase,hlcgobj,hlcgllvm;
  49. { tllvmsubscriptnode }
  50. function tllvmsubscriptnode.handle_platform_subscript: boolean;
  51. var
  52. newbase: tregister;
  53. fielddef: tdef;
  54. begin
  55. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  56. internalerror(2014011905);
  57. if is_packed_record_or_object(left.resultdef) then
  58. begin
  59. { typecast the result to the expected type, but don't actually index
  60. (that still has to be done by the generic code, so return false) }
  61. newbase:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  62. if is_ordinal(resultdef) then
  63. fielddef:=
  64. cgsize_orddef(
  65. int_cgsize(
  66. packedbitsloadsize(resultdef.packedbitsize)
  67. )
  68. )
  69. else
  70. fielddef:=resultdef;
  71. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,
  72. left.resultdef,
  73. cpointerdef.getreusable(fielddef),
  74. location.reference,newbase);
  75. reference_reset_base(location.reference,newbase,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  76. result:=false;
  77. end
  78. else
  79. begin
  80. hlcg.g_set_addr_nonbitpacked_field_ref(current_asmdata.CurrAsmList,tabstractrecorddef(left.resultdef),vs,location.reference);
  81. location.size:=def_cgsize(resultdef);
  82. result:=true;
  83. end;
  84. end;
  85. { tllvmvecnode }
  86. procedure tllvmvecnode.pass_generate_code;
  87. var
  88. locref: preference;
  89. hreg: tregister;
  90. arrptrelementdef: tdef;
  91. packedloadsize: aint;
  92. indirect: boolean;
  93. procedure getarrelementptrdef;
  94. begin
  95. if assigned(locref) then
  96. exit;
  97. case location.loc of
  98. LOC_SUBSETREF,LOC_CSUBSETREF:
  99. locref:[email protected];
  100. LOC_REFERENCE,LOC_CREFERENCE:
  101. locref:[email protected];
  102. else
  103. internalerror(2013111001);
  104. end;
  105. { special handling for s80real: inside aggregates (such as arrays) it's
  106. declared as an array of 10 bytes in order to force the allocation of
  107. the right size (llvm only supports s80real according to the ABI size/
  108. alignment) -> convert the pointer to this array into a pointer to the
  109. s80real type (loads from and stores to this type will always only store
  110. 10 bytes) }
  111. if (resultdef.typ=floatdef) and
  112. (tfloatdef(resultdef).floattype=s80real) then
  113. arrptrelementdef:=cpointerdef.getreusable(carraydef.getreusable(u8inttype,10))
  114. else
  115. arrptrelementdef:=cpointerdef.getreusable(resultdef);
  116. end;
  117. begin
  118. inherited;
  119. locref:=nil;
  120. { avoid uninitialised warning }
  121. arrptrelementdef:=nil;
  122. indirect:=
  123. not is_dynamicstring(left.resultdef) and
  124. not is_dynamic_array(left.resultdef);
  125. if (not arraytopointerconverted and
  126. indirect) or
  127. (constarrayoffset<>0) then
  128. begin
  129. { the result is currently a pointer to left.resultdef (the array type)
  130. -> convert it into a pointer to an element inside this array }
  131. getarrelementptrdef;
  132. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,arrptrelementdef);
  133. locref^:=thlcgllvm(hlcg).make_simple_ref(current_asmdata.CurrAsmList,location.reference,left.resultdef);
  134. if indirect then
  135. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_const(hreg,cpointerdef.getreusable(left.resultdef),
  136. locref^,ptruinttype,constarrayoffset,true))
  137. else
  138. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_const(hreg,left.resultdef,
  139. locref^,ptruinttype,constarrayoffset,false));
  140. reference_reset_base(locref^,hreg,0,locref^.temppos,locref^.alignment,locref^.volatility);
  141. end;
  142. { see comment in getarrelementptrdef }
  143. if (resultdef.typ=floatdef) and
  144. (tfloatdef(resultdef).floattype=s80real) then
  145. begin
  146. if not assigned(locref) then
  147. getarrelementptrdef;
  148. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  149. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,arrptrelementdef,cpointerdef.getreusable(resultdef),locref^.base,hreg);
  150. locref^.base:=hreg;
  151. end;
  152. { packed arrays are represented by an array of byte, but when we operate
  153. on them we treat them as arrays of elements of packedbitsloadsize()
  154. -> typecast }
  155. if is_packed_array(left.resultdef) and
  156. (tarraydef(left.resultdef).elementdef.typ in [enumdef,orddef]) then
  157. begin
  158. getarrelementptrdef;
  159. packedloadsize:=packedbitsloadsize(tarraydef(left.resultdef).elementdef.packedbitsize);
  160. arrptrelementdef:=cpointerdef.getreusable(cgsize_orddef(int_cgsize(packedloadsize)));
  161. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,
  162. cpointerdef.getreusable(u8inttype),
  163. arrptrelementdef,
  164. locref^);
  165. end;
  166. end;
  167. procedure tllvmvecnode.update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);
  168. var
  169. hreg: tregister;
  170. begin
  171. if l<>resultdef.size then
  172. internalerror(2013102602);
  173. if constarrayoffset<>0 then
  174. begin
  175. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  176. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,ptruinttype,constarrayoffset,maybe_const_reg,hreg);
  177. maybe_const_reg:=hreg;
  178. constarrayoffset:=0;
  179. end;
  180. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(resultdef));
  181. location.reference:=thlcgllvm(hlcg).make_simple_ref(current_asmdata.CurrAsmList,location.reference,left.resultdef);
  182. if not is_dynamicstring(left.resultdef) and
  183. not is_dynamic_array(left.resultdef) then
  184. begin
  185. { get address of indexed array element and convert pointer to array into
  186. pointer to the elementdef in the process }
  187. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(hreg,cpointerdef.getreusable(left.resultdef),
  188. location.reference,ptruinttype,maybe_const_reg,true));
  189. arraytopointerconverted:=true;
  190. end
  191. else
  192. { the array is already a pointer -> just index }
  193. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(hreg,left.resultdef,
  194. location.reference,ptruinttype,maybe_const_reg,false));
  195. reference_reset_base(location.reference,hreg,0,location.reference.temppos,location.reference.alignment,location.reference.volatility);
  196. location.reference.alignment:=newalignment(location.reference.alignment,l);
  197. end;
  198. procedure tllvmvecnode.update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l: aint);
  199. var
  200. sref: tsubsetreference;
  201. offsetreg, basereg, hreg, hreg2: tregister;
  202. alignpower: aint;
  203. temp, intloadsize : longint;
  204. defloadsize: tdef;
  205. begin
  206. { only orddefs are bitpacked. Even then we only need special code in }
  207. { case the bitpacked *byte size* is not a power of two, otherwise }
  208. { everything can be handled using the the regular array code. }
  209. if ((l mod 8) = 0) and
  210. (ispowerof2(l div 8,temp) or
  211. not is_ordinal(resultdef)
  212. {$ifndef cpu64bitalu}
  213. or is_64bitint(resultdef)
  214. {$endif not cpu64bitalu}
  215. ) then
  216. begin
  217. update_reference_reg_mul(maybe_const_reg,regsize,l div 8);
  218. exit;
  219. end;
  220. if (l>8*sizeof(aint)) then
  221. internalerror(200608051);
  222. { adjust the index by subtracting the lower bound of the array and adding
  223. any constant adjustments }
  224. sref.ref:=location.reference;
  225. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  226. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,ptruinttype,tarraydef(left.resultdef).lowrange-constarrayoffset,maybe_const_reg,hreg);
  227. constarrayoffset:=0;
  228. { multiply index with bitsize of every element }
  229. hreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  230. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_MUL,ptruinttype,l,hreg,hreg2);
  231. hreg:=hreg2;
  232. { keep alignment for index }
  233. sref.ref.alignment:=left.resultdef.alignment;
  234. intloadsize:=packedbitsloadsize(l);
  235. if not ispowerof2(intloadsize,temp) then
  236. internalerror(2006081201);
  237. defloadsize:=cgsize_orddef(int_cgsize(intloadsize));
  238. alignpower:=temp;
  239. { determine start of the 8/16/32/64 bits chunk that contains the wanted
  240. value: divide the index by 8 (we're working with a bitpacked array here,
  241. all quantities are expressed in bits), and then by the size of the
  242. chunks (alignpower) }
  243. hreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  244. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,ptruinttype,3+alignpower,hreg,hreg2);
  245. offsetreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  246. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHL,ptruinttype,alignpower,hreg2,offsetreg);
  247. { index the array using this chunk index }
  248. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,cpointerdef.getreusable(defloadsize));
  249. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(basereg,cpointerdef.getreusable(left.resultdef),
  250. sref.ref,ptruinttype,offsetreg,true));
  251. arraytopointerconverted:=true;
  252. reference_reset_base(sref.ref,basereg,0,sref.ref.temppos,sref.ref.alignment,sref.ref.volatility);
  253. { calculate the bit index inside that chunk: mask out
  254. the chunk index part }
  255. hreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  256. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,ptruinttype,(1 shl (3+alignpower))-1,hreg,hreg2);
  257. sref.bitindexreg:=hreg2;
  258. sref.startbit:=0;
  259. sref.bitlen:=resultdef.packedbitsize;
  260. if (left.location.loc=LOC_REFERENCE) then
  261. location.loc:=LOC_SUBSETREF
  262. else
  263. location.loc:=LOC_CSUBSETREF;
  264. location.sref:=sref;
  265. end;
  266. procedure tllvmvecnode.update_reference_offset(var ref: treference; index, mulsize: aint);
  267. begin
  268. if not is_packed_array(left.resultdef) or
  269. not (tarraydef(left.resultdef).elementdef.typ in [enumdef,orddef]) then
  270. inc(constarrayoffset,index)
  271. else
  272. inc(constarrayoffset,index*mulsize)
  273. end;
  274. begin
  275. cloadparentfpnode:=tllvmloadparentfpnode;
  276. csubscriptnode:=tllvmsubscriptnode;
  277. cvecnode:=tllvmvecnode;
  278. end.