LLVM API Documentation

lib/ExecutionEngine/MCJIT/MCJIT.h
Go to the documentation of this file.
00001 //===-- MCJIT.h - Class definition for the MCJIT ----------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_LIB_EXECUTIONENGINE_MCJIT_H
00011 #define LLVM_LIB_EXECUTIONENGINE_MCJIT_H
00012 
00013 #include "llvm/ADT/SmallVector.h"
00014 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00015 #include "llvm/ExecutionEngine/ObjectCache.h"
00016 #include "llvm/ExecutionEngine/RuntimeDyld.h"
00017 #include "llvm/PassManager.h"
00018 
00019 namespace llvm {
00020 
00021 class ObjectImage;
00022 
00023 // FIXME: This makes all kinds of horrible assumptions for the time being,
00024 // like only having one module, not needing to worry about multi-threading,
00025 // blah blah. Purely in get-it-up-and-limping mode for now.
00026 
00027 class MCJIT : public ExecutionEngine {
00028   MCJIT(Module *M, TargetMachine *tm, RTDyldMemoryManager *MemMgr,
00029         bool AllocateGVsWithCode);
00030 
00031   TargetMachine *TM;
00032   MCContext *Ctx;
00033   RTDyldMemoryManager *MemMgr;
00034   RuntimeDyld Dyld;
00035   SmallVector<JITEventListener*, 2> EventListeners;
00036 
00037   // FIXME: Add support for multiple modules
00038   bool IsLoaded;
00039   Module *M;
00040   OwningPtr<ObjectImage> LoadedObject;
00041 
00042   // An optional ObjectCache to be notified of compiled objects and used to
00043   // perform lookup of pre-compiled code to avoid re-compilation.
00044   ObjectCache *ObjCache;
00045 
00046 public:
00047   ~MCJIT();
00048 
00049   /// @name ExecutionEngine interface implementation
00050   /// @{
00051 
00052   /// Sets the object manager that MCJIT should use to avoid compilation.
00053   virtual void setObjectCache(ObjectCache *manager);
00054 
00055   /// finalizeObject - ensure the module is fully processed and is usable.
00056   ///
00057   /// It is the user-level function for completing the process of making the
00058   /// object usable for execution. It should be called after sections within an
00059   /// object have been relocated using mapSectionAddress.  When this method is
00060   /// called the MCJIT execution engine will reapply relocations for a loaded
00061   /// object.
00062   virtual void finalizeObject();
00063 
00064   virtual void *getPointerToBasicBlock(BasicBlock *BB);
00065 
00066   virtual void *getPointerToFunction(Function *F);
00067 
00068   virtual void *recompileAndRelinkFunction(Function *F);
00069 
00070   virtual void freeMachineCodeForFunction(Function *F);
00071 
00072   virtual GenericValue runFunction(Function *F,
00073                                    const std::vector<GenericValue> &ArgValues);
00074 
00075   /// getPointerToNamedFunction - This method returns the address of the
00076   /// specified function by using the dlsym function call.  As such it is only
00077   /// useful for resolving library symbols, not code generated symbols.
00078   ///
00079   /// If AbortOnFailure is false and no function with the given name is
00080   /// found, this function silently returns a null pointer. Otherwise,
00081   /// it prints a message to stderr and aborts.
00082   ///
00083   virtual void *getPointerToNamedFunction(const std::string &Name,
00084                                           bool AbortOnFailure = true);
00085 
00086   /// mapSectionAddress - map a section to its target address space value.
00087   /// Map the address of a JIT section as returned from the memory manager
00088   /// to the address in the target process as the running code will see it.
00089   /// This is the address which will be used for relocation resolution.
00090   virtual void mapSectionAddress(const void *LocalAddress,
00091                                  uint64_t TargetAddress) {
00092     Dyld.mapSectionAddress(LocalAddress, TargetAddress);
00093   }
00094 
00095   virtual void RegisterJITEventListener(JITEventListener *L);
00096   virtual void UnregisterJITEventListener(JITEventListener *L);
00097 
00098   /// @}
00099   /// @name (Private) Registration Interfaces
00100   /// @{
00101 
00102   static void Register() {
00103     MCJITCtor = createJIT;
00104   }
00105 
00106   static ExecutionEngine *createJIT(Module *M,
00107                                     std::string *ErrorStr,
00108                                     RTDyldMemoryManager *MemMgr,
00109                                     bool GVsWithCode,
00110                                     TargetMachine *TM);
00111 
00112   // @}
00113 
00114 protected:
00115   /// emitObject -- Generate a JITed object in memory from the specified module
00116   /// Currently, MCJIT only supports a single module and the module passed to
00117   /// this function call is expected to be the contained module.  The module
00118   /// is passed as a parameter here to prepare for multiple module support in 
00119   /// the future.
00120   ObjectBufferStream* emitObject(Module *M);
00121 
00122   void loadObject(Module *M);
00123 
00124   void NotifyObjectEmitted(const ObjectImage& Obj);
00125   void NotifyFreeingObject(const ObjectImage& Obj);
00126 };
00127 
00128 } // End llvm namespace
00129 
00130 #endif