LLVM API Documentation
00001 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===// 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 abstract interface that implements execution support 00011 // for LLVM. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 00016 #define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 00017 00018 #include "llvm-c/ExecutionEngine.h" 00019 #include "llvm/ADT/DenseMap.h" 00020 #include "llvm/ADT/SmallVector.h" 00021 #include "llvm/ADT/StringRef.h" 00022 #include "llvm/ADT/ValueMap.h" 00023 #include "llvm/MC/MCCodeGenInfo.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/Mutex.h" 00026 #include "llvm/Support/ValueHandle.h" 00027 #include "llvm/Target/TargetMachine.h" 00028 #include "llvm/Target/TargetOptions.h" 00029 #include <map> 00030 #include <string> 00031 #include <vector> 00032 00033 namespace llvm { 00034 00035 struct GenericValue; 00036 class Constant; 00037 class DataLayout; 00038 class ExecutionEngine; 00039 class Function; 00040 class GlobalVariable; 00041 class GlobalValue; 00042 class JITEventListener; 00043 class JITMemoryManager; 00044 class MachineCodeInfo; 00045 class Module; 00046 class MutexGuard; 00047 class ObjectCache; 00048 class RTDyldMemoryManager; 00049 class Triple; 00050 class Type; 00051 00052 /// \brief Helper class for helping synchronize access to the global address map 00053 /// table. 00054 class ExecutionEngineState { 00055 public: 00056 struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> { 00057 typedef ExecutionEngineState *ExtraData; 00058 static sys::Mutex *getMutex(ExecutionEngineState *EES); 00059 static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old); 00060 static void onRAUW(ExecutionEngineState *, const GlobalValue *, 00061 const GlobalValue *); 00062 }; 00063 00064 typedef ValueMap<const GlobalValue *, void *, AddressMapConfig> 00065 GlobalAddressMapTy; 00066 00067 private: 00068 ExecutionEngine &EE; 00069 00070 /// GlobalAddressMap - A mapping between LLVM global values and their 00071 /// actualized version... 00072 GlobalAddressMapTy GlobalAddressMap; 00073 00074 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 00075 /// used to convert raw addresses into the LLVM global value that is emitted 00076 /// at the address. This map is not computed unless getGlobalValueAtAddress 00077 /// is called at some point. 00078 std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap; 00079 00080 public: 00081 ExecutionEngineState(ExecutionEngine &EE); 00082 00083 GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) { 00084 return GlobalAddressMap; 00085 } 00086 00087 std::map<void*, AssertingVH<const GlobalValue> > & 00088 getGlobalAddressReverseMap(const MutexGuard &) { 00089 return GlobalAddressReverseMap; 00090 } 00091 00092 /// \brief Erase an entry from the mapping table. 00093 /// 00094 /// \returns The address that \p ToUnmap was happed to. 00095 void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap); 00096 }; 00097 00098 /// \brief Abstract interface for implementation execution of LLVM modules, 00099 /// designed to support both interpreter and just-in-time (JIT) compiler 00100 /// implementations. 00101 class ExecutionEngine { 00102 /// The state object holding the global address mapping, which must be 00103 /// accessed synchronously. 00104 // 00105 // FIXME: There is no particular need the entire map needs to be 00106 // synchronized. Wouldn't a reader-writer design be better here? 00107 ExecutionEngineState EEState; 00108 00109 /// The target data for the platform for which execution is being performed. 00110 const DataLayout *TD; 00111 00112 /// Whether lazy JIT compilation is enabled. 00113 bool CompilingLazily; 00114 00115 /// Whether JIT compilation of external global variables is allowed. 00116 bool GVCompilationDisabled; 00117 00118 /// Whether the JIT should perform lookups of external symbols (e.g., 00119 /// using dlsym). 00120 bool SymbolSearchingDisabled; 00121 00122 friend class EngineBuilder; // To allow access to JITCtor and InterpCtor. 00123 00124 protected: 00125 /// The list of Modules that we are JIT'ing from. We use a SmallVector to 00126 /// optimize for the case where there is only one module. 00127 SmallVector<Module*, 1> Modules; 00128 00129 void setDataLayout(const DataLayout *td) { TD = td; } 00130 00131 /// getMemoryforGV - Allocate memory for a global variable. 00132 virtual char *getMemoryForGV(const GlobalVariable *GV); 00133 00134 // To avoid having libexecutionengine depend on the JIT and interpreter 00135 // libraries, the execution engine implementations set these functions to ctor 00136 // pointers at startup time if they are linked in. 00137 static ExecutionEngine *(*JITCtor)( 00138 Module *M, 00139 std::string *ErrorStr, 00140 JITMemoryManager *JMM, 00141 bool GVsWithCode, 00142 TargetMachine *TM); 00143 static ExecutionEngine *(*MCJITCtor)( 00144 Module *M, 00145 std::string *ErrorStr, 00146 RTDyldMemoryManager *MCJMM, 00147 bool GVsWithCode, 00148 TargetMachine *TM); 00149 static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr); 00150 00151 /// LazyFunctionCreator - If an unknown function is needed, this function 00152 /// pointer is invoked to create it. If this returns null, the JIT will 00153 /// abort. 00154 void *(*LazyFunctionCreator)(const std::string &); 00155 00156 /// ExceptionTableRegister - If Exception Handling is set, the JIT will 00157 /// register dwarf tables with this function. 00158 typedef void (*EERegisterFn)(void*); 00159 EERegisterFn ExceptionTableRegister; 00160 EERegisterFn ExceptionTableDeregister; 00161 /// This maps functions to their exception tables frames. 00162 DenseMap<const Function*, void*> AllExceptionTables; 00163 00164 00165 public: 00166 /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and 00167 /// JITEmitter classes. It must be held while changing the internal state of 00168 /// any of those classes. 00169 sys::Mutex lock; 00170 00171 //===--------------------------------------------------------------------===// 00172 // ExecutionEngine Startup 00173 //===--------------------------------------------------------------------===// 00174 00175 virtual ~ExecutionEngine(); 00176 00177 /// create - This is the factory method for creating an execution engine which 00178 /// is appropriate for the current machine. This takes ownership of the 00179 /// module. 00180 /// 00181 /// \param GVsWithCode - Allocating globals with code breaks 00182 /// freeMachineCodeForFunction and is probably unsafe and bad for performance. 00183 /// However, we have clients who depend on this behavior, so we must support 00184 /// it. Eventually, when we're willing to break some backwards compatibility, 00185 /// this flag should be flipped to false, so that by default 00186 /// freeMachineCodeForFunction works. 00187 static ExecutionEngine *create(Module *M, 00188 bool ForceInterpreter = false, 00189 std::string *ErrorStr = 0, 00190 CodeGenOpt::Level OptLevel = 00191 CodeGenOpt::Default, 00192 bool GVsWithCode = true); 00193 00194 /// createJIT - This is the factory method for creating a JIT for the current 00195 /// machine, it does not fall back to the interpreter. This takes ownership 00196 /// of the Module and JITMemoryManager if successful. 00197 /// 00198 /// Clients should make sure to initialize targets prior to calling this 00199 /// function. 00200 static ExecutionEngine *createJIT(Module *M, 00201 std::string *ErrorStr = 0, 00202 JITMemoryManager *JMM = 0, 00203 CodeGenOpt::Level OptLevel = 00204 CodeGenOpt::Default, 00205 bool GVsWithCode = true, 00206 Reloc::Model RM = Reloc::Default, 00207 CodeModel::Model CMM = 00208 CodeModel::JITDefault); 00209 00210 /// addModule - Add a Module to the list of modules that we can JIT from. 00211 /// Note that this takes ownership of the Module: when the ExecutionEngine is 00212 /// destroyed, it destroys the Module as well. 00213 virtual void addModule(Module *M) { 00214 Modules.push_back(M); 00215 } 00216 00217 //===--------------------------------------------------------------------===// 00218 00219 const DataLayout *getDataLayout() const { return TD; } 00220 00221 /// removeModule - Remove a Module from the list of modules. Returns true if 00222 /// M is found. 00223 virtual bool removeModule(Module *M); 00224 00225 /// FindFunctionNamed - Search all of the active modules to find the one that 00226 /// defines FnName. This is very slow operation and shouldn't be used for 00227 /// general code. 00228 Function *FindFunctionNamed(const char *FnName); 00229 00230 /// runFunction - Execute the specified function with the specified arguments, 00231 /// and return the result. 00232 virtual GenericValue runFunction(Function *F, 00233 const std::vector<GenericValue> &ArgValues) = 0; 00234 00235 /// getPointerToNamedFunction - This method returns the address of the 00236 /// specified function by using the dlsym function call. As such it is only 00237 /// useful for resolving library symbols, not code generated symbols. 00238 /// 00239 /// If AbortOnFailure is false and no function with the given name is 00240 /// found, this function silently returns a null pointer. Otherwise, 00241 /// it prints a message to stderr and aborts. 00242 /// 00243 virtual void *getPointerToNamedFunction(const std::string &Name, 00244 bool AbortOnFailure = true) = 0; 00245 00246 /// mapSectionAddress - map a section to its target address space value. 00247 /// Map the address of a JIT section as returned from the memory manager 00248 /// to the address in the target process as the running code will see it. 00249 /// This is the address which will be used for relocation resolution. 00250 virtual void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) { 00251 llvm_unreachable("Re-mapping of section addresses not supported with this " 00252 "EE!"); 00253 } 00254 00255 /// finalizeObject - ensure the module is fully processed and is usable. 00256 /// 00257 /// It is the user-level function for completing the process of making the 00258 /// object usable for execution. It should be called after sections within an 00259 /// object have been relocated using mapSectionAddress. When this method is 00260 /// called the MCJIT execution engine will reapply relocations for a loaded 00261 /// object. This method has no effect for the legacy JIT engine or the 00262 /// interpeter. 00263 virtual void finalizeObject() {} 00264 00265 /// runStaticConstructorsDestructors - This method is used to execute all of 00266 /// the static constructors or destructors for a program. 00267 /// 00268 /// \param isDtors - Run the destructors instead of constructors. 00269 void runStaticConstructorsDestructors(bool isDtors); 00270 00271 /// runStaticConstructorsDestructors - This method is used to execute all of 00272 /// the static constructors or destructors for a particular module. 00273 /// 00274 /// \param isDtors - Run the destructors instead of constructors. 00275 void runStaticConstructorsDestructors(Module *module, bool isDtors); 00276 00277 00278 /// runFunctionAsMain - This is a helper function which wraps runFunction to 00279 /// handle the common task of starting up main with the specified argc, argv, 00280 /// and envp parameters. 00281 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 00282 const char * const * envp); 00283 00284 00285 /// addGlobalMapping - Tell the execution engine that the specified global is 00286 /// at the specified location. This is used internally as functions are JIT'd 00287 /// and as global variables are laid out in memory. It can and should also be 00288 /// used by clients of the EE that want to have an LLVM global overlay 00289 /// existing data in memory. Mappings are automatically removed when their 00290 /// GlobalValue is destroyed. 00291 void addGlobalMapping(const GlobalValue *GV, void *Addr); 00292 00293 /// clearAllGlobalMappings - Clear all global mappings and start over again, 00294 /// for use in dynamic compilation scenarios to move globals. 00295 void clearAllGlobalMappings(); 00296 00297 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 00298 /// particular module, because it has been removed from the JIT. 00299 void clearGlobalMappingsFromModule(Module *M); 00300 00301 /// updateGlobalMapping - Replace an existing mapping for GV with a new 00302 /// address. This updates both maps as required. If "Addr" is null, the 00303 /// entry for the global is removed from the mappings. This returns the old 00304 /// value of the pointer, or null if it was not in the map. 00305 void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 00306 00307 /// getPointerToGlobalIfAvailable - This returns the address of the specified 00308 /// global value if it is has already been codegen'd, otherwise it returns 00309 /// null. 00310 void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 00311 00312 /// getPointerToGlobal - This returns the address of the specified global 00313 /// value. This may involve code generation if it's a function. 00314 void *getPointerToGlobal(const GlobalValue *GV); 00315 00316 /// getPointerToFunction - The different EE's represent function bodies in 00317 /// different ways. They should each implement this to say what a function 00318 /// pointer should look like. When F is destroyed, the ExecutionEngine will 00319 /// remove its global mapping and free any machine code. Be sure no threads 00320 /// are running inside F when that happens. 00321 virtual void *getPointerToFunction(Function *F) = 0; 00322 00323 /// getPointerToBasicBlock - The different EE's represent basic blocks in 00324 /// different ways. Return the representation for a blockaddress of the 00325 /// specified block. 00326 virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0; 00327 00328 /// getPointerToFunctionOrStub - If the specified function has been 00329 /// code-gen'd, return a pointer to the function. If not, compile it, or use 00330 /// a stub to implement lazy compilation if available. See 00331 /// getPointerToFunction for the requirements on destroying F. 00332 virtual void *getPointerToFunctionOrStub(Function *F) { 00333 // Default implementation, just codegen the function. 00334 return getPointerToFunction(F); 00335 } 00336 00337 // The JIT overrides a version that actually does this. 00338 virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { } 00339 00340 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 00341 /// at the specified address. 00342 /// 00343 const GlobalValue *getGlobalValueAtAddress(void *Addr); 00344 00345 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. 00346 /// Ptr is the address of the memory at which to store Val, cast to 00347 /// GenericValue *. It is not a pointer to a GenericValue containing the 00348 /// address at which to store Val. 00349 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 00350 Type *Ty); 00351 00352 void InitializeMemory(const Constant *Init, void *Addr); 00353 00354 /// recompileAndRelinkFunction - This method is used to force a function which 00355 /// has already been compiled to be compiled again, possibly after it has been 00356 /// modified. Then the entry to the old copy is overwritten with a branch to 00357 /// the new copy. If there was no old copy, this acts just like 00358 /// VM::getPointerToFunction(). 00359 virtual void *recompileAndRelinkFunction(Function *F) = 0; 00360 00361 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 00362 /// corresponding to the machine code emitted to execute this function, useful 00363 /// for garbage-collecting generated code. 00364 virtual void freeMachineCodeForFunction(Function *F) = 0; 00365 00366 /// getOrEmitGlobalVariable - Return the address of the specified global 00367 /// variable, possibly emitting it to memory if needed. This is used by the 00368 /// Emitter. 00369 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 00370 return getPointerToGlobal((const GlobalValue *)GV); 00371 } 00372 00373 /// Registers a listener to be called back on various events within 00374 /// the JIT. See JITEventListener.h for more details. Does not 00375 /// take ownership of the argument. The argument may be NULL, in 00376 /// which case these functions do nothing. 00377 virtual void RegisterJITEventListener(JITEventListener *) {} 00378 virtual void UnregisterJITEventListener(JITEventListener *) {} 00379 00380 /// Sets the pre-compiled object cache. The ownership of the ObjectCache is 00381 /// not changed. Supported by MCJIT but not JIT. 00382 virtual void setObjectCache(ObjectCache *) { 00383 llvm_unreachable("No support for an object cache"); 00384 } 00385 00386 /// DisableLazyCompilation - When lazy compilation is off (the default), the 00387 /// JIT will eagerly compile every function reachable from the argument to 00388 /// getPointerToFunction. If lazy compilation is turned on, the JIT will only 00389 /// compile the one function and emit stubs to compile the rest when they're 00390 /// first called. If lazy compilation is turned off again while some lazy 00391 /// stubs are still around, and one of those stubs is called, the program will 00392 /// abort. 00393 /// 00394 /// In order to safely compile lazily in a threaded program, the user must 00395 /// ensure that 1) only one thread at a time can call any particular lazy 00396 /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock 00397 /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a 00398 /// lazy stub. See http://llvm.org/PR5184 for details. 00399 void DisableLazyCompilation(bool Disabled = true) { 00400 CompilingLazily = !Disabled; 00401 } 00402 bool isCompilingLazily() const { 00403 return CompilingLazily; 00404 } 00405 // Deprecated in favor of isCompilingLazily (to reduce double-negatives). 00406 // Remove this in LLVM 2.8. 00407 bool isLazyCompilationDisabled() const { 00408 return !CompilingLazily; 00409 } 00410 00411 /// DisableGVCompilation - If called, the JIT will abort if it's asked to 00412 /// allocate space and populate a GlobalVariable that is not internal to 00413 /// the module. 00414 void DisableGVCompilation(bool Disabled = true) { 00415 GVCompilationDisabled = Disabled; 00416 } 00417 bool isGVCompilationDisabled() const { 00418 return GVCompilationDisabled; 00419 } 00420 00421 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 00422 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 00423 /// resolve symbols in a custom way. 00424 void DisableSymbolSearching(bool Disabled = true) { 00425 SymbolSearchingDisabled = Disabled; 00426 } 00427 bool isSymbolSearchingDisabled() const { 00428 return SymbolSearchingDisabled; 00429 } 00430 00431 /// InstallLazyFunctionCreator - If an unknown function is needed, the 00432 /// specified function pointer is invoked to create it. If it returns null, 00433 /// the JIT will abort. 00434 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 00435 LazyFunctionCreator = P; 00436 } 00437 00438 /// InstallExceptionTableRegister - The JIT will use the given function 00439 /// to register the exception tables it generates. 00440 void InstallExceptionTableRegister(EERegisterFn F) { 00441 ExceptionTableRegister = F; 00442 } 00443 void InstallExceptionTableDeregister(EERegisterFn F) { 00444 ExceptionTableDeregister = F; 00445 } 00446 00447 /// RegisterTable - Registers the given pointer as an exception table. It 00448 /// uses the ExceptionTableRegister function. 00449 void RegisterTable(const Function *fn, void* res) { 00450 if (ExceptionTableRegister) { 00451 ExceptionTableRegister(res); 00452 AllExceptionTables[fn] = res; 00453 } 00454 } 00455 00456 /// DeregisterTable - Deregisters the exception frame previously registered 00457 /// for the given function. 00458 void DeregisterTable(const Function *Fn) { 00459 if (ExceptionTableDeregister) { 00460 DenseMap<const Function*, void*>::iterator frame = 00461 AllExceptionTables.find(Fn); 00462 if(frame != AllExceptionTables.end()) { 00463 ExceptionTableDeregister(frame->second); 00464 AllExceptionTables.erase(frame); 00465 } 00466 } 00467 } 00468 00469 /// DeregisterAllTables - Deregisters all previously registered pointers to an 00470 /// exception tables. It uses the ExceptionTableoDeregister function. 00471 void DeregisterAllTables(); 00472 00473 protected: 00474 explicit ExecutionEngine(Module *M); 00475 00476 void emitGlobals(); 00477 00478 void EmitGlobalVariable(const GlobalVariable *GV); 00479 00480 GenericValue getConstantValue(const Constant *C); 00481 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 00482 Type *Ty); 00483 }; 00484 00485 namespace EngineKind { 00486 // These are actually bitmasks that get or-ed together. 00487 enum Kind { 00488 JIT = 0x1, 00489 Interpreter = 0x2 00490 }; 00491 const static Kind Either = (Kind)(JIT | Interpreter); 00492 } 00493 00494 /// EngineBuilder - Builder class for ExecutionEngines. Use this by 00495 /// stack-allocating a builder, chaining the various set* methods, and 00496 /// terminating it with a .create() call. 00497 class EngineBuilder { 00498 private: 00499 Module *M; 00500 EngineKind::Kind WhichEngine; 00501 std::string *ErrorStr; 00502 CodeGenOpt::Level OptLevel; 00503 RTDyldMemoryManager *MCJMM; 00504 JITMemoryManager *JMM; 00505 bool AllocateGVsWithCode; 00506 TargetOptions Options; 00507 Reloc::Model RelocModel; 00508 CodeModel::Model CMModel; 00509 std::string MArch; 00510 std::string MCPU; 00511 SmallVector<std::string, 4> MAttrs; 00512 bool UseMCJIT; 00513 00514 /// InitEngine - Does the common initialization of default options. 00515 void InitEngine() { 00516 WhichEngine = EngineKind::Either; 00517 ErrorStr = NULL; 00518 OptLevel = CodeGenOpt::Default; 00519 MCJMM = NULL; 00520 JMM = NULL; 00521 Options = TargetOptions(); 00522 AllocateGVsWithCode = false; 00523 RelocModel = Reloc::Default; 00524 CMModel = CodeModel::JITDefault; 00525 UseMCJIT = false; 00526 } 00527 00528 public: 00529 /// EngineBuilder - Constructor for EngineBuilder. If create() is called and 00530 /// is successful, the created engine takes ownership of the module. 00531 EngineBuilder(Module *m) : M(m) { 00532 InitEngine(); 00533 } 00534 00535 /// setEngineKind - Controls whether the user wants the interpreter, the JIT, 00536 /// or whichever engine works. This option defaults to EngineKind::Either. 00537 EngineBuilder &setEngineKind(EngineKind::Kind w) { 00538 WhichEngine = w; 00539 return *this; 00540 } 00541 00542 /// setMCJITMemoryManager - Sets the MCJIT memory manager to use. This allows 00543 /// clients to customize their memory allocation policies for the MCJIT. This 00544 /// is only appropriate for the MCJIT; setting this and configuring the builder 00545 /// to create anything other than MCJIT will cause a runtime error. If create() 00546 /// is called and is successful, the created engine takes ownership of the 00547 /// memory manager. This option defaults to NULL. Using this option nullifies 00548 /// the setJITMemoryManager() option. 00549 EngineBuilder &setMCJITMemoryManager(RTDyldMemoryManager *mcjmm) { 00550 MCJMM = mcjmm; 00551 JMM = NULL; 00552 return *this; 00553 } 00554 00555 /// setJITMemoryManager - Sets the JIT memory manager to use. This allows 00556 /// clients to customize their memory allocation policies. This is only 00557 /// appropriate for either JIT or MCJIT; setting this and configuring the 00558 /// builder to create an interpreter will cause a runtime error. If create() 00559 /// is called and is successful, the created engine takes ownership of the 00560 /// memory manager. This option defaults to NULL. This option overrides 00561 /// setMCJITMemoryManager() as well. 00562 EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) { 00563 MCJMM = NULL; 00564 JMM = jmm; 00565 return *this; 00566 } 00567 00568 /// setErrorStr - Set the error string to write to on error. This option 00569 /// defaults to NULL. 00570 EngineBuilder &setErrorStr(std::string *e) { 00571 ErrorStr = e; 00572 return *this; 00573 } 00574 00575 /// setOptLevel - Set the optimization level for the JIT. This option 00576 /// defaults to CodeGenOpt::Default. 00577 EngineBuilder &setOptLevel(CodeGenOpt::Level l) { 00578 OptLevel = l; 00579 return *this; 00580 } 00581 00582 /// setTargetOptions - Set the target options that the ExecutionEngine 00583 /// target is using. Defaults to TargetOptions(). 00584 EngineBuilder &setTargetOptions(const TargetOptions &Opts) { 00585 Options = Opts; 00586 return *this; 00587 } 00588 00589 /// setRelocationModel - Set the relocation model that the ExecutionEngine 00590 /// target is using. Defaults to target specific default "Reloc::Default". 00591 EngineBuilder &setRelocationModel(Reloc::Model RM) { 00592 RelocModel = RM; 00593 return *this; 00594 } 00595 00596 /// setCodeModel - Set the CodeModel that the ExecutionEngine target 00597 /// data is using. Defaults to target specific default 00598 /// "CodeModel::JITDefault". 00599 EngineBuilder &setCodeModel(CodeModel::Model M) { 00600 CMModel = M; 00601 return *this; 00602 } 00603 00604 /// setAllocateGVsWithCode - Sets whether global values should be allocated 00605 /// into the same buffer as code. For most applications this should be set 00606 /// to false. Allocating globals with code breaks freeMachineCodeForFunction 00607 /// and is probably unsafe and bad for performance. However, we have clients 00608 /// who depend on this behavior, so we must support it. This option defaults 00609 /// to false so that users of the new API can safely use the new memory 00610 /// manager and free machine code. 00611 EngineBuilder &setAllocateGVsWithCode(bool a) { 00612 AllocateGVsWithCode = a; 00613 return *this; 00614 } 00615 00616 /// setMArch - Override the architecture set by the Module's triple. 00617 EngineBuilder &setMArch(StringRef march) { 00618 MArch.assign(march.begin(), march.end()); 00619 return *this; 00620 } 00621 00622 /// setMCPU - Target a specific cpu type. 00623 EngineBuilder &setMCPU(StringRef mcpu) { 00624 MCPU.assign(mcpu.begin(), mcpu.end()); 00625 return *this; 00626 } 00627 00628 /// setUseMCJIT - Set whether the MC-JIT implementation should be used 00629 /// (experimental). 00630 EngineBuilder &setUseMCJIT(bool Value) { 00631 UseMCJIT = Value; 00632 return *this; 00633 } 00634 00635 /// setMAttrs - Set cpu-specific attributes. 00636 template<typename StringSequence> 00637 EngineBuilder &setMAttrs(const StringSequence &mattrs) { 00638 MAttrs.clear(); 00639 MAttrs.append(mattrs.begin(), mattrs.end()); 00640 return *this; 00641 } 00642 00643 TargetMachine *selectTarget(); 00644 00645 /// selectTarget - Pick a target either via -march or by guessing the native 00646 /// arch. Add any CPU features specified via -mcpu or -mattr. 00647 TargetMachine *selectTarget(const Triple &TargetTriple, 00648 StringRef MArch, 00649 StringRef MCPU, 00650 const SmallVectorImpl<std::string>& MAttrs); 00651 00652 ExecutionEngine *create() { 00653 return create(selectTarget()); 00654 } 00655 00656 ExecutionEngine *create(TargetMachine *TM); 00657 }; 00658 00659 // Create wrappers for C Binding types (see CBindingWrapping.h). 00660 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef) 00661 00662 } // End llvm namespace 00663 00664 #endif