Register based vs Stack based VMs

Hey, I am trying to understand what are the design decisions taken when implementing a stack based VM against a register based one? What is the advantage of one over the other when designing a compiled language.

1 Like

I don’t think I’ve ever seen a register based VM, for a hopefully obvious reason - the machine being virtual, there’s no advantage (that I know of) to having a concept of registers.
With real CPUs, having data in registers is measurably faster than pushing and popping the same values to and from a stack.
But, unlike stack, whose space is “infinite” (limited in practice but very large compared to registers), the number of registers is finite and often small.
Thus, when building a compiler, if you want to use registers, you need fairly complex logic to do so (I don’t recall seeing a simple paper on this topic, search Internet for SSA and weep).
Therefore, most people opt for stack based VM because it’s much, much easier to implement and reason about.

2 Likes

@drazen.dotlic I think I found a paper doing a comparison of the performance between Stack based and Register based Virtual Machines.

Going through it now

1 Like

Very interesting, thanks for the find! Even after a very quick glance, it looks like register based VM is measurably faster than the stack based one, but it remains to be seen which exact metric are they using to compare the two.