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