14 #define DEBUG_TYPE "jit"
78 return unwrap(GenValRef)->IntVal.getBitWidth();
91 return unwrap(GenVal)->PointerVal;
97 return unwrap(GenVal)->FloatVal;
99 return unwrap(GenVal)->DoubleVal;
101 llvm_unreachable(
"LLVMGenericValueToFloat supports only float and double.");
122 *OutError =
strdup(Error.c_str());
134 *OutInterp =
wrap(Interp);
137 *OutError =
strdup(Error.c_str());
154 *OutError =
strdup(Error.c_str());
159 size_t SizeOfPassedOptions) {
161 memset(&options, 0,
sizeof(options));
164 memcpy(PassedOptions, &options,
165 std::min(
sizeof(options), SizeOfPassedOptions));
175 if (SizeOfPassedOptions >
sizeof(options)) {
177 "Refusing to use options struct that is larger than my own; assuming "
178 "LLVM library mismatch.");
187 memcpy(&options, PassedOptions, SizeOfPassedOptions);
207 *OutError =
strdup(Error.c_str());
216 reinterpret_cast<LLVMModuleRef>(MP),
225 reinterpret_cast<LLVMModuleRef>(MP),
235 reinterpret_cast<LLVMModuleRef>(MP),
245 unwrap(EE)->runStaticConstructorsDestructors(
false);
249 unwrap(EE)->runStaticConstructorsDestructors(
true);
253 unsigned ArgC,
const char *
const *ArgV,
254 const char *
const *EnvP) {
255 unwrap(EE)->finalizeObject();
257 std::vector<std::string> ArgVec;
258 for (
unsigned I = 0;
I != ArgC; ++
I)
259 ArgVec.push_back(ArgV[
I]);
261 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP);
267 unwrap(EE)->finalizeObject();
269 std::vector<GenericValue> ArgVec;
270 ArgVec.reserve(NumArgs);
271 for (
unsigned I = 0;
I != NumArgs; ++
I)
272 ArgVec.push_back(*
unwrap(Args[
I]));
275 *Result =
unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
280 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
295 unwrap(EE)->removeModule(Mod);
319 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
328 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
332 unwrap(EE)->finalizeObject();
334 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
341 struct SimpleBindingMMFunctions {
350 SimpleBindingMemoryManager(
const SimpleBindingMMFunctions& Functions,
352 virtual ~SimpleBindingMemoryManager();
354 virtual uint8_t *allocateCodeSection(
355 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
358 virtual uint8_t *allocateDataSection(
359 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
362 virtual bool finalizeMemory(std::string *ErrMsg);
365 SimpleBindingMMFunctions Functions;
369 SimpleBindingMemoryManager::SimpleBindingMemoryManager(
370 const SimpleBindingMMFunctions& Functions,
372 : Functions(Functions), Opaque(Opaque) {
373 assert(Functions.AllocateCodeSection &&
374 "No AllocateCodeSection function provided!");
375 assert(Functions.AllocateDataSection &&
376 "No AllocateDataSection function provided!");
377 assert(Functions.FinalizeMemory &&
378 "No FinalizeMemory function provided!");
379 assert(Functions.Destroy &&
380 "No Destroy function provided!");
383 SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
384 Functions.Destroy(Opaque);
387 uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
388 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
390 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID,
391 SectionName.
str().c_str());
394 uint8_t *SimpleBindingMemoryManager::allocateDataSection(
395 uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
396 StringRef SectionName,
bool isReadOnly) {
397 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
398 SectionName.
str().c_str(),
402 bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
403 char *errMsgCString = 0;
404 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
405 assert((result || !errMsgCString) &&
406 "Did not expect an error message if FinalizeMemory succeeded");
409 *ErrMsg = errMsgCString;
424 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
429 functions.AllocateCodeSection = AllocateCodeSection;
430 functions.AllocateDataSection = AllocateDataSection;
431 functions.FinalizeMemory = FinalizeMemory;
432 functions.Destroy = Destroy;
433 return wrap(
new SimpleBindingMemoryManager(functions, Opaque));
EngineBuilder & setMCJITMemoryManager(RTDyldMemoryManager *mcjmm)
LLVMBool(* LLVMMemoryManagerFinalizeMemoryCallback)(void *Opaque, char **ErrMsg)
struct LLVMOpaqueType * LLVMTypeRef
LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N)
uint64_t getZExtValue() const
Get zero extended value.
void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions)
void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP)
LLVMMCJITMemoryManagerRef MCJMM
struct LLVMOpaqueExecutionEngine * LLVMExecutionEngineRef
The main container class for the LLVM Intermediate Representation.
2: 32-bit floating point type
void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE)
void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal)
std::string str() const
str - Get the contents as an std::string.
void * LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, LLVMValueRef Fn)
LLVMContext ** unwrap(LLVMContextRef *Tys)
LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, unsigned OptLevel, char **OutError)
struct LLVMOpaqueTargetData * LLVMTargetDataRef
LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP, LLVMModuleRef *OutMod, char **OutError)
LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, LLVMModuleProviderRef MP, unsigned OptLevel, char **OutError)
double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal)
void * LLVMGenericValueToPointer(LLVMGenericValueRef GenVal)
static unsigned getBitWidth(Type *Ty, const DataLayout *TD)
struct LLVMOpaqueMCJITMemoryManager * LLVMMCJITMemoryManagerRef
#define DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ty, ref)
LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P)
void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, void *Addr)
#define llvm_unreachable(msg)
LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, LLVMModuleProviderRef MP, char **OutError)
struct LLVMOpaqueModuleProvider * LLVMModuleProviderRef
void(* LLVMMemoryManagerDestroyCallback)(void *Opaque)
EngineBuilder & setCodeModel(CodeModel::Model M)
LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(void *Opaque, LLVMMemoryManagerAllocateCodeSectionCallback AllocateCodeSection, LLVMMemoryManagerAllocateDataSectionCallback AllocateDataSection, LLVMMemoryManagerFinalizeMemoryCallback FinalizeMemory, LLVMMemoryManagerDestroyCallback Destroy)
int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned ArgC, const char *const *ArgV, const char *const *EnvP)
EngineBuilder & setEngineKind(EngineKind::Kind w)
LLVMBool NoFramePointerElim
unsigned NoFramePointerElim
LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, LLVMModuleRef M, char **OutError)
DataLayout * unwrap(LLVMTargetDataRef P)
int64_t getSExtValue() const
Get sign extended value.
EngineBuilder & setErrorStr(std::string *e)
LLVMTargetDataRef wrap(const DataLayout *P)
LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, unsigned long long N, LLVMBool IsSigned)
LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE)
void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM)
EngineBuilder & setUseMCJIT(bool Value)
void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE)
Abstract interface for implementation execution of LLVM modules, designed to support both interpreter...
struct LLVMOpaqueValue * LLVMValueRef
char *strdup(const char *s1);
void * LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global)
EngineBuilder & setTargetOptions(const TargetOptions &Opts)
LLVMContextRef * wrap(const LLVMContext **Tys)
uint8_t *(* LLVMMemoryManagerAllocateDataSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName, LLVMBool IsReadOnly)
struct LLVMOpaqueTargetLibraryInfotData * LLVMTargetLibraryInfoRef
LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, LLVMModuleRef M, char **OutError)
Class for arbitrary precision integers.
static char getTypeID(Type *Ty)
ExecutionEngine * create()
void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE)
void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M)
unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, LLVMBool IsSigned)
LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, LLVMModuleProviderRef MP, char **OutError)
LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, unsigned NumArgs, LLVMGenericValueRef *Args)
uint8_t *(* LLVMMemoryManagerAllocateCodeSectionCallback)(void *Opaque, uintptr_t Size, unsigned Alignment, unsigned SectionID, const char *SectionName)
struct LLVMOpaqueModule * LLVMModuleRef
unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef)
3: 64-bit floating point type
LLVMBool LLVMCreateMCJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, char **OutError)
void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F)
struct LLVMOpaqueGenericValue * LLVMGenericValueRef
always Inliner for always_inline functions
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
EngineBuilder & setOptLevel(CodeGenOpt::Level l)
LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, LLVMValueRef *OutFn)
LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, LLVMModuleRef *OutMod, char **OutError)