寄存器与堆栈 [英] registers vs stacks

查看:381
本文介绍了寄存器与堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用基于寄存器的虚拟机与使用基于堆栈的虚拟机究竟有什么优缺点?

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 Overview The relevant text from that doc is this:

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

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.

做出此决定的原因主要是,通过在某种程度上类似于底层硬件,可以将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 design Quoting it a bit:

毫无疑问,为堆栈计算机生成代码更容易.大多数新生编译器学生都可以做到这一点.除非您将其视为带有累加器的堆栈机,否则为寄存器机生成代码要困难一些. (这是可行的,尽管从性能的角度来看还是不理想的.)定位的简单性并不是什么大问题,至少对我而言不是这样,部分原因是实际上很少有人会直接定位它-我的意思是,来吧,您知道有多少人实际尝试为任何人都在乎的事情编写编译器?数字很​​小.另一个问题是,许多具有编译器知识的人已经可以轻松地瞄准寄存器计算机,因为这是所有常用的硬件CPU的功能.

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.

这篇关于寄存器与堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆