Compiling Techniques

Similar documents
High-level View of a Compiler

The View from 35,000 Feet

Introduction to Compiler

Overview of a Compiler

Compilers and Interpreters

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

Overview of a Compiler

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

Overview of a Compiler

CS 132 Compiler Construction

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

Compiler Construction

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Front End. Hwansoo Han

CS 352: Compilers: Principles and Practice

Compiler Construction LECTURE # 1

Compilers. Lecture 2 Overview. (original slides by Sam

Goals for course What is a compiler and briefly how does it work? Review everything you should know from 330 and before

COSE312: Compilers. Lecture 1 Overview of Compilers

CA Compiler Construction

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CS 4201 Compilers 2014/2015 Handout: Lab 1

LECTURE 3. Compiler Phases

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical Analysis. Introduction

Part 5 Program Analysis Principles and Techniques

Compiler Construction 1. Introduction. Oscar Nierstrasz

Compiling Regular Expressions COMP360

Compilers and Code Optimization EDOARDO FUSELLA

What do Compilers Produce?

Compiler Construction 1. Introduction. Oscar Nierstrasz

Undergraduate Compilers in a Day

Formats of Translated Programs

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

A simple syntax-directed

Parsing II Top-down parsing. Comp 412

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Intermediate Representations

LECTURE NOTES ON COMPILER DESIGN P a g e 2

The Structure of a Syntax-Directed Compiler

Introduction to Parsing

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

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

CS606- compiler instruction Solved MCQS From Midterm Papers

CS415 Compilers. Lexical Analysis

CS 415 Midterm Exam Spring 2002

CST-402(T): Language Processors

3. Parsing. Oscar Nierstrasz

11. a b c d e. 12. a b c d e. 13. a b c d e. 14. a b c d e. 15. a b c d e

Introduction to Compiler Design

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Compiler Design (40-414)

1 Lexical Considerations

COMPILER DESIGN LECTURE NOTES

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

EECS 6083 Intro to Parsing Context Free Grammars

Intermediate Representations

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

When do We Run a Compiler?

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

Context-Free Grammars

Formal Languages and Compilers Lecture I: Introduction to Compilers

CS 406/534 Compiler Construction Putting It All Together

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Compilers and interpreters

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

Compiler Construction

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Introduction to Compiler Construction

IR Optimization. May 15th, Tuesday, May 14, 13

Register allocation. TDT4205 Lecture 31

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

Introduction to Compiler Construction

Time : 1 Hour Max Marks : 30

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

COP 3402 Systems Software Syntax Analysis (Parser)

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Introduction to Compiler Construction

Semantic Analysis. Lecture 9. February 7, 2018

Compiler construction

CS 314 Principles of Programming Languages

Downloaded from Page 1. LR Parsing

Optimizing Finite Automata

Compiler Code Generation COMP360

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

Lexical Scanning COMP360

Parsing and Pattern Recognition

CS5363 Final Review. cs5363 1

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Compiler Optimisation

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

A Simple Syntax-Directed Translator

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Transcription:

Lecture 2: The view from 35000 feet 19 September 2017

Table of contents 1 2 Passes Representations 3 Instruction Selection Register Allocation Instruction Scheduling 4

of a compiler Source Compiler Machine Must recognise legal (and illegal) programs Must generate correct Must manage storage of all variables (and ) Must agree with OS & linker on format for object Big step up from assembly language; use higher level notations

Traditional two-pass compiler Source FrontEnd BackEnd Machine Code Use an intermediate representation () Front end maps legal source into maps into target machine Admits multiple front ends & multiple passes Typically, front end is O(n) or O(n log n), while back end is NPC (NP-complete)

A common fallacy two-pass compiler Fortran Frontend R Frontend Backend Target 1 Java Frontend Backend Target 2 Backend Target 3 Smalltalk Frontend Can we build n x m compilers with n+m components? Must en all language specific knowledge in each front end Must en all features in a single Must en all target specific knowledge in each back end Limited success in systems with very low-level s (e.g. LLVM) Active research area (e.g. Graal, Truffle)

The Frontend Passes Representations Lexer Source Scanner char Tokeniser token Parser Semantic Analyser Generator Recognise legal (& illegal) programs Report errors in a useful way Produce & preliminary storage map Shape the for the back end Much of front end construction can be automated

The Lexer Passes Representations Lexer Source Scanner char Tokeniser token Parser Semantic Analyser Generator Lexical analysis Recognises words in a character stream Produces tokens (words) from lexeme Collect identifier information Typical tokens include number, identifier, +,, new, while, if Example: x=y+2; becomes IDENTIFIER(x) EQUAL IDENTIFIER(y) PLUS CST(2) Lexer eliminates white space (including comments)

The Parser Passes Representations Lexer Source Scanner char Tokeniser token Parser Semantic Analyser Generator Recognises context-free syntax & reports errors Hand-d parsers are fairly easy to build Most books advocate using automatic parser generators

Semantic Analyser Passes Representations Lexer Source Scanner char Tokeniser token Parser Semantic Analyser Generator Guides context-sensitive ( semantic ) analysis Checks variable and function declared before use Type checking

Generator Passes Representations Lexer Source Scanner char Tokeniser token Parser Semantic Analyser Generator Generates the used by the rest of the compiler. Sometimes the is the.

Simple Expression Grammar Passes Representations 1 g o a l e x p r 2 e x p r e x p r op term 3 term 4 term number 5 i d 6 op + 7 S = g o a l T = {number, id,+, } N = { goal, expr, term, op} P = { 1, 2, 3, 4, 5, 6, 7 } This grammar defines simple expressions with addition & subtraction over number and id This grammar, like many, falls in a class called context-free grammars, abbreviated CFG

Derivations Passes Representations Given a CFG, we can derive sentences by repeated substitution 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 To recognise a valid sentence in a CFG, we reverse this process and build up a parse tree

Parse tree Passes Representations x + 2 -y goal expr expr op term expr op term - id(y) term + num(2) id(x) This contains a lot of unnecessary information.

Abstract Syntax Tree () Passes Representations - + id(y) id(x) num(2) The summarises grammatical structure, without including detail about the derivation. Compilers often use an abstract syntax tree This is much more concise s are one kind of intermediate representation ()

The Instruction Selection Register Allocation Instruction Scheduling Instruction Selection Register Allocation Instruction Scheduling Machine Translate into target machine Choose instructions to implement each operation Decide which value to keep in registers Ensure conformance with system interfaces Automation has been less successful in the back end

Instruction Selection Instruction Selection Register Allocation Instruction Scheduling Instruction Selection Register Allocation Instruction Scheduling Machine Produce fast, compact Take advantage of target features such as addressing modes Usually viewed as a pattern matching problem ad hoc methods, pattern matching, dynamic programming Example: madd instruction

Register Allocation Instruction Selection Register Allocation Instruction Scheduling Instruction Selection Register Allocation Instruction Scheduling Machine Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOADs & STOREs (spilling) Optimal allocation is NP-Complete (1 or k registers) Graph colouring problem Compilers approximate solutions to NP-Complete problems

Instruction Scheduling Instruction Selection Register Allocation Instruction Scheduling Instruction Selection Register Allocation Instruction Scheduling Machine 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

Three Pass Compiler Source FrontEnd Middle End BackEnd Machine Code Code Improvement (or Optimisation) Analyses and rewrites (or transforms) Primary goal is to reduce running time of the compiled May also improve space, power consumption,... Must preserve meaning of the Measured by values of named variables Subject of UG4 Compiler Optimisation

The Modern optimisers are structured as a series of passes e.g. LLVM Opt 1 Opt 2... Opt N Discover & propagate some constant value Move a computation to a less frequently executed place Specialise some computation based on context Discover a redundant computation & remove it Remove useless or unreachable En an idiom in some particularly efficient form

Modern Restructuring Compiler Source FrontEnd HL LL Restructurer Generator Middle End BackEnd Machine Code Translate from high-level (HL) to low-level (LL) Blocking for memory hierarchy and register reuse Vectorisation Parallelisation All based on dependence Also full and partial inlining Not covered in this course

Role of the runtime 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 for parallelism (communication and synchronization)

Programs related to compilers Pre-processor: Produces input to the compiler Processes Macro/Directives (e.g. #define, #include) Assembler: Translate assembly language to actual machine (binary) Performs actual allocation of variables Linker: Links together various compiled files and/or libraries Generate a full program that can be loaded and executed Debugger: Tight integration with compiler Uses meta-information from compiler (e.g. variable names) Virtual Machines: Executes virtual assembly typically embedded a just-in-time (jit) compiler

Next lecture Introduction to Lexical Analysis Decomposition of the input into a stream of tokens Construction of scanners from regular expressions