njvmutil.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. symtype,symconst,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. class function create_main_procdef(const name: string; potype: tproctypeoption; ps: tprocsym): tdef; override;
  31. class procedure InsertInitFinalTable; override;
  32. class procedure InsertThreadvarTablesTable; override;
  33. class procedure InsertThreadvars; override;
  34. class procedure InsertWideInitsTablesTable; override;
  35. class procedure InsertWideInits; override;
  36. class procedure InsertResourceTablesTable; override;
  37. class procedure InsertResourceInfo(ResourcesUsed : boolean); override;
  38. class procedure InsertMemorySizes; override;
  39. strict protected
  40. class procedure add_main_procdef_paras(pd: tdef); override;
  41. end;
  42. implementation
  43. uses
  44. verbose,globals,constexp,fmodule,
  45. aasmdata,aasmtai,cpubase,aasmcpu,
  46. symdef,symbase,symtable,defutil,jvmdef,
  47. nbas,ncnv,ncon,ninl,ncal,
  48. ppu,
  49. pass_1;
  50. class function tjvmnodeutils.initialize_data_node(p:tnode):tnode;
  51. begin
  52. if not assigned(p.resultdef) then
  53. typecheckpass(p);
  54. if ((p.resultdef.typ=stringdef) and
  55. not is_shortstring(p.resultdef) and
  56. not is_longstring(p.resultdef)) or
  57. is_dynamic_array(p.resultdef) then
  58. begin
  59. { Always initialise with empty string/array rather than nil. Java
  60. makes a distinction between an empty string/array and a null
  61. string/array, but we don't. We therefore have to pick which one we
  62. use to represent empty strings/arrays. I've chosen empty rather than
  63. null structures, because otherwise it becomes impossible to return
  64. an empty string to Java code (it would return null).
  65. On the consumer side, we do interpret both null and empty as the same
  66. thing, so Java code can pass in null strings/arrays and we'll
  67. interpret them correctly.
  68. }
  69. result:=cinlinenode.create(in_setlength_x,false,
  70. ccallparanode.create(genintconstnode(0),
  71. ccallparanode.create(p,nil)));
  72. end
  73. else
  74. { records/arrays/... are automatically initialised }
  75. result:=cnothingnode.create;
  76. end;
  77. class function tjvmnodeutils.finalize_data_node(p:tnode):tnode;
  78. begin
  79. // do nothing
  80. result:=cnothingnode.create;
  81. end;
  82. class function tjvmnodeutils.force_init: boolean;
  83. begin
  84. { we need an initialisation in case the al_globals list is not empty
  85. (that's where the initialisation for global records is added) }
  86. result:=
  87. inherited or
  88. not current_asmdata.asmlists[al_globals].empty;
  89. end;
  90. class procedure tjvmnodeutils.insertbssdata(sym: tstaticvarsym);
  91. begin
  92. { handled while generating the unit/program init code, or class
  93. constructor; add something to al_globals to indicate that we need to
  94. insert an init section though }
  95. if current_asmdata.asmlists[al_globals].empty and
  96. jvmimplicitpointertype(sym.vardef) then
  97. current_asmdata.asmlists[al_globals].concat(cai_align.Create(1));
  98. end;
  99. class function tjvmnodeutils.create_main_procdef(const name: string; potype: tproctypeoption; ps: tprocsym): tdef;
  100. begin
  101. if (potype=potype_proginit) then
  102. begin
  103. result:=inherited create_main_procdef('main', potype, ps);
  104. include(tprocdef(result).procoptions,po_global);
  105. tprocdef(result).visibility:=vis_public;
  106. end
  107. else
  108. result:=inherited create_main_procdef(name, potype, ps);
  109. end;
  110. class procedure tjvmnodeutils.InsertInitFinalTable;
  111. var
  112. hp : tused_unit;
  113. unitinits : TAsmList;
  114. unitclassname: string;
  115. mainpsym: tsym;
  116. mainpd: tprocdef;
  117. begin
  118. unitinits:=TAsmList.Create;
  119. hp:=tused_unit(usedunits.first);
  120. while assigned(hp) do
  121. begin
  122. { class constructors are automatically handled by the JVM }
  123. { call the unit init code and make it external }
  124. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  125. begin
  126. { trigger init code by referencing the class representing the
  127. unit; if necessary, it will register the fini code to run on
  128. exit}
  129. unitclassname:='';
  130. if assigned(hp.u.namespace) then
  131. unitclassname:=hp.u.namespace^+'.';
  132. unitclassname:=unitclassname+hp.u.realmodulename^;
  133. unitinits.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(unitclassname)));
  134. unitinits.concat(taicpu.op_none(a_pop));
  135. end;
  136. hp:=tused_unit(hp.next);
  137. end;
  138. { insert in main program routine }
  139. mainpsym:=tsym(current_module.localsymtable.find(mainaliasname));
  140. if not assigned(mainpsym) or
  141. (mainpsym.typ<>procsym) then
  142. internalerror(2011041901);
  143. mainpd:=tprocsym(mainpsym).find_procdef_bytype(potype_proginit);
  144. if not assigned(mainpd) then
  145. internalerror(2011041902);
  146. mainpd.exprasmlist.insertList(unitinits);
  147. unitinits.free;
  148. end;
  149. class procedure tjvmnodeutils.InsertThreadvarTablesTable;
  150. begin
  151. { not yet supported }
  152. end;
  153. class procedure tjvmnodeutils.InsertThreadvars;
  154. begin
  155. { not yet supported }
  156. end;
  157. class procedure tjvmnodeutils.InsertWideInitsTablesTable;
  158. begin
  159. { not required }
  160. end;
  161. class procedure tjvmnodeutils.InsertWideInits;
  162. begin
  163. { not required }
  164. end;
  165. class procedure tjvmnodeutils.InsertResourceTablesTable;
  166. begin
  167. { not supported }
  168. end;
  169. class procedure tjvmnodeutils.InsertResourceInfo(ResourcesUsed: boolean);
  170. begin
  171. { not supported }
  172. end;
  173. class procedure tjvmnodeutils.InsertMemorySizes;
  174. begin
  175. { not required }
  176. end;
  177. class procedure tjvmnodeutils.add_main_procdef_paras(pd: tdef);
  178. var
  179. pvs: tparavarsym;
  180. begin
  181. if (tprocdef(pd).proctypeoption=potype_proginit) then
  182. begin
  183. { add the args parameter }
  184. pvs:=tparavarsym.create('$args',1,vs_const,search_system_type('TJSTRINGARRAY').typedef,[]);
  185. tprocdef(pd).parast.insert(pvs);
  186. tprocdef(pd).calcparas;
  187. end;
  188. end;
  189. begin
  190. cnodeutils:=tjvmnodeutils;
  191. end.