|
|
|
@ -1,53 +1,16 @@
|
|
|
|
|
package chocopy.pa3;
|
|
|
|
|
|
|
|
|
|
import java.net.Socket;
|
|
|
|
|
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.AssignStmt;
|
|
|
|
|
import chocopy.common.astnodes.BinaryExpr;
|
|
|
|
|
import chocopy.common.astnodes.BooleanLiteral;
|
|
|
|
|
import chocopy.common.astnodes.CallExpr;
|
|
|
|
|
import chocopy.common.astnodes.ClassDef;
|
|
|
|
|
import chocopy.common.astnodes.ClassType;
|
|
|
|
|
import chocopy.common.astnodes.CompilerError;
|
|
|
|
|
import chocopy.common.astnodes.Errors;
|
|
|
|
|
import chocopy.common.astnodes.Expr;
|
|
|
|
|
import chocopy.common.astnodes.ExprStmt;
|
|
|
|
|
import chocopy.common.astnodes.ForStmt;
|
|
|
|
|
import chocopy.common.astnodes.FuncDef;
|
|
|
|
|
import chocopy.common.astnodes.GlobalDecl;
|
|
|
|
|
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.MemberExpr;
|
|
|
|
|
import chocopy.common.astnodes.MethodCallExpr;
|
|
|
|
|
import chocopy.common.astnodes.NonLocalDecl;
|
|
|
|
|
import chocopy.common.astnodes.NoneLiteral;
|
|
|
|
|
import chocopy.common.astnodes.Program;
|
|
|
|
|
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.astnodes.*;
|
|
|
|
|
import chocopy.common.analysis.types.*;
|
|
|
|
|
import chocopy.common.codegen.*;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
|
|
import static chocopy.common.codegen.RiscVBackend.Register.*;
|
|
|
|
|
import chocopy.common.codegen.RiscVBackend.Register;
|
|
|
|
|
|
|
|
|
@ -68,74 +31,17 @@ import chocopy.common.codegen.RiscVBackend.Register;
|
|
|
|
|
*/
|
|
|
|
|
public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
{
|
|
|
|
|
/** Label for built-in routines. */
|
|
|
|
|
protected final Label makeintLabel = new Label("makeint");
|
|
|
|
|
protected final Label strneqlLablel = new Label("strneql");
|
|
|
|
|
protected final Label streqlLablel = new Label("streql");
|
|
|
|
|
protected final Label makeboolLablel = new Label("makebool");
|
|
|
|
|
protected final Label strcatLablel = new Label("strcat");
|
|
|
|
|
protected final Label concatLablel = new Label("concat");
|
|
|
|
|
protected final Label conslistLablel = new Label("conslist");
|
|
|
|
|
|
|
|
|
|
/** A code generator emitting instructions to BACKEND. */
|
|
|
|
|
public CodeGenImpl(RiscVBackend backend)
|
|
|
|
|
{
|
|
|
|
|
super(backend);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates assembly code for PROGRAM.
|
|
|
|
|
*
|
|
|
|
|
* <p>This is the main driver that calls internal methods for emitting DATA section (globals,
|
|
|
|
|
* constants, prototypes, etc) as well as the the CODE section (predefined functions, built-in
|
|
|
|
|
* routines, and user-defined functions).
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void generate(Program program) {
|
|
|
|
|
System.out.println("Inside generate");
|
|
|
|
|
analyzeProgram(program);
|
|
|
|
|
|
|
|
|
|
backend.startData();
|
|
|
|
|
|
|
|
|
|
for (ClassInfo classInfo : this.classes) {
|
|
|
|
|
emitPrototype(classInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (ClassInfo classInfo : this.classes) {
|
|
|
|
|
emitDispatchTable(classInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (GlobalVarInfo global : this.globalVars) {
|
|
|
|
|
backend.emitGlobalLabel(global.getLabel());
|
|
|
|
|
emitConstant(
|
|
|
|
|
global.getInitialValue(),
|
|
|
|
|
global.getVarType(),
|
|
|
|
|
String.format("Initial value of global var: %s", global.getVarName()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
backend.startCode();
|
|
|
|
|
|
|
|
|
|
Label mainLabel = new Label("main");
|
|
|
|
|
backend.emitGlobalLabel(mainLabel);
|
|
|
|
|
backend.emitLUI(A0, HEAP_SIZE_BYTES >> 12, "Initialize heap size (in multiples of 4KB)");
|
|
|
|
|
backend.emitADD(S11, S11, A0, "Save heap size");
|
|
|
|
|
backend.emitJAL(heapInitLabel, "Call heap.init routine");
|
|
|
|
|
backend.emitMV(GP, A0, "Initialize heap pointer");
|
|
|
|
|
backend.emitMV(S10, GP, "Set beginning of heap");
|
|
|
|
|
backend.emitADD(S11, S10, S11, "Set end of heap (= start of heap + heap size)");
|
|
|
|
|
backend.emitMV(RA, ZERO, "No normal return from main program.");
|
|
|
|
|
backend.emitMV(FP, ZERO, "No preceding frame.");
|
|
|
|
|
|
|
|
|
|
emitTopLevel(program.statements);
|
|
|
|
|
|
|
|
|
|
for (FuncInfo funcInfo : this.functions) {
|
|
|
|
|
funcInfo.emitBody();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
emitStdFunc("alloc");
|
|
|
|
|
emitStdFunc("alloc2");
|
|
|
|
|
emitStdFunc("abort");
|
|
|
|
|
emitStdFunc("heap.init");
|
|
|
|
|
emitStdFunc("makeint");
|
|
|
|
|
|
|
|
|
|
emitCustomCode();
|
|
|
|
|
|
|
|
|
|
backend.startData();
|
|
|
|
|
emitConstants();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Operation on None. */
|
|
|
|
|
private final Label errorNone = new Label("error.None");
|
|
|
|
@ -144,10 +50,15 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
/** 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);
|
|
|
|
|
|
|
|
|
|
/** Label for built-in routine: makeint. */
|
|
|
|
|
protected final Label makeIntLabel = new Label("makeint");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -378,36 +289,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
return A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Register analyze(FuncDef node) {
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside FuncDef: " + node.name);
|
|
|
|
|
// function
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Register analyze(GlobalDecl node) {
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside GlobalDecl: ");
|
|
|
|
|
// function
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Register analyze(NonLocalDecl node) {
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside NonLocalDecl: ");
|
|
|
|
|
// function
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Register analyze(MethodCallExpr node) {
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside MethodCallExpr: " + node.method.member.name);
|
|
|
|
|
Register obj = node.method.object.dispatch(this);
|
|
|
|
@ -450,6 +331,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
|
|
|
|
|
// All expressions should save their end result in A0
|
|
|
|
|
expr.dispatch(this);
|
|
|
|
|
|
|
|
|
|
return A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -474,15 +356,6 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
return Register.A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: This is not correct. We need to use $int$prototype to create an integer literal, so commenting it
|
|
|
|
|
// @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)
|
|
|
|
|
{
|
|
|
|
@ -494,20 +367,31 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
return Register.A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Register analyze(AssignStmt node)
|
|
|
|
|
{
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside AssignStmt: ");
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside AssignStmt: ");
|
|
|
|
|
Type t = node.value.getInferredType();
|
|
|
|
|
if(t.isSpecialType() || t.isListType())
|
|
|
|
|
// if(t.isSpecialType() || t.isListType())
|
|
|
|
|
{
|
|
|
|
|
node.value.dispatch(this);
|
|
|
|
|
Register reg = node.value.dispatch(this);
|
|
|
|
|
if (reg == null)
|
|
|
|
|
reg = A0;
|
|
|
|
|
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)");
|
|
|
|
|
if(target instanceof Identifier)
|
|
|
|
|
{
|
|
|
|
|
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
|
|
|
|
|
backend.emitSW(reg, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Register ret = target.dispatch(this);
|
|
|
|
|
backend.emitSW(T1, ret, 0, "Set list element");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
@ -516,13 +400,9 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
{
|
|
|
|
|
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());
|
|
|
|
|
backend.emitSW(reg, Register.FP, -loc*4, "Load local variable: "+svi.getVarName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{//TODO: Object Assignment
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return Register.A0;
|
|
|
|
|
}
|
|
|
|
@ -545,7 +425,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
backend.emitLocalLabel(elseBlock, "Else part of if expression");
|
|
|
|
|
node.elseExpr.dispatch(this);
|
|
|
|
|
backend.emitLocalLabel(ln, "End of if expression");
|
|
|
|
|
return null;
|
|
|
|
|
return A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -562,63 +442,247 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside BinaryExpr: ");
|
|
|
|
|
node.left.dispatch(this);
|
|
|
|
|
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push on stack slot "+sp_off);
|
|
|
|
|
sp_off++;
|
|
|
|
|
node.right.dispatch(this);
|
|
|
|
|
sp_off--;
|
|
|
|
|
backend.emitLW(Register.T0, Register.FP, -sp_off*wordSize, "Pop stack slot "+sp_off);
|
|
|
|
|
// 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(">"))
|
|
|
|
|
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);
|
|
|
|
|
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("//"))
|
|
|
|
|
{
|
|
|
|
|
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
|
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
|
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(node.operator.equals(">="))
|
|
|
|
|
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
|
|
|
|
else if(node.operator.equals("<="))
|
|
|
|
|
else if(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");
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
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 A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Register analyze(UnaryExpr node)
|
|
|
|
|
{
|
|
|
|
|
node.operand.dispatch(this);
|
|
|
|
|
if(node.operator.equals("-"))
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -670,19 +734,25 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
@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.FP,-sp_off*wordSize,"Push argument "+i+" from last.");
|
|
|
|
|
sp_off++;
|
|
|
|
|
backend.emitSW(r,Register.T0,0,"Store element "+i+" from last.");
|
|
|
|
|
backend.emitADDI(Register.T0,Register.T0,wordSize,"Increment address");
|
|
|
|
|
i--;
|
|
|
|
|
}
|
|
|
|
|
backend.emitLI(Register.A0, l, "Pass list length");
|
|
|
|
|
backend.emitSW(Register.A0, Register.FP, -sp_off*wordSize, "Push argument "+i+" from last.");
|
|
|
|
|
sp_off++;
|
|
|
|
|
backend.emitADDI(Register.SP, Register.SP, -sp_off*wordSize, "Set SP to last argument.");
|
|
|
|
|
//TODO: Store reference to variable
|
|
|
|
|
backend.emitMV(Register.A0,Register.A1,"Load address of list");
|
|
|
|
|
return Register.A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -692,7 +762,7 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
public Register analyze(IntegerLiteral node) {
|
|
|
|
|
backend.emitLW(T6, FP, 0, "Inside IntegerLiteral: " + node.value);
|
|
|
|
|
backend.emitLI(A0, node.value, "Load integer literal " + node.value);
|
|
|
|
|
backend.emitJAL(makeIntLabel, "Box integer");
|
|
|
|
|
//backend.emitJAL(makeintLabel, "Box integer");
|
|
|
|
|
return A0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -714,12 +784,35 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Register analyze(IndexExpr node)
|
|
|
|
|
{
|
|
|
|
|
//System.out.println(node);
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
}
|
|
|
|
|
@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;
|
|
|
|
|
//
|
|
|
|
|
// if (node.list.getInferredType().isListType()) {
|
|
|
|
|
// backend.emitLW(vacantReg, FP, -sp_off * wordSize, String.format("Pop stack slot %d", sp_off));
|
|
|
|
|
//
|
|
|
|
|
// this.d(vacantReg);
|
|
|
|
|
// return this.a(vacantReg, index, A0, false);
|
|
|
|
|
// }else{
|
|
|
|
|
// this.a(0, vacantReg);
|
|
|
|
|
// Register a = a(index);
|
|
|
|
|
// Label ch = generateLocalLabel();
|
|
|
|
|
// backend.emitLW(a, vacantReg, getAttrOffset(strClass, "__len__"), "Load attribute: __len__");
|
|
|
|
|
// backend.emitBLTU(index, a, ch, "Ensure 0 <= idx < len");
|
|
|
|
|
// backend.emitJ(f, "Go to error handler");
|
|
|
|
|
// backend.emitLocalLabel(ch, "Index within bounds");
|
|
|
|
|
// this.c(index);
|
|
|
|
|
// Register a2 = this.a(false);
|
|
|
|
|
// this.a(1);
|
|
|
|
|
// return a2;
|
|
|
|
|
// }
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Register analyze(MemberExpr node)
|
|
|
|
|
{
|
|
|
|
@ -755,9 +848,19 @@ public class CodeGenImpl extends CodeGenBase
|
|
|
|
|
* 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. */
|
|
|
|
|