2015-10-28 10:40:04 +08:00
|
|
|
//===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OrcCBindingsStack.h"
|
|
|
|
#include "llvm-c/OrcBindings.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-06-24 05:45:29 +08:00
|
|
|
LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod) {
|
|
|
|
return wrap(new std::shared_ptr<Module>(unwrap(Mod)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod) {
|
|
|
|
delete unwrap(SharedMod);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMSharedObjectBufferRef
|
|
|
|
LLVMOrcMakeSharedObjectBuffer(LLVMMemoryBufferRef ObjBuffer) {
|
|
|
|
return wrap(new std::shared_ptr<MemoryBuffer>(unwrap(ObjBuffer)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LLVMOrcDisposeSharedObjectBufferRef(LLVMSharedObjectBufferRef SharedObjBuffer) {
|
|
|
|
delete unwrap(SharedObjBuffer);
|
|
|
|
}
|
|
|
|
|
2015-11-04 00:40:37 +08:00
|
|
|
LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
|
2015-10-28 10:40:04 +08:00
|
|
|
TargetMachine *TM2(unwrap(TM));
|
|
|
|
|
|
|
|
Triple T(TM2->getTargetTriple());
|
|
|
|
|
2016-05-27 01:20:35 +08:00
|
|
|
auto CompileCallbackMgr = orc::createLocalCompileCallbackManager(T, 0);
|
2015-10-28 10:40:04 +08:00
|
|
|
auto IndirectStubsMgrBuilder =
|
2016-05-27 01:20:35 +08:00
|
|
|
orc::createLocalIndirectStubsManagerBuilder(T);
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2016-04-26 05:21:20 +08:00
|
|
|
OrcCBindingsStack *JITStack = new OrcCBindingsStack(
|
|
|
|
*TM2, std::move(CompileCallbackMgr), IndirectStubsMgrBuilder);
|
2015-10-28 10:40:04 +08:00
|
|
|
|
|
|
|
return wrap(JITStack);
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:56:45 +08:00
|
|
|
const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
|
|
|
return J.getErrorMessage().c_str();
|
|
|
|
}
|
|
|
|
|
2015-10-28 10:40:04 +08:00
|
|
|
void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledName,
|
|
|
|
const char *SymbolName) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
|
|
|
std::string Mangled = J.mangle(SymbolName);
|
|
|
|
*MangledName = new char[Mangled.size() + 1];
|
|
|
|
strcpy(*MangledName, Mangled.c_str());
|
|
|
|
}
|
|
|
|
|
2016-04-26 05:21:20 +08:00
|
|
|
void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
|
2015-10-28 10:40:04 +08:00
|
|
|
|
2015-10-30 11:20:21 +08:00
|
|
|
LLVMOrcTargetAddress
|
|
|
|
LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
|
|
|
|
LLVMOrcLazyCompileCallbackFn Callback,
|
|
|
|
void *CallbackCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
|
|
|
return J.createLazyCompileCallback(Callback, CallbackCtx);
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:56:45 +08:00
|
|
|
LLVMOrcErrorCode LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
|
|
|
|
const char *StubName,
|
|
|
|
LLVMOrcTargetAddress InitAddr) {
|
2015-10-30 11:20:21 +08:00
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2016-04-26 03:56:45 +08:00
|
|
|
return J.createIndirectStub(StubName, InitAddr);
|
2015-10-30 11:20:21 +08:00
|
|
|
}
|
|
|
|
|
2016-04-26 03:56:45 +08:00
|
|
|
LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
|
|
|
|
const char *StubName,
|
|
|
|
LLVMOrcTargetAddress NewAddr) {
|
2015-10-30 11:20:21 +08:00
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2016-04-26 03:56:45 +08:00
|
|
|
return J.setIndirectStubPointer(StubName, NewAddr);
|
2015-10-30 11:20:21 +08:00
|
|
|
}
|
|
|
|
|
2015-10-28 10:40:04 +08:00
|
|
|
LLVMOrcModuleHandle
|
2017-06-24 05:45:29 +08:00
|
|
|
LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
|
|
|
|
LLVMSharedModuleRef Mod,
|
2015-10-28 10:40:04 +08:00
|
|
|
LLVMOrcSymbolResolverFn SymbolResolver,
|
|
|
|
void *SymbolResolverCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2017-06-24 05:45:29 +08:00
|
|
|
std::shared_ptr<Module> *M(unwrap(Mod));
|
|
|
|
return J.addIRModuleEager(*M, SymbolResolver, SymbolResolverCtx);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMOrcModuleHandle
|
2017-06-24 05:45:29 +08:00
|
|
|
LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
|
|
|
|
LLVMSharedModuleRef Mod,
|
2015-10-28 10:40:04 +08:00
|
|
|
LLVMOrcSymbolResolverFn SymbolResolver,
|
|
|
|
void *SymbolResolverCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2017-06-24 05:45:29 +08:00
|
|
|
std::shared_ptr<Module> *M(unwrap(Mod));
|
|
|
|
return J.addIRModuleLazy(*M, SymbolResolver, SymbolResolverCtx);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
|
|
|
J.removeModule(H);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMOrcTargetAddress LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
|
|
|
|
const char *SymbolName) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
|
|
|
auto Sym = J.findSymbol(SymbolName, true);
|
|
|
|
return Sym.getAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
|
|
|
|
delete unwrap(JITStack);
|
|
|
|
}
|