Download presentation
Presentation is loading. Please wait.
Published by인혜 피 Modified over 5 years ago
1
8 Code Generation Topics A simple code generator algorithm
Basic problems in storage management, instruction selection, register allocation, and the evaluation order Front-end Code optimization Source code Code generator IR Target program
2
8.1 Problems in Code Generator Design
8.1.1 Target Program Executable target modules Relocatable machine-language program (object module) Allow modules to be compiled separately Invoke pre-compiled modules Assembly program Generate symbolic instructions and use the macro facilities of the assembler to help generate code Improve readability
3
8.1 Problems in Code Generator Design
8.1.2 Instruction selection The nature of the instruction set of the target machine has a strong effect on the difficulty of instruction selection, e.g. the uniformity and completeness of the instruction set Instruction speeds and machine idioms are other important factors
4
8.1 Problems in Code Generator Design
If we do not care about the efficiency of the target program, instruction selection is straightforward. Three address code x := y + z (x, y, and z are statically allocated) MOV y, R0 /* load y into register R0 */ ADD z, R0 /* add z to R0 */ MOV R0, x /* store R0 to x */ This strategy often produces redundant loads and stores
5
8.1 Problems in Code Generator Design
sequence of three-address statement a := b + c d := a + e Is translated to MOV b, R0 ADD c, R0 MOV R0, a MOV a, R0 ADD e, R0 MOV R0, d b b+c Redundant instruction. If a isn’t used anymore, instruction 3 is redundant as well
6
8.1 Problems in Code Generator Design
8.1.3 Register Allocation Registers are the fastest computational unit on the target machine Register allocation select the set of variables that will reside in registers at each point in the program Register assignment pick the specific register that a variable will reside in
7
8.1 Problems in Code Generator Design
8.1.4 Evaluation Order The order in which computations are performed can affect the efficiency of the target code. Some computation orders require fewer registers to hold intermediate results than others. However, picking a best order in the general case is a difficult NP-complete problem.
8
8.2 Target Platform 选择可作为几种微机代表的寄存器机器 四字节组成一个字,有n个通用寄存器R0,R1, …,Rn-1。
8.2.1 Instruction System of the Target Platform 选择可作为几种微机代表的寄存器机器 四字节组成一个字,有n个通用寄存器R0,R1, …,Rn-1。 二地址指令 op 源,目的 MOV {源传到目的} ADD {源加到目的} SUB {目的减去源}
9
8.2 Target Platform 地址模式和它们的汇编语言形式及附加代价 模式 形式 地址 附加代价 绝对地址 M M 1
模式 形式 地址 附加代价 绝对地址 M M 寄存器 R R 变址 c(R) c + contents(R) 间接寄存器 *R contents(R) 间接变址 *c(R) contents(c + contents(R)) 1 直接量 #c (常数)c
10
8.2 Target Platform 指令实例 MOV R0, M MOV 4(R0), M
把 contents(4 + contents(R0)) 放到地址为M的地方去 MOV *4(R0), M 把contents(contents (4 + contents(R0) ) )放到地址为M的地方去 MOV #1, R0
11
8.2 Target Platform 8.2.2 Program and Instruction Costs
Instruction cost = 1 + costs associated with the addressing modes of the operands (source and desitination) Instruction Cost MOV R0,R1 1 MOV R5,M 2 ADD #1, R3 2 SUB 4(R0), *12(R1) 3
12
8.2 Target Platform a := b + c, a, b, and c are statically allocated
Different storage locations lead to different cost a := b + c, a, b, and c are statically allocated MOV b, R0 ADD c, R0 cost= 6 MOV R0, a
13
8.2 Target Platform a := b + c, a, b, and c are statically allocated
MOV b, R0 ADD c, R0 Cost= 6 MOV R0, a MOV b, a ADD c, a Cost= 6
14
8.2 Target Platform a := b + c, a, b, and c are statically allocated
If R0, R1, and R2 contain the address of a, b, and c, respectively, then MOV *R1, *R0 ADD *R2, *R0 Cost= 2
15
8.2 Target Platform a := b + c, a, b, and c are statically allocated
If R0, R1, and R2 contain the address of a, b, and c, respectively, then MOV *R1, *R0 ADD *R2, *R0 Cost= 2 If R1 and R2 contain the values of b and c, and b is no longer used afterwards: ADD R2, R1 MOV R1, a Cost= 3
16
8.3 Basic Blocks and Flow Graphs
maximal sequences of consecutive three-address instructions with the properties that there are no jumps into the middle of the block. Control will leave the block without halting or branching, except possibly at the last instruction in the block. flow graph: basic blocks + control flow information t1 := a * a t2 := a * b t3: = 2 *t2 t4: = t1 + t3 t5: = b * b t6: = t4 + t5
17
8.3 Basic Blocks and Flow Graphs
Algorithm that partitions a sequence of three-address instructions into basic blocks. First, determine the leaders The first three-address instruction in the intermediate code Any instruction that is the target of a conditional or unconditional jump Any instruction that immediately follows a conditional or unconditional jump Then, for each leader, its basic block consists of itself and all instructions up to but not including the next leader or the end of the intermediate program t1 := a * b …. L1: t2 = … … goto L1; t1 = …
18
8.3 Basic Blocks and Flow Graphs
(1)prod := 0 (2) i := 1 (3) t1 := 4* i (4) t2:= a[t1] (5) t3 := 4* I (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i +1 (11) i := t7 (12) if i <= 20 goto (3) B1 B2 (1) prod := 0 (2) i := 1 (3) t1 := 4* i (4) t2:= a[t1] (5 ) t3 := 4* i (6 ) t4 := b[t3] (7 ) t5 := t2 * t4 (8 ) t6 := prod + t5 (9 ) prod := t6 (10) t7 := i +1 (11) i := t7 (12 ) if i <= 20 goto (3)
19
8.3 Basic Blocks and Flow Graphs
8.3.2 Transformation of Basic Blocks Three address code x := y + z refers to y, z, and determine x If the value of a name in the basic block is referred at some point after the basic block, we call the name is live at the point Equivalent basic blocks Two basic blocks calculate the same expressions The values of the expressions represent the values of the same live names There are different transformations applicable to basic blocks local global
20
8.3 Basic Blocks and Flow Graphs
Delete local common sub-expression a := b + c a := b + c b := a d b := a d c := b + c c := b + c d := a d d := b Dead code elimination x := y + z not used anymore, x is called dead variable
21
8.3 Basic Blocks and Flow Graphs
Swap neighboring independent statements t1 := b + c t2 := x + y t2 := x + y t1 := b + c If and only if x,y not t1, b,c not t2 Arithmetic transformation x := x + 0 Could be deleted x := x * 1 Could be deleted x := y **2 Equivalent to x := y * y
22
Revision Instruction cost Determination of basic blocks
Transformation of basic blocks
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.