CS 406/534 Compiler Construction Putting It All Together

Similar documents
CS5363 Final Review. cs5363 1

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS 406/534 Compiler Construction Parsing Part I

Register Allocation. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CS415 Compilers. Lexical Analysis

CS 406/534 Compiler Construction Instruction Selection and Global Register Allocation

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Compiler Design. Register Allocation. Hwansoo Han

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Global Register Allocation via Graph Coloring

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

CJT^jL rafting Cm ompiler

CS 406/534 Compiler Construction Instruction Scheduling

CS /534 Compiler Construction University of Massachusetts Lowell

Alternatives for semantic processing

CS415 Compilers. Intermediate Represeation & Code Generation

CS 406/534 Compiler Construction Parsing Part II LL(1) and LR(1) Parsing

CS 406/534 Compiler Construction Intermediate Representation and Procedure Abstraction

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Review. Pat Morin COMP 3002

The View from 35,000 Feet

2068 (I) Attempt all questions.

Parsing II Top-down parsing. Comp 412

Syntax-Directed Translation. Lecture 14

CSCI312 Principles of Programming Languages

CS606- compiler instruction Solved MCQS From Midterm Papers

Appendix Set Notation and Concepts

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Introduction to Parsing. Comp 412

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

CS415 Compilers. Instruction Scheduling and Lexical Analysis

Register allocation. TDT4205 Lecture 31

Bottom-Up Parsing. Lecture 11-12

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

LECTURE NOTES ON COMPILER DESIGN P a g e 2

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Compiler Optimisation

register allocation saves energy register allocation reduces memory accesses.

CSE 582 Autumn 2002 Exam 11/26/02

Question Bank. 10CS63:Compiler Design

CS 4120 Introduction to Compilers

Instruction Selection: Preliminaries. Comp 412

BSCS Fall Mid Term Examination December 2012

Bottom-Up Parsing. Lecture 11-12

CS 406/534 Compiler Construction Instruction Scheduling beyond Basic Block and Code Generation

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

Front End. Hwansoo Han

Instruction Selection: Peephole Matching. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Compilers. Computer Science 431

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Lecture Compiler Middle-End

Evaluation Scheme L T P Total Credit Theory Mid Sem Exam

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

CSE 401 Midterm Exam Sample Solution 2/11/15

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CSCI 565 Compiler Design and Implementation Spring 2014

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Lecture 8: Deterministic Bottom-Up Parsing

Syntax Analysis Part I

Introduction to Lexing and Parsing

Intermediate Representations

Intermediate Representations

Code Merge. Flow Analysis. bookkeeping

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Syntactic Analysis. Top-Down Parsing

Syntax Analysis, III Comp 412

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

Compilers and Interpreters

Register Allocation. CS 502 Lecture 14 11/25/08

Lecture 7: Deterministic Bottom-Up Parsing

Data Flow Analysis. Program Analysis

Why Global Dataflow Analysis?

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

ELEC 876: Software Reengineering

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

CSE 5317 Midterm Examination 4 March Solutions

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

CSc 553. Principles of Compilation. 23 : Register Allocation. Department of Computer Science University of Arizona

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

CS415 Compilers. Procedure Abstractions. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Compiler phases. Non-tokens

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Transcription:

CS 406/534 Compiler Construction Putting It All Together Prof. Li Xu Dept. of Computer Science UMass Lowell Fall 2004 Part of the course lecture notes are based on Prof. Keith Cooper, Prof. Ken Kennedy and Dr. Linda Torczon s teaching materials at Rice University. All rights reserved. 1

Administravia Last lecture today Lab2 and lab3 graded Final exam now handed out, due 12/20 Deadline is firm: late exam will not be graded Extra credit lab3 presentation today CS406/534 Fall 2004, Prof. Li Xu 2 2

What We Did Last Time Program analysis and optimization Overview of compiler optimization Local optimization DAG Value numbering Control flow analysis CFG, DOM tree, natural loops Data flow analysis SSA Generic framework Typical data flow problems AVAIL, REACH, LIVE CS406/534 Fall 2004, Prof. Li Xu 3 3

Today s Goals Summary of the subjects we ve covered Perspectives and final remarks How will you use 91.406/534 knowledge? CS406/534 Fall 2004, Prof. Li Xu 4 4

High-level View Source code Compiler Machine code Errors Definitions Compiler consumes source code & produces target code usually translate high-level language programs into machine code Interpreter consumes executables & produces results virtual machine for the input code CS406/534 Fall 2004, Prof. Li Xu 5 5

Why Study Compilers? Compilers are important Enabling technology for languages, software development Allow programmers to focus on problem solving, hiding the hardware complexity Responsible for good system performance Compilers are useful Language processing is broadly applicable Compilers are fun Combine theory and practice Overlap with other CS subjects Hard problems Engineering and trade-offs Got a taste in the labs! CS406/534 Fall 2004, Prof. Li Xu 6 6

Structure of Compilers Front Middle End Back End 7 Register Allocation Instruction Scheduling Instruction Selection IR Optimization n Optimization 2 Optimization 1 IR CSA Parser Scanner Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples CS406/534 Fall 2004, Prof. Li Xu 7 Analysis

The Front-end Front Middle End Back End 8 Register Allocation Instruction Scheduling Instruction Selection IR Optimization n Optimization 2 Optimization 1 IR CSA Parser Scanner Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples CS406/534 Fall 2004, Prof. Li Xu 8 Analysis

Scanner Lexical Analysis Maps character stream into tokens Automate scanner construction Define tokens using Regular Expressions Construct NFA (Nondeterministic Finite Automata) to recognize REs Transform NFA to DFA Convert NFA to DFA through subset construction DFA minimization (set split) Building scanners from DFA Tools ANTLR, lex CS406/534 Fall 2004, Prof. Li Xu 9 9

Syntax Analysis Parsing language using CFG (context-free grammar) CFG grammar theory Derivation Parse tree Grammar ambiguity Parsing Top-down parsing recursive descent table-driven LL(1) Bottom-up parsing LR(1) shift reduce parsing CS406/534 Fall 2004, Prof. Li Xu 10 10

Top-down Predictive Parsing Basic idea Build parse tree from root. Given A α β, use look-ahead symbol to choose between α & β Recursive descent Table-driven LL(1) Left recursion elimination CS406/534 Fall 2004, Prof. Li Xu 11 11

Bottom-up Shift-Reduce Parsing Build reverse rightmost derivation The key is to find handle (rhs of production) All active handles include top of stack (TOS) Shift inputs until TOS is right end of a handle Language of handles is regular (finite) Build a handle-recognizing DFA ACTION & GOTO tables encode the DFA CS406/534 Fall 2004, Prof. Li Xu 12 12

Semantic Analysis Analyze context and semantics types and other semantic checks Attribute grammar associate evaluation rules with grammar production Ad-hoc build symbol table CS406/534 Fall 2004, Prof. Li Xu 13 13

Intermediate Representation Front Middle End Back End 14 Register Allocation Instruction Scheduling Instruction Selection IR Optimization n Optimization 2 Optimization 1 IR CSA Parser Scanner Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples CS406/534 Fall 2004, Prof. Li Xu 14 Analysis

Intermediate Representation Front-end translates program into IR format for further analysis and optimization IR encodes the compiler s knowledge of the program Largely machine-independent Move closer to standard machine model AST Tree: high-level Linear IR: low-level ILOC 3-address code Assembly-level operations Expose control flow, memory addressing unlimited virtual registers CS406/534 Fall 2004, Prof. Li Xu 15 15

Procedure Abstraction Procedure is key language construct for building large systems Name Space Caller-callee interface: linkage convention Control transfer Context protection Parameter passing and return value Run-time support for nested scopes Activation record, access link, display Inheritance and dynamic dispatch for OO multiple inheritance virtual method table CS406/534 Fall 2004, Prof. Li Xu 16 16

The Back-end Front Middle End Back End 17 Register Allocation Instruction Scheduling Instruction Selection IR Optimization n Optimization 2 Optimization 1 IR CSA Parser Scanner Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples CS406/534 Fall 2004, Prof. Li Xu 17 Analysis

Instruction selection Mapping IR into assembly code The Back-end Assumes a fixed storage mapping & code shape Combining operations, using address modes Instruction scheduling Reordering operations to hide latencies Assumes a fixed program (set of operations) Changes demand for registers Register allocation Deciding which values will reside in registers Changes the storage mapping, may add false sharing Concerns about placement of data & memory operations CS406/534 Fall 2004, Prof. Li Xu 18 18

Expressions Code Generation Recursive tree walk on AST Direct integration with parser Assignment Array reference Boolean & Relational Values If-then-else Case Loop Procedure call CS406/534 Fall 2004, Prof. Li Xu 19 19

Instruction Selection Hand-coded tree-walk code generator Automatic instruction selection Pattern matching Peephole Matching Tree-pattern matching through tiling CS406/534 Fall 2004, Prof. Li Xu 20 20

The Problem Instruction Scheduling Given a code fragment for some target machine and the latencies for each individual operation, reorder the operations to minimize execution time Build Precedence Graph List scheduling NP-complete problem Heuristics work well for basic blocks forward list scheduling backward list scheduling Scheduling for larger regions EBB and cloning Trace scheduling CS406/534 Fall 2004, Prof. Li Xu 21 21

Register Allocation Local register allocation top-down bottom-up Global register allocation Find live-range Build an interference graph G I Construct a k-coloring of interference graph Map colors onto physical registers CS406/534 Fall 2004, Prof. Li Xu 22 22

Web-based Live Ranges Connect common defs and uses Solve the Reaching data-flow problem! l1 def x def y def y def x use y l4 use x use y l3 use x def x l2 use x CS406/534 Fall 2004, Prof. Li Xu 23 23

Interference Graph The interference graph, G I Nodes in G I represent live ranges Edges in G I represent individual interferences For x, y G I, <x,y> iff x and y interfere A k-coloring of G I can be mapped into an allocation to k registers 3-colorable CS406/534 Fall 2004, Prof. Li Xu 24 24

Key Observation on Coloring Any vertex n that has fewer than k neighbors in the interference graph (n < k) can always be colored! Remove nodes n < k for G I, coloring for G I is also coloring for G I. CS406/534 Fall 2004, Prof. Li Xu 25 25

Chaitin s Algorithm 1. While vertices with < k neighbors in G I > Pick any vertex n such that n < k and put it on the stack > Remove that vertex and all edges incident to it from G I This will lower the degree of n s neighbors 2. If G I is non-empty (all vertices have k or more neighbors) then: > Pick a vertex n (using some heuristic) and spill the live range associated with n > Remove vertex n from G I, along with all edges incident to it and put it on the stack > If this causes some vertex in G I to have fewer than k neighbors, then go to step 1; otherwise, repeat step 2 3. If no spill, successively pop vertices off the stack and color them in the lowest color not used by some neighbor; otherwise, insert spill code, recompute G I and start from step 1 CS406/534 Fall 2004, Prof. Li Xu 26 26

Brigg s Improvement Nodes can still be colored even with > k neighbors if some neighbors have same color 1. While vertices with < k neighbors in G I > Pick any vertex n such that n < k and put it on the stack > Remove that vertex and all edges incident to it from G I This may create vertices with fewer than k neighbors 2. If G I is non-empty (all vertices have k or more neighbors) then: > Pick a vertex n (using some heuristic condition), push n on the stack and remove n from G I, along with all edges incident to it > If this causes some vertex in G I to have fewer than k neighbors, then go to step 1; otherwise, repeat step 2 3. Successively pop vertices off the stack and color them in the lowest color not used by some neighbor > If some vertex cannot be colored, then pick an uncolored vertex to spill, spill it, and restart at step 1 CS406/534 Fall 2004, Prof. Li Xu 27 27

The Middle-end: Optimizer Front Middle End Back End 28 Register Allocation Instruction Scheduling Instruction Selection IR Optimization n Optimization 2 Optimization 1 IR CSA Parser Scanner Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples CS406/534 Fall 2004, Prof. Li Xu 28 Analysis

Principles of Compiler Optimization safety Does applying the transformation change the results of executing the code? profitability Is there a reasonable expectation that applying the transformation will improve the code? opportunity Can we efficiently and frequently find places to apply optimization Optimizing compiler Program Analysis Program Transformation CS406/534 Fall 2004, Prof. Li Xu 29 29

Control-flow analysis Data-flow analysis Program Analysis CS406/534 Fall 2004, Prof. Li Xu 30 30

Control Flow Analysis Basic blocks Control flow graph Dominator tree Natural loops Dominance frontier the join points for SSA insert Ф node CFG DOM Tree CS406/534 Fall 2004, Prof. Li Xu 31 31

Data Flow Analysis compile-time reasoning about the runtime flow of values represent effects of each basic block propagate facts around control flow graph CS406/534 Fall 2004, Prof. Li Xu 32 32

DFA: The Big Picture Set up a set of equations that relate program properties at different program points in terms of the properties at "nearby" program points B IN(B) local(b) OUT(B) Transfer function Forward analysis: compute OUT(B) in terms IN(B) Available expressions Reaching definition Backward analysis: compute IN(B) in terms of OUT(B) Variable liveness Very busy expressions Meet function for join points Forward analysis: combine OUT(p) of predecessors to form IN(B) Backward analysis: combine IN(s) of successors to form OUT(B) CS406/534 Fall 2004, Prof. Li Xu 33 33

Available Expression Basic block b IN(b): expressions available at b s entry OUT(b): expressions available at b s exit Local sets : expressions defined in b and available on exit : expressions killed in b An expression is killed in b if operands are assigned in b Transfer function Meet function CS406/534 Fall 2004, Prof. Li Xu 34 34

More Data Flow Problems AVAIL Equations More data flow problems Reaching Definition Liveness CS406/534 Fall 2004, Prof. Li Xu 35 35

Compiler Optimization Local optimization DAG CSE Value numbering Global optimization enabled by DFA Global CSE (AVAIL) Constant propagation (Def-Use) Dead code elimination (Use-Def) Advanced topic: SSA CS406/534 Fall 2004, Prof. Li Xu 36 36

Perspective Front End Middle End Back End Scanner Parser CSA IR Optimization 1 Optimization 2 Analysis Optimization n IR Instruction Selection Instruction Scheduling Register Allocation Infrastructure: symbol tables, trees, graphs, intermediate representations, sets, tuples Front end: essentially solved problem Middle end: domain-specific language Back end: new architecture Verifying compiler, reliability, security CS406/534 Fall 2004, Prof. Li Xu 37 37

Interesting Stuff We Skipped Interprocedural analysis Alias (pointer) analysis Garbage collection Check the literature reference in EaC CS406/534 Fall 2004, Prof. Li Xu 38 38

How will you use 91.406/534 knowledge? As informed programmer As informed small language designer As informed hardware engineer As compiler writer CS406/534 Fall 2004, Prof. Li Xu 39 39

Informed Programmer Knowledge is power Compiler is no longer a black box Know how compiler works Implications Use of language features Avoid those can cause problem Give compiler hints Code optimization Don t optimize prematurely Don t write complicated code Debugging Understand the compiled code CS406/534 Fall 2004, Prof. Li Xu 40 40

Solving Problem the Compiler Way Solve problems from language/compiler perspective Implement simple language Extend language CS406/534 Fall 2004, Prof. Li Xu 41 41

Informed Hardware Engineer Compiler support for programmable hardware pervasive computing new back-ends for new processors Design new architectures what can compiler do and not do how to expose and use compiler to manage hardware resources CS406/534 Fall 2004, Prof. Li Xu 42 42

Compiler Writer Make a living by writing compilers! Theory Algorithms Engineering We have built: scanner parser AST tree builder, type checker register allocator instruction scheduler Used compiler generation tools ANTLR, lex, yacc, etc On track to jump into compiler development! CS406/534 Fall 2004, Prof. Li Xu 43 43

Compiler construction Theory Implementation Final Remarks How to use what you learned in 91.406/534? As informed programmer As informed small language designer As informed hardware engineer As compiler writer and live happily ever after CS406/534 Fall 2004, Prof. Li Xu 44 44

CS406/534 Fall 2004, Prof. Li Xu 45 45