tgcpu.pas 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. procedure gethltemptyped(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference); override;
  39. end;
  40. implementation
  41. uses
  42. verbose,
  43. cgbase,
  44. symconst,symdef,symsym,defutil,
  45. cpubase,aasmcpu,
  46. hlcgobj,hlcgcpu;
  47. { ttgjvm }
  48. function ttgjvm.getifspecialtemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference): boolean;
  49. var
  50. eledef: tdef;
  51. ndim: longint;
  52. sym: tsym;
  53. pd: tprocdef;
  54. begin
  55. result:=false;
  56. case def.typ of
  57. arraydef:
  58. begin
  59. if not is_dynamic_array(def) then
  60. begin
  61. { allocate an array of the right size }
  62. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  63. ndim:=0;
  64. eledef:=def;
  65. repeat
  66. if forcesize<>-1 then
  67. thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,forcesize div tarraydef(eledef).elesize,R_INTREGISTER)
  68. else
  69. thlcgjvm(hlcg).a_load_const_stack(list,s32inttype,tarraydef(eledef).elecount,R_INTREGISTER);
  70. eledef:=tarraydef(eledef).elementdef;
  71. inc(ndim);
  72. forcesize:=-1;
  73. until (eledef.typ<>arraydef) or
  74. is_dynamic_array(eledef);
  75. eledef:=tarraydef(def).elementdef;
  76. thlcgjvm(hlcg).g_newarray(list,def,ndim);
  77. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  78. { allocate the records }
  79. if is_record(eledef) then
  80. hlcg.g_initialize(list,def,ref);
  81. result:=true;
  82. end;
  83. end;
  84. recorddef:
  85. begin
  86. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  87. list.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(trecorddef(def).jvm_full_typename(true))));
  88. { the constructor doesn't return anything, so put a duplicate of the
  89. self pointer on the evaluation stack for use as function result
  90. after the constructor has run }
  91. list.concat(taicpu.op_none(a_dup));
  92. thlcgjvm(hlcg).incstack(list,2);
  93. { call the constructor }
  94. sym:=tsym(trecorddef(def).symtable.find('CREATE'));
  95. if assigned(sym) and
  96. (sym.typ=procsym) then
  97. begin
  98. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  99. if not assigned(pd) then
  100. internalerror(2011032701);
  101. end
  102. else
  103. internalerror(2011060301);
  104. hlcg.a_call_name(list,pd,pd.mangledname,false);
  105. thlcgjvm(hlcg).decstack(list,1);
  106. { store reference to instance }
  107. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  108. result:=true;
  109. end;
  110. setdef:
  111. begin
  112. if tsetdef(def).elementdef.typ=enumdef then
  113. begin
  114. { load enum class type }
  115. list.concat(taicpu.op_sym(a_ldc,current_asmdata.RefAsmSymbol(tenumdef(tsetdef(def).elementdef).getbasedef.classdef.jvm_full_typename(true))));
  116. thlcgjvm(hlcg).incstack(current_asmdata.CurrAsmList,1);
  117. { call tenumset.noneOf() class method }
  118. sym:=tsym(tobjectdef(java_juenumset).symtable.find('NONEOF'));
  119. if assigned(sym) and
  120. (sym.typ=procsym) then
  121. begin
  122. if tprocsym(sym).procdeflist.Count<>1 then
  123. internalerror(2011062801);
  124. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  125. end;
  126. hlcg.a_call_name(list,pd,pd.mangledname,false);
  127. { static calls method replaces parameter with set instance
  128. -> no change in stack height }
  129. end
  130. else
  131. begin
  132. list.concat(taicpu.op_sym(a_new,current_asmdata.RefAsmSymbol(java_jubitset.jvm_full_typename(true))));
  133. { the constructor doesn't return anything, so put a duplicate of the
  134. self pointer on the evaluation stack for use as function result
  135. after the constructor has run }
  136. list.concat(taicpu.op_none(a_dup));
  137. thlcgjvm(hlcg).incstack(list,2);
  138. { call the constructor }
  139. sym:=tsym(java_jubitset.symtable.find('CREATE'));
  140. if assigned(sym) and
  141. (sym.typ=procsym) then
  142. begin
  143. pd:=tprocsym(sym).find_bytype_parameterless(potype_constructor);
  144. if not assigned(pd) then
  145. internalerror(2011062802);
  146. end
  147. else
  148. internalerror(2011062803);
  149. hlcg.a_call_name(list,pd,pd.mangledname,false);
  150. { duplicate self pointer is removed }
  151. thlcgjvm(hlcg).decstack(list,1);
  152. end;
  153. { store reference to instance }
  154. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  155. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  156. result:=true;
  157. end;
  158. stringdef:
  159. begin
  160. if is_shortstring(def) then
  161. begin
  162. gettemp(list,java_jlobject.size,java_jlobject.alignment,temptype,ref);
  163. { add the maxlen parameter }
  164. thlcgjvm(hlcg).a_load_const_stack(list,u8inttype,tstringdef(def).len,R_INTREGISTER);
  165. { call the constructor }
  166. sym:=tsym(tobjectdef(java_shortstring).symtable.find('CREATEEMPTY'));
  167. if assigned(sym) and
  168. (sym.typ=procsym) then
  169. begin
  170. if tprocsym(sym).procdeflist.Count<>1 then
  171. internalerror(2011052404);
  172. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  173. end;
  174. hlcg.a_call_name(list,pd,pd.mangledname,false);
  175. { static calls method replaces parameter with string instance
  176. -> no change in stack height }
  177. { store reference to instance }
  178. thlcgjvm(hlcg).a_load_stack_ref(list,java_jlobject,ref,0);
  179. result:=true;
  180. end;
  181. end;
  182. end;
  183. end;
  184. function ttgjvm.alloctemp(list: TAsmList; size, alignment: longint; temptype: ttemptype; def: tdef): longint;
  185. begin
  186. { the JVM only supports 1 slot (= 4 bytes in FPC) and 2 slot (= 8 bytes in
  187. FPC) temps on the stack. double and int64 are 2 slots, the rest is one slot.
  188. There are no problems with reusing the same slot for a value of a different
  189. type. There are no alignment requirements either. }
  190. if size<4 then
  191. size:=4;
  192. if not(size in [4,8]) then
  193. internalerror(2010121401);
  194. { don't pass on "def", since we don't care if a slot is used again for a
  195. different type }
  196. result:=inherited alloctemp(list, size shr 2, 1, temptype, nil);
  197. end;
  198. procedure ttgjvm.setfirsttemp(l: longint);
  199. begin
  200. firsttemp:=l;
  201. lasttemp:=l;
  202. end;
  203. procedure ttgjvm.getlocal(list: TAsmList; size: longint; alignment: shortint; def: tdef; var ref: treference);
  204. begin
  205. if not getifspecialtemp(list,def,size,tt_persistent,ref) then
  206. inherited;
  207. end;
  208. procedure ttgjvm.gethltemp(list: TAsmList; def: tdef; forcesize: aint; temptype: ttemptype; out ref: treference);
  209. begin
  210. if not getifspecialtemp(list,def,forcesize,temptype,ref) then
  211. inherited;
  212. end;
  213. procedure ttgjvm.gethltemptyped(list: TAsmList; def: tdef; temptype: ttemptype; out ref: treference);
  214. begin
  215. gethltemp(list,def,def.size,temptype,ref);
  216. end;
  217. begin
  218. tgobjclass:=ttgjvm;
  219. end.