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

Download Report

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
Three-Address
波兰式
P-code.
P-代码
Code三地址码
● Three-Address Code
例1 请写出算术表达式 2*a+(b-3) 的三地址码
解:
(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
例2● example
请写出Sample TINY program的三地址码
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
解:Three-address code for the TINY program
read x
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
Three-Address Code
实现三地址码的数据结构
Quadruple四元式
Triple三元式
例1 请写出TINY program三地址码的四元式表示。
read x
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
例1 请写出TINY program三地址码的四元式表示。
read x
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
解:Quadruple implementation for the three-address code
(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, _ )
例2 请写出TINY program三地址码的三元式表示。
read x
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
解:Triple implementation for the three-address code
(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
quadruples 四元式的数据结构
Typedef enum { rd, gt, if_f, asn, lab, mul, sub, eq, wri, halt, …}
OpKind;
Typedef enum { Empty, IntConst, String } AddrKind;
Typedef struct
{ AddrKind kind;
Union
{ int val;
char * name;
} contents;
}Address;
Typedef struct
{ OpKind op;
Address addr1, addr2,addr3;
} Quad
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
Three-Address Code :
<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语句前的代码
Three-Address Code :
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
例1 请写出C function definition. 三地址码。
int f ( int x, int y )
{ return x + y + 1; }
解:This will translate into the following three-address code:
entry f
t1 = x + y
t2 = t1 + 1
return t2
●
example
例2
suppose the function f has been defined in C as in the
previous example. Then, the call
f ( 2+3, 4)
请写出the call三地址码。
解: translates to the three-address code
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 ;
优化技术简介—(b)常数传播
_tmp4 = 0 ;
f0 = _tmp4;
_tmp5 = 1 ;
f1 = _tmp5;
_tmp6 = 2 ;
i = _tmp6 ;
f0 = 0 ;
f1 = 1 ;
i = 2
;
优化技术简介—(c)代数简化
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
优化技术简介—代数简化
例:b = 5 + a + 10 ;
_tmp0 = 5 ;
_tmp1 = _tmp0 + a ;
_tmp2 = _tmp1 + 10 ;
b = _tmp2 ;
_tmp0 = 15 ;
_tmp1 = a + _tmp0 ;
b
= _tmp1 ;
优化技术简介—(d)降低运算强度
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
优化技术简介—(e)复写传播
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. 凡未被纳入某一基本块的语句,都是程序中控制流
程无法到达的语句,因而也是不会被执行到的语句,我
们可以把它们删除。
例:划分基本块
解:划分成四个基本块B1,B2,
B3,B4
(1)
B1
(2)
(3)
(1)
read (C)
(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表示及其应用
有向无环图(DAG----Directed Acyclic Graph)
基本块的DAG是在结点上带有标记的DAG
叶结点:独特的标识符(名字,常数)标记
内部结点:运算符号标记
各个结点:附加标识符标记
●
用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
●
仅含0,1,2型四元式的基本块的DAG构造算法:
首先,DAG为空。
对基本块的每一四元式,依次执行:
1.如果NODE(B)无定义,则构造一标记为B的叶结点并定
义NODE(B)为这个结点;
如果当前四元式是0型,则记NODE(B)的值为n,转4。
如果当前四元式是1型,则转2(1)。
如果当前四元式是2型,则:
(I) 如果NODE(1)无定义,则构造一标记为C的叶结
点并定义NODE(1) 为这个结点;
(II) 转2 (2)
2.
(1)如果NODE(B)是标记为常数的叶结点 ,则转2(3),否
则转3(1)。
(2)如果NODE(B)和NODE(C)都是标记为常数的叶结点,
则转2(4),否则转3(2)。
(3)执行op B(即合并已知量),令得到的新常数为P。如
果NODE(B)是处理当前四元式时新构造出来的结点,则删除
它。如果NODE(P)无定义,则构造一用P做标记的叶结点n。
置NODE(P)=n,转4。
(4)执行B op C(即合并已知量),令得到的新常数为P。
如果NODE(B)或NODE(C)是处理当前四元式时新构造出来的
结点,则删除它。如果NODE(P)无定义,则构造一用P做标
记的叶结点n。置NODE(P)=n,转4。
3.
(1)检查DAG中是否已有一结点,其唯一后继为
NODE(B),且标记为op(即找公共子表达式)。如果没
有,则构造该结点n,否则就把已有的结点作为它的结点
并设该结点为n,转4。
(2)检查中DAG中是否已有一结点,其左后继为
NODE(B),其右后继为NODE(C),且标记为op(即找公
共子表达式)。如果没有,则构造该结点n,否则就把已
有的结点作为它的结点并设该结点为n,转4。
4.
如果NODE(A)无定义,则把A附加在结点n上并令
NODE(A)=n;否则先把A从NODE(A)结点上附加标识符集
中删除(注意,如果NODE(A)是叶结点,则其标记A不删
除),把A附加到新结点n上并令NODE(A)=n。转处理下
一四元式。
而后,我们可由DAG重新生成原基本块的一个优化的代码
序列。
例:用DAG进行基本
块的优化
(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
例:
(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
n
1
3.14
(a)
To
例:
(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
T0
3.14
n2
6.28
(b)
T1
例:
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
(10)
T0:=3.14
T1:=2*T0
T2:=R+r
A:=T1*T2
B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)
(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代
码生成器的简单优化