CD - Chp6 Updated
CD - Chp6 Updated
COMPILER DESIGN
SUBJECT CODE: 203105351
Vaibhavi Patel, Assistant Professor
Computer Science & Engineering
CHAPTER-5
Intermediate code generation
Contents
• 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:
• Each instruction in triples presentation has three fields : op, arg1, and arg2.
• The results of respective sub-expressions are denoted by the position of expression.
• Triples represent similarity with DAG and syntax tree. They are equivalent to DAG while representing
expressions.
• 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
•Static single-assignment form (SSA) is an intermediate representation that facilitates certain code
optimizations.
•The first is that all assignments in SSA are to variables with distinct names; hence the term static single-
assignment.
Intermediate program in three-address code and SSA
p=a+b p1 = a + b
q=p-c q1 = P1 - c
p=q*d p2 = q1 *d
P= - P P3 = -P2
q=p+q q2= p3 + q1
•In the syntax directed translation, assignment statement is mainly deals with expressions. The expression
can be of type real, integer, array and records.
Consider the grammar
S → id := E
E → E1 + E2
E → - E1
E → (E1)
E → id
•The translation scheme of above grammar is given below:
Translation of expressions and assignment statements
• Addressing Array Elements
Layouts for a two-dimensional array:
• Semantic actions for array reference
• Semantic actions for array reference
There are 4 types of control statements: Each contains at least one Boolean condition, based on the
condition next statement will decide. So we may need to do an unconditional jump from one statement to
another to perform these control statements.
1. If..then..else...
2. If.. then...
3. While..do…
4. Case statement
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
1. IF E then S1 else S2
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
2. IF E then S1
1. If a<b go to (3)
If(a<b)
2. Go to (5)
x=y+z
3. t1 = y+z
4. x=t1
5. next statement
3. While E do S1
3. While E do S1
When you see keyword switch then we have two new labels test label and next
label and also t temporary variable is generated. Based on the evaluation of E, the t
value is generated. That t value helps to get the best-suited case for the t value. Test
label has all the possible t value and tests whether t value matches with any case.
After completion of all statements all control transfer to the next label which has
the next statements for evaluation
4. Case Statement
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
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
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.
THANK YOU
www.paruluniversity.ac.in