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