LLVM  8.0.1
OrcCBindings.cpp
Go to the documentation of this file.
1 //===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===//
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 #include "OrcCBindingsStack.h"
11 #include "llvm-c/OrcBindings.h"
13 
14 using namespace llvm;
15 
17  TargetMachine *TM2(unwrap(TM));
18 
19  Triple T(TM2->getTargetTriple());
20 
21  auto IndirectStubsMgrBuilder =
23 
24  OrcCBindingsStack *JITStack =
25  new OrcCBindingsStack(*TM2, std::move(IndirectStubsMgrBuilder));
26 
27  return wrap(JITStack);
28 }
29 
30 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
31  OrcCBindingsStack &J = *unwrap(JITStack);
32  return J.getErrorMessage().c_str();
33 }
34 
35 void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledName,
36  const char *SymbolName) {
37  OrcCBindingsStack &J = *unwrap(JITStack);
38  std::string Mangled = J.mangle(SymbolName);
39  *MangledName = new char[Mangled.size() + 1];
40  strcpy(*MangledName, Mangled.c_str());
41 }
42 
43 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
44 
46  LLVMOrcJITStackRef JITStack, LLVMOrcTargetAddress *RetAddr,
47  LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx) {
48  OrcCBindingsStack &J = *unwrap(JITStack);
49  if (auto Addr = J.createLazyCompileCallback(Callback, CallbackCtx)) {
50  *RetAddr = *Addr;
51  return LLVMErrorSuccess;
52  } else
53  return wrap(Addr.takeError());
54 }
55 
57  const char *StubName,
58  LLVMOrcTargetAddress InitAddr) {
59  OrcCBindingsStack &J = *unwrap(JITStack);
60  return wrap(J.createIndirectStub(StubName, InitAddr));
61 }
62 
64  const char *StubName,
65  LLVMOrcTargetAddress NewAddr) {
66  OrcCBindingsStack &J = *unwrap(JITStack);
67  return wrap(J.setIndirectStubPointer(StubName, NewAddr));
68 }
69 
71  LLVMOrcModuleHandle *RetHandle,
72  LLVMModuleRef Mod,
73  LLVMOrcSymbolResolverFn SymbolResolver,
74  void *SymbolResolverCtx) {
75  OrcCBindingsStack &J = *unwrap(JITStack);
76  std::unique_ptr<Module> M(unwrap(Mod));
77  if (auto Handle =
78  J.addIRModuleEager(std::move(M), SymbolResolver, SymbolResolverCtx)) {
79  *RetHandle = *Handle;
80  return LLVMErrorSuccess;
81  } else
82  return wrap(Handle.takeError());
83 }
84 
86  LLVMOrcModuleHandle *RetHandle,
87  LLVMModuleRef Mod,
88  LLVMOrcSymbolResolverFn SymbolResolver,
89  void *SymbolResolverCtx) {
90  OrcCBindingsStack &J = *unwrap(JITStack);
91  std::unique_ptr<Module> M(unwrap(Mod));
92  if (auto Handle =
93  J.addIRModuleLazy(std::move(M), SymbolResolver, SymbolResolverCtx)) {
94  *RetHandle = *Handle;
95  return LLVMErrorSuccess;
96  } else
97  return wrap(Handle.takeError());
98 }
99 
101  LLVMOrcModuleHandle *RetHandle,
103  LLVMOrcSymbolResolverFn SymbolResolver,
104  void *SymbolResolverCtx) {
105  OrcCBindingsStack &J = *unwrap(JITStack);
106  std::unique_ptr<MemoryBuffer> O(unwrap(Obj));
107  if (auto Handle =
108  J.addObject(std::move(O), SymbolResolver, SymbolResolverCtx)) {
109  *RetHandle = *Handle;
110  return LLVMErrorSuccess;
111  } else
112  return wrap(Handle.takeError());
113 }
114 
117  OrcCBindingsStack &J = *unwrap(JITStack);
118  return wrap(J.removeModule(H));
119 }
120 
122  LLVMOrcTargetAddress *RetAddr,
123  const char *SymbolName) {
124  OrcCBindingsStack &J = *unwrap(JITStack);
125  if (auto Addr = J.findSymbolAddress(SymbolName, true)) {
126  *RetAddr = *Addr;
127  return LLVMErrorSuccess;
128  } else
129  return wrap(Addr.takeError());
130 }
131 
133  LLVMOrcTargetAddress *RetAddr,
135  const char *SymbolName) {
136  OrcCBindingsStack &J = *unwrap(JITStack);
137  if (auto Addr = J.findSymbolAddressIn(H, SymbolName, true)) {
138  *RetAddr = *Addr;
139  return LLVMErrorSuccess;
140  } else
141  return wrap(Addr.takeError());
142 }
143 
145  auto *J = unwrap(JITStack);
146  auto Err = J->shutdown();
147  delete J;
148  return wrap(std::move(Err));
149 }
150 
152 {
153  unwrap(JITStack)->RegisterJITEventListener(unwrap(L));
154 }
155 
157 {
158  unwrap(JITStack)->UnregisterJITEventListener(unwrap(L));
159 }
Error createIndirectStub(StringRef StubName, JITTargetAddress Addr)
This class represents lattice values for constants.
Definition: AllocatorList.h:24
std::function< std::unique_ptr< IndirectStubsManager >)> createLocalIndirectStubsManagerBuilder(const Triple &T)
Create a local indriect stubs manager builder.
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:62
LLVMErrorRef LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx)
Add module to be eagerly compiled.
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:49
Error removeModule(orc::VModuleKey K)
LLVMErrorRef LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack, LLVMOrcTargetAddress *RetAddr, const char *SymbolName)
Get symbol address from JIT instance.
LLVMErrorRef LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack, LLVMOrcTargetAddress *RetAddr, LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx)
Create a lazy compile callback.
Expected< orc::VModuleKey > addIRModuleLazy(std::unique_ptr< Module > M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
LLVMErrorRef LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack, LLVMOrcTargetAddress *RetAddr, LLVMOrcModuleHandle H, const char *SymbolName)
Get symbol address from JIT instance, searching only the specified handle.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
Error setIndirectStubPointer(StringRef Name, JITTargetAddress Addr)
struct LLVMOpaqueTargetMachine * LLVMTargetMachineRef
Definition: TargetMachine.h:28
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
#define T
void LLVMOrcDisposeMangledSymbol(char *MangledName)
Dispose of a mangled symbol.
LLVMErrorRef LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack)
Dispose of an ORC JIT stack.
uint64_t LLVMOrcTargetAddress
Definition: OrcBindings.h:35
struct LLVMOpaqueError * LLVMErrorRef
Opaque reference to an error instance.
Definition: Error.h:26
LLVMErrorRef LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack, const char *StubName, LLVMOrcTargetAddress InitAddr)
Create a named indirect call stub.
void LLVMOrcUnregisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L)
Unegister a JIT Event Listener.
Expected< JITTargetAddress > findSymbolAddress(const std::string &Name, bool ExportedSymbolsOnly)
LLVMErrorRef LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle *RetHandle, LLVMMemoryBufferRef Obj, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx)
Add an object file.
#define H(x, y, z)
Definition: MD5.cpp:57
#define LLVMErrorSuccess
Definition: Error.h:21
LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM)
Create an ORC JIT stack.
const char * LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack)
Get the error message for the most recent error (if any).
void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledName, const char *SymbolName)
Mangle the given symbol.
const Triple & getTargetTriple() const
LLVMErrorRef LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack, const char *StubName, LLVMOrcTargetAddress NewAddr)
Set the pointer for the given indirect stub.
std::string mangle(StringRef Name)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
uint64_t LLVMOrcModuleHandle
Definition: OrcBindings.h:34
void LLVMOrcRegisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L)
Register a JIT Event Listener.
uint64_t(* LLVMOrcLazyCompileCallbackFn)(LLVMOrcJITStackRef JITStack, void *CallbackCtx)
Definition: OrcBindings.h:37
const std::string & getErrorMessage() const
uint64_t(* LLVMOrcSymbolResolverFn)(const char *Name, void *LookupCtx)
Definition: OrcBindings.h:36
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
Expected< orc::VModuleKey > addObject(std::unique_ptr< MemoryBuffer > ObjBuffer, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
Expected< orc::VModuleKey > addIRModuleEager(std::unique_ptr< Module > M, LLVMOrcSymbolResolverFn ExternalResolver, void *ExternalResolverCtx)
struct LLVMOpaqueJITEventListener * LLVMJITEventListenerRef
Definition: Types.h:164
Expected< JITTargetAddress > createLazyCompileCallback(LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx)
Expected< JITTargetAddress > findSymbolAddressIn(orc::VModuleKey K, const std::string &Name, bool ExportedSymbolsOnly)
LLVMErrorRef LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H)
Remove a module set from the JIT.
LLVMErrorRef LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod, LLVMOrcSymbolResolverFn SymbolResolver, void *SymbolResolverCtx)
Add module to be lazily compiled one function at a time.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:59
struct LLVMOrcOpaqueJITStack * LLVMOrcJITStackRef
Definition: OrcBindings.h:33