blob: a8a88d7cb2d2c67eb00b94ed777f5aa619c85009 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- GlobalMappingLayer.h - Run all IR through a functor ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Convenience layer for injecting symbols that will appear in calls to
11// findSymbol.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H
16#define LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H
17
18#include "llvm/ExecutionEngine/JITSymbol.h"
19#include <map>
20#include <memory>
21#include <string>
22
23namespace llvm {
24
25class Module;
26class JITSymbolResolver;
27
28namespace orc {
29
Andrew Scullcdfcccc2018-10-05 20:58:37 +010030/// Global mapping layer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010031///
32/// This layer overrides the findSymbol method to first search a local symbol
33/// table that the client can define. It can be used to inject new symbol
34/// mappings into the JIT. Beware, however: symbols within a single IR module or
35/// object file will still resolve locally (via RuntimeDyld's symbol table) -
36/// such internal references cannot be overriden via this layer.
37template <typename BaseLayerT>
38class GlobalMappingLayer {
39public:
40
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041 /// Handle to an added module.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042 using ModuleHandleT = typename BaseLayerT::ModuleHandleT;
43
Andrew Scullcdfcccc2018-10-05 20:58:37 +010044 /// Construct an GlobalMappingLayer with the given BaseLayer
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010045 GlobalMappingLayer(BaseLayerT &BaseLayer) : BaseLayer(BaseLayer) {}
46
Andrew Scullcdfcccc2018-10-05 20:58:37 +010047 /// Add the given module to the JIT.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048 /// @return A handle for the added modules.
49 Expected<ModuleHandleT>
50 addModule(std::shared_ptr<Module> M,
51 std::shared_ptr<JITSymbolResolver> Resolver) {
52 return BaseLayer.addModule(std::move(M), std::move(Resolver));
53 }
54
Andrew Scullcdfcccc2018-10-05 20:58:37 +010055 /// Remove the module set associated with the handle H.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 Error removeModule(ModuleHandleT H) { return BaseLayer.removeModule(H); }
57
Andrew Scullcdfcccc2018-10-05 20:58:37 +010058 /// Manually set the address to return for the given symbol.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010059 void setGlobalMapping(const std::string &Name, JITTargetAddress Addr) {
60 SymbolTable[Name] = Addr;
61 }
62
Andrew Scullcdfcccc2018-10-05 20:58:37 +010063 /// Remove the given symbol from the global mapping.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064 void eraseGlobalMapping(const std::string &Name) {
65 SymbolTable.erase(Name);
66 }
67
Andrew Scullcdfcccc2018-10-05 20:58:37 +010068 /// Search for the given named symbol.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010069 ///
70 /// This method will first search the local symbol table, returning
71 /// any symbol found there. If the symbol is not found in the local
72 /// table then this call will be passed through to the base layer.
73 ///
74 /// @param Name The name of the symbol to search for.
75 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
76 /// @return A handle for the given named symbol, if it exists.
77 JITSymbol findSymbol(const std::string &Name, bool ExportedSymbolsOnly) {
78 auto I = SymbolTable.find(Name);
79 if (I != SymbolTable.end())
80 return JITSymbol(I->second, JITSymbolFlags::Exported);
81 return BaseLayer.findSymbol(Name, ExportedSymbolsOnly);
82 }
83
Andrew Scullcdfcccc2018-10-05 20:58:37 +010084 /// Get the address of the given symbol in the context of the of the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085 /// module represented by the handle H. This call is forwarded to the
86 /// base layer's implementation.
87 /// @param H The handle for the module to search in.
88 /// @param Name The name of the symbol to search for.
89 /// @param ExportedSymbolsOnly If true, search only for exported symbols.
90 /// @return A handle for the given named symbol, if it is found in the
91 /// given module.
92 JITSymbol findSymbolIn(ModuleHandleT H, const std::string &Name,
93 bool ExportedSymbolsOnly) {
94 return BaseLayer.findSymbolIn(H, Name, ExportedSymbolsOnly);
95 }
96
Andrew Scullcdfcccc2018-10-05 20:58:37 +010097 /// Immediately emit and finalize the module set represented by the
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098 /// given handle.
99 /// @param H Handle for module set to emit/finalize.
100 Error emitAndFinalize(ModuleHandleT H) {
101 return BaseLayer.emitAndFinalize(H);
102 }
103
104private:
105 BaseLayerT &BaseLayer;
106 std::map<std::string, JITTargetAddress> SymbolTable;
107};
108
109} // end namespace orc
110} // end namespace llvm
111
112#endif // LLVM_EXECUTIONENGINE_ORC_GLOBALMAPPINGLAYER_H