1

The equation I have is x = y+z;

I have seen that when solving problems where x = y+z x,y,z are variables which are often converted to three address code like this: t1 = y+z; x = t1;

My doubt is when x = y+z is itself a three address code and therefore why we use temporary variables.

For example converting

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

3 address code:

i = 1
l1: if(i>=10) goto l2
    t1 = y+z;
    x = t1;
    goto l1;
l2:

Why can't we write x = y+z instead of t1 = y+z and x = t1;

  • Using temporaries is the standard and "naive" way of doing it. Recognizing a special case like this, where a temporary isn't needed, is usually something you do for extra credits in your course, when implementing an optimizer. – Some programmer dude Sep 01 '19 at 13:45
  • @Someprogrammerdude Thanks. I understood, that both are correct. I don't have enough reputation points to close the question. If you can close it, it will be better. – Dataset Colab Sep 01 '19 at 15:34

1 Answers1

0

x = y + z is a three address code.

No. It's a 4 address code.

Usually, the "three" means we have to consider a register variable or temporal variable, additionally.

A possible equivalent could be:

y = <some value>
z= <some value>
T1 = z
T1 += y
x = T1

Some developers use a assembler like syntax:

Move y, <some value>
Move z, <some value>
Move T1, z
Add T1, y
Move x, T1

As you may know, some important things to consider in order to use intermediate code, (or three address code expressions) :

  • "Three" means the limit, not rule, there can be less, like one or two operands.

    x++; x = y; x = y + z;

  • Start thinking with constants, numbers, or variables as "location (s)", each operand or address code ("P.O. Box"), represents a place in a computer's memory.

  • Unleast one of the variables, is a destination location of the result.

    w = a * 2;

  • That same destination variable can be used as a source operand.

    x = x * y; y = y + 1;

  • Three address code expressions, are short mathematical expressions, that are written, similar, to commonly used expressions, but, with lesser elements, because there meant to be translated to assembly code.

Besides usings not just, 1, 2 or 3 locations, is that some operations, cannot be done, in any location, usually the temporal variables represent a CPU register.

umlcat
  • 4,091
  • 3
  • 19
  • 29
  • 1
    @umicat Any references? As per the book Compilers - Principles, Techniques, and Tools by Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman: Pg 99: x = y op z is a three address code where x, y and z can be either name, constant or compiler generated temporaries. And once again in Pg: 366, authors have used x=y+z as three address code to explain Quadruples. – Dataset Colab Sep 09 '19 at 09:49