cpupara.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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,
  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: {$ifndef VER3_0}tcpuregisterarray{$else}array [0..0] of tsuperregister{$endif} = (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. begin
  213. paraofs:=0;
  214. for i:=0 to paras.count-1 do
  215. begin
  216. hp:=tparavarsym(paras[i]);
  217. if push_copyout_param(hp.varspez,hp.vardef,p.proccalloption) then
  218. begin
  219. { passed via array reference (instead of creating a new array
  220. type for every single parameter, use java_jlobject) }
  221. paracgsize:=OS_ADDR;
  222. paradef:=ptruinttype;
  223. end
  224. else if push_addr_param(hp.varspez, hp.vardef,p.proccalloption) 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 }
  246. paraloc^.loc:=LOC_REFERENCE;
  247. paraloc^.reference.offset:=paraofs;
  248. paraloc^.size:=paracgsize;
  249. paraloc^.def:=paradef;
  250. case side of
  251. callerside:
  252. begin
  253. paraloc^.loc:=LOC_REFERENCE;
  254. { we use a fake loc_reference to indicate the stack location;
  255. the offset (set above) will be used by ncal to order the
  256. parameters so they will be pushed in the right order }
  257. paraloc^.reference.index:=NR_EVAL_STACK_BASE;
  258. end;
  259. calleeside:
  260. begin
  261. paraloc^.loc:=LOC_REFERENCE;
  262. paraloc^.reference.index:=NR_STACK_POINTER_REG;
  263. end;
  264. else
  265. ;
  266. end;
  267. inc(paraofs);
  268. end;
  269. parasize:=paraofs;
  270. end;
  271. function tcpuparamanager.is_singleton_scalar_record(def: trecorddef): boolean;
  272. var
  273. i,fields: Integer;
  274. begin
  275. if not (def.size in [1,2,4,8]) then
  276. exit(false);
  277. fields:=0;
  278. for i:=0 to def.symtable.symlist.count-1 do
  279. begin
  280. if (tsym(def.symtable.symlist[i]).typ<>fieldvarsym) or
  281. (sp_static in tsym(def.symtable.symlist[i]).symoptions) then
  282. continue;
  283. if assigned(tfieldvarsym(def.symtable.symlist[i]).vardef) then
  284. begin
  285. Inc(fields);
  286. if fields>1 then
  287. exit(false);
  288. { search recursively }
  289. if (tstoreddef(tfieldvarsym(def.symtable.symlist[i]).vardef).typ=recorddef) and
  290. not is_singleton_scalar_record(trecorddef(tfieldvarsym(def.symtable.symlist[i]).vardef)) then
  291. exit(false);
  292. if (tstoreddef(tfieldvarsym(def.symtable.symlist[i]).vardef).typ=arraydef) and
  293. not is_singleton_scalar_array(tarraydef(tfieldvarsym(def.symtable.symlist[i]).vardef)) then
  294. exit(false);
  295. end;
  296. end;
  297. result:=(fields=1);
  298. end;
  299. function tcpuparamanager.is_singleton_scalar_array(def:tarraydef):boolean;
  300. begin
  301. result:=(def.size in [1,2,4,8]) and (def.elecount=1);
  302. end;
  303. function tcpuparamanager.create_paraloc_info(p : tabstractprocdef; side: tcallercallee):longint;
  304. var
  305. parasize : longint;
  306. begin
  307. parasize:=0;
  308. create_paraloc_info_intern(p,side,p.paras,parasize);
  309. { Create Function result paraloc }
  310. create_funcretloc_info(p,side);
  311. { We need to return the size allocated on the stack }
  312. result:=parasize;
  313. end;
  314. initialization
  315. ParaManager:=tcpuparamanager.create;
  316. end.