cpupara.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 has_strict_proc_signature: boolean; override;
  29. function get_saved_registers_int(calloption: tproccalloption): tcpuregisterarray;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 ret_in_param(def:tdef;pd:tabstractprocdef):boolean;override;
  33. function push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;override;
  34. function create_paraloc_info(p : TAbstractProcDef; side: tcallercallee):longint;override;
  35. function create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;override;
  36. function get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;override;
  37. { true if the location in paraloc can be reused as localloc }
  38. function param_use_paraloc(const cgpara: tcgpara): boolean; override;
  39. function is_stack_paraloc(paraloc: pcgparalocation): boolean;override;
  40. private
  41. procedure create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee; paras: tparalist;
  42. var parasize:longint);
  43. { true if the record recursively (including through nested records and
  44. arrays) contains just a single scalar value }
  45. function is_singleton_scalar_record(def:trecorddef):boolean;
  46. function is_singleton_scalar_array(def:tarraydef):boolean;
  47. end;
  48. implementation
  49. uses
  50. cutils,verbose,systems,
  51. defutil,wasmdef,tgcpu,
  52. aasmcpu,
  53. hlcgobj;
  54. function tcpuparamanager.has_strict_proc_signature: boolean;
  55. begin
  56. Result:=true;
  57. end;
  58. function tcpuparamanager.get_saved_registers_int(calloption: tproccalloption): tcpuregisterarray;
  59. const
  60. { dummy, not used for WebAssembly }
  61. saved_regs: tcpuregisterarray = (RS_NO);
  62. begin
  63. result:=saved_regs;
  64. end;
  65. function tcpuparamanager.keep_para_array_range(varspez: tvarspez; def: tdef; calloption: tproccalloption): boolean;
  66. begin
  67. { even though these don't need a high parameter (see push_high_param),
  68. we do have to keep the original parameter's array length because it's
  69. used by the compiler (to determine the size of the array to construct
  70. to pass to an array of const parameter) }
  71. if not is_array_of_const(def) then
  72. result:=inherited
  73. else
  74. result:=true;
  75. end;
  76. { true if a parameter is too large to copy and only the address is pushed }
  77. function tcpuparamanager.push_addr_param(varspez:tvarspez;def : tdef;calloption : tproccalloption) : boolean;
  78. begin
  79. result:=false;
  80. { var,out,constref always require address }
  81. if varspez in [vs_var,vs_out,vs_constref] then
  82. begin
  83. result:=true;
  84. exit;
  85. end;
  86. { Only vs_const, vs_value here }
  87. case def.typ of
  88. variantdef,
  89. formaldef :
  90. result:=true;
  91. recorddef :
  92. result:=(varspez=vs_const) or not is_singleton_scalar_record(trecorddef(def));
  93. arraydef :
  94. begin
  95. result:=(tarraydef(def).highrange>=tarraydef(def).lowrange) or
  96. is_open_array(def) or
  97. is_array_of_const(def) or
  98. is_array_constructor(def);
  99. end;
  100. objectdef :
  101. result:=is_object(def);
  102. stringdef :
  103. result:= (tstringdef(def).stringtype in [st_shortstring,st_longstring]);
  104. procvardef :
  105. result:=not(calloption in cdecl_pocalls) and not tprocvardef(def).is_addressonly;
  106. setdef :
  107. result:=not is_smallset(def);
  108. else
  109. ;
  110. end;
  111. end;
  112. function tcpuparamanager.ret_in_param(def:tdef;pd:tabstractprocdef):boolean;
  113. begin
  114. { This handles all managed types, including COM interfaces and Variants }
  115. if handle_common_ret_in_param(def,pd,result) then
  116. exit;
  117. if (def.typ=recorddef) and is_singleton_scalar_record(trecorddef(def)) then
  118. result:=false
  119. else
  120. result:=inherited;
  121. end;
  122. function tcpuparamanager.push_size(varspez: tvarspez; def: tdef; calloption: tproccalloption): longint;
  123. begin
  124. { all aggregate types are emulated using indirect pointer types }
  125. result:=inherited;
  126. end;
  127. function tcpuparamanager.get_funcretloc(p : tabstractprocdef; side: tcallercallee; forcetempdef: tdef): tcgpara;
  128. var
  129. paraloc : pcgparalocation;
  130. retcgsize : tcgsize;
  131. begin
  132. result.init;
  133. result.alignment:=get_para_align(p.proccalloption);
  134. if not assigned(forcetempdef) then
  135. result.def:=p.returndef
  136. else
  137. begin
  138. result.def:=forcetempdef;
  139. result.temporary:=true;
  140. end;
  141. result.def:=get_para_push_size(result.def);
  142. { void has no location }
  143. if is_void(result.def) then
  144. begin
  145. paraloc:=result.add_location;
  146. result.size:=OS_NO;
  147. result.intsize:=0;
  148. paraloc^.size:=OS_NO;
  149. paraloc^.def:=voidtype;
  150. paraloc^.loc:=LOC_VOID;
  151. exit;
  152. end;
  153. { Constructors return self instead of a boolean }
  154. if (p.proctypeoption=potype_constructor) then
  155. begin
  156. retcgsize:=OS_INT;
  157. result.intsize:=sizeof(pint);
  158. end
  159. //todo: wasm should have the similar
  160. {else if jvmimplicitpointertype(result.def) then
  161. begin
  162. retcgsize:=OS_ADDR;
  163. result.def:=cpointerdef.getreusable_no_free(result.def);
  164. end}
  165. else
  166. begin
  167. retcgsize:=def_cgsize(result.def);
  168. result.intsize:=result.def.size;
  169. end;
  170. result.size:=retcgsize;
  171. paraloc:=result.add_location;
  172. { all values are returned on the evaluation stack }
  173. paraloc^.loc:=LOC_REFERENCE;
  174. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  175. paraloc^.reference.offset:=0;
  176. paraloc^.size:=result.size;
  177. paraloc^.def:=result.def;
  178. end;
  179. function tcpuparamanager.param_use_paraloc(const cgpara: tcgpara): boolean;
  180. begin
  181. { all parameters are copied to the linear stack, so that their address
  182. can be taken }
  183. result:=false;
  184. end;
  185. function tcpuparamanager.is_stack_paraloc(paraloc: pcgparalocation): boolean;
  186. begin
  187. { all parameters are passed on the evaluation stack }
  188. result:=true;
  189. end;
  190. function tcpuparamanager.create_varargs_paraloc_info(p : tabstractprocdef; side: tcallercallee; varargspara:tvarargsparalist):longint;
  191. var
  192. parasize : longint;
  193. begin
  194. parasize:=0;
  195. { calculate the registers for the normal parameters }
  196. create_paraloc_info_intern(p,side,p.paras,parasize);
  197. { append the varargs }
  198. if assigned(varargspara) then
  199. begin
  200. if side=callerside then
  201. create_paraloc_info_intern(p,side,varargspara,parasize)
  202. else
  203. internalerror(2019021924);
  204. end;
  205. create_funcretloc_info(p,side);
  206. result:=parasize;
  207. end;
  208. procedure tcpuparamanager.create_paraloc_info_intern(p : tabstractprocdef; side: tcallercallee;paras:tparalist;
  209. var parasize:longint);
  210. var
  211. paraloc : pcgparalocation;
  212. i : integer;
  213. hp : tparavarsym;
  214. paracgsize : tcgsize;
  215. paraofs : longint;
  216. paradef : tdef;
  217. wbt : TWasmBasicType;
  218. begin
  219. paraofs:=0;
  220. for i:=0 to paras.count-1 do
  221. begin
  222. hp:=tparavarsym(paras[i]);
  223. if push_copyout_param(hp.varspez,hp.vardef,p.proccalloption) then
  224. begin
  225. { passed via array reference (instead of creating a new array
  226. type for every single parameter, use java_jlobject) }
  227. paracgsize:=OS_ADDR;
  228. paradef:=ptruinttype;
  229. end
  230. else if push_addr_param(hp.varspez, hp.vardef,p.proccalloption) then
  231. begin
  232. paracgsize:=OS_ADDR;
  233. paradef:=cpointerdef.getreusable_no_free(hp.vardef);
  234. end
  235. else
  236. begin
  237. if (hp.vardef.typ=recorddef) and
  238. (trecorddef(hp.vardef).contains_float_field) and
  239. defToWasmBasic(hp.vardef,wbt) then
  240. begin
  241. case wbt of
  242. wbt_f32:
  243. paracgsize:=OS_F32;
  244. wbt_f64:
  245. paracgsize:=OS_F64;
  246. else
  247. internalerror(2021101401);
  248. end;
  249. end
  250. else
  251. paracgsize:=def_cgsize(hp.vardef);
  252. if paracgsize=OS_NO then
  253. paracgsize:=OS_ADDR;
  254. paradef:=hp.vardef;
  255. end;
  256. paradef:=get_para_push_size(paradef);
  257. hp.paraloc[side].reset;
  258. hp.paraloc[side].size:=paracgsize;
  259. hp.paraloc[side].def:=paradef;
  260. hp.paraloc[side].alignment:=std_param_align;
  261. hp.paraloc[side].intsize:=tcgsize2size[paracgsize];
  262. paraloc:=hp.paraloc[side].add_location;
  263. { All parameters are passed on the evaluation stack, pushed from
  264. left to right (including self, if applicable). At the callee side,
  265. they're available as local variables 0..n-1 }
  266. paraloc^.loc:=LOC_REFERENCE;
  267. paraloc^.reference.offset:=paraofs;
  268. paraloc^.size:=paracgsize;
  269. paraloc^.def:=paradef;
  270. case side of
  271. callerside:
  272. begin
  273. paraloc^.loc:=LOC_REFERENCE;
  274. { we use a fake loc_reference to indicate the stack location;
  275. the offset (set above) will be used by ncal to order the
  276. parameters so they will be pushed in the right order }
  277. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  278. end;
  279. calleeside:
  280. begin
  281. paraloc^.loc:=LOC_REFERENCE;
  282. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  283. end;
  284. else
  285. ;
  286. end;
  287. inc(paraofs);
  288. end;
  289. parasize:=paraofs;
  290. end;
  291. function tcpuparamanager.is_singleton_scalar_record(def: trecorddef): boolean;
  292. var
  293. i,fields: Integer;
  294. begin
  295. if not (def.size in [1,2,4,8]) then
  296. exit(false);
  297. fields:=0;
  298. for i:=0 to def.symtable.symlist.count-1 do
  299. begin
  300. if (tsym(def.symtable.symlist[i]).typ<>fieldvarsym) or
  301. (sp_static in tsym(def.symtable.symlist[i]).symoptions) then
  302. continue;
  303. if assigned(tfieldvarsym(def.symtable.symlist[i]).vardef) then
  304. begin
  305. Inc(fields);
  306. if fields>1 then
  307. exit(false);
  308. { search recursively }
  309. if (tstoreddef(tfieldvarsym(def.symtable.symlist[i]).vardef).typ=recorddef) and
  310. not is_singleton_scalar_record(trecorddef(tfieldvarsym(def.symtable.symlist[i]).vardef)) then
  311. exit(false);
  312. if (tstoreddef(tfieldvarsym(def.symtable.symlist[i]).vardef).typ=arraydef) and
  313. not is_singleton_scalar_array(tarraydef(tfieldvarsym(def.symtable.symlist[i]).vardef)) then
  314. exit(false);
  315. end;
  316. end;
  317. result:=(fields=1);
  318. end;
  319. function tcpuparamanager.is_singleton_scalar_array(def:tarraydef):boolean;
  320. begin
  321. result:=(def.size in [1,2,4,8]) and (def.elecount=1);
  322. end;
  323. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  324. var
  325. parasize : longint;
  326. begin
  327. parasize:=0;
  328. create_paraloc_info_intern(p,side,p.paras,parasize);
  329. { Create Function result paraloc }
  330. create_funcretloc_info(p,side);
  331. { We need to return the size allocated on the stack }
  332. result:=parasize;
  333. end;
  334. initialization
  335. ParaManager:=tcpuparamanager.create;
  336. end.