Code generation from intermediate code involves either or both of two standard techniques Macro expansion and static simulation Macro expansion involves replacing each kind of intermediate code instruction with an equivalent sequence of target code instructions Static simulation involves a straight-line simulation of the effects of the intermediate code and generating target code to match these effects
• Code generation from intermediate code involves either or both of two standard techniques: – Macro expansion and Static simulation • Macro expansion involves replacing each kind of intermediate code instruction with an equivalent sequence of target code instructions • Static simulation involves a straight-line simulation of the effects of the intermediate code and generating target code to match these effects
Consider the expression(xx+3)+4, translate the P-code into three-address code Lad x oa X Ldc 3 Adi t1=x+3 Stn Ldc 4 Adi t2=t1+4 We perform a static simulation of the p-machine stack to find three-address equivalence for the given code
• Consider the expression (x=x+3) +4, translate the P-code into three-address code: Lad x Lod x Ldc 3 Adi t1=x+3 Stn x=t1 Ldc 4 Adi t2=t1+4 • We perform a static simulation of the P-machine stack to find three-address equivalence for the given code
<--top of stack 3 Address of x T1=x+3 <--top of stack Addrss of x
< -- top of stack 3 X Address of x T1=x+3 < -- top of stack T1 Addrss of x
X=tI <--top of stack TI top of stack 4 T1 T2=t1+4 <- top of stack
X=t1 < -- top of stack T1 < -- top of stack 4 T1 T2=t1+4 < -- top of stack T2
Now consider the case of translating from three address code to P-code, by simple macro expansion a three-address instruction a=b+c Can always be translated into the p-code sequence Ida O abc O adi
• Now consider the case of translating from threeaddress code to P-code, by simple macro expansion. • A three-address instruction: a = b + c • Can always be translated into the P-code sequence lda a lod b lod c adi sto