njvmmem.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. stringclass: tdef;
  137. begin
  138. if is_wide_or_unicode_string(left.resultdef) or
  139. is_ansistring(left.resultdef) then
  140. begin
  141. if is_ansistring(left.resultdef) then
  142. stringclass:=java_ansistring
  143. else
  144. stringclass:=java_jlstring;
  145. psym:=search_struct_member(tabstractrecorddef(stringclass),'CHARAT');
  146. if not assigned(psym) or
  147. (psym.typ<>procsym) then
  148. internalerror(2011031501);
  149. { Pascal strings are 1-based, Java strings 0-based }
  150. result:=ccallnode.create(ccallparanode.create(
  151. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  152. psym.owner,ctypeconvnode.create_explicit(left,stringclass),[]);
  153. left:=nil;
  154. right:=nil;
  155. exit;
  156. end
  157. else
  158. result:=inherited;
  159. end;
  160. procedure tjvmvecnode.pass_generate_code;
  161. var
  162. newsize: tcgsize;
  163. begin
  164. {$ifndef nounsupported}
  165. if left.resultdef.typ=stringdef then
  166. begin
  167. location:=left.location;
  168. exit;
  169. end;
  170. {$endif}
  171. { This routine is not used for Strings, as they are a class type and
  172. you have to use charAt() there to load a character (and you cannot
  173. change characters; you have to create a new string in that case)
  174. As far as arrays are concerned: we have to create a trefererence
  175. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  176. pointer to the array (i.e., left.location.register) }
  177. secondpass(left);
  178. newsize:=def_cgsize(resultdef);
  179. if left.location.loc=LOC_CREFERENCE then
  180. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  181. else
  182. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  183. { don't use left.resultdef, because it may be an open or regular array,
  184. and then asking for the size doesn't make any sense }
  185. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  186. location.reference.base:=left.location.register;
  187. secondpass(right);
  188. { simplify index location if necessary, since array references support
  189. an index in memory, but not an another array index }
  190. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  191. (right.location.reference.arrayreftype<>art_none) then
  192. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  193. { adjust index if necessary }
  194. if not is_special_array(left.resultdef) and
  195. (tarraydef(left.resultdef).lowrange<>0) and
  196. (right.location.loc<>LOC_CONSTANT) then
  197. begin
  198. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  199. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  200. if right.location.loc<>LOC_REGISTER then
  201. begin
  202. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  203. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  204. end;
  205. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  206. end;
  207. { create array reference }
  208. case right.location.loc of
  209. LOC_REGISTER,LOC_CREGISTER:
  210. begin
  211. location.reference.arrayreftype:=art_indexreg;
  212. location.reference.index:=right.location.register;
  213. end;
  214. LOC_REFERENCE,LOC_CREFERENCE:
  215. begin
  216. location.reference.arrayreftype:=art_indexref;
  217. location.reference.indexbase:=right.location.reference.base;
  218. location.reference.indexsymbol:=right.location.reference.symbol;
  219. location.reference.indexoffset:=right.location.reference.offset;
  220. end;
  221. LOC_CONSTANT:
  222. begin
  223. location.reference.arrayreftype:=art_indexconst;
  224. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  225. end
  226. else
  227. internalerror(2011012002);
  228. end;
  229. end;
  230. begin
  231. cvecnode:=tjvmvecnode;
  232. cloadparentfpnode:=tjvmloadparentfpnode;
  233. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  234. caddrnode:=tjvmaddrnode;
  235. end.