Home | History | Annotate | Download | only in Orc
      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 #include "llvm/ExecutionEngine/JITEventListener.h"
     13 
     14 using namespace llvm;
     15 
     16 LLVMOrcJITStackRef LLVMOrcCreateInstance(LLVMTargetMachineRef TM) {
     17   TargetMachine *TM2(unwrap(TM));
     18 
     19   Triple T(TM2->getTargetTriple());
     20 
     21   auto IndirectStubsMgrBuilder =
     22       orc::createLocalIndirectStubsManagerBuilder(T);
     23 
     24   OrcCBindingsStack *JITStack =
     25       new OrcCBindingsStack(*TM2, std::move(IndirectStubsMgrBuilder));
     26 
     27   return wrap(JITStack);
     28 }
     29 
     30 const char *LLVMOrcGetErrorMsg(LLVMOrcJITStackRef JITStack) {
     31   OrcCBindingsStack &J = *unwrap(JITStack);
     32   return J.getErrorMessage().c_str();
     33 }
     34 
     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 
     43 void LLVMOrcDisposeMangledSymbol(char *MangledName) { delete[] MangledName; }
     44 
     45 LLVMOrcErrorCode
     46 LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
     47                                  LLVMOrcTargetAddress *RetAddr,
     48                                  LLVMOrcLazyCompileCallbackFn Callback,
     49                                  void *CallbackCtx) {
     50   OrcCBindingsStack &J = *unwrap(JITStack);
     51   return J.createLazyCompileCallback(*RetAddr, Callback, CallbackCtx);
     52 }
     53 
     54 LLVMOrcErrorCode LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
     55                                            const char *StubName,
     56                                            LLVMOrcTargetAddress InitAddr) {
     57   OrcCBindingsStack &J = *unwrap(JITStack);
     58   return J.createIndirectStub(StubName, InitAddr);
     59 }
     60 
     61 LLVMOrcErrorCode LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
     62                                                const char *StubName,
     63                                                LLVMOrcTargetAddress NewAddr) {
     64   OrcCBindingsStack &J = *unwrap(JITStack);
     65   return J.setIndirectStubPointer(StubName, NewAddr);
     66 }
     67 
     68 LLVMOrcErrorCode
     69 LLVMOrcAddEagerlyCompiledIR(LLVMOrcJITStackRef JITStack,
     70                             LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod,
     71                             LLVMOrcSymbolResolverFn SymbolResolver,
     72                             void *SymbolResolverCtx) {
     73   OrcCBindingsStack &J = *unwrap(JITStack);
     74   std::unique_ptr<Module> M(unwrap(Mod));
     75   return J.addIRModuleEager(*RetHandle, std::move(M), SymbolResolver,
     76                             SymbolResolverCtx);
     77 }
     78 
     79 LLVMOrcErrorCode
     80 LLVMOrcAddLazilyCompiledIR(LLVMOrcJITStackRef JITStack,
     81                            LLVMOrcModuleHandle *RetHandle, LLVMModuleRef Mod,
     82                            LLVMOrcSymbolResolverFn SymbolResolver,
     83                            void *SymbolResolverCtx) {
     84   OrcCBindingsStack &J = *unwrap(JITStack);
     85   std::unique_ptr<Module> M(unwrap(Mod));
     86   return J.addIRModuleLazy(*RetHandle, std::move(M), SymbolResolver,
     87                            SymbolResolverCtx);
     88 }
     89 
     90 LLVMOrcErrorCode
     91 LLVMOrcAddObjectFile(LLVMOrcJITStackRef JITStack,
     92                      LLVMOrcModuleHandle *RetHandle,
     93                      LLVMMemoryBufferRef Obj,
     94                      LLVMOrcSymbolResolverFn SymbolResolver,
     95                      void *SymbolResolverCtx) {
     96   OrcCBindingsStack &J = *unwrap(JITStack);
     97   std::unique_ptr<MemoryBuffer> O(unwrap(Obj));
     98   return J.addObject(*RetHandle, std::move(O), SymbolResolver,
     99                      SymbolResolverCtx);
    100 }
    101 
    102 LLVMOrcErrorCode LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack,
    103                                      LLVMOrcModuleHandle H) {
    104   OrcCBindingsStack &J = *unwrap(JITStack);
    105   return J.removeModule(H);
    106 }
    107 
    108 LLVMOrcErrorCode LLVMOrcGetSymbolAddress(LLVMOrcJITStackRef JITStack,
    109                                          LLVMOrcTargetAddress *RetAddr,
    110                                          const char *SymbolName) {
    111   OrcCBindingsStack &J = *unwrap(JITStack);
    112   return J.findSymbolAddress(*RetAddr, SymbolName, true);
    113 }
    114 
    115 LLVMOrcErrorCode LLVMOrcGetSymbolAddressIn(LLVMOrcJITStackRef JITStack,
    116                                            LLVMOrcTargetAddress *RetAddr,
    117                                            LLVMOrcModuleHandle H,
    118                                            const char *SymbolName) {
    119   OrcCBindingsStack &J = *unwrap(JITStack);
    120   return J.findSymbolAddressIn(*RetAddr, H, SymbolName, true);
    121 }
    122 
    123 LLVMOrcErrorCode LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
    124   auto *J = unwrap(JITStack);
    125   auto Err = J->shutdown();
    126   delete J;
    127   return Err;
    128 }
    129 
    130 void LLVMOrcRegisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L)
    131 {
    132   unwrap(JITStack)->RegisterJITEventListener(unwrap(L));
    133 }
    134 
    135 void LLVMOrcUnregisterJITEventListener(LLVMOrcJITStackRef JITStack, LLVMJITEventListenerRef L)
    136 {
    137   unwrap(JITStack)->UnregisterJITEventListener(unwrap(L));
    138 }
    139