




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Yang; Class: Compiler Dsgn&implem; Subject: Computer Science; University: Western Michigan University; Term: Spring 2009;
Typology: Study notes
1 / 44
This page cannot be seen from the preview
Don't miss anything!





































Chapter^ 8:
Code^ Generation
CS5810^ Spring
2009
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
-^ 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
-^ 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
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^
,^ c D^ R0,
d ST R1 t
t^ =^ t
/^ d Chapter^ 8:
Code^ Generation
CS5810^ Spring
2009
,^ t
-^ 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
-^ 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
x=y‐
z^
b=a[i]
y LD^ R2,
z
i MUL
ST^ x,
a(R1) ST^ b,
x=*p
if^ x^ <
y^ goto L
p LD R2 0(R1)
x LD^ R2,
y
Chapter^ 8:
Code^ Generation
CS5810^ Spring
2009
ST^ x,
Chapter^ 6:
Intermediate
Code SUB Generation
-^ Generate code for the following three addressGenerate
code
for^
the^
following
three
address
sequences^ x^ =
a[i]y = b[i]
y^ =^ *q q^ =^ q
Z^ =^ x
*^ y^
*p^ =
y p^ =^ p
CS5810^ Spring
2009
Chapter^ 8:
Code^ Generation
-^ 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
-^ Construct basic blocks
1)^ i=12)^ J^
=^1
Construct
basic
blocks
of^ the
following
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)
-^ 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
-^ 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
c + ‐
a^ =^ b
+^ c b^ =^ a
-^ d^
b,d d^0
c^ =^ b
+^ c d^ =^ a
‐^ d
a c^0 b^0
Chapter^ 8:
Code^ Generation
CS5810^ Spring
2009