问题描述
使用基于寄存器的虚拟机与使用基于堆栈的虚拟机究竟有什么优缺点?
What exactly are the advantages and disadvantages to using a register-based virtual machine versus using a stack-based virtual machine?
在我看来,基于寄存器的机器似乎更直接编程,效率更高.那么,为什么JVM,CLR和Python VM都是基于堆栈的?
To me, it would seem as though a register based machine would be more straight-forward to program and more efficient. So why is it that the JVM, the CLR, and the Python VM are all stack-based?
推荐答案
已在某种程度上在Parrot VM的FAQ和相关文档中对此进行了回答:鹦鹉概述该文档的相关文本是这样的:
This has already been answered, to a certain level, in the Parrot VM's FAQ and associated documents:A Parrot OverviewThe relevant text from that doc is this:
做出此决定的原因主要是,通过在某种程度上类似于底层硬件,可以将Parrot字节码编译为高效的本地机器语言.
The reasoning for this decision is primarily that by resembling the underlying hardware to some extent, it's possible to compile down Parrot bytecode to efficient native machine language.
此外,许多高级语言程序由嵌套的函数和方法调用组成,有时带有词法变量以保存中间结果.在非JIT设置下,将弹出基于堆栈的VM,然后多次推送相同的操作数,而基于寄存器的VM将仅分配正确数量的寄存器并对其进行操作,这可以显着减少操作量和CPU时间.
Moreover, 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.
您可能还需要阅读以下内容:用于解释程序设计的寄存器与堆栈引用一下:
You may also want to read this: Registers vs stacks for interpreter designQuoting it a bit:
这篇关于寄存器与堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!