njvmld.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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;
  25. type
  26. tjvmloadnode = class(tcgloadnode)
  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,defutil,jvmdef,
  44. paramgr,
  45. cgbase,hlcgobj;
  46. { tjvmassignmentnode }
  47. function tjvmassignmentnode.pass_1: tnode;
  48. var
  49. target: tnode;
  50. begin
  51. { intercept writes to string elements, because Java strings are immutable
  52. -> detour via StringBuilder
  53. }
  54. target:=left.actualtargetnode;
  55. if (target.nodetype=vecn) and
  56. (is_wide_or_unicode_string(tvecnode(target).left.resultdef)
  57. {$ifndef nounsupported}
  58. or is_ansistring(tvecnode(target).left.resultdef)
  59. {$endif}
  60. ) then
  61. begin
  62. { prevent errors in case of an expression such as
  63. word(str[x]):=1234;
  64. }
  65. inserttypeconv_explicit(right,cwidechartype);
  66. result:=ccallnode.createintern('fpc_'+tstringdef(tvecnode(target).left.resultdef).stringtypname+'_setchar',
  67. ccallparanode.create(right,
  68. ccallparanode.create(tvecnode(target).right,
  69. ccallparanode.create(tvecnode(target).left.getcopy,nil))));
  70. result:=cassignmentnode.create(tvecnode(target).left,result);
  71. right:=nil;
  72. tvecnode(target).left:=nil;
  73. tvecnode(target).right:=nil;
  74. exit;
  75. end
  76. else
  77. result:=inherited;
  78. end;
  79. function tjvmloadnode.is_addr_param_load: boolean;
  80. begin
  81. result:=
  82. inherited and
  83. not jvmimplicitpointertype(tparavarsym(symtableentry).vardef);
  84. end;
  85. procedure tjvmloadnode.pass_generate_code;
  86. begin
  87. if (symtable.symtabletype=parasymtable) and
  88. (symtableentry.typ=paravarsym) and
  89. paramanager.push_copyout_param(tparavarsym(symtableentry).varspez,resultdef,tprocdef(symtable.defowner).proccalloption) then
  90. begin
  91. { the parameter is passed as an array of one element containing the
  92. parameter value }
  93. location_reset_ref(location,LOC_REFERENCE,def_cgsize(resultdef),4);
  94. location.reference.arrayreftype:=art_indexconst;
  95. location.reference.base:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  96. hlcg.a_load_loc_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,tparavarsym(symtableentry).localloc,location.reference.base);
  97. location.reference.indexoffset:=0;
  98. end
  99. else
  100. inherited pass_generate_code;
  101. end;
  102. { tjvmarrayconstructornode }
  103. procedure tjvmarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  104. var
  105. basereg: tregister;
  106. begin
  107. { arrays are implicitly dereferenced }
  108. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  109. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,ref,basereg);
  110. reference_reset_base(ref,basereg,0,1);
  111. ref.arrayreftype:=art_indexconst;
  112. ref.indexoffset:=0;
  113. end;
  114. procedure tjvmarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  115. begin
  116. inc(ref.indexoffset);
  117. end;
  118. begin
  119. cloadnode:=tjvmloadnode;
  120. cassignmentnode:=tjvmassignmentnode;
  121. carrayconstructornode:=tjvmarrayconstructornode;
  122. end.