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

1223 lines
57 KiB

package chocopy.pa3;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import chocopy.common.analysis.*;
import chocopy.common.astnodes.*;
import chocopy.common.analysis.types.*;
import chocopy.common.codegen.*;
import chocopy.common.codegen.RiscVBackend.Register;
import static 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
{
/** Label for built-in routines. */
protected final Label makeintLabel = new Label("makeint");
protected final Label strneqlLabel = new Label("strneql");
protected final Label streqlLabel = new Label("streql");
protected final Label makeboolLabel = new Label("makebool");
protected final Label strcatLabel = new Label("strcat");
protected final Label concatLabel = new Label("concat");
protected final Label conslistLabel = new Label("conslist");
/** 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");
/** Let's try to implement everything first.*/
private final Label errorNI = new Label("error.NI");
/** A code generator emitting instructions to BACKEND. */
public CodeGenImpl(RiscVBackend backend)
{
super(backend);
backend.defineSym("listHeaderWords", "4");
backend.defineSym("strHeaderWords", "4");
backend.defineSym("bool.True", "const_1");
backend.defineSym("bool.False", "const_0");
}
/**
* 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);
}
stmtAnalyzer.emitSizeLabel();
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());
// --- Prologue ---
// space for return address = 1
// space for control link = 1
// space for static link = 1
// space for locals = num of locals
int requiredStackSpace = (1 + 1 + 1 + funcInfo.getLocals().size())*wordSize;
backend.emitADDI(SP, SP, -requiredStackSpace, "Reserve space for stack frame.");
backend.emitSW(RA, SP, requiredStackSpace-4, "return address");
backend.emitSW(FP, SP, requiredStackSpace-8, "control link");
// if we want to add static link
//backend.emitSW(FP, SP, requiredStackSpace-12, "static link");
backend.emitADDI(FP, SP, requiredStackSpace, "New fp is at old SP.");
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
int emptySlotNum = 3;
for (StackVarInfo var : funcInfo.getLocals()) {
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");
emptySlotNum++;
}
// --- Function Body ---
// statements load all the variables that caller put on stack
// statements use fp to load the variables
// 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()) {
stmt.dispatch(stmtAnalyzer);
}
stmtAnalyzer.emitSizeLabel();
backend.emitJ(stmtAnalyzer.epilogue, "Jump to function epilogue");
// --- Epilogue ---
backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue");
backend.emitLW(RA, FP, -4, "Get return address");
backend.emitLW(FP, FP, -8, "Use control link to restore caller's fp");
backend.emitADDI(SP, SP, requiredStackSpace, "Restore stack pointer");
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 Stack<Label> elseBlock = new Stack<Label>();
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;
private final Register[] registerPool = {T2,T3, T4, T5, T6, A2, A3, A4, A5, A6};
private final boolean[] registerAvilMap = {true, true, true, true, true, true, true, true, true, true};
private int getRegister(){ //return a handle of a vacant register
for(int i = 0; i < 10; ++i)
if(registerAvilMap[i])
{
registerAvilMap[i]=false;
return i;
}
for(int i = 0; i < 10; ++i)
registerAvilMap[i] = true; //freeall;
return 0;
}
private void freeRegister(int handle){ //handle used to free the register
registerAvilMap[handle] = true;
}
private int getParamLocationOffset(int index, int paramSize) {
return (paramSize - index - 1) * wordSize;
}
private int getLocalVarLocationOffset(int index, int paramSize) {
return - (index - paramSize + 1) * wordSize;
}
private int getStaticLinkOffset(int paramSize) {
return paramSize * wordSize;
}
/**
* 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;
max_sp = max_sp >= sp_off?max_sp:sp_off;
}
public void emitSizeLabel(){
backend.defineSym(size_label, max_sp*wordSize);
}
// *********** functions start ***********
public Register analyze(CallExpr node) {
System.out.println("Inside CallExpr: " + node.function.name);
//ackend.emitLW(T6, FP, 0, "Inside CallExpr: " + node.function.name);
SymbolInfo Ty = sym.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(1);
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 {
// function
Identifier functionId = node.function;
FuncInfo calleeFunctionInfo = (FuncInfo) sym.get(node.function.name);
boolean isInMainFunction = funcInfo == null;
if (!isInMainFunction) {
System.out.println("--> caller: " + funcInfo.getFuncName() + " depth: " + funcInfo.getDepth());
System.out.println("--> callee: " + calleeFunctionInfo.getFuncName() + " depth: " + calleeFunctionInfo.getDepth());
int calleeDepth = calleeFunctionInfo.getDepth();
int callerDepth = funcInfo.getDepth();
int jumps = callerDepth - calleeDepth + 1;
int tmpHandle = getRegister();
Register tmp = registerPool[tmpHandle];
backend.emitMV(tmp, FP, "Load FP");
FuncInfo curr = funcInfo;
for (int i = 0; i < jumps; i++) {
backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link to " + curr.getFuncName());
curr = curr.getParentFuncInfo();
}
backend.emitSW(tmp, SP, -1 * wordSize, "Push static link onto active frame.");
freeRegister(tmpHandle);
} else {
backend.emitSW(FP, SP, -1 * wordSize, "Push static link onto active frame.");
}
List<Expr> args = node.args;
int spaceRequiredForArgs = (args.size() + 1)*4;
for (int i = 0; i < args.size(); i++) {
int argNum = i + 1;
int slotNum = argNum + 1; // We have extra slot for static link
Expr expr = args.get(i);
expr.dispatch(this);
String formalParamName = calleeFunctionInfo.getParams().get(i);
StackVarInfo formalParamInfo = (StackVarInfo) calleeFunctionInfo.getSymbolTable().get(formalParamName);
if (expr.getInferredType().equals(Type.INT_TYPE)) {
if ((functionId.name.equals("print")) &&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.INT_TYPE))) {
backend.emitJAL(makeintLabel, "Box integer");
} else {
// FIXME: passed argument does not match formal parameter
}
} else if (expr.getInferredType().equals(Type.BOOL_TYPE)) {
if ((functionId.name.equals("print"))&&(formalParamInfo.getVarType().equals(Type.OBJECT_TYPE) || formalParamInfo.getVarType().equals(Type.BOOL_TYPE))) {
backend.emitJAL(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 actual argument
backend.emitSW(A0, SP, -slotNum * wordSize, "Push actual argument for " + formalParamName + " onto stack");
}
backend.emitADDI(SP, SP, -spaceRequiredForArgs, "Set SP to last argument.");
backend.emitJAL(calleeFunctionInfo.getCodeLabel(), "Invoke function: " + functionId.name);
backend.emitADDI(SP, SP, spaceRequiredForArgs, "Set SP to stack frame top.");
}
return A0;
}
public Register analyze(MethodCallExpr node) {
System.out.println("Inside MethodCallExpr: " + node.method.member.name);
// backend.emitLW(T6, FP, 0, "Inside MethodCallExpr: " + node.method.member.name);
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 = null;
if(node.method.object instanceof CallExpr)
objectClass = (ClassInfo)globalSymbols.get(((CallExpr)node.method.object).getInferredType().className());
else
objectClass = (ClassInfo)globalSymbols.get(node.method.object.getInferredType().className());
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;
}
@Override
public Register analyze(ReturnStmt stmt) {
System.out.println("Inside ReturnStmt: ");
// backend.emitLW(T6, FP, 0, "Inside ReturnStmt: ");
Expr expr = stmt.value;
if (expr == null) {
backend.emitMV(A0, ZERO, "Return None");
return A0;
}
// All expressions should save their end result in A0
expr.dispatch(this);
backend.emitJ(this.epilogue, "Jump to function epilogue");
return A0;
}
// *********** functions end ***********
@Override
public Register analyze(NoneLiteral node)
{
System.out.println("Inside NoneLiteral: ");
// backend.emitLW(T6, FP, 0, "Inside NoneLiteral: ");
backend.emitMV(Register.A0, Register.ZERO, "Load none");
return Register.A0;
}
@Override
public Register analyze(StringLiteral node)
{
System.out.println("Inside StringLiteral: ");
//backend.emitLW(T6, FP, 0, "Inside StringLiteral: ");
Label l = constants.getStrConstant(node.value);
backend.emitLA(Register.A0, l, "Load string literal");
return Register.A0;
}
@Override
public Register analyze(BooleanLiteral node)
{
System.out.println("Inside BooleanLiteral: ");
// backend.emitLW(T6, FP, 0, "Inside BooleanLiteral: ");
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;
}
class StackVarRuntimeInfo {
public final int off;
public final Register sl;
public int regHandle;
StackVarRuntimeInfo(Register sl, int off){
this.off = off;
this.sl = sl;
regHandle = -1;
}
StackVarRuntimeInfo(Register sl, int off, int regHandle){
this.off = off;
this.sl = sl;
this.regHandle = regHandle;
}
public void free(){
if(regHandle >= 0){
freeRegister(regHandle);
regHandle = -1;
}
}
}
private Object getVar(Identifier id){
SymbolInfo info = sym.get(id.name);
if(info instanceof StackVarInfo)
return getStackVar((StackVarInfo)info);
else
return ((GlobalVarInfo)info).getLabel();
}
private StackVarRuntimeInfo getStackVar(StackVarInfo info){
FuncInfo varFuncInfo = info.getFuncInfo();
System.out.println("var name: " + info.getVarName());
System.out.println("varfuncInfo name: " + varFuncInfo.getFuncName());
System.out.println("var idx: " + varFuncInfo.getVarIndex(info.getVarName()));
int tmpHandle = getRegister();
Register tmp = registerPool[tmpHandle];
if (funcInfo != null && funcInfo != info.getFuncInfo()) {
int varFunDepth = varFuncInfo.getDepth();
int callerDepth = funcInfo.getDepth();
int jumps = callerDepth - varFunDepth;
System.out.println("varFunDepth: " + varFunDepth);
System.out.println("callerDepth: " + callerDepth);
backend.emitMV(tmp, FP, "Load FP");
FuncInfo curr = funcInfo;
for (int i = 0; i < jumps; i++) {
backend.emitLW(tmp, tmp, curr.getParams().size()*wordSize, "Load static link from " + curr.getFuncName() + " to " + curr.getParentFuncInfo().getBaseName());
curr = curr.getParentFuncInfo();
}
System.out.println("We are referencing non local variable");
} else {
System.out.println("We are referencing local variable");
backend.emitMV(tmp, FP, "We are referencing local variable");
}
int idx = varFuncInfo.getVarIndex(info.getVarName());
int paramSize = varFuncInfo.getParams().size();
int offset = getLocalVarLocationOffset(idx, paramSize);
return new StackVarRuntimeInfo(tmp, offset, tmpHandle);
}
@Override
public Register analyze(AssignStmt node)
{
System.out.println("Inside AssignStmt: ");
//backend.emitLW(T6, FP, 0, "Inside AssignStmt: ");
//Type t = node.value.getInferredType();
// if(t.isSpecialType() || t.isListType())
//{
Register reg = node.value.dispatch(this);
if (reg == null)
reg = A0;
for(Expr target: node.targets)
{
if(target instanceof Identifier)
{
Identifier targetID = (Identifier)target;
Object var = getVar(targetID);
if(var instanceof StackVarRuntimeInfo){
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
backend.emitSW(reg, rtinfo.sl, rtinfo.off ,"Store local variable: "+((Identifier)target).name);
rtinfo.free();
}
else
backend.emitSW(reg, (Label)var, T0, "Store Global variable: " + ((Identifier)targetID).name);
}
else
{
Register ret = target.dispatch(this);
backend.emitSW(T1, ret, 0, "Set list element");
}
}
//}
return reg;
}
@Override
public Register analyze(ExprStmt node)
{
System.out.println("Inside ExprStmt: ");
// backend.emitLW(T6, FP, 0, "Inside ExprStmt: ");
node.expr.dispatch(this);
return null;
}
@Override
public Register analyze(IfExpr node)
{
System.out.println("Inside IfExpr: ");
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel();
backend.emitBEQZ(result, ln,"Jump to end of loop");
node.thenExpr.dispatch(this);
backend.emitJ(ln, "Jump to end of if expression");
elseBlock.push(generateLocalLabel());
backend.emitLocalLabel(elseBlock.peek(), "Else part of if expression");
node.elseExpr.dispatch(this);
elseBlock.pop();
backend.emitLocalLabel(ln, "End of if expression");
return A0;
}
@Override
public Register analyze(IfStmt node)
{
System.out.println("Inside IfStmt: ");
Register result = node.condition.dispatch(this);
Label ln = generateLocalLabel();
backend.emitBEQZ(result, ln,"Jump to end of loop");
for(Stmt s:node.thenBody)
s.dispatch(this);
backend.emitJ(ln, "Jump to end of if statement");
elseBlock.push(generateLocalLabel());
backend.emitLocalLabel(elseBlock.peek(), "Else part of if statement");
for(Stmt s:node.elseBody)
s.dispatch(this);
elseBlock.pop();
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");
incSp(1);
backend.emitSW(Register.T0, Register.FP, -sp_off*wordSize, "Store address of second list");
incSp(1);
backend.emitSW(Register.T3, Register.FP, -sp_off*wordSize, "Store length of combined list");
incSp(1);
//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)
{
System.out.println("Inside BinaryExpr: ");
String operator = node.operator;
//backend.emitLW(T6, FP, 0, "Inside BinaryExpr: ");
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);
incSp(1);
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);
}
/* Maybe NA
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);
incSp(1);
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);
incSp(1);
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);
incSp(1);
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);
incSp(1);
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");
}
/*Maybe NA
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())
{
if(operator.equals("+"))
{
node.left.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push left operand on stack slot "+sp_off);
incSp(1);
node.right.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push right operand on stack slot "+sp_off);
incSp(1);
backend.emitADDI(Register.SP,Register.FP,-(sp_off-1)*wordSize,"Set SP to last argument");
backend.emitJAL(concatLabel, "Call concatenation routine");
backend.emitADDI(Register.SP, Register.FP, "-"+size_label, "Set SP to stack frame");
return Register.A0;
//addLists();
}
else
backend.emitJAL(errorNI, "Operator not implemented for list operands");
}
else if(node.left.getInferredType().equals(Type.STR_TYPE) && node.right.getInferredType().equals(Type.STR_TYPE))
{
if(operator.equals("=="))
{
incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last.");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last.");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitJAL(streqlLabel, "Invoke method:streql");
sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp");
}
else if(operator.equals("!="))
{
incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last.");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last.");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitJAL(strneqlLabel, "Invoke method:strneql");
sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp");
}
else if(operator.equals("+"))
{
incSp(2);
backend.emitSW(node.left.dispatch(this), FP, (1 - sp_off) *wordSize, "Push argument 0 from last.");
backend.emitSW(node.right.dispatch(this), FP, ( - sp_off) *wordSize, "Push argument 1 from last.");
backend.emitADDI(SP, FP, -sp_off * wordSize, "Set SP to last argument.");
backend.emitJAL(strcatLabel, "Invoke method:strcat");
sp_off -= 2;
backend.emitADDI(SP, FP, -sp_off*wordSize, "restore sp");
}
else
backend.emitJAL(errorNI, "Operator not implemented for String operands");
}
else if(operator.equals("is"))
{
node.right.dispatch(this);
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
incSp(1);
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, "Operator: is");
backend.emitSEQZ(Register.A0, Register.A0, "Result is True if XOR equals 0");
}
else
{
backend.emitJAL(errorNI, "Operator not implemented");
}
return A0;
}
@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");
backend.emitSUB(Register.A0, Register.ZERO, Register.A0, "Unary negation");
}
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
backend.emitJAL(errorNI, "Operator not implemented");
return Register.A0;
}
@Override
public Register analyze(Identifier node)
{
//backend.emitLW(T6, FP, 0, "Inside Identifier: ");
Identifier targetID = (Identifier)node;
Object var = getVar(targetID);
if(var instanceof StackVarRuntimeInfo){
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) var;
backend.emitLW(A0, rtinfo.sl, rtinfo.off,"Load local variable: "+node.name);
rtinfo.free();
}
else
backend.emitLW(A0, (Label) var, "Load local variable: "+node.name);
return Register.A0;
}
@Override
public Register analyze(WhileStmt node)
{
System.out.println("Inside WhileStmt: ");
Label startLoop = generateLocalLabel();
backend.emitLocalLabel(startLoop, "Beginning of while loop");
Register result = node.condition.dispatch(this);
Label endLoop = generateLocalLabel();
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");
return null;
}
@Override
public Register analyze(ListExpr node) {
int l = node.elements.size();
int i=l;
for(Expr exp:node.elements)
{
Register r = exp.dispatch(this);
if (exp.getInferredType().equals(Type.INT_TYPE) && !node.elements.get(0).getInferredType().equals(Type.INT_TYPE))
{
if(r!=Register.A0)
backend.emitMV(r, Register.A0, "Copy to Register A0");
backend.emitJAL(makeintLabel, "Box integer");
}
else if (exp.getInferredType().equals(Type.BOOL_TYPE) && !node.elements.get(0).getInferredType().equals(Type.BOOL_TYPE))
{
if(r!=Register.A0)
backend.emitMV(r, Register.A0, "Copy to Register A0");
backend.emitJAL(makeboolLabel, "Box boolean");
}
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push argument "+i+" from last");
incSp(1);
i--;
}
backend.emitLI(Register.A0, l, "Pass list length");
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push length of list");
incSp(1);
backend.emitADDI(Register.SP, Register.FP, -(sp_off-1)*wordSize, "Set SP to last argument");
backend.emitJAL(conslistLabel, "Move values to new list object");
backend.emitADDI(Register.SP, Register.FP, "-"+size_label, "Set SP to stack frame");
sp_off-=l;
return Register.A0;
}
@Override
public Register analyze(IntegerLiteral node) {
System.out.println("Inside IntegerLiteral: " + node.value);
//backend.emitLW(T6, FP, 0, "Inside IntegerLiteral: " + node.value);
backend.emitLI(A0, node.value, "Load integer literal " + node.value);
return A0;
}
@Override
public Register analyze(ForStmt node)
{
int r;
List<Integer> regs= new ArrayList<Integer>();
if (node.iterable.getInferredType().isListType())
{
Label startLoop = generateLocalLabel();
Label endLoop = generateLocalLabel();
Label temp = generateLocalLabel();
r=getRegister();
regs.add(r);
Register iden = registerPool[r];
node.iterable.dispatch(this);
r=getRegister();
regs.add(r);
Register l = registerPool[r];
r=getRegister();
regs.add(r);
Register ln = registerPool[r];
backend.emitBNEZ(Register.A0, temp, "Ensure not none");
backend.emitJ(errorNone, "Empty List");
backend.emitLocalLabel(temp, "Continue execution for for-loop");
backend.emitMV(l,Register.A0,"Location of list");
r=getRegister();
regs.add(r);
Register iter = registerPool[r];
backend.emitSW(l, Register.FP, -sp_off*wordSize, "Store index on stack");
incSp(1);
backend.emitMV(iter, Register.ZERO, "Initialize index variable");
backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
incSp(1);
backend.emitLocalLabel(startLoop, "Start for loop");
sp_off--;
backend.emitLW(iter, Register.FP, -sp_off*wordSize, "Load index from stack");
backend.emitLW(l, Register.FP, -(sp_off-1)*wordSize, "Store list location from stack");
backend.emitLW(ln, l, getAttrOffset(listClass, "__len__"), "Get attribute __len__");
backend.emitBGEU(iter, ln, endLoop, "Jump to end loop if counter exceeds length");
backend.emitADDI(iter, iter, 1, "Increment counter");
backend.emitSW(iter, Register.FP, -sp_off*wordSize, "Store index on stack");
incSp(1);
backend.emitADDI(iden, iter, 3, "Compute list element offset in words");
backend.emitLI(ln, wordSize, "Word size in bytes");
backend.emitMUL(iden, iden, ln, "Compute list element offset in bytes");
backend.emitADD(iden, l, iden, "Pointer to list element");
backend.emitLW(iden, iden, 0, "Set list element");
SymbolInfo info = sym.get(node.identifier.name);
if(info instanceof GlobalVarInfo)
{
GlobalVarInfo gvi=(GlobalVarInfo) info;
backend.emitSW(iden, gvi.getLabel(), Register.T0, "Assign global: "+node.identifier.name+"(using tmp register)");
}
else
{
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) getVar(node.identifier);
backend.emitSW(iden, rtinfo.sl, rtinfo.off,"Store local variable: "+node.identifier.name);
rtinfo.free();
}
for(Stmt stmt:node.body)
stmt.dispatch(this);
backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of for loop");
}
else if (node.iterable.getInferredType().equals(Type.STR_TYPE))
{
Label startLoop = generateLocalLabel();
Label endLoop = generateLocalLabel();
r=getRegister();
regs.add(r);
Register iden = registerPool[r];
node.iterable.dispatch(this);
r=getRegister();
regs.add(r);
Register l = registerPool[r];
r=getRegister();
regs.add(r);
Register ln = registerPool[r];
backend.emitMV(l,Register.A0,"Location of String");
r=getRegister();
regs.add(r);
Register iter = registerPool[r];
backend.emitMV(iter, Register.ZERO, "Initialize index variable");
backend.emitLocalLabel(startLoop, "Start for loop");
backend.emitLW(ln, l, getAttrOffset(strClass, "__len__"), "Get attribute __len__");
backend.emitBGE(iter, ln, endLoop, "Jump to end loop if counter exceeds length");
backend.emitADDI(iden, iter, 4 * wordSize, "Convert index to offset to char in bytes");
backend.emitLBU(iden, iden, 0, "Load character");
SymbolInfo info = sym.get(node.identifier.name);
if(info instanceof GlobalVarInfo)
{
GlobalVarInfo gvi=(GlobalVarInfo) info;
backend.emitSW(iden, gvi.getLabel(), Register.T0, "Assign global: "+node.identifier.name+"(using tmp register)");
}
else
{
StackVarRuntimeInfo rtinfo = (StackVarRuntimeInfo) getVar(node.identifier);
backend.emitSW(iden, rtinfo.sl, rtinfo.off,"Store local variable: "+node.identifier.name);
rtinfo.free();
}
for(Stmt stmt:node.body)
stmt.dispatch(this);
backend.emitADDI(iter, iter, 1, "Increment counter");
backend.emitJ(startLoop, "Jump to beginning of loop");
backend.emitLocalLabel(endLoop, "End of for loop");
}
else
{
System.out.println("Iterable not a list or a string");
}
for(int n:regs)
freeRegister(n);
return null;
}
@Override
public Register analyze(IndexExpr node)
{
incSp(1);
Register listObj = node.list.dispatch(this);
backend.emitSW(listObj, FP, -sp_off * wordSize, String.format("Push on stack slot %d", sp_off));
Register index = node.index.dispatch(this);
Register vacantReg = (index != A0) ? A0 : A1;
int tmpHandle = getRegister();
Register temp= registerPool[tmpHandle];
if (node.list.getInferredType().isListType())
{
backend.emitLW(vacantReg, FP, -sp_off * wordSize, String.format("Pop stack slot %d", sp_off));
final Label bp = generateLocalLabel();
backend.emitBNEZ(vacantReg, bp, "Ensure not None");
backend.emitJ(errorNone, "Go to error handler");
backend.emitLocalLabel(bp, "Not None");
final Label bt = generateLocalLabel();
backend.emitLW(temp, vacantReg, getAttrOffset(listClass, "__len__"), "Load attribute: __len__");
backend.emitBLTU(index, temp, bt, "Ensure 0 <= index < len");
backend.emitJ(errorOob, "Go to error handler");
backend.emitLocalLabel(bt, "Index within bounds");
backend.emitADDI(index, index, 4, "Compute list element offset in words");
backend.emitLI(temp, wordSize, "Word size in bytes");
backend.emitMUL(index, index, temp, "Compute list element offset in bytes");
backend.emitADD(index, vacantReg, index, "Pointer to list element");
backend.emitLW(Register.A0, index, 0, "Set list element");
}
else
{
backend.emitLW(vacantReg, FP, - sp_off * wordSize, String.format("Peek stack slot %d", sp_off- 1));
Label boundchk = generateLocalLabel();
backend.emitLW(temp, vacantReg, getAttrOffset(strClass, "__len__"), "Load attribute: __len__");
backend.emitBLTU(index, temp, boundchk, "Ensure 0 <= idx < len");
backend.emitJ(errorOob, "Go to error handler");
backend.emitLocalLabel(boundchk, "Index within bounds");
incSp(1);
backend.emitSW(index, RiscVBackend.Register.FP, -sp_off * wordSize, String.format("Push on stack slot %d",sp_off));
getAttrOffset(strClass, "__len__");
backend.emitLW(T0, FP, -sp_off * wordSize, String.format("Pop stack slot %d", sp_off));
--sp_off;
backend.emitSW(A1, FP, -sp_off*wordSize, String.format("Push Argument %d", sp_off));
backend.emitADDI(T0, T0, 4 * wordSize, "Convert index to offset to char in bytes");
backend.emitLBU(T0, T0, 0, "Load character");
backend.emitLA(vacantReg, strClass.getPrototypeLabel(), "Create Str for char");
backend.emitJAL(objectAllocLabel, "Alloc char");
backend.emitLI(T1, 1, "str size");
backend.emitSW(T1, vacantReg, 3*wordSize, "len");
backend.emitSW(T0, vacantReg, 4*wordSize, "ch");
}
freeRegister(tmpHandle);
-- sp_off;
return vacantReg;
}
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;
}
}
/**
* 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() {
emitStdFunc("concat");
emitStdFunc("conslist");
emitStdFunc("strcat");
emitStdFunc("streql");
emitStdFunc("strneql");
emitStdFunc("makeint");
emitStdFunc("makebool");
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);
if(errLabel==errorOob)
backend.emitLI(A0, ERROR_OOB, "Exit code for: " + msg);
else if(errLabel==errorDiv)
backend.emitLI(A0, ERROR_DIV_ZERO, "Exit code for: " + msg);
else if(errLabel==errorNI)
backend.emitLI(A0, ERROR_NYI, "Exit code for: " + msg);
else
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");
}
}