|
|
@ -1,27 +1,45 @@
|
|
|
|
package chocopy.pa3;
|
|
|
|
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.List;
|
|
|
|
|
|
|
|
|
|
|
|
import chocopy.common.analysis.AbstractNodeAnalyzer;
|
|
|
|
import chocopy.common.analysis.AbstractNodeAnalyzer;
|
|
|
|
import chocopy.common.analysis.SymbolTable;
|
|
|
|
import chocopy.common.analysis.SymbolTable;
|
|
|
|
import chocopy.common.astnodes.ReturnStmt;
|
|
|
|
import chocopy.common.analysis.types.Type;
|
|
|
|
import chocopy.common.astnodes.Stmt;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.StringLiteral;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.AssignStmt;
|
|
|
|
import chocopy.common.astnodes.AssignStmt;
|
|
|
|
import chocopy.common.astnodes.BinaryExpr;
|
|
|
|
import chocopy.common.astnodes.BinaryExpr;
|
|
|
|
import chocopy.common.astnodes.BooleanLiteral;
|
|
|
|
import chocopy.common.astnodes.BooleanLiteral;
|
|
|
|
import chocopy.common.astnodes.Expr;
|
|
|
|
import chocopy.common.astnodes.Expr;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.ExprStmt;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.ForStmt;
|
|
|
|
import chocopy.common.astnodes.Identifier;
|
|
|
|
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.IntegerLiteral;
|
|
|
|
import chocopy.common.astnodes.Literal;
|
|
|
|
import chocopy.common.astnodes.ListExpr;
|
|
|
|
import chocopy.common.codegen.*;
|
|
|
|
import chocopy.common.astnodes.ListType;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.NoneLiteral;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.ReturnStmt;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.Stmt;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.StringLiteral;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.TypedVar;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.UnaryExpr;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.VarDef;
|
|
|
|
|
|
|
|
import chocopy.common.astnodes.WhileStmt;
|
|
|
|
|
|
|
|
import chocopy.common.codegen.CodeGenBase;
|
|
|
|
|
|
|
|
import chocopy.common.codegen.FuncInfo;
|
|
|
|
|
|
|
|
import chocopy.common.codegen.GlobalVarInfo;
|
|
|
|
import chocopy.common.codegen.Label;
|
|
|
|
import chocopy.common.codegen.Label;
|
|
|
|
|
|
|
|
import chocopy.common.codegen.RiscVBackend;
|
|
|
|
import chocopy.common.codegen.RiscVBackend.Register;
|
|
|
|
import chocopy.common.codegen.RiscVBackend.Register;
|
|
|
|
|
|
|
|
import chocopy.common.codegen.SymbolInfo;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import chocopy.common.analysis.types.Type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static chocopy.common.codegen.RiscVBackend.Register.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This is where the main implementation of PA3 will live.
|
|
|
|
* This is where the main implementation of PA3 will live.
|
|
|
@ -165,37 +183,45 @@ public class CodeGenImpl extends CodeGenBase {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FIXME: More, of course.
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(AssignStmt node) {
|
|
|
|
public Void analyze(NoneLiteral node) {
|
|
|
|
if (sym.getParent() == null) {
|
|
|
|
backend.emitMV(Register.A0, Register.ZERO, "Load none");
|
|
|
|
Type t = node.value.getInferredType();
|
|
|
|
return null;
|
|
|
|
for(Expr target: node.targets)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(StringLiteral node) {
|
|
|
|
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
|
|
|
|
Label l = constants.getStrConstant(node.value);
|
|
|
|
if(t==Type.INT_TYPE)
|
|
|
|
backend.emitLA(Register.A0, l, "Load string literal");
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
IntegerLiteral lt = (IntegerLiteral)node.value;
|
|
|
|
}
|
|
|
|
backend.emitLI(Register.A0, lt.value, "Load integer literal "+lt.value);
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(IntegerLiteral node) {
|
|
|
|
|
|
|
|
backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(t==Type.BOOL_TYPE)
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(BooleanLiteral node)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
BooleanLiteral lt = (BooleanLiteral)node.value;
|
|
|
|
if(node.value==true)
|
|
|
|
if(lt.value==true)
|
|
|
|
backend.emitLI(Register.A0, 1, "Load boolean literal: true ");
|
|
|
|
backend.emitLI(Register.A0, 1, "Load boolean literal "+1);
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
backend.emitLI(Register.A0, 0, "Load boolean literal "+0);
|
|
|
|
backend.emitLI(Register.A0, 0, "Load boolean literal: false ");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(AssignStmt node)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
StringLiteral lt = (StringLiteral)node.value;
|
|
|
|
if (sym.getParent() == null)
|
|
|
|
System.out.println(strClass.getClassName()+" "+strClass.getTypeTag()+" "+strClass.getDispatchTableLabel().labelName);
|
|
|
|
{
|
|
|
|
Label l = constants.getStrConstant(lt.value);
|
|
|
|
Type t = node.value.getInferredType();
|
|
|
|
backend.emitLA(Register.A0, l, "Load string literal");
|
|
|
|
for(Expr target: node.targets)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
if(t.isSpecialType())
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
|
|
|
|
|
|
|
|
node.value.dispatch(this);
|
|
|
|
backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)");
|
|
|
|
backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
@ -212,11 +238,80 @@ public class CodeGenImpl extends CodeGenBase {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Void analyze(BinaryExpr node) {
|
|
|
|
public Void analyze(BinaryExpr node) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(ExprStmt node) {
|
|
|
|
|
|
|
|
node.expr.dispatch(this);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(ForStmt node) {
|
|
|
|
|
|
|
|
// control flow
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(Identifier node) {
|
|
|
|
|
|
|
|
// statement
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(IfExpr node) {
|
|
|
|
|
|
|
|
// control flow
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(IfStmt node) {
|
|
|
|
|
|
|
|
// control flow
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(IndexExpr node) {
|
|
|
|
|
|
|
|
// statement
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(ListExpr node) {
|
|
|
|
|
|
|
|
// statement
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(ListType node) {
|
|
|
|
|
|
|
|
// statement
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(TypedVar node) {
|
|
|
|
// statement
|
|
|
|
// statement
|
|
|
|
return defaultAction(node);
|
|
|
|
return defaultAction(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(UnaryExpr node) {
|
|
|
|
|
|
|
|
// statement
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(VarDef node) {
|
|
|
|
|
|
|
|
// statement
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(WhileStmt node) {
|
|
|
|
|
|
|
|
// control flow
|
|
|
|
|
|
|
|
return defaultAction(node);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|