ExecutionEngine.h revision 193323
1//===- ExecutionEngine.h - Abstract Execution Engine Interface --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the abstract interface that implements execution support 11// for LLVM. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_EXECUTION_ENGINE_H 16#define LLVM_EXECUTION_ENGINE_H 17 18#include <vector> 19#include <map> 20#include <string> 21#include "llvm/ADT/SmallVector.h" 22#include "llvm/System/Mutex.h" 23#include "llvm/Target/TargetMachine.h" 24 25namespace llvm { 26 27struct GenericValue; 28class Constant; 29class Function; 30class GlobalVariable; 31class GlobalValue; 32class Module; 33class ModuleProvider; 34class TargetData; 35class Type; 36class MutexGuard; 37class JITMemoryManager; 38class MachineCodeInfo; 39 40class ExecutionEngineState { 41private: 42 /// GlobalAddressMap - A mapping between LLVM global values and their 43 /// actualized version... 44 std::map<const GlobalValue*, void *> GlobalAddressMap; 45 46 /// GlobalAddressReverseMap - This is the reverse mapping of GlobalAddressMap, 47 /// used to convert raw addresses into the LLVM global value that is emitted 48 /// at the address. This map is not computed unless getGlobalValueAtAddress 49 /// is called at some point. 50 std::map<void *, const GlobalValue*> GlobalAddressReverseMap; 51 52public: 53 std::map<const GlobalValue*, void *> & 54 getGlobalAddressMap(const MutexGuard &) { 55 return GlobalAddressMap; 56 } 57 58 std::map<void*, const GlobalValue*> & 59 getGlobalAddressReverseMap(const MutexGuard &) { 60 return GlobalAddressReverseMap; 61 } 62}; 63 64 65class ExecutionEngine { 66 const TargetData *TD; 67 ExecutionEngineState state; 68 bool LazyCompilationDisabled; 69 bool GVCompilationDisabled; 70 bool SymbolSearchingDisabled; 71 bool DlsymStubsEnabled; 72 73protected: 74 /// Modules - This is a list of ModuleProvider's that we are JIT'ing from. We 75 /// use a smallvector to optimize for the case where there is only one module. 76 SmallVector<ModuleProvider*, 1> Modules; 77 78 void setTargetData(const TargetData *td) { 79 TD = td; 80 } 81 82 /// getMemoryforGV - Allocate memory for a global variable. 83 virtual char* getMemoryForGV(const GlobalVariable* GV); 84 85 // To avoid having libexecutionengine depend on the JIT and interpreter 86 // libraries, the JIT and Interpreter set these functions to ctor pointers 87 // at startup time if they are linked in. 88 typedef ExecutionEngine *(*EECtorFn)(ModuleProvider*, std::string*, 89 CodeGenOpt::Level OptLevel); 90 static EECtorFn JITCtor, InterpCtor; 91 92 /// LazyFunctionCreator - If an unknown function is needed, this function 93 /// pointer is invoked to create it. If this returns null, the JIT will abort. 94 void* (*LazyFunctionCreator)(const std::string &); 95 96 /// ExceptionTableRegister - If Exception Handling is set, the JIT will 97 /// register dwarf tables with this function 98 typedef void (*EERegisterFn)(void*); 99 static EERegisterFn ExceptionTableRegister; 100 101public: 102 /// lock - This lock is protects the ExecutionEngine, JIT, JITResolver and 103 /// JITEmitter classes. It must be held while changing the internal state of 104 /// any of those classes. 105 sys::Mutex lock; // Used to make this class and subclasses thread-safe 106 107 //===--------------------------------------------------------------------===// 108 // ExecutionEngine Startup 109 //===--------------------------------------------------------------------===// 110 111 virtual ~ExecutionEngine(); 112 113 /// create - This is the factory method for creating an execution engine which 114 /// is appropriate for the current machine. This takes ownership of the 115 /// module provider. 116 static ExecutionEngine *create(ModuleProvider *MP, 117 bool ForceInterpreter = false, 118 std::string *ErrorStr = 0, 119 CodeGenOpt::Level OptLevel = 120 CodeGenOpt::Default); 121 122 /// create - This is the factory method for creating an execution engine which 123 /// is appropriate for the current machine. This takes ownership of the 124 /// module. 125 static ExecutionEngine *create(Module *M); 126 127 /// createJIT - This is the factory method for creating a JIT for the current 128 /// machine, it does not fall back to the interpreter. This takes ownership 129 /// of the ModuleProvider and JITMemoryManager if successful. 130 static ExecutionEngine *createJIT(ModuleProvider *MP, 131 std::string *ErrorStr = 0, 132 JITMemoryManager *JMM = 0, 133 CodeGenOpt::Level OptLevel = 134 CodeGenOpt::Default); 135 136 /// addModuleProvider - Add a ModuleProvider to the list of modules that we 137 /// can JIT from. Note that this takes ownership of the ModuleProvider: when 138 /// the ExecutionEngine is destroyed, it destroys the MP as well. 139 virtual void addModuleProvider(ModuleProvider *P) { 140 Modules.push_back(P); 141 } 142 143 //===----------------------------------------------------------------------===// 144 145 const TargetData *getTargetData() const { return TD; } 146 147 148 /// removeModuleProvider - Remove a ModuleProvider from the list of modules. 149 /// Relases the Module from the ModuleProvider, materializing it in the 150 /// process, and returns the materialized Module. 151 virtual Module* removeModuleProvider(ModuleProvider *P, 152 std::string *ErrInfo = 0); 153 154 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules, 155 /// and deletes the ModuleProvider and owned Module. Avoids materializing 156 /// the underlying module. 157 virtual void deleteModuleProvider(ModuleProvider *P,std::string *ErrInfo = 0); 158 159 /// FindFunctionNamed - Search all of the active modules to find the one that 160 /// defines FnName. This is very slow operation and shouldn't be used for 161 /// general code. 162 Function *FindFunctionNamed(const char *FnName); 163 164 /// runFunction - Execute the specified function with the specified arguments, 165 /// and return the result. 166 /// 167 virtual GenericValue runFunction(Function *F, 168 const std::vector<GenericValue> &ArgValues) = 0; 169 170 /// runStaticConstructorsDestructors - This method is used to execute all of 171 /// the static constructors or destructors for a program, depending on the 172 /// value of isDtors. 173 void runStaticConstructorsDestructors(bool isDtors); 174 /// runStaticConstructorsDestructors - This method is used to execute all of 175 /// the static constructors or destructors for a module, depending on the 176 /// value of isDtors. 177 void runStaticConstructorsDestructors(Module *module, bool isDtors); 178 179 180 /// runFunctionAsMain - This is a helper function which wraps runFunction to 181 /// handle the common task of starting up main with the specified argc, argv, 182 /// and envp parameters. 183 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 184 const char * const * envp); 185 186 187 /// addGlobalMapping - Tell the execution engine that the specified global is 188 /// at the specified location. This is used internally as functions are JIT'd 189 /// and as global variables are laid out in memory. It can and should also be 190 /// used by clients of the EE that want to have an LLVM global overlay 191 /// existing data in memory. After adding a mapping for GV, you must not 192 /// destroy it until you've removed the mapping. 193 void addGlobalMapping(const GlobalValue *GV, void *Addr); 194 195 /// clearAllGlobalMappings - Clear all global mappings and start over again 196 /// use in dynamic compilation scenarios when you want to move globals 197 void clearAllGlobalMappings(); 198 199 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a 200 /// particular module, because it has been removed from the JIT. 201 void clearGlobalMappingsFromModule(Module *M); 202 203 /// updateGlobalMapping - Replace an existing mapping for GV with a new 204 /// address. This updates both maps as required. If "Addr" is null, the 205 /// entry for the global is removed from the mappings. This returns the old 206 /// value of the pointer, or null if it was not in the map. 207 void *updateGlobalMapping(const GlobalValue *GV, void *Addr); 208 209 /// getPointerToGlobalIfAvailable - This returns the address of the specified 210 /// global value if it is has already been codegen'd, otherwise it returns 211 /// null. 212 /// 213 void *getPointerToGlobalIfAvailable(const GlobalValue *GV); 214 215 /// getPointerToGlobal - This returns the address of the specified global 216 /// value. This may involve code generation if it's a function. After 217 /// getting a pointer to GV, it and all globals it transitively refers to have 218 /// been passed to addGlobalMapping. You must clear the mapping for each 219 /// referred-to global before destroying it. If a referred-to global RTG is a 220 /// function and this ExecutionEngine is a JIT compiler, calling 221 /// updateGlobalMapping(RTG, 0) will leak the function's machine code, so you 222 /// should call freeMachineCodeForFunction(RTG) instead. Note that 223 /// optimizations can move and delete non-external GlobalValues without 224 /// notifying the ExecutionEngine. 225 /// 226 void *getPointerToGlobal(const GlobalValue *GV); 227 228 /// getPointerToFunction - The different EE's represent function bodies in 229 /// different ways. They should each implement this to say what a function 230 /// pointer should look like. See getPointerToGlobal for the requirements on 231 /// destroying F and any GlobalValues it refers to. 232 /// 233 virtual void *getPointerToFunction(Function *F) = 0; 234 235 /// getPointerToFunctionOrStub - If the specified function has been 236 /// code-gen'd, return a pointer to the function. If not, compile it, or use 237 /// a stub to implement lazy compilation if available. See getPointerToGlobal 238 /// for the requirements on destroying F and any GlobalValues it refers to. 239 /// 240 virtual void *getPointerToFunctionOrStub(Function *F) { 241 // Default implementation, just codegen the function. 242 return getPointerToFunction(F); 243 } 244 245 // The JIT overrides a version that actually does this. 246 virtual void runJITOnFunction(Function *F, MachineCodeInfo *MCI = 0) { } 247 248 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 249 /// at the specified address. 250 /// 251 const GlobalValue *getGlobalValueAtAddress(void *Addr); 252 253 254 void StoreValueToMemory(const GenericValue &Val, GenericValue *Ptr, 255 const Type *Ty); 256 void InitializeMemory(const Constant *Init, void *Addr); 257 258 /// recompileAndRelinkFunction - This method is used to force a function 259 /// which has already been compiled to be compiled again, possibly 260 /// after it has been modified. Then the entry to the old copy is overwritten 261 /// with a branch to the new copy. If there was no old copy, this acts 262 /// just like VM::getPointerToFunction(). 263 /// 264 virtual void *recompileAndRelinkFunction(Function *F) = 0; 265 266 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 267 /// corresponding to the machine code emitted to execute this function, useful 268 /// for garbage-collecting generated code. 269 /// 270 virtual void freeMachineCodeForFunction(Function *F) = 0; 271 272 /// getOrEmitGlobalVariable - Return the address of the specified global 273 /// variable, possibly emitting it to memory if needed. This is used by the 274 /// Emitter. See getPointerToGlobal for the requirements on destroying GV and 275 /// any GlobalValues it refers to. 276 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 277 return getPointerToGlobal((GlobalValue*)GV); 278 } 279 280 /// DisableLazyCompilation - If called, the JIT will abort if lazy compilation 281 /// is ever attempted. 282 void DisableLazyCompilation(bool Disabled = true) { 283 LazyCompilationDisabled = Disabled; 284 } 285 bool isLazyCompilationDisabled() const { 286 return LazyCompilationDisabled; 287 } 288 289 /// DisableGVCompilation - If called, the JIT will abort if it's asked to 290 /// allocate space and populate a GlobalVariable that is not internal to 291 /// the module. 292 void DisableGVCompilation(bool Disabled = true) { 293 GVCompilationDisabled = Disabled; 294 } 295 bool isGVCompilationDisabled() const { 296 return GVCompilationDisabled; 297 } 298 299 /// DisableSymbolSearching - If called, the JIT will not try to lookup unknown 300 /// symbols with dlsym. A client can still use InstallLazyFunctionCreator to 301 /// resolve symbols in a custom way. 302 void DisableSymbolSearching(bool Disabled = true) { 303 SymbolSearchingDisabled = Disabled; 304 } 305 bool isSymbolSearchingDisabled() const { 306 return SymbolSearchingDisabled; 307 } 308 309 /// EnableDlsymStubs - 310 void EnableDlsymStubs(bool Enabled = true) { 311 DlsymStubsEnabled = Enabled; 312 } 313 bool areDlsymStubsEnabled() const { 314 return DlsymStubsEnabled; 315 } 316 317 /// InstallLazyFunctionCreator - If an unknown function is needed, the 318 /// specified function pointer is invoked to create it. If it returns null, 319 /// the JIT will abort. 320 void InstallLazyFunctionCreator(void* (*P)(const std::string &)) { 321 LazyFunctionCreator = P; 322 } 323 324 /// InstallExceptionTableRegister - The JIT will use the given function 325 /// to register the exception tables it generates. 326 static void InstallExceptionTableRegister(void (*F)(void*)) { 327 ExceptionTableRegister = F; 328 } 329 330 /// RegisterTable - Registers the given pointer as an exception table. It uses 331 /// the ExceptionTableRegister function. 332 static void RegisterTable(void* res) { 333 if (ExceptionTableRegister) 334 ExceptionTableRegister(res); 335 } 336 337protected: 338 explicit ExecutionEngine(ModuleProvider *P); 339 340 void emitGlobals(); 341 342 // EmitGlobalVariable - This method emits the specified global variable to the 343 // address specified in GlobalAddresses, or allocates new memory if it's not 344 // already in the map. 345 void EmitGlobalVariable(const GlobalVariable *GV); 346 347 GenericValue getConstantValue(const Constant *C); 348 void LoadValueFromMemory(GenericValue &Result, GenericValue *Ptr, 349 const Type *Ty); 350}; 351 352} // End llvm namespace 353 354#endif 355