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