cpupara.pas 13 KB

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