From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)

Slides:



Advertisements
Similar presentations
CS 31003: Compilers Introduction to Phases of Compiler.
Advertisements

From Cooper & Torczon1 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language?
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Compiler Construction Dr. Naveed Ejaz Lecture 2. 2 Two-pass Compiler Front End Back End source code IR machine code errors.
CS189A/172 - Winter 2008 Lecture 7: Software Specification, Architecture Specification.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Construction
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Compiler Construction CS 606 Sohail Aslam Lecture 2.
Introduction & Overview CS4533 from Cooper & Torczon.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
The View from 35,000 Feet Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
The View from 35,000 Feet Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
COMPSCI 322: Language and Compilers Class Hour: Hyer Hall 210: TThu 9:30am – 10:15am.
COP4020 Programming Languages
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
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.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1 Chapter 1 Introduction. 2 Outlines 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of.
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.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
컴파일러 첫째주 2005/09/01 권혁철. Making Languages Usable It was our belief that if FORTRAN, during its first months, were to translate any reasonable “scientific”
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.
The View from 35,000 Feet. Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and.
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
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 Dr. Naveed Ejaz Lecture 4. 2 The Back End Register Allocation:  Have each value in a register when it is used. Instruction selection.
Compiler Construction Composed By, Muhammad Bilal Qureshi.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
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.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CMPSC 160 Translation of Programming Languages Fall 2002 Instructor: Hugh McGuire Lecture 2 Phases of a Compiler Lexical Analysis.
1 Compiler Construction Vana Doufexi office CS dept.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Lecture 02: Compiler Overview Kwangman Man ( SangJi University.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
1. Course Goals To provide students with an understanding of the major phases of a compiler. To introduce students to the theory behind the various phases,
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Advanced Computer Systems
Introduction to Optimization
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
컴파일러 첫째주 2005/09/01 권혁철.
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
Lecture 2: General Structure of a Compiler
Introduction to Optimization
Intermediate Representations
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Lecture 02: Compiler Overview
Compiler Construction
Intermediate Representations
Introduction to Optimization
Compiler Construction
Compiler Construction
Presentation transcript:

from Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code) Must agree with OS & linker on format for object code High-level View of a Compiler Source code Machine code Compiler Errors

from Cooper & Torczon2 Traditional Two-pass Compiler Implications Use an intermediate representation ( IR ) Front end maps legal source code into IR Back end maps IR into target machine code Admits multiple front ends & multiple passes (better code) Typically, front end is O(n) or O(n log n), while back end is NPC Source code Front End Errors Machine code Back End IR

from Cooper & Torczon3 Can we build n x m compilers with n+m components? Must encode all language specific knowledge in each front end Must encode all features in a single IR Must encode all target specific knowledge in each back end Limited success in systems with very low-level IR s A Common Fallacy Fortran Scheme Java Smalltalk Front end Front end Front end Front end Back end Back end Target 2 Target 1 Target 3 Back end

from Cooper & Torczon4 Responsibilities Recognize legal (& illegal) programs Report errors in a useful way Produce IR & preliminary storage map Shape the code for the back end Much of front end construction can be automated The Front End Source code Scanner IR Parser Errors tokens

from Cooper & Torczon5 The Front End Scanner Maps character stream into words—the basic unit of syntax Produces words & their parts of speech x = x + y ; becomes ;  word  lexeme, part of speech  token  In casual speech, we call the pair a token Typical tokens include number, identifier, +, -, while, if Scanner eliminates white space Speed is important  use a specialized recognizer Source code Scanner IR Parser Errors tokens

from Cooper & Torczon6 The Front End Parser Recognizes context-free syntax & reports errors Guides context-sensitive analysis (type checking) Builds IR for source program Hand-coded parsers are fairly easy to build Most books advocate using automatic parser generators Source code Scanner IR Parser Errors tokens

from Cooper & Torczon7 The Front End Compilers often use an abstract syntax tree This is much more concise AST s are one form of intermediate representation ( IR ) + - The AST summarizes grammatical structure, without including detail about the derivation

from Cooper & Torczon8 The Back End Responsibilities Translate IR into target machine code Choose instructions to implement each IR operation Decide which value to keep in registers Ensure conformance with system interfaces Automation has been much less successful in the back end Errors IR Instruction Scheduling Instruction Selection Machine code Register Allocation IR

from Cooper & Torczon9 The Back End Instruction Selection Produce fast, compact code Take advantage of target features such as addressing modes Usually viewed as a pattern matching problem  ad hoc methods, pattern matching, dynamic programming This was the problem of the future in 1978  Spurred by transition from PDP-11 to VAX-11  Orthogonality of RISC simplified this problem Errors IR Instruction Scheduling Instruction Selection Machine code Register Allocation IR

from Cooper & Torczon10 The Back End Instruction Scheduling Avoid hardware stalls and interlocks Use all functional units productively Can increase lifetime of variables (changing the allocation) Optimal scheduling is NP-Complete in nearly all cases Good heuristic techniques are well understood Errors IR Instruction Scheduling Instruction Selection Machine code Register Allocation IR

from Cooper & Torczon11 The Back End Register allocation Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOAD s & STORE s Optimal allocation is NP-Complete (1 or k registers) Compilers approximate solutions to NP-Complete problems Errors IR Instruction Scheduling Instruction Selection Machine code Register Allocation IR

from Cooper & Torczon12 Traditional Three-pass Compiler Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code  May also improve space, power consumption, … Must preserve “meaning” of the code  Measured by values of named variables Errors Source Code Middle End Front End Machine code Back End IR

from Cooper & Torczon13 The Optimizer (or Middle End) Typical Transformations Discover & propagate some constant value Move a computation to a less frequently executed place Discover a redundant computation & remove it Remove useless or unreachable code Errors Opt1Opt1 Opt3Opt3 Opt2Opt2 OptnOptn... IR Modern optimizers are structured as a series of passes