diff options
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -146,7 +146,9 @@ std::list<ThunkAST>::iterator buildThunk(std::list<ThunkAST>::iterator it) curEnv = it->env; llvmState.lastSp = it->lastSp; - for (auto& a : it->ast) { + for (auto ait = it->ast.begin(); ait != it->ast.end(); ++ait) { + auto& a = *ait; + if (a->name.starts_with("__t")) { it->lastSp = llvmState.lastSp; next = buildThunk(next); @@ -155,8 +157,17 @@ std::list<ThunkAST>::iterator buildThunk(std::list<ThunkAST>::iterator it) llvmState.lastSp = it->lastSp; } - if (a->codegen(llvmState) == nullptr) { - return scope.end(); + auto next = ait; + ++next; + if (next != it->ast.end() && dynamic_cast<PushAST *>(ait->get()) && dynamic_cast<PopAST *>(next->get())) { + // Value (e.g. thunk) is immediately pushed then popped into a variable. + // Shortcut and simply rename the value: + Var::rename(a->name, (*next)->name); + it->ast.erase(next); + } else { + // Otherwise, just do the regular codegen. + if (a->codegen(llvmState) == nullptr) + return scope.end(); } } |