Code Generation Steps and Representing Target Codes - Paper | CMSC 430, Papers of Computer Science

Material Type: Paper; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Spring 2001;

Typology: Papers

Pre 2010

Uploaded on 07/30/2009

koofers-user-l35
koofers-user-l35 🇺🇸

3

(1)

9 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Code generation
source
code
front
end
opt.
back
end
target
code
IR IR
Code generation steps
1. Source co de
!
intermediate representation
generate intermediate representation during
parse based on syntax, sy mbol tables
2. Intermediate representation
!
target co de
instruction selection
choose inst ructi ons based on target instr set
memory management
decide storage for variables; allo cate registers
linkage code
determine prolog/epi l og code
instruction scheduling
choose inst ructi on execution order
CMSC 430
Lecture 12, Page 1
Abstract syntax trees
Abstract syntax tree (AST)
stores syntactic structure of program
+
<
x
>
*
<
2
> <
y
>
Building the AST
construct no de for LHS of production
ll i n elds using RHS of production
Example
E
0
::= E
1
'+' E
2
f
E
0
.val = node('+', E
1
.val, E
2
.val);
g
j
E
1
'*' E
2
f
E
0
.val = no de('*', E
1
.val, E
2
.val);
g
j
id
f
E
0
.val = no de(
id
.val);
g
j
num
f
E
0
.val = no de(
num
.val);
g
CMSC 430
Lecture 12, Page 2
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Code Generation Steps and Representing Target Codes - Paper | CMSC 430 and more Papers Computer Science in PDF only on Docsity!

Co

de

generation

s code ource

fr (^) ont

e nd

opt.

ba

ck

e nd

t code arget

IR

IR

Co

(^) d e gen

era

(^) tion

steps

Sour

c e co

de

!

i n t erm

e diat

e repr

e (^) sen

t ati

on

 (^) ge

n e r at

e i n t erm

e diat

e repr

e (^) sen

t ati

on

dur

i ng

parse

based

on

syn

tax

sy

m b ol

ta

b l e s

In

t erm

edi

ate

represen

tat

i on

!

target

co

de

 (^) i nstruct

i on

sel

e cti

on

c ho

ose

i nst

r uct

i ons

based

on

t arget

i nstr

set

 (^) m

em

ory

m

anagem

en

t

dec

i d e st orage

for

v ari

able

s;

al l o cate

regi

(^) st ers

 (^) l i nk

age

co

de

det

(^) erm

ine

prol

(^) og/epi

l og

co

de

 (^) i nstruct

i on

sc hedul

i n g

c ho

ose

i nst

r uct

i on

e xec

(^) u t i on

order

C MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^1)

Abst

ract

syn

tax

trees

Ab

stra

ct

syntax

tre

e (AST)

 stores

syn

tac

ti c struct

ur

e of

program

< x (^) >

< 2 > < y >

Bu

il di n g th

e AST

 const

r uct

n o (^) de

for

LHS

of

pro

(^) duct

i on



l l

i n

el ds

usi

ng

RH

S

of

pro

ducti

on

Exam

pl (^) e

E

0

::=

E

1 '+'

E

2

f E 0 .v al

=

no

de

('+',

E

1 .v al , E 2 (^) .v al ) ; g

j

E

1 '*'

E

2

f E 0 .v al

=

no

de

('*',

E

1 .v al , E 2 .v al ) ; g

j

id

f E 0 .v al

=

no

de

( id

.v al );

g

j

num

f E 0 .v al

=

no

de

( num

.v al ) ; g

C (^) MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^2)

Inst

ruct

i o n

se

le

ct

i o n

Co

(^) d e tem

pl ates

 te m pl

at

e for

e ac

h

l anguage

construct

 i gnor

e surroun

di

ng

con

t ex

t

 si m

p l e rec

u rsi

v e appr

oac

h

Lang

(^) uage

c onstr

uct

s

si m

p l e e xpressi

on

s

con

trol

structures

pr

o cedure

cal

(^) l s

com

pl ex

ex

pr

e ssio

n s

Ap

pl yin

g

t emp

la tes

 use

syn

tax

during

parse

 appl

y

tree

rewri

ti (^) ng

to

AST

C MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^3)

In

t e (^) r medi

at

e

represe

n t ation

W

e'l

l b e ta rge

tin

g RISC-

l i k e p r o ce

sso

r s

 l oad-

st ore

ar

c hi

t (^) ect

u re

 regi

ste

r -transfe

r l anguage

 three-address

co

de

 ex

p l i c it

l oad

s and

stores

Exam

pl es

l oad

r 1,

< addr

$

r

value

at

< addr

l oadi

r1,

< c on

st

$

r

value

of

< c onst

s tore

r1,

< addr

$

< add

r

r

m ove

r 1,

r

$

r

r

a dd

r

,

r2,

r

$

r

r

r

s ub

r

,

r2,

r

$

r

r

r

m ult

r 1,

r

,

r

$

r

r

r

j mp

< addr

$

jump

to

< addr

C (^) MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^4)

Si

m

pl

e

ex

pr

e (^) ssions

PLUS

x ID

PL

U S

NU

M

yID

loa

d

r2,

r

$

r

addr(

x)

loa

di

r

,

4

$

cons

(^) tant

loa

d

r5,

r

$

r

addr(

y)

add

r6,

r3,

r 5

$

r

4

y

add

r7,

r2,

r 6

$

r

x + ( 4 + y )

C MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^7)

Co

n tro

l st

ruct

ure

s

Assi

gnm

(^) en

t st atem

ent

lhs

rhs

Stra

tegy

 ev

al uate

rhs

t o a

v al ue

an

r v alue

 ev

al uate

lhs

t o an

addr

e s s

an

lv alue

i )

lvalue

i s

regi

ste

r )

move

i t

ii (^) ) lv alue

i s

address

)

s tore

i t

Regist

ers

versu

(^) s mem

o r y

 non-

al (^) i ased

scal

ars

)

c an

go

i n

a regi

st er

 aggregat

e or

p ot

en

ti al l y al i ased

)

i n

m em

or

y

C (^) MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^8)

Co

n tro

l st

ruct

ure

s

if-

then-

else

ev

al uate

t h e e xpressi

on

t o

true

or

false

i f

true

f al l t h rough

to

then

part

br

a n c h

arou

nd

else

part

i f

fals

e , branc

h

t o

else

part

fal

l through

t (^) o

n e xt

st atem

en

t

Exam

pl (^) e

r 1

expr

evaluat

e t he

e xpr

essi

on

i f

not

(r1)

br

L

c omp

ar

e

and

br anch

. ..

s t m (^) t s f or

then

part

b r

L

br anch

t o

exit

L1:

. ..

s t m (^) t s f or

else

part

L2:

. ..

f ol lowi

ng

st m t

C MSC

(^430)

L ec ture

(^1) 2, (^) P age

(^9)

Co

n tro

l st

ruct

ure

s

whi

le

lo op

ev

aluate

the

c ontr

ol

expr

e s sion

if

false

(^) , b r an

c h

b eyond

e n d

of

lo op

if

true

, fal

l thr

oug

h

i nto

lo op

b o dy

at

end,

r e-e

valuate

the

c ontr

ol

expr

e ss ion

if

true

, b r anch

t o

top

of

lo op

b o dy

if

false

(^) , fal

(^) l thr

ough

Exam

pl (^) e

r 1

expr

evaluat

e t he

e xpr

essi

on

i (^) f

not

(r1)

br

L

c omp

ar e

and

br anch

L1:

. (^) ..

lo op

b o dy

r (^1)

expr

i f

r1,

br

L 1

L2:

. ..

f ol lowi

ng

st m t

T

est

at

end

)

si m

p l e

l o (^) op

i s

one

blo

c k

C MSC

(^430)

L ecture

(^12)

, P a ge

(^10)

Bo

o l ean

e xpressi

o ns

Tw

o sc h o (^) ol s of

t hough

t on

repr

e sen

t (^) ati

on:

Num

er ica

(^) l V al ue

s

 assi

g n

num

e ri c al

v alu

(^) e s to

t r ue

an

d

f alse

 ev

aluate

b o ole

an

s like

ar

(^) ithmet

i c ex

pr

essi

ons

Con

trol

Fl o w

 r e pr

esent

b o ole

an

v alu

e by

lo c ation

i n

c o de

 c on

v ert

to

num

e r ic al

value

w he

n

s (^) t or

e d

Nei

(^) ther

r e pr

e s e n t ati

(^) on

dom

i nat

(^) es

the

other.

C MSC

(^430)

L ecture

(^12)

, P a ge (^13)

Bo

o l ean

e xpressi

o ns

Num

er ica

(^) l V al ue

s

 assi

g n

a

value

t o

true

(say

1 )

 assi

g n

a

value

t o

fals

e

(say

0 )

 use

har

dwar

e |

and

, or

, n ot

, xo

(^) r

Cho

(^) ose

v al ues

that

m

ak

e t h e hardw

ar

e w ork.

C (^) MSC

(^430)

L ecture

(^12)

, P a ge (^14)

Bo

o l ean

e xpressi

o ns

Num

er ica

(^) l V al ue

s

Sour

c e E x p ressi

on

Generat

ed

C o de

b or

( c

and

not

d )

t

not

d

t

c an

d

t

t

b or

t

a

<

b

i f

(a

< b)

b r L

t

0

br

L

L

: t 1

1

L

: nop

A

n um

e r i cal

represen

tat

io n

handl

es

l ogi

c w el l.

C MSC

(^430)

L ecture

(^12)

, P a ge (^15)

Bo

o l ean

e xpressi

o ns

Con

trol

Fl o w

 use

c ondi

t i onal

br

an

c hes

an

d c omp

ar ator

 chain

of

br anche

s to

e valuate

expr

e s sion

 c o de

lo ok

s terri

b le

Contr

ol

ow

r e pr

esentat

i on

works

wel

(^) l f or

expr

essi

ons

i n

c ondi

t i onal

state

m (^) e n t s.

Cl

ean

u p:

 br an

(^) c h

to

next

stat

ement

 br an

(^) c h

to

b r an

(^) c h

C (^) MSC

(^430)

L ecture

(^12)

, P a ge (^16)

Bo

o l ean

e xpressi

o ns

Real

it y

 eithe

r appr

o ac

h

wor

k s f airly

wel

(^) l

 nu

(^) meric

al

c o de

r e

e c ts

lo g i c al

c onstru

c ts

 c on

t r ol

ow

c o de

works

wel

l for

r elat

i ons

 c om

piler

c an

cho

ose

b ase

d on

c onte

xt

Con

trol

o w

 ac

c ou

nting

ni

g htmar

e |

tr ac

kin

g lab

els

 b ack

p atc

hi

ng

i s t he

right

answer

C MSC

(^430)

L ecture

(^12)

, P a ge (^19)