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

Slides:



Advertisements
Similar presentations
1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization.
Advertisements

Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
Target Code Generation
Code Generation.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
CS 31003: Compilers Introduction to Phases of Compiler.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
1 Compiler Construction Intermediate Code Generation.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction.
Code Generation Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Chapter 8 . Sequence Control
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Compiler Chapter# 5 Intermediate code generation.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
1 Code Generation. 2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compilers Modern Compiler Design
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Topic #9: Target Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Code Generation The target machine
CS 404 Introduction to Compiler Design
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Introduction to Compiler Construction
Intermediate code Jakub Yaghob
Compiler Construction (CS-636)
Chapter 1: Introduction to Compiling (Cont.)
Compiler Construction
Code Generation Part I Chapter 9
Code Generation.
Unit IV Code Generation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
CS 201 Compiler Construction
Code Generation Part I Chapter 9
Interval Partitioning of a Flow Graph
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Compiler Construction
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Target Code Generation
TARGET CODE GENERATION
Review: What is an activation record?
Presentation transcript:

1 Chapter10: Code generator

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 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 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 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 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 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 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 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 Examples MOVR0, M MOV4 (R0), M MOV*R0, M MOV*4 (R0), M MOV#1, R0

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

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 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 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 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 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 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 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 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 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 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 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 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 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 The End