SlideShare a Scribd company logo
Run-Time Environment
Lecture 14
Run-time Environment
• Compiler must cooperate with OS and other system
software to support implementation of different
abstractions (names, scopes, bindings, data types,
operators, procedures, parameters, flow-of-control) on
the target machine
• Compiler does this by Run-Time Environment in
which it assumes its target programs are being
executed
• Run-Time Environment deals with
– Layout and allocation of storage
– Access to variable and data
– Linkage between procedures
– Parameter passing
– Interface to OS, I/O devices etc
Storage Organization
Code
Static
Heap
Free Memory
Stack
• Compiler deals with logical address space
• OS maps the logical addresses to physical addresses
Memory locations for code are determined at compile
time. Usually placed in the low end of memory
Size of some program data are known at compile time –
can be placed another statically determined area
Dynamic space areas – size changes during
program execution.
• Heap
• Grows towards higher address
• Stores data allocated under program control
• Stack
• Grows towards lower address
• Stores activation records
Typical subdivision of run-time memory
Run-Time Environments
• How do we allocate the space for the generated target
code and the data object of our source programs?
• The places of the data objects that can be determined at
compile time will be allocated statically.
• But the places for the some of data objects will be
allocated at run-time.
• The allocation and de-allocation of the data objects is
managed by the run-time support package.
– run-time support package is loaded together with the
generated target code.
– the structure of the run-time support package depends on the
semantics of the programming language (especially the
semantics of procedures in that language).
Procedure Activations
• Each execution of a procedure is called as activation of
that procedure.
• An execution of a procedure P starts at the beginning of
the procedure body;
• When a procedure P is completed, it returns control to the
point immediately after the place where P was called.
• Lifetime of an activation of a procedure P is the sequence of
steps between the first and the last steps in execution of P
(including the other procedures called by P).
• If A and B are procedure activations, then their lifetimes
are either non-overlapping or are nested.
• If a procedure is recursive, a new activation can begin
before an earlier activation of the same procedure has
ended.
A call graph is a directed multi-graph where:
• the nodes are the procedures of the program and
• the edges represent calls between these procedures.
Used in optimization phase.
Acyclic € no recursion in the program Can
be computed statically.
Call Graph
var a: array [0 .. 10] of integer;
procedure main
begin readarray(); quicksort(1,9); end
procedure readarray
var i: integer
begin … 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 i := partition(m,n); quicksort(m,i-1); quicksort(i+1,n) end
Main
quicksort
readarray
partition
Example: Call Graph
Activation Tree/ Call Tree
• We can use a tree (called activation tree) to show
the way control enters and leaves activations.
• In an activation tree:
– Each node represents an activation of a procedure.
– The root represents the activation of the main program.
– The node A is a parent of the node B iff the control
flows from A to B.
– The node A is left to to the node B iff the lifetime of A
occurs before the lifetime of B.
Activation Tree (cont.)
program main;
procedure s;
begin ... end;
procedure p;
procedure q;
begin ... end;
begin q; s; end;
begin p; s; end;
enter main
enter p
enter q
exit q
enter s
exit s
exit p
enter s
exit s
exit main
A Nested Structure
Activation Tree (cont.)
main
p s
q s
• Activation Tree - cannot be computed statically
• Dynamic – may be different every time the
program is run
main
r() q (1,9)
p (1,9) q(1,3) q (5,9)
p (1,3) q(1,0) q (2,3) p (5,9) q(5,5) q (7,9)
p (2,3) q(2,1) q (3,3) p (7,9) q(7,7) q (9,9)
Activation Tree
Paths in the activation tree from root to some node
represent a sequence of active calls at runtime
main
r() q (1,9)
p (1,9) q(1,3) q (5,9)
p (1,3) q(1,0) q (2,3) p (5,9) q(5,5) q (7,9)
p (2,3) q(2,1) q (3,3) p (7,9) q(7,7) q (9,9)
Run-time Control Flow
• Procedure call/return – when a procedure
activation terminates, control returns to the
caller of this procedure.
• Parameter/return value – values are passed
into a procedure activation upon call. For a
function, a value may be returned to the caller.
• Variable addressing – when using an
identifier, language scope rules dictate the
binding.
Implementing Run-time control flow
• Historically, the first approach to solve the run-
time control flow problem (Fortran)
• All space allocated at compile time
– Code area – machine instructions for each
procedure
– Static area / procedure call frame/ activation
record
• single data area allocated for each procedure.
– local vars, parameters, return value, saved registers
• return address for each procedure.
Static Allocation
Code area
Data area
for procedure
A
Data area
for procedure
BCode
generated
for A
Code
generated
for B
return addr return addr
Local data,
parameters,
return
value,
registers
for A
Local data,
parameters,
return
value,
registers
for B
Static Allocation
• When A calls B:
– in A: evaluate actual parameters and place into B’s
data area, place RA in B’s data area, save any
registers or status data needed, update the program
counter (PC) to B’s code
• When the call returns
– in B: move return value to known place in data area,
update PC to value in RA
– in A: get return value, restore any saved registers or
status data
Call/Return processing in Static Allocation
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
…
return
PC
Call tree:
A
Local data,
parameters,
return
value,
registers
for A
Local data,
parameters,
return
value,
registers
for B
Static Allocation
Static Allocation
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
…
return
L1
dataLocal
for A
Local data
for B
PC
Call tree:
A
B
Static Allocation
Code area Activation Activation
A:
call B
L1:
B:
…
return
for pro
A
cedure for procedure
B
L1
Local
for A
data Local data
for B
PC
Call tree:
A
Activation
for procedure
A
Code area
A:
call B
return addr
dataLocal
for A
Activation
for procedure
B
return addr
Local data
for B
B:
call B
L2:
What happens???
Static Allocation: Recursion?
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
call B
L2:
return
dataLocal
for A
Local data
for B
PC
Call tree:
A
Static Allocation: Recursion
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
call B
L2:
return
dataLocal
for A
Local data
for B
PC L1
Call tree:
A
B
Static Allocation: Recursion
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
call B
L2:
return
dataLocal
for A
Local data
for B
PC L2
Call tree:
A
B
B
Static Allocation: Recursion
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
call B
L2:
return
dataLocal
for A
Local data
for B
PC L2
Call tree:
A
B
Static Allocation: Recursion
Code area Activation
for procedure
A
Activation
for procedure
B
A:
call B
L1:
B:
call B
L2:
return
dataLocal
for A
dataLocal
for B
PC L2
Call tree:
A
B
We’ve lost
the L1 label
so we can’t
get back to
A
Static Allocation: Recursion
• Variable addresses hard-coded, usually as
offset from data area where variable is
declared.
addr(x) = start of x's local scope + x's offset
Runtime Addressing in Static Allocation
Need a different approach to handle recursion.
• Code area – machine code for procedures
• Static data – often not associated with
procedures
• Stack (Control Stack) – runtime information
Stack Allocation
Control Stack
• The flow of the control in a program corresponds to a
depth-first traversal of the activation tree that:
– starts at the root,
– visits a node before its children, and
– recursively visits children at each node an a left-to-right
order.
• A stack (called control stack) can be used to keep track
of live procedure activations.
– An activation record is pushed onto the control stack as the
activation starts.
– That activation record is popped when that activation ends.
– Dynamic – grows and shrinks
• When node n is at the top of the control stack, the stack
contains the nodes along the path from n to the root.
Variable Scopes
• The same variable name can be used in the different parts of
the program.
• The scope rules of the language determine which declaration of
a name applies when the name appears in the program.
• An occurrence of a variable (a name) is:
– local: If that occurrence is in the same procedure in
which that name is declared.
– non-local: Otherwise (ie. it is declared outside of that
procedure)
a is local
b is non-local
procedure p;
var b:real;
procedure q;
var a: integer;
begin a := 1; b := 2; end;
begin ... end;
Activation Records
• Information needed by a single execution of a
procedure is managed using a contiguous block of
storage called activation record.
• An activation record is allocated when a procedure is
entered, and it is de-allocated when that procedure
exited.
• Size of each field can be determined at compile
time (Although actual location of the activation
record is determined at run-time).
– Except that if the procedure has a local variable
and its size depends on a parameter, its size is
determined at the run time.
Activation Records (cont.)
return value
actual
parameters
optional control
link
optional access
link
saved machine
status
local data
temporaries
The returned value of the called procedure is returned
in this field to the calling procedure. In practice, we may
use a machine register for the return value.
The field for actual parameters is used by the calling
procedure to supply parameters to the called procedure.
The optional control link points to the activation record
of the caller.
The optional access link is used to refer to nonlocal data
held in other activation records.
The field for saved machine status holds information about
the state of the machine before the procedure is called.
The field of local data holds data that local to an execution
of a procedure..
Temporary variables is stored in the field of temporaries.
Activation Records (Ex1)
program main;
procedure p;
var a:real;
procedure q;
var b:integer;
begin ... end;
begin q; end;
procedure s;
var c:integer;
begin ... end;
begin p; s; end;
main
p
a:
q
b:
stack
main
p
q
s
Activation Records for Recursive Procedures
program main;
procedure p;
function q(a:integer):integer;
begin
if (a=1) then q:=1;
else q:=a+q(a-1);
end;
begin q(3); end;
begin p; end;
main
p
a: 3
q(3)
a:2
q(2)
a:1
q(1)
Stack (growing downward)Call Tree
Main
readarray
Main
a: array
readarray
i: integer
Stack Allocation for quicksort 1
Stack (growing downward)
Main
a: array
quick(1,9)
i: integer
Call Tree
Main
readarray quick(1,9)
Stack Allocation for quicksort 2
Stack (growing downward)Call Tree
Main
readarray quick(1,9)
quick(1,3)
quick(1,0)
p(1,9)
p(1,9)
Main
a: array
quick(1,9)
i: integer
quick(1,3)
i: integer
quick(1,0)
i: integer
Stack Allocation for quicksort 3
Frame pointer $fp:
points to the first
word of the frame,
Stack pointer ($sp):
points to the last
word of the frame.
The frame consists of
the memory between
locations pointed by
$fp and $sp
Layout of the stack frame
Creation of An Activation Record
• Who allocates an activation record of a procedure?
– Some part of the activation record of a procedure is
created by that procedure immediately after that
procedure is entered.
– Some part is created by the caller of that procedure
before that procedure is entered.
• Who deallocates?
– Callee de-allocates the part allocated by Callee.
– Caller de-allocates the part allocated by Caller.
Creation of An Activation Record (cont.)
return value
actual parameters
optional control link
optional access link
saved machine status
local data
temporaries
return value
actual parameters
optional control link
optional access link
saved machine status
local data
temporaries
Callee’s Responsibility
Caller’s Activation Record
Caller’s Responsibility
Callee’s Activation Record
Callee’s responsibilities before running
• Allocate memory for the activation record/frame by
subtracting the frame’s size from $sp
• Save callee-saved registers in the frame. A callee must save
the values in these registers ($s0–$s7, $fp, and $ra) before
altering them
[since the caller expects to find these registers unchanged after
the call.]
– Register $fp is saved by every procedure that allocates a new
stack frame.
– $ra only needs to be saved if the callee itself makes a call.
– The other callee-saved registers that are used also must be saved.
• Establish the frame pointer by adding the stack frame’s size
minus four to $sp and storing the sum in $fp.
• Pass arguments: By convention, the first four arguments
are passed in registers $a0–$a3.
Any remaining arguments are pushed on the stack and
appear at the beginning of the called procedure’s stack
frame.
• Save caller-saved registers: The called procedure can
use these registers ($a0–$a3 and $t0–$t9) without first
saving their value. If the caller expects to use one of these
registers after a call, it must save its value before the call.
• Execute a jal instructionwhich jumps to the callee’s first
instruction and saves the return address in register $ra.
Caller’s Responsibility
Call Processing: Callee
• Initializes local data, calculate the offset of each
variable from the start of the frame
• The executing procedure uses the frame pointer
to quickly access values in its stack frame.
For example, an argument in the stack frame can
be loaded into register $v0 with the instruction
lw $v0, 0($fp)
• Begins local execution
• If the callee is a function that returns a value, place
the returned value in a special register e.g. $v0.
• Restore all callee-saved registers that were saved
upon procedure entry.
• Pop the stack frame by adding the frame size to
$sp.
• Return by jumping to the address in register $ra.
Added at the ‘return’ point(s) of the function
Callee’s responsibilities on returning
• Restore registers and status
• Copy the return value (if any) from activation
• Continue local execution
Added after the point of the call
Return Processing: Caller
Any Question ?

More Related Content

What's hot (20)

PDF
Intermediate code generation in Compiler Design
Kuppusamy P
 
PPTX
Dag representation of basic blocks
Jothi Lakshmi
 
PDF
Syntax Directed Definition and its applications
ShivanandManjaragi2
 
PPT
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
PPTX
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
PPTX
A Role of Lexical Analyzer
Archana Gopinath
 
PPTX
Free Space Management, Efficiency & Performance, Recovery and NFS
United International University
 
PPT
Intermediate code generation
RamchandraRegmi
 
PPTX
Intermediate code generator
sanchi29
 
PPTX
Logics for non monotonic reasoning-ai
ShaishavShah8
 
PPTX
Principle source of optimazation
Siva Sathya
 
PPTX
Frames
amitp26
 
PPTX
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
PPT
Computer architecture pipelining
Mazin Alwaaly
 
PPTX
Demand paging
Trinity Dwarka
 
PPT
Infix prefix postfix
Self-Employed
 
PDF
Syntax directed translation
Akshaya Arunan
 
PDF
COMPILER DESIGN Run-Time Environments
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Code optimization
veena venugopal
 
PPTX
Lexical analyzer generator lex
Anusuya123
 
Intermediate code generation in Compiler Design
Kuppusamy P
 
Dag representation of basic blocks
Jothi Lakshmi
 
Syntax Directed Definition and its applications
ShivanandManjaragi2
 
Intermediate code generation (Compiler Design)
Tasif Tanzim
 
Unit iv(simple code generator)
Kalaimathi Vijayakumar
 
A Role of Lexical Analyzer
Archana Gopinath
 
Free Space Management, Efficiency & Performance, Recovery and NFS
United International University
 
Intermediate code generation
RamchandraRegmi
 
Intermediate code generator
sanchi29
 
Logics for non monotonic reasoning-ai
ShaishavShah8
 
Principle source of optimazation
Siva Sathya
 
Frames
amitp26
 
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Computer architecture pipelining
Mazin Alwaaly
 
Demand paging
Trinity Dwarka
 
Infix prefix postfix
Self-Employed
 
Syntax directed translation
Akshaya Arunan
 
Code optimization
veena venugopal
 
Lexical analyzer generator lex
Anusuya123
 

Viewers also liked (7)

PDF
Run time storage
Rasineni Madhan Mohan Naidu
 
DOCX
Compiler Design Material
Dr. C.V. Suresh Babu
 
PPTX
compiler ppt on symbol table
nadarmispapaulraj
 
PPTX
Symbol table design (Compiler Construction)
Tech_MX
 
PDF
Operator precedence
Akshaya Arunan
 
PPT
Code Optimization
guest9f8315
 
Run time storage
Rasineni Madhan Mohan Naidu
 
Compiler Design Material
Dr. C.V. Suresh Babu
 
compiler ppt on symbol table
nadarmispapaulraj
 
Symbol table design (Compiler Construction)
Tech_MX
 
Operator precedence
Akshaya Arunan
 
Code Optimization
guest9f8315
 
Ad

Similar to Lecture 14 run time environment (20)

PPT
Runtimeenvironment
Anusuya123
 
PPTX
Compiler Design_Run time environments.pptx
RushaliDeshmukh2
 
PDF
Intermediate code optimization Unit-4.pdf
Himanshu883663
 
PPT
Compiler 2011-8-re1
Ganesh Amirineni
 
PPT
Compiler 2011-8-re1
Ganesh Amirineni
 
PPTX
Activation Racords and Run-time Environments _11_10_2024.pptx
RagheshKrishnanK
 
PPT
chapter-7-runtime-environments.ppt
ssuser0db64b
 
PPTX
Run time administration
Arjun Srivastava
 
PPTX
Compiler Design Unit 4
Jena Catherine Bel D
 
PPTX
Lecture 15 run timeenvironment_2
Iffat Anjum
 
PDF
Pdf for storage static allocation in compiler design
b85769103
 
PDF
09 implementing+subprograms
baran19901990
 
PPT
Chapter Seven(1)
bolovv
 
PDF
-ImplementSubprogram-theory of programming
javariagull777
 
PPTX
10 implementing subprograms
Munawar Ahmed
 
PPTX
iii-ii cd nCompiler design UNIT-V-1.pptx
nandan543979
 
DOCX
CD CLASS NOTES- UNIT-4.docx
KANDE ARCHANA
 
PPTX
UNIT IV Compiler.pptx RUNTIMEENVIRONMENT
KavithaNagendran1
 
PPT
memory_allocation_use1.ppt
KanchanTiwari45
 
Runtimeenvironment
Anusuya123
 
Compiler Design_Run time environments.pptx
RushaliDeshmukh2
 
Intermediate code optimization Unit-4.pdf
Himanshu883663
 
Compiler 2011-8-re1
Ganesh Amirineni
 
Compiler 2011-8-re1
Ganesh Amirineni
 
Activation Racords and Run-time Environments _11_10_2024.pptx
RagheshKrishnanK
 
chapter-7-runtime-environments.ppt
ssuser0db64b
 
Run time administration
Arjun Srivastava
 
Compiler Design Unit 4
Jena Catherine Bel D
 
Lecture 15 run timeenvironment_2
Iffat Anjum
 
Pdf for storage static allocation in compiler design
b85769103
 
09 implementing+subprograms
baran19901990
 
Chapter Seven(1)
bolovv
 
-ImplementSubprogram-theory of programming
javariagull777
 
10 implementing subprograms
Munawar Ahmed
 
iii-ii cd nCompiler design UNIT-V-1.pptx
nandan543979
 
CD CLASS NOTES- UNIT-4.docx
KANDE ARCHANA
 
UNIT IV Compiler.pptx RUNTIMEENVIRONMENT
KavithaNagendran1
 
memory_allocation_use1.ppt
KanchanTiwari45
 
Ad

More from Iffat Anjum (20)

PPTX
Fog computing ( foggy cloud)
Iffat Anjum
 
PPTX
Cognitive radio network_MS_defense_presentation
Iffat Anjum
 
PPT
Lecture 16 17 code-generation
Iffat Anjum
 
PPTX
Lecture 12 intermediate code generation
Iffat Anjum
 
PPT
Lecture 13 intermediate code generation 2.pptx
Iffat Anjum
 
PPTX
Lecture 11 semantic analysis 2
Iffat Anjum
 
PPTX
Lecture 09 syntax analysis 05
Iffat Anjum
 
PPTX
Lecture 10 semantic analysis 01
Iffat Anjum
 
PPTX
Lecture 07 08 syntax analysis-4
Iffat Anjum
 
PPT
Lecture 06 syntax analysis 3
Iffat Anjum
 
PPT
Lecture 05 syntax analysis 2
Iffat Anjum
 
PPT
Lecture 03 lexical analysis
Iffat Anjum
 
PPT
Lecture 04 syntax analysis
Iffat Anjum
 
PPTX
Lecture 02 lexical analysis
Iffat Anjum
 
PDF
Lecture 01 introduction to compiler
Iffat Anjum
 
PPT
Compiler Design - Introduction to Compiler
Iffat Anjum
 
PPTX
Distributed contention based mac protocol for cognitive radio
Iffat Anjum
 
PPT
On qo s provisioning in context aware wireless sensor networks for healthcare
Iffat Anjum
 
PPT
Data link control
Iffat Anjum
 
PPT
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Iffat Anjum
 
Fog computing ( foggy cloud)
Iffat Anjum
 
Cognitive radio network_MS_defense_presentation
Iffat Anjum
 
Lecture 16 17 code-generation
Iffat Anjum
 
Lecture 12 intermediate code generation
Iffat Anjum
 
Lecture 13 intermediate code generation 2.pptx
Iffat Anjum
 
Lecture 11 semantic analysis 2
Iffat Anjum
 
Lecture 09 syntax analysis 05
Iffat Anjum
 
Lecture 10 semantic analysis 01
Iffat Anjum
 
Lecture 07 08 syntax analysis-4
Iffat Anjum
 
Lecture 06 syntax analysis 3
Iffat Anjum
 
Lecture 05 syntax analysis 2
Iffat Anjum
 
Lecture 03 lexical analysis
Iffat Anjum
 
Lecture 04 syntax analysis
Iffat Anjum
 
Lecture 02 lexical analysis
Iffat Anjum
 
Lecture 01 introduction to compiler
Iffat Anjum
 
Compiler Design - Introduction to Compiler
Iffat Anjum
 
Distributed contention based mac protocol for cognitive radio
Iffat Anjum
 
On qo s provisioning in context aware wireless sensor networks for healthcare
Iffat Anjum
 
Data link control
Iffat Anjum
 
Pnp mac preemptive slot allocation and non preemptive transmission for provid...
Iffat Anjum
 

Recently uploaded (20)

PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
community health nursing question paper 2.pdf
Prince kumar
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 

Lecture 14 run time environment

  • 2. Run-time Environment • Compiler must cooperate with OS and other system software to support implementation of different abstractions (names, scopes, bindings, data types, operators, procedures, parameters, flow-of-control) on the target machine • Compiler does this by Run-Time Environment in which it assumes its target programs are being executed • Run-Time Environment deals with – Layout and allocation of storage – Access to variable and data – Linkage between procedures – Parameter passing – Interface to OS, I/O devices etc
  • 3. Storage Organization Code Static Heap Free Memory Stack • Compiler deals with logical address space • OS maps the logical addresses to physical addresses Memory locations for code are determined at compile time. Usually placed in the low end of memory Size of some program data are known at compile time – can be placed another statically determined area Dynamic space areas – size changes during program execution. • Heap • Grows towards higher address • Stores data allocated under program control • Stack • Grows towards lower address • Stores activation records Typical subdivision of run-time memory
  • 4. Run-Time Environments • How do we allocate the space for the generated target code and the data object of our source programs? • The places of the data objects that can be determined at compile time will be allocated statically. • But the places for the some of data objects will be allocated at run-time. • The allocation and de-allocation of the data objects is managed by the run-time support package. – run-time support package is loaded together with the generated target code. – the structure of the run-time support package depends on the semantics of the programming language (especially the semantics of procedures in that language).
  • 5. Procedure Activations • Each execution of a procedure is called as activation of that procedure. • An execution of a procedure P starts at the beginning of the procedure body; • When a procedure P is completed, it returns control to the point immediately after the place where P was called. • Lifetime of an activation of a procedure P is the sequence of steps between the first and the last steps in execution of P (including the other procedures called by P). • If A and B are procedure activations, then their lifetimes are either non-overlapping or are nested. • If a procedure is recursive, a new activation can begin before an earlier activation of the same procedure has ended.
  • 6. A call graph is a directed multi-graph where: • the nodes are the procedures of the program and • the edges represent calls between these procedures. Used in optimization phase. Acyclic € no recursion in the program Can be computed statically. Call Graph
  • 7. var a: array [0 .. 10] of integer; procedure main begin readarray(); quicksort(1,9); end procedure readarray var i: integer begin … 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 i := partition(m,n); quicksort(m,i-1); quicksort(i+1,n) end Main quicksort readarray partition Example: Call Graph
  • 8. Activation Tree/ Call Tree • We can use a tree (called activation tree) to show the way control enters and leaves activations. • In an activation tree: – Each node represents an activation of a procedure. – The root represents the activation of the main program. – The node A is a parent of the node B iff the control flows from A to B. – The node A is left to to the node B iff the lifetime of A occurs before the lifetime of B.
  • 9. Activation Tree (cont.) program main; procedure s; begin ... end; procedure p; procedure q; begin ... end; begin q; s; end; begin p; s; end; enter main enter p enter q exit q enter s exit s exit p enter s exit s exit main A Nested Structure
  • 11. • Activation Tree - cannot be computed statically • Dynamic – may be different every time the program is run main r() q (1,9) p (1,9) q(1,3) q (5,9) p (1,3) q(1,0) q (2,3) p (5,9) q(5,5) q (7,9) p (2,3) q(2,1) q (3,3) p (7,9) q(7,7) q (9,9) Activation Tree
  • 12. Paths in the activation tree from root to some node represent a sequence of active calls at runtime main r() q (1,9) p (1,9) q(1,3) q (5,9) p (1,3) q(1,0) q (2,3) p (5,9) q(5,5) q (7,9) p (2,3) q(2,1) q (3,3) p (7,9) q(7,7) q (9,9) Run-time Control Flow
  • 13. • Procedure call/return – when a procedure activation terminates, control returns to the caller of this procedure. • Parameter/return value – values are passed into a procedure activation upon call. For a function, a value may be returned to the caller. • Variable addressing – when using an identifier, language scope rules dictate the binding. Implementing Run-time control flow
  • 14. • Historically, the first approach to solve the run- time control flow problem (Fortran) • All space allocated at compile time – Code area – machine instructions for each procedure – Static area / procedure call frame/ activation record • single data area allocated for each procedure. – local vars, parameters, return value, saved registers • return address for each procedure. Static Allocation
  • 15. Code area Data area for procedure A Data area for procedure BCode generated for A Code generated for B return addr return addr Local data, parameters, return value, registers for A Local data, parameters, return value, registers for B Static Allocation
  • 16. • When A calls B: – in A: evaluate actual parameters and place into B’s data area, place RA in B’s data area, save any registers or status data needed, update the program counter (PC) to B’s code • When the call returns – in B: move return value to known place in data area, update PC to value in RA – in A: get return value, restore any saved registers or status data Call/Return processing in Static Allocation
  • 17. Code area Activation for procedure A Activation for procedure B A: call B L1: B: … return PC Call tree: A Local data, parameters, return value, registers for A Local data, parameters, return value, registers for B Static Allocation
  • 18. Static Allocation Code area Activation for procedure A Activation for procedure B A: call B L1: B: … return L1 dataLocal for A Local data for B PC Call tree: A B
  • 19. Static Allocation Code area Activation Activation A: call B L1: B: … return for pro A cedure for procedure B L1 Local for A data Local data for B PC Call tree: A
  • 20. Activation for procedure A Code area A: call B return addr dataLocal for A Activation for procedure B return addr Local data for B B: call B L2: What happens??? Static Allocation: Recursion?
  • 21. Code area Activation for procedure A Activation for procedure B A: call B L1: B: call B L2: return dataLocal for A Local data for B PC Call tree: A Static Allocation: Recursion
  • 22. Code area Activation for procedure A Activation for procedure B A: call B L1: B: call B L2: return dataLocal for A Local data for B PC L1 Call tree: A B Static Allocation: Recursion
  • 23. Code area Activation for procedure A Activation for procedure B A: call B L1: B: call B L2: return dataLocal for A Local data for B PC L2 Call tree: A B B Static Allocation: Recursion
  • 24. Code area Activation for procedure A Activation for procedure B A: call B L1: B: call B L2: return dataLocal for A Local data for B PC L2 Call tree: A B Static Allocation: Recursion
  • 25. Code area Activation for procedure A Activation for procedure B A: call B L1: B: call B L2: return dataLocal for A dataLocal for B PC L2 Call tree: A B We’ve lost the L1 label so we can’t get back to A Static Allocation: Recursion
  • 26. • Variable addresses hard-coded, usually as offset from data area where variable is declared. addr(x) = start of x's local scope + x's offset Runtime Addressing in Static Allocation
  • 27. Need a different approach to handle recursion. • Code area – machine code for procedures • Static data – often not associated with procedures • Stack (Control Stack) – runtime information Stack Allocation
  • 28. Control Stack • The flow of the control in a program corresponds to a depth-first traversal of the activation tree that: – starts at the root, – visits a node before its children, and – recursively visits children at each node an a left-to-right order. • A stack (called control stack) can be used to keep track of live procedure activations. – An activation record is pushed onto the control stack as the activation starts. – That activation record is popped when that activation ends. – Dynamic – grows and shrinks • When node n is at the top of the control stack, the stack contains the nodes along the path from n to the root.
  • 29. Variable Scopes • The same variable name can be used in the different parts of the program. • The scope rules of the language determine which declaration of a name applies when the name appears in the program. • An occurrence of a variable (a name) is: – local: If that occurrence is in the same procedure in which that name is declared. – non-local: Otherwise (ie. it is declared outside of that procedure) a is local b is non-local procedure p; var b:real; procedure q; var a: integer; begin a := 1; b := 2; end; begin ... end;
  • 30. Activation Records • Information needed by a single execution of a procedure is managed using a contiguous block of storage called activation record. • An activation record is allocated when a procedure is entered, and it is de-allocated when that procedure exited. • Size of each field can be determined at compile time (Although actual location of the activation record is determined at run-time). – Except that if the procedure has a local variable and its size depends on a parameter, its size is determined at the run time.
  • 31. Activation Records (cont.) return value actual parameters optional control link optional access link saved machine status local data temporaries The returned value of the called procedure is returned in this field to the calling procedure. In practice, we may use a machine register for the return value. The field for actual parameters is used by the calling procedure to supply parameters to the called procedure. The optional control link points to the activation record of the caller. The optional access link is used to refer to nonlocal data held in other activation records. The field for saved machine status holds information about the state of the machine before the procedure is called. The field of local data holds data that local to an execution of a procedure.. Temporary variables is stored in the field of temporaries.
  • 32. Activation Records (Ex1) program main; procedure p; var a:real; procedure q; var b:integer; begin ... end; begin q; end; procedure s; var c:integer; begin ... end; begin p; s; end; main p a: q b: stack main p q s
  • 33. Activation Records for Recursive Procedures program main; procedure p; function q(a:integer):integer; begin if (a=1) then q:=1; else q:=a+q(a-1); end; begin q(3); end; begin p; end; main p a: 3 q(3) a:2 q(2) a:1 q(1)
  • 34. Stack (growing downward)Call Tree Main readarray Main a: array readarray i: integer Stack Allocation for quicksort 1
  • 35. Stack (growing downward) Main a: array quick(1,9) i: integer Call Tree Main readarray quick(1,9) Stack Allocation for quicksort 2
  • 36. Stack (growing downward)Call Tree Main readarray quick(1,9) quick(1,3) quick(1,0) p(1,9) p(1,9) Main a: array quick(1,9) i: integer quick(1,3) i: integer quick(1,0) i: integer Stack Allocation for quicksort 3
  • 37. Frame pointer $fp: points to the first word of the frame, Stack pointer ($sp): points to the last word of the frame. The frame consists of the memory between locations pointed by $fp and $sp Layout of the stack frame
  • 38. Creation of An Activation Record • Who allocates an activation record of a procedure? – Some part of the activation record of a procedure is created by that procedure immediately after that procedure is entered. – Some part is created by the caller of that procedure before that procedure is entered. • Who deallocates? – Callee de-allocates the part allocated by Callee. – Caller de-allocates the part allocated by Caller.
  • 39. Creation of An Activation Record (cont.) return value actual parameters optional control link optional access link saved machine status local data temporaries return value actual parameters optional control link optional access link saved machine status local data temporaries Callee’s Responsibility Caller’s Activation Record Caller’s Responsibility Callee’s Activation Record
  • 40. Callee’s responsibilities before running • Allocate memory for the activation record/frame by subtracting the frame’s size from $sp • Save callee-saved registers in the frame. A callee must save the values in these registers ($s0–$s7, $fp, and $ra) before altering them [since the caller expects to find these registers unchanged after the call.] – Register $fp is saved by every procedure that allocates a new stack frame. – $ra only needs to be saved if the callee itself makes a call. – The other callee-saved registers that are used also must be saved. • Establish the frame pointer by adding the stack frame’s size minus four to $sp and storing the sum in $fp.
  • 41. • Pass arguments: By convention, the first four arguments are passed in registers $a0–$a3. Any remaining arguments are pushed on the stack and appear at the beginning of the called procedure’s stack frame. • Save caller-saved registers: The called procedure can use these registers ($a0–$a3 and $t0–$t9) without first saving their value. If the caller expects to use one of these registers after a call, it must save its value before the call. • Execute a jal instructionwhich jumps to the callee’s first instruction and saves the return address in register $ra. Caller’s Responsibility
  • 42. Call Processing: Callee • Initializes local data, calculate the offset of each variable from the start of the frame • The executing procedure uses the frame pointer to quickly access values in its stack frame. For example, an argument in the stack frame can be loaded into register $v0 with the instruction lw $v0, 0($fp) • Begins local execution
  • 43. • If the callee is a function that returns a value, place the returned value in a special register e.g. $v0. • Restore all callee-saved registers that were saved upon procedure entry. • Pop the stack frame by adding the frame size to $sp. • Return by jumping to the address in register $ra. Added at the ‘return’ point(s) of the function Callee’s responsibilities on returning
  • 44. • Restore registers and status • Copy the return value (if any) from activation • Continue local execution Added after the point of the call Return Processing: Caller