From 08f4a92102c9739e0736965186814e856ff727a1 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 28 Jun 2024 10:26:03 -0400 Subject: cache sp, minor refactors --- llvm.cpp | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'llvm.cpp') diff --git a/llvm.cpp b/llvm.cpp index c15d35d..38ec343 100644 --- a/llvm.cpp +++ b/llvm.cpp @@ -22,7 +22,7 @@ LLVMState::LLVMState(): modul("forsp", ctx), builder(ctx), inttype(llvm::Type::getInt64Ty(ctx)), - stacktype(llvm::VectorType::get(inttype, 16, false)), + stacktype(llvm::ArrayType::get(inttype, 16)), ftype(llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), llvm::ArrayRef {inttype->getPointerTo()}, false)), one(llvm::ConstantInt::get(inttype, 1)), zero(llvm::ConstantInt::get(inttype, 0)) @@ -36,24 +36,32 @@ LLVMState::LLVMState(): llvm::Value *LLVMState::createPush(llvm::Value *var) { - auto dspval = builder.CreateLoad(inttype, llvmSp); - auto inc = builder.CreateAdd(dspval, one); - builder.CreateStore(inc, llvmSp); + if (!lastSp) + lastSp = builder.CreateLoad(inttype, llvmSp); - auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dspval}); + auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, lastSp}); + lastSp = builder.CreateAdd(lastSp, one); return builder.CreateStore(var, gep); } llvm::Value *LLVMState::createPop() { - auto dspval = builder.CreateLoad(inttype, llvmSp); - auto dec = builder.CreateSub(dspval, one); - builder.CreateStore(dec, llvmSp); + if (!lastSp) + lastSp = builder.CreateLoad(inttype, llvmSp); - auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, dec}); + lastSp = builder.CreateSub(lastSp, one); + auto gep = builder.CreateGEP(stacktype, llvmStack, {zero, lastSp}); return builder.CreateLoad(inttype, gep); } +void LLVMState::commitSp() +{ + if (lastSp) { + builder.CreateStore(lastSp, llvmSp); + lastSp = nullptr; + } +} + llvm::Function *LLVMState::createFunction(const std::string& name) { auto func = llvm::Function::Create(ftype, llvm::Function::ExternalLinkage, -- cgit v1.2.3