You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ChocoPy/src/main/java/chocopy/pa3/CodeGenImpl.java

459 lines
17 KiB

package chocopy.pa3;
import static chocopy.common.codegen.RiscVBackend.Register.A0;
import static chocopy.common.codegen.RiscVBackend.Register.A1;
import static chocopy.common.codegen.RiscVBackend.Register.FP;
import static chocopy.common.codegen.RiscVBackend.Register.RA;
import static chocopy.common.codegen.RiscVBackend.Register.SP;
import static chocopy.common.codegen.RiscVBackend.Register.ZERO;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import chocopy.common.analysis.AbstractNodeAnalyzer;
import chocopy.common.analysis.SymbolTable;
import chocopy.common.analysis.types.Type;
import chocopy.common.astnodes.AssignStmt;
import chocopy.common.astnodes.BinaryExpr;
import chocopy.common.astnodes.BooleanLiteral;
import chocopy.common.astnodes.Expr;
import chocopy.common.astnodes.ExprStmt;
import chocopy.common.astnodes.ForStmt;
import chocopy.common.astnodes.Identifier;
import chocopy.common.astnodes.IfExpr;
import chocopy.common.astnodes.IfStmt;
import chocopy.common.astnodes.IndexExpr;
import chocopy.common.astnodes.IntegerLiteral;
import chocopy.common.astnodes.ListExpr;
import chocopy.common.astnodes.ListType;
import chocopy.common.astnodes.NoneLiteral;
import chocopy.common.astnodes.ReturnStmt;
import chocopy.common.astnodes.Stmt;
import chocopy.common.astnodes.StringLiteral;
import chocopy.common.astnodes.TypedVar;
import chocopy.common.astnodes.UnaryExpr;
import chocopy.common.astnodes.VarDef;
import chocopy.common.astnodes.WhileStmt;
import chocopy.common.codegen.CodeGenBase;
import chocopy.common.codegen.FuncInfo;
import chocopy.common.codegen.GlobalVarInfo;
import chocopy.common.codegen.Label;
import chocopy.common.codegen.RiscVBackend;
import chocopy.common.codegen.StackVarInfo;
import chocopy.common.codegen.RiscVBackend.Register;
import chocopy.common.codegen.SymbolInfo;
/**
* This is where the main implementation of PA3 will live.
*
* <p>
* A large part of the functionality has already been implemented in the base
* class, CodeGenBase. Make sure to read through that class, since you will want
* to use many of its fields and utility methods in this class when emitting
* code.
*
* <p>
* Also read the PDF spec for details on what the base class does and what APIs
* it exposes for its sub-class (this one). Of particular importance is knowing
* what all the SymbolInfo classes contain.
*/
public class CodeGenImpl extends CodeGenBase
{
/** A code generator emitting instructions to BACKEND. */
public CodeGenImpl(RiscVBackend backend)
{
super(backend);
}
/** Operation on None. */
private final Label errorNone = new Label("error.None");
/** Division by zero. */
private final Label errorDiv = new Label("error.Div");
/** Index out of bounds. */
private final Label errorOob = new Label("error.OOB");
/**
* Emits the top level of the program.
*
* <p>
* This method is invoked exactly once, and is surrounded by some boilerplate
* code that: (1) initializes the heap before the top-level begins and (2) exits
* after the top-level ends.
*
* <p>
* You only need to generate code for statements.
*
* @param statements top level statements
*/
protected void emitTopLevel(List<Stmt> statements)
{
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(null);
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, 4, "Top saved RA is 0.");
backend.emitADDI(FP, SP, 2 * backend.getWordSize(), "Set FP to previous SP.");
for (Stmt stmt : statements)
{
stmt.dispatch(stmtAnalyzer);
}
backend.emitLI(A0, EXIT_ECALL, "Code for ecall: exit");
backend.emitEcall(null);
}
/**
* Emits the code for a function described by FUNCINFO.
*
* <p>
* This method is invoked once per function and method definition. At the code
* generation stage, nested functions are emitted as separate functions of their
* 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`.
*/
protected void emitUserDefinedFunction(FuncInfo funcInfo)
{
backend.emitGlobalLabel(funcInfo.getCodeLabel());
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
for (Stmt stmt : funcInfo.getStatements())
{
stmt.dispatch(stmtAnalyzer);
}
backend.emitMV(A0, ZERO, "Returning None implicitly");
backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue");
// FIXME: {... reset fp etc. ...}
backend.emitJR(RA, "Return to caller");
}
/** An analyzer that encapsulates code generation for statements. */
private class StmtAnalyzer extends AbstractNodeAnalyzer<Void>
{
/*
* 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
* x = sym.get("x");
*
* A SymbolInfo can be one the following: - ClassInfo: a descriptor for classes
* - FuncInfo: a descriptor for functions/methods - AttrInfo: a descriptor for
* attributes - GlobalVarInfo: a descriptor for global variables - StackVarInfo:
* a descriptor for variables allocated on the stack, such as locals and
* parameters
*
* Since the input program is assumed to be semantically valid and well-typed at
* this stage, you can always assume that the symbol table contains valid
* information. For example, in an expression `foo()` you KNOW that
* sym.get("foo") will either be a FuncInfo or ClassInfo, but not any of the
* other infos and never null.
*
* The symbol table in funcInfo has already been populated in the base class:
* CodeGenBase. You do not need to add anything to the symbol table. Simply
* query it with an identifier name to get a descriptor for a function, class,
* variable, etc.
*
* The symbol table also maps nonlocal and global vars, so you only need to
* lookup one symbol table and it will fetch the appropriate info for the var
* that is currently in scope.
*/
/** Symbol table for my statements. */
private final SymbolTable<SymbolInfo> sym;
/** Label of code that exits from procedure. */
protected final Label epilogue;
/** The descriptor for the current function, or null at the top level. */
private final FuncInfo funcInfo;
/** Label of code that exits from block. */
protected Label elseBlock;
/** Variable to store offset from frame pointer to identify next
* empty space on stack frame to store variable*/
private int offset = 3;
private Map<SymbolInfo, Integer> offsetMap = new HashMap<>();
/**
* An analyzer for the function described by FUNCINFO0, which is null for the
* top level.
*/
StmtAnalyzer(FuncInfo funcInfo0)
{
funcInfo = funcInfo0;
if (funcInfo == null) {
sym = globalSymbols;
} else {
sym = funcInfo.getSymbolTable();
}
epilogue = generateLocalLabel();
}
// FIXME: Example of statement.
@Override
public Void analyze(ReturnStmt stmt)
{
// FIXME: Here, we emit an instruction that does nothing. Clearly,
// this is wrong, and you'll have to fix it.
// This is here just to demonstrate how to emit a
// RISC-V instruction.
backend.emitMV(ZERO, ZERO, "No-op");
return null;
}
@Override
public Void analyze(NoneLiteral node)
{
backend.emitMV(Register.A0, Register.ZERO, "Load none");
return null;
}
@Override
public Void analyze(StringLiteral node)
{
Label l = constants.getStrConstant(node.value);
backend.emitLA(Register.A0, l, "Load string literal");
return null;
}
@Override
public Void analyze(IntegerLiteral node)
{
backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value);
return null;
}
@Override
public Void analyze(BooleanLiteral node)
{
if(node.value==true)
backend.emitLI(Register.A0, 1, "Load boolean literal: true ");
else
backend.emitLI(Register.A0, 0, "Load boolean literal: false ");
return null;
}
@Override
public Void analyze(AssignStmt node)
{
Type t = node.value.getInferredType();
if(t.isSpecialType())
{
node.value.dispatch(this);
if (sym.getParent() == null)
{
for(Expr target: node.targets)
{
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)");
}
}
else
{
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
}
return null;
}
@Override
public Void analyze(ExprStmt node)
{
node.expr.dispatch(this);
return null;
}
@Override
public Void analyze(IfExpr node)
{
node.condition.dispatch(this);
Label ln = generateLocalLabel();
node.thenExpr.dispatch(this);
backend.emitJ(ln, "Jump to end of if expression");
backend.emitLocalLabel(elseBlock, "Else part of if expression");
node.elseExpr.dispatch(this);
backend.emitLocalLabel(ln, "End of if expression");
return null;
}
@Override
public Void analyze(IfStmt node)
{
node.condition.dispatch(this);
Label ln = generateLocalLabel();
for(Stmt s:node.thenBody)
s.dispatch(this);
backend.emitJ(ln, "Jump to end of if statement");
backend.emitLocalLabel(elseBlock, "Else part of if statement");
for(Stmt s:node.elseBody)
s.dispatch(this);
backend.emitLocalLabel(ln, "End of if statement");
return null;
}
@Override
public Void analyze(BinaryExpr node)
{
node.left.dispatch(this);
backend.emitSW(Register.A0, Register.FP, offset*4, "Push on stack slot "+offset);
offset++;
node.right.dispatch(this);
offset--;
backend.emitLW(Register.T0, Register.FP, (offset)*4, "Pop stack slot "+offset);
// Arithmetic Operators
if(node.operator.equals("+"))
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
else if(node.operator.equals("-"))
backend.emitSUB(Register.A0, Register.A0, Register.T0, "Sub operation");
else if(node.operator.equals("*"))
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Mul operation");
else if(node.operator.equals("/"))
backend.emitDIV(Register.A0, Register.A0, Register.T0, "Div operation");
else
{ // Comparison operators
elseBlock = generateLocalLabel();
String comment="Branch on not "+node.operator;
if(node.operator.equals("=="))
backend.emitBNE(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals("!="))
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals("<"))
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals(">"))
{
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
}
else if(node.operator.equals(">="))
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
else if(node.operator.equals("<="))
{
Label temp = generateLocalLabel();
backend.emitBEQ(Register.A0, Register.T0,temp, "Branch on "+node.operator);
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
backend.emitLocalLabel(temp, "True part of if check");
}
}
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
public Void analyze(Identifier 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
public Void analyze(VarDef node)
{
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);
}
@Override
public Void analyze(IndexExpr node) {
// statement
return defaultAction(node);
}
@Override
public Void analyze(ListExpr node) {
// statement
return defaultAction(node);
}
@Override
public Void analyze(ListType node) {
// statement
return defaultAction(node);
}
@Override
public Void analyze(WhileStmt node) {
// control flow
return defaultAction(node);
}
}
/**
* Emits custom code in the CODE segment.
*
* <p>This method is called after emitting the top level and the function bodies for each
* function.
*
* <p>You can use this method to emit anything you want outside of the top level or functions,
* e.g. custom routines that you may want to call from within your code to do common tasks. This
* is not strictly needed. You might not modify this at all and still complete the assignment.
*
* <p>To start you off, here is an implementation of three routines that will be commonly needed
* from within the code you will generate for statements.
*
* <p>The routines are error handlers for operations on None, index out of bounds, and division
* by zero. They never return to their caller. Just jump to one of these routines to throw an
* error and exit the program. For example, to throw an OOB error: backend.emitJ(errorOob, "Go
* to out-of-bounds error and abort");
*/
protected void emitCustomCode() {
emitErrorFunc(errorNone, "Operation on None");
emitErrorFunc(errorDiv, "Division by zero");
emitErrorFunc(errorOob, "Index out of bounds");
}
/** Emit an error routine labeled ERRLABEL that aborts with message MSG. */
private void emitErrorFunc(Label errLabel, String msg) {
backend.emitGlobalLabel(errLabel);
backend.emitLI(A0, ERROR_NONE, "Exit code for: " + msg);
backend.emitLA(A1, constants.getStrConstant(msg), "Load error message as str");
backend.emitADDI(
A1, A1, getAttrOffset(strClass, "__str__"), "Load address of attribute __str__");
backend.emitJ(abortLabel, "Abort");
}
}