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
13using namespace llvm;
14
15LLVMOrcJITStackRef 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
31void 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
39void LLVMOrcDisposeMangledSymbol(char *MangledName) {
40  delete[] MangledName;
41}
42
43LLVMOrcTargetAddress
44LLVMOrcCreateLazyCompileCallback(LLVMOrcJITStackRef JITStack,
45                                 LLVMOrcLazyCompileCallbackFn Callback,
46                                 void *CallbackCtx) {
47  OrcCBindingsStack &J = *unwrap(JITStack);
48  return J.createLazyCompileCallback(Callback, CallbackCtx);
49}
50
51void LLVMOrcCreateIndirectStub(LLVMOrcJITStackRef JITStack,
52                               const char *StubName,
53                               LLVMOrcTargetAddress InitAddr) {
54  OrcCBindingsStack &J = *unwrap(JITStack);
55  J.createIndirectStub(StubName, InitAddr);
56}
57
58void LLVMOrcSetIndirectStubPointer(LLVMOrcJITStackRef JITStack,
59                                   const char *StubName,
60                                   LLVMOrcTargetAddress NewAddr) {
61  OrcCBindingsStack &J = *unwrap(JITStack);
62  J.setIndirectStubPointer(StubName, NewAddr);
63}
64
65LLVMOrcModuleHandle
66LLVMOrcAddEagerlyCompiledIR(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
74LLVMOrcModuleHandle
75LLVMOrcAddLazilyCompiledIR(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
83void LLVMOrcRemoveModule(LLVMOrcJITStackRef JITStack, LLVMOrcModuleHandle H) {
84  OrcCBindingsStack &J = *unwrap(JITStack);
85  J.removeModule(H);
86}
87
88LLVMOrcTargetAddress 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
95void LLVMOrcDisposeInstance(LLVMOrcJITStackRef JITStack) {
96  delete unwrap(JITStack);
97}
98