1 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C bindings for the ExecutionEngine library.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "jit"
15 #include "llvm-c/ExecutionEngine.h"
16 #include "llvm/ExecutionEngine/ExecutionEngine.h"
17 #include "llvm/ExecutionEngine/GenericValue.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/Support/ErrorHandling.h"
26 // Wrapping the C bindings types.
27 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef)
29 inline DataLayout *unwrap(LLVMTargetDataRef P) {
30 return reinterpret_cast<DataLayout*>(P);
33 inline LLVMTargetDataRef wrap(const DataLayout *P) {
34 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P));
37 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) {
38 return reinterpret_cast<TargetLibraryInfo*>(P);
41 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) {
42 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P);
43 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X);
46 /*===-- Operations on generic values --------------------------------------===*/
48 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty,
51 GenericValue *GenVal = new GenericValue();
52 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned);
56 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) {
57 GenericValue *GenVal = new GenericValue();
58 GenVal->PointerVal = P;
62 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) {
63 GenericValue *GenVal = new GenericValue();
64 switch (unwrap(TyRef)->getTypeID()) {
68 case Type::DoubleTyID:
69 GenVal->DoubleVal = N;
72 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
77 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) {
78 return unwrap(GenValRef)->IntVal.getBitWidth();
81 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef,
83 GenericValue *GenVal = unwrap(GenValRef);
85 return GenVal->IntVal.getSExtValue();
87 return GenVal->IntVal.getZExtValue();
90 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) {
91 return unwrap(GenVal)->PointerVal;
94 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) {
95 switch (unwrap(TyRef)->getTypeID()) {
97 return unwrap(GenVal)->FloatVal;
98 case Type::DoubleTyID:
99 return unwrap(GenVal)->DoubleVal;
101 llvm_unreachable("LLVMGenericValueToFloat supports only float and double.");
105 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) {
106 delete unwrap(GenVal);
109 /*===-- Operations on execution engines -----------------------------------===*/
111 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE,
115 EngineBuilder builder(unwrap(M));
116 builder.setEngineKind(EngineKind::Either)
117 .setErrorStr(&Error);
118 if (ExecutionEngine *EE = builder.create()){
122 *OutError = strdup(Error.c_str());
126 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp,
130 EngineBuilder builder(unwrap(M));
131 builder.setEngineKind(EngineKind::Interpreter)
132 .setErrorStr(&Error);
133 if (ExecutionEngine *Interp = builder.create()) {
134 *OutInterp = wrap(Interp);
137 *OutError = strdup(Error.c_str());
141 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT,
146 EngineBuilder builder(unwrap(M));
147 builder.setEngineKind(EngineKind::JIT)
149 .setOptLevel((CodeGenOpt::Level)OptLevel);
150 if (ExecutionEngine *JIT = builder.create()) {
154 *OutError = strdup(Error.c_str());
158 void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions,
159 size_t SizeOfPassedOptions) {
160 LLVMMCJITCompilerOptions options;
161 memset(&options, 0, sizeof(options)); // Most fields are zero by default.
162 options.CodeModel = LLVMCodeModelJITDefault;
164 memcpy(PassedOptions, &options,
165 std::min(sizeof(options), SizeOfPassedOptions));
168 LLVMBool LLVMCreateMCJITCompilerForModule(
169 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M,
170 LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions,
172 LLVMMCJITCompilerOptions options;
173 // If the user passed a larger sized options struct, then they were compiled
174 // against a newer LLVM. Tell them that something is wrong.
175 if (SizeOfPassedOptions > sizeof(options)) {
177 "Refusing to use options struct that is larger than my own; assuming "
178 "LLVM library mismatch.");
182 // Defend against the user having an old version of the API by ensuring that
183 // any fields they didn't see are cleared. We must defend against fields being
184 // set to the bitwise equivalent of zero, and assume that this means "do the
185 // default" as if that option hadn't been available.
186 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options));
187 memcpy(&options, PassedOptions, SizeOfPassedOptions);
189 TargetOptions targetOptions;
190 targetOptions.NoFramePointerElim = options.NoFramePointerElim;
191 targetOptions.EnableFastISel = options.EnableFastISel;
194 EngineBuilder builder(unwrap(M));
195 builder.setEngineKind(EngineKind::JIT)
198 .setOptLevel((CodeGenOpt::Level)options.OptLevel)
199 .setCodeModel(unwrap(options.CodeModel))
200 .setTargetOptions(targetOptions);
202 builder.setMCJITMemoryManager(unwrap(options.MCJMM));
203 if (ExecutionEngine *JIT = builder.create()) {
207 *OutError = strdup(Error.c_str());
211 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE,
212 LLVMModuleProviderRef MP,
214 /* The module provider is now actually a module. */
215 return LLVMCreateExecutionEngineForModule(OutEE,
216 reinterpret_cast<LLVMModuleRef>(MP),
220 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp,
221 LLVMModuleProviderRef MP,
223 /* The module provider is now actually a module. */
224 return LLVMCreateInterpreterForModule(OutInterp,
225 reinterpret_cast<LLVMModuleRef>(MP),
229 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT,
230 LLVMModuleProviderRef MP,
233 /* The module provider is now actually a module. */
234 return LLVMCreateJITCompilerForModule(OutJIT,
235 reinterpret_cast<LLVMModuleRef>(MP),
240 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) {
244 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) {
245 unwrap(EE)->runStaticConstructorsDestructors(false);
248 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) {
249 unwrap(EE)->runStaticConstructorsDestructors(true);
252 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F,
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);
264 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F,
266 LLVMGenericValueRef *Args) {
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]));
274 GenericValue *Result = new GenericValue();
275 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec);
279 void *LLVMGetPointerToFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
280 return unwrap(EE)->getPointerToFunction(unwrap<Function>(F));
283 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) {
284 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F));
287 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){
288 unwrap(EE)->addModule(unwrap(M));
291 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){
292 /* The module provider is now actually a module. */
293 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP));
296 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M,
297 LLVMModuleRef *OutMod, char **OutError) {
298 Module *Mod = unwrap(M);
299 unwrap(EE)->removeModule(Mod);
304 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE,
305 LLVMModuleProviderRef MP,
306 LLVMModuleRef *OutMod, char **OutError) {
307 /* The module provider is now actually a module. */
308 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod,
312 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name,
313 LLVMValueRef *OutFn) {
314 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) {
321 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE,
323 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn));
326 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) {
327 return wrap(unwrap(EE)->getDataLayout());
330 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global,
332 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr);
335 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) {
336 unwrap(EE)->finalizeObject();
338 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global));
341 /*===-- Operations on memory managers -------------------------------------===*/
345 struct SimpleBindingMMFunctions {
346 uint8_t *(*AllocateCodeSection)(void *Opaque,
347 uintptr_t Size, unsigned Alignment,
349 uint8_t *(*AllocateDataSection)(void *Opaque,
350 uintptr_t Size, unsigned Alignment,
351 unsigned SectionID, LLVMBool IsReadOnly);
352 LLVMBool (*FinalizeMemory)(void *Opaque, char **ErrMsg);
353 void (*Destroy)(void *Opaque);
356 class SimpleBindingMemoryManager : public RTDyldMemoryManager {
358 SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions,
360 virtual ~SimpleBindingMemoryManager();
362 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
365 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
369 virtual bool finalizeMemory(std::string *ErrMsg);
372 SimpleBindingMMFunctions Functions;
376 SimpleBindingMemoryManager::SimpleBindingMemoryManager(
377 const SimpleBindingMMFunctions& Functions,
379 : Functions(Functions), Opaque(Opaque) {
380 assert(Functions.AllocateCodeSection &&
381 "No AllocateCodeSection function provided!");
382 assert(Functions.AllocateDataSection &&
383 "No AllocateDataSection function provided!");
384 assert(Functions.FinalizeMemory &&
385 "No FinalizeMemory function provided!");
386 assert(Functions.Destroy &&
387 "No Destroy function provided!");
390 SimpleBindingMemoryManager::~SimpleBindingMemoryManager() {
391 Functions.Destroy(Opaque);
394 uint8_t *SimpleBindingMemoryManager::allocateCodeSection(
395 uintptr_t Size, unsigned Alignment, unsigned SectionID) {
396 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID);
399 uint8_t *SimpleBindingMemoryManager::allocateDataSection(
400 uintptr_t Size, unsigned Alignment, unsigned SectionID, bool isReadOnly) {
401 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID,
405 bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) {
406 char *errMsgCString = 0;
407 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString);
408 assert((result || !errMsgCString) &&
409 "Did not expect an error message if FinalizeMemory succeeded");
412 *ErrMsg = errMsgCString;
418 } // anonymous namespace
420 LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager(
422 uint8_t *(*AllocateCodeSection)(void *Opaque,
423 uintptr_t Size, unsigned Alignment,
425 uint8_t *(*AllocateDataSection)(void *Opaque,
426 uintptr_t Size, unsigned Alignment,
427 unsigned SectionID, LLVMBool IsReadOnly),
428 LLVMBool (*FinalizeMemory)(void *Opaque, char **ErrMsg),
429 void (*Destroy)(void *Opaque)) {
431 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory ||
435 SimpleBindingMMFunctions functions;
436 functions.AllocateCodeSection = AllocateCodeSection;
437 functions.AllocateDataSection = AllocateDataSection;
438 functions.FinalizeMemory = FinalizeMemory;
439 functions.Destroy = Destroy;
440 return wrap(new SimpleBindingMemoryManager(functions, Opaque));
443 void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) {