Loading...
Searching...
No Matches
Compiler.hpp
1#pragma once
2#include <JitCpp/ClangDriver.hpp>
3
4#include <QDebug>
5
6#include <llvm/ExecutionEngine/JITLink/JITLink.h>
7#include <llvm/ExecutionEngine/Orc/ExecutionUtils.h>
8#include <llvm/ExecutionEngine/Orc/LLJIT.h>
9#include <llvm/ExecutionEngine/Orc/ThreadSafeModule.h>
10
11namespace Jit
12{
14{
15 using ModulePtr_t = std::unique_ptr<llvm::Module>;
16
17public:
20
21 void compile(
22 const std::string& cppCode, const std::vector<std::string>& flags,
23 CompilerOptions opts, llvm::orc::ThreadSafeContext& context);
24
25 template <class Signature_t>
26 llvm::Expected<std::function<Signature_t>> getFunction(std::string name)
27 {
28 auto& JIT = *m_jit;
29 // Look up the JIT'd code entry point.
30 auto EntrySym = JIT.lookup(name);
31 if(!EntrySym)
32 return EntrySym.takeError();
33
34 // Cast the entry point address to a function pointer.
35#if LLVM_VERSION_MAJOR <= 14
36 auto* Entry = (Signature_t*)EntrySym->getAddress();
37#else
38 auto* Entry = (Signature_t*)(*EntrySym).getValue();
39#endif
40 return std::function<Signature_t>(Entry);
41 }
42
43 const QString& errors() const noexcept
44 {
45 return m_errors;
46 }
47 std::unique_ptr<llvm::jitlink::InProcessMemoryManager> m_memmgr;
48 ClangCC1Driver m_driver;
49 std::unique_ptr<llvm::orc::LLJIT> m_jit;
50
51 llvm::orc::MangleAndInterner m_mangler;
52
53 QString m_errors;
54
55 int m_atExitId{};
56};
57}
Definition ClangDriver.hpp:14
Definition Compiler.hpp:14
Definition JitOptions.hpp:7