Chapter 5, Computing with Register Machines
Exercise 5.31
There are four cases given:
- saves and restores env around the evaluation of the operator.
- saves and restores env around the evaluation of each operand(except last one).
- saves and restores argl around the evaluation of each operand.
- saves and restores proc around the evaluation of the operand sequence.
Note that all the given expressions are procedure applications. Since every procedure application requires changing the env to the procedure where it is defined, we have to save env.
Do we need to worry about what expressions might get evaluated next and to make sure register contents are correct for those expressions?
The answer is no. Check env-sequence which takes care of it by saving and restoring env while evaluating each expression if there are more than one expression.
The point is we need not to care while evaluating an expression for preserving the register contents for other expressions which might get evaluated next! We only need to make sure that while evaluating the current expression, registers contain the right values.
For eg: while evaluating (f 'x 'y) we only need to worry about saves and restores for this procedure application. If there is another expression (+ a b) which follows it, we need not to worry about saving register contents for this another expression.
Now which registers we need to make sure have right contents while evaluating procedure application?
- To evaluate arguments, we must have correct
envso that all the operands can be evaluated. - Similarly we must have correct
envwhile evaluating operator. - After operator and operands are evaluted, we need to make sure that
procandarglcontains right contents so that procedure can be correctly applied. Note that in the procedure application we don’t needenvbecause we setupenvfrom theprocwhen we apply a procedure. Checkcompound-apply.
Note: We evaluate arguments from left to right in eceval. Later section, the compiler evalutes arguments in right to left but we need not to consider it because this exercise is specifically talking about saves/restores in eceval.
(f ‘x ‘y)
All of the above four saves and restores can be avoided for this case bec.:
- evaluating operator
frequires only lookup-variable value and does not modify env register. - Each operand is a quoted expression - whose evaluation does not change env, argl, and proc registers.
((f) ‘x ‘y)
-
First case can not be always avoided since we are invoking a procedure
(f). This can changeenv. Procedure invocation is done with env set to the environmet where the procedure was defined. Thus iffis not defined in the sameenvwhere we are invoking it then it will certainly changeenv. I wonder even iffis defined in the same env, how a compiler can figure out this thatenvoffis same as theenvfrom where it is invoked. -
Since all operands are still quoted expressions, all the remaining saves and restores can be avoided.
(f (g ‘x) y)
- First case can be avoided since we are only doing
lookup-variable-valuewhich does not modify env register. - 2nd case can not be avoided, Since
yis no longer quoted, we need correctenvwhereyis defined. Thusenvshould not change while evaluating the last argument(g 'x). But since evaluating(g 'x)can changeenv, we need to save and restoreenvbefore and after evaluating(g 'x)respectively. - 3rd and 4th case can not be avoided for the first argument because we need to do evaluatre
(g 'x)which is a procedure application. And procedure application changesarglandproc. - 3rd case can be avoided for the last argument. Since evaluating
yrequires onlylookup-variable-valueand does not changearglandproc. - Since 4th case is before and after the complete args list and since in this example 4th case can not be avoided for the first argument thus it can not be avoided even if second argument does not change
proc.
(f (g ‘x) ‘y)
This is also similar to the previous example with some differences:
- 2nd case can be avoided because
'yis quoted so we can avoid save and restore ofenvwhile evaluating(g 'x)because evaluating'ydoes not requireenv. - 3rd case again can be avoided for the last argument. Now since
'yis quoted(Unlikeyis a symbol in previous example), it’s evaluation does not changeargl.
All other cases in this example are same as in previoud example.