#### Transcript Chapter Code Generation代码生成6 6.1 Intermediate Code and

Chapter
6
Code Generation代码生成
6.1 Intermediate Code and Data Structures for Code Generation

6.2 Basic Code Generation Techniques 基本的代码生成技术
6.3 Code Generation of Data Structure References 数据结构应用的代

6.4 Code Generation of Control Statements and Logical
Expressions 控制语句和逻辑表达式的代码生成
6.5 Code Generation of Procedure and Function Calls 过程和函数

6.6 Code Generation in Commercial Compilers: Two Case Studies 商用编译器

6.7 TM: A Simple Target Machine TM：简单的目标机器
6.8 Code Generation for the Tiny Language TINY语言的代码生成器
6.9 A Survey of Code Optimizations Techniques 代码优化技术考察
6.10 Simple Optimizations for the TINY Code Generator TINY代

6.1 Intermediate Code and Data Structures for Code
Generation 中间代码和用于代码生成的数据结构
● intermediate
representation ( IR)
A data structure that represents the source program during
translation is called an intermediate representation, or IR,
for short.

●
two popular forms of intermediate code
波兰式
P-code.
P-代码
Code三地址码

(1) 2*a+(b-3) with syntax tree 2*a+(b-3) 的语法树
+
*
2
a
b
3
(2) The corresponding three-address code is 三地址码
T1 = 2 * a
T2 = b – 3
T3 = t1 + t2

read x ; { input an integer }
if 0 < x then { don’t compute if x <= 0 }
fact:=1;
repeat
fact:=fact*x;
x:=x-1
until x=0;
write fact { output factorial of x }
ends

t3=x-1
t1=x>0
x=t3
if_false t1 goto L1
t4= x= =0
fact=1
if_false t4 goto L2
label L2
write fact
t2=fact*x
label L1
fact=t2
halt
● Data Structures for the Implementation of

Triple三元式

t3=x-1
t1=x>0
x=t3
if_false t1 goto L1
t4= x= =0
fact=1
if_false t4 goto L2
label L2
write fact
t2=fact*x
label L1
fact=t2
halt

t3=x-1
t1=x>0
● example x=t3
if_false t1 goto L1
t4= x= =0
fact=1
if_false t4 goto L2
label L2
write fact
t2=fact*x
label L1
fact=t2
halt

(sub, x, 1, t3 )
(rd, x , _ , _ )
(asn, t3, x, _ )
(gt, x, 0, t1 )
(eq, x, 0, t4 )
(if_f, t1, L1, _ )
(if_f, t4, L2, _)
(asn, 1,fact, _ )
(wri, fact, _, _ )
(lab, L2, _ , _ )
(lab, L1, _ , _ )
(mul, fact, x, t2 )
halt, _, _, _ )
(asn, t2, fact, _ )

t3=x-1
t1=x>0
● example x=t3
if_false t1 goto L1
t4= x= =0
fact=1
if_false t4 goto L2
label L2
write fact
t2=fact*x
label L1
fact=t2
halt

(0)
(1)
(2)
(3)
(4)
(5)
(rd, x , _ )
(gt, x, 0, )
(if_f, (1), (11) )
(asn, 1,fact )
(mul, fact, x)
(asn, (4), fact )
(6)
(sub, x, 1 )
(7)
(asn, (6), x, )
(8)
(eq, x, 0 )
(9)
(if_f, (8), (4))
(10)
(wri, fact, _ )
(11) (halt, _, _)
● C code defining possible data structures for the
Typedef enum { rd, gt, if_f, asn, lab, mul, sub, eq, wri, halt, …}
OpKind;
Typedef enum { Empty, IntConst, String } AddrKind;
Typedef struct
Union
{ int val;
char * name;
} contents;
Typedef struct
{ OpKind op;
6.2 Basic Code Generation Techniques

●简单赋值语句的(四元式)中间代码生成

t :=arg1 op arg2
id.name, E.place
lookup(id.name) ;
emit(t := arg1 op arg2);
t: newtemp;

(1) S id := E
{ P:=lookup (id.name) ;
if Pnil then emit( P“:=”E.place)
else error }
(2) EE1+E2
{ E.place:= newtemp;
emit(E.place“:=” E1.place“+”E2.place)}
(3) E - E1
{ E.place:=newtemp;
emit(E.place“:=”“uminus” E1.place)}
(4) E( E1)
{ E.place:= E1.place}
(5) Eid
{ E.place:=newtemp;
P:=lookup(id.name);
if Pnil then E.place:=P
else error}
● Practical Code Generation
Procedure gencode (T: treenode);
Begin
If T is not nil then
Generate code to prepare for code of left child of T;
Gencode(left child of T);
Generate code to prepare for code of right child of T;
Gencode(right child of T);
Generate code to implement the action of T;
End;
6.3 Code Generation of Data Structure
References 数据结构应用的代码生成
6.4 Code Generation of Control Statements and
Logical Expressions 控制语句和逻辑表达式的代码生成
● Code Generation for If – and While – Statements
if-stmt → if ( exp ) stmt | if ( exp ) stmt else stmt
while-stmt → while ( exp ) stmt
● If Statements
if ( E ) S1 else S2
<code to evaluate E to t1>
if_false t1 goto L1
<code for S1>
goto L2
label L1
<code for S2>
label L2
If语句前的代码
If测试的代码

True情况下的代码

False情况下的代码
If语句后的代码
● While Statements
while ( E ) S
while语句前的代码
while测试的代码
label L1
<code to evaluate E to t1>
if_false t1 goto L2
<code for S>
goto L1
label L2

While体的代码

If语句后的代码
6.5 Code Generation of Procedure and Function Calls

● Intermediate Code for Procedures and Functions
function/procedure definition
Entry instruction
<code for the function body>
Return instruction
function/procedure call
Begin-argument-computation instruction
<code to compute the arguments >
Call instruction
●
example

int f ( int x, int y )
{ return x + y + 1; }

entry f
t1 = x + y
t2 = t1 + 1
return t2
●
example

suppose the function f has been defined in C as in the
previous example. Then, the call
f ( 2+3, 4)

begin_args
t1 = 2 + 3
arg t1
arg 4
call f
6.6 Code Generation in Commercial Compilers: Two Case
Studies 商用编译器中的代码生成：两个案例研究
6.7 TM: A Simple Target Machine TM：

6.8 Code Generation for the Tiny
Language TINY语言的代码生成器
6.9 A Survey of Code Optimizations
Techniques 代码优化技术考察
●
Principal Sources of Code Optimizations
1) Register Allocation
Good use of registers is the most important feature of
efficient code.
2) Unnecessary Operations
The second major source of code improvement is to avoid
generating code for operations that are redundant or
unnecessary.
3) Costly Operations

4) Prediction Program Behavior
● 优化技术简介—(a)常数合并
a = 10 * 5 + 6 - b;
_tmp0 = 10 ;
_tmp1 = 5 ;
_tmp2 = _tmp0 * _tmp1 ;
_tmp3 = 6 ;
_tmp4 = _tmp2 + _tmp3 ;
_tmp5 = _tmp4 – b;
a = _tmp5 ;
_tmp0 = 56 ;
_tmp1 = _tmp0 – b ;
a = _tmp1 ;

_tmp4 = 0 ;
f0 = _tmp4;
_tmp5 = 1 ;
f1 = _tmp5;
_tmp6 = 2 ;
i = _tmp6 ;
f0 = 0 ;
f1 = 1 ;
i = 2
;

x+0
0+x
x*1
1*x
0/x
x-0
=
=
=
=
=
=
x
x
x
x
0
x
b && true = b
b && false = false
b || true = true
b || false = b

_tmp0 = 5 ;
_tmp1 = _tmp0 + a ;
_tmp2 = _tmp1 + 10 ;
b = _tmp2 ;
_tmp0 = 15 ;
_tmp1 = a + _tmp0 ;
b
= _tmp1 ;

a) i*2 = 2*i = i+i = i<<2
b) i/2 = (int)(i*0.5)
c) 0-1 = -1
d) f*2 = 2.0 * f = f + f
e) f/2.0 = f*0.5

tmp2 = tmp1 ;
tmp3 = tmp2 * tmp1;
tmp4 = tmp3 ;
tmp5 = tmp3 * tmp2 ;
c = tmp5 + tmp4 ;
tmp3 = tmp1 * tmp1 ;
tmp5 = tmp3 * tmp1 ;
c = tmp5 + tmp3 ;
● 基本块内的优化

● 划分基本块的算法：
1．求出四元式程序之中各个基本块的入口语句。
2．对每一入口语句，构造其所属的基本块。它是由该

3. 凡未被纳入某一基本块的语句，都是程序中控制流

B3，B4
(1)
B1
(2)
(3)
(1)
(2)
A:= 0
(3)
B:= 1
B2
(4) L1: A:=A + B
(5)
if B>= C goto L2
B3
(6)
B:=B+1
(7)
goto L1
(8) L2: write (A)
B4
(9)
halt

(4)
(5)
(6)
(7)
(8)
(9)
● 基本块的DAG表示及其应用

●

0 型：A:=B(:=, B,
DAG结点
n1 A
n1
— ,A)
B
1 型: A:=op B(op, B, — ,A)
2 型: A:=B op C(op, B, C ,A)
n2
A
op
n1
B
n3 A
op
n2
n1
n1
n2
B
C
●

1．如果NODE（B）无定义，则构造一标记为B的叶结点并定

(I) 如果NODE(1)无定义，则构造一标记为C的叶结

(II) 转2 (2)
2．
（1）如果NODE(B)是标记为常数的叶结点 ，则转2(3)，否

（2）如果NODE(B)和NODE(C)都是标记为常数的叶结点，

（3）执行op B（即合并已知量），令得到的新常数为P。如

（4）执行B op C（即合并已知量），令得到的新常数为P。

3．
（1）检查DAG中是否已有一结点，其唯一后继为
NODE(B)，且标记为op（即找公共子表达式）。如果没

（2）检查中DAG中是否已有一结点，其左后继为
NODE(B)，其右后继为NODE(C)，且标记为op（即找公

4.

NODE(A)=n；否则先把A从N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=R+r
T5:=T3*T4
T6:=R-r
B:=T5*T6
n6
A,B
*
n5
T2
+
n1
3.14
T0
T1,T3 n3
6.28
R
(f)
n2
n4
r

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
T0:=3.14
T1:=2*T0
T2:=R+r
A:=T1*T2
B:=A
T3:=2*T0
T4:=R+r
T5:=T3*T4
T6:=R-r
B:=T5*T6
n6
A,B
*
n5
T2,T4
+
n1
3.14
T0
T1,T3 n3
6.28
R
(g)
n2
n4
r

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
T0:=3.14
T1:=2*T0
T2:=R+r
A:=T1*T2
B:=A
T3:=2*T0
T4:=R+r
T5:=T3*T4
T6:=R-r
B:=T5*T6
n6
A,B,T5
*
n5
T2,T4
+
n1
3.14
T0
T1,T3 n3
6.28
R
(h)
n2
n4
r

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
T0:=3.14
T1:=2*T0
T2:=R+r
A:=T1*T2
B:=A
T3:=2*T0
T4:=R+r
T5:=T3*T4
T6:=R-r
B:=T5*T6
n1
3.14
A,B,T5
n6
*
T2,T4
T0
T1,T3 n3
6.28
R
T6
n5
n7
+
n4
n2
r

(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
T0:=3.14
T1:=2*T0
T2:=R+r
A:=T1*T2
B:=A
T3:=2*T0
T4:=R+r
T5:=T3*T4
T6:=R-r
B:=T5*T6
n1
3.14
T0
B
n8
*
n6
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
T0:=3.14
T1:=6.28
T3:=6.28
T2:=R+r
T4:=T2
A:=6.28*T2
T5:=A
T6:=R-r
B:=A*T6
A,T5
*
T1,T3 n3
6.28
R
(j)
T2,T4
T6
n5
n7
+
n4
n2
r
6.10 Simple Optimizations for the TINY Code
Generator TINY代码生成器的简单优化
Chapter
6
Code Generation代码生成
6.1 Intermediate Code and Data Structures for Code Generation

6.2 Basic Code Generation Techniques 基本的代码生成技术
6.3 Code Generation of Data Structure References 数据结构应用的代

6.4 Code Generation of Control Statements and Logical
Expressions 控制语句和逻辑表达式的代码生成
6.5 Code Generation of Procedure and Function Calls 过程和函数

6.6 Code Generation in Commercial Compilers: Two Case Studies 商用编译器

6.7 TM: A Simple Target Machine TM：简单的目标机器
6.8 Code Generation for the Tiny Language TINY语言的代码生成器
6.9 A Survey of Code Optimizations Techniques 代码优化技术考察
6.10 Simple Optimizations for the TINY Code Generator TINY代