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

Blocks do not have proper return types in if-statements #1

Open
AndreasHassing opened this issue May 20, 2017 · 0 comments
Open

Blocks do not have proper return types in if-statements #1

AndreasHassing opened this issue May 20, 2017 · 0 comments
Assignees
Labels

Comments

@AndreasHassing
Copy link
Owner

In the following program (Fibonacci):

int fib(int n) {
  if (n == 0) {
    return 0;
  } else if (n == 1) {
    return 1;
  } else {
    return fib(n-1) + fib(n-2);
  }
}

void start() {
  print fib(14);
}

The compiled WASM-module won't run in browsers, due to the fact that the generated IF statement states that a I32 value must be returned, but the inner block does not define a return type.

Compiled WASM-module translated to WAST:

(module
  (type (;0;) (func (param i32)))
  (type (;1;) (func (param i32)))
  (type (;2;) (func (param i32) (result i32)))
  (type (;3;) (func))
  (import "imports" "printi" (func (;0;) (type 0)))
  (import "imports" "printc" (func (;1;) (type 1)))
  (func (;2;) (type 2) (param i32) (result i32)
    get_local 0
    i32.const 0
    i32.eq
    if i32  ;; label = @1
      block  ;; label = @2 ;; this block should also return an i32!
        i32.const 0
        return
      end
    else
      get_local 0
      i32.const 1
      i32.eq
      if i32  ;; label = @2
        block  ;; label = @3 ;; this block should also return an i32!
          i32.const 1
          return
        end
      else
        block  ;; label = @3 ;; this block should also return an i32!
          get_local 0
          i32.const 1
          i32.sub
          call 2
          get_local 0
          i32.const 2
          i32.sub
          call 2
          i32.add
          return
        end
      end
    end)
  (func (;3;) (type 3)
    i32.const 14
    call 2
    call 0)
  (memory (;0;) 1)
  (start 3))

Error generated by wast2wasm:

test.wast:15:9: type stack size too small at if true branch. got 0, expected at least 1
        return
        ^^^^^^
test.wast:24:11: type stack size too small at if true branch. got 0, expected at least 1
          return
          ^^^^^^
test.wast:37:11: type stack size too small at if false branch. got 0, expected at least 1
          return
          ^^^^^^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant