njvmld.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. Generate JVM assembler for nodes that handle loads and assignments
  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 njvmld;
  18. {$I fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. symtype,
  23. cgutils,
  24. node, ncgld, ncgnstld;
  25. type
  26. tjvmloadnode = class(tcgnestloadnode)
  27. protected
  28. function is_copyout_addr_param_load: boolean;
  29. function handle_threadvar_access: tnode; override;
  30. function keep_param_address_in_nested_struct: boolean; override;
  31. public
  32. function is_addr_param_load: boolean; override;
  33. procedure pass_generate_code; override;
  34. end;
  35. tjvmassignmentnode = class(tcgassignmentnode)
  36. protected
  37. function direct_shortstring_assignment: boolean; override;
  38. public
  39. function pass_1: tnode; override;
  40. end;
  41. tjvmarrayconstructornode = class(tcgarrayconstructornode)
  42. protected
  43. procedure makearrayref(var ref: treference; eledef: tdef); override;
  44. procedure advancearrayoffset(var ref: treference; elesize: asizeint); override;
  45. procedure wrapmanagedvarrec(var n: tnode);override;
  46. end;
  47. implementation
  48. uses
  49. verbose,
  50. aasmdata,
  51. nbas,nld,ncal,ncon,ninl,nmem,ncnv,
  52. symconst,symsym,symdef,symtable,defutil,jvmdef,
  53. paramgr,
  54. pass_1,
  55. cgbase,hlcgobj;
  56. { tjvmassignmentnode }
  57. function tjvmassignmentnode.direct_shortstring_assignment: boolean;
  58. begin
  59. if maybe_find_real_class_definition(right.resultdef,false)=java_jlstring then
  60. inserttypeconv_explicit(right,cunicodestringtype);
  61. result:=right.resultdef.typ=stringdef;
  62. end;
  63. function tjvmassignmentnode.pass_1: tnode;
  64. var
  65. block: tblocknode;
  66. tempn: ttempcreatenode;
  67. stat: tstatementnode;
  68. target: tnode;
  69. psym: tsym;
  70. begin
  71. { intercept writes to string elements, because Java strings are immutable
  72. -> detour via StringBuilder
  73. }
  74. target:=left.actualtargetnode;
  75. if (target.nodetype=vecn) and
  76. (is_wide_or_unicode_string(tvecnode(target).left.resultdef) or
  77. is_ansistring(tvecnode(target).left.resultdef)) then
  78. begin
  79. { prevent errors in case of an expression such as
  80. word(str[x]):=1234;
  81. }
  82. inserttypeconv_explicit(right,cwidechartype);
  83. result:=ccallnode.createintern('fpc_'+tstringdef(tvecnode(target).left.resultdef).stringtypname+'_setchar',
  84. ccallparanode.create(right,
  85. ccallparanode.create(tvecnode(target).right,
  86. ccallparanode.create(tvecnode(target).left.getcopy,nil))));
  87. result:=cassignmentnode.create(tvecnode(target).left,result);
  88. right:=nil;
  89. tvecnode(target).left:=nil;
  90. tvecnode(target).right:=nil;
  91. exit;
  92. end
  93. else if (target.nodetype=vecn) and
  94. is_shortstring(tvecnode(target).left.resultdef) then
  95. begin
  96. { prevent errors in case of an expression such as
  97. byte(str[x]):=12;
  98. }
  99. inserttypeconv_explicit(right,cansichartype);
  100. { call ShortstringClass(@shortstring).setChar(index,char) }
  101. tvecnode(target).left:=caddrnode.create_internal(tvecnode(target).left);
  102. { avoid useless typecheck when casting to shortstringclass }
  103. include(tvecnode(target).left.flags,nf_typedaddr);
  104. inserttypeconv_explicit(tvecnode(target).left,java_shortstring);
  105. psym:=search_struct_member(tabstractrecorddef(java_shortstring),'SETCHAR');
  106. if not assigned(psym) or
  107. (psym.typ<>procsym) then
  108. internalerror(2011052408);
  109. result:=
  110. ccallnode.create(
  111. ccallparanode.create(right,
  112. ccallparanode.create(tvecnode(target).right,nil)),
  113. tprocsym(psym),psym.owner,tvecnode(target).left,[]);
  114. right:=nil;
  115. tvecnode(target).left:=nil;
  116. tvecnode(target).right:=nil;
  117. exit;
  118. end
  119. else if target.resultdef.typ=formaldef then
  120. begin
  121. if right.resultdef.typ in [orddef,floatdef] then
  122. right:=cinlinenode.create(in_box_x,false,right)
  123. else if jvmimplicitpointertype(right.resultdef) then
  124. begin
  125. { we have to assign the address of a deep copy of the type to the
  126. object in the formalpara -> create a temp, assign the value to
  127. the temp, then assign the address in the temp to the para }
  128. block:=internalstatements(stat);
  129. tempn:=ctempcreatenode.create_value(right.resultdef,right.resultdef.size,
  130. tt_persistent,false,right);
  131. addstatement(stat,tempn);
  132. right:=caddrnode.create(ctemprefnode.create(tempn));
  133. inserttypeconv_explicit(right,java_jlobject);
  134. addstatement(stat,ctempdeletenode.create_normal_temp(tempn));
  135. addstatement(stat,ctypeconvnode.create_explicit(
  136. caddrnode.create(ctemprefnode.create(tempn)),java_jlobject));
  137. right:=block;
  138. end;
  139. typecheckpass(right);
  140. result:=inherited;
  141. exit;
  142. end
  143. else
  144. result:=inherited;
  145. end;
  146. function tjvmloadnode.is_copyout_addr_param_load: boolean;
  147. begin
  148. result:=
  149. { passed via array of one element }
  150. ((symtable.symtabletype=parasymtable) and
  151. (symtableentry.typ=paravarsym) and
  152. paramanager.push_copyout_param(tparavarsym(symtableentry).varspez,resultdef,tprocdef(symtable.defowner).proccalloption));
  153. end;
  154. function tjvmloadnode.handle_threadvar_access: tnode;
  155. var
  156. vs: tsym;
  157. begin
  158. { get the variable wrapping the threadvar }
  159. vs:=tsym(symtable.find(symtableentry.name+'$THREADVAR'));
  160. if not assigned(vs) or
  161. (vs.typ<>staticvarsym) then
  162. internalerror(2011082201);
  163. { get a read/write reference to the threadvar value }
  164. result:=cloadnode.create(vs,vs.owner);
  165. typecheckpass(result);
  166. result:=ccallnode.createinternmethod(result,'GETREADWRITEREFERENCE',nil);
  167. result:=ctypeconvnode.create_explicit(result,getpointerdef(resultdef));
  168. result:=cderefnode.create(result);
  169. end;
  170. function tjvmloadnode.keep_param_address_in_nested_struct: boolean;
  171. begin
  172. { we don't need an extra load when implicit pointer types are passed as
  173. var/out/constref parameter (since they are already pointers). However,
  174. when transfering them into a nestedfp struct, we do want to transfer the
  175. pointer and not make a deep copy in case they are var/out/constref (since
  176. changes made to the var/out parameter should propagate up) }
  177. result:=
  178. is_addr_param_load or
  179. ((symtableentry.typ=paravarsym) and
  180. jvmimplicitpointertype(tparavarsym(symtableentry).vardef) and
  181. (tparavarsym(symtableentry).varspez in [vs_var,vs_constref,vs_out]));
  182. end;
  183. function tjvmloadnode.is_addr_param_load: boolean;
  184. begin
  185. result:=
  186. (inherited is_addr_param_load and
  187. not jvmimplicitpointertype(tparavarsym(symtableentry).vardef) and
  188. (tparavarsym(symtableentry).vardef.typ<>formaldef)) or
  189. is_copyout_addr_param_load;
  190. end;
  191. procedure tjvmloadnode.pass_generate_code;
  192. begin
  193. if is_copyout_addr_param_load then
  194. begin
  195. { in case of nested access, load address of field in nestedfpstruct }
  196. if assigned(left) then
  197. generate_nested_access(tabstractnormalvarsym(symtableentry));
  198. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),4);
  199. location.reference.arrayreftype:=art_indexconst;
  200. location.reference.base:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  201. location.reference.indexoffset:=0;
  202. { load the field from the nestedfpstruct, or the parameter location.
  203. In both cases, the result is an array of one element containing the
  204. parameter value }
  205. if assigned(left) then
  206. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,left.location,location.reference.base)
  207. else
  208. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,tparavarsym(symtableentry).localloc,location.reference.base);
  209. end
  210. else if symtableentry.typ=procsym then
  211. { handled in tjvmcnvnode.first_proc_to_procvar }
  212. internalerror(2011072408)
  213. else
  214. inherited pass_generate_code;
  215. end;
  216. { tjvmarrayconstructornode }
  217. procedure tjvmarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  218. var
  219. basereg: tregister;
  220. begin
  221. { arrays are implicitly dereferenced }
  222. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  223. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,ref,basereg);
  224. reference_reset_base(ref,basereg,0,1);
  225. ref.arrayreftype:=art_indexconst;
  226. ref.indexoffset:=0;
  227. end;
  228. procedure tjvmarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  229. begin
  230. inc(ref.indexoffset);
  231. end;
  232. procedure tjvmarrayconstructornode.wrapmanagedvarrec(var n: tnode);
  233. var
  234. varrecdef: trecorddef;
  235. block: tblocknode;
  236. stat: tstatementnode;
  237. temp: ttempcreatenode;
  238. begin
  239. varrecdef:=trecorddef(search_system_type('TVARREC').typedef);
  240. block:=internalstatements(stat);
  241. temp:=ctempcreatenode.create(varrecdef,varrecdef.size,tt_persistent,false);
  242. addstatement(stat,temp);
  243. addstatement(stat,
  244. ccallnode.createinternmethod(
  245. ctemprefnode.create(temp),'INIT',ccallparanode.create(n,nil)));
  246. { note: this will not free the record contents, but just let its reference
  247. on the stack be reused -- which is ok, because the reference will be
  248. stored into the open array parameter }
  249. addstatement(stat,ctempdeletenode.create_normal_temp(temp));
  250. addstatement(stat,ctemprefnode.create(temp));
  251. n:=block;
  252. firstpass(n);
  253. end;
  254. begin
  255. cloadnode:=tjvmloadnode;
  256. cassignmentnode:=tjvmassignmentnode;
  257. carrayconstructornode:=tjvmarrayconstructornode;
  258. end.