Introduction to Compiler

Similar documents
High-level View of a Compiler

Compiling Techniques

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

The View from 35,000 Feet

Front End. Hwansoo Han

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

Overview of a Compiler

Compilers and Interpreters

Overview of a Compiler

Compiler Construction LECTURE # 1

Overview of a Compiler

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

CS 132 Compiler Construction

Compilers. Lecture 2 Overview. (original slides by Sam

Part 5 Program Analysis Principles and Techniques

Instruction Selection: Preliminaries. Comp 412

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

Lexical Analysis. Introduction

CS415 Compilers. Lexical Analysis

Introduction to Parsing

Compiler Construction

Compiler Design (40-414)

CS 352: Compilers: Principles and Practice

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Parsing II Top-down parsing. Comp 412

A simple syntax-directed

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

Instruction Selection and Scheduling

CS5363 Final Review. cs5363 1

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

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

Intermediate Representations

Formats of Translated Programs

CS415 Compilers. Intermediate Represeation & Code Generation

COSE312: Compilers. Lecture 1 Overview of Compilers

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

CA Compiler Construction

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

What do Compilers Produce?

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

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

Intermediate Representations

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

EECS 6083 Intro to Parsing Context Free Grammars

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

Context-Free Grammars

LECTURE 3. Compiler Phases

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

Instruction Selection, II Tree-pattern matching

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Compiling Regular Expressions COMP360

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

CS 406/534 Compiler Construction Putting It All Together

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

A programming language requires two major definitions A simple one pass compiler

CS606- compiler instruction Solved MCQS From Midterm Papers

Compiler Code Generation COMP360

CS 406/534 Compiler Construction Parsing Part I

Undergraduate Compilers in a Day

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

The Structure of a Syntax-Directed Compiler

A Simple Syntax-Directed Translator

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

Formal Languages and Compilers Lecture I: Introduction to Compilers

CMPT 379 Compilers. Parse trees

Homework & Announcements

The Structure of a Syntax-Directed Compiler

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CSE 3302 Programming Languages Lecture 2: Syntax

Compiler Optimisation

CPS 506 Comparative Programming Languages. Syntax Specification

Alternatives for semantic processing

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Introduction to Compiler Design

CSE302: Compiler Design

The Structure of a Compiler

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

Introduction. Compiler Design CSE Overview. 2 Syntax-Directed Translation. 3 Phases of Translation

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

CST-402(T): Language Processors

CSE 401/M501 Compilers

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

Downloaded from Page 1. LR Parsing

CS 314 Principles of Programming Languages

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

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

Optimizing Finite Automata

CSE 401 Midterm Exam Sample Solution 2/11/15

Class Information ANNOUCEMENTS

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

Compiler Design Overview. Compiler Design 1

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

Intermediate Representations

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

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

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

Transcription:

Formal Languages and Compiler (CSE322) Introduction to Compiler Jungsik Choi chjs@khu.ac.kr 2018. 3. 8

Traditional Two-pass Compiler Source Front End Back End Compiler Target High level functions Recognize legal program, generate correct code (OS & linker can accept) Manage the storage of all variables and code Two passes Use an intermediate representation () Front end maps legal source code into O(n) or O(n log n) Back end maps into target machine code typically NP-complete Admits multiple front ends & multiple passes (better code) 2

Front End Source Scanner tokens Parser Responsibilities Recognize legal (& illegal) programs Report errors in a useful way Produce & preliminary storage map Shape the code for the back end Much of front end construction can be automated 3

Front End - Scanner Source Scanner tokens Parser Scanner (Lexical Analysis) Maps character stream into words (basic units of syntax) sum=x+y; becomes <id,sum> <eq,=> <id,x> <add,+> <id,y> <sc,;> word lexeme, part of speech token type Typical tokens include number, identifier, keyword, operator Scanner eliminates white space and comments Produced by automatic scanner generator 4

Front End - Parser Source Scanner tokens Parser Parser Recognizes context-free syntax Guides context-sensitive ( semantic ) analysis E.g. type checking Builds for source program Produced by automatic parser generators 5

Front End Example (1) Context-free syntax can be put to better use 1. goal expr 2. expr expr op term 3. term 4. term number 5. id 6. op + 7. - S = goal T = {number, id, +, -} NT = {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 6

Front End Example (2) A parse can be represented by a tree (parse tree or syntax tree) x + 2 y expr expr op term term + <number,2> goal expr op term <id,y> 1. goal expr 2. expr expr op term 3. term 4. term number 5. id 6. op + 7. - <id,x> This contains a lot of unneeded information 7

Front End Example (3) Compilers often use an Abstract Syntax Tree (AST) AST summarizes grammatical structure, without including detail about the derivation + <id,y> <id,x> <number,2> This is much more concise ASTs are one kind of intermediate representation () 8

Back End Instruction Selection Instruction Scheduling Register Allocation Target Responsibilities Translate into target machined code Choose instructions to implement each operation Decide which values to keep in registers Find optimal order of instruction execution Ensure conformance with system interfaces Automation has been less successful in the back end 9

Back End Instruction Selection Instruction Selection Instruction Scheduling Register Allocation Target Instruction Selection Produce fast, compact code Take advantage of target features such as addressing modes Usually viewed as a pattern matching problem ad hoc methods, pattern matching, dynamic programming 10

Back End Instruction Scheduling Instruction Selection Instruction Scheduling Register Allocation Target Instruction Scheduling Avoid hardware stalls and interlocks Use all functional units productively Can increase lifetime of variables (changing the allocation) Optimal scheduling is NP-Complete in nearly all cases Heuristic techniques are well developed 11

Back End Register Allocation Instruction Selection Instruction Scheduling Register Allocation Target Register Allocation Have each value in a register when it is used Manage a limited set of resources Can change instruction choices & insert LOADs & STOREs Optimal allocation is NP-Complete Compilers approximate solutions to NP-Complete problems 12

Optimizing Compiler Source Front End Middle End Back End Target Compiler Code Optimizations Analyzes and rewrites (or transforms) Primary goal is to reduce Execution time, Space usage, Power consumption, Must preserve meaning of the code 13

Instruction Selection Example Simple Treewalk for initial code Peephole matching for desired code IDENT <a, ARP, 4> IDENT <b, ARP, 4> load 4 r 5 loadao r 0,r 5 r 6 loadi 8 r 7 loadao r 0,r 7 r 8 mult r 6,r 8 r 9 loadai r 0,4 r 5 loadai r 0,8 r 6 mult r 5,r 6 r 7 Tree Treewalk Code Desired Code 14

Instruction Scheduling Example Schedule Instructions considering Latency Dependences Generate fast executing code a a: loadai r 0,@w r 1 b: add r 1,r 1 r 1 c: loadai r 0,@x r 2 d: mult r 1,r 2 r 1 b d c The Code The Precedence Graph 15

Register Allocation Example Instruction selection assume infinite # of registers (virtual registers) Mapping virtual registers to physical registers Sometimes need register spill/fill code (only r 5 is used later) (r 5 is renamed with r 2 ) loadi 4 r 1 loadao r 0,r 1 r 2 loadi 8 r 3 loadao r 0,r 3 r 4 mult r 2,r 4 r 5 loadi 4 r 1 loadao r 0,r 1 r 1 loadi 8 r 2 loadao r 0,r 2 r 2 mult r 1,r 2 r 2 6 virtual registers 16 3 physical registers

Summary Source Front End Middle End Back End Target Compiler Front End: Process high-level programming language Middle End: Apply optimization for speed, power, space, Back End: Produce machine-level assembly code 17