LLVM API Documentation
00001 //===-- ExecutionEngineBindings.cpp - C bindings for EEs ------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the C bindings for the ExecutionEngine library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "jit" 00015 #include "llvm-c/ExecutionEngine.h" 00016 #include "llvm/ExecutionEngine/ExecutionEngine.h" 00017 #include "llvm/ExecutionEngine/GenericValue.h" 00018 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h" 00019 #include "llvm/IR/DerivedTypes.h" 00020 #include "llvm/IR/Module.h" 00021 #include "llvm/Support/ErrorHandling.h" 00022 #include <cstring> 00023 00024 using namespace llvm; 00025 00026 // Wrapping the C bindings types. 00027 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(GenericValue, LLVMGenericValueRef) 00028 00029 inline DataLayout *unwrap(LLVMTargetDataRef P) { 00030 return reinterpret_cast<DataLayout*>(P); 00031 } 00032 00033 inline LLVMTargetDataRef wrap(const DataLayout *P) { 00034 return reinterpret_cast<LLVMTargetDataRef>(const_cast<DataLayout*>(P)); 00035 } 00036 00037 inline TargetLibraryInfo *unwrap(LLVMTargetLibraryInfoRef P) { 00038 return reinterpret_cast<TargetLibraryInfo*>(P); 00039 } 00040 00041 inline LLVMTargetLibraryInfoRef wrap(const TargetLibraryInfo *P) { 00042 TargetLibraryInfo *X = const_cast<TargetLibraryInfo*>(P); 00043 return reinterpret_cast<LLVMTargetLibraryInfoRef>(X); 00044 } 00045 00046 /*===-- Operations on generic values --------------------------------------===*/ 00047 00048 LLVMGenericValueRef LLVMCreateGenericValueOfInt(LLVMTypeRef Ty, 00049 unsigned long long N, 00050 LLVMBool IsSigned) { 00051 GenericValue *GenVal = new GenericValue(); 00052 GenVal->IntVal = APInt(unwrap<IntegerType>(Ty)->getBitWidth(), N, IsSigned); 00053 return wrap(GenVal); 00054 } 00055 00056 LLVMGenericValueRef LLVMCreateGenericValueOfPointer(void *P) { 00057 GenericValue *GenVal = new GenericValue(); 00058 GenVal->PointerVal = P; 00059 return wrap(GenVal); 00060 } 00061 00062 LLVMGenericValueRef LLVMCreateGenericValueOfFloat(LLVMTypeRef TyRef, double N) { 00063 GenericValue *GenVal = new GenericValue(); 00064 switch (unwrap(TyRef)->getTypeID()) { 00065 case Type::FloatTyID: 00066 GenVal->FloatVal = N; 00067 break; 00068 case Type::DoubleTyID: 00069 GenVal->DoubleVal = N; 00070 break; 00071 default: 00072 llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); 00073 } 00074 return wrap(GenVal); 00075 } 00076 00077 unsigned LLVMGenericValueIntWidth(LLVMGenericValueRef GenValRef) { 00078 return unwrap(GenValRef)->IntVal.getBitWidth(); 00079 } 00080 00081 unsigned long long LLVMGenericValueToInt(LLVMGenericValueRef GenValRef, 00082 LLVMBool IsSigned) { 00083 GenericValue *GenVal = unwrap(GenValRef); 00084 if (IsSigned) 00085 return GenVal->IntVal.getSExtValue(); 00086 else 00087 return GenVal->IntVal.getZExtValue(); 00088 } 00089 00090 void *LLVMGenericValueToPointer(LLVMGenericValueRef GenVal) { 00091 return unwrap(GenVal)->PointerVal; 00092 } 00093 00094 double LLVMGenericValueToFloat(LLVMTypeRef TyRef, LLVMGenericValueRef GenVal) { 00095 switch (unwrap(TyRef)->getTypeID()) { 00096 case Type::FloatTyID: 00097 return unwrap(GenVal)->FloatVal; 00098 case Type::DoubleTyID: 00099 return unwrap(GenVal)->DoubleVal; 00100 default: 00101 llvm_unreachable("LLVMGenericValueToFloat supports only float and double."); 00102 } 00103 } 00104 00105 void LLVMDisposeGenericValue(LLVMGenericValueRef GenVal) { 00106 delete unwrap(GenVal); 00107 } 00108 00109 /*===-- Operations on execution engines -----------------------------------===*/ 00110 00111 LLVMBool LLVMCreateExecutionEngineForModule(LLVMExecutionEngineRef *OutEE, 00112 LLVMModuleRef M, 00113 char **OutError) { 00114 std::string Error; 00115 EngineBuilder builder(unwrap(M)); 00116 builder.setEngineKind(EngineKind::Either) 00117 .setErrorStr(&Error); 00118 if (ExecutionEngine *EE = builder.create()){ 00119 *OutEE = wrap(EE); 00120 return 0; 00121 } 00122 *OutError = strdup(Error.c_str()); 00123 return 1; 00124 } 00125 00126 LLVMBool LLVMCreateInterpreterForModule(LLVMExecutionEngineRef *OutInterp, 00127 LLVMModuleRef M, 00128 char **OutError) { 00129 std::string Error; 00130 EngineBuilder builder(unwrap(M)); 00131 builder.setEngineKind(EngineKind::Interpreter) 00132 .setErrorStr(&Error); 00133 if (ExecutionEngine *Interp = builder.create()) { 00134 *OutInterp = wrap(Interp); 00135 return 0; 00136 } 00137 *OutError = strdup(Error.c_str()); 00138 return 1; 00139 } 00140 00141 LLVMBool LLVMCreateJITCompilerForModule(LLVMExecutionEngineRef *OutJIT, 00142 LLVMModuleRef M, 00143 unsigned OptLevel, 00144 char **OutError) { 00145 std::string Error; 00146 EngineBuilder builder(unwrap(M)); 00147 builder.setEngineKind(EngineKind::JIT) 00148 .setErrorStr(&Error) 00149 .setOptLevel((CodeGenOpt::Level)OptLevel); 00150 if (ExecutionEngine *JIT = builder.create()) { 00151 *OutJIT = wrap(JIT); 00152 return 0; 00153 } 00154 *OutError = strdup(Error.c_str()); 00155 return 1; 00156 } 00157 00158 void LLVMInitializeMCJITCompilerOptions(LLVMMCJITCompilerOptions *PassedOptions, 00159 size_t SizeOfPassedOptions) { 00160 LLVMMCJITCompilerOptions options; 00161 memset(&options, 0, sizeof(options)); // Most fields are zero by default. 00162 options.CodeModel = LLVMCodeModelJITDefault; 00163 00164 memcpy(PassedOptions, &options, 00165 std::min(sizeof(options), SizeOfPassedOptions)); 00166 } 00167 00168 LLVMBool LLVMCreateMCJITCompilerForModule( 00169 LLVMExecutionEngineRef *OutJIT, LLVMModuleRef M, 00170 LLVMMCJITCompilerOptions *PassedOptions, size_t SizeOfPassedOptions, 00171 char **OutError) { 00172 LLVMMCJITCompilerOptions options; 00173 // If the user passed a larger sized options struct, then they were compiled 00174 // against a newer LLVM. Tell them that something is wrong. 00175 if (SizeOfPassedOptions > sizeof(options)) { 00176 *OutError = strdup( 00177 "Refusing to use options struct that is larger than my own; assuming " 00178 "LLVM library mismatch."); 00179 return 1; 00180 } 00181 00182 // Defend against the user having an old version of the API by ensuring that 00183 // any fields they didn't see are cleared. We must defend against fields being 00184 // set to the bitwise equivalent of zero, and assume that this means "do the 00185 // default" as if that option hadn't been available. 00186 LLVMInitializeMCJITCompilerOptions(&options, sizeof(options)); 00187 memcpy(&options, PassedOptions, SizeOfPassedOptions); 00188 00189 TargetOptions targetOptions; 00190 targetOptions.NoFramePointerElim = options.NoFramePointerElim; 00191 targetOptions.EnableFastISel = options.EnableFastISel; 00192 00193 std::string Error; 00194 EngineBuilder builder(unwrap(M)); 00195 builder.setEngineKind(EngineKind::JIT) 00196 .setErrorStr(&Error) 00197 .setUseMCJIT(true) 00198 .setOptLevel((CodeGenOpt::Level)options.OptLevel) 00199 .setCodeModel(unwrap(options.CodeModel)) 00200 .setTargetOptions(targetOptions); 00201 if (options.MCJMM) 00202 builder.setMCJITMemoryManager(unwrap(options.MCJMM)); 00203 if (ExecutionEngine *JIT = builder.create()) { 00204 *OutJIT = wrap(JIT); 00205 return 0; 00206 } 00207 *OutError = strdup(Error.c_str()); 00208 return 1; 00209 } 00210 00211 LLVMBool LLVMCreateExecutionEngine(LLVMExecutionEngineRef *OutEE, 00212 LLVMModuleProviderRef MP, 00213 char **OutError) { 00214 /* The module provider is now actually a module. */ 00215 return LLVMCreateExecutionEngineForModule(OutEE, 00216 reinterpret_cast<LLVMModuleRef>(MP), 00217 OutError); 00218 } 00219 00220 LLVMBool LLVMCreateInterpreter(LLVMExecutionEngineRef *OutInterp, 00221 LLVMModuleProviderRef MP, 00222 char **OutError) { 00223 /* The module provider is now actually a module. */ 00224 return LLVMCreateInterpreterForModule(OutInterp, 00225 reinterpret_cast<LLVMModuleRef>(MP), 00226 OutError); 00227 } 00228 00229 LLVMBool LLVMCreateJITCompiler(LLVMExecutionEngineRef *OutJIT, 00230 LLVMModuleProviderRef MP, 00231 unsigned OptLevel, 00232 char **OutError) { 00233 /* The module provider is now actually a module. */ 00234 return LLVMCreateJITCompilerForModule(OutJIT, 00235 reinterpret_cast<LLVMModuleRef>(MP), 00236 OptLevel, OutError); 00237 } 00238 00239 00240 void LLVMDisposeExecutionEngine(LLVMExecutionEngineRef EE) { 00241 delete unwrap(EE); 00242 } 00243 00244 void LLVMRunStaticConstructors(LLVMExecutionEngineRef EE) { 00245 unwrap(EE)->runStaticConstructorsDestructors(false); 00246 } 00247 00248 void LLVMRunStaticDestructors(LLVMExecutionEngineRef EE) { 00249 unwrap(EE)->runStaticConstructorsDestructors(true); 00250 } 00251 00252 int LLVMRunFunctionAsMain(LLVMExecutionEngineRef EE, LLVMValueRef F, 00253 unsigned ArgC, const char * const *ArgV, 00254 const char * const *EnvP) { 00255 unwrap(EE)->finalizeObject(); 00256 00257 std::vector<std::string> ArgVec; 00258 for (unsigned I = 0; I != ArgC; ++I) 00259 ArgVec.push_back(ArgV[I]); 00260 00261 return unwrap(EE)->runFunctionAsMain(unwrap<Function>(F), ArgVec, EnvP); 00262 } 00263 00264 LLVMGenericValueRef LLVMRunFunction(LLVMExecutionEngineRef EE, LLVMValueRef F, 00265 unsigned NumArgs, 00266 LLVMGenericValueRef *Args) { 00267 unwrap(EE)->finalizeObject(); 00268 00269 std::vector<GenericValue> ArgVec; 00270 ArgVec.reserve(NumArgs); 00271 for (unsigned I = 0; I != NumArgs; ++I) 00272 ArgVec.push_back(*unwrap(Args[I])); 00273 00274 GenericValue *Result = new GenericValue(); 00275 *Result = unwrap(EE)->runFunction(unwrap<Function>(F), ArgVec); 00276 return wrap(Result); 00277 } 00278 00279 void LLVMFreeMachineCodeForFunction(LLVMExecutionEngineRef EE, LLVMValueRef F) { 00280 unwrap(EE)->freeMachineCodeForFunction(unwrap<Function>(F)); 00281 } 00282 00283 void LLVMAddModule(LLVMExecutionEngineRef EE, LLVMModuleRef M){ 00284 unwrap(EE)->addModule(unwrap(M)); 00285 } 00286 00287 void LLVMAddModuleProvider(LLVMExecutionEngineRef EE, LLVMModuleProviderRef MP){ 00288 /* The module provider is now actually a module. */ 00289 LLVMAddModule(EE, reinterpret_cast<LLVMModuleRef>(MP)); 00290 } 00291 00292 LLVMBool LLVMRemoveModule(LLVMExecutionEngineRef EE, LLVMModuleRef M, 00293 LLVMModuleRef *OutMod, char **OutError) { 00294 Module *Mod = unwrap(M); 00295 unwrap(EE)->removeModule(Mod); 00296 *OutMod = wrap(Mod); 00297 return 0; 00298 } 00299 00300 LLVMBool LLVMRemoveModuleProvider(LLVMExecutionEngineRef EE, 00301 LLVMModuleProviderRef MP, 00302 LLVMModuleRef *OutMod, char **OutError) { 00303 /* The module provider is now actually a module. */ 00304 return LLVMRemoveModule(EE, reinterpret_cast<LLVMModuleRef>(MP), OutMod, 00305 OutError); 00306 } 00307 00308 LLVMBool LLVMFindFunction(LLVMExecutionEngineRef EE, const char *Name, 00309 LLVMValueRef *OutFn) { 00310 if (Function *F = unwrap(EE)->FindFunctionNamed(Name)) { 00311 *OutFn = wrap(F); 00312 return 0; 00313 } 00314 return 1; 00315 } 00316 00317 void *LLVMRecompileAndRelinkFunction(LLVMExecutionEngineRef EE, 00318 LLVMValueRef Fn) { 00319 return unwrap(EE)->recompileAndRelinkFunction(unwrap<Function>(Fn)); 00320 } 00321 00322 LLVMTargetDataRef LLVMGetExecutionEngineTargetData(LLVMExecutionEngineRef EE) { 00323 return wrap(unwrap(EE)->getDataLayout()); 00324 } 00325 00326 void LLVMAddGlobalMapping(LLVMExecutionEngineRef EE, LLVMValueRef Global, 00327 void* Addr) { 00328 unwrap(EE)->addGlobalMapping(unwrap<GlobalValue>(Global), Addr); 00329 } 00330 00331 void *LLVMGetPointerToGlobal(LLVMExecutionEngineRef EE, LLVMValueRef Global) { 00332 unwrap(EE)->finalizeObject(); 00333 00334 return unwrap(EE)->getPointerToGlobal(unwrap<GlobalValue>(Global)); 00335 } 00336 00337 /*===-- Operations on memory managers -------------------------------------===*/ 00338 00339 namespace { 00340 00341 struct SimpleBindingMMFunctions { 00342 uint8_t *(*AllocateCodeSection)(void *Opaque, 00343 uintptr_t Size, unsigned Alignment, 00344 unsigned SectionID); 00345 uint8_t *(*AllocateDataSection)(void *Opaque, 00346 uintptr_t Size, unsigned Alignment, 00347 unsigned SectionID, LLVMBool IsReadOnly); 00348 LLVMBool (*FinalizeMemory)(void *Opaque, char **ErrMsg); 00349 void (*Destroy)(void *Opaque); 00350 }; 00351 00352 class SimpleBindingMemoryManager : public RTDyldMemoryManager { 00353 public: 00354 SimpleBindingMemoryManager(const SimpleBindingMMFunctions& Functions, 00355 void *Opaque); 00356 virtual ~SimpleBindingMemoryManager(); 00357 00358 virtual uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, 00359 unsigned SectionID); 00360 00361 virtual uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, 00362 unsigned SectionID, 00363 bool isReadOnly); 00364 00365 virtual bool finalizeMemory(std::string *ErrMsg); 00366 00367 private: 00368 SimpleBindingMMFunctions Functions; 00369 void *Opaque; 00370 }; 00371 00372 SimpleBindingMemoryManager::SimpleBindingMemoryManager( 00373 const SimpleBindingMMFunctions& Functions, 00374 void *Opaque) 00375 : Functions(Functions), Opaque(Opaque) { 00376 assert(Functions.AllocateCodeSection && 00377 "No AllocateCodeSection function provided!"); 00378 assert(Functions.AllocateDataSection && 00379 "No AllocateDataSection function provided!"); 00380 assert(Functions.FinalizeMemory && 00381 "No FinalizeMemory function provided!"); 00382 assert(Functions.Destroy && 00383 "No Destroy function provided!"); 00384 } 00385 00386 SimpleBindingMemoryManager::~SimpleBindingMemoryManager() { 00387 Functions.Destroy(Opaque); 00388 } 00389 00390 uint8_t *SimpleBindingMemoryManager::allocateCodeSection( 00391 uintptr_t Size, unsigned Alignment, unsigned SectionID) { 00392 return Functions.AllocateCodeSection(Opaque, Size, Alignment, SectionID); 00393 } 00394 00395 uint8_t *SimpleBindingMemoryManager::allocateDataSection( 00396 uintptr_t Size, unsigned Alignment, unsigned SectionID, bool isReadOnly) { 00397 return Functions.AllocateDataSection(Opaque, Size, Alignment, SectionID, 00398 isReadOnly); 00399 } 00400 00401 bool SimpleBindingMemoryManager::finalizeMemory(std::string *ErrMsg) { 00402 char *errMsgCString = 0; 00403 bool result = Functions.FinalizeMemory(Opaque, &errMsgCString); 00404 assert((result || !errMsgCString) && 00405 "Did not expect an error message if FinalizeMemory succeeded"); 00406 if (errMsgCString) { 00407 if (ErrMsg) 00408 *ErrMsg = errMsgCString; 00409 free(errMsgCString); 00410 } 00411 return result; 00412 } 00413 00414 } // anonymous namespace 00415 00416 LLVMMCJITMemoryManagerRef LLVMCreateSimpleMCJITMemoryManager( 00417 void *Opaque, 00418 uint8_t *(*AllocateCodeSection)(void *Opaque, 00419 uintptr_t Size, unsigned Alignment, 00420 unsigned SectionID), 00421 uint8_t *(*AllocateDataSection)(void *Opaque, 00422 uintptr_t Size, unsigned Alignment, 00423 unsigned SectionID, LLVMBool IsReadOnly), 00424 LLVMBool (*FinalizeMemory)(void *Opaque, char **ErrMsg), 00425 void (*Destroy)(void *Opaque)) { 00426 00427 if (!AllocateCodeSection || !AllocateDataSection || !FinalizeMemory || 00428 !Destroy) 00429 return NULL; 00430 00431 SimpleBindingMMFunctions functions; 00432 functions.AllocateCodeSection = AllocateCodeSection; 00433 functions.AllocateDataSection = AllocateDataSection; 00434 functions.FinalizeMemory = FinalizeMemory; 00435 functions.Destroy = Destroy; 00436 return wrap(new SimpleBindingMemoryManager(functions, Opaque)); 00437 } 00438 00439 void LLVMDisposeMCJITMemoryManager(LLVMMCJITMemoryManagerRef MM) { 00440 delete unwrap(MM); 00441 } 00442