Lecture 02: Compiler Overview Kwangman Man (http://compiler.sangji.ac.kr, SangJi University.

Slides:



Advertisements
Similar presentations
Compilers and Language Translation
Advertisements

Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
Compiler Construction Dr. Naveed Ejaz Lecture 2. 2 Two-pass Compiler Front End Back End source code IR machine code errors.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Compiler Construction
Wrapping Up 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.
Overview of the Course. Critical Facts Welcome to CISC 672 — Advanced Compiler Construction Instructor: Dr. John Cavazos Office.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
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.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
COMPSCI 322: Language and Compilers Class Hour: Hyer Hall 210: TThu 9:30am – 10:15am.
CS 363 Comparative 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.
Overview of the Course Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Overview of the Course Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
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.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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 EECS 6083 Compiler Theory Based on slides from text web site: Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
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 Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
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.
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.
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.
Introduction to Parsing
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,
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
컴파일러 첫째주 2005/09/01 권혁철.
Introduction to Compiler Construction
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
Overview of the Course Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Lecture 2: General Structure of a Compiler
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
Introduction to Parsing
Overview of the Course Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Compiler Construction
Chapter 10: Compilers and Language Translation
Compiler Construction
Presentation transcript:

Lecture 02: Compiler Overview Kwangman Man ( SangJi University 2014

Translation What is a compiler? –A program that translates an executable program in one language into an executable program in another language. –The compiler should improve the program, in some way. Optimizations, … 2 Lecture 02: Compiler Overview, Source code Machine code Compiler Errors

What is an interpreter? –A program that reads an executable program and produces the results of executing that program. –Java is compiled to bytecodes(code for the Java VM) which are then interpreted or a hybrid strategy is used Just-in-timeI(JIT) compilation –Perl, Scheme, APL, … 3 Lecture 02: Compiler Overview,

Taking a Broader View Compiler Technology = Off-Line Processing –Goals: improved performance and language usability. Making it practical to use the full power of the language –Trade-off preprocessing time versus execution time (or space) –Rule performance of both compiler and application must be acceptable to the end user 4 Lecture 02: Compiler Overview,

Why Study Compilation? Compilers are important system software components –intimately interconnected with architecture, systems, programming methodology, and language design. Compilers include many applications of theory to practice –Scanning, parsing, static analysis, instruction selection, … Writing a compiler exposes practical algorithmic & engineering issues –Approximating hard problems; efficiency & scalability 5 Lecture 02: Compiler Overview,

Intrinsic interest Compiler construction involves ideas from many different parts of computer science 6 Artificial intelligence Greedy algorithms Heuristic search techniques Algorithms Graph algorithms, union-find Dynamic programming Theory DFAs & PDAs, pattern matching Fixed-point algorithms Systems Allocation & naming, Synchronization, locality Architecture Pipeline & hierarchy management Instruction set use Lecture 02: Compiler Overview,

Intrinsic merit  Compiler construction poses challenging and interesting problems: –Compilers must do a lot but also run fast –Compilers have primary responsibility for run-time performance –Compilers are responsible for making it acceptable to use the full power of the programming language –Computer architects perpetually create new challenges for the compiler by building more complex machines –Compilers must hide that complexity from the programmer –Success requires mastery of complex interactions 7 Lecture 02: Compiler Overview,

The View from 35,000 Feet Lecture 02: Compiler Overview,

High-level View of a Compiler 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 9 Source code Machine code Compiler Errors Lecture 02: Compiler Overview,

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 10 Source code Front End Errors Machine code Back End IR Lecture 02: Compiler Overview,

A Common Fallacy 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 11 Fortran Scheme Java Smalltalk Front end Front end Front end Front end Back end Back end Target 2 Target 1 Target 3 Back end Lecture 02: Compiler Overview,

12 Fortran Scheme Java Smalltalk Front end Front end Front end Front end Back end Back end Target 2 Target 1 Target 3 Back end IR Lecture 02: Compiler Overview,

The Front End Responsibilities –Recognize legal (& illegal) source programs –Report errors in a useful way –Produce IR & preliminary storage map –Shape the code for the back end, IR –Much of front end construction can be automated 13 Source code Scanner IR Parser Errors tokens Lecture 02: Compiler Overview,

Scanner, Lexical analyser Scanner –Maps character stream into tokens—the basic unit of syntax –Produces pairs x = x + y ; becomes = + ; word  lexeme, part of speech  token In casual speech, we call the pair a token –Typical tokens include number, identifier, +, –, new, while, if –eliminates white space including comments –Speed is important 14 Constants: 0,1,2, 3, … Variables, Names Operators Keywords Lecture 02: Compiler Overview,

Parser –Recognizes context-free syntax & reports errors –Guides context-sensitive (“semantic”) analysis, type checking –Builds IR for source program Hand-coded parsers are fairly easy to build Most books advocate using automatic parser generators 15 Lecture 02: Compiler Overview,

Context-free syntax is specified with a grammar SheepNoise  SheepNoise baa | baa –written in a variant of Backus–Naur Form(BNF) Formally, a grammar G = (S,N,T,P) –S is the start symbol –N is a set of non-terminal symbols –T is a set of terminal symbols or words –P is a set of productions or rewrite rules(P : N  N  T ) 16 Lecture 02: Compiler Overview,

Context-free syntax can be put to better use –defines simple expressions with addition & subtraction over “number” and “id” –called “context-free grammars”, abbreviated CFG goal  expr 2. expr  expr op term 3. | term 4. term  number 5. | id 6. op  + 7. | - S = goal T = { number, id, +, - } N = { goal, expr, term, op } P = { 1, 2, 3, 4, 5, 6, 7} Lecture 02: Compiler Overview,

Given a CFG, –can derive sentences by repeated substitution –To recognize a valid sentence in some CFG, we reverse this process and build up a parser. 18 Production Result goal 1 expr 2 expr op term 5 expr op y 7 expr - y 2 expr op term - y 4 expr op 2 - y 6 expr y 3 term y 5 x y Lecture 02: Compiler Overview,

A parse can be represented by a tree, parse tree or syntax tree –x y –contains a lot of unneeded informations 19 term optermexpr termexpr goal expr op + - Lecture 02: Compiler Overview,

Compilers often use an abstract syntax tree, AST. –is much more concise ASTs are one kind of intermediate representation(IR). –The AST summarizes grammatical structure, without including detail about the derivation Lecture 02: Compiler Overview,

The Back End Responsibilities –Translate IR into target machine code –Choose target instructions to implement each IR operation –Decide which value to keep in registers –Ensure conformance with system interfaces Automation has been less successful in the back end 21 Errors IR Register Allocation Instruction Selection Machine code Instruction Scheduling IR Lecture 02: Compiler Overview,

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 22 Lecture 02: Compiler Overview,

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 23 Lecture 02: Compiler Overview,

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 Heuristic techniques are well developed 24 Lecture 02: Compiler Overview,

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 25 Errors Source Code Middle End Front End Machine code Back End IR Lecture 02: Compiler Overview,

The Optimizer (or Middle End) Typical Transformations –Discover & propagate some constant value –Move a computation to a less frequently executed place –Specialize some computation based on context –Discover a redundant computation & remove it –Remove useless or unreachable code –Encode an idiom in some particularly efficient form 26 Errors Opt1Opt1 Opt3Opt3 Opt2Opt2 OptnOptn... IR Lecture 02: Compiler Overview,

Modern Restructuring Compiler Typical Restructuring Transformations: –Blocking for memory hierarchy and register reuse –Vectorization –Parallelization –All based on dependence –Also full and partial inlining 27 Errors Source Code Restructure r Front End Machine code Opt + Back End HL AST IR HL AST IR Gen Lecture 02: Compiler Overview,

Role of the Run-time System Memory management services –Allocate In the heap or in an activation record ( stack frame ) –Deallocate –Collect garbage Run-time type checking Error processing Interface to the operating system –Input and output Support of parallelism –Parallel thread initiation –Communication and synchronization 28 Lecture 02: Compiler Overview,