Semantic analysis and code generation
導入済み
5:58
計算理論とコンパイラ
This course asks what machines can decide, and how text becomes meaning. The first three units are theory: automata and regular languages, context-free…
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| symbol table/ˈsɪmbl ˈteɪbl/ | 記号表 |
| scope/skəʊp/ | The region of a program in which a particular name or binding can be accessed. |
| intermediate representation/ˌɪntəˈmiːdɪət ˌreprɪˈzenteɪʃn/ | 中間表現 |
| three-address code/θriː əˈdres kəʊd/ | intermediate code in which each instruction has at most one operator and three addresses |
| lexer/ˈleksə/ | the compiler stage that groups characters into tokens |
| Constant folding/ˈkɒnstənt ˈfəʊldɪŋ/ | working out constant expressions at compile time |
| constant propagation/ˈkɒnstənt ˌprɒpəˈɡeɪʃn/ | replacing a variable with its known constant value |
| dead-code elimination/ded kəʊd ɪˌlɪmɪˈneɪʃn/ | removing code whose results are never used |
| common subexpressions/ˈkɒmən ˌsʌbɪkˈspreʃnz/ | identical expressions computed more than once, which the compiler can compute once |
| Register allocation/ˈredʒɪstə ˌæləˈkeɪʃn/ | choosing which values live in the processor's limited registers |
| spilled/spɪld/ | (of a value) kept in memory because no register is free for it |
| live/laɪv/ | of a variable at a program point: its current value may still be read later |
| Loop-invariant code motion/luːp ɪnˈveərɪənt kəʊd ˈməʊʃn/ | moving a calculation that gives the same result every time out of a loop |
Add meaning to the tree
- The symbol table 符号表 maps each identifier to its type and scope 作用域. Type rules walk the tree from the bottom up: $\mathbf{id} + \mathbf{id}$ needs both sides to be numbers.
- The output is an intermediate representation 中间表示 (IR): three-address code 三地址码, one operation per line.

The symbol table binds —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Name resolution before type checking: scoping first.
The course program, exactly
count = a1 + b2 * 3witha1, b2: int: the table holds three int bindings, and the checker checks $b_2 \times 3$ and then $a_1 + (\ldots)$. The IR ist1 = b2 * 3; t2 = a1 + t1; count = t2: three instructions and two temporaries.- Change
b2tostrand the checker rejectsb2 * 3, reporting the position the lexer 词法分析器 recorded. Positions are what every error message is built on.
count = a1 + b2 * 3 emits IR —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Three-address: one operation per line, two temps here.
Why an IR
- Direct translation multiplies the work: $m$ languages $\times$ $n$ targets = $mn$ compilers. An IR makes it $m + n$. Optimisations work on the IR.
- Constant folding 常量折叠 uses literals only ($2 \times 3 \to 6$; $b_2 \times 3$ stays), and each optimisation must keep the program's meaning.
- Check names before types (scopes first), finish all the checks before emitting code, and never fold an operand that is not a literal.
Constant folding applies to —
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
b2 * 3 stays: b2 is not a literal - folding is literal-only.
Optimise, then allocate registers
- Simple IR optimisations: constant propagation 常量传播 and folding turn
a = 3; b = a + 4; c = b * 2intoc = 14; dead-code elimination 死代码消除 removesaandbif nothing else reads them; removing common subexpressions 公共子表达式 computesx * yonly once. - Register allocation 寄存器分配 is graph colouring: variables that are alive at the same time interfere and need different registers. Three variables that are all alive together form a triangle and need $3$ registers; if there are fewer, one is spilled 溢出 to memory.
After constant propagation and folding, what value does d hold in a = 5; b = a * 2; d = b - 3?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
b = 10, so d = 10 - 3 = 7.
Four variables are all alive at the same time. What is the minimum number of registers to hold them without spilling?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
They form a 4-clique in the interference graph, which needs 4 colours.
Put the compiler phases in order.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Each phase consumes the one before: tokens, then a tree, then a checked tree, then IR. Optimising on the IR lets every source language and target share the same passes.
Liveness decides the registers
- A variable is live 活跃 at a point if its value may still be read later. In
a = 1; b = a + 2; c = b * a; return c, after line 2 both $a$ and $b$ are live; after line 3 only $c$ is. - At most two values are live at once, so two registers are enough: $c$ can reuse $b$'s register. Liveness is computed backwards from the uses.
a = 1; b = a + 2; c = b * a; return c: the most values live at one point?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
After line 2, $a$ かつ $b$; after line 3, only $c$.
Worked example: moving work out of a loop
for i in 0..999: x = y * z; a[i] = x + icomputesy * za thousand times, although neither $y$ nor $z$ changes in the loop.- Loop-invariant code motion 循环不变量外提 computes it once before the loop: $\mathbf{999}$ multiplications saved.
x + idepends on $i$, so it must stay inside.
A loop of 1000 iterations computes y * z each time, with $y$ かつ $z$ unchanged. Multiplications saved by hoisting it?
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
One before the loop instead of 1000 inside.
In that loop, x + i can also be hoisted out.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It depends on $i$, which changes every iteration.
Marks that slip away
- Types before names. Resolve scopes 作用域 first; a type check on an undeclared name has nothing to check.
- Folding non-literals.
2 * 3folds to6;b2 * 3cannot fold, becauseb2is known only at run time. - Assuming every temporary gets a register. When live values outnumber registers, the allocator spills some to memory.
Recap
- The symbol table 符号表 maps names to type and scope; check names before types.
- Three-address IR:
t1 = b2 * 3; t2 = a1 + t1; count = t2. - An IR turns $m \times n$ compilers into $m + n$; fold only literal operands. For 3 languages and 4 targets:
| Design | You write |
|---|---|
| Direct | $3 \times 4 = 12$ compilers |
| Through an IR | $3 + 4 = 7$ parts |
何が問題でしたか?この内容はサイトの運営チームだけが確認します。