cpupara.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_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(list: TAsmList; 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; side: tcallercallee; varargspara:tvarargsparalist):longint;override;
  39. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  40. { true if the location in paraloc can be reused as localloc }
  41. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  42. { Returns true if the return value is actually a parameter pointer }
  43. function ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  44. function is_stack_paraloc(paraloc: pcgparalocation): 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,wasmdef,
  53. aasmcpu,
  54. hlcgobj;
  55. procedure tcpuparamanager.GetIntParaLoc(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 WebAssembly }
  63. saved_regs: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (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:=false;
  92. { var,out,constref always require address }
  93. if varspez in [vs_var,vs_out,vs_constref] then
  94. begin
  95. result:=true;
  96. exit;
  97. end;
  98. { Only vs_const, vs_value here }
  99. case def.typ of
  100. variantdef,
  101. formaldef :
  102. result:=true;
  103. recorddef :
  104. begin
  105. { Delphi stdcall passes records on the stack for call by value }
  106. result:=(varspez=vs_const) or (def.size=0);
  107. end;
  108. arraydef :
  109. begin
  110. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  111. is_open_array(def) or
  112. is_array_of_const(def) or
  113. is_array_constructor(def);
  114. end;
  115. objectdef :
  116. result:=is_object(def);
  117. stringdef :
  118. result:= (tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  119. procvardef :
  120. result:=not(calloption in cdecl_pocalls) and not tprocvardef(def).is_addressonly;
  121. setdef :
  122. result:=not is_smallset(def);
  123. else
  124. ;
  125. end;
  126. end;
  127. function tcpuparamanager.push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;
  128. begin
  129. { all aggregate types are emulated using indirect pointer types }
  130. result:=inherited;
  131. end;
  132. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  133. var
  134. paraloc : pcgparalocation;
  135. retcgsize : tcgsize;
  136. begin
  137. result.init;
  138. result.alignment:=get_para_align(p.proccalloption);
  139. if not assigned(forcetempdef) then
  140. result.def:=p.returndef
  141. else
  142. begin
  143. result.def:=forcetempdef;
  144. result.temporary:=true;
  145. end;
  146. result.def:=get_para_push_size(result.def);
  147. { void has no location }
  148. if is_void(result.def) then
  149. begin
  150. paraloc:=result.add_location;
  151. result.size:=OS_NO;
  152. result.intsize:=0;
  153. paraloc^.size:=OS_NO;
  154. paraloc^.def:=voidtype;
  155. paraloc^.loc:=LOC_VOID;
  156. exit;
  157. end;
  158. { Constructors return self instead of a boolean }
  159. if (p.proctypeoption=potype_constructor) then
  160. begin
  161. retcgsize:=OS_INT;
  162. result.intsize:=sizeof(pint);
  163. end
  164. //todo: wasm should have the similar
  165. {else if jvmimplicitpointertype(result.def) then
  166. begin
  167. retcgsize:=OS_ADDR;
  168. result.def:=cpointerdef.getreusable_no_free(result.def);
  169. end}
  170. else
  171. begin
  172. retcgsize:=def_cgsize(result.def);
  173. result.intsize:=result.def.size;
  174. end;
  175. result.size:=retcgsize;
  176. paraloc:=result.add_location;
  177. { all values are returned on the evaluation stack }
  178. paraloc^.loc:=LOC_REFERENCE;
  179. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  180. paraloc^.reference.offset:=0;
  181. paraloc^.size:=result.size;
  182. paraloc^.def:=result.def;
  183. end;
  184. function tcpuparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  185. begin
  186. { all parameters are copied by the VM to local variable locations }
  187. result:=true;
  188. end;
  189. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  190. begin
  191. { not as efficient as returning in param for jvmimplicitpointertypes,
  192. but in the latter case the routines are harder to use from Java
  193. (especially for arrays), because the caller then manually has to
  194. allocate the instance/array of the right size }
  195. Result:=false;
  196. end;
  197. function tcpuparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  198. begin
  199. { all parameters are passed on the evaluation stack }
  200. result:=true;
  201. end;
  202. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;
  203. var
  204. parasize : longint;
  205. begin
  206. parasize:=0;
  207. { calculate the registers for the normal parameters }
  208. create_paraloc_info_intern(p,side,p.paras,parasize);
  209. { append the varargs }
  210. if assigned(varargspara) then
  211. begin
  212. if side=callerside then
  213. create_paraloc_info_intern(p,side,varargspara,parasize)
  214. else
  215. internalerror(2019021924);
  216. end;
  217. create_funcretloc_info(p,side);
  218. result:=parasize;
  219. end;
  220. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  221. var parasize:longint);
  222. var
  223. paraloc : pcgparalocation;
  224. i : integer;
  225. hp : tparavarsym;
  226. paracgsize : tcgsize;
  227. paraofs : longint;
  228. paradef : tdef;
  229. begin
  230. paraofs:=0;
  231. for i:=0 to paras.count-1 do
  232. begin
  233. hp:=tparavarsym(paras[i]);
  234. if push_copyout_param(hp.varspez,hp.vardef,p.proccalloption) then
  235. begin
  236. { passed via array reference (instead of creating a new array
  237. type for every single parameter, use java_jlobject) }
  238. paracgsize:=OS_ADDR;
  239. paradef:=ptruinttype;
  240. end
  241. else if push_addr_param(hp.varspez, hp.vardef,p.proccalloption) then
  242. begin
  243. paracgsize:=OS_ADDR;
  244. paradef:=cpointerdef.getreusable_no_free(hp.vardef);
  245. end
  246. else
  247. begin
  248. paracgsize:=def_cgsize(hp.vardef);
  249. if paracgsize=OS_NO then
  250. paracgsize:=OS_ADDR;
  251. paradef:=hp.vardef;
  252. end;
  253. paradef:=get_para_push_size(paradef);
  254. hp.paraloc[side].reset;
  255. hp.paraloc[side].size:=paracgsize;
  256. hp.paraloc[side].def:=paradef;
  257. hp.paraloc[side].alignment:=std_param_align;
  258. hp.paraloc[side].intsize:=tcgsize2size[paracgsize];
  259. paraloc:=hp.paraloc[side].add_location;
  260. { All parameters are passed on the evaluation stack, pushed from
  261. left to right (including self, if applicable). At the callee side,
  262. they're available as local variables 0..n-1 }
  263. paraloc^.loc:=LOC_REFERENCE;
  264. paraloc^.reference.offset:=paraofs;
  265. paraloc^.size:=paracgsize;
  266. paraloc^.def:=paradef;
  267. case side of
  268. callerside:
  269. begin
  270. paraloc^.loc:=LOC_REFERENCE;
  271. { we use a fake loc_reference to indicate the stack location;
  272. the offset (set above) will be used by ncal to order the
  273. parameters so they will be pushed in the right order }
  274. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  275. end;
  276. calleeside:
  277. begin
  278. paraloc^.loc:=LOC_REFERENCE;
  279. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  280. end;
  281. else
  282. ;
  283. end;
  284. { 2 slots for 64 bit integers and floats, 1 slot for the rest }
  285. inc(paraofs);
  286. end;
  287. parasize:=paraofs;
  288. end;
  289. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  290. var
  291. parasize : longint;
  292. begin
  293. parasize:=0;
  294. create_paraloc_info_intern(p,side,p.paras,parasize);
  295. { Create Function result paraloc }
  296. create_funcretloc_info(p,side);
  297. { We need to return the size allocated on the stack }
  298. result:=parasize;
  299. end;
  300. initialization
  301. ParaManager:=tcpuparamanager.create;
  302. end.