Lecture 02: Compiler Overview

Slides:



Advertisements
Similar presentations
Compiler Construction Dr. Naveed Ejaz Lecture 2. 2 Two-pass Compiler Front End Back End source code IR machine code errors.
Advertisements

Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
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.
COMPSCI 322: Language and Compilers Class Hour: Hyer Hall 210: TThu 9:30am – 10:15am.
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.
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.
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.
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.
Introduction to Compiling
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.
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.
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.
Introduction to Parsing
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Advanced Computer Systems
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
컴파일러 첫째주 2005/09/01 권혁철.
Introduction to Compiler Construction
Lexical and Syntax Analysis
A Simple Syntax-Directed Translator
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.
CS416 Compiler Design lec00-outline September 19, 2018
Lecture 2: General Structure of a Compiler
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Lexical Analysis - An Introduction
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Compiler Construction
System Programming and administration
Introduction to Parsing
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.
CS416 Compiler Design lec00-outline February 23, 2019
Lexical Analysis - An Introduction
CISC 7120X Programming Languages and Compilers
Compiler Construction
Chapter 10: Compilers and Language Translation
Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler design Review COMP 442/6421 – Compiler Design
Presentation transcript:

Lecture 02: Compiler Overview Kwangman Man (http://compiler.sangji.ac.kr, kkman@sangji.ac.kr) SangJi University Fall 2015

1.1 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, … Source code Machine Compiler Errors Lecture 02: Compiler Overview, 2015.

Java is compiled to bytecodes(code for the Java VM) 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, … Lecture 02: Compiler Overview, 2015

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

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

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

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

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

1.3 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 Source code Machine Compiler Errors Lecture 02: Compiler Overview, 2015

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

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

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

The Front End Responsibilities Recognize legal (& illegal) 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 Scanner Parser Source code tokens IR Errors Lecture 02: Compiler Overview, 2015

Scanner, Lexical analyser Maps character stream into tokens—the basic unit of syntax Produces pairs x = x + y ; becomes <id,x> = <id,x> + <id,y> ; 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 Operators Constants: 0,1,2, 3, … Variables, Names Keywords Lecture 02: Compiler Overview, 2015

Hand-coded parsers are fairly easy to build 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 Lecture 02: Compiler Overview, 2015

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 ) (Example due to Dr. Scott K. Warren) Lecture 02: Compiler Overview, 2015

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. 1. 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, 2015

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 parse 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 + 2 - y 3 term + 2 - y 5 x + 2 - y Lecture 02: Compiler Overview, 2015

A parse can be represented by a tree, parse tree or syntax tree x + 2 - y contains a lot of unneeded informations term op expr goal <id,x> <number,2> <id,y> + - Lecture 02: Compiler Overview, 2015

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. + - <id,x> <number,2> <id,y> Lecture 02: Compiler Overview, 2015

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 less successful in the back end Errors IR Register Allocation Instruction Selection Machine code Scheduling Lecture 02: Compiler Overview, 2015

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

Compilers approximate solutions to NP-Complete problems Register Allocation Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOADs & STOREs Optimal allocation is NP-Complete(1 or k registers) Compilers approximate solutions to NP-Complete problems Lecture 02: Compiler Overview, 2015

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

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 Machine code Back IR Lecture 02: Compiler Overview, 2015

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 Errors Opt 1 3 2 n ... IR Lecture 02: Compiler Overview, 2015

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

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