blob: ce1ab594dfa39d400b8c9dea1646443aa954867e [file] [log] [blame]
Gordon Henriksen2a8cd892007-12-23 16:59:28 +00001//===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen2a8cd892007-12-23 16:59:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the C bindings for the ExecutionEngine library.
11//
12//===----------------------------------------------------------------------===//
13
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000014#include "llvm-c/ExecutionEngine.h"
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ExecutionEngine/GenericValue.h"
Filip Pizlo3fdbaff2013-05-22 02:46:43 +000017#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000018#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000021#include <cstring>
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000022
23using namespace llvm;
24
Chandler Carruthf58e3762014-04-22 03:04:17 +000025#define DEBUG_TYPE "jit"
26
Eric Christopher04d4e932013-04-22 22:47:22 +000027// Wrapping the C bindings types.
Filip Pizlodec20e42013-05-01 20:59:00 +000028DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
Eric Christopher04d4e932013-04-22 22:47:22 +000029
Eric Christopher04d4e932013-04-22 22:47:22 +000030
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000031inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
32 return
33 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
34}
35
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000036/*===-- Operations on generic values --------------------------------------===*/
37
38LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
39 unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +000040 LLVMBool IsSigned) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000041 GenericValue *GenVal = new GenericValue();
42 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
43 return wrap(GenVal);
44}
45
46LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
47 GenericValue *GenVal = new GenericValue();
48 GenVal->PointerVal = P;
49 return wrap(GenVal);
50}
51
52LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
53 GenericValue *GenVal = new GenericValue();
54 switch (unwrap(TyRef)->getTypeID()) {
55 case Type::FloatTyID:
56 GenVal->FloatVal = N;
57 break;
58 case Type::DoubleTyID:
59 GenVal->DoubleVal = N;
60 break;
61 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000062 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000063 }
64 return wrap(GenVal);
65}
66
67unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
68 return unwrap(GenValRef)->IntVal.getBitWidth();
69}
70
71unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
Chris Lattner25963c62010-01-09 22:27:07 +000072 LLVMBool IsSigned) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000073 GenericValue *GenVal = unwrap(GenValRef);
74 if (IsSigned)
75 return GenVal->IntVal.getSExtValue();
76 else
77 return GenVal->IntVal.getZExtValue();
78}
79
80void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
81 return unwrap(GenVal)->PointerVal;
82}
83
84double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
85 switch (unwrap(TyRef)->getTypeID()) {
86 case Type::FloatTyID:
87 return unwrap(GenVal)->FloatVal;
88 case Type::DoubleTyID:
89 return unwrap(GenVal)->DoubleVal;
90 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000091 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000092 }
93}
94
95void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
96 delete unwrap(GenVal);
97}
98
99/*===-- Operations on execution engines -----------------------------------===*/
100
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000101LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
102 LLVMModuleRef M,
103 char **OutError) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000104 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000105 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000106 builder.setEngineKind(EngineKind::Either)
107 .setErrorStr(&Error);
108 if (ExecutionEngine *EE = builder.create()){
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000109 *OutEE = wrap(EE);
110 return 0;
111 }
112 *OutError = strdup(Error.c_str());
113 return 1;
114}
115
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000116LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
117 LLVMModuleRef M,
118 char **OutError) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000119 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000120 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000121 builder.setEngineKind(EngineKind::Interpreter)
122 .setErrorStr(&Error);
123 if (ExecutionEngine *Interp = builder.create()) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000124 *OutInterp = wrap(Interp);
125 return 0;
126 }
127 *OutError = strdup(Error.c_str());
128 return 1;
129}
130
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000131LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
132 LLVMModuleRef M,
133 unsigned OptLevel,
134 char **OutError) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000135 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000136 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000137 builder.setEngineKind(EngineKind::JIT)
138 .setErrorStr(&Error)
139 .setOptLevel((CodeGenOpt::Level)OptLevel);
140 if (ExecutionEngine *JIT = builder.create()) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000141 *OutJIT = wrap(JIT);
142 return 0;
143 }
144 *OutError = strdup(Error.c_str());
145 return 1;
146}
147
Filip Pizlo85e0d272013-05-01 22:58:00 +0000148void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
149 size_t SizeOfPassedOptions) {
150 LLVMMCJITCompilerOptions options;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000151 memset(&options, 0, sizeof(options)); // Most fields are zero by default.
Filip Pizlo85e0d272013-05-01 22:58:00 +0000152 options.CodeModel = LLVMCodeModelJITDefault;
Filip Pizlo85e0d272013-05-01 22:58:00 +0000153
154 memcpy(PassedOptions, &options,
155 std::min(sizeof(options), SizeOfPassedOptions));
156}
157
158LLVMBool LLVMCreateMCJITCompilerForModule(
159 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
160 LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
161 char **OutError) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000162 LLVMMCJITCompilerOptions options;
163 // If the user passed a larger sized options struct, then they were compiled
164 // against a newer LLVM. Tell them that something is wrong.
165 if (SizeOfPassedOptions > sizeof(options)) {
166 *OutError = strdup(
Filip Pizlo85e0d272013-05-01 22:58:00 +0000167 "Refusing to use options struct that is larger than my own; assuming "
168 "LLVM library mismatch.");
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000169 return 1;
170 }
171
172 // Defend against the user having an old version of the API by ensuring that
173 // any fields they didn't see are cleared. We must defend against fields being
174 // set to the bitwise equivalent of zero, and assume that this means "do the
175 // default" as if that option hadn't been available.
Filip Pizlo85e0d272013-05-01 22:58:00 +0000176 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000177 memcpy(&options, PassedOptions, SizeOfPassedOptions);
178
179 TargetOptions targetOptions;
Filip Pizlo85e0d272013-05-01 22:58:00 +0000180 targetOptions.EnableFastISel = options.EnableFastISel;
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000181 std::unique_ptr<Module> Mod(unwrap(M));
182
183 if (Mod)
184 // Set function attribute "no-frame-pointer-elim" based on
185 // NoFramePointerElim.
Akira Hatanakae36505c2015-05-26 20:17:20 +0000186 for (auto &F : *Mod) {
187 auto Attrs = F.getAttributes();
188 auto Value = options.NoFramePointerElim ? "true" : "false";
189 Attrs = Attrs.addAttribute(F.getContext(), AttributeSet::FunctionIndex,
190 "no-frame-pointer-elim", Value);
191 F.setAttributes(Attrs);
192 }
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000193
194 std::string Error;
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000195 EngineBuilder builder(std::move(Mod));
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000196 builder.setEngineKind(EngineKind::JIT)
197 .setErrorStr(&Error)
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000198 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
Filip Pizlo85e0d272013-05-01 22:58:00 +0000199 .setCodeModel(unwrap(options.CodeModel))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000200 .setTargetOptions(targetOptions);
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000201 if (options.MCJMM)
Lang Hames4a5697e2014-12-03 00:51:19 +0000202 builder.setMCJITMemoryManager(
203 std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000204 if (ExecutionEngine *JIT = builder.create()) {
205 *OutJIT = wrap(JIT);
206 return 0;
207 }
208 *OutError = strdup(Error.c_str());
209 return 1;
210}
211
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000212LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
213 LLVMModuleProviderRef MP,
214 char **OutError) {
215 /* The module provider is now actually a module. */
216 return LLVMCreateExecutionEngineForModule(OutEE,
217 reinterpret_cast<LLVMModuleRef>(MP),
218 OutError);
219}
220
221LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
222 LLVMModuleProviderRef MP,
223 char **OutError) {
224 /* The module provider is now actually a module. */
225 return LLVMCreateInterpreterForModule(OutInterp,
226 reinterpret_cast<LLVMModuleRef>(MP),
227 OutError);
228}
229
230LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
231 LLVMModuleProviderRef MP,
232 unsigned OptLevel,
233 char **OutError) {
234 /* The module provider is now actually a module. */
235 return LLVMCreateJITCompilerForModule(OutJIT,
236 reinterpret_cast<LLVMModuleRef>(MP),
237 OptLevel, OutError);
238}
239
240
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000241void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
242 delete unwrap(EE);
243}
244
245void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
246 unwrap(EE)->runStaticConstructorsDestructors(false);
247}
248
249void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
250 unwrap(EE)->runStaticConstructorsDestructors(true);
251}
252
253int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
254 unsigned ArgC, const char * const *ArgV,
255 const char * const *EnvP) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000256 unwrap(EE)->finalizeObject();
257
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000258 std::vector<std::string> ArgVec;
259 for (unsigned I = 0; I != ArgC; ++I)
260 ArgVec.push_back(ArgV[I]);
261
262 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
263}
264
265LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
266 unsigned NumArgs,
267 LLVMGenericValueRef *Args) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000268 unwrap(EE)->finalizeObject();
269
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000270 std::vector<GenericValue> ArgVec;
271 ArgVec.reserve(NumArgs);
272 for (unsigned I = 0; I != NumArgs; ++I)
273 ArgVec.push_back(*unwrap(Args[I]));
274
275 GenericValue *Result = new GenericValue();
276 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
277 return wrap(Result);
278}
279
280void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000281}
282
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000283void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000284 unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000285}
286
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000287void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000288 /* The module provider is now actually a module. */
289 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
290}
291
292LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
293 LLVMModuleRef *OutMod, char **OutError) {
294 Module *Mod = unwrap(M);
295 unwrap(EE)->removeModule(Mod);
296 *OutMod = wrap(Mod);
297 return 0;
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000298}
299
Chris Lattner25963c62010-01-09 22:27:07 +0000300LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
301 LLVMModuleProviderRef MP,
302 LLVMModuleRef *OutMod, char **OutError) {
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000303 /* The module provider is now actually a module. */
304 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
305 OutError);
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000306}
307
Chris Lattner25963c62010-01-09 22:27:07 +0000308LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
309 LLVMValueRef *OutFn) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000310 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
311 *OutFn = wrap(F);
312 return 0;
313 }
314 return 1;
315}
Erick Tryzelaar8ac07c22008-03-27 00:27:14 +0000316
Filip Pizlo85e0d272013-05-01 22:58:00 +0000317void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
318 LLVMValueRef Fn) {
Eric Christopher79cc1e32014-09-02 22:28:02 +0000319 return nullptr;
Duncan Sands330134b2010-07-19 09:33:13 +0000320}
321
Erick Tryzelaar8ac07c22008-03-27 00:27:14 +0000322LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000323 return wrap(unwrap(EE)->getDataLayout());
Erick Tryzelaar8ac07c22008-03-27 00:27:14 +0000324}
Gordon Henriksen9f337542008-06-20 02:16:11 +0000325
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000326LLVMTargetMachineRef
327LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
328 return wrap(unwrap(EE)->getTargetMachine());
329}
330
Gordon Henriksen9f337542008-06-20 02:16:11 +0000331void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
332 void* Addr) {
333 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
334}
Chris Lattner41b43da2009-01-21 18:11:10 +0000335
336void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000337 unwrap(EE)->finalizeObject();
338
Chris Lattner41b43da2009-01-21 18:11:10 +0000339 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
340}
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000341
Peter Zotovc433cd72014-12-22 18:53:11 +0000342uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) {
343 return unwrap(EE)->getGlobalValueAddress(Name);
344}
345
346uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) {
347 return unwrap(EE)->getFunctionAddress(Name);
348}
349
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000350/*===-- Operations on memory managers -------------------------------------===*/
351
352namespace {
353
354struct SimpleBindingMMFunctions {
Anders Waldenborg9515b312013-09-30 19:11:32 +0000355 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
356 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
357 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
358 LLVMMemoryManagerDestroyCallback Destroy;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000359};
360
361class SimpleBindingMemoryManager : public RTDyldMemoryManager {
362public:
363 SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
364 void *Opaque);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000365 ~SimpleBindingMemoryManager() override;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000366
Craig Topperb51ff602014-03-08 07:51:20 +0000367 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
368 unsigned SectionID,
369 StringRef SectionName) override;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000370
Craig Topperb51ff602014-03-08 07:51:20 +0000371 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
372 unsigned SectionID, StringRef SectionName,
373 bool isReadOnly) override;
374
375 bool finalizeMemory(std::string *ErrMsg) override;
376
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000377private:
378 SimpleBindingMMFunctions Functions;
379 void *Opaque;
380};
381
382SimpleBindingMemoryManager::SimpleBindingMemoryManager(
383 const SimpleBindingMMFunctions& Functions,
384 void *Opaque)
385 : Functions(Functions), Opaque(Opaque) {
386 assert(Functions.AllocateCodeSection &&
387 "No AllocateCodeSection function provided!");
388 assert(Functions.AllocateDataSection &&
389 "No AllocateDataSection function provided!");
390 assert(Functions.FinalizeMemory &&
391 "No FinalizeMemory function provided!");
392 assert(Functions.Destroy &&
393 "No Destroy function provided!");
394}
395
396SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
397 Functions.Destroy(Opaque);
398}
399
400uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000401 uintptr_t Size, unsigned Alignment, unsigned SectionID,
402 StringRef SectionName) {
403 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
404 SectionName.str().c_str());
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000405}
406
407uint8_t *SimpleBindingMemoryManager::allocateDataSection(
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000408 uintptr_t Size, unsigned Alignment, unsigned SectionID,
409 StringRef SectionName, bool isReadOnly) {
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000410 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000411 SectionName.str().c_str(),
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000412 isReadOnly);
413}
414
415bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000416 char *errMsgCString = nullptr;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000417 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
418 assert((result || !errMsgCString) &&
419 "Did not expect an error message if FinalizeMemory succeeded");
420 if (errMsgCString) {
421 if (ErrMsg)
422 *ErrMsg = errMsgCString;
423 free(errMsgCString);
424 }
425 return result;
426}
427
428} // anonymous namespace
429
430LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
431 void *Opaque,
Anders Waldenborg9515b312013-09-30 19:11:32 +0000432 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
433 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
434 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
435 LLVMMemoryManagerDestroyCallback Destroy) {
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000436
437 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
438 !Destroy)
Craig Topper2617dcc2014-04-15 06:32:26 +0000439 return nullptr;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000440
441 SimpleBindingMMFunctions functions;
442 functions.AllocateCodeSection = AllocateCodeSection;
443 functions.AllocateDataSection = AllocateDataSection;
444 functions.FinalizeMemory = FinalizeMemory;
445 functions.Destroy = Destroy;
446 return wrap(new SimpleBindingMemoryManager(functions, Opaque));
447}
448
449void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {
450 delete unwrap(MM);
451}
452