Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 1 | //===-- MCJITMemoryManager.h - Definition for the Memory Manager ---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_MCJITMEMORYMANAGER_H |
| 11 | #define LLVM_LIB_EXECUTIONENGINE_MCJITMEMORYMANAGER_H |
| 12 | |
| 13 | #include "llvm/Module.h" |
| 14 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
| 15 | #include "llvm/ExecutionEngine/RuntimeDyld.h" |
| 16 | #include <assert.h> |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
| 20 | // The MCJIT memory manager is a layer between the standard JITMemoryManager |
| 21 | // and the RuntimeDyld interface that maps objects, by name, onto their |
| 22 | // matching LLVM IR counterparts in the module(s) being compiled. |
| 23 | class MCJITMemoryManager : public RTDyldMemoryManager { |
David Blaikie | 2d24e2a | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 24 | virtual void anchor(); |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 25 | JITMemoryManager *JMM; |
| 26 | |
| 27 | // FIXME: Multiple modules. |
| 28 | Module *M; |
| 29 | public: |
Jim Grosbach | c154514 | 2011-05-12 18:21:23 +0000 | [diff] [blame] | 30 | MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : JMM(jmm), M(m) {} |
Jim Grosbach | 3326fc1 | 2011-10-18 19:57:38 +0000 | [diff] [blame] | 31 | // We own the JMM, so make sure to delete it. |
| 32 | ~MCJITMemoryManager() { delete JMM; } |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 33 | |
Jim Grosbach | 61425c0 | 2012-01-16 22:26:39 +0000 | [diff] [blame] | 34 | uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment, |
| 35 | unsigned SectionID) { |
| 36 | return JMM->allocateDataSection(Size, Alignment, SectionID); |
| 37 | } |
| 38 | |
| 39 | uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment, |
| 40 | unsigned SectionID) { |
| 41 | return JMM->allocateCodeSection(Size, Alignment, SectionID); |
| 42 | } |
| 43 | |
Danil Malyshev | b474620 | 2012-03-21 19:13:08 +0000 | [diff] [blame] | 44 | virtual void *getPointerToNamedFunction(const std::string &Name, |
| 45 | bool AbortOnFailure = true) { |
| 46 | return JMM->getPointerToNamedFunction(Name, AbortOnFailure); |
| 47 | } |
| 48 | |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 49 | // Allocate ActualSize bytes, or more, for the named function. Return |
| 50 | // a pointer to the allocated memory and update Size to reflect how much |
| 51 | // memory was acutally allocated. |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 52 | uint8_t *startFunctionBody(const char *Name, uintptr_t &Size) { |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 53 | // FIXME: This should really reference the MCAsmInfo to get the global |
| 54 | // prefix. |
| 55 | if (Name[0] == '_') ++Name; |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 56 | Function *F = M->getFunction(Name); |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 57 | // Some ObjC names have a prefixed \01 in the IR. If we failed to find |
Sean Callanan | afe153c | 2011-11-12 02:31:32 +0000 | [diff] [blame] | 58 | // the symbol and it's of the ObjC conventions (starts with "-" or |
| 59 | // "+"), try prepending a \01 and see if we can find it that way. |
| 60 | if (!F && (Name[0] == '-' || Name[0] == '+')) |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 61 | F = M->getFunction((Twine("\1") + Name).str()); |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 62 | assert(F && "No matching function in JIT IR Module!"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 63 | return JMM->startFunctionBody(F, Size); |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Mark the end of the function, including how much of the allocated |
| 67 | // memory was actually used. |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 68 | void endFunctionBody(const char *Name, uint8_t *FunctionStart, |
| 69 | uint8_t *FunctionEnd) { |
Jim Grosbach | b027105 | 2011-04-08 17:31:24 +0000 | [diff] [blame] | 70 | // FIXME: This should really reference the MCAsmInfo to get the global |
| 71 | // prefix. |
| 72 | if (Name[0] == '_') ++Name; |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 73 | Function *F = M->getFunction(Name); |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 74 | // Some ObjC names have a prefixed \01 in the IR. If we failed to find |
Sean Callanan | afe153c | 2011-11-12 02:31:32 +0000 | [diff] [blame] | 75 | // the symbol and it's of the ObjC conventions (starts with "-" or |
| 76 | // "+"), try prepending a \01 and see if we can find it that way. |
| 77 | if (!F && (Name[0] == '-' || Name[0] == '+')) |
Jim Grosbach | 3ec2c7c | 2011-05-18 23:53:21 +0000 | [diff] [blame] | 78 | F = M->getFunction((Twine("\1") + Name).str()); |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 79 | assert(F && "No matching function in JIT IR Module!"); |
Jim Grosbach | c41ab78 | 2011-04-06 01:11:05 +0000 | [diff] [blame] | 80 | JMM->endFunctionBody(F, FunctionStart, FunctionEnd); |
Jim Grosbach | b572830 | 2011-04-04 23:20:40 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | }; |
| 84 | |
| 85 | } // End llvm namespace |
| 86 | |
| 87 | #endif |