• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
October 14, 2022 |4.5K Views

Three address code in Compiler Design

  Share  3 Likes
Description
Discussion

In this video, we will discussing what is three address code in compiler design.

Three address code is one of the widely used intermediate codes which is easy in terms of creating and converting to target code. It uses three addresses at most and one operator for defining any expression. 

Basically, three address codes are used in determining the sequence of actions for the compiler.

In three address codes, any expression is first broken down into such a way that no more than three addresses are required to express the code. These instructions are helpful in translating into assembly language easily.

Three operands are equated with mostly anyone binary operator and an assignment. Generally, Three Address Codes are represented as p = q op r. p, q, and r are operands. Operands can be any constants or temporary variables generated by the compiler. op represents an operator.

Some other examples of Three Address Codes are:

  • p = q + r
  • x = y x z

There are three ways to represent three address codes:

  • Quadruple
  • Triples
  • Indirect Triples

Quadruples: This representation consists of four fields: op, arg1, arg2, and result. op stands for operator and arg1, arg2 stands for two arguments or operands and the result is a variable used for storing the expression’s result.

Characteristics:

  • Easy in rearranging code for global optimization.
  • Quick access to temporary variables with the help of a symbol table.
  • Uses many temporary variables, that result in increasing time and space complexity.

Triples: It doesn’t use any additional temporary variable for representing a single operation, instead whenever such reference is needed, a pointer is used. So, it is consisting of three fields only: op, arg1, and arg2.

Disadvantage:

  • Temporary variables are implicit and so it becomes difficult to rearrange the code.
  • Difficult to optimize code because optimization requires the movement of intermediate code. When any triple is moved, any other triple pointing to it must also be updated, so it may create a disturbance.

Indirect Triples: It also makes use of a pointer to all the references to computations that are made individually and stored. It is quite similar to quadruple representation but it requires less space compared to that. In this, temporary variables are implicit, and rearranging the code is easier.

Three address code:
https://www.geeksforgeeks.org/three-address-code-compiler/