Three address code
From Wikipedia, the free encyclopedia
In computer science, three address code (TAC) is a form of representing intermediate code used by compilers to aid in the implementation of code-improving transformations. Each TAC can be described as a quadruple (operator, operand1, operand2, result).
Each statement has the general form of:
where x, y and z are variables, constants or temporary variables generated by the compiler. op represents any operator, e.g. an arithmetic operator.
Expressions containing more than one fundamental operation, such as:
are not representable in three address code as a single instruction. Instead, they are decomposed into an equivalent series of instructions, i.e.,
The term three address code is still used even if some instructions use more or less than two operands. The key features of three address code are that every instruction implements exactly one fundamental operation, and that the source and destination may refer to any available register.
A refinement of three address code is Static single assignment form.
The three address code table:
Assignment Statement | x := y op z | x, y, z are names, constants, or compiler generated temporaries; op is any binary operator |
x := op y | op is a unary operator | |
Indexed Assignment | x := y[i]
x[i] := y |
x, y, i are data objects |
Copy Statement | x := y | x, y are data objects |
Unconditional jump | goto L | L is the label of the next statement to be executed |
Conditional jump | if x relop y goto L | x, y are data objects
relop is a binary operator that compares x to y and indicates true/false L is the label of the next statement to be executed if relop indicates true |
Procedure calls and returns | param x | x is a parameter |
call p, n | p is the procedure, n is the number of parameters | |
return y | y is an optional return value | |
Address and pointer assignments |
x := &y x := *y *x := y |
x is set to the location of y
x is set to the value at the location y the value pointed to by x is set to the r-value of y |
Take this example of an C-program
main ( ) { int i; int b [12]; i=0; while(i<12){ b[i]=i; i=i+1; } }
It can be translated into a 3 address code:
i:=0 //an assignment L1: if i < 12 goto L2 //a conditional jump goto L3 // an unconditional jump L2: t0:=i*4 b[t0]:= i // indexed assignment i:=i+1 goto L1 L3 :