Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.

Similar presentations


Presentation on theme: "1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator."— Presentation transcript:

1 1 Chapter10: Code generator

2 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Syntax Analyzer Lexical Analyzer Symbol Table Manager Error Handler      

3 3 Introduction The code generator is the final phase of compiler. It takes as input an intermediate representation of the source program and produces as output an equivalent target program. The position of the code generation in the compiler model is as follows Source Program Front End Symbol Table Code Optimization Code Generator Interm. Code Target Program Interm. Code

4 4 Code generation * The code generation strategy generates target code for a sequence of three-address statement. It considers each statement in turn, remembering if any of the operands of the statement are currently in registers, and taking advantage of that fact if possible. It is assumed that for each operator in a statement there is a corresponding target language operator. * It is also assumed that the computed results can be left in registers as long as possible, storing them only if their register is needed for another computation or just before a procedure call, jump, or a labeled statement.

5 5 Issues in the design of a code generator Designing a code generator depends on the target language and the operating system 1. memory management 2. instruction selection 3. register allocation 4. evaluation order

6 6 Generic Issues 1. Input: The input to the code generator consists of the intermediate representation of the source program produced by the front end and the information in the symbol table. * The different forms of input are: a. Linear representation (postfix notation) b. Three-address code c. Virtual machine code (stack machine code) d. Graphical representation (syntax trees).

7 7 Generic Issues 2.Output: The output of the code generator is the target program which is of following forms: a. Absolute machine language program: It can be placed in a fixed location in memory and hence can be executed immediately. b. Relocatable machine language program (object module): It allows subprograms to be compiled separately. c. Assembly language program: It makes the process of code generation easier. Here symbolic instructions are generated and macro facilities of assembler are used to generate code.

8 8 Generic Issues 3.Memory Management: Names in the source program are mapped to addresses of data objects in runtime memory cooperatively by the front end and the code generator. 4.Instruction Selection: A target machine with a rich instruction set may provide several ways of implementing a given operation.

9 9 The Target Machine A byte addressable machine with four bytes to a word and n general purpose registers Two address instructions –opsource, destination Six addressing modes absolute MM register RR indexed c(R) c+content(R) ind register *R content(R) ind indexed *c(R) content(c+content(R)) literal #cc

10 10 Examples MOVR0, M MOV4 (R0), M MOV*R0, M MOV*4 (R0), M MOV#1, R0

11 11 Examples MOVR0, R1 MOVR0, M MOV#1, R0 MOV4 (R0), *12 (R1)

12 12 An Example Consider a := b + c 1. MOVb, R02. MOVb, a ADDc, R0 ADDc, a MOVR0, a 3. R0, R1, R2 contains4. R1, R2 contains the addresses of a, b, c the values of b, c MOV*R1, *R0 ADDR2, R1 ADD*R2, *R0 MOVR1, a

13 13 Basic Blocks * A basic block is a sequence of consecutive statements in which control enters at the beginning and leaves at the end without halt or possibility of branching except at the end (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)

14 14 Flow Graphs * A flow graph is a directed graph for three address statements * The nodes in the graph are basic blocks * There is an edge from B 1 to B 2 if B 2 immediately follows B 1 in some execution sequence B 2 immediately follows B 1 in program text there is a jump from B 1 to B 2 * B 1 is a predecessor of B 2, B 2 is a successor of B 1

15 15 An Example (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) B0B0 B1B1

16 16 Construction of Basic Blocks Determine the set of leaders –the first statement is a leader –the target of a jump is a leader –any statement immediately following a jump is a leader For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the program

17 17 Define and Use A three address statement x := y + z is said to define x and to use y and z A name is live in a basic block at a given point if its value is used after that point, perhaps in another basic block

18 18 Example (1)a := b + c a:(2,3,5), c:(4), d:(2) (2)e := a + d a:(3,5), c:(4), e:(3) (3)f := e - a a:(5), c:(4), f:(4) (4)e := f + c a:(5), e:(5) (5)g := e - a g:(?) b, c, d are live at the beginning of the block b:(1), c:(1,4), d:(2)

19 19 A Simple Code Generator Consider each statement in a basic block in turn, remembering if operands are in registers Computed results can be left in registers as long as possible, unless out of registers at the end of a basic block

20 20 Example d := (a - b) + (a - c) + (a - c) [ ] t := a - bMOV a, R0[R0:(t)] SUB b, R0[t:(R0)] u := a - cMOV a, R1[R0:(t), R1:(u)] SUB c, R1[t:(R0), u:(R1)] v := t + uADD R1, R0[R0:(v), R1:(u)] [v:(R0), u:(R1)] d := v + uADD R1, R0[R0:(d)] [d:(R0)] MOV R0, d[ ] [ ]

21 21 Conditional Statements Condition codes if x < y goto zCMP x, y CJ< z Condition code descriptors x := y + zMOV y, R0 if x < 0 goto zADD z, R0 MOV R0, x CJ< z

22 22 Loops A loop is a collection of nodes such that –1. all nodes in the collection are strongly connected –2. the collection of nodes has a unique entry An inner loop is one that contains no other loops

23 23 An Example a := b + c d := d - b e := a + f f := a - d b := d + f e := a - c b := d + c B1 B2B3 B4 b,c,d,e,f b,c,d,f b,c,d,e,f c,d,e,f b,c,d,e,f b,d,e,f a,c,d,f a,c,d,e a,c,d,e,f

24 24 An Example MOV R1, R0; ADD c, Ro SUB R1, R2; MOV R0, R3 ADD f, R3; MOV R3, e MOV R0, R3; SUB R2, R3 MOV R3, f MOV R2, R1; ADD f, R1 MOV R0, R1; SUB c, R3 MOV R3, e MOV R2, R1; ADD c, R1 B1 B2B3 B4 MOV R1, b; MOV R2, d MOV b, R1; MOV d, R2

25 25 The End


Download ppt "1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator."

Similar presentations


Ads by Google