ACD Unit-2 Part-3
ACD Unit-2 Part-3
UNIT – IV:
Symbol Tables: Symbol table format, organization for block structures languages, hashing, tree
structures representation of scope information. Block structures and non block structure storage
allocation: static, Runtime stack and heap storage allocation, storage allocation for arrays, strings
and records.
Code optimization: Consideration for Optimization, Scope of Optimization, local optimization,
loop optimization, frequency reduction, folding, DAG representation.
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.
Symbol table is used by various phases of compiler as follows :-
Lexical Analysis: Creates new table entries in the table, example like entries about
token.
Syntax Analysis: Adds information regarding attribute type, scope, dimension, line
of reference, use,etc in the table.
1
Symbol Table
Semantic Analysis: Uses available information in the table to check for semantics
i.e. to verify that expressions and assignments are semantically correct(type checking) and
update it accordingly.
Intermediate Code generation: Refers symbol table for knowing how much and what
type of run- time is allocated and table helps in adding temporary variable information.
Code Optimization: Uses information present in symbol table for machine dependent
optimization.
Code generation: Generates code by using address information of identifier present in the
table.
Symbol Table Operations:
2
Symbol Table...
Symbol Table Format:
Symbol table consists of names and its properties like type , values, size ,scope
etc..
Name Properties/Attributes
There are two types of name representations:
1. Fixed Length Name
2. Variable Length Name
4
Symbol Table...
Organization for Block Structures Languages:
The block structured language is a kind of language in which sections of source
code is within some matching pair of delimiters such as “{“ and “}” or begin
and end.
Such a section gets executed as one unit or one procedure or a function or it may be
controlled by some conditional statements (if, while, do-while).
Normally, block structured languages support structured programming
approach Example: C, C++, JAVA, ALGOL,PASCAL etc.
Non-block structured languages does not contain any blocks ,Examples are
LISP, FORTRAN and SNOBOL.
Implementation of Symbol Table:
The following data structures are used for organization of block structured
languages:
1. Linear List
2. Self-Organizing List
3. Hashing
4. Tree Structure
5
Symbol Table...
1. Linear List:
Linear list of records is the easiest way to implement the symbol table.
In this method, an array is used to store names and associated information.
The new names are added to the symbol table in the order they arrive.
The pointer “available” is maintained at the end of all stored records.
To retrieve the information about some name we start from beginning of array and go on
searching up to available pointer. If we reach at pointer available without finding a name we
get an error “use of undeclared name”.
While inserting a new name we should ensure that it is not already present. If it is
already present then another error occurs, i.e., “Multiple Defined Name”.
6
Symbol Table...
2. Self Organizing List:
In this method, symbol table is implemented 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.
A pointer “First” is maintained to point to first record of the symbol table
When the name is reference or created, it is moved to the front of the list.
The most frequently referred names will tend to be at the front of the list.
Hence, access time to most frequently referred names will be the least.
8
Symbol Table...
4. Tree Structure:
When the scope information is presented in hierarchical manner then it forms a tree
structure representation which is an efficient approach for symbol table organization.
This organization uses binary search tree for storing the names in symbol table.
We add two links left and right in each record in the search trees.
Whenever a name is to be added first, the name is searched in the tree.
If it does not exist then a record for new name is created and added at the proper position.
Each node of tree has following format:
a total
v
c 9
Block structures and Non Block structure storage allocation
Storage allocation refers to process of mapping the data code into appropriate location in the
main memory.
Compiler must carry out the storage allocation and provide access to variables and data.
Storage allocation strategies are:
1. Static Storage Allocation
For any program if we create memory at compile time, memory will be created in
the static area.
For any program if we create memory at compile time only, memory is created only
once.
It don’t support dynamic data structure i.e memory is created at compile time and
deallocated after program completion.
The drawback with static storage allocation is recursion is not supported.
Another drawback is size of data should be known at compile time
Eg- FORTRAN was designed to permit static storage allocation.
II. Stack Storage Allocation
Stack allocation is a procedure in which stack is used to organize the storage.
The stack used in stack allocation is known as control stack.
In this type of allocation, creation of data objects is performed dynamically.
In this activation records are created for the allocation of memory. 10
Block structures and Non Block structure storage allocation...
These activation records are pushed onto the stack using Last In First Out (LIFO) method.
Locals are stored in the activation records at run time and memory addressing is done by
using pointers and registers .
Recursion is supported in stack allocation.
Activation record contains 7 fields :
1. Return Value: It is used by calling procedure to return
a value to calling procedure.
2. Actual Parameter: It is used by calling procedures to
supply parameters to the called procedures.
3. Control Link: It is an optional field .It points to activation
record of the caller. It also known as dynamic link field.
4. Access Link: It is an optional field . It is used to
refer to non-local data held in other activation records.
It also known as static link field.
5. Saved Machine Status: It holds the information about
status of machine before the procedure is called.
6. Local Data: It holds the data that is local to the
7. execution of the
Temporaries: It procedure.
stores the value that arises in the evaluation of an expression.
11
Block structures and Non Block structure storage allocation...
Copy Propagation
Constant Folding
• Reduction in Strength
Common Sub Expression Elimination:
It is a compiler optimization technique of finding redundant expression evaluations, and
replacing them with a single computation . This saves the time overhead resulted by
evaluating the expression for more than once .
Before After
Copy Propagation
Example:
Before After
x=y
z=3+x
z=3+y
Dead Code Elimination
Code that is unreachable or that does not affect the program can be
eliminated.
Example :
Function1()
{
int a=10,b=20,c,d;
c=a+b;
d=b/a’
print(c);
return;
print(d)
; //
Dead
Code
}
Here,
the
value of
d will
not
print
and
functio
n will
return
Constant Folding
Example:
Before:
X=10+20*3/2;
After
X=40;
Moving the code outside the loop, whose value does not change for all
the
iterations .
Example:
Induction Variable Elimination:
Example:
Reduction in Strength :
It is an loop optimization technique in which expensive operations are replaced
with equivalent and less expensive operations.
Example:
Before After
C=8; C=8;
for(i=0;i<=10;i++) K=0;
{ for(i
A[i]=c*i; =0;i
} <=1
0;i+
+)
{
A[i]
=k;
K=k
+c;
}
Directed Acyclic Graph (DAG):
Directed Acyclic Graph (DAG) is a tool that depicts the structure of basic
blocks, helps to see the flow of values flowing among the basic blocks, and offers
optimization too. DAG is used to represent the flow graph.
DAG consists of :
Leaf nodes represent identifiers, names or constants.
Interior nodes represent operators.
Interior nodes also represent the results of expressions or the identifiers/name where
the values are to be stored or assigned.
Example:
t0 = a + b
t1 = t0 + c
d = t0 + t1
AST and DAG: