Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are block labels supported? #796

Closed
ColinEberhardt opened this issue May 6, 2018 · 5 comments
Closed

Are block labels supported? #796

ColinEberhardt opened this issue May 6, 2018 · 5 comments

Comments

@ColinEberhardt
Copy link

ref: xtuc/webassemblyjs#277 - I am struggling to make block labels work.

The spec indicates that blocks can have an optional label, and that br / br_if instructions may use a label or index.

Here's a simple function that counts to twenty:

(func $count 
  (local $x i32)
  (set_local $x (i32.const 0))

  (block 
    (loop 
      (call $log
        (get_local $x)
      )
      
      (set_local $x
        (i32.add 
          (get_local $x)
          (i32.const 1)
        )
      )
      ;; loop for 20
      (br_if 1 (i32.eq (get_local $x) (i32.const 20)))
      (br 0)
    )
  )
)

This works just fine.

My expectation is that the br_if instruction can reference the block by name rather than depth. For example:

(func $count 
  (local $x i32)
  (set_local $x (i32.const 0))

  (block $myblock       <---------
    (loop 
      (call $log
        (get_local $x)
      )
      
      (set_local $x
        (i32.add 
          (get_local $x)
          (i32.const 1)
        )
      )
      ;; loop for 20
      (br_if $myblock (i32.eq (get_local $x) (i32.const 20)))  <---------
      (br 0)
    )
  )
)

However this doesn't work.

I've been looking at the spec test suite and can only see one block with a label, but it is unused:

https://github.com/WebAssembly/testsuite/blob/master/block.wast#L9

I'd appreciate some help, or a working example :-)

@xtuc
Copy link
Contributor

xtuc commented May 6, 2018

WebAssembly/design#1064 includes it

@rossberg
Copy link
Member

rossberg commented May 6, 2018

What do you mean by "this doesn't work"? The labeled version of your code should work fine, and does in the reference interpreter. What tool were you using?

There are plenty of uses of labels in the test suite, see e.g. labels.wast or stack.wast.

@ColinEberhardt
Copy link
Author

Thanks @rossberg - I've narrowed things down a bit. I'm using the WeAssembly build of wabt:

const wasmModule = wabt.parseWat(inputWat, readFileSync(inputWat, "utf8"));
const { buffer } = wasmModule.toBinary({ write_debug_names: true });

Which is giving the following error:

CompileError: WasmCompile: Compiling wasm function #1:count failed: invalid break depth: 1652124964 @+82

Whereas, compiling the same file with wat2wasm --debug-names works just fine. However, I have noticed that the block label myblock does not appear in the custom name.

Anyhow, closing this issue :-)

@ColinEberhardt
Copy link
Author

Thanks @xtuc that explains why I'm not seeing the labels in custom name section. Anyhow, I'll investigate that compiler error to see if I can find the root cause.

@binji
Copy link
Member

binji commented May 6, 2018

@ColinEberhardt yeah, I believe someone else ran into the same issue recently. You have to call resolveNames for this to work. It seems there should be a better error for this (currently I think it's just an assertion).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants