labels.txt (2153B)
1 WASM Labels 2 3 Each structured control instruction introduces an implicit label. Labels are 4 targets for branch instructions that reference them with label indices. Unlike 5 with other index spaces, indexing of labels is relative by nesting depth, that 6 is, label 0 refers to the innermost structured control instruction enclosing 7 the referring branch instruction, while increasing indices refer to those 8 farther out. Consequently, labels can only be referenced from within the 9 associated structured control instruction. This also implies that branches can 10 only be directed outwards, “breaking” from the block of the control construct 11 they target. The exact effect depends on that control construct. In case of 12 block or if it is a forward jump, resuming execution after the matching end. In 13 case of loop it is a backward jump to the beginning of the loop. 14 15 Note 16 This enforces structured control flow. Intuitively, a branch targeting a block 17 or if behaves like a break statement in most C-like languages, while a branch 18 targeting a loop behaves like a continue statement. 19 20 Branch instructions come in several flavors: br performs an unconditional 21 branch, br_if performs a conditional branch, and br_table performs an indirect 22 branch through an operand indexing into the label vector that is an immediate 23 to the instruction, or to a default target if the operand is out of bounds. The 24 return instruction is a shortcut for an unconditional branch to the outermost 25 block, which implicitly is the body of the current function. Taking a branch 26 unwinds the operand stack up to the height where the targeted structured 27 control instruction was entered. However, branches may additionally consume 28 operands themselves, which they push back on the operand stack after unwinding. 29 Forward branches require operands according to the output of the targeted 30 block’s type, i.e., represent the values produced by the terminated block. 31 Backward branches require operands according to the input of the targeted 32 block’s type, i.e., represent the values consumed by the restarted block. 33 34 https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-control