njvmld.pas 9.6 KB

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