LLVM API Documentation

lib/ExecutionEngine/JIT/JIT.h

Go to the documentation of this file.
00001 //===-- JIT.h - Class definition for the JIT --------------------*- 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 // This file defines the top-level JIT data structure.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef JIT_H
00015 #define JIT_H
00016 
00017 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00018 #include "llvm/PassManager.h"
00019 
00020 namespace llvm {
00021 
00022 class Function;
00023 class GlobalValue;
00024 class Constant;
00025 class TargetMachine;
00026 class TargetJITInfo;
00027 class MachineCodeEmitter;
00028 
00029 class JITState {
00030 private:
00031   FunctionPassManager PM;  // Passes to compile a function
00032 
00033   /// PendingGlobals - Global variables which have had memory allocated for them
00034   /// while a function was code generated, but which have not been initialized
00035   /// yet.
00036   std::vector<const GlobalVariable*> PendingGlobals;
00037 
00038 public:
00039   explicit JITState(ModuleProvider *MP) : PM(MP) {}
00040 
00041   FunctionPassManager &getPM(const MutexGuard &L) {
00042     return PM;
00043   }
00044 
00045   std::vector<const GlobalVariable*> &getPendingGlobals(const MutexGuard &L) {
00046     return PendingGlobals;
00047   }
00048 };
00049 
00050 
00051 class JIT : public ExecutionEngine {
00052   TargetMachine &TM;       // The current target we are compiling to
00053   TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
00054   MachineCodeEmitter *MCE; // MCE object
00055 
00056   JITState *jitstate;
00057 
00058   JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji, 
00059       JITMemoryManager *JMM, bool Fast);
00060 public:
00061   ~JIT();
00062 
00063   static void Register() {
00064     JITCtor = create;
00065   }
00066   
00067   /// getJITInfo - Return the target JIT information structure.
00068   ///
00069   TargetJITInfo &getJITInfo() const { return TJI; }
00070 
00071   /// create - Create an return a new JIT compiler if there is one available
00072   /// for the current target.  Otherwise, return null.
00073   ///
00074   static ExecutionEngine *create(ModuleProvider *MP, std::string *Err,
00075                                  bool Fast = false) {
00076     return createJIT(MP, Err, 0, Fast);
00077   }
00078 
00079   virtual void addModuleProvider(ModuleProvider *MP);
00080   
00081   /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
00082   /// Relases the Module from the ModuleProvider, materializing it in the
00083   /// process, and returns the materialized Module.
00084   virtual Module *removeModuleProvider(ModuleProvider *MP,
00085                                        std::string *ErrInfo = 0);
00086 
00087   /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
00088   /// and deletes the ModuleProvider and owned Module.  Avoids materializing 
00089   /// the underlying module.
00090   virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0);
00091 
00092   /// runFunction - Start execution with the specified function and arguments.
00093   ///
00094   virtual GenericValue runFunction(Function *F,
00095                                    const std::vector<GenericValue> &ArgValues);
00096 
00097   /// getPointerToNamedFunction - This method returns the address of the
00098   /// specified function by using the dlsym function call.  As such it is only
00099   /// useful for resolving library symbols, not code generated symbols.
00100   ///
00101   /// If AbortOnFailure is false and no function with the given name is
00102   /// found, this function silently returns a null pointer. Otherwise,
00103   /// it prints a message to stderr and aborts.
00104   ///
00105   void *getPointerToNamedFunction(const std::string &Name,
00106                                   bool AbortOnFailure = true);
00107 
00108   // CompilationCallback - Invoked the first time that a call site is found,
00109   // which causes lazy compilation of the target function.
00110   //
00111   static void CompilationCallback();
00112 
00113   /// getPointerToFunction - This returns the address of the specified function,
00114   /// compiling it if necessary.
00115   ///
00116   void *getPointerToFunction(Function *F);
00117 
00118   /// getOrEmitGlobalVariable - Return the address of the specified global
00119   /// variable, possibly emitting it to memory if needed.  This is used by the
00120   /// Emitter.
00121   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
00122 
00123   /// getPointerToFunctionOrStub - If the specified function has been
00124   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
00125   /// a stub to implement lazy compilation if available.
00126   ///
00127   void *getPointerToFunctionOrStub(Function *F);
00128 
00129   /// recompileAndRelinkFunction - This method is used to force a function
00130   /// which has already been compiled, to be compiled again, possibly
00131   /// after it has been modified. Then the entry to the old copy is overwritten
00132   /// with a branch to the new copy. If there was no old copy, this acts
00133   /// just like JIT::getPointerToFunction().
00134   ///
00135   void *recompileAndRelinkFunction(Function *F);
00136 
00137   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
00138   /// Function.
00139   ///
00140   void freeMachineCodeForFunction(Function *F);
00141 
00142   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
00143   MachineCodeEmitter *getCodeEmitter() const { return MCE; }
00144   
00145   static ExecutionEngine *createJIT(ModuleProvider *MP, std::string *Err,
00146                                     JITMemoryManager *JMM, bool Fast);
00147   
00148 private:
00149   static MachineCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
00150   void runJITOnFunction (Function *F);
00151   
00152 protected:
00153 
00154   /// getMemoryforGV - Allocate memory for a global variable.
00155   virtual char* getMemoryForGV(const GlobalVariable* GV);
00156 
00157 };
00158 
00159 } // End llvm namespace
00160 
00161 #endif