blob: f7aba4cec04853d57d0c2bc8758ba4cd8cb9d4ed [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"
Sanjay Pateld9fb62e2015-06-01 21:56:56 +000021#include "llvm/Target/TargetOptions.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000022#include <cstring>
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000023
24using namespace llvm;
25
Chandler Carruthf58e3762014-04-22 03:04:17 +000026#define DEBUG_TYPE "jit"
27
Eric Christopher04d4e932013-04-22 22:47:22 +000028// Wrapping the C bindings types.
Filip Pizlodec20e42013-05-01 20:59:00 +000029DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
Eric Christopher04d4e932013-04-22 22:47:22 +000030
Eric Christopher04d4e932013-04-22 22:47:22 +000031
Diego Novillocd973c42015-07-27 18:27:23 +000032static LLVMTargetMachineRef wrap(const TargetMachine *P) {
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000033 return
34 reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
35}
36
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000037/*===-- Operations on generic values --------------------------------------===*/
38
39LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
40 unsigned long long N,
Chris Lattner25963c62010-01-09 22:27:07 +000041 LLVMBool IsSigned) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000042 GenericValue *GenVal = new GenericValue();
43 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
44 return wrap(GenVal);
45}
46
47LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
48 GenericValue *GenVal = new GenericValue();
49 GenVal->PointerVal = P;
50 return wrap(GenVal);
51}
52
53LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
54 GenericValue *GenVal = new GenericValue();
55 switch (unwrap(TyRef)->getTypeID()) {
56 case Type::FloatTyID:
57 GenVal->FloatVal = N;
58 break;
59 case Type::DoubleTyID:
60 GenVal->DoubleVal = N;
61 break;
62 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000063 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000064 }
65 return wrap(GenVal);
66}
67
68unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
69 return unwrap(GenValRef)->IntVal.getBitWidth();
70}
71
72unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
Chris Lattner25963c62010-01-09 22:27:07 +000073 LLVMBool IsSigned) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000074 GenericValue *GenVal = unwrap(GenValRef);
75 if (IsSigned)
76 return GenVal->IntVal.getSExtValue();
77 else
78 return GenVal->IntVal.getZExtValue();
79}
80
81void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
82 return unwrap(GenVal)->PointerVal;
83}
84
85double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
86 switch (unwrap(TyRef)->getTypeID()) {
87 case Type::FloatTyID:
88 return unwrap(GenVal)->FloatVal;
89 case Type::DoubleTyID:
90 return unwrap(GenVal)->DoubleVal;
91 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +000092 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
Gordon Henriksen2a8cd892007-12-23 16:59:28 +000093 }
94}
95
96void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
97 delete unwrap(GenVal);
98}
99
100/*===-- Operations on execution engines -----------------------------------===*/
101
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000102LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
103 LLVMModuleRef M,
104 char **OutError) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000105 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000106 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000107 builder.setEngineKind(EngineKind::Either)
108 .setErrorStr(&Error);
109 if (ExecutionEngine *EE = builder.create()){
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000110 *OutEE = wrap(EE);
111 return 0;
112 }
113 *OutError = strdup(Error.c_str());
114 return 1;
115}
116
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000117LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
118 LLVMModuleRef M,
119 char **OutError) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000120 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000121 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000122 builder.setEngineKind(EngineKind::Interpreter)
123 .setErrorStr(&Error);
124 if (ExecutionEngine *Interp = builder.create()) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000125 *OutInterp = wrap(Interp);
126 return 0;
127 }
128 *OutError = strdup(Error.c_str());
129 return 1;
130}
131
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000132LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
133 LLVMModuleRef M,
134 unsigned OptLevel,
135 char **OutError) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000136 std::string Error;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000137 EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000138 builder.setEngineKind(EngineKind::JIT)
139 .setErrorStr(&Error)
140 .setOptLevel((CodeGenOpt::Level)OptLevel);
141 if (ExecutionEngine *JIT = builder.create()) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000142 *OutJIT = wrap(JIT);
143 return 0;
144 }
145 *OutError = strdup(Error.c_str());
146 return 1;
147}
148
Filip Pizlo85e0d272013-05-01 22:58:00 +0000149void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
150 size_t SizeOfPassedOptions) {
151 LLVMMCJITCompilerOptions options;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000152 memset(&options, 0, sizeof(options)); // Most fields are zero by default.
Filip Pizlo85e0d272013-05-01 22:58:00 +0000153 options.CodeModel = LLVMCodeModelJITDefault;
Filip Pizlo85e0d272013-05-01 22:58:00 +0000154
155 memcpy(PassedOptions, &options,
156 std::min(sizeof(options), SizeOfPassedOptions));
157}
158
159LLVMBool LLVMCreateMCJITCompilerForModule(
160 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
161 LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
162 char **OutError) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000163 LLVMMCJITCompilerOptions options;
164 // If the user passed a larger sized options struct, then they were compiled
165 // against a newer LLVM. Tell them that something is wrong.
166 if (SizeOfPassedOptions > sizeof(options)) {
167 *OutError = strdup(
Filip Pizlo85e0d272013-05-01 22:58:00 +0000168 "Refusing to use options struct that is larger than my own; assuming "
169 "LLVM library mismatch.");
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000170 return 1;
171 }
172
173 // Defend against the user having an old version of the API by ensuring that
174 // any fields they didn't see are cleared. We must defend against fields being
175 // set to the bitwise equivalent of zero, and assume that this means "do the
176 // default" as if that option hadn't been available.
Filip Pizlo85e0d272013-05-01 22:58:00 +0000177 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000178 memcpy(&options, PassedOptions, SizeOfPassedOptions);
179
180 TargetOptions targetOptions;
Filip Pizlo85e0d272013-05-01 22:58:00 +0000181 targetOptions.EnableFastISel = options.EnableFastISel;
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000182 std::unique_ptr<Module> Mod(unwrap(M));
183
184 if (Mod)
185 // Set function attribute "no-frame-pointer-elim" based on
186 // NoFramePointerElim.
Akira Hatanakae36505c2015-05-26 20:17:20 +0000187 for (auto &F : *Mod) {
188 auto Attrs = F.getAttributes();
189 auto Value = options.NoFramePointerElim ? "true" : "false";
190 Attrs = Attrs.addAttribute(F.getContext(), AttributeSet::FunctionIndex,
191 "no-frame-pointer-elim", Value);
192 F.setAttributes(Attrs);
193 }
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000194
195 std::string Error;
Akira Hatanakaddf76aa2015-05-23 01:14:08 +0000196 EngineBuilder builder(std::move(Mod));
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000197 builder.setEngineKind(EngineKind::JIT)
198 .setErrorStr(&Error)
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000199 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
Filip Pizlo85e0d272013-05-01 22:58:00 +0000200 .setCodeModel(unwrap(options.CodeModel))
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000201 .setTargetOptions(targetOptions);
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000202 if (options.MCJMM)
Lang Hames4a5697e2014-12-03 00:51:19 +0000203 builder.setMCJITMemoryManager(
204 std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000205 if (ExecutionEngine *JIT = builder.create()) {
206 *OutJIT = wrap(JIT);
207 return 0;
208 }
209 *OutError = strdup(Error.c_str());
210 return 1;
211}
212
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000213LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
214 LLVMModuleProviderRef MP,
215 char **OutError) {
216 /* The module provider is now actually a module. */
217 return LLVMCreateExecutionEngineForModule(OutEE,
218 reinterpret_cast<LLVMModuleRef>(MP),
219 OutError);
220}
221
222LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
223 LLVMModuleProviderRef MP,
224 char **OutError) {
225 /* The module provider is now actually a module. */
226 return LLVMCreateInterpreterForModule(OutInterp,
227 reinterpret_cast<LLVMModuleRef>(MP),
228 OutError);
229}
230
231LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
232 LLVMModuleProviderRef MP,
233 unsigned OptLevel,
234 char **OutError) {
235 /* The module provider is now actually a module. */
236 return LLVMCreateJITCompilerForModule(OutJIT,
237 reinterpret_cast<LLVMModuleRef>(MP),
238 OptLevel, OutError);
239}
240
241
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000242void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
243 delete unwrap(EE);
244}
245
246void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
247 unwrap(EE)->runStaticConstructorsDestructors(false);
248}
249
250void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
251 unwrap(EE)->runStaticConstructorsDestructors(true);
252}
253
254int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
255 unsigned ArgC, const char * const *ArgV,
256 const char * const *EnvP) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000257 unwrap(EE)->finalizeObject();
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +0000258
259 std::vector<std::string> ArgVec(ArgV, ArgV + ArgC);
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000260 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
261}
262
263LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
264 unsigned NumArgs,
265 LLVMGenericValueRef *Args) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000266 unwrap(EE)->finalizeObject();
267
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000268 std::vector<GenericValue> ArgVec;
269 ArgVec.reserve(NumArgs);
270 for (unsigned I = 0; I != NumArgs; ++I)
271 ArgVec.push_back(*unwrap(Args[I]));
272
273 GenericValue *Result = new GenericValue();
274 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
275 return wrap(Result);
276}
277
278void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000279}
280
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000281void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000282 unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000283}
284
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000285void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000286 /* The module provider is now actually a module. */
287 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
288}
289
290LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
291 LLVMModuleRef *OutMod, char **OutError) {
292 Module *Mod = unwrap(M);
293 unwrap(EE)->removeModule(Mod);
294 *OutMod = wrap(Mod);
295 return 0;
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000296}
297
Chris Lattner25963c62010-01-09 22:27:07 +0000298LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
299 LLVMModuleProviderRef MP,
300 LLVMModuleRef *OutMod, char **OutError) {
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000301 /* The module provider is now actually a module. */
302 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
303 OutError);
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000304}
305
Chris Lattner25963c62010-01-09 22:27:07 +0000306LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
307 LLVMValueRef *OutFn) {
Gordon Henriksen2a8cd892007-12-23 16:59:28 +0000308 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
309 *OutFn = wrap(F);
310 return 0;
311 }
312 return 1;
313}
Erick Tryzelaar8ac07c22008-03-27 00:27:14 +0000314
Filip Pizlo85e0d272013-05-01 22:58:00 +0000315void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
316 LLVMValueRef Fn) {
Eric Christopher79cc1e32014-09-02 22:28:02 +0000317 return nullptr;
Duncan Sands330134b2010-07-19 09:33:13 +0000318}
319
Erick Tryzelaar8ac07c22008-03-27 00:27:14 +0000320LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
Mehdi Aminia3fcefb2015-07-16 16:34:23 +0000321 return wrap(&unwrap(EE)->getDataLayout());
Erick Tryzelaar8ac07c22008-03-27 00:27:14 +0000322}
Gordon Henriksen9f337542008-06-20 02:16:11 +0000323
Juergen Ributzka5fe955c2014-01-23 19:23:28 +0000324LLVMTargetMachineRef
325LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
326 return wrap(unwrap(EE)->getTargetMachine());
327}
328
Gordon Henriksen9f337542008-06-20 02:16:11 +0000329void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
330 void* Addr) {
331 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
332}
Chris Lattner41b43da2009-01-21 18:11:10 +0000333
334void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
Andrew Kaylor31be5ef2013-04-29 17:49:40 +0000335 unwrap(EE)->finalizeObject();
336
Chris Lattner41b43da2009-01-21 18:11:10 +0000337 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
338}
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000339
Peter Zotovc433cd72014-12-22 18:53:11 +0000340uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) {
341 return unwrap(EE)->getGlobalValueAddress(Name);
342}
343
344uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) {
345 return unwrap(EE)->getFunctionAddress(Name);
346}
347
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000348/*===-- Operations on memory managers -------------------------------------===*/
349
350namespace {
351
352struct SimpleBindingMMFunctions {
Anders Waldenborg9515b312013-09-30 19:11:32 +0000353 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
354 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
355 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
356 LLVMMemoryManagerDestroyCallback Destroy;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000357};
358
359class SimpleBindingMemoryManager : public RTDyldMemoryManager {
360public:
361 SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
362 void *Opaque);
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000363 ~SimpleBindingMemoryManager() override;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000364
Craig Topperb51ff602014-03-08 07:51:20 +0000365 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
366 unsigned SectionID,
367 StringRef SectionName) override;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000368
Craig Topperb51ff602014-03-08 07:51:20 +0000369 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
370 unsigned SectionID, StringRef SectionName,
371 bool isReadOnly) override;
372
373 bool finalizeMemory(std::string *ErrMsg) override;
374
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000375private:
376 SimpleBindingMMFunctions Functions;
377 void *Opaque;
378};
379
380SimpleBindingMemoryManager::SimpleBindingMemoryManager(
381 const SimpleBindingMMFunctions& Functions,
382 void *Opaque)
383 : Functions(Functions), Opaque(Opaque) {
384 assert(Functions.AllocateCodeSection &&
385 "No AllocateCodeSection function provided!");
386 assert(Functions.AllocateDataSection &&
387 "No AllocateDataSection function provided!");
388 assert(Functions.FinalizeMemory &&
389 "No FinalizeMemory function provided!");
390 assert(Functions.Destroy &&
391 "No Destroy function provided!");
392}
393
394SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
395 Functions.Destroy(Opaque);
396}
397
398uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000399 uintptr_t Size, unsigned Alignment, unsigned SectionID,
400 StringRef SectionName) {
401 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
402 SectionName.str().c_str());
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000403}
404
405uint8_t *SimpleBindingMemoryManager::allocateDataSection(
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000406 uintptr_t Size, unsigned Alignment, unsigned SectionID,
407 StringRef SectionName, bool isReadOnly) {
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000408 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
Filip Pizlo7aa695e02013-10-02 00:59:25 +0000409 SectionName.str().c_str(),
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000410 isReadOnly);
411}
412
413bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000414 char *errMsgCString = nullptr;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000415 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
416 assert((result || !errMsgCString) &&
417 "Did not expect an error message if FinalizeMemory succeeded");
418 if (errMsgCString) {
419 if (ErrMsg)
420 *ErrMsg = errMsgCString;
421 free(errMsgCString);
422 }
423 return result;
424}
425
426} // anonymous namespace
427
428LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
429 void *Opaque,
Anders Waldenborg9515b312013-09-30 19:11:32 +0000430 LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
431 LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
432 LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
433 LLVMMemoryManagerDestroyCallback Destroy) {
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000434
435 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
436 !Destroy)
Craig Topper2617dcc2014-04-15 06:32:26 +0000437 return nullptr;
Filip Pizlo3fdbaff2013-05-22 02:46:43 +0000438
439 SimpleBindingMMFunctions functions;
440 functions.AllocateCodeSection = AllocateCodeSection;
441 functions.AllocateDataSection = AllocateDataSection;
442 functions.FinalizeMemory = FinalizeMemory;
443 functions.Destroy = Destroy;
444 return wrap(new SimpleBindingMemoryManager(functions, Opaque));
445}
446
447void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {
448 delete unwrap(MM);
449}
450