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