Skip to content

Commit 2624a97

Browse files
committed
Clean-up: closures will not explicitly contain their arity
1 parent e6c8a9a commit 2624a97

File tree

3 files changed

+19
-59
lines changed

3 files changed

+19
-59
lines changed

compiler/lib-wasm/gc_target.ml

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ open Code_generation
2222

2323
type expression = Wasm_ast.expression Code_generation.t
2424

25-
let include_closure_arity = false
26-
2725
module Type = struct
2826
let value = W.Ref { nullable = false; typ = Eq }
2927

@@ -215,13 +213,7 @@ module Type = struct
215213
let closure_common_fields ~cps =
216214
let* fun_ty = function_type ~cps 1 in
217215
return
218-
(let function_pointer =
219-
[ { W.mut = false; typ = W.Value (Ref { nullable = false; typ = Type fun_ty }) }
220-
]
221-
in
222-
if include_closure_arity
223-
then { W.mut = false; typ = W.Value I32 } :: function_pointer
224-
else function_pointer)
216+
[ { W.mut = false; typ = W.Value (Ref { nullable = false; typ = Type fun_ty }) } ]
225217

226218
let closure_type_1 ~cps =
227219
register_type
@@ -807,9 +799,9 @@ module Memory = struct
807799
let set_field e idx e' = wasm_array_set e (Arith.const (Int32.of_int (idx + 1))) e'
808800

809801
let env_start arity =
810-
if arity = 0
811-
then 1
812-
else (if include_closure_arity then 1 else 0) + if arity = 1 then 1 else 2
802+
match arity with
803+
| 0 | 1 -> 1
804+
| _ -> 2
813805

814806
let load_function_pointer ~cps ~arity ?(skip_cast = false) closure =
815807
let arity = if cps then arity - 1 else arity in
@@ -1082,15 +1074,9 @@ module Closure = struct
10821074
{ mut = false; typ = Type.value }
10831075
(W.StructNew
10841076
( typ
1085-
, if arity = 0
1086-
then [ W.RefFunc f ]
1087-
else
1088-
let code_pointers =
1089-
if arity = 1 then [ W.RefFunc f ] else [ RefFunc curry_fun; RefFunc f ]
1090-
in
1091-
if include_closure_arity
1092-
then Const (I32 (Int32.of_int arity)) :: code_pointers
1093-
else code_pointers ))
1077+
, match arity with
1078+
| 0 | 1 -> [ W.RefFunc f ]
1079+
| _ -> [ RefFunc curry_fun; RefFunc f ] ))
10941080
in
10951081
return (W.GlobalGet name)
10961082
else
@@ -1111,17 +1097,9 @@ module Closure = struct
11111097
return
11121098
(W.StructNew
11131099
( typ
1114-
, (if arity = 0
1115-
then [ W.RefFunc f ]
1116-
else
1117-
let code_pointers =
1118-
if arity = 1
1119-
then [ W.RefFunc f ]
1120-
else [ RefFunc curry_fun; RefFunc f ]
1121-
in
1122-
if include_closure_arity
1123-
then W.Const (I32 (Int32.of_int arity)) :: code_pointers
1124-
else code_pointers)
1100+
, (match arity with
1101+
| 0 | 1 -> [ W.RefFunc f ]
1102+
| _ -> [ RefFunc curry_fun; RefFunc f ])
11251103
@ l ))
11261104
| (g, _) :: _ as functions ->
11271105
let function_count = List.length functions in
@@ -1154,14 +1132,9 @@ module Closure = struct
11541132
return
11551133
(W.StructNew
11561134
( typ
1157-
, (let code_pointers =
1158-
if arity = 1
1159-
then [ W.RefFunc f ]
1160-
else [ RefFunc curry_fun; RefFunc f ]
1161-
in
1162-
if include_closure_arity
1163-
then W.Const (I32 (Int32.of_int arity)) :: code_pointers
1164-
else code_pointers)
1135+
, (if arity = 1
1136+
then [ W.RefFunc f ]
1137+
else [ RefFunc curry_fun; RefFunc f ])
11651138
@ [ env ] ))
11661139
in
11671140
if is_last_fun functions f
@@ -1247,13 +1220,7 @@ module Closure = struct
12471220
in
12481221
let* closure = Memory.wasm_cast cl_ty (load closure) in
12491222
let* arg = load arg in
1250-
let closure_contents = [ W.RefFunc f; closure; arg ] in
1251-
return
1252-
(W.StructNew
1253-
( ty
1254-
, if include_closure_arity
1255-
then Const (I32 1l) :: closure_contents
1256-
else closure_contents ))
1223+
return (W.StructNew (ty, [ W.RefFunc f; closure; arg ]))
12571224

12581225
let curry_load ~cps ~arity m closure =
12591226
let m = m + 1 in
@@ -1283,12 +1250,7 @@ module Closure = struct
12831250
then [ W.RefFunc dummy_fun; RefNull (Type cl_typ) ]
12841251
else [ RefFunc curry_fun; RefFunc dummy_fun; RefNull (Type cl_typ) ]
12851252
in
1286-
return
1287-
(W.StructNew
1288-
( ty
1289-
, if include_closure_arity
1290-
then Const (I32 1l) :: closure_contents
1291-
else closure_contents ))
1253+
return (W.StructNew (ty, closure_contents))
12921254
end
12931255

12941256
module Math = struct

runtime/wasm/effect.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
(type $block (array (mut (ref eq))))
4646
(type $bytes (array (mut i8)))
4747
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
48-
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
48+
(type $closure (sub (struct (field (ref $function_1)))))
4949
(type $function_3
5050
(func (param (ref eq) (ref eq) (ref eq) (ref eq)) (result (ref eq))))
5151
(type $closure_3

runtime/wasm/obj.wat

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@
3434
(type $float (struct (field f64)))
3535
(type $float_array (array (mut f64)))
3636
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
37-
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
38-
(type $closure_last_arg
39-
(sub $closure (struct (;(field i32);) (field (ref $function_1)))))
40-
(type $function_2
41-
(func (param (ref eq) (ref eq) (ref eq)) (result (ref eq))))
37+
(type $closure (sub (struct (field (ref $function_1)))))
38+
(type $closure_last_arg (sub $closure (struct (field (ref $function_1)))))
39+
(type $function_2 (func (param (ref eq) (ref eq) (ref eq)) (result (ref eq))))
4240
(type $cps_closure (sub (struct (field (ref $function_2)))))
4341
(type $cps_closure_last_arg
4442
(sub $cps_closure (struct (field (ref $function_2)))))

0 commit comments

Comments
 (0)