njvmld.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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) then
  55. begin
  56. { prevent errors in case of an expression such as
  57. word(str[x]):=1234;
  58. }
  59. inserttypeconv_explicit(right,cwidechartype);
  60. result:=ccallnode.createintern('fpc_unicodestr_setchar',
  61. ccallparanode.create(right,
  62. ccallparanode.create(tvecnode(target).right,
  63. ccallparanode.create(tvecnode(target).left.getcopy,nil))));
  64. result:=cassignmentnode.create(tvecnode(target).left,result);
  65. right:=nil;
  66. tvecnode(target).left:=nil;
  67. tvecnode(target).right:=nil;
  68. exit;
  69. end
  70. else
  71. result:=inherited;
  72. end;
  73. function tjvmloadnode.is_addr_param_load: boolean;
  74. begin
  75. result:=
  76. inherited and
  77. not jvmimplicitpointertype(tparavarsym(symtableentry).vardef);
  78. end;
  79. { tjvmarrayconstructornode }
  80. procedure tjvmarrayconstructornode.makearrayref(var ref: treference; eledef: tdef);
  81. var
  82. basereg: tregister;
  83. begin
  84. { arrays are implicitly dereferenced }
  85. basereg:=hlcg.getaddressregister(current_asmdata.CurrAsmList,java_jlobject);
  86. hlcg.a_load_ref_reg(current_asmdata.CurrAsmList,java_jlobject,java_jlobject,ref,basereg);
  87. reference_reset_base(ref,basereg,0,1);
  88. ref.arrayreftype:=art_indexconst;
  89. ref.indexoffset:=0;
  90. end;
  91. procedure tjvmarrayconstructornode.advancearrayoffset(var ref: treference; elesize: asizeint);
  92. begin
  93. inc(ref.indexoffset);
  94. end;
  95. begin
  96. cloadnode:=tjvmloadnode;
  97. cassignmentnode:=tjvmassignmentnode;
  98. carrayconstructornode:=tjvmarrayconstructornode;
  99. end.