njvmmem.pas 17 KB

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