1 //===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
11 #define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
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"
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.
27 class MCJIT : public ExecutionEngine {
28 MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
29 bool AllocateGVsWithCode);
33 RTDyldMemoryManager *MemMgr;
35 SmallVector<JITEventListener*, 2> EventListeners;
37 // FIXME: Add support for multiple modules
40 OwningPtr<ObjectImage> LoadedObject;
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;
49 /// @name ExecutionEngine interface implementation
52 /// Sets the object manager that MCJIT should use to avoid compilation.
53 virtual void setObjectCache(ObjectCache *manager);
55 /// finalizeObject - ensure the module is fully processed and is usable.
57 /// It is the user-level function for completing the process of making the
58 /// object usable for execution. It should be called after sections within an
59 /// object have been relocated using mapSectionAddress. When this method is
60 /// called the MCJIT execution engine will reapply relocations for a loaded
62 virtual void finalizeObject();
64 virtual void *getPointerToBasicBlock(BasicBlock *BB);
66 virtual void *getPointerToFunction(Function *F);
68 virtual void *recompileAndRelinkFunction(Function *F);
70 virtual void freeMachineCodeForFunction(Function *F);
72 virtual GenericValue runFunction(Function *F,
73 const std::vector<GenericValue> &ArgValues);
75 /// getPointerToNamedFunction - This method returns the address of the
76 /// specified function by using the dlsym function call. As such it is only
77 /// useful for resolving library symbols, not code generated symbols.
79 /// If AbortOnFailure is false and no function with the given name is
80 /// found, this function silently returns a null pointer. Otherwise,
81 /// it prints a message to stderr and aborts.
83 virtual void *getPointerToNamedFunction(const std::string &Name,
84 bool AbortOnFailure = true);
86 /// mapSectionAddress - map a section to its target address space value.
87 /// Map the address of a JIT section as returned from the memory manager
88 /// to the address in the target process as the running code will see it.
89 /// This is the address which will be used for relocation resolution.
90 virtual void mapSectionAddress(const void *LocalAddress,
91 uint64_t TargetAddress) {
92 Dyld.mapSectionAddress(LocalAddress, TargetAddress);
95 virtual void RegisterJITEventListener(JITEventListener *L);
96 virtual void UnregisterJITEventListener(JITEventListener *L);
99 /// @name (Private) Registration Interfaces
102 static void Register() {
103 MCJITCtor = createJIT;
106 static ExecutionEngine *createJIT(Module *M,
107 std::string *ErrorStr,
108 RTDyldMemoryManager *MemMgr,
115 /// emitObject -- Generate a JITed object in memory from the specified module
116 /// Currently, MCJIT only supports a single module and the module passed to
117 /// this function call is expected to be the contained module. The module
118 /// is passed as a parameter here to prepare for multiple module support in
120 ObjectBufferStream* emitObject(Module *M);
122 void loadObject(Module *M);
124 void NotifyObjectEmitted(const ObjectImage& Obj);
125 void NotifyFreeingObject(const ObjectImage& Obj);
128 } // End llvm namespace