Whether I did it right or wrong in 3-address code?

65 Views Asked by At

Today, I'm done with my Compiler Construction finals paper at university. The finals paper included a question that asked me to convert a for loop into 3-address code. The function it asked me to convert was:

for(i=1;i<=10;i++) x=y+z

So, I did loop unrolling and converted the given statements to the equivalent expression:

x=(y+z)^10

Then, I made 3-address code of the converted code:

1

Please let me know if it is correct.

1

There are 1 best solutions below

0
Lambdaus On

Your converted code is wrong.

In the original, x is not dependent on past versions of x making the for loop dead code and loop unrolling useless as @Peter Cordes stated. If your looking for the correct answer, the correct non-optimized answer would be:

(0) i=1
(1) if(i > 10) goto 5
(2) x=y+z
(3) i=i+1 
(4) goto 1
(5) end

While, the correct optimized answer would be:

(0) x=y+z
(1) end