1193323Sed//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the abstract interface that implements execution support 11193323Sed// for LLVM. 12193323Sed// 13193323Sed//===----------------------------------------------------------------------===// 14193323Sed 15249423Sdim#ifndef LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 16249423Sdim#define LLVM_EXECUTIONENGINE_EXECUTIONENGINE_H 17193323Sed 18251662Sdim#include "llvm-c/ExecutionEngine.h" 19249423Sdim#include "llvm/ADT/DenseMap.h" 20193323Sed#include "llvm/ADT/SmallVector.h" 21203954Srdivacky#include "llvm/ADT/StringRef.h" 22198892Srdivacky#include "llvm/ADT/ValueMap.h" 23249423Sdim#include "llvm/MC/MCCodeGenInfo.h" 24234353Sdim#include "llvm/Support/ErrorHandling.h" 25249423Sdim#include "llvm/Support/Mutex.h" 26198090Srdivacky#include "llvm/Support/ValueHandle.h" 27193323Sed#include "llvm/Target/TargetMachine.h" 28234353Sdim#include "llvm/Target/TargetOptions.h" 29234353Sdim#include <map> 30234353Sdim#include <string> 31249423Sdim#include <vector> 32193323Sed 33193323Sednamespace llvm { 34193323Sed 35193323Sedstruct GenericValue; 36193323Sedclass Constant; 37198090Srdivackyclass ExecutionEngine; 38193323Sedclass Function; 39193323Sedclass GlobalVariable; 40193323Sedclass GlobalValue; 41195098Sedclass JITEventListener; 42195098Sedclass JITMemoryManager; 43195098Sedclass MachineCodeInfo; 44193323Sedclass Module; 45195098Sedclass MutexGuard; 46251662Sdimclass ObjectCache; 47243830Sdimclass DataLayout; 48234353Sdimclass Triple; 49193323Sedclass Type; 50193323Sed 51218893Sdim/// \brief Helper class for helping synchronize access to the global address map 52218893Sdim/// table. 53193323Sedclass ExecutionEngineState { 54198090Srdivackypublic: 55198892Srdivacky struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> { 56198892Srdivacky typedef ExecutionEngineState *ExtraData; 57198892Srdivacky static sys::Mutex *getMutex(ExecutionEngineState *EES); 58198892Srdivacky static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old); 59198892Srdivacky static void onRAUW(ExecutionEngineState *, const GlobalValue *, 60198892Srdivacky const GlobalValue *); 61198892Srdivacky }; 62198090Srdivacky 63198892Srdivacky typedef ValueMap<const GlobalValue *, void *, AddressMapConfig> 64198892Srdivacky GlobalAddressMapTy; 65198090Srdivacky 66193323Sedprivate: 67198090Srdivacky ExecutionEngine &EE; 68198090Srdivacky 69193323Sed /// GlobalAddressMap - A mapping between LLVM global values and their 70193323Sed /// actualized version... 71198892Srdivacky GlobalAddressMapTy GlobalAddressMap; 72193323Sed 73193323Sed /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 74193323Sed /// used to convert raw addresses into the LLVM global value that is emitted 75193323Sed /// at the address. This map is not computed unless getGlobalValueAtAddress 76193323Sed /// is called at some point. 77198090Srdivacky std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap; 78193323Sed 79193323Sedpublic: 80198892Srdivacky ExecutionEngineState(ExecutionEngine &EE); 81198090Srdivacky 82218893Sdim GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) { 83193323Sed return GlobalAddressMap; 84193323Sed } 85193323Sed 86198090Srdivacky std::map<void*, AssertingVH<const GlobalValue> > & 87193323Sed getGlobalAddressReverseMap(const MutexGuard &) { 88193323Sed return GlobalAddressReverseMap; 89193323Sed } 90198090Srdivacky 91218893Sdim /// \brief Erase an entry from the mapping table. 92218893Sdim /// 93243830Sdim /// \returns The address that \p ToUnmap was happed to. 94198090Srdivacky void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap); 95193323Sed}; 96193323Sed 97218893Sdim/// \brief Abstract interface for implementation execution of LLVM modules, 98218893Sdim/// designed to support both interpreter and just-in-time (JIT) compiler 99218893Sdim/// implementations. 100218893Sdimclass ExecutionEngine { 101218893Sdim /// The state object holding the global address mapping, which must be 102218893Sdim /// accessed synchronously. 103218893Sdim // 104218893Sdim // FIXME: There is no particular need the entire map needs to be 105218893Sdim // synchronized. Wouldn't a reader-writer design be better here? 106218893Sdim ExecutionEngineState EEState; 107193323Sed 108218893Sdim /// The target data for the platform for which execution is being performed. 109243830Sdim const DataLayout *TD; 110218893Sdim 111218893Sdim /// Whether lazy JIT compilation is enabled. 112198892Srdivacky bool CompilingLazily; 113218893Sdim 114218893Sdim /// Whether JIT compilation of external global variables is allowed. 115193323Sed bool GVCompilationDisabled; 116218893Sdim 117218893Sdim /// Whether the JIT should perform lookups of external symbols (e.g., 118218893Sdim /// using dlsym). 119193323Sed bool SymbolSearchingDisabled; 120193323Sed 121198090Srdivacky friend class EngineBuilder; // To allow access to JITCtor and InterpCtor. 122198090Srdivacky 123193323Sedprotected: 124218893Sdim /// The list of Modules that we are JIT'ing from. We use a SmallVector to 125218893Sdim /// optimize for the case where there is only one module. 126203954Srdivacky SmallVector<Module*, 1> Modules; 127221345Sdim 128243830Sdim void setDataLayout(const DataLayout *td) { TD = td; } 129221345Sdim 130193323Sed /// getMemoryforGV - Allocate memory for a global variable. 131218893Sdim virtual char *getMemoryForGV(const GlobalVariable *GV); 132193323Sed 133193323Sed // To avoid having libexecutionengine depend on the JIT and interpreter 134218893Sdim // libraries, the execution engine implementations set these functions to ctor 135218893Sdim // pointers at startup time if they are linked in. 136203954Srdivacky static ExecutionEngine *(*JITCtor)( 137203954Srdivacky Module *M, 138203954Srdivacky std::string *ErrorStr, 139203954Srdivacky JITMemoryManager *JMM, 140203954Srdivacky bool GVsWithCode, 141223017Sdim TargetMachine *TM); 142218893Sdim static ExecutionEngine *(*MCJITCtor)( 143218893Sdim Module *M, 144218893Sdim std::string *ErrorStr, 145218893Sdim JITMemoryManager *JMM, 146218893Sdim bool GVsWithCode, 147223017Sdim TargetMachine *TM); 148226633Sdim static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr); 149193323Sed 150193323Sed /// LazyFunctionCreator - If an unknown function is needed, this function 151218893Sdim /// pointer is invoked to create it. If this returns null, the JIT will 152218893Sdim /// abort. 153218893Sdim void *(*LazyFunctionCreator)(const std::string &); 154221345Sdim 155218893Sdim /// ExceptionTableRegister - If Exception Handling is set, the JIT will 156218893Sdim /// register dwarf tables with this function. 157193323Sed typedef void (*EERegisterFn)(void*); 158218893Sdim EERegisterFn ExceptionTableRegister; 159218893Sdim EERegisterFn ExceptionTableDeregister; 160221345Sdim /// This maps functions to their exception tables frames. 161221345Sdim DenseMap<const Function*, void*> AllExceptionTables; 162193323Sed 163221345Sdim 164193323Sedpublic: 165218893Sdim /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and 166193323Sed /// JITEmitter classes. It must be held while changing the internal state of 167193323Sed /// any of those classes. 168218893Sdim sys::Mutex lock; 169193323Sed 170193323Sed //===--------------------------------------------------------------------===// 171193323Sed // ExecutionEngine Startup 172193323Sed //===--------------------------------------------------------------------===// 173193323Sed 174193323Sed virtual ~ExecutionEngine(); 175193323Sed 176193323Sed /// create - This is the factory method for creating an execution engine which 177193323Sed /// is appropriate for the current machine. This takes ownership of the 178203954Srdivacky /// module. 179218893Sdim /// 180218893Sdim /// \param GVsWithCode - Allocating globals with code breaks 181218893Sdim /// freeMachineCodeForFunction and is probably unsafe and bad for performance. 182218893Sdim /// However, we have clients who depend on this behavior, so we must support 183221345Sdim /// it. Eventually, when we're willing to break some backwards compatibility, 184218893Sdim /// this flag should be flipped to false, so that by default 185218893Sdim /// freeMachineCodeForFunction works. 186203954Srdivacky static ExecutionEngine *create(Module *M, 187193323Sed bool ForceInterpreter = false, 188193323Sed std::string *ErrorStr = 0, 189193323Sed CodeGenOpt::Level OptLevel = 190226633Sdim CodeGenOpt::Default, 191198090Srdivacky bool GVsWithCode = true); 192198090Srdivacky 193193323Sed /// createJIT - This is the factory method for creating a JIT for the current 194193323Sed /// machine, it does not fall back to the interpreter. This takes ownership 195203954Srdivacky /// of the Module and JITMemoryManager if successful. 196198090Srdivacky /// 197198090Srdivacky /// Clients should make sure to initialize targets prior to calling this 198198090Srdivacky /// function. 199203954Srdivacky static ExecutionEngine *createJIT(Module *M, 200193323Sed std::string *ErrorStr = 0, 201193323Sed JITMemoryManager *JMM = 0, 202193323Sed CodeGenOpt::Level OptLevel = 203226633Sdim CodeGenOpt::Default, 204199481Srdivacky bool GVsWithCode = true, 205226633Sdim Reloc::Model RM = Reloc::Default, 206210299Sed CodeModel::Model CMM = 207226633Sdim CodeModel::JITDefault); 208193323Sed 209203954Srdivacky /// addModule - Add a Module to the list of modules that we can JIT from. 210203954Srdivacky /// Note that this takes ownership of the Module: when the ExecutionEngine is 211203954Srdivacky /// destroyed, it destroys the Module as well. 212203954Srdivacky virtual void addModule(Module *M) { 213203954Srdivacky Modules.push_back(M); 214193323Sed } 215221345Sdim 216218893Sdim //===--------------------------------------------------------------------===// 217193323Sed 218243830Sdim const DataLayout *getDataLayout() const { return TD; } 219193323Sed 220203954Srdivacky /// removeModule - Remove a Module from the list of modules. Returns true if 221203954Srdivacky /// M is found. 222203954Srdivacky virtual bool removeModule(Module *M); 223193323Sed 224193323Sed /// FindFunctionNamed - Search all of the active modules to find the one that 225193323Sed /// defines FnName. This is very slow operation and shouldn't be used for 226193323Sed /// general code. 227193323Sed Function *FindFunctionNamed(const char *FnName); 228221345Sdim 229193323Sed /// runFunction - Execute the specified function with the specified arguments, 230193323Sed /// and return the result. 231193323Sed virtual GenericValue runFunction(Function *F, 232193323Sed const std::vector<GenericValue> &ArgValues) = 0; 233193323Sed 234234353Sdim /// getPointerToNamedFunction - This method returns the address of the 235234353Sdim /// specified function by using the dlsym function call. As such it is only 236234353Sdim /// useful for resolving library symbols, not code generated symbols. 237234353Sdim /// 238234353Sdim /// If AbortOnFailure is false and no function with the given name is 239234353Sdim /// found, this function silently returns a null pointer. Otherwise, 240234353Sdim /// it prints a message to stderr and aborts. 241234353Sdim /// 242234353Sdim virtual void *getPointerToNamedFunction(const std::string &Name, 243234353Sdim bool AbortOnFailure = true) = 0; 244234353Sdim 245234353Sdim /// mapSectionAddress - map a section to its target address space value. 246234353Sdim /// Map the address of a JIT section as returned from the memory manager 247234353Sdim /// to the address in the target process as the running code will see it. 248234353Sdim /// This is the address which will be used for relocation resolution. 249243830Sdim virtual void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress) { 250234353Sdim llvm_unreachable("Re-mapping of section addresses not supported with this " 251234353Sdim "EE!"); 252234353Sdim } 253234353Sdim 254243830Sdim // finalizeObject - This method should be called after sections within an 255243830Sdim // object have been relocated using mapSectionAddress. When this method is 256243830Sdim // called the MCJIT execution engine will reapply relocations for a loaded 257243830Sdim // object. This method has no effect for the legacy JIT engine or the 258243830Sdim // interpeter. 259243830Sdim virtual void finalizeObject() {} 260243830Sdim 261193323Sed /// runStaticConstructorsDestructors - This method is used to execute all of 262218893Sdim /// the static constructors or destructors for a program. 263218893Sdim /// 264218893Sdim /// \param isDtors - Run the destructors instead of constructors. 265193323Sed void runStaticConstructorsDestructors(bool isDtors); 266218893Sdim 267193323Sed /// runStaticConstructorsDestructors - This method is used to execute all of 268218893Sdim /// the static constructors or destructors for a particular module. 269218893Sdim /// 270218893Sdim /// \param isDtors - Run the destructors instead of constructors. 271193323Sed void runStaticConstructorsDestructors(Module *module, bool isDtors); 272221345Sdim 273221345Sdim 274193323Sed /// runFunctionAsMain - This is a helper function which wraps runFunction to 275193323Sed /// handle the common task of starting up main with the specified argc, argv, 276193323Sed /// and envp parameters. 277193323Sed int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 278193323Sed const char * const * envp); 279193323Sed 280193323Sed 281193323Sed /// addGlobalMapping - Tell the execution engine that the specified global is 282193323Sed /// at the specified location. This is used internally as functions are JIT'd 283193323Sed /// and as global variables are laid out in memory. It can and should also be 284193323Sed /// used by clients of the EE that want to have an LLVM global overlay 285198090Srdivacky /// existing data in memory. Mappings are automatically removed when their 286198090Srdivacky /// GlobalValue is destroyed. 287193323Sed void addGlobalMapping(const GlobalValue *GV, void *Addr); 288221345Sdim 289218893Sdim /// clearAllGlobalMappings - Clear all global mappings and start over again, 290218893Sdim /// for use in dynamic compilation scenarios to move globals. 291193323Sed void clearAllGlobalMappings(); 292221345Sdim 293193323Sed /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 294193323Sed /// particular module, because it has been removed from the JIT. 295193323Sed void clearGlobalMappingsFromModule(Module *M); 296221345Sdim 297193323Sed /// updateGlobalMapping - Replace an existing mapping for GV with a new 298193323Sed /// address. This updates both maps as required. If "Addr" is null, the 299193323Sed /// entry for the global is removed from the mappings. This returns the old 300193323Sed /// value of the pointer, or null if it was not in the map. 301193323Sed void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 302221345Sdim 303193323Sed /// getPointerToGlobalIfAvailable - This returns the address of the specified 304193323Sed /// global value if it is has already been codegen'd, otherwise it returns 305193323Sed /// null. 306193323Sed void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 307193323Sed 308193323Sed /// getPointerToGlobal - This returns the address of the specified global 309218893Sdim /// value. This may involve code generation if it's a function. 310193323Sed void *getPointerToGlobal(const GlobalValue *GV); 311193323Sed 312193323Sed /// getPointerToFunction - The different EE's represent function bodies in 313193323Sed /// different ways. They should each implement this to say what a function 314198090Srdivacky /// pointer should look like. When F is destroyed, the ExecutionEngine will 315198892Srdivacky /// remove its global mapping and free any machine code. Be sure no threads 316198892Srdivacky /// are running inside F when that happens. 317193323Sed virtual void *getPointerToFunction(Function *F) = 0; 318193323Sed 319198892Srdivacky /// getPointerToBasicBlock - The different EE's represent basic blocks in 320198892Srdivacky /// different ways. Return the representation for a blockaddress of the 321198892Srdivacky /// specified block. 322198892Srdivacky virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0; 323221345Sdim 324193323Sed /// getPointerToFunctionOrStub - If the specified function has been 325193323Sed /// code-gen'd, return a pointer to the function. If not, compile it, or use 326198090Srdivacky /// a stub to implement lazy compilation if available. See 327198090Srdivacky /// getPointerToFunction for the requirements on destroying F. 328193323Sed virtual void *getPointerToFunctionOrStub(Function *F) { 329193323Sed // Default implementation, just codegen the function. 330193323Sed return getPointerToFunction(F); 331193323Sed } 332193323Sed 333193323Sed // The JIT overrides a version that actually does this. 334194178Sed virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { } 335193323Sed 336193323Sed /// getGlobalValueAtAddress - Return the LLVM global value object that starts 337193323Sed /// at the specified address. 338193323Sed /// 339193323Sed const GlobalValue *getGlobalValueAtAddress(void *Addr); 340193323Sed 341218893Sdim /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. 342218893Sdim /// Ptr is the address of the memory at which to store Val, cast to 343218893Sdim /// GenericValue *. It is not a pointer to a GenericValue containing the 344218893Sdim /// address at which to store Val. 345193323Sed void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 346226633Sdim Type *Ty); 347218893Sdim 348193323Sed void InitializeMemory(const Constant *Init, void *Addr); 349193323Sed 350218893Sdim /// recompileAndRelinkFunction - This method is used to force a function which 351218893Sdim /// has already been compiled to be compiled again, possibly after it has been 352218893Sdim /// modified. Then the entry to the old copy is overwritten with a branch to 353218893Sdim /// the new copy. If there was no old copy, this acts just like 354218893Sdim /// VM::getPointerToFunction(). 355193323Sed virtual void *recompileAndRelinkFunction(Function *F) = 0; 356193323Sed 357193323Sed /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 358193323Sed /// corresponding to the machine code emitted to execute this function, useful 359193323Sed /// for garbage-collecting generated code. 360193323Sed virtual void freeMachineCodeForFunction(Function *F) = 0; 361193323Sed 362193323Sed /// getOrEmitGlobalVariable - Return the address of the specified global 363193323Sed /// variable, possibly emitting it to memory if needed. This is used by the 364198090Srdivacky /// Emitter. 365193323Sed virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 366239462Sdim return getPointerToGlobal((const GlobalValue *)GV); 367193323Sed } 368195098Sed 369195098Sed /// Registers a listener to be called back on various events within 370195098Sed /// the JIT. See JITEventListener.h for more details. Does not 371195098Sed /// take ownership of the argument. The argument may be NULL, in 372195098Sed /// which case these functions do nothing. 373198090Srdivacky virtual void RegisterJITEventListener(JITEventListener *) {} 374198090Srdivacky virtual void UnregisterJITEventListener(JITEventListener *) {} 375195098Sed 376251662Sdim /// Sets the pre-compiled object cache. The ownership of the ObjectCache is 377251662Sdim /// not changed. Supported by MCJIT but not JIT. 378251662Sdim virtual void setObjectCache(ObjectCache *) { 379251662Sdim llvm_unreachable("No support for an object cache"); 380251662Sdim } 381251662Sdim 382198892Srdivacky /// DisableLazyCompilation - When lazy compilation is off (the default), the 383198892Srdivacky /// JIT will eagerly compile every function reachable from the argument to 384198892Srdivacky /// getPointerToFunction. If lazy compilation is turned on, the JIT will only 385198892Srdivacky /// compile the one function and emit stubs to compile the rest when they're 386198892Srdivacky /// first called. If lazy compilation is turned off again while some lazy 387198892Srdivacky /// stubs are still around, and one of those stubs is called, the program will 388198892Srdivacky /// abort. 389198892Srdivacky /// 390198892Srdivacky /// In order to safely compile lazily in a threaded program, the user must 391198892Srdivacky /// ensure that 1) only one thread at a time can call any particular lazy 392198892Srdivacky /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock 393198892Srdivacky /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a 394198892Srdivacky /// lazy stub. See http://llvm.org/PR5184 for details. 395193323Sed void DisableLazyCompilation(bool Disabled = true) { 396198892Srdivacky CompilingLazily = !Disabled; 397193323Sed } 398198892Srdivacky bool isCompilingLazily() const { 399198892Srdivacky return CompilingLazily; 400198892Srdivacky } 401198892Srdivacky // Deprecated in favor of isCompilingLazily (to reduce double-negatives). 402198892Srdivacky // Remove this in LLVM 2.8. 403193323Sed bool isLazyCompilationDisabled() const { 404198892Srdivacky return !CompilingLazily; 405193323Sed } 406193323Sed 407193323Sed /// DisableGVCompilation - If called, the JIT will abort if it's asked to 408193323Sed /// allocate space and populate a GlobalVariable that is not internal to 409193323Sed /// the module. 410193323Sed void DisableGVCompilation(bool Disabled = true) { 411193323Sed GVCompilationDisabled = Disabled; 412193323Sed } 413193323Sed bool isGVCompilationDisabled() const { 414193323Sed return GVCompilationDisabled; 415193323Sed } 416193323Sed 417193323Sed /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 418193323Sed /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 419193323Sed /// resolve symbols in a custom way. 420193323Sed void DisableSymbolSearching(bool Disabled = true) { 421193323Sed SymbolSearchingDisabled = Disabled; 422193323Sed } 423193323Sed bool isSymbolSearchingDisabled() const { 424193323Sed return SymbolSearchingDisabled; 425193323Sed } 426199481Srdivacky 427193323Sed /// InstallLazyFunctionCreator - If an unknown function is needed, the 428193323Sed /// specified function pointer is invoked to create it. If it returns null, 429193323Sed /// the JIT will abort. 430193323Sed void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 431193323Sed LazyFunctionCreator = P; 432193323Sed } 433221345Sdim 434193323Sed /// InstallExceptionTableRegister - The JIT will use the given function 435193323Sed /// to register the exception tables it generates. 436218893Sdim void InstallExceptionTableRegister(EERegisterFn F) { 437193323Sed ExceptionTableRegister = F; 438193323Sed } 439218893Sdim void InstallExceptionTableDeregister(EERegisterFn F) { 440218893Sdim ExceptionTableDeregister = F; 441218893Sdim } 442221345Sdim 443218893Sdim /// RegisterTable - Registers the given pointer as an exception table. It 444218893Sdim /// uses the ExceptionTableRegister function. 445221345Sdim void RegisterTable(const Function *fn, void* res) { 446218893Sdim if (ExceptionTableRegister) { 447193323Sed ExceptionTableRegister(res); 448221345Sdim AllExceptionTables[fn] = res; 449218893Sdim } 450193323Sed } 451193323Sed 452221345Sdim /// DeregisterTable - Deregisters the exception frame previously registered 453221345Sdim /// for the given function. 454221345Sdim void DeregisterTable(const Function *Fn) { 455221345Sdim if (ExceptionTableDeregister) { 456221345Sdim DenseMap<const Function*, void*>::iterator frame = 457221345Sdim AllExceptionTables.find(Fn); 458221345Sdim if(frame != AllExceptionTables.end()) { 459221345Sdim ExceptionTableDeregister(frame->second); 460221345Sdim AllExceptionTables.erase(frame); 461221345Sdim } 462221345Sdim } 463221345Sdim } 464221345Sdim 465218893Sdim /// DeregisterAllTables - Deregisters all previously registered pointers to an 466218893Sdim /// exception tables. It uses the ExceptionTableoDeregister function. 467218893Sdim void DeregisterAllTables(); 468218893Sdim 469193323Sedprotected: 470203954Srdivacky explicit ExecutionEngine(Module *M); 471193323Sed 472193323Sed void emitGlobals(); 473193323Sed 474193323Sed void EmitGlobalVariable(const GlobalVariable *GV); 475193323Sed 476193323Sed GenericValue getConstantValue(const Constant *C); 477221345Sdim void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 478226633Sdim Type *Ty); 479193323Sed}; 480193323Sed 481198090Srdivackynamespace EngineKind { 482198090Srdivacky // These are actually bitmasks that get or-ed together. 483198090Srdivacky enum Kind { 484198090Srdivacky JIT = 0x1, 485198090Srdivacky Interpreter = 0x2 486198090Srdivacky }; 487198090Srdivacky const static Kind Either = (Kind)(JIT | Interpreter); 488198090Srdivacky} 489198090Srdivacky 490198090Srdivacky/// EngineBuilder - Builder class for ExecutionEngines. Use this by 491198090Srdivacky/// stack-allocating a builder, chaining the various set* methods, and 492198090Srdivacky/// terminating it with a .create() call. 493198090Srdivackyclass EngineBuilder { 494218893Sdimprivate: 495203954Srdivacky Module *M; 496198090Srdivacky EngineKind::Kind WhichEngine; 497198090Srdivacky std::string *ErrorStr; 498198090Srdivacky CodeGenOpt::Level OptLevel; 499198090Srdivacky JITMemoryManager *JMM; 500198090Srdivacky bool AllocateGVsWithCode; 501234353Sdim TargetOptions Options; 502226633Sdim Reloc::Model RelocModel; 503199481Srdivacky CodeModel::Model CMModel; 504203954Srdivacky std::string MArch; 505203954Srdivacky std::string MCPU; 506203954Srdivacky SmallVector<std::string, 4> MAttrs; 507218893Sdim bool UseMCJIT; 508198090Srdivacky 509198090Srdivacky /// InitEngine - Does the common initialization of default options. 510198090Srdivacky void InitEngine() { 511198090Srdivacky WhichEngine = EngineKind::Either; 512198090Srdivacky ErrorStr = NULL; 513198090Srdivacky OptLevel = CodeGenOpt::Default; 514198090Srdivacky JMM = NULL; 515234353Sdim Options = TargetOptions(); 516198090Srdivacky AllocateGVsWithCode = false; 517226633Sdim RelocModel = Reloc::Default; 518226633Sdim CMModel = CodeModel::JITDefault; 519218893Sdim UseMCJIT = false; 520198090Srdivacky } 521198090Srdivacky 522218893Sdimpublic: 523198090Srdivacky /// EngineBuilder - Constructor for EngineBuilder. If create() is called and 524203954Srdivacky /// is successful, the created engine takes ownership of the module. 525203954Srdivacky EngineBuilder(Module *m) : M(m) { 526198090Srdivacky InitEngine(); 527198090Srdivacky } 528198090Srdivacky 529198090Srdivacky /// setEngineKind - Controls whether the user wants the interpreter, the JIT, 530198090Srdivacky /// or whichever engine works. This option defaults to EngineKind::Either. 531198090Srdivacky EngineBuilder &setEngineKind(EngineKind::Kind w) { 532198090Srdivacky WhichEngine = w; 533198090Srdivacky return *this; 534198090Srdivacky } 535198090Srdivacky 536198090Srdivacky /// setJITMemoryManager - Sets the memory manager to use. This allows 537198090Srdivacky /// clients to customize their memory allocation policies. If create() is 538198090Srdivacky /// called and is successful, the created engine takes ownership of the 539198090Srdivacky /// memory manager. This option defaults to NULL. 540198090Srdivacky EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) { 541198090Srdivacky JMM = jmm; 542198090Srdivacky return *this; 543198090Srdivacky } 544198090Srdivacky 545198090Srdivacky /// setErrorStr - Set the error string to write to on error. This option 546198090Srdivacky /// defaults to NULL. 547198090Srdivacky EngineBuilder &setErrorStr(std::string *e) { 548198090Srdivacky ErrorStr = e; 549198090Srdivacky return *this; 550198090Srdivacky } 551198090Srdivacky 552198090Srdivacky /// setOptLevel - Set the optimization level for the JIT. This option 553198090Srdivacky /// defaults to CodeGenOpt::Default. 554198090Srdivacky EngineBuilder &setOptLevel(CodeGenOpt::Level l) { 555198090Srdivacky OptLevel = l; 556198090Srdivacky return *this; 557198090Srdivacky } 558198090Srdivacky 559234353Sdim /// setTargetOptions - Set the target options that the ExecutionEngine 560234353Sdim /// target is using. Defaults to TargetOptions(). 561234353Sdim EngineBuilder &setTargetOptions(const TargetOptions &Opts) { 562234353Sdim Options = Opts; 563234353Sdim return *this; 564234353Sdim } 565234353Sdim 566226633Sdim /// setRelocationModel - Set the relocation model that the ExecutionEngine 567226633Sdim /// target is using. Defaults to target specific default "Reloc::Default". 568226633Sdim EngineBuilder &setRelocationModel(Reloc::Model RM) { 569226633Sdim RelocModel = RM; 570226633Sdim return *this; 571226633Sdim } 572226633Sdim 573199481Srdivacky /// setCodeModel - Set the CodeModel that the ExecutionEngine target 574226633Sdim /// data is using. Defaults to target specific default 575226633Sdim /// "CodeModel::JITDefault". 576199481Srdivacky EngineBuilder &setCodeModel(CodeModel::Model M) { 577199481Srdivacky CMModel = M; 578199481Srdivacky return *this; 579199481Srdivacky } 580199481Srdivacky 581198090Srdivacky /// setAllocateGVsWithCode - Sets whether global values should be allocated 582198090Srdivacky /// into the same buffer as code. For most applications this should be set 583198090Srdivacky /// to false. Allocating globals with code breaks freeMachineCodeForFunction 584198090Srdivacky /// and is probably unsafe and bad for performance. However, we have clients 585198090Srdivacky /// who depend on this behavior, so we must support it. This option defaults 586198090Srdivacky /// to false so that users of the new API can safely use the new memory 587198090Srdivacky /// manager and free machine code. 588198090Srdivacky EngineBuilder &setAllocateGVsWithCode(bool a) { 589198090Srdivacky AllocateGVsWithCode = a; 590198090Srdivacky return *this; 591198090Srdivacky } 592198090Srdivacky 593203954Srdivacky /// setMArch - Override the architecture set by the Module's triple. 594203954Srdivacky EngineBuilder &setMArch(StringRef march) { 595203954Srdivacky MArch.assign(march.begin(), march.end()); 596203954Srdivacky return *this; 597203954Srdivacky } 598203954Srdivacky 599203954Srdivacky /// setMCPU - Target a specific cpu type. 600203954Srdivacky EngineBuilder &setMCPU(StringRef mcpu) { 601203954Srdivacky MCPU.assign(mcpu.begin(), mcpu.end()); 602203954Srdivacky return *this; 603203954Srdivacky } 604203954Srdivacky 605218893Sdim /// setUseMCJIT - Set whether the MC-JIT implementation should be used 606218893Sdim /// (experimental). 607221345Sdim EngineBuilder &setUseMCJIT(bool Value) { 608218893Sdim UseMCJIT = Value; 609221345Sdim return *this; 610218893Sdim } 611218893Sdim 612203954Srdivacky /// setMAttrs - Set cpu-specific attributes. 613203954Srdivacky template<typename StringSequence> 614203954Srdivacky EngineBuilder &setMAttrs(const StringSequence &mattrs) { 615203954Srdivacky MAttrs.clear(); 616203954Srdivacky MAttrs.append(mattrs.begin(), mattrs.end()); 617203954Srdivacky return *this; 618203954Srdivacky } 619203954Srdivacky 620234353Sdim TargetMachine *selectTarget(); 621234353Sdim 622223017Sdim /// selectTarget - Pick a target either via -march or by guessing the native 623223017Sdim /// arch. Add any CPU features specified via -mcpu or -mattr. 624234353Sdim TargetMachine *selectTarget(const Triple &TargetTriple, 625234353Sdim StringRef MArch, 626234353Sdim StringRef MCPU, 627234353Sdim const SmallVectorImpl<std::string>& MAttrs); 628223017Sdim 629234353Sdim ExecutionEngine *create() { 630234353Sdim return create(selectTarget()); 631234353Sdim } 632234353Sdim 633234353Sdim ExecutionEngine *create(TargetMachine *TM); 634198090Srdivacky}; 635198090Srdivacky 636251662Sdim// Create wrappers for C Binding types (see CBindingWrapping.h). 637251662SdimDEFINE_SIMPLE_CONVERSION_FUNCTIONS(ExecutionEngine, LLVMExecutionEngineRef) 638251662Sdim 639193323Sed} // End llvm namespace 640193323Sed 641193323Sed#endif 642