Esempio n. 1
0
/*	Install all local declarations (identifiers) present in
	a function into that function's local symbol table
	*/
void localInstall(Tnode *root,struct Lsymbol **Lhead)
{
	struct Lsymbol *lnode;
	
	if(root == NULL)
		return;
	
	switch(root->NODETYPE)
	{
		case CONTINUE		:	localInstall(root->Ptr1,Lhead);
								localInstall(root->Ptr2,Lhead);
								break;
		
		case DECLSTATEMENT	:	decnode = root->Ptr1;
								localInstall(root->Ptr2,Lhead);
								break;
		
		case IDFRDECL		:	lnode = Llookup(root->NAME,Lhead);
								
								if(lnode != NULL)
								{
									if(root->TYPE != lnode->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++;
								}
								
								else Linstall(root->NAME,decnode->TYPE,Lhead);
								
								break;
	}
}
Esempio n. 2
0
void Linstall(char* name, int type){
    struct Lsymbol *check;
    check = Llookup(name);
    if(check != NULL)   //error on redefining the variable      
      {
        yyerror("local variable redefined ");
        printf(" %s",name);
        exit(0);
    }
    struct Lsymbol *temp;
    temp = (struct Lsymbol *)malloc(sizeof(struct Lsymbol));
    temp->name = (char *)malloc(sizeof(name));
    strcpy(temp->name,name);
    temp->type = type;
    if(temp->type == ARG_INT || temp->type == ARG_BOOLEAN || temp->type == ARG_REF_INT || temp->type == ARG_REF_BOOLEAN){
        temp->binding = argbind; //Make the appropriate value here
        argbind--;
    }
    else if(temp->type == ARG_PAIR || temp->type == ARG_REF_PAIR){
        temp->binding = argbind; //Make the appropriate value here
        argbind = argbind - 2;
    }
    else if(temp->type == STYPE_INT || temp->type == STYPE_BOOLEAN){
        temp->binding = localbind;
        localbind++;
    }
    else if(temp->type == STYPE_PAIR){
        temp->binding = localbind;
        localbind = localbind + 2;
    }
    temp->next = NULL;
    if(Lhead == NULL){
   	 Lhead = temp;   	 
   	 return;    
    }
    temp->next = Lhead;
    Lhead = temp;
    return;    
}
Esempio n. 3
0
/*	Installs function arguments into that function's local symbol table (with semantic checking)
	*/
void argLocalInstall(Tnode *root,struct Lsymbol **Lhead)
{
	struct Lsymbol *lnode;
	ArgStruct *arg;
	char typ[10];
	
	if(root == NULL)
		return;

	switch(root->NODETYPE)
	{
		case CONTINUE		:	argLocalInstall(root->Ptr1,Lhead);
								argLocalInstall(root->Ptr2,Lhead);
								break;
		
		case ARGSTATEMENT	:	argnode = root->Ptr1;
								argLocalInstall(root->Ptr2,Lhead);
								break;
		
		case IDALIASARG		:
		
		case IDARG			:	lnode = Llookup(root->NAME,Lhead);
								
								if(lnode != NULL)
								{
									if(argnode->TYPE != lnode->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 Linstall(root->NAME,argnode->TYPE,Lhead);
								
								break;
	}
}
Esempio n. 4
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				:	;
	}
}
Esempio n. 5
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!");
	}
}
Esempio n. 6
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;
}