tgcpu.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. if forcesize<>-1 then
  66. thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,forcesize div tarraydef(eledef).elesize,R_INTREGISTER)
  67. else
  68. thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,tarraydef(eledef).elecount,R_INTREGISTER);
  69. eledef:=tarraydef(eledef).elementdef;
  70. inc(ndim);
  71. forcesize:=-1;
  72. until (eledef.typ<>arraydef) or
  73. is_dynamic_array(eledef);
  74. eledef:=tarraydef(def).elementdef;
  75. thlcgjvm(hlcg).g_newarray(list,def,ndim);
  76. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  77. { allocate the records }
  78. if is_record(eledef) then
  79. hlcg.g_initialize(list,def,ref);
  80. result:=true;
  81. end;
  82. end;
  83. recorddef:
  84. begin
  85. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  86. list.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(trecorddef(def).jvm_full_typename(true))));
  87. { the constructor doesn't return anything, so put a duplicate of the
  88. self pointer on the evaluation stack for use as function result
  89. after the constructor has run }
  90. list.concat(taicpu.op_none(a_dup));
  91. thlcgjvm(hlcg).incstack(list,2);
  92. { call the constructor }
  93. sym:=tsym(trecorddef(def).symtable.find('CREATE'));
  94. if assigned(sym) and
  95. (sym.typ=procsym) then
  96. begin
  97. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  98. if not assigned(pd) then
  99. internalerror(2011032701);
  100. end;
  101. hlcg.a_call_name(list,pd,pd.mangledname,false);
  102. thlcgjvm(hlcg).decstack(list,1);
  103. { store reference to instance }
  104. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  105. result:=true;
  106. end;
  107. setdef:
  108. begin
  109. if is_smallset(def) then
  110. exit;
  111. {$ifndef nounsupported}
  112. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  113. result:=true;
  114. {$endif}
  115. end;
  116. end;
  117. end;
  118. function ttgjvm.alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint;
  119. begin
  120. { the JVM only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  121. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  122. There are no problems with reusing the same slot for a value of a different
  123. type. There are no alignment requirements either. }
  124. if size<4 then
  125. size:=4;
  126. if not(size in [4,8]) then
  127. internalerror(2010121401);
  128. { don't pass on "def", since we don't care if a slot is used again for a
  129. different type }
  130. result:=inherited alloctemp(list, size shr 2, 1, temptype, nil);
  131. end;
  132. procedure ttgjvm.setfirsttemp(l: longint);
  133. begin
  134. firsttemp:=l;
  135. lasttemp:=l;
  136. end;
  137. procedure ttgjvm.getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference);
  138. begin
  139. if not getifspecialtemp(list,def,size,tt_persistent,ref) then
  140. inherited;
  141. end;
  142. procedure ttgjvm.gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference);
  143. begin
  144. if not getifspecialtemp(list,def,forcesize,temptype,ref) then
  145. inherited;
  146. end;
  147. begin
  148. tgobjclass:=ttgjvm;
  149. end.