diff options
Diffstat (limited to 'llvm.cpp')
-rw-r--r-- | llvm.cpp | 26 |
1 files changed, 17 insertions, 9 deletions
@@ -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<llvm::Type *> {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, |