正在加载图片...
E. code gen('if’E. place 0' 'got 1. code 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 El into tl, evaluate E2 into t2, and then set t3 to their sum. Will tI and t2 be used any where 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 O 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 (c=0)T0=a*b (c=1)T1=c*d(c=2) (c=0)T0=T0+T1 (c=0)T0=T0-T1 Note that this algorithm expects that each temporary name will be assigned and used ctly 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. 1988E.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 高等教育资讯网 版权所有