Compiler Design Unit 5
Compiler Design Unit 5
Rules:
•The basic blocks are the nodes to the flow graph
•The block whose leader is the first statement is called initial block.
•There is a directed edge from block B1 to block B2 if B2
immediately follows B1 in the given sequence, we can say that B1 is
a predecessor of B2
(If-else) case
Q: Translate the expression into Three address code
if a<b then x=y+z else p=q+r
Sol: 3-address code :
L: 1 if a < b goto (3) B1 L1: if a < b goto (B3)
L: 2 goto (6)
L: 3 t1 = y + z B2 L2: goto (B4)
4 x = t1
5 goto (Next)
L: 6 t2 = q + r B3 L3: t1 = y + z
7 p = t2 x = t1
goto (Next)
8 goto (Next)
L4: t2 = q + r
B4
p = t2
goto (Next)
(Do-while) case
Q: Translate the expression into Three address code
do x = y + z while a < b
3 goto (4)
L: 4 if a < b goto (1)
L: 5 goto (NEXT)
B2 L2: if a < b goto (B1)
B4 L4: goto(NEXT)
CODE OPTIMIZATION
•It is required to produce an efficient target code.
•The semantic equivalence of the source program must not be
changed.
•It must be achieved without changing the algorithm of the
program.
•Optimized code
Executes faster.
Efficient memory usage.
Yielding better performance.
Compile-Time Evaluation
• Expressions whose values can be pre - computed at
the compilation time
• Two ways:
– Constant folding
– Constant propagation
CODE OPTIMIZATION TECHNIQUE
Compile-Time Evaluation
• Constant folding: Evaluation of an expression
with constant operands to replace the
expression with single value
• Example:
area := (22.0/7.0) * r ** 2
area := 3.14286 * r ** 2
CODE OPTIMIZATION TECHNIQUE
Compile-Time Evaluation
• Constant Propagation: Replace a variable with
constant which has been assigned to it earlier.
Example:
pi :=3.14286 area = 3.14286 * r ** 2
area = pi * r ** 2
CODE OPTIMIZATION TECHNIQUE
Example:
a := b * c temp := b * c
… a := temp
… …
x := b * c + 5 x := temp + 5
CODE OPTIMIZATION TECHNIQUE
Code Movement
Example: Unsafe code movement
If statement is
i=0
dead code because
If(i=1) this condition will
{ never satisfies
a=x+5
}
CODE OPTIMIZATION TECHNIQUE
Peephole optimization:
return z return y }
} }
CODE OPTIMIZATION TECHNIQUE
Goto L1 Goto L2
L1: Goto L2 L1: Goto L2 (can be deleted)
L2: a=b L2: a=b
CODE OPTIMIZATION TECHNIQUE
a++
DIRECTED ACYCLIC GRAPH
In compiler design, a directed acyclic graph (DAG)
is an abstract syntax tree (AST) with a unique node
for each value.
Or
RULE 2:
While constructing DAG, there is a check made to find if there is an existing
node with the same children. A new node is created only when such a node
does not exist. This action allows us to detect common sub-expressions and
eliminate the re-computation of the same.
RULE 3:
The assignments of the form x=y must not be performed until and unless it is a
must.
RULES FOR CONSTRUCTION OF DAG
Example: 2.
t0 = a + b
t1 = t0 + c
d = t0 + t1
Solution:
1)
3.
CONSTRUCTION OF DAG
Example: Consider the following three address code statements.
a=b*c 2) 5)
d=b
e=d*c
b=e
f=b+c
g=f+d
Solution: 3)
1)
6)
4)
CONSTRUCTION OF DAG
Ex:
DAG
t1 = b - c
t2 = a * t1
t3 = a + t2
t4 = t1 * d
t5 = t3 + t4
CODE GENERATOR
Design Issues in Code generator
In the code generation phase, various issues can arises:
1. Input to the code generator
2. Target program
3. Memory management
4. Instruction selection
5. Register allocation
6. Evaluation order
CODE GENERATOR
Design Issues in Code generator
1. Input to the code generator
•The input to the code generator contains the intermediate
representation of the source program and the information of the
symbol table. The source program is produced by the front end.
•Intermediate representation has the several choices:
a) Postfix notation
b) Syntax tree
c) Three address code
•The code generation phase needs complete error-free
intermediate code as an input requires.
CODE GENERATOR
Design Issues in Code generator
2. Target program:
The target program is the output of the code generator. The
output can be:
a) Assembly language: It allows subprogram to be separately
compiled.
b) Relocatable machine language: It makes the process of
code generation easier (for linker or obj).
c) Absolute machine language: It can be placed in a fixed
location in memory and can be executed immediately
(executable file).
CODE GENERATOR
Design Issues in Code generator
3. Memory management
• During code generation process the symbol table entries have
to be mapped to actual p addresses and levels have to be
mapped to instruction address.
• Mapping name in the source program to address of data is co-
operating done by the front end and code generator.
• Local variables are stack allocation in the activation record
while global variables are in static area.
CODE GENERATOR
Design Issues in Code generator
4. Instruction selection:
• Nature of instruction set of the target machine should be
complete and uniform.
• When you consider the efficiency of target machine then the
instruction speed and machine idioms are important factors.
• The quality of the generated code can be determined by its
speed and size.
CODE GENERATOR
Design Issues in Code generator
5. Register allocation
• Register can be accessed faster than memory. The instructions
involving operands in register are shorter and faster than
those involving in memory operand.
• The following sub problems arise when we use registers:
• Register allocation: In register allocation, we select the set of
variables that will reside in register.
• Register assignment: In Register assignment, we pick the register that
contains variable.
• Certain machine requires even-odd pairs of registers for some
operands and result.
CODE GENERATOR
Design Issues in Code generator
6. Evaluation order
The efficiency of the target code can be affected by the order in which the
computations are performed.
Some computation orders need fewer registers to hold results of
intermediate than others.
CODE GENERATOR
Target Machine
Target Machine
• The target computer is a type of byte-addressable machine. It
has 4 bytes to a word.
• The target machine has n general purpose registers, R0, R1,....,
Rn-1. It also has two-address instructions of the form:
op source, destination
Where, op is used as an op-code and source and destination are
used as a data field.
• It has the following op-codes:
• ADD (add source to destination)
• SUB (subtract source from destination)
• MOV (move source to destination)