Welcome to CSE131b: Compiler Construction

Similar documents
CS A331 Programming Language Concepts

Introduction. CS 2210 Compiler Design Wonsun Ahn

Undergraduate Compilers in a Day

Compiling Techniques

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

A simple syntax-directed

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

Usually, target code is semantically equivalent to source code, but not always!

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

Project Compiler. CS031 TA Help Session November 28, 2011

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

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

CSE4305: Compilers for Algorithmic Languages CSE5317: Design and Construction of Compilers

Overview of the Class

Important Project Dates

CSc 453. Code Generation I Christian Collberg. Compiler Phases. Code Generation Issues. Slide Compilers and Systems Software.

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

15-411/ Compiler Design

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

General Course Information. Catalogue Description. Objectives

Compilers and Code Optimization EDOARDO FUSELLA

Pioneering Compiler Design

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

CS426 Compiler Construction Fall 2006

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Compiler Construction

CS131: Programming Languages and Compilers. Spring 2017

Introduction. CSc 453. Compilers and Systems Software. 19 : Code Generation I. Department of Computer Science University of Arizona.

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

A Tour of Language Implementation

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

The Design and Implementation of Gnu Compiler Collection

CPSC 411: Introduction to Compiler Construction

LECTURE 3. Compiler Phases

CSE 401 Final Exam. December 16, 2010

Compiling Techniques

Compiler Construction

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

1DL321: Kompilatorteknik I (Compiler Design 1)

Introduction to Compilers

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

Working of the Compilers

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Compiler Design (40-414)

A Simple Syntax-Directed Translator

Intermediate Representations

CS 415 Midterm Exam Spring 2002

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

Who are we? Andre Platzer Out of town the first week GHC TAs Alex Crichton, senior in CS and ECE Ian Gillis, senior in CS

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

Introduction. Compilers and Interpreters

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

Compilers and Interpreters

An Overview of Compilation

ECE Introduction to Compilers and Translation Engineering

CS153: Compilers Lecture 1: Introduction

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

CMSC 350: COMPILER DESIGN

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

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Principles of Compiler Design

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Compiler construction Compiler Construction Course aims. Why learn to write a compiler? Lecture 1

Programming Languages

Why are there so many programming languages?

CST-402(T): Language Processors

Intermediate Code & Local Optimizations

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

INTRODUCTION PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/10

CSEP 501 Compilers. Overview and Administrivia Hal Perkins Winter /8/ Hal Perkins & UW CSE A-1

CS 406/534 Compiler Construction Putting It All Together

The View from 35,000 Feet

CS5363 Final Review. cs5363 1

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

Compiler construction. Course info. Today. Lecture 1: Introduction and project overview. Compiler Construction Why learn to write a compiler?

Introduction - CAP Course

Introduction to Compiler Construction

Advanced Compiler Construction

Life Cycle of Source Program - Compiler Design

Architectures. Code Generation Issues III. Code Generation Issues II. Machine Architectures I

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

Intermediate Representations

Compilers for Modern Architectures Course Syllabus, Spring 2015

CS202 Compiler Construction. Christian Skalka. Course prerequisites. Solid programming skills a must.

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

CSE 582 Autumn 2002 Exam 11/26/02

Semantic Analysis. Lecture 9. February 7, 2018

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

Lecture Compiler Middle-End

Principles of Compiler Construction ( )

Compilers Crash Course

arxiv: v1 [cs.pl] 30 Sep 2013

List of Figures. About the Authors. Acknowledgments

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Spring 2015

ECE573 Introduction to Compilers & Translators

CS/SE 153 Concepts of Compiler Design

Abstract Syntax Trees & Top-Down Parsing

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Transcription:

Welcome to CSE131b: Compiler Construction Lingjia Tang pic from: http://xkcd.com/303/

Course Information What are compilers? Why do we learn about them? History of compilers Structure of compilers A bit info on course projects

Course Staff Instructor: Lingjia Tang (2108) lingjia@cs.ucsd.edu TA: Xinxin Jing x7jin@cs.ucsd.edu Tutors: Haronid Moncivais Miller and more

http://cseweb.ucsd.edu/classes/sp13/cse131-b

Dragon Book

Grading Polices Project Final Midterm 35% 45% 20%

Grading Polices Project Final Midterm Extra credits: 35% 45% Class participation Piazza participation Projects/exam extra credits 20%

Prerequisites CSE 70 / CSE 110 CSE 100 CSE 105 CSE 130

What is a compiler?

Compiler Compiler

Compiler program in source language Compiler

Compiler program in source language Compiler program in target language

Compiler program in source language Compiler program in target language Error message

Compiler Input program in source language Compiler program in target language Error message Output

Compiler Input program in source language Compiler program in target language Translates Error message Output

Compiler Input program in source language Compiler program in target language Translates Error message Output Typically lower the level of abstraction of the program

Compiler Input program in source language Compiler program in target language Translates Error message Output Typically lower the level of abstraction of the program High-level programming language -> machine code (C/C++ -> X86, etc)

Compiler Input program in source language Compiler program in target language Translates Error message Output Typically lower the level of abstraction of the program High-level programming language -> machine code (C/C++ -> X86, etc) We expect the program produced by the compiler to be better, (faster, consumes less memory, etc) than the original program

Compiler Input program in source language Compiler program in target language Error message Output

Compiler Input program in source language Compiler program in target language Error message Output Interpreter

Compiler Input program in source language Compiler program in target language Error message Output Interpreter Interpreter

Compiler Input program in source language Compiler program in target language Error message Output Interpreter Program Input Interpreter

Compiler Input program in source language Compiler program in target language Error message Output Interpreter Program Input Interpreter output

Compiler Input program in source language Compiler program in target language Static: before runtime Error message Output Interpreter Program Input Interpreter output

Compiler Input program in source language Compiler program in target language Static: before runtime Error message Output Interpreter Program Input Interpreter Dynamic: during runtime output

Compiler Input program in source language Compiler program in target language Static: before runtime Error message Output Interpreter Program Input Interpreter Dynamic: during runtime output Examples: python, shell, javascript, PHP

Hybrid Example: Java program in source language Compiler intermediate program Input output Virtual Machine

Hybrid Example: Java program in source language Compiler intermediate program Interpreter Input Just-in-time compiler native machine code output Garbage Collection Virtual Machine

Hybrid Example: Java program in source language Compiler intermediate program Static Interpreter Input Just-in-time compiler native machine code output Garbage Collection Virtual Machine

Hybrid Example: Java program in source language Compiler intermediate program Static Interpreter Input Just-in-time compiler native machine code output Garbage Collection Virtual Machine Runtime

Hybrid Example: Java Sacrifice efficiency for portability, safety and other dynamic features program in source language Compiler intermediate program Static Interpreter Input Just-in-time compiler native machine code output Garbage Collection Virtual Machine Runtime

Why Learning Compiler? Build awesome, complex software Theory meets practice remember DFA, CFG in CSE105? Understand how SW interacts with HW Understand program language designs and features

Why Learning Compiler? cont. My code runs faster than your code... consumes less power consumes less memory more reliable..

Why Learning Compiler? cont. Who use compilers? everyone! Who develop compilers? many many companies...

Why Learning Compiler? cont. Who use compilers? everyone! Who develop compilers? many many companies...

Why Learning Compiler? cont. Who use compilers? everyone! Who develop compilers? many many companies...

Why Learning Compiler? cont. Who use compilers? everyone! program in PHP Who develop compilers? many many companies... program in C++

Why Learning Compiler? cont. Who use compilers? everyone! program in PHP Who develop compilers? many many companies... program in C++

Goal of this course For you to become compiler NINJA!

History of Compiler Machine Language Assembly programming too time consuming

History of Compiler Machine Language Assembly programming too time consuming High-level language

pics from wikipedia

Grace Hopper pics from wikipedia

Grace Hopper conceptualized the idea of machineindependent programming languages COBOL - (1959) "Nobody believed that," she said. "I had a running compiler and nobody would touch it. They told me computers could only do arithmetic. pics from wikipedia

Grace Hopper conceptualized the idea of machineindependent programming languages COBOL - (1959) "Nobody believed that," she said. "I had a running compiler and nobody would touch it. They told me computers could only do arithmetic. John Backus Won ACM Turing Award for profound, influential, and lasting contributions to the design of practical high-level programming systems, notably through his work on FORTRAN" pics from wikipedia

saman amarasinghe s slide

Compilers: map high-level abstract language to assembly language saman amarasinghe s slide

Compilers: map high-level abstract language to assembly language Simple mapping of a program to assembly language produces inefficient execution Higher the level of abstraction, more inefficiency If not efficient, high-level abstractions are useless saman amarasinghe s slide

Compilers: map high-level abstract language to assembly language Simple mapping of a program to assembly language produces inefficient execution Higher the level of abstraction, more inefficiency If not efficient, high-level abstractions are useless Compilers need to: provide a high level abstraction with performance of giving low-level instruction bridge the efficiency gap b/t high-level PL and low-level processor/isas saman amarasinghe s slide

The Structure of a Modern Compiler Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Machine Code

The Structure of a Modern Compiler Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Front end Machine Code

The Structure of a Modern Compiler Source Code Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Back end Code Generation Optimization Machine Code

while (y < z) { int x = a + b; y += x; } Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization

while (y < z) { int x = a + b; y += x; } Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Lexical analysis (Scanning): Identify logic pieces of the description.

while (y < z) { int x = a + b; y += x; } T_While T_LeftParen T_Identifier y T_Less T_Identifier z T_RightParen T_OpenBrace T_Int T_Identifier x T_Assign T_Identifier a T_Plus T_Identifier b T_Semicolon T_Identifier y T_PlusAssign T_Identifier x T_Semicolon T_CloseBrace Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Lexical analysis (Scanning): Group sequence of characters into lexemes smallest meaningful entity in a language (keywords, identifiers, constants)

while (y < z) { int x = a + b; y += x; } T_While T_LeftParen T_Identifier y T_Less T_Identifier z T_RightParen T_OpenBrace T_Int T_Identifier x T_Assign T_Identifier a T_Plus T_Identifier b T_Semicolon T_Identifier y T_PlusAssign T_Identifier x T_Semicolon T_CloseBrace Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Syntax analysis (Parsing): Identify how those pieces relate to each other

while (y < z) { int x = a + b; y += x; } While Sequence Lexical Analysis Syntax Analysis Semantic Analysis < = = IR Generation IR Optimization y z x + y + Code Generation Optimization a b y x Syntax analysis (Parsing): Convert a linear structure sequence of tokens to a hierarchical tree-like structure - abstract syntax tree (AST)

Syntax Analyzer (Parser) int * foo(i, j, k)) int i; int j; { for(i=0; i j) { fi(i>j) return j; } Not a keyword Extra parentheses Not an expression Missing increment Saman Amarasinghe 35 6.035 MIT Fall 1998

while (y < z) { int x = a + b; y += x; } While Sequence Lexical Analysis Syntax Analysis Semantic Analysis < = = IR Generation IR Optimization y z x + y + Code Generation Optimization a b y x Semantic Analysis: Identify the meaning of the overall structure

while (y < z) { int x = a + b; y += x; } While void Lexical Analysis Syntax Analysis Sequence void Semantic Analysis < bool = int = int IR Generation IR Optimization y int z int x + int int y + int int Code Generation Optimization a b y x int int int int Semantic Analysis: Rules of the language are checked (variable declaration, type checking)

Semantic Analyzer int * foo(i, j, k) int i; int j; { int x; x = x + j + N; return j; Type not declared Mismatched return type Uninitialized variable used } Undeclared variable Saman Amarasinghe 37 6.035 MIT Fall 1998

while (y < z) { int x = a + b; y += x; } While void Lexical Analysis Syntax Analysis Sequence void Semantic Analysis < bool = int = int IR Generation IR Optimization y int z int x + int int y + int int Code Generation Optimization a b y x int int int int Intermediate Representation (IR) Generation: Generate intermediate code

while (y < z) { int x = a + b; y += x; } Loop: x = a + b y = x + y _t1 = y < z if _t1 goto Loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization IR Generation: Makes it easy to port compiler to other architectures (e.g. X86 to MIPS) Enables optimizations that are not machine specific

while (y < z) { int x = a + b; y += x; } Loop: x = a + b y = x + y _t1 = y < z if _t1 goto Loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization IR Optimization: Optimize intermediate code (machine-independent)

while (y < z) { int x = a + b; y += x; } x = a + b Loop: y = x + y _t1 = y < z if _t1 goto Loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization IR Optimization: Optimize intermediate code (machine-independent)

while (y < z) { int x = a + b; y += x; } x = a + b Loop: y = x + y _t1 = y < z if _t1 goto Loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Gode Generation: Intermediate code is translated into native code

while (y < z) { int x = a + b; y += x; } add $1, $2, $3 Loop: add $4, $1, $4 slt $6, $1, $5 beq $6, loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Gode Generation: Intermediate code is translated into native code

while (y < z) { int x = a + b; y += x; } add $1, $2, $3 Loop: add $4, $1, $4 slt $6, $1, $5 beq $6, loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Gode Optimization: machine dependent optimization (register allocation, instruction selection, peephole, etc)

while (y < z) { int x = a + b; y += x; } add $1, $2, $3 Loop: add $4, $1, $4 blt $1, $5, loop Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Gode Optimization: machine dependent optimization (register allocation, instruction selection, peephole, etc)

Modern Compilers Lexical Analysis Syntax Analysis Matured Frontend Heavy Backend many passes of optimization Semantic Analysis IR Generation IR Optimization Code Generation Optimization

Compiler Optimization Frances E. Allen first woman to win Turing Award Introduced many of the abstractions, algorithms, and implementations that laid the groundwork for automatic program optimization technology

Architecture of GCC(GNU Compiler Collection) Architecture of gcc Source Code AST GENERIC High GIMPLE IRs many optimization passes here SSA Low GIMPLE RTL Machine Code

Architecture of GCC(GNU Compiler Collection) Architecture of gcc Source Code AST FrontEnd supports: C (gcc), C++ (g++), Objective- C, Objective-C++, Fortran (gfortran), Java (gcj), Ada (GNAT), and Go (gccgo) IRs many optimization passes here GENERIC High GIMPLE SSA Low GIMPLE RTL Machine code: X86, ARM, Alpha, MIPS, SPARC, PowerPC, etc Machine Code

Architecture of GCC(GNU Compiler Collection) Architecture of gcc Source Code AST FrontEnd supports: C (gcc), C++ (g++), Objective- C, Objective-C++, Fortran (gfortran), Java (gcj), Ada (GNAT), and Go (gccgo) IRs many optimization passes here GENERIC High GIMPLE SSA Low GIMPLE RTL Jeanne Ferrante et. al Machine code: X86, ARM, Alpha, MIPS, SPARC, PowerPC, etc Machine Code

Architecture of LLVM LLVM (Low Level Virtual Machine) developed in UIUC (2000) language-agnostic design

This course covers Lexical Analysis Syntax Analysis Semantic Analysis IR Generation Midterm IR Optimization Code Generation Optimization

The Course Project Compiler: Decaf -> MIPS have a working compiler by the end of the quarter will be able to run your Decaf programs! Source Language: Decaf Custom programming language: similar to Java or C++ (simplified feature set) Objected-oriented, inheritance, Strong type Target language: MIPS RISC ISA Implementation: C++

Programming Assignments Source Code Lexical Analysis Syntax Analysis P1 P2 10% 10% ~1 week 1.5 week Decaf Semantic Analysis P3 10% 3 weeks IR Generation P4 15% 4 weeks IR Optimization Code Generation Optimization MIPS Machine Code

2 person group find your partner by the end of this week Group members get the same grade Honor code: Discussion among groups is encouraged But implement the solution on your own Discussion Session?