正在加载图片...
TAC Three address code Three-address code (tac) will be the intermed iate representation used in our Decaf compiler. It is essentially a generic assembly language that falls in the lower-end of the mid-level IRs. Many compilers use an IR similar to TAC. It is a sequence of instructions, each of which can have at most three operands. The operands could be two operands to a binary arithmetic op 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, here is an arithmetic expression and its corresponding TAC translation t1=b*c 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 subprogram calls. Here is an example of the tac for a branching translation for an if-statement if (a< b+ c) t1 = b+c t2 = a< t1 工fZt2Goto0 L0:t4=b*c; And here is an example of the translation involving a function call and array access Int n Binky(arr [n] t1=4 t2=t1*n; t3=arr+ t2: LCall Binky(t4) Decaf TAc instruction formats The convention followed in the examples below is that tl, t2, and so on refer to variables(either from source program or temporaries)and Ll, L2, etc are used forTAC 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. Many compilers use an IR similar to TAC. It is a sequence of instructions, each of which can have at most three operands. The operands could be two operands to a binary arithmetic op 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, here is an arithmetic expression and its corresponding TAC translation: 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 subprogram calls. Here is an example of the TAC for a branching translation for 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 = t3; L0: t4 = b * c; c = t4; And here is an example of the translation involving 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); LCall _Binky(t4); Decaf TAC instruction formats The convention followed in the examples below is that t1, t2, and so on refer to variables (either from source program or temporaries) and L1, L2, etc. are used for
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有