2007-12-23 16:59:28 +00:00
|
|
|
//===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-23 16:59:28 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C bindings for the ExecutionEngine library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2013-05-22 02:46:43 +00:00
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
2013-05-01 20:59:00 +00:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2008-02-20 11:08:44 +00:00
|
|
|
#include <cstring>
|
2007-12-23 16:59:28 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 03:04:17 +00:00
|
|
|
#define DEBUG_TYPE "jit"
|
|
|
|
|
2013-04-22 22:47:22 +00:00
|
|
|
// Wrapping the C bindings types.
|
2013-05-01 20:59:00 +00:00
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
|
2013-04-22 22:47:22 +00:00
|
|
|
|
|
|
|
|
2014-01-23 19:23:28 +00:00
|
|
|
inline LLVMTargetMachineRef wrap(const TargetMachine *P) {
|
|
|
|
return
|
|
|
|
reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
|
|
|
|
}
|
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
/*===-- Operations on generic values --------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
|
|
|
|
unsigned long long N,
|
2010-01-09 22:27:07 +00:00
|
|
|
LLVMBool IsSigned) {
|
2007-12-23 16:59:28 +00:00
|
|
|
GenericValue *GenVal = new GenericValue();
|
|
|
|
GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
|
|
|
|
return wrap(GenVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
|
|
|
|
GenericValue *GenVal = new GenericValue();
|
|
|
|
GenVal->PointerVal = P;
|
|
|
|
return wrap(GenVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
|
|
|
|
GenericValue *GenVal = new GenericValue();
|
|
|
|
switch (unwrap(TyRef)->getTypeID()) {
|
|
|
|
case Type::FloatTyID:
|
|
|
|
GenVal->FloatVal = N;
|
|
|
|
break;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
GenVal->DoubleVal = N;
|
|
|
|
break;
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
|
2007-12-23 16:59:28 +00:00
|
|
|
}
|
|
|
|
return wrap(GenVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
|
|
|
|
return unwrap(GenValRef)->IntVal.getBitWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
|
2010-01-09 22:27:07 +00:00
|
|
|
LLVMBool IsSigned) {
|
2007-12-23 16:59:28 +00:00
|
|
|
GenericValue *GenVal = unwrap(GenValRef);
|
|
|
|
if (IsSigned)
|
|
|
|
return GenVal->IntVal.getSExtValue();
|
|
|
|
else
|
|
|
|
return GenVal->IntVal.getZExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
|
|
|
|
return unwrap(GenVal)->PointerVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
|
|
|
|
switch (unwrap(TyRef)->getTypeID()) {
|
|
|
|
case Type::FloatTyID:
|
|
|
|
return unwrap(GenVal)->FloatVal;
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return unwrap(GenVal)->DoubleVal;
|
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
|
2007-12-23 16:59:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
|
|
|
|
delete unwrap(GenVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===-- Operations on execution engines -----------------------------------===*/
|
|
|
|
|
2010-03-02 23:58:54 +00:00
|
|
|
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
char **OutError) {
|
2007-12-23 16:59:28 +00:00
|
|
|
std::string Error;
|
2014-08-19 04:04:25 +00:00
|
|
|
EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
|
2009-07-18 00:42:18 +00:00
|
|
|
builder.setEngineKind(EngineKind::Either)
|
|
|
|
.setErrorStr(&Error);
|
|
|
|
if (ExecutionEngine *EE = builder.create()){
|
2007-12-23 16:59:28 +00:00
|
|
|
*OutEE = wrap(EE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 23:58:54 +00:00
|
|
|
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
char **OutError) {
|
2007-12-23 16:59:28 +00:00
|
|
|
std::string Error;
|
2014-08-19 04:04:25 +00:00
|
|
|
EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
|
2009-07-18 00:42:18 +00:00
|
|
|
builder.setEngineKind(EngineKind::Interpreter)
|
|
|
|
.setErrorStr(&Error);
|
|
|
|
if (ExecutionEngine *Interp = builder.create()) {
|
2007-12-23 16:59:28 +00:00
|
|
|
*OutInterp = wrap(Interp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 23:58:54 +00:00
|
|
|
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
unsigned OptLevel,
|
|
|
|
char **OutError) {
|
2007-12-23 16:59:28 +00:00
|
|
|
std::string Error;
|
2014-08-19 04:04:25 +00:00
|
|
|
EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
|
2009-07-18 00:42:18 +00:00
|
|
|
builder.setEngineKind(EngineKind::JIT)
|
|
|
|
.setErrorStr(&Error)
|
|
|
|
.setOptLevel((CodeGenOpt::Level)OptLevel);
|
|
|
|
if (ExecutionEngine *JIT = builder.create()) {
|
2007-12-23 16:59:28 +00:00
|
|
|
*OutJIT = wrap(JIT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-01 22:58:00 +00:00
|
|
|
void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
|
|
|
|
size_t SizeOfPassedOptions) {
|
|
|
|
LLVMMCJITCompilerOptions options;
|
2013-05-22 02:46:43 +00:00
|
|
|
memset(&options, 0, sizeof(options)); // Most fields are zero by default.
|
2013-05-01 22:58:00 +00:00
|
|
|
options.CodeModel = LLVMCodeModelJITDefault;
|
|
|
|
|
|
|
|
memcpy(PassedOptions, &options,
|
|
|
|
std::min(sizeof(options), SizeOfPassedOptions));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMCreateMCJITCompilerForModule(
|
|
|
|
LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
|
|
|
|
LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
|
|
|
|
char **OutError) {
|
2013-04-29 17:49:40 +00:00
|
|
|
LLVMMCJITCompilerOptions options;
|
|
|
|
// If the user passed a larger sized options struct, then they were compiled
|
|
|
|
// against a newer LLVM. Tell them that something is wrong.
|
|
|
|
if (SizeOfPassedOptions > sizeof(options)) {
|
|
|
|
*OutError = strdup(
|
2013-05-01 22:58:00 +00:00
|
|
|
"Refusing to use options struct that is larger than my own; assuming "
|
|
|
|
"LLVM library mismatch.");
|
2013-04-29 17:49:40 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defend against the user having an old version of the API by ensuring that
|
|
|
|
// any fields they didn't see are cleared. We must defend against fields being
|
|
|
|
// set to the bitwise equivalent of zero, and assume that this means "do the
|
|
|
|
// default" as if that option hadn't been available.
|
2013-05-01 22:58:00 +00:00
|
|
|
LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
|
2013-04-29 17:49:40 +00:00
|
|
|
memcpy(&options, PassedOptions, SizeOfPassedOptions);
|
|
|
|
|
|
|
|
TargetOptions targetOptions;
|
|
|
|
targetOptions.NoFramePointerElim = options.NoFramePointerElim;
|
2013-05-01 22:58:00 +00:00
|
|
|
targetOptions.EnableFastISel = options.EnableFastISel;
|
2015-05-23 01:14:08 +00:00
|
|
|
std::unique_ptr<Module> Mod(unwrap(M));
|
|
|
|
|
|
|
|
if (Mod)
|
|
|
|
// Set function attribute "no-frame-pointer-elim" based on
|
|
|
|
// NoFramePointerElim.
|
|
|
|
setFunctionAttributes(/* CPU */ "", /* Features */ "", targetOptions, *Mod,
|
|
|
|
/* AlwaysRecordAttrs */ true);
|
2013-04-29 17:49:40 +00:00
|
|
|
|
|
|
|
std::string Error;
|
2015-05-23 01:14:08 +00:00
|
|
|
EngineBuilder builder(std::move(Mod));
|
2013-04-29 17:49:40 +00:00
|
|
|
builder.setEngineKind(EngineKind::JIT)
|
|
|
|
.setErrorStr(&Error)
|
|
|
|
.setOptLevel((CodeGenOpt::Level)options.OptLevel)
|
2013-05-01 22:58:00 +00:00
|
|
|
.setCodeModel(unwrap(options.CodeModel))
|
2013-04-29 17:49:40 +00:00
|
|
|
.setTargetOptions(targetOptions);
|
2013-05-22 02:46:43 +00:00
|
|
|
if (options.MCJMM)
|
2014-12-03 00:51:19 +00:00
|
|
|
builder.setMCJITMemoryManager(
|
|
|
|
std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
|
2013-04-29 17:49:40 +00:00
|
|
|
if (ExecutionEngine *JIT = builder.create()) {
|
|
|
|
*OutJIT = wrap(JIT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-02 23:58:54 +00:00
|
|
|
LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
|
|
|
|
LLVMModuleProviderRef MP,
|
|
|
|
char **OutError) {
|
|
|
|
/* The module provider is now actually a module. */
|
|
|
|
return LLVMCreateExecutionEngineForModule(OutEE,
|
|
|
|
reinterpret_cast<LLVMModuleRef>(MP),
|
|
|
|
OutError);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
|
|
|
|
LLVMModuleProviderRef MP,
|
|
|
|
char **OutError) {
|
|
|
|
/* The module provider is now actually a module. */
|
|
|
|
return LLVMCreateInterpreterForModule(OutInterp,
|
|
|
|
reinterpret_cast<LLVMModuleRef>(MP),
|
|
|
|
OutError);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
|
|
|
|
LLVMModuleProviderRef MP,
|
|
|
|
unsigned OptLevel,
|
|
|
|
char **OutError) {
|
|
|
|
/* The module provider is now actually a module. */
|
|
|
|
return LLVMCreateJITCompilerForModule(OutJIT,
|
|
|
|
reinterpret_cast<LLVMModuleRef>(MP),
|
|
|
|
OptLevel, OutError);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
|
|
|
|
delete unwrap(EE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
|
|
|
|
unwrap(EE)->runStaticConstructorsDestructors(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
|
|
|
|
unwrap(EE)->runStaticConstructorsDestructors(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
|
|
|
|
unsigned ArgC, const char * const *ArgV,
|
|
|
|
const char * const *EnvP) {
|
2013-04-29 17:49:40 +00:00
|
|
|
unwrap(EE)->finalizeObject();
|
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
std::vector<std::string> ArgVec;
|
|
|
|
for (unsigned I = 0; I != ArgC; ++I)
|
|
|
|
ArgVec.push_back(ArgV[I]);
|
|
|
|
|
|
|
|
return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
|
|
|
|
unsigned NumArgs,
|
|
|
|
LLVMGenericValueRef *Args) {
|
2013-04-29 17:49:40 +00:00
|
|
|
unwrap(EE)->finalizeObject();
|
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
std::vector<GenericValue> ArgVec;
|
|
|
|
ArgVec.reserve(NumArgs);
|
|
|
|
for (unsigned I = 0; I != NumArgs; ++I)
|
|
|
|
ArgVec.push_back(*unwrap(Args[I]));
|
|
|
|
|
|
|
|
GenericValue *Result = new GenericValue();
|
|
|
|
*Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
|
|
|
|
return wrap(Result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
|
|
|
|
}
|
|
|
|
|
2010-03-02 23:58:54 +00:00
|
|
|
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
|
2014-08-19 04:04:25 +00:00
|
|
|
unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
|
2010-03-02 23:58:54 +00:00
|
|
|
}
|
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
|
2010-03-02 23:58:54 +00:00
|
|
|
/* The module provider is now actually a module. */
|
|
|
|
LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
|
|
|
|
LLVMModuleRef *OutMod, char **OutError) {
|
|
|
|
Module *Mod = unwrap(M);
|
|
|
|
unwrap(EE)->removeModule(Mod);
|
|
|
|
*OutMod = wrap(Mod);
|
|
|
|
return 0;
|
2007-12-23 16:59:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 22:27:07 +00:00
|
|
|
LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
|
|
|
|
LLVMModuleProviderRef MP,
|
|
|
|
LLVMModuleRef *OutMod, char **OutError) {
|
2010-03-02 23:58:54 +00:00
|
|
|
/* The module provider is now actually a module. */
|
|
|
|
return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
|
|
|
|
OutError);
|
2007-12-23 16:59:28 +00:00
|
|
|
}
|
|
|
|
|
2010-01-09 22:27:07 +00:00
|
|
|
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
|
|
|
|
LLVMValueRef *OutFn) {
|
2007-12-23 16:59:28 +00:00
|
|
|
if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
|
|
|
|
*OutFn = wrap(F);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-27 00:27:14 +00:00
|
|
|
|
2013-05-01 22:58:00 +00:00
|
|
|
void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
|
|
|
|
LLVMValueRef Fn) {
|
2014-09-02 22:28:02 +00:00
|
|
|
return nullptr;
|
2010-07-19 09:33:13 +00:00
|
|
|
}
|
|
|
|
|
2008-03-27 00:27:14 +00:00
|
|
|
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
|
2012-10-08 16:38:25 +00:00
|
|
|
return wrap(unwrap(EE)->getDataLayout());
|
2008-03-27 00:27:14 +00:00
|
|
|
}
|
2008-06-20 02:16:11 +00:00
|
|
|
|
2014-01-23 19:23:28 +00:00
|
|
|
LLVMTargetMachineRef
|
|
|
|
LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
|
|
|
|
return wrap(unwrap(EE)->getTargetMachine());
|
|
|
|
}
|
|
|
|
|
2008-06-20 02:16:11 +00:00
|
|
|
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
|
|
|
|
void* Addr) {
|
|
|
|
unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
|
|
|
|
}
|
2009-01-21 18:11:10 +00:00
|
|
|
|
|
|
|
void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
|
2013-04-29 17:49:40 +00:00
|
|
|
unwrap(EE)->finalizeObject();
|
|
|
|
|
2009-01-21 18:11:10 +00:00
|
|
|
return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
|
|
|
|
}
|
2013-05-22 02:46:43 +00:00
|
|
|
|
2014-12-22 18:53:11 +00:00
|
|
|
uint64_t LLVMGetGlobalValueAddress(LLVMExecutionEngineRef EE, const char *Name) {
|
|
|
|
return unwrap(EE)->getGlobalValueAddress(Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LLVMGetFunctionAddress(LLVMExecutionEngineRef EE, const char *Name) {
|
|
|
|
return unwrap(EE)->getFunctionAddress(Name);
|
|
|
|
}
|
|
|
|
|
2013-05-22 02:46:43 +00:00
|
|
|
/*===-- Operations on memory managers -------------------------------------===*/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct SimpleBindingMMFunctions {
|
2013-09-30 19:11:32 +00:00
|
|
|
LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
|
|
|
|
LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
|
|
|
|
LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
|
|
|
|
LLVMMemoryManagerDestroyCallback Destroy;
|
2013-05-22 02:46:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SimpleBindingMemoryManager : public RTDyldMemoryManager {
|
|
|
|
public:
|
|
|
|
SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
|
|
|
|
void *Opaque);
|
2015-04-11 02:11:45 +00:00
|
|
|
~SimpleBindingMemoryManager() override;
|
2013-05-22 02:46:43 +00:00
|
|
|
|
2014-03-08 07:51:20 +00:00
|
|
|
uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
|
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) override;
|
|
|
|
|
|
|
|
uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
|
|
|
|
unsigned SectionID, StringRef SectionName,
|
|
|
|
bool isReadOnly) override;
|
|
|
|
|
|
|
|
bool finalizeMemory(std::string *ErrMsg) override;
|
2013-05-22 02:46:43 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
SimpleBindingMMFunctions Functions;
|
|
|
|
void *Opaque;
|
|
|
|
};
|
|
|
|
|
|
|
|
SimpleBindingMemoryManager::SimpleBindingMemoryManager(
|
|
|
|
const SimpleBindingMMFunctions& Functions,
|
|
|
|
void *Opaque)
|
|
|
|
: Functions(Functions), Opaque(Opaque) {
|
|
|
|
assert(Functions.AllocateCodeSection &&
|
|
|
|
"No AllocateCodeSection function provided!");
|
|
|
|
assert(Functions.AllocateDataSection &&
|
|
|
|
"No AllocateDataSection function provided!");
|
|
|
|
assert(Functions.FinalizeMemory &&
|
|
|
|
"No FinalizeMemory function provided!");
|
|
|
|
assert(Functions.Destroy &&
|
|
|
|
"No Destroy function provided!");
|
|
|
|
}
|
|
|
|
|
|
|
|
SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
|
|
|
|
Functions.Destroy(Opaque);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
|
2013-10-02 00:59:25 +00:00
|
|
|
uintptr_t Size, unsigned Alignment, unsigned SectionID,
|
|
|
|
StringRef SectionName) {
|
|
|
|
return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
|
|
|
|
SectionName.str().c_str());
|
2013-05-22 02:46:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *SimpleBindingMemoryManager::allocateDataSection(
|
2013-10-02 00:59:25 +00:00
|
|
|
uintptr_t Size, unsigned Alignment, unsigned SectionID,
|
|
|
|
StringRef SectionName, bool isReadOnly) {
|
2013-05-22 02:46:43 +00:00
|
|
|
return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
|
2013-10-02 00:59:25 +00:00
|
|
|
SectionName.str().c_str(),
|
2013-05-22 02:46:43 +00:00
|
|
|
isReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
2014-04-15 06:32:26 +00:00
|
|
|
char *errMsgCString = nullptr;
|
2013-05-22 02:46:43 +00:00
|
|
|
bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
|
|
|
|
assert((result || !errMsgCString) &&
|
|
|
|
"Did not expect an error message if FinalizeMemory succeeded");
|
|
|
|
if (errMsgCString) {
|
|
|
|
if (ErrMsg)
|
|
|
|
*ErrMsg = errMsgCString;
|
|
|
|
free(errMsgCString);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
|
|
|
|
void *Opaque,
|
2013-09-30 19:11:32 +00:00
|
|
|
LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
|
|
|
|
LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
|
|
|
|
LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
|
|
|
|
LLVMMemoryManagerDestroyCallback Destroy) {
|
2013-05-22 02:46:43 +00:00
|
|
|
|
|
|
|
if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
|
|
|
|
!Destroy)
|
2014-04-15 06:32:26 +00:00
|
|
|
return nullptr;
|
2013-05-22 02:46:43 +00:00
|
|
|
|
|
|
|
SimpleBindingMMFunctions functions;
|
|
|
|
functions.AllocateCodeSection = AllocateCodeSection;
|
|
|
|
functions.AllocateDataSection = AllocateDataSection;
|
|
|
|
functions.FinalizeMemory = FinalizeMemory;
|
|
|
|
functions.Destroy = Destroy;
|
|
|
|
return wrap(new SimpleBindingMemoryManager(functions, Opaque));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {
|
|
|
|
delete unwrap(MM);
|
|
|
|
}
|
|
|
|
|