blob: c17a39736ccca24e97ec3f977bf2cc404e92813b [file] [log] [blame]
Jim Grosbachb5728302011-04-04 23:20:40 +00001//===-- 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
18namespace 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.
23class MCJITMemoryManager : public RTDyldMemoryManager {
24 JITMemoryManager *JMM;
25
26 // FIXME: Multiple modules.
27 Module *M;
28public:
Jim Grosbachc1545142011-05-12 18:21:23 +000029 MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : JMM(jmm), M(m) {}
Jim Grosbach3326fc12011-10-18 19:57:38 +000030 // We own the JMM, so make sure to delete it.
31 ~MCJITMemoryManager() { delete JMM; }
Jim Grosbachb5728302011-04-04 23:20:40 +000032
33 // Allocate ActualSize bytes, or more, for the named function. Return
34 // a pointer to the allocated memory and update Size to reflect how much
35 // memory was acutally allocated.
Jim Grosbachc41ab782011-04-06 01:11:05 +000036 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size) {
Jim Grosbachb0271052011-04-08 17:31:24 +000037 // FIXME: This should really reference the MCAsmInfo to get the global
38 // prefix.
39 if (Name[0] == '_') ++Name;
Jim Grosbachb5728302011-04-04 23:20:40 +000040 Function *F = M->getFunction(Name);
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +000041 // Some ObjC names have a prefixed \01 in the IR. If we failed to find
42 // the symbol and it's of the ObjC conventions (starts with "-"), try
43 // prepending a \01 and see if we can find it that way.
44 if (!F && Name[0] == '-')
45 F = M->getFunction((Twine("\1") + Name).str());
Jim Grosbachb5728302011-04-04 23:20:40 +000046 assert(F && "No matching function in JIT IR Module!");
Jim Grosbachc41ab782011-04-06 01:11:05 +000047 return JMM->startFunctionBody(F, Size);
Jim Grosbachb5728302011-04-04 23:20:40 +000048 }
49
50 // Mark the end of the function, including how much of the allocated
51 // memory was actually used.
Jim Grosbachc41ab782011-04-06 01:11:05 +000052 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
53 uint8_t *FunctionEnd) {
Jim Grosbachb0271052011-04-08 17:31:24 +000054 // FIXME: This should really reference the MCAsmInfo to get the global
55 // prefix.
56 if (Name[0] == '_') ++Name;
Jim Grosbachb5728302011-04-04 23:20:40 +000057 Function *F = M->getFunction(Name);
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +000058 // 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 "-"), try
60 // prepending a \01 and see if we can find it that way.
61 if (!F && Name[0] == '-')
62 F = M->getFunction((Twine("\1") + Name).str());
Jim Grosbachb5728302011-04-04 23:20:40 +000063 assert(F && "No matching function in JIT IR Module!");
Jim Grosbachc41ab782011-04-06 01:11:05 +000064 JMM->endFunctionBody(F, FunctionStart, FunctionEnd);
Jim Grosbachb5728302011-04-04 23:20:40 +000065 }
66
67};
68
69} // End llvm namespace
70
71#endif