Three address code in Compiler Design

 In compiler design, three-address code (TAC), also known as intermediate code (IC), plays a crucial role in code optimization. It's a simplified representation of the source code generated during the compilation process, serving as a bridge between the high-level source code and the low-level machine code.


Key Characteristics:

  • Intermediate Representation: TAC acts as an intermediate stage between the original source code and the final machine code.
  • Three Operands: Each TAC instruction typically involves at most three operands and one operator. These operands can be variables, constants, or temporary variables generated by the compiler.
  • Assignment and Operation: The instruction format usually combines an assignment and a binary operation. The result of the operation is assigned to a specific operand.

Benefits of TAC:

  • Optimization: TAC facilitates various code optimization techniques like constant propagation, dead code elimination, and common subexpression elimination. These optimizations improve the efficiency of the generated machine code.
  • Target Independence: TAC is independent of the target machine architecture, allowing the compiler to focus on optimizing the logic without being constrained by specific machine instructions.
  • Simplicity: TAC offers a relatively simple and easy-to-understand representation, aiding in debugging and analyzing the compilation process.

Common Forms of TAC Instructions:

  • t1 := a + b: This instruction adds the values of a and b and stores the result in temporary variable t1.
  • t2 := t1 * c: This instruction multiplies the value in t1 with c and stores the result in t2.
  • if (t2 > 0) goto L1: This instruction performs a comparison and jumps to label L1 if the value in t2 is greater than 0.