tgcpu.pas 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {
  2. Copyright (C) 2010 by Jonas Maebe
  3. This unit handles the temporary variables for the JVM
  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. {
  18. This unit handles the temporary variables for the JVM.
  19. }
  20. unit tgcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,
  25. aasmdata,
  26. cgutils,
  27. symtype,tgobj;
  28. type
  29. { ttgjvm }
  30. ttgjvm = class(ttgobj)
  31. protected
  32. function getifspecialtemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference): boolean;
  33. function alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint; override;
  34. public
  35. procedure setfirsttemp(l : longint); override;
  36. procedure getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference); override;
  37. procedure gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference); override;
  38. end;
  39. implementation
  40. uses
  41. verbose,
  42. cgbase,
  43. symconst,symdef,symsym,defutil,
  44. cpubase,aasmcpu,
  45. hlcgobj,hlcgcpu;
  46. { ttgjvm }
  47. function ttgjvm.getifspecialtemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference): boolean;
  48. var
  49. eledef: tdef;
  50. ndim: longint;
  51. sym: tsym;
  52. pd: tprocdef;
  53. begin
  54. result:=false;
  55. case def.typ of
  56. arraydef:
  57. begin
  58. if not is_dynamic_array(def) then
  59. begin
  60. { allocate an array of the right size }
  61. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  62. ndim:=0;
  63. eledef:=def;
  64. repeat
  65. thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,tarraydef(eledef).elecount,R_INTREGISTER);
  66. eledef:=tarraydef(eledef).elementdef;
  67. inc(ndim);
  68. until (eledef.typ<>arraydef) or
  69. is_dynamic_array(eledef);
  70. eledef:=tarraydef(def).elementdef;
  71. thlcgjvm(hlcg).g_newarray(list,def,ndim);
  72. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  73. { allocate the records }
  74. if is_record(eledef) then
  75. hlcg.g_initialize(list,def,ref);
  76. result:=true;
  77. end;
  78. end;
  79. recorddef:
  80. begin
  81. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  82. list.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(trecorddef(def).jvm_full_typename(true))));
  83. { the constructor doesn't return anything, so put a duplicate of the
  84. self pointer on the evaluation stack for use as function result
  85. after the constructor has run }
  86. list.concat(taicpu.op_none(a_dup));
  87. thlcgjvm(hlcg).incstack(list,2);
  88. { call the constructor }
  89. sym:=tsym(trecorddef(def).symtable.find('CREATE'));
  90. if assigned(sym) and
  91. (sym.typ=procsym) then
  92. begin
  93. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  94. if not assigned(pd) then
  95. internalerror(2011032701);
  96. end;
  97. hlcg.a_call_name(list,pd,pd.mangledname,false);
  98. thlcgjvm(hlcg).decstack(list,1);
  99. { store reference to instance }
  100. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  101. result:=true;
  102. end;
  103. end;
  104. end;
  105. function ttgjvm.alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint;
  106. begin
  107. { the JVM only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  108. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  109. There are no problems with reusing the same slot for a value of a different
  110. type. There are no alignment requirements either. }
  111. if size<4 then
  112. size:=4;
  113. if not(size in [4,8]) then
  114. internalerror(2010121401);
  115. { don't pass on "def", since we don't care if a slot is used again for a
  116. different type }
  117. result:=inherited alloctemp(list, size shr 2, 1, temptype, nil);
  118. end;
  119. procedure ttgjvm.setfirsttemp(l: longint);
  120. begin
  121. firsttemp:=l;
  122. lasttemp:=l;
  123. end;
  124. procedure ttgjvm.getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference);
  125. begin
  126. if not getifspecialtemp(list,def,size,tt_persistent,ref) then
  127. inherited;
  128. end;
  129. procedure ttgjvm.gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference);
  130. begin
  131. if not getifspecialtemp(list,def,forcesize,temptype,ref) then
  132. inherited;
  133. end;
  134. begin
  135. tgobjclass:=ttgjvm;
  136. end.