Lecture Compiler Middle-End

Size: px
Start display at page:

Download "Lecture Compiler Middle-End"

Transcription

1 Lecture Compiler Middle-End Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu P. 1

2 What We Have Done A lot! Compiler Frontend Defining language Generating scanner and parser Generating parse tree (AST) Performing semantic analysis Type analysis Jianwen Zhu P. 2

3 Compiler Middle End Intermediate Representation (IR) Language-independent data structure to capture program Code generation From AST to IR Data flow analysis Infer program info from IR Optimization Code improvement IR->IR Covered by another course Jianwen Zhu P. 3

4 Intermediate Representation Jianwen Zhu P. 4

5 A Problem Modern compiler handles multiple languages gcc: GNU C Compiler gcc: GNU Compiler collection Assuming M languages + N processors Engineering effort for each lang/proc pair M * N effort Jianwen Zhu P. 5

6 A Solution M + N effort! X86 Lang 1 Tree 1 MIPS Lang 2 Tree 2 IR ARM Lang 3 Tree 3 AVR Intermediate Representation (IR) Jianwen Zhu P. 6

7 TinyC Program state defined by a set of variables Actions defined by statements Simplifying assumptions All statements are in a single procedure, and there are no procedure calls Only support int and bool primitive types Only support one-dimensional array No pointers are supported Jianwen Zhu P. 7

8 TinyC Example: Dot Product int A[100], B[100]; int sum, i; sum = i = 0; while( i < 100 ) { sum = sum + A[i] *B[i]; i = i + 1; } Jianwen Zhu P. 8

9 TinyC Syntax Defined by a set of production rules in Backus-Naur Form (BNF) Key constructs Declarations: define scalars or array variables Statements: assignment or control flow statements Expressions: transformations of scalar, defined primitive or program variable values Jianwen Zhu P. 9

10 TinyC in One Page program: declaration* statement* statement: variable '=' expression ';', 'if' '(' expression ') ' statement ( 'else' statement )*, 'while' ' (' expression ') ' statement, 'break' '; ', '{' declaration* statement* '}' declaration: type identifier ['=' expression] '; ', type identifier '[' expression ']' '; ' type: 'int', 'bool' expression: '-' expression, '!' expression, expression '+' expression, expression '-' expression, expression '*' expression, expression '/' expression, expression '^' expression, expression '>>' expression, expression '<<' expression, expression '&' expression, expression ' ' expression, expression '=' expression, expression '!=' expression, expression '<' expression, expression '<=' expression, expression '>' expression, expression '>=' expression, '(' expression ')', integer, identifier, 'TRUE', 'FALSE', identifier '[' expression ']' Jianwen Zhu P. 10

11 Notations A data type T corresponds to a set T, in particular the integer type Z corresponds to set Z A linked list or arrays whose elements are of type T corresponds to the power set of T, or the set of all subsets of T, denoted as T[] A record with fields a of type A, and b of type B corresponds to a set of named tuples, denoted as <a: A, b: B> A graph R whose nodes are of type A corresponds to a relation R: A x A A hash table or dictionary F that maps a value of type A to a value of type B corresponds to a function F: A --> B Jianwen Zhu P. 11

12 TinyIR Why IR? Decouple optimization algorithms from input languages and target architectures Definition: A TinyIR is a tuple <O, S,V, B> with the following elements: A set O = {lds, sts, lda, sta, ba, br, cnst, +, -, *, /, <<, >>, } of operation codes, which corresponds to the set of all virtual instruction types. A set S of symbols, which corresponds to the scalar and array variables A set V: <opcode: O, src1: V, src2: V, symb: S B Z> of virtual instructions, which corresponds to the expressions and control transfers in the program. A set B: V[] of basic blocks, each containing a sequence of virtual instructions Jianwen Zhu P. 12

13 From TinyC to TinyIR Constructs in TinyC have equivalent representation in TinyIR Declarations correspond to symbols Statements and expressions correspond to virtual instructions Virtual instructions are grouped within different basic blocks Jianwen Zhu P. 13

14 Dot product in TinyIR scalar sum; scalar i; array A[100]; array B[100]; B1: (0) cnst 0 (1) sts (0), sum (2) sts (0), i B2: (3) lds i (4) lda (3), A (5) lda (3), B (6) * (4) (5) (7) lds sum (8) + (6) (7) (9) sts (8), sum (10) cnst 1 (11) + (3) (10) (12) sts (11), i (13) cnst 100 (14) < (11) (13) (15) bt (14), B2 Jianwen Zhu P. 14

15 Code Generation Jianwen Zhu P. 15

16 Assignment Statment sum = 0 (0) cnst 0 (1) sts (0), sum RHS expression Store instruction Symbol for scalar variable Jianwen Zhu P. 16

17 If Statement if( c ) { stmt1; } else { stmt2; } Condition evaluation Branch instruction Fall-through else branch (10) c (11) bt L1 (12) stmt2 (20) ba L2 L1: (30) stmt1 Jump to merge point L2: Then branch Merge point Jianwen Zhu P. 17

18 While Statement (Layout 1) while( c ) { stmt; } Loop entry Condition Evaluation Loop body Loop back L1: (10)!c (11) bt L2 (12) stmt (20) ba L1 L2: Loop exit Jianwen Zhu P. 18

19 While Statement (Layout 2) while( c ) { stmt; } Loop entry Loop body Condition Evaluation Loop back (1) ba L3 L1: (10) stmt L3: (10) c (11) bt L1 L2: Loop exit Jianwen Zhu P. 19

20 Data Flow Analysis Jianwen Zhu P. 20

21 Data Flow Analysis A framework for proving facts about programs Reasons about lots of little facts Little or no interaction between facts Works best on properties about how program computes Based on all paths through program Including infeasible paths Jianwen Zhu P. 21

22 Available Expressions An expression e is available at program point p if e is computed on every path to p, and the value of e has not changed since the last time e is computed on Optimization If an expression is available, need not be recomputed (At least, if it s still in a register somewhere) Jianwen Zhu P. 22

23 Data Flow Facts Is expression e available? Facts: a + b is available a * b is available a + 1 is available Jianwen Zhu P. 23

24 Gen and Kill What is the effect of each statement on the set of facts? Stmt Gen Kill x := a + b a + b y := a * b a * b Jianwen Zhu P. 24

25 Computing Available Expressions {a + b} {a + b, a * b} {a + b, a * b} {a + b} {a + b} {a + b} Ø {a + b} Jianwen Zhu P. 25

26 Terminology A joint point is a program point where two branches meet Available expressions is a forward must problem Forward = Data flow from in to out Must = At join point, property must hold on all paths that are joined Jianwen Zhu P. 26

27 Data Flow Equations Let s be a statement succ(s) = { immediate successor statements of s } pred(s) = { immediate predecessor statements of s} In(s) = program point just before executing s Out(s) = program point just after executing s Jianwen Zhu P. 27

28 Liveness Analysis A variable v is live at program point p if v will be used on some execution path originating from p... before v is overwritten Optimization If a variable is not live, no need to keep it in a register If variable is dead at assignment, can eliminate assignment Jianwen Zhu P. 28

29 Data Flow Equations Available expressions is a forward must analysis Data flow propagate in same dir as CFG edges Expr is available only if available on all paths Liveness is a backward may problem To know if variable live, need to look at future uses Variable is live if used on some path Out(s) = s succ(s) In(s ) In(s) = Gen(s) (Out(s) - Kill(s)) Jianwen Zhu P. 29

30 Gen and Kill What is the effect of each statement on the set of facts? Stmt Gen Kill x := a + b a, b x y := a * b a, b y y > a a, y Jianwen Zhu P. 30

31 Computing Live Variables {a, b} {x, a, b} {x, {x, y, y, a, a} b} {y, a, b} {x} {y, a, b} {x, {x, y, y, a, a} b} Jianwen Zhu P. 31

32 Very Busy Expressions An expression e is very busy at point p if On every path from p, expression e is evaluated before the value of e is changed Optimization Can hoist very busy expression computation What kind of problem? Forward or backward? May or must? backward must Jianwen Zhu P. 32

33 Reaching Definitions A definition of a variable v is an assignment to v A definition of variable v reaches point p if There is no intervening assignment to v Also called def-use information What kind of problem? Forward or backward? May or must? forward may Jianwen Zhu P. 33

34 Space of Data Flow Analyses Forward Backward May Reaching definitions Live variables Must Available expressions Very busy expressions Most data flow analyses can be classified this way A few don t fit: bidirectional analysis Lots of literature on data flow analysis Jianwen Zhu P. 34

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts Compiler Structure Source Code Abstract Syntax Tree Control Flow Graph Object Code CMSC 631 Program Analysis and Understanding Fall 2003 Data Flow Analysis Source code parsed to produce AST AST transformed

More information

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

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions Agenda CS738: Advanced Compiler Optimizations Data Flow Analysis Amey Karkare karkare@cse.iitk.ac.in http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur Static analysis and compile-time

More information

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis Synthesis of output program (back-end) Intermediate Code Generation Optimization Before and after generating machine

More information

Data Flow Analysis. Program Analysis

Data Flow Analysis. Program Analysis Program Analysis https://www.cse.iitb.ac.in/~karkare/cs618/ Data Flow Analysis Amey Karkare Dept of Computer Science and Engg IIT Kanpur Visiting IIT Bombay karkare@cse.iitk.ac.in karkare@cse.iitb.ac.in

More information

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016 Name: Midterm 2 CMSC 430 Introduction to Compilers Fall 2016 November 21, 2016 Instructions This exam contains 7 pages, including this one. Make sure you have all the pages. Write your name on the top

More information

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack Formal Semantics Prof. Clarkson Fall 2015 Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack Review Previously in 3110: simple interpreter for expression language: abstract syntax

More information

CS 406/534 Compiler Construction Putting It All Together

CS 406/534 Compiler Construction Putting It All Together 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

More information

The Substitution Model

The Substitution Model The Substitution Model Prof. Clarkson Fall 2017 Today s music: Substitute by The Who Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on

More information

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Data-flow analysis These are techniques that derive information about the flow

More information

Lecture 3 Local Optimizations, Intro to SSA

Lecture 3 Local Optimizations, Intro to SSA Lecture 3 Local Optimizations, Intro to SSA I. Basic blocks & Flow graphs II. Abstraction 1: DAG III. Abstraction 2: Value numbering IV. Intro to SSA ALSU 8.4-8.5, 6.2.4 Phillip B. Gibbons 15-745: Local

More information

ELEC 876: Software Reengineering

ELEC 876: Software Reengineering ELEC 876: Software Reengineering () Dr. Ying Zou Department of Electrical & Computer Engineering Queen s University Compiler and Interpreter Compiler Source Code Object Compile Execute Code Results data

More information

More Dataflow Analysis

More Dataflow Analysis More Dataflow Analysis Steps to building analysis Step 1: Choose lattice Step 2: Choose direction of dataflow (forward or backward) Step 3: Create transfer function Step 4: Choose confluence operator (i.e.,

More information

Data-flow Analysis - Part 2

Data-flow Analysis - Part 2 - Part 2 Department of Computer Science Indian Institute of Science Bangalore 560 012 NPTEL Course on Compiler Design Data-flow analysis These are techniques that derive information about the flow of data

More information

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization Lecture Outline Global Optimization Global flow analysis Global constant propagation Liveness analysis Compiler Design I (2011) 2 Local Optimization Recall the simple basic-block optimizations Constant

More information

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1 Plan for Today Quiz 2 How to automate the process of performance optimization LLVM: Intro to Intermediate Representation Loops as iteration spaces Data-flow Analysis Intro Control-flow graph terminology

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Data Flow Analysis. CSCE Lecture 9-02/15/2018 Data Flow Analysis CSCE 747 - Lecture 9-02/15/2018 Data Flow Another view - program statements compute and transform data So, look at how that data is passed through the program. Reason about data dependence

More information

Compiler Optimization and Code Generation

Compiler Optimization and Code Generation Compiler Optimization and Code Generation Professor: Sc.D., Professor Vazgen Melikyan 1 Course Overview Introduction: Overview of Optimizations 1 lecture Intermediate-Code Generation 2 lectures Machine-Independent

More information

The Substitution Model. Nate Foster Spring 2018

The Substitution Model. Nate Foster Spring 2018 The Substitution Model Nate Foster Spring 2018 Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on single steps parser and lexer (in lab)

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters : Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Scanner Parser Static Analyzer Intermediate Representation Front End Back End Compiler / Interpreter

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Compiler Optimisation

Compiler Optimisation Compiler Optimisation 4 Dataflow Analysis Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018 Introduction This lecture:

More information

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz Compiler Design Fall 2015 Control-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

Lecture Compiler Backend

Lecture Compiler Backend Lecture 19-23 Compiler Backend Jianwen Zhu Electrical and Computer Engineering University of Toronto Jianwen Zhu 2009 - P. 1 Backend Tasks Instruction selection Map virtual instructions To machine instructions

More information

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

A main goal is to achieve a better performance. Code Optimization. Chapter 9

A main goal is to achieve a better performance. Code Optimization. Chapter 9 1 A main goal is to achieve a better performance Code Optimization Chapter 9 2 A main goal is to achieve a better performance source Code Front End Intermediate Code Code Gen target Code user Machineindependent

More information

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g., C++) to low-level assembly language that can be executed by hardware int a,

More information

Lecture 15 CIS 341: COMPILERS

Lecture 15 CIS 341: COMPILERS Lecture 15 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th No lecture on Thursday, March 22 Dr. Z will be away Zdancewic CIS 341: Compilers 2 Adding Integers

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

More information

Programming Language Processor Theory

Programming Language Processor Theory Programming Language Processor Theory Munehiro Takimoto Course Descriptions Method of Evaluation: made through your technical reports Purposes: understanding various theories and implementations of modern

More information

Lecture 2. Introduction to Data Flow Analysis

Lecture 2. Introduction to Data Flow Analysis Lecture 2 Introduction to Data Flow Analysis I II III Example: Reaching definition analysis Example: Liveness Analysis A General Framework (Theory in next lecture) Reading: Chapter 9.2 Advanced Compilers

More information

Intermediate representation

Intermediate representation Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing HIR semantic analysis HIR intermediate code gen.

More information

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR Pune Vidyarthi Griha s COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR By Prof. Anand N. Gharu (Assistant Professor) PVGCOE Computer Dept.. 22nd Jan 2018 CONTENTS :- 1. Role of lexical analysis 2.

More information

CS553 Lecture Generalizing Data-flow Analysis 3

CS553 Lecture Generalizing Data-flow Analysis 3 Generalizing Data-flow Analysis Announcements Project 2 writeup is available Read Stephenson paper Last Time Control-flow analysis Today C-Breeze Introduction Other types of data-flow analysis Reaching

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language

More information

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

Register Allocation. CS 502 Lecture 14 11/25/08 Register Allocation CS 502 Lecture 14 11/25/08 Where we are... Reasonably low-level intermediate representation: sequence of simple instructions followed by a transfer of control. a representation of static

More information

Homework Assignment #1 Sample Solutions

Homework Assignment #1 Sample Solutions Homework Assignment # Sample Solutions Due Monday /, at the start of lecture. For the following control flow graph, for the purposes of a forward data flow analysis (such as available expressions), show

More information

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment ECE 5775 (Fall 17) High-Level Digital Design Automation Static Single Assignment Announcements HW 1 released (due Friday) Student-led discussions on Tuesday 9/26 Sign up on Piazza: 3 students / group Meet

More information

CS 132 Compiler Construction

CS 132 Compiler Construction CS 132 Compiler Construction 1. Introduction 2 2. Lexical analysis 31 3. LL parsing 58 4. LR parsing 110 5. JavaCC and JTB 127 6. Semantic analysis 150 7. Translation and simplification 165 8. Liveness

More information

Principles of Programming Languages COMP251: Syntax and Grammars

Principles of Programming Languages COMP251: Syntax and Grammars Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2006

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 5 out Due Tuesday Nov 13 (14 days) Project 6 out Due

More information

DEMO A Language for Practice Implementation Comp 506, Spring 2018

DEMO A Language for Practice Implementation Comp 506, Spring 2018 DEMO A Language for Practice Implementation Comp 506, Spring 2018 1 Purpose This document describes the Demo programming language. Demo was invented for instructional purposes; it has no real use aside

More information

Compilers. Lecture 2 Overview. (original slides by Sam

Compilers. Lecture 2 Overview. (original slides by Sam Compilers Lecture 2 Overview Yannis Smaragdakis, U. Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Last time The compilation problem Source language High-level abstractions Easy

More information

Live Variable Analysis. Work List Iterative Algorithm Rehashed

Live Variable Analysis. Work List Iterative Algorithm Rehashed Putting Data Flow Analysis to Work Last Time Iterative Worklist Algorithm via Reaching Definitions Why it terminates. What it computes. Why it works. How fast it goes. Today Live Variable Analysis (backward

More information

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee 1 What Is a Programming Language? Language = syntax + semantics The syntax of a language is concerned with the form of a program: how

More information

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

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto Data Structures and Algorithms in Compiler Optimization Comp314 Lecture Dave Peixotto 1 What is a compiler Compilers translate between program representations Interpreters evaluate their input to produce

More information

CS 314 Principles of Programming Languages. Lecture 3

CS 314 Principles of Programming Languages. Lecture 3 CS 314 Principles of Programming Languages Lecture 3 Zheng Zhang Department of Computer Science Rutgers University Wednesday 14 th September, 2016 Zheng Zhang 1 CS@Rutgers University Class Information

More information

Lecture Notes on Intermediate Representation

Lecture Notes on Intermediate Representation Lecture Notes on Intermediate Representation 15-411: Compiler Design Frank Pfenning Lecture 10 September 26, 2013 1 Introduction In this lecture we discuss the middle end of the compiler. After the source

More information

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

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

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design PSD3A Principles of Compiler Design Unit : I-V 1 UNIT I - SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler -

More information

Writing Evaluators MIF08. Laure Gonnord

Writing Evaluators MIF08. Laure Gonnord Writing Evaluators MIF08 Laure Gonnord Laure.Gonnord@univ-lyon1.fr Evaluators, what for? Outline 1 Evaluators, what for? 2 Implementation Laure Gonnord (Lyon1/FST) Writing Evaluators 2 / 21 Evaluators,

More information

Compiler Optimisation

Compiler Optimisation Compiler Optimisation 3 Dataflow Analysis Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018 Introduction Optimisations

More information

Tour of common optimizations

Tour of common optimizations Tour of common optimizations Simple example foo(z) { x := 3 + 6; y := x 5 return z * y } Simple example foo(z) { x := 3 + 6; y := x 5; return z * y } x:=9; Applying Constant Folding Simple example foo(z)

More information

Declarative Intraprocedural Flow Analysis of Java Source Code

Declarative Intraprocedural Flow Analysis of Java Source Code Declarative Intraprocedural Flow Analysis of Java Source Code Emma Nilsson-Nyman Torbjörn Ekman Görel Hedin Eva Magnusson Dept of Computer Science Lund University Programming Tools Group University of

More information

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter

More information

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1 An Overview to Compiler Design 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1 Outline An Overview of Compiler Structure Front End Middle End Back End 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 2 Reading

More information

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1 Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1 1. Introduction Parsing is the task of Syntax Analysis Determining the syntax, or structure, of a program. The syntax is defined by the grammar rules

More information

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax

More information

CSE302: Compiler Design

CSE302: Compiler Design CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University January 30, 2007 Outline Recap

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

Code Genera*on for Control Flow Constructs

Code Genera*on for Control Flow Constructs Code Genera*on for Control Flow Constructs 1 Roadmap Last *me: Got the basics of MIPS CodeGen for some AST node types This *me: Do the rest of the AST nodes Introduce control flow graphs Scanner Parser

More information

Context-free grammars (CFG s)

Context-free grammars (CFG s) Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of

More information

Principles of Programming Languages COMP251: Syntax and Grammars

Principles of Programming Languages COMP251: Syntax and Grammars Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2007

More information

Advanced C Programming

Advanced C Programming Advanced C Programming Compilers Sebastian Hack hack@cs.uni-sb.de Christoph Weidenbach weidenbach@mpi-inf.mpg.de 20.01.2009 saarland university computer science 1 Contents Overview Optimizations Program

More information

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code Traditional Three-pass Compiler Source Code Front End IR Middle End IR Back End Machine code Errors Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce

More information

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

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002 Outline 5 Fall 2002 Lecture 17: Putting it all together From parsing to code generation Saman Amarasinghe 2 6.035 MIT Fall 1998 How to make the computer understand? Write a program using a programming

More information

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall INTRODUCTION TO LLVM Bo Wang SA 2016 Fall LLVM Basic LLVM IR LLVM Pass OUTLINE What is LLVM? LLVM is a compiler infrastructure designed as a set of reusable libraries with well-defined interfaces. Implemented

More information

Compiler Construction 2016/2017 Loop Optimizations

Compiler Construction 2016/2017 Loop Optimizations Compiler Construction 2016/2017 Loop Optimizations Peter Thiemann January 16, 2017 Outline 1 Loops 2 Dominators 3 Loop-Invariant Computations 4 Induction Variables 5 Array-Bounds Checks 6 Loop Unrolling

More information

Syntax and Grammars 1 / 21

Syntax and Grammars 1 / 21 Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?

More information

CS 314 Principles of Programming Languages. Lecture 9

CS 314 Principles of Programming Languages. Lecture 9 CS 314 Principles of Programming Languages Lecture 9 Zheng Zhang Department of Computer Science Rutgers University Wednesday 5 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Homework

More information

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology MIT 6.035 Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Program Analysis Compile-time reasoning about run-time behavior

More information

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573 What is a compiler? Xiaokang Qiu Purdue University ECE 573 August 21, 2017 What is a compiler? What is a compiler? Traditionally: Program that analyzes and translates from a high level language (e.g.,

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings

More information

Optimizing Finite Automata

Optimizing Finite Automata Optimizing Finite Automata We can improve the DFA created by MakeDeterministic. Sometimes a DFA will have more states than necessary. For every DFA there is a unique smallest equivalent DFA (fewest states

More information

OptiCode: Machine Code Deobfuscation for Malware Analysis

OptiCode: Machine Code Deobfuscation for Malware Analysis OptiCode: Machine Code Deobfuscation for Malware Analysis NGUYEN Anh Quynh, COSEINC CONFidence, Krakow - Poland 2013, May 28th 1 / 47 Agenda 1 Obfuscation problem in malware analysis

More information

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis CSE450 Translation of Programming Languages Lecture 4: Syntax Analysis http://xkcd.com/859 Structure of a Today! Compiler Source Language Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator

More information

Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation. Lecture 14 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,

More information

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM Flow Analysis Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM Helpful Reading: Sections 1.1-1.5, 2.1 Data-flow analysis (DFA) A framework for statically proving facts about program

More information

Introduction to Compilers

Introduction to Compilers Introduction to Compilers Compilers are language translators input: program in one language output: equivalent program in another language Introduction to Compilers Two types Compilers offline Data Program

More information

Foundations: Syntax, Semantics, and Graphs

Foundations: Syntax, Semantics, and Graphs Foundations: Syntax, Semantics, and Graphs Testing, Quality Assurance, and Maintenance Winter 2018 Prof. Arie Gurfinkel based on slides by Ruzica Pizkac, Claire Le Goues, Lin Tan, Marsha Chechik, and others

More information

Compiler Construction

Compiler Construction Compiler Construction Important facts: Name: Prof. Dr. Peter Thiemann Email: thiemann@informatik.uni-freiburg.de Office: 079-00-015 Exercises: Name: Dipl.-Inform. Manuel Geffken Email: geffken@informatik.uni-freiburg.de

More information

The View from 35,000 Feet

The View from 35,000 Feet The View from 35,000 Feet This lecture is taken directly from the Engineering a Compiler web site with only minor adaptations for EECS 6083 at University of Cincinnati Copyright 2003, Keith D. Cooper,

More information

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010 Last time Source code Scanner Tokens Parser What are compilers? Phases of a compiler Syntax tree Semantic Routines IR Optimizer IR Code Generation Executable Extra: Front-end vs. Back-end Scanner + Parser

More information

Lecture Notes on Intermediate Representation

Lecture Notes on Intermediate Representation Lecture Notes on Intermediate Representation 15-411: Compiler Design Frank Pfenning Lecture 10 1 Introduction In this lecture we discuss the middle end of the compiler. After the source has been parsed

More information

Control Flow Analysis

Control Flow Analysis Control Flow Analysis Last time Undergraduate compilers in a day Today Assignment 0 due Control-flow analysis Building basic blocks Building control-flow graphs Loops January 28, 2015 Control Flow Analysis

More information

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

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University CS415 Compilers Syntax Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Limits of Regular Languages Advantages of Regular Expressions

More information

Introduction to Lexing and Parsing

Introduction to Lexing and Parsing Introduction to Lexing and Parsing ECE 351: Compilers Jon Eyolfson University of Waterloo June 18, 2012 1 Riddle Me This, Riddle Me That What is a compiler? 1 Riddle Me This, Riddle Me That What is a compiler?

More information

Program Optimizations using Data-Flow Analysis

Program Optimizations using Data-Flow Analysis Program Optimizations using Data-Flow Analysis!Last time! Lattice theoretic framework for data-flow analysis!today! Dead-code elimination! Common sub-expression elimination (CSE)! Copy propagation! Constant

More information

Formats of Translated Programs

Formats of Translated Programs Formats of Translated Programs Compilers differ in the format of the target code they generate. Target formats may be categorized as assembly language, relocatable binary, or memory-image. Assembly Language

More information