Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 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 | |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/ObjectCache.h" |
Jim Grosbach | 348a548 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Chandler Carruth | 802d755 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 17 | #include "llvm/PassManager.h" |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 18 | |
| 19 | namespace llvm { |
| 20 | |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 21 | class ObjectImage; |
| 22 | |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 27 | class MCJIT : public ExecutionEngine { |
Jim Grosbach | bea6753 | 2012-08-21 15:42:49 +0000 | [diff] [blame] | 28 | MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr, |
| 29 | bool AllocateGVsWithCode); |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 30 | |
| 31 | TargetMachine *TM; |
| 32 | MCContext *Ctx; |
Jim Grosbach | 2dcef050 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 33 | RTDyldMemoryManager *MemMgr; |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 34 | RuntimeDyld Dyld; |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 35 | SmallVector<JITEventListener*, 2> EventListeners; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 36 | |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 37 | // FIXME: Add support for multiple modules |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 38 | bool IsLoaded; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 39 | Module *M; |
Andrew Kaylor | adc7056 | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 40 | OwningPtr<ObjectImage> LoadedObject; |
Jim Grosbach | 7b16249 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 41 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 46 | public: |
| 47 | ~MCJIT(); |
| 48 | |
| 49 | /// @name ExecutionEngine interface implementation |
| 50 | /// @{ |
| 51 | |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 52 | /// Sets the object manager that MCJIT should use to avoid compilation. |
| 53 | virtual void setObjectCache(ObjectCache *manager); |
| 54 | |
Andrew Kaylor | a714efc | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 55 | virtual void finalizeObject(); |
| 56 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 57 | virtual void *getPointerToBasicBlock(BasicBlock *BB); |
| 58 | |
| 59 | virtual void *getPointerToFunction(Function *F); |
| 60 | |
| 61 | virtual void *recompileAndRelinkFunction(Function *F); |
| 62 | |
| 63 | virtual void freeMachineCodeForFunction(Function *F); |
| 64 | |
| 65 | virtual GenericValue runFunction(Function *F, |
| 66 | const std::vector<GenericValue> &ArgValues); |
| 67 | |
Jim Grosbach | d527440 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 68 | /// getPointerToNamedFunction - This method returns the address of the |
| 69 | /// specified function by using the dlsym function call. As such it is only |
| 70 | /// useful for resolving library symbols, not code generated symbols. |
| 71 | /// |
| 72 | /// If AbortOnFailure is false and no function with the given name is |
| 73 | /// found, this function silently returns a null pointer. Otherwise, |
| 74 | /// it prints a message to stderr and aborts. |
| 75 | /// |
Danil Malyshev | 7e32578 | 2012-01-05 21:16:14 +0000 | [diff] [blame] | 76 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 77 | bool AbortOnFailure = true); |
Danil Malyshev | bfee542 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 78 | |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 79 | /// mapSectionAddress - map a section to its target address space value. |
| 80 | /// Map the address of a JIT section as returned from the memory manager |
| 81 | /// to the address in the target process as the running code will see it. |
| 82 | /// This is the address which will be used for relocation resolution. |
Jim Grosbach | 6d61397 | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 83 | virtual void mapSectionAddress(const void *LocalAddress, |
| 84 | uint64_t TargetAddress) { |
Jim Grosbach | 0ddb3a4 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 85 | Dyld.mapSectionAddress(LocalAddress, TargetAddress); |
| 86 | } |
| 87 | |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 88 | virtual void RegisterJITEventListener(JITEventListener *L); |
| 89 | virtual void UnregisterJITEventListener(JITEventListener *L); |
| 90 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 91 | /// @} |
| 92 | /// @name (Private) Registration Interfaces |
| 93 | /// @{ |
| 94 | |
| 95 | static void Register() { |
| 96 | MCJITCtor = createJIT; |
| 97 | } |
| 98 | |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 99 | static ExecutionEngine *createJIT(Module *M, |
| 100 | std::string *ErrorStr, |
Filip Pizlo | 9bc53e8 | 2013-05-14 19:29:00 +0000 | [diff] [blame] | 101 | RTDyldMemoryManager *MemMgr, |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 102 | bool GVsWithCode, |
Dylan Noblesmith | 8418fdc | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 103 | TargetMachine *TM); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 104 | |
| 105 | // @} |
Andrew Kaylor | 1a568c3 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 106 | |
| 107 | protected: |
| 108 | /// emitObject -- Generate a JITed object in memory from the specified module |
| 109 | /// Currently, MCJIT only supports a single module and the module passed to |
| 110 | /// this function call is expected to be the contained module. The module |
| 111 | /// is passed as a parameter here to prepare for multiple module support in |
| 112 | /// the future. |
Andrew Kaylor | ced4e8f | 2013-04-25 21:02:36 +0000 | [diff] [blame] | 113 | ObjectBufferStream* emitObject(Module *M); |
| 114 | |
| 115 | void loadObject(Module *M); |
Andrew Kaylor | d8ffd9c | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 116 | |
| 117 | void NotifyObjectEmitted(const ObjectImage& Obj); |
| 118 | void NotifyFreeingObject(const ObjectImage& Obj); |
Daniel Dunbar | 7e5d8a7 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | } // End llvm namespace |
| 122 | |
| 123 | #endif |