8.1.1 Three-Address code
8.1.1 Three-Address Code
a data structure that represents the source program during translation is called an intermediate representation or IR. for short Such an intermediate representation that resembles target code is called intermediate code Intermediate code is particularly useful when the goal of the compiler is to produce extremely efficient code; Intermediate code can also be useful in making a compiler more easily retarget-able Study two popular forms of intermediate code: Three Address code and p-code The most basic instruction of three-address code is designed to represent the evaluation of arithmetic expressions and has the following general form op z
• A data structure that represents the source program during translation is called an intermediate representation, or IR, for short • Such an intermediate representation that resembles target code is called intermediate code – Intermediate code is particularly useful when the goal of the compiler is to produce extremely efficient code; – Intermediate code can also be useful in making a compiler more easily retarget-able. • Study two popular forms of intermediate code: Three - Address code and P-code • The most basic instruction of three-address code is designed to represent the evaluation of arithmetic expressions and has the following general form: X=y op z
2*a+(b-3 )with syntax tree a The corresponding three-address code is T1=2*a T2=b-3 T3=t1+t2
2*a+(b-3) with syntax tree + * - 2 a b 3 The corresponding three-address code is T1 = 2 * a T2 = b – 3 T3 = t1 + t2
Figure 8. 1 Sample tiNY program i sample program in tiNY language -computes factorial readx; input an integer ifo>x then( dont compute ifx<=0) fact: =1 repeat fact: =fact*X X-X until xO write fact( output factorial ofx) ends
Figure 8.1 Sample TINY program: { sample program in TINY language -- computes factorial } read x ; { input an integer } if 0 > x then { don’t compute if x <= 0 } fact:=1; repeat fact:=fact*x; x:=x-1 until x=0; write fact { output factorial of x } ends
The Three-address codes for above tiny program readⅩ t1=x>0 if false tl goto Ll fact=1 label 2 t2=fact*x fact=t2 t3=x-1 X=t3 t4=x==0 if false t4 goto L2 write fact label li halt
• The Three-address codes for above TINY program read x t1=x>0 if_false t1 goto L1 fact=1 label L2 t2=fact*x fact=t2 t3=x-1 x=t3 t4= x= =0 if_false t4 goto L2 write fact label L1 halt