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;
|
|
|
|
|
2018-03-15 05:32:34 +08:00
|
|
|
LLVMSharedModuleRef LLVMOrcMakeSharedModule(LLVMModuleRef Mod) {
|
|
|
|
return wrap(new std::shared_ptr<Module>(unwrap(Mod)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMOrcDisposeSharedModuleRef(LLVMSharedModuleRef SharedMod) {
|
|
|
|
delete unwrap(SharedMod);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode
|
2015-10-30 11:20:21 +08:00
|
|
|
LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcTargetAddress *RetAddr,
|
2015-10-30 11:20:21 +08:00
|
|
|
LLVMOrcLazyCompileCallbackFn Callback,
|
|
|
|
void *CallbackCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2017-07-07 10:59:13 +08:00
|
|
|
return J.createLazyCompileCallback(*RetAddr, Callback, CallbackCtx);
|
2015-10-30 11:20:21 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode
|
2017-06-24 07:25:28 +08:00
|
|
|
LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcModuleHandle *RetHandle,
|
2018-03-15 05:32:34 +08:00
|
|
|
LLVMSharedModuleRef Mod,
|
2015-10-28 10:40:04 +08:00
|
|
|
LLVMOrcSymbolResolverFn SymbolResolver,
|
|
|
|
void *SymbolResolverCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2018-03-15 05:32:34 +08:00
|
|
|
std::shared_ptr<Module> *M(unwrap(Mod));
|
|
|
|
return J.addIRModuleEager(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode
|
2017-06-24 07:25:28 +08:00
|
|
|
LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcModuleHandle *RetHandle,
|
2018-03-15 05:32:34 +08:00
|
|
|
LLVMSharedModuleRef Mod,
|
2015-10-28 10:40:04 +08:00
|
|
|
LLVMOrcSymbolResolverFn SymbolResolver,
|
|
|
|
void *SymbolResolverCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2018-03-15 05:32:34 +08:00
|
|
|
std::shared_ptr<Module> *M(unwrap(Mod));
|
|
|
|
return J.addIRModuleLazy(*RetHandle, *M, SymbolResolver, SymbolResolverCtx);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2017-09-17 11:25:03 +08:00
|
|
|
LLVMOrcErrorCode
|
|
|
|
LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
|
|
|
|
LLVMOrcModuleHandle *RetHandle,
|
|
|
|
LLVMMemoryBufferRef Obj,
|
|
|
|
LLVMOrcSymbolResolverFn SymbolResolver,
|
|
|
|
void *SymbolResolverCtx) {
|
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
|
|
|
std::unique_ptr<MemoryBuffer> O(unwrap(Obj));
|
|
|
|
return J.addObject(*RetHandle, std::move(O), SymbolResolver,
|
|
|
|
SymbolResolverCtx);
|
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
|
|
|
|
LLVMOrcModuleHandle H) {
|
2015-10-28 10:40:04 +08:00
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2017-07-07 10:59:13 +08:00
|
|
|
return J.removeModule(H);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
|
|
|
|
LLVMOrcTargetAddress *RetAddr,
|
|
|
|
const char *SymbolName) {
|
2015-10-28 10:40:04 +08:00
|
|
|
OrcCBindingsStack &J = *unwrap(JITStack);
|
2017-07-07 10:59:13 +08:00
|
|
|
return J.findSymbolAddress(*RetAddr, SymbolName, true);
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 10:59:13 +08:00
|
|
|
LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
|
|
|
|
auto *J = unwrap(JITStack);
|
|
|
|
auto Err = J->shutdown();
|
|
|
|
delete J;
|
|
|
|
return Err;
|
2015-10-28 10:40:04 +08:00
|
|
|
}
|