blob: 063f3fb05c2de903d85765d467b4285c19b3cb23 [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"
Rafael Espindoladd396572014-08-01 19:28:15 +000027#include "llvm/Object/Archive.h"
David Blaikie35907d82014-04-29 22:04:55 +000028#include "llvm/Object/ObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/DynamicLibrary.h"
Torok Edwin6c2d2332009-07-07 17:32:34 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Host.h"
Chris Lattner6d8dd182006-05-08 22:00:52 +000033#include "llvm/Support/MutexGuard.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/TargetRegistry.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000035#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +000036#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000037#include <cmath>
38#include <cstring>
Chris Lattner29681de2003-11-19 21:08:57 +000039using namespace llvm;
Chris Lattner996fe012002-12-24 00:01:05 +000040
Chandler Carruthf58e3762014-04-22 03:04:17 +000041#define DEBUG_TYPE "jit"
42
Chris Lattnerc346ecd2006-12-19 22:43:32 +000043STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
44STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattner996fe012002-12-24 00:01:05 +000045
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000046// Pin the vtable to this file.
47void ObjectCache::anchor() {}
48void ObjectBuffer::anchor() {}
49void ObjectBufferStream::anchor() {}
50
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000051ExecutionEngine *(*ExecutionEngine::JITCtor)(
52 Module *M,
53 std::string *ErrorStr,
54 JITMemoryManager *JMM,
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000055 bool GVsWithCode,
Craig Topper2617dcc2014-04-15 06:32:26 +000056 TargetMachine *TM) = nullptr;
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000057ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
58 Module *M,
59 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +000060 RTDyldMemoryManager *MCJMM,
Craig Topper2617dcc2014-04-15 06:32:26 +000061 TargetMachine *TM) = nullptr;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000062ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
Craig Topper2617dcc2014-04-15 06:32:26 +000063 std::string *ErrorStr) =nullptr;
Chris Lattner2d52c1b2006-03-22 06:07:50 +000064
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000065ExecutionEngine::ExecutionEngine(Module *M)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +000066 : EEState(*this),
Craig Topper2617dcc2014-04-15 06:32:26 +000067 LazyFunctionCreator(nullptr) {
Jeffrey Yasskin4567db42009-10-27 20:30:28 +000068 CompilingLazily = false;
Evan Chengcdc00602008-09-24 16:25:55 +000069 GVCompilationDisabled = false;
Evan Cheng84a90552008-06-17 16:49:02 +000070 SymbolSearchingDisabled = false;
Lang Hamesbc876012014-04-18 06:48:23 +000071
72 // IR module verification is enabled by default in debug builds, and disabled
73 // by default in release builds.
74#ifndef NDEBUG
75 VerifyModules = true;
76#else
77 VerifyModules = false;
78#endif
79
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000080 Modules.push_back(M);
81 assert(M && "Module is null?");
Misha Brukman260b0c82003-10-16 21:18:05 +000082}
83
Brian Gaeke92f8b302003-09-04 22:57:27 +000084ExecutionEngine::~ExecutionEngine() {
Reid Spencer603682a2007-03-03 18:19:18 +000085 clearAllGlobalMappings();
Chris Lattner0621cae2006-08-16 01:24:12 +000086 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
87 delete Modules[i];
Brian Gaeke92f8b302003-09-04 22:57:27 +000088}
89
Jeffrey Yasskina4044332010-03-27 04:53:56 +000090namespace {
Daniel Dunbar868e3f02010-11-13 02:48:57 +000091/// \brief Helper class which uses a value handler to automatically deletes the
92/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskina4044332010-03-27 04:53:56 +000093class GVMemoryBlock : public CallbackVH {
94 GVMemoryBlock(const GlobalVariable *GV)
95 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
96
97public:
Daniel Dunbar868e3f02010-11-13 02:48:57 +000098 /// \brief Returns the address the GlobalVariable should be written into. The
99 /// GVMemoryBlock object prefixes that.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000100 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattner229907c2011-07-18 04:54:35 +0000101 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000102 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
103 void *RawMemory = ::operator new(
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000104 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000105 TD.getPreferredAlignment(GV))
106 + GVSize);
107 new(RawMemory) GVMemoryBlock(GV);
108 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
109 }
110
Craig Topperb51ff602014-03-08 07:51:20 +0000111 void deleted() override {
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000112 // We allocated with operator new and with some extra memory hanging off the
113 // end, so don't just delete this. I'm not sure if this is actually
114 // required.
115 this->~GVMemoryBlock();
116 ::operator delete(this);
117 }
118};
119} // anonymous namespace
120
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000121char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000122 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000123}
124
David Blaikie35907d82014-04-29 22:04:55 +0000125void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
126 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
127}
128
Rafael Espindolaacfd6282014-08-01 18:49:24 +0000129void ExecutionEngine::addArchive(std::unique_ptr<object::Archive> A) {
130 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
131}
132
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000133bool ExecutionEngine::removeModule(Module *M) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000134 for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
Devang Patel324fe892007-10-15 19:56:32 +0000135 E = Modules.end(); I != E; ++I) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000136 Module *Found = *I;
137 if (Found == M) {
Devang Patel324fe892007-10-15 19:56:32 +0000138 Modules.erase(I);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000139 clearGlobalMappingsFromModule(M);
140 return true;
Devang Patel324fe892007-10-15 19:56:32 +0000141 }
142 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000143 return false;
Nate Begeman617001d2009-01-23 19:27:28 +0000144}
145
Chris Lattner0621cae2006-08-16 01:24:12 +0000146Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
147 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000148 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattner0621cae2006-08-16 01:24:12 +0000149 return F;
150 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000151 return nullptr;
Chris Lattner0621cae2006-08-16 01:24:12 +0000152}
153
154
Zachary Turner2f825df2014-06-16 20:54:28 +0000155void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000156 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000157 void *OldVal;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000158
159 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
160 // GlobalAddressMap.
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000161 if (I == GlobalAddressMap.end())
Craig Topper2617dcc2014-04-15 06:32:26 +0000162 OldVal = nullptr;
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000163 else {
164 OldVal = I->second;
165 GlobalAddressMap.erase(I);
166 }
167
168 GlobalAddressReverseMap.erase(OldVal);
169 return OldVal;
170}
171
Chris Lattner6d8dd182006-05-08 22:00:52 +0000172void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000173 MutexGuard locked(lock);
Evan Cheng5cc53c32008-09-18 07:54:21 +0000174
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000175 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000176 << "\' to [" << Addr << "]\n";);
Zachary Turner2f825df2014-06-16 20:54:28 +0000177 void *&CurVal = EEState.getGlobalAddressMap()[GV];
Craig Topper2617dcc2014-04-15 06:32:26 +0000178 assert((!CurVal || !Addr) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000179 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000180
181 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000182 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000183 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000184 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000185 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000186 V = GV;
187 }
188}
189
Chris Lattner6d8dd182006-05-08 22:00:52 +0000190void ExecutionEngine::clearAllGlobalMappings() {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000191 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000192
Zachary Turner2f825df2014-06-16 20:54:28 +0000193 EEState.getGlobalAddressMap().clear();
194 EEState.getGlobalAddressReverseMap().clear();
Chris Lattner6d8dd182006-05-08 22:00:52 +0000195}
196
Nate Begeman8f83fc42008-05-21 16:34:48 +0000197void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000198 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000199
200 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000201 EEState.RemoveMapping(FI);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000202 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
203 GI != GE; ++GI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000204 EEState.RemoveMapping(GI);
Nate Begeman8f83fc42008-05-21 16:34:48 +0000205}
206
Chris Lattneree181732008-04-04 04:47:41 +0000207void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000208 MutexGuard locked(lock);
Chris Lattneree181732008-04-04 04:47:41 +0000209
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000210 ExecutionEngineState::GlobalAddressMapTy &Map =
Zachary Turner2f825df2014-06-16 20:54:28 +0000211 EEState.getGlobalAddressMap();
Chris Lattneree181732008-04-04 04:47:41 +0000212
Chris Lattner6d8dd182006-05-08 22:00:52 +0000213 // Deleting from the mapping?
Craig Topper2617dcc2014-04-15 06:32:26 +0000214 if (!Addr)
Zachary Turner2f825df2014-06-16 20:54:28 +0000215 return EEState.RemoveMapping(GV);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000216
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000217 void *&CurVal = Map[GV];
Chris Lattneree181732008-04-04 04:47:41 +0000218 void *OldVal = CurVal;
219
Zachary Turner2f825df2014-06-16 20:54:28 +0000220 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
221 EEState.getGlobalAddressReverseMap().erase(CurVal);
Chris Lattner6d8dd182006-05-08 22:00:52 +0000222 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000223
224 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000225 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000226 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000227 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000228 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000229 V = GV;
230 }
Chris Lattneree181732008-04-04 04:47:41 +0000231 return OldVal;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000232}
233
Chris Lattner6d8dd182006-05-08 22:00:52 +0000234void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000235 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000236
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000237 ExecutionEngineState::GlobalAddressMapTy::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000238 EEState.getGlobalAddressMap().find(GV);
239 return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000240}
241
Chris Lattner748e8572003-12-31 20:21:04 +0000242const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000243 MutexGuard locked(lock);
Reid Spencer79876f52005-07-12 15:51:55 +0000244
Chris Lattner748e8572003-12-31 20:21:04 +0000245 // If we haven't computed the reverse mapping yet, do so first.
Zachary Turner2f825df2014-06-16 20:54:28 +0000246 if (EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000247 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Zachary Turner2f825df2014-06-16 20:54:28 +0000248 I = EEState.getGlobalAddressMap().begin(),
249 E = EEState.getGlobalAddressMap().end(); I != E; ++I)
250 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
Daniel Dunbare4f47432010-11-13 00:55:42 +0000251 I->second, I->first));
Chris Lattner748e8572003-12-31 20:21:04 +0000252 }
253
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000254 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000255 EEState.getGlobalAddressReverseMap().find(Addr);
256 return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
Chris Lattner748e8572003-12-31 20:21:04 +0000257}
Chris Lattner5a0d4822003-12-26 06:50:30 +0000258
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000259namespace {
260class ArgvArray {
261 char *Array;
262 std::vector<char*> Values;
263public:
Craig Topper2617dcc2014-04-15 06:32:26 +0000264 ArgvArray() : Array(nullptr) {}
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000265 ~ArgvArray() { clear(); }
266 void clear() {
267 delete[] Array;
Craig Topper2617dcc2014-04-15 06:32:26 +0000268 Array = nullptr;
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000269 for (size_t I = 0, E = Values.size(); I != E; ++I) {
270 delete[] Values[I];
271 }
272 Values.clear();
273 }
274 /// Turn a vector of strings into a nice argv style array of pointers to null
275 /// terminated strings.
276 void *reset(LLVMContext &C, ExecutionEngine *EE,
277 const std::vector<std::string> &InputArgv);
278};
279} // anonymous namespace
280void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
281 const std::vector<std::string> &InputArgv) {
282 clear(); // Free the old contents.
Chandler Carruth5da3f052012-11-01 09:14:31 +0000283 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000284 Array = new char[(InputArgv.size()+1)*PtrSize];
Chris Lattner5a0d4822003-12-26 06:50:30 +0000285
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000286 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
Chris Lattner229907c2011-07-18 04:54:35 +0000287 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000288
289 for (unsigned i = 0; i != InputArgv.size(); ++i) {
290 unsigned Size = InputArgv[i].size()+1;
291 char *Dest = new char[Size];
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000292 Values.push_back(Dest);
David Greene0967d2d2010-01-05 01:27:39 +0000293 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukman835702a2005-04-21 22:36:52 +0000294
Chris Lattner5a0d4822003-12-26 06:50:30 +0000295 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
296 Dest[Size-1] = 0;
Misha Brukman835702a2005-04-21 22:36:52 +0000297
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000298 // Endian safe: Array[i] = (PointerTy)Dest;
299 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000300 SBytePtr);
301 }
302
303 // Null terminate it
Craig Topper2617dcc2014-04-15 06:32:26 +0000304 EE->StoreValueToMemory(PTOGV(nullptr),
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000305 (GenericValue*)(Array+InputArgv.size()*PtrSize),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000306 SBytePtr);
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000307 return Array;
Chris Lattner5a0d4822003-12-26 06:50:30 +0000308}
309
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000310void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
311 bool isDtors) {
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000312 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000313 GlobalVariable *GV = module->getNamedGlobal(Name);
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000314
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000315 // If this global has internal linkage, or if it has a use, then it must be
316 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
317 // this is the case, don't execute any of the global ctors, __main will do
318 // it.
319 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
320
Nick Lewycky0cbfcb22011-04-06 20:38:44 +0000321 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000322 // the init priority, which we ignore.
Chris Lattner00245f42012-01-24 13:41:11 +0000323 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
Craig Topper2617dcc2014-04-15 06:32:26 +0000324 if (!InitList)
Nick Lewycky0f857892011-04-11 22:11:20 +0000325 return;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000326 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner00245f42012-01-24 13:41:11 +0000327 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
Craig Topper2617dcc2014-04-15 06:32:26 +0000328 if (!CS) continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000329
330 Constant *FP = CS->getOperand(1);
331 if (FP->isNullValue())
Nick Lewycky0f857892011-04-11 22:11:20 +0000332 continue; // Found a sentinal value, ignore.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000333
334 // Strip off constant expression casts.
335 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
336 if (CE->isCast())
337 FP = CE->getOperand(0);
338
339 // Execute the ctor/dtor function!
340 if (Function *F = dyn_cast<Function>(FP))
341 runFunction(F, std::vector<GenericValue>());
342
343 // FIXME: It is marginally lame that we just do nothing here if we see an
344 // entry we don't recognize. It might not be unreasonable for the verifier
345 // to not even allow this and just assert here.
346 }
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000347}
348
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000349void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
350 // Execute global ctors/dtors for each module in the program.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000351 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
352 runStaticConstructorsDestructors(Modules[i], isDtors);
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000353}
354
Dan Gohmancf3e3012008-08-26 01:38:29 +0000355#ifndef NDEBUG
Duncan Sands1202d1b2007-12-14 19:38:31 +0000356/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
357static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000358 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000359 for (unsigned i = 0; i < PtrSize; ++i)
360 if (*(i + (uint8_t*)Loc))
361 return false;
362 return true;
363}
Dan Gohmancf3e3012008-08-26 01:38:29 +0000364#endif
Duncan Sands1202d1b2007-12-14 19:38:31 +0000365
Chris Lattner5a0d4822003-12-26 06:50:30 +0000366int ExecutionEngine::runFunctionAsMain(Function *Fn,
367 const std::vector<std::string> &argv,
368 const char * const * envp) {
369 std::vector<GenericValue> GVArgs;
370 GenericValue GVArgc;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000371 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov8c32c112007-06-03 19:17:35 +0000372
373 // Check main() type
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000374 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattner229907c2011-07-18 04:54:35 +0000375 FunctionType *FTy = Fn->getFunctionType();
376 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000377
378 // Check the argument types.
379 if (NumArgs > 3)
380 report_fatal_error("Invalid number of arguments of main() supplied");
381 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
382 report_fatal_error("Invalid type for third argument of main() supplied");
383 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
384 report_fatal_error("Invalid type for second argument of main() supplied");
385 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
386 report_fatal_error("Invalid type for first argument of main() supplied");
387 if (!FTy->getReturnType()->isIntegerTy() &&
388 !FTy->getReturnType()->isVoidTy())
389 report_fatal_error("Invalid return type of main() supplied");
390
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000391 ArgvArray CArgv;
392 ArgvArray CEnv;
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000393 if (NumArgs) {
394 GVArgs.push_back(GVArgc); // Arg #0 = argc.
395 if (NumArgs > 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000396 // Arg #1 = argv.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000397 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands1202d1b2007-12-14 19:38:31 +0000398 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000399 "argv[0] was null after CreateArgv");
400 if (NumArgs > 2) {
401 std::vector<std::string> EnvVars;
402 for (unsigned i = 0; envp[i]; ++i)
403 EnvVars.push_back(envp[i]);
Owen Anderson55f1c092009-08-13 21:58:54 +0000404 // Arg #2 = envp.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000405 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000406 }
407 }
408 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000409
Reid Spencer87aa65f2007-03-06 03:04:04 +0000410 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000411}
412
Alp Toker322db9e2014-05-31 21:26:17 +0000413void EngineBuilder::InitEngine() {
414 WhichEngine = EngineKind::Either;
415 ErrorStr = nullptr;
416 OptLevel = CodeGenOpt::Default;
417 MCJMM = nullptr;
418 JMM = nullptr;
419 Options = TargetOptions();
420 AllocateGVsWithCode = false;
421 RelocModel = Reloc::Default;
422 CMModel = CodeModel::JITDefault;
423 UseMCJIT = false;
424
425// IR module verification is enabled by default in debug builds, and disabled
426// by default in release builds.
427#ifndef NDEBUG
428 VerifyModules = true;
429#else
430 VerifyModules = false;
431#endif
432}
433
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000434ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000435 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000436
Nick Lewyckya53414f2008-03-08 02:49:45 +0000437 // Make sure we can resolve symbols in the program as well. The zero arg
438 // to the function tells DynamicLibrary to load the program, not a library.
Craig Topper2617dcc2014-04-15 06:32:26 +0000439 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
440 return nullptr;
Nick Lewyckya53414f2008-03-08 02:49:45 +0000441
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000442 assert(!(JMM && MCJMM));
443
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000444 // If the user specified a memory manager but didn't specify which engine to
445 // create, we assume they only want the JIT, and we fail if they only want
446 // the interpreter.
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000447 if (JMM || MCJMM) {
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000448 if (WhichEngine & EngineKind::JIT)
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000449 WhichEngine = EngineKind::JIT;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000450 else {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000451 if (ErrorStr)
452 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000453 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000454 }
455 }
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000456
457 if (MCJMM && ! UseMCJIT) {
458 if (ErrorStr)
459 *ErrorStr =
460 "Cannot create a legacy JIT with a runtime dyld memory "
461 "manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000462 return nullptr;
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000463 }
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000464
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000465 // Unless the interpreter was explicitly selected or the JIT is not linked,
466 // try making a JIT.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000467 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000468 Triple TT(M->getTargetTriple());
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000469 if (!TM->getTarget().hasJIT()) {
470 errs() << "WARNING: This target JIT is not designed for the host"
471 << " you are running. If bad things happen, please choose"
472 << " a different -march switch.\n";
473 }
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000474
Lang Hamesbc876012014-04-18 06:48:23 +0000475 ExecutionEngine *EE = nullptr;
476 if (UseMCJIT && ExecutionEngine::MCJITCtor)
477 EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
Rafael Espindolab9a23cd2014-07-31 01:14:09 +0000478 TheTM.release());
Lang Hamesbc876012014-04-18 06:48:23 +0000479 else if (ExecutionEngine::JITCtor)
480 EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
481 AllocateGVsWithCode, TheTM.release());
482
483 if (EE) {
484 EE->setVerifyModules(VerifyModules);
485 return EE;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000486 }
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000487 }
488
489 // If we can't make a JIT and we didn't request one specifically, try making
490 // an interpreter instead.
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000491 if (WhichEngine & EngineKind::Interpreter) {
492 if (ExecutionEngine::InterpCtor)
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000493 return ExecutionEngine::InterpCtor(M, ErrorStr);
Chris Lattner8bcc6442009-09-23 02:03:49 +0000494 if (ErrorStr)
495 *ErrorStr = "Interpreter has not been linked in.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000496 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000497 }
Chris Lattner8bcc6442009-09-23 02:03:49 +0000498
Craig Topper2617dcc2014-04-15 06:32:26 +0000499 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
500 !ExecutionEngine::MCJITCtor) {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000501 if (ErrorStr)
502 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000503 }
504
Craig Topper2617dcc2014-04-15 06:32:26 +0000505 return nullptr;
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000506}
507
Chris Lattner996fe012002-12-24 00:01:05 +0000508void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke1678e852003-08-13 18:16:14 +0000509 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattner996fe012002-12-24 00:01:05 +0000510 return getPointerToFunction(F);
511
Zachary Turnerc04b8922014-06-20 21:07:14 +0000512 MutexGuard locked(lock);
Zachary Turner2f825df2014-06-16 20:54:28 +0000513 if (void *P = EEState.getGlobalAddressMap()[GV])
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000514 return P;
Jeff Cohen69e849012006-02-07 05:11:57 +0000515
516 // Global variable might have been added since interpreter started.
517 if (GlobalVariable *GVar =
518 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
519 EmitGlobalVariable(GVar);
520 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000521 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000522
Zachary Turner2f825df2014-06-16 20:54:28 +0000523 return EEState.getGlobalAddressMap()[GV];
Chris Lattner996fe012002-12-24 00:01:05 +0000524}
525
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000526/// \brief Converts a Constant* into a GenericValue, including handling of
527/// ConstantExpr values.
Chris Lattner996fe012002-12-24 00:01:05 +0000528GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000529 // If its undefined, return the garbage.
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000530 if (isa<UndefValue>(C)) {
531 GenericValue Result;
532 switch (C->getType()->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000533 default:
534 break;
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000535 case Type::IntegerTyID:
536 case Type::X86_FP80TyID:
537 case Type::FP128TyID:
538 case Type::PPC_FP128TyID:
539 // Although the value is undefined, we still have to construct an APInt
540 // with the correct bit width.
541 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
542 break;
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000543 case Type::StructTyID: {
544 // if the whole struct is 'undef' just reserve memory for the value.
545 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
546 unsigned int elemNum = STy->getNumElements();
547 Result.AggregateVal.resize(elemNum);
548 for (unsigned int i = 0; i < elemNum; ++i) {
549 Type *ElemTy = STy->getElementType(i);
550 if (ElemTy->isIntegerTy())
551 Result.AggregateVal[i].IntVal =
552 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
553 else if (ElemTy->isAggregateType()) {
554 const Constant *ElemUndef = UndefValue::get(ElemTy);
555 Result.AggregateVal[i] = getConstantValue(ElemUndef);
556 }
557 }
558 }
559 }
560 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000561 case Type::VectorTyID:
562 // if the whole vector is 'undef' just reserve memory for the value.
563 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
564 const Type *ElemTy = VTy->getElementType();
565 unsigned int elemNum = VTy->getNumElements();
566 Result.AggregateVal.resize(elemNum);
567 if (ElemTy->isIntegerTy())
568 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000569 Result.AggregateVal[i].IntVal =
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000570 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000571 break;
572 }
573 return Result;
574 }
Chris Lattner9de0d142003-04-23 19:01:49 +0000575
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000576 // Otherwise, if the value is a ConstantExpr...
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000577 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000578 Constant *Op0 = CE->getOperand(0);
Chris Lattner9de0d142003-04-23 19:01:49 +0000579 switch (CE->getOpcode()) {
580 case Instruction::GetElementPtr: {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000581 // Compute the index
Reid Spencer4fd528f2007-03-06 22:23:15 +0000582 GenericValue Result = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000583 APInt Offset(DL->getPointerSizeInBits(), 0);
584 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
Misha Brukman835702a2005-04-21 22:36:52 +0000585
Reid Spencer87aa65f2007-03-06 03:04:04 +0000586 char* tmp = (char*) Result.PointerVal;
Nuno Lopesb6ad9822012-12-30 16:25:48 +0000587 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattner9de0d142003-04-23 19:01:49 +0000588 return Result;
589 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000590 case Instruction::Trunc: {
591 GenericValue GV = getConstantValue(Op0);
592 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
593 GV.IntVal = GV.IntVal.trunc(BitWidth);
594 return GV;
595 }
596 case Instruction::ZExt: {
597 GenericValue GV = getConstantValue(Op0);
598 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
599 GV.IntVal = GV.IntVal.zext(BitWidth);
600 return GV;
601 }
602 case Instruction::SExt: {
603 GenericValue GV = getConstantValue(Op0);
604 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
605 GV.IntVal = GV.IntVal.sext(BitWidth);
606 return GV;
607 }
608 case Instruction::FPTrunc: {
Dale Johannesena1336cf2007-09-17 18:44:13 +0000609 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000610 GenericValue GV = getConstantValue(Op0);
611 GV.FloatVal = float(GV.DoubleVal);
612 return GV;
613 }
614 case Instruction::FPExt:{
Dale Johannesena1336cf2007-09-17 18:44:13 +0000615 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000616 GenericValue GV = getConstantValue(Op0);
617 GV.DoubleVal = double(GV.FloatVal);
618 return GV;
619 }
620 case Instruction::UIToFP: {
621 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000622 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000623 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000624 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000625 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000626 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000627 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000628 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000629 false,
630 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000631 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000632 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000633 return GV;
634 }
635 case Instruction::SIToFP: {
636 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000637 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000638 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000639 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000640 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000641 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000642 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000643 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000644 true,
645 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000646 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000647 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000648 return GV;
649 }
650 case Instruction::FPToUI: // double->APInt conversion handles sign
651 case Instruction::FPToSI: {
652 GenericValue GV = getConstantValue(Op0);
653 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000654 if (Op0->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000655 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000656 else if (Op0->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000657 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000658 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover29178a32013-01-22 09:46:31 +0000659 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000660 uint64_t v;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000661 bool ignored;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000662 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000663 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000664 APFloat::rmTowardZero, &ignored);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000665 GV.IntVal = v; // endian?
666 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000667 return GV;
668 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000669 case Instruction::PtrToInt: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000670 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000671 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000672 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000673 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000674 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000675 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000676 return GV;
677 }
678 case Instruction::IntToPtr: {
679 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000680 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000681 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000682 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
683 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000684 return GV;
685 }
686 case Instruction::BitCast: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000687 GenericValue GV = getConstantValue(Op0);
Chris Lattner229907c2011-07-18 04:54:35 +0000688 Type* DestTy = CE->getType();
Reid Spencer4fd528f2007-03-06 22:23:15 +0000689 switch (Op0->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000690 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000691 case Type::IntegerTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000692 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnerfdd87902009-10-05 05:54:46 +0000693 if (DestTy->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000694 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000695 else if (DestTy->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000696 GV.DoubleVal = GV.IntVal.bitsToDouble();
697 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000698 case Type::FloatTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000699 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000700 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000701 break;
702 case Type::DoubleTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000703 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000704 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000705 break;
706 case Type::PointerTyID:
Duncan Sands19d0b472010-02-16 11:11:14 +0000707 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000708 break; // getConstantValue(Op0) above already converted it
709 }
710 return GV;
Chris Lattner9de0d142003-04-23 19:01:49 +0000711 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000712 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000713 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000714 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000715 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000716 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000717 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000718 case Instruction::UDiv:
719 case Instruction::SDiv:
720 case Instruction::URem:
721 case Instruction::SRem:
722 case Instruction::And:
723 case Instruction::Or:
724 case Instruction::Xor: {
725 GenericValue LHS = getConstantValue(Op0);
726 GenericValue RHS = getConstantValue(CE->getOperand(1));
727 GenericValue GV;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000728 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000729 default: llvm_unreachable("Bad add type!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000730 case Type::IntegerTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000731 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000732 default: llvm_unreachable("Invalid integer opcode");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000733 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
734 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
735 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
736 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
737 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
738 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
739 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
740 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
741 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
742 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
743 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000744 break;
745 case Type::FloatTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000746 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000747 default: llvm_unreachable("Invalid float opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000748 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000749 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000750 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000751 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000752 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000753 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000754 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000755 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000756 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000757 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000758 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000759 break;
760 case Type::DoubleTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000761 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000762 default: llvm_unreachable("Invalid double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000763 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000764 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000765 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000766 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000767 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000768 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000769 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000770 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000771 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000772 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000773 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000774 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000775 case Type::X86_FP80TyID:
776 case Type::PPC_FP128TyID:
777 case Type::FP128TyID: {
Tim Northover29178a32013-01-22 09:46:31 +0000778 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
779 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000780 switch (CE->getOpcode()) {
Daniel Dunbare4f47432010-11-13 00:55:42 +0000781 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000782 case Instruction::FAdd:
Tim Northover29178a32013-01-22 09:46:31 +0000783 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000784 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000785 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000786 case Instruction::FSub:
Tim Northover29178a32013-01-22 09:46:31 +0000787 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
788 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000789 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000790 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000791 case Instruction::FMul:
Tim Northover29178a32013-01-22 09:46:31 +0000792 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
793 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000794 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000795 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000796 case Instruction::FDiv:
Tim Northover29178a32013-01-22 09:46:31 +0000797 apfLHS.divide(APFloat(Sem, RHS.IntVal),
798 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000799 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000800 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000801 case Instruction::FRem:
Tim Northover29178a32013-01-22 09:46:31 +0000802 apfLHS.mod(APFloat(Sem, RHS.IntVal),
803 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000804 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000805 break;
806 }
807 }
808 break;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000809 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000810 return GV;
811 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000812 default:
813 break;
814 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000815
816 SmallString<256> Msg;
817 raw_svector_ostream OS(Msg);
818 OS << "ConstantExpr not handled: " << *CE;
819 report_fatal_error(OS.str());
Chris Lattner68cbcc32003-05-14 17:51:49 +0000820 }
Misha Brukman835702a2005-04-21 22:36:52 +0000821
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000822 // Otherwise, we have a simple constant.
Reid Spencer4fd528f2007-03-06 22:23:15 +0000823 GenericValue Result;
Chris Lattner6b727592004-06-17 18:19:28 +0000824 switch (C->getType()->getTypeID()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000825 case Type::FloatTyID:
826 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000827 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000828 case Type::DoubleTyID:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000829 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer87aa65f2007-03-06 03:04:04 +0000830 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000831 case Type::X86_FP80TyID:
832 case Type::FP128TyID:
833 case Type::PPC_FP128TyID:
Dale Johannesen54306fe2008-10-09 18:53:47 +0000834 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000835 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000836 case Type::IntegerTyID:
837 Result.IntVal = cast<ConstantInt>(C)->getValue();
838 break;
Chris Lattner996fe012002-12-24 00:01:05 +0000839 case Type::PointerTyID:
Reid Spencer6a0fd732004-07-18 00:41:27 +0000840 if (isa<ConstantPointerNull>(C))
Craig Topper2617dcc2014-04-15 06:32:26 +0000841 Result.PointerVal = nullptr;
Reid Spencer6a0fd732004-07-18 00:41:27 +0000842 else if (const Function *F = dyn_cast<Function>(C))
843 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000844 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer6a0fd732004-07-18 00:41:27 +0000845 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000846 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
847 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
848 BA->getBasicBlock())));
Reid Spencer6a0fd732004-07-18 00:41:27 +0000849 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000850 llvm_unreachable("Unknown constant pointer type!");
Chris Lattner996fe012002-12-24 00:01:05 +0000851 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000852 case Type::VectorTyID: {
853 unsigned elemNum;
854 Type* ElemTy;
855 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
856 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
857 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
858
859 if (CDV) {
860 elemNum = CDV->getNumElements();
861 ElemTy = CDV->getElementType();
862 } else if (CV || CAZ) {
863 VectorType* VTy = dyn_cast<VectorType>(C->getType());
864 elemNum = VTy->getNumElements();
865 ElemTy = VTy->getElementType();
866 } else {
867 llvm_unreachable("Unknown constant vector type!");
868 }
869
870 Result.AggregateVal.resize(elemNum);
871 // Check if vector holds floats.
872 if(ElemTy->isFloatTy()) {
873 if (CAZ) {
874 GenericValue floatZero;
875 floatZero.FloatVal = 0.f;
876 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
877 floatZero);
878 break;
879 }
880 if(CV) {
881 for (unsigned i = 0; i < elemNum; ++i)
882 if (!isa<UndefValue>(CV->getOperand(i)))
883 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
884 CV->getOperand(i))->getValueAPF().convertToFloat();
885 break;
886 }
887 if(CDV)
888 for (unsigned i = 0; i < elemNum; ++i)
889 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
890
891 break;
892 }
893 // Check if vector holds doubles.
894 if (ElemTy->isDoubleTy()) {
895 if (CAZ) {
896 GenericValue doubleZero;
897 doubleZero.DoubleVal = 0.0;
898 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
899 doubleZero);
900 break;
901 }
902 if(CV) {
903 for (unsigned i = 0; i < elemNum; ++i)
904 if (!isa<UndefValue>(CV->getOperand(i)))
905 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
906 CV->getOperand(i))->getValueAPF().convertToDouble();
907 break;
908 }
909 if(CDV)
910 for (unsigned i = 0; i < elemNum; ++i)
911 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
912
913 break;
914 }
915 // Check if vector holds integers.
916 if (ElemTy->isIntegerTy()) {
917 if (CAZ) {
918 GenericValue intZero;
919 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
920 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
921 intZero);
922 break;
923 }
924 if(CV) {
925 for (unsigned i = 0; i < elemNum; ++i)
926 if (!isa<UndefValue>(CV->getOperand(i)))
927 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
928 CV->getOperand(i))->getValue();
929 else {
930 Result.AggregateVal[i].IntVal =
931 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
932 }
933 break;
934 }
935 if(CDV)
936 for (unsigned i = 0; i < elemNum; ++i)
937 Result.AggregateVal[i].IntVal = APInt(
938 CDV->getElementType()->getPrimitiveSizeInBits(),
939 CDV->getElementAsInteger(i));
940
941 break;
942 }
943 llvm_unreachable("Unknown constant pointer type!");
944 }
945 break;
946
Chris Lattner996fe012002-12-24 00:01:05 +0000947 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000948 SmallString<256> Msg;
949 raw_svector_ostream OS(Msg);
950 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
951 report_fatal_error(OS.str());
Chris Lattner996fe012002-12-24 00:01:05 +0000952 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000953
Chris Lattner996fe012002-12-24 00:01:05 +0000954 return Result;
955}
956
Duncan Sands1202d1b2007-12-14 19:38:31 +0000957/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
958/// with the integer held in IntVal.
959static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
960 unsigned StoreBytes) {
961 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000962 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000963
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000964 if (sys::IsLittleEndianHost) {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000965 // Little-endian host - the source is ordered from LSB to MSB. Order the
966 // destination from LSB to MSB: Do a straight copy.
967 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000968 } else {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000969 // Big-endian host - the source is an array of 64 bit words ordered from
970 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
971 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
972 while (StoreBytes > sizeof(uint64_t)) {
973 StoreBytes -= sizeof(uint64_t);
974 // May not be aligned so use memcpy.
975 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
976 Src += sizeof(uint64_t);
977 }
978
979 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
980 }
981}
982
Evan Cheng09053e62008-11-04 06:10:31 +0000983void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattner229907c2011-07-18 04:54:35 +0000984 GenericValue *Ptr, Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000985 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1202d1b2007-12-14 19:38:31 +0000986
Reid Spencer87aa65f2007-03-06 03:04:04 +0000987 switch (Ty->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000988 default:
989 dbgs() << "Cannot store value of type " << *Ty << "!\n";
990 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +0000991 case Type::IntegerTyID:
992 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer87aa65f2007-03-06 03:04:04 +0000993 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000994 case Type::FloatTyID:
995 *((float*)Ptr) = Val.FloatVal;
996 break;
997 case Type::DoubleTyID:
998 *((double*)Ptr) = Val.DoubleVal;
999 break;
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001000 case Type::X86_FP80TyID:
1001 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1002 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001003 case Type::PointerTyID:
1004 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1005 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth93da3c82011-04-28 08:37:18 +00001006 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands1202d1b2007-12-14 19:38:31 +00001007
Reid Spencer87aa65f2007-03-06 03:04:04 +00001008 *((PointerTy*)Ptr) = Val.PointerVal;
1009 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001010 case Type::VectorTyID:
1011 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1012 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1013 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1014 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1015 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1016 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1017 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1018 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1019 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1020 }
1021 }
1022 break;
Chris Lattner996fe012002-12-24 00:01:05 +00001023 }
Duncan Sands1202d1b2007-12-14 19:38:31 +00001024
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001025 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands1202d1b2007-12-14 19:38:31 +00001026 // Host and target are different endian - reverse the stored bytes.
1027 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1028}
1029
1030/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1031/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1032static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1033 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greene82b63572013-01-14 21:04:45 +00001034 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1035 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands1202d1b2007-12-14 19:38:31 +00001036
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001037 if (sys::IsLittleEndianHost)
Duncan Sands1202d1b2007-12-14 19:38:31 +00001038 // Little-endian host - the destination must be ordered from LSB to MSB.
1039 // The source is ordered from LSB to MSB: Do a straight copy.
1040 memcpy(Dst, Src, LoadBytes);
1041 else {
1042 // Big-endian - the destination is an array of 64 bit words ordered from
1043 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1044 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1045 // a word.
1046 while (LoadBytes > sizeof(uint64_t)) {
1047 LoadBytes -= sizeof(uint64_t);
1048 // May not be aligned so use memcpy.
1049 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1050 Dst += sizeof(uint64_t);
1051 }
1052
1053 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1054 }
Chris Lattner996fe012002-12-24 00:01:05 +00001055}
1056
Misha Brukman857c21b2003-10-10 17:45:12 +00001057/// FIXME: document
1058///
Duncan Sands1202d1b2007-12-14 19:38:31 +00001059void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sandscd4a6be2008-03-10 16:38:37 +00001060 GenericValue *Ptr,
Chris Lattner229907c2011-07-18 04:54:35 +00001061 Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001062 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands5c65cb42007-12-10 17:43:13 +00001063
Duncan Sands1202d1b2007-12-14 19:38:31 +00001064 switch (Ty->getTypeID()) {
1065 case Type::IntegerTyID:
1066 // An APInt with all words initially zero.
1067 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1068 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1069 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +00001070 case Type::FloatTyID:
1071 Result.FloatVal = *((float*)Ptr);
1072 break;
1073 case Type::DoubleTyID:
Duncan Sands1202d1b2007-12-14 19:38:31 +00001074 Result.DoubleVal = *((double*)Ptr);
Reid Spencer87aa65f2007-03-06 03:04:04 +00001075 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001076 case Type::PointerTyID:
Reid Spencer87aa65f2007-03-06 03:04:04 +00001077 Result.PointerVal = *((PointerTy*)Ptr);
1078 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +00001079 case Type::X86_FP80TyID: {
1080 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands26d65392007-12-15 17:37:40 +00001081 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001082 uint64_t y[2];
1083 memcpy(y, Ptr, 10);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001084 Result.IntVal = APInt(80, y);
Dale Johannesena1336cf2007-09-17 18:44:13 +00001085 break;
1086 }
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001087 case Type::VectorTyID: {
1088 const VectorType *VT = cast<VectorType>(Ty);
1089 const Type *ElemT = VT->getElementType();
1090 const unsigned numElems = VT->getNumElements();
1091 if (ElemT->isFloatTy()) {
1092 Result.AggregateVal.resize(numElems);
1093 for (unsigned i = 0; i < numElems; ++i)
1094 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1095 }
1096 if (ElemT->isDoubleTy()) {
1097 Result.AggregateVal.resize(numElems);
1098 for (unsigned i = 0; i < numElems; ++i)
1099 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1100 }
1101 if (ElemT->isIntegerTy()) {
1102 GenericValue intZero;
1103 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1104 intZero.IntVal = APInt(elemBitWidth, 0);
1105 Result.AggregateVal.resize(numElems, intZero);
1106 for (unsigned i = 0; i < numElems; ++i)
1107 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1108 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1109 }
1110 break;
1111 }
Reid Spencer87aa65f2007-03-06 03:04:04 +00001112 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001113 SmallString<256> Msg;
1114 raw_svector_ostream OS(Msg);
1115 OS << "Cannot load value of type " << *Ty << "!";
1116 report_fatal_error(OS.str());
Chris Lattner7f389e82003-05-08 16:52:16 +00001117 }
Chris Lattner7f389e82003-05-08 16:52:16 +00001118}
1119
Chris Lattner996fe012002-12-24 00:01:05 +00001120void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greene0967d2d2010-01-05 01:27:39 +00001121 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesenb086d382008-08-07 01:30:15 +00001122 DEBUG(Init->dump());
Chris Lattner00245f42012-01-24 13:41:11 +00001123 if (isa<UndefValue>(Init))
Chris Lattner61753bf2004-10-16 18:19:26 +00001124 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001125
1126 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino69d62132006-01-20 18:18:40 +00001127 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001128 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino69d62132006-01-20 18:18:40 +00001129 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1130 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1131 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001132 }
1133
1134 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001135 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattner1dd86b12008-02-15 00:57:28 +00001136 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001137 }
1138
1139 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001140 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001141 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001142 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1143 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1144 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001145 }
1146
1147 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001148 const StructLayout *SL =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001149 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001150 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1151 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1152 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001153 }
1154
1155 if (const ConstantDataSequential *CDS =
1156 dyn_cast<ConstantDataSequential>(Init)) {
1157 // CDS is already laid out in host memory order.
1158 StringRef Data = CDS->getRawDataValues();
1159 memcpy(Addr, Data.data(), Data.size());
1160 return;
1161 }
1162
1163 if (Init->getType()->isFirstClassType()) {
Chris Lattner996fe012002-12-24 00:01:05 +00001164 GenericValue Val = getConstantValue(Init);
1165 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1166 return;
1167 }
1168
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001169 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinfbcc6632009-07-14 16:55:14 +00001170 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattner996fe012002-12-24 00:01:05 +00001171}
1172
Chris Lattner996fe012002-12-24 00:01:05 +00001173/// EmitGlobals - Emit all of the global variables to memory, storing their
1174/// addresses into GlobalAddress. This must make sure to copy the contents of
1175/// their initializers into the memory.
Chris Lattner996fe012002-12-24 00:01:05 +00001176void ExecutionEngine::emitGlobals() {
Chris Lattner996fe012002-12-24 00:01:05 +00001177 // Loop over all of the global variables in the program, allocating the memory
Chris Lattner0621cae2006-08-16 01:24:12 +00001178 // to hold them. If there is more than one module, do a prepass over globals
1179 // to figure out how the different modules should link together.
Chris Lattner229907c2011-07-18 04:54:35 +00001180 std::map<std::pair<std::string, Type*>,
Chris Lattner0621cae2006-08-16 01:24:12 +00001181 const GlobalValue*> LinkedGlobalsMap;
Misha Brukman835702a2005-04-21 22:36:52 +00001182
Chris Lattner0621cae2006-08-16 01:24:12 +00001183 if (Modules.size() != 1) {
1184 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001185 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001186 for (const auto &GV : M.globals()) {
1187 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1188 GV.hasAppendingLinkage() || !GV.hasName())
Chris Lattner0621cae2006-08-16 01:24:12 +00001189 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001190
1191 const GlobalValue *&GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001192 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
Chris Lattner0621cae2006-08-16 01:24:12 +00001193
1194 // If this is the first time we've seen this global, it is the canonical
1195 // version.
1196 if (!GVEntry) {
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001197 GVEntry = &GV;
Chris Lattner0621cae2006-08-16 01:24:12 +00001198 continue;
1199 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001200
Chris Lattner0621cae2006-08-16 01:24:12 +00001201 // If the existing global is strong, never replace it.
Nico Rieck7157bb72014-01-14 15:22:47 +00001202 if (GVEntry->hasExternalLinkage())
Chris Lattner0621cae2006-08-16 01:24:12 +00001203 continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001204
Chris Lattner0621cae2006-08-16 01:24:12 +00001205 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesence4396b2008-05-14 20:12:51 +00001206 // symbol. FIXME is this right for common?
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001207 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1208 GVEntry = &GV;
Chris Lattner9de0d142003-04-23 19:01:49 +00001209 }
Chris Lattner996fe012002-12-24 00:01:05 +00001210 }
Chris Lattner0621cae2006-08-16 01:24:12 +00001211 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001212
Chris Lattner0621cae2006-08-16 01:24:12 +00001213 std::vector<const GlobalValue*> NonCanonicalGlobals;
1214 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001215 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001216 for (const auto &GV : M.globals()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001217 // In the multi-module case, see what this global maps to.
1218 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001219 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001220 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001221 // If something else is the canonical global, ignore this one.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001222 if (GVEntry != &GV) {
1223 NonCanonicalGlobals.push_back(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001224 continue;
1225 }
1226 }
1227 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001228
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001229 if (!GV.isDeclaration()) {
1230 addGlobalMapping(&GV, getMemoryForGV(&GV));
Chris Lattner0621cae2006-08-16 01:24:12 +00001231 } else {
1232 // External variable reference. Try to use the dynamic loader to
1233 // get a pointer to it.
1234 if (void *SymAddr =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001235 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1236 addGlobalMapping(&GV, SymAddr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001237 else {
Chris Lattner2104b8d2010-04-07 22:58:41 +00001238 report_fatal_error("Could not resolve external global address: "
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001239 +GV.getName());
Chris Lattner0621cae2006-08-16 01:24:12 +00001240 }
1241 }
1242 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001243
Chris Lattner0621cae2006-08-16 01:24:12 +00001244 // If there are multiple modules, map the non-canonical globals to their
1245 // canonical location.
1246 if (!NonCanonicalGlobals.empty()) {
1247 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1248 const GlobalValue *GV = NonCanonicalGlobals[i];
1249 const GlobalValue *CGV =
1250 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1251 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1252 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesa67f06b2008-10-14 10:04:52 +00001253 addGlobalMapping(GV, Ptr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001254 }
1255 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001256
1257 // Now that all of the globals are set up in memory, loop through them all
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001258 // and initialize their contents.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001259 for (const auto &GV : M.globals()) {
1260 if (!GV.isDeclaration()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001261 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001262 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001263 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1264 if (GVEntry != &GV) // Not the canonical variable.
Chris Lattner0621cae2006-08-16 01:24:12 +00001265 continue;
1266 }
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001267 EmitGlobalVariable(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001268 }
1269 }
1270 }
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001271}
1272
1273// EmitGlobalVariable - This method emits the specified global variable to the
1274// address specified in GlobalAddresses, or allocates new memory if it's not
1275// already in the map.
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +00001276void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner748e8572003-12-31 20:21:04 +00001277 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattnerdc631732004-02-08 19:33:23 +00001278
Craig Topper2617dcc2014-04-15 06:32:26 +00001279 if (!GA) {
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001280 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001281 GA = getMemoryForGV(GV);
Andrew Kaylor3b442372013-11-15 17:52:54 +00001282
1283 // If we failed to allocate memory for this global, return.
Craig Topper2617dcc2014-04-15 06:32:26 +00001284 if (!GA) return;
Andrew Kaylor3b442372013-11-15 17:52:54 +00001285
Chris Lattner748e8572003-12-31 20:21:04 +00001286 addGlobalMapping(GV, GA);
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001287 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001288
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001289 // Don't initialize if it's thread local, let the client do it.
1290 if (!GV->isThreadLocal())
1291 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001292
Chris Lattner229907c2011-07-18 04:54:35 +00001293 Type *ElTy = GV->getType()->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001294 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattnerdf1f1522005-01-08 20:13:19 +00001295 NumInitBytes += (unsigned)GVSize;
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001296 ++NumGlobals;
Chris Lattner996fe012002-12-24 00:01:05 +00001297}
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001298
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001299ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1300 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001301}
1302
Zachary Turnerc04b8922014-06-20 21:07:14 +00001303sys::Mutex *
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001304ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001305 return &EES->EE.lock;
1306}
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001307
1308void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1309 const GlobalValue *Old) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001310 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1311 EES->GlobalAddressReverseMap.erase(OldVal);
1312}
1313
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001314void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1315 const GlobalValue *,
1316 const GlobalValue *) {
Craig Toppera2886c22012-02-07 05:05:23 +00001317 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1318 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001319}