1 //===-- MCJIT.h - Class definition for the MCJIT ----------------*- 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 #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H 11 #define LLVM_LIB_EXECUTIONENGINE_MCJIT_H 12 13 #include "llvm/ADT/SmallVector.h" 14 #include "llvm/ExecutionEngine/ExecutionEngine.h" 15 #include "llvm/ExecutionEngine/ObjectCache.h" 16 #include "llvm/ExecutionEngine/RuntimeDyld.h" 17 #include "llvm/PassManager.h" 18 19 namespace llvm { 20 21 class ObjectImage; 22 23 // FIXME: This makes all kinds of horrible assumptions for the time being, 24 // like only having one module, not needing to worry about multi-threading, 25 // blah blah. Purely in get-it-up-and-limping mode for now. 26 27 class MCJIT : public ExecutionEngine { 28 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr, 29 bool AllocateGVsWithCode); 30 31 TargetMachine *TM; 32 MCContext *Ctx; 33 RTDyldMemoryManager *MemMgr; 34 RuntimeDyld Dyld; 35 SmallVector<JITEventListener*, 2> EventListeners; 36 37 // FIXME: Add support for multiple modules 38 bool IsLoaded; 39 Module *M; 40 OwningPtr<ObjectImage> LoadedObject; 41 42 // An optional ObjectCache to be notified of compiled objects and used to 43 // perform lookup of pre-compiled code to avoid re-compilation. 44 ObjectCache *ObjCache; 45 46 public: 47 ~MCJIT(); 48 49 /// @name ExecutionEngine interface implementation 50 /// @{ 51 52 /// Sets the object manager that MCJIT should use to avoid compilation. 53 virtual void setObjectCache(ObjectCache *manager); 54 55 /// finalizeObject - ensure the module is fully processed and is usable. 56 /// 57 /// It is the user-level function for completing the process of making the 58 /// object usable for execution. It should be called after sections within an 59 /// object have been relocated using mapSectionAddress. When this method is 60 /// called the MCJIT execution engine will reapply relocations for a loaded 61 /// object. 62 virtual void finalizeObject(); 63 64 virtual void *getPointerToBasicBlock(BasicBlock *BB); 65 66 virtual void *getPointerToFunction(Function *F); 67 68 virtual void *recompileAndRelinkFunction(Function *F); 69 70 virtual void freeMachineCodeForFunction(Function *F); 71 72 virtual GenericValue runFunction(Function *F, 73 const std::vector<GenericValue> &ArgValues); 74 75 /// getPointerToNamedFunction - This method returns the address of the 76 /// specified function by using the dlsym function call. As such it is only 77 /// useful for resolving library symbols, not code generated symbols. 78 /// 79 /// If AbortOnFailure is false and no function with the given name is 80 /// found, this function silently returns a null pointer. Otherwise, 81 /// it prints a message to stderr and aborts. 82 /// 83 virtual void *getPointerToNamedFunction(const std::string &Name, 84 bool AbortOnFailure = true); 85 86 /// mapSectionAddress - map a section to its target address space value. 87 /// Map the address of a JIT section as returned from the memory manager 88 /// to the address in the target process as the running code will see it. 89 /// This is the address which will be used for relocation resolution. mapSectionAddress(const void * LocalAddress,uint64_t TargetAddress)90 virtual void mapSectionAddress(const void *LocalAddress, 91 uint64_t TargetAddress) { 92 Dyld.mapSectionAddress(LocalAddress, TargetAddress); 93 } 94 95 virtual void RegisterJITEventListener(JITEventListener *L); 96 virtual void UnregisterJITEventListener(JITEventListener *L); 97 98 /// @} 99 /// @name (Private) Registration Interfaces 100 /// @{ 101 Register()102 static void Register() { 103 MCJITCtor = createJIT; 104 } 105 106 static ExecutionEngine *createJIT(Module *M, 107 std::string *ErrorStr, 108 RTDyldMemoryManager *MemMgr, 109 bool GVsWithCode, 110 TargetMachine *TM); 111 112 // @} 113 114 protected: 115 /// emitObject -- Generate a JITed object in memory from the specified module 116 /// Currently, MCJIT only supports a single module and the module passed to 117 /// this function call is expected to be the contained module. The module 118 /// is passed as a parameter here to prepare for multiple module support in 119 /// the future. 120 ObjectBufferStream* emitObject(Module *M); 121 122 void loadObject(Module *M); 123 124 void NotifyObjectEmitted(const ObjectImage& Obj); 125 void NotifyFreeingObject(const ObjectImage& Obj); 126 }; 127 128 } // End llvm namespace 129 130 #endif 131