webassembly

What is supposed to happen to the block result if there is a branch out of a block?


Consider the following code:

    loop $FIRST
      block $SECOND (result i32)
        i32.constant 1 
        block $THIRD (result i32)
          i32.constant 1
          br $FIRST
        end
      end
      br $FIRST
    end

First, should it get an error if one of the constant instructions is removed? Second, when the branch inside the third block is taken to come to the beginning of the loop, what does the loop "see" on its stack and why?

I am new to Wasm and I have no idea how I can understand what can be seen at each statement.


Solution

  • This piece of code is already ill-typed as is, because the second block declares a single result of type i32 when in fact two are on the stack.

    Hence, your third block is fine, since its end is never reached.

    The second block is not fine. At the end, there are two i32's on the stack (one from the constant, one the result of the inner block), but it declares there to be only one.

    The loop again is okay, since its end also is never reached.

    You can make the program well-typed by removing the first i32.const. The second i32.const is never consumed, so is redundant and can be removed as well.