Daniel Dunbar | 6aec298 | 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 | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 14 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Jim Grosbach | f922910 | 2011-03-22 01:06:42 +0000 | [diff] [blame] | 15 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
Chandler Carruth | a1514e2 | 2012-12-04 07:12:27 +0000 | [diff] [blame] | 16 | #include "llvm/PassManager.h" |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 17 | |
| 18 | namespace llvm { |
| 19 | |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 20 | class ObjectImage; |
| 21 | |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 22 | // FIXME: This makes all kinds of horrible assumptions for the time being, |
| 23 | // like only having one module, not needing to worry about multi-threading, |
| 24 | // blah blah. Purely in get-it-up-and-limping mode for now. |
| 25 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 26 | class MCJIT : public ExecutionEngine { |
Jim Grosbach | 8005bcd | 2012-08-21 15:42:49 +0000 | [diff] [blame] | 27 | MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr, |
| 28 | bool AllocateGVsWithCode); |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 29 | |
| 30 | TargetMachine *TM; |
| 31 | MCContext *Ctx; |
Jim Grosbach | fcbe5b7 | 2011-04-04 23:04:39 +0000 | [diff] [blame] | 32 | RTDyldMemoryManager *MemMgr; |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 33 | RuntimeDyld Dyld; |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 34 | SmallVector<JITEventListener*, 2> EventListeners; |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 35 | |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 36 | // FIXME: Add support for multiple modules |
| 37 | bool isCompiled; |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 38 | Module *M; |
Andrew Kaylor | 3f23cef | 2012-10-02 21:18:39 +0000 | [diff] [blame] | 39 | OwningPtr<ObjectImage> LoadedObject; |
Jim Grosbach | 31649e6 | 2011-03-18 22:48:41 +0000 | [diff] [blame] | 40 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 41 | public: |
| 42 | ~MCJIT(); |
| 43 | |
| 44 | /// @name ExecutionEngine interface implementation |
| 45 | /// @{ |
| 46 | |
Andrew Kaylor | 2898988 | 2012-11-05 20:57:16 +0000 | [diff] [blame] | 47 | virtual void finalizeObject(); |
| 48 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 49 | virtual void *getPointerToBasicBlock(BasicBlock *BB); |
| 50 | |
| 51 | virtual void *getPointerToFunction(Function *F); |
| 52 | |
| 53 | virtual void *recompileAndRelinkFunction(Function *F); |
| 54 | |
| 55 | virtual void freeMachineCodeForFunction(Function *F); |
| 56 | |
| 57 | virtual GenericValue runFunction(Function *F, |
| 58 | const std::vector<GenericValue> &ArgValues); |
| 59 | |
Jim Grosbach | 34714a0 | 2011-03-22 18:05:27 +0000 | [diff] [blame] | 60 | /// getPointerToNamedFunction - This method returns the address of the |
| 61 | /// specified function by using the dlsym function call. As such it is only |
| 62 | /// useful for resolving library symbols, not code generated symbols. |
| 63 | /// |
| 64 | /// If AbortOnFailure is false and no function with the given name is |
| 65 | /// found, this function silently returns a null pointer. Otherwise, |
| 66 | /// it prints a message to stderr and aborts. |
| 67 | /// |
Danil Malyshev | 45a93d6 | 2012-01-05 21:16:14 +0000 | [diff] [blame] | 68 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 69 | bool AbortOnFailure = true); |
Danil Malyshev | 30b9e32 | 2012-03-28 21:46:36 +0000 | [diff] [blame] | 70 | |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 71 | /// mapSectionAddress - map a section to its target address space value. |
| 72 | /// Map the address of a JIT section as returned from the memory manager |
| 73 | /// to the address in the target process as the running code will see it. |
| 74 | /// This is the address which will be used for relocation resolution. |
Jim Grosbach | e940c1b | 2012-09-13 21:50:06 +0000 | [diff] [blame] | 75 | virtual void mapSectionAddress(const void *LocalAddress, |
| 76 | uint64_t TargetAddress) { |
Jim Grosbach | 020f4e8 | 2012-01-16 23:50:55 +0000 | [diff] [blame] | 77 | Dyld.mapSectionAddress(LocalAddress, TargetAddress); |
| 78 | } |
| 79 | |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 80 | virtual void RegisterJITEventListener(JITEventListener *L); |
| 81 | virtual void UnregisterJITEventListener(JITEventListener *L); |
| 82 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 83 | /// @} |
| 84 | /// @name (Private) Registration Interfaces |
| 85 | /// @{ |
| 86 | |
| 87 | static void Register() { |
| 88 | MCJITCtor = createJIT; |
| 89 | } |
| 90 | |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 91 | static ExecutionEngine *createJIT(Module *M, |
| 92 | std::string *ErrorStr, |
| 93 | JITMemoryManager *JMM, |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 94 | bool GVsWithCode, |
Dylan Noblesmith | c5b2858 | 2011-05-13 21:51:29 +0000 | [diff] [blame] | 95 | TargetMachine *TM); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 96 | |
| 97 | // @} |
Andrew Kaylor | ea708d1 | 2012-08-07 18:33:00 +0000 | [diff] [blame] | 98 | |
| 99 | protected: |
| 100 | /// emitObject -- Generate a JITed object in memory from the specified module |
| 101 | /// Currently, MCJIT only supports a single module and the module passed to |
| 102 | /// this function call is expected to be the contained module. The module |
| 103 | /// is passed as a parameter here to prepare for multiple module support in |
| 104 | /// the future. |
| 105 | void emitObject(Module *M); |
Andrew Kaylor | 776054d | 2012-11-06 18:51:59 +0000 | [diff] [blame] | 106 | |
| 107 | void NotifyObjectEmitted(const ObjectImage& Obj); |
| 108 | void NotifyFreeingObject(const ObjectImage& Obj); |
Daniel Dunbar | 6aec298 | 2010-11-17 16:06:43 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // End llvm namespace |
| 112 | |
| 113 | #endif |