Implementation of Simple Stack Allocation Scheme
Implementation of Simple Stack Allocation Scheme
Implementation of Simple Stack Allocation Scheme
A program consists of data and procedures. On execution of each procedure, some amount of memory is occupied, which
has information regarding the procedure, i.e., its actual parameters, number of arguments, return address, return values &
local data, etc. That part of memory is the Activation Record of that procedure.
Activation Record
An Activation Record is a data structure that is activated/ created when a procedure/function is invoked, and it contains the
following information about the function.
Actual Parameters
Number of Arguments
Return Address
Return Value
Old Stack Pointer (SP)
Local Data in a function or procedure
Here, Old SP stores the value of stack pointer of Activation Record of procedure which has called this procedure which
leads to the generation of this Activation Record, i.e., It is a pointer to the activation record of the caller.
top− It points to the top of the stack. top points to the top of the top activation record. In the figure, the top pointer
will point to the top of the C Activation Record.
Stack Pointer (SP)− It points to the activation record of the currently active procedure.
Actual Parameter− It is used by the calling procedure to supply parameters to the called procedure.
Return value− This field is used by the called procedure to return a value to the calling procedure. The size of each of the
above fields is determined at the time when the procedure is called. The size of almost all the fields can be determined at
compilation time.
It can analysis on how the memory is allocated at runtime when a procedure is called & when the value from the procedure
is returned.
Consider a procedure P(x1, x2, x3 … … xn).Three address code statement for the call of this procedure will be
param x1
param x2
…………….
param xn
call P, n
Execution of all the following statements related to the procedure will perform stack allocation at runtime.
𝐏𝐚𝐫𝐚𝐦 𝐱 ∶ Passing actual parameter x.
𝐜𝐚𝐥𝐥 𝐏, 𝐧− calling procedure P with n arguments.
𝐩𝐫𝐨𝐜𝐛𝐞𝐠𝐢𝐧− The first statement of procedure
𝐫𝐞𝐭𝐮𝐫(𝐯𝐚𝐥𝐮𝐞)− When the value is returned.
𝐞𝐧𝐝− Last statement of procedure.
Ans 2 Explain Storage Allocation in Block Structured Language.
Static storage allocation Static allocation supports the dynamic data structure that means memory is created only at
compile time and deallocated after program completion. The drawback with static storage allocation is that the size
and position of data objects should be known at compile time.
Blocks consist of one or more declarations and statements. A programming language that permits the creation of
blocks, including blocks nested within other blocks, is called a block-structured programming language. Blocks are
fundamental to structured programming, where control structures are formed from blocks.
Compiler must carry out the storage allocation and provide access to variables and data
Allocation can be done in two ways:
Compiler Time Allocation or Static Storage Allocation
A static storage allocation decision is taken by the compiler by looking only at the text of the program, but not by looking at
what the program does when it executes
Allocation is done at compile time
Runtime Allocation or Dynamic Storage Allocation
A storage allocation decision is dynamic if it can be decided only while program is running
Allocation is done at runtime
Storage allocation can be done for two types of data variable:
Local data
Non local data
The local data can be handled using activation record where as non-local data can be handled using scope information
The block structured storage allocation can be done using static scope
Definition of procedure
Declaration of a name or variable
Scope of declaration
Dynamic storage allocation includes,
Activation of procedure
Binding of a name
Lifetime of a binding
Activation Record
Information needed for each execution of a procedure is stored in a record called activation record
Procedure calls and returns are usually managed by a runtime stack called the control stack
Each live activation has an activation record or a frame
The root of activation tree is a the bottom of the stack
The current execution path specifies the content of the stack with the last activation as record in the top of the stack
Activation record contains 7 fields which are:
Returned
Actual parameters
Control link
Access link
Machine status
Local data
Temporary data
Size of each field of activation record can be estimated at compile time.