# SICP Solutions

### Chapter 5, Computing with Register Machines

#### Exercise 5.47

Interesting!

Few points how it will work by using the example suggested for testing(compiled f calls interpreted g):

• When f is compiled, it creates a compiled procedure and stores it in env.
• Since f contains a call to g, the compiled procedure will also contain instructions to invoke g which required first to lookup g and store the result of lookup in proc.
• Then compiled procedure will contain instructions to evaluate all the arguments of g and place those arguments in argl.
• Now, let’s talk about evaluator. When g is evaluated/interpreted, it also gets stored in the same env. Thus the instructions generated by compiler to lookup g will now return the procedure g!
• How evaluator invokes a compund procedure? It starts from entry point compound-apply, and at that point, reg proc should contain the required procedure to be evaluated and arguments should be in argl.
• Well, this has already been done by the instructions generated by compiler for invoking g.
• So, what’s the missing piece? Just two things: (i) how to transfer the control to evaluator from compiler and then (ii) how to return back to the compiler intructions after procedure gets executed by the evaluator instructions.
• For (i), while calling the procedure in the compiler, we just have to do one more test apart from the existing test of primitive-procedure?, now we also check for compound-procedure? and jump to the code for calling compound procedure. As suggested in the exercise we jump to the location compound-apply stored in compapp.
• For (ii), We use continue for return. We know that evaluator expects that while in compound-apply the place to return is stored on top of the stack. So we just need to save the place where we have to return on top of the stack! This can be done by writing a parallel a new procedure, say compound-proc-appl similar to compiled-proc-appl. And for each of the cases we save the register where we have to return on top of the stack!

That’s it!

Here are the changes: