Skip to content

Commit c042af6

Browse files
committed
Simplify handling of bound errors and division by zero
1 parent ec88745 commit c042af6

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

compiler/lib-wasm/generate.ml

+7-9
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,7 @@ module Generate (Target : Target_sig.S) = struct
816816
{ params = []; result = [] }
817817
(body ~result_typ:[] ~fall_through:(`Block pc) ~context:(`Block pc :: context))
818818
in
819-
if List.is_empty result_typ
820-
then handler
821-
else
822-
let* () = handler in
823-
instr (W.Return (Some (RefI31 (Const (I32 0l)))))
819+
handler
824820
else body ~result_typ ~fall_through ~context
825821

826822
let wrap_with_handlers p pc ~result_typ ~fall_through ~context body =
@@ -829,18 +825,20 @@ module Generate (Target : Target_sig.S) = struct
829825
need_bound_error_handler
830826
bound_error_pc
831827
(let* f =
832-
register_import ~name:"caml_bound_error" (Fun { params = []; result = [] })
828+
register_import
829+
~name:"caml_bound_error"
830+
(Fun { params = []; result = [ Value.value ] })
833831
in
834-
instr (CallInstr (f, [])))
832+
instr (Return_call (f, [])))
835833
(wrap_with_handler
836834
need_zero_divide_handler
837835
zero_divide_pc
838836
(let* f =
839837
register_import
840838
~name:"caml_raise_zero_divide"
841-
(Fun { params = []; result = [] })
839+
(Fun { params = []; result = [ Value.value ] })
842840
in
843-
instr (CallInstr (f, [])))
841+
instr (Return_call (f, [])))
844842
body)
845843
~result_typ
846844
~fall_through

runtime/wasm/bigarray.wat

+23-23
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
(func $ta_blit_to_string
7878
(param (ref extern)) (param i32) (param (ref $string)) (param i32)
7979
(param i32)))
80-
(import "fail" "caml_bound_error" (func $caml_bound_error))
80+
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
8181
(import "fail" "caml_raise_out_of_memory" (func $caml_raise_out_of_memory))
8282
(import "fail" "caml_invalid_argument"
8383
(func $caml_invalid_argument (param (ref eq))))
@@ -974,7 +974,7 @@
974974
(if (i32.ge_u (local.get $i)
975975
(array.get $int_array (struct.get $bigarray 2 (local.get $ba))
976976
(i32.const 0)))
977-
(then (call $caml_bound_error)))
977+
(then (return_call $caml_bound_error)))
978978
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $i)))
979979

980980
(func (export "caml_ba_set_1")
@@ -988,7 +988,7 @@
988988
(if (i32.ge_u (local.get $i)
989989
(array.get $int_array (struct.get $bigarray $ba_dim (local.get $ba))
990990
(i32.const 0)))
991-
(then (call $caml_bound_error)))
991+
(then (return_call $caml_bound_error)))
992992
(call $caml_ba_set_at_offset
993993
(local.get $ba) (local.get $i) (local.get $v))
994994
(ref.i31 (i32.const 0)))
@@ -1026,7 +1026,7 @@
10261026
(i32.ge_u (local.get $j)
10271027
(array.get $int_array (local.get $dim) (i32.const 1))))
10281028
(then
1029-
(call $caml_bound_error)))
1029+
(return_call $caml_bound_error)))
10301030
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
10311031

10321032
(func (export "caml_ba_set_2")
@@ -1062,7 +1062,7 @@
10621062
(i32.ge_u (local.get $j)
10631063
(array.get $int_array (local.get $dim) (i32.const 1))))
10641064
(then
1065-
(call $caml_bound_error)))
1065+
(return_call $caml_bound_error)))
10661066
(call $caml_ba_set_at_offset
10671067
(local.get $ba) (local.get $offset) (local.get $v))
10681068
(ref.i31 (i32.const 0)))
@@ -1120,7 +1120,7 @@
11201120
(i32.ge_u (local.get $k)
11211121
(array.get $int_array (local.get $dim) (i32.const 2)))))
11221122
(then
1123-
(call $caml_bound_error)))
1123+
(return_call $caml_bound_error)))
11241124
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
11251125

11261126
(func (export "caml_ba_set_3")
@@ -1173,7 +1173,7 @@
11731173
(i32.ge_u (local.get $k)
11741174
(array.get $int_array (local.get $dim) (i32.const 2)))))
11751175
(then
1176-
(call $caml_bound_error)))
1176+
(return_call $caml_bound_error)))
11771177
(call $caml_ba_set_at_offset
11781178
(local.get $ba) (local.get $offset) (local.get $v))
11791179
(ref.i31 (i32.const 0)))
@@ -1204,7 +1204,7 @@
12041204
(array.get $int_array (local.get $dim) (local.get $i)))
12051205
(if (i32.ge_u (local.get $idx) (local.get $l))
12061206
(then
1207-
(call $caml_bound_error)))
1207+
(drop (call $caml_bound_error))))
12081208
(local.set $offset
12091209
(i32.add (i32.mul (local.get $offset) (local.get $l))
12101210
(local.get $idx)))
@@ -1222,7 +1222,7 @@
12221222
(array.get $int_array (local.get $dim) (local.get $i)))
12231223
(if (i32.ge_u (local.get $idx) (local.get $l))
12241224
(then
1225-
(call $caml_bound_error)))
1225+
(drop (call $caml_bound_error))))
12261226
(local.set $offset
12271227
(i32.add (i32.mul (local.get $offset) (local.get $l))
12281228
(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)))
@@ -1276,7 +1276,7 @@
12761276
(array.get $int_array (local.get $dim) (local.get $i)))
12771277
(if (i32.ge_u (local.get $idx) (local.get $l))
12781278
(then
1279-
(call $caml_bound_error)))
1279+
(drop (call $caml_bound_error))))
12801280
(local.set $offset
12811281
(i32.add (i32.mul (local.get $offset) (local.get $l))
12821282
(local.get $idx)))
@@ -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 1))
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
(ref.i31 (call $ta_get16_ui8 (local.get $data) (local.get $p))))
19201920

19211921
(func (export "caml_ba_uint8_get32")
@@ -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 (call $caml_bound_error)))
1930+
(then (return_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 (call $caml_bound_error)))
1935+
(then (return_call $caml_bound_error)))
19361936
(return_call $ta_get32_ui8 (local.get $data) (local.get $p)))
19371937

19381938
(func (export "caml_ba_uint8_get64")
@@ -1944,12 +1944,12 @@
19441944
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19451945
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19461946
(if (i32.lt_s (local.get $p) (i32.const 0))
1947-
(then (call $caml_bound_error)))
1947+
(then (return_call $caml_bound_error)))
19481948
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
19491949
(array.get $int_array
19501950
(struct.get $bigarray $ba_dim (local.get $ba))
19511951
(i32.const 0)))
1952-
(then (call $caml_bound_error)))
1952+
(then (return_call $caml_bound_error)))
19531953
(i64.or
19541954
(i64.extend_i32_u
19551955
(call $ta_get32_ui8 (local.get $data) (local.get $p)))
@@ -1969,12 +1969,12 @@
19691969
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19701970
(local.set $d (ref.cast (ref i31) (local.get $v)))
19711971
(if (i32.lt_s (local.get $p) (i32.const 0))
1972-
(then (call $caml_bound_error)))
1972+
(then (return_call $caml_bound_error)))
19731973
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
19741974
(array.get $int_array
19751975
(struct.get $bigarray $ba_dim (local.get $ba))
19761976
(i32.const 0)))
1977-
(then (call $caml_bound_error)))
1977+
(then (return_call $caml_bound_error)))
19781978
(call $ta_set16_ui8 (local.get $data) (local.get $p) (local.get $d))
19791979
(ref.i31 (i32.const 0)))
19801980

@@ -1988,12 +1988,12 @@
19881988
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
19891989
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
19901990
(if (i32.lt_s (local.get $p) (i32.const 0))
1991-
(then (call $caml_bound_error)))
1991+
(then (return_call $caml_bound_error)))
19921992
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
19931993
(array.get $int_array
19941994
(struct.get $bigarray $ba_dim (local.get $ba))
19951995
(i32.const 0)))
1996-
(then (call $caml_bound_error)))
1996+
(then (return_call $caml_bound_error)))
19971997
(call $ta_set32_ui8 (local.get $data) (local.get $p) (local.get $d))
19981998
(ref.i31 (i32.const 0)))
19991999

@@ -2007,12 +2007,12 @@
20072007
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
20082008
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20092009
(if (i32.lt_s (local.get $p) (i32.const 0))
2010-
(then (call $caml_bound_error)))
2010+
(then (return_call $caml_bound_error)))
20112011
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
20122012
(array.get $int_array
20132013
(struct.get $bigarray $ba_dim (local.get $ba))
20142014
(i32.const 0)))
2015-
(then (call $caml_bound_error)))
2015+
(then (return_call $caml_bound_error)))
20162016
(call $ta_set32_ui8 (local.get $data) (local.get $p)
20172017
(i32.wrap_i64 (local.get $d)))
20182018
(call $ta_set32_ui8 (local.get $data)

runtime/wasm/fail.wat

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

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

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

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

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

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

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

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

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))
19+
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
2020
(import "fail" "caml_invalid_argument"
2121
(func $caml_invalid_argument (param $arg (ref eq))))
2222

@@ -162,10 +162,10 @@
162162
(local.set $s (ref.cast (ref $string) (local.get $v)))
163163
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
164164
(if (i32.lt_s (local.get $p) (i32.const 0))
165-
(then (call $caml_bound_error)))
165+
(then (return_call $caml_bound_error)))
166166
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
167167
(array.len (local.get $s)))
168-
(then (call $caml_bound_error)))
168+
(then (return_call $caml_bound_error)))
169169
(ref.i31 (i32.or
170170
(array.get_u $string (local.get $s) (local.get $p))
171171
(i32.shl (array.get_u $string (local.get $s)
@@ -179,10 +179,10 @@
179179
(local.set $s (ref.cast (ref $string) (local.get $v)))
180180
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
181181
(if (i32.lt_s (local.get $p) (i32.const 0))
182-
(then (call $caml_bound_error)))
182+
(then (return_call $caml_bound_error)))
183183
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
184184
(array.len (local.get $s)))
185-
(then (call $caml_bound_error)))
185+
(then (return_call $caml_bound_error)))
186186
(i32.or
187187
(i32.or
188188
(array.get_u $string (local.get $s) (local.get $p))
@@ -204,10 +204,10 @@
204204
(local.set $s (ref.cast (ref $string) (local.get $v)))
205205
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
206206
(if (i32.lt_s (local.get $p) (i32.const 0))
207-
(then (call $caml_bound_error)))
207+
(then (return_call $caml_bound_error)))
208208
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
209209
(array.len (local.get $s)))
210-
(then (call $caml_bound_error)))
210+
(then (return_call $caml_bound_error)))
211211
(i64.or
212212
(i64.or
213213
(i64.or
@@ -253,10 +253,10 @@
253253
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
254254
(local.set $v (i31.get_s (ref.cast (ref i31) (local.get 2))))
255255
(if (i32.lt_s (local.get $p) (i32.const 0))
256-
(then (call $caml_bound_error)))
256+
(then (return_call $caml_bound_error)))
257257
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
258258
(array.len (local.get $s)))
259-
(then (call $caml_bound_error)))
259+
(then (return_call $caml_bound_error)))
260260
(array.set $string (local.get $s) (local.get $p) (local.get $v))
261261
(array.set $string (local.get $s)
262262
(i32.add (local.get $p) (i32.const 1))
@@ -269,10 +269,10 @@
269269
(local.set $s (ref.cast (ref $string) (local.get 0)))
270270
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
271271
(if (i32.lt_s (local.get $p) (i32.const 0))
272-
(then (call $caml_bound_error)))
272+
(then (return_call $caml_bound_error)))
273273
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
274274
(array.len (local.get $s)))
275-
(then (call $caml_bound_error)))
275+
(then (return_call $caml_bound_error)))
276276
(array.set $string (local.get $s) (local.get $p) (local.get $v))
277277
(array.set $string (local.get $s)
278278
(i32.add (local.get $p) (i32.const 1))
@@ -291,10 +291,10 @@
291291
(local.set $s (ref.cast (ref $string) (local.get 0)))
292292
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
293293
(if (i32.lt_s (local.get $p) (i32.const 0))
294-
(then (call $caml_bound_error)))
294+
(then (return_call $caml_bound_error)))
295295
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
296296
(array.len (local.get $s)))
297-
(then (call $caml_bound_error)))
297+
(then (return_call $caml_bound_error)))
298298
(array.set $string (local.get $s) (local.get $p)
299299
(i32.wrap_i64 (local.get $v)))
300300
(array.set $string (local.get $s)

0 commit comments

Comments
 (0)