njvmmem.pas 19 KB

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