njvmmem.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. function pass_typecheck: tnode; override;
  27. procedure pass_generate_code; override;
  28. end;
  29. tjvmderefnode = class(tcgderefnode)
  30. function pass_typecheck:tnode;override;
  31. procedure pass_generate_code; override;
  32. end;
  33. tjvmloadvmtaddrnode = class(tcgloadvmtaddrnode)
  34. procedure pass_generate_code; override;
  35. end;
  36. tjvmvecnode = class(tcgvecnode)
  37. function pass_1: tnode; override;
  38. procedure pass_generate_code;override;
  39. end;
  40. implementation
  41. uses
  42. systems,globals,
  43. cutils,verbose,constexp,
  44. symconst,symtype,symtable,symsym,symdef,defutil,jvmdef,
  45. htypechk,
  46. nadd,ncal,ncnv,ncon,pass_1,
  47. aasmdata,aasmcpu,pass_2,
  48. cgutils,hlcgobj,hlcgcpu;
  49. {*****************************************************************************
  50. TJVMDEREFNODE
  51. *****************************************************************************}
  52. function tjvmderefnode.pass_typecheck: tnode;
  53. begin
  54. result:=inherited;
  55. if not(left.resultdef.typ=pointerdef) or
  56. ((left.resultdef<>voidpointertype) and
  57. not jvmimplicitpointertype(tpointerdef(left.resultdef).pointeddef)) then
  58. begin
  59. CGMessage(parser_e_illegal_expression);
  60. exit
  61. end;
  62. end;
  63. procedure tjvmderefnode.pass_generate_code;
  64. var
  65. implicitptr: boolean;
  66. begin
  67. secondpass(left);
  68. implicitptr:=jvmimplicitpointertype(tpointerdef(left.resultdef).pointeddef);
  69. if (left.resultdef.typ=pointerdef) and
  70. ((left.resultdef=voidpointertype) or
  71. implicitptr) then
  72. begin
  73. if implicitptr then
  74. { this is basically a typecast: the left node is a regular
  75. 'pointer', and we typecast it to an implicit pointer }
  76. location_copy(location,left.location)
  77. else
  78. begin
  79. { these are always arrays (used internally for pointers to var
  80. parameters stored in nestedfpstructs) }
  81. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  82. location_reset_ref(location,LOC_REFERENCE,OS_ADDR,4);
  83. reference_reset_base(location.reference,left.location.register,0,4);
  84. location.reference.arrayreftype:=art_indexconst;
  85. end;
  86. end
  87. else
  88. internalerror(2011052901);
  89. end;
  90. {*****************************************************************************
  91. TJVMADDRNODE
  92. *****************************************************************************}
  93. function tjvmaddrnode.pass_typecheck: tnode;
  94. begin
  95. result:=nil;
  96. typecheckpass(left);
  97. if codegenerror then
  98. exit;
  99. make_not_regable(left,[ra_addr_regable,ra_addr_taken]);
  100. if (left.resultdef.typ=procdef) or
  101. (
  102. (left.resultdef.typ=procvardef) and
  103. ((m_tp_procvar in current_settings.modeswitches) or
  104. (m_mac_procvar in current_settings.modeswitches))
  105. ) then
  106. begin
  107. result:=inherited;
  108. exit;
  109. end
  110. else
  111. begin
  112. if not(nf_internal in flags) and
  113. not jvmimplicitpointertype(left.resultdef) then
  114. begin
  115. CGMessage(parser_e_illegal_expression);
  116. exit
  117. end;
  118. result:=inherited;
  119. end;
  120. end;
  121. procedure tjvmaddrnode.pass_generate_code;
  122. var
  123. implicitptr: boolean;
  124. begin
  125. secondpass(left);
  126. implicitptr:=jvmimplicitpointertype(left.resultdef);
  127. if implicitptr or
  128. (nf_internal in flags) then
  129. begin
  130. if implicitptr then
  131. { this is basically a typecast: the left node is an implicit
  132. pointer, and we typecast it to a regular 'pointer'
  133. (java.lang.Object) }
  134. location_copy(location,left.location)
  135. else
  136. begin
  137. { these are always arrays (used internally for pointers to var
  138. parameters stored in nestedfpstructs) -> get base pointer to
  139. array }
  140. if (left.location.loc<>LOC_REFERENCE) or
  141. (left.location.reference.arrayreftype<>art_indexconst) or
  142. (left.location.reference.base=NR_NO) or
  143. assigned(left.location.reference.symbol) then
  144. internalerror(2011060701);
  145. location_reset(location,LOC_REGISTER,OS_ADDR);
  146. location.register:=left.location.reference.base;
  147. end;
  148. end
  149. else
  150. begin
  151. { procvar }
  152. {$ifndef nounsupported}
  153. location_reset(location,LOC_REGISTER,OS_ADDR);
  154. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  155. hlcg.a_load_const_reg(current_asmdata.CurrAsmList,java_jlobject,0,location.register);
  156. {$else}
  157. internalerror(2011051601);
  158. {$endif}
  159. end;
  160. end;
  161. {*****************************************************************************
  162. TJVMLOADVMTADDRNODE
  163. *****************************************************************************}
  164. procedure tjvmloadvmtaddrnode.pass_generate_code;
  165. begin
  166. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(
  167. tobjectdef(tclassrefdef(resultdef).pointeddef).jvm_full_typename(true))));
  168. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  169. location_reset(location,LOC_REGISTER,OS_ADDR);
  170. location.register:=hlcg.getaddressregister(current_asmdata.CurrAsmList,resultdef);
  171. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,resultdef,location.register);
  172. end;
  173. {*****************************************************************************
  174. TJVMVECNODE
  175. *****************************************************************************}
  176. function tjvmvecnode.pass_1: tnode;
  177. var
  178. psym: tsym;
  179. stringclass: tdef;
  180. begin
  181. if (left.resultdef.typ=stringdef) then
  182. begin
  183. case tstringdef(left.resultdef).stringtype of
  184. st_ansistring:
  185. stringclass:=java_ansistring;
  186. st_unicodestring,
  187. st_widestring:
  188. stringclass:=java_jlstring;
  189. st_shortstring:
  190. begin
  191. stringclass:=java_shortstring;
  192. left:=caddrnode.create_internal(left);
  193. end
  194. else
  195. internalerror(2011052407);
  196. end;
  197. psym:=search_struct_member(tabstractrecorddef(stringclass),'CHARAT');
  198. if not assigned(psym) or
  199. (psym.typ<>procsym) then
  200. internalerror(2011031501);
  201. { Pascal strings are 1-based, Java strings 0-based }
  202. result:=ccallnode.create(ccallparanode.create(
  203. caddnode.create(subn,right,genintconstnode(1)),nil),tprocsym(psym),
  204. psym.owner,ctypeconvnode.create_explicit(left,stringclass),[]);
  205. left:=nil;
  206. right:=nil;
  207. exit;
  208. end
  209. else
  210. result:=inherited;
  211. end;
  212. procedure tjvmvecnode.pass_generate_code;
  213. var
  214. newsize: tcgsize;
  215. begin
  216. if left.resultdef.typ=stringdef then
  217. internalerror(2011052702);
  218. { This routine is not used for Strings, as they are a class type and
  219. you have to use charAt() there to load a character (and you cannot
  220. change characters; you have to create a new string in that case)
  221. As far as arrays are concerned: we have to create a trefererence
  222. with arrayreftype in [art_indexreg,art_indexref], and ref.base =
  223. pointer to the array (i.e., left.location.register) }
  224. secondpass(left);
  225. newsize:=def_cgsize(resultdef);
  226. if left.location.loc=LOC_CREFERENCE then
  227. location_reset_ref(location,LOC_CREFERENCE,newsize,left.location.reference.alignment)
  228. else
  229. location_reset_ref(location,LOC_REFERENCE,newsize,left.location.reference.alignment);
  230. { don't use left.resultdef, because it may be an open or regular array,
  231. and then asking for the size doesn't make any sense }
  232. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,java_jlobject,java_jlobject,true);
  233. location.reference.base:=left.location.register;
  234. secondpass(right);
  235. { simplify index location if necessary, since array references support
  236. an index in memory, but not an another array index }
  237. if (right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  238. (right.location.reference.arrayreftype<>art_none) then
  239. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  240. { adjust index if necessary }
  241. if not is_special_array(left.resultdef) and
  242. (tarraydef(left.resultdef).lowrange<>0) and
  243. (right.location.loc<>LOC_CONSTANT) then
  244. begin
  245. thlcgjvm(hlcg).a_load_loc_stack(current_asmdata.CurrAsmList,right.resultdef,right.location);
  246. thlcgjvm(hlcg).a_op_const_stack(current_asmdata.CurrAsmList,OP_SUB,right.resultdef,tarraydef(left.resultdef).lowrange);
  247. if right.location.loc<>LOC_REGISTER then
  248. begin
  249. location_reset(right.location,LOC_REGISTER,def_cgsize(right.resultdef));
  250. right.location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,right.resultdef);
  251. end;
  252. thlcgjvm(hlcg).a_load_stack_reg(current_asmdata.CurrAsmList,right.resultdef,right.location.register);
  253. end;
  254. { create array reference }
  255. case right.location.loc of
  256. LOC_REGISTER,LOC_CREGISTER:
  257. begin
  258. location.reference.arrayreftype:=art_indexreg;
  259. location.reference.index:=right.location.register;
  260. end;
  261. LOC_REFERENCE,LOC_CREFERENCE:
  262. begin
  263. location.reference.arrayreftype:=art_indexref;
  264. location.reference.indexbase:=right.location.reference.base;
  265. location.reference.indexsymbol:=right.location.reference.symbol;
  266. location.reference.indexoffset:=right.location.reference.offset;
  267. end;
  268. LOC_CONSTANT:
  269. begin
  270. location.reference.arrayreftype:=art_indexconst;
  271. location.reference.indexoffset:=right.location.value-tarraydef(left.resultdef).lowrange;
  272. end
  273. else
  274. internalerror(2011012002);
  275. end;
  276. end;
  277. begin
  278. cderefnode:=tjvmderefnode;
  279. caddrnode:=tjvmaddrnode;
  280. cvecnode:=tjvmvecnode;
  281. cloadvmtaddrnode:=tjvmloadvmtaddrnode;
  282. end.