CS 31003: Compilers Introduction to Phases of Compiler.

Slides:



Advertisements
Similar presentations
UNIT-III By Mr. M. V. Nikum (B.E.I.T). Programming Language Lexical and Syntactic features of a programming Language are specified by its grammar Language:-
Advertisements

1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
Invitation to Computer Science 5th Edition
ITEC 352 Lecture 11 ISA - CPU. ISA (2) Review Questions? HW 2 due on Friday ISA –Machine language –Buses –Memory.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
COP4020 Programming Languages
Chapter 1. Introduction.
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.
CSC 338: Compiler design and implementation
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.
What on Earth? LEXEMETOKENPATTERN print p,r,i,n,t (leftpar( 4number4 *arith* 5number5 )rightpar) userAnswerID Letter followed by letters and digits “Game.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
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.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
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.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Introduction to Compiling
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.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Intermediate Code Representations
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Introduction to Language Programming Hierarchy of programming lang. Based on machine independences: 1. Machine language 2. Assembly language 3. Higher.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
A Simple Syntax-Directed Translator
Compiler Construction (CS-636)
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
CS 536 / Fall 2017 Introduction to programming languages and compilers
Compiler Construction
Course supervisor: Lubna Siddiqui
Compiler Construction
Compilers B V Sai Aravind (11CS10008).
Compiler design.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler Structures 1. Overview Objective
Presentation transcript:

CS 31003: Compilers Introduction to Phases of Compiler

Review: Phases of Compiler ● Next few lectures will focus on phases of compiler ● In particular, ■ Intermediate Code Generation ■ Code Optimization ■ Code Generation

Intermediate Code Generation ● In the process of translating a source program into target code, a compiler may construct one or more intermediate representations, which can have a variety of forms. ● Syntax trees are a form of intermediate representation; they are commonly used during syntax and semantic analysis

● After syntax and semantic analysis of the source program, many compilers generate an explicit low level or machine like intermediate representation, which we can think of as a program for an abstract machine. ● This has two important properties:  Should be easy to produce  Should be easy to translate into target machine code Intermediate Code Generation

Three Address Code ● An intermediate form called three-address code,which consists of a sequence of assembly like instructions with three operands per instruction. ● Each operand can act like register. t1=inttofloat(60) t2=id3*t1 t3=id2+t2 id1=t3

Three Address Code ● Some points about three address instructions: ■ Three address assignment has at most one operator on the right side.Thus fix the order in which operations are to be done ■ The compiler must genarate a temporary name to hold the value computed by a three address instruction. ■ Some three address instruction have fewer than three operands.

Code Optimization ● The machine-independent code-optimization phase attempts to improve the intermediate code so that better target code will result. ● Usually better means faster,but other objectives may be desired,such as shorter code,or target code that consumes less power. ● A simple intermediate code generation algorithm followed by code optimization is a reasonable way to generate good target code.

Code Optimization ● The optimizer can deduce that the conversion of 60 from integer to floating point can be done once and for all at compile time, by replacing the integer 60 by the floating point number ● t3 is used only once to transmit its value to id1 so the optimizer can transform into shorter sequence t1=id3* 60.0 id1=id2+t2

Code Optimization ● There is a great variation in the amount of code optimization different compilers perform. ● In those that do the most, the so called “optimizing compilers”, take significant time in this phase. Why to use optimization: ● There are simple optimizations that significantly improve the running time of target program without slowing down compilation too much

Code Generation ● The code generator takes as input an intermediate representation of source program and maps it into target language ● If the target language is machine code registers or memory locations are selected for each of the variables used by the program. Then the intermediate instructions are translated into sequence of machine instructions that perform the same task.

Code Generation ● A crucial aspect of code generation is the judicious assignment of registers to variables ● Using registers R1 and R2, the previous intermediate code gets translated into machine code: LDF R2,id3 MULF R2,R2,#60.0 LDF R1,id2 ADDF R1,R1,R2 STF id1,R1

Code Generation ● The first operand of each instruction specifies a destination. The F in each instruction tells us that it deals with floating –point numbers. The code in line 1 loads the contents of address id3 into register R2,then it multiplies it with floating point constant 60.0 ● The third instruction moves id2 into register R1.Finally the value in register R1 is stored the address of id1,so the code correctly implements the assignment statement.

Lexical Analysis ● A lexical analyzer reads characters from the input and groups them into “token objects”. ● A sequence of input characters that comprises a single token is called lexeme. Thus we can say that the lexical analyzer insulates a parser from the lexeme representation of tokens

Lexical Analysis ● Tasks of lexical analyzer: ■ Task 1: ○ Reads string of characters as input ○ Identify lexemes ○ Send the tokens to Syntax analyzer to convert into syntax trees. ■ Task 2: ○ Remove white spaces. ■ Task 3: ○ Swap x with successor ○ Remembers all the line numbers to report the line number in case of error

Lexical Analysis Lexical AnalyzerParser Symbol Table get Token Token

The End ● Up next: Detailed Discussion on Lexical Analyzer.