HeadlinesBriefing favicon HeadlinesBriefing.com

Wasm isn't a true stack machine—here's why

Hacker News •
×

WebAssembly, often dubbed a stack machine, turns out to be more of a register machine in practice. A developer writing bytecode by hand noticed that Wasm’s instruction set lacks true stack manipulation beyond a single drop opcode. While JVM offers dup, swap, and over, Wasm relies on locals for any reuse, blurring the stack‑machine label.

Stack machines like Forth use implicit operands, so reusing a value requires stack tricks such as dup and swap. Without these, a compiler must generate temporary locals, turning the code into a register‑style sequence. Wasm’s minimal stack operations force compilers to emit explicit locals for common‑subexpression elimination, losing the concise semantics that true stack VMs provide.

The takeaway is that developers accustomed to pure stack VMs may misinterpret Wasm’s execution model. While its binary encoding uses Reverse Polish notation, the runtime treats values as locals once the multi‑value extension arrives. Understanding this register‑machine core clarifies why optimizers must introduce locals, and why Wasm’s stack‑machine myth persists in documentation.

This nuance matters when building tooling. A Wasm compiler that ignores locals under the stack‑machine banner may generate bloated binaries, while a tool that exposes the underlying register semantics can emit tighter code. For teams porting existing stack‑based languages to Wasm, recognizing these constraints is vital to avoid performance regressions during optimization passes.