Skip to content
Snippets Groups Projects
Verified Commit fd08b327 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

[DO NOT BUILD] fix func calling

parent 37ea8f4f
No related branches found
No related tags found
No related merge requests found
package chocopy.pa3;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Stack;
import java.util.*;
import chocopy.common.analysis.SymbolTable;
import chocopy.common.analysis.AbstractNodeAnalyzer;
......@@ -219,7 +216,6 @@ public class CodeGenImpl extends CodeGenBase {
stmt.dispatch(stmtAnalyzer);
}
backend.emitLocalLabel(stmtAnalyzer.epilogue, "Epilogue");
betterBackend.emitFunctionEnd(funcInfo.getFuncName(), null, null);
}
......@@ -268,6 +264,16 @@ public class CodeGenImpl extends CodeGenBase {
private int _r_local_label_counter = 427;
// Extend StackVarInfo to add more info.
private Map<String, Integer> funcArgsMap = new HashMap<>(); // Map from StackVarInfo to offset of FP.
public void initArgsMap(List<String> args) {
for(int i = 0; i < args.size(); ++i) {
int offset = (i) * backend.getWordSize();
funcArgsMap.put(args.get(i), offset);
}
}
/** An analyzer for the function described by FUNCINFO0, which is null
* for the top level. */
StmtAnalyzer(FuncInfo funcInfo0) {
......@@ -320,12 +326,15 @@ public class CodeGenImpl extends CodeGenBase {
// do assign
if (symbolInfo instanceof GlobalVarInfo) {
GlobalVarInfo globalVarInfo = (GlobalVarInfo) symbolInfo;
symbolType = globalVarInfo.getVarType();
backend.emitSW(tmpReg, globalVarInfo.getLabel(), tmpReg2, "ASSIGN: Assign to global var");
} else if (symbolInfo instanceof StackVarInfo) {
StackVarInfo stackVarInfo = (StackVarInfo) symbolInfo;
symbolType = stackVarInfo.getVarType();
throw new RuntimeException("ASSIGN to stack variable: NOT IMPLEMENTED: StackVarInfo not implemented.");
if(funcArgsMap.containsKey(stackVarInfo.getVarName())) {
// is a function argument.
backend.emitSW(tmpReg, FP, funcArgsMap.get(stackVarInfo.getVarName()), "Assign to function param var");
}
else
throw new RuntimeException("ASSIGN to stack variable: NOT IMPLEMENTED: StackVarInfo not implemented.");
}
}
return null;
......@@ -453,15 +462,20 @@ public class CodeGenImpl extends CodeGenBase {
@Override
public Void analyze(Identifier id) {
// NOT DONE
SymbolInfo ident = sym.get(id.name);
// global var case
if (ident instanceof GlobalVarInfo) {
backend.emitLW(A0, ((GlobalVarInfo) ident).getLabel(), "Load identifier");
backend.emitSW(A0, SP, 0, "Push to stack");
} else if (ident instanceof StackVarInfo){ // local variable
//TODO: Figure out how we can keep track of whether the var has already been pushed to stack.
StackVarInfo identVar = (StackVarInfo) ident;
if(funcArgsMap.containsKey(identVar.getVarName())) {
// is a function arg.
backend.emitLW(T6, FP, funcArgsMap.get(identVar.getVarName()), "Idnet:get FuncArg identifier value to reg");
backend.emitSW(T6, SP, 0, "Push to stack without decrease SP");
return null; // DO NOT RUN THE FUCKING SILLY CODE BELOW
}
// TODO: Function arg works, but local var won't work.
if (id.getInferredType().equals(SymbolType.INT_TYPE)) {
IntegerLiteral value = (IntegerLiteral) (identVar.getInitialValue());
backend.emitLI(A0, value.value, "Load initial value of StackVar");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment