LLVM API Documentation

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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 #include <map>
00020 
00021 namespace llvm {
00022 
00023 class Function;
00024 class GlobalValue;
00025 class Constant;
00026 class TargetMachine;
00027 class TargetJITInfo;
00028 class MachineCodeEmitter;
00029 
00030 class JITState {
00031 private:
00032   FunctionPassManager PM;  // Passes to compile a function
00033 
00034   /// PendingGlobals - Global variables which have had memory allocated for them
00035   /// while a function was code generated, but which have not been initialized
00036   /// yet.
00037   std::vector<const GlobalVariable*> PendingGlobals;
00038 
00039 public:
00040   JITState(ModuleProvider *MP) : PM(MP) {}
00041 
00042   FunctionPassManager& getPM(const MutexGuard& locked) {
00043     return PM;
00044   }
00045 
00046   std::vector<const GlobalVariable*>& getPendingGlobals(const MutexGuard& locked) {
00047     return PendingGlobals;
00048   }
00049 };
00050 
00051 
00052 class JIT : public ExecutionEngine {
00053   TargetMachine &TM;       // The current target we are compiling to
00054   TargetJITInfo &TJI;      // The JITInfo for the target we are compiling to
00055   MachineCodeEmitter *MCE; // MCE object
00056 
00057   JITState state;
00058 
00059   JIT(ModuleProvider *MP, TargetMachine &tm, TargetJITInfo &tji);
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);
00075 
00076   /// run - Start execution with the specified function and arguments.
00077   ///
00078   virtual GenericValue runFunction(Function *F,
00079                                    const std::vector<GenericValue> &ArgValues);
00080 
00081   /// getPointerToNamedFunction - This method returns the address of the
00082   /// specified function by using the dlsym function call.  As such it is only
00083   /// useful for resolving library symbols, not code generated symbols.
00084   ///
00085   void *getPointerToNamedFunction(const std::string &Name);
00086 
00087   // CompilationCallback - Invoked the first time that a call site is found,
00088   // which causes lazy compilation of the target function.
00089   //
00090   static void CompilationCallback();
00091 
00092   /// getPointerToFunction - This returns the address of the specified function,
00093   /// compiling it if necessary.
00094   ///
00095   void *getPointerToFunction(Function *F);
00096 
00097   /// getOrEmitGlobalVariable - Return the address of the specified global
00098   /// variable, possibly emitting it to memory if needed.  This is used by the
00099   /// Emitter.
00100   void *getOrEmitGlobalVariable(const GlobalVariable *GV);
00101 
00102   /// getPointerToFunctionOrStub - If the specified function has been
00103   /// code-gen'd, return a pointer to the function.  If not, compile it, or use
00104   /// a stub to implement lazy compilation if available.
00105   ///
00106   void *getPointerToFunctionOrStub(Function *F);
00107 
00108   /// recompileAndRelinkFunction - This method is used to force a function
00109   /// which has already been compiled, to be compiled again, possibly
00110   /// after it has been modified. Then the entry to the old copy is overwritten
00111   /// with a branch to the new copy. If there was no old copy, this acts
00112   /// just like JIT::getPointerToFunction().
00113   ///
00114   void *recompileAndRelinkFunction(Function *F);
00115 
00116   /// freeMachineCodeForFunction - deallocate memory used to code-generate this
00117   /// Function.
00118   ///
00119   void freeMachineCodeForFunction(Function *F);
00120 
00121 private:
00122   static MachineCodeEmitter *createEmitter(JIT &J);
00123   void runJITOnFunction (Function *F);
00124 };
00125 
00126 } // End llvm namespace
00127 
00128 #endif