nllvmmem.pas 11 KB

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