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

fix id_local

parent f4fae353
No related branches found
No related tags found
No related merge requests found
Pipeline #393 canceled with stages
......@@ -214,9 +214,7 @@ public class CodeGenImpl extends CodeGenBase {
betterBackend.emitFunctionBegin(funcInfo.getFuncName(), null);
stmtAnalyzer.initArgsMap(funcInfo.getParams());
for(StackVarInfo local : funcInfo.getLocals()) {
// TODO: Do some thing to local variable
}
stmtAnalyzer.initLocalVars(funcInfo.getLocals(), -3 * backend.getWordSize());
// Emit code for function stmts
for (Stmt stmt : funcInfo.getStatements()) {
......@@ -273,11 +271,21 @@ public class CodeGenImpl extends CodeGenBase {
// Extend StackVarInfo to add more info.
private Map<String, Integer> funcArgsMap = new HashMap<>(); // Map from StackVarInfo to offset of FP.
private Map<String, Integer> localMap = 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 = (args.size() - i - 1) * backend.getWordSize();
funcArgsMap.put(args.get(i), offset);
localMap.put(args.get(i), offset);
}
}
public void initLocalVars(List<StackVarInfo> locals, int firstLocalOffset) {
for(int i = 0; i < locals.size(); ++i) {
StackVarInfo theLocal = locals.get(i);
theLocal.getInitialValue().dispatch(this);
// The literal initial value is pushed to stack. That's the local!
// No need to push it again.
int offset = firstLocalOffset - i * backend.getWordSize();
localMap.put(theLocal.getVarName(), offset);
}
}
......@@ -336,9 +344,9 @@ public class CodeGenImpl extends CodeGenBase {
backend.emitSW(tmpReg, globalVarInfo.getLabel(), tmpReg2, "ASSIGN: Assign to global var");
} else if (symbolInfo instanceof StackVarInfo) {
StackVarInfo stackVarInfo = (StackVarInfo) symbolInfo;
if(funcArgsMap.containsKey(stackVarInfo.getVarName())) {
if(localMap.containsKey(stackVarInfo.getVarName())) {
// is a function argument.
backend.emitSW(tmpReg, FP, funcArgsMap.get(stackVarInfo.getVarName()), "Assign to function param var");
backend.emitSW(tmpReg, FP, localMap.get(stackVarInfo.getVarName()), "Assign to function param var");
}
else
throw new RuntimeException("ASSIGN to stack variable: NOT IMPLEMENTED: StackVarInfo not implemented.");
......@@ -474,18 +482,12 @@ public class CodeGenImpl extends CodeGenBase {
betterBackend.emitPush(A0, "Ident: GlobalVar");
} else if (ident instanceof StackVarInfo){ // local variable
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");
if(localMap.containsKey(identVar.getVarName())) {
// is a function arg or local var.
backend.emitLW(T6, FP, localMap.get(identVar.getVarName()), "Idnet:get FuncArg identifier value to reg");
betterBackend.emitPush(T6, "Ident: Arg");
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");
betterBackend.emitPush(A0, "Ident: LocalVar");
}
}
return null;
}
......
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