<story><title>WebAssembly Troubles Part 1: WebAssembly Is Not a Stack Machine</title><url>http://troubles.md/posts/wasm-is-not-a-stack-machine/</url></story><parent_chain></parent_chain><comment><author>titzer</author><text>[one of the original Wasm designers here]&lt;p&gt;Responding to the OP, since there is no comment section on the site.&lt;p&gt;First off, this rant gets the history of Wasm wrong and the facts of Wasm wrong. I wouldn&amp;#x27;t unload on a random person on the internet generally, but I would like to point a sentence like:&lt;p&gt;&amp;gt; Not only that, but for the most part the WebAssembly specification team were flying blind.&lt;p&gt;It&amp;#x27;s an ad hominem. This really just impugns people and invites an argument. It might be cathartic, but generally it doesn&amp;#x27;t advance the conversation to cast aspersion like this.&lt;p&gt;And it&amp;#x27;s not true. I can tell you from first hand experience that a baseline compiler was absolutely on our minds, and Mozilla already had a baseline compiler in development throughout design. The Liftoff design that V8 shipped didn&amp;#x27;t look too different from the picture in our collective heads at the time. And all of us had considerable experience with JIT designs of all kinds.&lt;p&gt;As for the history. The history is wrong. The first iteration of Wasm was in fact a pre-order encoded AST. No stack. The second iteration was a post-order encoded AST, which we found through microbenchmarks, actually decoded considerably faster. The rub was how to support multi-value returns of function calls, since multi-value local constructs can be flattened by a producer. We considered a number of alternatives that preserved the AST-like structure before settling on that a structured stack machine is actually the best design solution, since it allowed the straightforward extension to multi-values that is there now (and will ship by default when we reach the two-engine implementation status).&lt;p&gt;As for the present. Wasm blocks and loops absolutely can take parameters; it&amp;#x27;s part of the multi-value extension which V8 implemented already a year ago. Block and loop parameters subsume SSA form and make locals wholly unnecessary (if that&amp;#x27;s your thing). Locals make no difference to an optimizing compiler like TurboFan or IonMonkey. And SSA form as an intermediate representation is not as compact as the stack machine with block and loop parameters which is the current design, as those extra moves take space and add an additional verification burden.&lt;p&gt;A final point. Calling Wasm &amp;quot;not a stack machine&amp;quot; is just a misunderstanding. All operators that work on values operate on the implicit operand stack. This is the very the definition of a stack machine. The fact that there is additional mutable local storage doesn&amp;#x27;t make it not a stack machine. Similarly, the JVM has mutable typed locals and yet is a stack machine as well. The JVM (prior to 6) allowed completely unstructured control flow and use of the stack, leading to a number of problems, including a potentially cubic verification time. We fixed that.&lt;p&gt;All that said, there might be a design mistake in Wasm bytecode. Personally, I think we should have implicitly loaded arguments to a function onto the operand stack, which would have made inlining even more like syntactic substitution and further shortened the bodies of very tiny functions. But this is a small thing and we didn&amp;#x27;t think about it at the time.&lt;p&gt;[edit: Perhaps &amp;quot;ad hominem&amp;quot; is a bit strong. It feels different to be on the receiving of a comment like &amp;quot;flying blind&amp;quot;--it doesn&amp;#x27;t mean the same thing to the sender and receiver--especially when this was really not the case, as I state here.]</text></comment>
<story><title>WebAssembly Troubles Part 1: WebAssembly Is Not a Stack Machine</title><url>http://troubles.md/posts/wasm-is-not-a-stack-machine/</url></story><parent_chain></parent_chain><comment><author>pizlonator</author><text>Recomputing liveness is not really a big deal. Can be quite cheap, especially over a register based IR.&lt;p&gt;I think that this article overstates the impact of all of this.</text></comment>