Skip to content

Commit dfbb093

Browse files
committed
Clean-up regarding function and primitive types
1 parent dcf9970 commit dfbb093

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
@@ -56,13 +56,13 @@ module Generate (Target : Target_sig.S) = struct
5656

5757
let repr_type r =
5858
match r with
59-
| Value -> Value.value
59+
| Value -> Type.value
6060
| Float -> F64
6161
| Int32 -> I32
6262
| Nativeint -> I32
6363
| Int64 -> I64
6464

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

6868
let box_value r e =
@@ -112,9 +112,6 @@ module Generate (Target : Target_sig.S) = struct
112112
];
113113
h
114114

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

@@ -666,7 +663,7 @@ module Generate (Target : Target_sig.S) = struct
666663
let name = Primitive.resolve name in
667664
try
668665
let typ = Hashtbl.find specialized_primitives name in
669-
let* f = register_import ~name (Fun (specialized_func_type typ)) in
666+
let* f = register_import ~name (Fun (specialized_primitive_type typ)) in
670667
let rec loop acc arg_typ l =
671668
match arg_typ, l with
672669
| [], [] -> box_value (snd typ) (return (W.Call (f, List.rev acc)))
@@ -677,7 +674,9 @@ module Generate (Target : Target_sig.S) = struct
677674
in
678675
loop [] (fst typ) l
679676
with Not_found ->
680-
let* f = register_import ~name (Fun (func_type (List.length l))) in
677+
let* f =
678+
register_import ~name (Fun (Type.primitive_type (List.length l)))
679+
in
681680
let rec loop acc l =
682681
match l with
683682
| [] -> return (W.Call (f, List.rev acc))
@@ -951,7 +950,7 @@ module Generate (Target : Target_sig.S) = struct
951950
instr (Br_table (e, List.map ~f:dest l, dest a.(len - 1)))
952951
| Raise (x, _) -> (
953952
let* e = load x in
954-
let* tag = register_import ~name:exception_name (Tag Value.value) in
953+
let* tag = register_import ~name:exception_name (Tag Type.value) in
955954
match fall_through with
956955
| `Catch -> instr (Push e)
957956
| `Block _ | `Return | `Skip -> (
@@ -1036,7 +1035,7 @@ module Generate (Target : Target_sig.S) = struct
10361035
wrap_with_handlers
10371036
p
10381037
pc
1039-
~result_typ:[ Value.value ]
1038+
~result_typ:[ Type.value ]
10401039
~fall_through:`Return
10411040
~context:[]
10421041
(fun ~result_typ ~fall_through ~context ->
@@ -1058,15 +1057,18 @@ module Generate (Target : Target_sig.S) = struct
10581057
| None -> Option.map ~f:(fun name -> name ^ ".init") unit_name
10591058
| Some _ -> None)
10601059
; param_names
1061-
; typ = func_type param_count
1060+
; typ =
1061+
(match name_opt with
1062+
| None -> Type.primitive_type param_count
1063+
| Some _ -> Type.func_type (param_count - 1))
10621064
; locals
10631065
; body
10641066
}
10651067
:: acc
10661068

10671069
let init_function ~context ~to_link =
10681070
let name = Code.Var.fresh_n "initialize" in
1069-
let typ = { W.params = []; result = [ Value.value ] } in
1071+
let typ = { W.params = []; result = [ Type.value ] } in
10701072
let locals, body =
10711073
function_body
10721074
~context
@@ -1232,7 +1234,7 @@ let fix_switch_branches p =
12321234
p.blocks;
12331235
!p'
12341236

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

12371239
let f ~context ~unit_name p ~live_vars ~in_cps ~deadcode_sentinal ~debug =
12381240
let t = Timer.make () 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
@@ -45,6 +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 $primitive_2 (func (param (ref eq) (ref eq)) (result (ref eq))))
4849
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
4950
(type $function_3
5051
(func (param (ref eq) (ref eq) (ref eq) (ref eq)) (result (ref eq))))
@@ -640,7 +641,7 @@
640641
(throw $ocaml_exception (local.get $exn)))
641642

642643
(global $caml_trampoline_ref (export "caml_trampoline_ref")
643-
(mut (ref null $function_1)) (ref.null $function_1))
644+
(mut (ref null $primitive_2)) (ref.null $primitive_2))
644645

645646
(func $caml_pop_fiber (result (ref eq))
646647
(local $f (ref $cps_fiber))

runtime/wasm/jslib.wat

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

115110
(func $wrap (export "wrap") (param anyref) (result (ref eq))
116111
(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,13 +24,14 @@
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_2))))
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)))
3333
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
34+
(type $primitive_2 (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
3637
(sub $closure (struct (;(field i32);) (field (ref $function_1)))))
@@ -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_2
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_2
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)