cpupara.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean; override;
  30. function push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;override;
  31. function push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean; override;
  32. function push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;override;
  33. {Returns a structure giving the information on the storage of the parameter
  34. (which must be an integer parameter)
  35. @param(nr Parameter number of routine, starting from 1)}
  36. procedure getintparaloc(pd : tabstractprocdef; nr : longint; var cgpara : tcgpara);override;
  37. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  38. function create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;override;
  39. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  40. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  41. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  42. function is_stack_paraloc(paraloc: pcgparalocation): boolean;override;
  43. private
  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(pd : tabstractprocdef; nr : longint; var cgpara : tcgpara);
  54. begin
  55. { not yet implemented/used }
  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) and
  63. not is_array_of_const(def) then
  64. result:=inherited
  65. else
  66. result:=false;
  67. end;
  68. function TJVMParaManager.keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  69. begin
  70. { even though these don't need a high parameter (see push_high_param),
  71. we do have to keep the original parameter's array length because it's
  72. used by the compiler (to determine the size of the array to construct
  73. to pass to an array of const parameter) }
  74. if not is_array_of_const(def) then
  75. result:=inherited
  76. else
  77. result:=true;
  78. end;
  79. { true if a parameter is too large to copy and only the address is pushed }
  80. function TJVMParaManager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  81. begin
  82. result:=
  83. jvmimplicitpointertype(def) or
  84. ((def.typ=formaldef) and
  85. not(varspez in [vs_var,vs_out]));
  86. end;
  87. function TJVMParaManager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  88. begin
  89. { in principle also for vs_constref, but since we can't have real
  90. references, that won't make a difference }
  91. result:=
  92. (varspez in [vs_var,vs_out,vs_constref]) and
  93. not jvmimplicitpointertype(def);
  94. end;
  95. function TJVMParaManager.push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;
  96. begin
  97. { all aggregate types are emulated using indirect pointer types }
  98. if def.typ in [arraydef,recorddef,setdef,stringdef] then
  99. result:=4
  100. else
  101. result:=inherited;
  102. end;
  103. function TJVMParaManager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  104. var
  105. paraloc : pcgparalocation;
  106. retcgsize : tcgsize;
  107. begin
  108. result.init;
  109. result.alignment:=get_para_align(p.proccalloption);
  110. if not assigned(forcetempdef) then
  111. result.def:=p.returndef
  112. else
  113. begin
  114. result.def:=forcetempdef;
  115. result.temporary:=true;
  116. end;
  117. result.def:=get_para_push_size(result.def);
  118. { void has no location }
  119. if is_void(result.def) then
  120. begin
  121. paraloc:=result.add_location;
  122. result.size:=OS_NO;
  123. result.intsize:=0;
  124. paraloc^.size:=OS_NO;
  125. paraloc^.def:=voidtype;
  126. paraloc^.loc:=LOC_VOID;
  127. exit;
  128. end;
  129. { Constructors return self instead of a boolean }
  130. if (p.proctypeoption=potype_constructor) then
  131. begin
  132. retcgsize:=OS_INT;
  133. result.intsize:=sizeof(pint);
  134. end
  135. else if jvmimplicitpointertype(result.def) then
  136. begin
  137. retcgsize:=OS_ADDR;
  138. result.def:=getpointerdef(result.def);
  139. end
  140. else
  141. begin
  142. retcgsize:=def_cgsize(result.def);
  143. result.intsize:=result.def.size;
  144. end;
  145. result.size:=retcgsize;
  146. paraloc:=result.add_location;
  147. { all values are returned on the evaluation stack }
  148. paraloc^.loc:=LOC_REFERENCE;
  149. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  150. paraloc^.reference.offset:=0;
  151. paraloc^.size:=result.size;
  152. paraloc^.def:=result.def;
  153. end;
  154. function TJVMParaManager.param_use_paraloc(const cgpara: tcgpara): boolean;
  155. begin
  156. { all parameters are copied by the VM to local variable locations }
  157. result:=true;
  158. end;
  159. function TJVMParaManager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  160. begin
  161. { not as efficient as returning in param for jvmimplicitpointertypes,
  162. but in the latter case the routines are harder to use from Java
  163. (especially for arrays), because the caller then manually has to
  164. allocate the instance/array of the right size }
  165. Result:=false;
  166. end;
  167. function TJVMParaManager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  168. begin
  169. { all parameters are passed on the evaluation stack }
  170. result:=true;
  171. end;
  172. function TJVMParaManager.create_varargs_paraloc_info(p : tabstractprocdef; varargspara:tvarargsparalist):longint;
  173. var
  174. parasize : longint;
  175. begin
  176. parasize:=0;
  177. { calculate the registers for the normal parameters }
  178. create_paraloc_info_intern(p,callerside,p.paras,parasize);
  179. { append the varargs }
  180. create_paraloc_info_intern(p,callerside,varargspara,parasize);
  181. result:=parasize;
  182. end;
  183. procedure TJVMParaManager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  184. var parasize:longint);
  185. var
  186. paraloc : pcgparalocation;
  187. i : integer;
  188. hp : tparavarsym;
  189. paracgsize : tcgsize;
  190. paraofs : longint;
  191. paradef : tdef;
  192. begin
  193. paraofs:=0;
  194. for i:=0 to paras.count-1 do
  195. begin
  196. hp:=tparavarsym(paras[i]);
  197. if push_copyout_param(hp.varspez,hp.vardef,p.proccalloption) then
  198. begin
  199. { passed via array reference (instead of creating a new array
  200. type for every single parameter, use java_jlobject) }
  201. paracgsize:=OS_ADDR;
  202. paradef:=java_jlobject;
  203. end
  204. else if jvmimplicitpointertype(hp.vardef) then
  205. begin
  206. paracgsize:=OS_ADDR;
  207. paradef:=getpointerdef(hp.vardef);
  208. end
  209. else
  210. begin
  211. paracgsize:=def_cgsize(hp.vardef);
  212. if paracgsize=OS_NO then
  213. paracgsize:=OS_ADDR;
  214. paradef:=hp.vardef;
  215. end;
  216. paradef:=get_para_push_size(paradef);
  217. hp.paraloc[side].reset;
  218. hp.paraloc[side].size:=paracgsize;
  219. hp.paraloc[side].def:=paradef;
  220. hp.paraloc[side].alignment:=std_param_align;
  221. hp.paraloc[side].intsize:=tcgsize2size[paracgsize];
  222. paraloc:=hp.paraloc[side].add_location;
  223. { All parameters are passed on the evaluation stack, pushed from
  224. left to right (including self, if applicable). At the callee side,
  225. they're available as local variables 0..n-1 (with 64 bit values
  226. taking up two slots) }
  227. paraloc^.loc:=LOC_REFERENCE;;
  228. paraloc^.reference.offset:=paraofs;
  229. paraloc^.size:=paracgsize;
  230. paraloc^.def:=paradef;
  231. case side of
  232. callerside:
  233. begin
  234. paraloc^.loc:=LOC_REFERENCE;
  235. { we use a fake loc_reference to indicate the stack location;
  236. the offset (set above) will be used by ncal to order the
  237. parameters so they will be pushed in the right order }
  238. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  239. end;
  240. calleeside:
  241. begin
  242. paraloc^.loc:=LOC_REFERENCE;
  243. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  244. end;
  245. end;
  246. { 2 slots for 64 bit integers and floats, 1 slot for the rest }
  247. if not(is_64bit(paradef) or
  248. ((paradef.typ=floatdef) and
  249. (tfloatdef(paradef).floattype=s64real))) then
  250. inc(paraofs)
  251. else
  252. inc(paraofs,2);
  253. end;
  254. parasize:=paraofs;
  255. end;
  256. function TJVMParaManager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  257. var
  258. parasize : longint;
  259. begin
  260. parasize:=0;
  261. create_paraloc_info_intern(p,side,p.paras,parasize);
  262. { Create Function result paraloc }
  263. create_funcretloc_info(p,side);
  264. { We need to return the size allocated on the stack }
  265. result:=parasize;
  266. end;
  267. begin
  268. ParaManager:=TJVMParaManager.create;
  269. end.