CS143 Handout #29
Autumn 2001
TAC
Handout written by Maggie Johnson and revised by me.
Three address code
Three-address code (TAC) will be the intermediate representation used in our piler. It is
essentially a generic assembly language that falls in the lower-end of the mid-level IRs. Some
variant of 2, 3 or 4 address code is monly used as an IR, since it maps well to most
assembly languages.
Our TAC is a sequence of instructions, each of which can have at most three operands. The
operands could be two operands to a binary arithmetic operator and the third the result location, or
an operand pare to zero and a second location to branch to, and so on. For example, below
on the left is an arithmetic expression and on the right, is a translation into TAC:
a = b * c + b * d _t1 = b * c;
_t2 = b * d;
_t3 = _t1 + _t2;
a = _t3;
Notice the use of temp variables created by piler as needed to keep the number of operands
down to three. Of course, it's a little plicated than the above example, because we have to
translate branching and looping instructions, as well as function and method calls. Here is an
example of the TAC branching instructions used to translate an if-statement:
if (a < b + c) _t1 = b + c;
a = a - c; _t2 = a < _t1;
c = b * c; IfZ _t2 Goto _L0;
_t3 = a - c;
a = _3;
_L0: _t4 = b * c;
c = _t4;
And here is an example of the TAC transla
29 TAC examples 来自淘豆网www.taodocs.com转载请标明出处.