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 = OrcCBindingsStack::createCompileCallbackMgr(T);
21   auto IndirectStubsMgrBuilder =
22     OrcCBindingsStack::createIndirectStubsMgrBuilder(T);
23 
24   OrcCBindingsStack *JITStack =
25     new OrcCBindingsStack(*TM2, std::move(CompileCallbackMgr),
26 			  IndirectStubsMgrBuilder);
27 
28   return wrap(JITStack);
29 }
30 
LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack,char ** MangledName,const char * SymbolName)31 void LLVMOrcGetMangledSymbol(LLVMOrcJITStackRef JITStack, char **MangledName,
32                              const char *SymbolName) {
33   OrcCBindingsStack &J = *unwrap(JITStack);
34   std::string Mangled = J.mangle(SymbolName);
35   *MangledName = new char[Mangled.size() + 1];
36   strcpy(*MangledName, Mangled.c_str());
37 }
38 
LLVMOrcDisposeMangledSymbol(char * MangledName)39 void LLVMOrcDisposeMangledSymbol(char *MangledName) {
40   delete[] MangledName;
41 }
42 
43 LLVMOrcTargetAddress
LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,LLVMOrcLazyCompileCallbackFn Callback,void * CallbackCtx)44 LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
45                                  LLVMOrcLazyCompileCallbackFn Callback,
46                                  void *CallbackCtx) {
47   OrcCBindingsStack &J = *unwrap(JITStack);
48   return J.createLazyCompileCallback(Callback, CallbackCtx);
49 }
50 
LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,const char * StubName,LLVMOrcTargetAddress InitAddr)51 void LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
52                                const char *StubName,
53                                LLVMOrcTargetAddress InitAddr) {
54   OrcCBindingsStack &J = *unwrap(JITStack);
55   J.createIndirectStub(StubName, InitAddr);
56 }
57 
LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,const char * StubName,LLVMOrcTargetAddress NewAddr)58 void LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
59                                    const char *StubName,
60                                    LLVMOrcTargetAddress NewAddr) {
61   OrcCBindingsStack &J = *unwrap(JITStack);
62   J.setIndirectStubPointer(StubName, NewAddr);
63 }
64 
65 LLVMOrcModuleHandle
LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,LLVMModuleRef Mod,LLVMOrcSymbolResolverFn SymbolResolver,void * SymbolResolverCtx)66 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
67                             LLVMOrcSymbolResolverFn SymbolResolver,
68                             void *SymbolResolverCtx) {
69   OrcCBindingsStack &J = *unwrap(JITStack);
70   Module *M(unwrap(Mod));
71   return J.addIRModuleEager(M, SymbolResolver, SymbolResolverCtx);
72 }
73 
74 LLVMOrcModuleHandle
LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,LLVMModuleRef Mod,LLVMOrcSymbolResolverFn SymbolResolver,void * SymbolResolverCtx)75 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack, LLVMModuleRef Mod,
76                            LLVMOrcSymbolResolverFn SymbolResolver,
77                            void *SymbolResolverCtx) {
78   OrcCBindingsStack &J = *unwrap(JITStack);
79   Module *M(unwrap(Mod));
80   return J.addIRModuleLazy(M, SymbolResolver, SymbolResolverCtx);
81 }
82 
LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,LLVMOrcModuleHandle H)83 void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
84   OrcCBindingsStack &J = *unwrap(JITStack);
85   J.removeModule(H);
86 }
87 
LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,const char * SymbolName)88 LLVMOrcTargetAddress LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
89                                              const char *SymbolName) {
90   OrcCBindingsStack &J = *unwrap(JITStack);
91   auto Sym = J.findSymbol(SymbolName, true);
92   return Sym.getAddress();
93 }
94 
LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack)95 void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
96   delete unwrap(JITStack);
97 }
98