0% found this document useful (0 votes)
107 views16 pages

Storage Organization

The document discusses storage organization in programming, detailing how logical address spaces are managed by the operating system and subdivided into various components like executable code, static and dynamic data objects, and automatic data objects. It explains activation records used in control stacks to manage procedure activations, and outlines different memory allocation strategies including static, stack, and heap allocation. Additionally, it covers the symbol table's role in compilers for managing variable names and scopes, and the complexities of representing scope information in block-structured languages.

Uploaded by

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

Storage Organization

The document discusses storage organization in programming, detailing how logical address spaces are managed by the operating system and subdivided into various components like executable code, static and dynamic data objects, and automatic data objects. It explains activation records used in control stacks to manage procedure activations, and outlines different memory allocation strategies including static, stack, and heap allocation. Additionally, it covers the symbol table's role in compilers for managing variable names and scopes, and the complexities of representing scope information in block-structured languages.

Uploaded by

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

Storage Organization

o When the target program executes then it runs in its own


logical address space in which the value of each program
has a location.
o The logical address space is shared among the compiler,
operating system and target machine for management and
organization. The operating system is used to map the
logical address into physical address which is usually spread
throughout the memory.

Subdivision of Run-time Memory:

o Runtime storage comes into blocks, where a byte is used to


show the smallest unit of addressable memory. Using the
four bytes a machine word can form. Object of multibyte is
stored in consecutive bytes and gives the first byte address.
o Run-time storage can be subdivide to hold the different
components of an executing program:

1. Generated executable code


2. Static data objects
3. Dynamic data-object- heap
4. Automatic data objects- stack

Activation Record
o Control stack is a run time stack which is used to keep track
of the live procedure activations i.e. it is used to find out the
procedures whose execution have not been completed.
o When it is called (activation begins) then the procedure
name will push on to the stack and when it returns
(activation ends) then it will popped.
o Activation record is used to manage the information needed
by a single execution of a procedure.
o An activation record is pushed into the stack when a
procedure is called and it is popped when the control returns
to the caller function.

The diagram below shows the contents of activation records:


Return Value: It is used by calling procedure to return a value to
calling procedure.

Actual Parameter: It is used by calling procedures to supply


parameters to the called procedures.

Control Link: It points to activation record of the caller.

Prime Ministers of India | List of Prime Minister of India (1947-2020)

Access Link: It is used to refer to non-local data held in other


activation records.

Saved Machine Status: It holds the information about status of


machine before the procedure is called.

Local Data: It holds the data that is local to the execution of the
procedure.

Temporaries: It stores the value that arises in the evaluation of


an expression.

Storage Allocation
The different ways to allocate memory are:

1. Static storage allocation


2. Stack storage allocation
3. Heap storage allocation

Static storage allocation


o In static allocation, names are bound to storage locations.
o If memory is created at compile time then the memory will
be created in static area and only once.
o Static allocation supports the dynamic data structure that
means memory is created only at compile time and
deallocated after program completion.
o The drawback with static storage allocation is that the size
and position of data objects should be known at compile
time.
o Another drawback is restriction of the recursion procedure.

Stack Storage Allocation


o In static storage allocation, storage is organized as a stack.
o An activation record is pushed into the stack when activation
begins and it is popped when the activation end.
o Activation record contains the locals so that they are bound
to fresh storage in each activation record. The value of locals
is deleted when the activation ends.
o It works on the basis of last-in-first-out (LIFO) and this
allocation supports the recursion process.

Heap Storage Allocation


o Heap allocation is the most flexible allocation scheme.
o Allocation and deallocation of memory can be done at any
time and at any place depending upon the user's
requirement.
o Heap allocation is used to allocate memory to the variables
dynamically and when the variables are no more used then
claim it back.
o Heap storage allocation supports the recursion process.

Example:
1. fact (int n)
2. {
3. if (n<=1)
4. return 1;
5. else
6. return (n * fact(n-1));
7. }
8. fact (6)

The dynamic allocation is as follows:


Symbol Table
Symbol table is an important data structure used in a compiler.

Symbol table is used to store the information about the


occurrence of various entities such as objects, classes, variable
name, interface, function name etc. it is used by both the analysis
and synthesis phases.

The symbol table used for following purposes:

o It is used to store the name of all entities in a structured


form at one place.
o It is used to verify if a variable has been declared.
o It is used to determine the scope of a name.
o It is used to implement type checking by verifying
assignments and expressions in the source code are
semantically correct.
A symbol table can either be linear or a hash table. Using the
following format, it maintains the entry for each name.

Keep Watching

Competitive questions on Structures in Hindi


00:00/03:34

1. <symbol name, type, attribute>

For example, suppose a variable store the information about the


following variable declaration:

1. static int salary

then, it stores an entry in the following format:

1. <salary, int, static>

The clause attribute contains the entries related to the name.

Implementation
The symbol table can be implemented in the unordered list if the
compiler is used to handle the small amount of data.

A symbol table can be implemented in one of the following


techniques:

o Linear (sorted or unsorted) list


o Hash table
o Binary search tree

Symbol table are mostly implemented as hash table.

Operations
The symbol table provides the following operations:
Insert ()
o Insert () operation is more frequently used in the analysis
phase when the tokens are identified and names are stored
in the table.
o The insert() operation is used to insert the information in the
symbol table like the unique name occurring in the source
code.
o In the source code, the attribute for a symbol is the
information associated with that symbol. The information
contains the state, value, type and scope about the symbol.
o The insert () function takes the symbol and its value in the
form of argument.

For example:
1. int x;

Should be processed by the compiler as:

1. insert (x, int)

lookup()
In the symbol table, lookup() operation is used to search a name.
It is used to determine:

o The existence of symbol in the table.


o The declaration of the symbol before it is used.
o Check whether the name is used in the scope.
o Initialization of the symbol.
o Checking whether the name is declared multiple times.

The basic format of lookup() function is as follows:

1. lookup (symbol)
This format is varies according to the programming language.

Data structure for symbol table


o A compiler contains two type of symbol table: global symbol
table and scope symbol table.
o Global symbol table can be accessed by all the procedures
and scope symbol table.

The scope of a name and symbol table is arranged in the


hierarchy structure as shown below:

1. int value=10;
2.
3. void sum_num()
4. {
5. int num_1;
6. int num_2;
7.
8. {
9. int num_3;
10. int num_4;
11. }
12.
13. int num_5;
14.
15. {
16. int_num 6;
17. int_num 7;
18. }
19. }
20.
21. Void sum_id
22. {
23. int id_1;
24. int id_2;
25.
26. {
27. int id_3;
28. int id_4;
29. }
30.
31. int num_5;
32. }

The above grammar can be represented in a hierarchical data


structure of symbol tables:
The global symbol table contains one global variable and two
procedure names. The name mentioned in the sum_num table is
not available for sum_id and its child tables.

Features of Java - Javatpoint


Data structure hierarchy of symbol table is stored in the semantic
analyzer. If you want to search the name in the symbol table then
you can search it using the following algorithm:
o First a symbol is searched in the current symbol table.
o If the name is found then search is completed else the name
will be searched in the symbol table of parent until,
o The name is found or global symbol is searched.

Representing Scope Information


In the source program, every name possesses a region of validity,
called the scope of that name.

The rules in a block-structured language are as follows:

1. If a name declared within block B then it will be valid only


within B.
2. If B1 block is nested within B2 then the name that is valid for
block B2 is also valid for B1 unless the name's identifier is
re-declared in B1.

o These scope rules need a more complicated organization of


symbol table than a list of associations between names and
attributes.
o Tables are organized into stack and each table contains the
list of names and their associated attributes.
o Whenever a new block is entered then a new table is
entered onto the stack. The new table holds the name that is
declared as local to this block.
o When the declaration is compiled then the table is searched
for a name.
o If the name is not found in the table then the new name is
inserted.
o When the name's reference is translated then each table is
searched, starting from the each table on the stack.

For example:

1. int x;
2. void f(int m) {
3. float x, y;
4. {
5. int i, j;
6. int u, v;
7. }
8. }
9. int g (int n)
10. {
11. bool t;
12. }
Fig: Symbol table organization that complies with static scope
information rules

STORAGE ALLOCATION STRATEGIES


The different storage allocation strategies are :
1. Static allocation - lays out storage for all data objects at compile time
2. Stack allocation - manages the run-time storage as a stack.
3. Heap allocation - allocates and deallocates 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, everytime a procedure is activated, its names are
bound to the same storage locations. Therefore values of local names are
retained across activations of a procedure.

That is, when control returns to a procedure the values of the locals are
the same as they were when control left the last time. From the type of a name,
the compiler decides the amount of storage for the name and decides where the
activation records go. At compile time, we can fill in the addresses at which the
target code can find the data it operates on.

STACK ALLOCATION OF SPACE

All compilers for languages that use procedures, functions or methods as


units of user-defined 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, that space is popped off the
stack.

Calling sequences:

Procedures called are implemented in what is called as calling sequence,


which consists of code that allocates an activation record on the stack and enters
information into its fields. A return sequence is similar to code to restore the
state of machine so the calling procedure can continue its execution after the
call. The code in calling sequence is often divided between the calling
procedure (caller) and the procedure it calls (callee).

When designing calling sequences and the layout of activation records,


the following principles are helpful:

Ø Values communicated between caller and callee are generally placed at


the beginning of the callee’s activation record, so they are as close as
possible to the caller’s activation record.

link, and the machine status fieldsFixed length items are generally placed in
the middle. Such i the control link, the access
Items whose size may not be known early enough are placed at the end of
the activation
record. The most common example is dynamically sized array, where the
value of one of
the callee’s parameters determines the length of the array.

to the end of fixed-length fields in the activation record. Fixed-length data


can then be accessed by fixed offsets, known to the intermediate-code
generator, relative to the top-of-stack pointer.We must locate the top-of-
stack pointer judiciously. A common approach is to have it point
The calling sequence and its division between caller and callee are as follows:
Ø The caller evaluates the actual parameters.
Ø The caller stores a return address and the old value of top_sp into the
callee’s activation record. The caller then increments the top_sp to the
respective positions.
Ø The callee saves the register values and other status information.
Ø The callee initializes its local data and begins execution.

A suitable, corresponding return sequence is:


Ø The callee places the return value next to the parameters.

Ø Using the information in the machine-status field, the callee restores


top_sp and other registers, and then branches to the return address that the
caller placed in the status field.

Ø Although top_sp has been decremented, the caller knows where the
return value is, relative to the current value of top_sp; the caller therefore
may use that value.

Variable length data on stack:

The run-time memory management system must deal frequently with the
allocation of space for objects, the sizes of which are not known at the compile
time, but which are local to a procedure and thus may be allocated on the stack.
The reason to prefer placing objects on the stack is that we avoid the expense of
garbage collecting their space. The same scheme works for objects of any type
if they are local to the procedure called and have a size that depends on the
parameters of the call.

HEAP ALLOCATION
Stack allocation strategy cannot be used if either of the following is possible :
1. The values of local names must be retained when an activation ends.
2. A called activation outlives the caller.

Heap allocation parcels out pieces of contiguous storage, as needed for


activation records or other objects. Pieces may be deallocated in any order, so
over the time the heap will consist of alternate areas that are free and in use.

Fig. 2.10 Records for live activations need not be adjacent in


heap

• The record for an activation of procedure r is retained when the activation


ends.
• Therefore, the record for the new activation q(1 , 9) cannot follow that for s
physically.
• If the retained activation record for r is deallocated, there will be free space in
the heap between the activation records for s and q.

You might also like