0% found this document useful (0 votes)
4 views

Unit-4 (Issues in Design of a Code Generator)

The document discusses code generation in compilers, focusing on the transformation of intermediate representations into target machine code. It covers various aspects of code generation, including register allocation, instruction selection, and the generation of different types of target code such as absolute and relocatable machine code. Additionally, it highlights the importance of optimizing code and managing resources effectively during the code generation process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit-4 (Issues in Design of a Code Generator)

The document discusses code generation in compilers, focusing on the transformation of intermediate representations into target machine code. It covers various aspects of code generation, including register allocation, instruction selection, and the generation of different types of target code such as absolute and relocatable machine code. Additionally, it highlights the importance of optimizing code and managing resources effectively during the code generation process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Code Gfeneratton

Final phae tn
Compiler. Miodel
. Ta7es inaut as. i intes me diate (code )
Pepresentation CIR)along: klitt Symbol tale
and Prpduue
efuivalent
cutput a s a
target Tmgay Semanttlasly
Sewp ront end Code
Btaiz Csdee yabes tage t
pttmze
Progam lntemudkate Lnbemdiate
Cade
Porogram
code
- Code optrniter and Code generato s
Often referedto as bach end
Cade qeneratohad,tare Primary tasse.
tnnstzclrion. Selckion,
Registesallotalton.and assignment
lnstcion ordeoing
Isshesvin Design
Code
Code,Jeneator
Tereakvr"
The tnput to te Code generabor i's
the intermediat ee epresentatton
Pro goam along hlite tPre informnation
Symbol Table

-Symbol table nsed to


determine tre runttme addess

data objet denoted by t e a m e s . Là tR.


Rncude;i
Threeddsess iode
gradruples .

*Triples
*ndirect Triples
maehihe Representaios.
ais Bjte code .
*Stach machine:s code
Represerntatron
tPosfi x. otatn
Graphite! Represen tatton
.DAG

ln thiue
Sytaz
phase'Tle asaUe
assine syntactteand
Seman tc" s:have beenA detected
meressay
Cone sion
type
hare cherhtng
been tahen plae
and
type
assume enfut
these
arget Poq1am :
Codeeqeneatos, Jenera/es. direrent
typesof;: tasget: codesisincluding .
Absolute Machine Progran
kaagrage Pegan
- Relo catate
CoBject.code. )) "
Assembly kangnage Dqsan

tarqet
imp a t t . : n
hignt code.
Assenanchitekue
aLhine has a

generatq
Signiticant

thati Ppdures
Of t e .

-machihe codi.
quality
A.MsSts Cornnon. tasget ma'hine

arhiteefure,r Q e·.*:.i
-RSe.(Reluced stnscktbr set compute)
-ci[c (.complex lnstutton set Compute)
RisC achine,i.has
- ManyRegisters
-Three addretinsiutrpns,:
isimple Addaesing mde and
inHuetion.set
Set a3chi tects%
CI SCsmacfihe.,sshas
Fe4Regis1s
"TTo SAddsess InstauLftons
rapiely ot'Addressin. i2nade
Sereral hegisterClauis: i ,it
yaiabie tergt. nstuctione
kli te:Sdeeffees
* Absòlute ach'ne'.
has tre adrantage that
Langnage Program
Fixed locaton, Meno y
in

- lmcdi ate, Exeuution..


Relo caFables Machine fanguage Pro gra
Subprograms to be tompileSeparate
Set. of elicatable Gßject mbdule
be
Linhed togethes anelboadad:o
exeitton
Iinhin
lenying loadey.
* Astembly
marest Aanguae 2Aaes.atieatoon.
theProie s ö.code'qeneration Zomai
>easeo.
Gene sate SynbolicaShs ebrn an
Use the naeofaiite Ot, the: assembler
to gereste tode .:
3. lnstr u | t t o n s e l e t e o h s r )
The code qeneratormust m4p the
into code isejuente that Can be
exeuted, ' by tre tarq
basqet machine.
.The Compleaite bfiPeiforming tis
mapping isis deteriined byi the fatos
Snch.sas.

Natuse o the, lnstueton set, Archiettuse


r The desired quality: f the Jenerated Code.
* For each tyPe ot trsee qddress Codehle can desiqn
tasgetode
Three address Statement
CAn be transltey
rnto

ADD Re,
ST

The:iabore srategy otten, Proalnas


7edndant toads ahd Stoses. t niti

se, Tiianslated. intD

3ADD:
ST a, Ro Redndant Loa
nd Stogei
ÄDD Ro Roves
d, Ro
.
tasqet mathine has
an lnrement instaution N) then..
three addressi Statement i a :Qt
le mented.by,.
LNCa::
Jettoni ng' Code.s

ADD Ro.j Ro#^


ST a)Ro

Reqìster AllocaHon.

hey Porblérn *a. code genevatioo


A
is deciding hat Ýalues to hold ch huhat
Reqisiex
The :Reqistex: 0tten

Snbdivided . int koo iSubpobleriss:s


4.Registes Allocation ’ During which kle sele
Set Variables tfaat: hlil resite tn
the
egistex each point.i tte Proqoam.
at

Register Aignment during lhith We piay


toat. a Yasiable oil
tre Speiftc ngiates
'estde t4..
s. Eraludtton Ordeg.
THe odes in nohh Computattons
Perfosmed Can afeck the
code·
tarqet best ovder in the genera l
- PRing xP- complete Posblem.
bS CaSe di ffrcutt
A ASimple Code Geieratoà..
i Regiskeg. andaddess deseriplors
a. The,.code genezatten Hgorihig.,es!:
3. Design 0f tie Fnctioret Keg: it:

code generatoai conrert intermediqte.


epresentation into taget Progran.

Code..qengrattoa S
thè.Primary
desiding how: to nse
I2gistex
There13:ae Four Prinipal, nsesof1:
reqistes
) ÍoG t:'máthine. iarchi teetuseome |0w
all gperand! Shoutdl be in,;KRgistex;
cu) Regiskers : Stor keporarict
(zü) Reqistess hod9lsfal Vales
. (iw. Registex f ten nsed to he. -zLntime
sterage. management

Generà<.i fem,t;mathine lnstructoo

reg,regeq
4. Regis ter and add vess Destripc
Rezister: destriptis jeeps trach of tie
the.
Yariables nameS Dhose Öurent:.alues
in the' "Registesui
Addess descriptor Heeps tract
Location locations iAShere theceNt
ValùeNÖf: tiati ariable .can besteund.

2:'The Code Geheration Algoritm. .


Thei fianit on qee KejcI).Ohich , Selects
Tegister for each memosy Locaton associated
lit the - tnLtton. .
lnstruttion for spe rattoni
Memory
For tobeeaddsessi intrutton a G= yt2.
do tre folowing
to Selet ieqisters
getReg(yr)
for c, and i Cail these:. Rx, Ry and Ry
Rz
IS.. yisnot lò. Ry, ISSNe: Ko. Ryy
’ smi lasly, I Z: inoti.in Bz:s 2$Sne
D
IsS ne .Ra, Ry,z
instsuutiDn ADD

Memory lnstnueti'onfor .oeY


Copy Statemert
For thre addies inslzuctton xy.
-

yis mat\, i Ry
y is aheady inn Ry, change Registeo
ofRy that i\sinelydes
as'one.o teralue tn Ry:.

Endinqithebasicibioeh:
For tênporary yalues do- groting
The yalues nhith tsty. afte
the ibo chtHi f s : ' found Some
here'nnermony sto She ,the

lnstructton

gster. andaddress descsipter


Managi LD.e,x
-For instrcion
tFre 'register: destriptv
change orly
egiste R s`it:hold
¢ Change he' add tes descripkor f
bi additacding: egis teir Ras, san
additonaf. Cocatton.
change tie
addsiss,idescripBor
meme Locatton

- For an speraion snehàs,A Rx, Ey, Ez


the Reqister descnplos for Rx.
change the
So that: it -hols nty,x
Chiange the: addrs6 descriptor forz.
So that i ¿ts onlylocation,. ts, Rx.
Remove Rx tre addresS descipto
any raziable. .se otpes, khan 1.
Cop statement. 4
Add to Register degnptes Py
change tie address detiptos for
Ony. kocaton.R:

3. Desiqn the.knckon getkag


Rules to inmplement qet keg I) for
three.*addess\s Instiuton. I
Tf y is altady
n a registers Hega
registe albeady: Göntaintng yi as Ry.
"I: t i ì aegiSteo
y isia but tee
n
registe cuinty
Shch reqistea ,asRy
egiste
- ith Y a u e t h e s , v s not
registes
meeded future
really
D
ther Locaton
. o s t i biH tysi.
addess eleseipPor. of v saye,
Somerohene beside s.Rsh:
-If

thëopesängss
it ris iot.n: used:s.Lateooh.
then ISSSLe $T V,R

spimal Code Generaton, foo Eepression

2. Generatng Code from Labeled Pxpresio


Tree
Exprrss) on alitn an lnguffhes
3. Evalnatng
Suppy ofRegistà »
.1. Ershor Dumbes:
humbes 'to eatk hode' hi
expression
Asining
tree that tells hono many egistes
evaluate
the node. This
ire needed to
nuber caled Er&hov nunbex
Rules
x4.Kabel atls. keare node
. The abel Of.an interi. node li t
Ckid... is label o child;
in,3. Thé.-tabeli- öt:an! eTOY pde, :kih
ait dffeientia .
tiose
oits,childrer
One plns tha Label
the dabels are ? the ame :
i

tg='e +t

(t

Genevati ng .3Code:foonm labelkd.iexpression


Tree
4.Generate. machinE Code:
and. froe: child enis
nterior nade
abel . alit:.equal Label, baeb
’ Recissireis generate i c defor the
hi nsingiti base
base " bt|, -The Tesnlt
of riqnt çhite appears insegisteeKith-!
Jenerate Code fori the left
sRersiveg
chiid
using base b: the resnut appeas in

- Generate the instruction, eP bttl

2. F r interior. rode klitf. Label an


childen Nit wnequal: kabels.
h’ Label fo: bìg chi.
for littte child:
m’ kabel
b base
Reursiely generate Code fer te
child nsing base b. Ihe :esnlt appeao
bi
in registes Rbth-t
RetuzBirely:generate Code foi. the
IiHte child, wsihg base b. 7he resnt appeat
in the egistes Kbtm-J
Ceneráte the instructon'
PBth Bbtm-bt7
, Bstl, Rbtm
a-For Leat representi ng spesand %, the
base. is bgeKEAte the :itnstructon
bD

base-Root -|
Next 'hered.-2
LD .R3,:d
LD

ADD :R3,Re, R
LO.Rasb
LD Ri

i3ADDRà, R, R
3. Eraluatng. Expressione:,ii aa lnsnffcient
Supply : Regste i
lhen. these are feje regìstes availaHe.
than the Label the, ot of toee.
Need to add Sbis Some instauetton
to Bko ie Value Subtsee into memo y .
and We. need to load trat values baet
ìnto egistes as neeged:

Modifcatton klith Pertons. algorithn inejo


klith prerions Labe- k
ngde
On eachi Side: to. picy
Larqe4. NodeSubN treeto;stoe
has
Vafne
atteast One -child klith Label a

|(O greaten Pi_ch tbe Larger child bo big" ane

e. Recu v8ireiy Code.fo tre bg child.


nsing b l, he esut : ani table 4 2,
3. Geneate mathhe Insluti on ST by ,P>.
4. Generate Code fox itele. chid as foll ows.
¢ Tr littte Chid has label o o) geateo, b:/
H. label
ttis algoithim. 7he.
Reuosirely ppy is
Resue t ppear
s. Gene vate te lnstructton LD Rr »t;
häg child N s toe ight
child N, toen generate te instoucton
toc is he,
Rr, Ro, Ro-l
Left Child generate
St.X.C.C.E Page No.

ex:
LD

LD

LD: Rye
ÀDD .R RiP

LD Kja
ADD RR22

You might also like