diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 7ef4ccc..24914e2 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -34,7 +34,7 @@ public class CodeGenImpl extends CodeGenBase protected final Label makeintLabel = new Label("makeint"); protected final Label strneqlLablel = new Label("strneql"); protected final Label streqlLablel = new Label("streql"); - protected final Label makeboolLablel = new Label("makebool"); + protected final Label makeboolLabel = new Label("makebool"); protected final Label strcatLablel = new Label("strcat"); protected final Label concatLablel = new Label("concat"); protected final Label conslistLablel = new Label("conslist"); @@ -115,7 +115,7 @@ public class CodeGenImpl extends CodeGenBase // space for static link = 1 // space for params = num of params // space for locals = num of locals - int requiredStackSpace = (1 + 1 + 1 + funcInfo.getLocals().size() + funcInfo.getParams().size())*wordSize; + int requiredStackSpace = (1 + 1 + 1 + funcInfo.getParams().size() + funcInfo.getLocals().size())*wordSize; backend.emitADDI(SP, SP, -requiredStackSpace, "Reserve space for stack frame."); backend.emitSW(RA, SP, requiredStackSpace-4, "return address"); @@ -127,12 +127,24 @@ public class CodeGenImpl extends CodeGenBase StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo); int emptySlotNum = 4; + + // example usage: backend.emitLW(A0, SP, varSlotMap.get(varName)) to load the variable using its name + Map varSlotMap = stmtAnalyzer.varSlotMap; + + for (int i = 0; i < funcInfo.getParams().size(); i++) { + String param = funcInfo.getParams().get(i); + backend.emitLW(A0, FP, -wordSize*i, "Load argument " + param + " from callers active frame"); + backend.emitSW(A0, SP, requiredStackSpace-emptySlotNum*wordSize, "Push argument " + param + " onto stack"); + varSlotMap.put(param, requiredStackSpace-emptySlotNum*wordSize); + emptySlotNum++; + } + for (StackVarInfo var : funcInfo.getLocals()) { - // store at requiredStackSpace-emptySlotNum*wordSize(SP), then emptySlotNum++ Literal varLitral = var.getInitialValue(); varLitral.dispatch(stmtAnalyzer); // All Literals should save locations for the values in A0 backend.emitSW(A0, SP, requiredStackSpace-emptySlotNum*wordSize, "Push local variable " + var.getVarName() + " onto stack"); + varSlotMap.put(var.getVarName(), requiredStackSpace-emptySlotNum*wordSize); emptySlotNum++; } @@ -142,7 +154,6 @@ public class CodeGenImpl extends CodeGenBase // example: 0(fp) is the last variable (z) while 8(fp) is the first variable (x) // for function with 3 params f(x, y, z) for (Stmt stmt : funcInfo.getStatements()) { - //System.out.println(stmt.toString()); stmt.dispatch(stmtAnalyzer); } backend.emitJ(stmtAnalyzer.epilogue, "Jump to function epilogue"); @@ -200,6 +211,11 @@ public class CodeGenImpl extends CodeGenBase /** Variable to keep track of offsets of stored variables */ private Map offsetMap = new HashMap<>(); + + /** Variable to keep track of slots of stored variables on stack */ + // example usage: backend.emitLW(A0, SP, varSlotMap.get(varName)) to load the variable using its name + private Map varSlotMap = new HashMap<>(); + private final String size_label; /** Variable to store offset from frame pointer to identify next * empty space on stack frame to store variable*/ @@ -247,7 +263,10 @@ public class CodeGenImpl extends CodeGenBase // *********** functions start *********** public Register analyze(CallExpr node) { + System.out.println("Inside CallExpr: " + node.function.name); backend.emitLW(T6, FP, 0, "Inside CallExpr: " + node.function.name); + + // FIXME: Why are we using globalSymbols here but not the sym??? SymbolInfo Ty = globalSymbols.get(node.function.name); if(Ty instanceof ClassInfo){ //object create @@ -281,6 +300,7 @@ public class CodeGenImpl extends CodeGenBase } else { // function Identifier functionId = node.function; + FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name); List args = node.args; int spaceRequiredForArgs = (args.size() + 1)*4; @@ -290,12 +310,27 @@ public class CodeGenImpl extends CodeGenBase int slotNum = argNum + 1; // We have extra slot for static link Expr expr = args.get(i); expr.dispatch(this); - // FIXME: Box primitives here + 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(makeintLabel, "Box integer"); + } else { + // FIXME: passed argument does not match formal parameter + } + } else if (expr.getInferredType().equals(Type.BOOL_TYPE)) { + if (formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.BOOL_TYPE)) { + backend.emitJAL(makeboolLabel, "Box boolean"); + } else { + // FIXME: passed argument does not match formal parameter + } + } // All expressions should save their end result in A0 - // So, once expr is evaluated add value inside A0 onto stack as an argument - backend.emitSW(A0, SP, -wordSize*slotNum, "Push argument " + argNum + " from left"); + // So, once expr is evaluated add value inside A0 onto stack as an actual argument + backend.emitSW(A0, SP, -wordSize*slotNum, "Push actual argument for " + formalParamName + " onto stack"); } backend.emitADDI(SP, SP, -spaceRequiredForArgs, "Set SP to last argument."); @@ -306,6 +341,7 @@ public class CodeGenImpl extends CodeGenBase } public Register analyze(MethodCallExpr node) { + System.out.println("Inside MethodCallExpr: " + node.method.member.name); backend.emitLW(T6, FP, 0, "Inside MethodCallExpr: " + node.method.member.name); Register obj = node.method.object.dispatch(this); int n_args = node.args.size(); @@ -338,6 +374,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(ReturnStmt stmt) { + System.out.println("Inside ReturnStmt: "); backend.emitLW(T6, FP, 0, "Inside ReturnStmt: "); Expr expr = stmt.value; if (expr == null) { @@ -347,7 +384,7 @@ public class CodeGenImpl extends CodeGenBase // All expressions should save their end result in A0 expr.dispatch(this); - + backend.emitJ(this.epilogue, "Jump to function epilogue"); return A0; } @@ -358,6 +395,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(NoneLiteral node) { + System.out.println("Inside NoneLiteral: "); backend.emitLW(T6, FP, 0, "Inside NoneLiteral: "); backend.emitMV(Register.A0, Register.ZERO, "Load none"); return Register.A0; @@ -366,6 +404,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(StringLiteral node) { + System.out.println("Inside StringLiteral: "); backend.emitLW(T6, FP, 0, "Inside StringLiteral: "); Label l = constants.getStrConstant(node.value); backend.emitLA(Register.A0, l, "Load string literal"); @@ -375,6 +414,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(BooleanLiteral node) { + System.out.println("Inside BooleanLiteral: "); backend.emitLW(T6, FP, 0, "Inside BooleanLiteral: "); if(node.value==true) backend.emitLI(Register.A0, 1, "Load boolean literal: true "); @@ -387,6 +427,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(AssignStmt node) { + System.out.println("Inside AssignStmt: "); backend.emitLW(T6, FP, 0, "Inside AssignStmt: "); Type t = node.value.getInferredType(); // if(t.isSpecialType() || t.isListType()) @@ -426,6 +467,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(ExprStmt node) { + System.out.println("Inside ExprStmt: "); backend.emitLW(T6, FP, 0, "Inside ExprStmt: "); node.expr.dispatch(this); return null; @@ -434,6 +476,8 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(IfExpr node) { + + System.out.println("Inside IfExpr: "); Register result = node.condition.dispatch(this); Label ln = generateLocalLabel(); backend.emitBEQZ(result, ln,"Jump to beginning of loop"); @@ -448,7 +492,10 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(IfStmt node) { + + System.out.println("Inside IfStmt: "); Register result = node.condition.dispatch(this); + Label ln = generateLocalLabel(); backend.emitBEQZ(result, ln,"Jump to beginning of loop"); for(Stmt s:node.thenBody) @@ -527,6 +574,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(BinaryExpr node) { + System.out.println("Inside BinaryExpr: "); String operator = node.operator; backend.emitLW(T6, FP, 0, "Inside BinaryExpr: "); @@ -737,13 +785,16 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(WhileStmt node) { + System.out.println("Inside WhileStmt: "); Label startLoop = generateLocalLabel(); backend.emitLocalLabel(startLoop, "Beginning of while loop"); Register result = node.condition.dispatch(this); Label endLoop = elseBlock; - backend.emitBEQZ(result, endLoop,"Jump to beginning of loop"); - for(Stmt stmt:node.body) + + 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; @@ -778,9 +829,10 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(IntegerLiteral node) { + System.out.println("Inside IntegerLiteral: " + node.value); backend.emitLW(T6, FP, 0, "Inside IntegerLiteral: " + node.value); backend.emitLI(A0, node.value, "Load integer literal " + node.value); - //backend.emitJAL(makeintLabel, "Box integer"); + return A0; }