|
|
@ -24,7 +24,6 @@ extern llvm::Function *curFunc;
|
|
|
|
extern llvm::Value *curEnv;
|
|
|
|
extern llvm::Value *curEnv;
|
|
|
|
|
|
|
|
|
|
|
|
int ThunkAST::tcount = 0;
|
|
|
|
int ThunkAST::tcount = 0;
|
|
|
|
int ThunkAST::envidx = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static inline auto loadEnv(LLVMState& llvmState, llvm::Value *index)
|
|
|
|
static inline auto loadEnv(LLVMState& llvmState, llvm::Value *index)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -61,7 +60,7 @@ PushAST::PushAST(const std::string& n): BaseAST(n) {}
|
|
|
|
llvm::Value *PushAST::codegen(LLVMState& llvmState) const
|
|
|
|
llvm::Value *PushAST::codegen(LLVMState& llvmState) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (auto [var, native] = Var::lookup(name, 1); var) {
|
|
|
|
if (auto [var, native] = Var::lookup(name, 1); var) {
|
|
|
|
auto index = llvm::ConstantInt::get(llvmState.inttype, ThunkAST::envidx++);
|
|
|
|
auto index = llvm::ConstantInt::get(llvmState.inttype, llvmState.envidx++);
|
|
|
|
Var::addLocal(name, index);
|
|
|
|
Var::addLocal(name, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -85,7 +84,7 @@ llvm::Value *PopAST::codegen(LLVMState& llvmState) const
|
|
|
|
if (name == "self") {
|
|
|
|
if (name == "self") {
|
|
|
|
v = {curFunc, true};
|
|
|
|
v = {curFunc, true};
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
auto index = llvm::ConstantInt::get(llvmState.inttype, ThunkAST::envidx++);
|
|
|
|
auto index = llvm::ConstantInt::get(llvmState.inttype, llvmState.envidx++);
|
|
|
|
auto pop = llvmState.createPop();
|
|
|
|
auto pop = llvmState.createPop();
|
|
|
|
storeEnv(llvmState, index, pop);
|
|
|
|
storeEnv(llvmState, index, pop);
|
|
|
|
|
|
|
|
|
|
|
@ -100,7 +99,7 @@ CallAST::CallAST(const std::string& n): BaseAST(n) {}
|
|
|
|
llvm::Value *CallAST::codegen(LLVMState& llvmState) const
|
|
|
|
llvm::Value *CallAST::codegen(LLVMState& llvmState) const
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (auto [var, native] = Var::lookup(name, 1); var && !native) {
|
|
|
|
if (auto [var, native] = Var::lookup(name, 1); var && !native) {
|
|
|
|
auto index = llvm::ConstantInt::get(llvmState.inttype, ThunkAST::envidx++);
|
|
|
|
auto index = llvm::ConstantInt::get(llvmState.inttype, llvmState.envidx++);
|
|
|
|
Var::addLocal(name, index);
|
|
|
|
Var::addLocal(name, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -118,6 +117,8 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
|
|
|
|
Var::addGlobal(name, Var {fn, true});
|
|
|
|
Var::addGlobal(name, Var {fn, true});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llvmState.commitSp();
|
|
|
|
|
|
|
|
|
|
|
|
bool couldRecur = Var::lookup("self").value != nullptr;
|
|
|
|
bool couldRecur = Var::lookup("self").value != nullptr;
|
|
|
|
auto localCount = Var::vars.back().size();
|
|
|
|
auto localCount = Var::vars.back().size();
|
|
|
|
if (!couldRecur || localCount == 0) {
|
|
|
|
if (!couldRecur || localCount == 0) {
|
|
|
@ -168,12 +169,14 @@ void ThunkAST::beginGen(LLVMState& llvmState)
|
|
|
|
entry = llvmState.createEntry(func);
|
|
|
|
entry = llvmState.createEntry(func);
|
|
|
|
body = llvm::BasicBlock::Create(llvmState.ctx, "body", func);
|
|
|
|
body = llvm::BasicBlock::Create(llvmState.ctx, "body", func);
|
|
|
|
env = func->getArg(0);
|
|
|
|
env = func->getArg(0);
|
|
|
|
|
|
|
|
lastSp = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
llvmState.builder.SetInsertPoint(body);
|
|
|
|
llvmState.builder.SetInsertPoint(body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThunkAST::endGen(LLVMState& llvmState)
|
|
|
|
void ThunkAST::endGen(LLVMState& llvmState)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
llvmState.commitSp();
|
|
|
|
llvmState.builder.CreateRetVoid();
|
|
|
|
llvmState.builder.CreateRetVoid();
|
|
|
|
llvmState.builder.SetInsertPoint(entry);
|
|
|
|
llvmState.builder.SetInsertPoint(entry);
|
|
|
|
|
|
|
|
|
|
|
|