2-Intermediate Code Generation-Quadruple

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Intermediate Code Generation

Unit 4

Sini Anna Alex


The description of three-address instructions

• Three address instructions can be implemented as


objects or as records with fields for the operator and
the operands. Three such representations are called
quadruples, triples, and indirect triples.
• Quadruples- A quadruple (or just quad) has four fields:
op, arg1, arg2, and result.
• The op field contains an internal code for the operator.
• For instance, the three-address instruction x = y +z is
represented by placing + in op, y in arg1, z in arg2, and
x in result.
Quadruple Representation
The following are some exceptions to this rule:
• 1. Instructions with unary operators like
x = minus y or x = y do not use arg2.
For a copy statement like x = y, op is =, while for most
other operations, the assignment operator is
implied.
• 2. Operators like param use neither arg2 nor result.
• 3. Conditional and unconditional jumps put the
target label in result. goto L
Quadruple representation of
a = b*-c + b*-c
Triples
A triple has only three fields, which we call op, arg1, and arg2. With
triples, the result of an operation is referred to by its position, so
moving an instruction may require us to change all references to that
result.
Triple Representation of 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 a
directing the triples themselves
Static Single Assignment Form(SSA)
• Intermediate Representation that facilitates
certain code optimizations.
• Intermediate program in three-address code and
SSA
• The same variable may be defined in two different
control-flow paths in a program.

• has two control-flow paths in which the variable x


gets defined.

• Here, (x1,x2) has the value x1 if the control flow


passes through the true part of the conditional and
the value x2 if the control flow passes through the
false part.
Types and Declarations
• The applications of types can be grouped under checking and
translation:
• Type checking: uses logical rules to reason about the
behavior of a program at run time.
– For example, the && operator in Java 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.
-For example, Type information is also needed to calculate the address
denoted by an array reference, to insert explicit type conversions.
Type Expressions
• Types have structure, which 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 array type int[2][3] can be read as “array of 2 arrays of
3 integers each" and written as a type expression
array(2,array(3,integer)). The operator array takes two
parameters, a number and a type.
Definition of type expressions
•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 fields. A type expression can
be formed by applying the record type constructor to the field names
and their types.
•A type expression can be formed by using the type constructor -> for
function types. We write s -> t for “function from type s to type t."

Type expression for int[2][3]


Type Equivalence
• When are two type expressions equivalent?
– Many type-checking rules have the form, “if two type expressions
are equal then return a certain type else error.“
• When type expressions are represented by graphs, two types
are structurally equivalent if and only if one of the following
conditions is true:
– They are the same basic type.
– They are formed by applying the same constructor to structurally
equivalent types.
– One is a type name that denotes the other.
• The first two conditions in the above definition lead to name
equivalence of type expressions. Name-equivalent
expressions are assigned the same value number.
CFG for valid Declarations in C
Language

* record -> can be a struct or union


Storage Layout for local names
• From the type of a name, we can determine the amount
of storage that will be needed for the name at run time.
• Eg: integer means 4 bytes of storage, int A[10].
• At compile time, we can use these amounts to assign
each name a relative address.
• A[2] array reference can be accessed by base address(A)+2*4.
• The type and relative address are saved in the symbol-
table entry for the name.
• Symbol Table gets added with the additional specifications.
• A is an identifier with type array(10,int)
Computing types and their widths
Syntax-directed translation of array types

The width of a type is the


number of storage units
needed for objects of
that type.
A basic type, such as a
character, integer, or
float, requires an integral
number of bytes.

SDT uses synthesized


attributes type and width for
each nonterminal and two
variables t and w to pass type
and width information.
In syntax-directed
translation, t and w would be
inherited attributes for C.
Sequences of Declarations
• Languages such as C and Java allow all the declarations
in a single procedure to be processed as a group.
• Therefore, we can use a variable, say offset, to keep
track of the next available relative address.
• For eg: We have a declaration in C as given below
int a;
struct{int b; float x;}p;

Total Storage allocation should be 4 + (4+8) =16 bytes.


(For int – 4 bytes , float – 8 bytes)
Computing the relative addresses of declared Handling of field names in records
names
A record type has the form
record(t), where record is the
type constructor, and t is a symbol
table object that holds
information about the fields of
this record type

D -> T id;D1 creates a symbol table entry


by executing
top . put (id . lexeme, T . type, offset).
Here top denotes the current symbol
table. Class Env implement symbol tables. The call
Env.push(top) pushes the current symbol table
denoted by top onto a stack. Variable top is
then set to a new symbol table. Similarly,
offset is pushed onto a stack called Stack.
Compute the type and relative address for the Declaration
statement given below

float x;
record { float a; float b;} p;
Use the given Grammar for computation:
THANK YOU

You might also like