• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------- OrcCBindings.cpp - C bindings for the Orc APIs -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "OrcCBindingsStack.h"
11 #include "llvm-c/OrcBindings.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 CompileCallbackMgr = orc::createLocalCompileCallbackManager(T, 0);
21   auto IndirectStubsMgrBuilder =
22       orc::createLocalIndirectStubsManagerBuilder(T);
23 
24   OrcCBindingsStack *JITStack = new OrcCBindingsStack(
25       *TM2, std::move(CompileCallbackMgr), IndirectStubsMgrBuilder);
26 
27   return wrap(JITStack);
28 }
29 
LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack)30 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
31   OrcCBindingsStack &J = *unwrap(JITStack);
32   return J.getErrorMessage().c_str();
33 }
34 
LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack,char ** MangledName,const char * SymbolName)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());
41 }
42 
LLVMOrcDisposeMangledSymbol(char * MangledName)43 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
44 
45 LLVMOrcTargetAddress
LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,LLVMOrcLazyCompileCallbackFn Callback,void * CallbackCtx)46 LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
47                                  LLVMOrcLazyCompileCallbackFn Callback,
48                                  void *CallbackCtx) {
49   OrcCBindingsStack &J = *unwrap(JITStack);
50   return J.createLazyCompileCallback(Callback, CallbackCtx);
51 }
52 
LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,const char * StubName,LLVMOrcTargetAddress InitAddr)53 LLVMOrcErrorCode LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
54                                            const char *StubName,
55                                            LLVMOrcTargetAddress InitAddr) {
56   OrcCBindingsStack &J = *unwrap(JITStack);
57   return J.createIndirectStub(StubName, InitAddr);
58 }
59 
LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,const char * StubName,LLVMOrcTargetAddress NewAddr)60 LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
61                                                const char *StubName,
62                                                LLVMOrcTargetAddress NewAddr) {
63   OrcCBindingsStack &J = *unwrap(JITStack);
64   return J.setIndirectStubPointer(StubName, NewAddr);
65 }
66 
67 LLVMOrcModuleHandle
LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,LLVMModuleRef Mod,LLVMOrcSymbolResolverFn SymbolResolver,void * SymbolResolverCtx)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);
74 }
75 
76 LLVMOrcModuleHandle
LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,LLVMModuleRef Mod,LLVMOrcSymbolResolverFn SymbolResolver,void * 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);
83 }
84 
LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,LLVMOrcModuleHandle H)85 void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
86   OrcCBindingsStack &J = *unwrap(JITStack);
87   J.removeModule(H);
88 }
89 
LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,const char * SymbolName)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();
95 }
96 
LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack)97 void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
98   delete unwrap(JITStack);
99 }
100