寄存器与堆栈

发布于 2024-07-07 07:52:03 字数 118 浏览 6 评论 0原文

与使用基于堆栈的虚拟机相比,使用基于寄存器的虚拟机到底有哪些优点和缺点?

对我来说,基于寄存器的机器似乎更容易编程并且更高效。 那么为什么 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

莫言歌 2024-07-14 07:52:04

Parrot VM 的常见问题解答和相关文档中已经在一定程度上回答了这个问题:
Parrot 概述
该文档的相关文本是这样的:

Parrot VM 将采用寄存器架构,而不是堆栈架构。 它还将具有极低级的操作,与 Perl 和 Python 等的中级操作相比,更类似于 Java。

做出此决定的原因主要是,通过在某种程度上类似于底层硬件,可以将 Parrot 字节码编译为高效的本机机器语言。

此外,许多高级语言程序都包含嵌套的函数和方法调用,有时还使用词法变量来保存中间结果。 在非 JIT 设置下,基于堆栈的 VM 会多次弹出并压入相同的操作数,而基于寄存器的 VM 只会分配适量的寄存器并对其进行操作,这可以显着减少操作量和 CPU 时间。

您可能还想阅读以下内容:解释器设计的寄存器与堆栈
稍微引用一​​下:

毫无疑问,为堆栈机生成代码更容易。 大多数编译器新生都可以做到这一点。 为寄存器机生成代码有点困难,除非您将其视为带有累加器的堆栈机。 (这是可行的,尽管从性能的角度来看不太理想)目标的简单性并不是什么大不了的事,至少对我来说不是,部分原因是很少有人真正会直接瞄准它 - 我的意思是,来吧,你认识有多少人真正尝试为任何人都关心的东西编写编译器? 数字很​​小。 另一个问题是,许多具有编译器知识的人已经习惯了以寄存器机为目标,因为这就是所有常用的硬件 CPU 的特点。

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:

the Parrot VM will have a register architecture, rather than a stack architecture. It will also have extremely low-level operations, more similar to Java's than the medium-level ops of Perl and Python and the like.

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.

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 design
Quoting it a bit:

There is no real doubt, it's easier to generate code for a stack machine. Most freshman compiler students can do that. Generating code for a register machine is a bit tougher, unless you're treating it as a stack machine with an accumulator. (Which is doable, albeit somewhat less than ideal from a performance standpoint) Simplicity of targeting isn't that big a deal, at least not for me, in part because so few people are actually going to directly target it--I mean, come on, how many people do you know who actually try to write a compiler for something anyone would ever care about? The numbers are small. The other issue there is that many of the folks with compiler knowledge already are comfortable targeting register machines, as that's what all hardware CPUs in common use are.

清音悠歌 2024-07-14 07:52:04

传统上,虚拟机实现者更喜欢基于堆栈的架构,而不是基于寄存器的架构,因为“虚拟机实现简单”,易于编写编译器后端 - 大多数虚拟机最初设计为托管单一语言和代码密度以及堆栈架构的可执行文件总是小于寄存器架构的可执行文件。 简单性和代码密度是性能成本。

研究表明,基于寄存器的架构比基于堆栈的架构平均需要少执行 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.

葮薆情 2024-07-14 07:52:04

构建基于堆栈的 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.

浅浅淡淡 2024-07-14 07:52:04

基于堆栈的虚拟机更简单,代码也更紧凑。 作为一个现实世界的例子,一位朋友(大约 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.

裂开嘴轻声笑有多痛 2024-07-14 07:52:04

对我来说,“基于寄存器”的虚拟机“更直接地编程”或“更高效”并不明显。 也许您认为虚拟寄存器会在 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.

一人独醉 2024-07-14 07:52:04

基于堆栈的虚拟机更容易生成代码。

基于寄存器的虚拟机更容易创建快速实现,也更容易生成高度优化的代码。

对于您的第一次尝试,我建议从基于堆栈的虚拟机开始。

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.

小情绪 2024-07-14 07:52:03

在硬件中实现时,基于寄存器的机器将变得更加高效,因为对较慢 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文