ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
llvm_compat.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
6
7#include <string>
8
9#include "LLVMJIT.h"
10#include "llvm/Support/Error.h"
11
12#include "clang/Basic/DiagnosticOptions.h" // logging
13#include "clang/Frontend/TextDiagnosticPrinter.h" //logging
14#include "clang/Basic/LangOptions.h"
15
16#if LLVM_VERSION_MAJOR >= 11 && LLVM_VERSION_MAJOR <= 16
17#include "llvm/ExecutionEngine/JITSymbol.h"
18#elif LLVM_VERSION_MAJOR >= 17
19#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
20#else // LLVM_VERSION_MAJOR < 11 -> deprecated
21#warning "LLVM>=11 required."
22#endif
23
24#if LLVM_VERSION_MAJOR >= 11 && LLVM_VERSION_MAJOR <= 12
25#include "llvm/ExecutionEngine/Orc/Core.h"
26#elif LLVM_VERSION_MAJOR >= 13
27#include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
28#else // LLVM_VERSION_MAJOR < 11 -> deprecated
29#warning "LLVM>=11 required."
30#endif
31
32#include "llvm_compat.hpp"
33
34namespace compat
35{
36
37std::unique_ptr<llvm::orc::ExecutionSession> createExecutionSession()
38{
39 std::unique_ptr<llvm::orc::ExecutionSession> ES{ nullptr };
40
41#if LLVM_VERSION_MAJOR >= 11 && LLVM_VERSION_MAJOR <= 12
42 ES = std::make_unique<llvm::orc::ExecutionSession>();
43#elif LLVM_VERSION_MAJOR >= 13 && LLVM_VERSION_MAJOR <= 16
44 ES = std::make_unique<llvm::orc::ExecutionSession>(cantFail(llvm::orc::SelfExecutorProcessControl::Create()));
45#elif LLVM_VERSION_MAJOR >= 17 && LLVM_VERSION_MAJOR <= 19
46 ES = std::make_unique<llvm::orc::ExecutionSession>(cantFail(llvm::orc::SelfExecutorProcessControl::Create()));
47#elif LLVM_VERSION_MAJOR >= 20
48 ES = std::make_unique<llvm::orc::ExecutionSession>(cantFail(llvm::orc::SelfExecutorProcessControl::Create()));
49#else // LLVM_VERSION_MAJOR < 11 -> deprecated
50 [[deprecated]]
51#endif
52
53 if (!ES)
54 llvm::report_fatal_error("Failed to create ES");
55 return ES;
56}
57
58std::unique_ptr<llvm::MemoryBuffer> get_virtual_source(llvm::StringRef code, clang::CompilerInstance &CI)
59{
60 // input file is mapped to memory area containing the code
61 std::unique_ptr<llvm::MemoryBuffer> buffer{ nullptr };
62 buffer = llvm::MemoryBuffer::getMemBufferCopy(code, "/etiss_llvm_clang_memory_mapped_file.c");
63
64#if LLVM_VERSION_MAJOR == 11
65 CI.getSourceManager().overrideFileContents(
66 CI.getFileManager().getVirtualFile("/etiss_llvm_clang_memory_mapped_file.c", buffer->getBufferSize(), 0),
67 buffer.get(), true);
68#elif LLVM_VERSION_MAJOR >= 12 && LLVM_VERSION_MAJOR <= 16
69 CI.getSourceManager().overrideFileContents(
70 CI.getFileManager().getVirtualFile("/etiss_llvm_clang_memory_mapped_file.c", buffer->getBufferSize(), 0),
71 *buffer);
72#elif LLVM_VERSION_MAJOR >= 17 && LLVM_VERSION_MAJOR <= 20
73 CI.getSourceManager().overrideFileContents(
74 CI.getFileManager().getVirtualFileRef("/etiss_llvm_clang_memory_mapped_file.c", buffer->getBufferSize(), 0),
75 *buffer);
76#else // LLVM_VERSION_MAJOR < 11 -> deprecated
77 [[deprecated]]
78#endif
79
80 return buffer;
81}
82
83void *get_function_ptr(const compat::lookup_symbol_T &func)
84{
85 void *ret_addr{ nullptr };
86
87#if LLVM_VERSION_MAJOR >= 11 && LLVM_VERSION_MAJOR <= 16
88 ret_addr = (void *)func.getAddress();
89#elif LLVM_VERSION_MAJOR >= 17 && LLVM_VERSION_MAJOR <= 20
90 ret_addr = func.getAddress().toPtr<void *>();
91#else // LLVM_VERSION_MAJOR < 11 -> deprecated
92 [[deprecated]]
93#endif
94
95 return ret_addr;
96}
97
98void createDiagnostics(clang::CompilerInstance &CI)
99{
100#if LLVM_VERSION_MAJOR >= 11 && LLVM_VERSION_MAJOR <= 12
101 auto diagOpts = new clang::DiagnosticOptions();
102 auto diagPrinter = new clang::TextDiagnosticPrinter(llvm::outs(), diagOpts);
103
104 CI.createDiagnostics(diagPrinter);
105#elif LLVM_VERSION_MAJOR >= 13 && LLVM_VERSION_MAJOR <= 20
106 auto diagOpts = llvm::makeIntrusiveRefCnt<clang::DiagnosticOptions>();
107 auto diagPrinter = std::make_unique<clang::TextDiagnosticPrinter>(llvm::errs(), diagOpts.get());
108
109 llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(new clang::DiagnosticIDs());
110#if LLVM_VERSION_MAJOR < 20
111 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
112 new clang::DiagnosticsEngine(diagID, diagOpts, diagPrinter.release()));
113#else
114 llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
115 new clang::DiagnosticsEngine(diagID, diagOpts, diagPrinter.release(), false));
116#endif
117 CI.setDiagnostics(diags.get());
118#else // LLVM_VERSION_MAJOR < 11 -> deprecated
119 [[deprecated]]
120#endif
121}
122
123} // namespace compat
std::unique_ptr< llvm::MemoryBuffer > get_virtual_source(llvm::StringRef code, clang::CompilerInstance &CI)
void * get_function_ptr(const compat::lookup_symbol_T &func)
void createDiagnostics(clang::CompilerInstance &CI)
std::unique_ptr< llvm::orc::ExecutionSession > createExecutionSession()