take env off stack; only save locals for recursion

main
Clyne 6 months ago
parent b16e418091
commit 0d8932e969
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -9,9 +9,9 @@ main: $(subst .cpp,.o,$(CXXFILES))
prog: main test.fp prog: main test.fp
./main < test.fp > forsp.ir ./main < test.fp > forsp.ir
llc -march=x86 -filetype=obj --relocation-model=pic forsp.ir -O1 llc -march=x86 -filetype=obj -relocation-model=pic forsp.ir -O2
$(CC) -c support.c -m32 -Os $(CC) -c support.c -m32 -O2
$(CC) support.o forsp.ir.o -m32 -Os $(CC) support.o forsp.ir.o -m32 -O2
clean: clean:
rm -f a.out main *.ir *.o rm -f a.out main *.ir *.o

@ -117,15 +117,17 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
Var::addGlobal(name, Var {fn, true}); Var::addGlobal(name, Var {fn, true});
} }
bool couldRecur = Var::lookup("self").value != nullptr;
auto localCount = Var::vars.back().size();
if (!couldRecur || localCount == 0) {
return llvmState.builder.CreateCall(llvmState.ftype, fn, llvm::ArrayRef {scope.back().env});
} else {
int i;
auto ptrty = llvmState.inttype->getPointerTo(); auto ptrty = llvmState.inttype->getPointerTo();
llvm::Type *type; auto type = llvm::VectorType::get(llvmState.inttype, localCount, false);
llvm::Value *mem; auto mem = llvmState.builder.CreateAlloca(type, nullptr);
if (auto sz = Var::vars.back().size(); sz > 0) {
type = llvm::VectorType::get(llvmState.inttype, sz, false);
mem = llvmState.builder.CreateAlloca(type, nullptr);
int i = 0; i = 0;
for (auto& [_, v] : Var::vars.back()) { for (auto& [_, v] : Var::vars.back()) {
if (!v.native) { if (!v.native) {
auto index = llvm::ConstantInt::get(llvmState.inttype, i++); auto index = llvm::ConstantInt::get(llvmState.inttype, i++);
@ -133,12 +135,10 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
llvmState.builder.CreateStore(loadEnv(llvmState, v.value), m, false); llvmState.builder.CreateStore(loadEnv(llvmState, v.value), m, false);
} }
} }
}
auto call = llvmState.builder.CreateCall(llvmState.ftype, fn, llvm::ArrayRef {scope.back().env}); auto call = llvmState.builder.CreateCall(llvmState.ftype, fn, llvm::ArrayRef {scope.back().env});
if (auto sz = Var::vars.back().size(); sz > 0) { i = 0;
int i = 0;
for (auto& [_, v] : Var::vars.back()) { for (auto& [_, v] : Var::vars.back()) {
if (!v.native) { if (!v.native) {
auto index = llvm::ConstantInt::get(llvmState.inttype, i++); auto index = llvm::ConstantInt::get(llvmState.inttype, i++);
@ -147,10 +147,10 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
storeEnv(llvmState, v.value, l); storeEnv(llvmState, v.value, l);
} }
} }
}
return call; return call;
} }
}
ThunkAST::ThunkAST(LLVMState& llvmState): ThunkAST::ThunkAST(LLVMState& llvmState):
ThunkAST(llvmState, std::string("__t") + std::to_string(tcount++)) {} ThunkAST(llvmState, std::string("__t") + std::to_string(tcount++)) {}

@ -53,7 +53,9 @@ int main()
auto envtype = llvm::VectorType::get(llvmState.inttype, ThunkAST::envidx, false); auto envtype = llvm::VectorType::get(llvmState.inttype, ThunkAST::envidx, false);
auto [t0, _] = Var::lookup("__t0"); auto [t0, _] = Var::lookup("__t0");
llvmState.builder.SetInsertPoint(entry); llvmState.builder.SetInsertPoint(entry);
auto env = llvmState.builder.CreateAlloca(envtype, nullptr); auto zerovec = llvm::ConstantVector::get(llvm::ArrayRef(llvmState.zero));
auto env = new llvm::GlobalVariable(llvmState.modul, envtype, false,
llvm::GlobalValue::InternalLinkage, zerovec, "env");
llvmState.builder.CreateCall(llvmState.ftype, t0, llvm::ArrayRef<llvm::Value *> {env}); llvmState.builder.CreateCall(llvmState.ftype, t0, llvm::ArrayRef<llvm::Value *> {env});
llvmState.builder.CreateRetVoid(); llvmState.builder.CreateRetVoid();

Loading…
Cancel
Save