njvmmem.pas 7.2 KB

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