Intermediate Code Generation - Compiler Design and Implementation | CS 5810, Papers of Computer Science

Material Type: Paper; Class: Compiler Dsgn&implem; Subject: Computer Science; University: Western Michigan University; Term: Spring 2009;

Typology: Papers

Pre 2010

Uploaded on 08/18/2009

koofers-user-f7o
koofers-user-f7o 🇺🇸

10 documents

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Intermediate
Code Generation
Intermediate
Code
Generation
Chapter6
Chapter6:IntermediateCode
Generation 1CS5810Spring2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

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

Intermediate

‐Code Generation

Intermediate

‐Code

Generation

Chapter

Chapter^ 6:^ Intermediate

Code Generation^

CS5810^ Spring^

2009

Why Intermediate CodeWhy

Intermediate

Code

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

Intermediate language levelsIntermediate

language

levels

Hi h^

-^ Medium -^ Low -^ High t1^ Å^ a[i,j+2] -^ Mediumt1^ Å

j^ +^2 t2 Å i * 20

  • Lowr1^ Å

[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

  • 216 f1^ Å^ [r7+r6]

Chapter^ 6:^ Intermediate

Code Generation^

CS5810^ Spring^

2009

Intermediate Languages TypesIntermediate

Languages

Types

-^ 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

ASTs^

and^ DAGs:b *^

b*

a^ :=^ b

*‐c^ +

b*‐c

:= a +

:= a +

*^ * b (uni)^

(uni)b

  • (uni)b

b^ ‐^ (uni)

‐^ (uni)b c

‐^ (uni)b

c^

c

Chapter^ 6:^ Intermediate

Code Generation^

CS5810^ Spring^

2009

DAGs for ExpressionsDAGs

for^

Expressions

-^ A node in a Directed Acyclic Graph (DAG) mayA^ node

in^ a^ Directed

Acyclic

Graph

(DAG)

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

Value

‐number method for DAG

Value

number

method

for^

DAG

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

TEST YOURSELF #1TEST

YOURSELF

-^ 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

Three

‐Address Code

Three

Address

Code

-^ 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

Addresses and InstructionsAddresses

and

Instructions

-^ 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

Three

‐Address

Code

Instruction

and

ddAddress

-^ 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

] 2

f

do^ i^

i^ +^ 1; while

(a[i]^ <

v);

If^ t^ <^3

v^ goto L

Chapter^ 6:^ Intermediate

Code Generation^

CS5810^ Spring^

2009

QuadruplesQuadruples

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

Static Single

‐Assignment Form

Static

Single

Assignment

Form

-^ 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

Static Single

‐Assignment Form

Static

Single

Assignment

Form

-^ 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