aboutsummaryrefslogtreecommitdiffstats
path: root/llvm.cpp
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2024-06-15 08:04:15 -0400
committerClyne Sullivan <clyne@bitgloo.com>2024-06-15 08:04:15 -0400
commit0ad40ced64ba5b2b83f7c9274efea1aa3677a534 (patch)
treed019d32a7919f52e4aa6897bfa279ed57ef7038f /llvm.cpp
parent47d7c964d075f92b9ce657f1b946f5bd6895439b (diff)
break code into separate files
Diffstat (limited to 'llvm.cpp')
-rw-r--r--llvm.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/llvm.cpp b/llvm.cpp
new file mode 100644
index 0000000..fc56d9e
--- /dev/null
+++ b/llvm.cpp
@@ -0,0 +1,64 @@
+#include "llvm.hpp"
+
+LLVMState::LLVMState():
+ ctx(),
+ modul("forsp", ctx),
+ builder(ctx),
+ inttype(llvm::Type::getInt32Ty(ctx)),
+ stacktype(llvm::VectorType::get(inttype, 12, false)),
+ ftype(llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), {}, false)),
+ one(llvm::ConstantInt::get(inttype, 1)),
+ zero(llvm::ConstantInt::get(inttype, 0))
+{
+ auto zerovec = llvm::ConstantVector::get(llvm::ArrayRef(zero));
+ llvmSp = new llvm::GlobalVariable(modul, inttype, false,
+ llvm::GlobalValue::ExternalLinkage, zero, "sp");
+ llvmStack = new llvm::GlobalVariable(modul, stacktype, false,
+ llvm::GlobalValue::ExternalLinkage, zerovec, "stack");
+}
+
+llvm::Value *LLVMState::createPush()
+{
+ auto dspval = builder.CreateLoad(inttype, llvmSp);
+ auto inc = builder.CreateAdd(dspval, one);
+ builder.CreateStore(inc, llvmSp, false);
+
+ return builder.CreateGEP(stacktype, llvmStack, {zero, dspval});
+}
+
+llvm::Value *LLVMState::createPop()
+{
+ auto dspval = builder.CreateLoad(inttype, llvmSp);
+ auto dec = builder.CreateSub(dspval, one);
+ builder.CreateStore(dec, llvmSp, false);
+
+ return builder.CreateGEP(stacktype, llvmStack, {zero, dec});
+}
+
+llvm::Function *LLVMState::createFunction(const std::string& name)
+{
+ return llvm::Function::Create(ftype, llvm::Function::ExternalLinkage,
+ name.c_str(), modul);
+}
+
+llvm::BasicBlock *LLVMState::createEntry(llvm::Function *func)
+{
+ return llvm::BasicBlock::Create(ctx, "entry", func);
+}
+
+llvm::Value *LLVMState::createVariable(const std::string& name)
+{
+ return new llvm::GlobalVariable(modul, inttype, false,
+ llvm::GlobalValue::InternalLinkage, zero, name);
+}
+
+llvm::Constant *LLVMState::createInt(int n)
+{
+ return llvm::ConstantInt::get(ctx, llvm::APInt(32, n, true));
+}
+
+void LLVMState::output()
+{
+ //std::cout << "LLVM:" << std::endl;
+ modul.print(llvm::errs(), nullptr);
+}