Csf401 Unit 04
Csf401 Unit 04
Unit-4:
Storage Organization
Subdivision of Runtime Memory
• The compiler demands for a block of memory to operating
system.
• The compiler utilizes this block of memory executing the
compiled program. This block of memory is called run time
storage.
• The run time storage is subdivided to hold code and data such
as, the generated target code and data objects.
• The size of generated code isCode area
fixed. Hence the target code
Static data
occupies the determined area of
area
the memory.
Stack
Heap
Subdivision of Runtime Memory
• The amount of memory required by the data objects is known at
the compiled time and hence data objects also can be placed at
the statically determined area of the memory.
• Stack is used to manage the active procedure.
• Managing of active procedures means when a call occurs then
execution of activation is interrupted and information about
status of the stack is saved on the stack.
• Heap area is the area of run time storage in which the other
information is stored. Code area
Static data
area
Stack
Heap
Activation Record
• The execution of a procedure is called its activation.
• An activation record contains all the necessary information required to call a
procedure.
Temporary values: stores the values that arise in the
evaluation of an expression. Temporary
Local variables: hold the data that is local to the value
execution of the procedure. Local
Machine status: holds the information about status of variables
machine just before the function call. Machine
status
Access link : refers to non-local data held in other
activation records. Access link
Control link: points to activation record of caller. Control link
Actual
Actual parameters: This field holds the information
parameters
about the actual parameters.
Return value
Return value: used by the called procedure to return a
value to calling procedure.
Compile-Time Layout of Local Data
• The amount of storage needed for a name is determined from
its type. (e.g.: int, char, float…)
• Storage for an aggregate, such as an array or record, must be
large enough to hold all it’s components.
• The field of local data is laid out as the declarations in a
procedure are examined at compile time.
• We keep a count of the memory locations that have been
allocated for previous declarations.
• From the count we determine a relative address of the storage
for a local with respect to some position such as the beginning
of the activation record.
Storage Allocation Strategies
Storage allocation strategies
The different storage allocation strategies are;
• Static allocation: lays out storage for all data objects at compile
time.
• Stack allocation: manages the run-time storage as a stack.
• Heap allocation: allocates and de-allocates storage as needed
at run time from a data area known as heap.
Static allocation
• In static allocation, names are bound to storage as the program
is compiled, so there is no need for a run-time support package.
• Since the bindings do not change at run-time, every time a
procedure is activated, its names are bounded to the same
storage location.
Stack allocation
• All compilers for languages that use procedures, functions or
methods as units of user define actions manage at least part of
their run-time memory as a stack.
• Each time a procedure is called, space for its local variables is
pushed onto a stack, and when the procedure terminates, the
space is popped off the stack.
Stack allocation: Calling Sequences
• Procedures calls are implemented by generating what are known as calling
sequences in the target code.
• A call sequence allocates an activation record and enters the information into its
fields.
• A Return sequence restore the state of machine so the calling procedure can
continue its execution.
• The code is calling sequence of often divided between the calling procedure (caller)
and procedure is calls (callee).
Parameter and return value Caller’s
Control link activation
Links and saved status record
Temporaries and local data Caller’s
Parameter and returned responsibili
value ty
Control link Callee’s
Links and saved status Callee’s activation
Temporaries and local data responsibili record
ty
Symbol Table
Symbol Table
• Symbol table is a data structure used by compiler to keep track
of semantics of a variable.
• Symbol table is built in lexical and syntax analysis phases.
• The items to be stored into symbol table are:
1. Variable names
2. Constants
3. Procedure names
4. Function names
5. Literal constants and strings
6. Compiler generated temporaries
7. Labels in source language
Symbol Table Entries
Data structures for a symbol table
List Data structure
• The name can be stored with the help of starting index and length of each
name.
• Linear list is a simplest kind of mechanism to implement the symbol table.
• In this method an array is used to store names and associated information.
• New names can be added in the order as they arrive.
• The list data structure using array is given below:
Name 1 Info 1
Name 2 Info 2
Name 3 Info 3
Name n Info n
Self organizing list
• This symbol table implementation is using linked list. A link field
is added to each record.
• We search the records in the order pointed by the link of link
field.
• The pointer “First” is maintained to point to first record of the
symbol table.
Name Info
1 1
Name Info
2 2
Name Info
3 3
Name Info
n n
Binary tree
• When the organization symbol table is by means of binary tree,
the node structure will as follows:
• The left child field stores the address of previous symbol.
• Right child field stores the address of next symbol.
• The symbol field is used to store the name of the symbols.
• Information field is used to give information about the symbol.