0% found this document useful (0 votes)
10 views22 pages

ACD Unit-2 Part-3

The document discusses compiler design focusing on symbol tables, storage allocation, and code optimization techniques. It explains the structure and operations of symbol tables, including their formats and implementations for block-structured languages, as well as various storage allocation strategies like static, stack, and heap allocation. Additionally, it covers local optimization techniques such as common sub-expression elimination, copy propagation, and loop optimization methods, along with the use of Directed Acyclic Graphs (DAG) for representing flow in basic blocks.

Uploaded by

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

ACD Unit-2 Part-3

The document discusses compiler design focusing on symbol tables, storage allocation, and code optimization techniques. It explains the structure and operations of symbol tables, including their formats and implementations for block-structured languages, as well as various storage allocation strategies like static, stack, and heap allocation. Additionally, it covers local optimization techniques such as common sub-expression elimination, copy propagation, and loop optimization methods, along with the use of Directed Acyclic Graphs (DAG) for representing flow in basic blocks.

Uploaded by

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

Compiler Design

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

1. Fixed Length Name Representation:


 A fixed space for each name is allocated
in symbol table.
 In this type of storage, if name is too small
then there is wastage of space.
 The name can be referred by pointer to Name Properties/Attributes
symbol table entry. S U M
A
Example: P I
the
SUM, A, PI, MAX are the variables that are stored
M A X
in the symbol table . Memory space is wasted in
case of variables A and PI as their length is less 3
than name field in the symbol table.
Symbol Table...
2. Variable Length Name Representation:
 A fixed space is not allocated for name in the symbol table.
 The name is stored with the help of starting index and length of each
name.
Example:
Instead of storing the names SUM, A, B and Name
Properties/Attributes
MAX in the symbol table directly , these Starting Length
name are stored in an array and they are Index
separated with delimiter. 0 4
The staring index of each name in the
5 2
array and its length including delimiter
6 2
is stored in the name field of symbol
table. 8 4

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.

 The names are referenced in the order as Name3,Name1,Name4 and


Name2. 7
Symbol Table...
3. Hashing:
 Hashing is an important technique used to search the records of symbol table.
 In hashing scheme, two tables are maintained – hash table and symbol table
 The hash table consists of K entries from 0, 1, 2, … to K-1. These entries are
basically pointers to symbol table pointing to the names of symbol table.
 To determine whether the ‘Name’ is in symbol table, we use a hash function ‘h’
such that h (name) will result any integer between 0 to K-1. We can search any
name by Position = h (name).
 Using the position we can obtain the exact locations of name in symbol table.
 The hash table and symbol table are shown below:

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:

 Example: variables such as Index , a, total ,c , v are organized as


follow:
Index

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...

III. Heap Allocation:


 Heap is a contiguous memory , Heap allocation is an allocation procedure in which heap
is used to manage the allocation of memory.
 Heap allocation is used to dynamically allocate memory to the variables and claim it
back when the variables are no more required.
 Size of Heap-memory is quite larger as compared to the Stack-memory.
 Heap-memory is accessible or exists as long as the whole application runs.
 It maintains a linked list for the free blocks and reuse the deallocated space using best
f i 1t 2.
Local Optimization Techniques
If the scope of the optimization is limited to certain specific block of
statements then it is called as local optimization.

 Common Sub Expression Elimination

 Copy Propagation

 Dead Code Elimination

 Constant Folding

 Loop optimization techniques

• Code Motion / Frequency Reduction

• Induction variable Elimination

• 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

It is the process of replacing the occurrences of targets of direct assignments


with their values. A direct assignment is an instruction of the form x = y , which
simply
assigns the value of y to x .

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

Constant folding is the process of recognizing and evaluating constant


expressions
at compile time rather than computing them at runtime.

Example:

Before:

X=10+20*3/2;

After

X=40;

If an Expression contains all the literals ,they must be folded to a single


value.
Loop Optimization Techniques:

 Code Motion/ Frequency Reduction:

Moving the code outside the loop, whose value does not change for all
the
iterations .

Example:
 Induction Variable Elimination:

A variable is said to be Induction variable, if the value of a variable changes for


every iteration in side the loop i.e. increase or decrease with fixed value . if the loop
contains such variables then we have to eliminate or minimize such variables inside
the loop.

Example:
 Reduction in Strength :
It is an loop optimization technique in which expensive operations are replaced
with equivalent and less expensive operations.

Exponent is replaced with multiplication ,multiplication is replaced with


addition in order reduce the strength of an expression.

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:

You might also like