8 Chapter Code Generation 8 1 Intermediate Code

  • Slides: 50
Download presentation
8 Chapter Code Generation代码生成 8. 1 Intermediate Code and Data Structures for Code Generation

8 Chapter Code Generation代码生成 8. 1 Intermediate Code and Data Structures for Code Generation 中间代码和用于代码生成的数据结构 8. 2 Basic Code Generation Techniques 基本的代码生成技术 8. 3 Code Generation of Data Structure References 数据结构应用的代 码生成 8. 4 Code Generation of Control Statements and Logical Expressions 控制语句和逻辑表达式的代码生成 8. 5 Code Generation of Procedure and Function Calls 过程和函数 调用的代码生成 8. 6 Code Generation in Commercial Compilers: Two Case Studies 商用编译器 中的代码生成:两个案例研究 8. 7 TM: A Simple Target Machine TM:简单的目标机器 8. 8 Code Generation for the Tiny Language TINY语言的代码生成器 8. 9 A Survey of Code Optimizations Techniques 代码优化技术考察 8. 10 Simple Optimizations for the TINY Code Generator TINY代 码生成器的简单优化

8. 1 Intermediate Code and Data Structures for Code Generation 中间代码和用于代码生成的数据结构 ● intermediate representation

8. 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 v. Three-Address Code三地址码 v波兰式 v. P-code. P-代码

● Three-Address Code 例1 请写出算术表达式 2*a+(b-3) 的三地址码 解: (1) 2*a+(b-3) with syntax tree 2*a+(b-3)

● 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 三地址码 T 1 = 2 * a T 2 = b – 3 T 3 = t 1 + t 2

例2 请写出Sample TINY program的三地址码 ● example read x ; { input an integer }

例2 请写出Sample TINY program的三地址码 ● example 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 t 3=x-1 t 1=x>0 x=t 3 if_false t 1 goto L 1 t 4= x= =0 fact=1 if_false t 4 goto L 2 label L 2 write fact t 2=fact*x label L 1 fact=t 2 halt

● Data Structures for the Implementation of Three-Address Code 实现三地址码的数据结构 Quadruple四元式 Triple三元式 例1 请写出TINY

● Data Structures for the Implementation of Three-Address Code 实现三地址码的数据结构 Quadruple四元式 Triple三元式 例1 请写出TINY program三地址码的四元式表示。 read x t 3=x-1 t 1=x>0 x=t 3 if_false t 1 goto L 1 t 4= x= =0 fact=1 if_false t 4 goto L 2 label L 2 write fact t 2=fact*x label L 1 fact=t 2 halt

 例1 请写出TINY program三地址码的四元式表示。 read x t 3=x-1 t 1=x>0 ● example x=t 3

例1 请写出TINY program三地址码的四元式表示。 read x t 3=x-1 t 1=x>0 ● example x=t 3 if_false t 1 goto L 1 t 4= x= =0 fact=1 if_false t 4 goto L 2 label L 2 write fact t 2=fact*x label L 1 fact=t 2 halt 解:Quadruple implementation for the three-address code (sub, x, 1, t 3 ) (rd, x , _ ) (asn, t 3, x, _ ) (gt, x, 0, t 1 ) (eq, x, 0, t 4 ) (if_f, t 1, L 1, _ ) (if_f, t 4, L 2, _) (asn, 1, fact, _ ) (wri, fact, _, _ ) (lab, L 2, _ ) (lab, L 1, _ ) (mul, fact, x, t 2 ) halt, _, _, _ ) (asn, t 2, fact, _ )

 例2 请写出TINY program三地址码的三元式表示。 read x t 3=x-1 t 1=x>0 ● example x=t 3

例2 请写出TINY program三地址码的三元式表示。 read x t 3=x-1 t 1=x>0 ● example x=t 3 if_false t 1 goto L 1 t 4= x= =0 fact=1 if_false t 4 goto L 2 label L 2 write fact t 2=fact*x label L 1 fact=t 2 halt 解:Triple implementation for the three-address code (0) (rd, x , _ ) (1) (gt, x, 0, ) (2) (if_f, (1), (11) ) (3) (asn, 1, fact ) (4) (mul, fact, x) (5) (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 {

● C code defining possible data structures for the quadruples 四元式的数据结构 Typedef enum { rd, gt, if_f, asn, lab, mul, sub, eq, wri, halt, …} Op. Kind; Typedef enum { Empty, Int. Const, String } Addr. Kind; Typedef struct { Addr. Kind kind; Union { int val; char * name; } contents; }Address; Typedef struct { Op. Kind op; Address addr 1, addr 2, addr 3; } Quad

8. 2 Basic Code Generation Techniques 基本的代码生成技术 ●简单赋值语句的(四元式)中间代码生成 四元式形式 : t : =arg 1

8. 2 Basic Code Generation Techniques 基本的代码生成技术 ●简单赋值语句的(四元式)中间代码生成 四元式形式 : t : =arg 1 op arg 2 语义属性: id. name, E. place 函数: lookup(id. name) ; 过程: emit(t : = arg 1 op arg 2); t: newtemp; 产生式和语义描述: (1) S id : = E { P: =lookup (id. name) ; if P nil then emit( P“: =”E. place) else error }

(2) E E 1+E 2 { E. place: = newtemp; emit(E. place“: =” E

(2) E E 1+E 2 { E. place: = newtemp; emit(E. place“: =” E 1. place“+”E 2. place)} (3) E - E 1 { E. place: =newtemp; emit(E. place“: =”“uminus” E 1. place)} (4) E ( E 1) { E. place: = E 1. place} (5) E id { E. place: =newtemp; P: =lookup(id. name); if P nil then E. place: =P else error}

● Practical Code Generation Procedure gencode (T: treenode); Begin If T is not nil

● 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;

8. 3 Code Generation of Data Structure References 数据结构应用的代码生成

8. 3 Code Generation of Data Structure References 数据结构应用的代码生成

8. 4 Code Generation of Control Statements and Logical Expressions 控制语句和逻辑表达式的代码生成 ● Code Generation

8. 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 ) S 1 else S 2 If语句前的代码 Three-Address

● If Statements if ( E ) S 1 else S 2 If语句前的代码 Three-Address Code : If测试的代码 <code to evaluate E to t 1> if_false t 1 goto L 1 <code for S 1> goto L 2 label L 1 <code for S 2> label L 2 条件 True情况下的代码 无条件转移 False情况下的代码 If语句后的代码

● While Statements while ( E ) S while语句前的代码 Three-Address Code : while测试的代码 label

● While Statements while ( E ) S while语句前的代码 Three-Address Code : while测试的代码 label L 1 <code to evaluate E to t 1> if_false t 1 goto L 2 <code for S> goto L 1 label L 2 条件转移 While体的代码 无条件转移 If语句后的代码

8. 5 Code Generation of Procedure and Function Calls 过程和函数调用的代码生成 ● Intermediate Code for

8. 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

● 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 t 1 = x + y t 2 = t 1 + 1 return t 2

● example 例2 suppose the function f has been defined in C as in

● 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 t 1 = 2 + 3 arg t 1 arg 4 call f

8. 6 Code Generation in Commercial Compilers: Two Case Studies 商用编译器中的代码生成:两个案例研究

8. 6 Code Generation in Commercial Compilers: Two Case Studies 商用编译器中的代码生成:两个案例研究

8. 7 TM: A Simple Target Machine TM: 简单的目标机器

8. 7 TM: A Simple Target Machine TM: 简单的目标机器

8. 8 Code Generation for the Tiny Language TINY语言的代码生成器

8. 8 Code Generation for the Tiny Language TINY语言的代码生成器

8. 9 A Survey of Code Optimizations Techniques 代码优化技术考察 ● Principal Sources of Code

8. 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.

● 优化技术简介—(a)常数合并 a = 10 * 5 + 6 - b; _tmp 0 =

● 优化技术简介—(a)常数合并 a = 10 * 5 + 6 - b; _tmp 0 = 10 ; _tmp 1 = 5 ; _tmp 2 = _tmp 0 * _tmp 1 ; _tmp 3 = 6 ; _tmp 4 = _tmp 2 + _tmp 3 ; _tmp 5 = _tmp 4 – b; a = _tmp 5 ; _tmp 0 = 56 ; _tmp 1 = _tmp 0 – b ; a = _tmp 1 ;

优化技术简介—(c)代数简化 x+0 = x 0+x = x x*1 = x 1*x = x 0/x

优化技术简介—(c)代数简化 x+0 = x 0+x = x x*1 = x 1*x = x 0/x = 0 x-0 = x b && true = b b && false = false b || true = true b || false = b

优化技术简介—(e)复写传播 tmp 2 = tmp 1 ; tmp 3 = tmp 2 * tmp

优化技术简介—(e)复写传播 tmp 2 = tmp 1 ; tmp 3 = tmp 2 * tmp 1; tmp 4 = tmp 3 ; tmp 5 = tmp 3 * tmp 2 ; c = tmp 5 + tmp 4 ; tmp 3 = tmp 1 * tmp 1 ; tmp 5 = tmp 3 * tmp 1 ; c = tmp 5 + tmp 3 ;

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 (7) T 4: =R+r (8) T 5: =T 3*T 4 (9) T 6: =R-r (10) B: =T 5*T 6 To n 1 3. 14 (a)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 (7) T 4: =R+r (8) T 5: =T 3*T 4 (9) T 6: =R-r (10) B: =T 5*T 6 n 1 n 2 T 0 T 1 3. 14 6. 28 (b)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r n 5 T 2 (4) A: =T 1*T 2 + (5) B: =A (6) T 3: =2*T 0 n 3 n 4 n 1 n 2 T 0 T 1 (7) T 4: =R+r 3. 14 6. 28 R r (8) T 5: =T 3*T 4 (c) (9) T 6: =R-r (10) B: =T 5*T 6

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 A n 6 (7) T 4: =R+r * (8) T 5: =T 3*T 4 (9) T 6: =R-r n 5 T 2 (10) B: =T 5*T 6 + n 3 n 4 n 1 n 2 T 0 T 1 3. 14 6. 28 R r (d)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 A, B n 6 (7) T 4: =R+r * (8) T 5: =T 3*T 4 (9) T 6: =R-r n 5 T 2 (10) B: =T 5*T 6 + n 4 n n 1 n 2 T 0 T 1 3 3. 14 6. 28 R r (e)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 (7) T 4: =R+r (8) T 5: =T 3*T 4 A, B n 6 (9) T 6: =R-r * (10) B: =T 5*T 6 n 5 T 2 + n 4 n 1 n 2 n 3 T 0 T 1, T 3 3. 14 6. 28 R r (f)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 (7) T 4: =R+r A, B n 6 (8) T 5: =T 3*T 4 * (9) T 6: =R-r (10) B: =T 5*T 6 n 5 T 2 , T 4 + n 4 n 1 n 2 n 3 T 0 T 1, T 3 3. 14 6. 28 R r (g)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 (7) T 4: =R+r A, B, T 5 n 6 (8) T 5: =T 3*T 4 (9) T 6: =R-r * (10) B: =T 5*T 6 n 5 T 2, T 4 + n 4 n 1 n 2 n 3 T 0 T 1, T 3 3. 14 6. 28 R r (h)

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2:

例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r (4) A: =T 1*T 2 (5) B: =A (6) T 3: =2*T 0 (7) T 4: =R+r A, B, T 5 (8) T 5: =T 3*T 4 n 6 (9) T 6: =R-r * (10) B: =T 5*T 6 T 2, T 4 T 6 n 5 n 7 + - n 4 n 1 n 2 n 3 T 0 T 1, T 3 3. 14 6. 28 R r

(1) T 0: =3. 14 (2) T 1: =6. 28 (3) T 3: =6.

(1) T 0: =3. 14 (2) T 1: =6. 28 (3) T 3: =6. 28 (4) T 2: =R+r (5) T 4: =T 2 (6) A: =6. 28*T 2 (7) T 5: =A (8) T 6: =R-r (9) B: =A*T 6 例: (1) T 0: =3. 14 (2) T 1: =2*T 0 (3) T 2: =R+r B (4) A: =T 1*T 2 n 8 (5) B: =A (6) T 3: =2*T 0 * (7) T 4: =R+r (8) T 5: =T 3*T 4 (9) T 6: =R-r A, T 5 n 6 (10) B: =T 5*T 6 * T 2, T 4 T 6 n 5 n 7 + n 4 n 1 n 2 T 0 T 1, T 3 n 3 3. 14 6. 28 R r (j)

8. 10 Simple Optimizations for the TINY Code Generator TINY代码生成器的简单优化

8. 10 Simple Optimizations for the TINY Code Generator TINY代码生成器的简单优化

8 Chapter Code Generation代码生成 8. 1 Intermediate Code and Data Structures for Code Generation

8 Chapter Code Generation代码生成 8. 1 Intermediate Code and Data Structures for Code Generation 中间代码和用于代码生成的数据结构 8. 2 Basic Code Generation Techniques 基本的代码生成技术 8. 3 Code Generation of Data Structure References 数据结构应用的代 码生成 8. 4 Code Generation of Control Statements and Logical Expressions 控制语句和逻辑表达式的代码生成 8. 5 Code Generation of Procedure and Function Calls 过程和函数 调用的代码生成 8. 6 Code Generation in Commercial Compilers: Two Case Studies 商用编译器 中的代码生成:两个案例研究 8. 7 TM: A Simple Target Machine TM:简单的目标机器 8. 8 Code Generation for the Tiny Language TINY语言的代码生成器 8. 9 A Survey of Code Optimizations Techniques 代码优化技术考察 8. 10 Simple Optimizations for the TINY Code Generator TINY代 码生成器的简单优化