CD Unit V - Material
CD Unit V - Material
Code enetation
Runthme
The
¬nironment 8
attocafion a n d deatloca ton o data Vasir1hla
objeck aze
Enunnment
sBates
name
stonge Vales
13
Heap p sttncatt
ocal Data t
i thim O Souice fProgranm along oith main
These links ae
optional y bcause eytne
wer doemo ue Subtunchisn whte developînq a
Source poqra but these ln ks aue activate d
ohenever Subfunctions e used lo prugram!
Acua paametut
Staht Allocahion
&stack Allbcahon
3- thap Attbcohom
4 shatic Atlocation:
A S he memoy veqcinow
Code fo (onsume
ud stoaqe totahions au
Corle 8 cduce Code kmown in aduance,
nuntme supdt packoo
Achivahin Retdd
F&memoy altotahn
8 Consun slatic deattocaRn is not
Oota Ttquue d
A chivahom Resd
| f s produce.
vaiables
onsune meon hohile Compiling poram
e stoed m mmemdy
Vasiabex oithin memdy,
medns aptestoinq
Droduce
vatiable s -fom umemdy.
acces
he Cormpilation data is bouod to a
m tRs alotaton
lbtaton in the mendy and
it doemot' chauge
red
when the prooram executes
Stack Atlocation6-
p a ae en and¥ pycedluve Catli and hely arivali,
An
etun vndae
Cond tunks and
Qte mamaged y mean qsta
Sawr dmachin e
States
memdy auotahon
Owors.1m ufo method and
lempdalles and
vCal data th attocaton shoaay is uey seful
atameks audF6 veculve oceure Call!
Aetutn Valuês
Cont nes and
Saved machine
States
Tempshe and
oCa Date.
Heap Atocation&
1. Call by value
actual pasame tës Qle valuaed
n Call by value
n pcedures
EX pogram Reference(tnput,output);
Va a,binteor
pYoceduvei Sosp (va y :inltger);
vah temp:inttg er ;
pascal
begn QnquOE.
s=omp s
nd
beqin
a t b-13
SwapCa,6)
o iteln (a =",a) s
unin (b=', b)
eud
Cau by Referencet
n Catl by refevence Cempiler ues addreas
vasl able
Ex t *x,*4$
Swap (1nt a, tot u)
tot tcmp
claog
poqraw
*y3
mobn c
Copy Restore e
* s alo Ccntled as Copy n, topybut
Tt a hybd blw CalL by value and Cal by
teference.
EXproqram Copyout( tnput,output)y
Van a : întqe"s
pceceue umtofe (vah t toltqa);
Dal (auquoq
a:0
eud
usten(a)
end
Name
4 Calt by
trad6onaty defned
as (opy rule pstolloase
Tt is
t teattd as maco -e; actua
1.The pwcedure
Such
Subsfitutd stor Pomaly
pasame tos ale
Catled a macoexpansion 6)
Substitution is
nlineunction-
au placed în palarthesis
9. The actua! posametos
f resewe the intqity
Ex Suoapt, alii) fy
alt-Aen
posibiono Code qeneiatovs
Sounce
font oamatat de rumko ode
end Code Code
opinitahin Nencoaton
Symbod
table
tRere
e e ale differenttupe g isues in the desgm
Code enelation
Subpmgams and
Subnuknes to be (ompiled
3memdy Momagment 6-
mames n the source pmaram to
Moppng the
areses data ebtels done by ront
end and the Code qenelatô" A în the name
Appwaches to Code
gmetatin isurst
Coe geneiah musB alon qcneiat the Csect
(ode t esenbal beCauwe a the o
Special Cases thal a Code enesatb mlqh
face. Some e tme desan qoals Code Cenas
Souncc
hile qenuaahg alp
C&t ucyan ctnei appilicalicn: aie
quide d bdE qntah ompsler
Easiy malitaiab
Testabl.
Ohrapmocheo the
o rcrencnaóye Kefe opi2eton)
4Maimleinab l
ADD CRo-2
mov Ro, a
-
Total cost s s
mov ba-3
ADO Ca 3
ADD Ra, * Ro -
2
ADD P, Ri
Mov. Pi, a
Conuert.an
Ihe man we Code geneatd fs-to
tnttrmediatu Code vepresentation
'into a Unea
enuencee machine insmueHons
Code qenesato we fastv nshurhions lew o-of
inshuckons, avoilable neqistu Onol auod
aedelet
ComputaNs
*Code qeneutd phases incude
4-Tnshurtion Selechon
Dnshon scheduin
3 Resis Aloca tion-
wemdy totadon
3 ai stud in memdy and y i sBded m
ne -2 MC V uxo-2 Va
e-3val n tuo3 V nt
mt me-3; nn
mone-45 Kop)
|oes/wytt ncs oyin
eyVauint u3va int
int bne-5 va lnt
nner stope
int one6
innu
int one_A; J
ept
3
pmg-tue co
int tuoo-1
i0to- 23
int two3 ion
fnt twou: ope3
Object Code Forms
Object code is the output generated by the code generation phase of a compiler. It represents the
lower-level, machine-specific code derived from the intermediate representation (IR) and is intended
to be executed on a specific hardware architecture.
Assembly Code
• A human-readable form of object code that uses mnemonic instructions instead of binary.
Ex:
MOV AX, BX; Move the value of BX into AX
ADD AX, 1; Increment the value in AX by 1
• Acts as an intermediate stage where the IR is translated into a form that can be further
processed into binary machine code by an assembler.
• Easier for humans to understand and debug, as it uses symbolic representations of machine
instructions.
Machine Code
• Linker: Combines multiple object files into a single executable, resolving references to
external symbols.
• Loader: Loads the executable file into memory, adjusting addresses and preparing the
program for execution.
For a simple expression like c = a + b * 5, the following illustrates the translation process
[[
12
Usage Counts:
A simple method of determining the savings to be realized by keeping variable x in a register
for the duration of loop L is to recognize that in our machine model we save one unit of cost
for each reference to x if x is in a register.
An approximate formula for the benefit to be realized from allocating a register to x within a
loop L is:
where,
-use(x, B) is the number of times x is used in B prior to any definition of x;
-live(x,B) is 1 if x is live on exit from B and is assigned a value in B and 0 otherwise.
Example: Consider the basic block in the inner loop in Fig 5.3 where jump and conditional
jumps have been omitted. Assume R0, R1 and R2 are allocated to hold values throughout the
loop. Variables live on entry into and on exit from each block are shown in the figure.
13