2
0

njvmld.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. function is_addr_param_load: boolean; override;
  28. procedure pass_generate_code; override;
  29. end;
  30. tjvmassignmentnode = class(tcgassignmentnode)
  31. function pass_1: tnode; override;
  32. end;
  33. tjvmarrayconstructornode = class(tcgarrayconstructornode)
  34. protected
  35. procedure makearrayref(var ref: treference; eledef: tdef); override;
  36. procedure advancearrayoffset(var ref: treference; elesize: asizeint); override;
  37. end;
  38. implementation
  39. uses
  40. verbose,
  41. aasmdata,
  42. nbas,nld,ncal,nmem,ncnv,
  43. symconst,symsym,symdef,symtable,defutil,jvmdef,
  44. paramgr,
  45. cgbase,hlcgobj;
  46. { tjvmassignmentnode }
  47. function tjvmassignmentnode.pass_1: tnode;
  48. var
  49. target: tnode;
  50. psym: tsym;
  51. begin
  52. { intercept writes to string elements, because Java strings are immutable
  53. -> detour via StringBuilder
  54. }
  55. target:=left.actualtargetnode;
  56. if (target.nodetype=vecn) and
  57. (is_wide_or_unicode_string(tvecnode(target).left.resultdef) or
  58. is_ansistring(tvecnode(target).left.resultdef)) then
  59. begin
  60. { prevent errors in case of an expression such as
  61. word(str[x]):=1234;
  62. }
  63. inserttypeconv_explicit(right,cwidechartype);
  64. result:=ccallnode.createintern('fpc_'+tstringdef(tvecnode(target).left.resultdef).stringtypname+'_setchar',
  65. ccallparanode.create(right,
  66. ccallparanode.create(tvecnode(target).right,
  67. ccallparanode.create(tvecnode(target).left.getcopy,nil))));
  68. result:=cassignmentnode.create(tvecnode(target).left,result);
  69. right:=nil;
  70. tvecnode(target).left:=nil;
  71. tvecnode(target).right:=nil;
  72. exit;
  73. end
  74. else if (target.nodetype=vecn) and
  75. is_shortstring(tvecnode(target).left.resultdef) then
  76. begin
  77. { prevent errors in case of an expression such as
  78. byte(str[x]):=12;
  79. }
  80. inserttypeconv_explicit(right,cchartype);
  81. { call ShortstringClass(@shortstring).setChar(index,char) }
  82. tvecnode(target).left:=caddrnode.create_internal(tvecnode(target).left);
  83. inserttypeconv_explicit(tvecnode(target).left,java_shortstring);
  84. psym:=search_struct_member(tabstractrecorddef(java_shortstring),'SETCHAR');
  85. if not assigned(psym) or
  86. (psym.typ<>procsym) then
  87. internalerror(2011052408);
  88. result:=
  89. ccallnode.create(
  90. ccallparanode.create(right,
  91. ccallparanode.create(tvecnode(target).right,nil)),
  92. tprocsym(psym),psym.owner,tvecnode(target).left,[]);
  93. right:=nil;
  94. tvecnode(target).left:=nil;
  95. tvecnode(target).right:=nil;
  96. exit;
  97. end
  98. else
  99. result:=inherited;
  100. end;
  101. function tjvmloadnode.is_addr_param_load: boolean;
  102. begin
  103. result:=
  104. inherited and
  105. not jvmimplicitpointertype(tparavarsym(symtableentry).vardef);
  106. end;
  107. procedure tjvmloadnode.pass_generate_code;
  108. begin
  109. if (symtable.symtabletype=parasymtable) and
  110. (symtableentry.typ=paravarsym) and
  111. paramanager.push_copyout_param(tparavarsym(symtableentry).varspez,resultdef,tprocdef(symtable.defowner).proccalloption) then
  112. begin
  113. { the parameter is passed as an array of one element containing the
  114. parameter value }
  115. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),4);
  116. location.reference.arrayreftype:=art_indexconst;
  117. location.reference.base:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  118. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,tparavarsym(symtableentry).localloc,location.reference.base);
  119. location.reference.indexoffset:=0;
  120. end
  121. else
  122. inherited pass_generate_code;
  123. end;
  124. { tjvmarrayconstructornode }
  125. procedure tjvmarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  126. var
  127. basereg: tregister;
  128. begin
  129. { arrays are implicitly dereferenced }
  130. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  131. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,ref,basereg);
  132. reference_reset_base(ref,basereg,0,1);
  133. ref.arrayreftype:=art_indexconst;
  134. ref.indexoffset:=0;
  135. end;
  136. procedure tjvmarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  137. begin
  138. inc(ref.indexoffset);
  139. end;
  140. begin
  141. cloadnode:=tjvmloadnode;
  142. cassignmentnode:=tjvmassignmentnode;
  143. carrayconstructornode:=tjvmarrayconstructornode;
  144. end.