cpupara.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. {
  2. Copyright (c) 2019 by Dmtiry Boyarintsev
  3. Calling conventions for the WebAssembly
  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 getintparaloc(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. private
  45. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  46. var parasize:longint);
  47. end;
  48. implementation
  49. uses
  50. cutils,verbose,systems,
  51. defutil,wasmdef,
  52. aasmcpu,
  53. hlcgobj;
  54. procedure tcpuparamanager.GetIntParaLoc(list: TAsmList; pd : tabstractprocdef; nr : longint; var cgpara : tcgpara);
  55. begin
  56. { not yet implemented/used }
  57. internalerror(2010121001);
  58. end;
  59. function tcpuparamanager.get_saved_registers_int(calloption: tproccalloption): tcpuregisterarray;
  60. const
  61. { dummy, not used for JVM }
  62. saved_regs: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (RS_NO);
  63. begin
  64. result:=saved_regs;
  65. end;
  66. function tcpuparamanager.push_high_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  67. begin
  68. { we don't need a separate high parameter, since all arrays in Java
  69. have an implicit associated length }
  70. if not is_open_array(def) and
  71. not is_array_of_const(def) then
  72. result:=inherited
  73. else
  74. result:=false;
  75. end;
  76. function tcpuparamanager.keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  77. begin
  78. { even though these don't need a high parameter (see push_high_param),
  79. we do have to keep the original parameter's array length because it's
  80. used by the compiler (to determine the size of the array to construct
  81. to pass to an array of const parameter) }
  82. if not is_array_of_const(def) then
  83. result:=inherited
  84. else
  85. result:=true;
  86. end;
  87. { true if a parameter is too large to copy and only the address is pushed }
  88. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  89. begin
  90. {result:=
  91. jvmimplicitpointertype(def) or
  92. ((def.typ=formaldef) and
  93. not(varspez in [vs_var,vs_out]));}
  94. //todo:
  95. result := false;
  96. end;
  97. function tcpuparamanager.push_copyout_param(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  98. begin
  99. { in principle also for vs_constref, but since we can't have real
  100. references, that won't make a difference }
  101. {result:=
  102. (varspez in [vs_var,vs_out,vs_constref]) and
  103. not jvmimplicitpointertype(def);}
  104. Result := false;
  105. end;
  106. function tcpuparamanager.push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;
  107. begin
  108. { all aggregate types are emulated using indirect pointer types }
  109. if def.typ in [arraydef,recorddef,setdef,stringdef] then
  110. result:=4
  111. else
  112. result:=inherited;
  113. end;
  114. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  115. var
  116. paraloc : pcgparalocation;
  117. retcgsize : tcgsize;
  118. begin
  119. result.init;
  120. result.alignment:=get_para_align(p.proccalloption);
  121. if not assigned(forcetempdef) then
  122. result.def:=p.returndef
  123. else
  124. begin
  125. result.def:=forcetempdef;
  126. result.temporary:=true;
  127. end;
  128. result.def:=get_para_push_size(result.def);
  129. { void has no location }
  130. if is_void(result.def) then
  131. begin
  132. paraloc:=result.add_location;
  133. result.size:=OS_NO;
  134. result.intsize:=0;
  135. paraloc^.size:=OS_NO;
  136. paraloc^.def:=voidtype;
  137. paraloc^.loc:=LOC_VOID;
  138. exit;
  139. end;
  140. { Constructors return self instead of a boolean }
  141. if (p.proctypeoption=potype_constructor) then
  142. begin
  143. retcgsize:=OS_INT;
  144. result.intsize:=sizeof(pint);
  145. end
  146. //todo: wasm should have the similar
  147. {else if jvmimplicitpointertype(result.def) then
  148. begin
  149. retcgsize:=OS_ADDR;
  150. result.def:=cpointerdef.getreusable_no_free(result.def);
  151. end}
  152. else
  153. begin
  154. retcgsize:=def_cgsize(result.def);
  155. result.intsize:=result.def.size;
  156. end;
  157. result.size:=retcgsize;
  158. paraloc:=result.add_location;
  159. { all values are returned on the evaluation stack }
  160. paraloc^.loc:=LOC_REFERENCE;
  161. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  162. paraloc^.reference.offset:=0;
  163. paraloc^.size:=result.size;
  164. paraloc^.def:=result.def;
  165. end;
  166. function tcpuparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  167. begin
  168. { all parameters are copied by the VM to local variable locations }
  169. result:=true;
  170. end;
  171. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  172. begin
  173. { not as efficient as returning in param for jvmimplicitpointertypes,
  174. but in the latter case the routines are harder to use from Java
  175. (especially for arrays), because the caller then manually has to
  176. allocate the instance/array of the right size }
  177. Result:=false;
  178. end;
  179. function tcpuparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  180. begin
  181. { all parameters are passed on the evaluation stack }
  182. result:=true;
  183. end;
  184. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;
  185. var
  186. parasize : longint;
  187. begin
  188. parasize:=0;
  189. { calculate the registers for the normal parameters }
  190. create_paraloc_info_intern(p,side,p.paras,parasize);
  191. { append the varargs }
  192. if assigned(varargspara) then
  193. begin
  194. if side=callerside then
  195. create_paraloc_info_intern(p,side,varargspara,parasize)
  196. else
  197. internalerror(2019021924);
  198. end;
  199. create_funcretloc_info(p,side);
  200. result:=parasize;
  201. end;
  202. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  203. var parasize:longint);
  204. var
  205. paraloc : pcgparalocation;
  206. i : integer;
  207. hp : tparavarsym;
  208. paracgsize : tcgsize;
  209. paraofs : longint;
  210. paradef : tdef;
  211. begin
  212. paraofs:=0;
  213. for i:=0 to paras.count-1 do
  214. begin
  215. hp:=tparavarsym(paras[i]);
  216. if push_copyout_param(hp.varspez,hp.vardef,p.proccalloption) then
  217. begin
  218. { passed via array reference (instead of creating a new array
  219. type for every single parameter, use java_jlobject) }
  220. paracgsize:=OS_ADDR;
  221. paradef:=java_jlobject;
  222. end
  223. //todo: wasm should have the similar
  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. initialization
  290. ParaManager:=tcpuparamanager.create;
  291. end.