MCJIT support for non-function sections.
[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) : JMM(jmm), M(m) {}
31   // We own the JMM, so make sure to delete it.
32   ~MCJITMemoryManager() { delete JMM; }
33
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
44   // Allocate ActualSize bytes, or more, for the named function. Return
45   // a pointer to the allocated memory and update Size to reflect how much
46   // memory was acutally allocated.
47   uint8_t *startFunctionBody(const char *Name, uintptr_t &Size) {
48     // FIXME: This should really reference the MCAsmInfo to get the global
49     //        prefix.
50     if (Name[0] == '_') ++Name;
51     Function *F = M->getFunction(Name);
52     // Some ObjC names have a prefixed \01 in the IR. If we failed to find
53     // the symbol and it's of the ObjC conventions (starts with "-" or 
54     // "+"), try prepending a \01 and see if we can find it that way.
55     if (!F && (Name[0] == '-' || Name[0] == '+'))
56       F = M->getFunction((Twine("\1") + Name).str());
57     assert(F && "No matching function in JIT IR Module!");
58     return JMM->startFunctionBody(F, Size);
59   }
60
61   // Mark the end of the function, including how much of the allocated
62   // memory was actually used.
63   void endFunctionBody(const char *Name, uint8_t *FunctionStart,
64                        uint8_t *FunctionEnd) {
65     // FIXME: This should really reference the MCAsmInfo to get the global
66     //        prefix.
67     if (Name[0] == '_') ++Name;
68     Function *F = M->getFunction(Name);
69     // Some ObjC names have a prefixed \01 in the IR. If we failed to find
70     // the symbol and it's of the ObjC conventions (starts with "-" or
71     // "+"), try prepending a \01 and see if we can find it that way.
72     if (!F && (Name[0] == '-' || Name[0] == '+'))
73       F = M->getFunction((Twine("\1") + Name).str());
74     assert(F && "No matching function in JIT IR Module!");
75     JMM->endFunctionBody(F, FunctionStart, FunctionEnd);
76   }
77
78 };
79
80 } // End llvm namespace
81
82 #endif