Question

Why are register-based virtual machines better than stack-based ones?

Specifically, in the Parrot VM's document, the designer explains the benefits of register machines:

[...] many programs in high-level languages consist of nested function and method calls, sometimes with lexical variables to hold intermediate results. Under non-JIT settings, a stack-based VM will be popping and then pushing the same operands many times, while a register-based VM will simply allocate the right amount of registers and operate on them, which can significantly reduce the amount of operations and CPU time.

but why are the same operands pushed many times?

Was it helpful?

Solution

It seems like they describe a VM which executes the code as described in the language design, bytecode-by-bytecode without compiling or optimisation. In that case it is true. Think about code doing something like this for example:

x = first(a,b,c)
y = second(a,b,c)
third(y,x)

With a register based system, you might be able to simply put the arguments in whatever position they're expected (if registers can be used to pass arguments). If all registers are "global", not per-function (or at least restored when poping the call-stack) you might not need to do anything between the call to first and second.

If you have a stack-based VM, you'd end up with something like (hopefully you do have swap):

push a
push b
push c
call first
push a # pushing same arguments again
push b
push c
call second
swap
call third

Also if you calculate a math expression which reuses the same variables, you might need to do something like this:

push a
push b
add
push a
push c
add
add

instead of (assuming there are registers a,b,c and you can destroy the contents of b and c):

add b, a
add c, a
add b, c # result in b

this avoids restoring a, which needed to be done in a separate push in the first case.

Then again, I'm just guessing the examples, maybe they meant some other case...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top