njvmld.pas 8.6 KB

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