njvmmem.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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,njvmcon,
  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. var
  65. implicitptr: boolean;
  66. begin
  67. secondpass(left);
  68. implicitptr:=jvmimplicitpointertype(tpointerdef(left.resultdef).pointeddef);
  69. if (left.resultdef.typ=pointerdef) and
  70. ((left.resultdef=voidpointertype) or
  71. implicitptr) then
  72. begin
  73. if implicitptr then
  74. begin
  75. { this is basically a typecast: the left node is a regular
  76. 'pointer', and we typecast it to an implicit pointer }
  77. location_copy(location,left.location);
  78. { these implicit pointer types (records, sets, shortstrings, ...)
  79. cannot be located in registers on native targets (since
  80. they're not pointers there) -> force into memory to avoid
  81. confusing the compiler; this can happen when typecasting a
  82. Java class type into a pshortstring and then dereferencing etc
  83. }
  84. if location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  85. hlcg.location_force_mem(current_asmdata.CurrAsmList,location,left.resultdef);
  86. end
  87. else
  88. begin
  89. { these are always arrays (used internally for pointers to var
  90. parameters stored in nestedfpstructs) }
  91. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  92. location_reset_ref(location,LOC_REFERENCE,OS_ADDR,4);
  93. reference_reset_base(location.reference,left.location.register,0,4);
  94. location.reference.arrayreftype:=art_indexconst;
  95. end;
  96. end
  97. else
  98. internalerror(2011052901);
  99. end;
  100. {*****************************************************************************
  101. TJVMADDRNODE
  102. *****************************************************************************}
  103. function tjvmaddrnode.pass_typecheck: tnode;
  104. begin
  105. result:=nil;
  106. typecheckpass(left);
  107. if codegenerror then
  108. exit;
  109. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  110. if (left.resultdef.typ=procdef) or
  111. (
  112. (left.resultdef.typ=procvardef) and
  113. ((m_tp_procvar in current_settings.modeswitches) or
  114. (m_mac_procvar in current_settings.modeswitches))
  115. ) then
  116. begin
  117. result:=inherited;
  118. exit;
  119. end
  120. else
  121. begin
  122. if not(nf_internal in flags) and
  123. not jvmimplicitpointertype(left.resultdef) then
  124. begin
  125. CGMessage(parser_e_illegal_expression);
  126. exit
  127. end;
  128. result:=inherited;
  129. end;
  130. end;
  131. procedure tjvmaddrnode.pass_generate_code;
  132. var
  133. implicitptr: boolean;
  134. begin
  135. secondpass(left);
  136. implicitptr:=jvmimplicitpointertype(left.resultdef);
  137. if implicitptr or
  138. (nf_internal in flags) then
  139. begin
  140. if implicitptr then
  141. { this is basically a typecast: the left node is an implicit
  142. pointer, and we typecast it to a regular 'pointer'
  143. (java.lang.Object) }
  144. location_copy(location,left.location)
  145. else
  146. begin
  147. { these are always arrays (used internally for pointers to var
  148. parameters stored in nestedfpstructs) -> get base pointer to
  149. array }
  150. if (left.location.loc<>LOC_REFERENCE) or
  151. (left.location.reference.arrayreftype<>art_indexconst) or
  152. (left.location.reference.base=NR_NO) or
  153. assigned(left.location.reference.symbol) then
  154. internalerror(2011060701);
  155. location_reset(location,LOC_REGISTER,OS_ADDR);
  156. location.register:=left.location.reference.base;
  157. end;
  158. end
  159. else
  160. begin
  161. { procvar }
  162. {$ifndef nounsupported}
  163. location_reset(location,LOC_REGISTER,OS_ADDR);
  164. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  165. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,java_jlobject,0,location.register);
  166. {$else}
  167. internalerror(2011051601);
  168. {$endif}
  169. end;
  170. end;
  171. {*****************************************************************************
  172. TJVMLOADVMTADDRNODE
  173. *****************************************************************************}
  174. procedure tjvmloadvmtaddrnode.pass_generate_code;
  175. begin
  176. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(
  177. tobjectdef(tclassrefdef(resultdef).pointeddef).jvm_full_typename(true))));
  178. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  179. location_reset(location,LOC_REGISTER,OS_ADDR);
  180. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  181. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  182. end;
  183. {*****************************************************************************
  184. TJVMVECNODE
  185. *****************************************************************************}
  186. function tjvmvecnode.pass_1: tnode;
  187. var
  188. psym: tsym;
  189. stringclass: tdef;
  190. begin
  191. if (left.resultdef.typ=stringdef) then
  192. begin
  193. case tstringdef(left.resultdef).stringtype of
  194. st_ansistring:
  195. stringclass:=java_ansistring;
  196. st_unicodestring,
  197. st_widestring:
  198. stringclass:=java_jlstring;
  199. st_shortstring:
  200. begin
  201. stringclass:=java_shortstring;
  202. left:=caddrnode.create_internal(left);
  203. { avoid useless typecheck when casting to shortstringclass }
  204. include(left.flags,nf_typedaddr);
  205. end
  206. else
  207. internalerror(2011052407);
  208. end;
  209. psym:=search_struct_member(tabstractrecorddef(stringclass),'CHARAT');
  210. if not assigned(psym) or
  211. (psym.typ<>procsym) then
  212. internalerror(2011031501);
  213. { Pascal strings are 1-based, Java strings 0-based }
  214. result:=ccallnode.create(ccallparanode.create(
  215. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  216. psym.owner,ctypeconvnode.create_explicit(left,stringclass),[]);
  217. left:=nil;
  218. right:=nil;
  219. exit;
  220. end
  221. else
  222. begin
  223. { keep indices that are enum constants that way, rather than
  224. transforming them into a load of the class instance that
  225. represents this constant (since we then would have to extract
  226. the int constant value again at run time anyway) }
  227. if right.nodetype=ordconstn then
  228. tjvmordconstnode(right).enumconstok:=true;
  229. result:=inherited;
  230. end;
  231. end;
  232. procedure tjvmvecnode.pass_generate_code;
  233. var
  234. psym: tsym;
  235. newsize: tcgsize;
  236. begin
  237. if left.resultdef.typ=stringdef then
  238. internalerror(2011052702);
  239. { This routine is not used for Strings, as they are a class type and
  240. you have to use charAt() there to load a character (and you cannot
  241. change characters; you have to create a new string in that case)
  242. As far as arrays are concerned: we have to create a trefererence
  243. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  244. pointer to the array (i.e., left.location.register) }
  245. secondpass(left);
  246. newsize:=def_cgsize(resultdef);
  247. if left.location.loc=LOC_CREFERENCE then
  248. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  249. else
  250. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  251. { don't use left.resultdef, because it may be an open or regular array,
  252. and then asking for the size doesn't make any sense }
  253. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  254. location.reference.base:=left.location.register;
  255. secondpass(right);
  256. { simplify index location if necessary, since array references support
  257. an index in memory, but not an another array index }
  258. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  259. (right.location.reference.arrayreftype<>art_none) then
  260. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  261. { replace enum class instance with the corresponding integer value }
  262. if (right.resultdef.typ=enumdef) then
  263. begin
  264. if (right.location.loc<>LOC_CONSTANT) then
  265. begin
  266. psym:=search_struct_member(tenumdef(right.resultdef).classdef,'FPCORDINAL');
  267. if not assigned(psym) or
  268. (psym.typ<>procsym) or
  269. (tprocsym(psym).ProcdefList.count<>1) then
  270. internalerror(2011062607);
  271. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  272. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(tprocsym(psym).procdeflist[0]),tprocdef(tprocsym(psym).procdeflist[0]).mangledname,false);
  273. { call replaces self parameter with longint result -> no stack
  274. height change }
  275. location_reset(right.location,LOC_REGISTER,OS_S32);
  276. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  277. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,s32inttype,right.location.register);
  278. end;
  279. { always force to integer location, because enums are handled as
  280. object instances (since that's what they are in Java) }
  281. right.resultdef:=s32inttype;
  282. right.location.size:=OS_S32;
  283. end;
  284. { adjust index if necessary }
  285. if not is_special_array(left.resultdef) and
  286. (tarraydef(left.resultdef).lowrange<>0) and
  287. (right.location.loc<>LOC_CONSTANT) then
  288. begin
  289. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  290. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  291. if right.location.loc<>LOC_REGISTER then
  292. begin
  293. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  294. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  295. end;
  296. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  297. end;
  298. { create array reference }
  299. case right.location.loc of
  300. LOC_REGISTER,LOC_CREGISTER:
  301. begin
  302. location.reference.arrayreftype:=art_indexreg;
  303. location.reference.index:=right.location.register;
  304. end;
  305. LOC_REFERENCE,LOC_CREFERENCE:
  306. begin
  307. location.reference.arrayreftype:=art_indexref;
  308. location.reference.indexbase:=right.location.reference.base;
  309. location.reference.indexsymbol:=right.location.reference.symbol;
  310. location.reference.indexoffset:=right.location.reference.offset;
  311. end;
  312. LOC_CONSTANT:
  313. begin
  314. location.reference.arrayreftype:=art_indexconst;
  315. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  316. end
  317. else
  318. internalerror(2011012002);
  319. end;
  320. end;
  321. begin
  322. cderefnode:=tjvmderefnode;
  323. caddrnode:=tjvmaddrnode;
  324. cvecnode:=tjvmvecnode;
  325. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  326. end.