5CS4-CD-Unit-4_ppt @zammers
5CS4-CD-Unit-4_ppt @zammers
Unit IV
Storage
organization By
Sourabh Banga
Asst. Prof
ACERC
Contents
• Runtime environment
• Storage organization
• Storage allocation strategies
• Activation records
• Accessing local and non local names
• Parameter passing
• Symbol table organization
• Data structures used in symbol table
Runtime Environment
• The compiler must implement various abstractions in the
source language definition such as
• Names used in a program
• Define the scope of variables
• Data types
• Operators
• Procedures
• Parameters and
• Flow of control constructs.
Definition- The compiler must co-operate with operating system and other
systems software to support the implementation of these abstractions on the
target machine. This can be done by the compiler by creating run-time
environment.
Runtime Environment
• A lot has to happen at run time to get your program
running.
• At run time, we need a system to map NAMES (in the
source program) to STORAGE on the machine.
• Allocation and deallocation of memory is handled by a
RUNTIME SUPPORT SYSTEM typically linked and
loaded along with the compiled target code.
• One of the primary responsibilities of the run-time
system is to manage ACTIVATIONS of procedures.
Storage organization
• Suppose that the compiler obtains memory from
the OS so that it can execute the compiled
program
• Program gets loaded on a newly created process
• This runtime storage must hold
• Generated target code
• Data objects
• A counterpart of the control stack to keep track of
procedure activations
Runtime Memory
code for function 1
PASCAL and C use extensions of the control
code for function 2 stack to manage activations of procedures
...
code for function n
Stack contains information about register
values, value of program counter and data objects
global / static area whose lifetimes are contained in that of an
activation
stack
Heap holds all other information. For example,
activations that cannot be represented as a tree.
free space
By convention, stack grows down and the top of
the stack is drawn towards the bottom of this slide
(value of top is usually kept in a register)
heap
Runtime Memory
code for function 1 code for function 1
... ...
stack stack
Stack grows
heap heap
Activation Record
• Information needed by a single returned value
execution of a procedure is
managed using an activation actual parameters
record or frame
optional control link
– Not all compilers use all of the
fields optional access link
– Pascal and C push activation saved machine status
record on the runtime stack
when procedure is called and local data
pop the activation record off the
stack when control returns to temporaries
the caller
Activation Record
1) Temporary values returned value
e.g. those arising in the evaluation of
expressions actual parameters
2) Local data optional control link
Data that is local to an execution of the
procedure optional access link
3) Saved machine status saved machine status
State of the machine info before
procedure is called. Values of program local data
counter and machine registers that
have to be restored when control temporaries
returns from the procedure
Activation Record
4) Access Link
refer to non-local data held in other activation
returned value
records
main()
{
i (int)
int k; global / static area
f(k);
}
main() stack
Activation
record k (int)
free space
f()
Activation
record k (int)
m (int)
heap
Stack-based Allocation
• In a stack-based allocation, the previous
restrictions are lifted (Pascal, C, etc)
– procedures are allowed to be called recursively
• Need to hold multiple activation records for the same
procedure
• Created as required and placed on the stack
– Each record will maintain a pointer to the record that activated it
– On completion, the current record will be deleted from the stack
and control is passed to the calling record
– Dynamic memory allocation is allowed
– Pointers to data locations are allowed
Stack-based Allocation
PROGRAM sort(input,output);
Position in Activation Records
VAR a : array[0..10] of Integer;
PROCEDURE readarray; Activation Tree On Stack
VAR i : Integer;
BEGIN
for i:= 1 to 9 do read(a[i]);
END;
FUNCTION partition(y,z : Integer): Integer; s
VAR i,j,x,v : Integer; s
BEGIN
…
END; a (array)
PROCEDURE quicksort(m,n : Integer);
VAR i : Integer;
BEGIN
if (n > m) then BEGIN
i := partition(m,n);
quicksort(m, i-1);
quicksort(i+1,n)
END
END;
BEGIN /* of main */
a[0] := -9999; a[10] := 9999;
readarray;
quicksort(1,9)
END.
Stack-based Allocation
PROGRAM sort(input,output); Position in Activation Records
VAR a : array[0..10] of Integer;
PROCEDURE readarray; Activation Tree On Stack
VAR i : Integer;
BEGIN
for i:= 1 to 9 do read(a[i]);
END;
FUNCTION partition(y,z : Integer): Integer; s
VAR i,j,x,v : Integer;
BEGIN
… s
END; a (array)
PROCEDURE quicksort(m,n : Integer);
VAR i : Integer;
BEGIN r q(1,9) q(1,9)
if (n > m) then BEGIN
i := partition(m,n);
quicksort(m, i-1);
quicksort(i+1,n)
p(1,9) q(1,3) i (integer)
END
END; q(1,3)
BEGIN /* of main */
a[0] := -9999; a[10] := 9999;
readarray;
quicksort(1,9) i (integer)
END.
Calling Sequences
• Procedure calls are
implemented by generating
returned value
calling sequences in the target
code actual parameters
local data
using offsets from its own activation
temporaries
record
– No need to know the middle part of
returned value
actual parameters
the callee’s activation record
optional control link
callee
optional access link
local data
temporaries
local data
temporaries
main()
{
int *p;
p = dangle();
}
Dangling References
Local variable i only exists in
dangle()
int *dangle()
{
int i = 23; When procedure completes
return &i; execution and control is
}
transferred to main(), the space for
main()
{
i does not exist anymore (pop
int *p; activation record for dangle off the
p = dangle();
} stack)
29
Call by value
• This is the simplest parameter passing method.
• The caller computes r-values for the actuals.
• The caller places the resulting values on the
stack, in the AR of the callee.
• The callee may change the parameters, but this
has no effect on the caller.
• This is the default protocol in Pascal, and the
ONLY protocol in C.
30
Parameter passing example
1) program reference( input, output );
2) var a, b: integer;
3) procedure swap( var x, y: integer );
4) var temp : integer;
5) begin
6) temp := x;
Specifies call-by-
7) x := y;
reference
8) y := temp;
9) end;
10)begin
11) a := 1; b := 2;
12) swap( a, b );
13) writeln( ‘a = ‘, a ); writeln( ‘b = ‘, b )
14)end.
31
Call by reference
• The caller passes the called procedure a
POINTER to the storage address of the actual
parameter.
• If the actual has an l-value, it is used.
• If the actual is an expression, we place the result
of the expression in a temporary and pass a
pointer to the temporary.
• Pascal uses call by reference if the “var”
keyword is used.
• C++ uses call by reference if the “&” operator is
specified. 32
Copy restore
• This is a hybrid between call-by-value and call-
by reference.
• Before callee is activated, we evaluate the
actuals and put their r-values in the AR for the
callee.
• But we also compute and save the l-values of
the actuals.
• In the return sequence, we copy the updated r-
values from the callee’s AR to the location for
the saved values. FORTRAN used this
approach.
33
Call by name (macro expansion)
• In this method, we just substitute the body
of the procedure for the procedure call.
• In the copied body, the formal parameters
are replaced by the text of the actuals.
• #define macros in C/C++ use this
technique.
34
SYMBOL TABLE
11/28/2020 35
INFORMATION PROVIDED BY
SYMBOL TABLE
11/28/2020 36
SYMBOL TABLE - NAMES
Variable and labels
Parameter
Constant
NAME Record
Record Field
Procedure
Array and files
11/28/2020 37
SYMBOL TABLE-ATTRIBUTES
• Each piece of information associated with a name is
called an attribute.
• Attributes are language dependent.
• Different classes of Symbols have different Attributes
Variable, Procedure or
Array
Constants function
11/28/2020 38
WHO CREATES SYMBOL TABLE??
11/28/2020 40
IMPLEMENTATION OF SYMBOL TABLE
11/28/2020 41
IMPLEMENTATION OF SYMBOL
TABLE
• Each entry in the symbol table can be implemented as a
record consisting of several field.
• These fields are dependent on the information to be
saved about the name
• But since the information about a name depends on the
usage of the name the entries in the symbol table
records will not be uniform.
• Hence to keep the symbol tables records uniform some
information are kept outside the symbol table and a
pointer to this information is stored in the symbol table
record.
11/28/2020 42
int LB1
a
UB1
SYMBOL TABLE
11/28/2020 43
WHERE SHOULD NAMES BE HELD??
11/28/2020 44
LB1
int
UB1
SYMBOL TABLE
A B
STRING TABLE
11/28/2020 45
SYMBOL TABLE AND SCOPE
• Symbol tables typically need to support multiple
declarations of the same identifier within a program.
11/28/2020 46
SYMBOL TABLE
ORGANIZATION
TOP
z Real
Y Real Symbol table for block q
x Real
Procedure P:
q Real
Var x,a :boolean;
a Real Symbol table for p
Procedure q: x Real
Var x,y,z : real;
begin
…… P Proc
end Symbol table for main Y Integer
begin
X Integer
….. 47
End
SYMBOL TABLE DATA STRUCTURES
Issues to consider : Operations required
• Insert
– Add symbol to symbol table
• Look UP
– Find symbol in the symbol table (and get its
attributes)
Insertion is done only once
Look Up is done many times
Need Fast Look Up
The data structure should be designed to allow the
compiler to find the record for each name quickly and to
store or retrieve data from that record quickly.
11/28/2020 48
LINKED LIST
A linear list of records is the easiest way to implement
symbol table.
The new names are added to the symbol table in the
order they arrive.
Whenever a new name is to be added to be added it is
first searched linearly or sequentially to check if or the
name is already present in the table or not and if not , it
is added accordingly.
• Time complexity – O(n)
• Advantage – less space , additions are simple
• Disadvantages - higher access time.
11/28/2020 49
UNSORTED LIST
01 PROGRAM Main
02 GLOBAL a,b
03 PROCEDURE P (PARAMETER x)
04 LOCAL a
On
05 BEGIN {P}
06 …a… Look up Complexity
07 …b…
08 …x…
09 END {P}
10 BEGIN{Main}
11 Call P(a)
12 END {Main}
Olog n
03 PROCEDURE P (PARAMETER x) Look up Complexity
04 LOCAL a 2
05 BEGIN {P}
06 …a… If stored as array (complex insertion)
On
07 …b…
08 …x…
Look up Complexity
09 END {P}
If stored as linked list (easy insertion)
10 BEGIN{Main}
11 Call P(a)
12 END {Main}
11/28/2020 52
BINARY TREE
a Variable
11/28/2020 1 Line4 Line6 53
BINARY TREE
On
Lookup complexity if tree
unbalanced
11/28/2020 54
HASH TABLE
• Table of k pointers numbered from zero to k-1 that points
to the symbol table and a record within the symbol table.
• To enter a name in to the symbol table we found out the
hash value of the name by applying a suitable hash
function.
• The hash function maps the name into an integer
between zero and k-1 and using this value as an index in
the hash table.
11/28/2020 55
M n a b P x
HASH TABLE - EXAMPLE 77 110 97 98 80 120
PROGRAM Main
0 Main Program 0 Line1
GLOBAL a,b
1
PROCEDURE
2 P(PARAMETER x)
3 LOCAL a
4 BEGIN (P)
…a…
5
…b…
6 P Procedure 1 Line 3
…x…
7 a Variable 10 Line4
Line2 a Variable 0 Line2
END (P)
8
BEGIN (Main)
9 bx Parameter
Variable 0 1Line2
Line3 b Variable 0 Line2
Call P(a)
10
End (Main)
H(Id) = (# of first letter + # of last letter) mod 11
56