Skip to content

Commit 70e4df2

Browse files
committed
Revert "Simplify handling of bound errors and division by zero"
This reverts commit fc651d6.
1 parent d7a52fa commit 70e4df2

File tree

5 files changed

+54
-54
lines changed

5 files changed

+54
-54
lines changed

Diff for: .github/workflows/build-wasm_of_ocaml.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: actions/cache/restore@v4
3939
with:
4040
path: binaryen
41-
key: ${{ runner.os }}-binaryen-version_118
41+
key: ${{ runner.os }}-binaryen-eh
4242

4343
- name: Checkout binaryen
4444
if: steps.cache-binaryen.outputs.cache-hit != 'true'
@@ -47,7 +47,7 @@ jobs:
4747
repository: WebAssembly/binaryen
4848
path: binaryen
4949
submodules: true
50-
ref: version_118
50+
ref: d200d06fef7f4edd331c1f928493beb332a2d910
5151

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

7070
- name: Set binaryen's path
7171
run: |

Diff for: compiler/lib/wasm/wa_generate.ml

+9-7
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,11 @@ module Generate (Target : Wa_target_sig.S) = struct
779779
{ params = []; result = [] }
780780
(body ~result_typ:[] ~fall_through:(`Block pc) ~context:(`Block pc :: context))
781781
in
782-
handler
782+
if List.is_empty result_typ
783+
then handler
784+
else
785+
let* () = handler in
786+
instr (W.Return (Some (RefI31 (Const (I32 0l)))))
783787
else body ~result_typ ~fall_through ~context
784788

785789
let wrap_with_handlers p pc ~result_typ ~fall_through ~context body =
@@ -788,20 +792,18 @@ module Generate (Target : Wa_target_sig.S) = struct
788792
need_bound_error_handler
789793
bound_error_pc
790794
(let* f =
791-
register_import
792-
~name:"caml_bound_error"
793-
(Fun { params = []; result = [ Value.value ] })
795+
register_import ~name:"caml_bound_error" (Fun { params = []; result = [] })
794796
in
795-
instr (Return_call (f, [])))
797+
instr (CallInstr (f, [])))
796798
(wrap_with_handler
797799
need_zero_divide_handler
798800
zero_divide_pc
799801
(let* f =
800802
register_import
801803
~name:"caml_raise_zero_divide"
802-
(Fun { params = []; result = [ Value.value ] })
804+
(Fun { params = []; result = [] })
803805
in
804-
instr (Return_call (f, [])))
806+
instr (CallInstr (f, [])))
805807
body)
806808
~result_typ
807809
~fall_through

Diff for: runtime/wasm/bigarray.wat

+23-23
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
(import "bindings" "ta_subarray"
6262
(func $ta_subarray
6363
(param (ref extern)) (param i32) (param i32) (result (ref extern))))
64-
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
64+
(import "fail" "caml_bound_error" (func $caml_bound_error))
6565
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
6666
(import "fail" "caml_invalid_argument"
6767
(func $caml_invalid_argument (param (ref eq))))
@@ -970,7 +970,7 @@
970970
(if (i32.ge_u (local.get $i)
971971
(array.get $int_array (struct.get $bigarray 2 (local.get $ba))
972972
(i32.const 0)))
973-
(then (return_call $caml_bound_error)))
973+
(then (call $caml_bound_error)))
974974
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $i)))
975975

976976
(func (export "caml_ba_set_1")
@@ -984,7 +984,7 @@
984984
(if (i32.ge_u (local.get $i)
985985
(array.get $int_array (struct.get $bigarray $ba_dim (local.get $ba))
986986
(i32.const 0)))
987-
(then (return_call $caml_bound_error)))
987+
(then (call $caml_bound_error)))
988988
(call $caml_ba_set_at_offset
989989
(local.get $ba) (local.get $i) (local.get $v))
990990
(ref.i31 (i32.const 0)))
@@ -1022,7 +1022,7 @@
10221022
(i32.ge_u (local.get $j)
10231023
(array.get $int_array (local.get $dim) (i32.const 1))))
10241024
(then
1025-
(return_call $caml_bound_error)))
1025+
(call $caml_bound_error)))
10261026
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
10271027

10281028
(func (export "caml_ba_set_2")
@@ -1058,7 +1058,7 @@
10581058
(i32.ge_u (local.get $j)
10591059
(array.get $int_array (local.get $dim) (i32.const 1))))
10601060
(then
1061-
(return_call $caml_bound_error)))
1061+
(call $caml_bound_error)))
10621062
(call $caml_ba_set_at_offset
10631063
(local.get $ba) (local.get $offset) (local.get $v))
10641064
(ref.i31 (i32.const 0)))
@@ -1116,7 +1116,7 @@
11161116
(i32.ge_u (local.get $j)
11171117
(array.get $int_array (local.get $dim) (i32.const 2)))))
11181118
(then
1119-
(return_call $caml_bound_error)))
1119+
(call $caml_bound_error)))
11201120
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
11211121

11221122
(func (export "caml_ba_set_3")
@@ -1169,7 +1169,7 @@
11691169
(i32.ge_u (local.get $k)
11701170
(array.get $int_array (local.get $dim) (i32.const 2)))))
11711171
(then
1172-
(return_call $caml_bound_error)))
1172+
(call $caml_bound_error)))
11731173
(call $caml_ba_set_at_offset
11741174
(local.get $ba) (local.get $offset) (local.get $v))
11751175
(ref.i31 (i32.const 0)))
@@ -1200,7 +1200,7 @@
12001200
(array.get $int_array (local.get $dim) (local.get $i)))
12011201
(if (i32.ge_u (local.get $idx) (local.get $l))
12021202
(then
1203-
(drop (call $caml_bound_error))))
1203+
(call $caml_bound_error)))
12041204
(local.set $offset
12051205
(i32.add (i32.mul (local.get $offset) (local.get $l))
12061206
(local.get $idx)))
@@ -1218,7 +1218,7 @@
12181218
(array.get $int_array (local.get $dim) (local.get $i)))
12191219
(if (i32.ge_u (local.get $idx) (local.get $l))
12201220
(then
1221-
(drop (call $caml_bound_error))))
1221+
(call $caml_bound_error)))
12221222
(local.set $offset
12231223
(i32.add (i32.mul (local.get $offset) (local.get $l))
12241224
(local.get $idx)))
@@ -1251,7 +1251,7 @@
12511251
(array.get $int_array (local.get $dim) (local.get $i)))
12521252
(if (i32.ge_u (local.get $idx) (local.get $l))
12531253
(then
1254-
(drop (call $caml_bound_error))))
1254+
(call $caml_bound_error)))
12551255
(local.set $offset
12561256
(i32.add (i32.mul (local.get $offset) (local.get $l))
12571257
(local.get $idx)))
@@ -1272,7 +1272,7 @@
12721272
(array.get $int_array (local.get $dim) (local.get $i)))
12731273
(if (i32.ge_u (local.get $idx) (local.get $l))
12741274
(then
1275-
(drop (call $caml_bound_error))))
1275+
(call $caml_bound_error)))
12761276
(local.set $offset
12771277
(i32.add (i32.mul (local.get $offset) (local.get $l))
12781278
(local.get $idx)))
@@ -1906,12 +1906,12 @@
19061906
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19071907
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19081908
(if (i32.lt_s (local.get $p) (i32.const 0))
1909-
(then (return_call $caml_bound_error)))
1909+
(then (call $caml_bound_error)))
19101910
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
19111911
(array.get $int_array
19121912
(struct.get $bigarray $ba_dim (local.get $ba))
19131913
(i32.const 0)))
1914-
(then (return_call $caml_bound_error)))
1914+
(then (call $caml_bound_error)))
19151915
(ref.i31 (i32.or
19161916
(call $ta_get_ui8 (local.get $data) (local.get $p))
19171917
(i32.shl (call $ta_get_ui8 (local.get $data)
@@ -1927,12 +1927,12 @@
19271927
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19281928
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19291929
(if (i32.lt_s (local.get $p) (i32.const 0))
1930-
(then (return_call $caml_bound_error)))
1930+
(then (call $caml_bound_error)))
19311931
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
19321932
(array.get $int_array
19331933
(struct.get $bigarray $ba_dim (local.get $ba))
19341934
(i32.const 0)))
1935-
(then (return_call $caml_bound_error)))
1935+
(then (call $caml_bound_error)))
19361936
(return_call $caml_copy_int32
19371937
(i32.or
19381938
(i32.or
@@ -1957,12 +1957,12 @@
19571957
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19581958
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19591959
(if (i32.lt_s (local.get $p) (i32.const 0))
1960-
(then (return_call $caml_bound_error)))
1960+
(then (call $caml_bound_error)))
19611961
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
19621962
(array.get $int_array
19631963
(struct.get $bigarray $ba_dim (local.get $ba))
19641964
(i32.const 0)))
1965-
(then (return_call $caml_bound_error)))
1965+
(then (call $caml_bound_error)))
19661966
(return_call $caml_copy_int64
19671967
(i64.or
19681968
(i64.or
@@ -2013,12 +2013,12 @@
20132013
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20142014
(local.set $d (ref.cast (ref i31) (local.get $v)))
20152015
(if (i32.lt_s (local.get $p) (i32.const 0))
2016-
(then (return_call $caml_bound_error)))
2016+
(then (call $caml_bound_error)))
20172017
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
20182018
(array.get $int_array
20192019
(struct.get $bigarray $ba_dim (local.get $ba))
20202020
(i32.const 0)))
2021-
(then (return_call $caml_bound_error)))
2021+
(then (call $caml_bound_error)))
20222022
(call $ta_set_ui8 (local.get $data) (local.get $p) (local.get $d))
20232023
(call $ta_set_ui8 (local.get $data)
20242024
(i32.add (local.get $p) (i32.const 1))
@@ -2036,12 +2036,12 @@
20362036
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20372037
(local.set $d (call $Int32_val (local.get $v)))
20382038
(if (i32.lt_s (local.get $p) (i32.const 0))
2039-
(then (return_call $caml_bound_error)))
2039+
(then (call $caml_bound_error)))
20402040
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
20412041
(array.get $int_array
20422042
(struct.get $bigarray $ba_dim (local.get $ba))
20432043
(i32.const 0)))
2044-
(then (return_call $caml_bound_error)))
2044+
(then (call $caml_bound_error)))
20452045
(call $ta_set_ui8 (local.get $data) (local.get $p)
20462046
(ref.i31 (local.get $d)))
20472047
(call $ta_set_ui8 (local.get $data)
@@ -2066,12 +2066,12 @@
20662066
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20672067
(local.set $d (call $Int64_val (local.get $v)))
20682068
(if (i32.lt_s (local.get $p) (i32.const 0))
2069-
(then (return_call $caml_bound_error)))
2069+
(then (call $caml_bound_error)))
20702070
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
20712071
(array.get $int_array
20722072
(struct.get $bigarray $ba_dim (local.get $ba))
20732073
(i32.const 0)))
2074-
(then (return_call $caml_bound_error)))
2074+
(then (call $caml_bound_error)))
20752075
(call $ta_set_ui8 (local.get $data) (local.get $p)
20762076
(ref.i31 (i32.wrap_i64 (local.get $d))))
20772077
(call $ta_set_ui8 (local.get $data)

Diff for: runtime/wasm/fail.wat

+6-8
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,10 @@
7373

7474
(data $index_out_of_bounds "index out of bounds")
7575

76-
(func (export "caml_bound_error") (result (ref eq))
77-
(call $caml_invalid_argument
76+
(func (export "caml_bound_error")
77+
(return_call $caml_invalid_argument
7878
(array.new_data $string $index_out_of_bounds
79-
(i32.const 0) (i32.const 19)))
80-
(ref.i31 (i32.const 0)))
79+
(i32.const 0) (i32.const 19))))
8180

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

@@ -88,11 +87,10 @@
8887

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

91-
(func (export "caml_raise_zero_divide") (result (ref eq))
92-
(call $caml_raise_constant
90+
(func (export "caml_raise_zero_divide")
91+
(return_call $caml_raise_constant
9392
(array.get $block (global.get $caml_global_data)
94-
(global.get $ZERO_DIVIDE_EXN)))
95-
(ref.i31 (i32.const 0)))
93+
(global.get $ZERO_DIVIDE_EXN))))
9694

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

Diff for: runtime/wasm/string.wat

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717

1818
(module
19-
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
19+
(import "fail" "caml_bound_error" (func $caml_bound_error))
2020
(import "fail" "caml_invalid_argument"
2121
(func $caml_invalid_argument (param $arg (ref eq))))
2222
(import "int32" "caml_copy_int32"
@@ -170,10 +170,10 @@
170170
(local.set $s (ref.cast (ref $string) (local.get $v)))
171171
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
172172
(if (i32.lt_s (local.get $p) (i32.const 0))
173-
(then (return_call $caml_bound_error)))
173+
(then (call $caml_bound_error)))
174174
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
175175
(array.len (local.get $s)))
176-
(then (return_call $caml_bound_error)))
176+
(then (call $caml_bound_error)))
177177
(ref.i31 (i32.or
178178
(array.get_u $string (local.get $s) (local.get $p))
179179
(i32.shl (array.get_u $string (local.get $s)
@@ -187,10 +187,10 @@
187187
(local.set $s (ref.cast (ref $string) (local.get $v)))
188188
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
189189
(if (i32.lt_s (local.get $p) (i32.const 0))
190-
(then (return_call $caml_bound_error)))
190+
(then (call $caml_bound_error)))
191191
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
192192
(array.len (local.get $s)))
193-
(then (return_call $caml_bound_error)))
193+
(then (call $caml_bound_error)))
194194
(return_call $caml_copy_int32
195195
(i32.or
196196
(i32.or
@@ -213,10 +213,10 @@
213213
(local.set $s (ref.cast (ref $string) (local.get $v)))
214214
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
215215
(if (i32.lt_s (local.get $p) (i32.const 0))
216-
(then (return_call $caml_bound_error)))
216+
(then (call $caml_bound_error)))
217217
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
218218
(array.len (local.get $s)))
219-
(then (return_call $caml_bound_error)))
219+
(then (call $caml_bound_error)))
220220
(return_call $caml_copy_int64
221221
(i64.or
222222
(i64.or
@@ -263,10 +263,10 @@
263263
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
264264
(local.set $v (i31.get_s (ref.cast (ref i31) (local.get 2))))
265265
(if (i32.lt_s (local.get $p) (i32.const 0))
266-
(then (return_call $caml_bound_error)))
266+
(then (call $caml_bound_error)))
267267
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
268268
(array.len (local.get $s)))
269-
(then (return_call $caml_bound_error)))
269+
(then (call $caml_bound_error)))
270270
(array.set $string (local.get $s) (local.get $p) (local.get $v))
271271
(array.set $string (local.get $s)
272272
(i32.add (local.get $p) (i32.const 1))
@@ -280,10 +280,10 @@
280280
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
281281
(local.set $v (call $Int32_val (local.get 2)))
282282
(if (i32.lt_s (local.get $p) (i32.const 0))
283-
(then (return_call $caml_bound_error)))
283+
(then (call $caml_bound_error)))
284284
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
285285
(array.len (local.get $s)))
286-
(then (return_call $caml_bound_error)))
286+
(then (call $caml_bound_error)))
287287
(array.set $string (local.get $s) (local.get $p) (local.get $v))
288288
(array.set $string (local.get $s)
289289
(i32.add (local.get $p) (i32.const 1))
@@ -303,10 +303,10 @@
303303
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
304304
(local.set $v (call $Int64_val (local.get 2)))
305305
(if (i32.lt_s (local.get $p) (i32.const 0))
306-
(then (return_call $caml_bound_error)))
306+
(then (call $caml_bound_error)))
307307
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
308308
(array.len (local.get $s)))
309-
(then (return_call $caml_bound_error)))
309+
(then (call $caml_bound_error)))
310310
(array.set $string (local.get $s) (local.get $p)
311311
(i32.wrap_i64 (local.get $v)))
312312
(array.set $string (local.get $s)

0 commit comments

Comments
 (0)