2007-12-24 00:59:28 +08:00
|
|
|
//===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-24 00:59:28 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the C bindings for the ExecutionEngine library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm-c/ExecutionEngine.h"
|
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2013-05-22 10:46:43 +08:00
|
|
|
#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
|
2013-05-02 04:59:00 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-06-02 05:56:56 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstring>
|
2007-12-24 00:59:28 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 11:04:17 +08:00
|
|
|
#define DEBUG_TYPE "jit"
|
|
|
|
|
2013-04-23 06:47:22 +08:00
|
|
|
// Wrapping the C bindings types.
|
2013-05-02 04:59:00 +08:00
|
|
|
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
|
2013-04-23 06:47:22 +08:00
|
|
|
|
|
|
|
|
2015-07-28 02:27:23 +08:00
|
|
|
static LLVMTargetMachineRef wrap(const TargetMachine *P) {
|
2014-01-24 03:23:28 +08:00
|
|
|
return
|
|
|
|
reinterpret_cast<LLVMTargetMachineRef>(const_cast<TargetMachine*>(P));
|
|
|
|
}
|
|
|
|
|
2007-12-24 00:59:28 +08:00
|
|
|
/*===-- Operations on generic values --------------------------------------===*/
|
|
|
|
|
|
|
|
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
|
|
|
|
unsigned long long N,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool IsSigned) {
|
2007-12-24 00:59:28 +08: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-15 00:55:14 +08:00
|
|
|
llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
|
2007-12-24 00:59:28 +08:00
|
|
|
}
|
|
|
|
return wrap(GenVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
|
|
|
|
return unwrap(GenValRef)->IntVal.getBitWidth();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool IsSigned) {
|
2007-12-24 00:59:28 +08: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-15 00:55:14 +08:00
|
|
|
llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
|
2007-12-24 00:59:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
|
|
|
|
delete unwrap(GenVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===-- Operations on execution engines -----------------------------------===*/
|
|
|
|
|
2010-03-03 07:58:54 +08:00
|
|
|
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
char **OutError) {
|
2007-12-24 00:59:28 +08:00
|
|
|
std::string Error;
|
2014-08-19 12:04:25 +08:00
|
|
|
EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
|
2009-07-18 08:42:18 +08:00
|
|
|
builder.setEngineKind(EngineKind::Either)
|
|
|
|
.setErrorStr(&Error);
|
|
|
|
if (ExecutionEngine *EE = builder.create()){
|
2007-12-24 00:59:28 +08:00
|
|
|
*OutEE = wrap(EE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-03 07:58:54 +08:00
|
|
|
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
char **OutError) {
|
2007-12-24 00:59:28 +08:00
|
|
|
std::string Error;
|
2014-08-19 12:04:25 +08:00
|
|
|
EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
|
2009-07-18 08:42:18 +08:00
|
|
|
builder.setEngineKind(EngineKind::Interpreter)
|
|
|
|
.setErrorStr(&Error);
|
|
|
|
if (ExecutionEngine *Interp = builder.create()) {
|
2007-12-24 00:59:28 +08:00
|
|
|
*OutInterp = wrap(Interp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-03 07:58:54 +08:00
|
|
|
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
|
|
|
|
LLVMModuleRef M,
|
|
|
|
unsigned OptLevel,
|
|
|
|
char **OutError) {
|
2007-12-24 00:59:28 +08:00
|
|
|
std::string Error;
|
2014-08-19 12:04:25 +08:00
|
|
|
EngineBuilder builder(std::unique_ptr<Module>(unwrap(M)));
|
2009-07-18 08:42:18 +08:00
|
|
|
builder.setEngineKind(EngineKind::JIT)
|
|
|
|
.setErrorStr(&Error)
|
|
|
|
.setOptLevel((CodeGenOpt::Level)OptLevel);
|
|
|
|
if (ExecutionEngine *JIT = builder.create()) {
|
2007-12-24 00:59:28 +08:00
|
|
|
*OutJIT = wrap(JIT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-05-02 06:58:00 +08:00
|
|
|
void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
|
|
|
|
size_t SizeOfPassedOptions) {
|
|
|
|
LLVMMCJITCompilerOptions options;
|
2013-05-22 10:46:43 +08:00
|
|
|
memset(&options, 0, sizeof(options)); // Most fields are zero by default.
|
2013-05-02 06:58:00 +08: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-30 01:49:40 +08: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-02 06:58:00 +08:00
|
|
|
"Refusing to use options struct that is larger than my own; assuming "
|
|
|
|
"LLVM library mismatch.");
|
2013-04-30 01:49:40 +08: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-02 06:58:00 +08:00
|
|
|
LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
|
2013-04-30 01:49:40 +08:00
|
|
|
memcpy(&options, PassedOptions, SizeOfPassedOptions);
|
|
|
|
|
|
|
|
TargetOptions targetOptions;
|
2013-05-02 06:58:00 +08:00
|
|
|
targetOptions.EnableFastISel = options.EnableFastISel;
|
2015-05-23 09:14:08 +08:00
|
|
|
std::unique_ptr<Module> Mod(unwrap(M));
|
|
|
|
|
|
|
|
if (Mod)
|
|
|
|
// Set function attribute "no-frame-pointer-elim" based on
|
|
|
|
// NoFramePointerElim.
|
2015-05-27 04:17:20 +08:00
|
|
|
for (auto &F : *Mod) {
|
|
|
|
auto Attrs = F.getAttributes();
|
|
|
|
auto Value = options.NoFramePointerElim ? "true" : "false";
|
|
|
|
Attrs = Attrs.addAttribute(F.getContext(), AttributeSet::FunctionIndex,
|
|
|
|
"no-frame-pointer-elim", Value);
|
|
|
|
F.setAttributes(Attrs);
|
|
|
|
}
|
2013-04-30 01:49:40 +08:00
|
|
|
|
|
|
|
std::string Error;
|
2015-05-23 09:14:08 +08:00
|
|
|
EngineBuilder builder(std::move(Mod));
|
2013-04-30 01:49:40 +08:00
|
|
|
builder.setEngineKind(EngineKind::JIT)
|
|
|
|
.setErrorStr(&Error)
|
|
|
|
.setOptLevel((CodeGenOpt::Level)options.OptLevel)
|
2013-05-02 06:58:00 +08:00
|
|
|
.setCodeModel(unwrap(options.CodeModel))
|
2013-04-30 01:49:40 +08:00
|
|
|
.setTargetOptions(targetOptions);
|
2013-05-22 10:46:43 +08:00
|
|
|
if (options.MCJMM)
|
2014-12-03 08:51:19 +08:00
|
|
|
builder.setMCJITMemoryManager(
|
|
|
|
std::unique_ptr<RTDyldMemoryManager>(unwrap(options.MCJMM)));
|
2013-04-30 01:49:40 +08:00
|
|
|
if (ExecutionEngine *JIT = builder.create()) {
|
|
|
|
*OutJIT = wrap(JIT);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*OutError = strdup(Error.c_str());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-03-03 07:58:54 +08: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-24 00:59:28 +08: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-30 01:49:40 +08:00
|
|
|
unwrap(EE)->finalizeObject();
|
2015-05-30 03:43:39 +08:00
|
|
|
|
|
|
|
std::vector<std::string> ArgVec(ArgV, ArgV + ArgC);
|
2007-12-24 00:59:28 +08:00
|
|
|
return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
|
|
|
|
unsigned NumArgs,
|
|
|
|
LLVMGenericValueRef *Args) {
|
2013-04-30 01:49:40 +08:00
|
|
|
unwrap(EE)->finalizeObject();
|
|
|
|
|
2007-12-24 00:59:28 +08: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-03 07:58:54 +08:00
|
|
|
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
|
2014-08-19 12:04:25 +08:00
|
|
|
unwrap(EE)->addModule(std::unique_ptr<Module>(unwrap(M)));
|
2010-03-03 07:58:54 +08:00
|
|
|
}
|
|
|
|
|
2007-12-24 00:59:28 +08:00
|
|
|
void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
|
2010-03-03 07:58:54 +08: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-24 00:59:28 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
|
|
|
|
LLVMModuleProviderRef MP,
|
|
|
|
LLVMModuleRef *OutMod, char **OutError) {
|
2010-03-03 07:58:54 +08:00
|
|
|
/* The module provider is now actually a module. */
|
|
|
|
return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
|
|
|
|
OutError);
|
2007-12-24 00:59:28 +08:00
|
|
|
}
|
|
|
|
|
2010-01-10 06:27:07 +08:00
|
|
|
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
|
|
|
|
LLVMValueRef *OutFn) {
|
2007-12-24 00:59:28 +08:00
|
|
|
if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
|
|
|
|
*OutFn = wrap(F);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2008-03-27 08:27:14 +08:00
|
|
|
|
2013-05-02 06:58:00 +08:00
|
|
|
void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
|
|
|
|
LLVMValueRef Fn) {
|
2014-09-03 06:28:02 +08:00
|
|
|
return nullptr;
|
2010-07-19 17:33:13 +08:00
|
|
|
}
|
|
|
|
|
2008-03-27 08:27:14 +08:00
|
|
|
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
|
2015-07-17 00:34:23 +08:00
|
|
|
return wrap(&unwrap(EE)->getDataLayout());
|
2008-03-27 08:27:14 +08:00
|
|
|
}
|
2008-06-20 10:16:11 +08:00
|
|
|
|
2014-01-24 03:23:28 +08:00
|
|
|
LLVMTargetMachineRef
|
|
|
|
LLVMGetExecutionEngineTargetMachine(LLVMExecutionEngineRef EE) {
|
|
|
|
return wrap(unwrap(EE)->getTargetMachine());
|
|
|
|
}
|
|
|
|
|
2008-06-20 10:16:11 +08:00
|
|
|
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
|
|
|
|
void* Addr) {
|
|
|
|
unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
|
|
|
|
}
|
2009-01-22 02:11:10 +08:00
|
|
|
|
|
|
|
void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
|
2013-04-30 01:49:40 +08:00
|
|
|
unwrap(EE)->finalizeObject();
|
|
|
|
|
2009-01-22 02:11:10 +08:00
|
|
|
return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
|
|
|
|
}
|
2013-05-22 10:46:43 +08:00
|
|
|
|
2014-12-23 02:53:11 +08: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 10:46:43 +08:00
|
|
|
/*===-- Operations on memory managers -------------------------------------===*/
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct SimpleBindingMMFunctions {
|
2013-10-01 03:11:32 +08:00
|
|
|
LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection;
|
|
|
|
LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection;
|
|
|
|
LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory;
|
|
|
|
LLVMMemoryManagerDestroyCallback Destroy;
|
2013-05-22 10:46:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class SimpleBindingMemoryManager : public RTDyldMemoryManager {
|
|
|
|
public:
|
|
|
|
SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
|
|
|
|
void *Opaque);
|
2015-04-11 10:11:45 +08:00
|
|
|
~SimpleBindingMemoryManager() override;
|
2013-05-22 10:46:43 +08:00
|
|
|
|
2014-03-08 15:51:20 +08: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 10:46:43 +08: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 08:59:25 +08:00
|
|
|
uintptr_t Size, unsigned Alignment, unsigned SectionID,
|
|
|
|
StringRef SectionName) {
|
|
|
|
return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
|
|
|
|
SectionName.str().c_str());
|
2013-05-22 10:46:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *SimpleBindingMemoryManager::allocateDataSection(
|
2013-10-02 08:59:25 +08:00
|
|
|
uintptr_t Size, unsigned Alignment, unsigned SectionID,
|
|
|
|
StringRef SectionName, bool isReadOnly) {
|
2013-05-22 10:46:43 +08:00
|
|
|
return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
|
2013-10-02 08:59:25 +08:00
|
|
|
SectionName.str().c_str(),
|
2013-05-22 10:46:43 +08:00
|
|
|
isReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
2014-04-15 14:32:26 +08:00
|
|
|
char *errMsgCString = nullptr;
|
2013-05-22 10:46:43 +08: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-10-01 03:11:32 +08:00
|
|
|
LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection,
|
|
|
|
LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection,
|
|
|
|
LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory,
|
|
|
|
LLVMMemoryManagerDestroyCallback Destroy) {
|
2013-05-22 10:46:43 +08:00
|
|
|
|
|
|
|
if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
|
|
|
|
!Destroy)
|
2014-04-15 14:32:26 +08:00
|
|
|
return nullptr;
|
2013-05-22 10:46:43 +08: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);
|
|
|
|
}
|
|
|
|
|