Skip to content

Commit

Permalink
Wasm runtime: use string syntactic sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Jan 31, 2025
1 parent c51c220 commit 3028b6c
Show file tree
Hide file tree
Showing 25 changed files with 198 additions and 435 deletions.
17 changes: 4 additions & 13 deletions runtime/wasm/array.wat
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(type $float (struct (field f64)))
(type $float_array (array (mut f64)))

(data $Array_make "Array.make")
(@string $Array_make "Array.make")

(global $empty_array (ref eq)
(array.new_fixed $block 1 (ref.i31 (i32.const 0))))
Expand All @@ -34,10 +34,7 @@
(local $sz i32) (local $b (ref $block)) (local $f f64)
(local.set $sz (i31.get_s (ref.cast (ref i31) (local.get $n))))
(if (i32.ge_u (local.get $sz) (i32.const 0xfffffff))
(then
(call $caml_invalid_argument
(array.new_data $string $Array_make
(i32.const 0) (i32.const 10)))))
(then (call $caml_invalid_argument (global.get $Array_make))))
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
(drop (block $not_float (result (ref eq))
(local.set $f
Expand All @@ -56,10 +53,7 @@
(local $sz i32) (local $f f64)
(local.set $sz (i31.get_s (ref.cast (ref i31) (local.get $n))))
(if (i32.ge_u (local.get $sz) (i32.const 0x7ffffff))
(then
(call $caml_invalid_argument
(array.new_data $string $Array_make
(i32.const 0) (i32.const 10)))))
(then (call $caml_invalid_argument (global.get $Array_make))))
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
(local.set $f
(struct.get $float 0
Expand All @@ -73,10 +67,7 @@
(local $sz i32)
(local.set $sz (i31.get_s (ref.cast (ref i31) (local.get $n))))
(if (i32.ge_u (local.get $sz) (i32.const 0x7ffffff))
(then
(call $caml_invalid_argument
(array.new_data $string $Array_make
(i32.const 0) (i32.const 10)))))
(then (call $caml_invalid_argument (global.get $Array_make))))
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
(array.new $float_array (f64.const 0) (local.get $sz)))

Expand Down
6 changes: 2 additions & 4 deletions runtime/wasm/backtrace.wat
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
(param (ref eq)) (result (ref eq))
(ref.i31 (i32.const 0)))

(data $raw_backtrace_slot_err
(@string $raw_backtrace_slot_err
"Printexc.get_raw_backtrace_slot: index out of bounds")

(func (export "caml_raw_backtrace_slot")
(param (ref eq) (ref eq)) (result (ref eq))
(call $caml_invalid_argument
(array.new_data $string $raw_backtrace_slot_err
(i32.const 0) (i32.const 52)))
(call $caml_invalid_argument (global.get $raw_backtrace_slot_err))
(ref.i31 (i32.const 0)))

(func (export "caml_convert_raw_backtrace_slot")
Expand Down
89 changes: 26 additions & 63 deletions runtime/wasm/bigarray.wat
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@

(global $bigarray_ops (export "bigarray_ops") (ref $custom_operations)
(struct.new $custom_operations
(array.new_fixed $string 9 ;; "_bigarr02"
(i32.const 95) (i32.const 98) (i32.const 105) (i32.const 103)
(i32.const 97) (i32.const 114) (i32.const 114) (i32.const 48)
(i32.const 50))
(@string "_bigarr02")
(ref.func $caml_ba_compare)
(ref.null $compare)
(ref.func $bigarray_hash)
Expand Down Expand Up @@ -596,7 +593,7 @@
(i32.mul (i32.add (i32.const 4) (local.get $num_dims)) (i32.const 4))
(i32.mul (i32.add (i32.const 4) (local.get $num_dims)) (i32.const 8))))

(data $intern_overflow
(@string $intern_overflow
"input_value: cannot read bigarray with 64-bit OCaml ints")

(func $bigarray_deserialize
Expand Down Expand Up @@ -680,10 +677,7 @@
(br $done))
;; int
(if (call $caml_deserialize_uint_1 (local.get $s))
(then
(call $caml_failwith
(array.new_data $string $intern_overflow
(i32.const 0) (i32.const 56))))))
(then (call $caml_failwith (global.get $intern_overflow)))))
;; int32
(loop $loop
(if (i32.lt_u (local.get $i) (local.get $len))
Expand Down Expand Up @@ -792,8 +786,8 @@

(global $CAML_BA_MAX_NUM_DIMS i32 (i32.const 16))

(data $ba_create_bad_dims "Bigarray.create: bad number of dimensions")
(data $ba_create_negative_dim "Bigarray.create: negative dimension")
(@string $ba_create_bad_dims "Bigarray.create: bad number of dimensions")
(@string $ba_create_negative_dim "Bigarray.create: negative dimension")

(func (export "caml_ba_create")
(param $vkind (ref eq)) (param $layout (ref eq)) (param $d (ref eq))
Expand All @@ -805,10 +799,7 @@
(local.set $vdim (ref.cast (ref $block) (local.get $d)))
(local.set $num_dims (i32.sub (array.len (local.get $vdim)) (i32.const 1)))
(if (i32.gt_u (local.get $num_dims) (global.get $CAML_BA_MAX_NUM_DIMS))
(then
(call $caml_invalid_argument
(array.new_data $string $ba_create_bad_dims
(i32.const 0) (i32.const 41)))))
(then (call $caml_invalid_argument (global.get $ba_create_bad_dims))))
(local.set $dim
(array.new $int_array (i32.const 0) (local.get $num_dims)))
(local.set $i (i32.const 0))
Expand All @@ -823,8 +814,7 @@
(if (i32.lt_s (local.get $n) (i32.const 0))
(then
(call $caml_invalid_argument
(array.new_data $string $ba_create_negative_dim
(i32.const 0) (i32.const 35)))))
(global.get $ba_create_negative_dim))))
(array.set $int_array
(local.get $dim) (local.get $i) (local.get $n))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
Expand All @@ -838,8 +828,8 @@
(local.get $kind)
(i31.get_s (ref.cast (ref i31) (local.get $layout)))))

(data $ta_unsupported_kind "Typed_array.to_genarray: unsupported kind")
(data $ta_too_large "Typed_array.to_genarray: too large")
(@string $ta_unsupported_kind "Typed_array.to_genarray: unsupported kind")
(@string $ta_too_large "Typed_array.to_genarray: too large")

(func (export "caml_ba_from_typed_array") (param (ref eq)) (result (ref eq))
(local $data (ref extern))
Expand All @@ -850,18 +840,12 @@
(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
(call $caml_invalid_argument
(array.new_data $string $ta_unsupported_kind
(i32.const 0) (i32.const 41)))))
(then (call $caml_invalid_argument (global.get $ta_unsupported_kind))))
(if (i32.eq (local.get $kind) (i32.const 14)) ;; Uint8ClampedArray
(then (local.set $kind (i32.const 3))))
(local.set $len (call $ta_length (local.get $data)))
(if (i32.lt_s (local.get $len) (i32.const 0))
(then
(call $caml_invalid_argument
(array.new_data $string $ta_too_large
(i32.const 0) (i32.const 34)))))
(then (call $caml_invalid_argument (global.get $ta_too_large))))
(struct.new $bigarray
(global.get $bigarray_ops)
(local.get $data)
Expand Down Expand Up @@ -1050,7 +1034,7 @@
(struct.get $float 0 (ref.cast (ref $float) (local.get $v))))
(return))

(data $Bigarray_dim "Bigarray.dim")
(@string $Bigarray_dim "Bigarray.dim")

(func $caml_ba_dim (export "caml_ba_dim")
(param (ref eq)) (param (ref eq)) (result (ref eq))
Expand All @@ -1061,9 +1045,7 @@
(ref.cast (ref $bigarray) (local.get 0))))
(local.set $i (i31.get_s (ref.cast (ref i31) (local.get 1))))
(if (i32.ge_u (local.get $i) (array.len (local.get $dim)))
(then (call $caml_invalid_argument
(array.new_data $string $Bigarray_dim
(i32.const 0) (i32.const 12)))))
(then (call $caml_invalid_argument (global.get $Bigarray_dim))))
(ref.i31 (array.get $int_array (local.get $dim) (local.get $i))))

(func (export "caml_ba_dim_1") (param (ref eq)) (result (ref eq))
Expand Down Expand Up @@ -1409,7 +1391,7 @@
(local.get $v))
(ref.i31 (i32.const 0)))

(data $too_many_indices "Bigarray.slice: too many indices")
(@string $too_many_indices "Bigarray.slice: too many indices")

(func (export "caml_ba_slice")
(param $vb (ref eq)) (param $vind (ref eq)) (result (ref eq))
Expand All @@ -1425,10 +1407,7 @@
(local.set $num_dims (struct.get $bigarray $ba_num_dims (local.get $b)))
(if (i32.gt_u (local.get $num_inds)
(struct.get $bigarray $ba_num_dims (local.get $b)))
(then
(call $caml_invalid_argument
(array.new_data $string $too_many_indices
(i32.const 0) (i32.const 32)))))
(then (call $caml_invalid_argument (global.get $too_many_indices))))
(local.set $sub_dim
(array.new $int_array (i32.const 0)
(i32.sub (local.get $num_dims) (local.get $num_inds))))
Expand Down Expand Up @@ -1492,7 +1471,7 @@
(struct.get $bigarray $ba_kind (local.get $b))
(struct.get $bigarray $ba_layout (local.get $b))))

(data $bad_subarray "Bigarray.sub: bad sub-array")
(@string $bad_subarray "Bigarray.sub: bad sub-array")

(func (export "caml_ba_sub")
(param $vba (ref eq)) (param $vofs (ref eq)) (param $vlen (ref eq))
Expand Down Expand Up @@ -1542,10 +1521,7 @@
(i32.gt_s (i32.add (local.get $ofs) (local.get $len))
(array.get $int_array (local.get $dim)
(local.get $changed_dim))))
(then
(call $caml_invalid_argument
(array.new_data $string $bad_subarray
(i32.const 0) (i32.const 27)))))
(then (call $caml_invalid_argument (global.get $bad_subarray))))
(local.set $new_dim
(array.new $int_array (i32.const 0) (local.get $num_dims)))
(array.copy $int_array $int_array
Expand Down Expand Up @@ -1658,7 +1634,7 @@
(struct.get $float 0 (ref.cast (ref $float) (local.get $v))))
(return (ref.i31 (i32.const 0))))

(data $dim_mismatch "Bigarray.blit: dimension mismatch")
(@string $dim_mismatch "Bigarray.blit: dimension mismatch")

(func (export "caml_ba_blit")
(param $vsrc (ref eq)) (param $vdst (ref eq)) (result (ref eq))
Expand All @@ -1672,10 +1648,7 @@
(local.set $len (struct.get $bigarray $ba_num_dims (local.get $dst)))
(if (i32.ne (local.get $len)
(struct.get $bigarray $ba_num_dims (local.get $src)))
(then
(call $caml_invalid_argument
(array.new_data $string $dim_mismatch
(i32.const 0) (i32.const 33)))))
(then (call $caml_invalid_argument (global.get $dim_mismatch))))
(local.set $sdim (struct.get $bigarray $ba_dim (local.get $src)))
(local.set $ddim (struct.get $bigarray $ba_dim (local.get $dst)))
(loop $loop
Expand All @@ -1685,19 +1658,17 @@
(array.get $int_array (local.get $sdim) (local.get $i))
(array.get $int_array (local.get $ddim) (local.get $i)))
(then
(call $caml_invalid_argument
(array.new_data $string $dim_mismatch
(i32.const 0) (i32.const 33)))))
(call $caml_invalid_argument (global.get $dim_mismatch))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $loop))))
(call $ta_blit
(struct.get $bigarray $ba_data (local.get $src))
(struct.get $bigarray $ba_data (local.get $dst)))
(ref.i31 (i32.const 0)))

(data $bad_number_dim "Bigarray.reshape: bad number of dimensions")
(data $negative_dim "Bigarray.reshape: negative dimension")
(data $size_mismatch "Bigarray.reshape: size mismatch")
(@string $bad_number_dim "Bigarray.reshape: bad number of dimensions")
(@string $negative_dim "Bigarray.reshape: negative dimension")
(@string $size_mismatch "Bigarray.reshape: size mismatch")

(func (export "caml_ba_reshape")
(param $vb (ref eq)) (param $vd (ref eq)) (result (ref eq))
Expand All @@ -1709,10 +1680,7 @@
(local.set $num_dims (i32.sub (array.len (local.get $vdim)) (i32.const 1)))
(local.set $b (ref.cast (ref $bigarray) (local.get $vb)))
(if (i32.gt_u (local.get $num_dims) (global.get $CAML_BA_MAX_NUM_DIMS))
(then
(call $caml_invalid_argument
(array.new_data $string $bad_number_dim
(i32.const 0) (i32.const 42)))))
(then (call $caml_invalid_argument (global.get $bad_number_dim))))
(local.set $num_elts (i64.const 1))
(local.set $dim (array.new $int_array (i32.const 0) (local.get $num_dims)))
(loop $loop
Expand All @@ -1725,9 +1693,7 @@
(i32.add (local.get $i) (i32.const 1))))))
(if (i32.lt_s (local.get $d) (i32.const 0))
(then
(call $caml_invalid_argument
(array.new_data $string $negative_dim
(i32.const 0) (i32.const 36)))))
(call $caml_invalid_argument (global.get $negative_dim))))
(array.set $int_array (local.get $dim) (local.get $i)
(local.get $d))
(local.set $num_elts
Expand All @@ -1741,10 +1707,7 @@
(if (i32.ne (i32.wrap_i64 (local.get $num_elts))
(call $caml_ba_get_size
(struct.get $bigarray $ba_dim (local.get $b))))
(then
(call $caml_invalid_argument
(array.new_data $string $size_mismatch
(i32.const 0) (i32.const 31)))))
(then (call $caml_invalid_argument (global.get $size_mismatch))))
(struct.new $bigarray
(global.get $bigarray_ops)
(struct.get $bigarray $ba_data (local.get $b))
Expand Down
4 changes: 2 additions & 2 deletions runtime/wasm/bigstring.wat
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@
(local.set $h (call $caml_hash_mix_int (local.get $h) (local.get $w))))
(i32.xor (local.get $h) (local.get $len)))

(data $buffer "buffer")
(@string $buffer "buffer")

(func (export "bigstring_to_array_buffer")
(param $bs (ref eq)) (result (ref eq))
(return_call $caml_js_get
(call $caml_ba_to_typed_array (local.get $bs))
(array.new_data $string $buffer (i32.const 0) (i32.const 6))))
(global.get $buffer)))

(export "bigstring_to_typed_array" (func $caml_ba_to_typed_array))

Expand Down
20 changes: 7 additions & 13 deletions runtime/wasm/compare.wat
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@
(call $clear_compare_stack)
(local.get $res))

(data $abstract_value "compare: abstract value")
(data $functional_value "compare: functional value")
(data $continuation_value "compare: continuation value")
(@string $abstract_value "compare: abstract value")
(@string $functional_value "compare: functional value")
(@string $continuation_value "compare: continuation value")

(func $do_compare_val
(param $stack (ref $compare_stack))
Expand Down Expand Up @@ -477,9 +477,7 @@
(br_if $next_item (i32.eqz (local.get $res)))
(return (local.get $res)))
(call $clear_compare_stack)
(call $caml_invalid_argument
(array.new_data $string $abstract_value
(i32.const 0) (i32.const 23)))
(call $caml_invalid_argument (global.get $abstract_value))
(ref.i31 (i32.const 0))))
(drop (block $v1_not_js (result (ref eq))
(local.set $js1
Expand Down Expand Up @@ -514,17 +512,15 @@
(i32.eqz (call $caml_is_closure (local.get $v2)))))
(call $clear_compare_stack)
(call $caml_invalid_argument
(array.new_data $string $functional_value
(i32.const 0) (i32.const 25)))))
(global.get $functional_value))))
(if (call $caml_is_continuation (local.get $v1))
(then
(drop (br_if $heterogeneous(ref.i31 (i32.const 0))
(i32.eqz
(call $caml_is_continuation (local.get $v2)))))
(call $clear_compare_stack)
(call $caml_invalid_argument
(array.new_data $string $continuation_value
(i32.const 0) (i32.const 27)))))
(global.get $continuation_value))))
(ref.i31 (i32.const 0)))) ;; fall through
;; heterogeneous comparison
(local.set $t1
Expand All @@ -549,9 +545,7 @@
(if (i32.eqz (local.get $res))
(then
(call $clear_compare_stack)
(call $caml_invalid_argument
(array.new_data $string $abstract_value
(i32.const 0) (i32.const 23)))))
(call $caml_invalid_argument (global.get $abstract_value))))
(return (local.get $res)))
(if (call $compare_stack_is_not_empty (local.get $stack))
(then
Expand Down
Loading

0 comments on commit 3028b6c

Please sign in to comment.