2
0

cpupara.pas 12 KB

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