寄存器与堆栈

时间:2020-03-06 15:02:43  来源:igfitidea点击:

与基于堆栈的虚拟机相比,使用基于寄存器的虚拟机的优缺点到底是什么?

在我看来,基于寄存器的机器似乎更容易编程并且效率更高。那么,为什么JVM,CLR和Python VM都是基于堆栈的呢?

解决方案

我们需要多少个寄存器?

我可能至少还需要一个。

以硬件实现的基于寄存器的计算机将变得更加高效,这仅仅是因为对较慢的RAM的访问较少。但是,在软件中,即使基于寄存器的体系结构也很可能在RAM中具有"寄存器"。在这种情况下,基于堆栈的计算机将同样高效。

此外,基于堆栈的VM将使编写编译器变得更加容易。我们不必处理寄存器分配策略。本质上,我们可以使用无限数量的寄存器。

更新:我在假设虚拟机已解释的情况下编写了此答案。对于JIT编译的VM,可能不成立。我浏览了这篇论文,似乎表明使用寄存器体系结构JIT编译的VM可能更有效。

构建基于堆栈的VM的原因之一是,实际的VM操作码可以更小,更简单(无需对操作数进行编码/解码)。这使生成的代码更小,并且还使VM代码更简单。

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

基于寄存器的VM易于为其创建快速实现,并易于为其生成高度优化的代码。

第一次尝试时,建议我们从基于堆栈的VM开始。

对我而言,"基于寄存器的"虚拟机将"更直接编程"或者"更高效",这并不是显而易见的。也许我们认为虚拟寄存器将在JIT编译阶段提供捷径?肯定不是这种情况,因为实际的处理器可能比VM具有更多或者更少的寄存器,并且这些寄存器可能以不同的方式使用。 (示例:要减少的值最好放在x86处理器上的ECX寄存器中。)如果实际计算机上的寄存器多于VM,那么我们浪费的资源更少,使用"寄存器"则毫无用基于"的编程。

在某种程度上,已经在Parrot VM的FAQ和相关文档中对此进行了回答:
鹦鹉概述
该文档的相关文本是这样的:

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.

我们可能还需要阅读以下内容:解释程序设计的寄存器与堆栈
引用一下:

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.

基于堆栈的VM更简单,代码更紧凑。举一个真实的例子,一个朋友(大约30年前)在Cosmac上使用自制的Forth VM构建了一个数据记录系统。在具有2k ROM和256字节RAM的计算机上,Forth VM是30字节代码。