1 Compiler Construction Overview. 2 Today’s Goals Summary of the subjects we’ve covered Perspectives and final remarks.

Slides:



Advertisements
Similar presentations
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Advertisements

P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Lecture 11: Code Optimization CS 540 George Mason University.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Program Representations. Representing programs Goals.
From AST to Code Generation Professor Yihjia Tsai Tamkang University.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Cpeg421-08S/final-review1 Course Review Tom St. John.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
Parsing V Introduction to LR(1) Parsers. from Cooper & Torczon2 LR(1) Parsers LR(1) parsers are table-driven, shift-reduce parsers that use a limited.
Compiler Summary Mooly Sagiv html://
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Compiler Construction
Intermediate Code. Local Optimizations
1 Liveness analysis and Register Allocation Cheng-Chia Chen.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
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.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Lexical and syntax analysis
Precision Going back to constant prop, in what cases would we lose precision?
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 10, 10/30/2003 Prof. Roy Levow.
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.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Review Joey Paquet,
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
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,
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction to Code Generation and Intermediate Representations
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
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
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.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
1 Compiler Construction Vana Doufexi office CS dept.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Review 1.Structure of the course Lexical Analysis Syntax Analysis Grammar & Language RG & DFA Top-down LL(1) Parsing Bottom-Up LR Layered Automation Semantic.
2016/7/9Page 1 Lecture 11: Semester Review COMP3100 Dept. Computer Science and Technology United International College.
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Introduction CI612 Compiler Design CI612 Compiler Design.
CPSC 388 – Compiler Design and Construction
Introduction to Code Generation
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Subject: Language Processor
Exam Topics Hal Perkins Autumn 2009
Data Flow Analysis Compiler Design
CS416 Compiler Design lec00-outline February 23, 2019
Static Single Assignment
Compiler Construction
Lecture 17: Register Allocation via Graph Colouring
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Exam Topics Hal Perkins Winter 2008
Presentation transcript:

1 Compiler Construction Overview

2 Today’s Goals Summary of the subjects we’ve covered Perspectives and final remarks

3 High-level View 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

4 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!

5 Structure of Compilers

6 The Front-end

7 Lexical Analysis Scanner 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

8 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 Operator precedence parsing Operator precedence parsing

9 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

10 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

11 Semantic Analysis Analyze context and semantics types and other semantic checks Attribute grammar associate evaluation rules with grammar production Ad-hoc build symbol table

12 Intermediate Representation

13 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

14 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

15 The Back-end

16 The Back-end Instruction selection Mapping IR into assembly code 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

17 Code Generation Expressions Recursive tree walk on AST Direct integration with parser Assignment Array reference Boolean & Relational Values If-then-else Case Loop Procedure call

18 Instruction Selection Hand-coded tree-walk code generator Automatic instruction selection Pattern matching Peephole Matching Tree-pattern matching through tiling

19 Instruction Scheduling The Problem 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

20 Register Allocation Local register allocation top-down bottom-up Global register allocation Find live-range Build an interference graph GI Construct a k-coloring of interference graph Map colors onto physical registers

21 Web-based Live Ranges Connect common defs and uses Solve the Reaching data-flow problem!

22 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, ∈ iff x and y interfere A k-coloring of G I can be mapped into an allocation to k registers

23 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

24 Chaitin’s Algorithm   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   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   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

25 Brigg’s Improvement Nodes can still be colored even with > k neighbors if some neighbors have same color   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   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   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

26 The Middle-end: Optimizer

27 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

28 Program Analysis Control-flow analysis Data-flow analysis

29 Control Flow Analysis Basic blocks Control flow graph Dominator tree Natural loops Dominance frontier the join points for SSA insert Ф node

30 Data Flow Analysis “compile-time reasoning about the runtime flow of values” represent effects of each basic block propagate facts around control flow graph

31 DFA: The Big Picture 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) Set up a set of equations that relate program properties at different program points in terms of the properties at "nearby" program points

32 Available Expression Basic block b IN(b): expressions available at b’s entry IN(b): expressions available at b’s entry OUT(b): expressiongs available at b’s exit OUT(b): expressiongs available at b’s exit Local sets Local sets def(b): expressions defined in b and available on exit def(b): expressions defined in b and available on exit killed(b): expressions killed in b killed(b): expressions killed in b An expression is killed in b if operands are assigned in b An expression is killed in b if operands are assigned in b Transfer function Transfer function OUT(b) = def(b) ∪ (IN(b) – killed(b)) OUT(b) = def(b) ∪ (IN(b) – killed(b)) Meet function Meet function IN(b) = IN(b) =

33 More Data Flow Problems AVAIL Equations AVAIL Equations More data flow problems More data flow problems Reaching Definition Reaching Definition Liveness Liveness meet function ∪∩ forward forward reaching reaching definition definition available available expression expression backward backward variable variable liveness liveness very busy very busy expression expression

34 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

35 Perspective Front end: essentially solved problem Middle end: domain-specific language Back end: new architecture Verifying compiler, reliability, security

36 Interesting Stuff We Skipped Interprocedural analysis Alias (pointer) analysis Garbage collection Check the literature reference in EaC

37 How will you use the knowledge? As informed programmer As informed small language designer As informed hardware engineer As compiler writer

38 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

39 Solving Problem the Compiler Way Solve problems from language/compiler perspective Implement simple language Extend language

40 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

41 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!

42 Final Remarks Compiler construction Theory Implementation How to use what you learned in this lecture? As informed programmer As informed small language designer As informed hardware engineer As compiler writer … and live happily ever after