0% found this document useful (0 votes)
2 views34 pages

Compiler

The document discusses run-time environments, focusing on memory allocation, program execution, and variable lifetime. It outlines static and dynamic allocation, the role of the operating system in program invocation, and the structure of activation records. Additionally, it covers variable scope, activation trees, and the relationship between procedure activation and memory management.

Uploaded by

mckienzie12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views34 pages

Compiler

The document discusses run-time environments, focusing on memory allocation, program execution, and variable lifetime. It outlines static and dynamic allocation, the role of the operating system in program invocation, and the structure of activation records. Additionally, it covers variable scope, activation trees, and the relationship between procedure activation and memory management.

Uploaded by

mckienzie12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODP, PDF, TXT or read online on Scribd
You are on page 1/ 34

Unity

Click to edit Master title style

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

S tatic Allocation: Pre-determin ed d u ring comp ilation .


a
s
-
t
T

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

Memory Layout Uninitialized


Data

(BSS)
Static Memory

Initialized Data Layout

(DS)


Low Address 4
4

Click to edit Master title style

Assumptions about Execution


Do These Assumptions Always Hold?
No, these assumptions do not always hold due to:
v

1. Sequential Execution:
v Control flows through the program in a w ell-defined order, step by step.
v

2. Procedu re Call an d Retu rn:


v

v W hen a procedure is called, control returns to the point immediately after the call upon completion.

• Concurrent/Parallel Execution: Programs can execute multiple threads or


processes simultaneously, breaking sequential control flow.
• Event-Driven Programming: Control flow is determined by events (e.g., user
inputs, signals) rather than a strict sequence.
• Non-Returning Procedures: Some calls (e.g., system calls, exceptions) may not 5
5
return to the caller directly.
Click to edit Master title style

Procedure Activation and


Variable Lifetime
v

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.

Variable Lifetime Vs. Scope


Lifetime: The period during execution when a variable is defined; a dynamic concept.
Scope: The region of the program where the variable is accessible; a static concept.

6
6
Click to edit Master title style

Procedure Activation Tree


v
De fin iti on :
v
v
v
v
An activation refers to a specific invocation of a function, with its own parameter values.
An activation can i nvoke another activation before it becomes inactive.

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

Activation Tree Example


v
1. Class Main(){
g(): -> Returns {1};
f(x: INT): -> Returns {
if x == 0 then g()
else f(x – 1)
fi
};
main(): { f(3); };
}

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 .

Th es e rul es de termin e w h ic h v ari ab le d ec la ration ap pl ies w he n a v a ria bl e n am e is refe re nc ed in a p ro gra m.


Variable Occurrence:

1. L o c a l:
• Th e v a ria ble is de c la re d w ithin th e s am e pro c e du re or blo c k w he re i t is re feren c e d.
• Ex am p le:

procedure p;
var a: integer;
begin
a := 1; // ` a` is local
end;

10
10
Click to edit Master title style

Variable Scope(Cont.)

•Lexical Scope: Most programming languages (e.g., C, Python)


use lexical (or static) scope, where the structure of the code
determines scope.
•Dynamic Scope: Some languages allow dynamic scope, where
the calling context determines variable access. 11
11
Click to edit Master title style

Ø The compiler is responsible for determining the layout of activation


records (ARs) at compile-time.
Ø It must also generate code to correctly access locations within the
activation record.

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(x: INT): -> Returns {


pon 3)
ent
if x == 0 then g() ●
Return ●
Stored ●
Stored ●
Stored ●
Stored ●
1
value
else f(x – 1) ●
Actual ●
3 ●
2 ●
1 ●
0 ●
none
fi parame
ter
}; ●
Control ●
Main f(3)AR ●
f(2)AR ●
f(1)AR ●
f(0)AR

Creation of An Activation Record


main(): { f(3); }; ●
link
Access ●
AR
none ●
none ●
none ●
none ●
none
}v
Main

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

Add(x, y): ->


Returns {x + y};
Compute(x, y): ->
Result = Add(x, y);
Compute() & Main() are the Caller
Result2 = Add(
In this scenario
Returns {
Return Result2 } Add() is the Callee
Function Main(){
result3 = Compute(10, 20);
Return 0;
}

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.

Subdivisions of Run-Time Storage ●


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. ●

2. Supports nested procedure calls through stack frames. ●


3. Grows and shrinks dynamically during execution.
4.Heap:
1. Used for dynamic memory allocation (e.g., malloc, new). Target
2. Managed via allocation and deallocation routines provided by Code
the run-time environment. 20
Storage
Click Organization(Cont.)
to edit Master title style
High Address


**sp** (Stack Pointer)
Stack

(Expands as needed for dynamic ●


HEAP
allocation)

•Stack Pointer (sp):


•Tracks the top of the stack during execution. ●
•Growth Direction:
•Stack grows downward (towards lower addresses).

Static Data
•Heap grows upward (towards higher addresses). ●
•Optimization:
•Proper subdivision ensures efficient memory
utilization and faster execution. Target Code
Low Address
21
Storage-Allocation
Click Strategies
to edit Master title style
Memory allocation in a run-time environment is essential for efficient management of data objects.
The allocation strategies are categorized based on the timing and manner of allocation.

• 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

- Local data are allocated on the stack.


• Heap allocation:- Memory allocation and deallocation can be done at any time.
- Requires memory reclamation support. Timing Runtime, Life time Dynamic

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

Caption01 appears here Caption02 appears here Caption03 appears here

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

You might also like