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