1 //===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "OrcCBindingsStack.h"
11 #include "llvm-c/OrcBindings.h"
15 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
16 TargetMachine *TM2(unwrap(TM));
18 Triple T(TM2->getTargetTriple());
20 auto CompileCallbackMgr = orc::createLocalCompileCallbackManager(T, 0);
21 auto IndirectStubsMgrBuilder =
22 orc::createLocalIndirectStubsManagerBuilder(T);
24 OrcCBindingsStack *JITStack = new OrcCBindingsStack(
25 *TM2, std::move(CompileCallbackMgr), IndirectStubsMgrBuilder);
27 return wrap(JITStack);
30 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
31 OrcCBindingsStack &J = *unwrap(JITStack);
32 return J.getErrorMessage().c_str();
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());
43 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
46 LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
47 LLVMOrcLazyCompileCallbackFn Callback,
49 OrcCBindingsStack &J = *unwrap(JITStack);
50 return J.createLazyCompileCallback(Callback, CallbackCtx);
53 LLVMOrcErrorCode LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
55 LLVMOrcTargetAddress InitAddr) {
56 OrcCBindingsStack &J = *unwrap(JITStack);
57 return J.createIndirectStub(StubName, InitAddr);
60 LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
62 LLVMOrcTargetAddress NewAddr) {
63 OrcCBindingsStack &J = *unwrap(JITStack);
64 return J.setIndirectStubPointer(StubName, NewAddr);
68 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
69 LLVMOrcSymbolResolverFn SymbolResolver,
70 void *SymbolResolverCtx) {
71 OrcCBindingsStack &J = *unwrap(JITStack);
72 Module *M(unwrap(Mod));
73 return J.addIRModuleEager(M, SymbolResolver, SymbolResolverCtx);
77 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
78 LLVMOrcSymbolResolverFn SymbolResolver,
79 void *SymbolResolverCtx) {
80 OrcCBindingsStack &J = *unwrap(JITStack);
81 Module *M(unwrap(Mod));
82 return J.addIRModuleLazy(M, SymbolResolver, SymbolResolverCtx);
85 void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
86 OrcCBindingsStack &J = *unwrap(JITStack);
90 LLVMOrcTargetAddress LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
91 const char *SymbolName) {
92 OrcCBindingsStack &J = *unwrap(JITStack);
93 auto Sym = J.findSymbol(SymbolName, true);
94 return Sym.getAddress();
97 void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
98 delete unwrap(JITStack);