njvmmem.pas 11 KB

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