Variable declarations and assignment

master
Apoorva Ranade 4 years ago
parent c64ee11534
commit f92308238b

@ -7,7 +7,9 @@ import static chocopy.common.codegen.RiscVBackend.Register.RA;
import static chocopy.common.codegen.RiscVBackend.Register.SP; import static chocopy.common.codegen.RiscVBackend.Register.SP;
import static chocopy.common.codegen.RiscVBackend.Register.ZERO; import static chocopy.common.codegen.RiscVBackend.Register.ZERO;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import chocopy.common.analysis.AbstractNodeAnalyzer; import chocopy.common.analysis.AbstractNodeAnalyzer;
import chocopy.common.analysis.SymbolTable; import chocopy.common.analysis.SymbolTable;
@ -38,6 +40,7 @@ import chocopy.common.codegen.FuncInfo;
import chocopy.common.codegen.GlobalVarInfo; import chocopy.common.codegen.GlobalVarInfo;
import chocopy.common.codegen.Label; import chocopy.common.codegen.Label;
import chocopy.common.codegen.RiscVBackend; import chocopy.common.codegen.RiscVBackend;
import chocopy.common.codegen.StackVarInfo;
import chocopy.common.codegen.RiscVBackend.Register; import chocopy.common.codegen.RiscVBackend.Register;
import chocopy.common.codegen.SymbolInfo; import chocopy.common.codegen.SymbolInfo;
@ -55,10 +58,12 @@ import chocopy.common.codegen.SymbolInfo;
* it exposes for its sub-class (this one). Of particular importance is knowing * it exposes for its sub-class (this one). Of particular importance is knowing
* what all the SymbolInfo classes contain. * what all the SymbolInfo classes contain.
*/ */
public class CodeGenImpl extends CodeGenBase { public class CodeGenImpl extends CodeGenBase
{
/** A code generator emitting instructions to BACKEND. */ /** A code generator emitting instructions to BACKEND. */
public CodeGenImpl(RiscVBackend backend) { public CodeGenImpl(RiscVBackend backend)
{
super(backend); super(backend);
} }
@ -82,14 +87,16 @@ public class CodeGenImpl extends CodeGenBase {
* *
* @param statements top level statements * @param statements top level statements
*/ */
protected void emitTopLevel(List<Stmt> statements) { protected void emitTopLevel(List<Stmt> statements)
{
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(null); StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(null);
backend.emitADDI(SP, SP, -2 * backend.getWordSize(), "Saved FP and saved RA (unused at top level)."); backend.emitADDI(SP, SP, -2 * backend.getWordSize(), "Saved FP and saved RA (unused at top level).");
backend.emitSW(ZERO, SP, 0, "Top saved FP is 0."); backend.emitSW(ZERO, SP, 0, "Top saved FP is 0.");
backend.emitSW(ZERO, SP, 4, "Top saved RA is 0."); backend.emitSW(ZERO, SP, 4, "Top saved RA is 0.");
backend.emitADDI(FP, SP, 2 * backend.getWordSize(), "Set FP to previous SP."); backend.emitADDI(FP, SP, 2 * backend.getWordSize(), "Set FP to previous SP.");
for (Stmt stmt : statements) { for (Stmt stmt : statements)
{
stmt.dispatch(stmtAnalyzer); stmt.dispatch(stmtAnalyzer);
} }
backend.emitLI(A0, EXIT_ECALL, "Code for ecall: exit"); backend.emitLI(A0, EXIT_ECALL, "Code for ecall: exit");
@ -105,11 +112,13 @@ public class CodeGenImpl extends CodeGenBase {
* own. So if function `bar` is nested within function `foo`, you only emit * 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`. * `foo`'s code for `foo` and only emit `bar`'s code for `bar`.
*/ */
protected void emitUserDefinedFunction(FuncInfo funcInfo) { protected void emitUserDefinedFunction(FuncInfo funcInfo)
{
backend.emitGlobalLabel(funcInfo.getCodeLabel()); backend.emitGlobalLabel(funcInfo.getCodeLabel());
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo); StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
for (Stmt stmt : funcInfo.getStatements()) { for (Stmt stmt : funcInfo.getStatements())
{
stmt.dispatch(stmtAnalyzer); stmt.dispatch(stmtAnalyzer);
} }
@ -121,7 +130,8 @@ public class CodeGenImpl extends CodeGenBase {
} }
/** An analyzer that encapsulates code generation for statements. */ /** An analyzer that encapsulates code generation for statements. */
private class StmtAnalyzer extends AbstractNodeAnalyzer<Void> { private class StmtAnalyzer extends AbstractNodeAnalyzer<Void>
{
/* /*
* The symbol table has all the info you need to determine what a given * 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 * identifier 'x' in the current scope is. You can use it as follows: SymbolInfo
@ -160,14 +170,16 @@ 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 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 offset = 3;
private Map<SymbolInfo, Integer> offsetMap = new HashMap<>();
/** /**
* 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.
*/ */
StmtAnalyzer(FuncInfo funcInfo0) { StmtAnalyzer(FuncInfo funcInfo0)
{
funcInfo = funcInfo0; funcInfo = funcInfo0;
if (funcInfo == null) { if (funcInfo == null) {
sym = globalSymbols; sym = globalSymbols;
@ -179,7 +191,8 @@ public class CodeGenImpl extends CodeGenBase {
// FIXME: Example of statement. // FIXME: Example of statement.
@Override @Override
public Void analyze(ReturnStmt stmt) { public Void analyze(ReturnStmt stmt)
{
// FIXME: Here, we emit an instruction that does nothing. Clearly, // FIXME: Here, we emit an instruction that does nothing. Clearly,
// this is wrong, and you'll have to fix it. // this is wrong, and you'll have to fix it.
// This is here just to demonstrate how to emit a // This is here just to demonstrate how to emit a
@ -189,18 +202,21 @@ public class CodeGenImpl extends CodeGenBase {
} }
@Override @Override
public Void analyze(NoneLiteral node) { public Void analyze(NoneLiteral node)
{
backend.emitMV(Register.A0, Register.ZERO, "Load none"); backend.emitMV(Register.A0, Register.ZERO, "Load none");
return null; return null;
} }
@Override @Override
public Void analyze(StringLiteral node) { public Void analyze(StringLiteral node)
{
Label l = constants.getStrConstant(node.value); Label l = constants.getStrConstant(node.value);
backend.emitLA(Register.A0, l, "Load string literal"); backend.emitLA(Register.A0, l, "Load string literal");
return null; return null;
} }
@Override @Override
public Void analyze(IntegerLiteral node) { public Void analyze(IntegerLiteral node)
{
backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value); backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value);
return null; return null;
} }
@ -216,38 +232,44 @@ public class CodeGenImpl extends CodeGenBase {
@Override @Override
public Void analyze(AssignStmt node) public Void analyze(AssignStmt node)
{ {
Type t = node.value.getInferredType();
if(t.isSpecialType())
{
node.value.dispatch(this);
if (sym.getParent() == null) if (sym.getParent() == null)
{ {
Type t = node.value.getInferredType();
for(Expr target: node.targets) for(Expr target: node.targets)
{
if(t.isSpecialType())
{ {
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name); GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
node.value.dispatch(this);
backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)"); backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)");
} }
} }
}
else else
{/* TODO: Assignments inside functions {
for(Expr target: node.targets) for(Expr target: node.targets)
{ {
StackVarInfo svi = (StackVarInfo) sym.get(((Identifier)target).name);
int loc = offsetMap.get(svi);
backend.emitSW(Register.A0, Register.FP, loc*4, "Load local variable: "+svi.getVarName());
}
}
}
else
{//TODO: Object Assignment
backend.emitGlobalLabel("$"+target.kind);
backend.emitWordLiteral(node.value);
}*/
} }
return null; return null;
} }
@Override @Override
public Void analyze(ExprStmt node) { public Void analyze(ExprStmt node)
{
node.expr.dispatch(this); node.expr.dispatch(this);
return null; return null;
} }
@Override @Override
public Void analyze(IfExpr node) { public Void analyze(IfExpr node)
{
node.condition.dispatch(this); node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
node.thenExpr.dispatch(this); node.thenExpr.dispatch(this);
@ -258,7 +280,8 @@ public class CodeGenImpl extends CodeGenBase {
return null; return null;
} }
@Override @Override
public Void analyze(IfStmt node) { public Void analyze(IfStmt node)
{
node.condition.dispatch(this); node.condition.dispatch(this);
Label ln = generateLocalLabel(); Label ln = generateLocalLabel();
for(Stmt s:node.thenBody) for(Stmt s:node.thenBody)
@ -271,13 +294,15 @@ public class CodeGenImpl extends CodeGenBase {
return null; return null;
} }
@Override @Override
public Void analyze(BinaryExpr node) { public Void 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, offset*4, "Push on stack slot "+offset);
offset++; offset++;
node.right.dispatch(this); node.right.dispatch(this);
offset--; offset--;
backend.emitLW(Register.T0, Register.FP, (offset)*4, "Pop stack slot "+offset); backend.emitLW(Register.T0, Register.FP, (offset)*4, "Pop stack slot "+offset);
// 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");
else if(node.operator.equals("-")) else if(node.operator.equals("-"))
@ -287,7 +312,7 @@ public class CodeGenImpl extends CodeGenBase {
else if(node.operator.equals("/")) else if(node.operator.equals("/"))
backend.emitDIV(Register.A0, Register.A0, Register.T0, "Div operation"); backend.emitDIV(Register.A0, Register.A0, Register.T0, "Div operation");
else else
{ { // Comparison operators
elseBlock = generateLocalLabel(); elseBlock = generateLocalLabel();
String comment="Branch on not "+node.operator; String comment="Branch on not "+node.operator;
if(node.operator.equals("==")) if(node.operator.equals("=="))
@ -313,19 +338,58 @@ public class CodeGenImpl extends CodeGenBase {
} }
return null; return null;
} }
@Override
public Void analyze(UnaryExpr node)
{
node.operand.dispatch(this);
if(node.operator.equals("-"))
{
backend.emitLI(Register.T0, -1, "Set value of Registr T0 to -1");
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1");
}
return null;
}
@Override @Override
public Void analyze(ForStmt node) { public Void analyze(Identifier node)
// control flow {
return defaultAction(node); if (sym.getParent() == null)
{
GlobalVarInfo gvi=(GlobalVarInfo) sym.get(node.name);
backend.emitLW(Register.A0, gvi.getLabel(), "Load global: "+gvi.getVarName());
}
else
{
StackVarInfo svi = (StackVarInfo) sym.get(node.name);
int loc = offsetMap.get(svi);
backend.emitLW(Register.A0, Register.FP, loc*4, "Load local variable: "+svi.getVarName());
}
return null;
} }
@Override @Override
public Void analyze(Identifier node) { public Void analyze(VarDef node)
// statement {
StackVarInfo svi = (StackVarInfo) sym.get(node.var.identifier.name);
node.value.dispatch(this);
backend.emitSW(Register.A0, Register.FP, offset*4, "Store variable "+node.var.identifier.name+" value in Stack");
offsetMap.put(svi, offset);
offset++;
return null;
}
@Override
public Void analyze(TypedVar node)
{
return null;
}
@Override
public Void analyze(ForStmt node) {
// control flow
return defaultAction(node); return defaultAction(node);
} }
@Override @Override
public Void analyze(IndexExpr node) { public Void analyze(IndexExpr node) {
// statement // statement
@ -345,23 +409,11 @@ public class CodeGenImpl extends CodeGenBase {
return defaultAction(node); return defaultAction(node);
} }
@Override
public Void analyze(TypedVar node) {
// statement
return defaultAction(node);
}
@Override
public Void analyze(UnaryExpr node) {
// statement
return defaultAction(node);
}
@Override
public Void analyze(VarDef node) {
// statement
return defaultAction(node);
}
@Override @Override
public Void analyze(WhileStmt node) { public Void analyze(WhileStmt node) {

Loading…
Cancel
Save