welcome to cs143: compilers...why study compilers? build a large, ambitious software system. see...

51
Welcome to CS143: Compilers One Handout Today: Course Information Why Study Compilers? A Quick History of Compilers The Structure of a Compiler

Upload: others

Post on 01-Sep-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Welcome to CS143: Compilers

● One Handout● Today:

– Course Information

– Why Study Compilers?

– A Quick History of Compilers

– The Structure of a Compiler

Page 2: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

http://cs143.stanford.edu

Page 4: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming
Page 5: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming
Page 6: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

60% Programming Assignments20% Written Assignments20% Midterm Exam

Grading Policies

Page 7: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

A word on the honor code...

Page 8: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 9: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

A Short History of Compilers

● First, there was nothing.● Then, there was machine code.● Then, there were assembly languages.

– e.g. A-0, A-2

● Programming expensive; 50% of costs for machines went into programming.

Page 10: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Enter John Backus

“I’m a terribly unscholarly person, and lazy. That was my motivating force in most of what I did, was how to avoid work”

- John Backus, interviewed in 2006

Page 11: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 12: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 13: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 14: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 15: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 16: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 17: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

Page 18: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

3Ω 6Ω6V

11

3Ω+

16Ω

=2Ω

Page 19: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

2Ω6V

11

3Ω+

16Ω

=2Ω

Page 20: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

2Ω6V

Page 21: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

2Ω6V

4Ω+2Ω=6Ω

Page 22: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

6V

4Ω+2Ω=6Ω

Page 23: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

6V

Page 24: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

6V

6 V

Page 25: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

6V

6 VTotal Cost: $4.75

Page 26: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

1.5V

1.5V

1.5V

1.5V

Page 27: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

AAA

1.5V

1.5V

1.5V

1.5V

AAA

AAA

AAA

Page 28: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

AAA

1.5V

1.5V

1.5V

1.5V

AAA

AAA

AAATotal Cost: $1.00

Page 29: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

From Description to Implementation

● Lexical analysis: Identify logical pieces of description

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

● Semantic analysis: Identify the meaning of those relations.

● IR Optimization: Simplify the intended structure.

● Code Generation: Fabricate the structure.● Optimization: Improve the resulting structure.

Page 30: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

The Structure of a Modern Compiler

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 31: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

The Structure of a Modern Compiler

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 32: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

The Structure of a Modern Compiler

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 33: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

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

Page 34: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

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

Page 35: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 36: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 37: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 38: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 39: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 40: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 41: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 42: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 43: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 44: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 45: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 46: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 47: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

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 48: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

The Course Project: Decaf

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

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

Page 49: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Programming Assignments

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 50: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Programming Assignments

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode

Page 51: Welcome to CS143: Compilers...Why Study Compilers? Build a large, ambitious software system. See theory come to life. Learn how to build programming languages. Learn how programming

Next Time...

Lexical Analysis

Syntax Analysis

Semantic Analysis

IR Generation

IR Optimization

Code Generation

Optimization

SourceCode

MachineCode