WASM is not quite a stack machine
72 points - today at 4:34 AM
SourceComments
Very well articulated and concise critique by somebody who seems to have a great amount of knowledge and experience with the topics.
The way I see it, the difference between register and stack vms is all about the instruction encoding. Register VMs have fatter instructions in exchange for needing fewer LOAD and STORE operations. Despite the name, register VMs also have a stack.
> In textual Wasm, for example, they are instead represented in a LISP-like notation – not any less or more efficient
The Text format, at least when it comes to instructions, it 1 to 1 with the binary format. The LISP-like syntax is mainly just syntax sugar[1].
‘(’ plaininstr instrs ‘)’ ≡ instrs plaininstr
So (in theory, as far as I understand it) you can just do `(local.get 2 local.get 0 local.get 1)` to mean `local.get 0 local.get 1 local.get 2`, and it works for (almost) any instruction.Unfortunately, in my limited testing, tools like `wat2wasm` and Binaryen's `wasm-as` don't seem to adhere to (my perhaps faulty understanding of) the spec, and demand all instructions in a folded block be folded and have the "correct" amount of arguments, which makes Binaryen do weird things like
(return
(tuple.make ;; Binaryen only pseudoinstruction
(local.get 0) ;; or w/e expression
(local.get 1) ;; or w/e expression
)
)
when this is perfectly valid local.get 0
local.get 1
return
tl;dr: the LISP syntax is just syntax sugar. The textual format is as "stack-like" as the binary format.Edit: An example that is easily done with the stack syntax and not with lisp syntax is the following:
call function_that_returns_multivalue
local.set 2 ;; last return
local.set 1 ;;
local.set 0 ;; first return
In LISP syntax this would be (local.set 0
(local.set 1
(local.set 2
(call function_that_returns_multivalue
( ;; whatever input paramters
)))))
I have not yet tried this with Binaryen but I doubt it flies.[1]: https://webassembly.github.io/spec/core/text/instructions.ht...
It has failed to deliver that - so much is clear now. You rarely see any awesome success story shown with regard to WASM nowadays. What happened to the old promises? "Electron will be SUPER fast thanks to WASM" or "use any language, WASM unifies it all for the larger browser ecosystem".
It feels as if WASM is on a step towards exctinction. Sure, it is mentioned, it is used, but let's be honest - only few people really use it. And that won't change either.