Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 1 | //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 10 | // This file defines the common interface used by the various execution engine |
| 11 | // subclasses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 3785fad | 2003-08-05 17:00:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Statistic.h" |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Chris Lattner | fd13129 | 2003-09-05 20:08:15 +0000 | [diff] [blame] | 22 | #include "llvm/ExecutionEngine/GenericValue.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MutexGuard.h" |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 25 | #include "llvm/System/DynamicLibrary.h" |
| 26 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 27 | #include <iostream> |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 29 | |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 32 | Statistic<> NumGlobals ("lli", "Number of global vars initialized"); |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 33 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 35 | ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; |
| 36 | ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; |
| 37 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 38 | ExecutionEngine::ExecutionEngine(ModuleProvider *P) { |
| 39 | Modules.push_back(P); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 40 | assert(P && "ModuleProvider is null?"); |
| 41 | } |
| 42 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 43 | ExecutionEngine::ExecutionEngine(Module *M) { |
Misha Brukman | 0570157 | 2003-10-17 18:31:59 +0000 | [diff] [blame] | 44 | assert(M && "Module is null?"); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 45 | Modules.push_back(new ExistingModuleProvider(M)); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 48 | ExecutionEngine::~ExecutionEngine() { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 49 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 50 | delete Modules[i]; |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 53 | /// FindFunctionNamed - Search all of the active modules to find the one that |
| 54 | /// defines FnName. This is very slow operation and shouldn't be used for |
| 55 | /// general code. |
| 56 | Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { |
| 57 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
| 58 | if (Function *F = Modules[i]->getModule()->getNamedFunction(FnName)) |
| 59 | return F; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 65 | /// addGlobalMapping - Tell the execution engine that the specified global is |
| 66 | /// at the specified location. This is used internally as functions are JIT'd |
| 67 | /// and as global variables are laid out in memory. It can and should also be |
| 68 | /// used by clients of the EE that want to have an LLVM global overlay |
| 69 | /// existing data in memory. |
| 70 | void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 71 | MutexGuard locked(lock); |
| 72 | |
| 73 | void *&CurVal = state.getGlobalAddressMap(locked)[GV]; |
| 74 | assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); |
| 75 | CurVal = Addr; |
| 76 | |
| 77 | // If we are using the reverse mapping, add it too |
| 78 | if (!state.getGlobalAddressReverseMap(locked).empty()) { |
| 79 | const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; |
| 80 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 81 | V = GV; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /// clearAllGlobalMappings - Clear all global mappings and start over again |
| 86 | /// use in dynamic compilation scenarios when you want to move globals |
| 87 | void ExecutionEngine::clearAllGlobalMappings() { |
| 88 | MutexGuard locked(lock); |
| 89 | |
| 90 | state.getGlobalAddressMap(locked).clear(); |
| 91 | state.getGlobalAddressReverseMap(locked).clear(); |
| 92 | } |
| 93 | |
| 94 | /// updateGlobalMapping - Replace an existing mapping for GV with a new |
| 95 | /// address. This updates both maps as required. If "Addr" is null, the |
| 96 | /// entry for the global is removed from the mappings. |
| 97 | void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 98 | MutexGuard locked(lock); |
| 99 | |
| 100 | // Deleting from the mapping? |
| 101 | if (Addr == 0) { |
| 102 | state.getGlobalAddressMap(locked).erase(GV); |
| 103 | if (!state.getGlobalAddressReverseMap(locked).empty()) |
| 104 | state.getGlobalAddressReverseMap(locked).erase(Addr); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | void *&CurVal = state.getGlobalAddressMap(locked)[GV]; |
| 109 | if (CurVal && !state.getGlobalAddressReverseMap(locked).empty()) |
| 110 | state.getGlobalAddressReverseMap(locked).erase(CurVal); |
| 111 | CurVal = Addr; |
| 112 | |
| 113 | // If we are using the reverse mapping, add it too |
| 114 | if (!state.getGlobalAddressReverseMap(locked).empty()) { |
| 115 | const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; |
| 116 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 117 | V = GV; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /// getPointerToGlobalIfAvailable - This returns the address of the specified |
| 122 | /// global value if it is has already been codegen'd, otherwise it returns null. |
| 123 | /// |
| 124 | void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { |
| 125 | MutexGuard locked(lock); |
| 126 | |
| 127 | std::map<const GlobalValue*, void*>::iterator I = |
| 128 | state.getGlobalAddressMap(locked).find(GV); |
| 129 | return I != state.getGlobalAddressMap(locked).end() ? I->second : 0; |
| 130 | } |
| 131 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 132 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 133 | /// at the specified address. |
| 134 | /// |
| 135 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 136 | MutexGuard locked(lock); |
| 137 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 138 | // If we haven't computed the reverse mapping yet, do so first. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 139 | if (state.getGlobalAddressReverseMap(locked).empty()) { |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 140 | for (std::map<const GlobalValue*, void *>::iterator |
| 141 | I = state.getGlobalAddressMap(locked).begin(), |
| 142 | E = state.getGlobalAddressMap(locked).end(); I != E; ++I) |
| 143 | state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, |
| 144 | I->first)); |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | std::map<void *, const GlobalValue*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 148 | state.getGlobalAddressReverseMap(locked).find(Addr); |
| 149 | return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 151 | |
| 152 | // CreateArgv - Turn a vector of strings into a nice argv style array of |
| 153 | // pointers to null terminated strings. |
| 154 | // |
| 155 | static void *CreateArgv(ExecutionEngine *EE, |
| 156 | const std::vector<std::string> &InputArgv) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 157 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 158 | char *Result = new char[(InputArgv.size()+1)*PtrSize]; |
| 159 | |
| 160 | DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); |
| 161 | const Type *SBytePtr = PointerType::get(Type::SByteTy); |
| 162 | |
| 163 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 164 | unsigned Size = InputArgv[i].size()+1; |
| 165 | char *Dest = new char[Size]; |
| 166 | DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 168 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 169 | Dest[Size-1] = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 171 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 172 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), |
| 173 | SBytePtr); |
| 174 | } |
| 175 | |
| 176 | // Null terminate it |
| 177 | EE->StoreValueToMemory(PTOGV(0), |
| 178 | (GenericValue*)(Result+InputArgv.size()*PtrSize), |
| 179 | SBytePtr); |
| 180 | return Result; |
| 181 | } |
| 182 | |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 183 | |
| 184 | /// runStaticConstructorsDestructors - This method is used to execute all of |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 185 | /// the static constructors or destructors for a program, depending on the |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 186 | /// value of isDtors. |
| 187 | void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { |
| 188 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 189 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 190 | // Execute global ctors/dtors for each module in the program. |
| 191 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
| 192 | GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name); |
| 193 | |
| 194 | // If this global has internal linkage, or if it has a use, then it must be |
| 195 | // an old-style (llvmgcc3) static ctor with __main linked in and in use. If |
| 196 | // this is the case, don't execute any of the global ctors, __main will do |
| 197 | // it. |
| 198 | if (!GV || GV->isExternal() || GV->hasInternalLinkage()) continue; |
| 199 | |
| 200 | // Should be an array of '{ int, void ()* }' structs. The first value is |
| 201 | // the init priority, which we ignore. |
| 202 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 203 | if (!InitList) continue; |
| 204 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 205 | if (ConstantStruct *CS = |
| 206 | dyn_cast<ConstantStruct>(InitList->getOperand(i))) { |
| 207 | if (CS->getNumOperands() != 2) break; // Not array of 2-element structs. |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 208 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 209 | Constant *FP = CS->getOperand(1); |
| 210 | if (FP->isNullValue()) |
| 211 | break; // Found a null terminator, exit. |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 212 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 213 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 214 | if (CE->getOpcode() == Instruction::Cast) |
| 215 | FP = CE->getOperand(0); |
| 216 | if (Function *F = dyn_cast<Function>(FP)) { |
| 217 | // Execute the ctor/dtor function! |
| 218 | runFunction(F, std::vector<GenericValue>()); |
| 219 | } |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 221 | } |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 224 | /// runFunctionAsMain - This is a helper function which wraps runFunction to |
| 225 | /// handle the common task of starting up main with the specified argc, argv, |
| 226 | /// and envp parameters. |
| 227 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 228 | const std::vector<std::string> &argv, |
| 229 | const char * const * envp) { |
| 230 | std::vector<GenericValue> GVArgs; |
| 231 | GenericValue GVArgc; |
| 232 | GVArgc.IntVal = argv.size(); |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 233 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
| 234 | if (NumArgs) { |
| 235 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 236 | if (NumArgs > 1) { |
| 237 | GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. |
| 238 | assert(((char **)GVTOP(GVArgs[1]))[0] && |
| 239 | "argv[0] was null after CreateArgv"); |
| 240 | if (NumArgs > 2) { |
| 241 | std::vector<std::string> EnvVars; |
| 242 | for (unsigned i = 0; envp[i]; ++i) |
| 243 | EnvVars.push_back(envp[i]); |
| 244 | GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. |
| 245 | } |
| 246 | } |
| 247 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 248 | return runFunction(Fn, GVArgs).IntVal; |
| 249 | } |
| 250 | |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 251 | /// If possible, create a JIT, unless the caller specifically requests an |
| 252 | /// Interpreter or there's an error. If even an Interpreter cannot be created, |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 253 | /// NULL is returned. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 254 | /// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 255 | ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 256 | bool ForceInterpreter) { |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 257 | ExecutionEngine *EE = 0; |
| 258 | |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 259 | // Unless the interpreter was explicitly selected, try making a JIT. |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 260 | if (!ForceInterpreter && JITCtor) |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 261 | EE = JITCtor(MP); |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 262 | |
| 263 | // If we can't make a JIT, make an interpreter instead. |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 264 | if (EE == 0 && InterpCtor) |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 265 | EE = InterpCtor(MP); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 266 | |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 267 | if (EE) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 268 | // Make sure we can resolve symbols in the program as well. The zero arg |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 269 | // to the function tells DynamicLibrary to load the program, not a library. |
Chris Lattner | 6f3ada5 | 2006-05-14 19:01:55 +0000 | [diff] [blame] | 270 | try { |
| 271 | sys::DynamicLibrary::LoadLibraryPermanently(0); |
| 272 | } catch (...) { |
| 273 | } |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 274 | } |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 275 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 276 | return EE; |
| 277 | } |
| 278 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 279 | /// getPointerToGlobal - This returns the address of the specified global |
| 280 | /// value. This may involve code generation if it's a function. |
| 281 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 282 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
Brian Gaeke | 37df460 | 2003-08-13 18:16:14 +0000 | [diff] [blame] | 283 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 284 | return getPointerToFunction(F); |
| 285 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 286 | MutexGuard locked(lock); |
Jeff Cohen | 68835dd | 2006-02-07 05:11:57 +0000 | [diff] [blame] | 287 | void *p = state.getGlobalAddressMap(locked)[GV]; |
| 288 | if (p) |
| 289 | return p; |
| 290 | |
| 291 | // Global variable might have been added since interpreter started. |
| 292 | if (GlobalVariable *GVar = |
| 293 | const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) |
| 294 | EmitGlobalVariable(GVar); |
| 295 | else |
| 296 | assert("Global hasn't had an address allocated yet!"); |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 297 | return state.getGlobalAddressMap(locked)[GV]; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 300 | /// FIXME: document |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 301 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 302 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
| 303 | GenericValue Result; |
Chris Lattner | 6f335f9 | 2004-10-26 05:35:14 +0000 | [diff] [blame] | 304 | if (isa<UndefValue>(C)) return Result; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 305 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 306 | if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) { |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 307 | switch (CE->getOpcode()) { |
| 308 | case Instruction::GetElementPtr: { |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 309 | Result = getConstantValue(CE->getOperand(0)); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 310 | std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); |
| 311 | uint64_t Offset = |
| 312 | TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 313 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 314 | if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | 3db4b62 | 2005-10-23 23:54:56 +0000 | [diff] [blame] | 315 | Result.IntVal += Offset; |
| 316 | else |
| 317 | Result.LongVal += Offset; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 318 | return Result; |
| 319 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 320 | case Instruction::Cast: { |
| 321 | // We only need to handle a few cases here. Almost all casts will |
| 322 | // automatically fold, just the ones involving pointers won't. |
| 323 | // |
| 324 | Constant *Op = CE->getOperand(0); |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 325 | GenericValue GV = getConstantValue(Op); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 326 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 327 | // Handle cast of pointer to pointer... |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 328 | if (Op->getType()->getTypeID() == C->getType()->getTypeID()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 329 | return GV; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 74cf819 | 2003-08-18 17:23:40 +0000 | [diff] [blame] | 331 | // Handle a cast of pointer to any integral type... |
Chris Lattner | c88a4ea | 2003-08-18 17:33:15 +0000 | [diff] [blame] | 332 | if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 333 | return GV; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 334 | |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 335 | // Handle cast of integer to a pointer... |
| 336 | if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral()) |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 337 | switch (Op->getType()->getTypeID()) { |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 338 | case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal); |
| 339 | case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal); |
| 340 | case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal); |
| 341 | case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal); |
| 342 | case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal); |
| 343 | case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal); |
| 344 | case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal); |
| 345 | case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal); |
| 346 | case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal); |
| 347 | default: assert(0 && "Unknown integral type!"); |
| 348 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 349 | break; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 350 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 351 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 352 | case Instruction::Add: |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 353 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
| 354 | default: assert(0 && "Bad add type!"); abort(); |
| 355 | case Type::LongTyID: |
| 356 | case Type::ULongTyID: |
Chris Lattner | 6b2125c | 2003-05-14 17:53:49 +0000 | [diff] [blame] | 357 | Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal + |
| 358 | getConstantValue(CE->getOperand(1)).LongVal; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 359 | break; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 360 | case Type::IntTyID: |
| 361 | case Type::UIntTyID: |
| 362 | Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal + |
| 363 | getConstantValue(CE->getOperand(1)).IntVal; |
| 364 | break; |
| 365 | case Type::ShortTyID: |
| 366 | case Type::UShortTyID: |
| 367 | Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal + |
| 368 | getConstantValue(CE->getOperand(1)).ShortVal; |
| 369 | break; |
| 370 | case Type::SByteTyID: |
| 371 | case Type::UByteTyID: |
| 372 | Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal + |
| 373 | getConstantValue(CE->getOperand(1)).SByteVal; |
| 374 | break; |
| 375 | case Type::FloatTyID: |
| 376 | Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + |
| 377 | getConstantValue(CE->getOperand(1)).FloatVal; |
| 378 | break; |
| 379 | case Type::DoubleTyID: |
| 380 | Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + |
| 381 | getConstantValue(CE->getOperand(1)).DoubleVal; |
| 382 | break; |
| 383 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 384 | return Result; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 385 | default: |
| 386 | break; |
| 387 | } |
| 388 | std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; |
| 389 | abort(); |
| 390 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 391 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 392 | switch (C->getType()->getTypeID()) { |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 393 | #define GET_CONST_VAL(TY, CTY, CLASS) \ |
| 394 | case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break |
| 395 | GET_CONST_VAL(Bool , bool , ConstantBool); |
| 396 | GET_CONST_VAL(UByte , unsigned char , ConstantUInt); |
| 397 | GET_CONST_VAL(SByte , signed char , ConstantSInt); |
| 398 | GET_CONST_VAL(UShort , unsigned short, ConstantUInt); |
| 399 | GET_CONST_VAL(Short , signed short , ConstantSInt); |
| 400 | GET_CONST_VAL(UInt , unsigned int , ConstantUInt); |
| 401 | GET_CONST_VAL(Int , signed int , ConstantSInt); |
Chris Lattner | 4b3141d | 2005-05-12 06:01:28 +0000 | [diff] [blame] | 402 | GET_CONST_VAL(ULong , uint64_t , ConstantUInt); |
| 403 | GET_CONST_VAL(Long , int64_t , ConstantSInt); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 404 | GET_CONST_VAL(Float , float , ConstantFP); |
| 405 | GET_CONST_VAL(Double , double , ConstantFP); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 406 | #undef GET_CONST_VAL |
| 407 | case Type::PointerTyID: |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 408 | if (isa<ConstantPointerNull>(C)) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 409 | Result.PointerVal = 0; |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 410 | else if (const Function *F = dyn_cast<Function>(C)) |
| 411 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
| 412 | else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) |
| 413 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
| 414 | else |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 415 | assert(0 && "Unknown constant pointer type!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 416 | break; |
| 417 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 418 | std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 419 | abort(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 420 | } |
| 421 | return Result; |
| 422 | } |
| 423 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 424 | /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr |
| 425 | /// is the address of the memory at which to store Val, cast to GenericValue *. |
| 426 | /// It is not a pointer to a GenericValue containing the address at which to |
| 427 | /// store Val. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 428 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 429 | void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 430 | const Type *Ty) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 431 | if (getTargetData()->isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 432 | switch (Ty->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 433 | case Type::BoolTyID: |
| 434 | case Type::UByteTyID: |
| 435 | case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; |
| 436 | case Type::UShortTyID: |
| 437 | case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; |
| 438 | Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; |
| 439 | break; |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 440 | Store4BytesLittleEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 441 | case Type::FloatTyID: |
| 442 | case Type::UIntTyID: |
| 443 | case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; |
| 444 | Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; |
| 445 | Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; |
| 446 | Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; |
| 447 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 448 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 449 | goto Store4BytesLittleEndian; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 450 | case Type::DoubleTyID: |
| 451 | case Type::ULongTyID: |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 452 | case Type::LongTyID: |
| 453 | Ptr->Untyped[0] = (unsigned char)(Val.ULongVal ); |
| 454 | Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8); |
| 455 | Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16); |
| 456 | Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24); |
| 457 | Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32); |
| 458 | Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40); |
| 459 | Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48); |
| 460 | Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56); |
| 461 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 462 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 463 | std::cout << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 464 | } |
| 465 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 466 | switch (Ty->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 467 | case Type::BoolTyID: |
| 468 | case Type::UByteTyID: |
| 469 | case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; |
| 470 | case Type::UShortTyID: |
| 471 | case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; |
| 472 | Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; |
| 473 | break; |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 474 | Store4BytesBigEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 475 | case Type::FloatTyID: |
| 476 | case Type::UIntTyID: |
| 477 | case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; |
| 478 | Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; |
| 479 | Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; |
| 480 | Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; |
| 481 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 482 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 483 | goto Store4BytesBigEndian; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 484 | case Type::DoubleTyID: |
| 485 | case Type::ULongTyID: |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 486 | case Type::LongTyID: |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 487 | Ptr->Untyped[7] = (unsigned char)(Val.ULongVal ); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 488 | Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8); |
| 489 | Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16); |
| 490 | Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24); |
| 491 | Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32); |
| 492 | Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40); |
| 493 | Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48); |
| 494 | Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56); |
| 495 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 496 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 497 | std::cout << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 502 | /// FIXME: document |
| 503 | /// |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 504 | GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, |
| 505 | const Type *Ty) { |
| 506 | GenericValue Result; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 507 | if (getTargetData()->isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 508 | switch (Ty->getTypeID()) { |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 509 | case Type::BoolTyID: |
| 510 | case Type::UByteTyID: |
| 511 | case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; |
| 512 | case Type::UShortTyID: |
| 513 | case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | |
| 514 | ((unsigned)Ptr->Untyped[1] << 8); |
| 515 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 516 | Load4BytesLittleEndian: |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 517 | case Type::FloatTyID: |
| 518 | case Type::UIntTyID: |
| 519 | case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | |
| 520 | ((unsigned)Ptr->Untyped[1] << 8) | |
| 521 | ((unsigned)Ptr->Untyped[2] << 16) | |
| 522 | ((unsigned)Ptr->Untyped[3] << 24); |
| 523 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 524 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 525 | goto Load4BytesLittleEndian; |
| 526 | case Type::DoubleTyID: |
| 527 | case Type::ULongTyID: |
| 528 | case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | |
| 529 | ((uint64_t)Ptr->Untyped[1] << 8) | |
| 530 | ((uint64_t)Ptr->Untyped[2] << 16) | |
| 531 | ((uint64_t)Ptr->Untyped[3] << 24) | |
| 532 | ((uint64_t)Ptr->Untyped[4] << 32) | |
| 533 | ((uint64_t)Ptr->Untyped[5] << 40) | |
| 534 | ((uint64_t)Ptr->Untyped[6] << 48) | |
| 535 | ((uint64_t)Ptr->Untyped[7] << 56); |
| 536 | break; |
| 537 | default: |
| 538 | std::cout << "Cannot load value of type " << *Ty << "!\n"; |
| 539 | abort(); |
| 540 | } |
| 541 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 542 | switch (Ty->getTypeID()) { |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 543 | case Type::BoolTyID: |
| 544 | case Type::UByteTyID: |
| 545 | case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; |
| 546 | case Type::UShortTyID: |
| 547 | case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | |
| 548 | ((unsigned)Ptr->Untyped[0] << 8); |
| 549 | break; |
| 550 | Load4BytesBigEndian: |
| 551 | case Type::FloatTyID: |
| 552 | case Type::UIntTyID: |
| 553 | case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | |
| 554 | ((unsigned)Ptr->Untyped[2] << 8) | |
| 555 | ((unsigned)Ptr->Untyped[1] << 16) | |
| 556 | ((unsigned)Ptr->Untyped[0] << 24); |
| 557 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 558 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 559 | goto Load4BytesBigEndian; |
| 560 | case Type::DoubleTyID: |
| 561 | case Type::ULongTyID: |
| 562 | case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | |
| 563 | ((uint64_t)Ptr->Untyped[6] << 8) | |
| 564 | ((uint64_t)Ptr->Untyped[5] << 16) | |
| 565 | ((uint64_t)Ptr->Untyped[4] << 24) | |
| 566 | ((uint64_t)Ptr->Untyped[3] << 32) | |
| 567 | ((uint64_t)Ptr->Untyped[2] << 40) | |
| 568 | ((uint64_t)Ptr->Untyped[1] << 48) | |
| 569 | ((uint64_t)Ptr->Untyped[0] << 56); |
| 570 | break; |
| 571 | default: |
| 572 | std::cout << "Cannot load value of type " << *Ty << "!\n"; |
| 573 | abort(); |
| 574 | } |
| 575 | } |
| 576 | return Result; |
| 577 | } |
| 578 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 579 | // InitializeMemory - Recursive function to apply a Constant value into the |
| 580 | // specified memory location... |
| 581 | // |
| 582 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 583 | if (isa<UndefValue>(Init)) { |
| 584 | return; |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 585 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) { |
| 586 | unsigned ElementSize = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 587 | getTargetData()->getTypeSize(CP->getType()->getElementType()); |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 588 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 589 | InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); |
| 590 | return; |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 591 | } else if (Init->getType()->isFirstClassType()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 592 | GenericValue Val = getConstantValue(Init); |
| 593 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 594 | return; |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 595 | } else if (isa<ConstantAggregateZero>(Init)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 596 | memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType())); |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 597 | return; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 600 | switch (Init->getType()->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 601 | case Type::ArrayTyID: { |
| 602 | const ConstantArray *CPA = cast<ConstantArray>(Init); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 603 | unsigned ElementSize = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 604 | getTargetData()->getTypeSize(CPA->getType()->getElementType()); |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 605 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 606 | InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
| 609 | |
| 610 | case Type::StructTyID: { |
| 611 | const ConstantStruct *CPS = cast<ConstantStruct>(Init); |
| 612 | const StructLayout *SL = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 613 | getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 614 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
| 615 | InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 616 | return; |
| 617 | } |
| 618 | |
| 619 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 620 | std::cerr << "Bad Type: " << *Init->getType() << "\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 621 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 622 | } |
| 623 | } |
| 624 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 625 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 626 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 627 | /// their initializers into the memory. |
| 628 | /// |
| 629 | void ExecutionEngine::emitGlobals() { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 630 | const TargetData *TD = getTargetData(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 631 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 632 | // Loop over all of the global variables in the program, allocating the memory |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 633 | // to hold them. If there is more than one module, do a prepass over globals |
| 634 | // to figure out how the different modules should link together. |
| 635 | // |
| 636 | std::map<std::pair<std::string, const Type*>, |
| 637 | const GlobalValue*> LinkedGlobalsMap; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 638 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 639 | if (Modules.size() != 1) { |
| 640 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
| 641 | Module &M = *Modules[m]->getModule(); |
| 642 | for (Module::const_global_iterator I = M.global_begin(), |
| 643 | E = M.global_end(); I != E; ++I) { |
| 644 | const GlobalValue *GV = I; |
| 645 | if (GV->hasInternalLinkage() || GV->isExternal() || |
| 646 | GV->hasAppendingLinkage() || !GV->hasName()) |
| 647 | continue;// Ignore external globals and globals with internal linkage. |
| 648 | |
| 649 | const GlobalValue *&GVEntry = |
| 650 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 651 | |
| 652 | // If this is the first time we've seen this global, it is the canonical |
| 653 | // version. |
| 654 | if (!GVEntry) { |
| 655 | GVEntry = GV; |
| 656 | continue; |
| 657 | } |
| 658 | |
| 659 | // If the existing global is strong, never replace it. |
| 660 | if (GVEntry->hasExternalLinkage()) |
| 661 | continue; |
| 662 | |
| 663 | // Otherwise, we know it's linkonce/weak, replace it if this is a strong |
| 664 | // symbol. |
| 665 | if (GV->hasExternalLinkage()) |
| 666 | GVEntry = GV; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 667 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 668 | } |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 669 | } |
| 670 | |
| 671 | std::vector<const GlobalValue*> NonCanonicalGlobals; |
| 672 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
| 673 | Module &M = *Modules[m]->getModule(); |
| 674 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 675 | I != E; ++I) { |
| 676 | // In the multi-module case, see what this global maps to. |
| 677 | if (!LinkedGlobalsMap.empty()) { |
| 678 | if (const GlobalValue *GVEntry = |
| 679 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { |
| 680 | // If something else is the canonical global, ignore this one. |
| 681 | if (GVEntry != &*I) { |
| 682 | NonCanonicalGlobals.push_back(I); |
| 683 | continue; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | if (!I->isExternal()) { |
| 689 | // Get the type of the global. |
| 690 | const Type *Ty = I->getType()->getElementType(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 691 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame^] | 692 | // Allocate some memory for it! |
| 693 | unsigned Size = TD->getTypeSize(Ty); |
| 694 | addGlobalMapping(I, new char[Size]); |
| 695 | } else { |
| 696 | // External variable reference. Try to use the dynamic loader to |
| 697 | // get a pointer to it. |
| 698 | if (void *SymAddr = |
| 699 | sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str())) |
| 700 | addGlobalMapping(I, SymAddr); |
| 701 | else { |
| 702 | std::cerr << "Could not resolve external global address: " |
| 703 | << I->getName() << "\n"; |
| 704 | abort(); |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | // If there are multiple modules, map the non-canonical globals to their |
| 710 | // canonical location. |
| 711 | if (!NonCanonicalGlobals.empty()) { |
| 712 | for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { |
| 713 | const GlobalValue *GV = NonCanonicalGlobals[i]; |
| 714 | const GlobalValue *CGV = |
| 715 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 716 | void *Ptr = getPointerToGlobalIfAvailable(CGV); |
| 717 | assert(Ptr && "Canonical global wasn't codegen'd!"); |
| 718 | addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV)); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | // Now that all of the globals are set up in memory, loop through them all and |
| 723 | // initialize their contents. |
| 724 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 725 | I != E; ++I) { |
| 726 | if (!I->isExternal()) { |
| 727 | if (!LinkedGlobalsMap.empty()) { |
| 728 | if (const GlobalValue *GVEntry = |
| 729 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) |
| 730 | if (GVEntry != &*I) // Not the canonical variable. |
| 731 | continue; |
| 732 | } |
| 733 | EmitGlobalVariable(I); |
| 734 | } |
| 735 | } |
| 736 | } |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 740 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 741 | // already in the map. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 742 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 743 | void *GA = getPointerToGlobalIfAvailable(GV); |
Chris Lattner | 23c4724 | 2004-02-08 19:33:23 +0000 | [diff] [blame] | 744 | DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n"); |
| 745 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 746 | const Type *ElTy = GV->getType()->getElementType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 747 | size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 748 | if (GA == 0) { |
| 749 | // If it's not already specified, allocate memory for the global. |
Chris Lattner | a98c545 | 2004-11-19 08:44:07 +0000 | [diff] [blame] | 750 | GA = new char[GVSize]; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 751 | addGlobalMapping(GV, GA); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 752 | } |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 754 | InitializeMemory(GV->getInitializer(), GA); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 755 | NumInitBytes += (unsigned)GVSize; |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 756 | ++NumGlobals; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 757 | } |