summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/spec/spec/switch.wast.js
blob: 403b2c376267c6cdbe5745440ed01e689a0aacfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* Copyright 2021 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// ./test/core/switch.wast

// ./test/core/switch.wast:1
let $0 = instantiate(`(module
  ;; Statement switch
  (func (export "stmt") (param $$i i32) (result i32)
    (local $$j i32)
    (local.set $$j (i32.const 100))
    (block $$switch
      (block $$7
        (block $$default
          (block $$6
            (block $$5
              (block $$4
                (block $$3
                  (block $$2
                    (block $$1
                      (block $$0
                        (br_table $$0 $$1 $$2 $$3 $$4 $$5 $$6 $$7 $$default
                          (local.get $$i)
                        )
                      ) ;; 0
                      (return (local.get $$i))
                    ) ;; 1
                    (nop)
                    ;; fallthrough
                  ) ;; 2
                  ;; fallthrough
                ) ;; 3
                (local.set $$j (i32.sub (i32.const 0) (local.get $$i)))
                (br $$switch)
              ) ;; 4
              (br $$switch)
            ) ;; 5
            (local.set $$j (i32.const 101))
            (br $$switch)
          ) ;; 6
          (local.set $$j (i32.const 101))
          ;; fallthrough
        ) ;; default
        (local.set $$j (i32.const 102))
      ) ;; 7
      ;; fallthrough
    )
    (return (local.get $$j))
  )

  ;; Expression switch
  (func (export "expr") (param $$i i64) (result i64)
    (local $$j i64)
    (local.set $$j (i64.const 100))
    (return
      (block $$switch (result i64)
        (block $$7
          (block $$default
            (block $$4
              (block $$5
                (block $$6
                  (block $$3
                    (block $$2
                      (block $$1
                        (block $$0
                          (br_table $$0 $$1 $$2 $$3 $$4 $$5 $$6 $$7 $$default
                            (i32.wrap_i64 (local.get $$i))
                          )
                        ) ;; 0
                        (return (local.get $$i))
                      ) ;; 1
                      (nop)
                      ;; fallthrough
                    ) ;; 2
                    ;; fallthrough
                  ) ;; 3
                  (br $$switch (i64.sub (i64.const 0) (local.get $$i)))
                ) ;; 6
                (local.set $$j (i64.const 101))
                ;; fallthrough
              ) ;; 4
              ;; fallthrough
            ) ;; 5
            ;; fallthrough
          ) ;; default
          (br $$switch (local.get $$j))
        ) ;; 7
        (i64.const -5)
      )
    )
  )

  ;; Argument switch
  (func (export "arg") (param $$i i32) (result i32)
    (return
      (block $$2 (result i32)
        (i32.add (i32.const 10)
          (block $$1 (result i32)
            (i32.add (i32.const 100)
              (block $$0 (result i32)
                (i32.add (i32.const 1000)
                  (block $$default (result i32)
                    (br_table $$0 $$1 $$2 $$default
                      (i32.mul (i32.const 2) (local.get $$i))
                      (i32.and (i32.const 3) (local.get $$i))
                    )
                  )
                )
              )
            )
          )
        )
      )
    )
  )

  ;; Corner cases
  (func (export "corner") (result i32)
    (block
      (br_table 0 (i32.const 0))
    )
    (i32.const 1)
  )
)`);

// ./test/core/switch.wast:120
assert_return(() => invoke($0, `stmt`, [0]), [value("i32", 0)]);

// ./test/core/switch.wast:121
assert_return(() => invoke($0, `stmt`, [1]), [value("i32", -1)]);

// ./test/core/switch.wast:122
assert_return(() => invoke($0, `stmt`, [2]), [value("i32", -2)]);

// ./test/core/switch.wast:123
assert_return(() => invoke($0, `stmt`, [3]), [value("i32", -3)]);

// ./test/core/switch.wast:124
assert_return(() => invoke($0, `stmt`, [4]), [value("i32", 100)]);

// ./test/core/switch.wast:125
assert_return(() => invoke($0, `stmt`, [5]), [value("i32", 101)]);

// ./test/core/switch.wast:126
assert_return(() => invoke($0, `stmt`, [6]), [value("i32", 102)]);

// ./test/core/switch.wast:127
assert_return(() => invoke($0, `stmt`, [7]), [value("i32", 100)]);

// ./test/core/switch.wast:128
assert_return(() => invoke($0, `stmt`, [-10]), [value("i32", 102)]);

// ./test/core/switch.wast:130
assert_return(() => invoke($0, `expr`, [0n]), [value("i64", 0n)]);

// ./test/core/switch.wast:131
assert_return(() => invoke($0, `expr`, [1n]), [value("i64", -1n)]);

// ./test/core/switch.wast:132
assert_return(() => invoke($0, `expr`, [2n]), [value("i64", -2n)]);

// ./test/core/switch.wast:133
assert_return(() => invoke($0, `expr`, [3n]), [value("i64", -3n)]);

// ./test/core/switch.wast:134
assert_return(() => invoke($0, `expr`, [6n]), [value("i64", 101n)]);

// ./test/core/switch.wast:135
assert_return(() => invoke($0, `expr`, [7n]), [value("i64", -5n)]);

// ./test/core/switch.wast:136
assert_return(() => invoke($0, `expr`, [-10n]), [value("i64", 100n)]);

// ./test/core/switch.wast:138
assert_return(() => invoke($0, `arg`, [0]), [value("i32", 110)]);

// ./test/core/switch.wast:139
assert_return(() => invoke($0, `arg`, [1]), [value("i32", 12)]);

// ./test/core/switch.wast:140
assert_return(() => invoke($0, `arg`, [2]), [value("i32", 4)]);

// ./test/core/switch.wast:141
assert_return(() => invoke($0, `arg`, [3]), [value("i32", 1116)]);

// ./test/core/switch.wast:142
assert_return(() => invoke($0, `arg`, [4]), [value("i32", 118)]);

// ./test/core/switch.wast:143
assert_return(() => invoke($0, `arg`, [5]), [value("i32", 20)]);

// ./test/core/switch.wast:144
assert_return(() => invoke($0, `arg`, [6]), [value("i32", 12)]);

// ./test/core/switch.wast:145
assert_return(() => invoke($0, `arg`, [7]), [value("i32", 1124)]);

// ./test/core/switch.wast:146
assert_return(() => invoke($0, `arg`, [8]), [value("i32", 126)]);

// ./test/core/switch.wast:148
assert_return(() => invoke($0, `corner`, []), [value("i32", 1)]);

// ./test/core/switch.wast:150
assert_invalid(
  () => instantiate(`(module (func (br_table 3 (i32.const 0))))`),
  `unknown label`,
);