Exercise 5.11: When we introduced save
and restore in 5.1.4, we didn’t specify what would happen
if you tried to restore a register that was not the last one saved, as in the
sequence
(save y)
(save x)
(restore y)
There are several reasonable possibilities for the meaning of restore:
(restore y) puts into y the last value saved on the stack,
regardless of what register that value came from. This is the way our
simulator behaves. Show how to take advantage of this behavior to eliminate
one instruction from the Fibonacci machine of 5.1.4 (Figure 5.12).
(restore y) puts into y the last value saved on the stack, but
only if that value was saved from y; otherwise, it signals an error.
Modify the simulator to behave this way. You will have to change save
to put the register name on the stack along with the value.
(restore y) puts into y the last value saved from y
regardless of what other registers were saved after y and not restored.
Modify the simulator to behave this way. You will have to associate a separate
stack with each register. You should make the initialize-stack
operation initialize all the register stacks.
练习 5.11:在 5.1.4 节引入 save 和 restore 时,我们没有规定如果试图恢复一个不是最后被保存的寄存器会发生什么,例如下面的指令序列:
(save y)
(save x)
(restore y)
restore 的含义有几种合理的可能性:
(restore y) 将栈上最后保存的值放入 y,不论该值来自哪个寄存器。这是我们的模拟器当前的行为。请说明如何利用这一行为从 5.1.4 节的 Fibonacci 机器(图 5.12)中省去一条指令。
(restore y) 将栈上最后保存的值放入 y,但仅当该值是从 y 保存的;否则报错。修改模拟器以实现这一行为。你需要修改 save,使其在将值压栈时同时压入寄存器名。
(restore y) 将最后从 y 保存的值放入 y,而不论 y 被保存之后还有哪些其他寄存器被保存且尚未恢复。修改模拟器以实现这一行为。你需要为每个寄存器维护一个独立的栈,并使 initialize-stack 操作初始化所有寄存器的栈。
(save y)
(save x)
(restore y)