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
./main < test.fp > forsp.ir
llc -march=x86 -filetype=obj --relocation-model=pic forsp.ir -O1
$(CC) -c support.c -m32 -Os
$(CC) support.o forsp.ir.o -m32 -Os
llc -march=x86 -filetype=obj -relocation-model=pic forsp.ir -O2
$(CC) -c support.c -m32 -O2
$(CC) support.o forsp.ir.o -m32 -O2
clean:
rm -f a.out main *.ir *.o

@ -117,15 +117,17 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
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();
llvm::Type *type;
llvm::Value *mem;
if (auto sz = Var::vars.back().size(); sz > 0) {
type = llvm::VectorType::get(llvmState.inttype, sz, false);
mem = llvmState.builder.CreateAlloca(type, nullptr);
auto type = llvm::VectorType::get(llvmState.inttype, localCount, false);
auto mem = llvmState.builder.CreateAlloca(type, nullptr);
int i = 0;
i = 0;
for (auto& [_, v] : Var::vars.back()) {
if (!v.native) {
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);
}
}
}
auto call = llvmState.builder.CreateCall(llvmState.ftype, fn, llvm::ArrayRef {scope.back().env});
if (auto sz = Var::vars.back().size(); sz > 0) {
int i = 0;
i = 0;
for (auto& [_, v] : Var::vars.back()) {
if (!v.native) {
auto index = llvm::ConstantInt::get(llvmState.inttype, i++);
@ -147,9 +147,9 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
storeEnv(llvmState, v.value, l);
}
}
}
return call;
}
}
ThunkAST::ThunkAST(LLVMState& llvmState):

@ -53,7 +53,9 @@ int main()
auto envtype = llvm::VectorType::get(llvmState.inttype, ThunkAST::envidx, false);
auto [t0, _] = Var::lookup("__t0");
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.CreateRetVoid();

Loading…
Cancel
Save