正在加载图片...
Using TAC with other languages The TAC generation that we have been looking at is fairly generic. Although we have talked about in the context of Decaf, a TAC generator for any programming language would generate a similar sequence of statements. For example, in the dragon book, the following format is used to define the TAC generation for a while loop. (P. 469 Aho/Sethi/Ullman) s -> while e do s1 ( s begin newlabel s. after newlabeli de gen(s begin:' E. code gen('if E place =!'0'goto safter gen('goto s begin) gen(safter One last idea before we finish.. A nice enhancement to a TAC generator is re-using temp variable names. For example, if we have the following expression E->E1+E2 Our usual steps would be to evaluate El into tl, evaluate E2 into t2, and then set t3 to their sum Will tl and t2 be used anywhere else in the program? How do we know when we can reuse these temp names? Here is a method from Aho/Sethi/Ullman(p. 480)for reusing temp names Keep a count c initialized to o 2)Whenever a temp name is used as an operand, decrement c by 3)Whenever a new temp is created, use this new temp and increase c by one x= a *b+c* (C (c=0)T0=T0+T1 TO Note that this algorithm expects that each temporary name will be assigned and used exactly once, which is true in the majority of cases Bibliography J. P. Bennett, Introduction to Compiling Techniques. Berkshire, England: McGraw-Hill, 1990 S Muchnick, Advanced Compiler Design and Implementation. San Francisco, CA: Morgan Kaufmann, 1997 A Pyster, Compiler design and Construction. New york, NY: Van Nostrand reinhold, 19888 Using TAC with other languages The TAC generation that we have been looking at is fairly generic. Although we have talked about it in the context of Decaf, a TAC generator for any programming language would generate a similar sequence of statements. For example, in the dragon book, the following format is used to define the TAC generation for a while loop. (P. 469 Aho/Sethi/Ullman) S -> while E do S1 { S.begin = newlabel; S.after = newlabel; S.code = gen(S.begin ':') E.code gen('if' E.place '=' '0' 'goto' S.after) S1.code gen('goto' S.begin) gen(S.after ':') } One last idea before we finish... A nice enhancement to a TAC generator is re-using temp variable names. For example, if we have the following expression: E -> E1 + E2 Our usual steps would be to evaluate E1 into t1, evaluate E2 into t2, and then set t3 to their sum. Will t1 and t2 be used anywhere else in the program? How do we know when we can reuse these temp names? Here is a method from Aho/Sethi/Ullman (p. 480) for reusing temp names: 1) Keep a count c initialized to 0. 2) Whenever a temp name is used as an operand, decrement c by 1 3) Whenever a new temp is created, use this new temp and increase c by one. x = a * b + c * d - e * f (c = 0) T0 = a * b (c = 1) T1 = c * d (c = 2) (c = 0) T0 = T0 + T1 (c = 1) T1 = e * f (c = 2) (c = 0) T0 = T0 - T1 x = T0 Note that this algorithm expects that each temporary name will be assigned and used exactly once, which is true in the majority of cases. Bibliography J.P. Bennett, Introduction to Compiling Techniques. Berkshire, England: McGraw-Hill, 1990. S. Muchnick, Advanced Compiler Design and Implementation. San Francisco, CA: Morgan Kaufmann, 1997. A. Pyster, Compiler Design and Construction. New York, NY: Van Nostrand Reinhold, 1988
<<向上翻页
©2008-现在 cucdc.com 高等教育资讯网 版权所有