aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-06-25 17:29:45 -0400
committerClyne Sullivan <clyne@bitgloo.com>2024-06-25 17:29:45 -0400
commit0d8932e96934dbeb0f1e21872693ca833a64341f (patch)
tree93c95a0bd94f143362144d8a0a7704a797eb8ac5
parentb16e418091bad151ed064b56f6c4d195242044d7 (diff)
take env off stack; only save locals for recursion
-rw-r--r--Makefile6
-rw-r--r--ast.cpp28
-rw-r--r--main.cpp4
3 files changed, 20 insertions, 18 deletions
diff --git a/Makefile b/Makefile
index fcba1f5..7d20637 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/ast.cpp b/ast.cpp
index 0e2d8c5..6aaa8a0 100644
--- a/ast.cpp
+++ b/ast.cpp
@@ -117,15 +117,17 @@ llvm::Value *CallAST::codegen(LLVMState& llvmState) const
Var::addGlobal(name, Var {fn, true});
}
- 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);
+ 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 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});
+ 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;
+ return call;
+ }
}
ThunkAST::ThunkAST(LLVMState& llvmState):
diff --git a/main.cpp b/main.cpp
index 5ae41bc..2ac1ff5 100644
--- a/main.cpp
+++ b/main.cpp
@@ -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();