Boxing integer and booleans done

master
Sanjar Ahmadov 3 years ago
commit 69939f08c3

@ -35,7 +35,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");
@ -116,7 +116,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");
@ -128,12 +128,24 @@ public class CodeGenImpl extends CodeGenBase
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
int emptySlotNum = 4;
// backend.emitLW(A0, SP, varSlotMap.get(varName)) to load the variable using its name
Map<String, Integer> 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++;
}
@ -143,7 +155,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");
@ -201,6 +212,10 @@ public class CodeGenImpl extends CodeGenBase
/** Variable to keep track of offsets of stored variables */
private Map<SymbolInfo, Integer> offsetMap = new HashMap<>();
/** Variable to keep track of slots of stored variables on stack */
private Map<String, Integer> 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*/
@ -239,7 +254,7 @@ public class CodeGenImpl extends CodeGenBase
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
@ -273,6 +288,7 @@ public class CodeGenImpl extends CodeGenBase
} else {
// function
Identifier functionId = node.function;
FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name);
List<Expr> args = node.args;
int spaceRequiredForArgs = (args.size() + 1)*4;
@ -282,12 +298,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.emitJ(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.emitJ(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.");
@ -433,9 +464,11 @@ public class CodeGenImpl extends CodeGenBase
@Override
public Register analyze(IfExpr node)
{
System.out.println("Inside IfExpr: ");
node.condition.dispatch(this);
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel();
backend.emitBEQZ(result, ln,"Jump to beginning of loop");
node.thenExpr.dispatch(this);
backend.emitJ(ln, "Jump to end of if expression");
backend.emitLocalLabel(elseBlock, "Else part of if expression");
@ -447,9 +480,12 @@ public class CodeGenImpl extends CodeGenBase
@Override
public Register analyze(IfStmt node)
{
System.out.println("Inside IfStmt: ");
node.condition.dispatch(this);
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel();
backend.emitBEQZ(result, ln,"Jump to beginning of loop");
for(Stmt s:node.thenBody)
s.dispatch(this);
backend.emitJ(ln, "Jump to end of if statement");
@ -740,15 +776,15 @@ public class CodeGenImpl extends CodeGenBase
System.out.println("Inside WhileStmt: ");
Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop");
node.condition.dispatch(this);
Register result = node.condition.dispatch(this);
Label endLoop = elseBlock;
for(Stmt stmt:node.body) {
System.out.println("Inside WhileStmt: for loop " + stmt.toString());
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");
System.out.println("Inside WhileStmt: Done with while");
return null;
}
@ -785,8 +821,6 @@ public class CodeGenImpl extends CodeGenBase
backend.emitLW(T6, FP, 0, "Inside IntegerLiteral: " + node.value);
backend.emitLI(A0, node.value, "Load integer literal " + node.value);
// FIXME: Remove line below later once you are boxing it in function call
backend.emitJAL(makeintLabel, "Box integer");
return A0;
}

Loading…
Cancel
Save