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