Skip to content

Commit

Permalink
Revert "Simplify handling of bound errors and division by zero"
Browse files Browse the repository at this point in the history
This reverts commit fc651d6.
  • Loading branch information
vouillon committed Sep 11, 2024
1 parent c2fb323 commit 88acffc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 54 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-wasm_of_ocaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
uses: actions/cache/restore@v4
with:
path: binaryen
key: ${{ runner.os }}-binaryen-version_118
key: ${{ runner.os }}-binaryen-eh

- name: Checkout binaryen
if: steps.cache-binaryen.outputs.cache-hit != 'true'
Expand All @@ -47,7 +47,7 @@ jobs:
repository: WebAssembly/binaryen
path: binaryen
submodules: true
ref: version_118
ref: d200d06fef7f4edd331c1f928493beb332a2d910

- name: Install ninja
if: steps.cache-binaryen.outputs.cache-hit != 'true'
Expand All @@ -65,7 +65,7 @@ jobs:
uses: actions/cache/save@v4
with:
path: binaryen
key: ${{ runner.os }}-binaryen-version_118
key: ${{ runner.os }}-binaryen-eh

- name: Set binaryen's path
run: |
Expand Down
16 changes: 9 additions & 7 deletions compiler/lib/wasm/wa_generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,11 @@ module Generate (Target : Wa_target_sig.S) = struct
{ params = []; result = [] }
(body ~result_typ:[] ~fall_through:(`Block pc) ~context:(`Block pc :: context))
in
handler
if List.is_empty result_typ
then handler
else
let* () = handler in
instr (W.Return (Some (RefI31 (Const (I32 0l)))))
else body ~result_typ ~fall_through ~context

let wrap_with_handlers p pc ~result_typ ~fall_through ~context body =
Expand All @@ -792,20 +796,18 @@ module Generate (Target : Wa_target_sig.S) = struct
need_bound_error_handler
bound_error_pc
(let* f =
register_import
~name:"caml_bound_error"
(Fun { params = []; result = [ Value.value ] })
register_import ~name:"caml_bound_error" (Fun { params = []; result = [] })
in
instr (Return_call (f, [])))
instr (CallInstr (f, [])))
(wrap_with_handler
need_zero_divide_handler
zero_divide_pc
(let* f =
register_import
~name:"caml_raise_zero_divide"
(Fun { params = []; result = [ Value.value ] })
(Fun { params = []; result = [] })
in
instr (Return_call (f, [])))
instr (CallInstr (f, [])))
body)
~result_typ
~fall_through
Expand Down
46 changes: 23 additions & 23 deletions runtime/wasm/bigarray.wat
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
(import "bindings" "ta_subarray"
(func $ta_subarray
(param (ref extern)) (param i32) (param i32) (result (ref extern))))
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
(import "fail" "caml_bound_error" (func $caml_bound_error))
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
(import "fail" "caml_invalid_argument"
(func $caml_invalid_argument (param (ref eq))))
Expand Down Expand Up @@ -970,7 +970,7 @@
(if (i32.ge_u (local.get $i)
(array.get $int_array (struct.get $bigarray 2 (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $i)))

(func (export "caml_ba_set_1")
Expand All @@ -984,7 +984,7 @@
(if (i32.ge_u (local.get $i)
(array.get $int_array (struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(call $caml_ba_set_at_offset
(local.get $ba) (local.get $i) (local.get $v))
(ref.i31 (i32.const 0)))
Expand Down Expand Up @@ -1022,7 +1022,7 @@
(i32.ge_u (local.get $j)
(array.get $int_array (local.get $dim) (i32.const 1))))
(then
(return_call $caml_bound_error)))
(call $caml_bound_error)))
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))

(func (export "caml_ba_set_2")
Expand Down Expand Up @@ -1058,7 +1058,7 @@
(i32.ge_u (local.get $j)
(array.get $int_array (local.get $dim) (i32.const 1))))
(then
(return_call $caml_bound_error)))
(call $caml_bound_error)))
(call $caml_ba_set_at_offset
(local.get $ba) (local.get $offset) (local.get $v))
(ref.i31 (i32.const 0)))
Expand Down Expand Up @@ -1116,7 +1116,7 @@
(i32.ge_u (local.get $k)
(array.get $int_array (local.get $dim) (i32.const 2)))))
(then
(return_call $caml_bound_error)))
(call $caml_bound_error)))
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))

(func (export "caml_ba_set_3")
Expand Down Expand Up @@ -1169,7 +1169,7 @@
(i32.ge_u (local.get $k)
(array.get $int_array (local.get $dim) (i32.const 2)))))
(then
(return_call $caml_bound_error)))
(call $caml_bound_error)))
(call $caml_ba_set_at_offset
(local.get $ba) (local.get $offset) (local.get $v))
(ref.i31 (i32.const 0)))
Expand Down Expand Up @@ -1200,7 +1200,7 @@
(array.get $int_array (local.get $dim) (local.get $i)))
(if (i32.ge_u (local.get $idx) (local.get $l))
(then
(drop (call $caml_bound_error))))
(call $caml_bound_error)))
(local.set $offset
(i32.add (i32.mul (local.get $offset) (local.get $l))
(local.get $idx)))
Expand All @@ -1218,7 +1218,7 @@
(array.get $int_array (local.get $dim) (local.get $i)))
(if (i32.ge_u (local.get $idx) (local.get $l))
(then
(drop (call $caml_bound_error))))
(call $caml_bound_error)))
(local.set $offset
(i32.add (i32.mul (local.get $offset) (local.get $l))
(local.get $idx)))
Expand Down Expand Up @@ -1251,7 +1251,7 @@
(array.get $int_array (local.get $dim) (local.get $i)))
(if (i32.ge_u (local.get $idx) (local.get $l))
(then
(drop (call $caml_bound_error))))
(call $caml_bound_error)))
(local.set $offset
(i32.add (i32.mul (local.get $offset) (local.get $l))
(local.get $idx)))
Expand All @@ -1272,7 +1272,7 @@
(array.get $int_array (local.get $dim) (local.get $i)))
(if (i32.ge_u (local.get $idx) (local.get $l))
(then
(drop (call $caml_bound_error))))
(call $caml_bound_error)))
(local.set $offset
(i32.add (i32.mul (local.get $offset) (local.get $l))
(local.get $idx)))
Expand Down Expand Up @@ -1906,12 +1906,12 @@
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
(array.get $int_array
(struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(ref.i31 (i32.or
(call $ta_get_ui8 (local.get $data) (local.get $p))
(i32.shl (call $ta_get_ui8 (local.get $data)
Expand All @@ -1927,12 +1927,12 @@
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
(array.get $int_array
(struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(return_call $caml_copy_int32
(i32.or
(i32.or
Expand All @@ -1957,12 +1957,12 @@
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
(array.get $int_array
(struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(return_call $caml_copy_int64
(i64.or
(i64.or
Expand Down Expand Up @@ -2013,12 +2013,12 @@
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(local.set $d (ref.cast (ref i31) (local.get $v)))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
(array.get $int_array
(struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(call $ta_set_ui8 (local.get $data) (local.get $p) (local.get $d))
(call $ta_set_ui8 (local.get $data)
(i32.add (local.get $p) (i32.const 1))
Expand All @@ -2036,12 +2036,12 @@
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(local.set $d (call $Int32_val (local.get $v)))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
(array.get $int_array
(struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(call $ta_set_ui8 (local.get $data) (local.get $p)
(ref.i31 (local.get $d)))
(call $ta_set_ui8 (local.get $data)
Expand All @@ -2066,12 +2066,12 @@
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(local.set $d (call $Int64_val (local.get $v)))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
(array.get $int_array
(struct.get $bigarray $ba_dim (local.get $ba))
(i32.const 0)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(call $ta_set_ui8 (local.get $data) (local.get $p)
(ref.i31 (i32.wrap_i64 (local.get $d))))
(call $ta_set_ui8 (local.get $data)
Expand Down
14 changes: 6 additions & 8 deletions runtime/wasm/fail.wat
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,10 @@

(data $index_out_of_bounds "index out of bounds")

(func (export "caml_bound_error") (result (ref eq))
(call $caml_invalid_argument
(func (export "caml_bound_error")
(return_call $caml_invalid_argument
(array.new_data $string $index_out_of_bounds
(i32.const 0) (i32.const 19)))
(ref.i31 (i32.const 0)))
(i32.const 0) (i32.const 19))))

(global $END_OF_FILE_EXN i32 (i32.const 4))

Expand All @@ -88,11 +87,10 @@

(global $ZERO_DIVIDE_EXN i32 (i32.const 5))

(func (export "caml_raise_zero_divide") (result (ref eq))
(call $caml_raise_constant
(func (export "caml_raise_zero_divide")
(return_call $caml_raise_constant
(array.get $block (global.get $caml_global_data)
(global.get $ZERO_DIVIDE_EXN)))
(ref.i31 (i32.const 0)))
(global.get $ZERO_DIVIDE_EXN))))

(global $NOT_FOUND_EXN i32 (i32.const 6))

Expand Down
26 changes: 13 additions & 13 deletions runtime/wasm/string.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

(module
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
(import "fail" "caml_bound_error" (func $caml_bound_error))
(import "fail" "caml_invalid_argument"
(func $caml_invalid_argument (param $arg (ref eq))))
(import "int32" "caml_copy_int32"
Expand Down Expand Up @@ -170,10 +170,10 @@
(local.set $s (ref.cast (ref $string) (local.get $v)))
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
(array.len (local.get $s)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(ref.i31 (i32.or
(array.get_u $string (local.get $s) (local.get $p))
(i32.shl (array.get_u $string (local.get $s)
Expand All @@ -187,10 +187,10 @@
(local.set $s (ref.cast (ref $string) (local.get $v)))
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
(array.len (local.get $s)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(return_call $caml_copy_int32
(i32.or
(i32.or
Expand All @@ -213,10 +213,10 @@
(local.set $s (ref.cast (ref $string) (local.get $v)))
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
(array.len (local.get $s)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(return_call $caml_copy_int64
(i64.or
(i64.or
Expand Down Expand Up @@ -263,10 +263,10 @@
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
(local.set $v (i31.get_s (ref.cast (ref i31) (local.get 2))))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
(array.len (local.get $s)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(array.set $string (local.get $s) (local.get $p) (local.get $v))
(array.set $string (local.get $s)
(i32.add (local.get $p) (i32.const 1))
Expand All @@ -280,10 +280,10 @@
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
(local.set $v (call $Int32_val (local.get 2)))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
(array.len (local.get $s)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(array.set $string (local.get $s) (local.get $p) (local.get $v))
(array.set $string (local.get $s)
(i32.add (local.get $p) (i32.const 1))
Expand All @@ -303,10 +303,10 @@
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
(local.set $v (call $Int64_val (local.get 2)))
(if (i32.lt_s (local.get $p) (i32.const 0))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
(array.len (local.get $s)))
(then (return_call $caml_bound_error)))
(then (call $caml_bound_error)))
(array.set $string (local.get $s) (local.get $p)
(i32.wrap_i64 (local.get $v)))
(array.set $string (local.get $s)
Expand Down

0 comments on commit 88acffc

Please sign in to comment.