You are on page 1of 24

1

Lecture # 22
RunTimeEnvironments
Chapter7

RuntimeEnvironments
Beforecodegenerationstaticsourcetextofa
programneedstoberelatedtotheactionsthat
mustoccuratruntimetoimplementtheprogram.
Asexecutionproceedssamenameinthesource
textcandenotedifferentdataobjectsinthetarget
machine.
Theallocationanddeallocationofdataobjectsis
managedbytheruntimesupportpackage.

7.1SourceLanguageIssues
Eachexecutionofafunctionisreferredto
asanactivationoftheprocedure/function
Ifthefunctionisrecursiveseveralofits
activationsmaybealiveatthesametime

ProcedureActivationand
Lifetime
Aprocedureisactivatedwhencalled
Thelifetimeofanactivationofaprocedure
isthesequenceofstepsbetweenthefirst
andlaststepsintheexecutionofthe
procedurebody
Aprocedureisrecursiveifanewactivation
canbeginbeforeanearlieractivationofthe
sameprocedurehasended

ActivationTree
Weuseanactivationtreetodepictthewaycontrol
entersandleavesactivations.Inanactivationtree:
Eachnoderepresentsactivationofaprocedure
Therootrepresentsactivationofmainprogram
Thenodeforaistheparentofthenodeforbif
thecontrolflowsfromactivationatob
Thenodeforaistotheleftofnodeforbifthe
lifetimeofaoccursbeforethelifetimeofb

ProcedureActivations:Example

program sort(input, output)


var a : array [0..10] of integer;
procedure readarray;
var i : integer;
begin
for i := 1 to 9 do read(a[i])
end;
function partition(y, z : integer) : integer
var i, j, x, v : integer;
begin
end
procedure quicksort(m, n : integer);
var i : integer;
begin
if (n > m) then begin
i := partition(m, n);
quicksort(m, i - 1);
quicksort(i + 1, n)
end
end;
begin
a[0] := -9999; a[10] := 9999;
readarray;
quicksort(1, 9)
end.

Activations:
begin sort
enter readarray
leave readarray
enter quicksort(1,9)
enter partition(1,9)
leave partition(1,9)
enter quicksort(1,3)

leave quicksort(1,3)
enter quicksort(5,9)

leave quicksort(5,9)
leave quicksort(1,9)
end sort.

ActivationTrees:Example
s
r

q(1,9)

p(1,9)

q(1,3)

p(1,3)

q(1,0) q(2,3)

q(5,9)
p(5,9)

p(2,3) q(2,1) q(3,3)

q(5,5) q(7,9)
p(7,9) q(7,7) q(9,9)

Activationtreeforthesortprogram
Note:alsoreferredtoasthedynamiccallgraph

ControlStack
Theflowofcontrolofaprogram
correspondstodepthfirsttraversalofthe
activationtree
Controlstackisusedtokeeptrackofthe
liveprocedureactivations.
Theideaistopushthenodewhen
activationbeginsandpopwhenitends

FactorialExamplediscussed
publicstaticintFactorial(intnum)
{
if(num==1)
{
fact=1;
}
else
{
fact=fact+num*Factorial(num);
}
returnfact;
}

10

AdditionofElementsofArray
recursively

publicstaticintAdd(intindex)
{
if(index==0)
{
sum=arr[index];
}
else
{
sum=sum+Add(arr[index]);
}
returnsum;
}

11

ControlStack
Activationtree:
s
r
p(1,9)
p(1,3)

q(1,9)
q(1,3)
q(1,0) q(2,3)

Control
stack:
s
q(1,9)
q(1,3)
q(2,3)

Activations:
begin sort
enter readarray
leave readarray
enter quicksort(1,9)
enter partition(1,9)
leave partition(1,9)
enter quicksort(1,3)
enter partition(1,3)
leave partition(1,3)
enter quicksort(1,0)
leave quicksort(1,0)
enter quicksort(2,3)

12

ScopeRules
Environmentdeterminesnametoobject
bindings:whichobjectsareinscope?

Variablexlocallydeclaredinp

Afunctionx

program prg;
var y : real;
function x(a : real) : real;
begin end;
procedure p;
var x : integer;
begin
x := 1;

end;
begin
y := x(0.0);

end.

13

BindingofNames
Ifanameisdeclaredonceinaprogramit
canholddifferentvaluesatruntime
Thetermenvironmentreferstoafunction
thatmapsnametoastoragelocation
Thetermstateisreferredtoafunctionthat
mapsastoragelocationtothevalueheld
there

14

MappingNamestoValues
environment
name

state
storage

var i;

i := 0;

i := i + 1;

value

15

MappingNamestoValues
Atcompiletime

Atruntime

environment
name

state
storage

var i; i := 0; i := i + 1;
Environment and states are different
An assignment changes the state but not the
environment

value

16

StaticandDynamicNotionsof
Bindings
StaticNotion

DynamicNotion

Definitionofaprocedure

Activationsofthe
procedure

Declarationofaname

Bindingsofthename

Scopeofadeclaration

Lifetimeofabinding

17

StorageOrganization

Runtimestoragemaybesubdividedas:
Thegeneratedtargetcode
Dataobjects
Controlstacktokeeptrackofprocedure
activations
Thesizeoftargetcodeisfixedatcompiletime
Similarlysizeofdataobjectsmaybeknown

18

Typicalsubdivisionofruntime
memory
Aseparateareaofruntimememorycalled
Heapholdsallotherruntimeinformation
Code
Static
Data
Stack
Heap

19

StackAllocation
Activationrecords(subroutineframes)ontherun
timestackholdthestateofasubroutine
Callingsequencesarecodestatementstocreate
activationsrecordsonthestackandenterdatain
them
Callerscallingsequenceentersactualarguments,
controllink,accesslink,andsavedmachinestate
Calleescallingsequenceinitializeslocaldata
Calleesreturnsequenceentersreturnvalue
Callersreturnsequenceremovesactivationrecord

20

ActivationRecords
(SubroutineFrames)
fp
(framepointer)

Returnedvalue
Actualparameters
Optionalcontrollink
Optionalaccesslink

Callers
responsibility
toinitialize

Savemachinestatus
Localdata
Temporaries

Callees
responsibility
toinitialize

21

ControlLinks
Thecontrollinkistheold
valueofthefp
Callersactivationrecord

fp
sp

Controllink

Stack
growth

Calleesactivationrecord

22

AccessLinks(StaticLinks)
s

a x

a x

a x

a x

q(1,9)
access
k v

q(1,9)
access
k v

q(1,9)
access
k v

q(1,9)
access
k v

q(1,3)
access
k v

q(1,3)
access
k v

q(1,3)
access
k v

p(1,3)
access
i j

p(1,3)
access
i j

Theaccesslinkpointstothe
activationrecordofthestatic
parentprocedure:
sisparentofr,e,andq
qisparentofp

e(1,3)
access

23

AccessingNonlocalData
Toimplementaccesstononlocaldataain
procedurep,thecompilergeneratescodeto
traversenpnaaccesslinkstoreachthe
activationrecordwherearesides
npisthenestingdepthofprocedurep
naisthenestingdepthoftheprocedure
containinga

24

ParameterPassingModes
Callbyvalue:evaluateactualparametersand
enterrvaluesinactivationrecord

Callbyreference:enterpointertothestorageof
theactualparameter

You might also like