LLVM API Documentation
00001 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- 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 abstract interface that implements execution support 00011 // for LLVM. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef EXECUTION_ENGINE_H 00016 #define EXECUTION_ENGINE_H 00017 00018 #include <vector> 00019 #include <map> 00020 #include <cassert> 00021 #include <string> 00022 #include "llvm/System/Mutex.h" 00023 00024 namespace llvm { 00025 00026 union GenericValue; 00027 class Constant; 00028 class Function; 00029 class GlobalVariable; 00030 class GlobalValue; 00031 class Module; 00032 class ModuleProvider; 00033 class TargetData; 00034 class Type; 00035 class MutexGuard; 00036 00037 class ExecutionEngineState { 00038 private: 00039 /// GlobalAddressMap - A mapping between LLVM global values and their 00040 /// actualized version... 00041 std::map<const GlobalValue*, void *> GlobalAddressMap; 00042 00043 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 00044 /// used to convert raw addresses into the LLVM global value that is emitted 00045 /// at the address. This map is not computed unless getGlobalValueAtAddress 00046 /// is called at some point. 00047 std::map<void *, const GlobalValue*> GlobalAddressReverseMap; 00048 00049 public: 00050 std::map<const GlobalValue*, void *> & 00051 getGlobalAddressMap(const MutexGuard &locked) { 00052 return GlobalAddressMap; 00053 } 00054 00055 std::map<void*, const GlobalValue*> & 00056 getGlobalAddressReverseMap(const MutexGuard& locked) { 00057 return GlobalAddressReverseMap; 00058 } 00059 }; 00060 00061 00062 class ExecutionEngine { 00063 Module &CurMod; 00064 const TargetData *TD; 00065 00066 ExecutionEngineState state; 00067 00068 protected: 00069 ModuleProvider *MP; 00070 00071 void setTargetData(const TargetData *td) { 00072 TD = td; 00073 } 00074 00075 // To avoid having libexecutionengine depend on the JIT and interpreter 00076 // libraries, the JIT and Interpreter set these functions to ctor pointers 00077 // at startup time if they are linked in. 00078 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*); 00079 static EECtorFn JITCtor, InterpCtor; 00080 00081 public: 00082 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and 00083 /// JITEmitter classes. It must be held while changing the internal state of 00084 /// any of those classes. 00085 sys::Mutex lock; // Used to make this class and subclasses thread-safe 00086 00087 ExecutionEngine(ModuleProvider *P); 00088 ExecutionEngine(Module *M); 00089 virtual ~ExecutionEngine(); 00090 00091 Module &getModule() const { return CurMod; } 00092 const TargetData *getTargetData() const { return TD; } 00093 00094 /// create - This is the factory method for creating an execution engine which 00095 /// is appropriate for the current machine. 00096 static ExecutionEngine *create(ModuleProvider *MP, 00097 bool ForceInterpreter = false); 00098 00099 /// runFunction - Execute the specified function with the specified arguments, 00100 /// and return the result. 00101 /// 00102 virtual GenericValue runFunction(Function *F, 00103 const std::vector<GenericValue> &ArgValues) = 0; 00104 00105 /// runStaticConstructorsDestructors - This method is used to execute all of 00106 /// the static constructors or destructors for a module, depending on the 00107 /// value of isDtors. 00108 void runStaticConstructorsDestructors(bool isDtors); 00109 00110 00111 /// runFunctionAsMain - This is a helper function which wraps runFunction to 00112 /// handle the common task of starting up main with the specified argc, argv, 00113 /// and envp parameters. 00114 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 00115 const char * const * envp); 00116 00117 00118 /// addGlobalMapping - Tell the execution engine that the specified global is 00119 /// at the specified location. This is used internally as functions are JIT'd 00120 /// and as global variables are laid out in memory. It can and should also be 00121 /// used by clients of the EE that want to have an LLVM global overlay 00122 /// existing data in memory. 00123 void addGlobalMapping(const GlobalValue *GV, void *Addr); 00124 00125 /// clearAllGlobalMappings - Clear all global mappings and start over again 00126 /// use in dynamic compilation scenarios when you want to move globals 00127 void clearAllGlobalMappings(); 00128 00129 /// updateGlobalMapping - Replace an existing mapping for GV with a new 00130 /// address. This updates both maps as required. If "Addr" is null, the 00131 /// entry for the global is removed from the mappings. 00132 void updateGlobalMapping(const GlobalValue *GV, void *Addr); 00133 00134 /// getPointerToGlobalIfAvailable - This returns the address of the specified 00135 /// global value if it is has already been codegen'd, otherwise it returns 00136 /// null. 00137 /// 00138 void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 00139 00140 /// getPointerToGlobal - This returns the address of the specified global 00141 /// value. This may involve code generation if it's a function. 00142 /// 00143 void *getPointerToGlobal(const GlobalValue *GV); 00144 00145 /// getPointerToFunction - The different EE's represent function bodies in 00146 /// different ways. They should each implement this to say what a function 00147 /// pointer should look like. 00148 /// 00149 virtual void *getPointerToFunction(Function *F) = 0; 00150 00151 /// getPointerToFunctionOrStub - If the specified function has been 00152 /// code-gen'd, return a pointer to the function. If not, compile it, or use 00153 /// a stub to implement lazy compilation if available. 00154 /// 00155 virtual void *getPointerToFunctionOrStub(Function *F) { 00156 // Default implementation, just codegen the function. 00157 return getPointerToFunction(F); 00158 } 00159 00160 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 00161 /// at the specified address. 00162 /// 00163 const GlobalValue *getGlobalValueAtAddress(void *Addr); 00164 00165 00166 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty); 00167 void InitializeMemory(const Constant *Init, void *Addr); 00168 00169 /// recompileAndRelinkFunction - This method is used to force a function 00170 /// which has already been compiled to be compiled again, possibly 00171 /// after it has been modified. Then the entry to the old copy is overwritten 00172 /// with a branch to the new copy. If there was no old copy, this acts 00173 /// just like VM::getPointerToFunction(). 00174 /// 00175 virtual void *recompileAndRelinkFunction(Function *F) = 0; 00176 00177 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 00178 /// corresponding to the machine code emitted to execute this function, useful 00179 /// for garbage-collecting generated code. 00180 /// 00181 virtual void freeMachineCodeForFunction(Function *F) = 0; 00182 00183 /// getOrEmitGlobalVariable - Return the address of the specified global 00184 /// variable, possibly emitting it to memory if needed. This is used by the 00185 /// Emitter. 00186 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 00187 return getPointerToGlobal((GlobalValue*)GV); 00188 } 00189 00190 protected: 00191 void emitGlobals(); 00192 00193 // EmitGlobalVariable - This method emits the specified global variable to the 00194 // address specified in GlobalAddresses, or allocates new memory if it's not 00195 // already in the map. 00196 void EmitGlobalVariable(const GlobalVariable *GV); 00197 00198 GenericValue getConstantValue(const Constant *C); 00199 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty); 00200 }; 00201 00202 } // End llvm namespace 00203 00204 #endif