1 //===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "OrcCBindingsStack.h"
10 #include "llvm-c/OrcBindings.h"
11 #include "llvm/ExecutionEngine/JITEventListener.h"
15 LLVMOrcJITStackRef
LLVMOrcCreateInstance(LLVMTargetMachineRef TM
) {
16 TargetMachine
*TM2(unwrap(TM
));
18 Triple
T(TM2
->getTargetTriple());
20 auto IndirectStubsMgrBuilder
=
21 orc::createLocalIndirectStubsManagerBuilder(T
);
23 OrcCBindingsStack
*JITStack
=
24 new OrcCBindingsStack(*TM2
, std::move(IndirectStubsMgrBuilder
));
26 return wrap(JITStack
);
29 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack
) {
30 OrcCBindingsStack
&J
= *unwrap(JITStack
);
31 return J
.getErrorMessage().c_str();
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());
42 void LLVMOrcDisposeMangledSymbol(char *MangledName
) { delete[] MangledName
; }
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
)) {
50 return LLVMErrorSuccess
;
52 return wrap(Addr
.takeError());
55 LLVMErrorRef
LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack
,
57 LLVMOrcTargetAddress InitAddr
) {
58 OrcCBindingsStack
&J
= *unwrap(JITStack
);
59 return wrap(J
.createIndirectStub(StubName
, InitAddr
));
62 LLVMErrorRef
LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack
,
64 LLVMOrcTargetAddress NewAddr
) {
65 OrcCBindingsStack
&J
= *unwrap(JITStack
);
66 return wrap(J
.setIndirectStubPointer(StubName
, NewAddr
));
69 LLVMErrorRef
LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack
,
70 LLVMOrcModuleHandle
*RetHandle
,
72 LLVMOrcSymbolResolverFn SymbolResolver
,
73 void *SymbolResolverCtx
) {
74 OrcCBindingsStack
&J
= *unwrap(JITStack
);
75 std::unique_ptr
<Module
> M(unwrap(Mod
));
77 J
.addIRModuleEager(std::move(M
), SymbolResolver
, SymbolResolverCtx
)) {
79 return LLVMErrorSuccess
;
81 return wrap(Handle
.takeError());
84 LLVMErrorRef
LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack
,
85 LLVMOrcModuleHandle
*RetHandle
,
87 LLVMOrcSymbolResolverFn SymbolResolver
,
88 void *SymbolResolverCtx
) {
89 OrcCBindingsStack
&J
= *unwrap(JITStack
);
90 std::unique_ptr
<Module
> M(unwrap(Mod
));
92 J
.addIRModuleLazy(std::move(M
), SymbolResolver
, SymbolResolverCtx
)) {
94 return LLVMErrorSuccess
;
96 return wrap(Handle
.takeError());
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
));
107 J
.addObject(std::move(O
), SymbolResolver
, SymbolResolverCtx
)) {
108 *RetHandle
= *Handle
;
109 return LLVMErrorSuccess
;
111 return wrap(Handle
.takeError());
114 LLVMErrorRef
LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack
,
115 LLVMOrcModuleHandle H
) {
116 OrcCBindingsStack
&J
= *unwrap(JITStack
);
117 return wrap(J
.removeModule(H
));
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)) {
126 return LLVMErrorSuccess
;
128 return wrap(Addr
.takeError());
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)) {
138 return LLVMErrorSuccess
;
140 return wrap(Addr
.takeError());
143 LLVMErrorRef
LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack
) {
144 auto *J
= unwrap(JITStack
);
145 auto Err
= J
->shutdown();
147 return wrap(std::move(Err
));
150 void LLVMOrcRegisterJITEventListener(LLVMOrcJITStackRef JITStack
, LLVMJITEventListenerRef L
)
152 unwrap(JITStack
)->RegisterJITEventListener(unwrap(L
));
155 void LLVMOrcUnregisterJITEventListener(LLVMOrcJITStackRef JITStack
, LLVMJITEventListenerRef L
)
157 unwrap(JITStack
)->UnregisterJITEventListener(unwrap(L
));