Jump to content

Talk:Three-address code

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Quuxplusone (talk | contribs) at 04:33, 13 June 2007 (my recent change to Example). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Example

The current example:

      i := 0                  ; assignment
L1:   if i < 10 goto L2       ; conditional jump
      goto L3                 ; unconditional jump
L2:   t0 := i*i
      t1 := &b                ; address-of operation
      t2 := t1 + i            ; t2 holds the address of b[i]
      *t2 := t0               ; store through pointer
      i := i + 1
      goto L1
L3:

Two areas of possible contention:

  • Line 5 explicitly computes the "address of" the array b, even though the array-name itself would suffice in C, because lvalues of type int[10] decay to int* in contexts such as this. I would prefer to keep the explicit address calculation, because after all TAC is not C, and we shouldn't assume that our TAC interpreter (or code generator, or whatever) follows the same rules as C when it comes to lvalue decay. The same argument would apply if we were converting an int to a double; the conversion should probably be explicit in the TAC representation. Also consider that readers of this article should not be expected to know the finer points of C. More pedagogical is better.
  • Line 6 does not multiply i by sizeof(int) before adding it to t1. I believe this is reasonable, for two reasons. One: pedagogical clarity. Readers should again not need to know the meaning of the sizeof operator in C. Adding a multiply-by-4 would also lengthen the example by one line, without contributing any new concepts. Two: real-world practicality. All common machines are word-addressible, so if this were the IR for a real compiler, there wouldn't be any "multiply" visible in the generated code. If we're obsessing over technical correctness, we can make the same argument that I rejected in line 5: in C, addition to a pointer is scaled by the size of the pointed-to element.*

I like the new example better than the old one; thanks, Ceroklis!

* - I would not object if the expression &b were changed to &b[0] for total type-correctness, but that's a pretty formidable-looking expression for something that's supposed to look simple. :) --Quuxplusone 04:33, 13 June 2007 (UTC)[reply]