Skip to content

Commit 939fda0

Browse files
committed
Simplify handling of bound errors and division by zero
1 parent e2c9df1 commit 939fda0

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

compiler/lib/wasm/wa_generate.ml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,7 @@ module Generate (Target : Wa_target_sig.S) = struct
761761
{ params = []; result = [] }
762762
(body ~result_typ:[] ~fall_through:(`Block pc) ~context:(`Block pc :: context))
763763
in
764-
if List.is_empty result_typ
765-
then handler
766-
else
767-
let* () = handler in
768-
instr (W.Return (Some (RefI31 (Const (I32 0l)))))
764+
handler
769765
else body ~result_typ ~fall_through ~context
770766

771767
let wrap_with_handlers p pc ~result_typ ~fall_through ~context body =
@@ -774,18 +770,20 @@ module Generate (Target : Wa_target_sig.S) = struct
774770
need_bound_error_handler
775771
bound_error_pc
776772
(let* f =
777-
register_import ~name:"caml_bound_error" (Fun { params = []; result = [] })
773+
register_import
774+
~name:"caml_bound_error"
775+
(Fun { params = []; result = [ Value.value ] })
778776
in
779-
instr (CallInstr (f, [])))
777+
instr (Return_call (f, [])))
780778
(wrap_with_handler
781779
need_zero_divide_handler
782780
zero_divide_pc
783781
(let* f =
784782
register_import
785783
~name:"caml_raise_zero_divide"
786-
(Fun { params = []; result = [] })
784+
(Fun { params = []; result = [ Value.value ] })
787785
in
788-
instr (CallInstr (f, [])))
786+
instr (Return_call (f, [])))
789787
body)
790788
~result_typ
791789
~fall_through

runtime/wasm/bigarray.wat

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
(import "bindings" "ta_subarray"
4545
(func $ta_subarray
4646
(param (ref extern)) (param i32) (param i32) (result (ref extern))))
47-
(import "fail" "caml_bound_error" (func $caml_bound_error))
47+
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
4848
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
4949
(import "fail" "caml_invalid_argument"
5050
(func $caml_invalid_argument (param (ref eq))))
@@ -953,7 +953,7 @@
953953
(if (i32.ge_u (local.get $i)
954954
(array.get $int_array (struct.get $bigarray 2 (local.get $ba))
955955
(i32.const 0)))
956-
(then (call $caml_bound_error)))
956+
(then (return_call $caml_bound_error)))
957957
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $i)))
958958

959959
(func (export "caml_ba_set_1")
@@ -967,7 +967,7 @@
967967
(if (i32.ge_u (local.get $i)
968968
(array.get $int_array (struct.get $bigarray $ba_dim (local.get $ba))
969969
(i32.const 0)))
970-
(then (call $caml_bound_error)))
970+
(then (return_call $caml_bound_error)))
971971
(call $caml_ba_set_at_offset
972972
(local.get $ba) (local.get $i) (local.get $v))
973973
(ref.i31 (i32.const 0)))
@@ -1005,7 +1005,7 @@
10051005
(i32.ge_u (local.get $j)
10061006
(array.get $int_array (local.get $dim) (i32.const 1))))
10071007
(then
1008-
(call $caml_bound_error)))
1008+
(return_call $caml_bound_error)))
10091009
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
10101010

10111011
(func (export "caml_ba_set_2")
@@ -1041,7 +1041,7 @@
10411041
(i32.ge_u (local.get $j)
10421042
(array.get $int_array (local.get $dim) (i32.const 1))))
10431043
(then
1044-
(call $caml_bound_error)))
1044+
(return_call $caml_bound_error)))
10451045
(call $caml_ba_set_at_offset
10461046
(local.get $ba) (local.get $offset) (local.get $v))
10471047
(ref.i31 (i32.const 0)))
@@ -1099,7 +1099,7 @@
10991099
(i32.ge_u (local.get $j)
11001100
(array.get $int_array (local.get $dim) (i32.const 2)))))
11011101
(then
1102-
(call $caml_bound_error)))
1102+
(return_call $caml_bound_error)))
11031103
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
11041104

11051105
(func (export "caml_ba_set_3")
@@ -1152,7 +1152,7 @@
11521152
(i32.ge_u (local.get $k)
11531153
(array.get $int_array (local.get $dim) (i32.const 2)))))
11541154
(then
1155-
(call $caml_bound_error)))
1155+
(return_call $caml_bound_error)))
11561156
(call $caml_ba_set_at_offset
11571157
(local.get $ba) (local.get $offset) (local.get $v))
11581158
(ref.i31 (i32.const 0)))
@@ -1183,7 +1183,7 @@
11831183
(array.get $int_array (local.get $dim) (local.get $i)))
11841184
(if (i32.ge_u (local.get $idx) (local.get $l))
11851185
(then
1186-
(call $caml_bound_error)))
1186+
(drop (call $caml_bound_error))))
11871187
(local.set $offset
11881188
(i32.add (i32.mul (local.get $offset) (local.get $l))
11891189
(local.get $idx)))
@@ -1201,7 +1201,7 @@
12011201
(array.get $int_array (local.get $dim) (local.get $i)))
12021202
(if (i32.ge_u (local.get $idx) (local.get $l))
12031203
(then
1204-
(call $caml_bound_error)))
1204+
(drop (call $caml_bound_error))))
12051205
(local.set $offset
12061206
(i32.add (i32.mul (local.get $offset) (local.get $l))
12071207
(local.get $idx)))
@@ -1234,7 +1234,7 @@
12341234
(array.get $int_array (local.get $dim) (local.get $i)))
12351235
(if (i32.ge_u (local.get $idx) (local.get $l))
12361236
(then
1237-
(call $caml_bound_error)))
1237+
(drop (call $caml_bound_error))))
12381238
(local.set $offset
12391239
(i32.add (i32.mul (local.get $offset) (local.get $l))
12401240
(local.get $idx)))
@@ -1255,7 +1255,7 @@
12551255
(array.get $int_array (local.get $dim) (local.get $i)))
12561256
(if (i32.ge_u (local.get $idx) (local.get $l))
12571257
(then
1258-
(call $caml_bound_error)))
1258+
(drop (call $caml_bound_error))))
12591259
(local.set $offset
12601260
(i32.add (i32.mul (local.get $offset) (local.get $l))
12611261
(local.get $idx)))
@@ -1889,12 +1889,12 @@
18891889
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
18901890
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
18911891
(if (i32.lt_s (local.get $p) (i32.const 0))
1892-
(then (call $caml_bound_error)))
1892+
(then (return_call $caml_bound_error)))
18931893
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
18941894
(array.get $int_array
18951895
(struct.get $bigarray $ba_dim (local.get $ba))
18961896
(i32.const 0)))
1897-
(then (call $caml_bound_error)))
1897+
(then (return_call $caml_bound_error)))
18981898
(ref.i31 (i32.or
18991899
(call $ta_get_ui8 (local.get $data) (local.get $p))
19001900
(i32.shl (call $ta_get_ui8 (local.get $data)
@@ -1910,12 +1910,12 @@
19101910
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19111911
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19121912
(if (i32.lt_s (local.get $p) (i32.const 0))
1913-
(then (call $caml_bound_error)))
1913+
(then (return_call $caml_bound_error)))
19141914
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
19151915
(array.get $int_array
19161916
(struct.get $bigarray $ba_dim (local.get $ba))
19171917
(i32.const 0)))
1918-
(then (call $caml_bound_error)))
1918+
(then (return_call $caml_bound_error)))
19191919
(return_call $caml_copy_int32
19201920
(i32.or
19211921
(i32.or
@@ -1940,12 +1940,12 @@
19401940
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19411941
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19421942
(if (i32.lt_s (local.get $p) (i32.const 0))
1943-
(then (call $caml_bound_error)))
1943+
(then (return_call $caml_bound_error)))
19441944
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
19451945
(array.get $int_array
19461946
(struct.get $bigarray $ba_dim (local.get $ba))
19471947
(i32.const 0)))
1948-
(then (call $caml_bound_error)))
1948+
(then (return_call $caml_bound_error)))
19491949
(return_call $caml_copy_int64
19501950
(i64.or
19511951
(i64.or
@@ -1996,12 +1996,12 @@
19961996
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19971997
(local.set $d (ref.cast (ref i31) (local.get $v)))
19981998
(if (i32.lt_s (local.get $p) (i32.const 0))
1999-
(then (call $caml_bound_error)))
1999+
(then (return_call $caml_bound_error)))
20002000
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
20012001
(array.get $int_array
20022002
(struct.get $bigarray $ba_dim (local.get $ba))
20032003
(i32.const 0)))
2004-
(then (call $caml_bound_error)))
2004+
(then (return_call $caml_bound_error)))
20052005
(call $ta_set_ui8 (local.get $data) (local.get $p) (local.get $d))
20062006
(call $ta_set_ui8 (local.get $data)
20072007
(i32.add (local.get $p) (i32.const 1))
@@ -2019,12 +2019,12 @@
20192019
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20202020
(local.set $d (call $Int32_val (local.get $v)))
20212021
(if (i32.lt_s (local.get $p) (i32.const 0))
2022-
(then (call $caml_bound_error)))
2022+
(then (return_call $caml_bound_error)))
20232023
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
20242024
(array.get $int_array
20252025
(struct.get $bigarray $ba_dim (local.get $ba))
20262026
(i32.const 0)))
2027-
(then (call $caml_bound_error)))
2027+
(then (return_call $caml_bound_error)))
20282028
(call $ta_set_ui8 (local.get $data) (local.get $p)
20292029
(ref.i31 (local.get $d)))
20302030
(call $ta_set_ui8 (local.get $data)
@@ -2049,12 +2049,12 @@
20492049
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20502050
(local.set $d (call $Int64_val (local.get $v)))
20512051
(if (i32.lt_s (local.get $p) (i32.const 0))
2052-
(then (call $caml_bound_error)))
2052+
(then (return_call $caml_bound_error)))
20532053
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
20542054
(array.get $int_array
20552055
(struct.get $bigarray $ba_dim (local.get $ba))
20562056
(i32.const 0)))
2057-
(then (call $caml_bound_error)))
2057+
(then (return_call $caml_bound_error)))
20582058
(call $ta_set_ui8 (local.get $data) (local.get $p)
20592059
(ref.i31 (i32.wrap_i64 (local.get $d))))
20602060
(call $ta_set_ui8 (local.get $data)

runtime/wasm/fail.wat

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@
5656

5757
(data $index_out_of_bounds "index out of bounds")
5858

59-
(func (export "caml_bound_error")
60-
(return_call $caml_invalid_argument
59+
(func (export "caml_bound_error") (result (ref eq))
60+
(call $caml_invalid_argument
6161
(array.new_data $string $index_out_of_bounds
62-
(i32.const 0) (i32.const 19))))
62+
(i32.const 0) (i32.const 19)))
63+
(i31.new (i32.const 0)))
6364

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

@@ -70,10 +71,11 @@
7071

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

73-
(func (export "caml_raise_zero_divide")
74-
(return_call $caml_raise_constant
74+
(func (export "caml_raise_zero_divide") (result (ref eq))
75+
(call $caml_raise_constant
7576
(array.get $block (global.get $caml_global_data)
76-
(global.get $ZERO_DIVIDE_EXN))))
77+
(global.get $ZERO_DIVIDE_EXN)))
78+
(i31.new (i32.const 0)))
7779

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

runtime/wasm/string.wat

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(module
2-
(import "fail" "caml_bound_error" (func $caml_bound_error))
2+
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
33
(import "fail" "caml_invalid_argument"
44
(func $caml_invalid_argument (param $arg (ref eq))))
55
(import "int32" "caml_copy_int32"
@@ -153,10 +153,10 @@
153153
(local.set $s (ref.cast (ref $string) (local.get $v)))
154154
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
155155
(if (i32.lt_s (local.get $p) (i32.const 0))
156-
(then (call $caml_bound_error)))
156+
(then (return_call $caml_bound_error)))
157157
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
158158
(array.len (local.get $s)))
159-
(then (call $caml_bound_error)))
159+
(then (return_call $caml_bound_error)))
160160
(ref.i31 (i32.or
161161
(array.get_u $string (local.get $s) (local.get $p))
162162
(i32.shl (array.get_u $string (local.get $s)
@@ -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 (call $caml_bound_error)))
173+
(then (return_call $caml_bound_error)))
174174
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
175175
(array.len (local.get $s)))
176-
(then (call $caml_bound_error)))
176+
(then (return_call $caml_bound_error)))
177177
(return_call $caml_copy_int32
178178
(i32.or
179179
(i32.or
@@ -196,10 +196,10 @@
196196
(local.set $s (ref.cast (ref $string) (local.get $v)))
197197
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
198198
(if (i32.lt_s (local.get $p) (i32.const 0))
199-
(then (call $caml_bound_error)))
199+
(then (return_call $caml_bound_error)))
200200
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
201201
(array.len (local.get $s)))
202-
(then (call $caml_bound_error)))
202+
(then (return_call $caml_bound_error)))
203203
(return_call $caml_copy_int64
204204
(i64.or
205205
(i64.or
@@ -246,10 +246,10 @@
246246
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
247247
(local.set $v (i31.get_s (ref.cast (ref i31) (local.get 2))))
248248
(if (i32.lt_s (local.get $p) (i32.const 0))
249-
(then (call $caml_bound_error)))
249+
(then (return_call $caml_bound_error)))
250250
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
251251
(array.len (local.get $s)))
252-
(then (call $caml_bound_error)))
252+
(then (return_call $caml_bound_error)))
253253
(array.set $string (local.get $s) (local.get $p) (local.get $v))
254254
(array.set $string (local.get $s)
255255
(i32.add (local.get $p) (i32.const 1))
@@ -263,10 +263,10 @@
263263
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
264264
(local.set $v (call $Int32_val (local.get 2)))
265265
(if (i32.lt_s (local.get $p) (i32.const 0))
266-
(then (call $caml_bound_error)))
266+
(then (return_call $caml_bound_error)))
267267
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
268268
(array.len (local.get $s)))
269-
(then (call $caml_bound_error)))
269+
(then (return_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))
@@ -286,10 +286,10 @@
286286
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
287287
(local.set $v (call $Int64_val (local.get 2)))
288288
(if (i32.lt_s (local.get $p) (i32.const 0))
289-
(then (call $caml_bound_error)))
289+
(then (return_call $caml_bound_error)))
290290
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
291291
(array.len (local.get $s)))
292-
(then (call $caml_bound_error)))
292+
(then (return_call $caml_bound_error)))
293293
(array.set $string (local.get $s) (local.get $p)
294294
(i32.wrap_i64 (local.get $v)))
295295
(array.set $string (local.get $s)

0 commit comments

Comments
 (0)