LLVM API Documentation
00001 //===- ExecutionEngine.h - Abstract Execution Engine Interface --*- 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 abstract interface that implements execution support 00011 // for LLVM. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_EXECUTION_ENGINE_H 00016 #define LLVM_EXECUTION_ENGINE_H 00017 00018 #include <vector> 00019 #include <map> 00020 #include <string> 00021 #include "llvm/System/Mutex.h" 00022 #include "llvm/ADT/SmallVector.h" 00023 00024 namespace llvm { 00025 00026 struct 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 class JITMemoryManager; 00037 00038 class ExecutionEngineState { 00039 private: 00040 /// GlobalAddressMap - A mapping between LLVM global values and their 00041 /// actualized version... 00042 std::map<const GlobalValue*, void *> GlobalAddressMap; 00043 00044 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 00045 /// used to convert raw addresses into the LLVM global value that is emitted 00046 /// at the address. This map is not computed unless getGlobalValueAtAddress 00047 /// is called at some point. 00048 std::map<void *, const GlobalValue*> GlobalAddressReverseMap; 00049 00050 public: 00051 std::map<const GlobalValue*, void *> & 00052 getGlobalAddressMap(const MutexGuard &) { 00053 return GlobalAddressMap; 00054 } 00055 00056 std::map<void*, const GlobalValue*> & 00057 getGlobalAddressReverseMap(const MutexGuard &) { 00058 return GlobalAddressReverseMap; 00059 } 00060 }; 00061 00062 00063 class ExecutionEngine { 00064 const TargetData *TD; 00065 ExecutionEngineState state; 00066 bool LazyCompilationDisabled; 00067 bool GVCompilationDisabled; 00068 bool SymbolSearchingDisabled; 00069 00070 protected: 00071 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We 00072 /// use a smallvector to optimize for the case where there is only one module. 00073 SmallVector<ModuleProvider*, 1> Modules; 00074 00075 void setTargetData(const TargetData *td) { 00076 TD = td; 00077 } 00078 00079 /// getMemoryforGV - Allocate memory for a global variable. 00080 virtual char* getMemoryForGV(const GlobalVariable* GV); 00081 00082 // To avoid having libexecutionengine depend on the JIT and interpreter 00083 // libraries, the JIT and Interpreter set these functions to ctor pointers 00084 // at startup time if they are linked in. 00085 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*, 00086 bool Fast); 00087 static EECtorFn JITCtor, InterpCtor; 00088 00089 /// LazyFunctionCreator - If an unknown function is needed, this function 00090 /// pointer is invoked to create it. If this returns null, the JIT will abort. 00091 void* (*LazyFunctionCreator)(const std::string &); 00092 00093 /// ExceptionTableRegister - If Exception Handling is set, the JIT will 00094 /// register dwarf tables with this function 00095 typedef void (*EERegisterFn)(void*); 00096 static EERegisterFn ExceptionTableRegister; 00097 00098 public: 00099 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and 00100 /// JITEmitter classes. It must be held while changing the internal state of 00101 /// any of those classes. 00102 sys::Mutex lock; // Used to make this class and subclasses thread-safe 00103 00104 //===--------------------------------------------------------------------===// 00105 // ExecutionEngine Startup 00106 //===--------------------------------------------------------------------===// 00107 00108 virtual ~ExecutionEngine(); 00109 00110 /// create - This is the factory method for creating an execution engine which 00111 /// is appropriate for the current machine. This takes ownership of the 00112 /// module provider. 00113 static ExecutionEngine *create(ModuleProvider *MP, 00114 bool ForceInterpreter = false, 00115 std::string *ErrorStr = 0, 00116 bool Fast = false); 00117 00118 /// create - This is the factory method for creating an execution engine which 00119 /// is appropriate for the current machine. This takes ownership of the 00120 /// module. 00121 static ExecutionEngine *create(Module *M); 00122 00123 /// createJIT - This is the factory method for creating a JIT for the current 00124 /// machine, it does not fall back to the interpreter. This takes ownership 00125 /// of the ModuleProvider and JITMemoryManager if successful. 00126 static ExecutionEngine *createJIT(ModuleProvider *MP, 00127 std::string *ErrorStr = 0, 00128 JITMemoryManager *JMM = 0, 00129 bool Fast = false); 00130 00131 00132 00133 /// addModuleProvider - Add a ModuleProvider to the list of modules that we 00134 /// can JIT from. Note that this takes ownership of the ModuleProvider: when 00135 /// the ExecutionEngine is destroyed, it destroys the MP as well. 00136 virtual void addModuleProvider(ModuleProvider *P) { 00137 Modules.push_back(P); 00138 } 00139 00140 //===----------------------------------------------------------------------===// 00141 00142 const TargetData *getTargetData() const { return TD; } 00143 00144 00145 /// removeModuleProvider - Remove a ModuleProvider from the list of modules. 00146 /// Relases the Module from the ModuleProvider, materializing it in the 00147 /// process, and returns the materialized Module. 00148 virtual Module* removeModuleProvider(ModuleProvider *P, 00149 std::string *ErrInfo = 0); 00150 00151 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, 00152 /// and deletes the ModuleProvider and owned Module. Avoids materializing 00153 /// the underlying module. 00154 virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0); 00155 00156 /// FindFunctionNamed - Search all of the active modules to find the one that 00157 /// defines FnName. This is very slow operation and shouldn't be used for 00158 /// general code. 00159 Function *FindFunctionNamed(const char *FnName); 00160 00161 /// runFunction - Execute the specified function with the specified arguments, 00162 /// and return the result. 00163 /// 00164 virtual GenericValue runFunction(Function *F, 00165 const std::vector<GenericValue> &ArgValues) = 0; 00166 00167 /// runStaticConstructorsDestructors - This method is used to execute all of 00168 /// the static constructors or destructors for a program, depending on the 00169 /// value of isDtors. 00170 void runStaticConstructorsDestructors(bool isDtors); 00171 /// runStaticConstructorsDestructors - This method is used to execute all of 00172 /// the static constructors or destructors for a module, depending on the 00173 /// value of isDtors. 00174 void runStaticConstructorsDestructors(Module *module, bool isDtors); 00175 00176 00177 /// runFunctionAsMain - This is a helper function which wraps runFunction to 00178 /// handle the common task of starting up main with the specified argc, argv, 00179 /// and envp parameters. 00180 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 00181 const char * const * envp); 00182 00183 00184 /// addGlobalMapping - Tell the execution engine that the specified global is 00185 /// at the specified location. This is used internally as functions are JIT'd 00186 /// and as global variables are laid out in memory. It can and should also be 00187 /// used by clients of the EE that want to have an LLVM global overlay 00188 /// existing data in memory. 00189 void addGlobalMapping(const GlobalValue *GV, void *Addr); 00190 00191 /// clearAllGlobalMappings - Clear all global mappings and start over again 00192 /// use in dynamic compilation scenarios when you want to move globals 00193 void clearAllGlobalMappings(); 00194 00195 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 00196 /// particular module, because it has been removed from the JIT. 00197 void clearGlobalMappingsFromModule(Module *M); 00198 00199 /// updateGlobalMapping - Replace an existing mapping for GV with a new 00200 /// address. This updates both maps as required. If "Addr" is null, the 00201 /// entry for the global is removed from the mappings. This returns the old 00202 /// value of the pointer, or null if it was not in the map. 00203 void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 00204 00205 /// getPointerToGlobalIfAvailable - This returns the address of the specified 00206 /// global value if it is has already been codegen'd, otherwise it returns 00207 /// null. 00208 /// 00209 void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 00210 00211 /// getPointerToGlobal - This returns the address of the specified global 00212 /// value. This may involve code generation if it's a function. 00213 /// 00214 void *getPointerToGlobal(const GlobalValue *GV); 00215 00216 /// getPointerToFunction - The different EE's represent function bodies in 00217 /// different ways. They should each implement this to say what a function 00218 /// pointer should look like. 00219 /// 00220 virtual void *getPointerToFunction(Function *F) = 0; 00221 00222 /// getPointerToFunctionOrStub - If the specified function has been 00223 /// code-gen'd, return a pointer to the function. If not, compile it, or use 00224 /// a stub to implement lazy compilation if available. 00225 /// 00226 virtual void *getPointerToFunctionOrStub(Function *F) { 00227 // Default implementation, just codegen the function. 00228 return getPointerToFunction(F); 00229 } 00230 00231 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 00232 /// at the specified address. 00233 /// 00234 const GlobalValue *getGlobalValueAtAddress(void *Addr); 00235 00236 00237 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 00238 const Type *Ty); 00239 void InitializeMemory(const Constant *Init, void *Addr); 00240 00241 /// recompileAndRelinkFunction - This method is used to force a function 00242 /// which has already been compiled to be compiled again, possibly 00243 /// after it has been modified. Then the entry to the old copy is overwritten 00244 /// with a branch to the new copy. If there was no old copy, this acts 00245 /// just like VM::getPointerToFunction(). 00246 /// 00247 virtual void *recompileAndRelinkFunction(Function *F) = 0; 00248 00249 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 00250 /// corresponding to the machine code emitted to execute this function, useful 00251 /// for garbage-collecting generated code. 00252 /// 00253 virtual void freeMachineCodeForFunction(Function *F) = 0; 00254 00255 /// getOrEmitGlobalVariable - Return the address of the specified global 00256 /// variable, possibly emitting it to memory if needed. This is used by the 00257 /// Emitter. 00258 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 00259 return getPointerToGlobal((GlobalValue*)GV); 00260 } 00261 00262 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation 00263 /// is ever attempted. 00264 void DisableLazyCompilation(bool Disabled = true) { 00265 LazyCompilationDisabled = Disabled; 00266 } 00267 bool isLazyCompilationDisabled() const { 00268 return LazyCompilationDisabled; 00269 } 00270 00271 /// DisableGVCompilation - If called, the JIT will abort if it's asked to 00272 /// allocate space and populate a GlobalVariable that is not internal to 00273 /// the module. 00274 void DisableGVCompilation(bool Disabled = true) { 00275 GVCompilationDisabled = Disabled; 00276 } 00277 bool isGVCompilationDisabled() const { 00278 return GVCompilationDisabled; 00279 } 00280 00281 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 00282 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 00283 /// resolve symbols in a custom way. 00284 void DisableSymbolSearching(bool Disabled = true) { 00285 SymbolSearchingDisabled = Disabled; 00286 } 00287 bool isSymbolSearchingDisabled() const { 00288 return SymbolSearchingDisabled; 00289 } 00290 00291 00292 /// InstallLazyFunctionCreator - If an unknown function is needed, the 00293 /// specified function pointer is invoked to create it. If it returns null, 00294 /// the JIT will abort. 00295 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 00296 LazyFunctionCreator = P; 00297 } 00298 00299 /// InstallExceptionTableRegister - The JIT will use the given function 00300 /// to register the exception tables it generates. 00301 static void InstallExceptionTableRegister(void (*F)(void*)) { 00302 ExceptionTableRegister = F; 00303 } 00304 00305 /// RegisterTable - Registers the given pointer as an exception table. It uses 00306 /// the ExceptionTableRegister function. 00307 static void RegisterTable(void* res) { 00308 if (ExceptionTableRegister) 00309 ExceptionTableRegister(res); 00310 } 00311 00312 protected: 00313 explicit ExecutionEngine(ModuleProvider *P); 00314 00315 void emitGlobals(); 00316 00317 // EmitGlobalVariable - This method emits the specified global variable to the 00318 // address specified in GlobalAddresses, or allocates new memory if it's not 00319 // already in the map. 00320 void EmitGlobalVariable(const GlobalVariable *GV); 00321 00322 GenericValue getConstantValue(const Constant *C); 00323 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 00324 const Type *Ty); 00325 }; 00326 00327 } // End llvm namespace 00328 00329 #endif