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