灯下 登录
计算机科学 / SICP / 5.5.7 Interfacing Compiled Code to the Evaluator

Exercise 5.47 · 习题

Exercise 5.47: This section described how to
modify the explicit-control evaluator so that interpreted code can call
compiled procedures. Show how to modify the compiler so that compiled
procedures can call not only primitive procedures and compiled procedures, but
interpreted procedures as well. This requires modifying
compile-procedure-call to handle the case of compound (interpreted)
procedures. Be sure to handle all the same target and linkage
combinations as in compile-proc-appl. To do the actual procedure
application, the code needs to jump to the evaluator’s compound-apply
entry point. This label cannot be directly referenced in object code (since
the assembler requires that all labels referenced by the code it is assembling
be defined there), so we will add a register called compapp to the
evaluator machine to hold this entry point, and add an instruction to
initialize it:

(assign compapp (label compound-apply))
;; branches if flag is set:
(branch (label external-entry))
read-eval-print-loop …

To test your code, start by defining a procedure f that calls a

procedure g. Use compile-and-go to compile the definition of

f and start the evaluator. Now, typing at the evaluator, define

g and try to call f.

练习 5.47:本节描述了如何修改显式控制求值器,使解释代码能够调用编译过程。请说明如何修改编译器,使编译过程不仅能调用基本过程和其他编译过程,还能调用解释过程。这需要修改 compile-procedure-call,以处理复合(解释型)过程的情况。务必处理与 compile-proc-appl 中相同的所有目标和链接组合。为了实际执行过程应用,代码需要跳转到求值器的 compound-apply 入口点。由于汇编器要求所有被引用的标号都在被汇编的代码中定义,该标号不能在目标代码中直接引用,因此我们将在求值器机器中增加一个名为 compapp 的寄存器来保存这一入口点,并增加一条初始化指令:

(assign compapp (label compound-apply))
;; branches if flag is set:
(branch (label external-entry))
read-eval-print-loop …

为测试代码,先定义一个调用过程 g 的过程 f。使用 compile-and-go 编译 f 的定义并启动求值器。然后在求值器提示符下定义 g,并尝试调用 f。

Racket #lang sicp
(assign compapp (label compound-apply))
 ;; branches if flag is set:
 (branch (label external-entry))
read-eval-print-loop …