diff --git a/src/main/java/chocopy/pa3/CodeGenImpl.java b/src/main/java/chocopy/pa3/CodeGenImpl.java index 81e3338..fee32c7 100644 --- a/src/main/java/chocopy/pa3/CodeGenImpl.java +++ b/src/main/java/chocopy/pa3/CodeGenImpl.java @@ -1,18 +1,43 @@ 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; +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.*; @@ -21,8 +46,12 @@ 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. * @@ -44,6 +73,68 @@ public class CodeGenImpl extends CodeGenBase public CodeGenImpl(RiscVBackend backend) { super(backend); + + } + + /** + * Generates assembly code for PROGRAM. + * + *
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. */
@@ -56,6 +147,11 @@ public class CodeGenImpl extends CodeGenBase
/** Not implemented. */
private final Label errorNI = new Label("error.NI");
+
+ /** Label for built-in routine: makeint. */
+ protected final Label makeIntLabel = new Label("makeint");
+
+
/**
* Emits the top level of the program.
*
@@ -97,17 +193,54 @@ public class CodeGenImpl extends CodeGenBase
protected void emitUserDefinedFunction(FuncInfo funcInfo)
{
backend.emitGlobalLabel(funcInfo.getCodeLabel());
+
+
+
+ // --- Prologue ---
+ // space for return address = 1
+ // space for control link = 1
+ // space for static link = 1
+ // space for params = num of params
+ // space for locals = num of locals
+ int requiredStackSpace = (1 + 1 + 1 + funcInfo.getLocals().size() + funcInfo.getParams().size())*wordSize;
+
+ backend.emitADDI(SP, SP, -requiredStackSpace, "Reserve space for stack frame.");
+ backend.emitSW(RA, SP, requiredStackSpace-4, "return address");
+ backend.emitSW(FP, SP, requiredStackSpace-8, "control link");
+ // if we want to add static link
+ //backend.emitSW(FP, SP, requiredStackSpace-12, "static link");
+ backend.emitADDI(FP, SP, requiredStackSpace, "New fp is at old SP.");
+
+
StmtAnalyzer stmtAnalyzer = new StmtAnalyzer(funcInfo);
-
- for (Stmt stmt : funcInfo.getStatements())
- {
+ int emptySlotNum = 4;
+ for (StackVarInfo var : funcInfo.getLocals()) {
+ // store at requiredStackSpace-emptySlotNum*wordSize(SP), then emptySlotNum++
+ Literal varLitral = var.getInitialValue();
+ varLitral.dispatch(stmtAnalyzer);
+ // All Literals should save locations for the values in A0
+ backend.emitSW(A0, SP, requiredStackSpace-emptySlotNum*wordSize, "Push local variable " + var.getVarName() + " onto stack");
+ emptySlotNum++;
+ }
+
+ // --- Function Body ---
+ // statements load all the variables that caller put on stack
+ // statements use fp to load the variables
+ // example: 0(fp) is the last variable (z) while 8(fp) is the first variable (x)
+ // for function with 3 params f(x, y, z)
+ for (Stmt stmt : funcInfo.getStatements()) {
+ //System.out.println(stmt.toString());
stmt.dispatch(stmtAnalyzer);
}
+ backend.emitJ(stmtAnalyzer.epilogue, "Jump to function epilogue");
- backend.emitMV(A0, ZERO, "Returning None implicitly");
+
+
+ // --- Epilogue ---
backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue");
-
- // FIXME: {... reset fp etc. ...}
+ backend.emitLW(RA, FP, -4, "Get return address");
+ backend.emitLW(FP, FP, -8, "Use control link to restore caller's fp");
+ backend.emitADDI(SP, SP, requiredStackSpace, "Restore stack pointer");
backend.emitJR(RA, "Return to caller");
}
@@ -182,8 +315,14 @@ public class CodeGenImpl extends CodeGenBase
sp_off+=i+1;
max_sp = max_sp >= sp_off?max_sp:sp_off;
}
-
+
+
+
+
+ // *********** functions start ***********
+
public Register analyze(CallExpr node) {
+ backend.emitLW(T6, FP, 0, "Inside CallExpr: " + node.function.name);
SymbolInfo Ty = globalSymbols.get(node.function.name);
if(Ty instanceof ClassInfo){
//object create
@@ -214,44 +353,150 @@ public class CodeGenImpl extends CodeGenBase
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 {
- //func call
+ } else {
+ // function
+ Identifier functionId = node.function;
+
+ List