3unit cd IntermediateCode_Part1
3unit cd IntermediateCode_Part1
Dr. N. Kalyani
Professor, CSE
Contents
Intermediate-Code Generation:
Variants of Syntax Trees
Three-Address Code
Types and Declarations
Type checking
Control Flow
Back Patching
Switch Statements
Intermediate Code for Procedures
Parsing & Intermediate Code generation
• In the analysis-synthesis model of a compiler, the front end
analyzes a source program and creates an intermediate
representation, from which the back end generates target code.
• Source language is confined to the front end, and details of the
target machine to the back end.
• While parsing, static checking, and intermediate-code generation
are done sequentially; All these can be combined and folded into
parsing.
Intermediate Code Representation
• Intermediate representations, including syntax trees and three-
address code.
• Syntax trees are high level; they depict the natural hierarchical
structure of the source program and are well suited to tasks
like static type checking /evaluation ordering.
• A low-level representation is suitable for machine-dependent
tasks like register allocation and instruction selection.
• “Three-address code" comes from instructions of the general
form x = y op z with three addresses: two for the operands y
and z and one for the result x.
• Three-address code can range from high to low-level,
depending on the choice of operators.
Variants of Syntax Trees
1. Directed Acyclic Graphs for Expressions
2. The Value-Number Method for Constructing DAG's
1. Directed Acyclic Graphs for Expressions
a + a * (b - c) + (b - c) * d
Construction of DAG
2. Quadruples
3. Triples
a + a * (b - c) + (b - c) * d
Addresses and Instructions to build Three-address code
Three -address code can be implemented using records called
quadruples and triples
An address can be one of the following:
A name : For convenience, we allow source-program names
(pointer to its symbol-table entry) to appear as addresses in
three-address code.
A constant : In practice, a compiler must deal with many
different types of constants and variables.
A compiler-generated temporary : Useful, especially in
optimizing compilers, to create a distinct name each time a
temporary is needed. These temporaries can be combined, if
possible, when registers are allocated to variables.
Addresses and Instructions to build Three-address code
a = (b * - c) + (b * - c)
Indirect triples
• Indirect triples consist of a listing of pointers to triples, rather than
a listing of triples themselves.
• With indirect triples, an optimizing compiler can move an
instruction by reordering the instruction list, without affecting the
triples themselves.
• When implemented in Java, an array of instruction objects is
analogous to an indirect triple representation, since Java treats the
array elements as references to objects.
Translate the arithmetic expression
a) a + - (a -b- c)
b) a = b[i] + c[j]
1. A syntax tree.
2. Quadruples.
3. Triples.
4. Indirect triples.
Static Single-Assignment Form
• Static single-assignment form (SSA) is an intermediate representation
that facilitates certain code optimizations.
• Two distinctive aspects distinguish SSA from three-address code.
• The first is that all assignments in SSA are to variables with distinct
names; hence the term static single-assignment.
• Note that subscripts distinguish each definition of variables p and q in the
SSA representation.
• The same variable may be defined in two different control-flow paths in
a program.
Types and Declarations
1 Type Expressions
2 Type Equivalence
3 Declarations
5 Sequences of Declarations
Types and Declaration
Type checking
• It uses logical rules to reason about the behavior of a program at
run time.
• Specifically, it ensures that the types of the operands match the
type expected by an operator.
• Example : && operator expects its two operands to be Booleans,
the result is also of type Boolean.
Translation Applications
• From the type of a name, a compiler can determine the storage
that will be needed for that name at run time.
• Type information is also needed to calculate the address denoted
by an array reference, to insert explicit type conversions.
1. Type Expressions
• Types have structure, which we shall represent using type
expressions.
• A type expression is either a basic type or is formed by
applying an operator called a type constructor to a type
expression.
• The sets of basic types and constructors depend on the
language to be checked.
1. Type Expressions
Definition of type expressions:
The above grammar that deals with basic and array types was used to
illustrate inherited attributes.
• Nonterminal D generates a sequence of declarations.
• Nonterminal T generates basic, array, or record types.
• Nonterminal B generates one of the basic types int and float.
• Nonterminal C, for "component," generates strings of zero or more
integers, each integer surrounded by brackets.
4. Storage Layout for Local Names
4. Storage Layout for Local Names