Compiler
Compiler
University
Run-time environments
Members ID
1. B e k a l u S i n t a y e h u UU91992R
2. M i c h a e l S o l o m o n UU91768R
3.
1
Click to edit Master title style
Definition
T
d
I
h
n
e
•
•
e
a
c
•
•
l
l
R
l
u
o
d
u
c
e
n
Allocation and Management: Handles memory and resources needed for target code and data objects.
Dyn amic Allocation : Det ermin ed d urin g prog ram execu tion (e.g ., malloc/free,
i
i
n
m
Run-Time Support Package: Provides services for allocation and deallocation of memory, loaded with the target code.
Dependency: Tailored to the programming language’ s semantics
g
e
m
E n
e
v
m
i
o
r
r
o
y
n m
a n
e
d
n t
r e
( R
s o
T
u
E )
r c
r
e
e
s
f
,
e r
e
s
i t
t
h
o
e r
t h
s
e
t a
i
t
n
i
f
c
r
a
a
l
s
l
t
y
r u
a
c
t
t
c
u
o
r e
m p
t
i
h
l
a
e
t
t
m
i m
a n
e
a
o
g
r
e s
d y
t
n
h
a
e
m
e
i
x
c
e
a l
c
l
u
y
t i
a
o
t
n
r
o
u
f
n
p
t
r
i
o
m
g
e
r
,
a m
d
s
e
,
p e
m
n
a
d
n
i
a
n
g
g
e s
o n
p r
t
o
h
g
e
r a
p
m
r o
e
g
x
r
e
a
c
m
u
m
t
i
i
n
o n
g l
b
a
y
n
a
g
l
u
l
a
o
g
c
e
a
'
t
s
i n
s
g
e m
a
a
n
n
d
t i c s .
2
2
Click to edit Master title style
Run-Time Resources
v
v
v
Initial Control: Program execution begins under the operating system's (OS) control through the loader.
Program Invocation Process:
v
Ø
Space Allocation: The OS allocates memory for the program.
Code Loading: The program's code is loaded into the allocated space.
Ø Execution Start: The OS transfers control to the program's entry point (e.g., main)
Ø
v
Ø
3
3
Click to edit Master title style
High Address
●
Stack
Dynamic Memory
Layout
●
●
Heap
●
●
Low Address 4
4
●
Click to edit Master title style
1. Sequential Execution:
v Control flows through the program in a w ell-defined order, step by step.
v
v W hen a procedure is called, control returns to the point immediately after the call upon completion.
1.
2.
3.
Procedure Activation:
v
v
v
v
v
v
1.
v
v
An activation occurs when a procedure is invoked, starting at the beginning of its body.
Lifetime: The duration from the first step of the procedure to its last step, including all procedures it calls.
Activation Characteristics:
Non-Overlapping or Nested: The lifetimes of procedure activations either do not overlap or are nested.
Recursive Activations: In recursive procedures, a new activation can begin before the previous one finishes, creating nested activations.
6
6
Click to edit Master title style
Re pres en tati on :
v The sequence of function cal ls is depicted as an activation tree..
Ke y Fe atu res :
•
1. Nodes:
• Each node represents a procedure acti vati on.
• The root node represents the acti vati on of the main program.
2. Parent-Chil d Relati onship:
• Node a is a parent of node b if control flows from a to b.
3.
4. Si bli ng O rde r:
5.
• Node a appears to the l eft of node b if a's lifetime occurs before b' s li fetime.
•
7
7
Click to edit Master title style
8
8
Click to edit Master title style
●
●
●
Stack
●
g ●
●
●
Other
Activation Tree
●
●
f ●
Space
Example(Cont.)
v
Main
f(3)
f(2)
f(1)
f(0)
g()
●
f ●
Data
●
f
●
●
f ●
●
main stack starts
• On many machines the Code(.txt)
at high-addresses and grows 9
9
towards lower addresses
Click to edit Master title style
Variable Scope
v
Definition:
Scope Rule:
v
v
v
Th e s c op e o f a v a ria ble re fe rs to the reg io n i n a prog ram w he re th e v ari ab le's d ec laratio n is v al id an d c an be a c c e s s ed .
procedure p;
var a: integer;
begin
a := 1; // ` a` is local
end;
10
10
Click to edit Master title style
Variable Scope(Cont.)
Key Insight:
Main Point
Ø The AR layout and the code generator must be designed together to
ensure seamless execution and efficient memory management.
12
12
Click to edit Master title style
Activation Records (AR)
An Activation Record (AR), also known as a procedure call frame, is a block of memory allocated to store
all necessary information for the execution of a function or procedure.
Components of an AR
●
Return
1.Return Value: Value
Stores the result of the function for the caller. May utilize machine registers for efficiency.
●
2.Actual Parameters: Actual
Contains arguments passed to the function by the caller. Parameters
3.Control Link:
●
Points to the AR of the caller, enabling the return of control after execution. Control Link
4.Access Link: ●
Access Link
References non-local variables located in other activation records.
5.Saved Machine Status:
●
Save Machine
Preserves the state of the machine (e.g., registers, program counter) before the procedure call.
●
Status
6.Local Data: ●
Local Data
Holds variables and constants specific to the function's execution. ●
7.Temporaries: Temporaries
Used for intermediate calculations or storing temporary values.
13
13
Click to edit Master title style
v
1. Class Main(){ AR
f( f(2) f(1) f(0) g
●
● ●
g(): -> Returns {1}; com
f(3)
f(2)
f(1)
link
Saved Registe Registe Registe Registe Registe
f(0)
g()
● ● ● ● ● ●
machin rs rs rs rs rs
e status
●
Local ●
none ●
none none ●
none none
data ●
●
Tempo ●
Used ●
Used ●
Used ●
Used ●
14
Used
raries 14
Creation of An
Click to edit Activation
Master Record(Cont.)
title style
●
●
●
●
Stack
●
Ret. Val. ●
● ●
●
3 ●
Other
●
●
Space
●
Data
●
(*)
●
Ret. Val. ●
●
●
2
Code(.txt)
●
(**)
15
15
Click to edit Master title style
Who Allocates?
• Caller:
Allocates some parts of the activation record before the procedure
is entered (e.g., actual parameters, control link).
• Example: When calling sum(a, b), the caller sets up a and b in the AR.
• Callee:
Allocates the remaining parts immediately after entering the
procedure (e.g., local variables, temporaries).
•Example: Inside sum, variables like result are allocated by the
callee.
•
Who Deallocates?
• Caller:
Deallocates the part it allocated (e.g., local variables).
1.Callee:
Deallocates the part it allocated (e.g., parameters, control
link).
2.
16
Click to edit Master title style
Caller:
• Allocates part of the activation record before the
procedure is called, including:
• Actual Parameters: Supplies arguments to
the callee
• Control Link: Points to the caller's activation record
for returning control
Callee:
•
• Allocates its portion after entering the
procedure, including:
• Local Data: Variables specific to the
procedure.
• Temporaries: Used for intermediate
calculations.
•
17
Caller Callee Example(cont.)
Click to edit Master title style
18
Variable Length Data
Click to edit Master title style
• Allocation Process:
• Variable-length data is allocated after
temporaries in memory.
• Ensures that the variable-length data does
not disrupt fixed-size fields (e.g., local
variables, saved machine state).
• Linking Mechanism:
• A link is maintained between the local data
and the allocated variable-length array to
ensure access.
Parameter Passing
During execution, the run-time environment is responsible for
managing the way parameters are passed to functions or
procedures. 1. By Value
2. By Reference
3. By Copy-Restore
4. By Name
19
Storage
Click Organization
to edit Master title style
The run-time environment organizes memory into distinct regions
to manage code execution, data storage, and dynamic allocation.
●
1.Target Code:
1. Contains the executable object code of the program. Stack
2. Loaded into a fixed memory region during program execution.
2.Static Data: ●
1. Holds global variables and constants with fixed memory ●
allocation.
2. Lifetime matches the entire execution of the program. ●
3.Control Stack:
1. Manages function calls, local variables, and return addresses. ●
●
**sp** (Stack Pointer)
Stack
●
• Static allocation:- Lay out storage at compile time only by studying the program text.
- Memory allocated at compile time will be in the static area. Timing Compiling time, Life time Entire
Program Execution
• Dynamic allocation:- Storage allocation decisions are made when the program is
running.
- Stack allocation:- Manage run-time allocation with a stack storage. Timing Runtime, Life time
Function Activation
22
Storage
Click Allocation
to edit forstyle
Master title Functions
Memory for function execution is allocated differently depending on the programming language and
its requirements. There are three primary strategies for allocating storage for functions.
1. Static Allocation
• All storage for data objects is allocated at compile time.
• Variables are stored globally, meaning their memory is fixed and persistent
throughout program execution.
• Local activation/deactivation of variables is managed by the symbol table.
• Recursion is very limited due to the lack of dynamic memory allocation.
•Example:
• Common in Fortran 77, where recursion is restricted or not supported.
23
Storage
Click Allocation
to edit forstyle
Master title Functions(Cont.)
2. Stack Allocation
•Key Features:
• Activation Records (AR) are created for each function call.
• ARs are pushed onto the stack during function calls and removed
when functions return, using a Last-In-First-Out (LIFO) order.
• Suitable for recursive functions as it dynamically manages memory
for each activation.
• Locals and parameters are stored in the AR and are deallocated when
the activation ends.
•Handling Variable Length Data:
• Pointers are used for variable-length data within the AR.
•Examples:
• C, Pascal, and most modern programming languages.
24
Storage
Click Allocation
to edit forstyle
Master title Functions(Cont.)
3. Heap Allocation
•Key Features:
• Used when stack allocation is not possible.
• Necessary for:
• Retaining local variables after function execution ends.
• Supporting functions that outlive their callers.
• Handling functions that return functions or data structures.
• Memory is dynamically allocated using malloc/free in C or new/delete
in C++.
•Use Cases:
• Memory-intensive applications with dynamic data structures.
• Managed by a garbage collector in some modern languages like Java
or Python.
25
Click to
Stack Vs.
edit
HEAP
Master
Allocation
title style
●
Stack ●
Heap
●
- Allocation/deallocation is automatic ●
- Allocation/deallocation is explicit
●
- Less expensive ●
- More expensive
●
- Space for allocation is limited ●
- Challenge is fragmentation
26
Click to edit
Content TitleMaster title style
27
27
Click to edit Master title style
28
Click to edit Master title style
Table
●
Title Title Title Title
29
29
Click to edit Master title style
Chart
£300,000
£250,000
£200,000
£150,000
£100,000
£50,000
£0
20YY 20YY 20YY 20YY 20YY
30
30
Click to edit Master title style
“
Quote appears here
Lorem ipsum dolor sit amet,
consectetuer adipiscing elit.”
- Author
31
31
Click to edit Master title style
Thank you
32
Click to edit Master title style
Thank you
33
Click to edit
Customise this
Master
Template
title style
Template editing
instructions and feedback
34
34