nllvmmem.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. subscripteddef,
  53. llvmfielddef: tdef;
  54. newbase: tregister;
  55. begin
  56. if not(location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) then
  57. internalerror(2014011905);
  58. if is_packed_record_or_object(left.resultdef) then
  59. begin
  60. { typecast the result to the expected type, but don't actually index
  61. (that still has to be done by the generic code, so return false) }
  62. newbase:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(resultdef));
  63. hlcg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.resultdef,getpointerdef(resultdef),location.reference,newbase);
  64. reference_reset_base(location.reference,newbase,0,location.reference.alignment);
  65. result:=false;
  66. end
  67. else
  68. begin
  69. { get the type of the corresponding field in the llvm shadow
  70. definition }
  71. llvmfielddef:=tabstractrecordsymtable(tabstractrecorddef(left.resultdef).symtable).llvmst[vs.llvmfieldnr].def;
  72. { load the address of that shadow field }
  73. newbase:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(llvmfielddef));
  74. location.reference:=thlcgllvm(hlcg).make_simple_ref(current_asmdata.CurrAsmList,location.reference,left.resultdef);
  75. if is_implicit_pointer_object_type(left.resultdef) then
  76. subscripteddef:=left.resultdef
  77. else
  78. subscripteddef:=getpointerdef(left.resultdef);
  79. current_asmdata.CurrAsmList.concat(taillvm.getelementptr_reg_size_ref_size_const(newbase,subscripteddef,location.reference,s32inttype,vs.llvmfieldnr,true));
  80. reference_reset_base(location.reference,newbase,vs.offsetfromllvmfield,newalignment(location.reference.alignment,vs.fieldoffset));
  81. { in case of an 80 bits extended type, typecast from an array of 10
  82. bytes (used because otherwise llvm will allocate the ABI-defined
  83. size for extended, which is usually larger) into an extended }
  84. if (llvmfielddef.typ=floatdef) and
  85. (tfloatdef(llvmfielddef).floattype=s80real) then
  86. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,getpointerdef(getarraydef(u8inttype,10)),getpointerdef(s80floattype),location.reference);
  87. { if it doesn't match the requested field exactly (variant record),
  88. adjust the type of the pointer }
  89. if (vs.offsetfromllvmfield<>0) or
  90. (llvmfielddef<>resultdef) then
  91. hlcg.g_ptrtypecast_ref(current_asmdata.CurrAsmList,getpointerdef(llvmfielddef),getpointerdef(resultdef),location.reference);
  92. location.size:=def_cgsize(resultdef);
  93. result:=true;
  94. end;
  95. end;
  96. { tllvmvecnode }
  97. procedure tllvmvecnode.pass_generate_code;
  98. var
  99. locref: preference;
  100. hreg: tregister;
  101. arrptrelementdef: tdef;
  102. procedure getarrelementptrdef;
  103. begin
  104. if assigned(locref) then
  105. exit;
  106. case location.loc of
  107. LOC_SUBSETREF,LOC_CSUBSETREF:
  108. locref:[email protected];
  109. LOC_REFERENCE,LOC_CREFERENCE:
  110. locref:[email protected];
  111. else
  112. internalerror(2013111001);
  113. end;
  114. { special handling for s80real: inside aggregates (such as arrays) it's
  115. declared as an array of 10 bytes in order to force the allocation of
  116. the right size (llvm only supports s80real according to the ABI size/
  117. alignment) -> convert the pointer to this array into a pointer to the
  118. s80real type (loads from and stores to this type will always only store
  119. 10 bytes) }
  120. if (resultdef.typ=floatdef) and
  121. (tfloatdef(resultdef).floattype=s80real) then
  122. arrptrelementdef:=getpointerdef(getarraydef(u8inttype,10))
  123. else
  124. arrptrelementdef:=getpointerdef(resultdef);
  125. end;
  126. begin
  127. inherited;
  128. locref:=nil;
  129. { avoid uninitialised warning }
  130. arrptrelementdef:=nil;
  131. if not arraytopointerconverted and
  132. not is_dynamicstring(left.resultdef) and
  133. not is_dynamic_array(left.resultdef) then
  134. begin
  135. { the result is currently a pointer to left.resultdef (the array type)
  136. -> convert it into a pointer to an element inside this array }
  137. getarrelementptrdef;
  138. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,arrptrelementdef);
  139. locref^:=thlcgllvm(hlcg).make_simple_ref(current_asmdata.CurrAsmList,location.reference,left.resultdef);
  140. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_const(hreg,getpointerdef(left.resultdef),
  141. locref^,ptruinttype,constarrayoffset,true));
  142. reference_reset_base(locref^,hreg,0,locref^.alignment);
  143. end;
  144. { see comment in getarrelementptrdef }
  145. if (resultdef.typ=floatdef) and
  146. (tfloatdef(resultdef).floattype=s80real) then
  147. begin
  148. if not assigned(locref) then
  149. getarrelementptrdef;
  150. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(resultdef));
  151. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,arrptrelementdef,getpointerdef(resultdef),locref^.base,hreg);
  152. locref^.base:=hreg;
  153. end;
  154. end;
  155. procedure tllvmvecnode.update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);
  156. var
  157. hreg: tregister;
  158. begin
  159. if l<>resultdef.size then
  160. internalerror(2013102602);
  161. if constarrayoffset<>0 then
  162. begin
  163. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  164. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,ptruinttype,constarrayoffset,maybe_const_reg,hreg);
  165. maybe_const_reg:=hreg;
  166. end;
  167. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(resultdef));
  168. location.reference:=thlcgllvm(hlcg).make_simple_ref(current_asmdata.CurrAsmList,location.reference,left.resultdef);
  169. if not is_dynamicstring(left.resultdef) and
  170. not is_dynamic_array(left.resultdef) then
  171. begin
  172. { get address of indexed array element and convert pointer to array into
  173. pointer to the elementdef in the process }
  174. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(hreg,getpointerdef(left.resultdef),
  175. location.reference,ptruinttype,maybe_const_reg,true));
  176. arraytopointerconverted:=true;
  177. end
  178. else
  179. { the array is already a pointer -> just index }
  180. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(hreg,left.resultdef,
  181. location.reference,ptruinttype,maybe_const_reg,false));
  182. reference_reset_base(location.reference,hreg,0,location.reference.alignment);
  183. location.reference.alignment:=newalignment(location.reference.alignment,l);
  184. end;
  185. procedure tllvmvecnode.update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l: aint);
  186. var
  187. sref: tsubsetreference;
  188. offsetreg, basereg, hreg, hreg2: tregister;
  189. alignpower: aint;
  190. temp, intloadsize : longint;
  191. defloadsize: tdef;
  192. begin
  193. { only orddefs are bitpacked. Even then we only need special code in }
  194. { case the bitpacked *byte size* is not a power of two, otherwise }
  195. { everything can be handled using the the regular array code. }
  196. if ((l mod 8) = 0) and
  197. (ispowerof2(l div 8,temp) or
  198. not is_ordinal(resultdef)
  199. {$ifndef cpu64bitalu}
  200. or is_64bitint(resultdef)
  201. {$endif not cpu64bitalu}
  202. ) then
  203. begin
  204. update_reference_reg_mul(maybe_const_reg,regsize,l div 8);
  205. exit;
  206. end;
  207. if (l>8*sizeof(aint)) then
  208. internalerror(200608051);
  209. { adjust the index by subtracting the lower bound of the array and adding
  210. any constant adjustments }
  211. sref.ref:=location.reference;
  212. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  213. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,ptruinttype,tarraydef(left.resultdef).lowrange-constarrayoffset,maybe_const_reg,hreg);
  214. { keep alignment for index }
  215. sref.ref.alignment:=left.resultdef.alignment;
  216. intloadsize:=packedbitsloadsize(l);
  217. if not ispowerof2(intloadsize,temp) then
  218. internalerror(2006081201);
  219. defloadsize:=cgsize_orddef(int_cgsize(intloadsize));
  220. alignpower:=temp;
  221. { determine start of the 8/16/32/64 bits chunk that contains the wanted
  222. value: divide the index by 8 (we're working with a bitpacked array here,
  223. all quantities are expressed in bits), and then by the size of the
  224. chunks (alignpower) }
  225. offsetreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  226. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,ptruinttype,3+alignpower,hreg,offsetreg);
  227. { index the array using this chunk index }
  228. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(defloadsize));
  229. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(basereg,getpointerdef(left.resultdef),
  230. sref.ref,ptruinttype,offsetreg,true));
  231. arraytopointerconverted:=true;
  232. reference_reset_base(sref.ref,basereg,0,sref.ref.alignment);
  233. { calculate the bit index inside that chunk }
  234. hreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  235. { multiple index with bitsize of every element }
  236. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_MUL,ptruinttype,l,hreg,hreg2);
  237. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  238. { mask out the chunk index part }
  239. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,ptruinttype,(1 shl (3+alignpower))-1,hreg2,hreg);
  240. sref.bitindexreg:=hreg;
  241. sref.startbit:=0;
  242. sref.bitlen:=resultdef.packedbitsize;
  243. if (left.location.loc=LOC_REFERENCE) then
  244. location.loc:=LOC_SUBSETREF
  245. else
  246. location.loc:=LOC_CSUBSETREF;
  247. location.sref:=sref;
  248. end;
  249. procedure tllvmvecnode.update_reference_offset(var ref: treference; index, mulsize: aint);
  250. begin
  251. inc(constarrayoffset,index);
  252. end;
  253. begin
  254. cloadparentfpnode:=tllvmloadparentfpnode;
  255. csubscriptnode:=tllvmsubscriptnode;
  256. cvecnode:=tllvmvecnode;
  257. end.