1 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the abstract interface that implements execution support 11 // for LLVM. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_EXECUTION_ENGINE_H 16 #define LLVM_EXECUTION_ENGINE_H 17 18 #include <vector> 19 #include <map> 20 #include <string> 21 #include "llvm/MC/MCCodeGenInfo.h" 22 #include "llvm/ADT/SmallVector.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/ValueMap.h" 25 #include "llvm/ADT/DenseMap.h" 26 #include "llvm/Support/ValueHandle.h" 27 #include "llvm/Support/Mutex.h" 28 #include "llvm/Target/TargetMachine.h" 29 30 namespace llvm { 31 32 struct GenericValue; 33 class Constant; 34 class ExecutionEngine; 35 class Function; 36 class GlobalVariable; 37 class GlobalValue; 38 class JITEventListener; 39 class JITMemoryManager; 40 class MachineCodeInfo; 41 class Module; 42 class MutexGuard; 43 class TargetData; 44 class Type; 45 46 /// \brief Helper class for helping synchronize access to the global address map 47 /// table. 48 class ExecutionEngineState { 49 public: 50 struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> { 51 typedef ExecutionEngineState *ExtraData; 52 static sys::Mutex *getMutex(ExecutionEngineState *EES); 53 static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old); 54 static void onRAUW(ExecutionEngineState *, const GlobalValue *, 55 const GlobalValue *); 56 }; 57 58 typedef ValueMap<const GlobalValue *, void *, AddressMapConfig> 59 GlobalAddressMapTy; 60 61 private: 62 ExecutionEngine &EE; 63 64 /// GlobalAddressMap - A mapping between LLVM global values and their 65 /// actualized version... 66 GlobalAddressMapTy GlobalAddressMap; 67 68 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 69 /// used to convert raw addresses into the LLVM global value that is emitted 70 /// at the address. This map is not computed unless getGlobalValueAtAddress 71 /// is called at some point. 72 std::map<void *, AssertingVH<const GlobalValue> > GlobalAddressReverseMap; 73 74 public: 75 ExecutionEngineState(ExecutionEngine &EE); 76 getGlobalAddressMap(const MutexGuard &)77 GlobalAddressMapTy &getGlobalAddressMap(const MutexGuard &) { 78 return GlobalAddressMap; 79 } 80 81 std::map<void*, AssertingVH<const GlobalValue> > & getGlobalAddressReverseMap(const MutexGuard &)82 getGlobalAddressReverseMap(const MutexGuard &) { 83 return GlobalAddressReverseMap; 84 } 85 86 /// \brief Erase an entry from the mapping table. 87 /// 88 /// \returns The address that \arg ToUnmap was happed to. 89 void *RemoveMapping(const MutexGuard &, const GlobalValue *ToUnmap); 90 }; 91 92 /// \brief Abstract interface for implementation execution of LLVM modules, 93 /// designed to support both interpreter and just-in-time (JIT) compiler 94 /// implementations. 95 class ExecutionEngine { 96 /// The state object holding the global address mapping, which must be 97 /// accessed synchronously. 98 // 99 // FIXME: There is no particular need the entire map needs to be 100 // synchronized. Wouldn't a reader-writer design be better here? 101 ExecutionEngineState EEState; 102 103 /// The target data for the platform for which execution is being performed. 104 const TargetData *TD; 105 106 /// Whether lazy JIT compilation is enabled. 107 bool CompilingLazily; 108 109 /// Whether JIT compilation of external global variables is allowed. 110 bool GVCompilationDisabled; 111 112 /// Whether the JIT should perform lookups of external symbols (e.g., 113 /// using dlsym). 114 bool SymbolSearchingDisabled; 115 116 friend class EngineBuilder; // To allow access to JITCtor and InterpCtor. 117 118 protected: 119 /// The list of Modules that we are JIT'ing from. We use a SmallVector to 120 /// optimize for the case where there is only one module. 121 SmallVector<Module*, 1> Modules; 122 setTargetData(const TargetData * td)123 void setTargetData(const TargetData *td) { TD = td; } 124 125 /// getMemoryforGV - Allocate memory for a global variable. 126 virtual char *getMemoryForGV(const GlobalVariable *GV); 127 128 // To avoid having libexecutionengine depend on the JIT and interpreter 129 // libraries, the execution engine implementations set these functions to ctor 130 // pointers at startup time if they are linked in. 131 static ExecutionEngine *(*JITCtor)( 132 Module *M, 133 std::string *ErrorStr, 134 JITMemoryManager *JMM, 135 CodeGenOpt::Level OptLevel, 136 bool GVsWithCode, 137 TargetMachine *TM); 138 static ExecutionEngine *(*MCJITCtor)( 139 Module *M, 140 std::string *ErrorStr, 141 JITMemoryManager *JMM, 142 CodeGenOpt::Level OptLevel, 143 bool GVsWithCode, 144 TargetMachine *TM); 145 static ExecutionEngine *(*InterpCtor)(Module *M, std::string *ErrorStr); 146 147 /// LazyFunctionCreator - If an unknown function is needed, this function 148 /// pointer is invoked to create it. If this returns null, the JIT will 149 /// abort. 150 void *(*LazyFunctionCreator)(const std::string &); 151 152 /// ExceptionTableRegister - If Exception Handling is set, the JIT will 153 /// register dwarf tables with this function. 154 typedef void (*EERegisterFn)(void*); 155 EERegisterFn ExceptionTableRegister; 156 EERegisterFn ExceptionTableDeregister; 157 /// This maps functions to their exception tables frames. 158 DenseMap<const Function*, void*> AllExceptionTables; 159 160 161 public: 162 /// lock - This lock protects the ExecutionEngine, JIT, JITResolver and 163 /// JITEmitter classes. It must be held while changing the internal state of 164 /// any of those classes. 165 sys::Mutex lock; 166 167 //===--------------------------------------------------------------------===// 168 // ExecutionEngine Startup 169 //===--------------------------------------------------------------------===// 170 171 virtual ~ExecutionEngine(); 172 173 /// create - This is the factory method for creating an execution engine which 174 /// is appropriate for the current machine. This takes ownership of the 175 /// module. 176 /// 177 /// \param GVsWithCode - Allocating globals with code breaks 178 /// freeMachineCodeForFunction and is probably unsafe and bad for performance. 179 /// However, we have clients who depend on this behavior, so we must support 180 /// it. Eventually, when we're willing to break some backwards compatibility, 181 /// this flag should be flipped to false, so that by default 182 /// freeMachineCodeForFunction works. 183 static ExecutionEngine *create(Module *M, 184 bool ForceInterpreter = false, 185 std::string *ErrorStr = 0, 186 CodeGenOpt::Level OptLevel = 187 CodeGenOpt::Default, 188 bool GVsWithCode = true); 189 190 /// createJIT - This is the factory method for creating a JIT for the current 191 /// machine, it does not fall back to the interpreter. This takes ownership 192 /// of the Module and JITMemoryManager if successful. 193 /// 194 /// Clients should make sure to initialize targets prior to calling this 195 /// function. 196 static ExecutionEngine *createJIT(Module *M, 197 std::string *ErrorStr = 0, 198 JITMemoryManager *JMM = 0, 199 CodeGenOpt::Level OptLevel = 200 CodeGenOpt::Default, 201 bool GVsWithCode = true, 202 Reloc::Model RM = Reloc::Default, 203 CodeModel::Model CMM = 204 CodeModel::JITDefault); 205 206 /// addModule - Add a Module to the list of modules that we can JIT from. 207 /// Note that this takes ownership of the Module: when the ExecutionEngine is 208 /// destroyed, it destroys the Module as well. addModule(Module * M)209 virtual void addModule(Module *M) { 210 Modules.push_back(M); 211 } 212 213 //===--------------------------------------------------------------------===// 214 getTargetData()215 const TargetData *getTargetData() const { return TD; } 216 217 /// removeModule - Remove a Module from the list of modules. Returns true if 218 /// M is found. 219 virtual bool removeModule(Module *M); 220 221 /// FindFunctionNamed - Search all of the active modules to find the one that 222 /// defines FnName. This is very slow operation and shouldn't be used for 223 /// general code. 224 Function *FindFunctionNamed(const char *FnName); 225 226 /// runFunction - Execute the specified function with the specified arguments, 227 /// and return the result. 228 virtual GenericValue runFunction(Function *F, 229 const std::vector<GenericValue> &ArgValues) = 0; 230 231 /// runStaticConstructorsDestructors - This method is used to execute all of 232 /// the static constructors or destructors for a program. 233 /// 234 /// \param isDtors - Run the destructors instead of constructors. 235 void runStaticConstructorsDestructors(bool isDtors); 236 237 /// runStaticConstructorsDestructors - This method is used to execute all of 238 /// the static constructors or destructors for a particular module. 239 /// 240 /// \param isDtors - Run the destructors instead of constructors. 241 void runStaticConstructorsDestructors(Module *module, bool isDtors); 242 243 244 /// runFunctionAsMain - This is a helper function which wraps runFunction to 245 /// handle the common task of starting up main with the specified argc, argv, 246 /// and envp parameters. 247 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 248 const char * const * envp); 249 250 251 /// addGlobalMapping - Tell the execution engine that the specified global is 252 /// at the specified location. This is used internally as functions are JIT'd 253 /// and as global variables are laid out in memory. It can and should also be 254 /// used by clients of the EE that want to have an LLVM global overlay 255 /// existing data in memory. Mappings are automatically removed when their 256 /// GlobalValue is destroyed. 257 void addGlobalMapping(const GlobalValue *GV, void *Addr); 258 259 /// clearAllGlobalMappings - Clear all global mappings and start over again, 260 /// for use in dynamic compilation scenarios to move globals. 261 void clearAllGlobalMappings(); 262 263 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 264 /// particular module, because it has been removed from the JIT. 265 void clearGlobalMappingsFromModule(Module *M); 266 267 /// updateGlobalMapping - Replace an existing mapping for GV with a new 268 /// address. This updates both maps as required. If "Addr" is null, the 269 /// entry for the global is removed from the mappings. This returns the old 270 /// value of the pointer, or null if it was not in the map. 271 void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 272 273 /// getPointerToGlobalIfAvailable - This returns the address of the specified 274 /// global value if it is has already been codegen'd, otherwise it returns 275 /// null. 276 void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 277 278 /// getPointerToGlobal - This returns the address of the specified global 279 /// value. This may involve code generation if it's a function. 280 void *getPointerToGlobal(const GlobalValue *GV); 281 282 /// getPointerToFunction - The different EE's represent function bodies in 283 /// different ways. They should each implement this to say what a function 284 /// pointer should look like. When F is destroyed, the ExecutionEngine will 285 /// remove its global mapping and free any machine code. Be sure no threads 286 /// are running inside F when that happens. 287 virtual void *getPointerToFunction(Function *F) = 0; 288 289 /// getPointerToBasicBlock - The different EE's represent basic blocks in 290 /// different ways. Return the representation for a blockaddress of the 291 /// specified block. 292 virtual void *getPointerToBasicBlock(BasicBlock *BB) = 0; 293 294 /// getPointerToFunctionOrStub - If the specified function has been 295 /// code-gen'd, return a pointer to the function. If not, compile it, or use 296 /// a stub to implement lazy compilation if available. See 297 /// getPointerToFunction for the requirements on destroying F. getPointerToFunctionOrStub(Function * F)298 virtual void *getPointerToFunctionOrStub(Function *F) { 299 // Default implementation, just codegen the function. 300 return getPointerToFunction(F); 301 } 302 303 // The JIT overrides a version that actually does this. 304 virtual void runJITOnFunction(Function *, MachineCodeInfo * = 0) { } 305 306 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 307 /// at the specified address. 308 /// 309 const GlobalValue *getGlobalValueAtAddress(void *Addr); 310 311 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. 312 /// Ptr is the address of the memory at which to store Val, cast to 313 /// GenericValue *. It is not a pointer to a GenericValue containing the 314 /// address at which to store Val. 315 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 316 Type *Ty); 317 318 void InitializeMemory(const Constant *Init, void *Addr); 319 320 /// recompileAndRelinkFunction - This method is used to force a function which 321 /// has already been compiled to be compiled again, possibly after it has been 322 /// modified. Then the entry to the old copy is overwritten with a branch to 323 /// the new copy. If there was no old copy, this acts just like 324 /// VM::getPointerToFunction(). 325 virtual void *recompileAndRelinkFunction(Function *F) = 0; 326 327 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 328 /// corresponding to the machine code emitted to execute this function, useful 329 /// for garbage-collecting generated code. 330 virtual void freeMachineCodeForFunction(Function *F) = 0; 331 332 /// getOrEmitGlobalVariable - Return the address of the specified global 333 /// variable, possibly emitting it to memory if needed. This is used by the 334 /// Emitter. getOrEmitGlobalVariable(const GlobalVariable * GV)335 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 336 return getPointerToGlobal((GlobalValue*)GV); 337 } 338 339 /// Registers a listener to be called back on various events within 340 /// the JIT. See JITEventListener.h for more details. Does not 341 /// take ownership of the argument. The argument may be NULL, in 342 /// which case these functions do nothing. RegisterJITEventListener(JITEventListener *)343 virtual void RegisterJITEventListener(JITEventListener *) {} UnregisterJITEventListener(JITEventListener *)344 virtual void UnregisterJITEventListener(JITEventListener *) {} 345 346 /// DisableLazyCompilation - When lazy compilation is off (the default), the 347 /// JIT will eagerly compile every function reachable from the argument to 348 /// getPointerToFunction. If lazy compilation is turned on, the JIT will only 349 /// compile the one function and emit stubs to compile the rest when they're 350 /// first called. If lazy compilation is turned off again while some lazy 351 /// stubs are still around, and one of those stubs is called, the program will 352 /// abort. 353 /// 354 /// In order to safely compile lazily in a threaded program, the user must 355 /// ensure that 1) only one thread at a time can call any particular lazy 356 /// stub, and 2) any thread modifying LLVM IR must hold the JIT's lock 357 /// (ExecutionEngine::lock) or otherwise ensure that no other thread calls a 358 /// lazy stub. See http://llvm.org/PR5184 for details. 359 void DisableLazyCompilation(bool Disabled = true) { 360 CompilingLazily = !Disabled; 361 } isCompilingLazily()362 bool isCompilingLazily() const { 363 return CompilingLazily; 364 } 365 // Deprecated in favor of isCompilingLazily (to reduce double-negatives). 366 // Remove this in LLVM 2.8. isLazyCompilationDisabled()367 bool isLazyCompilationDisabled() const { 368 return !CompilingLazily; 369 } 370 371 /// DisableGVCompilation - If called, the JIT will abort if it's asked to 372 /// allocate space and populate a GlobalVariable that is not internal to 373 /// the module. 374 void DisableGVCompilation(bool Disabled = true) { 375 GVCompilationDisabled = Disabled; 376 } isGVCompilationDisabled()377 bool isGVCompilationDisabled() const { 378 return GVCompilationDisabled; 379 } 380 381 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 382 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 383 /// resolve symbols in a custom way. 384 void DisableSymbolSearching(bool Disabled = true) { 385 SymbolSearchingDisabled = Disabled; 386 } isSymbolSearchingDisabled()387 bool isSymbolSearchingDisabled() const { 388 return SymbolSearchingDisabled; 389 } 390 391 /// InstallLazyFunctionCreator - If an unknown function is needed, the 392 /// specified function pointer is invoked to create it. If it returns null, 393 /// the JIT will abort. InstallLazyFunctionCreator(void * (* P)(const std::string &))394 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 395 LazyFunctionCreator = P; 396 } 397 398 /// InstallExceptionTableRegister - The JIT will use the given function 399 /// to register the exception tables it generates. InstallExceptionTableRegister(EERegisterFn F)400 void InstallExceptionTableRegister(EERegisterFn F) { 401 ExceptionTableRegister = F; 402 } InstallExceptionTableDeregister(EERegisterFn F)403 void InstallExceptionTableDeregister(EERegisterFn F) { 404 ExceptionTableDeregister = F; 405 } 406 407 /// RegisterTable - Registers the given pointer as an exception table. It 408 /// uses the ExceptionTableRegister function. RegisterTable(const Function * fn,void * res)409 void RegisterTable(const Function *fn, void* res) { 410 if (ExceptionTableRegister) { 411 ExceptionTableRegister(res); 412 AllExceptionTables[fn] = res; 413 } 414 } 415 416 /// DeregisterTable - Deregisters the exception frame previously registered 417 /// for the given function. DeregisterTable(const Function * Fn)418 void DeregisterTable(const Function *Fn) { 419 if (ExceptionTableDeregister) { 420 DenseMap<const Function*, void*>::iterator frame = 421 AllExceptionTables.find(Fn); 422 if(frame != AllExceptionTables.end()) { 423 ExceptionTableDeregister(frame->second); 424 AllExceptionTables.erase(frame); 425 } 426 } 427 } 428 429 /// DeregisterAllTables - Deregisters all previously registered pointers to an 430 /// exception tables. It uses the ExceptionTableoDeregister function. 431 void DeregisterAllTables(); 432 433 protected: 434 explicit ExecutionEngine(Module *M); 435 436 void emitGlobals(); 437 438 void EmitGlobalVariable(const GlobalVariable *GV); 439 440 GenericValue getConstantValue(const Constant *C); 441 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 442 Type *Ty); 443 }; 444 445 namespace EngineKind { 446 // These are actually bitmasks that get or-ed together. 447 enum Kind { 448 JIT = 0x1, 449 Interpreter = 0x2 450 }; 451 const static Kind Either = (Kind)(JIT | Interpreter); 452 } 453 454 /// EngineBuilder - Builder class for ExecutionEngines. Use this by 455 /// stack-allocating a builder, chaining the various set* methods, and 456 /// terminating it with a .create() call. 457 class EngineBuilder { 458 private: 459 Module *M; 460 EngineKind::Kind WhichEngine; 461 std::string *ErrorStr; 462 CodeGenOpt::Level OptLevel; 463 JITMemoryManager *JMM; 464 bool AllocateGVsWithCode; 465 Reloc::Model RelocModel; 466 CodeModel::Model CMModel; 467 std::string MArch; 468 std::string MCPU; 469 SmallVector<std::string, 4> MAttrs; 470 bool UseMCJIT; 471 472 /// InitEngine - Does the common initialization of default options. InitEngine()473 void InitEngine() { 474 WhichEngine = EngineKind::Either; 475 ErrorStr = NULL; 476 OptLevel = CodeGenOpt::Default; 477 JMM = NULL; 478 AllocateGVsWithCode = false; 479 RelocModel = Reloc::Default; 480 CMModel = CodeModel::JITDefault; 481 UseMCJIT = false; 482 } 483 484 public: 485 /// EngineBuilder - Constructor for EngineBuilder. If create() is called and 486 /// is successful, the created engine takes ownership of the module. EngineBuilder(Module * m)487 EngineBuilder(Module *m) : M(m) { 488 InitEngine(); 489 } 490 491 /// setEngineKind - Controls whether the user wants the interpreter, the JIT, 492 /// or whichever engine works. This option defaults to EngineKind::Either. setEngineKind(EngineKind::Kind w)493 EngineBuilder &setEngineKind(EngineKind::Kind w) { 494 WhichEngine = w; 495 return *this; 496 } 497 498 /// setJITMemoryManager - Sets the memory manager to use. This allows 499 /// clients to customize their memory allocation policies. If create() is 500 /// called and is successful, the created engine takes ownership of the 501 /// memory manager. This option defaults to NULL. setJITMemoryManager(JITMemoryManager * jmm)502 EngineBuilder &setJITMemoryManager(JITMemoryManager *jmm) { 503 JMM = jmm; 504 return *this; 505 } 506 507 /// setErrorStr - Set the error string to write to on error. This option 508 /// defaults to NULL. setErrorStr(std::string * e)509 EngineBuilder &setErrorStr(std::string *e) { 510 ErrorStr = e; 511 return *this; 512 } 513 514 /// setOptLevel - Set the optimization level for the JIT. This option 515 /// defaults to CodeGenOpt::Default. setOptLevel(CodeGenOpt::Level l)516 EngineBuilder &setOptLevel(CodeGenOpt::Level l) { 517 OptLevel = l; 518 return *this; 519 } 520 521 /// setRelocationModel - Set the relocation model that the ExecutionEngine 522 /// target is using. Defaults to target specific default "Reloc::Default". setRelocationModel(Reloc::Model RM)523 EngineBuilder &setRelocationModel(Reloc::Model RM) { 524 RelocModel = RM; 525 return *this; 526 } 527 528 /// setCodeModel - Set the CodeModel that the ExecutionEngine target 529 /// data is using. Defaults to target specific default 530 /// "CodeModel::JITDefault". setCodeModel(CodeModel::Model M)531 EngineBuilder &setCodeModel(CodeModel::Model M) { 532 CMModel = M; 533 return *this; 534 } 535 536 /// setAllocateGVsWithCode - Sets whether global values should be allocated 537 /// into the same buffer as code. For most applications this should be set 538 /// to false. Allocating globals with code breaks freeMachineCodeForFunction 539 /// and is probably unsafe and bad for performance. However, we have clients 540 /// who depend on this behavior, so we must support it. This option defaults 541 /// to false so that users of the new API can safely use the new memory 542 /// manager and free machine code. setAllocateGVsWithCode(bool a)543 EngineBuilder &setAllocateGVsWithCode(bool a) { 544 AllocateGVsWithCode = a; 545 return *this; 546 } 547 548 /// setMArch - Override the architecture set by the Module's triple. setMArch(StringRef march)549 EngineBuilder &setMArch(StringRef march) { 550 MArch.assign(march.begin(), march.end()); 551 return *this; 552 } 553 554 /// setMCPU - Target a specific cpu type. setMCPU(StringRef mcpu)555 EngineBuilder &setMCPU(StringRef mcpu) { 556 MCPU.assign(mcpu.begin(), mcpu.end()); 557 return *this; 558 } 559 560 /// setUseMCJIT - Set whether the MC-JIT implementation should be used 561 /// (experimental). setUseMCJIT(bool Value)562 EngineBuilder &setUseMCJIT(bool Value) { 563 UseMCJIT = Value; 564 return *this; 565 } 566 567 /// setMAttrs - Set cpu-specific attributes. 568 template<typename StringSequence> setMAttrs(const StringSequence & mattrs)569 EngineBuilder &setMAttrs(const StringSequence &mattrs) { 570 MAttrs.clear(); 571 MAttrs.append(mattrs.begin(), mattrs.end()); 572 return *this; 573 } 574 575 /// selectTarget - Pick a target either via -march or by guessing the native 576 /// arch. Add any CPU features specified via -mcpu or -mattr. 577 static TargetMachine *selectTarget(Module *M, 578 StringRef MArch, 579 StringRef MCPU, 580 const SmallVectorImpl<std::string>& MAttrs, 581 Reloc::Model RM, 582 CodeModel::Model CM, 583 std::string *Err); 584 585 ExecutionEngine *create(); 586 }; 587 588 } // End llvm namespace 589 590 #endif 591