cpupara.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl, Jonas Maebe
  3. Calling conventions 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. unit cpupara;
  17. {$i fpcdefs.inc}
  18. interface
  19. uses
  20. globtype,
  21. cclasses,
  22. aasmtai,aasmdata,
  23. cpubase,cpuinfo,
  24. symconst,symbase,symsym,symtype,symdef,paramgr,parabase,cgbase,cgutils;
  25. type
  26. { TJVMParaManager }
  27. TJVMParaManager=class(TParaManager)
  28. function push_high_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  29. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  30. function push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean; override;
  31. function push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;override;
  32. {Returns a structure giving the information on the storage of the parameter
  33. (which must be an integer parameter)
  34. @param(nr Parameter number of routine, starting from 1)}
  35. procedure getintparaloc(calloption : tproccalloption; nr : longint;var cgpara : TCGPara);override;
  36. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  37. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;override;
  38. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tcgpara;override;
  39. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  40. function ret_in_param(def: tdef; calloption: tproccalloption): boolean; override;
  41. function is_stack_paraloc(paraloc: pcgparalocation): boolean;override;
  42. private
  43. procedure create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  44. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  45. var parasize:longint);
  46. end;
  47. implementation
  48. uses
  49. cutils,verbose,systems,
  50. defutil,jvmdef,
  51. aasmcpu,
  52. hlcgobj;
  53. procedure TJVMParaManager.GetIntParaLoc(calloption : tproccalloption; nr : longint;var cgpara : tcgpara);
  54. begin
  55. { don't know whether it's an actual integer or a pointer (necessary for cgpara.def) }
  56. internalerror(2010121001);
  57. end;
  58. function TJVMParaManager.push_high_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  59. begin
  60. { we don't need a separate high parameter, since all arrays in Java
  61. have an implicit associated length }
  62. if not is_open_array(def) then
  63. result:=inherited
  64. else
  65. result:=false;
  66. end;
  67. { true if a parameter is too large to copy and only the address is pushed }
  68. function TJVMParaManager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  69. begin
  70. result:=
  71. jvmimplicitpointertype(def) or
  72. ((def.typ=formaldef) and
  73. not(varspez in [vs_var,vs_out]));
  74. end;
  75. function TJVMParaManager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  76. begin
  77. { in principle also for vs_constref, but since we can't have real
  78. references, that won't make a difference }
  79. result:=
  80. (varspez in [vs_var,vs_out,vs_constref]) and
  81. not jvmimplicitpointertype(def);
  82. end;
  83. function TJVMParaManager.push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;
  84. begin
  85. { all aggregate types are emulated using indirect pointer types }
  86. if def.typ in [arraydef,recorddef,setdef,stringdef] then
  87. result:=4
  88. else
  89. result:=inherited;
  90. end;
  91. procedure TJVMParaManager.create_funcretloc_info(p : tabstractprocdef; side: tcallercallee);
  92. begin
  93. p.funcretloc[side]:=get_funcretloc(p,side,p.returndef);
  94. end;
  95. function TJVMParaManager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; def: tdef): tcgpara;
  96. var
  97. paraloc : pcgparalocation;
  98. retcgsize : tcgsize;
  99. begin
  100. def:=get_para_push_size(def);
  101. result.init;
  102. result.alignment:=get_para_align(p.proccalloption);
  103. result.def:=def;
  104. { void has no location }
  105. if is_void(def) then
  106. begin
  107. paraloc:=result.add_location;
  108. result.size:=OS_NO;
  109. result.intsize:=0;
  110. paraloc^.size:=OS_NO;
  111. paraloc^.loc:=LOC_VOID;
  112. exit;
  113. end;
  114. { Constructors return self instead of a boolean }
  115. if (p.proctypeoption=potype_constructor) then
  116. begin
  117. retcgsize:=OS_INT;
  118. result.intsize:=sizeof(pint);
  119. end
  120. else
  121. begin
  122. retcgsize:=def_cgsize(def);
  123. result.intsize:=def.size;
  124. end;
  125. result.size:=retcgsize;
  126. paraloc:=result.add_location;
  127. { all values are returned on the evaluation stack }
  128. paraloc^.loc:=LOC_REFERENCE;
  129. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  130. paraloc^.reference.offset:=0;
  131. end;
  132. function TJVMParaManager.param_use_paraloc(const cgpara: tcgpara): boolean;
  133. begin
  134. { all parameters are copied by the VM to local variable locations }
  135. result:=true;
  136. end;
  137. function TJVMParaManager.ret_in_param(def: tdef; calloption: tproccalloption): boolean;
  138. begin
  139. { not as efficient as returning in param for jvmimplicitpointertypes,
  140. but in the latter case the routines are harder to use from Java
  141. (especially for arrays), because the caller then manually has to
  142. allocate the instance/array of the right size }
  143. Result:=false;
  144. end;
  145. function TJVMParaManager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  146. begin
  147. { all parameters are passed on the evaluation stack }
  148. result:=true;
  149. end;
  150. function TJVMParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  151. var
  152. parasize : longint;
  153. begin
  154. parasize:=0;
  155. { calculate the registers for the normal parameters }
  156. create_paraloc_info_intern(p,callerside,p.paras,parasize);
  157. { append the varargs }
  158. create_paraloc_info_intern(p,callerside,varargspara,parasize);
  159. result:=parasize;
  160. end;
  161. procedure TJVMParaManager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  162. var parasize:longint);
  163. var
  164. paraloc : pcgparalocation;
  165. i : integer;
  166. hp : tparavarsym;
  167. paracgsize : tcgsize;
  168. paraofs : longint;
  169. paradef : tdef;
  170. begin
  171. paraofs:=0;
  172. for i:=0 to paras.count-1 do
  173. begin
  174. hp:=tparavarsym(paras[i]);
  175. if push_copyout_param(hp.varspez,hp.vardef,p.proccalloption) then
  176. begin
  177. { passed via array reference (instead of creating a new array
  178. type for every single parameter, use java_jlobject) }
  179. paracgsize:=OS_ADDR;
  180. paradef:=java_jlobject;
  181. end
  182. else
  183. begin
  184. paracgsize:=def_cgsize(hp.vardef);
  185. if paracgsize=OS_NO then
  186. paracgsize:=OS_ADDR;
  187. paradef:=hp.vardef;
  188. end;
  189. paradef:=get_para_push_size(paradef);
  190. hp.paraloc[side].reset;
  191. hp.paraloc[side].size:=paracgsize;
  192. hp.paraloc[side].def:=paradef;
  193. hp.paraloc[side].alignment:=std_param_align;
  194. hp.paraloc[side].intsize:=tcgsize2size[paracgsize];
  195. paraloc:=hp.paraloc[side].add_location;
  196. { All parameters are passed on the evaluation stack, pushed from
  197. left to right (including self, if applicable). At the callee side,
  198. they're available as local variables 0..n-1 (with 64 bit values
  199. taking up two slots) }
  200. paraloc^.loc:=LOC_REFERENCE;;
  201. paraloc^.reference.offset:=paraofs;
  202. case side of
  203. callerside:
  204. begin
  205. paraloc^.loc:=LOC_REFERENCE;
  206. { we use a fake loc_reference to indicate the stack location;
  207. the offset (set above) will be used by ncal to order the
  208. parameters so they will be pushed in the right order }
  209. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  210. end;
  211. calleeside:
  212. begin
  213. paraloc^.loc:=LOC_REFERENCE;
  214. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  215. end;
  216. end;
  217. { 2 slots for 64 bit integers and floats, 1 slot for the rest }
  218. if not(is_64bit(paradef) or
  219. ((paradef.typ=floatdef) and
  220. (tfloatdef(paradef).floattype=s64real))) then
  221. inc(paraofs)
  222. else
  223. inc(paraofs,2);
  224. end;
  225. parasize:=paraofs;
  226. end;
  227. function TJVMParaManager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  228. var
  229. parasize : longint;
  230. begin
  231. parasize:=0;
  232. create_paraloc_info_intern(p,side,p.paras,parasize);
  233. { Create Function result paraloc }
  234. create_funcretloc_info(p,side);
  235. { We need to return the size allocated on the stack }
  236. result:=parasize;
  237. end;
  238. begin
  239. ParaManager:=TJVMParaManager.create;
  240. end.