SICP:5.1.3 Subroutines
SICP:5.1.3 Subroutines
本文逐段翻译 SICP 第二版 §5.1.3《Subroutines》,保留 GCD subroutine、continuation register、label-valued
assign/goto、nested subroutine calls 与 Figure 5.7 原始 GIF;图片原始尺寸为 405×203。
TL;DR
- 当同一 computation 被 machine 的多个 parts 使用时,应该共享 components,而不是重复构造它们。
- GCD example 从两份 duplicated controller sequence 演进为一份共享的
gcdsubroutine。 continueregister 可以保存 subroutine 的 return point;更一般的做法是直接把 controller label 存入 register。- 如果一个 subroutine 调用另一个 subroutine,共享单一
continueregister 会覆盖外层 return point;递归机制中的 stack 将解决这种 nested-call 问题。
目录
| 章节 | 说明 |
|---|---|
| 5.1.3 Subroutines | subroutine sharing 的动机 |
| Duplicating GCD computations | 两份 GCD sequence |
| Sharing data-path components | 复用 registers 与 operations |
| A continue register | 用 distinguished value 返回 |
| Labels as continuations | 通用的 return point |
| Nested subroutine calls | continuation register 的限制 |
5.1.3 Subroutines
设计 machine 执行 computation 时,我们经常希望让 computation 的不同 parts 共享 components,而不是复制这些 components。
Duplicating GCD computations
考虑一台包含两次 GCD computation 的 machine:一次计算 registers a 和 b contents 的 GCD,另一次计算 registers c 和 d contents 的 GCD。
我们可以先假设有一个 primitive gcd operation,然后把两次 gcd 分别展开成更 primitive 的 operations。Figure 5.7 展示了 resulting machine 中 GCD portions 的 data paths(没有展示它们如何连接到 machine 的其余部分),以及对应的 controller sequence portions。

Figure 5.7: Portions of the data paths and controller sequence for a machine with two GCD computations.
这台 machine 有两个 remainder operation boxes 和两个 equality-test boxes。如果 duplicated components 很复杂(remainder box 就是如此),这样构造 machine 并不 economical。
Sharing data-path components
只要不影响 larger machine 的其余 computation,我们就可以让两次 GCD computation 使用同一组 data-path components,从而避免 duplicate。
如果 controller 到达 gcd-2 时已经不再需要 registers a 和 b 中的 values(或者这些 values 可以移动到其他 registers 保存),就可以把 machine 改成:第二次 GCD 也使用 registers a 和 b,而不是 c 和 d。
这样,我们移除了 duplicated data-path components(data paths 又回到 Figure 5.1 的形式),但 controller 现在有两段 GCD sequences,它们除了 entry-point labels 不同之外完全相同:
gcd-1
(test (op =) (reg b) (const 0))
(branch (label after-gcd-1))
(assign t (op rem) (reg a) (reg b))
(assign a (reg b))
(assign b (reg t))
(goto (label gcd-1))
after-gcd-1
gcd-2
(test (op =) (reg b) (const 0))
(branch (label after-gcd-2))
(assign t (op rem) (reg a) (reg b))
(assign a (reg b))
(assign b (reg t))
(goto (label gcd-2))
after-gcd-2
更好的办法是:把两段 sequences 替换为指向一段共享 sequence 的 branches,也就是放在 instruction sequence 末尾的 gcd subroutine;subroutine 结束时,再 branch 回 main instruction sequence 中正确的位置。
A continue register
在 branch 到 gcd 之前,先把一个 distinguishing value(例如 0 或 1)放入特殊 register continue。GCD subroutine 结束时,根据 continue register 的 value,返回 after-gcd-1 或 after-gcd-2。
这样只需要一份 GCD instructions:
gcd
(test (op =) (reg b) (const 0))
(branch (label gcd-done))
(assign t (op rem) (reg a) (reg b))
(assign a (reg b))
(assign b (reg t))
(goto (label gcd))
gcd-done
(test (op =) (reg continue) (const 0))
(branch (label after-gcd-1))
(goto (label after-gcd-2))
;; Before branching to gcd from the first place where
;; it is needed, we place 0 in the continue register
(assign continue (const 0))
(goto (label gcd))
after-gcd-1
;; Before the second use of gcd, we place 1 in the continue register
(assign continue (const 1))
(goto (label gcd))
after-gcd-2
这消除了 Figure 5.8 中 duplicate 的 controller sequence。不过,如果 controller sequence 中存在很多 GCD computations,这种方法仍然 awkward:为了决定 gcd subroutine 结束后从哪里继续,data paths 中需要 tests,controller 中需要为所有调用位置添加 branch instructions。
Labels as continuations
实现 subroutines 的更 powerful method,是让 continue register 保存 controller sequence 中的一个 entry point label;subroutine 完成后,execution 就在这个 entry point 继续。
这种 strategy 需要 register machine 的 data paths 与 controller 之间有一种新的 connection:machine 必须能够把一个 controller-sequence label assign 给 register,并且能够从 register fetch 这个 value,把它当作指定 entry point 继续 execution。
因此,我们扩展 register-machine language 的 assign instruction,使它允许把 controller sequence 中的 label(作为一种特殊的 constant)assign 给 register;同时扩展 goto instruction,使它允许根据 register contents 指定的 entry point 继续执行,而不只是根据 constant label 指定 entry point。
使用这些新 constructs,可以让 gcd subroutine 结束时 branch 到 continue register 中保存的位置。结果如下:
gcd
(test (op =) (reg b) (const 0))
(branch (label gcd-done))
(assign t (op rem) (reg a) (reg b))
(assign a (reg b))
(assign b (reg t))
(goto (label gcd))
gcd-done
(goto (reg continue))
;; Before calling gcd, we assign to continue
;; the label to which gcd should return.
(assign continue (label after-gcd-1))
(goto (label gcd))
after-gcd-1
;; Here is the second call to gcd, with a different continuation.
(assign continue (label after-gcd-2))
(goto (label gcd))
after-gcd-2
Figure 5.10 所展示的 strategy 比 Figure 5.9 更简单,也更 general:调用者不必为每一个 possible return location 在 subroutine 内添加独立 test;它只需要把正确的 label 放入 continue。
Nested subroutine calls
一台包含多个 subroutines 的 machine,可以使用多个 continuation registers,例如 gcd-continue、factorial-continue;也可以让所有 subroutines 共享一个 continue register。
共享 register 更 economical,但如果一个 subroutine(sub1)调用另一个 subroutine(sub2),就必须小心:除非 sub1 在为调用 sub2 设置 continue 之前,把 continue 的 contents 保存到另一个 register 中,否则 sub1 完成时就不知道应该返回哪里。
下一节将发展用于处理 recursion 的 mechanism,它也会为 nested subroutine calls 提供更好的 solution。
小结
Subroutine 设计从 component sharing 开始:先消除 duplicated data paths,再将相同的 controller sequence 抽取为共享的 gcd subroutine。continue register 从保存简单 distinguishing value 发展到保存真正的 controller label,使 subroutine return point 由调用者动态指定。但共享单一 continuation 在 nested calls 中会产生覆盖问题,后续 stack mechanism 将解决这一限制。
参考
- Abelson, Sussman, and Sussman, Structure and Interpretation of Computer Programs, 2nd ed., §5.1.3 “Subroutines”.
- MIT Press HTML full text
评论 (0)