1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.

Slides:



Advertisements
Similar presentations
1 CIS 461 Compiler Design and Construction Fall 2014 Instructor: Hugh McGuire slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module.
Advertisements

Target Code Generation
Instruction Set Design
CS 31003: Compilers Introduction to Phases of Compiler.
1 Compiler Construction Intermediate Code Generation.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
Compiler Construction by Muhammad Bilal Zafar (AP)
Program Representations. Representing programs Goals.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Precision Going back to constant prop, in what cases would we lose precision?
A Portable Virtual Machine for Program Debugging and Directing Camil Demetrescu University of Rome “La Sapienza” Irene Finocchi University of Rome “Tor.
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Chapter 1 Introduction Major Data Structures in Compiler
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
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.
Compiler Construction (CS-636)
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Hello world !!! ASCII representation of hello.c.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Code Generation Part I Chapter 8 (1st ed. Ch.9)
CS 404 Introduction to Compiler Design
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Introduction to Compiler Construction
Compiler Construction (CS-636)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Construction
Compiler Construction
Code Generation.
Intermediate Representations
Unit IV Code Generation
Compiler Construction
Intermediate Representations
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Target Code Generation
Lecture 4: Instruction Set Design/Pipelining
TARGET CODE GENERATION
Programming language translators
Presentation transcript:

1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi

Outline 1. Code Generation 2. Issues in the Design of a Code Generator 1. Input to the Code Generator 2. The Target Program 3. Instruction Selection 4. Register Allocation 5. Evaluation Order 3. Summary 2

Code Generation Lecture:

Code Generation The final phase in our compiler model is the code generator It takes as input the intermediate representation (IR) produced by the front-end, along with the symbol table information, and produces as output a semantically equivalent target program The following figure shows the process flow; 4 Front End intermediate code Code Optimizer Code Generator intermediate code source program target program

Code Generation (Continue…) The requirements imposed on a code generator are severe  The target program must preserve semantic meaning of the source program  The target program should be of high quality; it must make effective use of the available resources of target machine  The code generator itself must run efficiently The problem of generating an optimal target program for a given source program is undecideable In practice, we must be content with heuristic techniques that generate good, but not necessarily optimal code 5

Code Generation (Continue…) Compilers that need to produce efficient target programs, include an optimization phase prior to code generation  The optimizer maps IR into IR from which the efficient code can be generated In general, the code-optimization and code- generation phases of a compiler, often referred to a the back end, may make multiple phases over the IR before generating the target program A code generator has three primary tasks;  Instruction selection, register allocation, instruction ordering 6

Issues in the Design of a Code Generator Although details can vary but tasks such as instruction selection, register allocation and assignment, and instruction ordering are encountered in design of almost all code generators The most important criteria of a good code generator is that it produces correct code Given the premium on correctness, designing a code generator so it can be easily implemented, tested, and maintained is an important design goal 7

Input to the Code Generator The input to the code generator includes;  The intermediate representation of the source program produced by the front end  Information in the symbol table that is used to determine the run-time addresses of the data objects denoted by the names in the IR The IR can be in any form;  Three-address representations such as quadruples, triples  Virtual machine representations such as bytecodes and stack-machine codes  Linear representations such as postfix notation  Graphical representations such as syntax trees & DAGs 8

Input to the Code Generator (Continue…) It is assumed that front-end of the compiler has scanned, parsed, and translated the source program into a relatively low-level IR It is also assumed that all syntactic and static semantic errors have been detected, that the necessary type checking has taken place, and that type-conversion operators have been inserted wherever necessary The code generator can therefore proceed on the assumption that its input is free of these kinds of errors 9

The Target Program The instruction-set architecture of the target machine has a significant impact on the difficulty of constructing a good code generator The most common target machine architectures are RISC (reduced instruction set computer), CISC (complex instruction set computer), and stack based  A RSIC machine typically have many registers, three- address instructions, simple addressing modes, and a relatively simple instruction-set architecture  A CICS machine typically has few registers, two-address instructions, a variety of address nodes, several register classes, variable-length instructions 10

The Target Program (Continue…) In a stack-based machine, operations are done by pushing operands onto a stack an then performing the operations on the operands at top of the stack To achieve high performance the top of the stack is typically kept in registers Stack-based machines almost disappeared because it was felt that the stack organization was too limiting and required too many swap and copy operations However, stack-based architectures were revived with the introduction of the Java Virtual Machine (JVM) 11

The Target Program (Continue…) The JVM is a software interpreter for Java bytecodes, as intermediate language produced by Java compilers To overcome high performance of interpretation, just-in-time (JIT) Java compilers have been created that translates bytecode during run time to the native hardware instruction set of the target machine Producing an absolute machine language program as output has the advantage; it can be placed in a fixed location in memory and immediately executed Programs can be compiled and executed quickly 12

The Target Program (Continue…) Producing a relocatable machine language program (object module) as output allows subprograms to be compiled separately A set of relocatable object modules can be linked together and loaded for execution by a linking loader Producing an assembly language program as output makes the process of code generation somewhat easier We can generate symbolic instructions and use the macro facilities of the assembler to help generate code 13

Instruction Selection The code generator must map the IR program into a code sequence that can be executed by the target machine The complexity of performing this mapping is determined by a factors such as;  The level of the IR  The nature of the instruction-set architecture  The desired quality of the generated code 14

Instruction Selection (Continue…) The level of the IR If the IR is high level, the code generator may translate each IR statement into a sequence of machine instructions using code templates Such statement by statement code generation, however often produces poor code that needs further optimization If the IR reflects some of the low-level details of the underlying machine, then the code generator can use this information to generator more efficient code 15

Instruction Selection (Continue…) The nature of the instruction-set architecture This factor has a strong effect on the difficulty of instruction selection For example, the uniformity and completeness of the instruction set are important factors If the target machine does not support each data type in a uniform manner, then it requires special handling Instruction speeds is another important factor If we do not care about the efficiency of the target program, instruction selection is straightforward 16

Instruction Selection (Continue…) For example x = y + z can be translated as follows; LD R0, y// R0 = y ADD R0, R0, z// R0 = R0 + z ST x, R0// x = R0 This strategy often produces redundant instructions e.g. a = b + c and d = a + e can be translated as; LD R0, b// R0 = b ADD R0, R0, c// R0 = R0 + c ST a, R0// a = R0 LD R0, a// R0 = a ADD R0, R0, e// R0 = R0 + e ST d, R0// d = R0 Statement four and three are redundant 17

Instruction Selection (Continue…) The desired quality of the generated code It is usually determined by its speed and size On most machines a given IR can be implemented by many different code sequences For example if the target machine has an increment instruction (INC), then the three-address statement a = a + 1 may be implemented more efficiently by the single instruction INC a We also need to know instruction costs in order to design good code sequences but that depends on the context of the construct being used 18

Register Allocation A key problem in code generation is deciding what values to hold in what registers Registers are the fastest computational unit on the target machine but we usually do not have enough of them to hold all values Values not held in registers need to reside in memory Instructions involving register operands are invariably shorter and faster than those involving operands in memory, so efficient utilization of registers is important 19

Register Allocation Continue…) The use of registers is often sub-divided into two sub-problems 1. Register allocation: during which we select the set of variables that will reside in registers at each point in the program 2. Register assignment: during which we pick the specific register that a variable will reside in Finding an optimal assignment to registers is difficult The problem is further complicated because the hardware and/or operating system of the target machine may require that certain register-usage conventions be observed 20

Evaluation Order The order in which computations are performed can affect the efficiency of the target code However, picking a best order of computations in the general case is a difficult problem We shall avoid the problem by generating code for the three-address statements in the order in which they have been produced by the intermediate code generator 21

22 Summary Any Questions?