Skip to content

Commit fe81bbd

Browse files
committed
feat: Implement additional gc instructions
1 parent b195f5b commit fe81bbd

File tree

8 files changed

+85
-34
lines changed

8 files changed

+85
-34
lines changed

src/expression.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,18 @@ caml_binaryen_array_new_data(value _module, value _type, value _name, value _off
20862086
CAMLreturn(alloc_BinaryenExpressionRef(exp));
20872087
}
20882088

2089+
CAMLprim value
2090+
caml_binaryen_array_new_elem(value _module, value _type, value _seg, value _offset, value _size) {
2091+
CAMLparam5(_module, _type, _seg, _offset, _size);
2092+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2093+
BinaryenHeapType type = BinaryenHeapType_val(_type);
2094+
char* seg = Safe_String_val(_seg);
2095+
BinaryenExpressionRef offset = BinaryenExpressionRef_val(_offset);
2096+
BinaryenExpressionRef size = BinaryenExpressionRef_val(_size);
2097+
BinaryenExpressionRef exp = BinaryenArrayNewElem(module, type, seg, offset, size);
2098+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2099+
}
2100+
20892101
CAMLprim value
20902102
caml_binaryen_array_new_fixed(value _module, value _type, value _values) {
20912103
CAMLparam3(_module, _type, _values);
@@ -2151,6 +2163,18 @@ caml_binaryen_array_copy__bytecode(value * argv) {
21512163
return caml_binaryen_array_copy(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
21522164
}
21532165

2166+
CAMLprim value
2167+
caml_binaryen_array_fill(value _module, value _ref, value _index, value _value, value _size) {
2168+
CAMLparam5(_module, _ref, _index, _value, _size);
2169+
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
2170+
BinaryenExpressionRef ref = BinaryenExpressionRef_val(_ref);
2171+
BinaryenExpressionRef index = BinaryenExpressionRef_val(_index);
2172+
BinaryenExpressionRef val = BinaryenExpressionRef_val(_value);
2173+
BinaryenExpressionRef size = BinaryenExpressionRef_val(_size);
2174+
BinaryenExpressionRef exp = BinaryenArrayFill(module, ref, index, val, size);
2175+
CAMLreturn(alloc_BinaryenExpressionRef(exp));
2176+
}
2177+
21542178
// Table operations
21552179
CAMLprim value
21562180
caml_binaryen_table_get(value _module, value _name, value _index, value _ty) {

src/expression.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -575,13 +575,13 @@ function caml_binaryen_ref_cast(wasm_mod, ref, typ) {
575575
function caml_binaryen_br_on(wasm_mod, op, name, ref, typ) {
576576
switch (op) {
577577
case Binaryen.BrOnNull:
578-
return wasm_mod.br_on.null(caml_jsstring_of_string(name), ref, typ);
578+
return wasm_mod.br_on_null(caml_jsstring_of_string(name), ref);
579579
case Binaryen.BrOnNonNull:
580-
return wasm_mod.br_on.non_null(caml_jsstring_of_string(name), ref, typ);
580+
return wasm_mod.br_on_non_null(caml_jsstring_of_string(name), ref);
581581
case Binaryen.BrOnCast:
582-
return wasm_mod.br_on.cast(caml_jsstring_of_string(name), ref, typ);
582+
return wasm_mod.br_on_cast(caml_jsstring_of_string(name), ref, typ);
583583
case Binaryen.BrOnCastFail:
584-
return wasm_mod.br_on.cast_fail(caml_jsstring_of_string(name), ref, typ);
584+
return wasm_mod.br_on_cast_fail(caml_jsstring_of_string(name), ref, typ);
585585
}
586586
}
587587

@@ -1821,11 +1821,7 @@ function caml_binaryen_struct_new(wasm_mod, operands, type) {
18211821
//Provides: caml_binaryen_struct_get
18221822
//Requires: caml_js_from_bool
18231823
function caml_binaryen_struct_get(wasm_mod, index, ref, type, signed) {
1824-
if (caml_js_from_bool(signed)) {
1825-
return wasm_mod.struct.get_s(index, ref, type);
1826-
} else {
1827-
return wasm_mod.struct.get_u(index, ref, type);
1828-
}
1824+
return wasm_mod.struct.get(index, ref, type, caml_js_from_bool(signed));
18291825
}
18301826

18311827
//Provides: caml_binaryen_struct_set
@@ -1851,6 +1847,17 @@ function caml_binaryen_array_new_data(wasm_mod, type, name, offset, size) {
18511847
);
18521848
}
18531849

1850+
//Provides: caml_binaryen_array_new_elem
1851+
//Requires: caml_jsstring_of_string
1852+
function caml_binaryen_array_new_elem(wasm_mod, type, name, offset, size) {
1853+
return wasm_mod.array.new_elem(
1854+
type,
1855+
caml_jsstring_of_string(name),
1856+
offset,
1857+
size
1858+
);
1859+
}
1860+
18541861
//Provides: caml_binaryen_array_new_fixed
18551862
//Requires: caml_list_to_js_array
18561863
function caml_binaryen_array_new_fixed(wasm_mod, type, values) {
@@ -1860,11 +1867,7 @@ function caml_binaryen_array_new_fixed(wasm_mod, type, values) {
18601867
//Provides: caml_binaryen_array_get
18611868
//Requires: caml_js_from_bool
18621869
function caml_binaryen_array_get(wasm_mod, ref, index, type, signed) {
1863-
if (caml_js_from_bool(signed)) {
1864-
return wasm_mod.array.get_s(ref, index, type);
1865-
} else {
1866-
return wasm_mod.array.get_u(ref, index, type);
1867-
}
1870+
return wasm_mod.array.get(ref, index, type, caml_js_from_bool(signed));
18681871
}
18691872

18701873
//Provides: caml_binaryen_array_set
@@ -1877,6 +1880,11 @@ function caml_binaryen_array_len(wasm_mod, ref) {
18771880
return wasm_mod.array.len(ref);
18781881
}
18791882

1883+
//Provides: caml_binaryen_array_fill
1884+
function caml_binaryen_array_fill(wasm_mod, ref, index, value, size) {
1885+
return wasm_mod.array.fill(ref, index, value, size);
1886+
}
1887+
18801888
//Provides: caml_binaryen_array_copy
18811889
function caml_binaryen_array_copy(
18821890
wasm_mod,

src/expression.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@ module Array = struct
899899
= "caml_binaryen_array_new_data"
900900
(** Module, type, data name, offset, size *)
901901

902+
external new_elem : Module.t -> Heap_type.t -> string -> t -> t -> t
903+
= "caml_binaryen_array_new_elem"
904+
(** Module, type, seg, offset, size *)
905+
902906
external new_fixed : Module.t -> Heap_type.t -> t list -> t
903907
= "caml_binaryen_array_new_fixed"
904908
(** Module, type, values *)
@@ -913,6 +917,10 @@ module Array = struct
913917
external len : Module.t -> t -> t = "caml_binaryen_array_len"
914918
(** Module, array *)
915919

920+
external fill : Module.t -> Heap_type.t -> t -> t -> t -> t
921+
= "caml_binaryen_array_fill"
922+
(** Module, type, seg, offset, size *)
923+
916924
external copy : Module.t -> t -> t -> t -> t -> t -> t
917925
= "caml_binaryen_array_copy__bytecode" "caml_binaryen_array_copy"
918926
(** Module, dest, dest index, src, src index, length *)

src/expression.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@ module Array : sig
394394
val new_data : Module.t -> Heap_type.t -> string -> t -> t -> t
395395
(** Module, type, data name, offset, size *)
396396

397+
val new_elem : Module.t -> Heap_type.t -> string -> t -> t -> t
398+
(** Module, type, seg, offset, size *)
399+
397400
val new_fixed : Module.t -> Heap_type.t -> t list -> t
398401
(** Module, type, values *)
399402

@@ -406,6 +409,9 @@ module Array : sig
406409
val len : Module.t -> t -> t
407410
(** Module, array *)
408411

412+
val fill : Module.t -> Heap_type.t -> t -> t -> t -> t
413+
(** Module, type, seg, offset, size *)
414+
409415
val copy : Module.t -> t -> t -> t -> t -> t -> t
410416
(** Module, dest, dest index, src, src index, length *)
411417
end

src/type_builder.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type struct_field = {
66
mutable_ : bool;
77
}
88

9+
(* Source: https://github.com/WebAssembly/binaryen/blob/64ba23996a10e229d46e41eb37736a55af87f79a/src/binaryen-c.h#L3618 *)
910
type error =
1011
| SelfSupertype of int
1112
| InvalidSupertype of int
@@ -21,7 +22,8 @@ external set_signature_type : t -> int -> Type.t -> Type.t -> unit
2122

2223
external set_struct_type :
2324
t -> int -> Type.t list -> Packed_type.t list -> bool list -> int -> unit
24-
= "caml_type_builder_set_struct_type__bytecode" "caml_type_builder_set_struct_type"
25+
= "caml_type_builder_set_struct_type__bytecode"
26+
"caml_type_builder_set_struct_type"
2527

2628
let set_struct_type builder index fields =
2729
let split_fields fields =
@@ -45,15 +47,15 @@ external get_temp_tuple_type : t -> Type.t list -> Type.t
4547
external get_temp_ref_type : t -> Heap_type.t -> bool -> Type.t
4648
= "caml_type_builder_get_temp_ref_type"
4749

48-
external set_sub_type : t -> int -> Type.t -> unit
50+
external set_sub_type : t -> int -> Heap_type.t -> unit
4951
= "caml_type_builder_set_sub_type"
5052

5153
external set_open : t -> int -> unit = "caml_type_builder_set_open"
5254

5355
external create_rec_group : t -> int -> int -> unit
5456
= "caml_type_builder_create_rec_group"
5557

56-
external build_and_dispose : t -> (Type.t array, int * int) result
58+
external build_and_dispose : t -> (Heap_type.t array, int * int) result
5759
= "caml_type_builder_build_and_dispose"
5860

5961
let build_and_dispose builder =

src/type_builder.mli

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type struct_field = {
66
mutable_ : bool;
77
}
88

9+
(* Source: https://github.com/WebAssembly/binaryen/blob/64ba23996a10e229d46e41eb37736a55af87f79a/src/binaryen-c.h#L3618 *)
910
type error =
1011
| SelfSupertype of int
1112
| InvalidSupertype of int
@@ -21,7 +22,7 @@ val set_array_type : t -> int -> Type.t -> Packed_type.t -> bool -> unit
2122
val get_temp_heap_type : t -> int -> Heap_type.t
2223
val get_temp_tuple_type : t -> Type.t list -> Type.t
2324
val get_temp_ref_type : t -> Heap_type.t -> bool -> Type.t
24-
val set_sub_type : t -> int -> Type.t -> unit
25+
val set_sub_type : t -> int -> Heap_type.t -> unit
2526
val set_open : t -> int -> unit
2627
val create_rec_group : t -> int -> int -> unit
27-
val build_and_dispose : t -> (Type.t list, error) result
28+
val build_and_dispose : t -> (Heap_type.t list, error) result

test/test.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
(module
121121
(type $0 (struct (field i31ref) (field (ref null $0))))
122122
(type $1 (array (mut i8)))
123-
(type $2 (func (result (ref $1) (ref $0))))
123+
(type $2 (func (result (ref (exact $1)) (ref (exact $0)))))
124124
(type $3 (func (param i32 i32) (result i32)))
125125
(type $4 (func))
126126
(type $5 (func (param anyref i32 i32) (result i32)))
@@ -170,7 +170,7 @@
170170
)
171171
)
172172
(func $gc (type $7) (param $0 anyref) (result anyref anyref)
173-
(local $1 (ref $1))
173+
(local $1 (ref (exact $1)))
174174
(array.set $1
175175
(local.tee $1
176176
(array.new_fixed $1 2
@@ -260,7 +260,7 @@
260260
)
261261
)
262262
(func $3 (type $type_4) (param $0 anyref) (result anyref anyref)
263-
(local $1 (ref $type_5))
263+
(local $1 (ref (exact $type_5)))
264264
(array.set $type_5
265265
(local.tee $1
266266
(array.new_fixed $type_5 2
@@ -343,7 +343,7 @@
343343
call $fimport$0
344344
)
345345
(func $3 (type $type_4) (param $0 anyref) (result anyref anyref)
346-
(local $1 (ref $type_5))
346+
(local $1 (ref (exact $type_5)))
347347
i32.const 0
348348
i32.const 255
349349
array.new_fixed $type_5 2

test/test.ml

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,25 @@ let _ =
325325
let i32 v = Expression.Const.make wasm_mod (Literal.int32 v) in
326326
let i31 v = Expression.I31.make wasm_mod (i32 v) in
327327
let cons first rest =
328-
Expression.Struct.new_ wasm_mod
329-
(Some [ first; rest ])
330-
(Type.get_heap_type list_type)
328+
Expression.Struct.new_ wasm_mod (Some [ first; rest ]) list_type
331329
in
332330
let empty () =
333-
Expression.Ref.null wasm_mod
334-
(Type.from_heap_type (Type.get_heap_type list_type) true)
331+
Expression.Ref.null wasm_mod (Type.from_heap_type list_type true)
335332
in
336333
Function.add_function wasm_mod "gc" Type.anyref
337334
(Type.create [| Type.anyref; Type.anyref |])
338-
[| array_u8_type; list_type |]
335+
[|
336+
Type.from_heap_type array_u8_type false;
337+
Type.from_heap_type list_type false;
338+
|]
339339
(Expression.Block.make wasm_mod "gc_block"
340340
[
341341
Expression.Local_set.make wasm_mod 1
342-
(Expression.Array.new_fixed wasm_mod
343-
(Type.get_heap_type array_u8_type)
342+
(Expression.Array.new_fixed wasm_mod array_u8_type
344343
[ i32 0l; i32 255l ]);
345344
Expression.Array.set wasm_mod
346-
(Expression.Local_get.make wasm_mod 1 array_u8_type)
345+
(Expression.Local_get.make wasm_mod 1
346+
(Type.from_heap_type array_u8_type false))
347347
(i32 1l) (i32 42l);
348348
Expression.Local_set.make wasm_mod 2
349349
(cons
@@ -353,8 +353,10 @@ let _ =
353353
(cons (i31 1l) (cons (i31 2l) (cons (i31 3l) (empty ())))));
354354
Expression.Tuple_make.make wasm_mod
355355
[
356-
Expression.Local_get.make wasm_mod 1 array_u8_type;
357-
Expression.Local_get.make wasm_mod 2 list_type;
356+
Expression.Local_get.make wasm_mod 1
357+
(Type.from_heap_type array_u8_type false);
358+
Expression.Local_get.make wasm_mod 2
359+
(Type.from_heap_type list_type false);
358360
];
359361
])
360362

0 commit comments

Comments
 (0)