Skip to content

Are block labels supported? #796

@ColinEberhardt

Description

@ColinEberhardt

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 :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions