njvmmem.pas 19 KB

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