Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 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. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the common interface used by the various execution engine |
| 11 | // subclasses. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "jit" |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 16 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
| 17 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/DerivedTypes.h" |
| 20 | #include "llvm/Module.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/ExecutionEngine/GenericValue.h" |
Chris Lattner | 5ea5b5c | 2009-08-23 22:49:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Edwin Török | f7cbfea | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MutexGuard.h" |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ValueHandle.h" |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | #include "llvm/System/DynamicLibrary.h" |
Duncan Sands | 2e6d342 | 2007-12-12 23:03:45 +0000 | [diff] [blame] | 29 | #include "llvm/System/Host.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | 357a27d | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 31 | #include <cmath> |
| 32 | #include <cstring> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | STATISTIC(NumInitBytes, "Number of bytes of global vars initialized"); |
| 36 | STATISTIC(NumGlobals , "Number of global vars initialized"); |
| 37 | |
Jeffrey Yasskin | 403335c | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 38 | ExecutionEngine *(*ExecutionEngine::JITCtor)( |
| 39 | Module *M, |
| 40 | std::string *ErrorStr, |
| 41 | JITMemoryManager *JMM, |
| 42 | CodeGenOpt::Level OptLevel, |
| 43 | bool GVsWithCode, |
| 44 | CodeModel::Model CMM, |
| 45 | StringRef MArch, |
| 46 | StringRef MCPU, |
| 47 | const SmallVectorImpl<std::string>& MAttrs) = 0; |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 48 | ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M, |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 49 | std::string *ErrorStr) = 0; |
Nicolas Geoffray | 0e757e1 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 50 | ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0; |
| 51 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 53 | ExecutionEngine::ExecutionEngine(Module *M) |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 54 | : EEState(*this), |
| 55 | LazyFunctionCreator(0) { |
Jeffrey Yasskin | 4f6ac2f | 2009-10-27 20:30:28 +0000 | [diff] [blame] | 56 | CompilingLazily = false; |
Evan Cheng | 9b8bc83 | 2008-09-24 16:25:55 +0000 | [diff] [blame] | 57 | GVCompilationDisabled = false; |
Evan Cheng | 5c62a69 | 2008-06-17 16:49:02 +0000 | [diff] [blame] | 58 | SymbolSearchingDisabled = false; |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 59 | Modules.push_back(M); |
| 60 | assert(M && "Module is null?"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 63 | ExecutionEngine::~ExecutionEngine() { |
| 64 | clearAllGlobalMappings(); |
| 65 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) |
| 66 | delete Modules[i]; |
| 67 | } |
| 68 | |
Jeffrey Yasskin | f63e58e | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 69 | namespace { |
| 70 | // This class automatically deletes the memory block when the GlobalVariable is |
| 71 | // destroyed. |
| 72 | class GVMemoryBlock : public CallbackVH { |
| 73 | GVMemoryBlock(const GlobalVariable *GV) |
| 74 | : CallbackVH(const_cast<GlobalVariable*>(GV)) {} |
| 75 | |
| 76 | public: |
| 77 | // Returns the address the GlobalVariable should be written into. The |
| 78 | // GVMemoryBlock object prefixes that. |
| 79 | static char *Create(const GlobalVariable *GV, const TargetData& TD) { |
| 80 | const Type *ElTy = GV->getType()->getElementType(); |
| 81 | size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy); |
| 82 | void *RawMemory = ::operator new( |
| 83 | TargetData::RoundUpAlignment(sizeof(GVMemoryBlock), |
| 84 | TD.getPreferredAlignment(GV)) |
| 85 | + GVSize); |
| 86 | new(RawMemory) GVMemoryBlock(GV); |
| 87 | return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock); |
| 88 | } |
| 89 | |
| 90 | virtual void deleted() { |
| 91 | // We allocated with operator new and with some extra memory hanging off the |
| 92 | // end, so don't just delete this. I'm not sure if this is actually |
| 93 | // required. |
| 94 | this->~GVMemoryBlock(); |
| 95 | ::operator delete(this); |
| 96 | } |
| 97 | }; |
| 98 | } // anonymous namespace |
| 99 | |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 100 | char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) { |
Jeffrey Yasskin | f63e58e | 2010-03-27 04:53:56 +0000 | [diff] [blame] | 101 | return GVMemoryBlock::Create(GV, *getTargetData()); |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 104 | /// removeModule - Remove a Module from the list of modules. |
| 105 | bool ExecutionEngine::removeModule(Module *M) { |
| 106 | for(SmallVector<Module *, 1>::iterator I = Modules.begin(), |
Devang Patel | 5d0d0d0 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 107 | E = Modules.end(); I != E; ++I) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 108 | Module *Found = *I; |
| 109 | if (Found == M) { |
Devang Patel | 5d0d0d0 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 110 | Modules.erase(I); |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 111 | clearGlobalMappingsFromModule(M); |
| 112 | return true; |
Devang Patel | 5d0d0d0 | 2007-10-15 19:56:32 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 115 | return false; |
Nate Begeman | b34045e | 2009-01-23 19:27:28 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | /// FindFunctionNamed - Search all of the active modules to find the one that |
| 119 | /// defines FnName. This is very slow operation and shouldn't be used for |
| 120 | /// general code. |
| 121 | Function *ExecutionEngine::FindFunctionNamed(const char *FnName) { |
| 122 | for (unsigned i = 0, e = Modules.size(); i != e; ++i) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 123 | if (Function *F = Modules[i]->getFunction(FnName)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | return F; |
| 125 | } |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | |
Jeffrey Yasskin | 43304d3 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 130 | void *ExecutionEngineState::RemoveMapping( |
| 131 | const MutexGuard &, const GlobalValue *ToUnmap) { |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 132 | GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap); |
Jeffrey Yasskin | 43304d3 | 2009-10-09 22:10:27 +0000 | [diff] [blame] | 133 | void *OldVal; |
| 134 | if (I == GlobalAddressMap.end()) |
| 135 | OldVal = 0; |
| 136 | else { |
| 137 | OldVal = I->second; |
| 138 | GlobalAddressMap.erase(I); |
| 139 | } |
| 140 | |
| 141 | GlobalAddressReverseMap.erase(OldVal); |
| 142 | return OldVal; |
| 143 | } |
| 144 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 145 | /// addGlobalMapping - Tell the execution engine that the specified global is |
| 146 | /// at the specified location. This is used internally as functions are JIT'd |
| 147 | /// and as global variables are laid out in memory. It can and should also be |
| 148 | /// used by clients of the EE that want to have an LLVM global overlay |
| 149 | /// existing data in memory. |
| 150 | void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) { |
| 151 | MutexGuard locked(lock); |
Evan Cheng | b83f697 | 2008-09-18 07:54:21 +0000 | [diff] [blame] | 152 | |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 153 | DEBUG(dbgs() << "JIT: Map \'" << GV->getName() |
Daniel Dunbar | 23e2b80 | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 154 | << "\' to [" << Addr << "]\n";); |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 155 | void *&CurVal = EEState.getGlobalAddressMap(locked)[GV]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 156 | assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!"); |
| 157 | CurVal = Addr; |
| 158 | |
| 159 | // If we are using the reverse mapping, add it too |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 160 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 161 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 162 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 163 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 164 | V = GV; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /// clearAllGlobalMappings - Clear all global mappings and start over again |
| 169 | /// use in dynamic compilation scenarios when you want to move globals |
| 170 | void ExecutionEngine::clearAllGlobalMappings() { |
| 171 | MutexGuard locked(lock); |
| 172 | |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 173 | EEState.getGlobalAddressMap(locked).clear(); |
| 174 | EEState.getGlobalAddressReverseMap(locked).clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Nate Begeman | f7113d9 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 177 | /// clearGlobalMappingsFromModule - Clear all global mappings that came from a |
| 178 | /// particular module, because it has been removed from the JIT. |
| 179 | void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) { |
| 180 | MutexGuard locked(lock); |
| 181 | |
| 182 | for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 183 | EEState.RemoveMapping(locked, FI); |
Nate Begeman | f7113d9 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 184 | } |
| 185 | for (Module::global_iterator GI = M->global_begin(), GE = M->global_end(); |
| 186 | GI != GE; ++GI) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 187 | EEState.RemoveMapping(locked, GI); |
Nate Begeman | f7113d9 | 2008-05-21 16:34:48 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | /// updateGlobalMapping - Replace an existing mapping for GV with a new |
| 192 | /// address. This updates both maps as required. If "Addr" is null, the |
| 193 | /// entry for the global is removed from the mappings. |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 194 | void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | MutexGuard locked(lock); |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 196 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 197 | ExecutionEngineState::GlobalAddressMapTy &Map = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 198 | EEState.getGlobalAddressMap(locked); |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 199 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 200 | // Deleting from the mapping? |
| 201 | if (Addr == 0) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 202 | return EEState.RemoveMapping(locked, GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 205 | void *&CurVal = Map[GV]; |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 206 | void *OldVal = CurVal; |
| 207 | |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 208 | if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty()) |
| 209 | EEState.getGlobalAddressReverseMap(locked).erase(CurVal); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | CurVal = Addr; |
| 211 | |
| 212 | // If we are using the reverse mapping, add it too |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 213 | if (!EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 214 | AssertingVH<const GlobalValue> &V = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 215 | EEState.getGlobalAddressReverseMap(locked)[Addr]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 216 | assert((V == 0 || GV == 0) && "GlobalMapping already established!"); |
| 217 | V = GV; |
| 218 | } |
Chris Lattner | fb3f0f8 | 2008-04-04 04:47:41 +0000 | [diff] [blame] | 219 | return OldVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /// getPointerToGlobalIfAvailable - This returns the address of the specified |
| 223 | /// global value if it is has already been codegen'd, otherwise it returns null. |
| 224 | /// |
| 225 | void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) { |
| 226 | MutexGuard locked(lock); |
| 227 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 228 | ExecutionEngineState::GlobalAddressMapTy::iterator I = |
| 229 | EEState.getGlobalAddressMap(locked).find(GV); |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 230 | return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /// getGlobalValueAtAddress - Return the LLVM global value object that starts |
| 234 | /// at the specified address. |
| 235 | /// |
| 236 | const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) { |
| 237 | MutexGuard locked(lock); |
| 238 | |
| 239 | // If we haven't computed the reverse mapping yet, do so first. |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 240 | if (EEState.getGlobalAddressReverseMap(locked).empty()) { |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 241 | for (ExecutionEngineState::GlobalAddressMapTy::iterator |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 242 | I = EEState.getGlobalAddressMap(locked).begin(), |
| 243 | E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I) |
| 244 | EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | I->first)); |
| 246 | } |
| 247 | |
Jeffrey Yasskin | 291d66a | 2009-08-07 19:54:29 +0000 | [diff] [blame] | 248 | std::map<void *, AssertingVH<const GlobalValue> >::iterator I = |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 249 | EEState.getGlobalAddressReverseMap(locked).find(Addr); |
| 250 | return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 253 | namespace { |
| 254 | class ArgvArray { |
| 255 | char *Array; |
| 256 | std::vector<char*> Values; |
| 257 | public: |
| 258 | ArgvArray() : Array(NULL) {} |
| 259 | ~ArgvArray() { clear(); } |
| 260 | void clear() { |
| 261 | delete[] Array; |
| 262 | Array = NULL; |
| 263 | for (size_t I = 0, E = Values.size(); I != E; ++I) { |
| 264 | delete[] Values[I]; |
| 265 | } |
| 266 | Values.clear(); |
| 267 | } |
| 268 | /// Turn a vector of strings into a nice argv style array of pointers to null |
| 269 | /// terminated strings. |
| 270 | void *reset(LLVMContext &C, ExecutionEngine *EE, |
| 271 | const std::vector<std::string> &InputArgv); |
| 272 | }; |
| 273 | } // anonymous namespace |
| 274 | void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE, |
| 275 | const std::vector<std::string> &InputArgv) { |
| 276 | clear(); // Free the old contents. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 277 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 278 | Array = new char[(InputArgv.size()+1)*PtrSize]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 279 | |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 280 | DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n"); |
Duncan Sands | f2519d6 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 281 | const Type *SBytePtr = Type::getInt8PtrTy(C); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | |
| 283 | for (unsigned i = 0; i != InputArgv.size(); ++i) { |
| 284 | unsigned Size = InputArgv[i].size()+1; |
| 285 | char *Dest = new char[Size]; |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 286 | Values.push_back(Dest); |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 287 | DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 288 | |
| 289 | std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); |
| 290 | Dest[Size-1] = 0; |
| 291 | |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 292 | // Endian safe: Array[i] = (PointerTy)Dest; |
| 293 | EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 294 | SBytePtr); |
| 295 | } |
| 296 | |
| 297 | // Null terminate it |
| 298 | EE->StoreValueToMemory(PTOGV(0), |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 299 | (GenericValue*)(Array+InputArgv.size()*PtrSize), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 300 | SBytePtr); |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 301 | return Array; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | |
| 305 | /// runStaticConstructorsDestructors - This method is used to execute all of |
Evan Cheng | 50a5882 | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 306 | /// the static constructors or destructors for a module, depending on the |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 307 | /// value of isDtors. |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 308 | void ExecutionEngine::runStaticConstructorsDestructors(Module *module, |
| 309 | bool isDtors) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 310 | const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors"; |
| 311 | |
| 312 | // Execute global ctors/dtors for each module in the program. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 313 | |
Evan Cheng | 50a5882 | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 314 | GlobalVariable *GV = module->getNamedGlobal(Name); |
| 315 | |
| 316 | // If this global has internal linkage, or if it has a use, then it must be |
| 317 | // an old-style (llvmgcc3) static ctor with __main linked in and in use. If |
| 318 | // this is the case, don't execute any of the global ctors, __main will do |
| 319 | // it. |
Rafael Espindola | a168fc9 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 320 | if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return; |
Evan Cheng | 50a5882 | 2008-09-30 15:51:21 +0000 | [diff] [blame] | 321 | |
| 322 | // Should be an array of '{ int, void ()* }' structs. The first value is |
| 323 | // the init priority, which we ignore. |
| 324 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 325 | if (!InitList) return; |
| 326 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 327 | if (ConstantStruct *CS = |
| 328 | dyn_cast<ConstantStruct>(InitList->getOperand(i))) { |
| 329 | if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. |
| 330 | |
| 331 | Constant *FP = CS->getOperand(1); |
| 332 | if (FP->isNullValue()) |
| 333 | break; // Found a null terminator, exit. |
| 334 | |
| 335 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) |
| 336 | if (CE->isCast()) |
| 337 | FP = CE->getOperand(0); |
| 338 | if (Function *F = dyn_cast<Function>(FP)) { |
| 339 | // Execute the ctor/dtor function! |
| 340 | runFunction(F, std::vector<GenericValue>()); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /// runStaticConstructorsDestructors - This method is used to execute all of |
| 346 | /// the static constructors or destructors for a program, depending on the |
| 347 | /// value of isDtors. |
| 348 | void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) { |
| 349 | // Execute global ctors/dtors for each module in the program. |
| 350 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 351 | runStaticConstructorsDestructors(Modules[m], isDtors); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Dan Gohman | c7e1ad0 | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 354 | #ifndef NDEBUG |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 355 | /// isTargetNullPtr - Return whether the target pointer stored at Loc is null. |
| 356 | static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) { |
| 357 | unsigned PtrSize = EE->getTargetData()->getPointerSize(); |
| 358 | for (unsigned i = 0; i < PtrSize; ++i) |
| 359 | if (*(i + (uint8_t*)Loc)) |
| 360 | return false; |
| 361 | return true; |
| 362 | } |
Dan Gohman | c7e1ad0 | 2008-08-26 01:38:29 +0000 | [diff] [blame] | 363 | #endif |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 364 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 365 | /// runFunctionAsMain - This is a helper function which wraps runFunction to |
| 366 | /// handle the common task of starting up main with the specified argc, argv, |
| 367 | /// and envp parameters. |
| 368 | int ExecutionEngine::runFunctionAsMain(Function *Fn, |
| 369 | const std::vector<std::string> &argv, |
| 370 | const char * const * envp) { |
| 371 | std::vector<GenericValue> GVArgs; |
| 372 | GenericValue GVArgc; |
| 373 | GVArgc.IntVal = APInt(32, argv.size()); |
| 374 | |
| 375 | // Check main() type |
| 376 | unsigned NumArgs = Fn->getFunctionType()->getNumParams(); |
| 377 | const FunctionType *FTy = Fn->getFunctionType(); |
Benjamin Kramer | f2052d5 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 378 | const Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 379 | switch (NumArgs) { |
| 380 | case 3: |
| 381 | if (FTy->getParamType(2) != PPInt8Ty) { |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 382 | report_fatal_error("Invalid type for third argument of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 383 | } |
| 384 | // FALLS THROUGH |
| 385 | case 2: |
| 386 | if (FTy->getParamType(1) != PPInt8Ty) { |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 387 | report_fatal_error("Invalid type for second argument of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 388 | } |
| 389 | // FALLS THROUGH |
| 390 | case 1: |
Duncan Sands | e92dee1 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 391 | if (!FTy->getParamType(0)->isIntegerTy(32)) { |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 392 | report_fatal_error("Invalid type for first argument of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 393 | } |
| 394 | // FALLS THROUGH |
| 395 | case 0: |
Duncan Sands | 10343d9 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 396 | if (!FTy->getReturnType()->isIntegerTy() && |
Benjamin Kramer | f2052d5 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 397 | !FTy->getReturnType()->isVoidTy()) { |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 398 | report_fatal_error("Invalid return type of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 399 | } |
| 400 | break; |
| 401 | default: |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 402 | report_fatal_error("Invalid number of arguments of main() supplied"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 405 | ArgvArray CArgv; |
| 406 | ArgvArray CEnv; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 407 | if (NumArgs) { |
| 408 | GVArgs.push_back(GVArgc); // Arg #0 = argc. |
| 409 | if (NumArgs > 1) { |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 410 | // Arg #1 = argv. |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 411 | GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv))); |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 412 | assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 413 | "argv[0] was null after CreateArgv"); |
| 414 | if (NumArgs > 2) { |
| 415 | std::vector<std::string> EnvVars; |
| 416 | for (unsigned i = 0; envp[i]; ++i) |
| 417 | EnvVars.push_back(envp[i]); |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 418 | // Arg #2 = envp. |
Jeffrey Yasskin | 900dd36 | 2010-03-26 00:59:12 +0000 | [diff] [blame] | 419 | GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | } |
| 423 | return runFunction(Fn, GVArgs).IntVal.getZExtValue(); |
| 424 | } |
| 425 | |
| 426 | /// If possible, create a JIT, unless the caller specifically requests an |
| 427 | /// Interpreter or there's an error. If even an Interpreter cannot be created, |
| 428 | /// NULL is returned. |
| 429 | /// |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 430 | ExecutionEngine *ExecutionEngine::create(Module *M, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 431 | bool ForceInterpreter, |
Evan Cheng | a6394fc | 2008-08-08 08:11:34 +0000 | [diff] [blame] | 432 | std::string *ErrorStr, |
Jeffrey Yasskin | 892956a | 2009-07-08 21:59:57 +0000 | [diff] [blame] | 433 | CodeGenOpt::Level OptLevel, |
| 434 | bool GVsWithCode) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 435 | return EngineBuilder(M) |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 436 | .setEngineKind(ForceInterpreter |
| 437 | ? EngineKind::Interpreter |
| 438 | : EngineKind::JIT) |
| 439 | .setErrorStr(ErrorStr) |
| 440 | .setOptLevel(OptLevel) |
| 441 | .setAllocateGVsWithCode(GVsWithCode) |
| 442 | .create(); |
| 443 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 444 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 445 | ExecutionEngine *EngineBuilder::create() { |
Nick Lewycky | baa3a03 | 2008-03-08 02:49:45 +0000 | [diff] [blame] | 446 | // Make sure we can resolve symbols in the program as well. The zero arg |
| 447 | // to the function tells DynamicLibrary to load the program, not a library. |
| 448 | if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr)) |
| 449 | return 0; |
| 450 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 451 | // If the user specified a memory manager but didn't specify which engine to |
| 452 | // create, we assume they only want the JIT, and we fail if they only want |
| 453 | // the interpreter. |
| 454 | if (JMM) { |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 455 | if (WhichEngine & EngineKind::JIT) |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 456 | WhichEngine = EngineKind::JIT; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 457 | else { |
Chris Lattner | 03344a2 | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 458 | if (ErrorStr) |
| 459 | *ErrorStr = "Cannot create an interpreter with a memory manager."; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 460 | return 0; |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 463 | |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 464 | // Unless the interpreter was explicitly selected or the JIT is not linked, |
| 465 | // try making a JIT. |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 466 | if (WhichEngine & EngineKind::JIT) { |
| 467 | if (ExecutionEngine::JITCtor) { |
| 468 | ExecutionEngine *EE = |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 469 | ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, |
Jeffrey Yasskin | 403335c | 2010-02-05 16:19:36 +0000 | [diff] [blame] | 470 | AllocateGVsWithCode, CMModel, |
| 471 | MArch, MCPU, MAttrs); |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 472 | if (EE) return EE; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 473 | } |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // If we can't make a JIT and we didn't request one specifically, try making |
| 477 | // an interpreter instead. |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 478 | if (WhichEngine & EngineKind::Interpreter) { |
| 479 | if (ExecutionEngine::InterpCtor) |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 480 | return ExecutionEngine::InterpCtor(M, ErrorStr); |
Chris Lattner | 03344a2 | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 481 | if (ErrorStr) |
| 482 | *ErrorStr = "Interpreter has not been linked in."; |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 483 | return 0; |
Reid Kleckner | fa3585b | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 484 | } |
Chris Lattner | 03344a2 | 2009-09-23 02:03:49 +0000 | [diff] [blame] | 485 | |
| 486 | if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) { |
| 487 | if (ErrorStr) |
| 488 | *ErrorStr = "JIT has not been linked in."; |
| 489 | } |
Chris Lattner | cd0d937 | 2009-09-23 01:46:04 +0000 | [diff] [blame] | 490 | return 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /// getPointerToGlobal - This returns the address of the specified global |
| 494 | /// value. This may involve code generation if it's a function. |
| 495 | /// |
| 496 | void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { |
| 497 | if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) |
| 498 | return getPointerToFunction(F); |
| 499 | |
| 500 | MutexGuard locked(lock); |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 501 | void *p = EEState.getGlobalAddressMap(locked)[GV]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 502 | if (p) |
| 503 | return p; |
| 504 | |
| 505 | // Global variable might have been added since interpreter started. |
| 506 | if (GlobalVariable *GVar = |
| 507 | const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV))) |
| 508 | EmitGlobalVariable(GVar); |
| 509 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 510 | llvm_unreachable("Global hasn't had an address allocated yet!"); |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 511 | return EEState.getGlobalAddressMap(locked)[GV]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /// This function converts a Constant* into a GenericValue. The interesting |
| 515 | /// part is if C is a ConstantExpr. |
Reid Spencer | 10ffdf1 | 2007-08-11 15:57:56 +0000 | [diff] [blame] | 516 | /// @brief Get a GenericValue for a Constant* |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 517 | GenericValue ExecutionEngine::getConstantValue(const Constant *C) { |
| 518 | // If its undefined, return the garbage. |
Jay Foad | 1c91e67 | 2010-01-15 08:32:58 +0000 | [diff] [blame] | 519 | if (isa<UndefValue>(C)) { |
| 520 | GenericValue Result; |
| 521 | switch (C->getType()->getTypeID()) { |
| 522 | case Type::IntegerTyID: |
| 523 | case Type::X86_FP80TyID: |
| 524 | case Type::FP128TyID: |
| 525 | case Type::PPC_FP128TyID: |
| 526 | // Although the value is undefined, we still have to construct an APInt |
| 527 | // with the correct bit width. |
| 528 | Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0); |
| 529 | break; |
| 530 | default: |
| 531 | break; |
| 532 | } |
| 533 | return Result; |
| 534 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 535 | |
| 536 | // If the value is a ConstantExpr |
| 537 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { |
| 538 | Constant *Op0 = CE->getOperand(0); |
| 539 | switch (CE->getOpcode()) { |
| 540 | case Instruction::GetElementPtr: { |
| 541 | // Compute the index |
| 542 | GenericValue Result = getConstantValue(Op0); |
| 543 | SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end()); |
| 544 | uint64_t Offset = |
| 545 | TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size()); |
| 546 | |
| 547 | char* tmp = (char*) Result.PointerVal; |
| 548 | Result = PTOGV(tmp + Offset); |
| 549 | return Result; |
| 550 | } |
| 551 | case Instruction::Trunc: { |
| 552 | GenericValue GV = getConstantValue(Op0); |
| 553 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 554 | GV.IntVal = GV.IntVal.trunc(BitWidth); |
| 555 | return GV; |
| 556 | } |
| 557 | case Instruction::ZExt: { |
| 558 | GenericValue GV = getConstantValue(Op0); |
| 559 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 560 | GV.IntVal = GV.IntVal.zext(BitWidth); |
| 561 | return GV; |
| 562 | } |
| 563 | case Instruction::SExt: { |
| 564 | GenericValue GV = getConstantValue(Op0); |
| 565 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
| 566 | GV.IntVal = GV.IntVal.sext(BitWidth); |
| 567 | return GV; |
| 568 | } |
| 569 | case Instruction::FPTrunc: { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 570 | // FIXME long double |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 571 | GenericValue GV = getConstantValue(Op0); |
| 572 | GV.FloatVal = float(GV.DoubleVal); |
| 573 | return GV; |
| 574 | } |
| 575 | case Instruction::FPExt:{ |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 576 | // FIXME long double |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 577 | GenericValue GV = getConstantValue(Op0); |
| 578 | GV.DoubleVal = double(GV.FloatVal); |
| 579 | return GV; |
| 580 | } |
| 581 | case Instruction::UIToFP: { |
| 582 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 583 | if (CE->getType()->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 584 | GV.FloatVal = float(GV.IntVal.roundToDouble()); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 585 | else if (CE->getType()->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 586 | GV.DoubleVal = GV.IntVal.roundToDouble(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 587 | else if (CE->getType()->isX86_FP80Ty()) { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 588 | const uint64_t zero[] = {0, 0}; |
| 589 | APFloat apf = APFloat(APInt(80, 2, zero)); |
Dan Gohman | 8faf868 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 590 | (void)apf.convertFromAPInt(GV.IntVal, |
| 591 | false, |
| 592 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 593 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 594 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 595 | return GV; |
| 596 | } |
| 597 | case Instruction::SIToFP: { |
| 598 | GenericValue GV = getConstantValue(Op0); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 599 | if (CE->getType()->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 600 | GV.FloatVal = float(GV.IntVal.signedRoundToDouble()); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 601 | else if (CE->getType()->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 602 | GV.DoubleVal = GV.IntVal.signedRoundToDouble(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 603 | else if (CE->getType()->isX86_FP80Ty()) { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 604 | const uint64_t zero[] = { 0, 0}; |
| 605 | APFloat apf = APFloat(APInt(80, 2, zero)); |
Dan Gohman | 8faf868 | 2008-02-29 01:27:13 +0000 | [diff] [blame] | 606 | (void)apf.convertFromAPInt(GV.IntVal, |
| 607 | true, |
| 608 | APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 609 | GV.IntVal = apf.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 610 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 611 | return GV; |
| 612 | } |
| 613 | case Instruction::FPToUI: // double->APInt conversion handles sign |
| 614 | case Instruction::FPToSI: { |
| 615 | GenericValue GV = getConstantValue(Op0); |
| 616 | uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 617 | if (Op0->getType()->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 618 | GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 619 | else if (Op0->getType()->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 620 | GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 621 | else if (Op0->getType()->isX86_FP80Ty()) { |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 622 | APFloat apf = APFloat(GV.IntVal); |
| 623 | uint64_t v; |
Dale Johannesen | 6e547b4 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 624 | bool ignored; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 625 | (void)apf.convertToInteger(&v, BitWidth, |
| 626 | CE->getOpcode()==Instruction::FPToSI, |
Dale Johannesen | 6e547b4 | 2008-10-09 23:00:39 +0000 | [diff] [blame] | 627 | APFloat::rmTowardZero, &ignored); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 628 | GV.IntVal = v; // endian? |
| 629 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 630 | return GV; |
| 631 | } |
| 632 | case Instruction::PtrToInt: { |
| 633 | GenericValue GV = getConstantValue(Op0); |
| 634 | uint32_t PtrWidth = TD->getPointerSizeInBits(); |
| 635 | GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal)); |
| 636 | return GV; |
| 637 | } |
| 638 | case Instruction::IntToPtr: { |
| 639 | GenericValue GV = getConstantValue(Op0); |
| 640 | uint32_t PtrWidth = TD->getPointerSizeInBits(); |
| 641 | if (PtrWidth != GV.IntVal.getBitWidth()) |
| 642 | GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth); |
| 643 | assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width"); |
| 644 | GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue())); |
| 645 | return GV; |
| 646 | } |
| 647 | case Instruction::BitCast: { |
| 648 | GenericValue GV = getConstantValue(Op0); |
| 649 | const Type* DestTy = CE->getType(); |
| 650 | switch (Op0->getType()->getTypeID()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 651 | default: llvm_unreachable("Invalid bitcast operand"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 652 | case Type::IntegerTyID: |
Duncan Sands | e92dee1 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 653 | assert(DestTy->isFloatingPointTy() && "invalid bitcast"); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 654 | if (DestTy->isFloatTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 655 | GV.FloatVal = GV.IntVal.bitsToFloat(); |
Chris Lattner | 82cdc06 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 656 | else if (DestTy->isDoubleTy()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 657 | GV.DoubleVal = GV.IntVal.bitsToDouble(); |
| 658 | break; |
| 659 | case Type::FloatTyID: |
Duncan Sands | e92dee1 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 660 | assert(DestTy->isIntegerTy(32) && "Invalid bitcast"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 661 | GV.IntVal.floatToBits(GV.FloatVal); |
| 662 | break; |
| 663 | case Type::DoubleTyID: |
Duncan Sands | e92dee1 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 664 | assert(DestTy->isIntegerTy(64) && "Invalid bitcast"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 665 | GV.IntVal.doubleToBits(GV.DoubleVal); |
| 666 | break; |
| 667 | case Type::PointerTyID: |
Duncan Sands | 10343d9 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 668 | assert(DestTy->isPointerTy() && "Invalid bitcast"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 669 | break; // getConstantValue(Op0) above already converted it |
| 670 | } |
| 671 | return GV; |
| 672 | } |
| 673 | case Instruction::Add: |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 674 | case Instruction::FAdd: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 675 | case Instruction::Sub: |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 676 | case Instruction::FSub: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 677 | case Instruction::Mul: |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 678 | case Instruction::FMul: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 679 | case Instruction::UDiv: |
| 680 | case Instruction::SDiv: |
| 681 | case Instruction::URem: |
| 682 | case Instruction::SRem: |
| 683 | case Instruction::And: |
| 684 | case Instruction::Or: |
| 685 | case Instruction::Xor: { |
| 686 | GenericValue LHS = getConstantValue(Op0); |
| 687 | GenericValue RHS = getConstantValue(CE->getOperand(1)); |
| 688 | GenericValue GV; |
| 689 | switch (CE->getOperand(0)->getType()->getTypeID()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 690 | default: llvm_unreachable("Bad add type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 691 | case Type::IntegerTyID: |
| 692 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 693 | default: llvm_unreachable("Invalid integer opcode"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 694 | case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break; |
| 695 | case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break; |
| 696 | case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break; |
| 697 | case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break; |
| 698 | case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break; |
| 699 | case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break; |
| 700 | case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break; |
| 701 | case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break; |
| 702 | case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break; |
| 703 | case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break; |
| 704 | } |
| 705 | break; |
| 706 | case Type::FloatTyID: |
| 707 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 708 | default: llvm_unreachable("Invalid float opcode"); |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 709 | case Instruction::FAdd: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 710 | GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 711 | case Instruction::FSub: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 712 | GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 713 | case Instruction::FMul: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 714 | GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break; |
| 715 | case Instruction::FDiv: |
| 716 | GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break; |
| 717 | case Instruction::FRem: |
| 718 | GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break; |
| 719 | } |
| 720 | break; |
| 721 | case Type::DoubleTyID: |
| 722 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 723 | default: llvm_unreachable("Invalid double opcode"); |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 724 | case Instruction::FAdd: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 725 | GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 726 | case Instruction::FSub: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 727 | GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 728 | case Instruction::FMul: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 729 | GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break; |
| 730 | case Instruction::FDiv: |
| 731 | GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break; |
| 732 | case Instruction::FRem: |
| 733 | GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break; |
| 734 | } |
| 735 | break; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 736 | case Type::X86_FP80TyID: |
| 737 | case Type::PPC_FP128TyID: |
| 738 | case Type::FP128TyID: { |
| 739 | APFloat apfLHS = APFloat(LHS.IntVal); |
| 740 | switch (CE->getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 741 | default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0); |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 742 | case Instruction::FAdd: |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 743 | apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 744 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 745 | break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 746 | case Instruction::FSub: |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 747 | apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 748 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 749 | break; |
Dan Gohman | 7ce405e | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 750 | case Instruction::FMul: |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 751 | apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 752 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 753 | break; |
| 754 | case Instruction::FDiv: |
| 755 | apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 756 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 757 | break; |
| 758 | case Instruction::FRem: |
| 759 | apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven); |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 760 | GV.IntVal = apfLHS.bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 761 | break; |
| 762 | } |
| 763 | } |
| 764 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 765 | } |
| 766 | return GV; |
| 767 | } |
| 768 | default: |
| 769 | break; |
| 770 | } |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 771 | std::string msg; |
| 772 | raw_string_ostream Msg(msg); |
| 773 | Msg << "ConstantExpr not handled: " << *CE; |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 774 | report_fatal_error(Msg.str()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | GenericValue Result; |
| 778 | switch (C->getType()->getTypeID()) { |
| 779 | case Type::FloatTyID: |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 780 | Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 781 | break; |
| 782 | case Type::DoubleTyID: |
Dale Johannesen | b9de9f0 | 2007-09-06 18:13:44 +0000 | [diff] [blame] | 783 | Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 784 | break; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 785 | case Type::X86_FP80TyID: |
| 786 | case Type::FP128TyID: |
| 787 | case Type::PPC_FP128TyID: |
Dale Johannesen | 49cc7ce | 2008-10-09 18:53:47 +0000 | [diff] [blame] | 788 | Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt(); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 789 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 790 | case Type::IntegerTyID: |
| 791 | Result.IntVal = cast<ConstantInt>(C)->getValue(); |
| 792 | break; |
| 793 | case Type::PointerTyID: |
| 794 | if (isa<ConstantPointerNull>(C)) |
| 795 | Result.PointerVal = 0; |
| 796 | else if (const Function *F = dyn_cast<Function>(C)) |
| 797 | Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F))); |
Chris Lattner | bfa6b85 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 798 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 799 | Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV))); |
Chris Lattner | bfa6b85 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 800 | else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) |
| 801 | Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>( |
| 802 | BA->getBasicBlock()))); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 803 | else |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 804 | llvm_unreachable("Unknown constant pointer type!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 805 | break; |
| 806 | default: |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 807 | std::string msg; |
| 808 | raw_string_ostream Msg(msg); |
| 809 | Msg << "ERROR: Constant unimplemented for type: " << *C->getType(); |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 810 | report_fatal_error(Msg.str()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 811 | } |
| 812 | return Result; |
| 813 | } |
| 814 | |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 815 | /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst |
| 816 | /// with the integer held in IntVal. |
| 817 | static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst, |
| 818 | unsigned StoreBytes) { |
| 819 | assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!"); |
| 820 | uint8_t *Src = (uint8_t *)IntVal.getRawData(); |
| 821 | |
Chris Lattner | fcaa7f8 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 822 | if (sys::isLittleEndianHost()) |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 823 | // Little-endian host - the source is ordered from LSB to MSB. Order the |
| 824 | // destination from LSB to MSB: Do a straight copy. |
| 825 | memcpy(Dst, Src, StoreBytes); |
| 826 | else { |
| 827 | // Big-endian host - the source is an array of 64 bit words ordered from |
| 828 | // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination |
| 829 | // from MSB to LSB: Reverse the word order, but not the bytes in a word. |
| 830 | while (StoreBytes > sizeof(uint64_t)) { |
| 831 | StoreBytes -= sizeof(uint64_t); |
| 832 | // May not be aligned so use memcpy. |
| 833 | memcpy(Dst + StoreBytes, Src, sizeof(uint64_t)); |
| 834 | Src += sizeof(uint64_t); |
| 835 | } |
| 836 | |
| 837 | memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes); |
| 838 | } |
| 839 | } |
| 840 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 841 | /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr |
| 842 | /// is the address of the memory at which to store Val, cast to GenericValue *. |
| 843 | /// It is not a pointer to a GenericValue containing the address at which to |
| 844 | /// store Val. |
Evan Cheng | d0e5e98 | 2008-11-04 06:10:31 +0000 | [diff] [blame] | 845 | void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, |
| 846 | GenericValue *Ptr, const Type *Ty) { |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 847 | const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty); |
| 848 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 849 | switch (Ty->getTypeID()) { |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 850 | case Type::IntegerTyID: |
| 851 | StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 852 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 853 | case Type::FloatTyID: |
| 854 | *((float*)Ptr) = Val.FloatVal; |
| 855 | break; |
| 856 | case Type::DoubleTyID: |
| 857 | *((double*)Ptr) = Val.DoubleVal; |
| 858 | break; |
Dale Johannesen | 2f29456 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 859 | case Type::X86_FP80TyID: |
| 860 | memcpy(Ptr, Val.IntVal.getRawData(), 10); |
| 861 | break; |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 862 | case Type::PointerTyID: |
| 863 | // Ensure 64 bit target pointers are fully initialized on 32 bit hosts. |
| 864 | if (StoreBytes != sizeof(PointerTy)) |
| 865 | memset(Ptr, 0, StoreBytes); |
| 866 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 867 | *((PointerTy*)Ptr) = Val.PointerVal; |
| 868 | break; |
| 869 | default: |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 870 | dbgs() << "Cannot store value of type " << *Ty << "!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 871 | } |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 872 | |
Chris Lattner | fcaa7f8 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 873 | if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian()) |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 874 | // Host and target are different endian - reverse the stored bytes. |
| 875 | std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr); |
| 876 | } |
| 877 | |
| 878 | /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting |
| 879 | /// from Src into IntVal, which is assumed to be wide enough and to hold zero. |
| 880 | static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) { |
| 881 | assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!"); |
| 882 | uint8_t *Dst = (uint8_t *)IntVal.getRawData(); |
| 883 | |
Chris Lattner | fcaa7f8 | 2009-01-22 19:53:00 +0000 | [diff] [blame] | 884 | if (sys::isLittleEndianHost()) |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 885 | // Little-endian host - the destination must be ordered from LSB to MSB. |
| 886 | // The source is ordered from LSB to MSB: Do a straight copy. |
| 887 | memcpy(Dst, Src, LoadBytes); |
| 888 | else { |
| 889 | // Big-endian - the destination is an array of 64 bit words ordered from |
| 890 | // LSW to MSW. Each word must be ordered from MSB to LSB. The source is |
| 891 | // ordered from MSB to LSB: Reverse the word order, but not the bytes in |
| 892 | // a word. |
| 893 | while (LoadBytes > sizeof(uint64_t)) { |
| 894 | LoadBytes -= sizeof(uint64_t); |
| 895 | // May not be aligned so use memcpy. |
| 896 | memcpy(Dst, Src + LoadBytes, sizeof(uint64_t)); |
| 897 | Dst += sizeof(uint64_t); |
| 898 | } |
| 899 | |
| 900 | memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes); |
| 901 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /// FIXME: document |
| 905 | /// |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 906 | void ExecutionEngine::LoadValueFromMemory(GenericValue &Result, |
Duncan Sands | f06c7a6 | 2008-03-10 16:38:37 +0000 | [diff] [blame] | 907 | GenericValue *Ptr, |
| 908 | const Type *Ty) { |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 909 | const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty); |
Duncan Sands | 7feee8f | 2007-12-10 17:43:13 +0000 | [diff] [blame] | 910 | |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 911 | switch (Ty->getTypeID()) { |
| 912 | case Type::IntegerTyID: |
| 913 | // An APInt with all words initially zero. |
| 914 | Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0); |
| 915 | LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes); |
| 916 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 917 | case Type::FloatTyID: |
| 918 | Result.FloatVal = *((float*)Ptr); |
| 919 | break; |
| 920 | case Type::DoubleTyID: |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 921 | Result.DoubleVal = *((double*)Ptr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 922 | break; |
Duncan Sands | e0a2b30 | 2007-12-14 19:38:31 +0000 | [diff] [blame] | 923 | case Type::PointerTyID: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 924 | Result.PointerVal = *((PointerTy*)Ptr); |
| 925 | break; |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 926 | case Type::X86_FP80TyID: { |
| 927 | // This is endian dependent, but it will only work on x86 anyway. |
Duncan Sands | 1d641aa | 2007-12-15 17:37:40 +0000 | [diff] [blame] | 928 | // FIXME: Will not trap if loading a signaling NaN. |
Dale Johannesen | 2f29456 | 2009-03-24 18:16:17 +0000 | [diff] [blame] | 929 | uint64_t y[2]; |
| 930 | memcpy(y, Ptr, 10); |
Duncan Sands | 8d00dd0 | 2007-11-28 10:36:19 +0000 | [diff] [blame] | 931 | Result.IntVal = APInt(80, 2, y); |
Dale Johannesen | c560da6 | 2007-09-17 18:44:13 +0000 | [diff] [blame] | 932 | break; |
| 933 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 934 | default: |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 935 | std::string msg; |
| 936 | raw_string_ostream Msg(msg); |
| 937 | Msg << "Cannot load value of type " << *Ty << "!"; |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 938 | report_fatal_error(Msg.str()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | // InitializeMemory - Recursive function to apply a Constant value into the |
| 943 | // specified memory location... |
| 944 | // |
| 945 | void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 946 | DEBUG(dbgs() << "JIT: Initializing " << Addr << " "); |
Dale Johannesen | 0ba4a0e | 2008-08-07 01:30:15 +0000 | [diff] [blame] | 947 | DEBUG(Init->dump()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 948 | if (isa<UndefValue>(Init)) { |
| 949 | return; |
| 950 | } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) { |
| 951 | unsigned ElementSize = |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 952 | getTargetData()->getTypeAllocSize(CP->getType()->getElementType()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 953 | for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) |
| 954 | InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize); |
| 955 | return; |
Chris Lattner | bfd482d | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 956 | } else if (isa<ConstantAggregateZero>(Init)) { |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 957 | memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType())); |
Chris Lattner | bfd482d | 2008-02-15 00:57:28 +0000 | [diff] [blame] | 958 | return; |
Dan Gohman | 61dbdbd | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 959 | } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) { |
| 960 | unsigned ElementSize = |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 961 | getTargetData()->getTypeAllocSize(CPA->getType()->getElementType()); |
Dan Gohman | 61dbdbd | 2008-05-20 03:20:09 +0000 | [diff] [blame] | 962 | for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i) |
| 963 | InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize); |
| 964 | return; |
| 965 | } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) { |
| 966 | const StructLayout *SL = |
| 967 | getTargetData()->getStructLayout(cast<StructType>(CPS->getType())); |
| 968 | for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i) |
| 969 | InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i)); |
| 970 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 971 | } else if (Init->getType()->isFirstClassType()) { |
| 972 | GenericValue Val = getConstantValue(Init); |
| 973 | StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); |
| 974 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 975 | } |
| 976 | |
David Greene | 7050bcc | 2010-01-05 01:27:39 +0000 | [diff] [blame] | 977 | dbgs() << "Bad Type: " << *Init->getType() << "\n"; |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 978 | llvm_unreachable("Unknown constant type to initialize memory with!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /// EmitGlobals - Emit all of the global variables to memory, storing their |
| 982 | /// addresses into GlobalAddress. This must make sure to copy the contents of |
| 983 | /// their initializers into the memory. |
| 984 | /// |
| 985 | void ExecutionEngine::emitGlobals() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 986 | |
| 987 | // Loop over all of the global variables in the program, allocating the memory |
| 988 | // to hold them. If there is more than one module, do a prepass over globals |
| 989 | // to figure out how the different modules should link together. |
| 990 | // |
| 991 | std::map<std::pair<std::string, const Type*>, |
| 992 | const GlobalValue*> LinkedGlobalsMap; |
| 993 | |
| 994 | if (Modules.size() != 1) { |
| 995 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 996 | Module &M = *Modules[m]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 997 | for (Module::const_global_iterator I = M.global_begin(), |
| 998 | E = M.global_end(); I != E; ++I) { |
| 999 | const GlobalValue *GV = I; |
Rafael Espindola | a168fc9 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 1000 | if (GV->hasLocalLinkage() || GV->isDeclaration() || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1001 | GV->hasAppendingLinkage() || !GV->hasName()) |
| 1002 | continue;// Ignore external globals and globals with internal linkage. |
| 1003 | |
| 1004 | const GlobalValue *&GVEntry = |
| 1005 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 1006 | |
| 1007 | // If this is the first time we've seen this global, it is the canonical |
| 1008 | // version. |
| 1009 | if (!GVEntry) { |
| 1010 | GVEntry = GV; |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | // If the existing global is strong, never replace it. |
| 1015 | if (GVEntry->hasExternalLinkage() || |
| 1016 | GVEntry->hasDLLImportLinkage() || |
| 1017 | GVEntry->hasDLLExportLinkage()) |
| 1018 | continue; |
| 1019 | |
| 1020 | // Otherwise, we know it's linkonce/weak, replace it if this is a strong |
Dale Johannesen | 49c4412 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 1021 | // symbol. FIXME is this right for common? |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1022 | if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage()) |
| 1023 | GVEntry = GV; |
| 1024 | } |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | std::vector<const GlobalValue*> NonCanonicalGlobals; |
| 1029 | for (unsigned m = 0, e = Modules.size(); m != e; ++m) { |
Jeffrey Yasskin | 62de4e7 | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1030 | Module &M = *Modules[m]; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1031 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 1032 | I != E; ++I) { |
| 1033 | // In the multi-module case, see what this global maps to. |
| 1034 | if (!LinkedGlobalsMap.empty()) { |
| 1035 | if (const GlobalValue *GVEntry = |
| 1036 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) { |
| 1037 | // If something else is the canonical global, ignore this one. |
| 1038 | if (GVEntry != &*I) { |
| 1039 | NonCanonicalGlobals.push_back(I); |
| 1040 | continue; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | if (!I->isDeclaration()) { |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1046 | addGlobalMapping(I, getMemoryForGV(I)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1047 | } else { |
| 1048 | // External variable reference. Try to use the dynamic loader to |
| 1049 | // get a pointer to it. |
| 1050 | if (void *SymAddr = |
Daniel Dunbar | 9198e93 | 2009-07-21 08:54:24 +0000 | [diff] [blame] | 1051 | sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1052 | addGlobalMapping(I, SymAddr); |
| 1053 | else { |
Chris Lattner | 8316f2d | 2010-04-07 22:58:41 +0000 | [diff] [blame^] | 1054 | report_fatal_error("Could not resolve external global address: " |
Edwin Török | f7cbfea | 2009-07-07 17:32:34 +0000 | [diff] [blame] | 1055 | +I->getName()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // If there are multiple modules, map the non-canonical globals to their |
| 1061 | // canonical location. |
| 1062 | if (!NonCanonicalGlobals.empty()) { |
| 1063 | for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) { |
| 1064 | const GlobalValue *GV = NonCanonicalGlobals[i]; |
| 1065 | const GlobalValue *CGV = |
| 1066 | LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())]; |
| 1067 | void *Ptr = getPointerToGlobalIfAvailable(CGV); |
| 1068 | assert(Ptr && "Canonical global wasn't codegen'd!"); |
Nuno Lopes | ec8dccb | 2008-10-14 10:04:52 +0000 | [diff] [blame] | 1069 | addGlobalMapping(GV, Ptr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | // Now that all of the globals are set up in memory, loop through them all |
| 1074 | // and initialize their contents. |
| 1075 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 1076 | I != E; ++I) { |
| 1077 | if (!I->isDeclaration()) { |
| 1078 | if (!LinkedGlobalsMap.empty()) { |
| 1079 | if (const GlobalValue *GVEntry = |
| 1080 | LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) |
| 1081 | if (GVEntry != &*I) // Not the canonical variable. |
| 1082 | continue; |
| 1083 | } |
| 1084 | EmitGlobalVariable(I); |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | // EmitGlobalVariable - This method emits the specified global variable to the |
| 1091 | // address specified in GlobalAddresses, or allocates new memory if it's not |
| 1092 | // already in the map. |
| 1093 | void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) { |
| 1094 | void *GA = getPointerToGlobalIfAvailable(GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1095 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1096 | if (GA == 0) { |
| 1097 | // If it's not already specified, allocate memory for the global. |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1098 | GA = getMemoryForGV(GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1099 | addGlobalMapping(GV, GA); |
| 1100 | } |
Nicolas Geoffray | 46fa153 | 2008-10-25 15:41:43 +0000 | [diff] [blame] | 1101 | |
| 1102 | // Don't initialize if it's thread local, let the client do it. |
| 1103 | if (!GV->isThreadLocal()) |
| 1104 | InitializeMemory(GV->getInitializer(), GA); |
| 1105 | |
| 1106 | const Type *ElTy = GV->getType()->getElementType(); |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1107 | size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1108 | NumInitBytes += (unsigned)GVSize; |
| 1109 | ++NumGlobals; |
| 1110 | } |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1111 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1112 | ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE) |
| 1113 | : EE(EE), GlobalAddressMap(this) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
Jeffrey Yasskin | 6ddfe5c | 2009-10-23 22:37:43 +0000 | [diff] [blame] | 1116 | sys::Mutex *ExecutionEngineState::AddressMapConfig::getMutex( |
| 1117 | ExecutionEngineState *EES) { |
| 1118 | return &EES->EE.lock; |
| 1119 | } |
| 1120 | void ExecutionEngineState::AddressMapConfig::onDelete( |
| 1121 | ExecutionEngineState *EES, const GlobalValue *Old) { |
| 1122 | void *OldVal = EES->GlobalAddressMap.lookup(Old); |
| 1123 | EES->GlobalAddressReverseMap.erase(OldVal); |
| 1124 | } |
| 1125 | |
| 1126 | void ExecutionEngineState::AddressMapConfig::onRAUW( |
| 1127 | ExecutionEngineState *, const GlobalValue *, const GlobalValue *) { |
Jeffrey Yasskin | 2be2403 | 2009-10-13 17:42:08 +0000 | [diff] [blame] | 1128 | assert(false && "The ExecutionEngine doesn't know how to handle a" |
| 1129 | " RAUW on a value it has a global mapping for."); |
| 1130 | } |