njvmmem.pas 10 KB

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