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

BUG FIX: nested implementation bug, id_local bug

parent b8445de5
No related branches found
No related tags found
No related merge requests found
Pipeline #392 passed with warnings with stages
in 27 minutes and 36 seconds
......@@ -221,6 +221,7 @@ public class CodeGenImpl extends CodeGenBase {
}
betterBackend.emitFunctionBegin(funcInfo.getFuncName(), null);
stmtAnalyzer.pushLocalVars(funcInfo.getLocals()); // regMgr should NEVER be used, because of this statement.
// Emit code for function stmts
for (Stmt stmt : funcInfo.getStatements()) {
......@@ -277,20 +278,26 @@ public class CodeGenImpl extends CodeGenBase {
// Extend StackVarInfo to add more info.
private Map<String, Integer> localMap = new HashMap<>(); // Map from StackVarInfo to offset of FP.
public void initArgsMap(List<String> args) {
private void initArgsMap(List<String> args) {
for(int i = 0; i < args.size(); ++i) {
int offset = (args.size() - i - 1) * backend.getWordSize();
localMap.put(args.get(i), offset);
}
}
public void initLocalVars(List<StackVarInfo> locals, int firstLocalOffset) {
private void initLocalVars(List<StackVarInfo> locals, int firstLocalOffset) {
for(int i = 0; i < locals.size(); ++i) {
StackVarInfo theLocal = locals.get(i);
int offset = firstLocalOffset - i * backend.getWordSize();
localMap.put(theLocal.getVarName(), offset);
}
}
public void pushLocalVars(List<StackVarInfo> locals) {
// This function generates some asm code, so it should only be called at RIGHT time.
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);
}
}
private void emitLoadNestedParentVar(StackVarInfo varInfo, RiscVBackend.Register tmpReg, RiscVBackend.Register valReg_to_assign) {
......@@ -310,10 +317,10 @@ public class CodeGenImpl extends CodeGenBase {
++counter;
}
assert curr != null;
assert counter == curr.getDepth() - hisOwner.getDepth();
assert counter == funcInfo.getDepth() - hisOwner.getDepth();
//! assert end
int depthDiff = curr.getDepth() - hisOwner.getDepth();
int depthDiff = funcInfo.getDepth() - hisOwner.getDepth();
// If depthDiff is 0, then the variable is in current frame.
backend.emitMV(tmpReg, FP, "LOAD NESTED VAR: Load current FP.");
for(int i = 0; i < depthDiff; ++i) {
......@@ -339,9 +346,10 @@ public class CodeGenImpl extends CodeGenBase {
} else {
sym = funcInfo.getSymbolTable();
}
epilogue = generateLocalLabel();
if(funcInfo != null) {
// The init function below won't generate any asm code, so the stmtAnalyzer can be constructed at any time.
// because the constructor doesn't generate code! It won't affect anything.
initArgsMap(funcInfo.getParams());
initLocalVars(funcInfo.getLocals(), -3 * backend.getWordSize());
mapFuncInfoToStmtAnalyzer.put(funcInfo, this);
......
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