njvmmem.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. function pass_1: tnode; override;
  48. procedure pass_generate_code;override;
  49. end;
  50. implementation
  51. uses
  52. systems,globals,procinfo,
  53. cutils,verbose,constexp,
  54. aasmbase,
  55. symconst,symtype,symtable,symsym,symdef,symcpu,defutil,jvmdef,
  56. htypechk,paramgr,
  57. nadd,ncal,ncnv,ncon,nld,nutils,
  58. pass_1,njvmcon,
  59. aasmdata,aasmcpu,pass_2,
  60. cgutils,hlcgobj,hlcgcpu;
  61. {*****************************************************************************
  62. TJVMDEREFNODE
  63. *****************************************************************************}
  64. function tjvmderefnode.pass_typecheck: tnode;
  65. begin
  66. result:=inherited pass_typecheck;
  67. if assigned(result) then
  68. exit;
  69. { don't allow dereferencing untyped pointers, because how this has to
  70. be done depends on whether it's a pointer to an implicit pointer type
  71. or not }
  72. if is_voidpointer(left.resultdef) then
  73. CGMessage(parser_e_illegal_expression);
  74. end;
  75. procedure tjvmderefnode.pass_generate_code;
  76. var
  77. implicitptr: boolean;
  78. begin
  79. secondpass(left);
  80. implicitptr:=jvmimplicitpointertype(resultdef);
  81. if implicitptr then
  82. begin
  83. { this is basically a typecast: the left node is a regular
  84. 'pointer', and we typecast it to an implicit pointer }
  85. location_copy(location,left.location);
  86. { these implicit pointer types (records, sets, shortstrings, ...)
  87. cannot be located in registers on native targets (since
  88. they're not pointers there) -> force into memory to avoid
  89. confusing the compiler; this can happen when typecasting a
  90. Java class type into a pshortstring and then dereferencing etc
  91. }
  92. if location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  93. hlcg.location_force_mem(current_asmdata.CurrAsmList,location,left.resultdef);
  94. end
  95. else
  96. begin
  97. { these are always arrays (used internally for pointers to var
  98. parameters stored in nestedfpstructs, and by programmers for any
  99. kind of pointers) }
  100. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  101. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),4);
  102. reference_reset_base(location.reference,left.location.register,0,4);
  103. location.reference.arrayreftype:=art_indexconst;
  104. if (left.nodetype<>addrn) and
  105. not(resultdef.typ in [orddef,floatdef]) and
  106. not is_voidpointer(resultdef) and
  107. ((resultdef.typ<>objectdef) or
  108. (find_real_class_definition(tobjectdef(resultdef),false)<>java_jlobject)) then
  109. location.reference.checkcast:=true;
  110. end
  111. end;
  112. {*****************************************************************************
  113. TJVMSUBSCRIPTNODE
  114. *****************************************************************************}
  115. function tjvmsubscriptnode.handle_platform_subscript: boolean;
  116. begin
  117. result:=false;
  118. if is_java_class_or_interface(left.resultdef) or
  119. (left.resultdef.typ=recorddef) then
  120. begin
  121. if (location.loc<>LOC_REFERENCE) or
  122. (location.reference.index<>NR_NO) or
  123. assigned(location.reference.symbol) then
  124. internalerror(2011011301);
  125. location.reference.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  126. result:=true;
  127. end
  128. end;
  129. {*****************************************************************************
  130. TJVMADDRNODE
  131. *****************************************************************************}
  132. function tjvmaddrnode.isrefparaload: boolean;
  133. begin
  134. result:=
  135. (left.nodetype=loadn) and
  136. (tloadnode(left).symtableentry.typ=paravarsym) and
  137. paramanager.push_copyout_param(tparavarsym(tloadnode(left).symtableentry).varspez,
  138. left.resultdef,
  139. tabstractprocdef(tloadnode(left).symtableentry.owner.defowner).proccalloption);
  140. end;
  141. function tjvmaddrnode.isarrayele0load: boolean;
  142. begin
  143. result:=
  144. (left.nodetype=vecn) and
  145. (tvecnode(left).left.resultdef.typ=arraydef) and
  146. (tvecnode(left).right.nodetype=ordconstn) and
  147. (tordconstnode(tvecnode(left).right).value=tarraydef(tvecnode(left).left.resultdef).lowrange);
  148. end;
  149. function tjvmaddrnode.isdererence: boolean;
  150. var
  151. target: tnode;
  152. begin
  153. target:=actualtargetnode(@left)^;
  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(2011072506);
  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(2011041901);
  297. result:=ccallnode.create(nil,tprocsym(vs),vs.owner,left,[]);
  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))));
  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.pass_1: tnode;
  316. var
  317. psym: tsym;
  318. stringclass: tdef;
  319. begin
  320. if (left.resultdef.typ=stringdef) then
  321. begin
  322. case tstringdef(left.resultdef).stringtype of
  323. st_ansistring:
  324. stringclass:=java_ansistring;
  325. st_unicodestring,
  326. st_widestring:
  327. stringclass:=java_jlstring;
  328. st_shortstring:
  329. begin
  330. stringclass:=java_shortstring;
  331. left:=caddrnode.create_internal(left);
  332. { avoid useless typecheck when casting to shortstringclass }
  333. include(left.flags,nf_typedaddr);
  334. end
  335. else
  336. internalerror(2011052407);
  337. end;
  338. psym:=search_struct_member(tabstractrecorddef(stringclass),'CHARAT');
  339. if not assigned(psym) or
  340. (psym.typ<>procsym) then
  341. internalerror(2011031501);
  342. { Pascal strings are 1-based, Java strings 0-based }
  343. result:=ccallnode.create(ccallparanode.create(
  344. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  345. psym.owner,ctypeconvnode.create_explicit(left,stringclass),[]);
  346. left:=nil;
  347. right:=nil;
  348. exit;
  349. end
  350. else
  351. begin
  352. { keep indices that are enum constants that way, rather than
  353. transforming them into a load of the class instance that
  354. represents this constant (since we then would have to extract
  355. the int constant value again at run time anyway) }
  356. if right.nodetype=ordconstn then
  357. tjvmordconstnode(right).enumconstok:=true;
  358. result:=inherited;
  359. end;
  360. end;
  361. procedure tjvmvecnode.pass_generate_code;
  362. var
  363. psym: tsym;
  364. newsize: tcgsize;
  365. begin
  366. if left.resultdef.typ=stringdef then
  367. internalerror(2011052702);
  368. { This routine is not used for Strings, as they are a class type and
  369. you have to use charAt() there to load a character (and you cannot
  370. change characters; you have to create a new string in that case)
  371. As far as arrays are concerned: we have to create a trefererence
  372. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  373. pointer to the array (i.e., left.location.register) }
  374. secondpass(left);
  375. newsize:=def_cgsize(resultdef);
  376. if left.location.loc=LOC_CREFERENCE then
  377. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  378. else
  379. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  380. { don't use left.resultdef, because it may be an open or regular array,
  381. and then asking for the size doesn't make any sense }
  382. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  383. location.reference.base:=left.location.register;
  384. secondpass(right);
  385. if (right.expectloc=LOC_JUMP)<>
  386. (right.location.loc=LOC_JUMP) then
  387. internalerror(2011090501);
  388. { simplify index location if necessary, since array references support
  389. an index in memory, but not an another array index }
  390. if (right.location.loc=LOC_JUMP) or
  391. ((right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  392. (right.location.reference.arrayreftype<>art_none)) then
  393. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  394. { replace enum class instance with the corresponding integer value }
  395. if (right.resultdef.typ=enumdef) then
  396. begin
  397. if (right.location.loc<>LOC_CONSTANT) then
  398. begin
  399. psym:=search_struct_member(tcpuenumdef(tenumdef(right.resultdef).getbasedef).classdef,'FPCORDINAL');
  400. if not assigned(psym) or
  401. (psym.typ<>procsym) or
  402. (tprocsym(psym).ProcdefList.count<>1) then
  403. internalerror(2011062607);
  404. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  405. hlcg.a_call_name(current_asmdata.CurrAsmList,tprocdef(tprocsym(psym).procdeflist[0]),tprocdef(tprocsym(psym).procdeflist[0]).mangledname,[],nil,false);
  406. { call replaces self parameter with longint result -> no stack
  407. height change }
  408. location_reset(right.location,LOC_REGISTER,OS_S32);
  409. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,s32inttype);
  410. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,s32inttype,right.location.register);
  411. end;
  412. { always force to integer location, because enums are handled as
  413. object instances (since that's what they are in Java) }
  414. right.resultdef:=s32inttype;
  415. right.location.size:=OS_S32;
  416. end
  417. else if (right.location.loc<>LOC_CONSTANT) and
  418. ((right.resultdef.typ<>orddef) or
  419. (torddef(right.resultdef).ordtype<>s32bit)) then
  420. begin
  421. { Java array indices are always 32 bit signed integers }
  422. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,s32inttype,true);
  423. right.resultdef:=s32inttype;
  424. end;
  425. { adjust index if necessary }
  426. if not is_special_array(left.resultdef) and
  427. (tarraydef(left.resultdef).lowrange<>0) and
  428. (right.location.loc<>LOC_CONSTANT) then
  429. begin
  430. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  431. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  432. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  433. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  434. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  435. end;
  436. { create array reference }
  437. case right.location.loc of
  438. LOC_REGISTER,LOC_CREGISTER:
  439. begin
  440. location.reference.arrayreftype:=art_indexreg;
  441. location.reference.index:=right.location.register;
  442. end;
  443. LOC_REFERENCE,LOC_CREFERENCE:
  444. begin
  445. location.reference.arrayreftype:=art_indexref;
  446. location.reference.indexbase:=right.location.reference.base;
  447. location.reference.indexsymbol:=right.location.reference.symbol;
  448. location.reference.indexoffset:=right.location.reference.offset;
  449. end;
  450. LOC_CONSTANT:
  451. begin
  452. location.reference.arrayreftype:=art_indexconst;
  453. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  454. end
  455. else
  456. internalerror(2011012002);
  457. end;
  458. end;
  459. begin
  460. cderefnode:=tjvmderefnode;
  461. csubscriptnode:=tjvmsubscriptnode;
  462. caddrnode:=tjvmaddrnode;
  463. cvecnode:=tjvmvecnode;
  464. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  465. end.