Changed variable name and fixed indentation

master
Apoorva Ranade 4 years ago
parent aed2114af0
commit 748b42fc53

@ -148,20 +148,20 @@ public class CodeGenImpl extends CodeGenBase
/** Label of code that exits from block. */ /** Label of code that exits from block. */
protected Label elseBlock; protected Label elseBlock;
/** Variable to keep track of offsets of stored variables */
private Map<SymbolInfo, Integer> offsetMap = new HashMap<>();
private final String size_label;
/** Variable to store offset from frame pointer to identify next /** Variable to store offset from frame pointer to identify next
* empty space on stack frame to store variable*/ * empty space on stack frame to store variable*/
private int offset = 3; private int sp_off;
private Map<SymbolInfo, Integer> offsetMap = new HashMap<>(); /** Variable to store maximum possible offset depending on stack size.*/
private int max_sp;
/** /**
* An analyzer for the function described by FUNCINFO0, which is null for the * An analyzer for the function described by FUNCINFO0, which is null for the
* top level. * top level.
*/ */
private final String size_label;
private int sp_off, max_sp;
/** Variable to store offset from frame pointer to identify next
* empty space on stack frame to store variable*/
/** An analyzer for the function described by FUNCINFO0, which is null for the top level. */
StmtAnalyzer(FuncInfo funcInfo0) { StmtAnalyzer(FuncInfo funcInfo0) {
funcInfo = funcInfo0; funcInfo = funcInfo0;
if (funcInfo == null) { if (funcInfo == null) {
@ -314,11 +314,11 @@ public class CodeGenImpl extends CodeGenBase
public Register analyze(BinaryExpr node) public Register analyze(BinaryExpr node)
{ {
node.left.dispatch(this); node.left.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -offset*4, "Push on stack slot "+offset); backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
offset++; sp_off++;
node.right.dispatch(this); node.right.dispatch(this);
offset--; sp_off--;
backend.emitLW(Register.T0, Register.FP, -offset*4, "Pop stack slot "+offset); backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
// Arithmetic Operators // Arithmetic Operators
if(node.operator.equals("+")) if(node.operator.equals("+"))
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation"); backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
@ -361,7 +361,7 @@ public class CodeGenImpl extends CodeGenBase
node.operand.dispatch(this); node.operand.dispatch(this);
if(node.operator.equals("-")) if(node.operator.equals("-"))
{ {
backend.emitLI(Register.T0, -1, "Set value of Registr T0 to -1"); backend.emitLI(Register.T0, -1, "Set value of Register T0 to -1");
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1"); backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1");
} }
return Register.A0; return Register.A0;
@ -388,9 +388,9 @@ public class CodeGenImpl extends CodeGenBase
{ {
StackVarInfo svi = (StackVarInfo) sym.get(node.var.identifier.name); StackVarInfo svi = (StackVarInfo) sym.get(node.var.identifier.name);
node.value.dispatch(this); node.value.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -offset*4, "Store variable "+node.var.identifier.name+" value in Stack"); backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Store variable "+node.var.identifier.name+" value in Stack");
offsetMap.put(svi, offset); offsetMap.put(svi, sp_off);
offset++; sp_off++;
return null; return null;
} }
@ -414,14 +414,14 @@ public class CodeGenImpl extends CodeGenBase
for(Expr exp:node.elements) for(Expr exp:node.elements)
{ {
Register r = exp.dispatch(this); Register r = exp.dispatch(this);
backend.emitSW(r,Register.FP,-offset*4,"Push argument "+i+" from last."); backend.emitSW(r,Register.FP,-sp_off*wordSize,"Push argument "+i+" from last.");
offset++; sp_off++;
i--; i--;
} }
backend.emitLI(Register.A0, l, "Pass list length"); backend.emitLI(Register.A0, l, "Pass list length");
backend.emitSW(Register.A0, Register.FP, -offset*4, "Push argument "+i+" from last."); backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push argument "+i+" from last.");
offset++; sp_off++;
backend.emitADDI(Register.SP, Register.SP, -offset*4, "Set SP to last argument."); backend.emitADDI(Register.SP, Register.SP, -sp_off*wordSize, "Set SP to last argument.");
//TODO: Store reference to variable //TODO: Store reference to variable
return Register.A0; return Register.A0;
} }
@ -445,13 +445,15 @@ public class CodeGenImpl extends CodeGenBase
@Override @Override
public Register analyze(IndexExpr node) { public Register analyze(IndexExpr node)
{
System.out.println(node); System.out.println(node);
return defaultAction(node); return defaultAction(node);
} }
public Register analyze(MemberExpr node) { public Register analyze(MemberExpr node)
{
ClassInfo objectClass = (ClassInfo) globalSymbols.get(node.object.getInferredType().className()); ClassInfo objectClass = (ClassInfo) globalSymbols.get(node.object.getInferredType().className());
Label label = generateLocalLabel(); Label label = generateLocalLabel();
Register obj = node.object.dispatch(this); Register obj = node.object.dispatch(this);
@ -464,7 +466,8 @@ public class CodeGenImpl extends CodeGenBase
return A0; return A0;
} }
public Register analyze(MethodCallExpr node) { public Register analyze(MethodCallExpr node)
{
Register obj = node.method.object.dispatch(this); Register obj = node.method.object.dispatch(this);
int n_args = node.args.size(); int n_args = node.args.size();
@ -495,13 +498,10 @@ public class CodeGenImpl extends CodeGenBase
public Register analyze(ReturnStmt node) { public Register analyze(ReturnStmt node)
{
return null; return null;
} }
// FIXME: More, of course.
} }

Loading…
Cancel
Save