Skip to content

Commit

Permalink
Merge pull request #53 from ocaml-wasm/extern-conv
Browse files Browse the repository at this point in the history
Rename instructions extern.{in,ex}ternalize to follow the Wasm GC spec
  • Loading branch information
vouillon committed Sep 11, 2024
2 parents 8d502f6 + b9a93a3 commit 60a1203
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 33 deletions.
4 changes: 1 addition & 3 deletions compiler/lib/wasm/wa_asm_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,7 @@ module Output () = struct
| RefEq _
| RefNull _
| Br_on_cast _
| Br_on_cast_fail _
| ExternExternalize _
| ExternInternalize _ -> assert false (* Not supported *)
| Br_on_cast_fail _ -> assert false (* Not supported *)

and instruction m i =
match i with
Expand Down
2 changes: 0 additions & 2 deletions compiler/lib/wasm/wa_ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ type expression =
| RefTest of ref_type * expression
| RefEq of expression * expression
| RefNull of heap_type
| ExternInternalize of expression
| ExternExternalize of expression
| Br_on_cast of int * ref_type * ref_type * expression
| Br_on_cast_fail of int * ref_type * ref_type * expression
| IfExpr of value_type * expression * expression * expression
Expand Down
2 changes: 0 additions & 2 deletions compiler/lib/wasm/wa_code_generation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,6 @@ let rec is_smi e =
| StructGet _
| RefCast _
| RefNull _
| ExternInternalize _
| ExternExternalize _
| Br_on_cast _
| Br_on_cast_fail _ -> false
| BinOp ((F32 _ | F64 _), _, _) | RefTest _ | RefEq _ -> true
Expand Down
4 changes: 1 addition & 3 deletions compiler/lib/wasm/wa_gc_target.ml
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,7 @@ module Value = struct
| ArrayLen e'
| StructGet (_, _, _, e')
| RefCast (_, e')
| RefTest (_, e')
| ExternInternalize e'
| ExternExternalize e' -> effect_free e'
| RefTest (_, e') -> effect_free e'
| BinOp (_, e1, e2)
| ArrayNew (_, e1, e2)
| ArrayNewData (_, _, e1, e2)
Expand Down
4 changes: 1 addition & 3 deletions compiler/lib/wasm/wa_initialize_locals.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ let rec scan_expression ctx e =
| RefCast (_, e')
| RefTest (_, e')
| Br_on_cast (_, _, _, e')
| Br_on_cast_fail (_, _, _, e')
| ExternInternalize e'
| ExternExternalize e' -> scan_expression ctx e'
| Br_on_cast_fail (_, _, _, e') -> scan_expression ctx e'
| BinOp (_, e', e'')
| ArrayNew (_, e', e'')
| ArrayNewData (_, _, e', e'')
Expand Down
2 changes: 0 additions & 2 deletions compiler/lib/wasm/wa_wat_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,6 @@ let expression_or_instructions ctx st in_function =
:: ref_type st ty'
:: expression e)
]
| ExternInternalize e -> [ List (Atom "extern.internalize" :: expression e) ]
| ExternExternalize e -> [ List (Atom "extern.externalize" :: expression e) ]
| IfExpr (ty, cond, ift, iff) ->
[ List
((Atom "if" :: block_type st { params = []; result = [ ty ] })
Expand Down
6 changes: 3 additions & 3 deletions runtime/wasm/bigarray.wat
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@
(local $len i32)
(local.set $data
(call $ta_normalize
(ref.as_non_null (extern.externalize (call $unwrap (local.get 0))))))
(ref.as_non_null (extern.convert_any (call $unwrap (local.get 0))))))
(local.set $kind (call $ta_kind (local.get $data)))
(if (i32.lt_s (local.get $kind) (i32.const 0))
(then
Expand All @@ -777,7 +777,7 @@

(func (export "caml_ba_to_typed_array") (param (ref eq)) (result (ref eq))
(call $wrap
(extern.internalize
(any.convert_extern
(struct.get $bigarray $ba_data
(ref.cast (ref $bigarray) (local.get 0))))))

Expand Down Expand Up @@ -2104,7 +2104,7 @@
(local $a (ref extern)) (local $len i32) (local $i i32)
(local $s (ref $string))
(local.set $a
(ref.as_non_null (extern.externalize (call $unwrap (local.get 0)))))
(ref.as_non_null (extern.convert_any (call $unwrap (local.get 0)))))
(local.set $len (call $ta_length (local.get $a)))
(local.set $s (array.new $string (i32.const 0) (local.get $len)))
(loop $loop
Expand Down
2 changes: 1 addition & 1 deletion runtime/wasm/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(system
"which wasm-merge > /dev/null || (echo 'Error: Binaryen tools not found in the PATH'; false)")
(system
"wasm-merge --version | grep -q 'version \\(11[789]\\|1[2-9][0-9]\\)' || (echo 'Error: Binaryen version 117 or greater is currently required'; false)")
"wasm-merge --version | grep -q 'version \\(11[89]\\|1[2-9][0-9]\\)' || (echo 'Error: Binaryen version 118 or greater is currently required'; false)")
(pipe-stdout
(run
wasm-merge
Expand Down
16 changes: 8 additions & 8 deletions runtime/wasm/jslib.wat
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
(local.set 1 (call $caml_jsbytes_of_string (local.get 1)))))
(return_call $wrap
(call $get
(ref.as_non_null (extern.externalize (call $unwrap (local.get 0))))
(ref.as_non_null (extern.convert_any (call $unwrap (local.get 0))))
(call $unwrap (local.get 1)))))

(func (export "caml_js_set")
Expand Down Expand Up @@ -291,7 +291,7 @@
(i32.add (local.get $i) (i32.const 1)))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $loop))))
(return (struct.new $js (extern.internalize (local.get $a'))))))
(return (struct.new $js (any.convert_extern (local.get $a'))))))
(local.set $fa (ref.cast (ref $float_array) (local.get $va)))
(local.set $l (array.len (local.get $fa)))
(local.set $a' (call $new_array (local.get $l)))
Expand All @@ -304,7 +304,7 @@
(array.get $float_array (local.get $fa) (local.get $i))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $loop))))
(struct.new $js (extern.internalize (local.get $a'))))
(struct.new $js (any.convert_extern (local.get $a'))))

(func (export "caml_js_to_array")
(param (ref eq)) (result (ref eq))
Expand All @@ -313,7 +313,7 @@
(local $fa (ref $float_array))
(local $i i32) (local $l i32)
(local.set $a
(ref.as_non_null (extern.externalize (call $unwrap (local.get 0)))))
(ref.as_non_null (extern.convert_any (call $unwrap (local.get 0)))))
(local.set $l (call $array_length (local.get $a)))
(if (local.get $l)
(then
Expand Down Expand Up @@ -591,7 +591,7 @@
(local.set $l (array.get $block (local.get $b) (i32.const 2)))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $loop))))
(struct.new $js (extern.internalize (local.get $a))))
(struct.new $js (any.convert_extern (local.get $a))))

(func (export "caml_list_of_js_array")
(param (ref eq)) (result (ref eq))
Expand All @@ -600,7 +600,7 @@
(local $len i32)
(local $a (ref extern))
(local.set $a
(ref.as_non_null (extern.externalize (call $unwrap (local.get 0)))))
(ref.as_non_null (extern.convert_any (call $unwrap (local.get 0)))))
(local.set $len (call $array_length (local.get $a)))
(local.set $i (i32.const 0))
(local.set $l (ref.i31 (i32.const 0)))
Expand All @@ -625,7 +625,7 @@

(func (export "caml_wrap_exception") (param externref) (result (ref eq))
(local $exn anyref)
(local.set $exn (extern.internalize (local.get 0)))
(local.set $exn (any.convert_extern (local.get 0)))
;; ZZZ special case for stack overflows?
(block $undef
(return
Expand All @@ -643,7 +643,7 @@
(call $caml_jsstring_of_string
(array.new_data $string $toString
(i32.const 0) (i32.const 8))))
(extern.internalize (call $new_array (i32.const 0))))))))
(any.convert_extern (call $new_array (i32.const 0))))))))

(func (export "caml_js_error_option_of_exception")
(param (ref eq)) (result (ref eq))
Expand Down
10 changes: 5 additions & 5 deletions runtime/wasm/jsstring.wat
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
(func (export "jsstring_compare")
(param $s anyref) (param $s' anyref) (result i32)
(return_call $compare_strings
(extern.externalize (local.get $s))
(extern.externalize (local.get $s'))))
(extern.convert_any (local.get $s))
(extern.convert_any (local.get $s'))))

(func (export "jsstring_test") (param $s anyref) (result i32)
(return_call $is_string (extern.externalize (local.get $s))))
(return_call $is_string (extern.convert_any (local.get $s))))

(export "jsstring_hash" (func $hash_string))

Expand All @@ -78,7 +78,7 @@
(if (global.get $builtins_available)
(then
(return
(extern.internalize
(any.convert_extern
(call $decodeStringFromUTF8Array (local.get $s)
(local.get $pos)
(i32.add (local.get $pos) (local.get $len)))))))
Expand All @@ -93,7 +93,7 @@
(if (global.get $builtins_available)
(then
(return_call $encodeStringToUTF8Array
(extern.externalize (local.get $s)))))
(extern.convert_any (local.get $s)))))
(return_call $string_of_jsstring_fallback (local.get $s)))

;; Fallback implementation of string conversion functions
Expand Down
2 changes: 1 addition & 1 deletion runtime/wasm/sys.wat
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
(call $caml_raise_sys_error
(call $caml_string_of_jsstring
(call $caml_js_meth_call
(call $wrap (extern.internalize (local.get $exn)))
(call $wrap (any.convert_extern (local.get $exn)))
(array.new_data $string $toString (i32.const 0) (i32.const 8))
(array.new_fixed $block 1 (ref.i31 (i32.const 0)))))))
)

0 comments on commit 60a1203

Please sign in to comment.