summaryrefslogtreecommitdiff
path: root/lib/ExecutionEngine/MCJIT/MCJITMemoryManager.h
blob: bc7080d2d30f9ce0589642ffbdfc26d137baf082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//===-- MCJITMemoryManager.h - Definition for the Memory Manager ---C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIB_EXECUTIONENGINE_MCJITMEMORYMANAGER_H
#define LLVM_LIB_EXECUTIONENGINE_MCJITMEMORYMANAGER_H

#include "llvm/Module.h"
#include "llvm/ExecutionEngine/JITMemoryManager.h"
#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include <assert.h>

namespace llvm {

// The MCJIT memory manager is a layer between the standard JITMemoryManager
// and the RuntimeDyld interface that maps objects, by name, onto their
// matching LLVM IR counterparts in the module(s) being compiled.
class MCJITMemoryManager : public RTDyldMemoryManager {
  virtual void anchor();
  JITMemoryManager *JMM;

  // FIXME: Multiple modules.
  Module *M;
public:
  MCJITMemoryManager(JITMemoryManager *jmm, Module *m) : JMM(jmm), M(m) {}
  // We own the JMM, so make sure to delete it.
  ~MCJITMemoryManager() { delete JMM; }

  // Allocate ActualSize bytes, or more, for the named function. Return
  // a pointer to the allocated memory and update Size to reflect how much
  // memory was acutally allocated.
  uint8_t *startFunctionBody(const char *Name, uintptr_t &Size) {
    // FIXME: This should really reference the MCAsmInfo to get the global
    //        prefix.
    if (Name[0] == '_') ++Name;
    Function *F = M->getFunction(Name);
    // Some ObjC names have a prefixed \01 in the IR. If we failed to find
    // the symbol and it's of the ObjC conventions (starts with "-" or 
    // "+"), try prepending a \01 and see if we can find it that way.
    if (!F && (Name[0] == '-' || Name[0] == '+'))
      F = M->getFunction((Twine("\1") + Name).str());
    assert(F && "No matching function in JIT IR Module!");
    return JMM->startFunctionBody(F, Size);
  }

  // Mark the end of the function, including how much of the allocated
  // memory was actually used.
  void endFunctionBody(const char *Name, uint8_t *FunctionStart,
                       uint8_t *FunctionEnd) {
    // FIXME: This should really reference the MCAsmInfo to get the global
    //        prefix.
    if (Name[0] == '_') ++Name;
    Function *F = M->getFunction(Name);
    // Some ObjC names have a prefixed \01 in the IR. If we failed to find
    // the symbol and it's of the ObjC conventions (starts with "-" or
    // "+"), try prepending a \01 and see if we can find it that way.
    if (!F && (Name[0] == '-' || Name[0] == '+'))
      F = M->getFunction((Twine("\1") + Name).str());
    assert(F && "No matching function in JIT IR Module!");
    JMM->endFunctionBody(F, FunctionStart, FunctionEnd);
  }

};

} // End llvm namespace

#endif