SICP:5.5.6 Lexical Addressing
SICP:5.5.6 Lexical Addressing
本文逐段翻译 SICP 第二版 §5.5.6《Lexical Addressing》,说明 compiler 如何通过 lexical address 优化 variable lookup,并引入 compile-time environment、
lexical-address-lookup、lexical-address-set!以及 Exercises 5.39–5.44。
TL;DR
- 解决的问题:普通
lookup-variable-value必须从当前 frame 向外逐层比较变量名;嵌套层次深、frame 中变量多时,lookup 成本会很高。 - 核心判断:在 lexical scoping 下,compiler 可以在 compile time 确定变量位于当前环境外第几个 frame、该 frame 中的第几个位置。
- 实现方式:用
(frame-number, displacement-number)表示 lexical address,runtime 直接按地址取值或写值。 - 边界:普通 lambda binding 可以静态定位;global environment 仍需动态查找,因为全局名称可能在交互式环境中被定义或重新定义。
- 延伸:Exercises 5.39–5.44 将 lexical address 扩展到 lookup、assignment、compile-time env、internal definitions 与 primitive open-coding。
目录
| 章节 | 说明 |
|---|---|
| 5.5.6 Lexical Addressing | variable lookup 的优化动机 |
| Lexical addresses | frame number 与 displacement number |
| Compile-time environment | compiler 如何记录 binding 位置 |
| 练习 | Exercises 5.39–5.44 |
| 脚注 | 原文脚注 45–47 |
5.5.6 Lexical Addressing
compiler 中一个常见的优化机会是 variable lookup。当前的 compiler 使用 evaluator machine 的 lookup-variable-value operation 查找变量。这个 operation 从当前环境中的第一个 frame 开始,逐个比较 frame 中当前 bound 的变量;如果没有找到,就继续检查外层 frame。这种查找对于深度嵌套的 frame,或者包含许多变量的 frame,可能会相当昂贵。
例如,考虑下面的 expression:
(let ((x 3) (y 4))
(lambda (a b c d e)
(let ((y (* a b x))
(z (+ c d x)))
(* x y z))))
将 let expression 展开成 lambda application 后,它等价于:
((lambda (x y)
(lambda (a b c d e)
((lambda (y z) (* x y z))
(* a b x)
(+ c d x))))
3
4)
在这个 expression 的 (* x y z) 中,每次 lookup x 时,lookup-variable-value 都必须先确定:x 不是第一个 frame 中的 y 或 z,也不是第二个 frame 中的 a、b、c、d 或 e。如果允许我们暂时假定程序中没有 define,变量只通过 lambda binding,那么 compiler 就可以在 compile time 确定,这里的 x 位于当前环境外两层,并且是该 frame 中的第一个变量。
这依赖于 lexical scoping 的一个重要性质:runtime environment 的结构与程序的 lexical structure 平行。也就是说,当我们看到某个变量的出现位置时,可以沿着源程序中的嵌套结构向外追踪它对应的 binding。compiler 因而能够预先计算变量的 lexical address。
Lexical addresses
我们为 evaluator machine 增加一个新的 operation lexical-address-lookup。它接收一个 environment 和一个 lexical address。lexical address 由两个数字组成:
- frame number:从当前 environment 开始,需要跳过多少个 frame;
- displacement number:在目标 frame 中,需要跳过多少个变量。
operation 返回指定地址处的变量值。compiler 生成的 code 不再调用 lookup-variable-value,而是生成对 lexical-address-lookup 的调用。
类似地,我们可以用 lexical-address-set! 替代 set-variable-value!,按照 lexical address 修改变量的值。
为了说明 lexical address,考虑下面的 expression,其中 <e1> 和 <e2> 是两个待分析的子表达式:
((lambda (x y)
(lambda (a b c d e)
((lambda (y z) <e1>)
<e2>
(+ c d x))))
3
4)
在 <e1> 中,变量的 lexical addresses 是:
x的地址是(2, 0);y的地址是(0, 0);c的地址是(1, 2)。
在 <e2> 中,变量的 lexical addresses 是:
x的地址是(1, 0);y的地址是(1, 1);c的地址是(0, 2)。
注意,同一个变量名在不同的 lexical position 可能对应不同的地址。<e1> 中的 y 是最近一层 lambda 的 parameter;<e2> 中的 y 则是外层 lambda 的第二个 parameter。lexical address 描述的是某个 occurrence 所引用的 binding,而不是单纯描述变量名。
Compile-time environment
为了在 compile time 计算 lexical address,我们需要一种 data structure,记录在每个 variable access 位置上,各个变量位于哪些 frame、哪些位置。这个 data structure 只需要记录变量名,不需要记录 runtime values。它可以表示为一个 frame list,其中每个 frame 又是一个变量 list:
((y z) (a b c d e) (x y))
这里最内层 frame 位于 list 的最前面;每个 frame 中的变量顺序就是它们在对应 lambda 的 parameter list 中出现的顺序。
为了让 compiler 使用这个信息,需要向 compile 增加一个额外的 compile-time environment 参数,并将这个参数传递给所有 code generator。编译 top-level expression 时,compile-time environment 从空 list 开始。
当 compiler 编译 lambda body 时,compile-lambda-body 会把 procedure 的 formal parameters 组成一个新的 frame,并将这个 frame 加入 compile-time environment。随后,body 中的 sequence 会在扩展后的 compile-time environment 下编译。
在每个 variable access 位置,compile-variable 和 compile-assignment 使用 compile-time environment 生成相应的 lexical address。这样,runtime code 可以直接使用 lexical-address-lookup 或 lexical-address-set!,无需在一组 frame 中逐个比较变量名。
练习
Exercise 5.39
写出 lexical-address-lookup,它接收一个 lexical address 和一个 runtime environment,并返回该地址处的值。如果该值是 *unassigned*,则 signal an error。还要写出 lexical-address-set!,它接收一个 lexical address、一个 value 和一个 environment,并修改该地址处的值。
Exercise 5.40
修改 compiler,使它维护 compile-time environment。为 compile 和各个 code generator 增加 compile-time environment 参数,并在 compile-lambda-body 中扩展该环境。
Exercise 5.41
写出 procedure find-variable。它接收一个变量和一个 compile-time environment,并返回该变量对应的 lexical address。
在前面 expression 的 <e1> 中,compile-time environment 是:
((y z) (a b c d e) (x y))
因此,find-variable 应产生下面的结果:
(find-variable 'c '((y z) (a b c d e) (x y)))
(1 2)
(find-variable 'x '((y z) (a b c d e) (x y)))
(2 0)
(find-variable 'w '((y z) (a b c d e) (x y)))
not-found
Exercise 5.42
使用 find-variable 重写 compile-variable 和 compile-assignment,使它们生成 lexical-address instructions。
如果 find-variable 返回 not-found,说明该变量没有出现在 compile-time environment 中。此时使用 evaluator operations,像以前一样动态搜索 binding。compile-time environment 中唯一可能找不到的变量位于 global environment 中;global environment 是 runtime environment 的一部分,但不是 compile-time environment 的一部分。
也可以使用 (op get-global-environment),直接在 global environment 中查找变量。用本节开头的 nested lambda combination 测试你的实现。
lexical address 不能用于访问 global environment,因为 global names 可能在交互式环境中被定义或重新定义。假设 internal definitions 已经被 scan out,那么只有 top-level definitions 会作用于 global environment;编译 definition 时,也不应该把被定义的名字加入 compile-time environment。
Exercise 5.43
在 block structure 中出现的 internal definitions 并不是真正的 definitions。procedure body 应该表现得好像这些 internal variables 是普通的 lambda variables:先安装这些 variables,再使用正确的 value 通过 set! 初始化它们。
修改 compiler,使它在编译 procedure body 之前执行与 scan-out transformation 相同的转换。参见 §4.1.6 和 Exercise 4.16。
Exercise 5.44
compile-time environment 还可以用于其他目的。Exercise 5.38 要求通过把 primitive procedure 的名字视为 reserved words 来 open-code primitive procedures。如果 program 重新 bind 了这些名字,compiler 仍然会把它们 open-code 成 primitive operation,而忽略新的 binding。例如:
(lambda (+ * a b x y)
(+ (* a x) (* b y)))
这个 procedure 可以用 +matrix、*matrix 以及四个 matrix 调用。即使如此,open-coding compiler 仍会在 body 中把 + 和 * open-code 成 primitive operations。
修改 open-coding compiler,使它查询 compile-time environment,从而为涉及 primitive names 的 expression 生成正确的 code。只要 program 不定义或 set! 这些 names,这样的实现就可以正常工作。
脚注
小结
本节把 variable lookup 从 runtime 的逐层名字搜索,转化为 compile time 计算、runtime 直接访问的 lexical address。地址由 frame number 和 displacement number 组成,分别描述目标 frame 的层次与变量位置。为了生成这些地址,compiler 维护只包含变量名的 compile-time environment,并将其沿 lambda body 向内扩展。普通 lexical bindings 因而可以高效定位;global bindings 和 internal definitions 则需要保留动态处理或先执行 scan-out transformation。
参考资料
- Harold Abelson、Gerald Jay Sussman、Julie Sussman:SICP 第二版 MIT Press 页面
- SICP HTML full text
- SICP:5.5.5 An Example of Compiled Code(前一节:compiled code 示例)
- SICP:5.5.7 Compiling and Running(后一节:compiled code 的运行)
评论 (0)