blob: bc7080d2d30f9ce0589642ffbdfc26d137baf082 [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 {
David Blaikie2d24e2a2011-12-20 02:50:00 +000024 virtual void anchor();
Jim Grosbachb5728302011-04-04 23:20:40 +000025 JITMemoryManager *JMM;
26
27 // FIXME: Multiple modules.
28 Module *M;
29public:
Jim Grosbachc1545142011-05-12 18:21:23 +000030 MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : JMM(jmm), M(m) {}
Jim Grosbach3326fc12011-10-18 19:57:38 +000031 // We own the JMM, so make sure to delete it.
32 ~MCJITMemoryManager() { delete JMM; }
Jim Grosbachb5728302011-04-04 23:20:40 +000033
34 // Allocate ActualSize bytes, or more, for the named function. Return
35 // a pointer to the allocated memory and update Size to reflect how much
36 // memory was acutally allocated.
Jim Grosbachc41ab782011-04-06 01:11:05 +000037 uint8_t *startFunctionBody(const char *Name, uintptr_t &Size) {
Jim Grosbachb0271052011-04-08 17:31:24 +000038 // FIXME: This should really reference the MCAsmInfo to get the global
39 // prefix.
40 if (Name[0] == '_') ++Name;
Jim Grosbachb5728302011-04-04 23:20:40 +000041 Function *F = M->getFunction(Name);
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +000042 // Some ObjC names have a prefixed \01 in the IR. If we failed to find
Sean Callananafe153c2011-11-12 02:31:32 +000043 // the symbol and it's of the ObjC conventions (starts with "-" or
44 // "+"), try prepending a \01 and see if we can find it that way.
45 if (!F && (Name[0] == '-' || Name[0] == '+'))
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +000046 F = M->getFunction((Twine("\1") + Name).str());
Jim Grosbachb5728302011-04-04 23:20:40 +000047 assert(F && "No matching function in JIT IR Module!");
Jim Grosbachc41ab782011-04-06 01:11:05 +000048 return JMM->startFunctionBody(F, Size);
Jim Grosbachb5728302011-04-04 23:20:40 +000049 }
50
51 // Mark the end of the function, including how much of the allocated
52 // memory was actually used.
Jim Grosbachc41ab782011-04-06 01:11:05 +000053 void endFunctionBody(const char *Name, uint8_t *FunctionStart,
54 uint8_t *FunctionEnd) {
Jim Grosbachb0271052011-04-08 17:31:24 +000055 // FIXME: This should really reference the MCAsmInfo to get the global
56 // prefix.
57 if (Name[0] == '_') ++Name;
Jim Grosbachb5728302011-04-04 23:20:40 +000058 Function *F = M->getFunction(Name);
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +000059 // Some ObjC names have a prefixed \01 in the IR. If we failed to find
Sean Callananafe153c2011-11-12 02:31:32 +000060 // the symbol and it's of the ObjC conventions (starts with "-" or
61 // "+"), try prepending a \01 and see if we can find it that way.
62 if (!F && (Name[0] == '-' || Name[0] == '+'))
Jim Grosbach3ec2c7c2011-05-18 23:53:21 +000063 F = M->getFunction((Twine("\1") + Name).str());
Jim Grosbachb5728302011-04-04 23:20:40 +000064 assert(F && "No matching function in JIT IR Module!");
Jim Grosbachc41ab782011-04-06 01:11:05 +000065 JMM->endFunctionBody(F, FunctionStart, FunctionEnd);
Jim Grosbachb5728302011-04-04 23:20:40 +000066 }
67
68};
69
70} // End llvm namespace
71
72#endif