Skip to content

Commit 8d416c5

Browse files
committed
Clean-up regarding function and primitive types
1 parent a5152d3 commit 8d416c5

File tree

8 files changed

+66
-50
lines changed

8 files changed

+66
-50
lines changed

compiler/lib-wasm/curry.ml

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ open Code_generation
2424
module Make (Target : Target_sig.S) = struct
2525
open Target
2626

27-
let func_type n =
28-
{ W.params = List.init ~len:(n + 1) ~f:(fun _ -> Value.value)
29-
; result = [ Value.value ]
30-
}
31-
3227
let bind_parameters l =
3328
List.fold_left
3429
~f:(fun l x ->
@@ -102,7 +97,7 @@ module Make (Target : Target_sig.S) = struct
10297
let param_names = args @ [ f ] in
10398
let locals, body = function_body ~context ~param_names ~body in
10499
W.Function
105-
{ name; exported_name = None; typ = func_type 1; param_names; locals; body }
100+
{ name; exported_name = None; typ = Type.func_type 1; param_names; locals; body }
106101

107102
let curry_name n m = Printf.sprintf "curry_%d_%d" n m
108103

@@ -130,7 +125,7 @@ module Make (Target : Target_sig.S) = struct
130125
let param_names = [ x; f ] in
131126
let locals, body = function_body ~context ~param_names ~body in
132127
W.Function
133-
{ name; exported_name = None; typ = func_type 1; param_names; locals; body }
128+
{ name; exported_name = None; typ = Type.func_type 1; param_names; locals; body }
134129
:: functions
135130

136131
let curry ~arity ~name = curry ~arity arity ~name
@@ -174,7 +169,7 @@ module Make (Target : Target_sig.S) = struct
174169
let param_names = args @ [ f ] in
175170
let locals, body = function_body ~context ~param_names ~body in
176171
W.Function
177-
{ name; exported_name = None; typ = func_type 2; param_names; locals; body }
172+
{ name; exported_name = None; typ = Type.func_type 2; param_names; locals; body }
178173

179174
let cps_curry_name n m = Printf.sprintf "cps_curry_%d_%d" n m
180175

@@ -206,7 +201,7 @@ module Make (Target : Target_sig.S) = struct
206201
let param_names = [ x; cont; f ] in
207202
let locals, body = function_body ~context ~param_names ~body in
208203
W.Function
209-
{ name; exported_name = None; typ = func_type 2; param_names; locals; body }
204+
{ name; exported_name = None; typ = Type.func_type 2; param_names; locals; body }
210205
:: functions
211206

212207
let cps_curry ~arity ~name = cps_curry ~arity arity ~name
@@ -243,7 +238,13 @@ module Make (Target : Target_sig.S) = struct
243238
let param_names = l @ [ f ] in
244239
let locals, body = function_body ~context ~param_names ~body in
245240
W.Function
246-
{ name; exported_name = None; typ = func_type arity; param_names; locals; body }
241+
{ name
242+
; exported_name = None
243+
; typ = Type.primitive_type (arity + 1)
244+
; param_names
245+
; locals
246+
; body
247+
}
247248

248249
let cps_apply ~context ~arity ~name =
249250
assert (arity > 2);
@@ -271,7 +272,7 @@ module Make (Target : Target_sig.S) = struct
271272
(List.map ~f:(fun x -> `Var x) (List.tl l))
272273
in
273274
let* make_iterator =
274-
register_import ~name:"caml_apply_continuation" (Fun (func_type 0))
275+
register_import ~name:"caml_apply_continuation" (Fun (Type.primitive_type 1))
275276
in
276277
let iterate = Var.fresh_n "iterate" in
277278
let* () = store iterate (return (W.Call (make_iterator, [ args ]))) in
@@ -283,7 +284,13 @@ module Make (Target : Target_sig.S) = struct
283284
let param_names = l @ [ f ] in
284285
let locals, body = function_body ~context ~param_names ~body in
285286
W.Function
286-
{ name; exported_name = None; typ = func_type arity; param_names; locals; body }
287+
{ name
288+
; exported_name = None
289+
; typ = Type.primitive_type (arity + 1)
290+
; param_names
291+
; locals
292+
; body
293+
}
287294

288295
let dummy ~context ~cps ~arity ~name =
289296
let arity = if cps then arity + 1 else arity in
@@ -311,7 +318,13 @@ module Make (Target : Target_sig.S) = struct
311318
let param_names = l @ [ f ] in
312319
let locals, body = function_body ~context ~param_names ~body in
313320
W.Function
314-
{ name; exported_name = None; typ = func_type arity; param_names; locals; body }
321+
{ name
322+
; exported_name = None
323+
; typ = Type.func_type arity
324+
; param_names
325+
; locals
326+
; body
327+
}
315328

316329
let f ~context =
317330
IntMap.iter

compiler/lib-wasm/gc_target.ml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ module Type = struct
202202
]
203203
})
204204

205-
let func_type n =
206-
{ W.params = List.init ~len:(n + 1) ~f:(fun _ -> value); result = [ value ] }
205+
let primitive_type n =
206+
{ W.params = List.init ~len:n ~f:(fun _ -> value); result = [ value ] }
207+
208+
let func_type n = primitive_type (n + 1)
207209

208210
let function_type ~cps n =
209211
let n = if cps then n + 1 else n in
@@ -423,8 +425,6 @@ module Type = struct
423425
end
424426

425427
module Value = struct
426-
let value = Type.value
427-
428428
let block_type =
429429
let* t = Type.block_type in
430430
return (W.Ref { nullable = false; typ = Type t })
@@ -742,13 +742,13 @@ module Memory = struct
742742
let a = Code.Var.fresh_n "a" in
743743
let i = Code.Var.fresh_n "i" in
744744
block_expr
745-
{ params = []; result = [ Value.value ] }
745+
{ params = []; result = [ Type.value ] }
746746
(let* () = store a e in
747747
let* () = store ~typ:I32 i (Value.int_val e') in
748748
let* () =
749749
drop
750750
(block_expr
751-
{ params = []; result = [ Value.value ] }
751+
{ params = []; result = [ Type.value ] }
752752
(let* block = Type.block_type in
753753
let* a = load a in
754754
let* e =
@@ -778,7 +778,7 @@ module Memory = struct
778778
(let* () =
779779
drop
780780
(block_expr
781-
{ params = []; result = [ Value.value ] }
781+
{ params = []; result = [ Type.value ] }
782782
(let* block = Type.block_type in
783783
let* a = load a in
784784
let* () =
@@ -839,7 +839,7 @@ module Memory = struct
839839
let* () =
840840
drop
841841
(block_expr
842-
{ params = []; result = [ Value.value ] }
842+
{ params = []; result = [ Type.value ] }
843843
(let* e =
844844
if_match
845845
~typ:(Some (W.Ref { nullable = false; typ = Type fun_ty }))
@@ -1403,7 +1403,7 @@ let () =
14031403
let arity = List.length args in
14041404
(* [Type.func_type] counts one additional argument for the closure environment (absent
14051405
here) *)
1406-
let* f = register_import ~name (Fun (Type.func_type (arity - 1))) in
1406+
let* f = register_import ~name (Fun (Type.primitive_type arity)) in
14071407
let args = List.map ~f:transl_prim_arg args in
14081408
let* args = expression_list Fun.id args in
14091409
return (W.Call (f, args))
@@ -1667,19 +1667,19 @@ let externref = W.Ref { nullable = true; typ = Extern }
16671667

16681668
let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
16691669
let* js_tag = register_import ~name:"javascript_exception" (Tag externref) in
1670-
let* ocaml_tag = register_import ~name:"ocaml_exception" (Tag Value.value) in
1670+
let* ocaml_tag = register_import ~name:"ocaml_exception" (Tag Type.value) in
16711671
let* f =
16721672
register_import
16731673
~name:"caml_wrap_exception"
1674-
(Fun { params = [ externref ]; result = [ Value.value ] })
1674+
(Fun { params = [ externref ]; result = [ Type.value ] })
16751675
in
16761676
block
16771677
{ params = []; result = result_typ }
16781678
(let* () =
16791679
store
16801680
x
16811681
(block_expr
1682-
{ params = []; result = [ Value.value ] }
1682+
{ params = []; result = [ Type.value ] }
16831683
(let* exn =
16841684
block_expr
16851685
{ params = []; result = [ externref ] }
@@ -1690,7 +1690,7 @@ let handle_exceptions ~result_typ ~fall_through ~context body x exn_handler =
16901690
~result_typ:[ externref ]
16911691
~fall_through:`Skip
16921692
~context:(`Skip :: `Skip :: `Catch :: context))
1693-
[ ocaml_tag, 1, Value.value; js_tag, 0, externref ]
1693+
[ ocaml_tag, 1, Type.value; js_tag, 0, externref ]
16941694
in
16951695
instr (W.Push e))
16961696
in

compiler/lib-wasm/generate.ml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@ module Generate (Target : Target_sig.S) = struct
5454

5555
let repr_type r =
5656
match r with
57-
| Value -> Value.value
57+
| Value -> Type.value
5858
| Float -> F64
5959
| Int32 -> I32
6060
| Nativeint -> I32
6161
| Int64 -> I64
6262

63-
let specialized_func_type (params, result) =
63+
let specialized_primitive_type (params, result) =
6464
{ W.params = List.map ~f:repr_type params; result = [ repr_type result ] }
6565

6666
let box_value r e =
@@ -110,9 +110,6 @@ module Generate (Target : Target_sig.S) = struct
110110
];
111111
h
112112

113-
let func_type n =
114-
{ W.params = List.init ~len:n ~f:(fun _ -> Value.value); result = [ Value.value ] }
115-
116113
let float_bin_op' op f g =
117114
Memory.box_float (op (Memory.unbox_float f) (Memory.unbox_float g))
118115

@@ -664,7 +661,7 @@ module Generate (Target : Target_sig.S) = struct
664661
let name = Primitive.resolve name in
665662
try
666663
let typ = Hashtbl.find specialized_primitives name in
667-
let* f = register_import ~name (Fun (specialized_func_type typ)) in
664+
let* f = register_import ~name (Fun (specialized_primitive_type typ)) in
668665
let rec loop acc arg_typ l =
669666
match arg_typ, l with
670667
| [], [] -> box_value (snd typ) (return (W.Call (f, List.rev acc)))
@@ -675,7 +672,9 @@ module Generate (Target : Target_sig.S) = struct
675672
in
676673
loop [] (fst typ) l
677674
with Not_found ->
678-
let* f = register_import ~name (Fun (func_type (List.length l))) in
675+
let* f =
676+
register_import ~name (Fun (Type.primitive_type (List.length l)))
677+
in
679678
let rec loop acc l =
680679
match l with
681680
| [] -> return (W.Call (f, List.rev acc))
@@ -949,7 +948,7 @@ module Generate (Target : Target_sig.S) = struct
949948
instr (Br_table (e, List.map ~f:dest l, dest a.(len - 1)))
950949
| Raise (x, _) -> (
951950
let* e = load x in
952-
let* tag = register_import ~name:exception_name (Tag Value.value) in
951+
let* tag = register_import ~name:exception_name (Tag Type.value) in
953952
match fall_through with
954953
| `Catch -> instr (Push e)
955954
| `Block _ | `Return | `Skip -> (
@@ -1034,7 +1033,7 @@ module Generate (Target : Target_sig.S) = struct
10341033
wrap_with_handlers
10351034
p
10361035
pc
1037-
~result_typ:[ Value.value ]
1036+
~result_typ:[ Type.value ]
10381037
~fall_through:`Return
10391038
~context:[]
10401039
(fun ~result_typ ~fall_through ~context ->
@@ -1056,15 +1055,18 @@ module Generate (Target : Target_sig.S) = struct
10561055
| None -> Option.map ~f:(fun name -> name ^ ".init") unit_name
10571056
| Some _ -> None)
10581057
; param_names
1059-
; typ = func_type param_count
1058+
; typ =
1059+
(match name_opt with
1060+
| None -> Type.primitive_type param_count
1061+
| Some _ -> Type.func_type (param_count - 1))
10601062
; locals
10611063
; body
10621064
}
10631065
:: acc
10641066

10651067
let init_function ~context ~to_link =
10661068
let name = Code.Var.fresh_n "initialize" in
1067-
let typ = { W.params = []; result = [ Value.value ] } in
1069+
let typ = { W.params = []; result = [ Type.value ] } in
10681070
let locals, body =
10691071
function_body
10701072
~context
@@ -1230,7 +1232,7 @@ let fix_switch_branches p =
12301232
p.blocks;
12311233
!p'
12321234

1233-
let start () = make_context ~value_type:Gc_target.Value.value
1235+
let start () = make_context ~value_type:Gc_target.Type.value
12341236

12351237
let f ~context ~unit_name p ~live_vars ~in_cps ~deadcode_sentinal ~debug =
12361238
let p = if effects_cps () then fix_switch_branches p else p in

compiler/lib-wasm/target_sig.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ module type S = sig
9696
val unbox_nativeint : expression -> expression
9797
end
9898

99-
module Value : sig
99+
module Type : sig
100100
val value : Wasm_ast.value_type
101101

102+
val func_type : int -> Wasm_ast.func_type
103+
104+
val primitive_type : int -> Wasm_ast.func_type
105+
end
106+
107+
module Value : sig
102108
val unit : expression
103109

104110
val val_int : expression -> expression

runtime/wasm/domain.wat

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
(func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq))))
2424

2525
(type $block (array (mut (ref eq))))
26-
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
27-
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
2826

2927
(func (export "caml_atomic_cas")
3028
(param $ref (ref eq)) (param $o (ref eq)) (param $n (ref eq))

runtime/wasm/effect.wat

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
(type $block (array (mut (ref eq))))
4646
(type $bytes (array (mut i8)))
47+
(type $primitive (func (param (ref eq) (ref eq)) (result (ref eq))))
4748
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
4849
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
4950
(type $function_3
@@ -642,7 +643,7 @@
642643
(throw $ocaml_exception (local.get $exn)))
643644

644645
(global $caml_trampoline_ref (export "caml_trampoline_ref")
645-
(mut (ref null $function_1)) (ref.null $function_1))
646+
(mut (ref null $primitive)) (ref.null $primitive))
646647

647648
(func $caml_pop_fiber (result (ref eq))
648649
(local $f (ref $cps_fiber))

runtime/wasm/jslib.wat

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@
104104
(type $float_array (array (mut f64)))
105105
(type $bytes (array (mut i8)))
106106
(type $js (struct (field anyref)))
107-
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
108-
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
109-
(type $function_2
110-
(func (param (ref eq) (ref eq) (ref eq)) (result (ref eq))))
111-
(type $cps_closure (sub (struct (field (ref $function_2)))))
112107

113108
(func $wrap (export "wrap") (param anyref) (result (ref eq))
114109
(block $is_eq (result (ref eq))

runtime/wasm/obj.wat

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
(import "effect" "caml_is_continuation"
2525
(func $caml_is_continuation (param (ref eq)) (result i32)))
2626
(import "effect" "caml_trampoline_ref"
27-
(global $caml_trampoline_ref (mut (ref null $function_1))))
27+
(global $caml_trampoline_ref (mut (ref null $primitive))))
2828

2929
(type $block (array (mut (ref eq))))
3030
(type $bytes (array (mut i8)))
3131
(type $float (struct (field f64)))
3232
(type $float_array (array (mut f64)))
33+
(type $primitive (func (param (ref eq) (ref eq)) (result (ref eq))))
3334
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
3435
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
3536
(type $closure_last_arg
@@ -460,7 +461,7 @@
460461
(struct.get $closure 0
461462
(br_on_cast_fail $cps (ref eq) (ref $closure)
462463
(local.get $f))))))
463-
(return_call_ref $function_1
464+
(return_call_ref $primitive
464465
(local.get $f)
465466
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $x))
466467
(ref.as_non_null (global.get $caml_trampoline_ref))))
@@ -480,7 +481,7 @@
480481
(call $caml_callback_1 (local.get $f) (local.get $x))
481482
(local.get $y)))
482483
(else
483-
(return_call_ref $function_1
484+
(return_call_ref $primitive
484485
(local.get $f)
485486
(array.new_fixed $block 3 (ref.i31 (i32.const 0))
486487
(local.get $x) (local.get $y))

0 commit comments

Comments
 (0)