njvmmem.pas 21 KB

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