cpupara.pas 12 KB

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