njvmmem.pas 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. function pass_1: tnode; override;
  27. procedure pass_generate_code;override;
  28. end;
  29. implementation
  30. uses
  31. systems,
  32. cutils,verbose,constexp,
  33. symconst,symtype,symtable,symsym,symdef,defutil,
  34. nadd,ncal,ncnv,ncon,
  35. aasmdata,pass_2,
  36. cgutils,hlcgobj,hlcgcpu;
  37. {*****************************************************************************
  38. TJVMVECNODE
  39. *****************************************************************************}
  40. function tjvmvecnode.pass_1: tnode;
  41. var
  42. psym: tsym;
  43. begin
  44. if is_wide_or_unicode_string(left.resultdef) then
  45. begin
  46. psym:=search_struct_member(java_jlstring,'CHARAT');
  47. if not assigned(psym) or
  48. (psym.typ<>procsym) then
  49. internalerror(2011031501);
  50. { Pascal strings are 1-based, Java strings 0-based }
  51. result:=ccallnode.create(ccallparanode.create(
  52. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  53. psym.owner,ctypeconvnode.create_explicit(left,java_jlstring),[]);
  54. left:=nil;
  55. right:=nil;
  56. exit;
  57. end
  58. else
  59. result:=inherited;
  60. end;
  61. procedure tjvmvecnode.pass_generate_code;
  62. var
  63. newsize: tcgsize;
  64. begin
  65. { This routine is not used for Strings, as they are a class type and
  66. you have to use charAt() there to load a character (and you cannot
  67. change characters; you have to create a new string in that case)
  68. As far as arrays are concerned: we have to create a trefererence
  69. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  70. pointer to the array (i.e., left.location.register) }
  71. secondpass(left);
  72. newsize:=def_cgsize(resultdef);
  73. if left.location.loc=LOC_CREFERENCE then
  74. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  75. else
  76. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  77. { don't use left.resultdef, because it may be an open or regular array,
  78. and then asking for the size doesn't make any sense }
  79. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  80. location.reference.base:=left.location.register;
  81. secondpass(right);
  82. { simplify index location if necessary, since array references support
  83. an index in memory, but not an another array index }
  84. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  85. (right.location.reference.arrayreftype<>art_none) then
  86. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  87. { adjust index if necessary }
  88. if not is_special_array(left.resultdef) and
  89. (tarraydef(left.resultdef).lowrange<>0) and
  90. (right.location.loc<>LOC_CONSTANT) then
  91. begin
  92. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  93. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  94. if right.location.loc<>LOC_REGISTER then
  95. begin
  96. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  97. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  98. end;
  99. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  100. end;
  101. { create array reference }
  102. case right.location.loc of
  103. LOC_REGISTER,LOC_CREGISTER:
  104. begin
  105. location.reference.arrayreftype:=art_indexreg;
  106. location.reference.index:=right.location.register;
  107. end;
  108. LOC_REFERENCE,LOC_CREFERENCE:
  109. begin
  110. location.reference.arrayreftype:=art_indexref;
  111. location.reference.indexbase:=right.location.reference.base;
  112. location.reference.indexsymbol:=right.location.reference.symbol;
  113. location.reference.indexoffset:=right.location.reference.offset;
  114. end;
  115. LOC_CONSTANT:
  116. begin
  117. location.reference.arrayreftype:=art_indexconst;
  118. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  119. end
  120. else
  121. internalerror(2011012002);
  122. end;
  123. end;
  124. begin
  125. cvecnode:=tjvmvecnode;
  126. end.