TPDE
Loading...
Searching...
No Matches
LLVMCompiler.hpp
1// SPDX-FileCopyrightText: 2025 Contributors to TPDE <https://tpde.org>
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3#pragma once
4
5#include <cstdint>
6#include <functional>
7#include <memory>
8#include <string_view>
9#include <vector>
10
11namespace llvm {
12class GlobalValue;
13class Module;
14class Triple;
15} // namespace llvm
16
17namespace tpde_llvm {
18
19class JITMapperImpl;
20
21/// In-memory mapper for JIT execution. Memory and registered unwind info will
22/// be released on destruction.
23class JITMapper {
24private:
25 friend class JITMapperImpl;
26
27 std::unique_ptr<JITMapperImpl> impl;
28
29public:
30 explicit JITMapper(std::unique_ptr<JITMapperImpl> impl);
31 ~JITMapper();
32
33 JITMapper(const JITMapper &) = delete;
34 JITMapper(JITMapper &&other);
35
36 JITMapper &operator=(const JITMapper &) = delete;
37 JITMapper &operator=(JITMapper &&other);
38
39 /// Get the address for a global, which must be contained in the compiled
40 /// module.
41 void *lookup_global(llvm::GlobalValue *) const;
42
43 /// Get the range, mapped to memory
44 std::pair<void *, size_t> get_mapped_range() const;
45
46 /// Indicate whether compilation and in-memory mapping was successful.
47 operator bool() const { return impl != nullptr; }
48};
49
50/// Compiler for LLVM modules
51class LLVMCompiler {
52protected:
53 LLVMCompiler() = default;
54
55public:
56 virtual ~LLVMCompiler();
57
58 LLVMCompiler(const LLVMCompiler &) = delete;
59 LLVMCompiler &operator=(const LLVMCompiler &) = delete;
60
61 /// Create a compiler for the specified target triple; returns null if the
62 /// triple is not supported. The only supported code model is small, the only
63 /// supported relocation model is PIC.
64 static std::unique_ptr<LLVMCompiler> create(const llvm::Triple &triple);
65
66 /// Compile the module to an object file and emit it into the buffer. The
67 /// module might be modified during compilation.
68 /// \returns true on success.
69 virtual bool compile_to_elf(llvm::Module &mod, std::vector<uint8_t> &buf) = 0;
70
71 /// Compile the module and map it into memory, calling resolver to resolve
72 /// references to external symbols. This function will also register unwind
73 /// information. The module might be modified during compilation.
74 virtual JITMapper
75 compile_and_map(llvm::Module &mod,
76 std::function<void *(std::string_view)> resolver) = 0;
77};
78
79} // namespace tpde_llvm
In-memory mapper for JIT execution.
void * lookup_global(llvm::GlobalValue *) const
Get the address for a global, which must be contained in the compiled module.
std::pair< void *, size_t > get_mapped_range() const
Get the range, mapped to memory.
virtual JITMapper compile_and_map(llvm::Module &mod, std::function< void *(std::string_view)> resolver)=0
Compile the module and map it into memory, calling resolver to resolve references to external symbols...
virtual bool compile_to_elf(llvm::Module &mod, std::vector< uint8_t > &buf)=0
Compile the module to an object file and emit it into the buffer.
static std::unique_ptr< LLVMCompiler > create(const llvm::Triple &triple)
Create a compiler for the specified target triple; returns null if the triple is not supported.