






























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: Paper; Class: Compiler Dsgn&implem; Subject: Computer Science; University: Western Michigan University; Term: Spring 2009;
Typology: Papers
1 / 38
This page cannot be seen from the preview
Don't miss anything!































Chapter
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
I t^ di t
Parser^
Static Checker
Intermediate^ Code^ Generator
Code Generator
Front^ end
Back^ end
Program^1
Architecture
1
Program^1
Architecture
1
Program^1
Architecture
2
Program^1
Architecture
2 Interm.^ C d
Program^1 Program
Architecture
3 Architecture
Program^1 Program
Architecture
3 Architecture Code
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
Program^1
Architecture
4
Program^1
Architecture
4
Hi h^
-^ Medium -^ Low -^ High t1^ Å^ a[i,j+2] -^ Mediumt1^ Å
j^ +^2 t2 Å i * 20
[fp‐4] r2 Å r1 + 2
t2^ Å^ i
*^20 t3^ Å^ t
+^ t t4^ Å^ 4 * t
r2^ Å^ r
+^2 r3^ Å^ [fp
‐8] 4 Å^ 3*
t4^ Å^4
*^ t t5^ Å^ addr
a Å
r4^ Å^ r3*20r5^ Å^ r
+^ r2 Å *
t6^ Å^ t
+^ t^4 t7^ Å^ *t
r6^ Å^4
*^ r r7^ Å^ fp
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
-^ Graphical IRs: Abstract Syntax trees DAGsGraphical
IRs:^ Abstract
Syntax
trees
,^ DAGs
Control
Flow^
Graphs
-^ Linear IRs:•^ Linear
IRs:
-^ Stack
based
(postfix) Th^
dd^
d^ (^
d^ l^
)
-^ Three
address
code^ (
quadruples)
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
:= a +
:= a +
*^ * b (uni)^
(uni)b
b^ ‐^ (uni)
‐^ (uni)b c
‐^ (uni)b
c^
c
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
-^ A node in a Directed Acyclic Graph (DAG) mayA^ node
in^ a^ Directed
Acyclic
Graph
may
have^ more
than^
one^ parent
d *^ ‐ a
a+a*(b
‐c)+(b
‐c)*d
c b
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
id 1 num+
(^1021) 2 3 = +
i =^ i^ +^
=^
1 3 4 5 (^10) i
Input: Label op, node l, and node rOutput: The value number in the array with <op, l, r>Method: Search the array for M=<op, l, r>. If M exists, return M;otherwise create a new node N=<op, l, r>, and return its valuenumber
CS5810^ Spring^
2009
number. Chapter^ 6:^ Intermediate
Code Generation
-^ Construct the DAG and identify the valueConstruct
the^ DAG
and^ identify
the^ value
numbers
for^ the
sub‐expressions ((x+y)
‐((x+y)*(x
‐y)))+((x+y)*(x
‐y)) CS5810^ Spring^
2009
Chapter^ 6:^ Intermediate
Code Generation
-^ At most one operator on the right hand sideAt^ most
one^ operator
on^ the
right^
hand^
side
-^ Expression
like^ x+y*z
has^ to
be^ translated
t^ =b‐c^1 t^ =a*t^2
1
d *^ ‐ a
t^ a t^2
1 t^ =a+t^3
2 t^ =t^ *d^41
c b
t^ t^4
d t^ =t^ +t^53
4
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
-^ An address can beAn^
address
can^ be
-^ A^ name^ •^ In actual
implementation name is replaced by a In^ actual
implementation
,^ name^
is^ replaced
by^ a
pointer
to^ its^ symbol
‐table^ entry
-^ A^ constant –^ A^ compiler–generated
temporary
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
-^ At most one operator on the right hand sideAt^ most
one^ operator
on^ the
right^
hand^
side L:^ t^ =^1
i+ i^ =^ t^1 *
do i = i + 1;
t^ =^ i *^2
t^ =^ a[t^3
f
do^ i^
i^ +^ 1; while
(a[i]^ <
v);
If^ t^ <^3
v^ goto L
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring^
2009
A quadruple has 4 fields: op, arg1, arg2, result
minus
t^ =^ minus^1
c t^ b * t
c^
t^1
0 1 2 minus^3
t^ =^ b^ *^2
t^1 t^ =^ minus^3
c t^ b * t
*b c *
t^1
t^2 t^3
3 4 + 5
t^ =^ b^4
*^ t^3 t^ =^ t^5
+^ t^4 t
*^ b t^1
tt^3
4 tt^4
5
5 =
a^ =^ t^5
t^5
a
CS5810^ Spring^
2009
Chapter^ 6:^ Intermediate
Code Generation
-^ SSA is an IR that facilitate certain codeSSA
is^ an^
IR^ that
facilitate
certain
code
optimization^ –^ All^ assignments
are^ to
vars with
distinct
names
g p^ =^ a^
+^ b q^ p^
c
p^ =^ a^1
+^ b q^ p^
c
q^ =^ p^ ‐
c p^ =^ q *
d p = e^
p
q^ =^ p^11
‐^ c p^ =^ q^2
*^ d 1 p = e
p
p^ =^ e^
-^ p q =^ p +
q
p^ =^ e^3
-^ p^2 q^ =^ p^2
+^ q^3
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring
2009
-^ What if the same variable is defined in twoWhat
if^ the
same
variable
is^ defined
in^ two
different
control
flow^
paths?^ if (flag)
if^ (flag)
x^ 1;
if^ (flag)
x^ =^ ‐1;^1 else
x^ =^ ‐1; else
x = 1;
else
x^ =^ 1;^2 x^ φ
(x^ x )
x^ =^ 1; y^ =^ x^ *
a;
x^ =^ φ^3
(x,x^1
y^ =^ x^3
*^ a;
Chapter^ 6:^ Intermediate
Code Generation^
CS5810^ Spring
2009