Skip to content

Commit 2fc08eb

Browse files
committed
Revert "Simplify handling of bound errors and division by zero"
This reverts commit fc651d6352b70822017b888e97158ec144fac73e.
1 parent 042cddb commit 2fc08eb

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

compiler/lib-wasm/generate.ml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,11 @@ module Generate (Target : Target_sig.S) = struct
823823
{ params = []; result = [] }
824824
(body ~result_typ:[] ~fall_through:(`Block pc) ~context:(`Block pc :: context))
825825
in
826-
handler
826+
if List.is_empty result_typ
827+
then handler
828+
else
829+
let* () = handler in
830+
instr (W.Return (Some (RefI31 (Const (I32 0l)))))
827831
else body ~result_typ ~fall_through ~context
828832

829833
let wrap_with_handlers p pc ~result_typ ~fall_through ~context body =
@@ -832,20 +836,18 @@ module Generate (Target : Target_sig.S) = struct
832836
need_bound_error_handler
833837
bound_error_pc
834838
(let* f =
835-
register_import
836-
~name:"caml_bound_error"
837-
(Fun { params = []; result = [ Value.value ] })
839+
register_import ~name:"caml_bound_error" (Fun { params = []; result = [] })
838840
in
839-
instr (Return (Some (Call (f, [])))))
841+
instr (CallInstr (f, [])))
840842
(wrap_with_handler
841843
need_zero_divide_handler
842844
zero_divide_pc
843845
(let* f =
844846
register_import
845847
~name:"caml_raise_zero_divide"
846-
(Fun { params = []; result = [ Value.value ] })
848+
(Fun { params = []; result = [] })
847849
in
848-
instr (Return (Some (Call (f, [])))))
850+
instr (CallInstr (f, [])))
849851
body)
850852
~result_typ
851853
~fall_through

runtime/wasm/bigarray.wat

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
(func $ta_blit_to_bytes
7878
(param (ref extern)) (param i32) (param (ref $bytes)) (param i32)
7979
(param i32)))
80-
(import "fail" "caml_bound_error" (func $caml_bound_error (result (ref eq))))
80+
(import "fail" "caml_bound_error" (func $caml_bound_error))
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))))
@@ -1062,7 +1062,7 @@
10621062
(if (i32.ge_u (local.get $i)
10631063
(array.get $int_array (struct.get $bigarray 2 (local.get $ba))
10641064
(i32.const 0)))
1065-
(then (return_call $caml_bound_error)))
1065+
(then (call $caml_bound_error)))
10661066
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $i)))
10671067

10681068
(func (export "caml_ba_set_1")
@@ -1076,7 +1076,7 @@
10761076
(if (i32.ge_u (local.get $i)
10771077
(array.get $int_array (struct.get $bigarray $ba_dim (local.get $ba))
10781078
(i32.const 0)))
1079-
(then (return_call $caml_bound_error)))
1079+
(then (call $caml_bound_error)))
10801080
(call $caml_ba_set_at_offset
10811081
(local.get $ba) (local.get $i) (local.get $v))
10821082
(ref.i31 (i32.const 0)))
@@ -1114,7 +1114,7 @@
11141114
(i32.ge_u (local.get $j)
11151115
(array.get $int_array (local.get $dim) (i32.const 1))))
11161116
(then
1117-
(return_call $caml_bound_error)))
1117+
(call $caml_bound_error)))
11181118
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
11191119

11201120
(func (export "caml_ba_set_2")
@@ -1150,7 +1150,7 @@
11501150
(i32.ge_u (local.get $j)
11511151
(array.get $int_array (local.get $dim) (i32.const 1))))
11521152
(then
1153-
(return_call $caml_bound_error)))
1153+
(call $caml_bound_error)))
11541154
(call $caml_ba_set_at_offset
11551155
(local.get $ba) (local.get $offset) (local.get $v))
11561156
(ref.i31 (i32.const 0)))
@@ -1208,7 +1208,7 @@
12081208
(i32.ge_u (local.get $k)
12091209
(array.get $int_array (local.get $dim) (i32.const 2)))))
12101210
(then
1211-
(return_call $caml_bound_error)))
1211+
(call $caml_bound_error)))
12121212
(return_call $caml_ba_get_at_offset (local.get $ba) (local.get $offset)))
12131213

12141214
(func (export "caml_ba_set_3")
@@ -1261,7 +1261,7 @@
12611261
(i32.ge_u (local.get $k)
12621262
(array.get $int_array (local.get $dim) (i32.const 2)))))
12631263
(then
1264-
(return_call $caml_bound_error)))
1264+
(call $caml_bound_error)))
12651265
(call $caml_ba_set_at_offset
12661266
(local.get $ba) (local.get $offset) (local.get $v))
12671267
(ref.i31 (i32.const 0)))
@@ -1292,7 +1292,7 @@
12921292
(array.get $int_array (local.get $dim) (local.get $i)))
12931293
(if (i32.ge_u (local.get $idx) (local.get $l))
12941294
(then
1295-
(drop (call $caml_bound_error))))
1295+
(call $caml_bound_error)))
12961296
(local.set $offset
12971297
(i32.add (i32.mul (local.get $offset) (local.get $l))
12981298
(local.get $idx)))
@@ -1310,7 +1310,7 @@
13101310
(array.get $int_array (local.get $dim) (local.get $i)))
13111311
(if (i32.ge_u (local.get $idx) (local.get $l))
13121312
(then
1313-
(drop (call $caml_bound_error))))
1313+
(call $caml_bound_error)))
13141314
(local.set $offset
13151315
(i32.add (i32.mul (local.get $offset) (local.get $l))
13161316
(local.get $idx)))
@@ -1343,7 +1343,7 @@
13431343
(array.get $int_array (local.get $dim) (local.get $i)))
13441344
(if (i32.ge_u (local.get $idx) (local.get $l))
13451345
(then
1346-
(drop (call $caml_bound_error))))
1346+
(call $caml_bound_error)))
13471347
(local.set $offset
13481348
(i32.add (i32.mul (local.get $offset) (local.get $l))
13491349
(local.get $idx)))
@@ -1364,7 +1364,7 @@
13641364
(array.get $int_array (local.get $dim) (local.get $i)))
13651365
(if (i32.ge_u (local.get $idx) (local.get $l))
13661366
(then
1367-
(drop (call $caml_bound_error))))
1367+
(call $caml_bound_error)))
13681368
(local.set $offset
13691369
(i32.add (i32.mul (local.get $offset) (local.get $l))
13701370
(local.get $idx)))
@@ -2011,12 +2011,12 @@
20112011
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
20122012
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20132013
(if (i32.lt_s (local.get $p) (i32.const 0))
2014-
(then (return_call $caml_bound_error)))
2014+
(then (call $caml_bound_error)))
20152015
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
20162016
(array.get $int_array
20172017
(struct.get $bigarray $ba_dim (local.get $ba))
20182018
(i32.const 0)))
2019-
(then (drop (call $caml_bound_error))))
2019+
(then (call $caml_bound_error)))
20202020
(ref.i31 (call $ta_get16_ui8 (local.get $data) (local.get $p))))
20212021

20222022
(func (export "caml_ba_uint8_get32")
@@ -2028,12 +2028,12 @@
20282028
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
20292029
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20302030
(if (i32.lt_s (local.get $p) (i32.const 0))
2031-
(then (drop (call $caml_bound_error))))
2031+
(then (call $caml_bound_error)))
20322032
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
20332033
(array.get $int_array
20342034
(struct.get $bigarray $ba_dim (local.get $ba))
20352035
(i32.const 0)))
2036-
(then (drop (call $caml_bound_error))))
2036+
(then (call $caml_bound_error)))
20372037
(return_call $ta_get32_ui8 (local.get $data) (local.get $p)))
20382038

20392039
(func (export "caml_ba_uint8_get64")
@@ -2045,12 +2045,12 @@
20452045
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
20462046
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20472047
(if (i32.lt_s (local.get $p) (i32.const 0))
2048-
(then (drop (call $caml_bound_error))))
2048+
(then (call $caml_bound_error)))
20492049
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
20502050
(array.get $int_array
20512051
(struct.get $bigarray $ba_dim (local.get $ba))
20522052
(i32.const 0)))
2053-
(then (drop (call $caml_bound_error))))
2053+
(then (call $caml_bound_error)))
20542054
(i64.or
20552055
(i64.extend_i32_u
20562056
(call $ta_get32_ui8 (local.get $data) (local.get $p)))
@@ -2070,12 +2070,12 @@
20702070
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20712071
(local.set $d (ref.cast (ref i31) (local.get $v)))
20722072
(if (i32.lt_s (local.get $p) (i32.const 0))
2073-
(then (return_call $caml_bound_error)))
2073+
(then (call $caml_bound_error)))
20742074
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
20752075
(array.get $int_array
20762076
(struct.get $bigarray $ba_dim (local.get $ba))
20772077
(i32.const 0)))
2078-
(then (return_call $caml_bound_error)))
2078+
(then (call $caml_bound_error)))
20792079
(call $ta_set16_ui8 (local.get $data) (local.get $p) (local.get $d))
20802080
(ref.i31 (i32.const 0)))
20812081

@@ -2089,12 +2089,12 @@
20892089
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
20902090
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
20912091
(if (i32.lt_s (local.get $p) (i32.const 0))
2092-
(then (return_call $caml_bound_error)))
2092+
(then (call $caml_bound_error)))
20932093
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
20942094
(array.get $int_array
20952095
(struct.get $bigarray $ba_dim (local.get $ba))
20962096
(i32.const 0)))
2097-
(then (return_call $caml_bound_error)))
2097+
(then (call $caml_bound_error)))
20982098
(call $ta_set32_ui8 (local.get $data) (local.get $p) (local.get $d))
20992099
(ref.i31 (i32.const 0)))
21002100

@@ -2108,12 +2108,12 @@
21082108
(local.set $data (struct.get $bigarray $ba_data (local.get $ba)))
21092109
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
21102110
(if (i32.lt_s (local.get $p) (i32.const 0))
2111-
(then (return_call $caml_bound_error)))
2111+
(then (call $caml_bound_error)))
21122112
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
21132113
(array.get $int_array
21142114
(struct.get $bigarray $ba_dim (local.get $ba))
21152115
(i32.const 0)))
2116-
(then (return_call $caml_bound_error)))
2116+
(then (call $caml_bound_error)))
21172117
(call $ta_set32_ui8 (local.get $data) (local.get $p)
21182118
(i32.wrap_i64 (local.get $d)))
21192119
(call $ta_set32_ui8 (local.get $data)

runtime/wasm/fail.wat

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@
7373

7474
(@string $index_out_of_bounds "index out of bounds")
7575

76-
(func (export "caml_bound_error") (result (ref eq))
77-
(call $caml_invalid_argument (global.get $index_out_of_bounds))
78-
(ref.i31 (i32.const 0)))
76+
(func (export "caml_bound_error")
77+
(call $caml_invalid_argument (global.get $index_out_of_bounds)))
7978

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

@@ -86,11 +85,10 @@
8685

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

89-
(func (export "caml_raise_zero_divide") (result (ref eq))
90-
(call $caml_raise_constant
88+
(func (export "caml_raise_zero_divide")
89+
(return_call $caml_raise_constant
9190
(array.get $block (global.get $caml_global_data)
92-
(global.get $ZERO_DIVIDE_EXN)))
93-
(ref.i31 (i32.const 0)))
91+
(global.get $ZERO_DIVIDE_EXN))))
9492

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

runtime/wasm/string.wat

Lines changed: 13 additions & 13 deletions
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

@@ -159,10 +159,10 @@
159159
(local.set $s (ref.cast (ref $bytes) (local.get $v)))
160160
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
161161
(if (i32.lt_s (local.get $p) (i32.const 0))
162-
(then (return_call $caml_bound_error)))
162+
(then (call $caml_bound_error)))
163163
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
164164
(array.len (local.get $s)))
165-
(then (drop (call $caml_bound_error))))
165+
(then (call $caml_bound_error)))
166166
(ref.i31 (i32.or
167167
(array.get_u $bytes (local.get $s) (local.get $p))
168168
(i32.shl (array.get_u $bytes (local.get $s)
@@ -176,10 +176,10 @@
176176
(local.set $s (ref.cast (ref $bytes) (local.get $v)))
177177
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
178178
(if (i32.lt_s (local.get $p) (i32.const 0))
179-
(then (drop (call $caml_bound_error))))
179+
(then (call $caml_bound_error)))
180180
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
181181
(array.len (local.get $s)))
182-
(then (drop (call $caml_bound_error))))
182+
(then (call $caml_bound_error)))
183183
(i32.or
184184
(i32.or
185185
(array.get_u $bytes (local.get $s) (local.get $p))
@@ -201,10 +201,10 @@
201201
(local.set $s (ref.cast (ref $bytes) (local.get $v)))
202202
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get $i))))
203203
(if (i32.lt_s (local.get $p) (i32.const 0))
204-
(then (drop (call $caml_bound_error))))
204+
(then (call $caml_bound_error)))
205205
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
206206
(array.len (local.get $s)))
207-
(then (drop (call $caml_bound_error))))
207+
(then (call $caml_bound_error)))
208208
(i64.or
209209
(i64.or
210210
(i64.or
@@ -250,10 +250,10 @@
250250
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
251251
(local.set $v (i31.get_s (ref.cast (ref i31) (local.get 2))))
252252
(if (i32.lt_s (local.get $p) (i32.const 0))
253-
(then (return_call $caml_bound_error)))
253+
(then (call $caml_bound_error)))
254254
(if (i32.ge_u (i32.add (local.get $p) (i32.const 1))
255255
(array.len (local.get $s)))
256-
(then (return_call $caml_bound_error)))
256+
(then (call $caml_bound_error)))
257257
(array.set $bytes (local.get $s) (local.get $p) (local.get $v))
258258
(array.set $bytes (local.get $s)
259259
(i32.add (local.get $p) (i32.const 1))
@@ -266,10 +266,10 @@
266266
(local.set $s (ref.cast (ref $bytes) (local.get 0)))
267267
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
268268
(if (i32.lt_s (local.get $p) (i32.const 0))
269-
(then (return_call $caml_bound_error)))
269+
(then (call $caml_bound_error)))
270270
(if (i32.ge_u (i32.add (local.get $p) (i32.const 3))
271271
(array.len (local.get $s)))
272-
(then (return_call $caml_bound_error)))
272+
(then (call $caml_bound_error)))
273273
(array.set $bytes (local.get $s) (local.get $p) (local.get $v))
274274
(array.set $bytes (local.get $s)
275275
(i32.add (local.get $p) (i32.const 1))
@@ -288,10 +288,10 @@
288288
(local.set $s (ref.cast (ref $bytes) (local.get 0)))
289289
(local.set $p (i31.get_s (ref.cast (ref i31) (local.get 1))))
290290
(if (i32.lt_s (local.get $p) (i32.const 0))
291-
(then (return_call $caml_bound_error)))
291+
(then (call $caml_bound_error)))
292292
(if (i32.ge_u (i32.add (local.get $p) (i32.const 7))
293293
(array.len (local.get $s)))
294-
(then (return_call $caml_bound_error)))
294+
(then (call $caml_bound_error)))
295295
(array.set $bytes (local.get $s) (local.get $p)
296296
(i32.wrap_i64 (local.get $v)))
297297
(array.set $bytes (local.get $s)

0 commit comments

Comments
 (0)