Unit - 4 Pushdown Automata
Unit - 4 Pushdown Automata
PushdownCompiler
Automata
Prepared By:
Prof. Shital Sharma
Unit – 7Programming
System : Compiler (2150708) 1
Causes of Large Semantic Gap
Two aspects of compilation are:
Generate code to implement meaning of a source program in the
execution domain (target code generation)
Provide diagnostics for violations of PL semantics in a program
(Error reporting)
Unit – 7 : Compiler 2
Binding & Binding Time
Unit – 7 : Compiler 3
Binding & Binding Time
A Binding is the association of an attribute of a program entity
with a value.
Binding time is the time at which a binding is actually performed.
The following binding times arise in compilers:
1. Language definition time of a programming language L, which is
the time at which features of the language are specified.
2. Language implementation time of a programming language L,
which is the time at which the design of a language translator for
L is finalized.
3. Compilation time of a program P.
4. Execution init time of a procedure proc
5. Execution time of a procedure proc
Unit – 7 : Compiler 4
Memory Allocation
Unit – 7 : Compiler 5
Memory Allocation
Memory allocation involves three important tasks:
1. Determine the amount of memory required to represent the
value of data item.
2. Use of appropriate memory allocation model.
3. Determine appropriate memory mappings to access the value.
Memory allocation are mainly divides into two types:
1.Static binding
2.Dynamic binding
Unit – 7 : Compiler 6
Static memory allocation
In static memory allocation, memory is allocated to a variable
before the execution of a program begins.
Static memory allocation is typically performed during
compilation.
No memory allocation or deallocation actions are performed
during the execution of a program. Thus, variables remain
permanently allocated
Unit – 7 : Compiler 7
Dynamic memory allocation
In dynamic memory allocation, memory bindings are established
and destroyed during the execution of a program.
Dynamic memory allocation has two flavors:
1. Automatic allocation
2. Program controlled allocation
In automatic dynamic allocation, memory is allocated to the
variables declared in a program unit when the program unit is
entered during execution and is deallocated when the program
unit is exit.
In program controlled dynamic allocation, a program can allocate
or deallocate memory at arbitrary points during its execution.
Unit – 7 : Compiler 8
Memory Allocation in block structured language
The block is a sequence of statements containing the local data and declarations
which are enclosed within the delimiters.
A
{
Statements
…..
}
A block structured language uses dynamic memory allocation.
Finding the scope of the variable means checking the visibility within the block .
Following are the rules used to determine the scope of the variable:
1. Variable X is accessed within the block B1 if it can be accessed by any statement
situated in block B1.
2. Variable X is accessed by any statement in block B2 if block B2 is situated in block
B1.
Unit – 7 : Compiler 9
Memory Allocation in block structured language
There are two types of variable situated in the block structured language
1. Local variable
2. Non local variable
To understand local and non-local variable consider the following example:
Procedure A
{
int x,y,z
Procedure Local variables Nonlocal variables
Procedure B
{ A x,y,z
Int a,b B a,b x,y,z
}
C m,n x,y,z
Procedure C
{
Int m,n
}
}
Unit – 7 : Compiler 10
Memory Allocation in block structured language
Variables x, y and z are local variables to procedure A but those
are non-local to block B and C because these variable are not
defined locally within the block B and C but are accessible within
these blocks.
Automatic dynamic allocation is implemented using the extended
stack model.
Unit – 7 : Compiler 11
Compilation of Expression
Unit – 7 : Compiler 12
A toy code generator for expressions
The major issues in code generation for expressions are as follows:
1. Determination of an evaluation order for the operators in an
expression.
2. Selection of instruction to be used in target code.
3. Use of registers.
Unit – 7 : Compiler 13
Operand Descriptor
An operand descriptor has the following fields:
1. Attributes: Contains the subfields type, length and miscellaneous
information
2. Addressability: Specifies where the operand is located, and how it
can be accessed. It has two subfields
I. Addressability code: Takes the values 'M' (operand is in
memory), and 'R' (operand is in register).
II. Address: Address of a CPU register or memory word.
Ex: a*b Attribute Addressability
MOVER AREG, A (int, 1) M, Address(a)
(int, 1) M, Address(b)
MULT AREG, B (int, 1) R, Address(AREG)
Unit – 7 : Compiler 14
Register Descriptors
A register descriptor has two fields
1. Status: Contains the code free or occupied to indicate register
status.
2. Operand descriptor #: If status = occupied, this field contains the
descriptor for the operand contained in the register.
In above Example the register descriptor for AREG after generating
code for a*b would be
Occupied #3
Unit – 7 : Compiler 15
Optimization Techniques
Unit – 7 : Compiler 28
Optimization & Types of optimization
Code optimization aims at improving the execution efficiency of a
program.
This achieved in two ways:
1. Redundancies in a program are eliminated.
2. Computations in a program are rearranged or rewritten to make
it execute efficiently.
There are two types of optimization:
Local optimization: the optimizing transformation are applied over
small segments of a program consisting of a few statements.
Global optimization: the optimizing transformation are applied
over program unit, i.e. over a function or a procedure.
Unit – 7 : Compiler 29
Compile time evaluation
Compile time evaluation means shifting of computations from run
time to compile time.
Constant folding
In this technique the value of variable is replaced and
computation of an expression is done at compilation time.
Example:
pi = 3.14; r = 5;
Area = pi * r * r;
Here at the compilation time the value of pi is replaced by 3.14
and r by 5 then computation of 3.14 * 5 * 5 is done during
compilation.
Unit – 7 : Compiler 30
Common sub expressions elimination
The common sub expression is an expression appearing
repeatedly in the program which is computed previously.
If the operands of this sub expression do not get changed at all
then result of such sub expression is used instead of re-computing
it each time.
Example:
t1 := 4 * i t1 := 4 * i
t2 := a[t1] t2 := a[t1]
t3 := 4 * j t3 := 4 * j
t4 : = 4 * i t4 : = 4 * i
t5:= n t5:= n
t6 := b[t4]+t5 t6 := b[t1]+t5
Unit – 7 : Compiler 31
Frequency reduction
Optimization can be obtained by moving some amount of code
outside the loop and placing it just before entering in the loop.
This method is also called loop invariant computation.
Example:
While(i<=max-1) N=max-1;
{ While(i<=N)
sum=sum+a[i]; {
} sum=sum+a[i];
}
Unit – 7 : Compiler 32
Strength Reduction
Strength of certain operators is higher than others.
For instance strength of * is higher than +.
In this technique the higher strength operators can be replaced by
lower strength operators.
Example:
for(i=1;i<=50;i++) temp=7;
{ for(i=1;i<=50;i++)
count = i*7; {
} count = temp;
temp = temp+7;
}
Unit – 7 : Compiler 34
Control & Dataflow Analysis
Unit – 7 : Compiler 35
Control flow analysis
Control flow analysis analyses a program to collect information
concerning its structure.
The control flow concept of interest are:
1. Predecessors & successors
2. Paths
3. Ancestors & descendants
4. Dominators
Unit – 7 : Compiler 36
Predecessors & successors
Predecessor of B2 y=2 B1
successor of B1 y=y+2 B2
Unit – 7 : Compiler 37
Path & Ancestors, Descendants
y=2 B1 Ancestors of B2
Path
y=y+2 B2 Descendants of B1
Unit – 7 : Compiler 38
Dominators
In a flow graph, a node d dominates n if every path to node n from initial node
goes through d only. This can be denoted as ’d dom n'.
Every initial node dominates all the remaining nodes in the flow graph.
Every node dominates itself.
1
3 4
5
Node 1 is initial node and it dominates every node as it is initial node.
Node 2 dominates 3, 4 and 5.
Node 3 dominates itself similarly node 4 dominates itself.
Unit – 7 : Compiler 39
Dataflow Analysis
Dataflow property represent the certain information regarding
usefulness of data items for the purpose of optimization.
The data flow properties are:
1. Available expression
2. Live variable
Unit – 7 : Compiler 40
Available Expression
An expression x+y is available at a program point w if and only if along all paths
are reaching to w.
1. The expression x+y is said to be available at its evaluation point.
2. Neither of the two operands get modified before their use.
B1: t1=4*i
B4: t4=a[t2]
Expression 4 * i is the available expression for B2, B3 and B4 because this
expression has not been changed by any of the block before appearing in B4.
Unit – 7 : Compiler 41
Live variable
A live variable x is live at point p if there is a path from p to the
exit, along which the value of x is used before it is redefined.
Otherwise the variable is said to be dead at the point.
Unit – 7 : Compiler 42
Parameter Passing Methods
Unit – 7 : Compiler 43
Parameter passing methods
There are two types of parameters, Formal parameters & Actual
parameters.
And based on these parameters there are various parameter
passing methods, the common methods are:
1. Call by value
2. Call by result
3.Call by reference
4. Call by name
Unit – 7 : Compiler 44
Call by value
This is the simplest method of parameter passing.
The call by value method of passing arguments to a function
copies the actual value of an argument into the formal parameter
of the function.
The operations on formal parameters do not change the values of
a parameter.
Unit – 7 : Compiler 45
Call by name
It is similar to call by value with one difference.
At the time of return from the called function, the values of formal
parameters are copied back into corresponding actual parameter.
Unit – 7 : Compiler 46
Call by reference
This method is also called as call by address or call by location.
The call by reference method of passing arguments to a function
copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual
argument used in the call.
It means the changes made to the parameter affect the passed
argument.
Unit – 7 : Compiler 47
Call by Name
This is less popular method of parameter passing.
Procedure is treated like macro.
The procedure body is substituted for call in caller with actual
parameters substituted for formals.
The local names of called procedure and names of calling
procedure are distinct.
Unit – 7 : Compiler 48