njvmld.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. end;
  29. tjvmassignmentnode = class(tcgassignmentnode)
  30. function pass_1: tnode; override;
  31. end;
  32. tjvmarrayconstructornode = class(tcgarrayconstructornode)
  33. protected
  34. procedure makearrayref(var ref: treference; eledef: tdef); override;
  35. procedure advancearrayoffset(var ref: treference; elesize: asizeint); override;
  36. end;
  37. implementation
  38. uses
  39. verbose,
  40. aasmdata,
  41. nbas,nld,ncal,nmem,ncnv,
  42. symsym,symdef,defutil,jvmdef,
  43. cgbase,hlcgobj;
  44. { tjvmassignmentnode }
  45. function tjvmassignmentnode.pass_1: tnode;
  46. var
  47. target: tnode;
  48. begin
  49. { intercept writes to string elements, because Java strings are immutable
  50. -> detour via StringBuilder
  51. }
  52. target:=left.actualtargetnode;
  53. if (target.nodetype=vecn) and
  54. (is_wide_or_unicode_string(tvecnode(target).left.resultdef)
  55. {$ifndef nounsupported}
  56. or is_ansistring(tvecnode(target).left.resultdef)
  57. {$endif}
  58. ) 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
  75. result:=inherited;
  76. end;
  77. function tjvmloadnode.is_addr_param_load: boolean;
  78. begin
  79. result:=
  80. inherited and
  81. not jvmimplicitpointertype(tparavarsym(symtableentry).vardef);
  82. end;
  83. { tjvmarrayconstructornode }
  84. procedure tjvmarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  85. var
  86. basereg: tregister;
  87. begin
  88. { arrays are implicitly dereferenced }
  89. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  90. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,ref,basereg);
  91. reference_reset_base(ref,basereg,0,1);
  92. ref.arrayreftype:=art_indexconst;
  93. ref.indexoffset:=0;
  94. end;
  95. procedure tjvmarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  96. begin
  97. inc(ref.indexoffset);
  98. end;
  99. begin
  100. cloadnode:=tjvmloadnode;
  101. cassignmentnode:=tjvmassignmentnode;
  102. carrayconstructornode:=tjvmarrayconstructornode;
  103. end.