From bb3b65c230d64639f30d6657ee9f70e6cec3970a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vouillon?= Date: Tue, 20 Aug 2024 21:33:04 +0200 Subject: [PATCH] Revert "Simplify handling of bound errors and division by zero" This reverts commit fc651d6352b70822017b888e97158ec144fac73e. --- compiler/lib-wasm/generate.ml | 16 ++++++------ runtime/wasm/bigarray.wat | 46 +++++++++++++++++------------------ runtime/wasm/fail.wat | 14 +++++------ runtime/wasm/string.wat | 26 ++++++++++---------- 4 files changed, 51 insertions(+), 51 deletions(-) diff --git a/compiler/lib-wasm/generate.ml b/compiler/lib-wasm/generate.ml index 93edc4323a..308f8d5602 100644 --- a/compiler/lib-wasm/generate.ml +++ b/compiler/lib-wasm/generate.ml @@ -816,7 +816,11 @@ module Generate (Target : 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 = @@ -825,20 +829,18 @@ module Generate (Target : 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 diff --git a/runtime/wasm/bigarray.wat b/runtime/wasm/bigarray.wat index c981dedf0c..812055eca7 100644 --- a/runtime/wasm/bigarray.wat +++ b/runtime/wasm/bigarray.wat @@ -77,7 +77,7 @@ (func $ta_blit_to_string (param (ref extern)) (param i32) (param (ref $string)) (param i32) (param i32))) - (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)))) @@ -974,7 +974,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") @@ -988,7 +988,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))) @@ -1026,7 +1026,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") @@ -1062,7 +1062,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))) @@ -1120,7 +1120,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") @@ -1173,7 +1173,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))) @@ -1204,7 +1204,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))) @@ -1222,7 +1222,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))) @@ -1255,7 +1255,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))) @@ -1276,7 +1276,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))) @@ -1910,12 +1910,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 (call $ta_get16_ui8 (local.get $data) (local.get $p)))) (func (export "caml_ba_uint8_get32") @@ -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 $ta_get32_ui8 (local.get $data) (local.get $p))) (func (export "caml_ba_uint8_get64") @@ -1944,12 +1944,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))) (i64.or (i64.extend_i32_u (call $ta_get32_ui8 (local.get $data) (local.get $p))) @@ -1969,12 +1969,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_set16_ui8 (local.get $data) (local.get $p) (local.get $d)) (ref.i31 (i32.const 0))) @@ -1988,12 +1988,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))) (call $ta_set32_ui8 (local.get $data) (local.get $p) (local.get $d)) (ref.i31 (i32.const 0))) @@ -2007,12 +2007,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))) (call $ta_set32_ui8 (local.get $data) (local.get $p) (i32.wrap_i64 (local.get $d))) (call $ta_set32_ui8 (local.get $data) diff --git a/runtime/wasm/fail.wat b/runtime/wasm/fail.wat index a19522030f..e3dc000d55 100644 --- a/runtime/wasm/fail.wat +++ b/runtime/wasm/fail.wat @@ -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)) @@ -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)) diff --git a/runtime/wasm/string.wat b/runtime/wasm/string.wat index 5bcf93ab87..bf43b2f9e4 100644 --- a/runtime/wasm/string.wat +++ b/runtime/wasm/string.wat @@ -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)))) @@ -162,10 +162,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) @@ -179,10 +179,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))) (i32.or (i32.or (array.get_u $string (local.get $s) (local.get $p)) @@ -204,10 +204,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))) (i64.or (i64.or (i64.or @@ -253,10 +253,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)) @@ -269,10 +269,10 @@ (local.set $s (ref.cast (ref $string) (local.get 0))) (local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1)))) (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)) @@ -291,10 +291,10 @@ (local.set $s (ref.cast (ref $string) (local.get 0))) (local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1)))) (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)