nllvmmem.pas 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. {$mode objfpc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cgutils,
  23. symtype,
  24. node,ncgnstmm, ncgmem;
  25. type
  26. tllvmloadparentfpnode = class(tcgnestloadparentfpnode)
  27. end;
  28. tllvmvecnode= class(tcgvecnode)
  29. private
  30. constarrayoffset: aint;
  31. arraytopointerconverted: boolean;
  32. public
  33. procedure pass_generate_code; override;
  34. procedure update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint); override;
  35. procedure update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l: aint); override;
  36. procedure update_reference_offset(var ref: treference; index, mulsize: aint); override;
  37. end;
  38. implementation
  39. uses
  40. verbose,cutils,
  41. aasmdata,aasmllvm,
  42. symconst,symdef,defutil,
  43. nmem,
  44. cpubase,llvmbase,hlcgobj;
  45. { tllvmvecnode }
  46. procedure tllvmvecnode.pass_generate_code;
  47. var
  48. locref: preference;
  49. hreg: tregister;
  50. arrptrelementdef: tdef;
  51. procedure getarrelementptrdef;
  52. begin
  53. if assigned(locref) then
  54. exit;
  55. case location.loc of
  56. LOC_SUBSETREF,LOC_CSUBSETREF:
  57. locref:[email protected];
  58. LOC_REFERENCE,LOC_CREFERENCE:
  59. locref:[email protected];
  60. else
  61. internalerror(2013111001);
  62. end;
  63. { special handling for s80real: inside aggregates (such as arrays) it's
  64. declared as an array of 10 bytes in order to force the allocation of
  65. the right size (llvm only supports s80real according to the ABI size/
  66. alignment) -> convert the pointer to this array into a pointer to the
  67. s80real type (loads from and stores to this type will always only store
  68. 10 bytes) }
  69. if (resultdef.typ=floatdef) and
  70. (tfloatdef(resultdef).floattype=s80real) then
  71. arrptrelementdef:=getpointerdef(getarraydef(u8inttype,10))
  72. else
  73. arrptrelementdef:=getpointerdef(resultdef);
  74. end;
  75. begin
  76. inherited;
  77. locref:=nil;
  78. if not arraytopointerconverted then
  79. begin
  80. { the result is currently a pointer left.resultdef (the array type)
  81. -> convert it into a pointer to an element inside this array }
  82. getarrelementptrdef;
  83. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,arrptrelementdef);
  84. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_const(hreg,getpointerdef(left.resultdef),
  85. locref^,ptruinttype,constarrayoffset,true));
  86. reference_reset_base(locref^,hreg,0,locref^.alignment);
  87. end;
  88. { see comment in getarrelementptrdef }
  89. if (resultdef.typ=floatdef) and
  90. (tfloatdef(resultdef).floattype=s80real) then
  91. begin
  92. getarrelementptrdef;
  93. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(resultdef));
  94. hlcg.a_load_reg_reg(current_asmdata.CurrAsmList,arrptrelementdef,getpointerdef(resultdef),locref^.base,hreg);
  95. locref^.base:=hreg;
  96. end;
  97. end;
  98. procedure tllvmvecnode.update_reference_reg_mul(maybe_const_reg: tregister; regsize: tdef; l: aint);
  99. var
  100. hreg: tregister;
  101. begin
  102. if l<>resultdef.size then
  103. internalerror(2013102602);
  104. if constarrayoffset<>0 then
  105. begin
  106. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  107. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_ADD,ptruinttype,constarrayoffset,maybe_const_reg,hreg);
  108. maybe_const_reg:=hreg;
  109. end;
  110. hreg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(resultdef));
  111. { get address of indexed array element and convert pointer to array into
  112. pointer to the elementdef in the process }
  113. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(hreg,getpointerdef(left.resultdef),
  114. location.reference,ptruinttype,maybe_const_reg,true));
  115. arraytopointerconverted:=true;
  116. reference_reset_base(location.reference,hreg,0,location.reference.alignment);
  117. location.reference.alignment:=newalignment(location.reference.alignment,l);
  118. end;
  119. procedure tllvmvecnode.update_reference_reg_packed(maybe_const_reg: tregister; regsize: tdef; l: aint);
  120. var
  121. sref: tsubsetreference;
  122. offsetreg, basereg, hreg, hreg2: tregister;
  123. alignpower: aint;
  124. temp, intloadsize : longint;
  125. defloadsize: tdef;
  126. begin
  127. { only orddefs are bitpacked. Even then we only need special code in }
  128. { case the bitpacked *byte size* is not a power of two, otherwise }
  129. { everything can be handled using the the regular array code. }
  130. if ((l mod 8) = 0) and
  131. (ispowerof2(l div 8,temp) or
  132. not is_ordinal(resultdef)
  133. {$ifndef cpu64bitalu}
  134. or is_64bitint(resultdef)
  135. {$endif not cpu64bitalu}
  136. ) then
  137. begin
  138. update_reference_reg_mul(maybe_const_reg,regsize,l div 8);
  139. exit;
  140. end;
  141. if (l>8*sizeof(aint)) then
  142. internalerror(200608051);
  143. { adjust the index by subtracting the lower bound of the array and adding
  144. any constant adjustments }
  145. sref.ref:=location.reference;
  146. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  147. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SUB,ptruinttype,tarraydef(left.resultdef).lowrange-constarrayoffset,maybe_const_reg,hreg);
  148. { keep alignment for index }
  149. sref.ref.alignment:=left.resultdef.alignment;
  150. intloadsize:=packedbitsloadsize(l);
  151. if not ispowerof2(intloadsize,temp) then
  152. internalerror(2006081201);
  153. defloadsize:=cgsize_orddef(int_cgsize(intloadsize));
  154. alignpower:=temp;
  155. { determine start of the 8/16/32/64 bits chunk that contains the wanted
  156. value: divide the index by 8 (we're working with a bitpacked array here,
  157. all quantities are expressed in bits), and then by the size of the
  158. chunks (alignpower) }
  159. offsetreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  160. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_SHR,ptruinttype,3+alignpower,hreg,offsetreg);
  161. { index the array using this chunk index }
  162. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,getpointerdef(defloadsize));
  163. current_asmdata.CurrAsmList.Concat(taillvm.getelementptr_reg_size_ref_size_reg(basereg,getpointerdef(left.resultdef),
  164. sref.ref,ptruinttype,offsetreg,true));
  165. arraytopointerconverted:=true;
  166. reference_reset_base(sref.ref,basereg,0,sref.ref.alignment);
  167. { calculate the bit index inside that chunk }
  168. hreg2:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  169. { multiple index with bitsize of every element }
  170. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_MUL,ptruinttype,l,hreg,hreg2);
  171. hreg:=hlcg.getintregister(current_asmdata.CurrAsmList,ptruinttype);
  172. { mask out the chunk index part }
  173. hlcg.a_op_const_reg_reg(current_asmdata.CurrAsmList,OP_AND,ptruinttype,(1 shl (3+alignpower))-1,hreg2,hreg);
  174. sref.bitindexreg:=hreg;
  175. sref.startbit:=0;
  176. sref.bitlen:=resultdef.packedbitsize;
  177. if (left.location.loc=LOC_REFERENCE) then
  178. location.loc:=LOC_SUBSETREF
  179. else
  180. location.loc:=LOC_CSUBSETREF;
  181. location.sref:=sref;
  182. end;
  183. procedure tllvmvecnode.update_reference_offset(var ref: treference; index, mulsize: aint);
  184. begin
  185. inc(constarrayoffset,index);
  186. end;
  187. begin
  188. cloadparentfpnode:=tllvmloadparentfpnode;
  189. cvecnode:=tllvmvecnode;
  190. end.