Implementation of Code optimization

//c code for illustrating Code optimization


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

struct TAC
{
char res[5];
char op[2];
char arg1[5];
char arg2[5];
}quad[10];




struct ref
{
char * id;
int label;
struct node *ptr;
}ref[10];
int cnt=0;
int n;


typedef struct node
{
struct node *left;
struct node *right;
char * val;
char arg1[10];
char arg2[10];
char lable1;
char label2;
}node;
struct node *nptr;
struct node *ptr;
void printtree();
node * mknode(node *left,node *right,char *val,char *arg1,char *arg2,char l1,char l2);
int search(char*);
void main()
{
int i,sres;
printf("Enter how many TAC");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter res");
scanf("%s",quad[i].res);
printf("Enter op");
scanf("%s",quad[i].op);

printf("Enter arg1");
scanf("%s",quad[i].arg1);
printf("Enter arg2");
scanf("%s",quad[i].arg2);

}

printf("\nTAC : \n");
for(i=0;i<n;i++)
{
printf("\n%s",quad[i].res);
printf("=");
printf("%s",quad[i].arg1);
printf("%s",quad[i].op);
printf("%s",quad[i].arg2);
}
printf("\nAfter Optimization: \n");
for(i=0;i<n;i++)
{
 if(isalnum(quad[i].arg1[0]))
    {
     i=search(quad[i].arg1);

    if(i==0)
         {
    char c[10];
char ch;
    ptr=mknode(0,0,quad[i].arg1,c,c,ch,ch);
char * newstr=(char*)malloc(strlen(quad[i].arg1)+1);
strcpy(newstr,quad[i].arg1);
ref[cnt].id=newstr;
ref[cnt].ptr=ptr;
cnt++;
}
     }
 if(isalnum(quad[i].arg2[0]))
    {
    sres=search(quad[i].arg2);

    if(sres==0)
         {
    char c[10];
char ch;
    ptr=mknode(0,0,quad[i].arg2,c,c,ch,ch);
char * newstr=(char*)malloc(strlen(quad[i].arg2)+1);
strcpy(newstr,quad[i].arg2);
ref[cnt].id=newstr;
ref[cnt].ptr=ptr;
cnt++;
}
      }
 if(strcmp(quad[i].op,"*")==0)
     {sres=search(quad[i].op);

    if(sres==0)
         {
      char ch1=quad[i].res[0];
        char ch2=quad[i].arg1[0]; 
        ptr=mknode(ref[0].ptr,ref[1].ptr,quad[i].op,quad[i].arg1,quad[i].arg2,ch1,ch2);
        char * newstr=(char*)malloc(strlen(quad[i].op)+1);
strcpy(newstr,quad[i].op);
ref[cnt].id=newstr;
ref[cnt].ptr=ptr;
cnt++;
    }
   else
{ref[0].ptr->label2=quad[i].res[0];
         }

}
 
}
printtree();

}

int search(char *ID)
{
int i;
if(cnt==0)
 return 0;
else
   {
    for(i=0;i<cnt;i++)
{
  if(strcmp(ID,ref[i].id)==0)

        {return 1;
        }
}
    return 0;
   }
}
void printtree()
{
int i=0;
for(i=0;i<n;i++)
{if(strcmp(quad[i].op,"*")==0)
  {int sres;
  sres=search(quad[i].op);
  if(sres==1)
   {
     for(i=0;i<cnt;i++)
      {
      if(strcmp(ref[i].id,"*")==0)
        {struct node * newnode=(node*)malloc(sizeof(node));
         newnode=ref[i].ptr;
                printf("\n %c  =  %s %s  %s",newnode->lable1,newnode->arg1,newnode->val,newnode->arg2);
                printf("\n%c = %c",ref[0].ptr->label2,newnode->lable1);
                printf("\n");
         }
      }
   }
  }
}

}

node * mknode(node *left,node *right,char *val,char *arg1,char *arg2,char l1,char l2)
{
struct node * newnode=(node*)malloc(sizeof(node));
char * newstr=(char*)malloc(strlen(val)+1);
strcpy(newstr,val);
newnode->left=left;
newnode->right=right;
newnode->val=newstr;
strcpy(newnode->arg1,arg1);
strcpy(newnode->arg2,arg2);
newnode->lable1=l1;
newnode->label2=l2;
return(newnode);
}

/*
OUTPUT:
srushti@srushti-virtual-machine:~$ cc optimization.c
srushti@srushti-virtual-machine:~$ ./a.out
Enter how many TAC2
Enter res p
Enter op *
Enter arg1 4
Enter arg2 5
Enter res q
Enter op *
Enter arg1 4
Enter arg2 5

TAC :

p=4*5
q=4*5
After Optimization:

 p  =  4 *  5
q = p
srushti@srushti-virtual-machine:~$

*/

Popular posts from this blog

DDL DML DCL and TCL

Implementation of Calculator using lex and yacc

A Register Allocation algorithm that translates the given code into one with a fixed number of registers.