The Stack Machine
WebAssembly is a stack machine. Instructions don't name registers: they pop their operands from an implicit value stack and push their results back onto it. Understanding the stack is the key to reading any Wasm code.
Watching the stack
Let's compute (2 + 3) * 4 in flat (one instruction per line) form and track the stack after each instruction:
(module
(func (export "main") (result i32)
i32.const 2 ;; stack: [2]
i32.const 3 ;; stack: [2, 3]
i32.add ;; pops 3 and 2, pushes 5 -> [5]
i32.const 4 ;; stack: [5, 4]
i32.mul)) ;; pops 4 and 5, pushes 20 -> [20]20
Every instruction has a type that describes its effect on the stack:
| Instruction | Type | Effect |
|---|---|---|
i32.const 7 |
[] → [i32] |
push a constant |
i32.add |
[i32 i32] → [i32] |
pop two, push their sum |
i32.eqz |
[i32] → [i32] |
pop one, push 1 if it was zero |
drop |
[t] → [] |
throw away the top value |
local.get $x |
[] → [t] |
push a local |
call $f |
[params] → [results] |
pop arguments, push results |
Operand order matters
For non-commutative operations the first operand is the one that was pushed first (deeper in the stack):
(module
(import "console" "log" (func $log (param i32)))
(func (export "main")
i32.const 10
i32.const 3
i32.sub ;; 10 - 3, not 3 - 10
call $log
i32.const 10
i32.const 3
i32.div_s ;; 10 / 3 = 3 (truncated)
call $log))7
3
Folded form: the same code as expressions
Long flat sequences are hard to read. The text format lets you fold instructions into S-expressions, where the operands are written inside the parentheses. The assembler simply unfolds them: operands first, then the instruction. These two functions compile to byte-for-byte identical code:
(module
(import "console" "log" (func $log (param i32)))
(func $flat (param $x i32) (result i32) ;; x*x + 1
local.get $x
local.get $x
i32.mul
i32.const 1
i32.add)
(func $folded (param $x i32) (result i32)
(i32.add
(i32.mul (local.get $x) (local.get $x))
(i32.const 1)))
(func (export "main")
(call $log (call $flat (i32.const 9)))
(call $log (call $folded (i32.const 9)))))82
82
You can freely mix the styles. A common idiom is to push values flat and then apply a folded instruction, or to fold just the arguments of a call. In this course we use whichever is clearer.
Note: Folding is only syntax.
(i32.add (A) (B))always means "run A, run B, theni32.add". It never changes evaluation order, and you can't skip operands.
The stack is typed and checked
The validator simulates the stack for every function and knows the type of every slot. It rejects code that would pop from an empty stack, use a value of the wrong type, or leave extra values behind:
(module
(func (export "main") (result i32)
i32.const 1
i32.add)) ;; needs two operands, only one is availablemain.wat:4:5: error: type mismatch in i32.add, expected [i32, i32] but got [i32]
i32.add)) ;; needs two operands, only one is available
^^^^^^^
Leaving an unused value is also an error, so you drop values you don't need:
(module
(import "console" "log" (func $log (param i32)))
(func $answer (result i32) (i32.const 42))
(func (export "main")
call $answer
drop ;; discard the result
(call $log (i32.const 1))))1
Stack vs locals
The value stack is not memory: you can't index into it, and values deep in the stack can't be read directly. When
you need a value more than once, store it in a local (local.set/local.tee). Engines turn both the stack and
the locals into machine registers when they compile, so locals are not slower.
(module
(import "console" "log" (func $log (param i32)))
;; cube(x) = x * x * x, reading x from a local three times
(func $cube (param $x i32) (result i32)
local.get $x
local.get $x
i32.mul
local.get $x
i32.mul)
(func (export "main")
(call $log (call $cube (i32.const 5)))))125
Why a stack machine?
- Compact: instructions need no operand fields for registers, so the bytecode is small.
- Easy to validate in one forward pass.
- Easy to compile: engines quickly map stack slots to registers.
The stack discipline is also structured: blocks and functions each see only their own part of the stack, which you will meet in the Control Flow section.
Test yourself
After i32.const 20, i32.const 5, i32.sub, what is on the stack?
- [-15]
- [15]
- [20, 5]
- [25]
What does the folded form (i32.mul (local.get 0) (i32.const 3)) compile to?
i32.mul,local.get 0,i32.const 3local.get 0,i32.const 3,i32.muli32.const 3,local.get 0,i32.mul- A call to a helper function
A function with no result calls $f, which returns an i32. What must happen to that value?
- Nothing, it is discarded automatically
- It must be consumed, for example with
drop - It becomes the return value
- It is stored in local 0