njvmmem.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. protected
  27. function isrefparaload: boolean;
  28. function isarrayele0load: boolean;
  29. public
  30. function pass_typecheck: tnode; override;
  31. procedure pass_generate_code; override;
  32. end;
  33. tjvmderefnode = class(tcgderefnode)
  34. function pass_typecheck: tnode; override;
  35. procedure pass_generate_code; override;
  36. end;
  37. tjvmloadvmtaddrnode = class(tcgloadvmtaddrnode)
  38. procedure pass_generate_code; override;
  39. end;
  40. tjvmvecnode = class(tcgvecnode)
  41. function pass_1: tnode; override;
  42. procedure pass_generate_code;override;
  43. end;
  44. implementation
  45. uses
  46. systems,globals,
  47. cutils,verbose,constexp,
  48. symconst,symtype,symtable,symsym,symdef,defutil,jvmdef,
  49. htypechk,paramgr,
  50. nadd,ncal,ncnv,ncon,nld,pass_1,njvmcon,
  51. aasmdata,aasmcpu,pass_2,
  52. cgutils,hlcgobj,hlcgcpu;
  53. {*****************************************************************************
  54. TJVMDEREFNODE
  55. *****************************************************************************}
  56. function tjvmderefnode.pass_typecheck: tnode;
  57. begin
  58. result:=inherited pass_typecheck;
  59. if assigned(result) then
  60. exit;
  61. { don't allow dereferencing untyped pointers, because how this has to
  62. be done depends on whether it's a pointer to an implicit pointer type
  63. or not }
  64. if is_voidpointer(left.resultdef) then
  65. CGMessage(parser_e_illegal_expression);
  66. end;
  67. procedure tjvmderefnode.pass_generate_code;
  68. var
  69. implicitptr: boolean;
  70. begin
  71. secondpass(left);
  72. implicitptr:=jvmimplicitpointertype(resultdef);
  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, and by programmers for any
  91. kind of pointers) }
  92. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  93. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),4);
  94. reference_reset_base(location.reference,left.location.register,0,4);
  95. location.reference.arrayreftype:=art_indexconst;
  96. if (left.nodetype<>addrn) and
  97. not(resultdef.typ in [orddef,floatdef]) and
  98. not is_voidpointer(resultdef) and
  99. ((resultdef.typ<>objectdef) or
  100. (find_real_class_definition(tobjectdef(resultdef),false)<>java_jlobject)) then
  101. location.reference.checkcast:=true;
  102. end
  103. end;
  104. {*****************************************************************************
  105. TJVMADDRNODE
  106. *****************************************************************************}
  107. function tjvmaddrnode.isrefparaload: boolean;
  108. begin
  109. result:=
  110. (left.nodetype=loadn) and
  111. (tloadnode(left).symtableentry.typ=paravarsym) and
  112. paramanager.push_copyout_param(tparavarsym(tloadnode(left).symtableentry).varspez,
  113. left.resultdef,
  114. tabstractprocdef(tloadnode(left).symtableentry.owner.defowner).proccalloption);
  115. end;
  116. function tjvmaddrnode.isarrayele0load: boolean;
  117. begin
  118. result:=
  119. (left.nodetype=vecn) and
  120. (tvecnode(left).left.resultdef.typ=arraydef) and
  121. (tvecnode(left).right.nodetype=ordconstn) and
  122. (tordconstnode(tvecnode(left).right).value=0);
  123. end;
  124. function tjvmaddrnode.pass_typecheck: tnode;
  125. var
  126. fsym: tsym;
  127. begin
  128. result:=nil;
  129. typecheckpass(left);
  130. if codegenerror then
  131. exit;
  132. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  133. { in TP/Delphi, @procvar = contents of procvar and @@procvar =
  134. address of procvar. In case of a procedure of object, this works
  135. by letting the first addrnode typecast the procvar into a tmethod
  136. record followed by subscripting its "code" field (= first field),
  137. and if there's a second addrnode then it takes the address of
  138. this code field (which is hence also the address of the procvar).
  139. In Java, such ugly hacks don't work -> replace first addrnode
  140. with getting procvar.method.code, and second addrnode with
  141. the class for procedure of object}
  142. if not(nf_internal in flags) and
  143. ((m_tp_procvar in current_settings.modeswitches) or
  144. (m_mac_procvar in current_settings.modeswitches)) and
  145. (((left.nodetype=addrn) and
  146. (taddrnode(left).left.resultdef.typ=procvardef)) or
  147. (left.resultdef.typ=procvardef)) then
  148. begin
  149. if (left.nodetype=addrn) and
  150. (taddrnode(left).left.resultdef.typ=procvardef) then
  151. begin
  152. { double address -> pointer that is the address of the
  153. procvardef (don't allow for non-object procvars, as they
  154. aren't implicitpointerdefs) }
  155. if not jvmimplicitpointertype(taddrnode(left).left.resultdef) then
  156. CGMessage(parser_e_illegal_expression)
  157. else
  158. begin
  159. { an internal address node will observe "normal" address
  160. operator semantics (= take the actual address!) }
  161. result:=caddrnode.create_internal(taddrnode(left).left);
  162. result:=ctypeconvnode.create_explicit(result,tprocvardef(taddrnode(left).left.resultdef).classdef);
  163. taddrnode(left).left:=nil;
  164. end;
  165. end
  166. else if left.resultdef.typ=procvardef then
  167. begin
  168. if not tprocvardef(left.resultdef).is_addressonly then
  169. begin
  170. { the "code" field from the procvar }
  171. result:=caddrnode.create_internal(left);
  172. result:=ctypeconvnode.create_explicit(result,tprocvardef(left.resultdef).classdef);
  173. { procvarclass.method }
  174. fsym:=search_struct_member(tprocvardef(left.resultdef).classdef,'METHOD');
  175. if not assigned(fsym) or
  176. (fsym.typ<>fieldvarsym) then
  177. internalerror(2011072501);
  178. result:=csubscriptnode.create(fsym,result);
  179. { procvarclass.method.code }
  180. fsym:=search_struct_member(trecorddef(tfieldvarsym(fsym).vardef),'CODE');
  181. if not assigned(fsym) or
  182. (fsym.typ<>fieldvarsym) then
  183. internalerror(2011072502);
  184. result:=csubscriptnode.create(fsym,result);
  185. left:=nil
  186. end
  187. else
  188. { convert contents to plain pointer }
  189. begin
  190. result:=ctypeconvnode.create_explicit(left,java_jlobject);
  191. include(result.flags,nf_load_procvar);
  192. left:=nil;
  193. end;
  194. end
  195. else
  196. internalerror(2011072506);
  197. end
  198. else if (left.resultdef.typ=procdef) then
  199. begin
  200. result:=inherited;
  201. exit;
  202. end
  203. else
  204. begin
  205. if not jvmimplicitpointertype(left.resultdef) then
  206. begin
  207. { allow taking the address of a copy-out parameter (it's an
  208. array reference) or of the first element of an array }
  209. if not isrefparaload and
  210. not isarrayele0load then
  211. begin
  212. CGMessage(parser_e_illegal_expression);
  213. exit
  214. end;
  215. end;
  216. result:=inherited;
  217. end;
  218. end;
  219. procedure tjvmaddrnode.pass_generate_code;
  220. var
  221. implicitptr: boolean;
  222. begin
  223. secondpass(left);
  224. implicitptr:=jvmimplicitpointertype(left.resultdef);
  225. if implicitptr then
  226. { this is basically a typecast: the left node is an implicit
  227. pointer, and we typecast it to a regular 'pointer'
  228. (java.lang.Object) }
  229. location_copy(location,left.location)
  230. else
  231. begin
  232. { these are always arrays (used internally for pointers to var
  233. parameters stored in nestedfpstructs) -> get base pointer to
  234. array }
  235. if (left.location.loc<>LOC_REFERENCE) or
  236. (left.location.reference.arrayreftype<>art_indexconst) or
  237. (left.location.reference.base=NR_NO) or
  238. (left.location.reference.indexoffset<>0) or
  239. assigned(left.location.reference.symbol) then
  240. internalerror(2011060701);
  241. location_reset(location,LOC_REGISTER,OS_ADDR);
  242. location.register:=left.location.reference.base;
  243. end;
  244. end;
  245. {*****************************************************************************
  246. TJVMLOADVMTADDRNODE
  247. *****************************************************************************}
  248. procedure tjvmloadvmtaddrnode.pass_generate_code;
  249. begin
  250. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(
  251. tabstractrecorddef(tclassrefdef(resultdef).pointeddef).jvm_full_typename(true))));
  252. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  253. location_reset(location,LOC_REGISTER,OS_ADDR);
  254. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  255. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  256. end;
  257. {*****************************************************************************
  258. TJVMVECNODE
  259. *****************************************************************************}
  260. function tjvmvecnode.pass_1: tnode;
  261. var
  262. psym: tsym;
  263. stringclass: tdef;
  264. begin
  265. if (left.resultdef.typ=stringdef) then
  266. begin
  267. case tstringdef(left.resultdef).stringtype of
  268. st_ansistring:
  269. stringclass:=java_ansistring;
  270. st_unicodestring,
  271. st_widestring:
  272. stringclass:=java_jlstring;
  273. st_shortstring:
  274. begin
  275. stringclass:=java_shortstring;
  276. left:=caddrnode.create_internal(left);
  277. { avoid useless typecheck when casting to shortstringclass }
  278. include(left.flags,nf_typedaddr);
  279. end
  280. else
  281. internalerror(2011052407);
  282. end;
  283. psym:=search_struct_member(tabstractrecorddef(stringclass),'CHARAT');
  284. if not assigned(psym) or
  285. (psym.typ<>procsym) then
  286. internalerror(2011031501);
  287. { Pascal strings are 1-based, Java strings 0-based }
  288. result:=ccallnode.create(ccallparanode.create(
  289. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  290. psym.owner,ctypeconvnode.create_explicit(left,stringclass),[]);
  291. left:=nil;
  292. right:=nil;
  293. exit;
  294. end
  295. else
  296. begin
  297. { keep indices that are enum constants that way, rather than
  298. transforming them into a load of the class instance that
  299. represents this constant (since we then would have to extract
  300. the int constant value again at run time anyway) }
  301. if right.nodetype=ordconstn then
  302. tjvmordconstnode(right).enumconstok:=true;
  303. result:=inherited;
  304. end;
  305. end;
  306. procedure tjvmvecnode.pass_generate_code;
  307. var
  308. psym: tsym;
  309. newsize: tcgsize;
  310. begin
  311. if left.resultdef.typ=stringdef then
  312. internalerror(2011052702);
  313. { This routine is not used for Strings, as they are a class type and
  314. you have to use charAt() there to load a character (and you cannot
  315. change characters; you have to create a new string in that case)
  316. As far as arrays are concerned: we have to create a trefererence
  317. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  318. pointer to the array (i.e., left.location.register) }
  319. secondpass(left);
  320. newsize:=def_cgsize(resultdef);
  321. if left.location.loc=LOC_CREFERENCE then
  322. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  323. else
  324. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  325. { don't use left.resultdef, because it may be an open or regular array,
  326. and then asking for the size doesn't make any sense }
  327. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  328. location.reference.base:=left.location.register;
  329. secondpass(right);
  330. { simplify index location if necessary, since array references support
  331. an index in memory, but not an another array index }
  332. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  333. (right.location.reference.arrayreftype<>art_none) then
  334. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  335. { replace enum class instance with the corresponding integer value }
  336. if (right.resultdef.typ=enumdef) then
  337. begin
  338. if (right.location.loc<>LOC_CONSTANT) then
  339. begin
  340. psym:=search_struct_member(tenumdef(right.resultdef).classdef,'FPCORDINAL');
  341. if not assigned(psym) or
  342. (psym.typ<>procsym) or
  343. (tprocsym(psym).ProcdefList.count<>1) then
  344. internalerror(2011062607);
  345. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  346. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(tprocsym(psym).procdeflist[0]),tprocdef(tprocsym(psym).procdeflist[0]).mangledname,false);
  347. { call replaces self parameter with longint result -> no stack
  348. height change }
  349. location_reset(right.location,LOC_REGISTER,OS_S32);
  350. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  351. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,s32inttype,right.location.register);
  352. end;
  353. { always force to integer location, because enums are handled as
  354. object instances (since that's what they are in Java) }
  355. right.resultdef:=s32inttype;
  356. right.location.size:=OS_S32;
  357. end;
  358. { adjust index if necessary }
  359. if not is_special_array(left.resultdef) and
  360. (tarraydef(left.resultdef).lowrange<>0) and
  361. (right.location.loc<>LOC_CONSTANT) then
  362. begin
  363. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  364. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  365. if right.location.loc<>LOC_REGISTER then
  366. begin
  367. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  368. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  369. end;
  370. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  371. end;
  372. { create array reference }
  373. case right.location.loc of
  374. LOC_REGISTER,LOC_CREGISTER:
  375. begin
  376. location.reference.arrayreftype:=art_indexreg;
  377. location.reference.index:=right.location.register;
  378. end;
  379. LOC_REFERENCE,LOC_CREFERENCE:
  380. begin
  381. location.reference.arrayreftype:=art_indexref;
  382. location.reference.indexbase:=right.location.reference.base;
  383. location.reference.indexsymbol:=right.location.reference.symbol;
  384. location.reference.indexoffset:=right.location.reference.offset;
  385. end;
  386. LOC_CONSTANT:
  387. begin
  388. location.reference.arrayreftype:=art_indexconst;
  389. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  390. end
  391. else
  392. internalerror(2011012002);
  393. end;
  394. end;
  395. begin
  396. cderefnode:=tjvmderefnode;
  397. caddrnode:=tjvmaddrnode;
  398. cvecnode:=tjvmvecnode;
  399. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  400. end.