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