Move getPointerToNamedFunction() from JIT/MCJIT to JITMemoryManager.
[oota-llvm.git] / lib / ExecutionEngine / MCJIT / MCJITMemoryManager.h
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 {
24   virtual void anchor();
25   JITMemoryManager *JMM;
26
27   // FIXME: Multiple modules.
28   Module *M;
29 public:
30   MCJITMemoryManager(JITMemoryManager *jmm, Module *m) :
31     JMM(jmm?jmm:JITMemoryManager::CreateDefaultMemManager()), M(m) {}
32   // We own the JMM, so make sure to delete it.
33   ~MCJITMemoryManager() { delete JMM; }
34
35   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
36                                unsigned SectionID) {
37     return JMM->allocateDataSection(Size, Alignment, SectionID);
38   }
39
40   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
41                                unsigned SectionID) {
42     return JMM->allocateCodeSection(Size, Alignment, SectionID);
43   }
44
45   virtual void *getPointerToNamedFunction(const std::string &Name,
46                                           bool AbortOnFailure = true) {
47     return JMM->getPointerToNamedFunction(Name, AbortOnFailure);
48   }
49
50   // Allocate ActualSize bytes, or more, for the named function. Return
51   // a pointer to the allocated memory and update Size to reflect how much
52   // memory was acutally allocated.
53   uint8_t *startFunctionBody(const char *Name, uintptr_t &Size) {
54     // FIXME: This should really reference the MCAsmInfo to get the global
55     //        prefix.
56     if (Name[0] == '_') ++Name;
57     Function *F = M->getFunction(Name);
58     // Some ObjC names have a prefixed \01 in the IR. If we failed to find
59     // the symbol and it's of the ObjC conventions (starts with "-" or
60     // "+"), try prepending a \01 and see if we can find it that way.
61     if (!F && (Name[0] == '-' || Name[0] == '+'))
62       F = M->getFunction((Twine("\1") + Name).str());
63     assert(F && "No matching function in JIT IR Module!");
64     return JMM->startFunctionBody(F, Size);
65   }
66
67   // Mark the end of the function, including how much of the allocated
68   // memory was actually used.
69   void endFunctionBody(const char *Name, uint8_t *FunctionStart,
70                        uint8_t *FunctionEnd) {
71     // FIXME: This should really reference the MCAsmInfo to get the global
72     //        prefix.
73     if (Name[0] == '_') ++Name;
74     Function *F = M->getFunction(Name);
75     // Some ObjC names have a prefixed \01 in the IR. If we failed to find
76     // the symbol and it's of the ObjC conventions (starts with "-" or
77     // "+"), try prepending a \01 and see if we can find it that way.
78     if (!F && (Name[0] == '-' || Name[0] == '+'))
79       F = M->getFunction((Twine("\1") + Name).str());
80     assert(F && "No matching function in JIT IR Module!");
81     JMM->endFunctionBody(F, FunctionStart, FunctionEnd);
82   }
83
84 };
85
86 } // End llvm namespace
87
88 #endif