CD - Ch.6
CD - Ch.6
• Retargeting is facilitated
• Machine independent Code Optimization can be applied.
Intermediate representations
Intermediate codes can be represented in a variety of following ways and they have their
own benefits.
– Three address code
• Quadruples
• Triples
– Trees
– SSA forms
Intermediate representations - Three address code
• A three-address code has at most three address locations to calculate the expression.
• A three-address code can be represented in two forms :
– Quadruples
– Triples
Intermediate representations - Three address code- Quadruples
Each instruction in quadruples presentation is divided into four fields: operator, arg1, arg2, and result.
•For example:
a = b + c * d;
is represented below in quadruples format:
• Triples face the problem of code immovability while optimization, as the results are positional and
changing the order or position of an expression may cause problems.
Intermediate representations - Trees
• Syntax tree is a variant of the parse tree, where each leaf represents an operand and each interior node
represent an operator.
• A sentence a * (b +d) would have the following syntax tree
Each control statement has one or more Boolean expressions. So let name that Boolean expression as E. so if
E is true then control transfer to the E.True label. And if E is false then control transfer to the E.False label.
1. IF E then S1 else S2
•Content inside
1. IF E then S1 else S2
•Content inside
Convert this code into Three address code
1. If a<b go to (3)
2. Go to (6)
If(a<b) 3. t1 = y+z
x=y+z 4. x=t1
else: 5. go to 9
p=q+r 6. t2=q+r
7. p=t2
8. go to 9
9. next statement
2. IF E then S1
•Content inside
2. IF E then S1
•Content inside
Convert this code into Three address code
Switch(i+j) 1. t1=i+j
{ 2. go to 9
3. t2=y+z
Case 1: x=y+z 4. x=t2
Case 2: p=q+r 5. t3=q+r
Default: u=v+w 6. p=t3
7. t4=v+w
}
8. u=t4
9. if t1=1 go to 3
10.if t1=2 go to 5
11.next statement
Short-Circuit Code
•Content inside
When We place multiple Boolean operator like “and,” “or” and “not” in one statement and
that can also translate a boolean expression into three-address code without generating
code for any of the boolean operators and without having the code necessarily evaluate the
entire expression. This style of evaluation is sometimes called “short-circuit” or “jumping”
code.
For example,
If(a>b or c<d and e>=f)
If three address code generate for just a>b and go to next statement without considering
entire statement
Boolean Expressions
•Content inside
Boolean expressions have two primary purposes. They are used to compute logical
values, but more often they are used as conditional expressions in statements that alter the
flow of control, such as if-then-else, or while-do statements.
Boolean expressions are composed of the boolean operators ( and, or, and not ) applied
to elements that are boolean variables or relational expressions. Relational expressions are
of the form E1 relop E2, where E1 and E2 are arithmetic expressions.
Here we consider boolean expressions generated by the following grammar :
E →E or E | E and E | not E | ( E ) | id relop id | true | false
Boolean Expressions
•Content inside
Methods of Translating Boolean Expressions: There are two principal methods of
representing the value of a boolean expression. They are :
• To encode true and false numerically and to evaluate a boolean expression analogously to
an arithmetic expression. Often, 1 is used to denote true and 0 to denote false.
• To implement boolean expressions by flow of control, that is, representing the value of a
boolean expression by a position reached in a program. This method is particularly
convenient in implementing the boolean expressions in flow-of-control statements, such
as the if-then and while-do statements.
Backpatching
•Content inside
The easiest way to implement the syntax-directed definitions for Boolean expressions is to use two passes. First,
just construct a syntax tree for the input, and then just walk the tree in depth-first order and compute the
translations.
The main problem for generating code for Boolean expressions and flow of control statements in one pass or
single pass is that in the process of singles pass we may not know the labels for go to or jump statements that
control must go to a particular statement if there is a jump statement.
Therefore, a series of branching statements with the targets of the jumps left unspecified is generated. Each
statement will be put on a list of goto statements whose labels will be filled in when the proper label can be
determined. We call this subsequent filling in of labels backpatching.
www.paruluniversity.ac.in