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" |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 27 | #include <iostream> |
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 | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 32 | Statistic<> NumGlobals ("lli", "Number of global vars initialized"); |
Chris Lattner | c2ee9b9 | 2003-11-19 21:08:57 +0000 | [diff] [blame] | 33 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 35 | ExecutionEngine::EECtorFn ExecutionEngine::JITCtor = 0; |
| 36 | ExecutionEngine::EECtorFn ExecutionEngine::InterpCtor = 0; |
| 37 | |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 38 | ExecutionEngine::ExecutionEngine(ModuleProvider *P) : |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 39 | CurMod(*P->getModule()), MP(P) { |
| 40 | assert(P && "ModuleProvider is null?"); |
| 41 | } |
| 42 | |
| 43 | ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) { |
Misha Brukman | 0570157 | 2003-10-17 18:31:59 +0000 | [diff] [blame] | 44 | assert(M && "Module is null?"); |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 47 | ExecutionEngine::~ExecutionEngine() { |
Misha Brukman | 7b2b40f | 2003-10-14 21:36:31 +0000 | [diff] [blame] | 48 | delete MP; |
Brian Gaeke | 8e53948 | 2003-09-04 22:57:27 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 51 | /// addGlobalMapping - Tell the execution engine that the specified global is |
| 52 | /// at the specified location. This is used internally as functions are JIT'd |
| 53 | /// and as global variables are laid out in memory. It can and should also be |
| 54 | /// used by clients of the EE that want to have an LLVM global overlay |
| 55 | /// existing data in memory. |
| 56 | void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 57 | MutexGuard locked(lock); |
| 58 | |
| 59 | void *&CurVal = state.getGlobalAddressMap(locked)[GV]; |
| 60 | assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); |
| 61 | CurVal = Addr; |
| 62 | |
| 63 | // If we are using the reverse mapping, add it too |
| 64 | if (!state.getGlobalAddressReverseMap(locked).empty()) { |
| 65 | const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; |
| 66 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 67 | V = GV; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /// clearAllGlobalMappings - Clear all global mappings and start over again |
| 72 | /// use in dynamic compilation scenarios when you want to move globals |
| 73 | void ExecutionEngine::clearAllGlobalMappings() { |
| 74 | MutexGuard locked(lock); |
| 75 | |
| 76 | state.getGlobalAddressMap(locked).clear(); |
| 77 | state.getGlobalAddressReverseMap(locked).clear(); |
| 78 | } |
| 79 | |
| 80 | /// updateGlobalMapping - Replace an existing mapping for GV with a new |
| 81 | /// address. This updates both maps as required. If "Addr" is null, the |
| 82 | /// entry for the global is removed from the mappings. |
| 83 | void ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 84 | MutexGuard locked(lock); |
| 85 | |
| 86 | // Deleting from the mapping? |
| 87 | if (Addr == 0) { |
| 88 | state.getGlobalAddressMap(locked).erase(GV); |
| 89 | if (!state.getGlobalAddressReverseMap(locked).empty()) |
| 90 | state.getGlobalAddressReverseMap(locked).erase(Addr); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | void *&CurVal = state.getGlobalAddressMap(locked)[GV]; |
| 95 | if (CurVal && !state.getGlobalAddressReverseMap(locked).empty()) |
| 96 | state.getGlobalAddressReverseMap(locked).erase(CurVal); |
| 97 | CurVal = Addr; |
| 98 | |
| 99 | // If we are using the reverse mapping, add it too |
| 100 | if (!state.getGlobalAddressReverseMap(locked).empty()) { |
| 101 | const GlobalValue *&V = state.getGlobalAddressReverseMap(locked)[Addr]; |
| 102 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 103 | V = GV; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// getPointerToGlobalIfAvailable - This returns the address of the specified |
| 108 | /// global value if it is has already been codegen'd, otherwise it returns null. |
| 109 | /// |
| 110 | void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { |
| 111 | MutexGuard locked(lock); |
| 112 | |
| 113 | std::map<const GlobalValue*, void*>::iterator I = |
| 114 | state.getGlobalAddressMap(locked).find(GV); |
| 115 | return I != state.getGlobalAddressMap(locked).end() ? I->second : 0; |
| 116 | } |
| 117 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 118 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 119 | /// at the specified address. |
| 120 | /// |
| 121 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 122 | MutexGuard locked(lock); |
| 123 | |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 124 | // If we haven't computed the reverse mapping yet, do so first. |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 125 | if (state.getGlobalAddressReverseMap(locked).empty()) { |
Chris Lattner | e7fd553 | 2006-05-08 22:00:52 +0000 | [diff] [blame] | 126 | for (std::map<const GlobalValue*, void *>::iterator |
| 127 | I = state.getGlobalAddressMap(locked).begin(), |
| 128 | E = state.getGlobalAddressMap(locked).end(); I != E; ++I) |
| 129 | state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, |
| 130 | I->first)); |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | std::map<void *, const GlobalValue*>::iterator I = |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 134 | state.getGlobalAddressReverseMap(locked).find(Addr); |
| 135 | return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 137 | |
| 138 | // CreateArgv - Turn a vector of strings into a nice argv style array of |
| 139 | // pointers to null terminated strings. |
| 140 | // |
| 141 | static void *CreateArgv(ExecutionEngine *EE, |
| 142 | const std::vector<std::string> &InputArgv) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 143 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 144 | char *Result = new char[(InputArgv.size()+1)*PtrSize]; |
| 145 | |
| 146 | DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); |
| 147 | const Type *SBytePtr = PointerType::get(Type::SByteTy); |
| 148 | |
| 149 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 150 | unsigned Size = InputArgv[i].size()+1; |
| 151 | char *Dest = new char[Size]; |
| 152 | DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 154 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 155 | Dest[Size-1] = 0; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 157 | // Endian safe: Result[i] = (PointerTy)Dest; |
| 158 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize), |
| 159 | SBytePtr); |
| 160 | } |
| 161 | |
| 162 | // Null terminate it |
| 163 | EE->StoreValueToMemory(PTOGV(0), |
| 164 | (GenericValue*)(Result+InputArgv.size()*PtrSize), |
| 165 | SBytePtr); |
| 166 | return Result; |
| 167 | } |
| 168 | |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 169 | |
| 170 | /// runStaticConstructorsDestructors - This method is used to execute all of |
| 171 | /// the static constructors or destructors for a module, depending on the |
| 172 | /// value of isDtors. |
| 173 | void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { |
| 174 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
| 175 | GlobalVariable *GV = CurMod.getNamedGlobal(Name); |
Chris Lattner | b003d76 | 2006-04-22 05:02:46 +0000 | [diff] [blame] | 176 | |
| 177 | // If this global has internal linkage, or if it has a use, then it must be |
| 178 | // an old-style (llvmgcc3) static ctor with __main linked in and in use. If |
| 179 | // this is the case, don't execute any of the global ctors, __main will do it. |
| 180 | if (!GV || GV->isExternal() || GV->hasInternalLinkage()) return; |
Chris Lattner | 9ca6cda | 2006-03-08 18:42:46 +0000 | [diff] [blame] | 181 | |
| 182 | // Should be an array of '{ int, void ()* }' structs. The first value is the |
| 183 | // init priority, which we ignore. |
| 184 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 185 | if (!InitList) return; |
| 186 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 187 | if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ |
| 188 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 189 | |
| 190 | Constant *FP = CS->getOperand(1); |
| 191 | if (FP->isNullValue()) |
| 192 | return; // Found a null terminator, exit. |
| 193 | |
| 194 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 195 | if (CE->getOpcode() == Instruction::Cast) |
| 196 | FP = CE->getOperand(0); |
| 197 | if (Function *F = dyn_cast<Function>(FP)) { |
| 198 | // Execute the ctor/dtor function! |
| 199 | runFunction(F, std::vector<GenericValue>()); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 204 | /// runFunctionAsMain - This is a helper function which wraps runFunction to |
| 205 | /// handle the common task of starting up main with the specified argc, argv, |
| 206 | /// and envp parameters. |
| 207 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 208 | const std::vector<std::string> &argv, |
| 209 | const char * const * envp) { |
| 210 | std::vector<GenericValue> GVArgs; |
| 211 | GenericValue GVArgc; |
| 212 | GVArgc.IntVal = argv.size(); |
Chris Lattner | f24d099 | 2004-08-16 01:05:35 +0000 | [diff] [blame] | 213 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
| 214 | if (NumArgs) { |
| 215 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 216 | if (NumArgs > 1) { |
| 217 | GVArgs.push_back(PTOGV(CreateArgv(this, argv))); // Arg #1 = argv. |
| 218 | assert(((char **)GVTOP(GVArgs[1]))[0] && |
| 219 | "argv[0] was null after CreateArgv"); |
| 220 | if (NumArgs > 2) { |
| 221 | std::vector<std::string> EnvVars; |
| 222 | for (unsigned i = 0; envp[i]; ++i) |
| 223 | EnvVars.push_back(envp[i]); |
| 224 | GVArgs.push_back(PTOGV(CreateArgv(this, EnvVars))); // Arg #2 = envp. |
| 225 | } |
| 226 | } |
| 227 | } |
Chris Lattner | 87f0310 | 2003-12-26 06:50:30 +0000 | [diff] [blame] | 228 | return runFunction(Fn, GVArgs).IntVal; |
| 229 | } |
| 230 | |
Misha Brukman | 1968416 | 2003-10-16 21:18:05 +0000 | [diff] [blame] | 231 | /// If possible, create a JIT, unless the caller specifically requests an |
| 232 | /// 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] | 233 | /// NULL is returned. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 234 | /// |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 235 | ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP, |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 236 | bool ForceInterpreter) { |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 237 | ExecutionEngine *EE = 0; |
| 238 | |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 239 | // Unless the interpreter was explicitly selected, try making a JIT. |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 240 | if (!ForceInterpreter && JITCtor) |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 241 | EE = JITCtor(MP); |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 242 | |
| 243 | // If we can't make a JIT, make an interpreter instead. |
Chris Lattner | 2fe4bb0 | 2006-03-22 06:07:50 +0000 | [diff] [blame] | 244 | if (EE == 0 && InterpCtor) |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 245 | EE = InterpCtor(MP); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 247 | if (EE) { |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 248 | // 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] | 249 | // to the function tells DynamicLibrary to load the program, not a library. |
| 250 | sys::DynamicLibrary::LoadLibraryPermanently(0); |
Chris Lattner | 726c1ef | 2006-03-23 05:22:51 +0000 | [diff] [blame] | 251 | } |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 252 | |
Brian Gaeke | 82d8277 | 2003-09-03 20:34:19 +0000 | [diff] [blame] | 253 | return EE; |
| 254 | } |
| 255 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 256 | /// getPointerToGlobal - This returns the address of the specified global |
| 257 | /// value. This may involve code generation if it's a function. |
| 258 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 259 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
Brian Gaeke | 37df460 | 2003-08-13 18:16:14 +0000 | [diff] [blame] | 260 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 261 | return getPointerToFunction(F); |
| 262 | |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 263 | MutexGuard locked(lock); |
Jeff Cohen | 68835dd | 2006-02-07 05:11:57 +0000 | [diff] [blame] | 264 | void *p = state.getGlobalAddressMap(locked)[GV]; |
| 265 | if (p) |
| 266 | return p; |
| 267 | |
| 268 | // Global variable might have been added since interpreter started. |
| 269 | if (GlobalVariable *GVar = |
| 270 | const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) |
| 271 | EmitGlobalVariable(GVar); |
| 272 | else |
| 273 | assert("Global hasn't had an address allocated yet!"); |
Reid Spencer | ee44863 | 2005-07-12 15:51:55 +0000 | [diff] [blame] | 274 | return state.getGlobalAddressMap(locked)[GV]; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 277 | /// FIXME: document |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 278 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 279 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
| 280 | GenericValue Result; |
Chris Lattner | 6f335f9 | 2004-10-26 05:35:14 +0000 | [diff] [blame] | 281 | if (isa<UndefValue>(C)) return Result; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 283 | if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) { |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 284 | switch (CE->getOpcode()) { |
| 285 | case Instruction::GetElementPtr: { |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 286 | Result = getConstantValue(CE->getOperand(0)); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 287 | std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end()); |
| 288 | uint64_t Offset = |
| 289 | TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 290 | |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 291 | if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | 3db4b62 | 2005-10-23 23:54:56 +0000 | [diff] [blame] | 292 | Result.IntVal += Offset; |
| 293 | else |
| 294 | Result.LongVal += Offset; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 295 | return Result; |
| 296 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 297 | case Instruction::Cast: { |
| 298 | // We only need to handle a few cases here. Almost all casts will |
| 299 | // automatically fold, just the ones involving pointers won't. |
| 300 | // |
| 301 | Constant *Op = CE->getOperand(0); |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 302 | GenericValue GV = getConstantValue(Op); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 304 | // Handle cast of pointer to pointer... |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 305 | if (Op->getType()->getTypeID() == C->getType()->getTypeID()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 306 | return GV; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 74cf819 | 2003-08-18 17:23:40 +0000 | [diff] [blame] | 308 | // Handle a cast of pointer to any integral type... |
Chris Lattner | c88a4ea | 2003-08-18 17:33:15 +0000 | [diff] [blame] | 309 | if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral()) |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 310 | return GV; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 312 | // Handle cast of integer to a pointer... |
| 313 | if (isa<PointerType>(C->getType()) && Op->getType()->isIntegral()) |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 314 | switch (Op->getType()->getTypeID()) { |
Chris Lattner | 7d1bd33 | 2004-03-16 08:38:56 +0000 | [diff] [blame] | 315 | case Type::BoolTyID: return PTOGV((void*)(uintptr_t)GV.BoolVal); |
| 316 | case Type::SByteTyID: return PTOGV((void*)( intptr_t)GV.SByteVal); |
| 317 | case Type::UByteTyID: return PTOGV((void*)(uintptr_t)GV.UByteVal); |
| 318 | case Type::ShortTyID: return PTOGV((void*)( intptr_t)GV.ShortVal); |
| 319 | case Type::UShortTyID: return PTOGV((void*)(uintptr_t)GV.UShortVal); |
| 320 | case Type::IntTyID: return PTOGV((void*)( intptr_t)GV.IntVal); |
| 321 | case Type::UIntTyID: return PTOGV((void*)(uintptr_t)GV.UIntVal); |
| 322 | case Type::LongTyID: return PTOGV((void*)( intptr_t)GV.LongVal); |
| 323 | case Type::ULongTyID: return PTOGV((void*)(uintptr_t)GV.ULongVal); |
| 324 | default: assert(0 && "Unknown integral type!"); |
| 325 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 326 | break; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 327 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 329 | case Instruction::Add: |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 330 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
| 331 | default: assert(0 && "Bad add type!"); abort(); |
| 332 | case Type::LongTyID: |
| 333 | case Type::ULongTyID: |
Chris Lattner | 6b2125c | 2003-05-14 17:53:49 +0000 | [diff] [blame] | 334 | Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal + |
| 335 | getConstantValue(CE->getOperand(1)).LongVal; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 336 | break; |
Chris Lattner | 5f90cb8 | 2004-07-11 08:01:11 +0000 | [diff] [blame] | 337 | case Type::IntTyID: |
| 338 | case Type::UIntTyID: |
| 339 | Result.IntVal = getConstantValue(CE->getOperand(0)).IntVal + |
| 340 | getConstantValue(CE->getOperand(1)).IntVal; |
| 341 | break; |
| 342 | case Type::ShortTyID: |
| 343 | case Type::UShortTyID: |
| 344 | Result.ShortVal = getConstantValue(CE->getOperand(0)).ShortVal + |
| 345 | getConstantValue(CE->getOperand(1)).ShortVal; |
| 346 | break; |
| 347 | case Type::SByteTyID: |
| 348 | case Type::UByteTyID: |
| 349 | Result.SByteVal = getConstantValue(CE->getOperand(0)).SByteVal + |
| 350 | getConstantValue(CE->getOperand(1)).SByteVal; |
| 351 | break; |
| 352 | case Type::FloatTyID: |
| 353 | Result.FloatVal = getConstantValue(CE->getOperand(0)).FloatVal + |
| 354 | getConstantValue(CE->getOperand(1)).FloatVal; |
| 355 | break; |
| 356 | case Type::DoubleTyID: |
| 357 | Result.DoubleVal = getConstantValue(CE->getOperand(0)).DoubleVal + |
| 358 | getConstantValue(CE->getOperand(1)).DoubleVal; |
| 359 | break; |
| 360 | } |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 361 | return Result; |
Chris Lattner | 9a23122 | 2003-05-14 17:51:49 +0000 | [diff] [blame] | 362 | default: |
| 363 | break; |
| 364 | } |
| 365 | std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; |
| 366 | abort(); |
| 367 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 368 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 369 | switch (C->getType()->getTypeID()) { |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 370 | #define GET_CONST_VAL(TY, CTY, CLASS) \ |
| 371 | case Type::TY##TyID: Result.TY##Val = (CTY)cast<CLASS>(C)->getValue(); break |
| 372 | GET_CONST_VAL(Bool , bool , ConstantBool); |
| 373 | GET_CONST_VAL(UByte , unsigned char , ConstantUInt); |
| 374 | GET_CONST_VAL(SByte , signed char , ConstantSInt); |
| 375 | GET_CONST_VAL(UShort , unsigned short, ConstantUInt); |
| 376 | GET_CONST_VAL(Short , signed short , ConstantSInt); |
| 377 | GET_CONST_VAL(UInt , unsigned int , ConstantUInt); |
| 378 | GET_CONST_VAL(Int , signed int , ConstantSInt); |
Chris Lattner | 4b3141d | 2005-05-12 06:01:28 +0000 | [diff] [blame] | 379 | GET_CONST_VAL(ULong , uint64_t , ConstantUInt); |
| 380 | GET_CONST_VAL(Long , int64_t , ConstantSInt); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 381 | GET_CONST_VAL(Float , float , ConstantFP); |
| 382 | GET_CONST_VAL(Double , double , ConstantFP); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 383 | #undef GET_CONST_VAL |
| 384 | case Type::PointerTyID: |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 385 | if (isa<ConstantPointerNull>(C)) |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 386 | Result.PointerVal = 0; |
Reid Spencer | 40cf2f9 | 2004-07-18 00:41:27 +0000 | [diff] [blame] | 387 | else if (const Function *F = dyn_cast<Function>(C)) |
| 388 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
| 389 | else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C)) |
| 390 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
| 391 | else |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 392 | assert(0 && "Unknown constant pointer type!"); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 393 | break; |
| 394 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 395 | std::cout << "ERROR: Constant unimp for type: " << *C->getType() << "\n"; |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 396 | abort(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 397 | } |
| 398 | return Result; |
| 399 | } |
| 400 | |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 401 | /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr |
| 402 | /// is the address of the memory at which to store Val, cast to GenericValue *. |
| 403 | /// It is not a pointer to a GenericValue containing the address at which to |
| 404 | /// store Val. |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 405 | /// |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 406 | void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 407 | const Type *Ty) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 408 | if (getTargetData()->isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 409 | switch (Ty->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 410 | case Type::BoolTyID: |
| 411 | case Type::UByteTyID: |
| 412 | case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; |
| 413 | case Type::UShortTyID: |
| 414 | case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255; |
| 415 | Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255; |
| 416 | break; |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 417 | Store4BytesLittleEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 418 | case Type::FloatTyID: |
| 419 | case Type::UIntTyID: |
| 420 | case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255; |
| 421 | Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255; |
| 422 | Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255; |
| 423 | Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255; |
| 424 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 425 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 426 | goto Store4BytesLittleEndian; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 427 | case Type::DoubleTyID: |
| 428 | case Type::ULongTyID: |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 429 | case Type::LongTyID: |
| 430 | Ptr->Untyped[0] = (unsigned char)(Val.ULongVal ); |
| 431 | Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 8); |
| 432 | Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 16); |
| 433 | Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 24); |
| 434 | Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 32); |
| 435 | Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 40); |
| 436 | Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 48); |
| 437 | Ptr->Untyped[7] = (unsigned char)(Val.ULongVal >> 56); |
| 438 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 439 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 440 | std::cout << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 441 | } |
| 442 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 443 | switch (Ty->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 444 | case Type::BoolTyID: |
| 445 | case Type::UByteTyID: |
| 446 | case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break; |
| 447 | case Type::UShortTyID: |
| 448 | case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255; |
| 449 | Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255; |
| 450 | break; |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 451 | Store4BytesBigEndian: |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 452 | case Type::FloatTyID: |
| 453 | case Type::UIntTyID: |
| 454 | case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255; |
| 455 | Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255; |
| 456 | Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255; |
| 457 | Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255; |
| 458 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 459 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | 2be5079 | 2003-04-23 20:41:01 +0000 | [diff] [blame] | 460 | goto Store4BytesBigEndian; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 461 | case Type::DoubleTyID: |
| 462 | case Type::ULongTyID: |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 463 | case Type::LongTyID: |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 464 | Ptr->Untyped[7] = (unsigned char)(Val.ULongVal ); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 465 | Ptr->Untyped[6] = (unsigned char)(Val.ULongVal >> 8); |
| 466 | Ptr->Untyped[5] = (unsigned char)(Val.ULongVal >> 16); |
| 467 | Ptr->Untyped[4] = (unsigned char)(Val.ULongVal >> 24); |
| 468 | Ptr->Untyped[3] = (unsigned char)(Val.ULongVal >> 32); |
| 469 | Ptr->Untyped[2] = (unsigned char)(Val.ULongVal >> 40); |
| 470 | Ptr->Untyped[1] = (unsigned char)(Val.ULongVal >> 48); |
| 471 | Ptr->Untyped[0] = (unsigned char)(Val.ULongVal >> 56); |
| 472 | break; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 473 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 474 | std::cout << "Cannot store value of type " << *Ty << "!\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Misha Brukman | 4afac18 | 2003-10-10 17:45:12 +0000 | [diff] [blame] | 479 | /// FIXME: document |
| 480 | /// |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 481 | GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, |
| 482 | const Type *Ty) { |
| 483 | GenericValue Result; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 484 | if (getTargetData()->isLittleEndian()) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 485 | switch (Ty->getTypeID()) { |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 486 | case Type::BoolTyID: |
| 487 | case Type::UByteTyID: |
| 488 | case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; |
| 489 | case Type::UShortTyID: |
| 490 | case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | |
| 491 | ((unsigned)Ptr->Untyped[1] << 8); |
| 492 | break; |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 493 | Load4BytesLittleEndian: |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 494 | case Type::FloatTyID: |
| 495 | case Type::UIntTyID: |
| 496 | case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | |
| 497 | ((unsigned)Ptr->Untyped[1] << 8) | |
| 498 | ((unsigned)Ptr->Untyped[2] << 16) | |
| 499 | ((unsigned)Ptr->Untyped[3] << 24); |
| 500 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 501 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 502 | goto Load4BytesLittleEndian; |
| 503 | case Type::DoubleTyID: |
| 504 | case Type::ULongTyID: |
| 505 | case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | |
| 506 | ((uint64_t)Ptr->Untyped[1] << 8) | |
| 507 | ((uint64_t)Ptr->Untyped[2] << 16) | |
| 508 | ((uint64_t)Ptr->Untyped[3] << 24) | |
| 509 | ((uint64_t)Ptr->Untyped[4] << 32) | |
| 510 | ((uint64_t)Ptr->Untyped[5] << 40) | |
| 511 | ((uint64_t)Ptr->Untyped[6] << 48) | |
| 512 | ((uint64_t)Ptr->Untyped[7] << 56); |
| 513 | break; |
| 514 | default: |
| 515 | std::cout << "Cannot load value of type " << *Ty << "!\n"; |
| 516 | abort(); |
| 517 | } |
| 518 | } else { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 519 | switch (Ty->getTypeID()) { |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 520 | case Type::BoolTyID: |
| 521 | case Type::UByteTyID: |
| 522 | case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; |
| 523 | case Type::UShortTyID: |
| 524 | case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | |
| 525 | ((unsigned)Ptr->Untyped[0] << 8); |
| 526 | break; |
| 527 | Load4BytesBigEndian: |
| 528 | case Type::FloatTyID: |
| 529 | case Type::UIntTyID: |
| 530 | case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | |
| 531 | ((unsigned)Ptr->Untyped[2] << 8) | |
| 532 | ((unsigned)Ptr->Untyped[1] << 16) | |
| 533 | ((unsigned)Ptr->Untyped[0] << 24); |
| 534 | break; |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 535 | case Type::PointerTyID: if (getTargetData()->getPointerSize() == 4) |
Chris Lattner | f88b9a6 | 2003-05-08 16:52:16 +0000 | [diff] [blame] | 536 | goto Load4BytesBigEndian; |
| 537 | case Type::DoubleTyID: |
| 538 | case Type::ULongTyID: |
| 539 | case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | |
| 540 | ((uint64_t)Ptr->Untyped[6] << 8) | |
| 541 | ((uint64_t)Ptr->Untyped[5] << 16) | |
| 542 | ((uint64_t)Ptr->Untyped[4] << 24) | |
| 543 | ((uint64_t)Ptr->Untyped[3] << 32) | |
| 544 | ((uint64_t)Ptr->Untyped[2] << 40) | |
| 545 | ((uint64_t)Ptr->Untyped[1] << 48) | |
| 546 | ((uint64_t)Ptr->Untyped[0] << 56); |
| 547 | break; |
| 548 | default: |
| 549 | std::cout << "Cannot load value of type " << *Ty << "!\n"; |
| 550 | abort(); |
| 551 | } |
| 552 | } |
| 553 | return Result; |
| 554 | } |
| 555 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 556 | // InitializeMemory - Recursive function to apply a Constant value into the |
| 557 | // specified memory location... |
| 558 | // |
| 559 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 560 | if (isa<UndefValue>(Init)) { |
| 561 | return; |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 562 | } else if (const ConstantPacked *CP = dyn_cast<ConstantPacked>(Init)) { |
| 563 | unsigned ElementSize = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 564 | getTargetData()->getTypeSize(CP->getType()->getElementType()); |
Robert Bocchino | 7c2b7c7 | 2006-01-20 18:18:40 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 566 | InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); |
| 567 | return; |
Chris Lattner | bd1d382 | 2004-10-16 18:19:26 +0000 | [diff] [blame] | 568 | } else if (Init->getType()->isFirstClassType()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 569 | GenericValue Val = getConstantValue(Init); |
| 570 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 571 | return; |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 572 | } else if (isa<ConstantAggregateZero>(Init)) { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 573 | memset(Addr, 0, (size_t)getTargetData()->getTypeSize(Init->getType())); |
Chris Lattner | dd2c82a | 2004-02-15 05:54:06 +0000 | [diff] [blame] | 574 | return; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 577 | switch (Init->getType()->getTypeID()) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 578 | case Type::ArrayTyID: { |
| 579 | const ConstantArray *CPA = cast<ConstantArray>(Init); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 580 | unsigned ElementSize = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 581 | getTargetData()->getTypeSize(CPA->getType()->getElementType()); |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 582 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 583 | InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
| 587 | case Type::StructTyID: { |
| 588 | const ConstantStruct *CPS = cast<ConstantStruct>(Init); |
| 589 | const StructLayout *SL = |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 590 | getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); |
Alkis Evlogimenos | 15876bb | 2004-08-04 08:44:43 +0000 | [diff] [blame] | 591 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
| 592 | InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->MemberOffsets[i]); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
| 596 | default: |
Chris Lattner | 0a8e8e1 | 2004-07-15 02:51:31 +0000 | [diff] [blame] | 597 | std::cerr << "Bad Type: " << *Init->getType() << "\n"; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 598 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 599 | } |
| 600 | } |
| 601 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 602 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 603 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 604 | /// their initializers into the memory. |
| 605 | /// |
| 606 | void ExecutionEngine::emitGlobals() { |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 607 | const TargetData *TD = getTargetData(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 608 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 609 | // Loop over all of the global variables in the program, allocating the memory |
| 610 | // to hold them. |
Chris Lattner | 6e63088 | 2005-07-11 02:49:16 +0000 | [diff] [blame] | 611 | Module &M = getModule(); |
| 612 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 613 | I != E; ++I) |
| 614 | if (!I->isExternal()) { |
| 615 | // Get the type of the global... |
| 616 | const Type *Ty = I->getType()->getElementType(); |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 617 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 618 | // Allocate some memory for it! |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 619 | unsigned Size = TD->getTypeSize(Ty); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 620 | addGlobalMapping(I, new char[Size]); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 621 | } else { |
Brian Gaeke | 322cdb2 | 2003-10-10 17:02:42 +0000 | [diff] [blame] | 622 | // External variable reference. Try to use the dynamic loader to |
| 623 | // get a pointer to it. |
Reid Spencer | df5a37e | 2004-11-29 14:11:29 +0000 | [diff] [blame] | 624 | if (void *SymAddr = sys::DynamicLibrary::SearchForAddressOfSymbol( |
| 625 | I->getName().c_str())) |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 626 | addGlobalMapping(I, SymAddr); |
Chris Lattner | d8c03bf | 2003-04-23 19:01:49 +0000 | [diff] [blame] | 627 | else { |
| 628 | std::cerr << "Could not resolve external global address: " |
| 629 | << I->getName() << "\n"; |
| 630 | abort(); |
| 631 | } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 632 | } |
Misha Brukman | edf128a | 2005-04-21 22:36:52 +0000 | [diff] [blame] | 633 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 634 | // Now that all of the globals are set up in memory, loop through them all and |
| 635 | // initialize their contents. |
Chris Lattner | 6e63088 | 2005-07-11 02:49:16 +0000 | [diff] [blame] | 636 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 637 | I != E; ++I) |
| 638 | if (!I->isExternal()) |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 639 | EmitGlobalVariable(I); |
| 640 | } |
| 641 | |
| 642 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 643 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 644 | // already in the map. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 645 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 646 | void *GA = getPointerToGlobalIfAvailable(GV); |
Chris Lattner | 23c4724 | 2004-02-08 19:33:23 +0000 | [diff] [blame] | 647 | DEBUG(std::cerr << "Global '" << GV->getName() << "' -> " << GA << "\n"); |
| 648 | |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 649 | const Type *ElTy = GV->getType()->getElementType(); |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 650 | size_t GVSize = (size_t)getTargetData()->getTypeSize(ElTy); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 651 | if (GA == 0) { |
| 652 | // If it's not already specified, allocate memory for the global. |
Chris Lattner | a98c545 | 2004-11-19 08:44:07 +0000 | [diff] [blame] | 653 | GA = new char[GVSize]; |
Chris Lattner | 55d8648 | 2003-12-31 20:21:04 +0000 | [diff] [blame] | 654 | addGlobalMapping(GV, GA); |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 655 | } |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 657 | InitializeMemory(GV->getInitializer(), GA); |
Chris Lattner | 813c815 | 2005-01-08 20:13:19 +0000 | [diff] [blame] | 658 | NumInitBytes += (unsigned)GVSize; |
Chris Lattner | 24b0a18 | 2003-12-20 02:45:37 +0000 | [diff] [blame] | 659 | ++NumGlobals; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 660 | } |