]> code.bitgloo.com Git - clyne/forspll.git/commitdiff
take env off stack; only save locals for recursion
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 25 Jun 2024 21:29:45 +0000 (17:29 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 25 Jun 2024 21:29:45 +0000 (17:29 -0400)
Makefile
ast.cpp
main.cpp

index fcba1f53523a23cd64b4692f67b1cb8a6fe4358f..7d20637e62cd813d157b519428cfaa08093ec33d 100644 (file)
--- 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 0e2d8c540ca3213197e405ccfd0ba092fac1757f..6aaa8a0ab6a43bf1a5c0d2758e1f2f2295266494 100644 (file)
--- 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):
index 5ae41bca857277f94fc1cd0456c785e48832b4a9..2ac1ff5be7799718195bb4a441ad790e172912fc 100644 (file)
--- 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();