njvmmem.pas 20 KB

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