njvmcal.pas 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. jvmdef;
  40. {*****************************************************************************
  41. TJVMCALLNODE
  42. *****************************************************************************}
  43. procedure tjvmcallnode.extra_pre_call_code;
  44. begin
  45. { when calling a constructor, first create a new instance, except
  46. when calling it from another constructor (because then this has
  47. already been done before calling the current constructor) }
  48. if procdefinition.typ<>procdef then
  49. exit;
  50. if tprocdef(procdefinition).proctypeoption<>potype_constructor then
  51. exit;
  52. if (methodpointer.resultdef.typ<>classrefdef) then
  53. exit;
  54. current_asmdata.CurrAsmList.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(tobjectdef(tprocdef(procdefinition).owner.defowner).jvm_full_typename)));
  55. { the constructor doesn't return anything, so put a duplicate of the
  56. self pointer on the evaluation stack for use as function result
  57. after the constructor has run }
  58. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_dup));
  59. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,2);
  60. end;
  61. procedure tjvmcallnode.set_result_location(realresdef: tstoreddef);
  62. begin
  63. location_reset_ref(location,LOC_REFERENCE,def_cgsize(realresdef),1);
  64. tg.gettemp(current_asmdata.CurrAsmList,realresdef.size,1,tt_normal,location.reference);
  65. end;
  66. procedure tjvmcallnode.do_release_unused_return_value;
  67. begin
  68. if (tprocdef(procdefinition).proctypeoption=potype_constructor) and
  69. (current_procinfo.procdef.proctypeoption=potype_constructor) then
  70. exit;
  71. case resultdef.size of
  72. 0:
  73. ;
  74. 1..4:
  75. begin
  76. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop));
  77. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,1);
  78. end;
  79. 8:
  80. begin
  81. current_asmdata.CurrAsmList.concat(taicpu.op_none(a_pop2));
  82. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,2);
  83. end
  84. else
  85. internalerror(2011010305);
  86. end;
  87. end;
  88. procedure tjvmcallnode.extra_post_call_code;
  89. var
  90. totalremovesize: longint;
  91. realresdef: tdef;
  92. begin
  93. if not assigned(typedef) then
  94. realresdef:=tstoreddef(resultdef)
  95. else
  96. realresdef:=tstoreddef(typedef);
  97. { a constructor doesn't actually return a value in the jvm }
  98. if (tprocdef(procdefinition).proctypeoption=potype_constructor) then
  99. totalremovesize:=pushedparasize
  100. else
  101. totalremovesize:=pushedparasize-realresdef.size;
  102. { remove parameters from internal evaluation stack counter (in case of
  103. e.g. no parameters and a result, it can also increase) }
  104. if totalremovesize>0 then
  105. thlcgjvm(hlcg).decstack(current_asmdata.CurrAsmList,totalremovesize shr 2)
  106. else if totalremovesize<0 then
  107. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,(-totalremovesize) shr 2);
  108. end;
  109. begin
  110. ccallnode:=tjvmcallnode;
  111. end.