njvmcal.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. JVM-specific code for call nodes
  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 njvmcal;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. symdef,
  22. ncgcal;
  23. type
  24. { tjvmcallnode }
  25. tjvmcallnode = class(tcgcallnode)
  26. protected
  27. procedure extra_pre_call_code; override;
  28. procedure set_result_location(realresdef: tstoreddef); override;
  29. procedure do_release_unused_return_value;override;
  30. procedure extra_post_call_code; override;
  31. end;
  32. implementation
  33. uses
  34. verbose,globtype,
  35. symconst,symtype,defutil,ncal,
  36. cgbase,cgutils,tgobj,procinfo,
  37. cpubase,aasmdata,aasmcpu,
  38. hlcgobj,hlcgcpu,
  39. node,
  40. jvmdef;
  41. {*****************************************************************************
  42. TJVMCALLNODE
  43. *****************************************************************************}
  44. procedure tjvmcallnode.extra_pre_call_code;
  45. begin
  46. { when calling a constructor, first create a new instance, except
  47. when calling it from another constructor (because then this has
  48. already been done before calling the current constructor) }
  49. if procdefinition.typ<>procdef then
  50. exit;
  51. if tabstractprocdef(procdefinition).proctypeoption<>potype_constructor then
  52. exit;
  53. if not(methodpointer.resultdef.typ in [classrefdef,recorddef]) then
  54. exit;
  55. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(tabstractrecorddef(tabstractprocdef(procdefinition).owner.defowner).jvm_full_typename(true))));
  56. { the constructor doesn't return anything, so put a duplicate of the
  57. self pointer on the evaluation stack for use as function result
  58. after the constructor has run }
  59. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
  60. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,2);
  61. end;
  62. procedure tjvmcallnode.set_result_location(realresdef: tstoreddef);
  63. begin
  64. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),1);
  65. { in case of jvmimplicitpointertype(), the function will have allocated
  66. it already and we don't have to allocate it again here }
  67. if not jvmimplicitpointertype(realresdef) then
  68. tg.gethltemp(current_asmdata.CurrAsmList,realresdef,realresdef.size,tt_normal,location.reference)
  69. else
  70. tg.gethltemp(current_asmdata.CurrAsmList,java_jlobject,java_jlobject.size,tt_normal,location.reference);
  71. end;
  72. procedure tjvmcallnode.do_release_unused_return_value;
  73. begin
  74. if (tabstractprocdef(procdefinition).proctypeoption=potype_constructor) and
  75. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  76. exit;
  77. case resultdef.size of
  78. 0:
  79. ;
  80. 1..4:
  81. begin
  82. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop));
  83. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  84. end;
  85. 8:
  86. begin
  87. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop2));
  88. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  89. end
  90. else
  91. internalerror(2011010305);
  92. end;
  93. end;
  94. procedure tjvmcallnode.extra_post_call_code;
  95. var
  96. totalremovesize: longint;
  97. realresdef: tdef;
  98. begin
  99. if not assigned(typedef) then
  100. realresdef:=tstoreddef(resultdef)
  101. else
  102. realresdef:=tstoreddef(typedef);
  103. { a constructor doesn't actually return a value in the jvm }
  104. if (tabstractprocdef(procdefinition).proctypeoption=potype_constructor) then
  105. totalremovesize:=pushedparasize
  106. else
  107. { even a byte takes up a full stackslot -> align size to multiple of 4 }
  108. totalremovesize:=pushedparasize-(align(realresdef.size,4) shr 2);
  109. { remove parameters from internal evaluation stack counter (in case of
  110. e.g. no parameters and a result, it can also increase) }
  111. if totalremovesize>0 then
  112. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,totalremovesize)
  113. else if totalremovesize<0 then
  114. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,-totalremovesize);
  115. { if this was an inherited constructor call, initialise all fields that
  116. are wrapped types following it }
  117. if (tabstractprocdef(procdefinition).proctypeoption=potype_constructor) and
  118. (cnf_inherited in callnodeflags) then
  119. thlcgjvm(hlcg).gen_initialize_fields_code(current_asmdata.CurrAsmList);
  120. end;
  121. begin
  122. ccallnode:=tjvmcallnode;
  123. end.