55
Code Generation and Optimisation Introduction

Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Code Generation and Optimisation

Introduction

Page 2: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Code Generation and Optimisation

Introduction

Page 3: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

What is a compiler?

Source Program

Target Program

Easy for humans to understand

Easy for machines to execute

Compiler

"Programs must be written for people to read, and only incidentally for machines to execute.“

– Abel and Sussman

Page 4: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

What is a compiler?

Source Program

Target Program

Easy for humans to understand

Easy for machines to execute

Compiler

"Programs must be written for people to read, and only incidentally for machines to execute.“

– Abel and Sussman

Page 5: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example 1

a = 1;

while (n > 1)

{

a = n * a;

n = n - 1;

}

C Program

ld c, 1

loop:

cp 2

jr c, end

ld b, a

mlt bc

sub 1

jr loop

end:

Z180 Assembly

Compiler

Page 6: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example 1

a = 1;

while (n > 1)

{

a = n * a;

n = n - 1;

}

C Program

ld c, 1

loop:

cp 2

jr c, end

ld b, a

mlt bc

sub 1

jr loop

end:

Z180 Assembly

Compiler

Page 7: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example 2

unsigned 1 x;

while (1)

{

x = ~x;

}

Handel-C Program Digital Circuit

Compiler

x

Page 8: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example 2

unsigned 1 x;

while (1)

{

x = ~x;

}

Handel-C Program Digital Circuit

Compiler

x

Page 9: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example 3

<tt>

Hello world!

</tt>

HTML File

/Courier

12

selectfont

72

500

moveto

(Hello world!)

show

showpage

PostScript File

Compiler

Page 10: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example 3

<tt>

Hello world!

</tt>

HTML File

/Courier

12

selectfont

72

500

moveto

(Hello world!)

show

showpage

PostScript File

Compiler

Page 11: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

COMPILER ORIGINS

When and why did compilers come about?

Page 12: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

COMPILER ORIGINS

When and why did compilers come about?

Page 13: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

The world’s first compiler

Developed by Grace Hopper, 1952.

Compiler for language “A”.

“A” for arithmetic (led to COBOL).

Targeted the UNIVAC-I machine.

Grace Hopper PhD (Maths), Admiral (US Navy), Computer Science “man of the year”, 1969.

UNIVAC-I Clock: 2.5Mhz,

Memory: 12,000 digits, Price: $1,500,000.

Page 14: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

The world’s first compiler

Developed by Grace Hopper, 1952.

Compiler for language “A”.

“A” for arithmetic (led to COBOL).

Targeted the UNIVAC-I machine.

Grace Hopper PhD (Maths), Admiral (US Navy), Computer Science “man of the year”, 1969.

UNIVAC-I Clock: 2.5Mhz,

Memory: 12,000 digits, Price: $1,500,000.

Page 15: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

The world’s first compiler

The inventor of compilers also popularised the term “debugging”.

Excerpt from Grace Hopper’s log book.

Page 16: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

The world’s first compiler

The inventor of compilers also popularised the term “debugging”.

Excerpt from Grace Hopper’s log book.

Page 17: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

The compiler that took off

First FORTRAN compiler, 1957.

John Backus Lead developer of FORTRAN, Co-inventor of BNF, Turing award winner, 1977.

"Much of my work has come from being lazy. I didn't like writing programs, and so, when I was working on the IBM 701, writing programs for computing missile trajectories, I started work on a programming system to make it easier to write programs.”

Made programming easier.

40 FORTRAN compilers targeting various machines by 1963!

Brought portability.

Page 18: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

The compiler that took off

First FORTRAN compiler, 1957.

John Backus Lead developer of FORTRAN, Co-inventor of BNF, Turing award winner, 1977.

"Much of my work has come from being lazy. I didn't like writing programs, and so, when I was working on the IBM 701, writing programs for computing missile trajectories, I started work on a programming system to make it easier to write programs.”

Made programming easier.

40 FORTRAN compilers targeting various machines by 1963!

Brought portability.

Page 19: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

MOTIVATION

Why study compilers?

Page 20: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

MOTIVATION

Why study compilers?

Page 21: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

To understand how computers work

Computer

Architectures

Compilers

Programming

Languages

Page 22: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

To understand how computers work

Computer

Architectures

Compilers

Programming

Languages

Page 23: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

To practice applying your knowledge

Compilers

Algorithms and Data

Structures

Programming

Theory of Computation

Page 24: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

To practice applying your knowledge

Compilers

Algorithms and Data

Structures

Programming

Theory of Computation

Page 25: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Other motivations

Understand compiler errors.

Understand run-time effects of programs, especially efficiency.

“If you can write a compiler, you can write any program.”

“Out of stack space.” “Heap exhausted.” “Type is more polymorphic than expected.” “The impossible happened.”

int fac(int n) {

if (n <= 1)

return 1;

else

return n*fac(n-1);

}

a = 1;

while (n > 1)

{

a = n * a;

n = n - 1;

}

Vs.

Page 26: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Other motivations

Understand compiler errors.

Understand run-time effects of programs, especially efficiency.

“If you can write a compiler, you can write any program.”

“Out of stack space.” “Heap exhausted.” “Type is more polymorphic than expected.” “The impossible happened.”

int fac(int n) {

if (n <= 1)

return 1;

else

return n*fac(n-1);

}

a = 1;

while (n > 1)

{

a = n * a;

n = n - 1;

}

Vs.

Page 27: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

THIS MODULE

Structure and content

Page 28: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

THIS MODULE

Structure and content

Page 29: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

LSA & CGO

Target Program

Easy for machines to execute

Source Program (String)

Easy for humans to write and understand

Easy for compiler to process

Source Program

(Data structure)

Page 30: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

LSA & CGO

Target Program

Easy for machines to execute

Source Program (String)

Easy for humans to write and understand

Easy for compiler to process

Source Program

(Data structure)

Page 31: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Learning Method

1. Learn how compilers work by constructing our own compiler and studying it together.

2. Learn how compilers work by constructing your own compiler from scratch. (With help from demonstrators, if requested!)

Page 32: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Learning Method

1. Learn how compilers work by constructing our own compiler and studying it together.

2. Learn how compilers work by constructing your own compiler from scratch. (With help from demonstrators, if requested!)

Page 33: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Our Compiler: source language

Our own language, called Tower.

The first version is very simple and is gradually extended with new features, one-by-one.

Statements

Procedures

Data Structures

Arithmetic Expressions

Bo

tto

m u

p

Types

Page 34: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Our Compiler: source language

Our own language, called Tower.

The first version is very simple and is gradually extended with new features, one-by-one.

Statements

Procedures

Data Structures

Arithmetic Expressions

Bo

tto

m u

p

Types

Page 35: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Our Compiler: target language

The MIPS processor.

Has a simple instruction set (RISC).

Is widely used in teaching.

Used in the PlayStation II.

Simple yet realistic.

li r1, 1

loop:

li r2, 1

bgt r0, r2, end

mlt r1, r0, r1

subi r0, r0, 1

end:

Page 36: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Our Compiler: target language

The MIPS processor.

Has a simple instruction set (RISC).

Is widely used in teaching.

Used in the PlayStation II.

Simple yet realistic.

li r1, 1

loop:

li r2, 1

bgt r0, r2, end

mlt r1, r0, r1

subi r0, r0, 1

end:

Page 37: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example

a := 1;

while (n > 1)

(

a := n * a;

n := n - 1

)

Tower Program

li r1, 1

loop:

li r2, 1

bgt r0, r2, end

mlt r1, r0, r1

subi r0, r0, 1

end:

MIPS Assembly

Our compiler

Page 38: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example

a := 1;

while (n > 1)

(

a := n * a;

n := n - 1

)

Tower Program

li r1, 1

loop:

li r2, 1

bgt r0, r2, end

mlt r1, r0, r1

subi r0, r0, 1

end:

MIPS Assembly

Our compiler

Page 39: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Your Compiler: source language

A picture description language.

For example, the description

defines the following picture.

hor 0, 1, 3 + ver 1, 0, 3

x

y

Page 40: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Your Compiler: source language

A picture description language.

For example, the description

defines the following picture.

hor 0, 1, 3 + ver 1, 0, 3

x

y

Page 41: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Your Compiler: target language

Turtle-graphics commands.

A turtle is a robot with a pen and the ability roam a 2D canvas.

For example, executing the program

causes the turtle to draw the canvas:

Forward, Rotate, Lower, Forward, Forward, Raise, Rotate, Forward, Rotate, Forward, Rotate, Lower, Forward, Forward

x

y

Page 42: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Your Compiler: target language

Turtle-graphics commands.

A turtle is a robot with a pen and the ability roam a 2D canvas.

For example, executing the program

causes the turtle to draw the canvas:

Forward, Rotate, Lower, Forward, Forward, Raise, Rotate, Forward, Rotate, Forward, Rotate, Lower, Forward, Forward

x

y

Page 43: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example

hor 0, 1, 3 + ver 1, 0, 3

Picture description

Forward, Rotate, Lower, Forward, Forward, Raise, Rotate, Forward, Rotate, Forward, Rotate, Lower, Forward, Forward

Turtle commands

Your compiler

Page 44: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Example

hor 0, 1, 3 + ver 1, 0, 3

Picture description

Forward, Rotate, Lower, Forward, Forward, Raise, Rotate, Forward, Rotate, Forward, Rotate, Lower, Forward, Forward

Turtle commands

Your compiler

Page 45: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Implementation language

A compiler can be written in any programming language.

Haskell is a good choice:

– algebraic data types;

– and pattern matching.

Our compiler and your compiler will be written in Haskell.

All needed Haskell skills will be taught as part of this module.

Page 46: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Implementation language

A compiler can be written in any programming language.

Haskell is a good choice:

– algebraic data types;

– and pattern matching.

Our compiler and your compiler will be written in Haskell.

All needed Haskell skills will be taught as part of this module.

Page 47: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Practicals

Week Topic

3 Semantics of Pictures

4 Semantics of Turtle Graphics

5 Chapters 2-4 (Pen & Paper)

6 A Picture to Turtle Compiler

7 Chapters 5-6 (Pen & Paper)

8 Optimising Turtle code

9 Chapters 7-10 (Pen & Paper)

4 lab practicals.

3 pen & paper practicals.

Each two hours.

Page 48: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Practicals

Week Topic

3 Semantics of Pictures

4 Semantics of Turtle Graphics

5 Chapters 2-4 (Pen & Paper)

6 A Picture to Turtle Compiler

7 Chapters 5-6 (Pen & Paper)

8 Optimising Turtle code

9 Chapters 7-10 (Pen & Paper)

4 lab practicals.

3 pen & paper practicals.

Each two hours.

Page 49: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Lectures

Approximately 11 lectures, with notes arranged into 10 chapters.

A single chapter may be covered in less or more than one lecture.

Chapter Title

1 Introduction

2 Haskell for compiler writers

3 Our first compiler

4 Booleans, conditionals, and loops

5 Optimisation = analysis + transformation

6 Register Allocation via graph colouring

7 Local variables & procedures

8 Arrays & dynamic allocation

9 Type checking

10 Garbage collection

Page 50: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Lectures

Approximately 11 lectures, with notes arranged into 10 chapters.

A single chapter may be covered in less or more than one lecture.

Chapter Title

1 Introduction

2 Haskell for compiler writers

3 Our first compiler

4 Booleans, conditionals, and loops

5 Optimisation = analysis + transformation

6 Register Allocation via graph colouring

7 Local variables & procedures

8 Arrays & dynamic allocation

9 Type checking

10 Garbage collection

Page 51: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Timetable

This term’s CGO timetable is horrendously complex!

Check evision every week.

Also check CGO website every Sunday (some lectures will be cancelled).

Page 52: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Timetable

This term’s CGO timetable is horrendously complex!

Check evision every week.

Also check CGO website every Sunday (some lectures will be cancelled).

Page 53: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Recommended books

Page 54: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Recommended books

Page 55: Code Generation and Optimisation · What is a compiler? Source Program Target Easy for humans to understand Easy for machines to execute Compiler "Programs must be written for people

Assessment

A 90 minute closed exam in first week of summer term.

Practicals, especially pen and paper ones, will be excellent practice for the exam.

So please ask for help in the practicals if you get stuck.

A past exam paper will also be provided for practice.