package chocopy.pa3;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import chocopy.common.analysis.*;
import chocopy.common.astnodes.*;
import chocopy.common.analysis.types.*;
import chocopy.common.codegen.*;

import chocopy.common.codegen.RiscVBackend.Register;
import static chocopy.common.codegen.RiscVBackend.Register.*;


/**
 * This is where the main implementation of PA3 will live.
 *
 * <p>
 * A large part of the functionality has already been implemented in the base
 * class, CodeGenBase. Make sure to read through that class, since you will want
 * to use many of its fields and utility methods in this class when emitting
 * code.
 *
 * <p>
 * Also read the PDF spec for details on what the base class does and what APIs
 * it exposes for its sub-class (this one). Of particular importance is knowing
 * what all the SymbolInfo classes contain.
 */
public class CodeGenImpl extends CodeGenBase 
{
    /** Label for built-in routines. */
    protected final Label makeintLabel = new Label("makeint");
    protected final Label strneqlLabel = new Label("strneql");
    protected final Label streqlLabel = new Label("streql");
    protected final Label makeboolLabel = new Label("makebool");
    protected final Label strcatLabel = new Label("strcat");
    protected final Label concatLabel = new Label("concat");
    protected final Label conslistLabel = new Label("conslist");
    protected final Label initcharsLabel = new Label("initchars");  
    protected final Label allCharsLabel = new Label("allChars");
      
    /** Variable to indicate if Strings are used in the program for optimization */
    private boolean flag = false;

    /** Operation on None. */
    private final Label errorNone = new Label("error.None");
    /** Division by zero. */
    private final Label errorDiv = new Label("error.Div");
    /** Index out of bounds. */
    private final Label errorOob = new Label("error.OOB");
    
    /** Let's try to implement everything first.*/
    private final Label errorNI = new Label("error.NI");
    
    /** A code generator emitting instructions to BACKEND. */
    public CodeGenImpl(RiscVBackend backend) 
    {
    		
        super(backend);
        backend.defineSym("listHeaderWords", "4"); 
        backend.defineSym("strHeaderWords", "4");
        backend.defineSym("bool.True", "const_1"); 
        backend.defineSym("bool.False", "const_0");
    }
    
    
    /**
     * Emits the top level of the program.
     *
     * <p>
     * This method is invoked exactly once, and is surrounded by some boilerplate
     * code that: (1) initializes the heap before the top-level begins and (2) exits
     * after the top-level ends.
     *
     * <p>
     * You only need to generate code for statements.
     *
     * @param statements top level statements
     */
    protected void emitTopLevel(List<Stmt> statements) 
    {
    		String mainlabel = "@..main.size";
        StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(null);
        backend.emitADDI(SP, SP, "-"+mainlabel, "Saved FP and saved RA (unused at top level).");
        backend.emitSW(ZERO, SP, mainlabel+"-4", "Top saved FP is 0.");
        backend.emitSW(ZERO, SP, mainlabel+"-8", "Top saved RA is 0.");
        backend.emitADDI(FP, SP, mainlabel, "Set FP to previous SP.");
        backend.emitJAL(initcharsLabel,"Initialize one-character strings");
        for (Stmt stmt : statements) 
        {
            stmt.dispatch(stmtAnalyzer);
        }
        stmtAnalyzer.emitSizeLabel();
        backend.emitLI(A0, EXIT_ECALL, "Code for ecall: exit");
        backend.emitEcall(null);
    }

    /**
     * Emits the code for a function described by FUNCINFO.
     *
     * <p>
     * This method is invoked once per function and method definition. At the code
     * generation stage, nested functions are emitted as separate functions of their
     * own. So if function `bar` is nested within function `foo`, you only emit
     * `foo`'s code for `foo` and only emit `bar`'s code for `bar`.
     */
    protected void emitUserDefinedFunction(FuncInfo funcInfo) 
    {
        backend.emitGlobalLabel(funcInfo.getCodeLabel());
        
        // --- Prologue ---
        String sizelabel = "@" + funcInfo.getFuncName()+".size";
        backend.emitADDI(SP, SP, "-"+sizelabel, "Reserve space for stack frame.");
        backend.emitSW(RA, SP, sizelabel+"-4", "return address");
        backend.emitSW(FP, SP, sizelabel + "-8", "control link");
        backend.emitADDI(FP, SP, sizelabel, "New FP is at old SP.");
        
        StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
        int emptySlotNum = 3;
        for (StackVarInfo var : funcInfo.getLocals()) 
        {
        		Literal varLiteral = var.getInitialValue();
        		Register reg = varLiteral.dispatch(stmtAnalyzer);
        		// All Literals should save locations for the values in A0
    			backend.emitSW(reg, SP, sizelabel+String.format("-%d",emptySlotNum*wordSize), "Push local variable: " + var.getVarName() + " onto stack");
    			emptySlotNum++;
        }
        
        
        // --- Function Body ---
        for (Stmt stmt : funcInfo.getStatements()) 
        {
            stmt.dispatch(stmtAnalyzer);
        }
        stmtAnalyzer.emitSizeLabel();
        backend.emitJ(stmtAnalyzer.epilogue, "Jump to function epilogue");
        
        
        // --- Epilogue ---
        backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue");
        backend.emitLW(RA, FP, -4, "Get return address");
        backend.emitLW(FP, FP, -8, "Use control link to restore caller's fp");
        backend.emitADDI(SP, SP, sizelabel, "Restore stack pointer");
        backend.emitJR(RA, "Return to caller");
    }

    /** An analyzer that encapsulates code generation for statements. */
    private class StmtAnalyzer extends AbstractNodeAnalyzer<Register> 
    {
        /*
         * The symbol table has all the info you need to determine what a given
         * identifier 'x' in the current scope is. You can use it as follows: SymbolInfo
         * x = sym.get("x");
         *
         * A SymbolInfo can be one the following: - ClassInfo: a descriptor for classes
         * - FuncInfo: a descriptor for functions/methods - AttrInfo: a descriptor for
         * attributes - GlobalVarInfo: a descriptor for global variables - StackVarInfo:
         * a descriptor for variables allocated on the stack, such as locals and
         * parameters
         *
         * Since the input program is assumed to be semantically valid and well-typed at
         * this stage, you can always assume that the symbol table contains valid
         * information. For example, in an expression `foo()` you KNOW that
         * sym.get("foo") will either be a FuncInfo or ClassInfo, but not any of the
         * other infos and never null.
         *
         * The symbol table in funcInfo has already been populated in the base class:
         * CodeGenBase. You do not need to add anything to the symbol table. Simply
         * query it with an identifier name to get a descriptor for a function, class,
         * variable, etc.
         *
         * The symbol table also maps nonlocal and global vars, so you only need to
         * lookup one symbol table and it will fetch the appropriate info for the var
         * that is currently in scope.
         */

        /** Symbol table for my statements. */
        private final SymbolTable<SymbolInfo> sym;

        /** Label of code that exits from procedure. */
        protected final Label epilogue;

        /** The descriptor for the current function, or null at the top level. */
        private final FuncInfo funcInfo;

        /** Label of code that exits from block. */
        protected Stack<Label> elseBlock = new Stack<Label>();

        /** Label which stores total size of function on stack */   
        private final String size_label;
        /** Variable to store offset from frame pointer to identify next 
          * empty space on stack frame to store variable*/
        private int sp_off;
        /** Variable to store maximum possible offset depending on stack size.*/
        private int max_sp;
        private boolean l_value = false;
        private final Register[] registerPool = {T2,T3, T4, T5, T6, A2, A3, A4, A5, A6, A7};
        private final boolean[] registerAvilMap = {true, true, true, true, true, true, true, true, true, true, true};
        private int getRegister()
        { //return a handle of a vacant register
            for(int i = 0; i < 11; ++i)
                if(registerAvilMap[i])
                {
                    registerAvilMap[i]=false;
                    return i;
                }
            for(int i = 0; i < 11; ++i)
            {
                System.out.println("Insufficient Registers!!!");
                registerAvilMap[i] = true; //freeall;
            }
            registerAvilMap[0]=false;
            return 0;
        }
        private void freeRegister(int handle)
        { //handle used to free the register
            registerAvilMap[handle] = true;
        }
               
        private int getLocalVarLocationOffset(int index, int paramSize) {
    			return - (index - paramSize + 1) * wordSize;
        }
        
        /**
         * An analyzer for the function described by FUNCINFO0, which is null for the
         * top level.
         */
        StmtAnalyzer(FuncInfo funcInfo0) 
        {
            funcInfo = funcInfo0;
            if (funcInfo == null) 
            {
                sym = globalSymbols;
                sp_off = max_sp = 2;
                size_label = "@..main.size";
            } 
            else 
            {
                sym = funcInfo.getSymbolTable();
                sp_off = max_sp = funcInfo0.getLocals().size() + 2 + 1;
                size_label = "@"+funcInfo0.getFuncName()+".size";
            }
            epilogue = generateLocalLabel();
        }
        private void incSp(int i)
        {
            sp_off+=i;
            max_sp = max_sp >= sp_off?max_sp:sp_off;
        }
        
        public void emitSizeLabel()
        {
            backend.defineSym(size_label, max_sp*wordSize);
        }
        
        // *********** functions start ***********
        
        public Register analyze(CallExpr node) 
        {		
            SymbolInfo ty = sym.get(node.function.name);
            if(ty instanceof ClassInfo)
            {
                //object create
                ClassInfo cls = (ClassInfo) ty;
                backend.emitLA(A0, cls.getPrototypeLabel(), String.format("Load pointer to prototype of: %s", cls.getClassName()));
                backend.emitJAL(objectAllocLabel, "Allocate new object in A0");
                backend.emitSW(A0, FP, -sp_off*wordSize, String.format("Push on stack slot %d", sp_off));
                incSp(1);

                backend.emitSW(A0, FP, -sp_off*wordSize, "Push argument 0 from last.");
                backend.emitADDI(SP, FP, -sp_off*wordSize, "Set SP to last argument.");
                backend.emitLW(A1, A0, getDispatchTableOffset(), "Load address of object's dispatch table");
                backend.emitLW(A1, A1, getMethodOffset(cls, "__init__"), String.format("Load address of method: %s.__init__", cls.getClassName()));
                backend.emitJALR(A1, String.format("Invoke method: %s.__init", cls.getClassName()));
                backend.emitADDI(SP, FP, "-"+size_label, "Set SP to stack frame top.");
                -- sp_off;
                backend.emitLW(A0, FP, -sp_off*wordSize, String.format("Pop stack slot %d", sp_off));
            } 
            else 
            {	    			
	        	// function
	        	Identifier functionId = node.function;
	    		FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name);
	    		boolean isInMainFunction = funcInfo == null;
                if (!isInMainFunction) 
                {
                    int calleeDepth = calleeFunctionInfo.getDepth();
                    int callerDepth = funcInfo.getDepth();
                    int jumps = callerDepth - calleeDepth + 1;
                    
                    int tmpHandle = getRegister();
                    Register tmp = registerPool[tmpHandle];
                    
                    backend.emitMV(tmp, FP, "Load FP");
                    
                    FuncInfo curr = funcInfo;
                    for (int i = 0; i < jumps; i++) 
                    {
                    		backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link to " + curr.getFuncName());
                    		curr = curr.getParentFuncInfo();
                    }
                    backend.emitSW(tmp, FP, -(sp_off+1) * wordSize, "Push static link onto active frame.");
                    freeRegister(tmpHandle);
                } 
                else 
                {
	        		backend.emitSW(FP, FP, -(sp_off+1) * wordSize, "Push static link onto active frame.");
	    		}
                incSp(1);

     			List<Expr> args = node.args;
                int spaceRequiredForArgs = (args.size() + 1)*4;
                for (int i = 0; i < args.size(); i++) 
                {
                		incSp(1);
	        			Expr expr = args.get(i);
	        			expr.dispatch(this);
	        		
	        			String formalParamName = calleeFunctionInfo.getParams().get(i);
	        			StackVarInfo formalParamInfo = (StackVarInfo) calleeFunctionInfo.getSymbolTable().get(formalParamName);
	        			
                    if (expr.getInferredType().equals(Type.INT_TYPE)) 
                    {
                    		if (!formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) && !formalParamInfo.getVarType().equals(Type.INT_TYPE)) {
                 		   backend.emitJAL(errorNI, "Passed argument does not match formal parameter");
                 	   }
                    		
                    		else if ((functionId.name.equals("print")) &&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.INT_TYPE))) 
                        {
                            if(!(args.size() == 1 && (args.get(0) instanceof CallExpr) && 
                                    (sym.get(((CallExpr) args.get(0)).function.name) instanceof ClassInfo)))
                                backend.emitJAL(makeintLabel, "Box integer");


                        } 

                    } 
                    else if (expr.getInferredType().equals(Type.BOOL_TYPE)) 
                    { 
                        if(!formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) && !formalParamInfo.getVarType().equals(Type.BOOL_TYPE)) 
                        {
                        	backend.emitJAL(errorNI, "Passed argument does not match formal parameter");
                        } 
                        else if ((functionId.name.equals("print"))&&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.BOOL_TYPE))) 
                        {
	                		if(!(args.size() == 1 && (args.get(0) instanceof CallExpr) && 
                                (sym.get(((CallExpr) args.get(0)).function.name) instanceof ClassInfo)))
                                backend.emitJAL(makeboolLabel, "Box boolean");
                        } 
                    }
	        			
	        			// All expressions should save their end result in A0
	        			// So, once expr is evaluated add value inside A0 onto stack as an actual argument
                    backend.emitSW(A0, FP, -(sp_off) * wordSize, "Push actual argument for " + formalParamName + " onto stack");
                }
	        		backend.emitADDI(SP, FP, -(sp_off)*wordSize, "Set SP to last argument.");
	        		backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name);
	        		backend.emitADDI(SP, FP, "-"+size_label, "Set SP to stack frame top.");
                sp_off-=spaceRequiredForArgs/wordSize;
            }
            
            return A0;
        }
        
        public Register analyze(MethodCallExpr node) 
        {
        		Register obj = node.method.object.dispatch(this);
            int n_args = node.args.size();
            
            Label label = generateLocalLabel();
            backend.emitBNEZ(obj, label, "Ensure not None");
            backend.emitJ(errorNone, "Go to error handler");
            backend.emitLocalLabel(label, "Not None");
            incSp(n_args+1);
            backend.emitSW(obj, FP, (n_args - sp_off) *wordSize, String.format("Push argument %d from last.", n_args));

            for (int i = 0; i < n_args; ++i) 
                backend.emitSW(node.args.get(i).dispatch(this), FP, (n_args - i - 1 - sp_off) * wordSize, 
                    String.format("Push argument %d from last.", n_args - i - 1));
            
            backend.emitLW(A0, FP, (n_args- sp_off) * wordSize, String.format("Peek stack slot %d", sp_off - (n_args + 1)));
            ClassInfo objectClass = null;
            if(node.method.object instanceof CallExpr)
                objectClass = (ClassInfo)globalSymbols.get(((CallExpr)node.method.object).getInferredType().className());
            else 
                objectClass = (ClassInfo)globalSymbols.get(node.method.object.getInferredType().className());
            backend.emitLW(A1, A0, getDispatchTableOffset(), "Load address of object's dispatch table");
            backend.emitLW(A1, A1, getMethodOffset(objectClass, node.method.member.name), 
                String.format("Load address of method: %s.%s", objectClass.getClassName(), node.method.member.name));
            backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
            backend.emitJALR(A1, String.format("Invoke method: %s.%s", objectClass.getClassName(), node.method.member.name));
            backend.emitADDI(SP, FP, "-" + size_label, "Set SP to stack frame top");
            sp_off -= n_args+1;
    
            return A0;
        }
        
        
        @Override
        public Register analyze(ReturnStmt stmt) 
        {
        		Expr expr = stmt.value;
            if (expr == null) {
                backend.emitMV(A0, ZERO, "Return None");
            } else {
                // All expressions should save their end result in A0
           	    expr.dispatch(this);
            }
            
        		backend.emitJ(this.epilogue, "Jump to function epilogue");
            return A0;
        }
        
        // *********** functions end ***********

        @Override
        public Register analyze(NoneLiteral node) 
        {
            backend.emitMV(Register.A0, Register.ZERO, "Load none");
            return Register.A0;
        }
        
        @Override
        public Register analyze(StringLiteral node) 
        {
            flag=true;
            Label l = constants.getStrConstant(node.value);
            backend.emitLA(Register.A0, l, "Load string literal");
            return Register.A0;
        }
        
        @Override
        public Register analyze(BooleanLiteral node) 
        {
            if(node.value==true)
                backend.emitLI(Register.A0, 1, "Load boolean literal: true ");
            else
                backend.emitLI(Register.A0, 0, "Load boolean literal: false ");
            return Register.A0;
        }
        
        class StackVarRuntimeInfo 
        {
            public final int off;
            public final Register sl;
            public int regHandle;
            StackVarRuntimeInfo(Register sl, int off)
            {
                this.off = off;
                this.sl = sl;
                regHandle = -1;
            }
            StackVarRuntimeInfo(Register sl, int off, int regHandle)
            {
                this.off = off;
                this.sl = sl;
                this.regHandle = regHandle;
            }
            public void free()
            {
                if(regHandle >= 0)
                {
                    freeRegister(regHandle);
                    regHandle = -1;
                }
            }
        }
        
        private Object getVar(Identifier id)
        {
            if(id.getInferredType().equals(Type.STR_TYPE))
                flag=true;
            SymbolInfo info = sym.get(id.name);
            if(info instanceof StackVarInfo)
                return getStackVar((StackVarInfo)info);
            else
                return ((GlobalVarInfo)info).getLabel();
        }
        
        private StackVarRuntimeInfo getStackVar(StackVarInfo info)
        {
    		FuncInfo varFuncInfo = info.getFuncInfo();
        		
            int tmpHandle = getRegister();
            Register tmp = registerPool[tmpHandle];
                
            if (funcInfo != null && funcInfo != info.getFuncInfo()) 
            {                 
                int varFunDepth = varFuncInfo.getDepth();
                int callerDepth = funcInfo.getDepth();
                int jumps = callerDepth - varFunDepth;
                backend.emitMV(tmp, FP, "Load FP");
                
                FuncInfo curr = funcInfo;
                for (int i = 0; i < jumps; i++) 
                {
                		backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link from " + curr.getFuncName() + " to " + curr.getParentFuncInfo().getBaseName());
                		curr = curr.getParentFuncInfo();
                }
            }
            else 
            {
        		backend.emitMV(tmp, FP, "Referencing local variable: "+info.getVarName());
        	}	
        	int idx = varFuncInfo.getVarIndex(info.getVarName());
        	int paramSize = varFuncInfo.getParams().size();
    		int offset = getLocalVarLocationOffset(idx, paramSize);
        	return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
        }
        
        @Override
        public Register analyze(AssignStmt node) 
        {
            Register reg = node.value.dispatch(this);
            if (reg == null)
                reg = A0;
            for(Expr target: node.targets)
            {
                if(target instanceof Identifier)
                {
                    Identifier targetID = (Identifier)target;
                    Object var = getVar(targetID);
                    if(var instanceof StackVarRuntimeInfo)
                    {
                        StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
                        backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name);
                        rtinfo.free();
                    } 
                    else
                        backend.emitSW(reg, (Label)var, T0, "Store Global variable: " + ((Identifier)targetID).name);
                }
                else
                {
                    backend.emitSW(reg, Register.FP, -sp_off*wordSize, "Store value to be stored");
                    incSp(1);
                    boolean old_lvalue = l_value;
                    l_value = true;
                    Register ret = target.dispatch(this);
                    l_value = old_lvalue;
                    sp_off--;
                    Register tmp = reg;
                    int tmpHandle = -1;
                    if(ret.equals(reg))
                    {
                        tmpHandle = getRegister();
                        tmp = registerPool[tmpHandle];
                    }
                    backend.emitLW(tmp, Register.FP, -sp_off*wordSize, "Load value to be stored");
                    backend.emitSW(tmp, ret, 0, "Set list element"); 
                    if(tmpHandle >= 0)
                        freeRegister(tmpHandle);
                }
            }
            return null;
        }
        
        @Override
        public Register analyze(ExprStmt node) 
        {
            node.expr.dispatch(this);
            return null;
        }
        
        @Override
        public Register analyze(IfExpr node) 
        {
        	Register result = node.condition.dispatch(this);
            Label ln = generateLocalLabel();
            elseBlock.push(generateLocalLabel());

            backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop");
            result = node.thenExpr.dispatch(this);
            if(result != A0)
                backend.emitMV(A0, result, "Move result");
            backend.emitJ(ln, "Jump to end of if expression");
            backend.emitLocalLabel(elseBlock.peek(), "Else part of if expression");
            result = node.elseExpr.dispatch(this);
            if(result != A0)
                backend.emitMV(A0, result, "Move result");
            elseBlock.pop();
            backend.emitLocalLabel(ln, "End of if expression");
            return A0;
        }
        
        @Override
        public Register analyze(IfStmt node) 
        {
        	Register result = node.condition.dispatch(this);
            Label ln = generateLocalLabel();
            elseBlock.push(generateLocalLabel());
            backend.emitBEQZ(result, elseBlock.peek(),"Jump to end of loop");
            for(Stmt s:node.thenBody)
                s.dispatch(this);
            backend.emitJ(ln, "Jump to end of if statement");
            backend.emitLocalLabel(elseBlock.peek(), "Else part of if statement");
            for(Stmt s:node.elseBody)
                s.dispatch(this);
            elseBlock.pop();
            backend.emitLocalLabel(ln, "End of if statement");
            return null;
        }
        
        @Override
        public Register analyze(BinaryExpr node) 
        {
        	String operator = node.operator;
            
            if(node.left.getInferredType().equals(Type.INT_TYPE) && node.right.getInferredType().equals(Type.INT_TYPE))
            {
                Register r = node.right.dispatch(this);
                backend.emitSW(r, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
                incSp(1);
                Register l = node.left.dispatch(this);
                if(l != A0)
                    backend.emitMV(A0, l, "mv l to A0");
                sp_off--;
                backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
            
                // Arithmetic Operators
                if(operator.equals("+"))
                    backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
                else if(operator.equals("-"))
                    backend.emitSUB(Register.A0, Register.A0, Register.T0, "Sub operation");
                else if(operator.equals("*"))
                    backend.emitMUL(Register.A0, Register.A0, Register.T0, "Mul operation");
                else if(operator.equals("//"))
                {
                    Label label = generateLocalLabel();
                    backend.emitBNEZ(Register.T0, label, "Check for Divide-by-zero");
                    backend.emitJAL(errorDiv, "Divide-by-zero error");
                    backend.emitLocalLabel(label, "Divide since divisor not zero");
                    backend.emitDIV(Register.A0, Register.A0, Register.T0, "Divide operation");
                }
                else if(operator.equals("%"))
                {
                    Label label = generateLocalLabel();
                    backend.emitBNEZ(Register.T0, label, "Check for Divide-by-zero");
                    backend.emitJAL(errorDiv, "Divide-by-zero error");
                    backend.emitLocalLabel(label, "Divide since divisor not zero");
                    backend.emitREM(Register.A0, Register.A0, Register.T0, "Modulus operation");
                }
                else
                {   // Comparison operators
                    String comment="Operator: "+operator;
                    if(operator.equals("=="))
                    {   
                        backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for equality");
                        backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
                    }
                    else if(operator.equals("!="))
                    {
                        backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for inequality");
                        backend.emitSNEZ(Register.A0, Register.A0, "Result is True if XOR does not equal 0");
                    }   
                    else if(operator.equals("<"))
                        backend.emitSLT(Register.A0,Register.A0, Register.T0, comment);
                    else if(operator.equals(">"))
                        backend.emitSLT(Register.A0, Register.T0, Register.A0, comment);
                    else if(operator.equals("<="))
                    {
                        backend.emitADDI(Register.T0,Register.T0, 1, "Increment by 1");
                        backend.emitSLT(Register.A0, Register.A0, Register.T0, comment);
                    }
                    else if(operator.equals(">="))
                    {
                        backend.emitADDI(Register.A0,Register.A0, 1, "Increment by 1");
                        backend.emitSLT(Register.A0, T0, A0, comment);
                    }
                    else
                    {
                        backend.emitJAL(errorNI, "Operator not implemented for integer operands");
                    }
                }
            }
            else if(node.left.getInferredType().equals(Type.BOOL_TYPE) && node.right.getInferredType().equals(Type.BOOL_TYPE))
            {  
                // Comparison operators
                if(operator.equals("=="))
                {
                    node.right.dispatch(this);
                    backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
                    incSp(1);
                    node.left.dispatch(this);
                    sp_off--;
                    backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
                    backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for equality");
                    backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
                }
                else if(operator.equals("!="))
                {
                    node.right.dispatch(this);
                    backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
                    incSp(1);
                    node.left.dispatch(this);
                    sp_off--;
                    backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
                    backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for inequality");
                    backend.emitSNEZ(Register.A0, Register.A0, "Result is True if XOR does not equal 0");
                }  
                else if(operator.equals("and"))
                {
                    Label label = generateLocalLabel();
                    node.left.dispatch(this);
                    backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
                    incSp(1);
                    backend.emitBEQZ(Register.A0, label, "If first operand is false, don't check second");
                    node.right.dispatch(this);
                    sp_off--;
                    backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
                    backend.emitAND(Register.A0, Register.A0, Register.T0, "AND operation");
                    backend.emitLocalLabel(label, "Next step after AND");
                }
                else if(operator.equals("or"))
                {
                    Label label = generateLocalLabel();
                    node.left.dispatch(this);
                    backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
                    incSp(1);
                    backend.emitBNEZ(Register.A0, label, "If first operand is true, don't check second");
                    node.right.dispatch(this);
                    sp_off--;
                    backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
                    backend.emitOR(Register.A0, Register.A0, Register.T0, "OR operation");
                    backend.emitLocalLabel(label, "Next step after OR");
                }
                else
                {
                    backend.emitJAL(errorNI, "Operator not implemented for boolean operands");
                }
            }
            else if(node.left.getInferredType().isListType() && node.right.getInferredType().isListType())
            {
                if(operator.equals("+"))
                {
                    incSp(2);
                    backend.emitSW(node.left.dispatch(this), Register.FP, -(sp_off-1)*wordSize, "Push left operand on stack slot "+sp_off);
                    backend.emitSW(node.right.dispatch(this), Register.FP, -(sp_off)*wordSize, "Push right operand on stack slot "+sp_off);
                    backend.emitADDI(Register.SP,Register.FP,-(sp_off)*wordSize,"Set SP to last argument");
                    backend.emitJAL(concatLabel, "Call concatenation routine");
                    backend.emitADDI(Register.SP, Register.FP, "-"+size_label, "Set SP to stack frame");
                    sp_off -= 2;
                    return Register.A0;
                }
                else
                    backend.emitJAL(errorNI, "Operator not implemented for list operands");
                 
            }
            else if(node.left.getInferredType().equals(Type.STR_TYPE) && node.right.getInferredType().equals(Type.STR_TYPE))
            {
                if(operator.equals("=="))
                {
                    incSp(2);
                    backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last");
                    backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last");
                    backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
                    backend.emitJAL(streqlLabel, "Invoke method:streql");
                    sp_off -= 2;
                    backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore sp");
                }
                else if(operator.equals("!="))
                {
                    incSp(2);
                    backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last");
                    backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last");
                    backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
                    backend.emitJAL(strneqlLabel, "Invoke method:strneql");
                    sp_off -= 2;
                    backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore SP");
                }
                else if(operator.equals("+"))
                {
                    incSp(1);
                    backend.emitSW(node.left.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 0 from last");
                    incSp(1);
                    backend.emitSW(node.right.dispatch(this), FP, (- sp_off) *wordSize, "Push argument 1 from last");
                    backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument");
                    backend.emitJAL(strcatLabel, "Invoke method:strcat");
                    sp_off -= 2;
                    backend.emitADDI(SP, FP, -sp_off*wordSize, "Restore sp");
                }
                else
                    backend.emitJAL(errorNI, "Operator not implemented for String operands");
            }
            else if(operator.equals("is"))
            {
                Register l = node.right.dispatch(this);
                backend.emitSW(l, FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
                incSp(1);
                Register r = node.left.dispatch(this);
                sp_off--;
                backend.emitLW(T0, FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
                backend.emitXOR(A0,  r, T0, "Operator: is");
                backend.emitSEQZ(A0,  r, "Result is True if XOR equals 0");
            }
            else
            {
                backend.emitJAL(errorNI, "Operator not implemented");
            }
            return A0;
        }
        
        @Override
        public Register analyze(UnaryExpr node) 
        {
            if(node.operator.equals("-") && node.getInferredType().equals(Type.INT_TYPE))
                backend.emitSUB(A0, ZERO, node.operand.dispatch(this), "Unary negation");
            else if(node.operator.equals("not") && node.getInferredType().equals(Type.BOOL_TYPE))
                backend.emitSEQZ(A0, node.operand.dispatch(this), "Not operation on Register A0");
            else
                backend.emitJAL(errorNI, "Operator not implemented");
            return A0;
        }
        @Override
        public Register analyze(Identifier node) 
        {
        	Identifier targetID = (Identifier)node;
            Object var = getVar(targetID);
            if(var instanceof StackVarRuntimeInfo){
                StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
                backend.emitLW(A0, rtinfo.sl, rtinfo.off,"Load local variable: "+node.name);
                rtinfo.free();
            }
            else 
                backend.emitLW(A0, (Label) var, "Load local variable: "+node.name);

            return Register.A0;
        }
         
        @Override
        public Register analyze(WhileStmt node) 
        {
        	Label startLoop = generateLocalLabel(); 
            backend.emitLocalLabel(startLoop, "Beginning of while loop");
            Register result = node.condition.dispatch(this);
            Label endLoop = generateLocalLabel();

            backend.emitBEQZ(result, endLoop,"Jump to end of the loop");
            for(Stmt stmt:node.body) 
                stmt.dispatch(this);
            
            backend.emitJ(startLoop, "Jump to beginning of loop");
            backend.emitLocalLabel(endLoop, "End of while loop");
            return null;
        }
        
        @Override
        public Register analyze(ListExpr node) {
            int l = node.elements.size();
            int i=l;
            for(Expr exp:node.elements)
            {
                Register r = exp.dispatch(this);
                if (exp.getInferredType().equals(Type.INT_TYPE) && !node.elements.get(0).getInferredType().equals(Type.INT_TYPE))
                {
                    if(r!=Register.A0)
                        backend.emitMV(r, Register.A0, "Copy to Register A0");
                    backend.emitJAL(makeintLabel, "Box integer");
                }
                else if (exp.getInferredType().equals(Type.BOOL_TYPE) && !node.elements.get(0).getInferredType().equals(Type.BOOL_TYPE)) 
                {
                    if(r!=Register.A0)
                        backend.emitMV(r, Register.A0, "Copy to Register A0");
                    backend.emitJAL(makeboolLabel, "Box boolean");
                }
                backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push argument "+i+" from last");
                incSp(1);
                i--;
            }
            backend.emitLI(Register.A0, l, "Pass list length");
            backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push length of list");
            incSp(1);
            backend.emitADDI(Register.SP, Register.FP, -(sp_off-1)*wordSize, "Set SP to last argument");
            backend.emitJAL(conslistLabel, "Move values to new list object");
            backend.emitADDI(Register.SP, Register.FP, "-"+size_label, "Set SP to stack frame");
            sp_off-=l+1;
            return Register.A0;
        }

        @Override
        public Register analyze(IntegerLiteral node) 
        {
    		backend.emitLI(A0, node.value, "Load integer literal " + node.value);    		
            return A0;
        }
        
        @Override
        public Register analyze(ForStmt node) 
        {
            int r;
            List<Integer> regs= new ArrayList<Integer>();
            if (node.iterable.getInferredType().isListType()) 
            {
                Label startLoop = generateLocalLabel();
                Label endLoop = generateLocalLabel();
                Label temp = generateLocalLabel();
                
                r=getRegister();
                regs.add(r);
                Register iden = registerPool[r];
                
                node.iterable.dispatch(this);
                
                r=getRegister();
                regs.add(r);
                Register l = registerPool[r];
                
                r=getRegister();
                regs.add(r);
                Register ln = registerPool[r];
                
                backend.emitBNEZ(Register.A0, temp, "Ensure not none");
                backend.emitJ(errorNone, "Empty List");
                backend.emitLocalLabel(temp, "Continue execution for for-loop");
                backend.emitMV(l,Register.A0,"Location of list");
                
                r=getRegister();
                regs.add(r);
                Register iter = registerPool[r];
                
                backend.emitSW(l, Register.FP, -sp_off*wordSize, "Store location on stack");
                incSp(1);
                
                backend.emitMV(iter, Register.ZERO, "Initialize index variable");
                backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
                incSp(1);
                
                backend.emitLocalLabel(startLoop, "Start for loop");
                backend.emitLW(iter, Register.FP, -(sp_off-1)*wordSize, "Load index from stack");
                backend.emitLW(l, Register.FP, -(sp_off-2)*wordSize, "Store list location from stack");
                backend.emitLW(ln, l, getAttrOffset(listClass, "__len__"), "Get attribute __len__");
                backend.emitBGEU(iter, ln, endLoop, "Jump to end loop if counter exceeds length");
                backend.emitADDI(iter, iter, 1, "Increment counter");
                backend.emitSW(iter, Register.FP, -(sp_off-1)*wordSize, "Store index on stack");
                backend.emitADDI(iden, iter, 3, "Compute list element offset in words");
                backend.emitLI(ln, wordSize, "Word size in bytes");
                backend.emitMUL(iden, iden, ln, "Compute list element offset in bytes");
                backend.emitADD(iden, l, iden, "Pointer to list element");
                backend.emitLW(iden, iden, 0, "Set list element");
                SymbolInfo info = sym.get(node.identifier.name);
                if(info instanceof GlobalVarInfo)
                {
                    GlobalVarInfo gvi=(GlobalVarInfo) info;
                    backend.emitSW(iden, gvi.getLabel(), Register.T0, "Assign global: "+node.identifier.name+"(using tmp register)");
                }
                	else
                {
                    StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) getVar(node.identifier);
                    backend.emitSW(iden, rtinfo.sl, rtinfo.off,"Store local variable: "+node.identifier.name);
                    rtinfo.free();
                }
                for(Stmt stmt:node.body)
                    stmt.dispatch(this);
                sp_off -= 2;
                backend.emitJ(startLoop, "Jump to beginning of loop");
                backend.emitLocalLabel(endLoop, "End of for loop");
            		}
            	else if (node.iterable.getInferredType().equals(Type.STR_TYPE))
            {
                Label startLoop = generateLocalLabel();
                Label endLoop = generateLocalLabel();
                r=getRegister();
                regs.add(r);
                Register iden = registerPool[r];
                node.iterable.dispatch(this);
                r=getRegister();
                regs.add(r);
                Register l = registerPool[r];
                r=getRegister();
                regs.add(r);
                Register ln = registerPool[r];
                backend.emitMV(l,Register.A0,"Location of String");
                r=getRegister();
                regs.add(r);
                Register iter = registerPool[r];
                backend.emitSW(l, Register.FP, -sp_off*wordSize, "Store location on stack");
                incSp(1);
                backend.emitMV(iter, Register.ZERO, "Initialize index variable");
                backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
                incSp(1);
                backend.emitLocalLabel(startLoop, "Start for loop");
                backend.emitLW(iter, Register.FP, -(sp_off-1)*wordSize, "Load index from stack");
                backend.emitLW(l, Register.FP, -(sp_off-2)*wordSize, "Load string location from stack");
                backend.emitLW(ln, l, getAttrOffset(strClass, "__len__"), "Get attribute __len__");
                backend.emitBGEU(iter, ln, endLoop, "Jump to end loop if counter exceeds length");
                backend.emitADDI(iden, iter, 1, "Increment counter");
                backend.emitSW(iden, Register.FP, -(sp_off-1)*wordSize, "Store index on stack");
                backend.emitADDI(iden, iter, 4 * wordSize, "Convert index to offset to char in bytes");
                backend.emitADD(iden, l, iden, "Get pointer to char");
                backend.emitLBU(iden, iden, 0, "Load character");
                backend.emitLI(iter, 20, "Load register");
                backend.emitMUL(iden, iden, iter, "Multiply by size of String object");
                backend.emitLA(l, allCharsLabel, "Index into single-char table");
                backend.emitADD(l, l, iden, "Add size to chartable index");
                SymbolInfo info = sym.get(node.identifier.name);
                if(info instanceof GlobalVarInfo)
                {
                    GlobalVarInfo gvi=(GlobalVarInfo) info;
                    backend.emitMV(Register.A0,l,"Copy value");
                    backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+node.identifier.name+"(using tmp register)");
                }
                else
                {
                    StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) getVar(node.identifier);
                    backend.emitSW(l, rtinfo.sl, rtinfo.off,"Store local variable: "+node.identifier.name);
                    rtinfo.free();
                }
                for(Stmt stmt:node.body)
                    stmt.dispatch(this);
                sp_off -= 2;
                backend.emitJ(startLoop, "Jump to beginning of loop");
                backend.emitLocalLabel(endLoop, "End of for loop");
            } 
            else
            {
                backend.emitJAL(errorNI, "Operator not implemented");
            }
            for(int n:regs) {
                freeRegister(n);
            }
            return null;
        }
         
        @Override
        public Register analyze(IndexExpr node) 
        {
            boolean old_lvalue = l_value;
            l_value = false;
            Register listObj = node.list.dispatch(this);
            backend.emitSW(listObj, FP, -sp_off * wordSize, String.format("Push on stack slot %d", sp_off));
            incSp(1);
            
            Register index = node.index.dispatch(this);
            l_value = old_lvalue;
            Register vacantReg = (index != A0) ? A0 : A1;
            int tmpHandle = getRegister();
            Register temp = registerPool[tmpHandle];

            if (node.list.getInferredType().isListType()) 
            {
                backend.emitLW(vacantReg, FP, -(sp_off-1) * wordSize, String.format("Pop stack slot %d", sp_off-1));
                final Label bp = generateLocalLabel();
                backend.emitBNEZ(vacantReg, bp, "Ensure not None");
                backend.emitJ(errorNone, "Go to error handler");
                backend.emitLocalLabel(bp, "Not None");
                final Label bt = generateLocalLabel();
                backend.emitLW(temp, vacantReg, getAttrOffset(listClass, "__len__"), "Load attribute: __len__");
                backend.emitBLTU(index, temp, bt, "Ensure 0 <= index < len");
                backend.emitJ(errorOob, "Go to error handler");
                backend.emitLocalLabel(bt, "Index within bounds");
                backend.emitADDI(index, index, 4, "Compute list element offset in words");
                backend.emitLI(temp, wordSize, "Word size in bytes");
                backend.emitMUL(index, index, temp, "Compute list element offset in bytes");
                backend.emitADD(vacantReg, vacantReg, index, "Pointer to list element");
                if(l_value)
                    backend.emitMV(A0,vacantReg,"Copy Result");
                else 
                    backend.emitLW(A0, vacantReg, 0, "Load list element"); 
            } 
            else 
            {
                backend.emitLW(vacantReg, FP, - (sp_off-1) * wordSize, String.format("Peek stack slot %d", sp_off-  1));
                Label boundchk = generateLocalLabel();
                backend.emitLW(temp, vacantReg, getAttrOffset(strClass, "__len__"), "Load attribute: __len__");
                backend.emitBLTU(index, temp, boundchk, "Ensure 0 <= idx < len");
                backend.emitJ(errorOob, "Go to error handler");
                backend.emitLocalLabel(boundchk, "Index within bounds");
                backend.emitADDI(T0, vacantReg, 4 * wordSize, "Convert index to offset to char in bytes");
                backend.emitADD(T0, T0, index, "load char loc");
                backend.emitLBU(T0, T0, 0, "Load character");
                backend.emitSLLI(A0, T0, 2, "A0 = T0 * 4");
                backend.emitSLLI(T0, T0, 4, "T0 = T0 * 16");
                backend.emitADD(T0, T0, A0, "T0*20 = offset to all chars");
                backend.emitLA(A0, allCharsLabel, "All chars");
                backend.emitADD(A0, A0, T0, "get the char");
            }
            freeRegister(tmpHandle);
            -- sp_off;
            return A0;
        }
  
        public Register analyze(MemberExpr node) 
        {
            ClassInfo objectClass = (ClassInfo) globalSymbols.get(node.object.getInferredType().className());
            Label label = generateLocalLabel();
            boolean old_lvalue = l_value;
            l_value = false;
            Register obj = node.object.dispatch(this);
            l_value = old_lvalue;

            backend.emitBNEZ(obj, label, "Ensure not None");
            backend.emitJ(errorNone, "Go to error handler");
            backend.emitLocalLabel(label, "Not None");
            if(l_value)
                backend.emitADDI(A0, obj, getAttrOffset(objectClass, node.member.name), "get address of attr");
            else
                backend.emitLW(A0, obj, getAttrOffset(objectClass, node.member.name),
                    String.format("Get value of attribute: %s.%s",  objectClass.getClassName(), node.member.name));
            return A0;
        }
    }

    /**
     * Emits custom code in the CODE segment.
     *
     * <p>This method is called after emitting the top level and the function bodies for each
     * function.
     *
     * <p>You can use this method to emit anything you want outside of the top level or functions,
     * e.g. custom routines that you may want to call from within your code to do common tasks. This
     * is not strictly needed. You might not modify this at all and still complete the assignment.
     *
     * <p>To start you off, here is an implementation of three routines that will be commonly needed
     * from within the code you will generate for statements.
     *
     * <p>The routines are error handlers for operations on None, index out of bounds, and division
     * by zero. They never return to their caller. Just jump to one of these routines to throw an
     * error and exit the program. For example, to throw an OOB error: backend.emitJ(errorOob, "Go
     * to out-of-bounds error and abort");
     */
    protected void emitCustomCode() 
    {
        emitStdFunc("concat");
        emitStdFunc("conslist");
        emitStdFunc("strcat");
        emitStdFunc("streql");
        emitStdFunc("strneql");
        emitStdFunc("makeint");
        emitStdFunc("makebool");
        if(flag)
        {
            emitStdFunc("initchars");
            emitStdFunc("allChars");
        }
        else
            emitStdFunc("dummy");
        emitErrorFunc(errorNone, "Operation on None");
        emitErrorFunc(errorDiv, "Division by zero");
        emitErrorFunc(errorOob, "Index out of bounds");
        emitErrorFunc(errorNI, "Not Implemented.");
    }

    /** Emit an error routine labeled ERRLABEL that aborts with message MSG. */
    private void emitErrorFunc(Label errLabel, String msg) 
    {
        backend.emitGlobalLabel(errLabel);
        if(errLabel==errorOob)
            backend.emitLI(A0, ERROR_OOB, "Exit code for: " + msg);
        else if(errLabel==errorDiv)
            backend.emitLI(A0, ERROR_DIV_ZERO, "Exit code for: " + msg);
        else if(errLabel==errorNI)
            backend.emitLI(A0, ERROR_NYI, "Exit code for: " + msg);    
        else    
            backend.emitLI(A0, ERROR_NONE, "Exit code for: " + msg);
        backend.emitLA(A1, constants.getStrConstant(msg), "Load error message as str");
        backend.emitADDI(
                A1, A1, getAttrOffset(strClass, "__str__"), "Load address of attribute __str__");
        backend.emitJ(abortLabel, "Abort");
    }
}
