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

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

In particular, in the Parrot VM document, the developer explains the advantages of registration machines:

[...] many programs in high-level languages ​​consist of nested calls to functions and methods, sometimes with lexical variables for storing intermediate results. In parameters other than JIT, the stack-based virtual machine will be displayed, and then the same operands will be pressed repeatedly, while the registry-based virtual machine simply selects the necessary number of registers and works on them, which can significantly reduce the number of operations and processor time.

but why are the same operands repeatedly put forward?

+6
source share
1 answer

They seem to describe a virtual machine that runs code, as described in the language design, bytecode-by-bytecode without compilation or optimization. In this case, it is true. Think about the code doing something like this, for example:

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

In a case-based system, you can simply put arguments in whatever position they expect (if registers can be used to pass arguments). If all the registers are "global," and not for each function (or at least restored when the call stack is loaded), you may not need to do anything between the first and second calls.

If you have a stack-based virtual machine, you get something like (I hope you 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 are calculating a mathematical expression that uses the same variables, you might need to do something like this:

 push a push b add push a push c add add 

instead (if 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 the restoration of a , which had to be performed by a separate click in the first case.

Again, I’m just guessing about the examples, maybe they meant another case ...

+7
source

Source: https://habr.com/ru/post/904839/


All Articles