tgcpu.pas 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. else
  102. internalerror(2011060301);
  103. hlcg.a_call_name(list,pd,pd.mangledname,false);
  104. thlcgjvm(hlcg).decstack(list,1);
  105. { store reference to instance }
  106. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  107. result:=true;
  108. end;
  109. setdef:
  110. begin
  111. if is_smallset(def) then
  112. exit;
  113. {$ifndef nounsupported}
  114. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  115. result:=true;
  116. {$endif}
  117. end;
  118. stringdef:
  119. begin
  120. if is_shortstring(def) then
  121. begin
  122. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  123. { add the maxlen parameter }
  124. thlcgjvm(hlcg).a_load_const_stack(list,u8inttype,tstringdef(def).len,R_INTREGISTER);
  125. { call the constructor }
  126. sym:=tsym(tobjectdef(java_shortstring).symtable.find('CREATEEMPTY'));
  127. if assigned(sym) and
  128. (sym.typ=procsym) then
  129. begin
  130. if tprocsym(sym).procdeflist.Count<>1 then
  131. internalerror(2011052404);
  132. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  133. end;
  134. hlcg.a_call_name(list,pd,pd.mangledname,false);
  135. { static calls method replaces parameter with string instance
  136. -> no change in stack height }
  137. { store reference to instance }
  138. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  139. result:=true;
  140. end;
  141. end;
  142. end;
  143. end;
  144. function ttgjvm.alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint;
  145. begin
  146. { the JVM only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  147. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  148. There are no problems with reusing the same slot for a value of a different
  149. type. There are no alignment requirements either. }
  150. if size<4 then
  151. size:=4;
  152. if not(size in [4,8]) then
  153. internalerror(2010121401);
  154. { don't pass on "def", since we don't care if a slot is used again for a
  155. different type }
  156. result:=inherited alloctemp(list, size shr 2, 1, temptype, nil);
  157. end;
  158. procedure ttgjvm.setfirsttemp(l: longint);
  159. begin
  160. firsttemp:=l;
  161. lasttemp:=l;
  162. end;
  163. procedure ttgjvm.getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference);
  164. begin
  165. if not getifspecialtemp(list,def,size,tt_persistent,ref) then
  166. inherited;
  167. end;
  168. procedure ttgjvm.gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference);
  169. begin
  170. if not getifspecialtemp(list,def,forcesize,temptype,ref) then
  171. inherited;
  172. end;
  173. begin
  174. tgobjclass:=ttgjvm;
  175. end.