njvmmem.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. Generate JVM 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 njvmmem;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cgbase,cpubase,
  23. node,nmem,ncgmem;
  24. type
  25. tjvmvecnode = class(tcgvecnode)
  26. procedure pass_generate_code;override;
  27. end;
  28. implementation
  29. uses
  30. systems,
  31. cutils,verbose,
  32. symdef,defutil,
  33. aasmdata,pass_2,
  34. cgutils,hlcgobj,hlcgcpu;
  35. {*****************************************************************************
  36. TJVMVECNODE
  37. *****************************************************************************}
  38. procedure tjvmvecnode.pass_generate_code;
  39. var
  40. newsize: tcgsize;
  41. begin
  42. { This routine is not used for Strings, as they are a class type and
  43. you have to use charAt() there to load a character (and you cannot
  44. change characters; you have to create a new string in that case)
  45. As far as arrays are concerned: we have to create a trefererence
  46. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  47. pointer to the array (i.e., left.location.register) }
  48. secondpass(left);
  49. newsize:=def_cgsize(resultdef);
  50. if left.location.loc=LOC_CREFERENCE then
  51. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  52. else
  53. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  54. { don't use left.resultdef, because it may be an open or regular array,
  55. and then asking for the size doesn't make any sense }
  56. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  57. location.reference.base:=left.location.register;
  58. secondpass(right);
  59. { simplify index location if necessary, since array references support
  60. an index in memory, but not an another array index }
  61. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  62. (right.location.reference.arrayreftype<>art_none) then
  63. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  64. { adjust index if necessary }
  65. if not is_special_array(left.resultdef) and
  66. (tarraydef(left.resultdef).lowrange<>0) and
  67. (right.location.loc<>LOC_CONSTANT) then
  68. begin
  69. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  70. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  71. if right.location.loc<>LOC_REGISTER then
  72. begin
  73. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  74. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  75. end;
  76. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  77. end;
  78. { create array reference }
  79. case right.location.loc of
  80. LOC_REGISTER,LOC_CREGISTER:
  81. begin
  82. location.reference.arrayreftype:=art_indexreg;
  83. location.reference.index:=right.location.register;
  84. end;
  85. LOC_REFERENCE,LOC_CREFERENCE:
  86. begin
  87. location.reference.arrayreftype:=art_indexref;
  88. location.reference.indexbase:=right.location.reference.base;
  89. location.reference.indexsymbol:=right.location.reference.symbol;
  90. location.reference.indexoffset:=right.location.reference.offset;
  91. end;
  92. LOC_CONSTANT:
  93. begin
  94. location.reference.arrayreftype:=art_indexconst;
  95. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  96. end
  97. else
  98. internalerror(2011012002);
  99. end;
  100. end;
  101. begin
  102. cvecnode:=tjvmvecnode;
  103. end.