Code Generation and Optimisationtarget language The MIPS processor. Has a simple instruction set...

28
Code Generation and Optimisation Introduction

Transcript of Code Generation and Optimisationtarget language The MIPS processor. Has a simple instruction set...

Code Generation and Optimisation

Introduction

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

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

Example 2

unsigned 1 x;

while (1)

{

x = ~x;

}

Handel-C Program Digital Circuit

Compiler

x

Example 3

<tt>

Hello world!

</tt>

HTML File

/Courier

12

selectfont

72

500

moveto

(Hello world!)

show

showpage

PostScript File

Compiler

COMPILER ORIGINS

When and why did compilers come about?

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.

The world’s first compiler

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

Excerpt from Grace Hopper’s log book.

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.

MOTIVATION

Why study compilers?

To understand how computers work

Computer

Architectures

Compilers

Programming

Languages

To practice applying your knowledge

Compilers

Algorithms and Data

Structures

Programming

Theory of Computation

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.

THIS MODULE

Structure and content

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)

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!)

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

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:

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

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

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

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

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.

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.

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

Timetable

This term’s CGO timetable is horrendously complex!

Check evision every week.

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

Recommended books

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.