njvmmem.pas 20 KB

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