LLVM  10.0.0svn
OrcCBindings.cpp
Go to the documentation of this file.
1 //===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===//
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 #include "OrcCBindingsStack.h"
10 #include "llvm-c/OrcBindings.h"
12 
13 using namespace llvm;
14 
16  TargetMachine *TM2(unwrap(TM));
17 
18  Triple T(TM2->getTargetTriple());
19 
20  auto IndirectStubsMgrBuilder =
22 
23  OrcCBindingsStack *JITStack =
24  new OrcCBindingsStack(*TM2, std::move(IndirectStubsMgrBuilder));
25 
26  return wrap(JITStack);
27 }
28 
29 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
30  OrcCBindingsStack &J = *unwrap(JITStack);
31  return J.getErrorMessage().c_str();
32 }
33 
34 void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledName,
35  const char *SymbolName) {
36  OrcCBindingsStack &J = *unwrap(JITStack);
37  std::string Mangled = J.mangle(SymbolName);
38  *MangledName = new char[Mangled.size() + 1];
39  strcpy(*MangledName, Mangled.c_str());
40 }
41 
42 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
43 
45  LLVMOrcJITStackRef JITStack, LLVMOrcTargetAddress *RetAddr,
46  LLVMOrcLazyCompileCallbackFn Callback, void *CallbackCtx) {
47  OrcCBindingsStack &J = *unwrap(JITStack);
48  if (auto Addr = J.createLazyCompileCallback(Callback, CallbackCtx)) {
49  *RetAddr = *Addr;
50  return LLVMErrorSuccess;
51  } else
52  return wrap(Addr.takeError());
53 }
54 
56  const char *StubName,
57  LLVMOrcTargetAddress InitAddr) {
58  OrcCBindingsStack &J = *unwrap(JITStack);
59  return wrap(J.createIndirectStub(StubName, InitAddr));
60 }
61 
63  const char *StubName,
64  LLVMOrcTargetAddress NewAddr) {
65  OrcCBindingsStack &J = *unwrap(JITStack);
66  return wrap(J.setIndirectStubPointer(StubName, NewAddr));
67 }
68 
70  LLVMOrcModuleHandle *RetHandle,
71  LLVMModuleRef Mod,
72  LLVMOrcSymbolResolverFn SymbolResolver,
73  void *SymbolResolverCtx) {
74  OrcCBindingsStack &J = *unwrap(JITStack);
75  std::unique_ptr<Module> M(unwrap(Mod));
76  if (auto Handle =
77  J.addIRModuleEager(std::move(M), SymbolResolver, SymbolResolverCtx)) {
78  *RetHandle = *Handle;
79  return LLVMErrorSuccess;
80  } else
81  return wrap(Handle.takeError());
82 }
83 
85  LLVMOrcModuleHandle *RetHandle,
86  LLVMModuleRef Mod,
87  LLVMOrcSymbolResolverFn SymbolResolver,
88  void *SymbolResolverCtx) {
89  OrcCBindingsStack &J = *unwrap(JITStack);
90  std::unique_ptr<Module> M(unwrap(Mod));
91  if (auto Handle =
92  J.addIRModuleLazy(std::move(M), SymbolResolver, SymbolResolverCtx)) {
93  *RetHandle = *Handle;
94  return LLVMErrorSuccess;
95  } else
96  return wrap(Handle.takeError());
97 }
98 
100  LLVMOrcModuleHandle *RetHandle,
102  LLVMOrcSymbolResolverFn SymbolResolver,
103  void *SymbolResolverCtx) {
104  OrcCBindingsStack &J = *unwrap(JITStack);
105  std::unique_ptr<MemoryBuffer> O(unwrap(Obj));
106  if (auto Handle =
107  J.addObject(std::move(O), SymbolResolver, SymbolResolverCtx)) {
108  *RetHandle = *Handle;
109  return LLVMErrorSuccess;
110  } else
111  return wrap(Handle.takeError());
112 }
113 
116  OrcCBindingsStack &J = *unwrap(JITStack);
117  return wrap(J.removeModule(H));
118 }
119 
121  LLVMOrcTargetAddress *RetAddr,
122  const char *SymbolName) {
123  OrcCBindingsStack &J = *unwrap(JITStack);
124  if (auto Addr = J.findSymbolAddress(SymbolName, true)) {
125  *RetAddr = *Addr;
126  return LLVMErrorSuccess;
127  } else
128  return wrap(Addr.takeError());
129 }
130 
132  LLVMOrcTargetAddress *RetAddr,
134  const char *SymbolName) {
135  OrcCBindingsStack &J = *unwrap(JITStack);
136  if (auto Addr = J.findSymbolAddressIn(H, SymbolName, true)) {
137  *RetAddr = *Addr;
138  return LLVMErrorSuccess;
139  } else
140  return wrap(Addr.takeError());
141 }
142 
144  auto *J = unwrap(JITStack);
145  auto Err = J->shutdown();
146  delete J;
147  return wrap(std::move(Err));
148 }
149 
151 {
152  unwrap(JITStack)->RegisterJITEventListener(unwrap(L));
153 }
154 
156 {
157  unwrap(JITStack)->UnregisterJITEventListener(unwrap(L));
158 }
Error createIndirectStub(StringRef StubName, JITTargetAddress Addr)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:204
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:43
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:199
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:65
struct LLVMOrcOpaqueJITStack * LLVMOrcJITStackRef
Definition: OrcBindings.h:33