25

http://code.google.com/p/unladen-swallow/wiki/ProjectPlan我引用:

“使用 JIT 还可以让我们将 Python 从基于堆栈的机器转移到注册机器,这已被证明可以提高其他类似语言的性能(Ierusalimschy 等人,2005 年;Shi 等人,2005 年)。”

在大学里,我为带有递归过程的语言构建了一个简单的编译器——它为每个调用的过程维护堆栈帧——这样它们就可以被递归调用,这样参数和返回值就可以工作......

2件事:

1)考虑到上面引用中使用的术语,我认为我实施的东西将被视为“基于堆栈的机器”是否正确?

2) 如果我在第 (1) 点的假设是正确的,那么“注册机”是如何工作的?即它与基于堆栈的机器有何不同?

谢谢!

4

6 回答 6

24

A register machine is a hardware or software unit that when working with data takes it from memory, puts it in a location where it can work with it quickly, and then returns the result.

For example a regular CPU is a register machine. Since the ALU (the unit that works with numbers in a CPU) can only work with numbers in a register.

A stack based machine adds the data onto a stack and then either pops or pushes stuff onto it.

For example, adding two numbers would be

Push 2 // Push 2 onto the stack
Push 3 // Push 3 onto the stack
Add // Add the top two things on the stack.

When in a register machine it would be something like this.

Load x, r0 // Load x onto register 0
Load y, r1 // Load y onto register 1
Add r0, r1, r2 // Add 1 and 2 and store the result in register 2
于 2009-10-25T23:58:22.323 回答
11

注册机也几乎总是有一个堆栈。

但是堆栈机器很少有架构上可见的寄存器,或者它可能只有一两个。

寄存器机器可能有一些堆栈操作,甚至可能有堆栈寻址模式。

不同之处在于方向之一。寄存器机器将主要具有对寄存器进行操作的指令,并且将具有少数用于在寄存器和堆栈或内存之间加载和存储的操作。

堆栈机器..这些是非常罕见的,因为实际的硬件设备..将直接在堆栈上使用其指令进行操作,并且将有一些用于在堆栈和内存之间加载和存储的操作。

现在,根据引用的论文,硬件注册机器比硬件堆栈机器快的原因可能与软件“注册”虚拟机比软件“堆栈”机器快的原因无关。

对于软件虚拟机,显然需要执行的指令更少。这是根据引用论文中的声明凭经验确定的,但我想这是因为在寄存器机器中需要执行的开销指令(如推送、弹出和交换)要少得多,并且因为寄存器机器可以轻松重用操作数(如果它们仍然存在)躺在寄存器文件中,不需要加载或推送操作。当然,这真的只是记忆;它们是虚拟寄存器。

于 2009-10-26T00:22:01.590 回答
5

A register machine uses a fixed number of registers or buckets for storing intermediate values for computation. For example the "add" instruction could add the values in two specific registers and store the result in another register.

A stack based machine uses a stack for storing intermediate values during computation. For example, to add two numbers the "add" instructions pops off two values from the stack, adds them, and pushes the result back onto the stack.

于 2009-10-25T23:59:02.253 回答
4

1)考虑到上面引用中使用的术语,我认为我实施的东西将被视为“基于堆栈的机器”是否正确?

并不真地。某种堆栈几乎是实现递归函数调用的唯一方法。但是“基于堆栈的机器”在通过堆栈做所有事情方面走得更远。不仅是函数调用,还有算术运算。在某种程度上,它们的行为就像每条机器指令都是通过堆栈处理的函数调用。它使机器设计非常简单,但汇编程序/机器代码却很难编写。

2) 如果我在第 (1) 点的假设是正确的,那么“注册机”是如何工作的?即它与基于堆栈的机器有何不同?

寄存器机器有一些快速的内部存储(寄存器),并且对这些寄存器中的数据执行大部分操作。还有用于在寄存器和主存储器之间复制数据的附加机器指令。

IIRC有两种堆栈机器:

  • 累加器机器有一个“累加器”,它基本上是一个保存计算结果的寄存器(也可能提供一个操作数),大多数机器指令都在累加器上运行。
  • “纯”堆栈机器在使用操作数后将计算结果放在堆栈顶部。
于 2009-10-26T00:21:59.047 回答
2

A register machine is an abstract machine whose opcodes are defined by reference to their operation on a set of named registers, rather than by their operation on the top portion of a stack.

In a register machine: add could be defined to take three register names as operands, add the contents of the first two, and place the result in the third. (More common is the design where only one or two are named and the result always goes in a special accumulator register, but that's not the point.)

In a stack machine: add could be defined to pop two operands from the stack, add them, and push the result onto the stack.

于 2009-10-25T23:58:21.507 回答
2

Did your compiler generate machine code? If so, then its target was a register machine (nearly all CPU designs are register machines).

Stack machines store all values on a stack, whereas register machines have a fixed number of storage slots whose "addresses" do not change (unlike stack machines).

于 2009-10-26T00:02:41.100 回答