Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 976
Pteromys Avatar asked Jan 01 '12 13:01

Pteromys


People also ask

What is a register based virtual machine?

Register Based Virtual Machines In the register based implementation of a virtual machine, the data structure where the operands are stored is based on the registers of the CPU. There are no PUSH or POP operations here, but the instructions need to contain the addresses (the registers) of the operands.

What does register based mean?

Register-based research is research where register data are used. A study can rely solely on register data or register data can be used to supplement other datasets (e.g. clinical data or data obtained by a questionnaire survey).

What is a stack-based virtual machine?

In computer science, computer engineering and programming language implementations, a stack machine is a computer processor or a virtual machine in which the primary interaction is moving short-lived temporary values to and from a push down stack. In the case of a hardware processor, a hardware stack is used.

Why is JVM stack-based?

A stack-based design makes very few assumptions about the target hardware (registers, CPU features), so it's easy to implement a VM on a wide variety of hardware. Since the operands for instructions are largely implicit, the object code will tend to be smaller.


1 Answers

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

like image 84
viraptor Avatar answered Sep 22 '22 14:09

viraptor