njvmmem.pas 6.3 KB

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