0% found this document useful (0 votes)
9 views33 pages

CD - Ch.6

The document covers intermediate code generation in compiler design, detailing various representations such as quadruples, triples, trees, and SSA forms. It discusses the benefits of intermediate representations, including machine independence and code optimization, and provides examples of translating expressions and control statements into three-address code. Additionally, it addresses Boolean expressions, short-circuit code, and the concept of backpatching for managing jump statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views33 pages

CD - Ch.6

The document covers intermediate code generation in compiler design, detailing various representations such as quadruples, triples, trees, and SSA forms. It discusses the benefits of intermediate representations, including machine independence and code optimization, and provides examples of translating expressions and control statements into three-address code. Additionally, it addresses Boolean expressions, short-circuit code, and the concept of backpatching for managing jump statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

COMPILER DESIGN

SUBJECT CODE: 203105351


Vaibhavi Patel, Assistant Professor
Computer Science & Engineering
CHAPTER-5
Intermediate code generation
Contents
• Different intermediate representations –
– Quadruples
– Triples
– Trees
– SSA forms
•Translation of expressions
•Assignment statements
Intermediate Representation

Fig 6.1: Intermediate Representation (IR)

Intermediate Representation (IR):


– An abstract machine language
– Expresses operations of target machine
– Not specific to any particular machine
– independent of source language.
Intermediate Representation- Benefits

• 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:

Fig 6.2 Quadruples example


Intermediate representations - Three address code- Triples
• 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.

Fig 6.3 Triples example

• 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

Fig 6.4 Example of syntax tree


Intermediate representations - SSA
•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

(a) Three-address code. (b) Static single-assignment form.


Translation of expressions and assignment statements
•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

Fig 6.5 Three-address code for expressions


Addressing Array Elements
Layouts for a two-dimensional array:
Semantic actions for array reference
Semantic actions for array reference

Fig 6.6 Semantic actions for array references


Semantic actions for array reference

Nonterminal L has three synthesized attributes:


•L.addr
•L.array
•L.type
CHAPTER-6
Intermediate code generation
Flow of control statements
•Content inside
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
•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

If(a<b) 1. If a<b go to (3)


x=y+z 2. Go to (5)
3. t1 = y+z
4. x=t1
5. next statement
3. While E do S1
•Content inside
3. While E do S1
•Content inside
Convert this code into Three address code

Int i=0 1. i=0


While (i<=10) 2. if i<=10 go to 4
3. go to 7
printf(i=%d,i);
4. print i
i++; 5. i=i+1
6. go to 2
… 7. next statement
4. Case Statement
•Content inside
4. Case Statement
•Content
When youinside
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
•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

You might also like