예제 #1
0
void Ginstall(char* name, int type,int size,struct ArgStruct *Arglist){
    /*checking whether already a variable of same name exists*/
    struct Gsymbol *check;
    check = Glookup(name);
    if(check != NULL)	//error on redefining the variable		
	  {
        yyerror("variable redefined ");
        printf(" %s",name);
        exit(0);
    }
    struct Gsymbol *temp;
    temp = (struct Gsymbol *)malloc(sizeof(struct Gsymbol));
    temp->name = (char *)malloc(sizeof(name));
    strcpy(temp->name,name);
    temp->type = type;
    temp->size = size;
    temp->Arglist = Arglist;
    temp->next = NULL;
    if(temp->type == STYPE_FUNCTION_INT || temp->type == STYPE_FUNCTION_BOOLEAN || temp->type == STYPE_FUNCTION_PAIR){
        temp->binding = functionbind;   //Make the appropriate value here
        functionbind++;
    }
    else if(temp->type == STYPE_INT || temp->type == STYPE_BOOLEAN || temp->type == STYPE_PAIR || temp->type == STYPE_ARR_INT || temp->type == STYPE_ARR_BOOLEAN){
        temp->binding = globalbind;
        globalbind = globalbind + temp->size;
    }
    /*adding the node to symbol table entries*/
    if(Ghead == NULL){
   	 Ghead = temp;   	 
   	 return;    
    }
    temp->next = Ghead;
    Ghead = temp;
    return;
}
예제 #2
0
void Ginstall(char* name,int type,int size, struct ArgStruct* arglist){
		
		if(Glookup(name) != NULL){
				error("Duplicate var names");
			}
		
		struct Gsymbol *t = (struct Gsymbol*)malloc(sizeof(struct Gsymbol));	
		t-> NAME = name;
		t-> TYPE = type;
		t->SIZE = size;
		t->ARGLIST = arglist;
		t->NEXT = NULL;
		t->BINDING = symbolAddress;
		
		symbolAddress += size;
		
		if(symbolTable == NULL){
				symbolTable = t;	
			}
		else{
			 struct Gsymbol *tmp = symbolTable;
			 while(tmp->NEXT != NULL){
					tmp = tmp-> NEXT;
				 }
			tmp->NEXT = t;
		}
	}
예제 #3
0
struct GSymbol* GInstall(char*name, TypeTable *type, int size, ArgStruct *arglist){
    if(Glookup(name) != NULL)	//error on redefining the variable
	  {
        yyerror("GInstall : Global variable redefined ");
        printf(" %s",name);
        exit(1);
    }
    GSymbol *temp = (GSymbol *)malloc(sizeof(GSymbol));
    temp->name =  (char *)malloc(sizeof(name));
    strcpy(temp->name,name);
    temp->type = type;
    temp->size = size;
    if(arglist != NULL){
        size = 0;
        Argtemp = arglist;
        while(Argtemp != NULL){
            size = size + 1;
            Argtemp = Argtemp->next;
        }
        temp->size = size;
    }
    temp->arglist = arglist;
    temp->next = NULL;
    return temp;
}
예제 #4
0
void setGentry(struct Tnode *t){
	struct Gsymbol *g = Glookup(getName(t));
	
	if(g == NULL)
		error("Lookup Error");
		
	t->Gentry = g;
}
예제 #5
0
/*	Check a function's definition for semantic errors
	*/
int funcSemanticCheck(Tnode *root)
{
	ArgStruct *arg;
	struct Lsymbol *Lhead;
	
	Lhead = NULL;
	
	if(root == NULL)
		return;
	
	switch(root->NODETYPE)
	{
		case CONTINUE		:	funcSemanticCheck(root->Ptr1);
								funcSemanticCheck(root->Ptr2);
								return;
		
		case FUNCBLOCK		:	argLocalInstall(root->ArgList,&Lhead);
								
								gtemp = Glookup(root->NAME);
								functype = root->Ptr1->TYPE;
								
								funcTypeCheck(root);
								
								if(gtemp)
								{
									funcArgCheck(root->ArgList);
									
									arg = gtemp->ARGLIST;
									
									while(arg)
									{
										if(arg->FLAG == 0)
										{
											printf("\nSIL:%d: Error: parameter '%s' has been defined in global declaration of function '%s', but is missing in definition",root->ArgList->LINE,arg->NAME,gtemp->NAME);
											error++;
										}
										arg = arg->NEXT;
									}
								}
								
								localInstall(root->Ptr2,&Lhead);
								bodySemanticCheck(root->Ptr3,&Lhead);
								
								return;
		
		case MAINBLOCK		:	functype = root->TYPE;
								localInstall(root->Ptr1,&Lhead);
								bodySemanticCheck(root->Ptr2,&Lhead);
								
								return;
	}
}
예제 #6
0
void validateFunction(char* name, int type, struct ArgStruct *ArgList,struct Tnode *Body){
    Gtemp = Glookup(name);

    /*check function is declared or not*/
    if(Gtemp == NULL){
        printf("Error : undefined function %s",name);
        exit(1);
    }
    
    /*return value type check*/
    if(!(Gtemp->type == STYPE_FUNCTION_INT && type == STYPE_INT) && !(Gtemp->type == STYPE_FUNCTION_BOOLEAN && type == STYPE_BOOLEAN) && !(Gtemp->type == STYPE_FUNCTION_PAIR && type == STYPE_PAIR)){
        printf("Error : Conflicting types of the function %s : type mismatch",name);
        exit(1);
    }
    
    if(!(Gtemp->type == STYPE_FUNCTION_INT && Body->Ptr2->TYPE == TYPE_INT) && !(Gtemp->type == STYPE_FUNCTION_BOOLEAN && Body->Ptr2->TYPE == TYPE_BOOLEAN) && !(Gtemp->type == STYPE_FUNCTION_PAIR && Body->Ptr2->TYPE == TYPE_PAIR)){
        printf("Error : Conflicting type of the function and returning type of the function %s : type mismatch",name);
        exit(1);
    }

    /*checking the type and name of arguments match with declaration , also sequence maintained as in decl*/
    Argtemp = Gtemp->Arglist;
    while(Argtemp != NULL && ArgList != NULL){ 
        if(strcmp(Argtemp->name,ArgList->name) != 0){
            printf("Error : Conflicts in function %s : mismatch in arguement name %s", name, Argtemp->name);
            exit(1);
        }
        if(Argtemp->type != ArgList->type){
            printf("Error : Conflicts in function %s : mismatch in arguement type %s",name,Argtemp->name);
            exit(1);
        }
        if(Argtemp->passType != ArgList->passType){
            printf("Error : Conflicts in function %s : mismatch in arguement passType %s", name,Argtemp->name);
            exit(1);
        }
        Argtemp = Argtemp->next;
        ArgList = ArgList->next; 
    }
    
    /*No of arguments in both declaration and definition must be same*/
    if(Argtemp != NULL || ArgList != NULL){
        printf("Error : Conflicts in function %s : mismatch in number of arguements", name);
        exit(1);
    }
}
예제 #7
0
void validate_function(char *fname,TypeTable *rtype, ArgStruct *arglist,struct ASTNode *body){
    //check function is declared
    Gtemp = Glookup(fname);
    if(Gtemp == NULL){
        yyerror("validate_function : function has not been declared");
        printf(" %s",fname);
        exit(1);
    }

    //compare the return type as declared and as from the body of the function
    if(!(strcmp(Gtemp->type->name,rtype->name) == 0 && strcmp(body->type->name,rtype->name) == 0)){
        yyerror("validate_function : return type doesnot match with definition or with return type of body");
        printf("%s\n",fname);
        exit(1);
    }

    //compare the type and names of the arguments as in the sequence of declaration
    ArgStruct *temp1,*temp2;
    temp1 = Gtemp->arglist;
    temp2 = arglist;
    while(temp1 != NULL && temp2 != NULL){
        if(strcmp(temp1->name,temp2->name) != 0){
            yyerror("validate_function : argument names donot match");
            printf("%s\n",fname);
            exit(1);
        }

        if(strcmp(temp1->type->name,temp2->type->name) != 0){
            yyerror("validate_function : type of arguments donot match");
            printf("%s\n",fname);
            exit(1);
        }

        temp1 = temp1->next;
        temp2 = temp2->next;
    }
    if(temp1 != NULL || temp2 != NULL ){
        yyerror("validate_function: no of arguments donot match");
        printf("%s\n",fname);
        exit(1);
    }
}
예제 #8
0
/*	Install all global declarations (identifiers,functions,arrays)
	present in source program into a global symbol table
	*/
void globalInstall(Tnode *root)
{
	struct Gsymbol *gnode;
	ArgStruct *arg;
	
	if(root == NULL)
		return;
	
	switch(root->NODETYPE)
	{
		case CONTINUE		:	globalInstall(root->Ptr1);
								globalInstall(root->Ptr2);
								break;
		
		case DECLSTATEMENT	:	decnode = root->Ptr1;
								globalInstall(root->Ptr2);
								break;
		
		case IDFRDECL		:
		
		case ARRAYDECL		:
		
		case FUNCDECL		:	gnode = Glookup(root->NAME);
								
								if(gnode != NULL)
								{
									if(root->TYPE != gnode->TYPE)
										printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME);
									else
										printf("\nSIL:%d: Error: redeclaration of '%s'",root->LINE,root->NAME);
									
									error++;
								}
								
								globalInstall(root->ArgList);
								
								if(gnode == NULL)
									Ginstall(root->NAME,decnode->TYPE,root->VALUE,Arghead);
								
								break;
		
		case ARGSTATEMENT	:	argnode = root->Ptr1;
								entry = 1;
								globalInstall(root->Ptr2);
								break;
		
		case IDALIASARG		:
		
		case IDARG			:	arg = argLookup(root->NAME,Arghead);
								
								if(arg != NULL)
								{
									if(root->TYPE != arg->TYPE)
										printf("\nSIL:%d: Error: conflicting types for '%s'",root->LINE,root->NAME);
									else
										printf("\nSIL:%d: Error: redefinition of parameter '%s'",root->LINE,root->NAME);
									
									error++;
								}
								
								else argInstall(root->NAME,argnode->TYPE);
								
								break;
	}
}
예제 #9
0
/*	Check function body for the following-
	1) correct return type
	2) if a function is called,
		a) function that is called must have a global declaration
		b) call parameters must have same type as the arguments in function declaration
		c) number of call parameters must be equal to number of arguments in function declaration
	3) if, while condition must be a logical expression
	4) type checking during assignment operation
	5) read/write operations must not be allowed on boolean variables
	6) +,-,*,/,%,>,>=,<,<=,==,!= operations must not be allowed on logical expressions
	7) AND, OR and NOT operations must not be allowed on arithmetic expressions
	8) all identifiers must have either a local or global declaration
	*/
int bodySemanticCheck(Tnode *root,struct Lsymbol **Lhead)
{
	int t1,t2;
	char typ1[10],typ2[10];
	struct Lsymbol *lnode;
	struct Gsymbol *gnode;
	static struct Gsymbol *gfunc;
	static ArgStruct *Ahead;
	static int argcnt,paramcnt;
	
	if(root == NULL)
		return 0;
	
	switch(root->NODETYPE)
	{
		case CONTINUE		:	bodySemanticCheck(root->Ptr1,Lhead);
								bodySemanticCheck(root->Ptr2,Lhead);
								return;
		
		case RET			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if(functype == INTGR)
									strcpy(typ1,"integer");
								else if(functype == BOOL)
									strcpy(typ1,"boolean");
								
								if(t1 == INTGR)
									strcpy(typ2,"integer");
								else if(t1 == BOOL)
									strcpy(typ2,"boolean");
								
								if(functype != t1)
								{
									printf("\nSIL:%d: Error: incompatible types when returning type '%s' but '%s' was expected",root->LINE,typ2,typ1);
									error++;
								}
								
								return;
		
		case FUNCCALL		:	gfunc = Glookup(root->NAME);
								
								if((gfunc == NULL) || (gfunc->SIZE == -1))
								{
									printf("\nSIL:%d: Error: undefined reference to '%s'",root->LINE,root->NAME);
									error++;
								}
								
								Ahead = gfunc->ARGLIST;
								argcnt = 0;
								paramcnt = 0;
								
								t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if(Ahead != NULL)
								{
									printf("\nSIL:%d: Error: too few arguments to function '%s'",root->LINE,gfunc->NAME);
									error++;
								}
								
								else if(argcnt < paramcnt)
								{
									printf("\nSIL:%d: Error: too many arguments to function '%s'",root->LINE,gfunc->NAME);
									error++;
								}
								
								return gtemp->TYPE;
		
		case FUNCPARAM		:	paramcnt++;
		
								t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if(Ahead == NULL)
									return;
								
								if(t1 != Ahead->TYPE)
								{
									if(Ahead->TYPE == INTGR)
										strcpy(typ1,"integer");
									else if(Ahead->TYPE == BOOL)
										strcpy(typ1,"boolean");
									
									printf("\nSIL:%d: Error: incompatible type for argument %d of '%s'; expected argument of type '%s'",root->LINE,argcnt+1,gfunc->NAME,typ1);
									
									error++;
								}
								
								argcnt++;
								Ahead = Ahead->NEXT;
								
								return 0;
		
		case ITERATIVE		:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if((t1 != BOOL) && (t1 != 0))
								{
									printf("\nSIL:%d: Error: while condition is not a logical expression",root->LINE);
									error++;
								}
								
								bodySemanticCheck(root->Ptr2,Lhead);
								return;

		case CONDITIONAL	:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if((t1 != BOOL) && (t1 != 0))
								{
									printf("\nSIL:%d: Error: if condition is not a logical expression",root->LINE);
									error++;
								}
								
								bodySemanticCheck(root->Ptr2,Lhead);
								bodySemanticCheck(root->Ptr3,Lhead);
								return;
		
		case ASSIGN			:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if((lnode == NULL) && (gnode == NULL))
								{
									printf("\nSIL:%d: Error: '%s' is an undeclared identifier",root->LINE,root->NAME);
									error++;
								}
								
								t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if(lnode != NULL)
									checkLocalAssign(root,lnode,t1);
								
								else if(gnode != NULL)
								{
									checkGlobalAssign(root,gnode,t1);
									if(((t1 == gnode->TYPE) || (t1 == 0)) && (gnode->SIZE>0))
									{
										printf("\nSIL:%d: Error: incompatible types when assigning to type '%s[%d]' from type '%s' variable",root->LINE,typ1,gnode->SIZE,typ2);
										error++;
									}
								}
								
								return 0;
		
		case ARRAYASSIGN	:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if((lnode == NULL) && (gnode == NULL))
								{
									printf("\nSIL:%d: Error: '%s' is an undeclared identifier",root->LINE,root->NAME);
									error++;
								}
								
								t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if(t1 != 0)
								{
									if((t1 != INTGR))
									{
										printf("\nSIL:%d: Error: array subscript is not an integer",root->LINE);
										error++;
									}
								
									if((lnode != NULL) || (gnode->SIZE == 0))
									{
										printf("\nSIL:%d: Error: subscripted value is not an array",root->LINE);
										error ++;
									}
								}
								
								t1 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if(lnode != NULL)
									checkLocalAssign(root,lnode,t1);
								
								else if(gnode != NULL)
									checkGlobalAssign(root,gnode,t1);
								
								return 0;
		
		case RD				:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if((lnode == NULL) && (gnode == NULL))
								{
									printf("\nSIL:%d: Error: '%s' is an undeclared identifier",root->LINE,root->NAME);
									error++;
								}
								
								else
								{
									if(lnode != NULL)
										t1 = (lnode->TYPE == BOOL);
									else if(gnode !=NULL)
										t1 = (gnode->TYPE == BOOL);
									
									if(t1)
									{
										printf("\nSIL:%d: Error: read operation is not allowed on boolean variables",root->LINE);
										error++;
									}
								}
								
								return 0;
		
		case ARRAYRD		:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if((lnode == NULL) && (gnode == NULL))
								{
									printf("\nSIL:%d: Error: '%s' is an undeclared identifier",root->LINE,root->NAME);
									error++;
								}
								
								else if(((lnode != NULL) && (lnode->TYPE == BOOL)) || ((gnode != NULL) && (gnode->TYPE == BOOL)))
								{
									printf("\nSIL:%d: Error: read operation is not allowed on boolean variables",root->LINE);
									error++;
								}
								
								else
								{
									t1 = bodySemanticCheck(root->Ptr1,Lhead);
									
									if(t1 != 0)
									{
										if((t1 != INTGR))
										{
											printf("\nSIL:%d: Error: array subscript is not an integer",root->LINE);
											error++;
										}
									
										if((lnode != NULL) || (gnode->SIZE == 0))
										{
											printf("\nSIL:%d: Error: subscripted value is not an array",root->LINE);
											error ++;
										}
									}
								}
								
								return 0;
		
		case WRIT			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if((t1 != INTGR) && (t1 != 0))
								{
									printf("\nSIL:%d: Error: write operation not allowed on boolean variables",root->LINE);
									error++;
								}
								
								return 0;
		
		case DIV			:	if((root->Ptr2->VALUE == 0) && (root->Ptr2->NODETYPE == NUM))
									printf("\nSIL:%d: Warning: division by zero",root->LINE);
								
								t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
							
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: / does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case ADD			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: + does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case SUB			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: - does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case MUL			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: * does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case MOD			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: %% does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case GT				:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: > does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case LT				:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: < does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case GTE			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: >= does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case LTE			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: <= does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case EQ				:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: == does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case NE				:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == INTGR) && (t2 == INTGR))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: != does not support logical expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case And			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == BOOL) && (t2 == BOOL))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: AND does not support arithmetic expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case Or				:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								t2 = bodySemanticCheck(root->Ptr2,Lhead);
								
								if((t1 == BOOL) && (t2 == BOOL))
									return root->TYPE;
								
								else if((t1 != 0) && (t2 != 0))
								{
									printf("\nSIL:%d: Error: OR does not support arithmetic expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case Not			:	t1 = bodySemanticCheck(root->Ptr1,Lhead);
								
								if(t1 == BOOL)
									return root->TYPE;
								
								else if(t1 != 0)
								{
									printf("\nSIL:%d: Error: NOT does not support arithmetic expressions",root->LINE);
									error++;
								}
								
								return 0;
		
		case True			:
		
		case False			:	return root->TYPE;
		
		case IDADDR			:
		
		case IDFR			:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if((lnode == NULL) && (gnode == NULL))
								{
									printf("\nSIL:%d: Error: '%s' is an undeclared identifier",root->LINE,root->NAME);
									error++;
									return 0;
								}
								
								else if((lnode == NULL) && (gnode != NULL) && (gnode->SIZE > 0))
								{
									printf("\nSIL:%d: Error: '%s' has been globally declared as an array",root->LINE,root->NAME);
									error++;
								}
								
								if(lnode != NULL)
									return lnode->TYPE;
								else
									return gnode->TYPE;
		
		case ARRAYIDFR		:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if((lnode == NULL) && (gnode == NULL))
								{
									printf("\nSIL:%d: Error: '%s' is an undeclared identifier",root->LINE,root->NAME);
									error++;
								}
								
								else
								{
									t1 = bodySemanticCheck(root->Ptr1,Lhead);
									
									if(t1 != 0)
									{
										if((t1 != INTGR))
										{
											printf("\nSIL:%d: Error: array subscript is not an integer",root->LINE);
											error++;
										}
									
										if((lnode != NULL) || (gnode->SIZE == 0))
										{
											printf("\nSIL:%d: Error: subscripted value is not an array",root->LINE);
											error ++;
										}
										
										if(lnode != NULL)
											return lnode->TYPE;
										else
											return gnode->TYPE;
									}
								}
								
								return 0;
		
		case NUM			:	return INTGR;
		
		default				:	;
	}
}
예제 #10
0
int codeGenerate(Tnode *root)
{
	int loc,r,r1,r2;
	int lbl1,lbl2;
	struct Gsymbol *TEMP;
	
	if(root==NULL)
		return;

	switch(root->NODETYPE)
	{
		case CONTINUE		:	codeGenerate(root->Ptr1);
								codeGenerate(root->Ptr2);
								
								return;
		
		case ITERATIVE		:	fprintf(fp,"\n*** ITERATION ***\n");
								lbl1 = getLabel();
								lbl2 = getLabel();
								fprintf(fp,"Label%d:\n",lbl1);
								r = codeGenerate(root ->Ptr1);
								fprintf(fp,"JZ R%d Label%d\n",r,lbl2);
								freeReg();
								codeGenerate(root ->Ptr2);
								fprintf(fp,"JMP Label%d\n",lbl1);
								fprintf(fp,"Label%d:\n",lbl2);
								
								return;
		
		case CONDITIONAL	:	fprintf(fp,"\n*** CONDITIONAL ***\n");
								r = codeGenerate(root->Ptr1);
								lbl1 = getLabel();
								lbl2 = getLabel();
								
								fprintf(fp,"JZ R%d Label%d\n",r,lbl1);
								freeReg();
								codeGenerate(root->Ptr2);
								fprintf(fp,"JMP Label%d\n",lbl2);
								fprintf(fp,"Label%d:\n",lbl1);
								codeGenerate(root->Ptr3);
								fprintf(fp,"Label%d:\n",lbl2);
								
								return;
		
		case RD				:	fprintf(fp,"\n*** READ ***\n");
								loc = Glookup(root->NAME)->LOCATION;
								r = getReg();
								fprintf(fp,"IN R%d\n",r);
								fprintf(fp,"MOV [%d] R%d\n",loc,r);
								freeReg();
								
								return -1;
		
		case ARRAYRD		:	fprintf(fp,"\n*** ARRAY READ ***\n");
								r = getReg();
								fprintf(fp,"IN R%d\n",r);
								loc = Glookup(root->NAME)->LOCATION;
								r1 = codeGenerate(root->Ptr1);
								r2 = getReg();
								fprintf(fp,"MOV R%d %d\n",r2,loc);
								fprintf(fp,"ADD R%d R%d\n",r1,r2);
								freeReg();
								fprintf(fp,"MOV [R%d] R%d\n",r1,r);
								freeReg();
								freeReg();
								
								return -1;
		
		case WRIT			:	fprintf(fp,"\n*** WRITE ***\n");
								r = codeGenerate(root->Ptr1);
								fprintf(fp,"OUT R%d\n",r);
								freeReg();
								
								return -1;
		
		case ASSIGN			:	fprintf(fp,"\n*** ASSIGNMENT ***\n");
								loc = Glookup(root->NAME)->LOCATION;
								r = codeGenerate(root->Ptr1);
								fprintf(fp,"MOV [%d] R%d\n",loc,r);
								freeReg();
								
								return;
		
		case GT				:	r = codeGenerate(root->Ptr1);
								codeGenerate(root->Ptr2);
								fprintf(fp,"GT R%d R%d\n",r,r+1);
								freeReg();
								
								return r;
		
		case LT				:	r = codeGenerate(root->Ptr1);
								codeGenerate(root->Ptr2);
								fprintf(fp,"LT R%d R%d\n",r,r+1);
								freeReg();
								
								return r;
		
		case EQ				:	r = codeGenerate(root->Ptr1);
								codeGenerate(root->Ptr2);
								fprintf(fp,"EQ R%d R%d\n",r,r+1);
								freeReg();
								
								return r;
		
		case NE				:	r = codeGenerate(root->Ptr1);
								codeGenerate(root->Ptr2);
								fprintf(fp,"NE R%d R%d\n",r,r+1);
								freeReg();
								
								return r;
		
		case NUM			:	r=getReg();
								fprintf(fp,"MOV R%d %d \n",r,root->VALUE);
								
								return r;
		
		case ADD			:	r=codeGenerate(root->Ptr1);
								r1=codeGenerate(root->Ptr2);
								fprintf(fp,"ADD R%d R%d\n",r,r1);
								freeReg();
								
								return r;
		
		case SUB			:	r=codeGenerate(root->Ptr1);
								r1=codeGenerate(root->Ptr2);
								fprintf(fp,"SUB R%d R%d\n",r,r1);
								freeReg();
								
								return r;
		
		case MUL			:	r=codeGenerate(root->Ptr1);
								r1=codeGenerate(root->Ptr2);
								fprintf(fp,"MUL R%d R%d\n",r,r1);
								freeReg();
								
								return r;
		
		case DIV			:	r=codeGenerate(root->Ptr1);
								r1=codeGenerate(root->Ptr2);
								fprintf(fp,"DIV R%d R%d\n",r,r1);
								freeReg();
								
								return r;
		
		case IDFR			:	loc = Glookup(root->NAME)->LOCATION;
								r=getReg();
								fprintf(fp,"MOV R%d [%d]\n",r,loc);
								
								return r;
		
		case ARRAYIDFR		:	r = codeGenerate(root->Ptr1);
								loc = Glookup(root->NAME)->LOCATION;
								r1 = getReg();
								fprintf(fp,"MOV R%d %d\n",r1,loc);
								fprintf(fp,"ADD R%d R%d\n",r,r1);
								fprintf(fp,"MOV R%d [R%d]\n",r1,r);
								freeReg();
								
								return r1;
	}
}
예제 #11
0
int evalBody(Tnode *root,struct Lsymbol **Lhead)
{
	int t;
	struct Gsymbol *gnode;
	struct Lsymbol *lnode;
	
	if(root == NULL)
		return;
	
	switch(root->NODETYPE)
	{
		case CONTINUE		:	evalBody(root->Ptr1,Lhead);
								return evalBody(root->Ptr2,Lhead);
		
		case FUNCCALL		:	gnode = Glookup(root->NAME);
								Arghead = gnode->ARGLIST;
								
								struct Lsymbol *Ltable;
								Ltable = NULL;
								
								if(Arghead != NULL)
									funcParamInstall(root->Ptr1,Lhead,&Ltable);
								
								tempnode = searchFunc(root->NAME,funcroot);
								return interpreter(tempnode,&Ltable);
		
		case IDADDR			:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if(lnode != NULL)
									binding = lnode->BINDING;
								else binding = gnode->BINDING;
								
								return *binding;
		
		case RET			:	return evalBody(root->Ptr1,Lhead);
		
		case ITERATIVE		:	while(evalBody(root->Ptr1,Lhead))
									evalBody(root->Ptr2,Lhead);
								return;
		
		case CONDITIONAL	:	if(evalBody(root->Ptr1,Lhead))
									evalBody(root->Ptr2,Lhead);
								else
									evalBody(root->Ptr3,Lhead);
								return;
		
		case ASSIGN			:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if(lnode != NULL)
									*lnode->BINDING = evalBody(root->Ptr1,Lhead);
								else *gnode->BINDING = evalBody(root->Ptr1,Lhead);
								
								return;
		
		case ARRAYASSIGN	:	gnode = Glookup(root->NAME);
								gnode->BINDING[evalBody(root->Ptr1,Lhead)] = evalBody(root->Ptr2,Lhead);
								return;
		
		case RD				:	scanf("%d",&var);
								lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if(lnode != NULL)
									*lnode->BINDING = var;
								else *gnode->BINDING = var;
								return;
		
		case ARRAYRD		:	scanf("%d",&var);
								gnode = Glookup(root->NAME);
								gnode->BINDING[evalBody(root->Ptr1,Lhead)] = var;
								return;
		
		case WRIT			:	printf("%d\n",evalBody(root->Ptr1,Lhead));
								return;
		
		case ADD			:	return evalBody(root->Ptr1,Lhead) + evalBody(root->Ptr2,Lhead);
		
		case SUB			:	return evalBody(root->Ptr1,Lhead) - evalBody(root->Ptr2,Lhead);
		
		case MUL			:	return evalBody(root->Ptr1,Lhead) * evalBody(root->Ptr2,Lhead);
		
		case DIV			:	return evalBody(root->Ptr1,Lhead) / evalBody(root->Ptr2,Lhead);
		
		case MOD			:	return evalBody(root->Ptr1,Lhead) % evalBody(root->Ptr2,Lhead);
		
		case GT				:	return evalBody(root->Ptr1,Lhead) > evalBody(root->Ptr2,Lhead);
		
		case LT				:	return evalBody(root->Ptr1,Lhead) < evalBody(root->Ptr2,Lhead);
		
		case GTE			:	return evalBody(root->Ptr1,Lhead) >= evalBody(root->Ptr2,Lhead);
		
		case LTE			:	return evalBody(root->Ptr1,Lhead) <= evalBody(root->Ptr2,Lhead);
		
		case EQ				:	return evalBody(root->Ptr1,Lhead) == evalBody(root->Ptr2,Lhead);
		
		case NE				:	return evalBody(root->Ptr1,Lhead) != evalBody(root->Ptr2,Lhead);
		
		case And			:	return evalBody(root->Ptr1,Lhead) && evalBody(root->Ptr2,Lhead);
		
		case Or				:	return evalBody(root->Ptr1,Lhead) || evalBody(root->Ptr2,Lhead);
		
		case Not			:	return !evalBody(root->Ptr1,Lhead);
		
		case True			:	return 1;
		
		case False			:	return 0;
		
		case IDFR			:	lnode = Llookup(root->NAME,Lhead);
								gnode = Glookup(root->NAME);
								
								if(lnode != NULL)
									return *lnode->BINDING;
								else return *gnode->BINDING;
		
		case ARRAYIDFR		:	gnode = Glookup(root->NAME);
								return gnode->BINDING[evalBody(root->Ptr1,Lhead)];
		
		case NUM			:	return root->VALUE;
		
		default				:	printf("How did flag get this value!");
	}
}
예제 #12
0
int codegen(struct Tnode* root)
{
	// printf("Evaluate %d\n",root -> NODE);
	int number,a,r1,r2,r3,l1,l2,i,status = 0;
	struct  Tnode *temp ,*temp1;
	struct Tnode *Ttemp;
	if(intermediate == NULL)
	{
		printf("Error while generating assemblycode\n");
		exit(1);
	}

	if(root == NULL)
	{
		return 0;
	}

	
	if(root -> NODE == NODE_EXPR )
	{
		tree_iter = 0;
		Arg_callee = root -> Ptr3;
		Argtemp = Arg_callee;
		while(Argtemp != NULL)
		{
			++tree_iter;
			// printf("%d : %s\n",tree_iter,Argtemp -> NAME );
			Argtemp = Argtemp -> NEXT;
		}
		Ttemp = root;
		while( tree_iter != 1 )
		{
			// printf("executed\n");	
			if(Ttemp -> Ptr1 -> NODE == NODE_ID)
			{
				Argtemp = Arg_callee;
				for (i = 1 ; i < tree_iter; i++)
				{
					Argtemp = Argtemp -> NEXT;
				}
				// if(Argtemp -> TYPE == TypeLookup("amp_integer") || Argtemp -> TYPE == TypeLookup("amp_boolean") )
				// 	isamp = 1; 
				if(Argtemp -> amp)
					isamp = 1;   
				// else if(Argtemp -> amp == 1)
				// 		fld = 1; 
			}
			if(Ttemp -> Ptr1 -> NODE == NODE_FIELD)
			{
				Argtemp = Arg_callee;
				for (i = 1 ; i < tree_iter; i++)
				{
					Argtemp = Argtemp -> NEXT;
				}
				 if(Argtemp -> amp == 1)
						fld = 1;
			}	
			a = codegen(Ttemp -> Ptr1);
			// printf("IN HERE 1 : %s \n",Ttemp -> Ptr1 -> NAME );
			fprintf(intermediate, "PUSH R%d\n", a);
			freereg();	
			Ttemp =  Ttemp -> Ptr2;
			// printf("executed\n");		
			tree_iter--;
		}
			if(Ttemp -> NODE == NODE_ID)
			{
				Argtemp = Arg_callee;
				for (i = 1 ; i < tree_iter; i++)
				{
					Argtemp = Argtemp -> NEXT;
				}
				if(Argtemp -> amp)
					isamp = 1;   
			}
			if(Ttemp -> NODE == NODE_FIELD)
			{
				Argtemp = Arg_callee;
				for (i = 1 ; i < tree_iter; i++)
				{
					Argtemp = Argtemp -> NEXT;
				}
				 if(Argtemp -> amp == 1)
						fld = 1;
			}
			a = codegen(Ttemp);
			// printf("IN HERE 1 : %s \n",Ttemp -> NAME );
			fprintf(intermediate, "PUSH R%d\n", a);
			freereg();	
			// printf("executed\n");		

	}
	else if(root -> NODE == DEFAULT)
	{
		codegen(root -> Ptr1);
		codegen(root -> Ptr2);
	}
	else
	{
	    temp = root;

		switch(temp -> NODE)
		{
			case NODE_T   :
								/*printf("---T---");*/
								r1 = getreg();
								fprintf(intermediate, "MOV R%d , %d\n",r1,temp -> VALUE);
								// return temp -> VALUE;
								return r1;
								break;
			case NODE_F   :
								/*printf("---F---");*/
								r1 = getreg();
								fprintf(intermediate, "MOV R%d , %d\n",r1,temp -> VALUE);
								// return temp -> VALUE;
								return r1;
								break;					
			case NODE_LE  :
								/*printf("---LE---");*/
								r1 = codegen(temp -> Ptr1);
								r2 = codegen(temp -> Ptr2);
								fprintf(intermediate, "LE R%d , R%d\n",r1,r2);
								freereg();
								return r1;		
								break;
			case NODE_GE  :
								/*printf("---GE---");*/
								r1 = codegen(temp -> Ptr1);
								r2 = codegen(temp -> Ptr2);
								fprintf(intermediate, "GE R%d , R%d\n",r1,r2);
								freereg();
								return r1;
								break;
			case NODE_LT  :
								// printf("---LT---");
								r1 = codegen(temp -> Ptr1);
								r2 = codegen(temp -> Ptr2);
								fprintf(intermediate, "LT R%d , R%d\n",r1,r2);
								freereg();
								return r1;
								break;
			case NODE_GT  :
								/*printf("---GT---");*/
								r1 = codegen(temp -> Ptr1);
								r2 = codegen(temp -> Ptr2);
								fprintf(intermediate, "GT R%d , R%d\n",r1,r2);
								freereg();
								return r1;
								break;
			case NODE_DEQ  :
								/*printf("---DEQ---");*/
								r1 = codegen(temp -> Ptr1);
								r2 = codegen(temp -> Ptr2);
								fprintf(intermediate, "EQ R%d , R%d\n",r1,r2);
								freereg();
								return r1;
								break;
			case NODE_NEQ  :
								/*printf("---NEQ---");*/

								r1 = codegen(temp -> Ptr1);
								r2 = codegen(temp -> Ptr2);
								fprintf(intermediate, "NE R%d , R%d\n",r1,r2);
								freereg();
								return r1;
								break;
			case NODE_AND  :
								r1 = codegen(temp -> Ptr1);
								r2 = getreg();
								fprintf(intermediate,"MOV R%d, 1\n",r2);
								l1 = getlabel();
								fprintf(intermediate, "JZ R%d, L%d\n",r1,l1);
								r3 = codegen(temp -> Ptr2);
								fprintf(intermediate, "MOV R%d, R%d\n",r2,r3);
								freereg();
								fprintf(intermediate, "L%d:\n",l1);
								fprintf(intermediate, "MUL R%d, R%d\n",r1,r2);
								freereg();
								return r1;
								break;
            case NODE_OR  :
								r1 = codegen(temp -> Ptr1);
								r2 = getreg();
								fprintf(intermediate,"MOV R%d, 1\n",r2);
								l1 = getlabel();
								fprintf(intermediate, "JNZ R%d, L%d\n",r1,l1);
								r3 = codegen(temp -> Ptr2);
								fprintf(intermediate, "MOV R%d, R%d\n",r2,r3);
								freereg();
								fprintf(intermediate, "L%d:\n",l1);
								fprintf(intermediate, "ADD R%d, R%d\n",r1,r2);
								freereg();
								return r1;
								break;
			case NODE_NOT  :
								r1 = codegen(temp -> Ptr2);
								l1 = getlabel();
								fprintf(intermediate, "JNZ R%d, L%d\n",r1,l1);
								fprintf(intermediate, "MOV R%d, 1\n",r1);
								l2 = getlabel();
								fprintf(intermediate, "JMP L%d\n",l2);
								fprintf(intermediate, "L%d:\n",l1);
								fprintf(intermediate, "MOV R%d, 0\n",r1);										
								fprintf(intermediate, "L%d:\n",l2);
								return r1;
			case NODE_ID   :
								// printf("---ID---\n");
								// return *(temp -> Gentry -> BINDING);
								r1 = getreg();
								Ltemp = Llookup(temp -> NAME);
								if(Ltemp != NULL)
								{
									Ltemp = Lhead;
									Loffset = 0;
									while(strcmp(temp -> NAME , Ltemp -> NAME) != 0)
									{
										Loffset = Loffset + 1;
										Ltemp = Ltemp -> NEXT;
									}
									
									r2 = getreg();
									fprintf(intermediate, "MOV R%d,BP\n",r2);
									fprintf(intermediate, "MOV R%d,%d\n",r1,Loffset+1);
									fprintf(intermediate, "ADD R%d,R%d\n",r2,r1);
									if(isamp)
									{
										fprintf(intermediate, "MOV R%d,R%d\n",r1,r2);
										isamp = 0; 
									}
									else
									{
										fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r2);
										if(fld)
										{
											fprintf(intermediate, "MOV R%d,R%d\n",r1,r2);
											fld = 0;
										}
									}
									freereg();

									// fprintf(intermediate, "MOV R%d,[%d]\n",r1,(Ltemp -> BINDING));
								}
								else
								{
									Argtemp = Arglookup(temp -> NAME);
	// BRING THIGS FROM STACK
									if(Argtemp != NULL)
									{
										Argtemp = Arghead;
										offset = 0;

										while(strcmp(Argtemp -> NAME,temp -> NAME) != 0)
										{
											offset = offset + 1;
											Argtemp = Argtemp -> NEXT;
										}
									
										r2 = getreg();
										fprintf(intermediate, "MOV R%d,BP\n",r2);
			 							r3 = getreg();
			 							fprintf(intermediate, "MOV R%d,2\n",r3);
			 							fprintf(intermediate, "SUB R%d,R%d\n",r2,r3);
			 							fprintf(intermediate, "MOV R%d,%d\n",r3,(offset+1));
										fprintf(intermediate, "SUB R%d,R%d\n",r2,r3);
										freereg();
										// if(isamp && (Argtemp -> TYPE == TypeLookup("amp_integer") ||Argtemp -> TYPE == TypeLookup("amp_boolean")))

										if(( isamp || fld )  && (Argtemp -> amp == 1))
										{
											fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r2); //BP-2-(offset+1)
											isamp = 0;
											fld = 0;
										}
				
										else
										{
											fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r2); //BP-2-(offset+1)
											// if(Argtemp -> TYPE == TypeLookup("amp_integer") ||Argtemp -> TYPE == TypeLookup("amp_boolean") )
											if(Argtemp -> amp == 1)
											{
												fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r1); 
											}
										}
										
										freereg();
									}
									else
									{
										if(isamp)
										{
											fprintf(intermediate, "MOV R%d,%d\n",r1,(temp -> Gentry -> BINDING));
											isamp = 0;
										}
										else
										{
											fprintf(intermediate, "MOV R%d,[%d]\n",r1,(temp -> Gentry -> BINDING));
											if(fld)
											{
												fprintf(intermediate, "MOV R%d,%d\n",r1,(temp -> Gentry -> BINDING));
												fld = 0;
											}
										}
									}
								}
								// fprintf(intermediate, "OUT R%d\n",r1 );
								return r1;
								break;


			case NODE_FIELD :		
								r1 = getreg();
								// printf("%d\n",r1 );
								Ltemp = Llookup(temp -> NAME);
								if(Ltemp != NULL)
								{
									Ltemp = Lhead;
									Loffset = 0;
									while(strcmp(temp -> NAME , Ltemp -> NAME) != 0)
									{
										Loffset = Loffset + 1;
										Ltemp = Ltemp -> NEXT;
									}
									
									r2 = getreg();
									fprintf(intermediate, "MOV R%d,BP\n",r2);
									fprintf(intermediate, "MOV R%d,%d\n",r1,Loffset+1);
									fprintf(intermediate, "ADD R%d,R%d\n",r2,r1);
									fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r2);
									freereg();

									fldtemp2 = fldtemp1 = Ltemp -> TYPE -> fields;
									flditer = 0;
									temp1 = temp;
									while(temp1 -> Ptr2 != NULL)
									{
										flditer = 1;
										fldtemp2 = fldtemp1;
										while(fldtemp2 != NULL)
										{
											if(strcmp(fldtemp2 -> NAME,temp1 -> Ptr2 -> NAME) == 0)
											{
												r2 = getreg();
												fprintf(intermediate, "MOV R%d, %d\n",r2,flditer);
												fprintf(intermediate, "ADD R%d, R%d\n",r2,r1);
												fprintf(intermediate, "MOV R%d, [R%d]\n",r1,r2);
												freereg();
												break;
											}
											flditer++;
											fldtemp2 = fldtemp2 -> NEXT;
										}
										temp1 = temp1 -> Ptr2;
									}

									if(fld == 1)
									{
										fprintf(intermediate, "MOV R%d, R%d\n",r1,r2);

										fld = 0;
									}
								}
								else
								{
									Argtemp = Arglookup(temp -> NAME);
	
									if(Argtemp != NULL)
									{
										Argtemp = Arghead;
										offset = 0;

										while(strcmp(Argtemp -> NAME,temp -> NAME) != 0)
										{
											offset = offset + 1;
											Argtemp = Argtemp -> NEXT;
										}
									
										r2 = getreg();
										fprintf(intermediate, "MOV R%d,BP\n",r2);
			 							r3 = getreg();
			 							fprintf(intermediate, "MOV R%d,2\n",r3);
			 							fprintf(intermediate, "SUB R%d,R%d\n",r2,r3);
			 							fprintf(intermediate, "MOV R%d,%d\n",r3,(offset+1));
										fprintf(intermediate, "SUB R%d,R%d\n",r2,r3);
										freereg();

										fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r2); //BP-2-(offset+1)
										freereg();
										if(Argtemp -> amp)
										{
											fprintf(intermediate, "MOV R%d,[R%d]\n",r1,r1); //BP-2-(offset+1)
											
										}
										fldtemp2 = fldtemp1 = Argtemp -> TYPE -> fields;
										flditer = 0;
										temp1 = temp;

										while(temp1 -> Ptr2 != NULL)
										{
											flditer = 1;
											fldtemp2 = fldtemp1;
											while(fldtemp2 != NULL)
											{
												if(strcmp(fldtemp2 -> NAME,temp1 -> Ptr2 -> NAME) == 0)
												{
													r2 = getreg();
													fprintf(intermediate, "MOV R%d, %d\n",r2,flditer);
													fprintf(intermediate, "ADD R%d, R%d\n",r2,r1);
													fprintf(intermediate, "MOV R%d, [R%d]\n",r1,r2);
													freereg();

													break;
												}
												flditer++;
												fldtemp2 = fldtemp2 -> NEXT;
											}
											temp1 = temp1 -> Ptr2;
										}

										if(fld == 1)
										{
											fprintf(intermediate, "MOV R%d, R%d\n",r1,r2);

											fld = 0;
										}
									}
									else
									{
										// printf("here %d\n", r1);
										fprintf(intermediate, "MOV R%d,[%d]\n",r1,(temp -> Gentry -> BINDING));

										fldtemp2 = fldtemp1 = temp -> Gentry -> TYPE -> fields;
										flditer = 0;
										temp1 = temp;

										while(temp1 -> Ptr2 != NULL)
										{
											flditer = 1;
											fldtemp2 = fldtemp1;

											while(fldtemp2 != NULL)
											{
												if(strcmp(fldtemp2 -> NAME,temp1 -> Ptr2 -> NAME) == 0)
												{
													r2 = getreg();
													fprintf(intermediate, "MOV R%d, %d\n",r2,flditer);
													fprintf(intermediate, "ADD R%d, R%d\n",r2,r1);
													fprintf(intermediate, "MOV R%d, [R%d]\n",r1,r2);
													freereg();

													break;
												}
												flditer++;
												fldtemp2 = fldtemp2 -> NEXT;
											}
											temp1 = temp1 -> Ptr2;
										}

										if(fld == 1)
										{
											fprintf(intermediate, "MOV R%d, R%d\n",r1,r2);

											fld = 0;
										}

									}
								}

	                            return r1;
								break;
			case NODE_ARRAY :
									//printf("---ID ARR---/n");
									offset = codegen(temp -> Ptr2);
									r1 = getreg();
									fprintf(intermediate, "MOV R%d , %d\n",r1,(temp -> Ptr1 -> Gentry -> BINDING));
									// printf("1234\n");
									// r2 = getreg();
									// fprintf(intermediate, "MOV R%d , %d\n",r2,(temp -> Ptr1 -> Gentry -> SIZE));
									// fprintf(intermediate, "GT R%d, R%d\n",r2,offset);
									// l1 = getlabel();
									// fprintf(intermediate, "JNZ R%d, L%d\n",r2,l1);
									// freereg();
									// fprintf(intermediate, "HALT\n");
									// fprintf(intermediate, "L%d:\n",l1);
									// printf("REACHED\n");
									fprintf(intermediate, "ADD R%d , R%d\n",r1,offset);
									
									fprintf(intermediate, "MOV R%d , [R%d]\n",offset,r1);
									if(fld)
									{
										fprintf(intermediate, "MOV R%d , R%d\n",offset,r1);
										fld = 0;	
									}
									freereg();
									// fprintf(intermediate,"OUT R%d\n",offset);
									return offset;
									break;

			case NODE_NUM :
									// printf("NODE_NUM is %d\n",(root -> VALUE));
									// return (root -> VALUE); 
									r1 = getreg();
									fprintf(intermediate, "MOV R%d , %d\n",r1,(root -> VALUE));
									// fprintf(intermediate, "OUT %d\n",(root -> VALUE) );
									return r1;
									break;
			case NODE_PLUS :
									// printf("---PLUS---");
				 					// return (Evaluate(temp -> Ptr1) + Evaluate(temp -> Ptr2));
									r1 = codegen(temp -> Ptr1);
									r2 = codegen(temp -> Ptr2);
									fprintf(intermediate, "ADD R%d ,R%d\n",r1,r2);
									freereg();
									return r1;
									break;
 			case NODE_MINUS :
 									/*printf("---MINUS---");*/
									// return (Evaluate(temp -> Ptr1) - Evaluate(temp -> Ptr2));
 									r1 = codegen(temp -> Ptr1);
									r2 = codegen(temp -> Ptr2);
									fprintf(intermediate, "SUB R%d ,R%d\n",r1,r2);
									freereg();
									return r1;
									break;
 			case NODE_MUL :
 									/*printf("---MUL---");*/
									// return (Evaluate(temp -> Ptr1) * Evaluate(temp -> Ptr2));
									r1 = codegen(temp -> Ptr1);
									r2 = codegen(temp -> Ptr2);
									fprintf(intermediate, "MUL R%d ,R%d\n",r1,r2);
									freereg();
									return r1;
									break;
			case NODE_DIV :
 									/*printf("---DIV---");*/
									// return (Evaluate(temp -> Ptr1) / Evaluate(temp -> Ptr2));
									r1 = codegen(temp -> Ptr1);
									r2 = codegen(temp -> Ptr2);
									l1 = getlabel();
									fprintf(intermediate, "JZ R%d, L%d\n",r2,l1);
									fprintf(intermediate, "DIV R%d ,R%d\n",r1,r2);
									freereg();
									fprintf(intermediate, "JMP L%d\n",l2);
									fprintf(intermediate, "L%d:\n",l1);
									fprintf(intermediate, "HALT\n");
									fprintf(intermediate, "L%d:\n",l2);
									
									return r1;
									break;
			case NODE_MOD :
 									/*printf("---MOD---");*/
									// return (Evaluate(temp -> Ptr1) % Evaluate(temp -> Ptr2));
									r1 = codegen(temp -> Ptr1);
									r2 = codegen(temp -> Ptr2);
									fprintf(intermediate, "MOD R%d ,R%d\n",r1,r2);
									freereg();
									return r1;
									break;
			case NODE_ASGN :
									 // printf("---ASGN---");
								number = codegen(temp -> Ptr2);
								if(temp -> Ptr1 -> NODE == NODE_FIELD)
								{
									fld = 1;
									r1 = codegen(temp -> Ptr1);
									fprintf(intermediate, "MOV [R%d] ,R%d\n",r1,number);
									freereg();
								}
									// printf("%d\n",number );
									// *(temp -> Ptr1 -> Gentry -> BINDING) = number; ///// brought from ID
									//r1 = getreg();
								else
								{
									Ltemp = Llookup(temp -> Ptr1 -> NAME);
									if(Ltemp != NULL)
									{
										Ltemp = Lhead;
										Loffset = 0;

										while(strcmp(temp -> Ptr1 -> NAME , Ltemp -> NAME) != 0)
										{
											Loffset = Loffset + 1;
											Ltemp = Ltemp -> NEXT;
										}
										r1 = getreg();
										r2 = getreg();
										fprintf(intermediate, "MOV R%d,BP\n",r2);
										fprintf(intermediate, "MOV R%d,%d\n",r1,Loffset+1);
										fprintf(intermediate, "ADD R%d,R%d\n",r2,r1);
										// fprintf(intermediate, "MOV R%d,[%d]\n",r1,r2);
										
										fprintf(intermediate, "MOV [R%d],R%d\n",r2,number);
										freereg();
										freereg();
									}
									else
									{
										Argtemp = Arglookup(temp -> Ptr1 -> NAME);
		// BRING THIGS FROM STACK
										if(Argtemp != NULL)
										{
											Argtemp = Arghead;
											offset = 0;

											while(strcmp(Argtemp -> NAME,temp -> Ptr1 -> NAME) != 0)
											{
												offset = offset + 1;
												Argtemp = Argtemp -> NEXT;
											}

											r1 = getreg();
											fprintf(intermediate, "MOV R%d,BP\n",r1);
				 							r2 = getreg();
				 							fprintf(intermediate, "MOV R%d,2\n",r2);
				 							fprintf(intermediate, "SUB R%d,R%d\n",r1,r2);
				 							
				 							fprintf(intermediate, "MOV R%d,%d\n",r2,(offset+1));
											fprintf(intermediate, "SUB R%d,R%d\n",r1,r2);
											freereg();
											// if(Argtemp -> TYPE == TypeLookup("amp_integer") ||Argtemp -> TYPE == TypeLookup("amp_boolean") )
											if(Argtemp -> amp == 1)
											{
												r2 = getreg();
												fprintf(intermediate, "MOV R%d,[R%d]\n",r2,r1); //BP-2-(offset+1)
												fprintf(intermediate, "MOV [R%d],R%d\n",r2,number); //BP-2-(offset+1)
												freereg();
											}
											else
											{
												fprintf(intermediate, "MOV [R%d],R%d\n",r1,number); //BP-2-(offset+1)
											}
											freereg();
										}
										else
										{
											// printf("%d\n", (temp -> Ptr1 -> Gentry -> BINDING));
											fprintf(intermediate, "MOV [%d], R%d\n",(temp -> Ptr1 -> Gentry -> BINDING),number);
										}
									}
								}
									
									freereg();
									break;


			case NODE_ARRAY_ASGN :
										offset1 = codegen(temp -> Ptr2);
										// fprintf(intermediate, "OUT R%d\n",offset1);
										// printf("QWER\n");
										// r2 = getreg();
										// fprintf(intermediate, "MOV R%d , %d\n",r2,(temp -> Ptr1 -> Gentry -> SIZE));
										// fprintf(intermediate, "GT R%d, R%d\n",r2,offset);
										// l1 = getlabel();
										// fprintf(intermediate, "JNZ R%d, L%d\n",r2,l1);
										// freereg();
										// fprintf(intermediate, "HALT\n");
										// fprintf(intermediate, "L%d:\n",l1);
										// // freereg();
										
										r1 = getreg();
										fprintf(intermediate, "MOV R%d , %d\n",r1,(temp -> Ptr1 -> Gentry -> BINDING));
										fprintf(intermediate, "ADD R%d , R%d\n",offset1,r1);
										freereg();
										r1 = codegen(temp -> Ptr3);

										// fprintf(intermediate, "OUT R%d\n",r1);
										 // printf("r1 : %d\n",r1 );
										 // printf("offset : %d\n",offset1 );
										fprintf(intermediate, "MOV [R%d] , R%d\n",offset1,r1);
										freereg();
										freereg();
										// printf("ALSO HERE\n");
										break;
			case NODE_READ :
									/*printf("---READ---");*/
									r1 = getreg();
									fprintf(intermediate, "IN R%d\n",r1);

									if(temp -> Ptr2 -> NODE == NODE_FIELD)
									{
										fld = 1;
										r2 = codegen(temp -> Ptr2);
										fprintf(intermediate, "MOV [R%d] ,R%d\n",r2,r1);
										freereg();
									}
									
									else
									{
										Ltemp = Llookup(temp -> Ptr2 -> NAME);
										if(Ltemp != NULL)
										{
											Ltemp = Lhead;
											Loffset = 0;

											while(strcmp(temp -> Ptr2 -> NAME , Ltemp -> NAME) != 0)
											{
												Loffset = Loffset + 1;
												Ltemp = Ltemp -> NEXT;
											}
											r2 = getreg();
											r3 = getreg();
											fprintf(intermediate, "MOV R%d,BP\n",r3);
											fprintf(intermediate, "MOV R%d,%d\n",r2,Loffset+1);
											fprintf(intermediate, "ADD R%d,R%d\n",r3,r2);
											fprintf(intermediate, "MOV [R%d],R%d\n",r3,r1);
											freereg();
											freereg();
										}
										
										else
										{
											Argtemp = Arglookup(temp -> Ptr2 -> NAME);

											if(Argtemp != NULL)
											{
												Argtemp = Arghead;
												offset = 0;

												while(strcmp(Argtemp -> NAME,temp -> Ptr2 -> NAME) != 0)
												{
													offset = offset + 1;
													Argtemp = Argtemp -> NEXT;
												}

												r2 = getreg();
												fprintf(intermediate, "MOV R%d,BP\n",r2);
					 							r3 = getreg();
					 							fprintf(intermediate, "MOV R%d,2\n",r3);
					 							fprintf(intermediate, "SUB R%d,R%d\n",r2,r3);
					 							fprintf(intermediate, "MOV R%d,%d\n",r3,(offset+1));
												fprintf(intermediate, "SUB R%d,R%d\n",r2,r3);
												freereg();

												// if(Argtemp -> TYPE == TypeLookup("amp_integer") ||Argtemp -> TYPE == TypeLookup("amp_boolean") )
												if(Argtemp -> amp)
												{
													r3 = getreg();
													fprintf(intermediate, "MOV R%d,[R%d]\n",r3,r2); //BP-2-(offset+1)
													fprintf(intermediate, "MOV [R%d],R%d\n",r3,r1); //BP-2-(offset+1)
													freereg();
												}
												else
												{
													fprintf(intermediate, "MOV [R%d],R%d\n",r2,r1); //BP-2-(offset+1)
												}
												freereg();
											}
											else
											{
												fprintf(intermediate, "MOV [%d],R%d\n",(temp -> Ptr2 -> Gentry -> BINDING),r1);
											}
										}
								}
									freereg();
									break;
			case NODE_ARRAY_READ :
									offset = codegen(temp -> Ptr3);
		    						r1 = getreg();
									fprintf(intermediate, "MOV R%d , %d\n",r1,(temp -> Ptr2 -> Gentry -> BINDING));
									
									r2 = getreg();
									fprintf(intermediate, "MOV R%d , %d\n",r2,(temp -> Ptr2 -> Gentry -> SIZE));
									fprintf(intermediate, "GT R%d, R%d\n",r2,offset);
									l1 = getlabel();
									fprintf(intermediate, "JNZ R%d, L%d\n",r2,l1);
									// freereg();
									fprintf(intermediate, "HALT\n");
									fprintf(intermediate, "L%d:\n",l1);
									
									freereg();
									fprintf(intermediate, "ADD R%d , R%d\n",offset,r1);
									freereg();
		    						r1 = getreg();
									fprintf(intermediate, "IN R%d\n",r1);
									fprintf(intermediate, "MOV [R%d], R%d\n",offset,r1);
									freereg();
									freereg();
									break;
			case NODE_WRITE :
									// printf("---WRITE---");
									number = codegen(temp -> Ptr2);
									fprintf(intermediate, "OUT R%d\n",number);
									freereg();
									break;
			case NODE_IF  :
									/*printf("---IF---");*/
									l1 = getlabel();
									number = codegen(temp -> Ptr1);
									fprintf(intermediate, "JZ R%d , L%d\n",number,l1);
									number = codegen(temp -> Ptr2);
									fprintf(intermediate, "L%d:\n",l1);
									freereg();
									break;
			case NODE_IF_ELSE:
									/*printf("---IF_ELSE---");*/
									number = codegen(temp -> Ptr1);
									l1 = getlabel();
									l2 = getlabel();
									fprintf(intermediate, "JZ R%d , L%d\n",number,l1);
									freereg();
									number = codegen(temp -> Ptr2);
									fprintf(intermediate, "JMP L%d\n",l2);
									fprintf(intermediate, "L%d:\n",l1);
									freereg();
									number = codegen(temp -> Ptr3);
									fprintf(intermediate, "L%d:\n",l2);
									break;
		 	case NODE_WHILE :
		 							// printf("---WHILE----");
		 							l1 = getlabel();
		 							l2 = getlabel();
		 							fprintf(intermediate, "L%d:\n",l1);
		 							number = codegen(temp -> Ptr1);
		 							fprintf(intermediate, "JZ R%d , L%d\n",number,l2);
		 							freereg();
									number = codegen(temp -> Ptr2);
		 							fprintf(intermediate, "JMP L%d\n",l1);
		 							fprintf(intermediate, "L%d:\n",l2);
		 							freereg();

		 							break;

		 	case NODE_FUNC :
		 							// printf("counter : %d\n",counter);
		 							for(i = 0; i <= counter; i++)
		 								fprintf(intermediate, "PUSH R%d\n",i);
		 							status = counter;

		 							freeallreg();
		 							if(temp -> Ptr3 == NULL && temp -> Ptr2 == NULL)
		 							{
		 							}

		 							else if(temp -> Ptr2 != NULL)
		 							{
		 								// fprintf(intermediate,"IN HERE\n");
		 								// printf("func name : %s \t tree_iter : %d\n",temp -> NAME,tree_iter);
		 								codegen(temp -> Ptr2); //more than 1 argument
		 							}
		 							else
		 							{
		 								
		 								if((temp -> Ptr3 -> NODE == NODE_ID) || (temp -> Ptr3 -> NODE == NODE_FIELD) || (temp -> Ptr3 -> NODE == NODE_ARRAY))
										{
											// Argtemp = Arg_callee;
											Argtemp = temp -> Ptr3 -> Ptr3;
									
											// if(Argtemp -> TYPE == TypeLookup("amp_integer") || Argtemp -> TYPE == TypeLookup("amp_boolean") )
											// 	isamp = 1; 
											// if(Argtemp -> amp)
											// 	isamp = 1;   
											 if(Argtemp -> amp == 1)
													fld = 1; 
										}
										

		 								a = codegen(temp -> Ptr3);
		 								fprintf(intermediate, "PUSH R%d\n",a);
		 								freereg();
		 							}


		 							fprintf(intermediate, "PUSH R0\n"); //space for return value

		 							Gtemp = Glookup(temp -> NAME);
		 							fprintf(intermediate, "CALL F%d\n",Gtemp -> BINDING);

		 							r1 = getreg();
                                    fprintf(intermediate, "POP R%d\n",r1);	// for return value
                                    freereg();

                                    r2 = getreg();

                                    Argtemp = Gtemp -> Arglist;
                                    temporary = 0;

                                    while(Argtemp != NULL)
                                    {
                                    	temporary = temporary + 1;
		 								fprintf(intermediate, "POP R%d\n",r2);
		 								Argtemp = Argtemp -> NEXT;
                                    }

                                    freereg();

                                    for(i = status; i >= 0; i--)
                                    {
                                    	temporary = temporary + 1;
		 								fprintf(intermediate, "POP R%d\n",i);
                                    }
		 							counter = status;
		 							r1 = getreg();
		 							r2 = getreg();
		 							fprintf(intermediate, "MOV R%d, %d\n",r1,(temporary+1));
		 							fprintf(intermediate, "MOV R%d, SP\n",r2);
		 							
		 							fprintf(intermediate, "ADD R%d, R%d\n",r2,r1);
		 							fprintf(intermediate, "MOV R%d, [R%d]\n",r1,r2);
		 							freereg();

		 							return r1;

		 							break;



		 	case NODE_RET : 
	 								res = codegen(temp -> Ptr2);
		 							r1 = getreg();
		 							fprintf(intermediate, "MOV R%d,BP\n",r1);
		 							r2 = getreg();
		 							fprintf(intermediate, "MOV R%d,2\n",r2);
		 							fprintf(intermediate, "SUB R%d,R%d\n",r1,r2);
		 							freereg();
		 							fprintf(intermediate, "MOV [R%d],R%d\n",r1,res);
		 							freereg();
		 							freereg();
		 							Ltemp = Lhead;
		 							while(Ltemp != NULL)
		 							{                                                                
                                        fprintf(intermediate, "POP R0\n");
                                        Ltemp = Ltemp -> NEXT;
                                    }

		 							fprintf(intermediate, "MOV BP,[SP]\n");
		 							fprintf(intermediate, "POP R0\n");
                                    fprintf(intermediate, "RET\n");

                                    break;

            case NODE_ALLOC :	
            						for(i = 0; i <= counter; i++)
		 								fprintf(intermediate, "PUSH R%d\n",i);
		 							status = counter;

		 							freeallreg();
		 							fprintf(intermediate, "PUSH R0\n");
		 							
		 							fprintf(intermediate, "CALL ALLOC\n");
		 							
		 							fprintf(intermediate, "POP R0\n");
									
									temporary = 0;
									for(i = status; i >= 0; i--)
                                    {
                                    	temporary = temporary + 1;
		 								fprintf(intermediate, "POP R%d\n",i);
                                    }
		 							
		 							counter = status;
		 							
		 							r1 = getreg();
		 							r2 = getreg();
		 							fprintf(intermediate, "MOV R%d, %d\n",r1,(temporary+1));
		 							fprintf(intermediate, "MOV R%d, SP\n",r2);
		 							fprintf(intermediate, "ADD R%d, R%d\n",r2,r1);
		 							fprintf(intermediate, "MOV R%d, [R%d]\n",r1,r2);
		 							freereg();

		 							return r1;

		 	 case NODE_ALLOC1 :	
		 	 						
            						for(i = 0; i <= counter; i++)
		 								fprintf(intermediate, "PUSH R%d\n",i);
		 							status = counter;

		 							freeallreg();
		 	 						r1 = codegen(temp -> Ptr2);
		 	 						fprintf(intermediate, "PUSH R%d\n",r1 );
		 	 						freereg();
		 							
		 							fprintf(intermediate, "PUSH R0\n");
		 							
		 							fprintf(intermediate, "CALL ALLOCATION\n");
		 							
		 							fprintf(intermediate, "POP R0\n");
		 							fprintf(intermediate, "POP R0\n");
									
									temporary = 0;
									for(i = status; i >= 0; i--)
                                    {
                                    	temporary = temporary + 1;
		 								fprintf(intermediate, "POP R%d\n",i);
                                    }
		 							
		 							counter = status;
		 							
		 							r1 = getreg();
		 							r2 = getreg();
		 							fprintf(intermediate, "MOV R%d, %d\n",r1,(temporary+1+1));
		 							fprintf(intermediate, "MOV R%d, SP\n",r2);
		 							fprintf(intermediate, "ADD R%d, R%d\n",r2,r1);
		 							fprintf(intermediate, "MOV R%d, [R%d]\n",r1,r2);
		 							freereg();

		 							return r1;

		 	case NODE_FREE1 :
		 							r1 = getreg();
		 							for(i = 0; i <= counter; i++)
		 								fprintf(intermediate, "PUSH R%d\n",i);
		 							status = counter;

		 							freeallreg();
		 							isamp = 1;
		 							r1 = codegen(temp -> Ptr2);
		 							r2 = getreg();
		 							fprintf(intermediate, "MOV R%d, [R%d]\n",r2,r1);

		 							fprintf(intermediate, "PUSH R%d\n",r2);
		 							freereg();
		 							freereg();
		 							fprintf(intermediate, "PUSH R0\n");

		 							fprintf(intermediate, "CALL FREE1\n");
		 							
		 							fprintf(intermediate, "POP R0\n");//for return value 

		 							isamp = 1;
		 							r1 = codegen(temp -> Ptr2);
		 							fprintf(intermediate, "MOV [R%d], R0\n",r1);
		 							freereg();
		 							fprintf(intermediate, "POP R0\n"); // for argument

									for(i = status; i >= 0; i--)
                                    {
		 								fprintf(intermediate, "POP R%d\n",i);
                                    }
		 							
		 							counter = status;

		 							return;

		 	case NODE_FREE :
		 							r1 = getreg();
		 							r1 = codegen(temp -> Ptr2);
		 							for(i = 0; i <= counter; i++)
		 								fprintf(intermediate, "PUSH R%d\n",i);
		 							status = counter;

		 							freeallreg();
		 							fprintf(intermediate, "PUSH R%d\n",r1);

		 							fprintf(intermediate, "CALL FREE\n");
		 							
		 							fprintf(intermediate, "POP R0\n"); // for argument
									
									for(i = status; i >= 0; i--)
                                    {
		 								fprintf(intermediate, "POP R%d\n",i);
                                    }
		 							
		 							counter = status;

		 							return;		 	case NODE_NILL :
		 					r1 = getreg();
		 					fprintf(intermediate, "MOV R%d, %d\n",r1,temp -> VALUE);
		 					return r1;

			default:
					printf("NODETYPE is %d\n",temp -> NODE);
					printf("Error : Unknown Node Type\n");
					exit(1);	
		}														
	}
	return 0;
}