Three address code (TAC) has the advantage of being easily modified to optimize the code. Code optimization is where the newly generated intermediate code is processed to remove redundancy in order to produce efficient TAC that will eventually be used to generate object code.
Our previous example is not sufficient to illustrate code optimization; we will use the following source code:
#include <stdio.h>
int main()
{
int a=1;
int b=2;
int c=3;
a= a+b;
a= a+c; //a is now a+b+c
c= a-b; //in the end,c=a+c
return 0;
}
In TAC, here’s a comparison between the newly generated intermediate code and the optimized code :
Newly generated intermediate code (TAC)
int main() {
int a = 1;
int b = 2;
int c = 3;
t1 = a + b; // t1 = 1 + 2
a = t1; // a = t1 (a is now a+b)
t2 = a + c; // t2 = (a+b) + 3
a = t2; // a = t2 (a is now a+b+c)
t3 = a - b; // t3 = (a+b+c) - 2
c = t3; // c = t3 (c is now a+b+c)
return 0;
}
Optimized (TAC )
int main() {
int a = 6; // Optimized: a = 6 (1 + 2 + 3)
// (Constant Folding)
int c = a; // Optimized: c = a (c is now a+b+c)
// (Algebraic simplification)
return 0;
}
From here, we move onto the final stage, Code Generation, which produces object code from our optimized TAC
© 2024 Vedesh Kungebeharry. All rights reserved.