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   virtual Module *removeModuleProvider(ModuleProvider *MP,
00081                                        std::string *ErrInfo = 0);
00082 
00083   /// runFunction - Start execution with the specified function and arguments.
00084   ///
00085   virtual GenericValue runFunction(Function *F,
00086                                    const std::vector<GenericValue> &ArgValues);
00087 
00088   /// getPointerToNamedFunction - This method returns the address of the
00089   /// specified function by using the dlsym function call.  As such it is only
00090   /// useful for resolving library symbols, not code generated symbols.
00091   ///
00092   void *getPointerToNamedFunction(const std::string &Name);
00093 
00094   // CompilationCallback - Invoked the first time that a call site is found,
00095   // which causes lazy compilation of the target function.
00096   //
00097   static void CompilationCallback();
00098 
00099   /// getPointerToFunction - This returns the address of the specified function,
00100   /// compiling it if necessary.
00101   ///
00102   void *getPointerToFunction(Function *F);
00103 
00104   /// getOrEmitGlobalVariable - Return the address of the specified global
00105   /// variable, possibly emitting it to memory if needed.  This is used by the
00106   /// Emitter.
00107   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
00108 
00109   /// getPointerToFunctionOrStub - If the specified function has been
00110   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
00111   /// a stub to implement lazy compilation if available.
00112   ///
00113   void *getPointerToFunctionOrStub(Function *F);
00114 
00115   /// recompileAndRelinkFunction - This method is used to force a function
00116   /// which has already been compiled, to be compiled again, possibly
00117   /// after it has been modified. Then the entry to the old copy is overwritten
00118   /// with a branch to the new copy. If there was no old copy, this acts
00119   /// just like JIT::getPointerToFunction().
00120   ///
00121   void *recompileAndRelinkFunction(Function *F);
00122 
00123   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
00124   /// Function.
00125   ///
00126   void freeMachineCodeForFunction(Function *F);
00127 
00128   /// getCodeEmitter - Return the code emitter this JIT is emitting into.
00129   MachineCodeEmitter *getCodeEmitter() const { return MCE; }
00130   
00131   static ExecutionEngine *createJIT(ModuleProvider *MP, std::string *Err,
00132                                     JITMemoryManager *JMM, bool Fast);
00133   
00134 private:
00135   static MachineCodeEmitter *createEmitter(JIT &J, JITMemoryManager *JMM);
00136   void runJITOnFunction (Function *F);
00137   
00138 protected:
00139 
00140   /// getMemoryforGV - Allocate memory for a global variable.
00141   virtual char* getMemoryForGV(const GlobalVariable* GV);
00142 
00143 };
00144 
00145 } // End llvm namespace
00146 
00147 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.