[go: up one dir, main page]

0% found this document useful (0 votes)
1K views5 pages

Three Address Codes

Three address code is an intermediate code representation that uses at most three operands and one operator per instruction. It stores computation results in temporary variables generated by the compiler. There are three common representations: quadruples with four fields per instruction, triples that use pointers instead of temporaries, and indirect triples that store temporary references separately. For the expression (x + y) * (y + z) + (x + y + z), the three address code would compute each subexpression and store it in a temporary variable before combining the results.

Uploaded by

renumathav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views5 pages

Three Address Codes

Three address code is an intermediate code representation that uses at most three operands and one operator per instruction. It stores computation results in temporary variables generated by the compiler. There are three common representations: quadruples with four fields per instruction, triples that use pointers instead of temporaries, and indirect triples that store temporary references separately. For the expression (x + y) * (y + z) + (x + y + z), the three address code would compute each subexpression and store it in a temporary variable before combining the results.

Uploaded by

renumathav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Three address code in Compiler

Prerequisite – Intermediate Code Generation


Three address code is a type of intermediate code which is easy to generate and can
be easily converted to machine code.It makes use of at most three addresses and one
operator to represent an expression and the value computed at each instruction is
stored in temporary variable generated by compiler. The compiler decides the order of
operation given by three address code.
General representation –
x = y op z
Where a, b or c represents operands like names, constants or compiler generated
temporaries and op represents the operator
Example-1: Convert the expression a * – (b + c) into three address code.

Example-2: Write three address code for following code


for(i = 1; i<=10; i++)
{
a[i] = x * 5;
}
Implementation of Three Address Code –
There are 3 representations of three address code namely
1. Quadruple
2. Triples
3. Indirect Triples
1. Quadruple –
It is structure with consist of 4 fields namely op, arg1, arg2 and result. op denotes the
operator and arg1 and arg2 denotes the two operands and result is used to store the
result of the expression.
Advantage –
 Easy to rearrange code for global optimization.
 One can quickly access value of temporary variables using symbol table.
Disadvantage –
 Contain lot of temporaries.
 Temporary variable creation increases time and space complexity.
Example – Consider expression a = b * – c + b * – c.
The three address code is:
t1 = uminus c
t2 = t1 * b
t3 = uminus c
t4 = t3 * b
t5 = t2 + t4
a = t5

2. Triples –
This representation doesn’t make use of extra temporary variable to represent a single
operation instead when a reference to another triple’s value is needed, a pointer to that
triple is used. So, it consist of only three fields namely op, arg1 and arg2.
Disadvantage –

 Temporaries are implicit and difficult to rearrange code.


 It is difficult to optimize because optimization involves moving intermediate code.
When a triple is moved, any other triple referring to it must be updated also. With
help of pointer one can directly access symbol table entry.
Example – Consider expression a = b * – c + b * – c

3. Indirect Triples –
This representation makes use of pointer to the listing of all references to computations
which is made separately and stored. Its similar in utility as compared to quadruple
representation but requires less space than it. Temporaries are implicit and easier to
rearrange code.
Example – Consider expression a = b * – c + b * – c

Question – Write quadruple, triples and indirect triples for following expression : (x + y)
* (y + z) + (x + y + z)
Explanation – The three address code is:
t1 = x + y
t2 = y + z
t3 = t1 * t2
t4 = t1 + z
t5 = t3 + t4

You might also like