@@ -258,10 +258,17 @@ All control flow structures, except `case`, are statements.
258
258
* ` br ` : branch to a given label in an enclosing construct (see below)
259
259
* ` br_if ` : conditionally branch to a given label in an enclosing construct
260
260
* ` tableswitch ` : a jump table transferring which may jump either to enclosed
261
- ` case ` blocks or to labels in enclosing constructs (see below
262
- for a more detailed description)
263
- * ` case ` : must be an immediate child of ` tableswitch ` ; has a label declared
264
- in the ` tableswitch ` 's table and a body (as above, see below)
261
+ ` case ` and ` default ` blocks or to labels in enclosing constructs
262
+ identified by enclosed ` case_br ` and ` default_br ` nodes (see
263
+ below for a more detailed description)
264
+ * ` case ` : must be an immediate child of ` tableswitch ` ; has an immediate index
265
+ value and a body (as above, see below)
266
+ * ` case_br ` : must be an immediate child of ` tableswitch ` ; has an immediate index
267
+ value and a label (as above, see below)
268
+ * ` default ` : must be an immediate child of ` tableswitch ` ; has a body
269
+ (as above, see below)
270
+ * ` default_br ` : must be an immediate child of ` tableswitch ` ; has a a label
271
+ (as above, see below)
265
272
* ` return ` : return zero or more values from this function
266
273
267
274
References to labels must occur within an * enclosing construct* that defined
@@ -272,18 +279,31 @@ one can arrange `block`s to put labels wherever one wants to jump to, except
272
279
for one restriction: one can't jump into the middle of a loop from outside
273
280
it. This restriction ensures the well-structured property discussed below.
274
281
275
- ` tableswitch ` instructions have a zero-based array of labels, a label index,
276
- a "default" label, an index operand, and a list of ` case ` nodes. A ` tableswitch `
277
- selects which label to branch to by looking up the index value in the label
278
- array, and transferring control to that label. If the index is out of bounds,
279
- it transfers control to the "default" label.
280
-
281
- ` case ` nodes can only appear as immediate children of ` tableswitch ` statements.
282
- They have a label, which must be declared in the immediately enclosing
283
- ` tableswitch ` 's array, and a body which can contain arbitrary code. Control
284
- falls through the end of a ` case ` block into the following ` case ` block, or
285
- the end of the ` tableswitch ` in the case of the last ` case ` .
286
-
282
+ ` tableswitch ` instructions have a label index operand, and contain a sequence
283
+ of ` case ` , ` case_br ` , ` default ` , and ` default_br ` nodes. The following
284
+ restrictions apply to the sequence:
285
+
286
+ - There must be either exactly one ` default ` node or exactly one
287
+ ` default_br ` node.
288
+ - Each ` case ` and ` case_br ` must have a unique index immediate value
289
+ among all ` case ` and ` case_br ` nodes.
290
+ - For each ` case ` and ` case_br ` with a non-zero index immediate value,
291
+ there must also be a ` case ` or ` case_br ` present with an index
292
+ immediate value of one less.
293
+
294
+ A ` tableswitch ` first chooses a child node by selecting the ` case ` or
295
+ ` case_br ` node with the immediate index value equal to the ` index ` operand,
296
+ if there is such a ` case ` or ` case_br ` node present, or the ` default ` or
297
+ ` default_br ` node otherwise.
298
+
299
+ If the chosen child node is a ` case ` , or ` default ` , ` tableswitch ` transfers
300
+ control to the node's body. If the chosen child node is a ` case_br ` or
301
+ ` default_br ` , ` tableswitch ` transfers control directly to the node's label.
302
+
303
+ ` case ` , ` case_br ` , ` default ` , and ` default_br ` nodes can only appear as
304
+ immediate children of ` tableswitch ` statements. Control falls through the end
305
+ of ` case ` and ` default ` bodies, into the next ` tableswitch ` child node in AST
306
+ order, or exiting the ` tableswitch ` in the case of the last child node.
287
307
288
308
## Calls
289
309
0 commit comments