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
11#include <score_plugin_jit_export.h>
12
13namespace Jit
14{
15class SCORE_PLUGIN_JIT_EXPORT JitCompiler
16{
17 using ModulePtr_t = std::unique_ptr<llvm::Module>;
18
19public:
22
23 void compile(
24 const std::string& cppCode, const std::vector<std::string>& flags,
25 CompilerOptions opts, llvm::orc::ThreadSafeContext& context);
26
27 // Whether the executor is driven by an Orc Platform (orc_rt: native TLS, real
28 // static-init/atexit, and -- on COFF -- SEH/exception registration). Set at
29 // construction; on COFF the platform is mandatory.
30 bool m_useNativePlatform{};
31
32 template <class Signature_t>
33 llvm::Expected<std::function<Signature_t>> getFunction(std::string name)
34 {
35 auto& JIT = *m_jit;
36 // Look up the JIT'd code entry point.
37 auto EntrySym = JIT.lookup(name);
38 if(!EntrySym)
39 return EntrySym.takeError();
40
41 // Cast the entry point address to a function pointer.
42#if LLVM_VERSION_MAJOR <= 14
43 auto* Entry = (Signature_t*)EntrySym->getAddress();
44#else
45 auto* Entry = (Signature_t*)(*EntrySym).getValue();
46#endif
47 return std::function<Signature_t>(Entry);
48 }
49
50 const QString& errors() const noexcept
51 {
52 return m_errors;
53 }
54 std::unique_ptr<llvm::jitlink::JITLinkMemoryManager> m_memmgr;
55 ClangCC1Driver m_driver;
56 std::unique_ptr<llvm::orc::LLJIT> m_jit;
57
58 llvm::orc::MangleAndInterner m_mangler;
59
60 QString m_errors;
61
62 int m_atExitId{};
63};
64}
Definition ClangDriver.hpp:14
Definition Compiler.hpp:16
Definition JitOptions.hpp:7