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 00023 namespace llvm { 00024 00025 union GenericValue; 00026 class Constant; 00027 class Function; 00028 class GlobalVariable; 00029 class GlobalValue; 00030 class Module; 00031 class ModuleProvider; 00032 class TargetData; 00033 class Type; 00034 class IntrinsicLowering; 00035 00036 class ExecutionEngine { 00037 Module &CurMod; 00038 const TargetData *TD; 00039 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 protected: 00050 ModuleProvider *MP; 00051 00052 void setTargetData(const TargetData &td) { 00053 TD = &td; 00054 } 00055 00056 public: 00057 ExecutionEngine(ModuleProvider *P); 00058 ExecutionEngine(Module *M); 00059 virtual ~ExecutionEngine(); 00060 00061 Module &getModule() const { return CurMod; } 00062 const TargetData &getTargetData() const { return *TD; } 00063 00064 /// create - This is the factory method for creating an execution engine which 00065 /// is appropriate for the current machine. If specified, the 00066 /// IntrinsicLowering implementation should be allocated on the heap. 00067 static ExecutionEngine *create(ModuleProvider *MP, bool ForceInterpreter, 00068 IntrinsicLowering *IL = 0); 00069 00070 /// runFunction - Execute the specified function with the specified arguments, 00071 /// and return the result. 00072 /// 00073 virtual GenericValue runFunction(Function *F, 00074 const std::vector<GenericValue> &ArgValues) = 0; 00075 00076 /// runFunctionAsMain - This is a helper function which wraps runFunction to 00077 /// handle the common task of starting up main with the specified argc, argv, 00078 /// and envp parameters. 00079 int runFunctionAsMain(Function *Fn, const std::vector<std::string> &argv, 00080 const char * const * envp); 00081 00082 00083 void addGlobalMapping(const GlobalValue *GV, void *Addr) { 00084 void *&CurVal = GlobalAddressMap[GV]; 00085 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); 00086 CurVal = Addr; 00087 00088 // If we are using the reverse mapping, add it too 00089 if (!GlobalAddressReverseMap.empty()) { 00090 const GlobalValue *&V = GlobalAddressReverseMap[Addr]; 00091 assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 00092 V = GV; 00093 } 00094 } 00095 00096 /// updateGlobalMapping - Replace an existing mapping for GV with a new 00097 /// address. This updates both maps as required. 00098 void updateGlobalMapping(const GlobalValue *GV, void *Addr) { 00099 void *&CurVal = GlobalAddressMap[GV]; 00100 if (CurVal && !GlobalAddressReverseMap.empty()) 00101 GlobalAddressReverseMap.erase(CurVal); 00102 CurVal = Addr; 00103 00104 // If we are using the reverse mapping, add it too 00105 if (!GlobalAddressReverseMap.empty()) { 00106 const GlobalValue *&V = GlobalAddressReverseMap[Addr]; 00107 assert((V == 0 || GV == 0) && "GlobalMapping already established!"); 00108 V = GV; 00109 } 00110 } 00111 00112 /// getPointerToGlobalIfAvailable - This returns the address of the specified 00113 /// global value if it is available, otherwise it returns null. 00114 /// 00115 void *getPointerToGlobalIfAvailable(const GlobalValue *GV) { 00116 std::map<const GlobalValue*, void*>::iterator I = GlobalAddressMap.find(GV); 00117 return I != GlobalAddressMap.end() ? I->second : 0; 00118 } 00119 00120 /// getPointerToGlobal - This returns the address of the specified global 00121 /// value. This may involve code generation if it's a function. 00122 /// 00123 void *getPointerToGlobal(const GlobalValue *GV); 00124 00125 /// getPointerToFunction - The different EE's represent function bodies in 00126 /// different ways. They should each implement this to say what a function 00127 /// pointer should look like. 00128 /// 00129 virtual void *getPointerToFunction(Function *F) = 0; 00130 00131 /// getPointerToFunctionOrStub - If the specified function has been 00132 /// code-gen'd, return a pointer to the function. If not, compile it, or use 00133 /// a stub to implement lazy compilation if available. 00134 /// 00135 virtual void *getPointerToFunctionOrStub(Function *F) { 00136 // Default implementation, just codegen the function. 00137 return getPointerToFunction(F); 00138 } 00139 00140 /// getGlobalValueAtAddress - Return the LLVM global value object that starts 00141 /// at the specified address. 00142 /// 00143 const GlobalValue *getGlobalValueAtAddress(void *Addr); 00144 00145 00146 void StoreValueToMemory(GenericValue Val, GenericValue *Ptr, const Type *Ty); 00147 void InitializeMemory(const Constant *Init, void *Addr); 00148 00149 /// recompileAndRelinkFunction - This method is used to force a function 00150 /// which has already been compiled to be compiled again, possibly 00151 /// after it has been modified. Then the entry to the old copy is overwritten 00152 /// with a branch to the new copy. If there was no old copy, this acts 00153 /// just like VM::getPointerToFunction(). 00154 /// 00155 virtual void *recompileAndRelinkFunction(Function *F) = 0; 00156 00157 /// freeMachineCodeForFunction - Release memory in the ExecutionEngine 00158 /// corresponding to the machine code emitted to execute this function, useful 00159 /// for garbage-collecting generated code. 00160 /// 00161 virtual void freeMachineCodeForFunction(Function *F) = 0; 00162 00163 /// getOrEmitGlobalVariable - Return the address of the specified global 00164 /// variable, possibly emitting it to memory if needed. This is used by the 00165 /// Emitter. 00166 virtual void *getOrEmitGlobalVariable(const GlobalVariable *GV) { 00167 return getPointerToGlobal((GlobalValue*)GV); 00168 } 00169 00170 protected: 00171 void emitGlobals(); 00172 00173 // EmitGlobalVariable - This method emits the specified global variable to the 00174 // address specified in GlobalAddresses, or allocates new memory if it's not 00175 // already in the map. 00176 void EmitGlobalVariable(const GlobalVariable *GV); 00177 00178 GenericValue getConstantValue(const Constant *C); 00179 GenericValue LoadValueFromMemory(GenericValue *Ptr, const Type *Ty); 00180 }; 00181 00182 } // End llvm namespace 00183 00184 #endif