njvmutil.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {
  2. Copyright (c) 20011 by Jonas Maebe
  3. JVM version of some node tree helper routines
  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 njvmutil;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,
  22. ngenutil,
  23. symsym;
  24. type
  25. tjvmnodeutils = class(tnodeutils)
  26. class function initialize_data_node(p:tnode):tnode; override;
  27. class function finalize_data_node(p:tnode):tnode; override;
  28. class function force_init: boolean; override;
  29. class procedure insertbssdata(sym: tstaticvarsym); override;
  30. end;
  31. implementation
  32. uses
  33. verbose,constexp,
  34. aasmdata,aasmtai,
  35. symconst,symtype,symdef,symbase,symtable,defutil,jvmdef,
  36. nbas,ncnv,ncon,ninl,ncal,
  37. pass_1;
  38. class function tjvmnodeutils.initialize_data_node(p:tnode):tnode;
  39. begin
  40. if not assigned(p.resultdef) then
  41. typecheckpass(p);
  42. if ((p.resultdef.typ=stringdef) and
  43. not is_shortstring(p.resultdef) and
  44. not is_longstring(p.resultdef)) or
  45. is_dynamic_array(p.resultdef) then
  46. begin
  47. { Always initialise with empty string/array rather than nil. Java
  48. makes a distinction between an empty string/array and a null
  49. string/array, but we don't. We therefore have to pick which one we
  50. use to represent empty strings/arrays. I've chosen empty rather than
  51. null structures, because otherwise it becomes impossible to return
  52. an empty string to Java code (it would return null).
  53. On the consumer side, we do interpret both null and empty as the same
  54. thing, so Java code can pass in null strings/arrays and we'll
  55. interpret them correctly.
  56. }
  57. result:=cinlinenode.create(in_setlength_x,false,
  58. ccallparanode.create(genintconstnode(0),
  59. ccallparanode.create(p,nil)));
  60. end
  61. else
  62. { records/arrays/... are automatically initialised }
  63. result:=cnothingnode.create;
  64. end;
  65. class function tjvmnodeutils.finalize_data_node(p:tnode):tnode;
  66. begin
  67. // do nothing
  68. result:=cnothingnode.create;
  69. end;
  70. class function tjvmnodeutils.force_init: boolean;
  71. begin
  72. { we need an initialisation in case the al_globals list is not empty
  73. (that's where the initialisation for global records is added) }
  74. result:=not current_asmdata.asmlists[al_globals].empty;
  75. end;
  76. class procedure tjvmnodeutils.insertbssdata(sym: tstaticvarsym);
  77. begin
  78. { handled while generating the unit/program init code, or class
  79. constructor; add something to al_globals to indicate that we need to
  80. insert an init section though }
  81. if current_asmdata.asmlists[al_globals].empty and
  82. jvmimplicitpointertype(sym.vardef) then
  83. current_asmdata.asmlists[al_globals].concat(cai_align.Create(1));
  84. end;
  85. begin
  86. cnodeutils:=tjvmnodeutils;
  87. end.