xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
15ffd83dbSDimitry Andric //===--------------- OrcV2CBindings.cpp - C bindings OrcV2 APIs -----------===//
25ffd83dbSDimitry Andric //
35ffd83dbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45ffd83dbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55ffd83dbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ffd83dbSDimitry Andric //
75ffd83dbSDimitry Andric //===----------------------------------------------------------------------===//
85ffd83dbSDimitry Andric 
9e8d8bef9SDimitry Andric #include "llvm-c/LLJIT.h"
105ffd83dbSDimitry Andric #include "llvm-c/Orc.h"
11e8d8bef9SDimitry Andric #include "llvm-c/OrcEE.h"
125ffd83dbSDimitry Andric #include "llvm-c/TargetMachine.h"
135ffd83dbSDimitry Andric 
145ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
155ffd83dbSDimitry Andric #include "llvm/ExecutionEngine/Orc/LLJIT.h"
16*fe6060f1SDimitry Andric #include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
17e8d8bef9SDimitry Andric #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
18e8d8bef9SDimitry Andric #include "llvm/ExecutionEngine/SectionMemoryManager.h"
195ffd83dbSDimitry Andric 
205ffd83dbSDimitry Andric using namespace llvm;
215ffd83dbSDimitry Andric using namespace llvm::orc;
225ffd83dbSDimitry Andric 
235ffd83dbSDimitry Andric namespace llvm {
245ffd83dbSDimitry Andric namespace orc {
255ffd83dbSDimitry Andric 
26e8d8bef9SDimitry Andric class InProgressLookupState;
27e8d8bef9SDimitry Andric 
285ffd83dbSDimitry Andric class OrcV2CAPIHelper {
295ffd83dbSDimitry Andric public:
305ffd83dbSDimitry Andric   using PoolEntry = SymbolStringPtr::PoolEntry;
315ffd83dbSDimitry Andric   using PoolEntryPtr = SymbolStringPtr::PoolEntryPtr;
325ffd83dbSDimitry Andric 
33*fe6060f1SDimitry Andric   // Move from SymbolStringPtr to PoolEntryPtr (no change in ref count).
34*fe6060f1SDimitry Andric   static PoolEntryPtr moveFromSymbolStringPtr(SymbolStringPtr S) {
355ffd83dbSDimitry Andric     PoolEntryPtr Result = nullptr;
365ffd83dbSDimitry Andric     std::swap(Result, S.S);
375ffd83dbSDimitry Andric     return Result;
385ffd83dbSDimitry Andric   }
395ffd83dbSDimitry Andric 
40*fe6060f1SDimitry Andric   // Move from a PoolEntryPtr to a SymbolStringPtr (no change in ref count).
41*fe6060f1SDimitry Andric   static SymbolStringPtr moveToSymbolStringPtr(PoolEntryPtr P) {
42*fe6060f1SDimitry Andric     SymbolStringPtr S;
43*fe6060f1SDimitry Andric     S.S = P;
44*fe6060f1SDimitry Andric     return S;
45*fe6060f1SDimitry Andric   }
46*fe6060f1SDimitry Andric 
47*fe6060f1SDimitry Andric   // Copy a pool entry to a SymbolStringPtr (increments ref count).
48*fe6060f1SDimitry Andric   static SymbolStringPtr copyToSymbolStringPtr(PoolEntryPtr P) {
49e8d8bef9SDimitry Andric     return SymbolStringPtr(P);
50e8d8bef9SDimitry Andric   }
51e8d8bef9SDimitry Andric 
525ffd83dbSDimitry Andric   static PoolEntryPtr getRawPoolEntryPtr(const SymbolStringPtr &S) {
535ffd83dbSDimitry Andric     return S.S;
545ffd83dbSDimitry Andric   }
555ffd83dbSDimitry Andric 
56e8d8bef9SDimitry Andric   static void retainPoolEntry(PoolEntryPtr P) {
57e8d8bef9SDimitry Andric     SymbolStringPtr S(P);
58e8d8bef9SDimitry Andric     S.S = nullptr;
59e8d8bef9SDimitry Andric   }
60e8d8bef9SDimitry Andric 
615ffd83dbSDimitry Andric   static void releasePoolEntry(PoolEntryPtr P) {
625ffd83dbSDimitry Andric     SymbolStringPtr S;
635ffd83dbSDimitry Andric     S.S = P;
645ffd83dbSDimitry Andric   }
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric   static InProgressLookupState *extractLookupState(LookupState &LS) {
67e8d8bef9SDimitry Andric     return LS.IPLS.release();
68e8d8bef9SDimitry Andric   }
69e8d8bef9SDimitry Andric 
70e8d8bef9SDimitry Andric   static void resetLookupState(LookupState &LS, InProgressLookupState *IPLS) {
71e8d8bef9SDimitry Andric     return LS.reset(IPLS);
72e8d8bef9SDimitry Andric   }
735ffd83dbSDimitry Andric };
745ffd83dbSDimitry Andric 
75e8d8bef9SDimitry Andric } // namespace orc
76e8d8bef9SDimitry Andric } // namespace llvm
775ffd83dbSDimitry Andric 
785ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionSession, LLVMOrcExecutionSessionRef)
79e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(SymbolStringPool, LLVMOrcSymbolStringPoolRef)
805ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OrcV2CAPIHelper::PoolEntry,
815ffd83dbSDimitry Andric                                    LLVMOrcSymbolStringPoolEntryRef)
82e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationUnit,
83e8d8bef9SDimitry Andric                                    LLVMOrcMaterializationUnitRef)
84*fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(MaterializationResponsibility,
85*fe6060f1SDimitry Andric                                    LLVMOrcMaterializationResponsibilityRef)
865ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITDylib, LLVMOrcJITDylibRef)
87e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ResourceTracker, LLVMOrcResourceTrackerRef)
88e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DefinitionGenerator,
89e8d8bef9SDimitry Andric                                    LLVMOrcDefinitionGeneratorRef)
90e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(InProgressLookupState, LLVMOrcLookupStateRef)
915ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeContext,
925ffd83dbSDimitry Andric                                    LLVMOrcThreadSafeContextRef)
935ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThreadSafeModule, LLVMOrcThreadSafeModuleRef)
945ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(JITTargetMachineBuilder,
955ffd83dbSDimitry Andric                                    LLVMOrcJITTargetMachineBuilderRef)
96e8d8bef9SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectLayer, LLVMOrcObjectLayerRef)
97*fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRTransformLayer, LLVMOrcIRTransformLayerRef)
98*fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ObjectTransformLayer,
99*fe6060f1SDimitry Andric                                    LLVMOrcObjectTransformLayerRef)
100*fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(DumpObjects, LLVMOrcDumpObjectsRef)
101*fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IndirectStubsManager,
102*fe6060f1SDimitry Andric                                    LLVMOrcIndirectStubsManagerRef)
103*fe6060f1SDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LazyCallThroughManager,
104*fe6060f1SDimitry Andric                                    LLVMOrcLazyCallThroughManagerRef)
1055ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJITBuilder, LLVMOrcLLJITBuilderRef)
1065ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LLJIT, LLVMOrcLLJITRef)
1075ffd83dbSDimitry Andric DEFINE_SIMPLE_CONVERSION_FUNCTIONS(TargetMachine, LLVMTargetMachineRef)
1085ffd83dbSDimitry Andric 
109e8d8bef9SDimitry Andric namespace llvm {
110e8d8bef9SDimitry Andric namespace orc {
111e8d8bef9SDimitry Andric 
112e8d8bef9SDimitry Andric class CAPIDefinitionGenerator final : public DefinitionGenerator {
113e8d8bef9SDimitry Andric public:
114e8d8bef9SDimitry Andric   CAPIDefinitionGenerator(
115e8d8bef9SDimitry Andric       void *Ctx,
116e8d8bef9SDimitry Andric       LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate)
117e8d8bef9SDimitry Andric       : Ctx(Ctx), TryToGenerate(TryToGenerate) {}
118e8d8bef9SDimitry Andric 
119e8d8bef9SDimitry Andric   Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
120e8d8bef9SDimitry Andric                       JITDylibLookupFlags JDLookupFlags,
121e8d8bef9SDimitry Andric                       const SymbolLookupSet &LookupSet) override {
122e8d8bef9SDimitry Andric 
123e8d8bef9SDimitry Andric     // Take the lookup state.
124e8d8bef9SDimitry Andric     LLVMOrcLookupStateRef LSR = ::wrap(OrcV2CAPIHelper::extractLookupState(LS));
125e8d8bef9SDimitry Andric 
126e8d8bef9SDimitry Andric     // Translate the lookup kind.
127e8d8bef9SDimitry Andric     LLVMOrcLookupKind CLookupKind;
128e8d8bef9SDimitry Andric     switch (K) {
129e8d8bef9SDimitry Andric     case LookupKind::Static:
130e8d8bef9SDimitry Andric       CLookupKind = LLVMOrcLookupKindStatic;
131e8d8bef9SDimitry Andric       break;
132e8d8bef9SDimitry Andric     case LookupKind::DLSym:
133e8d8bef9SDimitry Andric       CLookupKind = LLVMOrcLookupKindDLSym;
134e8d8bef9SDimitry Andric       break;
135e8d8bef9SDimitry Andric     }
136e8d8bef9SDimitry Andric 
137e8d8bef9SDimitry Andric     // Translate the JITDylibSearchFlags.
138e8d8bef9SDimitry Andric     LLVMOrcJITDylibLookupFlags CJDLookupFlags;
139e8d8bef9SDimitry Andric     switch (JDLookupFlags) {
140e8d8bef9SDimitry Andric     case JITDylibLookupFlags::MatchExportedSymbolsOnly:
141e8d8bef9SDimitry Andric       CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchExportedSymbolsOnly;
142e8d8bef9SDimitry Andric       break;
143e8d8bef9SDimitry Andric     case JITDylibLookupFlags::MatchAllSymbols:
144e8d8bef9SDimitry Andric       CJDLookupFlags = LLVMOrcJITDylibLookupFlagsMatchAllSymbols;
145e8d8bef9SDimitry Andric       break;
146e8d8bef9SDimitry Andric     }
147e8d8bef9SDimitry Andric 
148e8d8bef9SDimitry Andric     // Translate the lookup set.
149e8d8bef9SDimitry Andric     std::vector<LLVMOrcCLookupSetElement> CLookupSet;
150e8d8bef9SDimitry Andric     CLookupSet.reserve(LookupSet.size());
151e8d8bef9SDimitry Andric     for (auto &KV : LookupSet) {
152e8d8bef9SDimitry Andric       LLVMOrcSymbolLookupFlags SLF;
153e8d8bef9SDimitry Andric       LLVMOrcSymbolStringPoolEntryRef Name =
154e8d8bef9SDimitry Andric         ::wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(KV.first));
155e8d8bef9SDimitry Andric       switch (KV.second) {
156e8d8bef9SDimitry Andric       case SymbolLookupFlags::RequiredSymbol:
157e8d8bef9SDimitry Andric         SLF = LLVMOrcSymbolLookupFlagsRequiredSymbol;
158e8d8bef9SDimitry Andric         break;
159e8d8bef9SDimitry Andric       case SymbolLookupFlags::WeaklyReferencedSymbol:
160e8d8bef9SDimitry Andric         SLF = LLVMOrcSymbolLookupFlagsWeaklyReferencedSymbol;
161e8d8bef9SDimitry Andric         break;
162e8d8bef9SDimitry Andric       }
163e8d8bef9SDimitry Andric       CLookupSet.push_back({Name, SLF});
164e8d8bef9SDimitry Andric     }
165e8d8bef9SDimitry Andric 
166e8d8bef9SDimitry Andric     // Run the C TryToGenerate function.
167e8d8bef9SDimitry Andric     auto Err = unwrap(TryToGenerate(::wrap(this), Ctx, &LSR, CLookupKind,
168e8d8bef9SDimitry Andric                                     ::wrap(&JD), CJDLookupFlags,
169e8d8bef9SDimitry Andric                                     CLookupSet.data(), CLookupSet.size()));
170e8d8bef9SDimitry Andric 
171e8d8bef9SDimitry Andric     // Restore the lookup state.
172e8d8bef9SDimitry Andric     OrcV2CAPIHelper::resetLookupState(LS, ::unwrap(LSR));
173e8d8bef9SDimitry Andric 
174e8d8bef9SDimitry Andric     return Err;
175e8d8bef9SDimitry Andric   }
176e8d8bef9SDimitry Andric 
177e8d8bef9SDimitry Andric private:
178e8d8bef9SDimitry Andric   void *Ctx;
179e8d8bef9SDimitry Andric   LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction TryToGenerate;
180e8d8bef9SDimitry Andric };
181e8d8bef9SDimitry Andric 
182e8d8bef9SDimitry Andric } // end namespace orc
183e8d8bef9SDimitry Andric } // end namespace llvm
184e8d8bef9SDimitry Andric 
185*fe6060f1SDimitry Andric namespace {
186*fe6060f1SDimitry Andric 
187*fe6060f1SDimitry Andric class OrcCAPIMaterializationUnit : public llvm::orc::MaterializationUnit {
188*fe6060f1SDimitry Andric public:
189*fe6060f1SDimitry Andric   OrcCAPIMaterializationUnit(
190*fe6060f1SDimitry Andric       std::string Name, SymbolFlagsMap InitialSymbolFlags,
191*fe6060f1SDimitry Andric       SymbolStringPtr InitSymbol, void *Ctx,
192*fe6060f1SDimitry Andric       LLVMOrcMaterializationUnitMaterializeFunction Materialize,
193*fe6060f1SDimitry Andric       LLVMOrcMaterializationUnitDiscardFunction Discard,
194*fe6060f1SDimitry Andric       LLVMOrcMaterializationUnitDestroyFunction Destroy)
195*fe6060f1SDimitry Andric       : llvm::orc::MaterializationUnit(std::move(InitialSymbolFlags),
196*fe6060f1SDimitry Andric                                        std::move(InitSymbol)),
197*fe6060f1SDimitry Andric         Name(std::move(Name)), Ctx(Ctx), Materialize(Materialize),
198*fe6060f1SDimitry Andric         Discard(Discard), Destroy(Destroy) {}
199*fe6060f1SDimitry Andric 
200*fe6060f1SDimitry Andric   ~OrcCAPIMaterializationUnit() {
201*fe6060f1SDimitry Andric     if (Ctx)
202*fe6060f1SDimitry Andric       Destroy(Ctx);
203*fe6060f1SDimitry Andric   }
204*fe6060f1SDimitry Andric 
205*fe6060f1SDimitry Andric   StringRef getName() const override { return Name; }
206*fe6060f1SDimitry Andric 
207*fe6060f1SDimitry Andric   void materialize(std::unique_ptr<MaterializationResponsibility> R) override {
208*fe6060f1SDimitry Andric     void *Tmp = Ctx;
209*fe6060f1SDimitry Andric     Ctx = nullptr;
210*fe6060f1SDimitry Andric     Materialize(Tmp, wrap(R.release()));
211*fe6060f1SDimitry Andric   }
212*fe6060f1SDimitry Andric 
213*fe6060f1SDimitry Andric private:
214*fe6060f1SDimitry Andric   void discard(const JITDylib &JD, const SymbolStringPtr &Name) override {
215*fe6060f1SDimitry Andric     Discard(Ctx, wrap(&JD), wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)));
216*fe6060f1SDimitry Andric   }
217*fe6060f1SDimitry Andric 
218*fe6060f1SDimitry Andric   std::string Name;
219*fe6060f1SDimitry Andric   void *Ctx = nullptr;
220*fe6060f1SDimitry Andric   LLVMOrcMaterializationUnitMaterializeFunction Materialize = nullptr;
221*fe6060f1SDimitry Andric   LLVMOrcMaterializationUnitDiscardFunction Discard = nullptr;
222*fe6060f1SDimitry Andric   LLVMOrcMaterializationUnitDestroyFunction Destroy = nullptr;
223*fe6060f1SDimitry Andric };
224*fe6060f1SDimitry Andric 
225*fe6060f1SDimitry Andric static JITSymbolFlags toJITSymbolFlags(LLVMJITSymbolFlags F) {
226*fe6060f1SDimitry Andric 
227*fe6060f1SDimitry Andric   JITSymbolFlags JSF;
228*fe6060f1SDimitry Andric 
229*fe6060f1SDimitry Andric   if (F.GenericFlags & LLVMJITSymbolGenericFlagsExported)
230*fe6060f1SDimitry Andric     JSF |= JITSymbolFlags::Exported;
231*fe6060f1SDimitry Andric   if (F.GenericFlags & LLVMJITSymbolGenericFlagsWeak)
232*fe6060f1SDimitry Andric     JSF |= JITSymbolFlags::Weak;
233*fe6060f1SDimitry Andric   if (F.GenericFlags & LLVMJITSymbolGenericFlagsCallable)
234*fe6060f1SDimitry Andric     JSF |= JITSymbolFlags::Callable;
235*fe6060f1SDimitry Andric   if (F.GenericFlags & LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly)
236*fe6060f1SDimitry Andric     JSF |= JITSymbolFlags::MaterializationSideEffectsOnly;
237*fe6060f1SDimitry Andric 
238*fe6060f1SDimitry Andric   JSF.getTargetFlags() = F.TargetFlags;
239*fe6060f1SDimitry Andric 
240*fe6060f1SDimitry Andric   return JSF;
241*fe6060f1SDimitry Andric }
242*fe6060f1SDimitry Andric 
243*fe6060f1SDimitry Andric static LLVMJITSymbolFlags fromJITSymbolFlags(JITSymbolFlags JSF) {
244*fe6060f1SDimitry Andric   LLVMJITSymbolFlags F = {0, 0};
245*fe6060f1SDimitry Andric   if (JSF & JITSymbolFlags::Exported)
246*fe6060f1SDimitry Andric     F.GenericFlags |= LLVMJITSymbolGenericFlagsExported;
247*fe6060f1SDimitry Andric   if (JSF & JITSymbolFlags::Weak)
248*fe6060f1SDimitry Andric     F.GenericFlags |= LLVMJITSymbolGenericFlagsWeak;
249*fe6060f1SDimitry Andric   if (JSF & JITSymbolFlags::Callable)
250*fe6060f1SDimitry Andric     F.GenericFlags |= LLVMJITSymbolGenericFlagsCallable;
251*fe6060f1SDimitry Andric   if (JSF & JITSymbolFlags::MaterializationSideEffectsOnly)
252*fe6060f1SDimitry Andric     F.GenericFlags |= LLVMJITSymbolGenericFlagsMaterializationSideEffectsOnly;
253*fe6060f1SDimitry Andric 
254*fe6060f1SDimitry Andric   F.TargetFlags = JSF.getTargetFlags();
255*fe6060f1SDimitry Andric 
256*fe6060f1SDimitry Andric   return F;
257*fe6060f1SDimitry Andric }
258*fe6060f1SDimitry Andric 
259*fe6060f1SDimitry Andric static SymbolMap toSymbolMap(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) {
260*fe6060f1SDimitry Andric   SymbolMap SM;
261*fe6060f1SDimitry Andric   for (size_t I = 0; I != NumPairs; ++I) {
262*fe6060f1SDimitry Andric     JITSymbolFlags Flags = toJITSymbolFlags(Syms[I].Sym.Flags);
263*fe6060f1SDimitry Andric     SM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] =
264*fe6060f1SDimitry Andric         JITEvaluatedSymbol(Syms[I].Sym.Address, Flags);
265*fe6060f1SDimitry Andric   }
266*fe6060f1SDimitry Andric   return SM;
267*fe6060f1SDimitry Andric }
268*fe6060f1SDimitry Andric 
269*fe6060f1SDimitry Andric static SymbolDependenceMap
270*fe6060f1SDimitry Andric toSymbolDependenceMap(LLVMOrcCDependenceMapPairs Pairs, size_t NumPairs) {
271*fe6060f1SDimitry Andric   SymbolDependenceMap SDM;
272*fe6060f1SDimitry Andric   for (size_t I = 0; I != NumPairs; ++I) {
273*fe6060f1SDimitry Andric     JITDylib *JD = unwrap(Pairs[I].JD);
274*fe6060f1SDimitry Andric     SymbolNameSet Names;
275*fe6060f1SDimitry Andric 
276*fe6060f1SDimitry Andric     for (size_t J = 0; J != Pairs[I].Names.Length; ++J) {
277*fe6060f1SDimitry Andric       auto Sym = Pairs[I].Names.Symbols[J];
278*fe6060f1SDimitry Andric       Names.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Sym)));
279*fe6060f1SDimitry Andric     }
280*fe6060f1SDimitry Andric     SDM[JD] = Names;
281*fe6060f1SDimitry Andric   }
282*fe6060f1SDimitry Andric   return SDM;
283*fe6060f1SDimitry Andric }
284*fe6060f1SDimitry Andric 
285*fe6060f1SDimitry Andric } // end anonymous namespace
286*fe6060f1SDimitry Andric 
287e8d8bef9SDimitry Andric void LLVMOrcExecutionSessionSetErrorReporter(
288e8d8bef9SDimitry Andric     LLVMOrcExecutionSessionRef ES, LLVMOrcErrorReporterFunction ReportError,
289e8d8bef9SDimitry Andric     void *Ctx) {
290e8d8bef9SDimitry Andric   unwrap(ES)->setErrorReporter(
291e8d8bef9SDimitry Andric       [=](Error Err) { ReportError(Ctx, wrap(std::move(Err))); });
292e8d8bef9SDimitry Andric }
293e8d8bef9SDimitry Andric 
294e8d8bef9SDimitry Andric LLVMOrcSymbolStringPoolRef
295e8d8bef9SDimitry Andric LLVMOrcExecutionSessionGetSymbolStringPool(LLVMOrcExecutionSessionRef ES) {
296*fe6060f1SDimitry Andric   return wrap(
297*fe6060f1SDimitry Andric       unwrap(ES)->getExecutorProcessControl().getSymbolStringPool().get());
298e8d8bef9SDimitry Andric }
299e8d8bef9SDimitry Andric 
300e8d8bef9SDimitry Andric void LLVMOrcSymbolStringPoolClearDeadEntries(LLVMOrcSymbolStringPoolRef SSP) {
301e8d8bef9SDimitry Andric   unwrap(SSP)->clearDeadEntries();
302e8d8bef9SDimitry Andric }
303e8d8bef9SDimitry Andric 
3045ffd83dbSDimitry Andric LLVMOrcSymbolStringPoolEntryRef
3055ffd83dbSDimitry Andric LLVMOrcExecutionSessionIntern(LLVMOrcExecutionSessionRef ES, const char *Name) {
3065ffd83dbSDimitry Andric   return wrap(
307*fe6060f1SDimitry Andric       OrcV2CAPIHelper::moveFromSymbolStringPtr(unwrap(ES)->intern(Name)));
3085ffd83dbSDimitry Andric }
3095ffd83dbSDimitry Andric 
310e8d8bef9SDimitry Andric void LLVMOrcRetainSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) {
311e8d8bef9SDimitry Andric   OrcV2CAPIHelper::retainPoolEntry(unwrap(S));
312e8d8bef9SDimitry Andric }
313e8d8bef9SDimitry Andric 
3145ffd83dbSDimitry Andric void LLVMOrcReleaseSymbolStringPoolEntry(LLVMOrcSymbolStringPoolEntryRef S) {
3155ffd83dbSDimitry Andric   OrcV2CAPIHelper::releasePoolEntry(unwrap(S));
3165ffd83dbSDimitry Andric }
3175ffd83dbSDimitry Andric 
318e8d8bef9SDimitry Andric const char *LLVMOrcSymbolStringPoolEntryStr(LLVMOrcSymbolStringPoolEntryRef S) {
319e8d8bef9SDimitry Andric   return unwrap(S)->getKey().data();
320e8d8bef9SDimitry Andric }
321e8d8bef9SDimitry Andric 
322e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef
323e8d8bef9SDimitry Andric LLVMOrcJITDylibCreateResourceTracker(LLVMOrcJITDylibRef JD) {
324e8d8bef9SDimitry Andric   auto RT = unwrap(JD)->createResourceTracker();
325e8d8bef9SDimitry Andric   // Retain the pointer for the C API client.
326e8d8bef9SDimitry Andric   RT->Retain();
327e8d8bef9SDimitry Andric   return wrap(RT.get());
328e8d8bef9SDimitry Andric }
329e8d8bef9SDimitry Andric 
330e8d8bef9SDimitry Andric LLVMOrcResourceTrackerRef
331e8d8bef9SDimitry Andric LLVMOrcJITDylibGetDefaultResourceTracker(LLVMOrcJITDylibRef JD) {
332e8d8bef9SDimitry Andric   auto RT = unwrap(JD)->getDefaultResourceTracker();
333e8d8bef9SDimitry Andric   // Retain the pointer for the C API client.
334e8d8bef9SDimitry Andric   return wrap(RT.get());
335e8d8bef9SDimitry Andric }
336e8d8bef9SDimitry Andric 
337e8d8bef9SDimitry Andric void LLVMOrcReleaseResourceTracker(LLVMOrcResourceTrackerRef RT) {
338e8d8bef9SDimitry Andric   ResourceTrackerSP TmpRT(unwrap(RT));
339e8d8bef9SDimitry Andric   TmpRT->Release();
340e8d8bef9SDimitry Andric }
341e8d8bef9SDimitry Andric 
342e8d8bef9SDimitry Andric void LLVMOrcResourceTrackerTransferTo(LLVMOrcResourceTrackerRef SrcRT,
343e8d8bef9SDimitry Andric                                       LLVMOrcResourceTrackerRef DstRT) {
344e8d8bef9SDimitry Andric   ResourceTrackerSP TmpRT(unwrap(SrcRT));
345e8d8bef9SDimitry Andric   TmpRT->transferTo(*unwrap(DstRT));
346e8d8bef9SDimitry Andric }
347e8d8bef9SDimitry Andric 
348e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcResourceTrackerRemove(LLVMOrcResourceTrackerRef RT) {
349e8d8bef9SDimitry Andric   ResourceTrackerSP TmpRT(unwrap(RT));
350e8d8bef9SDimitry Andric   return wrap(TmpRT->remove());
351e8d8bef9SDimitry Andric }
352e8d8bef9SDimitry Andric 
353e8d8bef9SDimitry Andric void LLVMOrcDisposeDefinitionGenerator(LLVMOrcDefinitionGeneratorRef DG) {
354e8d8bef9SDimitry Andric   std::unique_ptr<DefinitionGenerator> TmpDG(unwrap(DG));
355e8d8bef9SDimitry Andric }
356e8d8bef9SDimitry Andric 
357e8d8bef9SDimitry Andric void LLVMOrcDisposeMaterializationUnit(LLVMOrcMaterializationUnitRef MU) {
358e8d8bef9SDimitry Andric   std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
359e8d8bef9SDimitry Andric }
360e8d8bef9SDimitry Andric 
361*fe6060f1SDimitry Andric LLVMOrcMaterializationUnitRef LLVMOrcCreateCustomMaterializationUnit(
362*fe6060f1SDimitry Andric     const char *Name, void *Ctx, LLVMOrcCSymbolFlagsMapPairs Syms,
363*fe6060f1SDimitry Andric     size_t NumSyms, LLVMOrcSymbolStringPoolEntryRef InitSym,
364*fe6060f1SDimitry Andric     LLVMOrcMaterializationUnitMaterializeFunction Materialize,
365*fe6060f1SDimitry Andric     LLVMOrcMaterializationUnitDiscardFunction Discard,
366*fe6060f1SDimitry Andric     LLVMOrcMaterializationUnitDestroyFunction Destroy) {
367*fe6060f1SDimitry Andric   SymbolFlagsMap SFM;
368*fe6060f1SDimitry Andric   for (size_t I = 0; I != NumSyms; ++I)
369*fe6060f1SDimitry Andric     SFM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] =
370*fe6060f1SDimitry Andric         toJITSymbolFlags(Syms[I].Flags);
371e8d8bef9SDimitry Andric 
372*fe6060f1SDimitry Andric   auto IS = OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(InitSym));
373e8d8bef9SDimitry Andric 
374*fe6060f1SDimitry Andric   return wrap(new OrcCAPIMaterializationUnit(
375*fe6060f1SDimitry Andric       Name, std::move(SFM), std::move(IS), Ctx, Materialize, Discard, Destroy));
376e8d8bef9SDimitry Andric }
377e8d8bef9SDimitry Andric 
378*fe6060f1SDimitry Andric LLVMOrcMaterializationUnitRef
379*fe6060f1SDimitry Andric LLVMOrcAbsoluteSymbols(LLVMOrcCSymbolMapPairs Syms, size_t NumPairs) {
380*fe6060f1SDimitry Andric   SymbolMap SM = toSymbolMap(Syms, NumPairs);
381e8d8bef9SDimitry Andric   return wrap(absoluteSymbols(std::move(SM)).release());
382e8d8bef9SDimitry Andric }
383e8d8bef9SDimitry Andric 
384*fe6060f1SDimitry Andric LLVMOrcMaterializationUnitRef LLVMOrcLazyReexports(
385*fe6060f1SDimitry Andric     LLVMOrcLazyCallThroughManagerRef LCTM, LLVMOrcIndirectStubsManagerRef ISM,
386*fe6060f1SDimitry Andric     LLVMOrcJITDylibRef SourceJD, LLVMOrcCSymbolAliasMapPairs CallableAliases,
387*fe6060f1SDimitry Andric     size_t NumPairs) {
388*fe6060f1SDimitry Andric 
389*fe6060f1SDimitry Andric   SymbolAliasMap SAM;
390*fe6060f1SDimitry Andric   for (size_t I = 0; I != NumPairs; ++I) {
391*fe6060f1SDimitry Andric     auto pair = CallableAliases[I];
392*fe6060f1SDimitry Andric     JITSymbolFlags Flags = toJITSymbolFlags(pair.Entry.Flags);
393*fe6060f1SDimitry Andric     SymbolStringPtr Name =
394*fe6060f1SDimitry Andric         OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair.Entry.Name));
395*fe6060f1SDimitry Andric     SAM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(pair.Name))] =
396*fe6060f1SDimitry Andric         SymbolAliasMapEntry(Name, Flags);
397*fe6060f1SDimitry Andric   }
398*fe6060f1SDimitry Andric 
399*fe6060f1SDimitry Andric   return wrap(lazyReexports(*unwrap(LCTM), *unwrap(ISM), *unwrap(SourceJD),
400*fe6060f1SDimitry Andric                             std::move(SAM))
401*fe6060f1SDimitry Andric                   .release());
402*fe6060f1SDimitry Andric }
403*fe6060f1SDimitry Andric 
404*fe6060f1SDimitry Andric void LLVMOrcDisposeMaterializationResponsibility(
405*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR) {
406*fe6060f1SDimitry Andric   std::unique_ptr<MaterializationResponsibility> TmpMR(unwrap(MR));
407*fe6060f1SDimitry Andric }
408*fe6060f1SDimitry Andric 
409*fe6060f1SDimitry Andric LLVMOrcJITDylibRef LLVMOrcMaterializationResponsibilityGetTargetDylib(
410*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR) {
411*fe6060f1SDimitry Andric   return wrap(&unwrap(MR)->getTargetJITDylib());
412*fe6060f1SDimitry Andric }
413*fe6060f1SDimitry Andric 
414*fe6060f1SDimitry Andric LLVMOrcExecutionSessionRef
415*fe6060f1SDimitry Andric LLVMOrcMaterializationResponsibilityGetExecutionSession(
416*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR) {
417*fe6060f1SDimitry Andric   return wrap(&unwrap(MR)->getExecutionSession());
418*fe6060f1SDimitry Andric }
419*fe6060f1SDimitry Andric 
420*fe6060f1SDimitry Andric LLVMOrcCSymbolFlagsMapPairs LLVMOrcMaterializationResponsibilityGetSymbols(
421*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR, size_t *NumPairs) {
422*fe6060f1SDimitry Andric 
423*fe6060f1SDimitry Andric   auto Symbols = unwrap(MR)->getSymbols();
424*fe6060f1SDimitry Andric   LLVMOrcCSymbolFlagsMapPairs Result = static_cast<LLVMOrcCSymbolFlagsMapPairs>(
425*fe6060f1SDimitry Andric       safe_malloc(Symbols.size() * sizeof(LLVMOrcCSymbolFlagsMapPair)));
426*fe6060f1SDimitry Andric   size_t I = 0;
427*fe6060f1SDimitry Andric   for (auto const &pair : Symbols) {
428*fe6060f1SDimitry Andric     auto Name = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(pair.first));
429*fe6060f1SDimitry Andric     auto Flags = pair.second;
430*fe6060f1SDimitry Andric     Result[I] = {Name, fromJITSymbolFlags(Flags)};
431*fe6060f1SDimitry Andric     I++;
432*fe6060f1SDimitry Andric   }
433*fe6060f1SDimitry Andric   *NumPairs = Symbols.size();
434*fe6060f1SDimitry Andric   return Result;
435*fe6060f1SDimitry Andric }
436*fe6060f1SDimitry Andric 
437*fe6060f1SDimitry Andric void LLVMOrcDisposeCSymbolFlagsMap(LLVMOrcCSymbolFlagsMapPairs Pairs) {
438*fe6060f1SDimitry Andric   free(Pairs);
439*fe6060f1SDimitry Andric }
440*fe6060f1SDimitry Andric 
441*fe6060f1SDimitry Andric LLVMOrcSymbolStringPoolEntryRef
442*fe6060f1SDimitry Andric LLVMOrcMaterializationResponsibilityGetInitializerSymbol(
443*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR) {
444*fe6060f1SDimitry Andric   auto Sym = unwrap(MR)->getInitializerSymbol();
445*fe6060f1SDimitry Andric   return wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Sym));
446*fe6060f1SDimitry Andric }
447*fe6060f1SDimitry Andric 
448*fe6060f1SDimitry Andric LLVMOrcSymbolStringPoolEntryRef *
449*fe6060f1SDimitry Andric LLVMOrcMaterializationResponsibilityGetRequestedSymbols(
450*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR, size_t *NumSymbols) {
451*fe6060f1SDimitry Andric 
452*fe6060f1SDimitry Andric   auto Symbols = unwrap(MR)->getRequestedSymbols();
453*fe6060f1SDimitry Andric   LLVMOrcSymbolStringPoolEntryRef *Result =
454*fe6060f1SDimitry Andric       static_cast<LLVMOrcSymbolStringPoolEntryRef *>(safe_malloc(
455*fe6060f1SDimitry Andric           Symbols.size() * sizeof(LLVMOrcSymbolStringPoolEntryRef)));
456*fe6060f1SDimitry Andric   size_t I = 0;
457*fe6060f1SDimitry Andric   for (auto &Name : Symbols) {
458*fe6060f1SDimitry Andric     Result[I] = wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name));
459*fe6060f1SDimitry Andric     I++;
460*fe6060f1SDimitry Andric   }
461*fe6060f1SDimitry Andric   *NumSymbols = Symbols.size();
462*fe6060f1SDimitry Andric   return Result;
463*fe6060f1SDimitry Andric }
464*fe6060f1SDimitry Andric 
465*fe6060f1SDimitry Andric void LLVMOrcDisposeSymbols(LLVMOrcSymbolStringPoolEntryRef *Symbols) {
466*fe6060f1SDimitry Andric   free(Symbols);
467*fe6060f1SDimitry Andric }
468*fe6060f1SDimitry Andric 
469*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyResolved(
470*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR, LLVMOrcCSymbolMapPairs Symbols,
471*fe6060f1SDimitry Andric     size_t NumPairs) {
472*fe6060f1SDimitry Andric   SymbolMap SM = toSymbolMap(Symbols, NumPairs);
473*fe6060f1SDimitry Andric   return wrap(unwrap(MR)->notifyResolved(std::move(SM)));
474*fe6060f1SDimitry Andric }
475*fe6060f1SDimitry Andric 
476*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcMaterializationResponsibilityNotifyEmitted(
477*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR) {
478*fe6060f1SDimitry Andric   return wrap(unwrap(MR)->notifyEmitted());
479*fe6060f1SDimitry Andric }
480*fe6060f1SDimitry Andric 
481*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcMaterializationResponsibilityDefineMaterializing(
482*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR,
483*fe6060f1SDimitry Andric     LLVMOrcCSymbolFlagsMapPairs Syms, size_t NumSyms) {
484*fe6060f1SDimitry Andric   SymbolFlagsMap SFM;
485*fe6060f1SDimitry Andric   for (size_t I = 0; I != NumSyms; ++I)
486*fe6060f1SDimitry Andric     SFM[OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Syms[I].Name))] =
487*fe6060f1SDimitry Andric         toJITSymbolFlags(Syms[I].Flags);
488*fe6060f1SDimitry Andric 
489*fe6060f1SDimitry Andric   return wrap(unwrap(MR)->defineMaterializing(std::move(SFM)));
490*fe6060f1SDimitry Andric }
491*fe6060f1SDimitry Andric 
492*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcMaterializationResponsibilityReplace(
493*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR,
494*fe6060f1SDimitry Andric     LLVMOrcMaterializationUnitRef MU) {
495*fe6060f1SDimitry Andric   std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
496*fe6060f1SDimitry Andric   return wrap(unwrap(MR)->replace(std::move(TmpMU)));
497*fe6060f1SDimitry Andric }
498*fe6060f1SDimitry Andric 
499*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcMaterializationResponsibilityDelegate(
500*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR,
501*fe6060f1SDimitry Andric     LLVMOrcSymbolStringPoolEntryRef *Symbols, size_t NumSymbols,
502*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef *Result) {
503*fe6060f1SDimitry Andric   SymbolNameSet Syms;
504*fe6060f1SDimitry Andric   for (size_t I = 0; I != NumSymbols; I++) {
505*fe6060f1SDimitry Andric     Syms.insert(OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Symbols[I])));
506*fe6060f1SDimitry Andric   }
507*fe6060f1SDimitry Andric   auto OtherMR = unwrap(MR)->delegate(Syms);
508*fe6060f1SDimitry Andric 
509*fe6060f1SDimitry Andric   if (!OtherMR) {
510*fe6060f1SDimitry Andric     return wrap(OtherMR.takeError());
511*fe6060f1SDimitry Andric   }
512*fe6060f1SDimitry Andric   *Result = wrap(OtherMR->release());
513*fe6060f1SDimitry Andric   return LLVMErrorSuccess;
514*fe6060f1SDimitry Andric }
515*fe6060f1SDimitry Andric 
516*fe6060f1SDimitry Andric void LLVMOrcMaterializationResponsibilityAddDependencies(
517*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR,
518*fe6060f1SDimitry Andric     LLVMOrcSymbolStringPoolEntryRef Name,
519*fe6060f1SDimitry Andric     LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs) {
520*fe6060f1SDimitry Andric 
521*fe6060f1SDimitry Andric   SymbolDependenceMap SDM = toSymbolDependenceMap(Dependencies, NumPairs);
522*fe6060f1SDimitry Andric   auto Sym = OrcV2CAPIHelper::moveToSymbolStringPtr(unwrap(Name));
523*fe6060f1SDimitry Andric   unwrap(MR)->addDependencies(Sym, SDM);
524*fe6060f1SDimitry Andric }
525*fe6060f1SDimitry Andric 
526*fe6060f1SDimitry Andric void LLVMOrcMaterializationResponsibilityAddDependenciesForAll(
527*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR,
528*fe6060f1SDimitry Andric     LLVMOrcCDependenceMapPairs Dependencies, size_t NumPairs) {
529*fe6060f1SDimitry Andric 
530*fe6060f1SDimitry Andric   SymbolDependenceMap SDM = toSymbolDependenceMap(Dependencies, NumPairs);
531*fe6060f1SDimitry Andric   unwrap(MR)->addDependenciesForAll(SDM);
532*fe6060f1SDimitry Andric }
533*fe6060f1SDimitry Andric 
534*fe6060f1SDimitry Andric void LLVMOrcMaterializationResponsibilityFailMaterialization(
535*fe6060f1SDimitry Andric     LLVMOrcMaterializationResponsibilityRef MR) {
536*fe6060f1SDimitry Andric   unwrap(MR)->failMaterialization();
537*fe6060f1SDimitry Andric }
538*fe6060f1SDimitry Andric 
539*fe6060f1SDimitry Andric void LLVMOrcIRTransformLayerEmit(LLVMOrcIRTransformLayerRef IRLayer,
540*fe6060f1SDimitry Andric                                  LLVMOrcMaterializationResponsibilityRef MR,
541*fe6060f1SDimitry Andric                                  LLVMOrcThreadSafeModuleRef TSM) {
542*fe6060f1SDimitry Andric   std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM));
543*fe6060f1SDimitry Andric   unwrap(IRLayer)->emit(
544*fe6060f1SDimitry Andric       std::unique_ptr<MaterializationResponsibility>(unwrap(MR)),
545*fe6060f1SDimitry Andric       std::move(*TmpTSM));
546*fe6060f1SDimitry Andric }
547*fe6060f1SDimitry Andric 
548e8d8bef9SDimitry Andric LLVMOrcJITDylibRef
549e8d8bef9SDimitry Andric LLVMOrcExecutionSessionCreateBareJITDylib(LLVMOrcExecutionSessionRef ES,
550e8d8bef9SDimitry Andric                                           const char *Name) {
551e8d8bef9SDimitry Andric   return wrap(&unwrap(ES)->createBareJITDylib(Name));
552e8d8bef9SDimitry Andric }
553e8d8bef9SDimitry Andric 
554e8d8bef9SDimitry Andric LLVMErrorRef
555e8d8bef9SDimitry Andric LLVMOrcExecutionSessionCreateJITDylib(LLVMOrcExecutionSessionRef ES,
556e8d8bef9SDimitry Andric                                       LLVMOrcJITDylibRef *Result,
557e8d8bef9SDimitry Andric                                       const char *Name) {
558e8d8bef9SDimitry Andric   auto JD = unwrap(ES)->createJITDylib(Name);
559e8d8bef9SDimitry Andric   if (!JD)
560e8d8bef9SDimitry Andric     return wrap(JD.takeError());
561e8d8bef9SDimitry Andric   *Result = wrap(&*JD);
562e8d8bef9SDimitry Andric   return LLVMErrorSuccess;
563e8d8bef9SDimitry Andric }
564e8d8bef9SDimitry Andric 
565e8d8bef9SDimitry Andric LLVMOrcJITDylibRef
566e8d8bef9SDimitry Andric LLVMOrcExecutionSessionGetJITDylibByName(LLVMOrcExecutionSessionRef ES,
567e8d8bef9SDimitry Andric                                          const char *Name) {
568e8d8bef9SDimitry Andric   return wrap(unwrap(ES)->getJITDylibByName(Name));
569e8d8bef9SDimitry Andric }
570e8d8bef9SDimitry Andric 
571e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcJITDylibDefine(LLVMOrcJITDylibRef JD,
572e8d8bef9SDimitry Andric                                    LLVMOrcMaterializationUnitRef MU) {
573e8d8bef9SDimitry Andric   std::unique_ptr<MaterializationUnit> TmpMU(unwrap(MU));
574e8d8bef9SDimitry Andric 
575e8d8bef9SDimitry Andric   if (auto Err = unwrap(JD)->define(TmpMU)) {
576e8d8bef9SDimitry Andric     TmpMU.release();
577e8d8bef9SDimitry Andric     return wrap(std::move(Err));
578e8d8bef9SDimitry Andric   }
579e8d8bef9SDimitry Andric   return LLVMErrorSuccess;
580e8d8bef9SDimitry Andric }
581e8d8bef9SDimitry Andric 
582e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcJITDylibClear(LLVMOrcJITDylibRef JD) {
583e8d8bef9SDimitry Andric   return wrap(unwrap(JD)->clear());
5845ffd83dbSDimitry Andric }
5855ffd83dbSDimitry Andric 
5865ffd83dbSDimitry Andric void LLVMOrcJITDylibAddGenerator(LLVMOrcJITDylibRef JD,
587e8d8bef9SDimitry Andric                                  LLVMOrcDefinitionGeneratorRef DG) {
588e8d8bef9SDimitry Andric   unwrap(JD)->addGenerator(std::unique_ptr<DefinitionGenerator>(unwrap(DG)));
589e8d8bef9SDimitry Andric }
590e8d8bef9SDimitry Andric 
591e8d8bef9SDimitry Andric LLVMOrcDefinitionGeneratorRef LLVMOrcCreateCustomCAPIDefinitionGenerator(
592e8d8bef9SDimitry Andric     LLVMOrcCAPIDefinitionGeneratorTryToGenerateFunction F, void *Ctx) {
593e8d8bef9SDimitry Andric   auto DG = std::make_unique<CAPIDefinitionGenerator>(Ctx, F);
594e8d8bef9SDimitry Andric   return wrap(DG.release());
5955ffd83dbSDimitry Andric }
5965ffd83dbSDimitry Andric 
5975ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(
598e8d8bef9SDimitry Andric     LLVMOrcDefinitionGeneratorRef *Result, char GlobalPrefix,
5995ffd83dbSDimitry Andric     LLVMOrcSymbolPredicate Filter, void *FilterCtx) {
6005ffd83dbSDimitry Andric   assert(Result && "Result can not be null");
6015ffd83dbSDimitry Andric   assert((Filter || !FilterCtx) &&
6025ffd83dbSDimitry Andric          "if Filter is null then FilterCtx must also be null");
6035ffd83dbSDimitry Andric 
6045ffd83dbSDimitry Andric   DynamicLibrarySearchGenerator::SymbolPredicate Pred;
6055ffd83dbSDimitry Andric   if (Filter)
6065ffd83dbSDimitry Andric     Pred = [=](const SymbolStringPtr &Name) -> bool {
607e8d8bef9SDimitry Andric       return Filter(FilterCtx, wrap(OrcV2CAPIHelper::getRawPoolEntryPtr(Name)));
6085ffd83dbSDimitry Andric     };
6095ffd83dbSDimitry Andric 
6105ffd83dbSDimitry Andric   auto ProcessSymsGenerator =
6115ffd83dbSDimitry Andric       DynamicLibrarySearchGenerator::GetForCurrentProcess(GlobalPrefix, Pred);
6125ffd83dbSDimitry Andric 
6135ffd83dbSDimitry Andric   if (!ProcessSymsGenerator) {
6145ffd83dbSDimitry Andric     *Result = 0;
6155ffd83dbSDimitry Andric     return wrap(ProcessSymsGenerator.takeError());
6165ffd83dbSDimitry Andric   }
6175ffd83dbSDimitry Andric 
6185ffd83dbSDimitry Andric   *Result = wrap(ProcessSymsGenerator->release());
6195ffd83dbSDimitry Andric   return LLVMErrorSuccess;
6205ffd83dbSDimitry Andric }
6215ffd83dbSDimitry Andric 
6225ffd83dbSDimitry Andric LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void) {
6235ffd83dbSDimitry Andric   return wrap(new ThreadSafeContext(std::make_unique<LLVMContext>()));
6245ffd83dbSDimitry Andric }
6255ffd83dbSDimitry Andric 
6265ffd83dbSDimitry Andric LLVMContextRef
6275ffd83dbSDimitry Andric LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx) {
6285ffd83dbSDimitry Andric   return wrap(unwrap(TSCtx)->getContext());
6295ffd83dbSDimitry Andric }
6305ffd83dbSDimitry Andric 
6315ffd83dbSDimitry Andric void LLVMOrcDisposeThreadSafeContext(LLVMOrcThreadSafeContextRef TSCtx) {
6325ffd83dbSDimitry Andric   delete unwrap(TSCtx);
6335ffd83dbSDimitry Andric }
6345ffd83dbSDimitry Andric 
635*fe6060f1SDimitry Andric LLVMErrorRef
636*fe6060f1SDimitry Andric LLVMOrcThreadSafeModuleWithModuleDo(LLVMOrcThreadSafeModuleRef TSM,
637*fe6060f1SDimitry Andric                                     LLVMOrcGenericIRModuleOperationFunction F,
638*fe6060f1SDimitry Andric                                     void *Ctx) {
639*fe6060f1SDimitry Andric   return wrap(unwrap(TSM)->withModuleDo(
640*fe6060f1SDimitry Andric       [&](Module &M) { return unwrap(F(Ctx, wrap(&M))); }));
641*fe6060f1SDimitry Andric }
642*fe6060f1SDimitry Andric 
6435ffd83dbSDimitry Andric LLVMOrcThreadSafeModuleRef
6445ffd83dbSDimitry Andric LLVMOrcCreateNewThreadSafeModule(LLVMModuleRef M,
6455ffd83dbSDimitry Andric                                  LLVMOrcThreadSafeContextRef TSCtx) {
6465ffd83dbSDimitry Andric   return wrap(
6475ffd83dbSDimitry Andric       new ThreadSafeModule(std::unique_ptr<Module>(unwrap(M)), *unwrap(TSCtx)));
6485ffd83dbSDimitry Andric }
6495ffd83dbSDimitry Andric 
6505ffd83dbSDimitry Andric void LLVMOrcDisposeThreadSafeModule(LLVMOrcThreadSafeModuleRef TSM) {
6515ffd83dbSDimitry Andric   delete unwrap(TSM);
6525ffd83dbSDimitry Andric }
6535ffd83dbSDimitry Andric 
6545ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcJITTargetMachineBuilderDetectHost(
6555ffd83dbSDimitry Andric     LLVMOrcJITTargetMachineBuilderRef *Result) {
6565ffd83dbSDimitry Andric   assert(Result && "Result can not be null");
6575ffd83dbSDimitry Andric 
6585ffd83dbSDimitry Andric   auto JTMB = JITTargetMachineBuilder::detectHost();
6595ffd83dbSDimitry Andric   if (!JTMB) {
6605ffd83dbSDimitry Andric     Result = 0;
6615ffd83dbSDimitry Andric     return wrap(JTMB.takeError());
6625ffd83dbSDimitry Andric   }
6635ffd83dbSDimitry Andric 
6645ffd83dbSDimitry Andric   *Result = wrap(new JITTargetMachineBuilder(std::move(*JTMB)));
6655ffd83dbSDimitry Andric   return LLVMErrorSuccess;
6665ffd83dbSDimitry Andric }
6675ffd83dbSDimitry Andric 
6685ffd83dbSDimitry Andric LLVMOrcJITTargetMachineBuilderRef
669e8d8bef9SDimitry Andric LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(LLVMTargetMachineRef TM) {
6705ffd83dbSDimitry Andric   auto *TemplateTM = unwrap(TM);
6715ffd83dbSDimitry Andric 
6725ffd83dbSDimitry Andric   auto JTMB =
6735ffd83dbSDimitry Andric       std::make_unique<JITTargetMachineBuilder>(TemplateTM->getTargetTriple());
6745ffd83dbSDimitry Andric 
6755ffd83dbSDimitry Andric   (*JTMB)
6765ffd83dbSDimitry Andric       .setCPU(TemplateTM->getTargetCPU().str())
6775ffd83dbSDimitry Andric       .setRelocationModel(TemplateTM->getRelocationModel())
6785ffd83dbSDimitry Andric       .setCodeModel(TemplateTM->getCodeModel())
6795ffd83dbSDimitry Andric       .setCodeGenOptLevel(TemplateTM->getOptLevel())
6805ffd83dbSDimitry Andric       .setFeatures(TemplateTM->getTargetFeatureString())
6815ffd83dbSDimitry Andric       .setOptions(TemplateTM->Options);
6825ffd83dbSDimitry Andric 
6835ffd83dbSDimitry Andric   LLVMDisposeTargetMachine(TM);
6845ffd83dbSDimitry Andric 
6855ffd83dbSDimitry Andric   return wrap(JTMB.release());
6865ffd83dbSDimitry Andric }
6875ffd83dbSDimitry Andric 
6885ffd83dbSDimitry Andric void LLVMOrcDisposeJITTargetMachineBuilder(
6895ffd83dbSDimitry Andric     LLVMOrcJITTargetMachineBuilderRef JTMB) {
6905ffd83dbSDimitry Andric   delete unwrap(JTMB);
6915ffd83dbSDimitry Andric }
6925ffd83dbSDimitry Andric 
693*fe6060f1SDimitry Andric char *LLVMOrcJITTargetMachineBuilderGetTargetTriple(
694*fe6060f1SDimitry Andric     LLVMOrcJITTargetMachineBuilderRef JTMB) {
695*fe6060f1SDimitry Andric   auto Tmp = unwrap(JTMB)->getTargetTriple().str();
696*fe6060f1SDimitry Andric   char *TargetTriple = (char *)malloc(Tmp.size() + 1);
697*fe6060f1SDimitry Andric   strcpy(TargetTriple, Tmp.c_str());
698*fe6060f1SDimitry Andric   return TargetTriple;
699*fe6060f1SDimitry Andric }
700*fe6060f1SDimitry Andric 
701*fe6060f1SDimitry Andric void LLVMOrcJITTargetMachineBuilderSetTargetTriple(
702*fe6060f1SDimitry Andric     LLVMOrcJITTargetMachineBuilderRef JTMB, const char *TargetTriple) {
703*fe6060f1SDimitry Andric   unwrap(JTMB)->getTargetTriple() = Triple(TargetTriple);
704*fe6060f1SDimitry Andric }
705*fe6060f1SDimitry Andric 
706*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcObjectLayerAddObjectFile(LLVMOrcObjectLayerRef ObjLayer,
707*fe6060f1SDimitry Andric                                              LLVMOrcJITDylibRef JD,
708*fe6060f1SDimitry Andric                                              LLVMMemoryBufferRef ObjBuffer) {
709*fe6060f1SDimitry Andric   return wrap(unwrap(ObjLayer)->add(
710*fe6060f1SDimitry Andric       *unwrap(JD), std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
711*fe6060f1SDimitry Andric }
712*fe6060f1SDimitry Andric 
713*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcObjectLayerRef ObjLayer,
714*fe6060f1SDimitry Andric                                              LLVMOrcResourceTrackerRef RT,
715*fe6060f1SDimitry Andric                                              LLVMMemoryBufferRef ObjBuffer) {
716*fe6060f1SDimitry Andric   return wrap(
717*fe6060f1SDimitry Andric       unwrap(ObjLayer)->add(ResourceTrackerSP(unwrap(RT)),
718*fe6060f1SDimitry Andric                             std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
719*fe6060f1SDimitry Andric }
720*fe6060f1SDimitry Andric 
721*fe6060f1SDimitry Andric void LLVMOrcObjectLayerEmit(LLVMOrcObjectLayerRef ObjLayer,
722*fe6060f1SDimitry Andric                             LLVMOrcMaterializationResponsibilityRef R,
723*fe6060f1SDimitry Andric                             LLVMMemoryBufferRef ObjBuffer) {
724*fe6060f1SDimitry Andric   unwrap(ObjLayer)->emit(
725*fe6060f1SDimitry Andric       std::unique_ptr<MaterializationResponsibility>(unwrap(R)),
726*fe6060f1SDimitry Andric       std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer)));
727*fe6060f1SDimitry Andric }
728*fe6060f1SDimitry Andric 
729d409305fSDimitry Andric void LLVMOrcDisposeObjectLayer(LLVMOrcObjectLayerRef ObjLayer) {
730e8d8bef9SDimitry Andric   delete unwrap(ObjLayer);
731e8d8bef9SDimitry Andric }
732e8d8bef9SDimitry Andric 
733*fe6060f1SDimitry Andric void LLVMOrcIRTransformLayerSetTransform(
734*fe6060f1SDimitry Andric     LLVMOrcIRTransformLayerRef IRTransformLayer,
735*fe6060f1SDimitry Andric     LLVMOrcIRTransformLayerTransformFunction TransformFunction, void *Ctx) {
736*fe6060f1SDimitry Andric   unwrap(IRTransformLayer)
737*fe6060f1SDimitry Andric       ->setTransform(
738*fe6060f1SDimitry Andric           [=](ThreadSafeModule TSM,
739*fe6060f1SDimitry Andric               MaterializationResponsibility &R) -> Expected<ThreadSafeModule> {
740*fe6060f1SDimitry Andric             LLVMOrcThreadSafeModuleRef TSMRef =
741*fe6060f1SDimitry Andric                 wrap(new ThreadSafeModule(std::move(TSM)));
742*fe6060f1SDimitry Andric             if (LLVMErrorRef Err = TransformFunction(Ctx, &TSMRef, wrap(&R))) {
743*fe6060f1SDimitry Andric               assert(!TSMRef && "TSMRef was not reset to null on error");
744*fe6060f1SDimitry Andric               return unwrap(Err);
745*fe6060f1SDimitry Andric             }
746*fe6060f1SDimitry Andric             return std::move(*unwrap(TSMRef));
747*fe6060f1SDimitry Andric           });
748*fe6060f1SDimitry Andric }
749*fe6060f1SDimitry Andric 
750*fe6060f1SDimitry Andric void LLVMOrcObjectTransformLayerSetTransform(
751*fe6060f1SDimitry Andric     LLVMOrcObjectTransformLayerRef ObjTransformLayer,
752*fe6060f1SDimitry Andric     LLVMOrcObjectTransformLayerTransformFunction TransformFunction, void *Ctx) {
753*fe6060f1SDimitry Andric   unwrap(ObjTransformLayer)
754*fe6060f1SDimitry Andric       ->setTransform([TransformFunction, Ctx](std::unique_ptr<MemoryBuffer> Obj)
755*fe6060f1SDimitry Andric                          -> Expected<std::unique_ptr<MemoryBuffer>> {
756*fe6060f1SDimitry Andric         LLVMMemoryBufferRef ObjBuffer = wrap(Obj.release());
757*fe6060f1SDimitry Andric         if (LLVMErrorRef Err = TransformFunction(Ctx, &ObjBuffer)) {
758*fe6060f1SDimitry Andric           assert(!ObjBuffer && "ObjBuffer was not reset to null on error");
759*fe6060f1SDimitry Andric           return unwrap(Err);
760*fe6060f1SDimitry Andric         }
761*fe6060f1SDimitry Andric         return std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer));
762*fe6060f1SDimitry Andric       });
763*fe6060f1SDimitry Andric }
764*fe6060f1SDimitry Andric 
765*fe6060f1SDimitry Andric LLVMOrcDumpObjectsRef LLVMOrcCreateDumpObjects(const char *DumpDir,
766*fe6060f1SDimitry Andric                                                const char *IdentifierOverride) {
767*fe6060f1SDimitry Andric   assert(DumpDir && "DumpDir should not be null");
768*fe6060f1SDimitry Andric   assert(IdentifierOverride && "IdentifierOverride should not be null");
769*fe6060f1SDimitry Andric   return wrap(new DumpObjects(DumpDir, IdentifierOverride));
770*fe6060f1SDimitry Andric }
771*fe6060f1SDimitry Andric 
772*fe6060f1SDimitry Andric void LLVMOrcDisposeDumpObjects(LLVMOrcDumpObjectsRef DumpObjects) {
773*fe6060f1SDimitry Andric   delete unwrap(DumpObjects);
774*fe6060f1SDimitry Andric }
775*fe6060f1SDimitry Andric 
776*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcDumpObjects_CallOperator(LLVMOrcDumpObjectsRef DumpObjects,
777*fe6060f1SDimitry Andric                                              LLVMMemoryBufferRef *ObjBuffer) {
778*fe6060f1SDimitry Andric   std::unique_ptr<MemoryBuffer> OB(unwrap(*ObjBuffer));
779*fe6060f1SDimitry Andric   if (auto Result = (*unwrap(DumpObjects))(std::move(OB))) {
780*fe6060f1SDimitry Andric     *ObjBuffer = wrap(Result->release());
781*fe6060f1SDimitry Andric     return LLVMErrorSuccess;
782*fe6060f1SDimitry Andric   } else {
783*fe6060f1SDimitry Andric     *ObjBuffer = nullptr;
784*fe6060f1SDimitry Andric     return wrap(Result.takeError());
785*fe6060f1SDimitry Andric   }
786*fe6060f1SDimitry Andric }
787*fe6060f1SDimitry Andric 
7885ffd83dbSDimitry Andric LLVMOrcLLJITBuilderRef LLVMOrcCreateLLJITBuilder(void) {
7895ffd83dbSDimitry Andric   return wrap(new LLJITBuilder());
7905ffd83dbSDimitry Andric }
7915ffd83dbSDimitry Andric 
7925ffd83dbSDimitry Andric void LLVMOrcDisposeLLJITBuilder(LLVMOrcLLJITBuilderRef Builder) {
7935ffd83dbSDimitry Andric   delete unwrap(Builder);
7945ffd83dbSDimitry Andric }
7955ffd83dbSDimitry Andric 
7965ffd83dbSDimitry Andric void LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(
7975ffd83dbSDimitry Andric     LLVMOrcLLJITBuilderRef Builder, LLVMOrcJITTargetMachineBuilderRef JTMB) {
798*fe6060f1SDimitry Andric   unwrap(Builder)->setJITTargetMachineBuilder(std::move(*unwrap(JTMB)));
799*fe6060f1SDimitry Andric   LLVMOrcDisposeJITTargetMachineBuilder(JTMB);
8005ffd83dbSDimitry Andric }
8015ffd83dbSDimitry Andric 
802e8d8bef9SDimitry Andric void LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(
803e8d8bef9SDimitry Andric     LLVMOrcLLJITBuilderRef Builder,
804e8d8bef9SDimitry Andric     LLVMOrcLLJITBuilderObjectLinkingLayerCreatorFunction F, void *Ctx) {
805e8d8bef9SDimitry Andric   unwrap(Builder)->setObjectLinkingLayerCreator(
806e8d8bef9SDimitry Andric       [=](ExecutionSession &ES, const Triple &TT) {
807e8d8bef9SDimitry Andric         auto TTStr = TT.str();
808e8d8bef9SDimitry Andric         return std::unique_ptr<ObjectLayer>(
809e8d8bef9SDimitry Andric             unwrap(F(Ctx, wrap(&ES), TTStr.c_str())));
810e8d8bef9SDimitry Andric       });
811e8d8bef9SDimitry Andric }
812e8d8bef9SDimitry Andric 
8135ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcCreateLLJIT(LLVMOrcLLJITRef *Result,
8145ffd83dbSDimitry Andric                                 LLVMOrcLLJITBuilderRef Builder) {
8155ffd83dbSDimitry Andric   assert(Result && "Result can not be null");
8165ffd83dbSDimitry Andric 
8175ffd83dbSDimitry Andric   if (!Builder)
8185ffd83dbSDimitry Andric     Builder = LLVMOrcCreateLLJITBuilder();
8195ffd83dbSDimitry Andric 
8205ffd83dbSDimitry Andric   auto J = unwrap(Builder)->create();
8215ffd83dbSDimitry Andric   LLVMOrcDisposeLLJITBuilder(Builder);
8225ffd83dbSDimitry Andric 
8235ffd83dbSDimitry Andric   if (!J) {
8245ffd83dbSDimitry Andric     Result = 0;
8255ffd83dbSDimitry Andric     return wrap(J.takeError());
8265ffd83dbSDimitry Andric   }
8275ffd83dbSDimitry Andric 
8285ffd83dbSDimitry Andric   *Result = wrap(J->release());
8295ffd83dbSDimitry Andric   return LLVMErrorSuccess;
8305ffd83dbSDimitry Andric }
8315ffd83dbSDimitry Andric 
8325ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcDisposeLLJIT(LLVMOrcLLJITRef J) {
8335ffd83dbSDimitry Andric   delete unwrap(J);
8345ffd83dbSDimitry Andric   return LLVMErrorSuccess;
8355ffd83dbSDimitry Andric }
8365ffd83dbSDimitry Andric 
8375ffd83dbSDimitry Andric LLVMOrcExecutionSessionRef LLVMOrcLLJITGetExecutionSession(LLVMOrcLLJITRef J) {
8385ffd83dbSDimitry Andric   return wrap(&unwrap(J)->getExecutionSession());
8395ffd83dbSDimitry Andric }
8405ffd83dbSDimitry Andric 
8415ffd83dbSDimitry Andric LLVMOrcJITDylibRef LLVMOrcLLJITGetMainJITDylib(LLVMOrcLLJITRef J) {
8425ffd83dbSDimitry Andric   return wrap(&unwrap(J)->getMainJITDylib());
8435ffd83dbSDimitry Andric }
8445ffd83dbSDimitry Andric 
8455ffd83dbSDimitry Andric const char *LLVMOrcLLJITGetTripleString(LLVMOrcLLJITRef J) {
8465ffd83dbSDimitry Andric   return unwrap(J)->getTargetTriple().str().c_str();
8475ffd83dbSDimitry Andric }
8485ffd83dbSDimitry Andric 
8495ffd83dbSDimitry Andric char LLVMOrcLLJITGetGlobalPrefix(LLVMOrcLLJITRef J) {
8505ffd83dbSDimitry Andric   return unwrap(J)->getDataLayout().getGlobalPrefix();
8515ffd83dbSDimitry Andric }
8525ffd83dbSDimitry Andric 
8535ffd83dbSDimitry Andric LLVMOrcSymbolStringPoolEntryRef
8545ffd83dbSDimitry Andric LLVMOrcLLJITMangleAndIntern(LLVMOrcLLJITRef J, const char *UnmangledName) {
855*fe6060f1SDimitry Andric   return wrap(OrcV2CAPIHelper::moveFromSymbolStringPtr(
8565ffd83dbSDimitry Andric       unwrap(J)->mangleAndIntern(UnmangledName)));
8575ffd83dbSDimitry Andric }
8585ffd83dbSDimitry Andric 
8595ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFile(LLVMOrcLLJITRef J, LLVMOrcJITDylibRef JD,
8605ffd83dbSDimitry Andric                                        LLVMMemoryBufferRef ObjBuffer) {
8615ffd83dbSDimitry Andric   return wrap(unwrap(J)->addObjectFile(
8625ffd83dbSDimitry Andric       *unwrap(JD), std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
8635ffd83dbSDimitry Andric }
8645ffd83dbSDimitry Andric 
865e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddObjectFileWithRT(LLVMOrcLLJITRef J,
866e8d8bef9SDimitry Andric                                              LLVMOrcResourceTrackerRef RT,
867e8d8bef9SDimitry Andric                                              LLVMMemoryBufferRef ObjBuffer) {
868e8d8bef9SDimitry Andric   return wrap(unwrap(J)->addObjectFile(
869e8d8bef9SDimitry Andric       ResourceTrackerSP(unwrap(RT)),
870e8d8bef9SDimitry Andric       std::unique_ptr<MemoryBuffer>(unwrap(ObjBuffer))));
871e8d8bef9SDimitry Andric }
872e8d8bef9SDimitry Andric 
8735ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModule(LLVMOrcLLJITRef J,
8745ffd83dbSDimitry Andric                                          LLVMOrcJITDylibRef JD,
8755ffd83dbSDimitry Andric                                          LLVMOrcThreadSafeModuleRef TSM) {
876e8d8bef9SDimitry Andric   std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM));
877e8d8bef9SDimitry Andric   return wrap(unwrap(J)->addIRModule(*unwrap(JD), std::move(*TmpTSM)));
878e8d8bef9SDimitry Andric }
879e8d8bef9SDimitry Andric 
880e8d8bef9SDimitry Andric LLVMErrorRef LLVMOrcLLJITAddLLVMIRModuleWithRT(LLVMOrcLLJITRef J,
881e8d8bef9SDimitry Andric                                                LLVMOrcResourceTrackerRef RT,
882e8d8bef9SDimitry Andric                                                LLVMOrcThreadSafeModuleRef TSM) {
883e8d8bef9SDimitry Andric   std::unique_ptr<ThreadSafeModule> TmpTSM(unwrap(TSM));
884e8d8bef9SDimitry Andric   return wrap(unwrap(J)->addIRModule(ResourceTrackerSP(unwrap(RT)),
885e8d8bef9SDimitry Andric                                      std::move(*TmpTSM)));
8865ffd83dbSDimitry Andric }
8875ffd83dbSDimitry Andric 
8885ffd83dbSDimitry Andric LLVMErrorRef LLVMOrcLLJITLookup(LLVMOrcLLJITRef J,
8895ffd83dbSDimitry Andric                                 LLVMOrcJITTargetAddress *Result,
8905ffd83dbSDimitry Andric                                 const char *Name) {
8915ffd83dbSDimitry Andric   assert(Result && "Result can not be null");
8925ffd83dbSDimitry Andric 
8935ffd83dbSDimitry Andric   auto Sym = unwrap(J)->lookup(Name);
8945ffd83dbSDimitry Andric   if (!Sym) {
8955ffd83dbSDimitry Andric     *Result = 0;
8965ffd83dbSDimitry Andric     return wrap(Sym.takeError());
8975ffd83dbSDimitry Andric   }
8985ffd83dbSDimitry Andric 
8995ffd83dbSDimitry Andric   *Result = Sym->getAddress();
9005ffd83dbSDimitry Andric   return LLVMErrorSuccess;
9015ffd83dbSDimitry Andric }
902e8d8bef9SDimitry Andric 
903*fe6060f1SDimitry Andric LLVMOrcObjectLayerRef LLVMOrcLLJITGetObjLinkingLayer(LLVMOrcLLJITRef J) {
904*fe6060f1SDimitry Andric   return wrap(&unwrap(J)->getObjLinkingLayer());
905*fe6060f1SDimitry Andric }
906*fe6060f1SDimitry Andric 
907*fe6060f1SDimitry Andric LLVMOrcObjectTransformLayerRef
908*fe6060f1SDimitry Andric LLVMOrcLLJITGetObjTransformLayer(LLVMOrcLLJITRef J) {
909*fe6060f1SDimitry Andric   return wrap(&unwrap(J)->getObjTransformLayer());
910*fe6060f1SDimitry Andric }
911*fe6060f1SDimitry Andric 
912e8d8bef9SDimitry Andric LLVMOrcObjectLayerRef
913e8d8bef9SDimitry Andric LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(
914e8d8bef9SDimitry Andric     LLVMOrcExecutionSessionRef ES) {
915e8d8bef9SDimitry Andric   assert(ES && "ES must not be null");
916e8d8bef9SDimitry Andric   return wrap(new RTDyldObjectLinkingLayer(
917e8d8bef9SDimitry Andric       *unwrap(ES), [] { return std::make_unique<SectionMemoryManager>(); }));
918e8d8bef9SDimitry Andric }
919e8d8bef9SDimitry Andric 
920e8d8bef9SDimitry Andric void LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(
921e8d8bef9SDimitry Andric     LLVMOrcObjectLayerRef RTDyldObjLinkingLayer,
922e8d8bef9SDimitry Andric     LLVMJITEventListenerRef Listener) {
923e8d8bef9SDimitry Andric   assert(RTDyldObjLinkingLayer && "RTDyldObjLinkingLayer must not be null");
924e8d8bef9SDimitry Andric   assert(Listener && "Listener must not be null");
925e8d8bef9SDimitry Andric   reinterpret_cast<RTDyldObjectLinkingLayer *>(unwrap(RTDyldObjLinkingLayer))
926e8d8bef9SDimitry Andric       ->registerJITEventListener(*unwrap(Listener));
927e8d8bef9SDimitry Andric }
928*fe6060f1SDimitry Andric 
929*fe6060f1SDimitry Andric LLVMOrcIRTransformLayerRef LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J) {
930*fe6060f1SDimitry Andric   return wrap(&unwrap(J)->getIRTransformLayer());
931*fe6060f1SDimitry Andric }
932*fe6060f1SDimitry Andric 
933*fe6060f1SDimitry Andric const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J) {
934*fe6060f1SDimitry Andric   return unwrap(J)->getDataLayout().getStringRepresentation().c_str();
935*fe6060f1SDimitry Andric }
936*fe6060f1SDimitry Andric 
937*fe6060f1SDimitry Andric LLVMOrcIndirectStubsManagerRef
938*fe6060f1SDimitry Andric LLVMOrcCreateLocalIndirectStubsManager(const char *TargetTriple) {
939*fe6060f1SDimitry Andric   auto builder = createLocalIndirectStubsManagerBuilder(Triple(TargetTriple));
940*fe6060f1SDimitry Andric   return wrap(builder().release());
941*fe6060f1SDimitry Andric }
942*fe6060f1SDimitry Andric 
943*fe6060f1SDimitry Andric void LLVMOrcDisposeIndirectStubsManager(LLVMOrcIndirectStubsManagerRef ISM) {
944*fe6060f1SDimitry Andric   std::unique_ptr<IndirectStubsManager> TmpISM(unwrap(ISM));
945*fe6060f1SDimitry Andric }
946*fe6060f1SDimitry Andric 
947*fe6060f1SDimitry Andric LLVMErrorRef LLVMOrcCreateLocalLazyCallThroughManager(
948*fe6060f1SDimitry Andric     const char *TargetTriple, LLVMOrcExecutionSessionRef ES,
949*fe6060f1SDimitry Andric     LLVMOrcJITTargetAddress ErrorHandlerAddr,
950*fe6060f1SDimitry Andric     LLVMOrcLazyCallThroughManagerRef *Result) {
951*fe6060f1SDimitry Andric   auto LCTM = createLocalLazyCallThroughManager(Triple(TargetTriple),
952*fe6060f1SDimitry Andric                                                 *unwrap(ES), ErrorHandlerAddr);
953*fe6060f1SDimitry Andric 
954*fe6060f1SDimitry Andric   if (!LCTM)
955*fe6060f1SDimitry Andric     return wrap(LCTM.takeError());
956*fe6060f1SDimitry Andric   *Result = wrap(LCTM->release());
957*fe6060f1SDimitry Andric   return LLVMErrorSuccess;
958*fe6060f1SDimitry Andric }
959*fe6060f1SDimitry Andric 
960*fe6060f1SDimitry Andric void LLVMOrcDisposeLazyCallThroughManager(
961*fe6060f1SDimitry Andric     LLVMOrcLazyCallThroughManagerRef LCM) {
962*fe6060f1SDimitry Andric   std::unique_ptr<LazyCallThroughManager> TmpLCM(unwrap(LCM));
963*fe6060f1SDimitry Andric }
964