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