Docsity

Notes on Code Generation - Compiler Design and Implementation | CS 5810, Study notes of Computer Science

Partial preview of the text

Download Notes on Code Generation - Compiler Design and Implementation | CS 5810 and more Study notes Computer Science in PDF only on Docsity!

Code GenerationCode

Generation^ Chapter

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

Code GenerationCode

Generation

Front^ End

Code Optimizer

Code Generator

Intermediate

Intermediate

Code^

Code^

Targetprogram

-^ Severe

requirements

-^ Target

program

preserve

the^ semantics

-^ Effective use of available resourcesEffective

use^ of

available

resources

-^ Itself

must

be^ efficient

-^ Primary

tasks I^ t^

ti^

l^ ti

-^ Instruction

selection

-^ Register

allocation

and^ assignment

-^ Instruction

ordering

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

The Target ProgramThe

Target

Program

-^ RISC

(reduced

instruction

set^

computer)

SC ( educed

st uct o

set co

pute )

-^ Many

registers,

three

‐address

instructions,

simple

addressing

modes,

simple

instruction

set^ arch

-^ CISC

(complex

instruction

set^

computer)

-^ few

registers,

two‐

address

instructions,

various

addressing modes variable

length instruction set

addressing

modes

,^ variable

‐length

instruction

set

-^ Stack

‐based

Machine Pushing operands onto a stack– Pushing

operands

onto

a^ stack

-^ Almost

disappeared,

then

revived

with

Java

Virtual

Machine

(JVM)

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

The Target ProgramThe

Target

Program

-^ Absolute machine

‐language programs

Absolute

machine

language

programs

-^ Can

be^ placed

in^ a^

fixed

location

in^ memory

and

immediately

executed

-^ Relocatable machine

‐language

programs

-^ Subprograms

can^

be^ complied

separately,

then

linked

together

and

loaded

for^ execution

by

linking

loader A^

bl^ l

-^ Assembly

‐language

program

-^ Code

generation

easier,

need

assembly

step

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

Register AllocationRegister

Allocation

-^ Register

allocation:

select

the^

set^ of

vars that

will

g reside^

in^ registers

-^ Register

assignment:

pick

the^

specific

register

that a

var^

will reside in that

a^ var

will

reside

in

L R1 aL^ R

,^ a A^ R1,

b M R0 c

t^ =^ a

+^ b t^ =^ t

*^ c^

M^ R

,^ c D^ R0,

d ST R1 t

t^ =^ t

/^ d Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

ST^ R

,^ t

Evaluation OrderEvaluation

Order

-^ The order can affect the efficiencyThe

order

can

affect

the

efficiency

-^ Some

orders

require

fewer

registers

Picking best order is NP complete– Picking

best

order

is^ NP

‐complete

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

The Target LanguageThe

Target

Language

-^ Addressing

mode dd ess

g^

ode

-^ Variable

name

x

-^ Indexed

address

a(r)

-^ LD^ R1,

a(R2)

is^ R^

=^ contents(a+contents(R2))

-^ An

integer

indexed

by^ a

register

-^ LD R1 100(R2) is R1 = contents(100+contents(R2))•^ LD^ R

,^ 100(R2)

is^ R

=^ contents(100+contents(R2))

-^ Indirect

addressing

modes

-^ LD^ R1,

*100(R2) is

R1=contents(contents(100+contents(R2))

-^ Immediate

constant

address

mode

-^ LD^ R1,

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

The Target Language ExampleThe

Target

Language

Example

x=y‐

z^

b=a[i]

LD^ R1,

y LD^ R2,

z

LD^ R1,

i MUL

R1,

R1,^

SUB

R1,

R1,

R

ST^ x,

R

,^ ,

LD^ R2,

a(R1) ST^ b,

R

,^

x=*p

if^ x^ <

y^ goto L

LD^ R1,

p LD R2 0(R1)

LD^ R1,

x LD^ R2,

y

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

LD^ R

,^ 0(R1)

ST^ x,

R^

Chapter^ 6:

Intermediate

Code SUB Generation

R1,

R1,

R

BLTZ

R1,

L

TEST YOURSELF

TEST

YOURSELF

-^ Generate code for the following three addressGenerate

code

for^

the^

following

three

address

sequences^ x^ =

a[i]y = b[i]

y^ =^ *q q^ =^ q

+^4

Z^ =^ x

*^ y^

*p^ =

y p^ =^ p

+^4

CS5810^ Spring

2009

Chapter^ 8:

Code^ Generation

Basic Blocks and Flow GraphsBasic

Blocks

and

Flow

Graphs

-^ Partition

IR^ into

bas

ic^ blocks,

which

are^

maximal

a t t o

to bas c b oc s,

c^ a e

a^

a

sequences

of^ consecutive

three

‐address

instructions

that

-^ The

flow

of^ control

can^

only^

enter

the^ basic

block

through

the^ first

instruction

Control leave the block without halting/branching– Control

leave

the^ block

without

halting/branching

,

except

at^ the

last^

instruction

-^ The

basic

blocks

become

the^

nodes

of^ a

flow

graph,

whose

edges

indicate

which

blocks

can

follow

which

other

blocks

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

TEST YOURSELF

TEST

YOURSELF

-^ Construct basic blocks

1)^ i=12)^ J^

=^1

Construct

basic

blocks

of^ the

following

IR^

3)^ T

=^10 *

I 4)^ T

=^ t1^ +

j 5)^ T

=^8 *^ t 6)^ T

t^

88

for^ i from

1 to

10 do for j from 1 to 10 do

6)^ T

=^ t3^ –

88 7)^ A[t4]

=^ 0. 8)^ J^

=^ j+^1 9)^ If j<=10 goto (3)

for^ j

from

1 to

10 do a[i,j]=0. for i from 1 to 10 do

9)^ If

j<=

goto^ (3) 10)^ I^

=^ I^ +^1 11)^ If

I^ <=^10

goto (2) 12)^ I^

=^1

for^ i

from

1 to

10 do a[i,i]=1.

) 13)^ T5^ =

I^ – 1 14)^ T

6 =^88

*^ t 15)^ A[t6]

=^ 1.

CS5810^ Spring

2009

Chapter^ 8:

Code^ Generation

16)^ I^

=^ i+ 17)^ I^

i<=^10

goto (13)

Next

‐Use Information

Next

Use

Information

-^ Knowing when the value of a

var^

will be used

Knowing

when

the

value

of^ a

var^

will^

be^ used

next

is^ essential

for^

good

code

generation

-^ j uses the value of x computed at i if•^ j^ uses

the

value

of^ x

computed

at^ i^

if

-^ statement

i assigns

to^ x

If j h

d^

d^

l^

h j

-^ If^ j

has^

x^ as^

an^ operand,

and

control

can^

reach

j

from

i without

an^ intervening

assignment

to^ x

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

Optimization of Basic BlocksOptimization

of^

Basic

Blocks

-^ Substa

ntial

improvement

can

be^ ga

ined

by

Substa t a

p o e

e t ca

be ga

ed by

performing

local

optimization

-^ DAG

Representation

of^ basic

blocks

p

-^ A^ node

for^ each

initial

values

of^ the

vars

-^ Node

for^ each

statement

-^ Children

are^ the

statements

of^ the

last^ definitions

-^ Labeled

by^ operators

and^ vars for

which

it^ is^ the

last

definition

in^ the

block

-^ Some

nodes

are^ “output

nodes”

whose

vars are

live

on^ exit^ •^ Part of global flow analysis discussed later•^ Part

of^ global

flow^

analysis

,^ discussed

later

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009

DAGs for Basic BlocksDAGs

for

Basic

Blocks

c + ‐

a^ =^ b

+^ c b^ =^ a

-^ d^

b,d d^0

  • b

c^ =^ b

+^ c d^ =^ a

‐^ d

a c^0 b^0

Chapter^ 8:

Code^ Generation

CS5810^ Spring

2009