Intermediate Code Generation

After all structures are generated without any errors, a simplified intermediate code is generated. In c compilation, the intermediate code is three address code (TAC).

TAC :

  • consists of simplified instructions, and operations only take 3 operands , a destination , 2 source operands and an operation.  

  • TAC uses multiple intermediate variables to arrive at the value for complex expressions.

For example, the following operation,

D=b*b4*a*c;

Could be represented as:

t1 = b * b      // Compute the square of b and store 
                //it in temporary variable t1
t2 = 4 * a      // Compute 4 times the value of a and 
                //store it in temporary variable t2
t3 = t2 * c     // Multiply the result of t2 by c 
                //and store it in temporary variable t3
t4 = t1 - t3    // Subtract the value of t3 from t1 
                //and store it in temporary variable t4
D = t4          // Assign the value in t4 to variable D

Note that the above TAC code is not exact, it has been simplified for the purposes of demonstration.

The actual source code and corresponding TAC code is shown here:

C Source (source.c)

#include <stdio.h>
int main()
{   
    float D,b,a,c;
    D= b*b - 4*a*c; 
    return 0;
}

Intermediate TAC (source.c.005t.gimple)

main ()
{
  int D.3251;

  {
    float D;
    float b;
    float a;
    float c;

    _1 = b * b;
    _2 = a * 4.0e+0;
    _3 = c * _2;
    D = _1 - _3;
    D.3251 = 0;
    return D.3251;
  }
  D.3251 = 0;
  return D.3251;
}


The TAC was obtained by running the command “gcc -fdump-tree-all -o program.exe source.c”

See all files here: https://drive.google.com/drive/folders/1dDYCaNddQFpAF2oDknWrEFkPpj692xQ7?usp=sharing

© 2024  Vedesh Kungebeharry. All rights reserved. 

Leave a comment