MCJIT.h revision 1c489455ea5fac43a5f20911dfb5486630eb0160
1//===-- MCJIT.h - Class definition for the MCJIT ----------------*- 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_MCJIT_H
11#define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
12
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ExecutionEngine/ExecutionEngine.h"
15#include "llvm/ExecutionEngine/ObjectCache.h"
16#include "llvm/ExecutionEngine/RuntimeDyld.h"
17#include "llvm/PassManager.h"
18
19namespace llvm {
20
21class ObjectImage;
22
23// FIXME: This makes all kinds of horrible assumptions for the time being,
24// like only having one module, not needing to worry about multi-threading,
25// blah blah. Purely in get-it-up-and-limping mode for now.
26
27class MCJIT : public ExecutionEngine {
28  MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
29        bool AllocateGVsWithCode);
30
31  TargetMachine *TM;
32  MCContext *Ctx;
33  RTDyldMemoryManager *MemMgr;
34  RuntimeDyld Dyld;
35  SmallVector<JITEventListener*, 2> EventListeners;
36
37  // FIXME: Add support for multiple modules
38  bool IsLoaded;
39  Module *M;
40  OwningPtr<ObjectImage> LoadedObject;
41
42  // An optional ObjectCache to be notified of compiled objects and used to
43  // perform lookup of pre-compiled code to avoid re-compilation.
44  ObjectCache *ObjCache;
45
46public:
47  ~MCJIT();
48
49  /// @name ExecutionEngine interface implementation
50  /// @{
51
52  /// Sets the object manager that MCJIT should use to avoid compilation.
53  virtual void setObjectCache(ObjectCache *manager);
54
55  virtual void finalizeObject();
56
57  virtual void *getPointerToBasicBlock(BasicBlock *BB);
58
59  virtual void *getPointerToFunction(Function *F);
60
61  virtual void *recompileAndRelinkFunction(Function *F);
62
63  virtual void freeMachineCodeForFunction(Function *F);
64
65  virtual GenericValue runFunction(Function *F,
66                                   const std::vector<GenericValue> &ArgValues);
67
68  /// getPointerToNamedFunction - This method returns the address of the
69  /// specified function by using the dlsym function call.  As such it is only
70  /// useful for resolving library symbols, not code generated symbols.
71  ///
72  /// If AbortOnFailure is false and no function with the given name is
73  /// found, this function silently returns a null pointer. Otherwise,
74  /// it prints a message to stderr and aborts.
75  ///
76  virtual void *getPointerToNamedFunction(const std::string &Name,
77                                          bool AbortOnFailure = true);
78
79  /// mapSectionAddress - map a section to its target address space value.
80  /// Map the address of a JIT section as returned from the memory manager
81  /// to the address in the target process as the running code will see it.
82  /// This is the address which will be used for relocation resolution.
83  virtual void mapSectionAddress(const void *LocalAddress,
84                                 uint64_t TargetAddress) {
85    Dyld.mapSectionAddress(LocalAddress, TargetAddress);
86  }
87
88  virtual void RegisterJITEventListener(JITEventListener *L);
89  virtual void UnregisterJITEventListener(JITEventListener *L);
90
91  /// @}
92  /// @name (Private) Registration Interfaces
93  /// @{
94
95  static void Register() {
96    MCJITCtor = createJIT;
97  }
98
99  static ExecutionEngine *createJIT(Module *M,
100                                    std::string *ErrorStr,
101                                    JITMemoryManager *JMM,
102                                    bool GVsWithCode,
103                                    TargetMachine *TM);
104
105  // @}
106
107protected:
108  /// emitObject -- Generate a JITed object in memory from the specified module
109  /// Currently, MCJIT only supports a single module and the module passed to
110  /// this function call is expected to be the contained module.  The module
111  /// is passed as a parameter here to prepare for multiple module support in
112  /// the future.
113  ObjectBufferStream* emitObject(Module *M);
114
115  void loadObject(Module *M);
116
117  void NotifyObjectEmitted(const ObjectImage& Obj);
118  void NotifyFreeingObject(const ObjectImage& Obj);
119};
120
121} // End llvm namespace
122
123#endif
124