njvmld.pas 9.2 KB

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