xref: /openbsd-src/gnu/llvm/llvm/tools/lli/ExecutionUtils.h (revision 73471bf04ceb096474c7f0fa83b1b65c70a787a1)
1 //===- ExecutionUtils.h - Utilities for executing code in lli ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Contains utilities for executing code in lli.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_TOOLS_LLI_EXECUTIONUTILS_H
14 #define LLVM_TOOLS_LLI_EXECUTIONUTILS_H
15 
16 #include "llvm/ExecutionEngine/JITSymbol.h"
17 #include "llvm/ExecutionEngine/Orc/Core.h"
18 #include "llvm/ExecutionEngine/Orc/Mangling.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 
22 #include <memory>
23 #include <utility>
24 
25 namespace llvm {
26 
27 enum class BuiltinFunctionKind {
28   DumpDebugDescriptor,
29   DumpDebugObjects,
30 };
31 
32 // Utility class to expose symbols for special-purpose functions to the JIT.
33 class LLIBuiltinFunctionGenerator : public orc::DefinitionGenerator {
34 public:
35   LLIBuiltinFunctionGenerator(std::vector<BuiltinFunctionKind> Enabled,
36                               orc::MangleAndInterner &Mangle);
37 
38   Error tryToGenerate(orc::LookupState &LS, orc::LookupKind K,
39                       orc::JITDylib &JD, orc::JITDylibLookupFlags JDLookupFlags,
40                       const orc::SymbolLookupSet &Symbols) override;
41 
appendDebugObject(const char * Addr,size_t Size)42   void appendDebugObject(const char *Addr, size_t Size) {
43     TestOut->os().write(Addr, Size);
44   }
45 
46 private:
47   orc::SymbolMap BuiltinFunctions;
48   std::unique_ptr<ToolOutputFile> TestOut;
49 
expose(orc::SymbolStringPtr Name,T * Handler)50   template <typename T> void expose(orc::SymbolStringPtr Name, T *Handler) {
51     BuiltinFunctions[Name] = JITEvaluatedSymbol(
52         pointerToJITTargetAddress(Handler), JITSymbolFlags::Exported);
53   }
54 
55   static std::unique_ptr<ToolOutputFile> createToolOutput();
56 };
57 
58 } // end namespace llvm
59 
60 #endif // LLVM_TOOLS_LLI_EXECUTIONUTILS_H
61