正在加载图片...
CS143 Handout #29 Autumn 200 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 Decaf compiler. It is essentially a ably language that falls in the lower-end of the mid-level IRs. Some variant of 2 3 or 4 address code is fairly commonly 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 to compare 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 t1= b t2 =b* d: down to three. Of course, it's a little more complicated 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) tl =b+ci t1 2 Goto L0: ftt z3=4 And here is an example of the TAC translation for a function call and array access Int ni n LCall ReadIntegeri n ReadInteger()i Binky(arr[n]) t2=t1*n; rr t2 t4=*(t3); PushParam LCall BinkCS143 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 Decaf compiler. 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 fairly commonly 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 to compare 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 the compiler as needed to keep the number of operands down to three. Of course, it's a little more complicated 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) a = a - c; c = b * c; _t1 = b + c; _t2 = a < _t1; IfZ _t2 Goto _L0; _t3 = a - c; a = _3; _L0: _t4 = b * c; c = _t4; And here is an example of the TAC translation for a function call and array access: int n; n = ReadInteger(); Binky(arr[n]); n = LCall _ReadInteger; _t1 = 4; _t2 = _t1 * n; _t3 = arr + _t2; _t4 = *(_t3); PushParam _t4; LCall _Binky;
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有