blob: 0135a5285e7dad52d55c5c86a8d5b82dbbb2911d [file] [log] [blame]
Misha Brukman857c21b2003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00009//
Chris Lattner996fe012002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
Daniel Dunbar868e3f02010-11-13 02:48:57 +000016#include "llvm/ADT/SmallString.h"
Chris Lattner390d78b2009-08-23 22:49:13 +000017#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/ExecutionEngine/JITMemoryManager.h"
20#include "llvm/ExecutionEngine/ObjectCache.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Operator.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000026#include "llvm/IR/ValueHandle.h"
David Blaikie35907d82014-04-29 22:04:55 +000027#include "llvm/Object/ObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000028#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/DynamicLibrary.h"
Torok Edwin6c2d2332009-07-07 17:32:34 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Host.h"
Chris Lattner6d8dd182006-05-08 22:00:52 +000032#include "llvm/Support/MutexGuard.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Support/TargetRegistry.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000034#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +000035#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000036#include <cmath>
37#include <cstring>
Chris Lattner29681de2003-11-19 21:08:57 +000038using namespace llvm;
Chris Lattner996fe012002-12-24 00:01:05 +000039
Chandler Carruthf58e3762014-04-22 03:04:17 +000040#define DEBUG_TYPE "jit"
41
Chris Lattnerc346ecd2006-12-19 22:43:32 +000042STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
43STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattner996fe012002-12-24 00:01:05 +000044
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000045// Pin the vtable to this file.
46void ObjectCache::anchor() {}
47void ObjectBuffer::anchor() {}
48void ObjectBufferStream::anchor() {}
49
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000050ExecutionEngine *(*ExecutionEngine::JITCtor)(
51 Module *M,
52 std::string *ErrorStr,
53 JITMemoryManager *JMM,
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000054 bool GVsWithCode,
Craig Topper2617dcc2014-04-15 06:32:26 +000055 TargetMachine *TM) = nullptr;
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000056ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
57 Module *M,
58 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +000059 RTDyldMemoryManager *MCJMM,
Craig Topper2617dcc2014-04-15 06:32:26 +000060 TargetMachine *TM) = nullptr;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000061ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
Craig Topper2617dcc2014-04-15 06:32:26 +000062 std::string *ErrorStr) =nullptr;
Chris Lattner2d52c1b2006-03-22 06:07:50 +000063
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000064ExecutionEngine::ExecutionEngine(Module *M)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +000065 : EEState(*this),
Craig Topper2617dcc2014-04-15 06:32:26 +000066 LazyFunctionCreator(nullptr) {
Jeffrey Yasskin4567db42009-10-27 20:30:28 +000067 CompilingLazily = false;
Evan Chengcdc00602008-09-24 16:25:55 +000068 GVCompilationDisabled = false;
Evan Cheng84a90552008-06-17 16:49:02 +000069 SymbolSearchingDisabled = false;
Lang Hamesbc876012014-04-18 06:48:23 +000070
71 // IR module verification is enabled by default in debug builds, and disabled
72 // by default in release builds.
73#ifndef NDEBUG
74 VerifyModules = true;
75#else
76 VerifyModules = false;
77#endif
78
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000079 Modules.push_back(M);
80 assert(M && "Module is null?");
Misha Brukman260b0c82003-10-16 21:18:05 +000081}
82
Brian Gaeke92f8b302003-09-04 22:57:27 +000083ExecutionEngine::~ExecutionEngine() {
Reid Spencer603682a2007-03-03 18:19:18 +000084 clearAllGlobalMappings();
Chris Lattner0621cae2006-08-16 01:24:12 +000085 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
86 delete Modules[i];
Brian Gaeke92f8b302003-09-04 22:57:27 +000087}
88
Jeffrey Yasskina4044332010-03-27 04:53:56 +000089namespace {
Daniel Dunbar868e3f02010-11-13 02:48:57 +000090/// \brief Helper class which uses a value handler to automatically deletes the
91/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskina4044332010-03-27 04:53:56 +000092class GVMemoryBlock : public CallbackVH {
93 GVMemoryBlock(const GlobalVariable *GV)
94 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
95
96public:
Daniel Dunbar868e3f02010-11-13 02:48:57 +000097 /// \brief Returns the address the GlobalVariable should be written into. The
98 /// GVMemoryBlock object prefixes that.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000099 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattner229907c2011-07-18 04:54:35 +0000100 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000101 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
102 void *RawMemory = ::operator new(
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000103 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000104 TD.getPreferredAlignment(GV))
105 + GVSize);
106 new(RawMemory) GVMemoryBlock(GV);
107 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
108 }
109
Craig Topperb51ff602014-03-08 07:51:20 +0000110 void deleted() override {
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000111 // We allocated with operator new and with some extra memory hanging off the
112 // end, so don't just delete this. I'm not sure if this is actually
113 // required.
114 this->~GVMemoryBlock();
115 ::operator delete(this);
116 }
117};
118} // anonymous namespace
119
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000120char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000121 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000122}
123
David Blaikie35907d82014-04-29 22:04:55 +0000124void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
125 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
126}
127
Rafael Espindolaacfd6282014-08-01 18:49:24 +0000128void ExecutionEngine::addArchive(std::unique_ptr<object::Archive> A) {
129 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
130}
131
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000132bool ExecutionEngine::removeModule(Module *M) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000133 for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
Devang Patel324fe892007-10-15 19:56:32 +0000134 E = Modules.end(); I != E; ++I) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000135 Module *Found = *I;
136 if (Found == M) {
Devang Patel324fe892007-10-15 19:56:32 +0000137 Modules.erase(I);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000138 clearGlobalMappingsFromModule(M);
139 return true;
Devang Patel324fe892007-10-15 19:56:32 +0000140 }
141 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000142 return false;
Nate Begeman617001d2009-01-23 19:27:28 +0000143}
144
Chris Lattner0621cae2006-08-16 01:24:12 +0000145Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
146 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000147 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattner0621cae2006-08-16 01:24:12 +0000148 return F;
149 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000150 return nullptr;
Chris Lattner0621cae2006-08-16 01:24:12 +0000151}
152
153
Zachary Turner2f825df2014-06-16 20:54:28 +0000154void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000155 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000156 void *OldVal;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000157
158 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
159 // GlobalAddressMap.
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000160 if (I == GlobalAddressMap.end())
Craig Topper2617dcc2014-04-15 06:32:26 +0000161 OldVal = nullptr;
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000162 else {
163 OldVal = I->second;
164 GlobalAddressMap.erase(I);
165 }
166
167 GlobalAddressReverseMap.erase(OldVal);
168 return OldVal;
169}
170
Chris Lattner6d8dd182006-05-08 22:00:52 +0000171void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000172 MutexGuard locked(lock);
Evan Cheng5cc53c32008-09-18 07:54:21 +0000173
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000174 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000175 << "\' to [" << Addr << "]\n";);
Zachary Turner2f825df2014-06-16 20:54:28 +0000176 void *&CurVal = EEState.getGlobalAddressMap()[GV];
Craig Topper2617dcc2014-04-15 06:32:26 +0000177 assert((!CurVal || !Addr) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000178 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000179
180 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000181 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000182 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000183 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000184 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000185 V = GV;
186 }
187}
188
Chris Lattner6d8dd182006-05-08 22:00:52 +0000189void ExecutionEngine::clearAllGlobalMappings() {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000190 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000191
Zachary Turner2f825df2014-06-16 20:54:28 +0000192 EEState.getGlobalAddressMap().clear();
193 EEState.getGlobalAddressReverseMap().clear();
Chris Lattner6d8dd182006-05-08 22:00:52 +0000194}
195
Nate Begeman8f83fc42008-05-21 16:34:48 +0000196void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000197 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000198
199 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000200 EEState.RemoveMapping(FI);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000201 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
202 GI != GE; ++GI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000203 EEState.RemoveMapping(GI);
Nate Begeman8f83fc42008-05-21 16:34:48 +0000204}
205
Chris Lattneree181732008-04-04 04:47:41 +0000206void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000207 MutexGuard locked(lock);
Chris Lattneree181732008-04-04 04:47:41 +0000208
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000209 ExecutionEngineState::GlobalAddressMapTy &Map =
Zachary Turner2f825df2014-06-16 20:54:28 +0000210 EEState.getGlobalAddressMap();
Chris Lattneree181732008-04-04 04:47:41 +0000211
Chris Lattner6d8dd182006-05-08 22:00:52 +0000212 // Deleting from the mapping?
Craig Topper2617dcc2014-04-15 06:32:26 +0000213 if (!Addr)
Zachary Turner2f825df2014-06-16 20:54:28 +0000214 return EEState.RemoveMapping(GV);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000215
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000216 void *&CurVal = Map[GV];
Chris Lattneree181732008-04-04 04:47:41 +0000217 void *OldVal = CurVal;
218
Zachary Turner2f825df2014-06-16 20:54:28 +0000219 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
220 EEState.getGlobalAddressReverseMap().erase(CurVal);
Chris Lattner6d8dd182006-05-08 22:00:52 +0000221 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000222
223 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000224 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000225 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000226 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000227 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000228 V = GV;
229 }
Chris Lattneree181732008-04-04 04:47:41 +0000230 return OldVal;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000231}
232
Chris Lattner6d8dd182006-05-08 22:00:52 +0000233void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000234 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000235
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000236 ExecutionEngineState::GlobalAddressMapTy::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000237 EEState.getGlobalAddressMap().find(GV);
238 return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000239}
240
Chris Lattner748e8572003-12-31 20:21:04 +0000241const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000242 MutexGuard locked(lock);
Reid Spencer79876f52005-07-12 15:51:55 +0000243
Chris Lattner748e8572003-12-31 20:21:04 +0000244 // If we haven't computed the reverse mapping yet, do so first.
Zachary Turner2f825df2014-06-16 20:54:28 +0000245 if (EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000246 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Zachary Turner2f825df2014-06-16 20:54:28 +0000247 I = EEState.getGlobalAddressMap().begin(),
248 E = EEState.getGlobalAddressMap().end(); I != E; ++I)
249 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
Daniel Dunbare4f47432010-11-13 00:55:42 +0000250 I->second, I->first));
Chris Lattner748e8572003-12-31 20:21:04 +0000251 }
252
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000253 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000254 EEState.getGlobalAddressReverseMap().find(Addr);
255 return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
Chris Lattner748e8572003-12-31 20:21:04 +0000256}
Chris Lattner5a0d4822003-12-26 06:50:30 +0000257
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000258namespace {
259class ArgvArray {
260 char *Array;
261 std::vector<char*> Values;
262public:
Craig Topper2617dcc2014-04-15 06:32:26 +0000263 ArgvArray() : Array(nullptr) {}
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000264 ~ArgvArray() { clear(); }
265 void clear() {
266 delete[] Array;
Craig Topper2617dcc2014-04-15 06:32:26 +0000267 Array = nullptr;
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000268 for (size_t I = 0, E = Values.size(); I != E; ++I) {
269 delete[] Values[I];
270 }
271 Values.clear();
272 }
273 /// Turn a vector of strings into a nice argv style array of pointers to null
274 /// terminated strings.
275 void *reset(LLVMContext &C, ExecutionEngine *EE,
276 const std::vector<std::string> &InputArgv);
277};
278} // anonymous namespace
279void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
280 const std::vector<std::string> &InputArgv) {
281 clear(); // Free the old contents.
Chandler Carruth5da3f052012-11-01 09:14:31 +0000282 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000283 Array = new char[(InputArgv.size()+1)*PtrSize];
Chris Lattner5a0d4822003-12-26 06:50:30 +0000284
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000285 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
Chris Lattner229907c2011-07-18 04:54:35 +0000286 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000287
288 for (unsigned i = 0; i != InputArgv.size(); ++i) {
289 unsigned Size = InputArgv[i].size()+1;
290 char *Dest = new char[Size];
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000291 Values.push_back(Dest);
David Greene0967d2d2010-01-05 01:27:39 +0000292 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukman835702a2005-04-21 22:36:52 +0000293
Chris Lattner5a0d4822003-12-26 06:50:30 +0000294 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
295 Dest[Size-1] = 0;
Misha Brukman835702a2005-04-21 22:36:52 +0000296
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000297 // Endian safe: Array[i] = (PointerTy)Dest;
298 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000299 SBytePtr);
300 }
301
302 // Null terminate it
Craig Topper2617dcc2014-04-15 06:32:26 +0000303 EE->StoreValueToMemory(PTOGV(nullptr),
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000304 (GenericValue*)(Array+InputArgv.size()*PtrSize),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000305 SBytePtr);
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000306 return Array;
Chris Lattner5a0d4822003-12-26 06:50:30 +0000307}
308
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000309void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
310 bool isDtors) {
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000311 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000312 GlobalVariable *GV = module->getNamedGlobal(Name);
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000313
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000314 // If this global has internal linkage, or if it has a use, then it must be
315 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
316 // this is the case, don't execute any of the global ctors, __main will do
317 // it.
318 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
319
Nick Lewycky0cbfcb22011-04-06 20:38:44 +0000320 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000321 // the init priority, which we ignore.
Chris Lattner00245f42012-01-24 13:41:11 +0000322 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
Craig Topper2617dcc2014-04-15 06:32:26 +0000323 if (!InitList)
Nick Lewycky0f857892011-04-11 22:11:20 +0000324 return;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000325 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner00245f42012-01-24 13:41:11 +0000326 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
Craig Topper2617dcc2014-04-15 06:32:26 +0000327 if (!CS) continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000328
329 Constant *FP = CS->getOperand(1);
330 if (FP->isNullValue())
Nick Lewycky0f857892011-04-11 22:11:20 +0000331 continue; // Found a sentinal value, ignore.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000332
333 // Strip off constant expression casts.
334 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
335 if (CE->isCast())
336 FP = CE->getOperand(0);
337
338 // Execute the ctor/dtor function!
339 if (Function *F = dyn_cast<Function>(FP))
340 runFunction(F, std::vector<GenericValue>());
341
342 // FIXME: It is marginally lame that we just do nothing here if we see an
343 // entry we don't recognize. It might not be unreasonable for the verifier
344 // to not even allow this and just assert here.
345 }
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000346}
347
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000348void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
349 // Execute global ctors/dtors for each module in the program.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000350 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
351 runStaticConstructorsDestructors(Modules[i], isDtors);
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000352}
353
Dan Gohmancf3e3012008-08-26 01:38:29 +0000354#ifndef NDEBUG
Duncan Sands1202d1b2007-12-14 19:38:31 +0000355/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
356static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000357 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000358 for (unsigned i = 0; i < PtrSize; ++i)
359 if (*(i + (uint8_t*)Loc))
360 return false;
361 return true;
362}
Dan Gohmancf3e3012008-08-26 01:38:29 +0000363#endif
Duncan Sands1202d1b2007-12-14 19:38:31 +0000364
Chris Lattner5a0d4822003-12-26 06:50:30 +0000365int ExecutionEngine::runFunctionAsMain(Function *Fn,
366 const std::vector<std::string> &argv,
367 const char * const * envp) {
368 std::vector<GenericValue> GVArgs;
369 GenericValue GVArgc;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000370 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov8c32c112007-06-03 19:17:35 +0000371
372 // Check main() type
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000373 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattner229907c2011-07-18 04:54:35 +0000374 FunctionType *FTy = Fn->getFunctionType();
375 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000376
377 // Check the argument types.
378 if (NumArgs > 3)
379 report_fatal_error("Invalid number of arguments of main() supplied");
380 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
381 report_fatal_error("Invalid type for third argument of main() supplied");
382 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
383 report_fatal_error("Invalid type for second argument of main() supplied");
384 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
385 report_fatal_error("Invalid type for first argument of main() supplied");
386 if (!FTy->getReturnType()->isIntegerTy() &&
387 !FTy->getReturnType()->isVoidTy())
388 report_fatal_error("Invalid return type of main() supplied");
389
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000390 ArgvArray CArgv;
391 ArgvArray CEnv;
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000392 if (NumArgs) {
393 GVArgs.push_back(GVArgc); // Arg #0 = argc.
394 if (NumArgs > 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000395 // Arg #1 = argv.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000396 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands1202d1b2007-12-14 19:38:31 +0000397 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000398 "argv[0] was null after CreateArgv");
399 if (NumArgs > 2) {
400 std::vector<std::string> EnvVars;
401 for (unsigned i = 0; envp[i]; ++i)
402 EnvVars.push_back(envp[i]);
Owen Anderson55f1c092009-08-13 21:58:54 +0000403 // Arg #2 = envp.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000404 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000405 }
406 }
407 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000408
Reid Spencer87aa65f2007-03-06 03:04:04 +0000409 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000410}
411
Alp Toker322db9e2014-05-31 21:26:17 +0000412void EngineBuilder::InitEngine() {
413 WhichEngine = EngineKind::Either;
414 ErrorStr = nullptr;
415 OptLevel = CodeGenOpt::Default;
416 MCJMM = nullptr;
417 JMM = nullptr;
418 Options = TargetOptions();
419 AllocateGVsWithCode = false;
420 RelocModel = Reloc::Default;
421 CMModel = CodeModel::JITDefault;
422 UseMCJIT = false;
423
424// IR module verification is enabled by default in debug builds, and disabled
425// by default in release builds.
426#ifndef NDEBUG
427 VerifyModules = true;
428#else
429 VerifyModules = false;
430#endif
431}
432
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000433ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000434 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000435
Nick Lewyckya53414f2008-03-08 02:49:45 +0000436 // Make sure we can resolve symbols in the program as well. The zero arg
437 // to the function tells DynamicLibrary to load the program, not a library.
Craig Topper2617dcc2014-04-15 06:32:26 +0000438 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
439 return nullptr;
Nick Lewyckya53414f2008-03-08 02:49:45 +0000440
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000441 assert(!(JMM && MCJMM));
442
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000443 // If the user specified a memory manager but didn't specify which engine to
444 // create, we assume they only want the JIT, and we fail if they only want
445 // the interpreter.
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000446 if (JMM || MCJMM) {
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000447 if (WhichEngine & EngineKind::JIT)
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000448 WhichEngine = EngineKind::JIT;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000449 else {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000450 if (ErrorStr)
451 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000452 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000453 }
454 }
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000455
456 if (MCJMM && ! UseMCJIT) {
457 if (ErrorStr)
458 *ErrorStr =
459 "Cannot create a legacy JIT with a runtime dyld memory "
460 "manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000461 return nullptr;
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000462 }
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000463
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000464 // Unless the interpreter was explicitly selected or the JIT is not linked,
465 // try making a JIT.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000466 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000467 Triple TT(M->getTargetTriple());
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000468 if (!TM->getTarget().hasJIT()) {
469 errs() << "WARNING: This target JIT is not designed for the host"
470 << " you are running. If bad things happen, please choose"
471 << " a different -march switch.\n";
472 }
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000473
Lang Hamesbc876012014-04-18 06:48:23 +0000474 ExecutionEngine *EE = nullptr;
475 if (UseMCJIT && ExecutionEngine::MCJITCtor)
476 EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
Rafael Espindolab9a23cd2014-07-31 01:14:09 +0000477 TheTM.release());
Lang Hamesbc876012014-04-18 06:48:23 +0000478 else if (ExecutionEngine::JITCtor)
479 EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
480 AllocateGVsWithCode, TheTM.release());
481
482 if (EE) {
483 EE->setVerifyModules(VerifyModules);
484 return EE;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000485 }
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000486 }
487
488 // If we can't make a JIT and we didn't request one specifically, try making
489 // an interpreter instead.
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000490 if (WhichEngine & EngineKind::Interpreter) {
491 if (ExecutionEngine::InterpCtor)
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000492 return ExecutionEngine::InterpCtor(M, ErrorStr);
Chris Lattner8bcc6442009-09-23 02:03:49 +0000493 if (ErrorStr)
494 *ErrorStr = "Interpreter has not been linked in.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000495 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000496 }
Chris Lattner8bcc6442009-09-23 02:03:49 +0000497
Craig Topper2617dcc2014-04-15 06:32:26 +0000498 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
499 !ExecutionEngine::MCJITCtor) {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000500 if (ErrorStr)
501 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000502 }
503
Craig Topper2617dcc2014-04-15 06:32:26 +0000504 return nullptr;
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000505}
506
Chris Lattner996fe012002-12-24 00:01:05 +0000507void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke1678e852003-08-13 18:16:14 +0000508 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattner996fe012002-12-24 00:01:05 +0000509 return getPointerToFunction(F);
510
Zachary Turnerc04b8922014-06-20 21:07:14 +0000511 MutexGuard locked(lock);
Zachary Turner2f825df2014-06-16 20:54:28 +0000512 if (void *P = EEState.getGlobalAddressMap()[GV])
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000513 return P;
Jeff Cohen69e849012006-02-07 05:11:57 +0000514
515 // Global variable might have been added since interpreter started.
516 if (GlobalVariable *GVar =
517 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
518 EmitGlobalVariable(GVar);
519 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000520 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000521
Zachary Turner2f825df2014-06-16 20:54:28 +0000522 return EEState.getGlobalAddressMap()[GV];
Chris Lattner996fe012002-12-24 00:01:05 +0000523}
524
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000525/// \brief Converts a Constant* into a GenericValue, including handling of
526/// ConstantExpr values.
Chris Lattner996fe012002-12-24 00:01:05 +0000527GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000528 // If its undefined, return the garbage.
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000529 if (isa<UndefValue>(C)) {
530 GenericValue Result;
531 switch (C->getType()->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000532 default:
533 break;
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000534 case Type::IntegerTyID:
535 case Type::X86_FP80TyID:
536 case Type::FP128TyID:
537 case Type::PPC_FP128TyID:
538 // Although the value is undefined, we still have to construct an APInt
539 // with the correct bit width.
540 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
541 break;
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000542 case Type::StructTyID: {
543 // if the whole struct is 'undef' just reserve memory for the value.
544 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
545 unsigned int elemNum = STy->getNumElements();
546 Result.AggregateVal.resize(elemNum);
547 for (unsigned int i = 0; i < elemNum; ++i) {
548 Type *ElemTy = STy->getElementType(i);
549 if (ElemTy->isIntegerTy())
550 Result.AggregateVal[i].IntVal =
551 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
552 else if (ElemTy->isAggregateType()) {
553 const Constant *ElemUndef = UndefValue::get(ElemTy);
554 Result.AggregateVal[i] = getConstantValue(ElemUndef);
555 }
556 }
557 }
558 }
559 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000560 case Type::VectorTyID:
561 // if the whole vector is 'undef' just reserve memory for the value.
562 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
563 const Type *ElemTy = VTy->getElementType();
564 unsigned int elemNum = VTy->getNumElements();
565 Result.AggregateVal.resize(elemNum);
566 if (ElemTy->isIntegerTy())
567 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000568 Result.AggregateVal[i].IntVal =
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000569 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000570 break;
571 }
572 return Result;
573 }
Chris Lattner9de0d142003-04-23 19:01:49 +0000574
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000575 // Otherwise, if the value is a ConstantExpr...
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000576 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000577 Constant *Op0 = CE->getOperand(0);
Chris Lattner9de0d142003-04-23 19:01:49 +0000578 switch (CE->getOpcode()) {
579 case Instruction::GetElementPtr: {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000580 // Compute the index
Reid Spencer4fd528f2007-03-06 22:23:15 +0000581 GenericValue Result = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000582 APInt Offset(DL->getPointerSizeInBits(), 0);
583 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
Misha Brukman835702a2005-04-21 22:36:52 +0000584
Reid Spencer87aa65f2007-03-06 03:04:04 +0000585 char* tmp = (char*) Result.PointerVal;
Nuno Lopesb6ad9822012-12-30 16:25:48 +0000586 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattner9de0d142003-04-23 19:01:49 +0000587 return Result;
588 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000589 case Instruction::Trunc: {
590 GenericValue GV = getConstantValue(Op0);
591 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
592 GV.IntVal = GV.IntVal.trunc(BitWidth);
593 return GV;
594 }
595 case Instruction::ZExt: {
596 GenericValue GV = getConstantValue(Op0);
597 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
598 GV.IntVal = GV.IntVal.zext(BitWidth);
599 return GV;
600 }
601 case Instruction::SExt: {
602 GenericValue GV = getConstantValue(Op0);
603 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
604 GV.IntVal = GV.IntVal.sext(BitWidth);
605 return GV;
606 }
607 case Instruction::FPTrunc: {
Dale Johannesena1336cf2007-09-17 18:44:13 +0000608 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000609 GenericValue GV = getConstantValue(Op0);
610 GV.FloatVal = float(GV.DoubleVal);
611 return GV;
612 }
613 case Instruction::FPExt:{
Dale Johannesena1336cf2007-09-17 18:44:13 +0000614 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000615 GenericValue GV = getConstantValue(Op0);
616 GV.DoubleVal = double(GV.FloatVal);
617 return GV;
618 }
619 case Instruction::UIToFP: {
620 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000621 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000622 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000623 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000624 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000625 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000626 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000627 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000628 false,
629 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000630 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000631 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000632 return GV;
633 }
634 case Instruction::SIToFP: {
635 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000636 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000637 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000638 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000639 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000640 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000641 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000642 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000643 true,
644 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000645 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000646 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000647 return GV;
648 }
649 case Instruction::FPToUI: // double->APInt conversion handles sign
650 case Instruction::FPToSI: {
651 GenericValue GV = getConstantValue(Op0);
652 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000653 if (Op0->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000654 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000655 else if (Op0->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000656 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000657 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover29178a32013-01-22 09:46:31 +0000658 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000659 uint64_t v;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000660 bool ignored;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000661 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000662 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000663 APFloat::rmTowardZero, &ignored);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000664 GV.IntVal = v; // endian?
665 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000666 return GV;
667 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000668 case Instruction::PtrToInt: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000669 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000670 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000671 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000672 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000673 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000674 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000675 return GV;
676 }
677 case Instruction::IntToPtr: {
678 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000679 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000680 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000681 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
682 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000683 return GV;
684 }
685 case Instruction::BitCast: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000686 GenericValue GV = getConstantValue(Op0);
Chris Lattner229907c2011-07-18 04:54:35 +0000687 Type* DestTy = CE->getType();
Reid Spencer4fd528f2007-03-06 22:23:15 +0000688 switch (Op0->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000689 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000690 case Type::IntegerTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000691 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnerfdd87902009-10-05 05:54:46 +0000692 if (DestTy->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000693 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000694 else if (DestTy->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000695 GV.DoubleVal = GV.IntVal.bitsToDouble();
696 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000697 case Type::FloatTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000698 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000699 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000700 break;
701 case Type::DoubleTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000702 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000703 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000704 break;
705 case Type::PointerTyID:
Duncan Sands19d0b472010-02-16 11:11:14 +0000706 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000707 break; // getConstantValue(Op0) above already converted it
708 }
709 return GV;
Chris Lattner9de0d142003-04-23 19:01:49 +0000710 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000711 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000712 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000713 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000714 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000715 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000716 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000717 case Instruction::UDiv:
718 case Instruction::SDiv:
719 case Instruction::URem:
720 case Instruction::SRem:
721 case Instruction::And:
722 case Instruction::Or:
723 case Instruction::Xor: {
724 GenericValue LHS = getConstantValue(Op0);
725 GenericValue RHS = getConstantValue(CE->getOperand(1));
726 GenericValue GV;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000727 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000728 default: llvm_unreachable("Bad add type!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000729 case Type::IntegerTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000730 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000731 default: llvm_unreachable("Invalid integer opcode");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000732 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
733 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
734 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
735 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
736 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
737 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
738 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
739 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
740 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
741 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
742 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000743 break;
744 case Type::FloatTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000745 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000746 default: llvm_unreachable("Invalid float opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000747 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000748 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000749 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000750 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000751 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000752 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000753 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000754 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000755 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000756 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000757 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000758 break;
759 case Type::DoubleTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000760 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000761 default: llvm_unreachable("Invalid double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000762 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000763 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000764 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000765 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000766 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000767 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000768 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000769 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000770 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000771 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000772 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000773 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000774 case Type::X86_FP80TyID:
775 case Type::PPC_FP128TyID:
776 case Type::FP128TyID: {
Tim Northover29178a32013-01-22 09:46:31 +0000777 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
778 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000779 switch (CE->getOpcode()) {
Daniel Dunbare4f47432010-11-13 00:55:42 +0000780 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000781 case Instruction::FAdd:
Tim Northover29178a32013-01-22 09:46:31 +0000782 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000783 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000784 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000785 case Instruction::FSub:
Tim Northover29178a32013-01-22 09:46:31 +0000786 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
787 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000788 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000789 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000790 case Instruction::FMul:
Tim Northover29178a32013-01-22 09:46:31 +0000791 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
792 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000793 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000794 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000795 case Instruction::FDiv:
Tim Northover29178a32013-01-22 09:46:31 +0000796 apfLHS.divide(APFloat(Sem, RHS.IntVal),
797 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000798 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000799 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000800 case Instruction::FRem:
Tim Northover29178a32013-01-22 09:46:31 +0000801 apfLHS.mod(APFloat(Sem, RHS.IntVal),
802 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000803 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000804 break;
805 }
806 }
807 break;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000808 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000809 return GV;
810 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000811 default:
812 break;
813 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000814
815 SmallString<256> Msg;
816 raw_svector_ostream OS(Msg);
817 OS << "ConstantExpr not handled: " << *CE;
818 report_fatal_error(OS.str());
Chris Lattner68cbcc32003-05-14 17:51:49 +0000819 }
Misha Brukman835702a2005-04-21 22:36:52 +0000820
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000821 // Otherwise, we have a simple constant.
Reid Spencer4fd528f2007-03-06 22:23:15 +0000822 GenericValue Result;
Chris Lattner6b727592004-06-17 18:19:28 +0000823 switch (C->getType()->getTypeID()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000824 case Type::FloatTyID:
825 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000826 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000827 case Type::DoubleTyID:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000828 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer87aa65f2007-03-06 03:04:04 +0000829 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000830 case Type::X86_FP80TyID:
831 case Type::FP128TyID:
832 case Type::PPC_FP128TyID:
Dale Johannesen54306fe2008-10-09 18:53:47 +0000833 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000834 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000835 case Type::IntegerTyID:
836 Result.IntVal = cast<ConstantInt>(C)->getValue();
837 break;
Chris Lattner996fe012002-12-24 00:01:05 +0000838 case Type::PointerTyID:
Reid Spencer6a0fd732004-07-18 00:41:27 +0000839 if (isa<ConstantPointerNull>(C))
Craig Topper2617dcc2014-04-15 06:32:26 +0000840 Result.PointerVal = nullptr;
Reid Spencer6a0fd732004-07-18 00:41:27 +0000841 else if (const Function *F = dyn_cast<Function>(C))
842 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000843 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer6a0fd732004-07-18 00:41:27 +0000844 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000845 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
846 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
847 BA->getBasicBlock())));
Reid Spencer6a0fd732004-07-18 00:41:27 +0000848 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000849 llvm_unreachable("Unknown constant pointer type!");
Chris Lattner996fe012002-12-24 00:01:05 +0000850 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000851 case Type::VectorTyID: {
852 unsigned elemNum;
853 Type* ElemTy;
854 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
855 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
856 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
857
858 if (CDV) {
859 elemNum = CDV->getNumElements();
860 ElemTy = CDV->getElementType();
861 } else if (CV || CAZ) {
862 VectorType* VTy = dyn_cast<VectorType>(C->getType());
863 elemNum = VTy->getNumElements();
864 ElemTy = VTy->getElementType();
865 } else {
866 llvm_unreachable("Unknown constant vector type!");
867 }
868
869 Result.AggregateVal.resize(elemNum);
870 // Check if vector holds floats.
871 if(ElemTy->isFloatTy()) {
872 if (CAZ) {
873 GenericValue floatZero;
874 floatZero.FloatVal = 0.f;
875 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
876 floatZero);
877 break;
878 }
879 if(CV) {
880 for (unsigned i = 0; i < elemNum; ++i)
881 if (!isa<UndefValue>(CV->getOperand(i)))
882 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
883 CV->getOperand(i))->getValueAPF().convertToFloat();
884 break;
885 }
886 if(CDV)
887 for (unsigned i = 0; i < elemNum; ++i)
888 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
889
890 break;
891 }
892 // Check if vector holds doubles.
893 if (ElemTy->isDoubleTy()) {
894 if (CAZ) {
895 GenericValue doubleZero;
896 doubleZero.DoubleVal = 0.0;
897 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
898 doubleZero);
899 break;
900 }
901 if(CV) {
902 for (unsigned i = 0; i < elemNum; ++i)
903 if (!isa<UndefValue>(CV->getOperand(i)))
904 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
905 CV->getOperand(i))->getValueAPF().convertToDouble();
906 break;
907 }
908 if(CDV)
909 for (unsigned i = 0; i < elemNum; ++i)
910 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
911
912 break;
913 }
914 // Check if vector holds integers.
915 if (ElemTy->isIntegerTy()) {
916 if (CAZ) {
917 GenericValue intZero;
918 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
919 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
920 intZero);
921 break;
922 }
923 if(CV) {
924 for (unsigned i = 0; i < elemNum; ++i)
925 if (!isa<UndefValue>(CV->getOperand(i)))
926 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
927 CV->getOperand(i))->getValue();
928 else {
929 Result.AggregateVal[i].IntVal =
930 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
931 }
932 break;
933 }
934 if(CDV)
935 for (unsigned i = 0; i < elemNum; ++i)
936 Result.AggregateVal[i].IntVal = APInt(
937 CDV->getElementType()->getPrimitiveSizeInBits(),
938 CDV->getElementAsInteger(i));
939
940 break;
941 }
942 llvm_unreachable("Unknown constant pointer type!");
943 }
944 break;
945
Chris Lattner996fe012002-12-24 00:01:05 +0000946 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000947 SmallString<256> Msg;
948 raw_svector_ostream OS(Msg);
949 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
950 report_fatal_error(OS.str());
Chris Lattner996fe012002-12-24 00:01:05 +0000951 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000952
Chris Lattner996fe012002-12-24 00:01:05 +0000953 return Result;
954}
955
Duncan Sands1202d1b2007-12-14 19:38:31 +0000956/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
957/// with the integer held in IntVal.
958static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
959 unsigned StoreBytes) {
960 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000961 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000962
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000963 if (sys::IsLittleEndianHost) {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000964 // Little-endian host - the source is ordered from LSB to MSB. Order the
965 // destination from LSB to MSB: Do a straight copy.
966 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000967 } else {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000968 // Big-endian host - the source is an array of 64 bit words ordered from
969 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
970 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
971 while (StoreBytes > sizeof(uint64_t)) {
972 StoreBytes -= sizeof(uint64_t);
973 // May not be aligned so use memcpy.
974 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
975 Src += sizeof(uint64_t);
976 }
977
978 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
979 }
980}
981
Evan Cheng09053e62008-11-04 06:10:31 +0000982void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattner229907c2011-07-18 04:54:35 +0000983 GenericValue *Ptr, Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000984 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1202d1b2007-12-14 19:38:31 +0000985
Reid Spencer87aa65f2007-03-06 03:04:04 +0000986 switch (Ty->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000987 default:
988 dbgs() << "Cannot store value of type " << *Ty << "!\n";
989 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +0000990 case Type::IntegerTyID:
991 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer87aa65f2007-03-06 03:04:04 +0000992 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000993 case Type::FloatTyID:
994 *((float*)Ptr) = Val.FloatVal;
995 break;
996 case Type::DoubleTyID:
997 *((double*)Ptr) = Val.DoubleVal;
998 break;
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +0000999 case Type::X86_FP80TyID:
1000 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1001 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001002 case Type::PointerTyID:
1003 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1004 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth93da3c82011-04-28 08:37:18 +00001005 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands1202d1b2007-12-14 19:38:31 +00001006
Reid Spencer87aa65f2007-03-06 03:04:04 +00001007 *((PointerTy*)Ptr) = Val.PointerVal;
1008 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001009 case Type::VectorTyID:
1010 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1011 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1012 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1013 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1014 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1015 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1016 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1017 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1018 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1019 }
1020 }
1021 break;
Chris Lattner996fe012002-12-24 00:01:05 +00001022 }
Duncan Sands1202d1b2007-12-14 19:38:31 +00001023
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001024 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands1202d1b2007-12-14 19:38:31 +00001025 // Host and target are different endian - reverse the stored bytes.
1026 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1027}
1028
1029/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1030/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1031static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1032 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greene82b63572013-01-14 21:04:45 +00001033 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1034 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands1202d1b2007-12-14 19:38:31 +00001035
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001036 if (sys::IsLittleEndianHost)
Duncan Sands1202d1b2007-12-14 19:38:31 +00001037 // Little-endian host - the destination must be ordered from LSB to MSB.
1038 // The source is ordered from LSB to MSB: Do a straight copy.
1039 memcpy(Dst, Src, LoadBytes);
1040 else {
1041 // Big-endian - the destination is an array of 64 bit words ordered from
1042 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1043 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1044 // a word.
1045 while (LoadBytes > sizeof(uint64_t)) {
1046 LoadBytes -= sizeof(uint64_t);
1047 // May not be aligned so use memcpy.
1048 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1049 Dst += sizeof(uint64_t);
1050 }
1051
1052 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1053 }
Chris Lattner996fe012002-12-24 00:01:05 +00001054}
1055
Misha Brukman857c21b2003-10-10 17:45:12 +00001056/// FIXME: document
1057///
Duncan Sands1202d1b2007-12-14 19:38:31 +00001058void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sandscd4a6be2008-03-10 16:38:37 +00001059 GenericValue *Ptr,
Chris Lattner229907c2011-07-18 04:54:35 +00001060 Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001061 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands5c65cb42007-12-10 17:43:13 +00001062
Duncan Sands1202d1b2007-12-14 19:38:31 +00001063 switch (Ty->getTypeID()) {
1064 case Type::IntegerTyID:
1065 // An APInt with all words initially zero.
1066 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1067 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1068 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +00001069 case Type::FloatTyID:
1070 Result.FloatVal = *((float*)Ptr);
1071 break;
1072 case Type::DoubleTyID:
Duncan Sands1202d1b2007-12-14 19:38:31 +00001073 Result.DoubleVal = *((double*)Ptr);
Reid Spencer87aa65f2007-03-06 03:04:04 +00001074 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001075 case Type::PointerTyID:
Reid Spencer87aa65f2007-03-06 03:04:04 +00001076 Result.PointerVal = *((PointerTy*)Ptr);
1077 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +00001078 case Type::X86_FP80TyID: {
1079 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands26d65392007-12-15 17:37:40 +00001080 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001081 uint64_t y[2];
1082 memcpy(y, Ptr, 10);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001083 Result.IntVal = APInt(80, y);
Dale Johannesena1336cf2007-09-17 18:44:13 +00001084 break;
1085 }
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001086 case Type::VectorTyID: {
1087 const VectorType *VT = cast<VectorType>(Ty);
1088 const Type *ElemT = VT->getElementType();
1089 const unsigned numElems = VT->getNumElements();
1090 if (ElemT->isFloatTy()) {
1091 Result.AggregateVal.resize(numElems);
1092 for (unsigned i = 0; i < numElems; ++i)
1093 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1094 }
1095 if (ElemT->isDoubleTy()) {
1096 Result.AggregateVal.resize(numElems);
1097 for (unsigned i = 0; i < numElems; ++i)
1098 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1099 }
1100 if (ElemT->isIntegerTy()) {
1101 GenericValue intZero;
1102 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1103 intZero.IntVal = APInt(elemBitWidth, 0);
1104 Result.AggregateVal.resize(numElems, intZero);
1105 for (unsigned i = 0; i < numElems; ++i)
1106 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1107 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1108 }
1109 break;
1110 }
Reid Spencer87aa65f2007-03-06 03:04:04 +00001111 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001112 SmallString<256> Msg;
1113 raw_svector_ostream OS(Msg);
1114 OS << "Cannot load value of type " << *Ty << "!";
1115 report_fatal_error(OS.str());
Chris Lattner7f389e82003-05-08 16:52:16 +00001116 }
Chris Lattner7f389e82003-05-08 16:52:16 +00001117}
1118
Chris Lattner996fe012002-12-24 00:01:05 +00001119void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greene0967d2d2010-01-05 01:27:39 +00001120 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesenb086d382008-08-07 01:30:15 +00001121 DEBUG(Init->dump());
Chris Lattner00245f42012-01-24 13:41:11 +00001122 if (isa<UndefValue>(Init))
Chris Lattner61753bf2004-10-16 18:19:26 +00001123 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001124
1125 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino69d62132006-01-20 18:18:40 +00001126 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001127 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino69d62132006-01-20 18:18:40 +00001128 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1129 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1130 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001131 }
1132
1133 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001134 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattner1dd86b12008-02-15 00:57:28 +00001135 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001136 }
1137
1138 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001139 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001140 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001141 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1142 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1143 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001144 }
1145
1146 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001147 const StructLayout *SL =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001148 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001149 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1150 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1151 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001152 }
1153
1154 if (const ConstantDataSequential *CDS =
1155 dyn_cast<ConstantDataSequential>(Init)) {
1156 // CDS is already laid out in host memory order.
1157 StringRef Data = CDS->getRawDataValues();
1158 memcpy(Addr, Data.data(), Data.size());
1159 return;
1160 }
1161
1162 if (Init->getType()->isFirstClassType()) {
Chris Lattner996fe012002-12-24 00:01:05 +00001163 GenericValue Val = getConstantValue(Init);
1164 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1165 return;
1166 }
1167
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001168 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinfbcc6632009-07-14 16:55:14 +00001169 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattner996fe012002-12-24 00:01:05 +00001170}
1171
Chris Lattner996fe012002-12-24 00:01:05 +00001172/// EmitGlobals - Emit all of the global variables to memory, storing their
1173/// addresses into GlobalAddress. This must make sure to copy the contents of
1174/// their initializers into the memory.
Chris Lattner996fe012002-12-24 00:01:05 +00001175void ExecutionEngine::emitGlobals() {
Chris Lattner996fe012002-12-24 00:01:05 +00001176 // Loop over all of the global variables in the program, allocating the memory
Chris Lattner0621cae2006-08-16 01:24:12 +00001177 // to hold them. If there is more than one module, do a prepass over globals
1178 // to figure out how the different modules should link together.
Chris Lattner229907c2011-07-18 04:54:35 +00001179 std::map<std::pair<std::string, Type*>,
Chris Lattner0621cae2006-08-16 01:24:12 +00001180 const GlobalValue*> LinkedGlobalsMap;
Misha Brukman835702a2005-04-21 22:36:52 +00001181
Chris Lattner0621cae2006-08-16 01:24:12 +00001182 if (Modules.size() != 1) {
1183 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001184 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001185 for (const auto &GV : M.globals()) {
1186 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1187 GV.hasAppendingLinkage() || !GV.hasName())
Chris Lattner0621cae2006-08-16 01:24:12 +00001188 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001189
1190 const GlobalValue *&GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001191 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
Chris Lattner0621cae2006-08-16 01:24:12 +00001192
1193 // If this is the first time we've seen this global, it is the canonical
1194 // version.
1195 if (!GVEntry) {
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001196 GVEntry = &GV;
Chris Lattner0621cae2006-08-16 01:24:12 +00001197 continue;
1198 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001199
Chris Lattner0621cae2006-08-16 01:24:12 +00001200 // If the existing global is strong, never replace it.
Nico Rieck7157bb72014-01-14 15:22:47 +00001201 if (GVEntry->hasExternalLinkage())
Chris Lattner0621cae2006-08-16 01:24:12 +00001202 continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001203
Chris Lattner0621cae2006-08-16 01:24:12 +00001204 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesence4396b2008-05-14 20:12:51 +00001205 // symbol. FIXME is this right for common?
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001206 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1207 GVEntry = &GV;
Chris Lattner9de0d142003-04-23 19:01:49 +00001208 }
Chris Lattner996fe012002-12-24 00:01:05 +00001209 }
Chris Lattner0621cae2006-08-16 01:24:12 +00001210 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001211
Chris Lattner0621cae2006-08-16 01:24:12 +00001212 std::vector<const GlobalValue*> NonCanonicalGlobals;
1213 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001214 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001215 for (const auto &GV : M.globals()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001216 // In the multi-module case, see what this global maps to.
1217 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001218 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001219 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001220 // If something else is the canonical global, ignore this one.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001221 if (GVEntry != &GV) {
1222 NonCanonicalGlobals.push_back(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001223 continue;
1224 }
1225 }
1226 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001227
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001228 if (!GV.isDeclaration()) {
1229 addGlobalMapping(&GV, getMemoryForGV(&GV));
Chris Lattner0621cae2006-08-16 01:24:12 +00001230 } else {
1231 // External variable reference. Try to use the dynamic loader to
1232 // get a pointer to it.
1233 if (void *SymAddr =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001234 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1235 addGlobalMapping(&GV, SymAddr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001236 else {
Chris Lattner2104b8d2010-04-07 22:58:41 +00001237 report_fatal_error("Could not resolve external global address: "
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001238 +GV.getName());
Chris Lattner0621cae2006-08-16 01:24:12 +00001239 }
1240 }
1241 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001242
Chris Lattner0621cae2006-08-16 01:24:12 +00001243 // If there are multiple modules, map the non-canonical globals to their
1244 // canonical location.
1245 if (!NonCanonicalGlobals.empty()) {
1246 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1247 const GlobalValue *GV = NonCanonicalGlobals[i];
1248 const GlobalValue *CGV =
1249 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1250 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1251 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesa67f06b2008-10-14 10:04:52 +00001252 addGlobalMapping(GV, Ptr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001253 }
1254 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001255
1256 // Now that all of the globals are set up in memory, loop through them all
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001257 // and initialize their contents.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001258 for (const auto &GV : M.globals()) {
1259 if (!GV.isDeclaration()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001260 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001261 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001262 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1263 if (GVEntry != &GV) // Not the canonical variable.
Chris Lattner0621cae2006-08-16 01:24:12 +00001264 continue;
1265 }
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001266 EmitGlobalVariable(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001267 }
1268 }
1269 }
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001270}
1271
1272// EmitGlobalVariable - This method emits the specified global variable to the
1273// address specified in GlobalAddresses, or allocates new memory if it's not
1274// already in the map.
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +00001275void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner748e8572003-12-31 20:21:04 +00001276 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattnerdc631732004-02-08 19:33:23 +00001277
Craig Topper2617dcc2014-04-15 06:32:26 +00001278 if (!GA) {
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001279 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001280 GA = getMemoryForGV(GV);
Andrew Kaylor3b442372013-11-15 17:52:54 +00001281
1282 // If we failed to allocate memory for this global, return.
Craig Topper2617dcc2014-04-15 06:32:26 +00001283 if (!GA) return;
Andrew Kaylor3b442372013-11-15 17:52:54 +00001284
Chris Lattner748e8572003-12-31 20:21:04 +00001285 addGlobalMapping(GV, GA);
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001286 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001287
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001288 // Don't initialize if it's thread local, let the client do it.
1289 if (!GV->isThreadLocal())
1290 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001291
Chris Lattner229907c2011-07-18 04:54:35 +00001292 Type *ElTy = GV->getType()->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001293 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattnerdf1f1522005-01-08 20:13:19 +00001294 NumInitBytes += (unsigned)GVSize;
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001295 ++NumGlobals;
Chris Lattner996fe012002-12-24 00:01:05 +00001296}
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001297
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001298ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1299 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001300}
1301
Zachary Turnerc04b8922014-06-20 21:07:14 +00001302sys::Mutex *
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001303ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001304 return &EES->EE.lock;
1305}
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001306
1307void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1308 const GlobalValue *Old) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001309 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1310 EES->GlobalAddressReverseMap.erase(OldVal);
1311}
1312
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001313void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1314 const GlobalValue *,
1315 const GlobalValue *) {
Craig Toppera2886c22012-02-07 05:05:23 +00001316 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1317 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001318}