WebAssembly / design

WebAssembly Design Documents
http://webassembly.org
Apache License 2.0
11.37k stars 691 forks source link

Confusing semantics of br-instructions #1259

Open joelonsql opened 5 years ago

joelonsql commented 5 years ago

From https://webassembly.org/docs/semantics/

"br: branch to a given label in an enclosing construct"

The br-family instructions in WebAssembly are very different from assemblies like LLVM IR where you can jump to arbitrary labels. I think this should be pointed out in the documentation.

I think this would be a better description of this instruction:

"br: break to block at given nesting-level in an enclosing construct"

Since "br" works more like a "break" in C than a "goto" in C or a "br" in LLVM IR.

kripken commented 5 years ago

I agree. Earlier discussion here: https://github.com/WebAssembly/design/issues/445

Still not too late to fix this! :)

binji commented 5 years ago

@joelonsql The semantics document is useful as an overview, but has not been updated in a long time. You may want to look at the spec instead. In particular, br is defined here:

Each structured control instruction introduces an implicit label. Labels are targets for branch instructions that reference them with label indices. Unlike with other index spaces, indexing of labels is relative by nesting depth, that is, label 0 refers to the innermost structured control instruction enclosing the referring branch instruction, while increasing indices refer to those farther out. Consequently, labels can only be referenced from within the associated structured control instruction. This also implies that branches can only be directed outwards, “breaking” from the block of the control construct they target. The exact effect depends on that control construct. In case of block or if it is a forward jump, resuming execution after the matching end. In case of loop it is a backward jump to the beginning of the loop.

amirouche commented 4 years ago

@binji The spec is still not clear enough. And @joelonsql point still holds.

binji commented 4 years ago

@amirouche how is the spec unclear?