|
|
@ -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");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(StringLiteral node) {
|
|
|
|
|
|
|
|
Label l = constants.getStrConstant(node.value);
|
|
|
|
|
|
|
|
backend.emitLA(Register.A0, l, "Load string literal");
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(IntegerLiteral node) {
|
|
|
|
|
|
|
|
backend.emitLI(Register.A0, node.value, "Load integer literal "+node.value);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void 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 null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Void analyze(AssignStmt node)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (sym.getParent() == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
Type t = node.value.getInferredType();
|
|
|
|
Type t = node.value.getInferredType();
|
|
|
|
for(Expr target: node.targets)
|
|
|
|
for(Expr target: node.targets)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if(t.isSpecialType())
|
|
|
|
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
|
|
|
|
|
|
|
|
if(t==Type.INT_TYPE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
IntegerLiteral lt = (IntegerLiteral)node.value;
|
|
|
|
|
|
|
|
backend.emitLI(Register.A0, lt.value, "Load integer literal "+lt.value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(t==Type.BOOL_TYPE)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
BooleanLiteral lt = (BooleanLiteral)node.value;
|
|
|
|
|
|
|
|
if(lt.value==true)
|
|
|
|
|
|
|
|
backend.emitLI(Register.A0, 1, "Load boolean literal "+1);
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
backend.emitLI(Register.A0, 0, "Load boolean literal "+0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
StringLiteral lt = (StringLiteral)node.value;
|
|
|
|
GlobalVarInfo gvi=(GlobalVarInfo)sym.get(((Identifier)target).name);
|
|
|
|
System.out.println(strClass.getClassName()+" "+strClass.getTypeTag()+" "+strClass.getDispatchTableLabel().labelName);
|
|
|
|
node.value.dispatch(this);
|
|
|
|
Label l = constants.getStrConstant(lt.value);
|
|
|
|
backend.emitSW(Register.A0, gvi.getLabel(), Register.T0, "Assign global: "+gvi.getVarName()+"(using tmp register)");
|
|
|
|
backend.emitLA(Register.A0, l, "Load string literal");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
// statement
|
|
|
|
return null;
|
|
|
|
return defaultAction(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@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
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|