Back to News
Advertisement
Advertisement

⚡ Community Insights

Discussion Sentiment

67% Positive

Analyzed from 388 words in the discussion.

Trending Topics

#stack#wasm#local#seems#dup#java#doesn#register#vms#machine

Discussion (9 Comments)Read Original on HackerNews

ufo•about 1 hour 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.

pjjpo•2 minutes ago
> Despite the name, register VMs also have a stack.

Out of curiosity what do you think about this - in spite of the name, stack machines also have yet another stack. Ok I don't like that wording, but locals are basically the stack frames people know of from their computer arch class I think.

It doesn't change the fact that Wasm operations have to have the execution stack as one or more of the operands. Seems like a stack machines to me too, though I don't know more details on why the specific design of Wasm would make optimizing compilers harder to write than JVM as the article suggests (I think?).

U1F984•5 minutes ago
Java does use dup in some cases, e.g.

   public static void test() { 
      new Object();
   }

         0: new           #2                  // class java/lang/Object
         3: dup
         4: invokespecial #1                  // Method java/lang/Object."<init>":()V
         7: pop
         8: return
Hendrikto•39 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•about 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.
bsder•about 2 hours ago
But how do you handle arguments or loop index variables? Your liveness is the entire function? You have to compile all the WASM chunks together in order to do any optimization? That seems ... problematic.

Edit: Yep. In article referenced from the original: http://troubles.md/posts/wasm-is-not-a-stack-machine/

Double edit: Some of this has already been fixed in WASM: https://github.com/WebAssembly/multi-value

jedisct1•about 2 hours ago
Compiling WASM to C is a really good option: https://00f.net/2023/12/11/webassembly-compilation-to-c/
kg•about 1 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•28 minutes ago
You could use "local.tee". I kind of is "store" + "duplicate".
asibahi•18 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`)