njvmmem.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. tjvmaddrnode = class(tcgaddrnode)
  26. function pass_typecheck: tnode; override;
  27. procedure pass_generate_code; override;
  28. end;
  29. tjvmloadvmtaddrnode = class(tcgloadvmtaddrnode)
  30. procedure pass_generate_code; override;
  31. end;
  32. tjvmloadparentfpnode = class(tcgloadparentfpnode)
  33. procedure pass_generate_code;override;
  34. end;
  35. tjvmvecnode = class(tcgvecnode)
  36. function pass_1: tnode; override;
  37. procedure pass_generate_code;override;
  38. end;
  39. implementation
  40. uses
  41. systems,globals,
  42. cutils,verbose,constexp,
  43. symconst,symtype,symtable,symsym,symdef,defutil,jvmdef,
  44. htypechk,
  45. nadd,ncal,ncnv,ncon,pass_1,
  46. aasmdata,aasmcpu,pass_2,
  47. cgutils,hlcgobj,hlcgcpu;
  48. {*****************************************************************************
  49. TJVMADDRNODE
  50. *****************************************************************************}
  51. function tjvmaddrnode.pass_typecheck: tnode;
  52. begin
  53. result:=nil;
  54. typecheckpass(left);
  55. if codegenerror then
  56. exit;
  57. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  58. if (left.resultdef.typ=procdef) or
  59. (
  60. (left.resultdef.typ=procvardef) and
  61. ((m_tp_procvar in current_settings.modeswitches) or
  62. (m_mac_procvar in current_settings.modeswitches))
  63. ) then
  64. begin
  65. result:=inherited;
  66. exit;
  67. end;
  68. if not jvmimplicitpointertype(left.resultdef) then
  69. begin
  70. CGMessage(parser_e_illegal_expression);
  71. exit
  72. end;
  73. resultdef:=java_jlobject;
  74. if mark_read_written then
  75. begin
  76. { This is actually only "read", but treat it nevertheless as }
  77. { modified due to the possible use of pointers }
  78. { To avoid false positives regarding "uninitialised" }
  79. { warnings when using arrays, perform it in two steps }
  80. set_varstate(left,vs_written,[]);
  81. { vsf_must_be_valid so it doesn't get changed into }
  82. { vsf_referred_not_inited }
  83. set_varstate(left,vs_read,[vsf_must_be_valid]);
  84. end;
  85. end;
  86. procedure tjvmaddrnode.pass_generate_code;
  87. begin
  88. secondpass(left);
  89. if jvmimplicitpointertype(left.resultdef) then
  90. begin
  91. { this is basically a typecast: the left node is an implicit
  92. pointer, and we typecast it to a regular 'pointer'
  93. (java.lang.Object) }
  94. location_copy(location,left.location);
  95. end
  96. else
  97. begin
  98. {$ifndef nounsupported}
  99. location_reset(location,LOC_REGISTER,OS_ADDR);
  100. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  101. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,java_jlobject,0,location.register);
  102. {$else}
  103. internalerror(2011051601);
  104. {$endif}
  105. end;
  106. end;
  107. {*****************************************************************************
  108. TJVMLOADVMTADDRNODE
  109. *****************************************************************************}
  110. procedure tjvmloadvmtaddrnode.pass_generate_code;
  111. begin
  112. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(
  113. tobjectdef(tclassrefdef(resultdef).pointeddef).jvm_full_typename(true))));
  114. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  115. location_reset(location,LOC_REGISTER,OS_ADDR);
  116. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  117. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  118. end;
  119. { tjvmloadparentfpnode }
  120. procedure tjvmloadparentfpnode.pass_generate_code;
  121. begin
  122. {$ifndef nounsupported}
  123. location_reset(location,LOC_REGISTER,OS_ADDR);
  124. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  125. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,java_jlobject,0,location.register);
  126. {$else}
  127. internalerror(2011041301);
  128. {$endif}
  129. end;
  130. {*****************************************************************************
  131. TJVMVECNODE
  132. *****************************************************************************}
  133. function tjvmvecnode.pass_1: tnode;
  134. var
  135. psym: tsym;
  136. begin
  137. if is_wide_or_unicode_string(left.resultdef) then
  138. begin
  139. psym:=search_struct_member(java_jlstring,'CHARAT');
  140. if not assigned(psym) or
  141. (psym.typ<>procsym) then
  142. internalerror(2011031501);
  143. { Pascal strings are 1-based, Java strings 0-based }
  144. result:=ccallnode.create(ccallparanode.create(
  145. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  146. psym.owner,ctypeconvnode.create_explicit(left,java_jlstring),[]);
  147. left:=nil;
  148. right:=nil;
  149. exit;
  150. end
  151. else
  152. result:=inherited;
  153. end;
  154. procedure tjvmvecnode.pass_generate_code;
  155. var
  156. newsize: tcgsize;
  157. begin
  158. {$ifndef nounsupported}
  159. if left.resultdef.typ=stringdef then
  160. begin
  161. location:=left.location;
  162. exit;
  163. end;
  164. {$endif}
  165. { This routine is not used for Strings, as they are a class type and
  166. you have to use charAt() there to load a character (and you cannot
  167. change characters; you have to create a new string in that case)
  168. As far as arrays are concerned: we have to create a trefererence
  169. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  170. pointer to the array (i.e., left.location.register) }
  171. secondpass(left);
  172. newsize:=def_cgsize(resultdef);
  173. if left.location.loc=LOC_CREFERENCE then
  174. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  175. else
  176. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  177. { don't use left.resultdef, because it may be an open or regular array,
  178. and then asking for the size doesn't make any sense }
  179. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  180. location.reference.base:=left.location.register;
  181. secondpass(right);
  182. { simplify index location if necessary, since array references support
  183. an index in memory, but not an another array index }
  184. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  185. (right.location.reference.arrayreftype<>art_none) then
  186. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  187. { adjust index if necessary }
  188. if not is_special_array(left.resultdef) and
  189. (tarraydef(left.resultdef).lowrange<>0) and
  190. (right.location.loc<>LOC_CONSTANT) then
  191. begin
  192. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  193. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  194. if right.location.loc<>LOC_REGISTER then
  195. begin
  196. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  197. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  198. end;
  199. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  200. end;
  201. { create array reference }
  202. case right.location.loc of
  203. LOC_REGISTER,LOC_CREGISTER:
  204. begin
  205. location.reference.arrayreftype:=art_indexreg;
  206. location.reference.index:=right.location.register;
  207. end;
  208. LOC_REFERENCE,LOC_CREFERENCE:
  209. begin
  210. location.reference.arrayreftype:=art_indexref;
  211. location.reference.indexbase:=right.location.reference.base;
  212. location.reference.indexsymbol:=right.location.reference.symbol;
  213. location.reference.indexoffset:=right.location.reference.offset;
  214. end;
  215. LOC_CONSTANT:
  216. begin
  217. location.reference.arrayreftype:=art_indexconst;
  218. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  219. end
  220. else
  221. internalerror(2011012002);
  222. end;
  223. end;
  224. begin
  225. cvecnode:=tjvmvecnode;
  226. cloadparentfpnode:=tjvmloadparentfpnode;
  227. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  228. caddrnode:=tjvmaddrnode;
  229. end.