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"
11 #include "llvm/ExecutionEngine/JITEventListener.h"
12
13 using namespace llvm;
14
LLVMOrcCreateInstance(LLVMTargetMachineRef TM)15 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
16 TargetMachine *TM2(unwrap(TM));
17
18 Triple T(TM2->getTargetTriple());
19
20 auto IndirectStubsMgrBuilder =
21 orc::createLocalIndirectStubsManagerBuilder(T);
22
23 OrcCBindingsStack *JITStack =
24 new OrcCBindingsStack(*TM2, std::move(IndirectStubsMgrBuilder));
25
26 return wrap(JITStack);
27 }
28
LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack)29 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
30 OrcCBindingsStack &J = *unwrap(JITStack);
31 return J.getErrorMessage().c_str();
32 }
33
LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack,char ** MangledName,const char * SymbolName)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
LLVMOrcDisposeMangledSymbol(char * MangledName)42 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
43
LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,LLVMOrcTargetAddress * RetAddr,LLVMOrcLazyCompileCallbackFn Callback,void * CallbackCtx)44 LLVMErrorRef LLVMOrcCreateLazyCompileCallback(
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
LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,const char * StubName,LLVMOrcTargetAddress InitAddr)55 LLVMErrorRef LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
56 const char *StubName,
57 LLVMOrcTargetAddress InitAddr) {
58 OrcCBindingsStack &J = *unwrap(JITStack);
59 return wrap(J.createIndirectStub(StubName, InitAddr));
60 }
61
LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,const char * StubName,LLVMOrcTargetAddress NewAddr)62 LLVMErrorRef LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
63 const char *StubName,
64 LLVMOrcTargetAddress NewAddr) {
65 OrcCBindingsStack &J = *unwrap(JITStack);
66 return wrap(J.setIndirectStubPointer(StubName, NewAddr));
67 }
68
LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,LLVMOrcModuleHandle * RetHandle,LLVMModuleRef Mod,LLVMOrcSymbolResolverFn SymbolResolver,void * SymbolResolverCtx)69 LLVMErrorRef LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
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
LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,LLVMOrcModuleHandle * RetHandle,LLVMModuleRef Mod,LLVMOrcSymbolResolverFn SymbolResolver,void * SymbolResolverCtx)84 LLVMErrorRef LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
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
LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,LLVMOrcModuleHandle * RetHandle,LLVMMemoryBufferRef Obj,LLVMOrcSymbolResolverFn SymbolResolver,void * SymbolResolverCtx)99 LLVMErrorRef LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
100 LLVMOrcModuleHandle *RetHandle,
101 LLVMMemoryBufferRef Obj,
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
LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,LLVMOrcModuleHandle H)114 LLVMErrorRef LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
115 LLVMOrcModuleHandle H) {
116 OrcCBindingsStack &J = *unwrap(JITStack);
117 return wrap(J.removeModule(H));
118 }
119
LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,LLVMOrcTargetAddress * RetAddr,const char * SymbolName)120 LLVMErrorRef LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
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
LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack,LLVMOrcTargetAddress * RetAddr,LLVMOrcModuleHandle H,const char * SymbolName)131 LLVMErrorRef LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack,
132 LLVMOrcTargetAddress *RetAddr,
133 LLVMOrcModuleHandle H,
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
LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack)143 LLVMErrorRef LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
144 auto *J = unwrap(JITStack);
145 auto Err = J->shutdown();
146 delete J;
147 return wrap(std::move(Err));
148 }
149
LLVMOrcRegisterJITEventListener(LLVMOrcJITStackRef JITStack,LLVMJITEventListenerRef L)150 void LLVMOrcRegisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L)
151 {
152 unwrap(JITStack)->RegisterJITEventListener(unwrap(L));
153 }
154
LLVMOrcUnregisterJITEventListener(LLVMOrcJITStackRef JITStack,LLVMJITEventListenerRef L)155 void LLVMOrcUnregisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L)
156 {
157 unwrap(JITStack)->UnregisterJITEventListener(unwrap(L));
158 }
159