SICP:5.5.3 Compiling Combinations
SICP:5.5.3 Compiling Combinations
本文逐段翻译 SICP 第二版 §5.5.3《Compiling Combinations》,保留
compile-application、construct-arglist、primitive/compiled dispatch、tail-recursive compiled procedure application,以及compile-proc-appl的完整 code generator 与脚注 39–41。
TL;DR
- Procedure application 的 compiled code 依次完成 operator compilation、argument-list construction 和 procedure call。
- Operator 以 target
proc、linkagenext编译;operands 以 targetval编译,再逆序构造argl。 compile-procedure-call根据proc是 primitive 还是 compiled procedure 做 dispatch。- Compiled procedure 的
returnlinkage 可以直接跳转,不保存continue,因此 compiled code 保持 tail recursion。 compile-proc-appl根据 target 是否为val、linkage 是否为return分成四种情况。
目录
| 章节 | 说明 |
|---|---|
| 5.5.3 Compiling Combinations | procedure application 的 compilation |
| Compiling applications | operator、operands 与 argl |
| Constructing the argument list | 从最后一个 operand 开始构造 |
| Applying procedures | primitive/compiled dispatch |
| Applying compiled procedures | 四种 target/linkage cases |
| 脚注 | 原文脚注 39–41 |
5.5.3 Compiling Combinations
Compilation process 的 essence,是 procedure applications 的 compilation。
Compiling applications
给定 target 和 linkage 后,一个 combination 的 compiled code 具有如下形式:
<compilation of operator, target proc, linkage next>
<evaluate operands and construct argument list in argl>
<compilation of procedure call with given target and linkage>
在 evaluate operator 和 operands 时,env、proc、argl registers 可能需要被 save 和 restore。
注意,这是 compiler 中唯一指定 target 为 val 以外 register 的地方。
所需 code 由 compile-application 生成。它递归 compile operator,生成把待 apply procedure 放入 proc 的 code;同时 compile operands,生成 evaluate application 中各个 operands 的 code。
Operand instructions 由 construct-arglist 与构造 arguments list 并把它放入 argl 的 code 组合。所得 argument-list code 再与 procedure code 和执行 procedure call 的 code 组合;procedure-call code 由 compile-procedure-call 产生。
Append code sequences 时,evaluate operator 周围必须 preserve env,因为 operator evaluation 可能修改随后 evaluate operands 所需的 env。构造 argument list 周围必须 preserve proc,因为 operand evaluation 可能修改实际 procedure application 需要的 proc。此外,整个过程都必须 preserve continue,因为 procedure call 的 linkage 需要它。
(define (compile-application exp target linkage)
(let ((proc-code
(compile (operator exp) 'proc 'next))
(operand-codes
(map (lambda (operand)
(compile operand 'val 'next))
(operands exp))))
(preserving '(env continue)
proc-code
(preserving '(proc continue)
(construct-arglist operand-codes)
(compile-procedure-call
target linkage)))))
Constructing the argument list
Argument-list construction code 会把每个 operand evaluate 到 val,然后把这个 value cons 到正在 argl 中累积的 argument list 上。
由于我们按顺序把 arguments cons 到 argl,必须从最后一个 argument 开始,最后处理第一个 argument,才能让 resulting list 中的 arguments 保持 first-to-last order。
为了避免仅为这组 evaluation 初始化 argl 为空 list 所浪费的一条 instruction,让第一个 code sequence 负责构造 initial argl。
Argument-list construction 的一般形式是:
<compilation of last operand, targeted to val>
(assign argl (op list) (reg val))
<compilation of next operand, targeted to val>
(assign argl (op cons) (reg val) (reg argl))
...
<compilation of first operand, targeted to val>
(assign argl (op cons) (reg val) (reg argl))
除了第一个 operand 外,每个 operand evaluation 周围都必须 preserve argl,否则已经累积的 arguments 会丢失。除了最后一个 operand 外,每个 operand evaluation 周围还必须 preserve env,供之后的 operand evaluations 使用。
由于第一个 operand 的特殊处理,以及 argl 与 env 需要在不同 locations preserve,编译这段 code 稍微 tricky。
construct-arglist 接受 evaluate individual operands 的 code。若完全没有 operands,它只 emit:
(assign argl (const ()))
否则,construct-arglist 生成以最后一个 argument 初始化 argl 的 code,然后 append evaluate 其余 arguments 的 code,并依次把它们 adjoin 到 argl。
为了从最后到最前处理 arguments,必须把 compile-application 提供的 operand code sequences list reverse。
(define (construct-arglist operand-codes)
(let ((operand-codes (reverse operand-codes)))
(if (null? operand-codes)
(make-instruction-sequence '() '(argl)
'((assign argl (const ()))))
(let ((code-to-get-last-arg
(append-instruction-sequences
(car operand-codes)
(make-instruction-sequence '(val) '(argl)
'((assign argl (op list) (reg val)))))))
(if (null? (cdr operand-codes))
code-to-get-last-arg
(preserving '(env)
code-to-get-last-arg
(code-to-get-rest-args
(cdr operand-codes))))))))
(define (code-to-get-rest-args operand-codes)
(let ((code-for-next-arg
(preserving '(argl)
(car operand-codes)
(make-instruction-sequence '(val argl) '(argl)
'((assign argl
(op cons) (reg val) (reg argl)))))))
(if (null? (cdr operand-codes))
code-for-next-arg
(preserving '(env)
code-for-next-arg
(code-to-get-rest-args
(cdr operand-codes))))))
Applying procedures
Evaluate combination elements 后,compiled code 必须把 proc 中的 procedure apply 到 argl 中的 arguments。
这段 code 执行的 dispatch,基本与 §4.1.1 metacircular evaluator 的 apply procedure、或 §5.4.1 explicit-control evaluator 的 apply-dispatch entry point 相同。
它检查待 apply procedure 是 primitive procedure 还是 compiled procedure。对于 primitive procedure,使用 apply-primitive-procedure;compiled procedure 的处理方式稍后说明。
Procedure-application code 的形式如下:
(test (op primitive-procedure?) (reg proc))
(branch (label primitive-branch))
compiled-branch
<code to apply compiled procedure with given target
and appropriate linkage>
primitive-branch
(assign <target>
(op apply-primitive-procedure)
(reg proc)
(reg argl))
<linkage>
after-call
注意,compiled branch 必须跳过 primitive branch。因此,如果原 procedure call 的 linkage 是 next,compound branch 必须使用跳转到 primitive branch 之后 label 的 linkage。
这与 compile-if 中 true branch 使用的 linkage 类似。
(define (compile-procedure-call target linkage)
(let ((primitive-branch (make-label 'primitive-branch))
(compiled-branch (make-label 'compiled-branch))
(after-call (make-label 'after-call)))
(let ((compiled-linkage
(if (eq? linkage 'next) after-call linkage)))
(append-instruction-sequences
(make-instruction-sequence '(proc) '()
`((test (op primitive-procedure?) (reg proc))
(branch (label ,primitive-branch))))
(parallel-instruction-sequences
(append-instruction-sequences
compiled-branch
(compile-proc-appl target compiled-linkage))
(append-instruction-sequences
primitive-branch
(end-with-linkage linkage
(make-instruction-sequence '(proc argl)
(list target)
`((assign ,target
(op apply-primitive-procedure)
(reg proc)
(reg argl)))))))
after-call))))
Primitive 和 compound branches 与 compile-if 的 true/false branches 一样,使用 parallel-instruction-sequences append,而不是普通的 append-instruction-sequences,因为它们不会 sequentially execute。
Applying compiled procedures
处理 procedure application 是 compiler 中最 subtle 的部分,尽管它生成的 instruction sequences 很短。
compile-lambda 构造的 compiled procedure 有一个 entry point,它是一个 label,标识 procedure code 的开始位置。Entry point 处的 code 在 val 中计算 result,并执行 (goto (reg continue)) return。
因此,如果 linkage 是一个 label,给定 target 和 linkage 的 compiled-procedure application(由 compile-proc-appl 生成)可以预期具有如下形式:
(assign continue (label proc-return))
(assign val (op compiled-procedure-entry) (reg proc))
(goto (reg val))
proc-return
(assign <target> (reg val)) ; included if target is not val
(goto (label <linkage>)) ; linkage code
如果 linkage 是 return,则形式为:
(save continue)
(assign continue (label proc-return))
(assign val (op compiled-procedure-entry) (reg proc))
(goto (reg val))
proc-return
(assign <target> (reg val)) ; included if target is not val
(restore continue)
(goto (reg continue)) ; linkage code
这段 code 设置 continue,使 procedure return 到 proc-return label,然后跳转到 procedure entry point。
proc-return 处的 code 会把 procedure result 从 val 转移到 target register(如果需要),然后跳转到 linkage 指定的 location。
Linkage 总是 return 或 label,因为 compile-procedure-call 会把 compound-procedure branch 的 next linkage 替换为 after-call label。
如果 target 不是 val,compiler 确实会生成上面这样的 code。^39
不过,target 通常是 val;compiler 只有在把 operator evaluation target 到 proc 时才指定其他 register。因此,procedure result 会直接放进 target register,不需要 return 到 special location 再复制它。
我们可以简化 code:设置 continue,使 procedure 直接“return”到 caller linkage 指定的位置:
<set up continue for linkage>
(assign val (op compiled-procedure-entry) (reg proc))
(goto (reg val))
如果 linkage 是一个 label,就把 continue 设置为 return 到该 label:
(assign continue (label <linkage>))
(assign val (op compiled-procedure-entry) (reg proc))
(goto (reg val))
Procedure body 结束时的 (goto (reg continue)),此时等价于 proc-return 处原本会执行的 (goto (label <linkage>))。
如果 linkage 是 return,根本不需要 setup continue:它已经保存了所需的 location。
(assign val (op compiled-procedure-entry) (reg proc))
(goto (reg val))
Procedure body 结束时的 (goto (reg continue)) 会直接跳转到原本 proc-return 中 (goto (reg continue)) 要跳转的位置。
Tail-recursive compiled code
通过这种 return linkage implementation,compiler 生成 tail-recursive code。Procedure body 的最后一步调用 procedure 时,会直接 transfer,不在 stack 上保存 information。
如果把 target 为 val、linkage 为 return 的 procedure call 像 non-val target 那样处理,就会破坏 tail recursion。
System 仍然会为任意 expression 给出相同 value,但每次调用 procedure 时都会 save continue,并在 call 完成后 return,以撤销这个无用 save;这些 extra saves 会在 procedure-call nest 中累积。^40
compile-proc-appl
compile-proc-appl 根据 call target 是否为 val,以及 linkage 是否为 return,考虑四种 cases。
注意,instruction sequences 被声明为修改所有 registers,因为执行 procedure body 可能以任意方式改变 registers。^41
另外,target 为 val、linkage 为 return 的 case,被声明为需要 continue:虽然 two-instruction sequence 没有显式使用 continue,但必须确保进入 compiled procedure 时 continue 已有正确 value。
(define (compile-proc-appl target linkage)
(cond ((and (eq? target 'val)
(not (eq? linkage 'return)))
(make-instruction-sequence '(proc) all-regs
`((assign continue (label ,linkage))
(assign val (op compiled-procedure-entry)
(reg proc))
(goto (reg val)))))
((and (not (eq? target 'val))
(not (eq? linkage 'return)))
(let ((proc-return (make-label 'proc-return)))
(make-instruction-sequence '(proc) all-regs
`((assign continue (label ,proc-return))
(assign val (op compiled-procedure-entry)
(reg proc))
(goto (reg val))
,proc-return
(assign ,target (reg val))
(goto (label ,linkage))))))
((and (eq? target 'val)
(eq? linkage 'return))
(make-instruction-sequence '(proc continue) all-regs
'((assign val (op compiled-procedure-entry)
(reg proc))
(goto (reg val)))))
((and (not (eq? target 'val))
(eq? linkage 'return))
(error "return linkage, target not val -- COMPILE"
target))))
脚注
脚注 39
实际上,当 target 不是 val 且 linkage 是 return 时,我们会 signal 一个 error,因为唯一请求 return linkage 的地方是 procedure compilation,而我们的 convention 是 procedures 把 values return 到 val。
脚注 40
让 compiler 生成 tail-recursive code 看起来似乎很 straightforward。但大多数 common languages 的 compilers(包括 C 和 Pascal)并不会这样做,因此这些 languages 不能只用 procedure call 表示 iterative processes。
这些 languages 中 tail recursion 的困难在于:它们的 implementations 使用 stack 保存 procedure arguments、local variables 以及 return addresses。
本书描述的 Scheme implementations 把 arguments 和 variables 存在会被 garbage-collected 的 memory 中。使用 stack 保存 variables 和 arguments 的理由,是它避免了那些本来不需要 garbage collection 的 languages 进行 garbage collection,并且通常被认为更 efficient。
Sophisticated Lisp compilers 实际上可以使用 stack 保存 arguments,同时不破坏 tail recursion(参见 Hanson 1990)。此外,stack allocation 是否真的比 garbage collection 更 efficient,本身也存在争论;细节似乎取决于 computer architecture 的 fine points(参见 Appel 1987 以及 Miller and Rozas 1994 的 opposing views)。
脚注 41
all-regs variable 绑定到所有 registers 的 names list:
(define all-regs '(env proc val argl continue))
小结
Combination compilation 把 operator 放入 proc,以逆序 operand evaluation 构造 argl,再根据 procedure representation dispatch。Compiled procedure application 通过巧妙设置 continue,在不额外保存 stack state 的情况下保持 tail recursion;compile-proc-appl 则用 target/linkage 的四种组合覆盖所有调用情形。
参考
- Abelson, Sussman, and Sussman, Structure and Interpretation of Computer Programs, 2nd ed., §5.5.3 “Compiling Combinations”.
- MIT Press HTML full text
评论 (0)