Topic 2 · Class activity 6 · Week 4 · Friday

Three-Address Code by Hand

Translate a tree yourself, redraw it as quadruples, then make the compiler agree with you.

Format pairsTime 50 min

The point

Translate a syntax tree to three-address code by hand, then make the compiler agree with you. Where you differ, one of you has made a design decision without noticing.

Do this

  1. By hand. Write the TAC for
    int a; int b; int c;
    a = 1 + 2;
    b = a + a;
    c = a + b + 3;
    print(c);
    Use t0, t1, … for temporaries and number every instruction.
  2. Now as quadruples. Redraw your listing as a four-column table — op, arg1, arg2, result — exactly as in §9 of the lecture notes.
  3. Count your temporaries. How many did you use in total? How many were live at once? Those are different numbers, and the second is the one a register allocator cares about.
  4. Compare with a partner. Different instruction counts mean different choices — find out what they were.
  5. Now the compiler. Implement the TAC generation and read out.tac. Reconcile every difference from your listing.
  6. Hand-execute your TAC. With a table of variable values, step through the listing and confirm it prints what the source should. This is how you will debug phase 6 next week.

Deliverable

Your handwritten TAC, the same listing as quadruples, the compiler's out.tac, and a numbered list of the differences with an explanation for each. This is Lab Question 8.