From 2db4a635f83939006e29a938796ab0c22fba1a87 Mon Sep 17 00:00:00 2001 From: Apoorva Ranade Date: Thu, 6 May 2021 00:22:48 +0530 Subject: [PATCH] Clean up --- src/main/java/chocopy/pa3/CodeGenImpl.java | 82 +++------------------- 1 file changed, 10 insertions(+), 72 deletions(-) diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 0269d7e..340a1f8 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -349,7 +349,8 @@ public class CodeGenImpl extends CodeGenBase } else if (expr.getInferredType().equals(Type.BOOL_TYPE)) { if ((functionId.name.equals("print"))&&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.BOOL_TYPE))) { backend.emitJAL(makeboolLabel, "Box boolean"); - } else { + } + else { // FIXME: passed argument does not match formal parameter } } @@ -434,7 +435,7 @@ public class CodeGenImpl extends CodeGenBase @Override public Register analyze(StringLiteral node) { - System.out.println("Inside StringLiteral: "); + 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"); @@ -605,70 +606,7 @@ public class CodeGenImpl extends CodeGenBase backend.emitLocalLabel(ln, "End of if statement"); return null; } - public void copyList(Register dest, Register src, Register size) { - Label begin = generateLocalLabel(); - Label exit = generateLocalLabel(); - backend.emitLocalLabel(begin, "Start loop for copying"); - backend.emitBEQ(size, Register.ZERO, exit, "Exit when copy completes"); - backend.emitLW(Register.A2, src, 0, "Load Word from Source"); - backend.emitSW(Register.A2, dest, 0, "Store Word in Destination"); - backend.emitADDI(src, src, wordSize, "Increment Source Address"); - backend.emitADDI(dest, dest, wordSize, "Increment Destination Address"); - backend.emitADDI(size, size, -1, "Decrement size left to copy"); - backend.emitJ(begin, "Jump to beginning of loop"); - backend.emitLocalLabel(exit,"Exit loop for copying"); - } - private Register addLists() { - // Check for Null operands - Label label = generateLocalLabel(); - backend.emitBNEZ(Register.A0, label, "Left operand is NULL"); - backend.emitBNEZ(Register.T0, label, "Right operand is NULL"); - backend.emitJAL(errorNone, "Operand is Null"); - backend.emitLocalLabel(label, "List Addition"); - - //Get lengths of the two lists - backend.emitLW(Register.T1, Register.A0,0,"Load length of first list"); - backend.emitLW(Register.T2, Register.T0,0,"Load length of second list"); - backend.emitADD(Register.A1, Register.T2,Register.T1,"Add lengths of lists"); - backend.emitMV(Register.T3,Register.A1,"Store total length combined list"); - backend.emitADDI(Register.A1, Register.A1,1,"Add 1 to store length of new list"); - - //Store address and length of lists on stack - backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Store address of first list"); - incSp(1); - backend.emitSW(Register.T0, Register.FP, -sp_off*wordSize, "Store address of second list"); - incSp(1); - backend.emitSW(Register.T3, Register.FP, -sp_off*wordSize, "Store length of combined list"); - incSp(1); - - //Allocate space on heap - backend.emitLA(Register.A0, listClass.getPrototypeLabel(), "Load empty list"); - backend.emitJAL(objectAllocResizeLabel, "Allocate list"); - backend.emitMV(Register.A1, Register.A0, "Store address of allocated space"); - backend.emitMV(Register.T5, Register.A1, "Make a copy of address of allocated space"); - - //Pop length and address from stack - sp_off--; - backend.emitLW(Register.T3, Register.FP, -sp_off*wordSize, "Load length of combined list"); - sp_off--; - backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Load address of second list"); - sp_off--; - backend.emitLW(Register.A0, Register.FP, -sp_off*wordSize, "Load address of first list"); - - backend.emitLW(Register.T1, Register.A0,0,"Load length of first list"); - backend.emitLW(Register.T2, Register.T0,0,"Load length of second list"); - - //Copy each list in newly allocated space - backend.emitSW(Register.T3,Register.T5,0,"Store length of combined list "); - backend.emitADDI(Register.T5,Register.T5,wordSize,"Increment address"); - backend.emitADDI(Register.A0,Register.A0,wordSize,"Increment address"); - copyList(Register.T5, Register.A0, Register.T1); - backend.emitADDI(Register.T0,Register.T0,wordSize,"Increment address"); - copyList(Register.T5, Register.T0, Register.T2); - - backend.emitMV(Register.A0,Register.A1,"Load address of combined list"); - return Register.A0; - } + @Override public Register analyze(BinaryExpr node) { @@ -751,7 +689,6 @@ public class CodeGenImpl extends CodeGenBase { // Comparison operators - String comment="Operator: "+operator; if(operator.equals("==")) { node.right.dispatch(this); @@ -907,7 +844,7 @@ public class CodeGenImpl extends CodeGenBase public Register analyze(Identifier node) { //backend.emitLW(T6, FP, 0, "Inside Identifier: "); - + System.out.println(node.getInferredType()); Identifier targetID = (Identifier)node; Object var = getVar(targetID); if(var instanceof StackVarRuntimeInfo){ @@ -1076,12 +1013,13 @@ public class CodeGenImpl extends CodeGenBase 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 string location from stack"); + sp_off--; + backend.emitLW(iter, Register.FP, -(sp_off)*wordSize, "Load index from stack"); + backend.emitLW(l, Register.FP, -(sp_off-1)*wordSize, "Store 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(iter, iter, 1, "Increment counter"); - backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack"); + backend.emitADDI(iden, iter, 1, "Increment counter"); + backend.emitSW(iden, Register.FP, -sp_off*wordSize, "Store index on stack"); incSp(1); backend.emitADDI(iden, iter, 4 * wordSize, "Convert index to offset to char in bytes"); backend.emitADD(iden, l, iden, "Get pointer to char");