welcome to cs143: compilers - stanford university · welcome to cs143: compilers ... a quick...

60
Welcome to CS143: Compilers Course Information Why Study Compilers? A Quick History of Compilers The Structure of a Compiler

Upload: ngonhi

Post on 09-Apr-2018

232 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Welcome to CS143: Compilers

● Course Information● Why Study Compilers?● A Quick History of Compilers● The Structure of a Compiler

Page 2: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Course Staff

Instructor: Keith Schwarz([email protected])

TA: Jinchao Ye([email protected])

TA: Naran Bayanbat([email protected])

Page 3: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

http://cs143.stanford.edu

Page 4: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm
Page 5: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm
Page 6: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Grading Policies

Page 7: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

60% Programming Assignments20% Written Assignments20% Midterm Exam

Grading Policies

Page 8: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

60% Programming Assignments20% Written Assignments20% Midterm Exam

Grading Policies

Page 9: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

60% Programming Assignments20% Written Assignments20% Midterm Exam

Grading Policies

Page 10: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

60% Programming Assignments20% Written Assignments20% Midterm Exam

Grading Policies

Midterm exam:July 25,

11:00AM – 1:00PM, Location TBA

Midterm exam:July 25,

11:00AM – 1:00PM, Location TBA

Page 11: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

A Word on the Honor Code...

Page 12: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Prerequisites

CS107

CS103

Page 13: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Why Study Compilers?

● Build a large, ambitious software system.

● See theory come to life.● Learn how to build programming

languages.● Learn how programming languages

work.● Learn tradeoffs in language design.

Page 14: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

A Short History of Compilers

● First, there was nothing.● Then, there was machine code.● Then, there were assembly languages.● Programming expensive; 50% of costs for

machines went into programming.

Page 15: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Image: http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Grace_Hopper.jpg/300px-Grace_Hopper.jpghttp://www.nytimes.com/2007/03/20/business/20backus.html

High-Level Languages

Page 16: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Image: http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Grace_Hopper.jpg/300px-Grace_Hopper.jpghttp://www.nytimes.com/2007/03/20/business/20backus.html

High-Level Languages

Rear Admiral Grace Hopper, inventor ofA-0, COBOL, and the

term “compiler.”

Rear Admiral Grace Hopper, inventor ofA-0, COBOL, and the

term “compiler.”

Page 17: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Image: http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Grace_Hopper.jpg/300px-Grace_Hopper.jpghttp://www.nytimes.com/2007/03/20/business/20backus.html

High-Level Languages

Page 18: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Image: http://upload.wikimedia.org/wikipedia/commons/thumb/5/55/Grace_Hopper.jpg/300px-Grace_Hopper.jpghttp://www.nytimes.com/2007/03/20/business/20backus.html

High-Level Languages

John Backus, team lead on FORTRAN.

John Backus, team lead on FORTRAN.

Page 19: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

How does a compiler work?

Page 20: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 21: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 22: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 23: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 24: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 25: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 26: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

Page 27: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

3Ω 6Ω6V

11

3Ω+

16Ω

=2Ω

Page 28: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

2Ω6V

11

3Ω+

16Ω

=2Ω

Page 29: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

2Ω6V

Page 30: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

2Ω6V

4Ω+ 2Ω=6Ω

Page 31: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

6V

4Ω+ 2Ω=6Ω

Page 32: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

6V

Page 33: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

6V

6 V

Page 34: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

6V

6 VTotal Cost: $4.75

Page 35: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

1.5V

1.5V

1.5V

1.5V

Page 36: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

AAA

1.5V

1.5V

1.5V

1.5V

AAA

AAA

AAA

Page 37: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

AAA

1.5V

1.5V

1.5V

1.5V

AAA

AAA

AAATotal Cost: $1.00

Page 38: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

From Description to Implementation

● Lexical analysis (Scanning): Identify logical pieces of the description.

● Syntax analysis (Parsing): Identify how those pieces relate to each other.

● Semantic analysis: Identify the meaning of the overall structure.

● IR Generation: Design one possible structure.

● IR Optimization: Simplify the intended structure.

● Generation: Fabricate the structure.

● Optimization: Improve the resulting structure.

Page 39: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

The Structure of a Modern Compiler

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 40: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

The Structure of a Modern Compiler

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 41: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

The Structure of a Modern Compiler

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 42: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

Page 43: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

Page 44: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

T_WhileT_LeftParenT_Identifier yT_LessT_Identifier zT_RightParenT_OpenBraceT_IntT_Identifier xT_AssignT_Identifier aT_PlusT_Identifier bT_SemicolonT_Identifier yT_PlusAssignT_Identifier xT_SemicolonT_CloseBrace

Page 45: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

T_WhileT_LeftParenT_Identifier yT_LessT_Identifier zT_RightParenT_OpenBraceT_IntT_Identifier xT_AssignT_Identifier aT_PlusT_Identifier bT_SemicolonT_Identifier yT_PlusAssignT_Identifier xT_SemicolonT_CloseBrace

Page 46: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

While

<

Sequence

=

x +

a b

=

y +

y x

y z

Page 47: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

While

Sequence

=

x +

a b

=

y +

y x

<

y z

Page 48: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

While

Sequence

=

x +

a b

=

y +

y x

int

int int

int

int int

int

int int

int

void

void

Semantic Analysis

<

y z

int int

bool

Page 49: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

While

Sequence

=

x +

a b

=

y +

y x

int

int int

int

int int

int

int int

int

void

void

<

y z

int int

bool

Page 50: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

Loop: x = a + b y = x + y _t1 = y < z if _t1 goto Loop

IR Generation

Page 51: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

Loop: x = a + b y = x + y _t1 = y < z if _t1 goto Loop

Page 52: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

x = a + bLoop: y = x + y _t1 = y < z if _t1 goto Loop

IR Optimization

Page 53: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

x = a + bLoop: y = x + y _t1 = y < z if _t1 goto Loop

Page 54: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

add $1, $2, $3Loop: add $4, $1, $4 slt $6, $1, $5 beq $6, loop

Page 55: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

add $1, $2, $3Loop: add $4, $1, $4 slt $6, $1, $5 beq $6, loop

Page 56: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

while (y < z) { int x = a + b; y += x;}

add $1, $2, $3Loop: add $4, $1, $4 blt $1, $5, loop

Page 57: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

The Course Project: Decaf

● Custom programming language similar to Java or C++.

● Object-oriented with free functions.● Single inheritance with interfaces.

Page 58: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Programming Assignments

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 59: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Programming Assignments

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 60: Welcome to CS143: Compilers - Stanford University · Welcome to CS143: Compilers ... A Quick History of Compilers The Structure of a Compiler. Course Staff Instructor: ... 20% Midterm

Next Time...

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode