Search code examples
webassembly

What's the difference between "block" and "loop" in WebAssembly spec?


As titled, the spec says that "loop" is

a block with a label at the beginning which may be used to form loops.

and for "block":

the beginning of a block construct, a sequence of instructions with a label at the end.

But with the help of "br" (used for switching branch to a labeled block), I can form the same control structure even with "block", right?. So, what's the difference between these two instructions?


Solution

  • A br to a block label jumps to the end of the contained instruction sequence -- it behaves like a break statement in C.

    A br to a loop label jumps to the start of the contained instruction sequence -- it behaves like a continue statement in C.

    The former enables a forward jump, the latter a backwards jump. Neither can express the other.