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
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
arglcontains right contents so that procedure can be correctly applied. Note that in the procedure application we don’t need
envbecause we setup
procwhen we apply a procedure. Check
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 change
env. Procedure invocation is done with env set to the environmet where the procedure was defined. Thus if
fis not defined in the same
envwhere we are invoking it then it will certainly change
env. I wonder even if
fis defined in the same env, how a compiler can figure out this that
fis same as the
envfrom 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 correct
yis defined. Thus
envshould not change while evaluating the last argument
(g 'x). But since evaluating
(g 'x)can change
env, we need to save and restore
envbefore and after evaluating
- 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 changes
- 3rd case can be avoided for the last argument. Since evaluating
lookup-variable-valueand does not change
- 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
(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 of
(g 'x)because evaluating
'ydoes not require
- 3rd case again can be avoided for the last argument. Now since
yis a symbol in previous example), it’s evaluation does not change
All other cases in this example are same as in previoud example.