nllvmmem.pas 11 KB

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