njvmutil.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. begin
  75. p.free;
  76. { records/arrays/... are automatically initialised }
  77. result:=cnothingnode.create;
  78. end;
  79. end;
  80. class function tjvmnodeutils.finalize_data_node(p:tnode):tnode;
  81. begin
  82. // do nothing
  83. p.free;
  84. result:=cnothingnode.create;
  85. end;
  86. class function tjvmnodeutils.force_init: boolean;
  87. begin
  88. { we need an initialisation in case the al_globals list is not empty
  89. (that's where the initialisation for global records is added) }
  90. result:=
  91. inherited or
  92. not current_asmdata.asmlists[al_globals].empty;
  93. end;
  94. class procedure tjvmnodeutils.insertbssdata(sym: tstaticvarsym);
  95. begin
  96. { handled while generating the unit/program init code, or class
  97. constructor; add something to al_globals to indicate that we need to
  98. insert an init section though }
  99. if current_asmdata.asmlists[al_globals].empty and
  100. jvmimplicitpointertype(sym.vardef) then
  101. current_asmdata.asmlists[al_globals].concat(cai_align.Create(1));
  102. end;
  103. class function tjvmnodeutils.create_main_procdef(const name: string; potype: tproctypeoption; ps: tprocsym): tdef;
  104. begin
  105. if (potype=potype_proginit) then
  106. begin
  107. result:=inherited create_main_procdef('main', potype, ps);
  108. include(tprocdef(result).procoptions,po_global);
  109. tprocdef(result).visibility:=vis_public;
  110. end
  111. else
  112. result:=inherited create_main_procdef(name, potype, ps);
  113. end;
  114. class procedure tjvmnodeutils.InsertInitFinalTable;
  115. var
  116. hp : tused_unit;
  117. unitinits : TAsmList;
  118. unitclassname: string;
  119. mainpsym: tsym;
  120. mainpd: tprocdef;
  121. begin
  122. unitinits:=TAsmList.Create;
  123. hp:=tused_unit(usedunits.first);
  124. while assigned(hp) do
  125. begin
  126. { class constructors are automatically handled by the JVM }
  127. { call the unit init code and make it external }
  128. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  129. begin
  130. { trigger init code by referencing the class representing the
  131. unit; if necessary, it will register the fini code to run on
  132. exit}
  133. unitclassname:='';
  134. if assigned(hp.u.namespace) then
  135. unitclassname:=hp.u.namespace^+'.';
  136. unitclassname:=unitclassname+hp.u.realmodulename^;
  137. unitinits.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(unitclassname)));
  138. unitinits.concat(taicpu.op_none(a_pop));
  139. end;
  140. hp:=tused_unit(hp.next);
  141. end;
  142. { insert in main program routine }
  143. mainpsym:=tsym(current_module.localsymtable.find(mainaliasname));
  144. if not assigned(mainpsym) or
  145. (mainpsym.typ<>procsym) then
  146. internalerror(2011041901);
  147. mainpd:=tprocsym(mainpsym).find_procdef_bytype(potype_proginit);
  148. if not assigned(mainpd) then
  149. internalerror(2011041902);
  150. mainpd.exprasmlist.insertList(unitinits);
  151. unitinits.free;
  152. end;
  153. class procedure tjvmnodeutils.InsertThreadvarTablesTable;
  154. begin
  155. { not yet supported }
  156. end;
  157. class procedure tjvmnodeutils.InsertThreadvars;
  158. begin
  159. { not yet supported }
  160. end;
  161. class procedure tjvmnodeutils.InsertWideInitsTablesTable;
  162. begin
  163. { not required }
  164. end;
  165. class procedure tjvmnodeutils.InsertWideInits;
  166. begin
  167. { not required }
  168. end;
  169. class procedure tjvmnodeutils.InsertResourceTablesTable;
  170. begin
  171. { not supported }
  172. end;
  173. class procedure tjvmnodeutils.InsertResourceInfo(ResourcesUsed: boolean);
  174. begin
  175. { not supported }
  176. end;
  177. class procedure tjvmnodeutils.InsertMemorySizes;
  178. begin
  179. { not required }
  180. end;
  181. class procedure tjvmnodeutils.add_main_procdef_paras(pd: tdef);
  182. var
  183. pvs: tparavarsym;
  184. begin
  185. if (tprocdef(pd).proctypeoption=potype_proginit) then
  186. begin
  187. { add the args parameter }
  188. pvs:=tparavarsym.create('$args',1,vs_const,search_system_type('TJSTRINGARRAY').typedef,[]);
  189. tprocdef(pd).parast.insert(pvs);
  190. tprocdef(pd).calcparas;
  191. end;
  192. end;
  193. begin
  194. cnodeutils:=tjvmnodeutils;
  195. end.