WASM is not quite a stack machine

(purplesyringa.moe)

43 points | by signa11 6 hours ago ago

10 comments

  • asibahi a few seconds ago

    I dont really disagree with the main premise of the article, which is that WASM is not really a stack language, but this part just gave me pause:

    > 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].

        ‘(’      ‘)’ ≡   
    
    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.

    [1]: https://webassembly.github.io/spec/core/text/instructions.ht...

  • ufo 40 minutes ago

    The author seems to complain about a lack of stack manip expressions like dup and rot, but at least for me that's what I would expect from an average programming language stack machine. Even Java, which does have those instructions, doesn't use them --- reuse happens via local variables.

    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.

  • Hendrikto 30 minutes ago

    The series of articles linked at the end (troubles.md/posts/wasm-is-not-a-stack-machine/) is even more interesting, imo.

    Very well articulated and concise critique by somebody who seems to have a great amount of knowledge and experience with the topics.

  • stevefan1999 3 hours ago

    I'm trying to implement a WASM to C compiler, and because of that not-quite-so-stack behavior, I can actually guarantee that it will always build an expression and I don't have to discard or reset stack value! Everything stays within that function, which is very neat, and I think it is one of the reason WAT, the textual format is so neat, that you can represent it with a S-Expression.

  • kg an hour ago

    The lack of a dup opcode in Wasm as mentioned in the post is quite annoying when trying to generate compact code. I wish something like it had made it into the spec.

    • thomasmg 19 minutes ago

      You could use "local.tee". I kind of is "store" + "duplicate".

      • asibahi 10 minutes ago

        `local.tee` doesn't duplicate. it just doesn't remove the value from the stack. (so it is "just" `local.set` followed by `local.get`)