Introduction to Compilers

Similar documents
PLAGIARISM. Administrivia. Compilers. CS143 11:00-12:15TT B03 Gates. Text. Staff. Instructor. TAs. Office hours, contact info on 143 web site

Introduction. Compilers and Interpreters

Administrivia. Compilers. CS143 10:30-11:50TT Gates B01. Text. Staff. Instructor. TAs. The Purple Dragon Book. Alex Aiken. Aho, Lam, Sethi & Ullman

Introduction to Programming Languages and Compilers. CS164 11:00-12:30 TT 10 Evans. UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164)

Compiler Design 1. Introduction to Programming Language Design and to Compilation

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

1DL321: Kompilatorteknik I (Compiler Design 1)

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

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

PLAGIARISM. Administrivia. Course home page: Introduction to Programming Languages and Compilers

Introduction to Programming Languages and Compilers. CS164 11:00-12:00 MWF 306 Soda

CS 132 Compiler Construction

Programming Language Design and Implementation. Cunning Plan. Your Host For The Semester. Wes Weimer TR 9:30-10:45 MEC 214. Who Are We?

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compilers. Lecture 2 Overview. (original slides by Sam

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

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

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Compilers and Interpreters

Compiler Construction

Compilers Crash Course

Overview of a Compiler

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

ICOM 4036 Spring 2004

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

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Goals of Program Optimization (1 of 2)

Compiler Code Generation COMP360

The View from 35,000 Feet

Life Cycle of Source Program - Compiler Design

Overview of a Compiler

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

CST-402(T): Language Processors

Question Bank. 10CS63:Compiler Design

SOFTWARE ARCHITECTURE 5. COMPILER

Overview of a Compiler

Optimization Prof. James L. Frankel Harvard University

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

CS 352: Compilers: Principles and Practice

CS 4201 Compilers 2014/2015 Handout: Lab 1

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

Code Optimization. Code Optimization

CS131: Programming Languages and Compilers. Spring 2017

Optimizing Compilers. Vineeth Kashyap Department of Computer Science, UCSB. SIAM Algorithms Seminar, 2014

A simple syntax-directed

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

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

Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

Compiler Construction 1. Introduction. Oscar Nierstrasz

Undergraduate Compilers in a Day

Syntax. In Text: Chapter 3

Chapter 2 A Quick Tour

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

CPS 506 Comparative Programming Languages. Syntax Specification

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

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

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

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

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

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

Pioneering Compiler Design

Final Exam Review Questions

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

COMPILER DESIGN. For COMPUTER SCIENCE

CSE 3302 Programming Languages Lecture 2: Syntax

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

CS152 Programming Language Paradigms Prof. Tom Austin, Fall Syntax & Semantics, and Language Design Criteria

Compiler Optimization

CS5363 Final Review. cs5363 1

Syntax. A. Bellaachia Page: 1

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

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

Theory and Compiling COMP360

Compiler Construction 1. Introduction. Oscar Nierstrasz

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

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Compiler Design (40-414)

CMPT 379 Compilers. Anoop Sarkar.

Appendix A The DL Language

VIVA QUESTIONS WITH ANSWERS

Software Lesson 2 Outline

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

A Simple Syntax-Directed Translator

Introduction to Compiler Construction

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

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

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

COMPILER DESIGN LECTURE NOTES

Programming Language Processor Theory

COSE312: Compilers. Lecture 1 Overview of Compilers

Compilers and Code Optimization EDOARDO FUSELLA

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

Compiler, Assembler, and Linker

Many of the following slides are taken with permission from. The book is used explicitly in CS 2505 and CS 3214 and as a reference in CS 2506.

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Introduction to Compiler Construction

Transcription:

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 C executable Interpreters Program Data online I Output Output 2 History of Compilers History of Compilers From Machine Code to High-Level Programming Languages Grace Hopper (1906 1992) coined the term compiler 192: wrote the first compiler A for the A-0 language B-0, ARITH-MATIC, MATH-MATHIC, FLOW-MATIC, COBOL to Assembly Code even for the earliest machines (UNIVAC, IBM 701/704), the cost of software exceeded the hardware cost John Backus (1924 2007) 19: Speedcoding interpreted: code ran 10-20x slower interpreter used up 10 memory words 19: FORTRAN I FORmula TRANslator translated high-level code to assembly [ Backus-Naur Form (BNF) ] 4 FORTRAN I 194-7: FORTRAN I project compiler released in 197 first successful high-level programming language 198 more than 0% of all software is written in FORTRAN huge impact on computer science led to an enormous body of theoretical work even modern compilers preserve the outlines of the FORTRAN I compiler The Structure of a Compiler 6 1

The Bigger Picture int printf(); int main() int A[1000]; a[i] = a[i] 1; printf( %d, a[i]); source code Preprocessor modified source code Compiler assembly code #include <stdio.h> #define N 1000 int main() int A[N]; a[i] = a[i] 1; printf( %d, a[i]); movl $a, %eax addl (%eax, %ebx, 4), %ecx call printf Typical Phases of a Modern Compiler character stream Lexical Analyzer token stream Syntax Analyzer syntax tree intermediate representation Machine-Independent Code Optimizer intermediate representation Code Generator Symbol Table Assembler Semantic Analyzer 6: e8 fc ff ff ff call 7<main0x14> 7: R_86_PC2 printf relocatable machine code syntax tree Machine-Dependent Code Optimizer Linker/Loader Intermediate Code Generator 8048ba: e8 09 00 00 00 call 8048c8 7 8 Basic Structure of a Compiler Basic Structure of a Compiler 1. 2.. 4.. Code Generation or Scanning recognize the words in the input This is an example sentence. This, is, an, example, sentence,. acknowledgements: contains adapted material from Alex Aiken 9 10 or Scanning recognize the words in the input Not as trivial as it looks. Consider: thisi sane xam plesent ence. input: character stream of source program process: split stream into lexemes according to the grammar of the language build attributed tokens output: token stream a token contains the token name (id) and the token attributes < token name, attribute values > 11 12 2

Example: or Parsing position = initial rate * 60 1. lexeme position token <id, 1> 2. lexeme = token. lexeme initial token <id, 2> 4. lexeme token <>. lexeme rate token <id, > 6. lexeme * token <*> 7. lexeme 60 token <number, 60> understand the structure of the input This example is a longer sentence article noun verb article adjective noun subject object <id,1> <id,2> <> <id,> <*> <number,60> sentence 1 14 input: token stream process: transform token stream into a syntax (parse) tree grammar of the language by starting at the start symbol and repeatedly applying productions output: syntax tree, symbol table Example position = initial rate * 60 <id,1> <id,2> <> <id,> <*> <number,60> expr expr term expr term term term term * term / ( expr ) expr expr term <id,1> <> <id,2> <*> <, >, < >, <, >, < * >, <, 7 > term term * <id,> <number, 60> 7 1 16 Another Example position = rate * 60 initial <id,1> <id,2> <*> <number,60> <> <id, > vs <id,1> <*> <id,1> <> understand the meaning of the input this is too hard for compilers semantic analysis is restricted to detecting inconsistencies Tom said Jerry left his phone at home. whose phone? <id,2> <> <number,60> <id,> which syntax tree is correct? <*> <id,> <id,2> <number,60> Tom said Tom left his phone at home. how many people are we talking about? 17 18

compilers catch inconsistencies input: syntax tree, symbol table process: semantic consistency checking, type checking output: (semantically sound) syntax tree, possibly added coercions Tom left her phone at home. type mismatch position = initial rate * 60 1 position float = 2 initial float rate float <id, 1> <id, 1> = <id, 2> * <id, > 60 <id, 2> * <id, > inttofloat 60 19 20 Examples type checking public static void fun(int a, java.lang.string[] s) int sum = a s; no strong counterpart in English; similar to editing. is a little bit like editing. variable bindings int i = 4; int i = 6; printf( %d\n, i); is akin to editing. here: reduce number of words. 21 22 Common (High-Level) s automatically modify programs so that they use less of some resource. run faster use less memory use less (disk) space consume less energy reduce number of network accesses common subexpression elimination dead code elimination code motion constant propagation partial-redundancy elimination loop optimizations 2 24 4

Code Generation Example Code Generation or CodeGen x = y * 0 can be optimized to x = 0 translation into another language, e.g., English into Korean unfortunately, this rule is not correct. s should to be conservative, i.e., be correct under all circumstances. I miss you 보고싶다 2 26 Code Generation Proportions of the Compiler Phases More formally input: intermediate code process: map to machine code register allocation memory locations FORTRAN modern compilers L P S O CG L P S O CG t1 = id * 60.0 id1 = id2 t1 ldf r2, id mulf r2, r2, #60.0 ldf r, id2 addf r, r, r2 stf id1, r 27 28 s in a Modern Compiler Summary Scalar replacement of array references Data-cache optimizations Procedure integration Tail-call optimization, including tail-recursion elimination Scalar replacement of aggregates Sparse conditional constant propagation Interprocedural constant propagation Procedure specialization and cloning Sparse conditional constant propagation Global value numbering Local and global copy propagation Constant Sparse conditional constant propagation folding Local and global CSE Partial redundancy elimination Loop-invariant code motion Algebraic simplifications, including reassociation In-line expansion Leaf-routine optimization Shrink wrapping Machine idioms Tail merging Branch optimizations and conditional moves Software pipelining with loop unrolling, variable expansion, register renaming, and hierarchical reduction Basic-block and branch scheduling Register allocation by graph coloring Basic-block and branch scheduling Intraprocedural I-cache optimization Instruction prefetching Data prefetching Branch prediction Compilers are fundamental to Computer Science Basic Structure of a Compiler 1. from a character stream to tokens 2. from tokens to an AST. type checking 4. varying degree of complexity. Code Generation from IR to machine code Code hoisting Induction-variable strength reduction Linear-function test replacement Induction-variable removal Unnecessary bounds-checking removal Control-flow optimizations Interprocedural register allocation Aggregation of global references Interprocedural I-cache optimization This class covers all phases except optimizations We will build a simple but fully functional compiler for a Pascal-based language source: Steven Muchnick, Advanced Compiler Design & Implementation 29 0