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