Skip to content

Commit ef69b9f

Browse files
committed
Store custom LLVM types in LLVMCompilerContext
1 parent ee32ec4 commit ef69b9f

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "llvmconstantregister.h"
1616
#include "llvmifstatement.h"
1717
#include "llvmloop.h"
18-
#include "llvmtypes.h"
1918
#include "llvmloopscope.h"
2019

2120
using namespace libscratchcpp;
@@ -1851,8 +1850,8 @@ void LLVMCodeBuilder::createProcedureCall(BlockPrototype *prototype, const Compi
18511850
void LLVMCodeBuilder::initTypes()
18521851
{
18531852
llvm::PointerType *pointerType = llvm::PointerType::get(llvm::Type::getInt8Ty(m_llvmCtx), 0);
1854-
m_valueDataType = LLVMTypes::createValueDataType(m_llvmCtx);
1855-
m_stringPtrType = LLVMTypes::createStringPtrType(m_llvmCtx);
1853+
m_valueDataType = m_ctx->valueDataType();
1854+
m_stringPtrType = m_ctx->stringPtrType();
18561855
}
18571856

18581857
void LLVMCodeBuilder::createVariableMap()

src/engine/internal/llvm/llvmcompilercontext.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "llvmcompilercontext.h"
1111
#include "llvmcoroutine.h"
12+
#include "llvmtypes.h"
1213

1314
using namespace libscratchcpp;
1415

@@ -18,10 +19,16 @@ LLVMCompilerContext::LLVMCompilerContext(IEngine *engine, Target *target) :
1819
m_module(std::make_unique<llvm::Module>(target ? target->name() : "", *m_llvmCtx)),
1920
m_llvmCtxPtr(m_llvmCtx.get()),
2021
m_modulePtr(m_module.get()),
21-
m_jit((initTarget(), llvm::orc::LLJITBuilder().create())),
22-
m_llvmCoroResumeFunction(createCoroResumeFunction()),
23-
m_llvmCoroDestroyFunction(createCoroDestroyFunction())
22+
m_jit((initTarget(), llvm::orc::LLJITBuilder().create()))
2423
{
24+
// Create functions
25+
m_llvmCoroResumeFunction = createCoroResumeFunction();
26+
m_llvmCoroDestroyFunction = createCoroDestroyFunction();
27+
28+
// Create types
29+
m_valueDataType = LLVMTypes::createValueDataType(*m_llvmCtx);
30+
m_stringPtrType = LLVMTypes::createStringPtrType(*m_llvmCtx);
31+
2532
if (!m_jit) {
2633
llvm::errs() << "error: failed to create JIT: " << toString(m_jit.takeError()) << "\n";
2734
return;
@@ -129,6 +136,16 @@ void LLVMCompilerContext::destroyCoroutine(void *handle)
129136
m_coroDestroyFunction(handle);
130137
}
131138

139+
llvm::StructType *LLVMCompilerContext::valueDataType() const
140+
{
141+
return m_valueDataType;
142+
}
143+
144+
llvm::StructType *LLVMCompilerContext::stringPtrType() const
145+
{
146+
return m_stringPtrType;
147+
}
148+
132149
void LLVMCompilerContext::initTarget()
133150
{
134151
llvm::InitializeNativeTarget();

src/engine/internal/llvm/llvmcompilercontext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class LLVMCompilerContext : public CompilerContext
3131
llvm::Function *coroutineResumeFunction() const;
3232
void destroyCoroutine(void *handle);
3333

34+
llvm::StructType *valueDataType() const;
35+
llvm::StructType *stringPtrType() const;
36+
3437
template<typename T>
3538
T lookupFunction(const std::string &name)
3639
{
@@ -49,6 +52,7 @@ class LLVMCompilerContext : public CompilerContext
4952
using DestroyCoroFuncType = void (*)(void *);
5053

5154
void initTarget();
55+
5256
llvm::Function *createCoroResumeFunction();
5357
llvm::Function *createCoroDestroyFunction();
5458

@@ -65,6 +69,9 @@ class LLVMCompilerContext : public CompilerContext
6569

6670
llvm::Function *m_llvmCoroDestroyFunction = nullptr;
6771
DestroyCoroFuncType m_coroDestroyFunction = nullptr;
72+
73+
llvm::StructType *m_valueDataType = nullptr;
74+
llvm::StructType *m_stringPtrType = nullptr;
6875
};
6976

7077
} // namespace libscratchcpp

test/llvm/llvmexecutablecode_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LLVMExecutableCodeTest : public testing::Test
3030
m_module = m_ctx->module();
3131
m_llvmCtx = m_ctx->llvmCtx();
3232
m_builder = std::make_unique<llvm::IRBuilder<>>(*m_llvmCtx);
33-
m_valueDataType = LLVMTypes::createValueDataType(*m_llvmCtx);
33+
m_valueDataType = m_ctx->valueDataType();
3434
test_function(nullptr, nullptr, nullptr, nullptr, nullptr); // force dependency
3535

3636
m_script = std::make_unique<Script>(&m_target, nullptr, &m_engine);

0 commit comments

Comments
 (0)