Three-Address Code (TAC)

where t1 and t2 are compiler-generated temporary names.

The use of names for the intermediate values computed by a program allows three-address code to be rearranged easily.

Example: Three-address code is a linearized representation of a syntax
tree or a DAG in which explicit names correspond to the interior nodes of the
graph.
The DAG in Fig. 6.3 is repeated in Fig. 6.8, together with a correspond-
ing three-address code sequence.

Addresses and Instructions

Three-address code is built from two concepts: addresses and instructions. In
object-oriented terms, these concepts correspond to classes, and the various
kinds of addresses and instructions correspond to appropriate subclasses.
Alternatively, three-address code can be implemented using records with fields
for the addresses; records called quadruples and triples.
An address can be one of the following:

Instruction TypeSyntax ExampleDescription
Binary Assignmentx = y op zBinary arithmetic/logical operations
Unary Assignmentx = op yUnary operations (negation, type cast)
Copyx = yAssign value of one variable to another
Unconditional Jumpgoto LJumps to a labeled instruction
Conditional Jump (Boolean)if x goto LJump if condition is true
Conditional Jump (False)ifFalse x goto LJump if condition is false
Relational Conditional Jumpif x relop y goto LJump if relational condition is true
Procedure Call / Returnparam x, call p, nUsed to call procedures/functions
Indexed Copy (Array Access)x = y[i], x[i] = yAccess or assign array elements
Address / Pointer Handlingx = &y, x = *y, *x = yPointer-based assignments

Leave a Reply

Your email address will not be published. Required fields are marked *