fixed conflicts after merging bill/merge-cond-cls
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
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.AssignStmt;
|
||||
@@ -34,29 +45,40 @@ 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;
|
||||
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* 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.
|
||||
* <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 {
|
||||
public class CodeGenImpl extends CodeGenBase
|
||||
{
|
||||
|
||||
/** A code generator emitting instructions to BACKEND. */
|
||||
public CodeGenImpl(RiscVBackend backend) {
|
||||
public CodeGenImpl(RiscVBackend backend)
|
||||
{
|
||||
super(backend);
|
||||
}
|
||||
|
||||
@@ -66,26 +88,30 @@ public class CodeGenImpl extends CodeGenBase {
|
||||
private final Label errorDiv = new Label("error.Div");
|
||||
/** Index out of bounds. */
|
||||
private final Label errorOob = new Label("error.OOB");
|
||||
|
||||
|
||||
/**
|
||||
* Emits the top level of the program.
|
||||
*
|
||||
* <p>This method is invoked exactly once, and is surrounded by some boilerplate code that: (1)
|
||||
* initializes the heap before the top-level begins and (2) exits after the top-level ends.
|
||||
* <p>
|
||||
* 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.
|
||||
* <p>
|
||||
* You only need to generate code for statements.
|
||||
*
|
||||
* @param statements top level statements
|
||||
*/
|
||||
protected void emitTopLevel(List<Stmt> 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.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) {
|
||||
for (Stmt stmt : statements)
|
||||
{
|
||||
stmt.dispatch(stmtAnalyzer);
|
||||
}
|
||||
backend.emitLI(A0, EXIT_ECALL, "Code for ecall: exit");
|
||||
@@ -95,12 +121,14 @@ public class CodeGenImpl extends CodeGenBase {
|
||||
/**
|
||||
* 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`.
|
||||
* <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) {
|
||||
protected void emitUserDefinedFunction(FuncInfo funcInfo)
|
||||
{
|
||||
backend.emitGlobalLabel(funcInfo.getCodeLabel());
|
||||
|
||||
|
||||
@@ -143,36 +171,32 @@ public class CodeGenImpl extends CodeGenBase {
|
||||
}
|
||||
|
||||
/** An analyzer that encapsulates code generation for statements. */
|
||||
private class StmtAnalyzer extends AbstractNodeAnalyzer<Void> {
|
||||
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");
|
||||
* 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
|
||||
* 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.
|
||||
* 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 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.
|
||||
* 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. */
|
||||
@@ -184,298 +208,452 @@ public class CodeGenImpl extends CodeGenBase {
|
||||
/** The descriptor for the current function, or null at the top level. */
|
||||
private final FuncInfo funcInfo;
|
||||
|
||||
/** An analyzer for the function described by FUNCINFO0, which is null for the top level. */
|
||||
/** 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// *********** functions start ***********
|
||||
|
||||
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 {
|
||||
System.out.println("*** CallExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside CallExpr: " + node.function.name);
|
||||
|
||||
// function
|
||||
Identifier functionId = node.function;
|
||||
|
||||
List<Expr> args = node.args;
|
||||
int spaceRequiredForArgs = (args.size() + 1)*4;
|
||||
//backend.emitSW(A0, SP, -4, "Put static link");
|
||||
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);
|
||||
// All expressions should save their end result in A0
|
||||
// So, once expr is evaluated add value inside A0 onto stack as an argument
|
||||
backend.emitSW(A0, SP, -4*slotNum, "Push argument " + argNum + " from left");
|
||||
}
|
||||
|
||||
backend.emitADDI(SP, SP, -spaceRequiredForArgs, "Set SP to last argument.");
|
||||
backend.emitJAL(new Label("$"+functionId.name), "Invoke function: " + functionId.name);
|
||||
backend.emitADDI(SP, SP, spaceRequiredForArgs, "Set SP to stack frame top.");
|
||||
}
|
||||
return A0;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public Void analyze(FuncDef node) {
|
||||
// System.out.println("*** FuncDef");
|
||||
// backend.emitLW(T6, FP, 0, "Inside FuncDef");
|
||||
// // function
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public Void analyze(GlobalDecl node) {
|
||||
// System.out.println("*** GlobalDecl");
|
||||
// backend.emitLW(T6, FP, 0, "Inside GlobalDecl");
|
||||
// // function
|
||||
// return defaultAction(node);
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public Void analyze(NonLocalDecl node) {
|
||||
// System.out.println("*** NonLocalDecl");
|
||||
// backend.emitLW(T6, FP, 0, "Inside NonLocalDecl");
|
||||
// // function
|
||||
// return defaultAction(node);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Void analyze(ReturnStmt stmt) {
|
||||
public Register analyze(ReturnStmt stmt) {
|
||||
System.out.println("*** ReturnStmt");
|
||||
backend.emitLW(T6, FP, 0, "Inside ReturnStmt");
|
||||
// Expr expr = stmt.value;
|
||||
// // All expressions should save their end result in A0
|
||||
// expr.dispatch(this);
|
||||
backend.emitLW(A0, FP, 0, "Load var: " + "last");
|
||||
return null;
|
||||
return A0;
|
||||
}
|
||||
|
||||
|
||||
// *********** functions end ***********
|
||||
|
||||
|
||||
@Override
|
||||
public Register analyze(NoneLiteral node)
|
||||
{
|
||||
backend.emitMV(Register.A0, Register.ZERO, "Load none");
|
||||
return Register.A0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(CallExpr node) {
|
||||
System.out.println("*** CallExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside CallExpr: " + node.function.name);
|
||||
|
||||
// function
|
||||
Identifier functionId = node.function;
|
||||
|
||||
List<Expr> args = node.args;
|
||||
int spaceRequiredForArgs = (args.size() + 1)*4;
|
||||
//backend.emitSW(A0, SP, -4, "Put static link");
|
||||
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);
|
||||
// All expressions should save their end result in A0
|
||||
// So, once expr is evaluated add value inside A0 onto stack as an argument
|
||||
backend.emitSW(A0, SP, -4*slotNum, "Push argument " + argNum + " from left");
|
||||
}
|
||||
|
||||
backend.emitADDI(SP, SP, -spaceRequiredForArgs, "Set SP to last argument.");
|
||||
backend.emitJAL(new Label("$"+functionId.name), "Invoke function: " + functionId.name);
|
||||
backend.emitADDI(SP, SP, spaceRequiredForArgs, "Set SP to stack frame top.");
|
||||
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 Void analyze(MethodCallExpr node) {
|
||||
System.out.println("*** MethodCallExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside MethodCallExpr");
|
||||
// function
|
||||
return defaultAction(node);
|
||||
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 Void analyze(FuncDef node) {
|
||||
System.out.println("*** FuncDef");
|
||||
backend.emitLW(T6, FP, 0, "Inside FuncDef");
|
||||
// function
|
||||
return defaultAction(node);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Void analyze(GlobalDecl node) {
|
||||
System.out.println("*** GlobalDecl");
|
||||
backend.emitLW(T6, FP, 0, "Inside GlobalDecl");
|
||||
// function
|
||||
return defaultAction(node);
|
||||
public Register analyze(BinaryExpr node)
|
||||
{
|
||||
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(">"))
|
||||
{
|
||||
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
||||
backend.emitBEQ(Register.A0, Register.T0,elseBlock, comment);
|
||||
}
|
||||
else if(node.operator.equals(">="))
|
||||
backend.emitBLT(Register.A0, Register.T0,elseBlock, comment);
|
||||
else if(node.operator.equals("<="))
|
||||
{
|
||||
Label temp = generateLocalLabel();
|
||||
backend.emitBEQ(Register.A0, Register.T0,temp, "Branch on "+node.operator);
|
||||
backend.emitBGE(Register.A0, Register.T0,elseBlock, comment);
|
||||
backend.emitLocalLabel(temp, "True part of if check");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Void analyze(NonLocalDecl node) {
|
||||
System.out.println("*** NonLocalDecl");
|
||||
backend.emitLW(T6, FP, 0, "Inside NonLocalDecl");
|
||||
// function
|
||||
return defaultAction(node);
|
||||
public Register analyze(UnaryExpr node)
|
||||
{
|
||||
node.operand.dispatch(this);
|
||||
if(node.operator.equals("-"))
|
||||
{
|
||||
backend.emitLI(Register.T0, -1, "Set value of Register T0 to -1");
|
||||
backend.emitMUL(Register.A0, Register.A0, Register.T0, "Multiply by -1");
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// methods below are only for testing, remove or comment them when merging
|
||||
@Override
|
||||
public Void analyze(AssignStmt node) {
|
||||
System.out.println("+++ Inside AssignStmt");
|
||||
backend.emitLW(T6, FP, 0, "Inside AssignStmt");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(BinaryExpr node) {
|
||||
System.out.println("+++ Inside BinaryExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside BinaryExpr");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(BooleanLiteral node) {
|
||||
System.out.println("+++ Inside BooleanLiteral");
|
||||
backend.emitLW(T6, FP, 0, "Inside BooleanLiteral");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(ClassDef node) {
|
||||
System.out.println("+++ Inside ClassDef");
|
||||
backend.emitLW(T6, FP, 0, "Inside ClassDef");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(ClassType node) {
|
||||
System.out.println("+++ Inside ClassType");
|
||||
backend.emitLW(T6, FP, 0, "Inside ClassType");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(CompilerError node) {
|
||||
System.out.println("+++ Inside CompilerError");
|
||||
backend.emitLW(T6, FP, 0, "Inside CompilerError");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(Errors node) {
|
||||
System.out.println("+++ Inside Errors");
|
||||
backend.emitLW(T6, FP, 0, "Inside Errors");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(ExprStmt node) {
|
||||
System.out.println("+++ Inside ExprStmt");
|
||||
backend.emitLW(T6, FP, 0, "Inside ExprStmt: " + node.expr.kind);
|
||||
node.expr.dispatch(this);
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(ForStmt node) {
|
||||
System.out.println("+++ Inside ForStmt");
|
||||
backend.emitLW(T6, FP, 0, "Inside ForStmt");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Void analyze(Identifier node) {
|
||||
System.out.println("+++ Inside Identifier");
|
||||
// List<String> params = funcInfo.getParams();
|
||||
// int i = 0;
|
||||
// for (i = 0; i < params.size(); i++) {
|
||||
// if (params.get(i).equals(node.name)) break;
|
||||
// }
|
||||
// backend.emitLW(A0, SP, ((i+1)-params.size())*4, "Load param " + (i+1) + " from left");
|
||||
backend.emitLW(T6, FP, 0, "Inside Identifier: " + node.name);
|
||||
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 Void analyze(IfExpr node) {
|
||||
System.out.println("+++ Inside IfExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside IfExpr");
|
||||
return defaultAction(node);
|
||||
}
|
||||
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");
|
||||
|
||||
@Override
|
||||
public Void analyze(IfStmt node) {
|
||||
System.out.println("+++ Inside IfStmt");
|
||||
backend.emitLW(T6, FP, 0, "Inside IfStmt");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(IndexExpr node) {
|
||||
System.out.println("+++ Inside IndexExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside IndexExpr");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(IntegerLiteral node) {
|
||||
System.out.println("+++ Inside IntegerLiteral");
|
||||
backend.emitLA(A0, new Label("$int$prototype"), "Load prototype");
|
||||
backend.emitJAL(new Label("ra, alloc"), "");
|
||||
backend.emitLI(T0, node.value, "Load integer " + node.value);
|
||||
backend.emitSW(T0, A0, "@.__int__", null);
|
||||
backend.emitLW(T6, FP, 0, "Inside IntegerLiteral");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(ListExpr node) {
|
||||
System.out.println("+++ Inside ListExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside ListExpr");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(ListType node) {
|
||||
System.out.println("+++ Inside ListType");
|
||||
backend.emitLW(T6, FP, 0, "Inside ListType");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(MemberExpr node) {
|
||||
System.out.println("+++ Inside MemberExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside MemberExpr");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Void analyze(NoneLiteral node) {
|
||||
System.out.println("+++ Inside NoneLiteral");
|
||||
backend.emitLW(T6, FP, 0, "Inside NoneLiteral");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(Program node) {
|
||||
System.out.println("+++ Inside Program");
|
||||
backend.emitLW(T6, FP, 0, "Inside Program");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Void analyze(StringLiteral node) {
|
||||
System.out.println("+++ Inside StringLiteral");
|
||||
backend.emitLW(T6, FP, 0, "Inside StringLiteral");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(TypedVar node) {
|
||||
System.out.println("+++ Inside TypedVar");
|
||||
backend.emitLW(T6, FP, 0, "Inside TypedVar");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(UnaryExpr node) {
|
||||
System.out.println("+++ Inside UnaryExpr");
|
||||
backend.emitLW(T6, FP, 0, "Inside UnaryExpr");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(VarDef node) {
|
||||
System.out.println("+++ Inside VarDef");
|
||||
backend.emitLW(T6, FP, 0, "Inside VarDef");
|
||||
return defaultAction(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void analyze(WhileStmt node) {
|
||||
System.out.println("+++ Inside WhileStmt");
|
||||
backend.emitLW(T6, FP, 0, "Inside WhileStmt");
|
||||
return defaultAction(node);
|
||||
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);
|
||||
backend.emitSW(r,Register.FP,-sp_off*wordSize,"Push argument "+i+" from last.");
|
||||
sp_off++;
|
||||
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
|
||||
return Register.A0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// @Override
|
||||
// public Void analyze(IntegerLiteral node) {
|
||||
// System.out.println("+++ Inside IntegerLiteral");
|
||||
// backend.emitLA(A0, new Label("$int$prototype"), "Load prototype");
|
||||
// backend.emitJAL(new Label("ra, alloc"), "");
|
||||
// backend.emitLI(T0, node.value, "Load integer " + node.value);
|
||||
// backend.emitSW(T0, A0, "@.__int__", null);
|
||||
// backend.emitLW(T6, FP, 0, "Inside IntegerLiteral");
|
||||
// return defaultAction(node);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// extras end here
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user