Unit Viii
Unit Viii
Unit Viii
CODE GENERATION
Machine dependent code generation, object
code forms, generic code generation algorithm,
Register allocation and assignment. Using DAG
representation of Block.
• The final phase in our compiler model is the code generator. It takes intermediate
representation (IR) produced by the front end of the compiler as input along with
relevant symbol table information and produces semantically equivalent target
program as output.
• The following diagram shows the position of code generator.
Sum = 0 t1 = 4*i
t2=a[t1]
i=0
t3=Sum+t2
B1 Sum=t3
t4=i + 1
i = t4
B2 If i <= 10 goto B2
• The DAG representation for basic block B2 is given as below:
• Consider the numbering as t1 = 4*I (1)
t2=a[t1] (2)
t3 t3=Sum+t2 (3)
+ Sum=t3 (4)
t4=i + 1 (5)
i = t4 (6)
If i <= 10 goto B2 (7)
t2 (1)
[] <=
sum
10
t1 t4 ,i
* +
a
i 1
4
• Construction of DAG for the basic block is done by applying the following steps:
1. If y is undefined in the three address statement x = y op z then create y similarly create z if
undefined.
2. Create a node (op) whose left child is node (y) and right child is node (z). Also check for common
subexpressions if any.
3. Check for a node labeled op, which has a child node (y), such that node n will be node (y)
4. Delete x from the list of identifiers for node (x). And append x to the list of attached identifiers
for node n found in 2.
Applications of DAG
• DAG is used in determining the common subexpressions.
• DAG is used in determining which names are used inside the block and computed
outside the block.
• DAG is used in determining which statements of the block could have their
computed value outside the block.
• DAG is used in simplifying the list of quadruples by eliminating the common
subexpression and not performing the assignment of the form x=y unless it is
necessary.
T HE
hink +ve work ard ducate,