njvmmem.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. begin
  154. result:=
  155. (left.nodetype=derefn);
  156. end;
  157. function tjvmaddrnode.pass_typecheck: tnode;
  158. var
  159. fsym: tsym;
  160. begin
  161. result:=nil;
  162. typecheckpass(left);
  163. if codegenerror then
  164. exit;
  165. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  166. { in TP/Delphi, @procvar = contents of procvar and @@procvar =
  167. address of procvar. In case of a procedure of object, this works
  168. by letting the first addrnode typecast the procvar into a tmethod
  169. record followed by subscripting its "code" field (= first field),
  170. and if there's a second addrnode then it takes the address of
  171. this code field (which is hence also the address of the procvar).
  172. In Java, such ugly hacks don't work -> replace first addrnode
  173. with getting procvar.method.code, and second addrnode with
  174. the class for procedure of object}
  175. if not(nf_internal in flags) and
  176. ((m_tp_procvar in current_settings.modeswitches) or
  177. (m_mac_procvar in current_settings.modeswitches)) and
  178. (((left.nodetype=addrn) and
  179. (taddrnode(left).left.resultdef.typ=procvardef)) or
  180. (left.resultdef.typ=procvardef)) then
  181. begin
  182. if (left.nodetype=addrn) and
  183. (taddrnode(left).left.resultdef.typ=procvardef) then
  184. begin
  185. { double address -> pointer that is the address of the
  186. procvardef (don't allow for non-object procvars, as they
  187. aren't implicitpointerdefs) }
  188. if not jvmimplicitpointertype(taddrnode(left).left.resultdef) then
  189. CGMessage(parser_e_illegal_expression)
  190. else
  191. begin
  192. { an internal address node will observe "normal" address
  193. operator semantics (= take the actual address!) }
  194. result:=caddrnode.create_internal(taddrnode(left).left);
  195. result:=ctypeconvnode.create_explicit(result,tcpuprocvardef(taddrnode(left).left.resultdef).classdef);
  196. taddrnode(left).left:=nil;
  197. end;
  198. end
  199. else if left.resultdef.typ=procvardef then
  200. begin
  201. if not tprocvardef(left.resultdef).is_addressonly then
  202. begin
  203. { the "code" field from the procvar }
  204. result:=caddrnode.create_internal(left);
  205. result:=ctypeconvnode.create_explicit(result,tcpuprocvardef(left.resultdef).classdef);
  206. { procvarclass.method }
  207. fsym:=search_struct_member(tcpuprocvardef(left.resultdef).classdef,'METHOD');
  208. if not assigned(fsym) or
  209. (fsym.typ<>fieldvarsym) then
  210. internalerror(2011072501);
  211. result:=csubscriptnode.create(fsym,result);
  212. { procvarclass.method.code }
  213. fsym:=search_struct_member(trecorddef(tfieldvarsym(fsym).vardef),'CODE');
  214. if not assigned(fsym) or
  215. (fsym.typ<>fieldvarsym) then
  216. internalerror(2011072502);
  217. result:=csubscriptnode.create(fsym,result);
  218. left:=nil
  219. end
  220. else
  221. { convert contents to plain pointer }
  222. begin
  223. result:=ctypeconvnode.create_explicit(left,java_jlobject);
  224. include(result.flags,nf_load_procvar);
  225. left:=nil;
  226. end;
  227. end
  228. else
  229. internalerror(2011072503);
  230. end
  231. else if (left.resultdef.typ=procdef) then
  232. begin
  233. result:=inherited;
  234. exit;
  235. end
  236. else
  237. begin
  238. if not jvmimplicitpointertype(left.resultdef) then
  239. begin
  240. { allow taking the address of a copy-out parameter (it's an
  241. array reference), of the first element of an array and of a
  242. pointer derefence }
  243. if not isrefparaload and
  244. not isarrayele0load and
  245. not isdererence then
  246. begin
  247. CGMessage(parser_e_illegal_expression);
  248. exit
  249. end;
  250. end;
  251. result:=inherited;
  252. end;
  253. end;
  254. procedure tjvmaddrnode.pass_generate_code;
  255. var
  256. implicitptr: boolean;
  257. begin
  258. secondpass(left);
  259. implicitptr:=jvmimplicitpointertype(left.resultdef);
  260. if implicitptr then
  261. { this is basically a typecast: the left node is an implicit
  262. pointer, and we typecast it to a regular 'pointer'
  263. (java.lang.Object) }
  264. location_copy(location,left.location)
  265. else
  266. begin
  267. { these are always arrays (used internally for pointers to var
  268. parameters stored in nestedfpstructs) -> get base pointer to
  269. array }
  270. if (left.location.loc<>LOC_REFERENCE) or
  271. (left.location.reference.arrayreftype<>art_indexconst) or
  272. (left.location.reference.base=NR_NO) or
  273. (left.location.reference.indexoffset<>0) or
  274. assigned(left.location.reference.symbol) then
  275. internalerror(2011060701);
  276. location_reset(location,LOC_REGISTER,OS_ADDR);
  277. location.register:=left.location.reference.base;
  278. end;
  279. end;
  280. {*****************************************************************************
  281. TJVMLOADVMTADDRNODE
  282. *****************************************************************************}
  283. function tjvmloadvmtaddrnode.pass_1: tnode;
  284. var
  285. vs: tsym;
  286. begin
  287. result:=nil;
  288. if is_javaclass(left.resultdef) and
  289. (left.nodetype<>typen) and
  290. (left.resultdef.typ<>classrefdef) then
  291. begin
  292. { call java.lang.Object.getClass() }
  293. vs:=search_struct_member(tobjectdef(left.resultdef),'GETCLASS');
  294. if not assigned(vs) or
  295. (tsym(vs).typ<>procsym) then
  296. internalerror(2011041903);
  297. result:=ccallnode.create(nil,tprocsym(vs),vs.owner,left,[],nil);
  298. inserttypeconv_explicit(result,resultdef);
  299. { reused }
  300. left:=nil;
  301. end;
  302. end;
  303. procedure tjvmloadvmtaddrnode.pass_generate_code;
  304. begin
  305. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(
  306. tabstractrecorddef(tclassrefdef(resultdef).pointeddef).jvm_full_typename(true),AT_METADATA)));
  307. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  308. location_reset(location,LOC_REGISTER,OS_ADDR);
  309. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  310. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  311. end;
  312. {*****************************************************************************
  313. TJVMVECNODE
  314. *****************************************************************************}
  315. function tjvmvecnode.gen_array_rangecheck: tnode;
  316. begin
  317. { JVM does the range checking for us }
  318. result:=nil;
  319. end;
  320. function tjvmvecnode.pass_1: tnode;
  321. var
  322. psym: tsym;
  323. stringclass: tdef;
  324. begin
  325. if (left.resultdef.typ=stringdef) then
  326. begin
  327. case tstringdef(left.resultdef).stringtype of
  328. st_ansistring:
  329. stringclass:=java_ansistring;
  330. st_unicodestring,
  331. st_widestring:
  332. stringclass:=java_jlstring;
  333. st_shortstring:
  334. begin
  335. stringclass:=java_shortstring;
  336. left:=caddrnode.create_internal(left);
  337. { avoid useless typecheck when casting to shortstringclass }
  338. include(taddrnode(left).addrnodeflags,anf_typedaddr);
  339. end
  340. else
  341. internalerror(2011052407);
  342. end;
  343. psym:=search_struct_member(tabstractrecorddef(stringclass),'CHARAT');
  344. if not assigned(psym) or
  345. (psym.typ<>procsym) then
  346. internalerror(2011031502);
  347. { Pascal strings are 1-based, Java strings 0-based }
  348. result:=ccallnode.create(ccallparanode.create(
  349. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  350. psym.owner,ctypeconvnode.create_explicit(left,stringclass),[],nil);
  351. left:=nil;
  352. right:=nil;
  353. exit;
  354. end
  355. else
  356. begin
  357. { keep indices that are enum constants that way, rather than
  358. transforming them into a load of the class instance that
  359. represents this constant (since we then would have to extract
  360. the int constant value again at run time anyway) }
  361. if right.nodetype=ordconstn then
  362. tjvmordconstnode(right).enumconstok:=true;
  363. result:=inherited;
  364. end;
  365. end;
  366. procedure tjvmvecnode.pass_generate_code;
  367. var
  368. psym: tsym;
  369. newsize: tcgsize;
  370. begin
  371. if left.resultdef.typ=stringdef then
  372. internalerror(2011052702);
  373. { This routine is not used for Strings, as they are a class type and
  374. you have to use charAt() there to load a character (and you cannot
  375. change characters; you have to create a new string in that case)
  376. As far as arrays are concerned: we have to create a trefererence
  377. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  378. pointer to the array (i.e., left.location.register) }
  379. secondpass(left);
  380. newsize:=def_cgsize(resultdef);
  381. if left.location.loc=LOC_CREFERENCE then
  382. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment,left.location.reference.volatility)
  383. else
  384. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment,left.location.reference.volatility);
  385. { don't use left.resultdef, because it may be an open or regular array,
  386. and then asking for the size doesn't make any sense }
  387. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  388. location.reference.base:=left.location.register;
  389. secondpass(right);
  390. if (right.expectloc=LOC_JUMP)<>
  391. (right.location.loc=LOC_JUMP) then
  392. internalerror(2011090501);
  393. { simplify index location if necessary, since array references support
  394. an index in memory, but not an another array index }
  395. if (right.location.loc=LOC_JUMP) or
  396. ((right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  397. (right.location.reference.arrayreftype<>art_none)) then
  398. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  399. { replace enum class instance with the corresponding integer value }
  400. if (right.resultdef.typ=enumdef) then
  401. begin
  402. if (right.location.loc<>LOC_CONSTANT) then
  403. begin
  404. psym:=search_struct_member(tcpuenumdef(tenumdef(right.resultdef).getbasedef).classdef,'FPCORDINAL');
  405. if not assigned(psym) or
  406. (psym.typ<>procsym) or
  407. (tprocsym(psym).ProcdefList.count<>1) then
  408. internalerror(2011062607);
  409. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  410. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(tprocsym(psym).procdeflist[0]),tprocdef(tprocsym(psym).procdeflist[0]).mangledname,[],nil,false);
  411. { call replaces self parameter with longint result -> no stack
  412. height change }
  413. location_reset(right.location,LOC_REGISTER,OS_S32);
  414. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  415. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,s32inttype,right.location.register);
  416. end;
  417. { always force to integer location, because enums are handled as
  418. object instances (since that's what they are in Java) }
  419. right.resultdef:=s32inttype;
  420. right.location.size:=OS_S32;
  421. end
  422. else if (right.location.loc<>LOC_CONSTANT) and
  423. ((right.resultdef.typ<>orddef) or
  424. (torddef(right.resultdef).ordtype<>s32bit)) then
  425. begin
  426. { Java array indices are always 32 bit signed integers }
  427. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,s32inttype,true);
  428. right.resultdef:=s32inttype;
  429. end;
  430. { adjust index if necessary }
  431. if not is_special_array(left.resultdef) and
  432. (tarraydef(left.resultdef).lowrange<>0) and
  433. (right.location.loc<>LOC_CONSTANT) then
  434. begin
  435. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  436. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  437. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  438. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  439. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  440. end;
  441. { create array reference }
  442. case right.location.loc of
  443. LOC_REGISTER,LOC_CREGISTER:
  444. begin
  445. location.reference.arrayreftype:=art_indexreg;
  446. location.reference.index:=right.location.register;
  447. end;
  448. LOC_REFERENCE,LOC_CREFERENCE:
  449. begin
  450. location.reference.arrayreftype:=art_indexref;
  451. location.reference.indexbase:=right.location.reference.base;
  452. location.reference.indexsymbol:=right.location.reference.symbol;
  453. location.reference.indexoffset:=right.location.reference.offset;
  454. end;
  455. LOC_CONSTANT:
  456. begin
  457. location.reference.arrayreftype:=art_indexconst;
  458. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  459. end
  460. else
  461. internalerror(2011012002);
  462. end;
  463. end;
  464. begin
  465. cderefnode:=tjvmderefnode;
  466. csubscriptnode:=tjvmsubscriptnode;
  467. caddrnode:=tjvmaddrnode;
  468. cvecnode:=tjvmvecnode;
  469. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  470. end.