Literals and Global Assignment

master
Apoorva Ranade 3 years ago
parent 913fd45dc1
commit 2f0acdbbf5

@ -1,27 +1,45 @@
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.SymbolTable;
import chocopy.common.astnodes.ReturnStmt;
import chocopy.common.astnodes.Stmt;
import chocopy.common.astnodes.StringLiteral;
import chocopy.common.analysis.types.Type;
import chocopy.common.astnodes.AssignStmt;
import chocopy.common.astnodes.BinaryExpr;
import chocopy.common.astnodes.BooleanLiteral;
import chocopy.common.astnodes.Expr;
import chocopy.common.astnodes.ExprStmt;
import chocopy.common.astnodes.ForStmt;
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.Literal;
import chocopy.common.codegen.*;
import chocopy.common.astnodes.ListExpr;
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.RiscVBackend;
import chocopy.common.codegen.RiscVBackend.Register;
import java.util.List;
import chocopy.common.analysis.types.Type;
import static chocopy.common.codegen.RiscVBackend.Register.*;
import chocopy.common.codegen.SymbolInfo;
/**
* This is where the main implementation of PA3 will live.
@ -165,37 +183,45 @@ public class CodeGenImpl extends CodeGenBase {
return null;
}
// FIXME: More, of course.
@Override
public Void analyze(AssignStmt node) {
if (sym.getParent() == null) {
public Void analyze(NoneLiteral node) {
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();
for(Expr target: node.targets)
{
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
if(t.isSpecialType())
{
StringLiteral lt = (StringLiteral)node.value;
System.out.println(strClass.getClassName()+" "+strClass.getTypeTag()+" "+strClass.getDispatchTableLabel().labelName);
Label l = constants.getStrConstant(lt.value);
backend.emitLA(Register.A0, l, "Load string literal");
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
@ -209,14 +235,83 @@ public class CodeGenImpl extends CodeGenBase {
}
return null;
}
@Override
public Void analyze(BinaryExpr node) {
// statement
return defaultAction(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
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);
}
}
/**

Loading…
Cancel
Save