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