written 8.4 years ago by | • modified 8.4 years ago |
Mumbai University > Computer Engineering > Sem6 > System Programming and Compiler Construction
Marks: 10M
Year: May 2015
written 8.4 years ago by | • modified 8.4 years ago |
Mumbai University > Computer Engineering > Sem6 > System Programming and Compiler Construction
Marks: 10M
Year: May 2015
written 8.4 years ago by | • modified 8.4 years ago |
Intermediate code Generation phase takes as input parse tree representation and generates an intermediate representation.
So that the translation process can be done faster.
Program that performs ICG is called Intermediate code Generation.
While translating a source program into a functionally equivalent object code representation, a parser may first generate an intermediate representation.
This makes retargeting of the code possible and allows some optimizations to be carried out that would otherwise not be possible.
The following are commonly used intermediate representations:
1. Postfix notation
$$\text{Ab – Cd * Ab - +}$$
2. Syntax tree
3. Three-address code
$$T1 = B * C \\ T2 = A + T2 \\ T3 = T3 + D$$
Sometimes a statement might contain less than three references; but it is still called a three-address statement.
The following are the three-address statements used to represent various programming language constructs:
Used for representing arithmetic expressions:
$$ X = Y \ op \ Z \\ X = \ \ op \ Y \\ X = Y $$
$$If A \gt B \ \ goto L \\ goto \ \ L \ \ $$
$$x = y[i] \\ x[i] = y \\ x = ^*y \\ ^* x = y$$
$$Param \ \ T \\ Call \ \ p,n$$
Representation of three address code
Records with fields for the operators and operands can be used to represent three-address statements.
It is possible to use a record structure with four fields: the first holds the operator, the next two hold the operand1 and operand2, respectively, and the last one holds the result.
This representation of a three-address statement is called a "quadruple representation".
2. Triple Representation
The contents of the operand1, operand2, and result fields are therefore normally the pointers to the symbol records for the names represented by these fields.
Hence, it becomes necessary to enter temporary names into the symbol table as they are created.
This can be avoided by using the position of the statement to refer to a temporary value.
If this is done, then a record structure with three fields is enough to represent the three-address statements:
The first holds the operator value, and the next two holding values for the operand1 and operand2, respectively. Such a representation is called a "triple representation".
The contents of the operand1 and operand2 fields are either pointers to the symbol table records, or they are pointers to records (for temporary names) within the triple representation itself.
For example, a triple representation of the three-address code for the statement x = (a + b)* ˆ’ c/d.
3. Indirect Triple Representation
Another representation uses an additional array to list the pointers to the triples in the desired order.
This is called an indirect triple representation. For example, a triple representation of the three-address code for the statement x = ( a + b )* ˆ’ c/d
Comparison