Essentials of Compilation:前言与致谢
Essentials of Compilation:前言与致谢
本篇翻译原书 Preface 与 Acknowledgments,说明本书的 incremental compiler 教学路线、章节依赖、实现环境与写作背景。
TL;DR
- 本书通过逐章增加 language feature,在每一章构建一个完整 compiler,而不是把 compiler 拆成彼此孤立的 pass 教程。
- compiler 被拆成大量 nanopass;每个 pass 只完成一个任务,因此可以独立测试,也更容易理解语言特性如何推动 compiler design。
- Racket 版使用 Racket 同时实现 compiler 和 input language,目标是 x86 assembly,并遵循 Linux/macOS Intel hardware 上的 System V calling convention。
- 章节路线从 AST、recursion、variables 和 register allocation 开始,逐步加入 conditionals、loops、tuples、functions、closures、dynamic typing、gradual typing 与 generics。
目录
| 章节 | 内容 |
|---|---|
| 1. Preface | 写作目标、incremental approach 与课程使用方式 |
| 2. 章节依赖 | 各章引入的 language feature 与 compiler concept |
| 3. 实现环境 | Racket、x86、calling convention 与 runtime |
| 4. Acknowledgments | 课程、研究与协作者致谢 |
1. Preface
程序员按下 “run” button、软件开始执行的那一刻,带有某种 magic:用 high-level language 写出的 program,竟然能在只会搬运 bits 的 computer 上运行。本书揭示了让这一刻成为可能的 compiler 技术。自 1950 年代 Backus 及其同事的 groundbreaking work 开始,computer scientists 发现了构造 compiler 的方法:自动把 high-level program 翻译成 machine code。
本书带读者完成一次旅程:为一种小而强大的 language 构造自己的 compiler。沿途会解释 compiler 所依赖的 essential concepts、algorithms 和 data structures,并帮助读者理解 program 如何映射到 computer hardware。这种理解有助于分析 hardware/software 交界处的问题,例如 execution time、software errors 与 security vulnerabilities。对希望从事 compiler construction 的读者,本书也希望成为继续学习 just-in-time compilation、program analysis 与 program optimization 的 stepping-stone;对希望设计和实现 programming language 的读者,本书将 language design choice 与其对 compiler 和 generated code 的影响联系起来。
compiler 通常由一系列 stages 组成,把 program 逐步翻译成 hardware 最终执行的 code。本书把这种方式推到极致:将 compiler 拆成大量 nanopass,每个 pass 只执行一个 task。这样既能独立测试每个 pass,也能把注意力集中在一个局部问题上,使 compiler 更容易理解。
常见的 compiler 教材会让每一章负责一个 pass;这种组织方式的问题在于,它会掩盖 language feature 如何促成 compiler design choice。本书采用 incremental approach:每一章都构建一个完整 compiler,从只包含 arithmetic 和 variables 的小型 input language 开始;后续章节再增加 language feature,并在必要时扩展 compiler。
1.1 为什么选择这些 language feature
这些 language feature 被选择出来,是为了引出 compiler 中的 fundamental concepts 和 algorithms:
| 章节主题 | 新增内容 | 引出的核心概念 |
|---|---|---|
| Preliminaries、Integers and Variables | integer arithmetic、local variables | abstract syntax trees、recursive functions |
| Register Allocation | 把 variables 放入 machine registers | graph coloring |
| Booleans and Conditionals | conditional expressions | 将条件表达式翻译为 conditional goto 的递归算法 |
| Loops and Dataflow Analysis | loops、mutable variables | register allocator 中的 dataflow analysis |
| Tuples and Garbage Collection | heap-allocated tuples | garbage collection |
| Functions | first-class functions,但没有 lexical scoping | procedure call stack、calling conventions、tail calls,以及它们和 register allocation / GC 的关系 |
| Lexically Scoped Functions | anonymous functions、lexical scoping、lambda | closure conversion:把 lambda 翻译为 functions 与 tuples 的组合 |
| Dynamic Typing | dynamic typing 与 Any | 用 Any 作为 dynamic language 的编译目标 |
| Gradual Typing | 同一 program 中的 static / dynamic 区域 | proxies,使 values 可以安全跨越两个区域 |
| Parametric Polymorphism | generics 与 autoboxing | 复用 Any 和 casts 实现 polymorphic functions |
本书没有包含很多其他 language feature。取舍依据是:某个 feature 带来的 incidental complexity,是否值得它所暴露的 fundamental concept。例如,本书选择 tuples 而不是 records,因为两者都能引出 heap allocation 和 garbage collection,但 records 会带来更多 incidental complexity。
1.2 课程使用方式
自 2009 年起,本书的 drafts 被用作 University of Colorado 和 Indiana University 面向高年级 undergraduate 与一年级 graduate students 的 16 周 compiler course 教材。学生通常已经掌握 programming、data structures and algorithms,以及 discrete mathematics 的基础。
课程开始时,学生组成 2–4 人小组,每两周完成一章,从 LangVar 开始,到 LangLambda 结束。许多章节包含 challenge problem,通常交给 graduate students。课程最后两周用于 final project:学生自行设计并实现一个 compiler extension;后面的章节可以为这些 project 提供支持。
对于约 10 周的 quarter system 课程,建议完成到 Tuples 或 Functions,并为每个 compiler pass 提供 scaffolding code。若课程要强调 functional language,可以跳过 Loops 并加入 Lambda;若要强调 dynamically typed language,则加入 Dynamic Typing。
2. 章节依赖
Racket edition 的依赖关系如下:
flowchart LR
P[Preliminaries] --> V[Variables]
V --> R[Registers]
R --> C[Conditionals]
C --> T[Tuples]
T -. efficient tail calls .-> F[Functions]
T --> L[Lambda]
F --> L
C --> D[Dynamic Typing]
C --> W[Loops]
L --> G[Gradual Typing]
D --> G
G --> Y[Generics]
Functions 只在实现 efficient tail calls 时依赖 Tuples;Lambda 同时依赖 Tuples 与 Functions;Gradual Typing 依赖 Lambda 和 Dynamic Typing;Generics 依赖 Gradual Typing。
3. 实现环境
Racket edition 使用 Racket 同时实现 compiler 和 input language,因此读者应熟悉 Racket 或 Scheme。配套代码位于 public-student-support-code。
compiler 的 target 是 x86 assembly;读者有 computer systems 课程背景会有帮助,但不是必要条件。本书只介绍 compiler 所需的 x86-64 assembly 部分。
生成的 assembly 遵循 System V calling convention,因此当 runtime system(用 C 编写)在 Linux 或 macOS 的 Intel hardware 上通过 GNU C compiler(gcc)编译时,可以与之协同工作。Windows 上的 gcc 使用 Microsoft x64 calling convention,所以本书生成的 assembly 不能直接与 Windows runtime 协同;一种 workaround 是使用 Linux 作为 guest operating system 的 virtual machine。
4. Acknowledgments
Indiana University 的 compiler construction tradition 可以追溯到 Daniel Friedman 在 1970s 和 1980s 对 programming languages 的研究与课程。Friedman 的学生 Kent Dybvig 实现了 Chez Scheme——一个高效、production-quality 的 Scheme compiler。1990s 和 2000s 期间,Dybvig 讲授 compiler course 并持续开发 Chez Scheme。
compiler course 在保留 real-world compiler 内容的同时,逐渐加入新的 pedagogical ideas。Friedman 的一个想法是把 compiler 拆成许多小 passes;另一个称为 “the game” 的想法,是使用 interpreters 测试每个 pass 生成的 code。
Dybvig 在学生 Dipanwita Sarkar 和 Andrew Keep 的协助下,开发了支持这种教学方式的 infrastructure,并将课程演化为使用更小的 nanopasses。本书许多 compiler design decisions 都受到 Dybvig assignment descriptions 的启发。2000s 中期,Dybvig 的学生 Abdulaziz Ghuloum 注意到课程的 front-to-back 组织方式让学生难以理解 compiler design 的 rationale,于是提出了本书所采用的 incremental approach。
感谢曾在 IU compiler course 担任 teaching assistant 的 Carl Factora、Ryan Scott、Cameron Swords 和 Chris Wailes;感谢 Andre Kuhlenschmidt 对 garbage collector 和 x86 interpreter 的工作,Michael Vollmer 对 efficient tail calls 的工作,以及 Michael Vitousek 对 IU 第一轮 incremental compiler course 的帮助。
感谢 Bor-Yuh Chang、John Clements、Jay McCarthy、Joseph Near、Ryan Newton、Nate Nystrom、Peter Thiemann、Andrew Tolmach 和 Michael Wollowski 教授基于本书 drafts 开设课程并提供反馈。
感谢 Ronald Garcia 帮助 Jeremy 度过 2000s 初期的 Dybvig compiler course,尤其感谢他找到了那个让 garbage collector 开始进行 wild goose chase 的 bug。
— Jeremy G. Siek,Bloomington, Indiana
原文信息
- 原文标题:
Preface、Acknowledgments - 对应版本:Racket-MIT-press edition
- 关联章节:系列索引
参考资料
评论 (0)