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

736 lines
33 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.astnodes.*;
import chocopy.common.analysis.types.*;
import chocopy.common.codegen.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import chocopy.common.codegen.RiscVBackend.Register;
/**
* 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");
/** Not implemented. */
private final Label errorNI = new Label("error.NI");
/**
* 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<Register> {
/*
* 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 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
* empty space on stack frame to store variable*/
private int sp_off;
/** 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
* top level.
*/
StmtAnalyzer(FuncInfo funcInfo0) {
funcInfo = funcInfo0;
if (funcInfo == null) {
sym = globalSymbols;
sp_off = max_sp = 2;
size_label = "@..main.size";
} else {
sym = funcInfo.getSymbolTable();
sp_off = max_sp = funcInfo0.getLocals().size() + 2;
size_label = "@"+funcInfo0.getFuncName()+".size";
}
epilogue = generateLocalLabel();
}
private void incSp(int i){
sp_off+=i+1;
max_sp = max_sp >= sp_off?max_sp:sp_off;
}
public Register analyze(CallExpr node) {
SymbolInfo Ty = globalSymbols.get(node.function.name);
if(Ty instanceof ClassInfo){
//object create
ClassInfo cls = (ClassInfo) Ty;
/**
la a0, $DoublingVector$prototype # Load pointer to prototype of: DoublingVector
jal alloc # Allocate new object in A0
sw a0, -12(fp) # Push on stack slot 3
sw a0, -16(fp) # Push argument 0 from last.
addi sp, fp, -16 # Set SP to last argument.
lw a1, 8(a0) # Load address of object's dispatch table
lw a1, 0(a1) # Load address of method: DoublingVector.__init__
jalr a1 # Invoke method: DoublingVector.__init__
addi sp, fp, -@..main.size # Set SP to stack frame top.
lw a0, -12(fp) # Pop stack slot 3
*/
backend.emitLA(A0, cls.getPrototypeLabel(),
String.format("Load pointer to prototype of: %s", cls.getClassName()));
backend.emitJAL(objectAllocLabel, "Allocate new object in A0");
backend.emitSW(A0, FP, -sp_off*wordSize, String.format("Push on stack slot %d", sp_off));
incSp(0);
backend.emitSW(A0, FP, -sp_off*wordSize, "Push argument 0 from last.");
backend.emitADDI(SP, FP, sp_off, "Set SP to last argument.");
backend.emitLW(A1, A0, getDispatchTableOffset(), "Load address of object's dispatch table");
backend.emitLW(A1, A1, getMethodOffset(cls, "__init__"), String.format("Load address of method: %s.__init__", cls.getClassName()));
backend.emitJALR(A1, String.format("Invoke method: %s.__init", cls.getClassName()));
backend.emitADDI(SP, FP, "-"+size_label, "Set SP to stack frame top.");
-- sp_off;
backend.emitLW(A0, FP, -sp_off*wordSize, String.format("Pop stack slot %d", sp_off));
} else {
//func call
}
return A0;
}
@Override
public Register analyze(NoneLiteral node)
{
backend.emitMV(Register.A0, Register.ZERO, "Load none");
return Register.A0;
}
@Override
public Register analyze(StringLiteral node)
{
Label l = constants.getStrConstant(node.value);
backend.emitLA(Register.A0, l, "Load string literal");
return Register.A0;
}
@Override
public Register analyze(IntegerLiteral node)
{
backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value);
return Register.A0;
}
@Override
public Register 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 Register.A0;
}
@Override
public Register analyze(AssignStmt node)
{
Type t = node.value.getInferredType();
if(t.isSpecialType() || t.isListType())
{
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 Register.A0;
}
@Override
public Register analyze(ExprStmt node)
{
node.expr.dispatch(this);
return null;
}
@Override
public Register 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 Register 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;
}
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");
sp_off++;
backend.emitSW(Register.T0, Register.FP, -sp_off*wordSize, "Store address of second list");
sp_off++;
backend.emitSW(Register.T3, Register.FP, -sp_off*wordSize, "Store length of combined list");
sp_off++;
//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)
{
String operator = node.operator;
if(node.left.getInferredType().equals(Type.INT_TYPE) && node.right.getInferredType().equals(Type.INT_TYPE))
{
node.right.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
sp_off++;
node.left.dispatch(this);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
// Arithmetic Operators
if(operator.equals("+"))
backend.emitADD(Register.A0, Register.A0, Register.T0, "Add operation");
else if(operator.equals("-"))
backend.emitSUB(Register.A0, Register.A0, Register.T0, "Sub operation");
else if(operator.equals("*"))
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Mul operation");
else if(operator.equals("//"))
{
Label label = generateLocalLabel();
backend.emitBNEZ(Register.T0, label, "Check for Divide-by-zero");
backend.emitJAL(errorDiv, "Divide-by-zero error");
backend.emitLocalLabel(label, "Divide since divisor not zero");
backend.emitDIV(Register.A0, Register.A0, Register.T0, "Divide operation");
}
else if(operator.equals("%"))
{
Label label = generateLocalLabel();
backend.emitBNEZ(Register.T0, label, "Check for Divide-by-zero");
backend.emitJAL(errorDiv, "Divide-by-zero error");
backend.emitLocalLabel(label, "Divide since divisor not zero");
backend.emitREM(Register.A0, Register.A0, Register.T0, "Modulus operation");
}
else
{ // Comparison operators
String comment="Operator: "+operator;
if(operator.equals("=="))
{
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for equality");
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else if(operator.equals("!="))
{
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for inequality");
backend.emitSNEZ(Register.A0, Register.A0, "Result is True if XOR does not equal 0");
}
else if(operator.equals("<"))
backend.emitSLT(Register.A0,Register.A0, Register.T0, comment);
else if(operator.equals(">"))
backend.emitSLT(Register.A0, Register.T0, Register.A0, comment);
else if(operator.equals("<="))
{
backend.emitADDI(Register.T0,Register.T0, 1, "Increment by 1");
backend.emitSLT(Register.A0, Register.A0, Register.T0, comment);
}
else if(operator.equals(">="))
{
backend.emitADDI(Register.A0,Register.A0, 1, "Increment by 1");
backend.emitSLT(Register.A0, Register.A0, Register.T0, comment);
}
else if(operator.equals("is"))
{
backend.emitXOR(Register.A0, Register.A0, Register.T0, comment);
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else
{
backend.emitJAL(errorNI, "Operator not implemented for integer operands");
}
}
}
else if(node.left.getInferredType().equals(Type.BOOL_TYPE) && node.right.getInferredType().equals(Type.BOOL_TYPE))
{
// Comparison operators
String comment="Operator: "+operator;
if(operator.equals("=="))
{
node.right.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
sp_off++;
node.left.dispatch(this);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for equality");
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else if(operator.equals("!="))
{
node.right.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
sp_off++;
node.left.dispatch(this);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
backend.emitXOR(Register.A0, Register.A0, Register.T0, "Check for inequality");
backend.emitSNEZ(Register.A0, Register.A0, "Result is True if XOR does not equal 0");
}
else if(operator.equals("and"))
{
Label label = generateLocalLabel();
node.left.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
sp_off++;
backend.emitBEQZ(Register.A0, label, "If first operand is false, don't check second");
node.right.dispatch(this);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
backend.emitAND(Register.A0, Register.A0, Register.T0, "AND operation");
backend.emitLocalLabel(label, "Next step after AND");
}
else if(operator.equals("or"))
{
Label label = generateLocalLabel();
node.left.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
sp_off++;
backend.emitBNEZ(Register.A0, label, "If first operand is true, don't check second");
node.right.dispatch(this);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
backend.emitOR(Register.A0, Register.A0, Register.T0, "OR operation");
backend.emitLocalLabel(label, "Next step after OR");
}
else if(operator.equals("is"))
{
backend.emitXOR(Register.A0, Register.A0, Register.T0, comment);
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else
{
backend.emitJAL(errorNI, "Operator not implemented for boolean operands");
}
}
else if(node.left.getInferredType().isListType() && node.right.getInferredType().isListType())
{
node.right.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
sp_off++;
node.left.dispatch(this);
sp_off--;
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
if(operator.equals("+"))
addLists();
}
else if(node.left.getInferredType().equals(Type.STR_TYPE) && node.right.getInferredType().equals(Type.STR_TYPE))
{
}
else
{
backend.emitJAL(errorNI, "Operator not implemented");
}
return null;
}
@Override
public Register analyze(UnaryExpr node)
{
if(node.operator.equals("-") && node.getInferredType().equals(Type.INT_TYPE))
{
node.operand.dispatch(this);
backend.emitLI(Register.T0, -1, "Set value of Register T0 to -1");
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1");
}
else if(node.operator.equals("not") && node.getInferredType().equals(Type.BOOL_TYPE))
{
node.operand.dispatch(this);
backend.emitSEQZ(Register.T0, Register.A0, "Not operation on Register A0");
}
else
return null;
return Register.A0;
}
@Override
public Register 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 Register analyze(VarDef node)
{
StackVarInfo svi = (StackVarInfo) sym.get(node.var.identifier.name);
node.value.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Store variable "+node.var.identifier.name+" value in Stack");
offsetMap.put(svi, sp_off);
sp_off++;
return null;
}
@Override
public Register analyze(WhileStmt node)
{
Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop");
node.condition.dispatch(this);
Label endLoop = elseBlock;
for(Stmt stmt:node.body)
stmt.dispatch(this);
backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of while loop");
return null;
}
@Override
public Register analyze(ListExpr node) {
int l = node.elements.size();
backend.emitLI(Register.A1, l+1, "Load length of list+1 in words");
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.T0, Register.A1, "Make a copy of address of allocated space");
int i = l;
backend.emitLI(Register.A0,l,"Load length of list in words");
backend.emitSW(Register.A0,Register.T0,0,"Store length of list: "+i);
backend.emitADDI(Register.T0,Register.T0,wordSize,"Increment address");
for(Expr exp:node.elements)
{
Register r = exp.dispatch(this);
backend.emitSW(r,Register.T0,0,"Store element "+i+" from last.");
backend.emitADDI(Register.T0,Register.T0,wordSize,"Increment address");
i--;
}
backend.emitMV(Register.A0,Register.A1,"Load address of list");
return Register.A0;
}
@Override
public Register analyze(ForStmt node) {
System.out.println(node);
/*
node.
Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop");
node.condition.dispatch(this);
Label endLoop = elseBlock;
for(Stmt stmt:node.body)
stmt.dispatch(this);
backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of while loop");*/
return null;
}
@Override
public Register analyze(IndexExpr node)
{
System.out.println(node);
return defaultAction(node);
}
public Register analyze(MemberExpr node)
{
ClassInfo objectClass = (ClassInfo) globalSymbols.get(node.object.getInferredType().className());
Label label = generateLocalLabel();
Register obj = node.object.dispatch(this);
backend.emitBNEZ(obj, label, "Ensure not None");
backend.emitJ(errorNone, "Go to error handler");
backend.emitLocalLabel(label, "Not None");
backend.emitLW(A0, obj, getAttrOffset(objectClass, node.member.name),
String.format("Get attribute: %s.%s", objectClass.getClassName(), node.member.name));
return A0;
}
public Register analyze(MethodCallExpr node)
{
Register obj = node.method.object.dispatch(this);
int n_args = node.args.size();
Label label = generateLocalLabel();
backend.emitBNEZ(obj, label, "Ensure not None");
backend.emitJ(errorNone, "Go to error handler");
backend.emitLocalLabel(label, "Not None");
incSp(n_args+1);
backend.emitSW(obj, FP, (n_args - sp_off) *wordSize, String.format("Push argument %d from last.", n_args));
for (int i = 0; i < n_args; ++i)
backend.emitSW(node.args.get(i).dispatch(this), FP, (n_args - i - 1 - sp_off) * wordSize,
String.format("Push argument %d from last.", n_args - i - 1));
backend.emitLW(A0, FP, (n_args- sp_off) * wordSize, String.format("Peek stack slot %d", sp_off - (n_args + 1)));
ClassInfo objectClass = (ClassInfo)sym.get(((Identifier)node.method.object).name);
backend.emitLW(A1, A0, getDispatchTableOffset(), "Load address of object's dispatch table");
backend.emitLW(A1, A1, getMethodOffset(objectClass, node.method.member.name),
String.format("Load address of method: %s.%s", objectClass.getClassName(), node.method.member.name));
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitJALR(A1, String.format("Invoke method: %s.%s", objectClass.getClassName(), node.method.member.name));
backend.emitInsn(String.format("addi sp, fp, -%s", size_label), "Set SP to stack frame top.");
sp_off -= n_args+1;
return A0;
}
public Register analyze(ReturnStmt node)
{
return null;
}
}
/**
* 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");
emitErrorFunc(errorNI, "Not Implemented");
}
/** 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");
}
}