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