0% found this document useful (0 votes)
2 views21 pages

Intermediate Code Generation

Uploaded by

shrutikurade0
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)
2 views21 pages

Intermediate Code Generation

Uploaded by

shrutikurade0
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/ 21

Compiler Course

Chapter 4-B
Intermediate Code Generation
Introduction
⚫ Intermediate code is the interface between front end
and back end in a compiler
⚫ In this chapter we study intermediate representations,
static type checking and intermediate code generation

Static Intermediate Code


Parser
Checker Code Generator Generator
Front end Back end
Intermediate Code Generation
/ Intermediate Language
⚫ A compiler may produce an explicit intermediate codes
representing the source program.
⚫ These intermediate codes are generally machine
(architecture independent). But the level of intermediate
codes is close to the level of machine codes.
Intermediate Code Generation
/ Intermediate Language
⚫ Ex:
newval := oldval * fact + 1
id1 := id2 * id3 + 1

MULT id2, id3, temp1 Intermediates Codes (Quadraples)

ADD temp1, #1, temp2


MOV temp2, ,id1
3-address form
⚫ Has the form:
⚫ op-code operandam, operandum, resultant

This is closer to a mathematical functional form, which


would be

resultant = op-code (operandam, operandum)


3-address form
⚫ A 3-address instruction would redundantly state the

address of the i variable twice: as the operandum and as the


resultant as follows:

⚫ ADD 0x01, &i, &i ; 3-address

⚫ This form is the most convenient from a programming

perspective.
Three address code
⚫ In a three address code there is at most one operator at
the right side of an instruction
⚫ Example:
a+a*(b-c)+(b-c)*d
+
t1 = b – c
+ * t2 = a * t1
t3 = a + t2
* t4 = t1 * d
d
t5 = t3 + t4
a -

b c
Data structures for three address
codes
⚫ Quadruples
⚫ Has four fields: op, arg1, arg2 and result

⚫ Triples
⚫ Temporaries are not used and instead references to
instructions are made

⚫ Indirect triples
⚫ In addition to triples we use a list of pointers to triples
Implementation of 3-address
• 3-address statement is an abstract form of
intermediate code
• Need a concrete form
• Records with fields for operator and operands
• Quadruples
• Triples
• Indirect triples

Copyright (c) 2011 Ioanna Dionysiou


Quadruples
• It is a record structure with 4 fields
• Operator
• Argument 1
• Argument 2
• Result
op arg1 arg2 result
unimus c t1
a:= b * -c + b * -c
* b t1 t2
t1 := -c unimus c t3
t2 := b * t1
t3 := -c * b t3 t4
t4 := b * t3
+ t2 t4 t5
t5 := t2 + t4
a := t5 := t5 a

Copyright (c) 2011 Ioanna Dionysiou


Triples
• It is a record structure with 3 fields
• Operator
• Argument 1 (pointer to symbol table or triple)
• Argument 2 (pointer to symbol table or triple)
• Idea
• avoid entering temporary names to symbol table
• Refer to a temp value by the position of that statement that
computes it
a:= b * -c + b * -c op arg1 arg2
unimus c
t1 := -c
t2 := b * t1 * b (0)
t3 := -c unimus c
t4 := b * t3
* b (2)
t5 := t2 + t4
a := t5 + (1) (3)
assign a (4)
Copyright (c) 2011 Ioanna Dionysiou
Indirect Triples
• It is a record structure with 3 fields
• Operator
• Argument 1 (pointer to symbol table or triple)
• Argument 2 (pointer to symbol table or triple)
• Idea
• List pointers to triples instead listing triples

statement op arg1 arg2


(0) (14) unimus c
(1) (15) * b (14)
(2) (16) unimus c
(3) (17) * b (16)
(4) (18) + (15) (17)
(5) (19) assign a (18)

Copyright (c) 2011 Ioanna Dionysiou


In-class Exercise
• Show the 3-address code for the following
statements:

a := 5 + 6
c := a + 3

• Try to optimize the 3-address code generated

Copyright (c) 2011 Ioanna Dionysiou


Three address code
Example t1 = minus c
t2 = b * t1
⚫ b * minus c + b * minus c t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5

Quadruples Triples Indirect Triples


op arg1 arg2 result op arg1 arg2 op op arg1 arg2
minus c t1 0 minus c 35 (0) 0 minus c
* b t1 t2 1 * b (0) 36 (1) 1 * b (0)
minus c t3 2 minus c 37 (2) 2 minus c
* b t3 t4 3 * b (2) b (2)
38 (3) 3 *
+ t2 t4 t5 4 + (1) (3) 39 (4) 4 + (1) (3)
= t5 a 5 = a (4) 40 (5) 5 = a (4)
Type Expressions
Example: int[2][3]
array(2,array(3,integer))

⚫ A basic type is a type expression


⚫ A type name is a type expression
⚫ A type expression can be formed by applying the array type
constructor to a number and a type expression.
⚫ A record is a data structure with named field
⚫ A type expression can be formed by using the type constructor → for
function types
⚫ If s and t are type expressions, then their Cartesian product s*t is a
type expression
⚫ Type expressions may contain variables whose values are type
expressions
Static Single Assignment
Static Single Assignment

You might also like