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 | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 3634373 | 2006-12-19 22:43:32 +0000 | [diff] [blame] | 29 | STATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); |
| 30 | STATISTIC(NumGlobals , "Number of global vars initialized"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 32 | ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; |
| 33 | ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; |
| 34 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 35 | ExecutionEngine::ExecutionEngine(ModuleProvider *P) { |
Chris Lattner | 3d6e33d | 2006-11-09 19:31:15 +0000 | [diff] [blame] | 36 | LazyCompilationDisabled = false; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 37 | Modules.push_back(P); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 38 | assert(P && "ModuleProvider is null?"); |
| 39 | } |
| 40 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 41 | ExecutionEngine::ExecutionEngine(Module *M) { |
Chris Lattner | 3d6e33d | 2006-11-09 19:31:15 +0000 | [diff] [blame] | 42 | LazyCompilationDisabled = false; |
Misha Brukman | 0570157 | 2003-10-17 18:31:59 +0000 | [diff] [blame] | 43 | assert(M && "Module is null?"); |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 44 | Modules.push_back(new ExistingModuleProvider(M)); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 47 | ExecutionEngine::~ExecutionEngine() { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 48 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 49 | delete Modules[i]; |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 52 | /// FindFunctionNamed - Search all of the active modules to find the one that |
| 53 | /// defines FnName. This is very slow operation and shouldn't be used for |
| 54 | /// general code. |
| 55 | Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { |
| 56 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 57 | if (Function *F = Modules[i]->getModule()->getFunction(FnName)) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 58 | return F; |
| 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 64 | /// addGlobalMapping - Tell the execution engine that the specified global is |
| 65 | /// at the specified location. This is used internally as functions are JIT'd |
| 66 | /// and as global variables are laid out in memory. It can and should also be |
| 67 | /// used by clients of the EE that want to have an LLVM global overlay |
| 68 | /// existing data in memory. |
| 69 | void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 70 | MutexGuard locked(lock); |
| 71 | |
| 72 | void *&CurVal = state.getGlobalAddressMap(locked)[GV]; |
| 73 | assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); |
| 74 | CurVal = Addr; |
| 75 | |
| 76 | // If we are using the reverse mapping, add it too |
| 77 | if (!state.getGlobalAddressReverseMap(locked).empty()) { |
| 78 | const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; |
| 79 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 80 | V = GV; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /// clearAllGlobalMappings - Clear all global mappings and start over again |
| 85 | /// use in dynamic compilation scenarios when you want to move globals |
| 86 | void ExecutionEngine::clearAllGlobalMappings() { |
| 87 | MutexGuard locked(lock); |
| 88 | |
| 89 | state.getGlobalAddressMap(locked).clear(); |
| 90 | state.getGlobalAddressReverseMap(locked).clear(); |
| 91 | } |
| 92 | |
| 93 | /// updateGlobalMapping - Replace an existing mapping for GV with a new |
| 94 | /// address. This updates both maps as required. If "Addr" is null, the |
| 95 | /// entry for the global is removed from the mappings. |
| 96 | void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 97 | MutexGuard locked(lock); |
| 98 | |
| 99 | // Deleting from the mapping? |
| 100 | if (Addr == 0) { |
| 101 | state.getGlobalAddressMap(locked).erase(GV); |
| 102 | if (!state.getGlobalAddressReverseMap(locked).empty()) |
| 103 | state.getGlobalAddressReverseMap(locked).erase(Addr); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | void *&CurVal = state.getGlobalAddressMap(locked)[GV]; |
| 108 | if (CurVal && !state.getGlobalAddressReverseMap(locked).empty()) |
| 109 | state.getGlobalAddressReverseMap(locked).erase(CurVal); |
| 110 | CurVal = Addr; |
| 111 | |
| 112 | // If we are using the reverse mapping, add it too |
| 113 | if (!state.getGlobalAddressReverseMap(locked).empty()) { |
| 114 | const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; |
| 115 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 116 | V = GV; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// getPointerToGlobalIfAvailable - This returns the address of the specified |
| 121 | /// global value if it is has already been codegen'd, otherwise it returns null. |
| 122 | /// |
| 123 | void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { |
| 124 | MutexGuard locked(lock); |
| 125 | |
| 126 | std::map<const GlobalValue*, void*>::iterator I = |
| 127 | state.getGlobalAddressMap(locked).find(GV); |
| 128 | return I != state.getGlobalAddressMap(locked).end() ? I->second : 0; |
| 129 | } |
| 130 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 131 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 132 | /// at the specified address. |
| 133 | /// |
| 134 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 135 | MutexGuard locked(lock); |
| 136 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 137 | // If we haven't computed the reverse mapping yet, do so first. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 138 | if (state.getGlobalAddressReverseMap(locked).empty()) { |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 139 | for (std::map<const GlobalValue*, void *>::iterator |
| 140 | I = state.getGlobalAddressMap(locked).begin(), |
| 141 | E = state.getGlobalAddressMap(locked).end(); I != E; ++I) |
| 142 | state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, |
| 143 | I->first)); |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | std::map<void *, const GlobalValue*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 147 | state.getGlobalAddressReverseMap(locked).find(Addr); |
| 148 | return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 150 | |
| 151 | // CreateArgv - Turn a vector of strings into a nice argv style array of |
| 152 | // pointers to null terminated strings. |
| 153 | // |
| 154 | static void *CreateArgv(ExecutionEngine *EE, |
| 155 | const std::vector<std::string> &InputArgv) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 156 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 157 | char *Result = new char[(InputArgv.size()+1)*PtrSize]; |
| 158 | |
Bill Wendling | 480f093 | 2006-11-27 23:54:50 +0000 | [diff] [blame] | 159 | DOUT << "ARGV = " << (void*)Result << "\n"; |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 160 | const Type *SBytePtr = PointerType::get(Type::Int8Ty); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 161 | |
| 162 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 163 | unsigned Size = InputArgv[i].size()+1; |
| 164 | char *Dest = new char[Size]; |
Bill Wendling | 480f093 | 2006-11-27 23:54:50 +0000 | [diff] [blame] | 165 | DOUT << "ARGV[" << i << "] = " << (void*)Dest << "\n"; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 166 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 167 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 168 | Dest[Size-1] = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 170 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 171 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), |
| 172 | SBytePtr); |
| 173 | } |
| 174 | |
| 175 | // Null terminate it |
| 176 | EE->StoreValueToMemory(PTOGV(0), |
| 177 | (GenericValue*)(Result+InputArgv.size()*PtrSize), |
| 178 | SBytePtr); |
| 179 | return Result; |
| 180 | } |
| 181 | |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 182 | |
| 183 | /// runStaticConstructorsDestructors - This method is used to execute all of |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 184 | /// the static constructors or destructors for a program, depending on the |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 185 | /// value of isDtors. |
| 186 | void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { |
| 187 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 188 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 189 | // Execute global ctors/dtors for each module in the program. |
| 190 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
| 191 | GlobalVariable *GV = Modules[m]->getModule()->getNamedGlobal(Name); |
| 192 | |
| 193 | // If this global has internal linkage, or if it has a use, then it must be |
| 194 | // an old-style (llvmgcc3) static ctor with __main linked in and in use. If |
| 195 | // this is the case, don't execute any of the global ctors, __main will do |
| 196 | // it. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 197 | if (!GV || GV->isDeclaration() || GV->hasInternalLinkage()) continue; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 198 | |
| 199 | // Should be an array of '{ int, void ()* }' structs. The first value is |
| 200 | // the init priority, which we ignore. |
| 201 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 202 | if (!InitList) continue; |
| 203 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 204 | if (ConstantStruct *CS = |
| 205 | dyn_cast<ConstantStruct>(InitList->getOperand(i))) { |
| 206 | if (CS->getNumOperands() != 2) break; // Not array of 2-element structs. |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 207 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 208 | Constant *FP = CS->getOperand(1); |
| 209 | if (FP->isNullValue()) |
| 210 | break; // Found a null terminator, exit. |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 211 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 212 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 213 | if (CE->isCast()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 214 | FP = CE->getOperand(0); |
| 215 | if (Function *F = dyn_cast<Function>(FP)) { |
| 216 | // Execute the ctor/dtor function! |
| 217 | runFunction(F, std::vector<GenericValue>()); |
| 218 | } |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 220 | } |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 223 | /// runFunctionAsMain - This is a helper function which wraps runFunction to |
| 224 | /// handle the common task of starting up main with the specified argc, argv, |
| 225 | /// and envp parameters. |
| 226 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 227 | const std::vector<std::string> &argv, |
| 228 | const char * const * envp) { |
| 229 | std::vector<GenericValue> GVArgs; |
| 230 | GenericValue GVArgc; |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 231 | GVArgc.Int32Val = argv.size(); |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 232 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
| 233 | if (NumArgs) { |
| 234 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 235 | if (NumArgs > 1) { |
| 236 | GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. |
| 237 | assert(((char **)GVTOP(GVArgs[1]))[0] && |
| 238 | "argv[0] was null after CreateArgv"); |
| 239 | if (NumArgs > 2) { |
| 240 | std::vector<std::string> EnvVars; |
| 241 | for (unsigned i = 0; envp[i]; ++i) |
| 242 | EnvVars.push_back(envp[i]); |
| 243 | GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. |
| 244 | } |
| 245 | } |
| 246 | } |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 247 | return runFunction(Fn, GVArgs).Int32Val; |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 250 | /// If possible, create a JIT, unless the caller specifically requests an |
| 251 | /// 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] | 252 | /// NULL is returned. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 253 | /// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 254 | ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 255 | bool ForceInterpreter) { |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 256 | ExecutionEngine *EE = 0; |
| 257 | |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 258 | // Unless the interpreter was explicitly selected, try making a JIT. |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 259 | if (!ForceInterpreter && JITCtor) |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 260 | EE = JITCtor(MP); |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 261 | |
| 262 | // If we can't make a JIT, make an interpreter instead. |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 263 | if (EE == 0 && InterpCtor) |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 264 | EE = InterpCtor(MP); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 266 | if (EE) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 267 | // 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] | 268 | // to the function tells DynamicLibrary to load the program, not a library. |
Chris Lattner | 6f3ada5 | 2006-05-14 19:01:55 +0000 | [diff] [blame] | 269 | try { |
| 270 | sys::DynamicLibrary::LoadLibraryPermanently(0); |
| 271 | } catch (...) { |
| 272 | } |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 273 | } |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 274 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 275 | return EE; |
| 276 | } |
| 277 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 278 | /// getPointerToGlobal - This returns the address of the specified global |
| 279 | /// value. This may involve code generation if it's a function. |
| 280 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 281 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
Brian Gaeke | 37df460 | 2003-08-13 18:16:14 +0000 | [diff] [blame] | 282 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 283 | return getPointerToFunction(F); |
| 284 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 285 | MutexGuard locked(lock); |
Jeff Cohen | 68835dd | 2006-02-07 05:11:57 +0000 | [diff] [blame] | 286 | void *p = state.getGlobalAddressMap(locked)[GV]; |
| 287 | if (p) |
| 288 | return p; |
| 289 | |
| 290 | // Global variable might have been added since interpreter started. |
| 291 | if (GlobalVariable *GVar = |
| 292 | const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) |
| 293 | EmitGlobalVariable(GVar); |
| 294 | else |
Chris Lattner | 64f150f | 2007-02-14 06:20:04 +0000 | [diff] [blame] | 295 | assert(0 && "Global hasn't had an address allocated yet!"); |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 296 | return state.getGlobalAddressMap(locked)[GV]; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 299 | /// This macro is used to handle a variety of situations involing integer |
| 300 | /// values where the action should be done to one of the GenericValue members. |
| 301 | /// THEINTTY is a const Type * for the integer type. ACTION1 comes before |
| 302 | /// the GenericValue, ACTION2 comes after. |
| 303 | #define DO_FOR_INTEGER(THEINTTY, ACTION) \ |
| 304 | { \ |
| 305 | unsigned BitWidth = cast<IntegerType>(THEINTTY)->getBitWidth(); \ |
| 306 | if (BitWidth == 1) {\ |
| 307 | ACTION(Int1Val); \ |
| 308 | } else if (BitWidth <= 8) {\ |
| 309 | ACTION(Int8Val); \ |
| 310 | } else if (BitWidth <= 16) {\ |
| 311 | ACTION(Int16Val); \ |
| 312 | } else if (BitWidth <= 32) { \ |
| 313 | ACTION(Int32Val); \ |
| 314 | } else if (BitWidth <= 64) { \ |
| 315 | ACTION(Int64Val); \ |
| 316 | } else {\ |
| 317 | assert(0 && "Not implemented: integer types > 64 bits"); \ |
| 318 | } \ |
| 319 | } |
| 320 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 321 | /// This function converts a Constant* into a GenericValue. The interesting |
| 322 | /// part is if C is a ConstantExpr. |
| 323 | /// @brief Get a GenericValue for a Constnat* |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 324 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 325 | // Declare the result as garbage. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 326 | GenericValue Result; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 327 | |
| 328 | // If its undefined, return the garbage. |
Chris Lattner | 6f335f9 | 2004-10-26 05:35:14 +0000 | [diff] [blame] | 329 | if (isa<UndefValue>(C)) return Result; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 330 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 331 | // If the value is a ConstantExpr |
| 332 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 333 | switch (CE->getOpcode()) { |
| 334 | case Instruction::GetElementPtr: { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 335 | // Compute the index |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 336 | Result = getConstantValue(CE->getOperand(0)); |
Chris Lattner | 829621c | 2007-02-10 20:35:22 +0000 | [diff] [blame] | 337 | SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 338 | uint64_t Offset = |
Chris Lattner | 829621c | 2007-02-10 20:35:22 +0000 | [diff] [blame] | 339 | TD->getIndexedOffset(CE->getOperand(0)->getType(), |
| 340 | &Indices[0], Indices.size()); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 341 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 342 | if (getTargetData()->getPointerSize() == 4) |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 343 | Result.Int32Val += Offset; |
Chris Lattner | 3db4b62 | 2005-10-23 23:54:56 +0000 | [diff] [blame] | 344 | else |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 345 | Result.Int64Val += Offset; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 346 | return Result; |
| 347 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 348 | case Instruction::Trunc: |
| 349 | case Instruction::ZExt: |
| 350 | case Instruction::SExt: |
| 351 | case Instruction::FPTrunc: |
| 352 | case Instruction::FPExt: |
| 353 | case Instruction::UIToFP: |
| 354 | case Instruction::SIToFP: |
| 355 | case Instruction::FPToUI: |
| 356 | case Instruction::FPToSI: |
| 357 | break; |
| 358 | case Instruction::PtrToInt: { |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 359 | Constant *Op = CE->getOperand(0); |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 360 | GenericValue GV = getConstantValue(Op); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 361 | return GV; |
| 362 | } |
| 363 | case Instruction::BitCast: { |
| 364 | // Bit casts are no-ops but we can only return the GV of the operand if |
| 365 | // they are the same basic type (pointer->pointer, packed->packed, etc.) |
| 366 | Constant *Op = CE->getOperand(0); |
| 367 | GenericValue GV = getConstantValue(Op); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 368 | if (Op->getType()->getTypeID() == C->getType()->getTypeID()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 369 | return GV; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 370 | break; |
| 371 | } |
| 372 | case Instruction::IntToPtr: { |
| 373 | // IntToPtr casts are just so special. Cast to intptr_t first. |
| 374 | Constant *Op = CE->getOperand(0); |
| 375 | GenericValue GV = getConstantValue(Op); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 376 | #define INT_TO_PTR_ACTION(FIELD) \ |
| 377 | return PTOGV((void*)(uintptr_t)GV.FIELD) |
| 378 | DO_FOR_INTEGER(Op->getType(), INT_TO_PTR_ACTION) |
| 379 | #undef INT_TO_PTR_ACTION |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 380 | break; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 381 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 382 | case Instruction::Add: |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 383 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
| 384 | default: assert(0 && "Bad add type!"); abort(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 385 | case Type::IntegerTyID: |
| 386 | #define ADD_ACTION(FIELD) \ |
| 387 | Result.FIELD = getConstantValue(CE->getOperand(0)).FIELD + \ |
| 388 | getConstantValue(CE->getOperand(1)).FIELD; |
| 389 | DO_FOR_INTEGER(CE->getOperand(0)->getType(),ADD_ACTION); |
| 390 | #undef ADD_ACTION |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 391 | break; |
| 392 | case Type::FloatTyID: |
| 393 | Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + |
| 394 | getConstantValue(CE->getOperand(1)).FloatVal; |
| 395 | break; |
| 396 | case Type::DoubleTyID: |
| 397 | Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + |
| 398 | getConstantValue(CE->getOperand(1)).DoubleVal; |
| 399 | break; |
| 400 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 401 | return Result; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 402 | default: |
| 403 | break; |
| 404 | } |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 405 | cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 406 | abort(); |
| 407 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 408 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 409 | switch (C->getType()->getTypeID()) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 410 | #define GET_CONST_VAL(TY, CTY, CLASS, GETMETH) \ |
| 411 | case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->GETMETH(); break |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 412 | GET_CONST_VAL(Float , float , ConstantFP, getValue); |
| 413 | GET_CONST_VAL(Double, double , ConstantFP, getValue); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 414 | #undef GET_CONST_VAL |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 415 | case Type::IntegerTyID: { |
| 416 | unsigned BitWidth = cast<IntegerType>(C->getType())->getBitWidth(); |
| 417 | if (BitWidth == 1) |
| 418 | Result.Int1Val = (bool)cast<ConstantInt>(C)->getZExtValue(); |
| 419 | else if (BitWidth <= 8) |
| 420 | Result.Int8Val = (uint8_t )cast<ConstantInt>(C)->getZExtValue(); |
| 421 | else if (BitWidth <= 16) |
| 422 | Result.Int16Val = (uint16_t )cast<ConstantInt>(C)->getZExtValue(); |
| 423 | else if (BitWidth <= 32) |
| 424 | Result.Int32Val = (uint32_t )cast<ConstantInt>(C)->getZExtValue(); |
| 425 | else if (BitWidth <= 64) |
| 426 | Result.Int64Val = (uint64_t )cast<ConstantInt>(C)->getZExtValue(); |
| 427 | else |
Reid Spencer | 93a2875 | 2007-03-03 06:18:03 +0000 | [diff] [blame] | 428 | Result.APIntVal = const_cast<APInt*>(&cast<ConstantInt>(C)->getValue()); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 429 | break; |
| 430 | } |
| 431 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 432 | case Type::PointerTyID: |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 433 | if (isa<ConstantPointerNull>(C)) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 434 | Result.PointerVal = 0; |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 435 | else if (const Function *F = dyn_cast<Function>(C)) |
| 436 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
| 437 | else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) |
| 438 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
| 439 | else |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 440 | assert(0 && "Unknown constant pointer type!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 441 | break; |
| 442 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 443 | cerr << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 444 | abort(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 445 | } |
| 446 | return Result; |
| 447 | } |
| 448 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 449 | /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr |
| 450 | /// is the address of the memory at which to store Val, cast to GenericValue *. |
| 451 | /// It is not a pointer to a GenericValue containing the address at which to |
| 452 | /// store Val. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 453 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 454 | void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 455 | const Type *Ty) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 456 | if (getTargetData()->isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 457 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 458 | case Type::IntegerTyID: { |
| 459 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 460 | uint64_t BitMask = cast<IntegerType>(Ty)->getBitMask(); |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 461 | GenericValue TmpVal = Val; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 462 | if (BitWidth <= 8) |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 463 | Ptr->Untyped[0] = Val.Int8Val & BitMask; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 464 | else if (BitWidth <= 16) { |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 465 | TmpVal.Int16Val &= BitMask; |
| 466 | Ptr->Untyped[0] = TmpVal.Int16Val & 255; |
| 467 | Ptr->Untyped[1] = (TmpVal.Int16Val >> 8) & 255; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 468 | } else if (BitWidth <= 32) { |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 469 | TmpVal.Int32Val &= BitMask; |
| 470 | Ptr->Untyped[0] = TmpVal.Int32Val & 255; |
| 471 | Ptr->Untyped[1] = (TmpVal.Int32Val >> 8) & 255; |
| 472 | Ptr->Untyped[2] = (TmpVal.Int32Val >> 16) & 255; |
| 473 | Ptr->Untyped[3] = (TmpVal.Int32Val >> 24) & 255; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 474 | } else if (BitWidth <= 64) { |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 475 | TmpVal.Int64Val &= BitMask; |
| 476 | Ptr->Untyped[0] = (unsigned char)(TmpVal.Int64Val ); |
| 477 | Ptr->Untyped[1] = (unsigned char)(TmpVal.Int64Val >> 8); |
| 478 | Ptr->Untyped[2] = (unsigned char)(TmpVal.Int64Val >> 16); |
| 479 | Ptr->Untyped[3] = (unsigned char)(TmpVal.Int64Val >> 24); |
| 480 | Ptr->Untyped[4] = (unsigned char)(TmpVal.Int64Val >> 32); |
| 481 | Ptr->Untyped[5] = (unsigned char)(TmpVal.Int64Val >> 40); |
| 482 | Ptr->Untyped[6] = (unsigned char)(TmpVal.Int64Val >> 48); |
| 483 | Ptr->Untyped[7] = (unsigned char)(TmpVal.Int64Val >> 56); |
Reid Spencer | 93a2875 | 2007-03-03 06:18:03 +0000 | [diff] [blame] | 484 | } else { |
| 485 | uint64_t *Dest = (uint64_t*)Ptr; |
| 486 | const uint64_t *Src = Val.APIntVal->getRawData(); |
| 487 | for (uint32_t i = 0; i < Val.APIntVal->getNumWords(); ++i) |
| 488 | Dest[i] = Src[i]; |
| 489 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | Store4BytesLittleEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 493 | case Type::FloatTyID: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 494 | Ptr->Untyped[0] = Val.Int32Val & 255; |
| 495 | Ptr->Untyped[1] = (Val.Int32Val >> 8) & 255; |
| 496 | Ptr->Untyped[2] = (Val.Int32Val >> 16) & 255; |
| 497 | Ptr->Untyped[3] = (Val.Int32Val >> 24) & 255; |
| 498 | break; |
| 499 | case Type::PointerTyID: |
| 500 | if (getTargetData()->getPointerSize() == 4) |
| 501 | goto Store4BytesLittleEndian; |
| 502 | /* FALL THROUGH */ |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 503 | case Type::DoubleTyID: |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 504 | Ptr->Untyped[0] = (unsigned char)(Val.Int64Val ); |
| 505 | Ptr->Untyped[1] = (unsigned char)(Val.Int64Val >> 8); |
| 506 | Ptr->Untyped[2] = (unsigned char)(Val.Int64Val >> 16); |
| 507 | Ptr->Untyped[3] = (unsigned char)(Val.Int64Val >> 24); |
| 508 | Ptr->Untyped[4] = (unsigned char)(Val.Int64Val >> 32); |
| 509 | Ptr->Untyped[5] = (unsigned char)(Val.Int64Val >> 40); |
| 510 | Ptr->Untyped[6] = (unsigned char)(Val.Int64Val >> 48); |
| 511 | Ptr->Untyped[7] = (unsigned char)(Val.Int64Val >> 56); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 512 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 513 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 514 | cerr << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 515 | } |
| 516 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 517 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 518 | case Type::IntegerTyID: { |
| 519 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
Reid Spencer | c103057 | 2007-01-19 21:13:56 +0000 | [diff] [blame] | 520 | uint64_t BitMask = cast<IntegerType>(Ty)->getBitMask(); |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 521 | GenericValue TmpVal = Val; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 522 | if (BitWidth <= 8) |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 523 | Ptr->Untyped[0] = Val.Int8Val & BitMask; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 524 | else if (BitWidth <= 16) { |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 525 | TmpVal.Int16Val &= BitMask; |
| 526 | Ptr->Untyped[1] = TmpVal.Int16Val & 255; |
| 527 | Ptr->Untyped[0] = (TmpVal.Int16Val >> 8) & 255; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 528 | } else if (BitWidth <= 32) { |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 529 | TmpVal.Int32Val &= BitMask; |
| 530 | Ptr->Untyped[3] = TmpVal.Int32Val & 255; |
| 531 | Ptr->Untyped[2] = (TmpVal.Int32Val >> 8) & 255; |
| 532 | Ptr->Untyped[1] = (TmpVal.Int32Val >> 16) & 255; |
| 533 | Ptr->Untyped[0] = (TmpVal.Int32Val >> 24) & 255; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 534 | } else if (BitWidth <= 64) { |
Reid Spencer | f89aec6 | 2007-01-18 01:24:02 +0000 | [diff] [blame] | 535 | TmpVal.Int64Val &= BitMask; |
| 536 | Ptr->Untyped[7] = (unsigned char)(TmpVal.Int64Val ); |
| 537 | Ptr->Untyped[6] = (unsigned char)(TmpVal.Int64Val >> 8); |
| 538 | Ptr->Untyped[5] = (unsigned char)(TmpVal.Int64Val >> 16); |
| 539 | Ptr->Untyped[4] = (unsigned char)(TmpVal.Int64Val >> 24); |
| 540 | Ptr->Untyped[3] = (unsigned char)(TmpVal.Int64Val >> 32); |
| 541 | Ptr->Untyped[2] = (unsigned char)(TmpVal.Int64Val >> 40); |
| 542 | Ptr->Untyped[1] = (unsigned char)(TmpVal.Int64Val >> 48); |
| 543 | Ptr->Untyped[0] = (unsigned char)(TmpVal.Int64Val >> 56); |
Reid Spencer | 93a2875 | 2007-03-03 06:18:03 +0000 | [diff] [blame] | 544 | } else { |
| 545 | uint64_t *Dest = (uint64_t*)Ptr; |
| 546 | const uint64_t *Src = Val.APIntVal->getRawData(); |
| 547 | for (uint32_t i = 0; i < Val.APIntVal->getNumWords(); ++i) |
| 548 | Dest[i] = Src[i]; |
| 549 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 550 | break; |
| 551 | } |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 552 | Store4BytesBigEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 553 | case Type::FloatTyID: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 554 | Ptr->Untyped[3] = Val.Int32Val & 255; |
| 555 | Ptr->Untyped[2] = (Val.Int32Val >> 8) & 255; |
| 556 | Ptr->Untyped[1] = (Val.Int32Val >> 16) & 255; |
| 557 | Ptr->Untyped[0] = (Val.Int32Val >> 24) & 255; |
| 558 | break; |
| 559 | case Type::PointerTyID: |
| 560 | if (getTargetData()->getPointerSize() == 4) |
| 561 | goto Store4BytesBigEndian; |
| 562 | /* FALL THROUGH */ |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 563 | case Type::DoubleTyID: |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 564 | Ptr->Untyped[7] = (unsigned char)(Val.Int64Val ); |
| 565 | Ptr->Untyped[6] = (unsigned char)(Val.Int64Val >> 8); |
| 566 | Ptr->Untyped[5] = (unsigned char)(Val.Int64Val >> 16); |
| 567 | Ptr->Untyped[4] = (unsigned char)(Val.Int64Val >> 24); |
| 568 | Ptr->Untyped[3] = (unsigned char)(Val.Int64Val >> 32); |
| 569 | Ptr->Untyped[2] = (unsigned char)(Val.Int64Val >> 40); |
| 570 | Ptr->Untyped[1] = (unsigned char)(Val.Int64Val >> 48); |
| 571 | Ptr->Untyped[0] = (unsigned char)(Val.Int64Val >> 56); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 572 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 573 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 574 | cerr << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 579 | /// FIXME: document |
| 580 | /// |
Reid Spencer | f0f09a9 | 2007-03-03 08:36:29 +0000 | [diff] [blame^] | 581 | void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, |
| 582 | GenericValue *Ptr, |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 583 | const Type *Ty) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 584 | if (getTargetData()->isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 585 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 586 | case Type::IntegerTyID: { |
| 587 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 588 | if (BitWidth <= 8) |
| 589 | Result.Int8Val = Ptr->Untyped[0]; |
| 590 | else if (BitWidth <= 16) { |
| 591 | Result.Int16Val = (unsigned)Ptr->Untyped[0] | |
| 592 | ((unsigned)Ptr->Untyped[1] << 8); |
| 593 | } else if (BitWidth <= 32) { |
| 594 | Result.Int32Val = (unsigned)Ptr->Untyped[0] | |
| 595 | ((unsigned)Ptr->Untyped[1] << 8) | |
| 596 | ((unsigned)Ptr->Untyped[2] << 16) | |
| 597 | ((unsigned)Ptr->Untyped[3] << 24); |
| 598 | } else if (BitWidth <= 64) { |
| 599 | Result.Int64Val = (uint64_t)Ptr->Untyped[0] | |
| 600 | ((uint64_t)Ptr->Untyped[1] << 8) | |
| 601 | ((uint64_t)Ptr->Untyped[2] << 16) | |
| 602 | ((uint64_t)Ptr->Untyped[3] << 24) | |
| 603 | ((uint64_t)Ptr->Untyped[4] << 32) | |
| 604 | ((uint64_t)Ptr->Untyped[5] << 40) | |
| 605 | ((uint64_t)Ptr->Untyped[6] << 48) | |
| 606 | ((uint64_t)Ptr->Untyped[7] << 56); |
| 607 | } else |
Reid Spencer | f0f09a9 | 2007-03-03 08:36:29 +0000 | [diff] [blame^] | 608 | *(Result.APIntVal) = APInt(BitWidth, BitWidth/64, (uint64_t*)Ptr); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 609 | break; |
| 610 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 611 | Load4BytesLittleEndian: |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 612 | case Type::FloatTyID: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 613 | Result.Int32Val = (unsigned)Ptr->Untyped[0] | |
| 614 | ((unsigned)Ptr->Untyped[1] << 8) | |
| 615 | ((unsigned)Ptr->Untyped[2] << 16) | |
| 616 | ((unsigned)Ptr->Untyped[3] << 24); |
| 617 | break; |
| 618 | case Type::PointerTyID: |
| 619 | if (getTargetData()->getPointerSize() == 4) |
| 620 | goto Load4BytesLittleEndian; |
| 621 | /* FALL THROUGH */ |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 622 | case Type::DoubleTyID: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 623 | Result.Int64Val = (uint64_t)Ptr->Untyped[0] | |
| 624 | ((uint64_t)Ptr->Untyped[1] << 8) | |
| 625 | ((uint64_t)Ptr->Untyped[2] << 16) | |
| 626 | ((uint64_t)Ptr->Untyped[3] << 24) | |
| 627 | ((uint64_t)Ptr->Untyped[4] << 32) | |
| 628 | ((uint64_t)Ptr->Untyped[5] << 40) | |
| 629 | ((uint64_t)Ptr->Untyped[6] << 48) | |
| 630 | ((uint64_t)Ptr->Untyped[7] << 56); |
| 631 | break; |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 632 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 633 | cerr << "Cannot load value of type " << *Ty << "!\n"; |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 634 | abort(); |
| 635 | } |
| 636 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 637 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 638 | case Type::IntegerTyID: { |
Reid Spencer | 93a2875 | 2007-03-03 06:18:03 +0000 | [diff] [blame] | 639 | uint32_t BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 640 | if (BitWidth <= 8) |
| 641 | Result.Int8Val = Ptr->Untyped[0]; |
| 642 | else if (BitWidth <= 16) { |
| 643 | Result.Int16Val = (unsigned)Ptr->Untyped[1] | |
| 644 | ((unsigned)Ptr->Untyped[0] << 8); |
| 645 | } else if (BitWidth <= 32) { |
| 646 | Result.Int32Val = (unsigned)Ptr->Untyped[3] | |
| 647 | ((unsigned)Ptr->Untyped[2] << 8) | |
| 648 | ((unsigned)Ptr->Untyped[1] << 16) | |
| 649 | ((unsigned)Ptr->Untyped[0] << 24); |
| 650 | } else if (BitWidth <= 64) { |
| 651 | Result.Int64Val = (uint64_t)Ptr->Untyped[7] | |
| 652 | ((uint64_t)Ptr->Untyped[6] << 8) | |
| 653 | ((uint64_t)Ptr->Untyped[5] << 16) | |
| 654 | ((uint64_t)Ptr->Untyped[4] << 24) | |
| 655 | ((uint64_t)Ptr->Untyped[3] << 32) | |
| 656 | ((uint64_t)Ptr->Untyped[2] << 40) | |
| 657 | ((uint64_t)Ptr->Untyped[1] << 48) | |
| 658 | ((uint64_t)Ptr->Untyped[0] << 56); |
| 659 | } else |
Reid Spencer | f0f09a9 | 2007-03-03 08:36:29 +0000 | [diff] [blame^] | 660 | *(Result.APIntVal) = APInt(BitWidth, BitWidth/64, (uint64_t*)Ptr); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 661 | break; |
| 662 | } |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 663 | Load4BytesBigEndian: |
| 664 | case Type::FloatTyID: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 665 | Result.Int32Val = (unsigned)Ptr->Untyped[3] | |
| 666 | ((unsigned)Ptr->Untyped[2] << 8) | |
| 667 | ((unsigned)Ptr->Untyped[1] << 16) | |
| 668 | ((unsigned)Ptr->Untyped[0] << 24); |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 669 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 670 | case Type::PointerTyID: |
| 671 | if (getTargetData()->getPointerSize() == 4) |
| 672 | goto Load4BytesBigEndian; |
| 673 | /* FALL THROUGH */ |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 674 | case Type::DoubleTyID: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 675 | Result.Int64Val = (uint64_t)Ptr->Untyped[7] | |
| 676 | ((uint64_t)Ptr->Untyped[6] << 8) | |
| 677 | ((uint64_t)Ptr->Untyped[5] << 16) | |
| 678 | ((uint64_t)Ptr->Untyped[4] << 24) | |
| 679 | ((uint64_t)Ptr->Untyped[3] << 32) | |
| 680 | ((uint64_t)Ptr->Untyped[2] << 40) | |
| 681 | ((uint64_t)Ptr->Untyped[1] << 48) | |
| 682 | ((uint64_t)Ptr->Untyped[0] << 56); |
| 683 | break; |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 684 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 685 | cerr << "Cannot load value of type " << *Ty << "!\n"; |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 686 | abort(); |
| 687 | } |
| 688 | } |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 691 | // InitializeMemory - Recursive function to apply a Constant value into the |
| 692 | // specified memory location... |
| 693 | // |
| 694 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 695 | if (isa<UndefValue>(Init)) { |
| 696 | return; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 697 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 698 | unsigned ElementSize = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 699 | getTargetData()->getTypeSize(CP->getType()->getElementType()); |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 700 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 701 | InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); |
| 702 | return; |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 703 | } else if (Init->getType()->isFirstClassType()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 704 | GenericValue Val = getConstantValue(Init); |
| 705 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 706 | return; |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 707 | } else if (isa<ConstantAggregateZero>(Init)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 708 | memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType())); |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 709 | return; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 712 | switch (Init->getType()->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 713 | case Type::ArrayTyID: { |
| 714 | const ConstantArray *CPA = cast<ConstantArray>(Init); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 715 | unsigned ElementSize = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 716 | getTargetData()->getTypeSize(CPA->getType()->getElementType()); |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 717 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 718 | InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 719 | return; |
| 720 | } |
| 721 | |
| 722 | case Type::StructTyID: { |
| 723 | const ConstantStruct *CPS = cast<ConstantStruct>(Init); |
| 724 | const StructLayout *SL = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 725 | getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 726 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
Chris Lattner | b1919e2 | 2007-02-10 19:55:17 +0000 | [diff] [blame] | 727 | InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | |
| 731 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 732 | cerr << "Bad Type: " << *Init->getType() << "\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 733 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 734 | } |
| 735 | } |
| 736 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 737 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 738 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 739 | /// their initializers into the memory. |
| 740 | /// |
| 741 | void ExecutionEngine::emitGlobals() { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 742 | const TargetData *TD = getTargetData(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 743 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 744 | // 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] | 745 | // to hold them. If there is more than one module, do a prepass over globals |
| 746 | // to figure out how the different modules should link together. |
| 747 | // |
| 748 | std::map<std::pair<std::string, const Type*>, |
| 749 | const GlobalValue*> LinkedGlobalsMap; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 750 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 751 | if (Modules.size() != 1) { |
| 752 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
| 753 | Module &M = *Modules[m]->getModule(); |
| 754 | for (Module::const_global_iterator I = M.global_begin(), |
| 755 | E = M.global_end(); I != E; ++I) { |
| 756 | const GlobalValue *GV = I; |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 757 | if (GV->hasInternalLinkage() || GV->isDeclaration() || |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 758 | GV->hasAppendingLinkage() || !GV->hasName()) |
| 759 | continue;// Ignore external globals and globals with internal linkage. |
| 760 | |
| 761 | const GlobalValue *&GVEntry = |
| 762 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 763 | |
| 764 | // If this is the first time we've seen this global, it is the canonical |
| 765 | // version. |
| 766 | if (!GVEntry) { |
| 767 | GVEntry = GV; |
| 768 | continue; |
| 769 | } |
| 770 | |
| 771 | // If the existing global is strong, never replace it. |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 772 | if (GVEntry->hasExternalLinkage() || |
| 773 | GVEntry->hasDLLImportLinkage() || |
| 774 | GVEntry->hasDLLExportLinkage()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 775 | continue; |
| 776 | |
| 777 | // Otherwise, we know it's linkonce/weak, replace it if this is a strong |
| 778 | // symbol. |
Anton Korobeynikov | 78ee7b7 | 2006-12-01 00:25:12 +0000 | [diff] [blame] | 779 | if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 780 | GVEntry = GV; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 782 | } |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | std::vector<const GlobalValue*> NonCanonicalGlobals; |
| 786 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
| 787 | Module &M = *Modules[m]->getModule(); |
| 788 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 789 | I != E; ++I) { |
| 790 | // In the multi-module case, see what this global maps to. |
| 791 | if (!LinkedGlobalsMap.empty()) { |
| 792 | if (const GlobalValue *GVEntry = |
| 793 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { |
| 794 | // If something else is the canonical global, ignore this one. |
| 795 | if (GVEntry != &*I) { |
| 796 | NonCanonicalGlobals.push_back(I); |
| 797 | continue; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 802 | if (!I->isDeclaration()) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 803 | // Get the type of the global. |
| 804 | const Type *Ty = I->getType()->getElementType(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 805 | |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 806 | // Allocate some memory for it! |
| 807 | unsigned Size = TD->getTypeSize(Ty); |
| 808 | addGlobalMapping(I, new char[Size]); |
| 809 | } else { |
| 810 | // External variable reference. Try to use the dynamic loader to |
| 811 | // get a pointer to it. |
| 812 | if (void *SymAddr = |
| 813 | sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName().c_str())) |
| 814 | addGlobalMapping(I, SymAddr); |
| 815 | else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 816 | cerr << "Could not resolve external global address: " |
| 817 | << I->getName() << "\n"; |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 818 | abort(); |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | // If there are multiple modules, map the non-canonical globals to their |
| 824 | // canonical location. |
| 825 | if (!NonCanonicalGlobals.empty()) { |
| 826 | for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { |
| 827 | const GlobalValue *GV = NonCanonicalGlobals[i]; |
| 828 | const GlobalValue *CGV = |
| 829 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 830 | void *Ptr = getPointerToGlobalIfAvailable(CGV); |
| 831 | assert(Ptr && "Canonical global wasn't codegen'd!"); |
| 832 | addGlobalMapping(GV, getPointerToGlobalIfAvailable(CGV)); |
| 833 | } |
| 834 | } |
| 835 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 836 | // Now that all of the globals are set up in memory, loop through them all |
| 837 | // and initialize their contents. |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 838 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 839 | I != E; ++I) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 840 | if (!I->isDeclaration()) { |
Chris Lattner | fe85403 | 2006-08-16 01:24:12 +0000 | [diff] [blame] | 841 | if (!LinkedGlobalsMap.empty()) { |
| 842 | if (const GlobalValue *GVEntry = |
| 843 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) |
| 844 | if (GVEntry != &*I) // Not the canonical variable. |
| 845 | continue; |
| 846 | } |
| 847 | EmitGlobalVariable(I); |
| 848 | } |
| 849 | } |
| 850 | } |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 854 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 855 | // already in the map. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 856 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 857 | void *GA = getPointerToGlobalIfAvailable(GV); |
Bill Wendling | 480f093 | 2006-11-27 23:54:50 +0000 | [diff] [blame] | 858 | DOUT << "Global '" << GV->getName() << "' -> " << GA << "\n"; |
Chris Lattner | 23c4724 | 2004-02-08 19:33:23 +0000 | [diff] [blame] | 859 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 860 | const Type *ElTy = GV->getType()->getElementType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 861 | size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 862 | if (GA == 0) { |
| 863 | // If it's not already specified, allocate memory for the global. |
Chris Lattner | a98c545 | 2004-11-19 08:44:07 +0000 | [diff] [blame] | 864 | GA = new char[GVSize]; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 865 | addGlobalMapping(GV, GA); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 866 | } |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 867 | |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 868 | InitializeMemory(GV->getInitializer(), GA); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 869 | NumInitBytes += (unsigned)GVSize; |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 870 | ++NumGlobals; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 871 | } |