寄存器与堆栈
与使用基于堆栈的虚拟机相比,使用基于寄存器的虚拟机到底有哪些优点和缺点?
对我来说,基于寄存器的机器似乎更容易编程并且更高效。 那么为什么 JVM、CLR 和 Python VM 都是基于堆栈的呢?
What exactly are the advantages and disadvantages to using a register-based virtual machine versus using a stack-based virtual machine?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Parrot VM 的常见问题解答和相关文档中已经在一定程度上回答了这个问题:
Parrot 概述
该文档的相关文本是这样的:
您可能还想阅读以下内容:解释器设计的寄存器与堆栈
稍微引用一下:
This has already been answered, to a certain level, in the Parrot VM's FAQ and associated documents:
A Parrot Overview
The relevant text from that doc is this:
You may also want to read this: Registers vs stacks for interpreter design
Quoting it a bit:
传统上,虚拟机实现者更喜欢基于堆栈的架构,而不是基于寄存器的架构,因为“虚拟机实现简单”,易于编写编译器后端 - 大多数虚拟机最初设计为托管单一语言和代码密度以及堆栈架构的可执行文件总是小于寄存器架构的可执行文件。 简单性和代码密度是性能成本。
研究表明,基于寄存器的架构比基于堆栈的架构平均需要少执行 47% 的 VM 指令,并且寄存器代码比相应的堆栈代码大 25%,但由于代码较大,这会增加获取更多 VM 指令的成本每个 VM 指令的大小仅涉及 1.07% 的额外实际机器负载,可以忽略不计。 基于寄存器的虚拟机的整体性能是,执行标准基准测试所需的时间平均减少了 32.3%。
Traditionally, virtual machine implementors have favored stack-based architectures over register-based due to 'simplicity of VM implementation' ease of writing a compiler back-end - most VMs are originally designed to host a single language and code density and executables for stack architecture are invariably smaller than executables for register architectures. The simplicity and code density are a cost of performance.
Studies have shown that a registered-based architecture requires an average of 47% less executed VM instructions than stack-based architecture, and the register code is 25% larger than corresponding stack code but this increase cost of fetching more VM instructions due to larger code size involves only 1.07% extra real machine loads per VM instruction which is negligible. The overall performance of the register-based VM is that it takes, on average, 32.3% less time to execute standard benchmarks.
构建基于堆栈的 VM 的原因之一是实际的 VM 操作码可以更小、更简单(无需对操作数进行编码/解码)。 这使得生成的代码更小,也使得VM代码更简单。
One reason for building stack-based VMs is that that actual VM opcodes can be smaller and simpler (no need to encode/decode operands). This makes the generated code smaller, and also makes the VM code simpler.
基于堆栈的虚拟机更简单,代码也更紧凑。 作为一个现实世界的例子,一位朋友(大约 30 年前)在 Cosmac 上使用自制的 Forth VM 构建了一个数据记录系统。 Forth VM 是一台具有 2k ROM 和 256 字节 RAM 的机器上的 30 字节代码。
Stack based VM's are simpler and the code is much more compact. As a real world example, a friend built (about 30 years ago) a data logging system with a homebrew Forth VM on a Cosmac. The Forth VM was 30 bytes of code on a machine with 2k of ROM and 256 bytes of RAM.
对我来说,“基于寄存器”的虚拟机“更直接地编程”或“更高效”并不明显。 也许您认为虚拟寄存器会在 JIT 编译阶段提供捷径? 当然情况并非如此,因为真实的处理器可能比VM具有更多或更少的寄存器,并且这些寄存器可以以不同的方式使用。 (例如:要递减的值最好放置在 x86 处理器上的 ECX 寄存器中。)如果真实机器的寄存器比虚拟机多,那么您就会浪费资源,而使用“寄存器”则不会获得任何资源。基于”的编程。
It is not obvious to me that a "register-based" virtual machine would be "more straight-forward to program" or "more efficient". Perhaps you are thinking that the virtual registers would provide a short-cut during the JIT compilation phase? This would certainly not be the case, since the real processor may have more or fewer registers than the VM, and those registers may be used in different ways. (Example: values that are going to be decremented are best placed in the ECX register on x86 processors.) If the real machine has more registers than the VM, then you're wasting resources, fewer and you've gained nothing using "register-based" programming.
基于堆栈的虚拟机更容易生成代码。
基于寄存器的虚拟机更容易创建快速实现,也更容易生成高度优化的代码。
对于您的第一次尝试,我建议从基于堆栈的虚拟机开始。
Stack based VMs are easier to generate code for.
Register based VMs are easier to create fast implementations for, and easier to generate highly optimized code for.
For your first attempt, I recommend starting with a stack based VM.
在硬件中实现时,基于寄存器的机器将变得更加高效,因为对较慢 RAM 的访问较少。 然而,在软件中,即使是基于寄存器的架构也很可能在 RAM 中拥有“寄存器”。 在这种情况下,基于堆栈的机器将同样高效。
此外,基于堆栈的虚拟机将使编写编译器变得更加容易。 您不必处理寄存器分配策略。 本质上,您可以使用无限数量的寄存器。
更新:我在假设有一个解释的虚拟机的情况下写了这个答案。 对于 JIT 编译的虚拟机来说,这可能不成立。 我遇到了 这篇论文 这似乎表明 JIT 已编译VM 使用寄存器架构可能会更有效。
Implemented in hardware, a register-based machine is going to be more efficient simply because there are fewer accesses to the slower RAM. In software, however, even a register based architecture will most likely have the "registers" in RAM. A stack based machine is going to be just as efficient in that case.
In addition a stack-based VM is going to make it a lot easier to write compilers. You don't have to deal with register allocation strategies. You have, essentially, an unlimited number of registers to work with.
Update: I wrote this answer assuming an interpreted VM. It may not hold true for a JIT compiled VM. I ran across this paper which seems to indicate that a JIT compiled VM may be more efficient using a register architecture.