blob: 41283e98ceb87aa806aa7a0a359b741b6f2ea569 [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
Chris Lattneree937c82003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +000016#include "llvm/ExecutionEngine/ExecutionEngine.h"
Daniel Dunbar868e3f02010-11-13 02:48:57 +000017#include "llvm/ADT/SmallString.h"
Chris Lattner390d78b2009-08-23 22:49:13 +000018#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/ExecutionEngine/JITMemoryManager.h"
21#include "llvm/ExecutionEngine/ObjectCache.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Constants.h"
23#include "llvm/IR/DataLayout.h"
24#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Module.h"
26#include "llvm/IR/Operator.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000027#include "llvm/IR/ValueHandle.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
Chris Lattnerc346ecd2006-12-19 22:43:32 +000040STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
41STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattner996fe012002-12-24 00:01:05 +000042
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000043// Pin the vtable to this file.
44void ObjectCache::anchor() {}
45void ObjectBuffer::anchor() {}
46void ObjectBufferStream::anchor() {}
47
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000048ExecutionEngine *(*ExecutionEngine::JITCtor)(
49 Module *M,
50 std::string *ErrorStr,
51 JITMemoryManager *JMM,
Jeffrey Yasskin31faeff2010-02-05 16:19:36 +000052 bool GVsWithCode,
Craig Topper2617dcc2014-04-15 06:32:26 +000053 TargetMachine *TM) = nullptr;
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000054ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
55 Module *M,
56 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +000057 RTDyldMemoryManager *MCJMM,
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000058 bool GVsWithCode,
Craig Topper2617dcc2014-04-15 06:32:26 +000059 TargetMachine *TM) = nullptr;
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000060ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
Craig Topper2617dcc2014-04-15 06:32:26 +000061 std::string *ErrorStr) =nullptr;
Chris Lattner2d52c1b2006-03-22 06:07:50 +000062
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000063ExecutionEngine::ExecutionEngine(Module *M)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +000064 : EEState(*this),
Craig Topper2617dcc2014-04-15 06:32:26 +000065 LazyFunctionCreator(nullptr) {
Jeffrey Yasskin4567db42009-10-27 20:30:28 +000066 CompilingLazily = false;
Evan Chengcdc00602008-09-24 16:25:55 +000067 GVCompilationDisabled = false;
Evan Cheng84a90552008-06-17 16:49:02 +000068 SymbolSearchingDisabled = false;
Lang Hamesbc876012014-04-18 06:48:23 +000069
70 // IR module verification is enabled by default in debug builds, and disabled
71 // by default in release builds.
72#ifndef NDEBUG
73 VerifyModules = true;
74#else
75 VerifyModules = false;
76#endif
77
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000078 Modules.push_back(M);
79 assert(M && "Module is null?");
Misha Brukman260b0c82003-10-16 21:18:05 +000080}
81
Brian Gaeke92f8b302003-09-04 22:57:27 +000082ExecutionEngine::~ExecutionEngine() {
Reid Spencer603682a2007-03-03 18:19:18 +000083 clearAllGlobalMappings();
Chris Lattner0621cae2006-08-16 01:24:12 +000084 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
85 delete Modules[i];
Brian Gaeke92f8b302003-09-04 22:57:27 +000086}
87
Jeffrey Yasskina4044332010-03-27 04:53:56 +000088namespace {
Daniel Dunbar868e3f02010-11-13 02:48:57 +000089/// \brief Helper class which uses a value handler to automatically deletes the
90/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskina4044332010-03-27 04:53:56 +000091class GVMemoryBlock : public CallbackVH {
92 GVMemoryBlock(const GlobalVariable *GV)
93 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
94
95public:
Daniel Dunbar868e3f02010-11-13 02:48:57 +000096 /// \brief Returns the address the GlobalVariable should be written into. The
97 /// GVMemoryBlock object prefixes that.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000098 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattner229907c2011-07-18 04:54:35 +000099 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000100 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
101 void *RawMemory = ::operator new(
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000102 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000103 TD.getPreferredAlignment(GV))
104 + GVSize);
105 new(RawMemory) GVMemoryBlock(GV);
106 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
107 }
108
Craig Topperb51ff602014-03-08 07:51:20 +0000109 void deleted() override {
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000110 // We allocated with operator new and with some extra memory hanging off the
111 // end, so don't just delete this. I'm not sure if this is actually
112 // required.
113 this->~GVMemoryBlock();
114 ::operator delete(this);
115 }
116};
117} // anonymous namespace
118
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000119char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000120 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000121}
122
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000123bool ExecutionEngine::removeModule(Module *M) {
Craig Topperaf0dea12013-07-04 01:31:24 +0000124 for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
Devang Patel324fe892007-10-15 19:56:32 +0000125 E = Modules.end(); I != E; ++I) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000126 Module *Found = *I;
127 if (Found == M) {
Devang Patel324fe892007-10-15 19:56:32 +0000128 Modules.erase(I);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000129 clearGlobalMappingsFromModule(M);
130 return true;
Devang Patel324fe892007-10-15 19:56:32 +0000131 }
132 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000133 return false;
Nate Begeman617001d2009-01-23 19:27:28 +0000134}
135
Chris Lattner0621cae2006-08-16 01:24:12 +0000136Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
137 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000138 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattner0621cae2006-08-16 01:24:12 +0000139 return F;
140 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000141 return nullptr;
Chris Lattner0621cae2006-08-16 01:24:12 +0000142}
143
144
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000145void *ExecutionEngineState::RemoveMapping(const MutexGuard &,
146 const GlobalValue *ToUnmap) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000147 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000148 void *OldVal;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000149
150 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
151 // GlobalAddressMap.
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000152 if (I == GlobalAddressMap.end())
Craig Topper2617dcc2014-04-15 06:32:26 +0000153 OldVal = nullptr;
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000154 else {
155 OldVal = I->second;
156 GlobalAddressMap.erase(I);
157 }
158
159 GlobalAddressReverseMap.erase(OldVal);
160 return OldVal;
161}
162
Chris Lattner6d8dd182006-05-08 22:00:52 +0000163void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
164 MutexGuard locked(lock);
Evan Cheng5cc53c32008-09-18 07:54:21 +0000165
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000166 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000167 << "\' to [" << Addr << "]\n";);
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000168 void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
Craig Topper2617dcc2014-04-15 06:32:26 +0000169 assert((!CurVal || !Addr) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000170 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000171
172 // If we are using the reverse mapping, add it too.
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000173 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000174 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000175 EEState.getGlobalAddressReverseMap(locked)[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000176 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000177 V = GV;
178 }
179}
180
Chris Lattner6d8dd182006-05-08 22:00:52 +0000181void ExecutionEngine::clearAllGlobalMappings() {
182 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000183
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000184 EEState.getGlobalAddressMap(locked).clear();
185 EEState.getGlobalAddressReverseMap(locked).clear();
Chris Lattner6d8dd182006-05-08 22:00:52 +0000186}
187
Nate Begeman8f83fc42008-05-21 16:34:48 +0000188void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
189 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000190
191 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000192 EEState.RemoveMapping(locked, FI);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000193 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
194 GI != GE; ++GI)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000195 EEState.RemoveMapping(locked, GI);
Nate Begeman8f83fc42008-05-21 16:34:48 +0000196}
197
Chris Lattneree181732008-04-04 04:47:41 +0000198void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Chris Lattner6d8dd182006-05-08 22:00:52 +0000199 MutexGuard locked(lock);
Chris Lattneree181732008-04-04 04:47:41 +0000200
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000201 ExecutionEngineState::GlobalAddressMapTy &Map =
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000202 EEState.getGlobalAddressMap(locked);
Chris Lattneree181732008-04-04 04:47:41 +0000203
Chris Lattner6d8dd182006-05-08 22:00:52 +0000204 // Deleting from the mapping?
Craig Topper2617dcc2014-04-15 06:32:26 +0000205 if (!Addr)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000206 return EEState.RemoveMapping(locked, GV);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000207
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000208 void *&CurVal = Map[GV];
Chris Lattneree181732008-04-04 04:47:41 +0000209 void *OldVal = CurVal;
210
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000211 if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
212 EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
Chris Lattner6d8dd182006-05-08 22:00:52 +0000213 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000214
215 // If we are using the reverse mapping, add it too.
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000216 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000217 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000218 EEState.getGlobalAddressReverseMap(locked)[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000219 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000220 V = GV;
221 }
Chris Lattneree181732008-04-04 04:47:41 +0000222 return OldVal;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000223}
224
Chris Lattner6d8dd182006-05-08 22:00:52 +0000225void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
226 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000227
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000228 ExecutionEngineState::GlobalAddressMapTy::iterator I =
229 EEState.getGlobalAddressMap(locked).find(GV);
Craig Topper2617dcc2014-04-15 06:32:26 +0000230 return I != EEState.getGlobalAddressMap(locked).end() ? I->second : nullptr;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000231}
232
Chris Lattner748e8572003-12-31 20:21:04 +0000233const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spencer79876f52005-07-12 15:51:55 +0000234 MutexGuard locked(lock);
235
Chris Lattner748e8572003-12-31 20:21:04 +0000236 // If we haven't computed the reverse mapping yet, do so first.
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000237 if (EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000238 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000239 I = EEState.getGlobalAddressMap(locked).begin(),
240 E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
Daniel Dunbare4f47432010-11-13 00:55:42 +0000241 EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
242 I->second, I->first));
Chris Lattner748e8572003-12-31 20:21:04 +0000243 }
244
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000245 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +0000246 EEState.getGlobalAddressReverseMap(locked).find(Addr);
Craig Topper2617dcc2014-04-15 06:32:26 +0000247 return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : nullptr;
Chris Lattner748e8572003-12-31 20:21:04 +0000248}
Chris Lattner5a0d4822003-12-26 06:50:30 +0000249
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000250namespace {
251class ArgvArray {
252 char *Array;
253 std::vector<char*> Values;
254public:
Craig Topper2617dcc2014-04-15 06:32:26 +0000255 ArgvArray() : Array(nullptr) {}
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000256 ~ArgvArray() { clear(); }
257 void clear() {
258 delete[] Array;
Craig Topper2617dcc2014-04-15 06:32:26 +0000259 Array = nullptr;
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000260 for (size_t I = 0, E = Values.size(); I != E; ++I) {
261 delete[] Values[I];
262 }
263 Values.clear();
264 }
265 /// Turn a vector of strings into a nice argv style array of pointers to null
266 /// terminated strings.
267 void *reset(LLVMContext &C, ExecutionEngine *EE,
268 const std::vector<std::string> &InputArgv);
269};
270} // anonymous namespace
271void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
272 const std::vector<std::string> &InputArgv) {
273 clear(); // Free the old contents.
Chandler Carruth5da3f052012-11-01 09:14:31 +0000274 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000275 Array = new char[(InputArgv.size()+1)*PtrSize];
Chris Lattner5a0d4822003-12-26 06:50:30 +0000276
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000277 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
Chris Lattner229907c2011-07-18 04:54:35 +0000278 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000279
280 for (unsigned i = 0; i != InputArgv.size(); ++i) {
281 unsigned Size = InputArgv[i].size()+1;
282 char *Dest = new char[Size];
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000283 Values.push_back(Dest);
David Greene0967d2d2010-01-05 01:27:39 +0000284 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukman835702a2005-04-21 22:36:52 +0000285
Chris Lattner5a0d4822003-12-26 06:50:30 +0000286 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
287 Dest[Size-1] = 0;
Misha Brukman835702a2005-04-21 22:36:52 +0000288
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000289 // Endian safe: Array[i] = (PointerTy)Dest;
290 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000291 SBytePtr);
292 }
293
294 // Null terminate it
Craig Topper2617dcc2014-04-15 06:32:26 +0000295 EE->StoreValueToMemory(PTOGV(nullptr),
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000296 (GenericValue*)(Array+InputArgv.size()*PtrSize),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000297 SBytePtr);
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000298 return Array;
Chris Lattner5a0d4822003-12-26 06:50:30 +0000299}
300
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000301void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
302 bool isDtors) {
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000303 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000304 GlobalVariable *GV = module->getNamedGlobal(Name);
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000305
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000306 // If this global has internal linkage, or if it has a use, then it must be
307 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
308 // this is the case, don't execute any of the global ctors, __main will do
309 // it.
310 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
311
Nick Lewycky0cbfcb22011-04-06 20:38:44 +0000312 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000313 // the init priority, which we ignore.
Chris Lattner00245f42012-01-24 13:41:11 +0000314 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
Craig Topper2617dcc2014-04-15 06:32:26 +0000315 if (!InitList)
Nick Lewycky0f857892011-04-11 22:11:20 +0000316 return;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000317 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner00245f42012-01-24 13:41:11 +0000318 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
Craig Topper2617dcc2014-04-15 06:32:26 +0000319 if (!CS) continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000320
321 Constant *FP = CS->getOperand(1);
322 if (FP->isNullValue())
Nick Lewycky0f857892011-04-11 22:11:20 +0000323 continue; // Found a sentinal value, ignore.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000324
325 // Strip off constant expression casts.
326 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
327 if (CE->isCast())
328 FP = CE->getOperand(0);
329
330 // Execute the ctor/dtor function!
331 if (Function *F = dyn_cast<Function>(FP))
332 runFunction(F, std::vector<GenericValue>());
333
334 // FIXME: It is marginally lame that we just do nothing here if we see an
335 // entry we don't recognize. It might not be unreasonable for the verifier
336 // to not even allow this and just assert here.
337 }
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000338}
339
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000340void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
341 // Execute global ctors/dtors for each module in the program.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000342 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
343 runStaticConstructorsDestructors(Modules[i], isDtors);
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000344}
345
Dan Gohmancf3e3012008-08-26 01:38:29 +0000346#ifndef NDEBUG
Duncan Sands1202d1b2007-12-14 19:38:31 +0000347/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
348static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000349 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000350 for (unsigned i = 0; i < PtrSize; ++i)
351 if (*(i + (uint8_t*)Loc))
352 return false;
353 return true;
354}
Dan Gohmancf3e3012008-08-26 01:38:29 +0000355#endif
Duncan Sands1202d1b2007-12-14 19:38:31 +0000356
Chris Lattner5a0d4822003-12-26 06:50:30 +0000357int ExecutionEngine::runFunctionAsMain(Function *Fn,
358 const std::vector<std::string> &argv,
359 const char * const * envp) {
360 std::vector<GenericValue> GVArgs;
361 GenericValue GVArgc;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000362 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov8c32c112007-06-03 19:17:35 +0000363
364 // Check main() type
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000365 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattner229907c2011-07-18 04:54:35 +0000366 FunctionType *FTy = Fn->getFunctionType();
367 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000368
369 // Check the argument types.
370 if (NumArgs > 3)
371 report_fatal_error("Invalid number of arguments of main() supplied");
372 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
373 report_fatal_error("Invalid type for third argument of main() supplied");
374 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
375 report_fatal_error("Invalid type for second argument of main() supplied");
376 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
377 report_fatal_error("Invalid type for first argument of main() supplied");
378 if (!FTy->getReturnType()->isIntegerTy() &&
379 !FTy->getReturnType()->isVoidTy())
380 report_fatal_error("Invalid return type of main() supplied");
381
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000382 ArgvArray CArgv;
383 ArgvArray CEnv;
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000384 if (NumArgs) {
385 GVArgs.push_back(GVArgc); // Arg #0 = argc.
386 if (NumArgs > 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000387 // Arg #1 = argv.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000388 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands1202d1b2007-12-14 19:38:31 +0000389 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000390 "argv[0] was null after CreateArgv");
391 if (NumArgs > 2) {
392 std::vector<std::string> EnvVars;
393 for (unsigned i = 0; envp[i]; ++i)
394 EnvVars.push_back(envp[i]);
Owen Anderson55f1c092009-08-13 21:58:54 +0000395 // Arg #2 = envp.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000396 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000397 }
398 }
399 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000400
Reid Spencer87aa65f2007-03-06 03:04:04 +0000401 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000402}
403
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000404ExecutionEngine *ExecutionEngine::create(Module *M,
Reid Spencer603682a2007-03-03 18:19:18 +0000405 bool ForceInterpreter,
Evan Cheng7ff05bf2008-08-08 08:11:34 +0000406 std::string *ErrorStr,
Jeffrey Yasskin70415d92009-07-08 21:59:57 +0000407 CodeGenOpt::Level OptLevel,
408 bool GVsWithCode) {
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000409 EngineBuilder EB = EngineBuilder(M)
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000410 .setEngineKind(ForceInterpreter
411 ? EngineKind::Interpreter
412 : EngineKind::JIT)
413 .setErrorStr(ErrorStr)
414 .setOptLevel(OptLevel)
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000415 .setAllocateGVsWithCode(GVsWithCode);
416
417 return EB.create();
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000418}
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000419
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000420/// createJIT - This is the factory method for creating a JIT for the current
421/// machine, it does not fall back to the interpreter. This takes ownership
422/// of the module.
423ExecutionEngine *ExecutionEngine::createJIT(Module *M,
424 std::string *ErrorStr,
425 JITMemoryManager *JMM,
Dylan Noblesmith19a58df2011-12-01 21:49:21 +0000426 CodeGenOpt::Level OL,
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000427 bool GVsWithCode,
Evan Cheng2129f592011-07-19 06:37:02 +0000428 Reloc::Model RM,
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000429 CodeModel::Model CMM) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000430 if (!ExecutionEngine::JITCtor) {
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000431 if (ErrorStr)
432 *ErrorStr = "JIT has not been linked in.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000433 return nullptr;
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000434 }
435
436 // Use the defaults for extra parameters. Users can use EngineBuilder to
437 // set them.
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000438 EngineBuilder EB(M);
439 EB.setEngineKind(EngineKind::JIT);
440 EB.setErrorStr(ErrorStr);
441 EB.setRelocationModel(RM);
442 EB.setCodeModel(CMM);
443 EB.setAllocateGVsWithCode(GVsWithCode);
444 EB.setOptLevel(OL);
445 EB.setJITMemoryManager(JMM);
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000446
Peter Collingbournedff24782011-12-07 23:58:57 +0000447 // TODO: permit custom TargetOptions here
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000448 TargetMachine *TM = EB.selectTarget();
Craig Topper2617dcc2014-04-15 06:32:26 +0000449 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return nullptr;
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000450
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000451 return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
Dylan Noblesmith0bd34fb2011-05-13 22:02:53 +0000452}
453
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000454ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000455 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000456
Nick Lewyckya53414f2008-03-08 02:49:45 +0000457 // Make sure we can resolve symbols in the program as well. The zero arg
458 // to the function tells DynamicLibrary to load the program, not a library.
Craig Topper2617dcc2014-04-15 06:32:26 +0000459 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
460 return nullptr;
Nick Lewyckya53414f2008-03-08 02:49:45 +0000461
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000462 assert(!(JMM && MCJMM));
463
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000464 // If the user specified a memory manager but didn't specify which engine to
465 // create, we assume they only want the JIT, and we fail if they only want
466 // the interpreter.
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000467 if (JMM || MCJMM) {
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000468 if (WhichEngine & EngineKind::JIT)
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000469 WhichEngine = EngineKind::JIT;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000470 else {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000471 if (ErrorStr)
472 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000473 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000474 }
475 }
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000476
477 if (MCJMM && ! UseMCJIT) {
478 if (ErrorStr)
479 *ErrorStr =
480 "Cannot create a legacy JIT with a runtime dyld memory "
481 "manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000482 return nullptr;
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000483 }
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000484
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000485 // Unless the interpreter was explicitly selected or the JIT is not linked,
486 // try making a JIT.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000487 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000488 Triple TT(M->getTargetTriple());
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000489 if (!TM->getTarget().hasJIT()) {
490 errs() << "WARNING: This target JIT is not designed for the host"
491 << " you are running. If bad things happen, please choose"
492 << " a different -march switch.\n";
493 }
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000494
Lang Hamesbc876012014-04-18 06:48:23 +0000495 ExecutionEngine *EE = nullptr;
496 if (UseMCJIT && ExecutionEngine::MCJITCtor)
497 EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
498 AllocateGVsWithCode, TheTM.release());
499 else if (ExecutionEngine::JITCtor)
500 EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
501 AllocateGVsWithCode, TheTM.release());
502
503 if (EE) {
504 EE->setVerifyModules(VerifyModules);
505 return EE;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000506 }
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000507 }
508
509 // If we can't make a JIT and we didn't request one specifically, try making
510 // an interpreter instead.
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000511 if (WhichEngine & EngineKind::Interpreter) {
512 if (ExecutionEngine::InterpCtor)
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000513 return ExecutionEngine::InterpCtor(M, ErrorStr);
Chris Lattner8bcc6442009-09-23 02:03:49 +0000514 if (ErrorStr)
515 *ErrorStr = "Interpreter has not been linked in.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000516 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000517 }
Chris Lattner8bcc6442009-09-23 02:03:49 +0000518
Craig Topper2617dcc2014-04-15 06:32:26 +0000519 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
520 !ExecutionEngine::MCJITCtor) {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000521 if (ErrorStr)
522 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000523 }
524
Craig Topper2617dcc2014-04-15 06:32:26 +0000525 return nullptr;
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000526}
527
Chris Lattner996fe012002-12-24 00:01:05 +0000528void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke1678e852003-08-13 18:16:14 +0000529 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattner996fe012002-12-24 00:01:05 +0000530 return getPointerToFunction(F);
531
Reid Spencer79876f52005-07-12 15:51:55 +0000532 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000533 if (void *P = EEState.getGlobalAddressMap(locked)[GV])
534 return P;
Jeff Cohen69e849012006-02-07 05:11:57 +0000535
536 // Global variable might have been added since interpreter started.
537 if (GlobalVariable *GVar =
538 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
539 EmitGlobalVariable(GVar);
540 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000541 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000542
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000543 return EEState.getGlobalAddressMap(locked)[GV];
Chris Lattner996fe012002-12-24 00:01:05 +0000544}
545
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000546/// \brief Converts a Constant* into a GenericValue, including handling of
547/// ConstantExpr values.
Chris Lattner996fe012002-12-24 00:01:05 +0000548GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000549 // If its undefined, return the garbage.
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000550 if (isa<UndefValue>(C)) {
551 GenericValue Result;
552 switch (C->getType()->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000553 default:
554 break;
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000555 case Type::IntegerTyID:
556 case Type::X86_FP80TyID:
557 case Type::FP128TyID:
558 case Type::PPC_FP128TyID:
559 // Although the value is undefined, we still have to construct an APInt
560 // with the correct bit width.
561 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
562 break;
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000563 case Type::StructTyID: {
564 // if the whole struct is 'undef' just reserve memory for the value.
565 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
566 unsigned int elemNum = STy->getNumElements();
567 Result.AggregateVal.resize(elemNum);
568 for (unsigned int i = 0; i < elemNum; ++i) {
569 Type *ElemTy = STy->getElementType(i);
570 if (ElemTy->isIntegerTy())
571 Result.AggregateVal[i].IntVal =
572 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
573 else if (ElemTy->isAggregateType()) {
574 const Constant *ElemUndef = UndefValue::get(ElemTy);
575 Result.AggregateVal[i] = getConstantValue(ElemUndef);
576 }
577 }
578 }
579 }
580 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000581 case Type::VectorTyID:
582 // if the whole vector is 'undef' just reserve memory for the value.
583 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
584 const Type *ElemTy = VTy->getElementType();
585 unsigned int elemNum = VTy->getNumElements();
586 Result.AggregateVal.resize(elemNum);
587 if (ElemTy->isIntegerTy())
588 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000589 Result.AggregateVal[i].IntVal =
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000590 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000591 break;
592 }
593 return Result;
594 }
Chris Lattner9de0d142003-04-23 19:01:49 +0000595
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000596 // Otherwise, if the value is a ConstantExpr...
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000597 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000598 Constant *Op0 = CE->getOperand(0);
Chris Lattner9de0d142003-04-23 19:01:49 +0000599 switch (CE->getOpcode()) {
600 case Instruction::GetElementPtr: {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000601 // Compute the index
Reid Spencer4fd528f2007-03-06 22:23:15 +0000602 GenericValue Result = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000603 APInt Offset(DL->getPointerSizeInBits(), 0);
604 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
Misha Brukman835702a2005-04-21 22:36:52 +0000605
Reid Spencer87aa65f2007-03-06 03:04:04 +0000606 char* tmp = (char*) Result.PointerVal;
Nuno Lopesb6ad9822012-12-30 16:25:48 +0000607 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattner9de0d142003-04-23 19:01:49 +0000608 return Result;
609 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000610 case Instruction::Trunc: {
611 GenericValue GV = getConstantValue(Op0);
612 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
613 GV.IntVal = GV.IntVal.trunc(BitWidth);
614 return GV;
615 }
616 case Instruction::ZExt: {
617 GenericValue GV = getConstantValue(Op0);
618 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
619 GV.IntVal = GV.IntVal.zext(BitWidth);
620 return GV;
621 }
622 case Instruction::SExt: {
623 GenericValue GV = getConstantValue(Op0);
624 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
625 GV.IntVal = GV.IntVal.sext(BitWidth);
626 return GV;
627 }
628 case Instruction::FPTrunc: {
Dale Johannesena1336cf2007-09-17 18:44:13 +0000629 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000630 GenericValue GV = getConstantValue(Op0);
631 GV.FloatVal = float(GV.DoubleVal);
632 return GV;
633 }
634 case Instruction::FPExt:{
Dale Johannesena1336cf2007-09-17 18:44:13 +0000635 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000636 GenericValue GV = getConstantValue(Op0);
637 GV.DoubleVal = double(GV.FloatVal);
638 return GV;
639 }
640 case Instruction::UIToFP: {
641 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000642 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000643 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000644 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000645 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000646 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000647 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000648 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000649 false,
650 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000651 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000652 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000653 return GV;
654 }
655 case Instruction::SIToFP: {
656 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000657 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000658 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000659 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000660 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000661 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000662 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000663 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000664 true,
665 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000666 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000667 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000668 return GV;
669 }
670 case Instruction::FPToUI: // double->APInt conversion handles sign
671 case Instruction::FPToSI: {
672 GenericValue GV = getConstantValue(Op0);
673 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000674 if (Op0->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000675 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000676 else if (Op0->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000677 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000678 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover29178a32013-01-22 09:46:31 +0000679 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000680 uint64_t v;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000681 bool ignored;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000682 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000683 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000684 APFloat::rmTowardZero, &ignored);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000685 GV.IntVal = v; // endian?
686 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000687 return GV;
688 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000689 case Instruction::PtrToInt: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000690 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000691 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000692 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000693 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000694 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000695 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000696 return GV;
697 }
698 case Instruction::IntToPtr: {
699 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000700 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000701 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000702 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
703 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000704 return GV;
705 }
706 case Instruction::BitCast: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000707 GenericValue GV = getConstantValue(Op0);
Chris Lattner229907c2011-07-18 04:54:35 +0000708 Type* DestTy = CE->getType();
Reid Spencer4fd528f2007-03-06 22:23:15 +0000709 switch (Op0->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000710 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000711 case Type::IntegerTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000712 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnerfdd87902009-10-05 05:54:46 +0000713 if (DestTy->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000714 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000715 else if (DestTy->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000716 GV.DoubleVal = GV.IntVal.bitsToDouble();
717 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000718 case Type::FloatTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000719 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000720 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000721 break;
722 case Type::DoubleTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000723 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000724 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000725 break;
726 case Type::PointerTyID:
Duncan Sands19d0b472010-02-16 11:11:14 +0000727 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000728 break; // getConstantValue(Op0) above already converted it
729 }
730 return GV;
Chris Lattner9de0d142003-04-23 19:01:49 +0000731 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000732 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000733 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000734 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000735 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000736 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000737 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000738 case Instruction::UDiv:
739 case Instruction::SDiv:
740 case Instruction::URem:
741 case Instruction::SRem:
742 case Instruction::And:
743 case Instruction::Or:
744 case Instruction::Xor: {
745 GenericValue LHS = getConstantValue(Op0);
746 GenericValue RHS = getConstantValue(CE->getOperand(1));
747 GenericValue GV;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000748 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000749 default: llvm_unreachable("Bad add type!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000750 case Type::IntegerTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000751 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000752 default: llvm_unreachable("Invalid integer opcode");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000753 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
754 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
755 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
756 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
757 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
758 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
759 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
760 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
761 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
762 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
763 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000764 break;
765 case Type::FloatTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000766 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000767 default: llvm_unreachable("Invalid float opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000768 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000769 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000770 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000771 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000772 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000773 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000774 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000775 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000776 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000777 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000778 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000779 break;
780 case Type::DoubleTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000781 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000782 default: llvm_unreachable("Invalid double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000783 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000784 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000785 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000786 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000787 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000788 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000789 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000790 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000791 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000792 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000793 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000794 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000795 case Type::X86_FP80TyID:
796 case Type::PPC_FP128TyID:
797 case Type::FP128TyID: {
Tim Northover29178a32013-01-22 09:46:31 +0000798 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
799 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000800 switch (CE->getOpcode()) {
Daniel Dunbare4f47432010-11-13 00:55:42 +0000801 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000802 case Instruction::FAdd:
Tim Northover29178a32013-01-22 09:46:31 +0000803 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000804 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000805 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000806 case Instruction::FSub:
Tim Northover29178a32013-01-22 09:46:31 +0000807 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
808 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000809 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000810 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000811 case Instruction::FMul:
Tim Northover29178a32013-01-22 09:46:31 +0000812 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
813 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000814 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000815 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000816 case Instruction::FDiv:
Tim Northover29178a32013-01-22 09:46:31 +0000817 apfLHS.divide(APFloat(Sem, RHS.IntVal),
818 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000819 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000820 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000821 case Instruction::FRem:
Tim Northover29178a32013-01-22 09:46:31 +0000822 apfLHS.mod(APFloat(Sem, RHS.IntVal),
823 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000824 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000825 break;
826 }
827 }
828 break;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000829 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000830 return GV;
831 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000832 default:
833 break;
834 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000835
836 SmallString<256> Msg;
837 raw_svector_ostream OS(Msg);
838 OS << "ConstantExpr not handled: " << *CE;
839 report_fatal_error(OS.str());
Chris Lattner68cbcc32003-05-14 17:51:49 +0000840 }
Misha Brukman835702a2005-04-21 22:36:52 +0000841
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000842 // Otherwise, we have a simple constant.
Reid Spencer4fd528f2007-03-06 22:23:15 +0000843 GenericValue Result;
Chris Lattner6b727592004-06-17 18:19:28 +0000844 switch (C->getType()->getTypeID()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000845 case Type::FloatTyID:
846 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000847 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000848 case Type::DoubleTyID:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000849 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer87aa65f2007-03-06 03:04:04 +0000850 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000851 case Type::X86_FP80TyID:
852 case Type::FP128TyID:
853 case Type::PPC_FP128TyID:
Dale Johannesen54306fe2008-10-09 18:53:47 +0000854 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000855 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000856 case Type::IntegerTyID:
857 Result.IntVal = cast<ConstantInt>(C)->getValue();
858 break;
Chris Lattner996fe012002-12-24 00:01:05 +0000859 case Type::PointerTyID:
Reid Spencer6a0fd732004-07-18 00:41:27 +0000860 if (isa<ConstantPointerNull>(C))
Craig Topper2617dcc2014-04-15 06:32:26 +0000861 Result.PointerVal = nullptr;
Reid Spencer6a0fd732004-07-18 00:41:27 +0000862 else if (const Function *F = dyn_cast<Function>(C))
863 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000864 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer6a0fd732004-07-18 00:41:27 +0000865 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000866 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
867 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
868 BA->getBasicBlock())));
Reid Spencer6a0fd732004-07-18 00:41:27 +0000869 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000870 llvm_unreachable("Unknown constant pointer type!");
Chris Lattner996fe012002-12-24 00:01:05 +0000871 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000872 case Type::VectorTyID: {
873 unsigned elemNum;
874 Type* ElemTy;
875 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
876 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
877 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
878
879 if (CDV) {
880 elemNum = CDV->getNumElements();
881 ElemTy = CDV->getElementType();
882 } else if (CV || CAZ) {
883 VectorType* VTy = dyn_cast<VectorType>(C->getType());
884 elemNum = VTy->getNumElements();
885 ElemTy = VTy->getElementType();
886 } else {
887 llvm_unreachable("Unknown constant vector type!");
888 }
889
890 Result.AggregateVal.resize(elemNum);
891 // Check if vector holds floats.
892 if(ElemTy->isFloatTy()) {
893 if (CAZ) {
894 GenericValue floatZero;
895 floatZero.FloatVal = 0.f;
896 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
897 floatZero);
898 break;
899 }
900 if(CV) {
901 for (unsigned i = 0; i < elemNum; ++i)
902 if (!isa<UndefValue>(CV->getOperand(i)))
903 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
904 CV->getOperand(i))->getValueAPF().convertToFloat();
905 break;
906 }
907 if(CDV)
908 for (unsigned i = 0; i < elemNum; ++i)
909 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
910
911 break;
912 }
913 // Check if vector holds doubles.
914 if (ElemTy->isDoubleTy()) {
915 if (CAZ) {
916 GenericValue doubleZero;
917 doubleZero.DoubleVal = 0.0;
918 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
919 doubleZero);
920 break;
921 }
922 if(CV) {
923 for (unsigned i = 0; i < elemNum; ++i)
924 if (!isa<UndefValue>(CV->getOperand(i)))
925 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
926 CV->getOperand(i))->getValueAPF().convertToDouble();
927 break;
928 }
929 if(CDV)
930 for (unsigned i = 0; i < elemNum; ++i)
931 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
932
933 break;
934 }
935 // Check if vector holds integers.
936 if (ElemTy->isIntegerTy()) {
937 if (CAZ) {
938 GenericValue intZero;
939 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
940 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
941 intZero);
942 break;
943 }
944 if(CV) {
945 for (unsigned i = 0; i < elemNum; ++i)
946 if (!isa<UndefValue>(CV->getOperand(i)))
947 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
948 CV->getOperand(i))->getValue();
949 else {
950 Result.AggregateVal[i].IntVal =
951 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
952 }
953 break;
954 }
955 if(CDV)
956 for (unsigned i = 0; i < elemNum; ++i)
957 Result.AggregateVal[i].IntVal = APInt(
958 CDV->getElementType()->getPrimitiveSizeInBits(),
959 CDV->getElementAsInteger(i));
960
961 break;
962 }
963 llvm_unreachable("Unknown constant pointer type!");
964 }
965 break;
966
Chris Lattner996fe012002-12-24 00:01:05 +0000967 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000968 SmallString<256> Msg;
969 raw_svector_ostream OS(Msg);
970 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
971 report_fatal_error(OS.str());
Chris Lattner996fe012002-12-24 00:01:05 +0000972 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000973
Chris Lattner996fe012002-12-24 00:01:05 +0000974 return Result;
975}
976
Duncan Sands1202d1b2007-12-14 19:38:31 +0000977/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
978/// with the integer held in IntVal.
979static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
980 unsigned StoreBytes) {
981 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000982 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000983
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000984 if (sys::IsLittleEndianHost) {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000985 // Little-endian host - the source is ordered from LSB to MSB. Order the
986 // destination from LSB to MSB: Do a straight copy.
987 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000988 } else {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000989 // Big-endian host - the source is an array of 64 bit words ordered from
990 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
991 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
992 while (StoreBytes > sizeof(uint64_t)) {
993 StoreBytes -= sizeof(uint64_t);
994 // May not be aligned so use memcpy.
995 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
996 Src += sizeof(uint64_t);
997 }
998
999 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
1000 }
1001}
1002
Evan Cheng09053e62008-11-04 06:10:31 +00001003void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattner229907c2011-07-18 04:54:35 +00001004 GenericValue *Ptr, Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001005 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1202d1b2007-12-14 19:38:31 +00001006
Reid Spencer87aa65f2007-03-06 03:04:04 +00001007 switch (Ty->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001008 default:
1009 dbgs() << "Cannot store value of type " << *Ty << "!\n";
1010 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001011 case Type::IntegerTyID:
1012 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer87aa65f2007-03-06 03:04:04 +00001013 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +00001014 case Type::FloatTyID:
1015 *((float*)Ptr) = Val.FloatVal;
1016 break;
1017 case Type::DoubleTyID:
1018 *((double*)Ptr) = Val.DoubleVal;
1019 break;
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001020 case Type::X86_FP80TyID:
1021 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1022 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001023 case Type::PointerTyID:
1024 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1025 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth93da3c82011-04-28 08:37:18 +00001026 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands1202d1b2007-12-14 19:38:31 +00001027
Reid Spencer87aa65f2007-03-06 03:04:04 +00001028 *((PointerTy*)Ptr) = Val.PointerVal;
1029 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001030 case Type::VectorTyID:
1031 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1032 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1033 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1034 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1035 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1036 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1037 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1038 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1039 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1040 }
1041 }
1042 break;
Chris Lattner996fe012002-12-24 00:01:05 +00001043 }
Duncan Sands1202d1b2007-12-14 19:38:31 +00001044
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001045 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands1202d1b2007-12-14 19:38:31 +00001046 // Host and target are different endian - reverse the stored bytes.
1047 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1048}
1049
1050/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1051/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1052static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1053 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greene82b63572013-01-14 21:04:45 +00001054 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1055 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands1202d1b2007-12-14 19:38:31 +00001056
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001057 if (sys::IsLittleEndianHost)
Duncan Sands1202d1b2007-12-14 19:38:31 +00001058 // Little-endian host - the destination must be ordered from LSB to MSB.
1059 // The source is ordered from LSB to MSB: Do a straight copy.
1060 memcpy(Dst, Src, LoadBytes);
1061 else {
1062 // Big-endian - the destination is an array of 64 bit words ordered from
1063 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1064 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1065 // a word.
1066 while (LoadBytes > sizeof(uint64_t)) {
1067 LoadBytes -= sizeof(uint64_t);
1068 // May not be aligned so use memcpy.
1069 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1070 Dst += sizeof(uint64_t);
1071 }
1072
1073 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1074 }
Chris Lattner996fe012002-12-24 00:01:05 +00001075}
1076
Misha Brukman857c21b2003-10-10 17:45:12 +00001077/// FIXME: document
1078///
Duncan Sands1202d1b2007-12-14 19:38:31 +00001079void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sandscd4a6be2008-03-10 16:38:37 +00001080 GenericValue *Ptr,
Chris Lattner229907c2011-07-18 04:54:35 +00001081 Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001082 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands5c65cb42007-12-10 17:43:13 +00001083
Duncan Sands1202d1b2007-12-14 19:38:31 +00001084 switch (Ty->getTypeID()) {
1085 case Type::IntegerTyID:
1086 // An APInt with all words initially zero.
1087 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1088 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1089 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +00001090 case Type::FloatTyID:
1091 Result.FloatVal = *((float*)Ptr);
1092 break;
1093 case Type::DoubleTyID:
Duncan Sands1202d1b2007-12-14 19:38:31 +00001094 Result.DoubleVal = *((double*)Ptr);
Reid Spencer87aa65f2007-03-06 03:04:04 +00001095 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001096 case Type::PointerTyID:
Reid Spencer87aa65f2007-03-06 03:04:04 +00001097 Result.PointerVal = *((PointerTy*)Ptr);
1098 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +00001099 case Type::X86_FP80TyID: {
1100 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands26d65392007-12-15 17:37:40 +00001101 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001102 uint64_t y[2];
1103 memcpy(y, Ptr, 10);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001104 Result.IntVal = APInt(80, y);
Dale Johannesena1336cf2007-09-17 18:44:13 +00001105 break;
1106 }
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001107 case Type::VectorTyID: {
1108 const VectorType *VT = cast<VectorType>(Ty);
1109 const Type *ElemT = VT->getElementType();
1110 const unsigned numElems = VT->getNumElements();
1111 if (ElemT->isFloatTy()) {
1112 Result.AggregateVal.resize(numElems);
1113 for (unsigned i = 0; i < numElems; ++i)
1114 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1115 }
1116 if (ElemT->isDoubleTy()) {
1117 Result.AggregateVal.resize(numElems);
1118 for (unsigned i = 0; i < numElems; ++i)
1119 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1120 }
1121 if (ElemT->isIntegerTy()) {
1122 GenericValue intZero;
1123 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1124 intZero.IntVal = APInt(elemBitWidth, 0);
1125 Result.AggregateVal.resize(numElems, intZero);
1126 for (unsigned i = 0; i < numElems; ++i)
1127 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1128 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1129 }
1130 break;
1131 }
Reid Spencer87aa65f2007-03-06 03:04:04 +00001132 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001133 SmallString<256> Msg;
1134 raw_svector_ostream OS(Msg);
1135 OS << "Cannot load value of type " << *Ty << "!";
1136 report_fatal_error(OS.str());
Chris Lattner7f389e82003-05-08 16:52:16 +00001137 }
Chris Lattner7f389e82003-05-08 16:52:16 +00001138}
1139
Chris Lattner996fe012002-12-24 00:01:05 +00001140void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greene0967d2d2010-01-05 01:27:39 +00001141 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesenb086d382008-08-07 01:30:15 +00001142 DEBUG(Init->dump());
Chris Lattner00245f42012-01-24 13:41:11 +00001143 if (isa<UndefValue>(Init))
Chris Lattner61753bf2004-10-16 18:19:26 +00001144 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001145
1146 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino69d62132006-01-20 18:18:40 +00001147 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001148 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino69d62132006-01-20 18:18:40 +00001149 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1150 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1151 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001152 }
1153
1154 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001155 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattner1dd86b12008-02-15 00:57:28 +00001156 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001157 }
1158
1159 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001160 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001161 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001162 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1163 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1164 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001165 }
1166
1167 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001168 const StructLayout *SL =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001169 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001170 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1171 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1172 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001173 }
1174
1175 if (const ConstantDataSequential *CDS =
1176 dyn_cast<ConstantDataSequential>(Init)) {
1177 // CDS is already laid out in host memory order.
1178 StringRef Data = CDS->getRawDataValues();
1179 memcpy(Addr, Data.data(), Data.size());
1180 return;
1181 }
1182
1183 if (Init->getType()->isFirstClassType()) {
Chris Lattner996fe012002-12-24 00:01:05 +00001184 GenericValue Val = getConstantValue(Init);
1185 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1186 return;
1187 }
1188
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001189 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinfbcc6632009-07-14 16:55:14 +00001190 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattner996fe012002-12-24 00:01:05 +00001191}
1192
Chris Lattner996fe012002-12-24 00:01:05 +00001193/// EmitGlobals - Emit all of the global variables to memory, storing their
1194/// addresses into GlobalAddress. This must make sure to copy the contents of
1195/// their initializers into the memory.
Chris Lattner996fe012002-12-24 00:01:05 +00001196void ExecutionEngine::emitGlobals() {
Chris Lattner996fe012002-12-24 00:01:05 +00001197 // Loop over all of the global variables in the program, allocating the memory
Chris Lattner0621cae2006-08-16 01:24:12 +00001198 // to hold them. If there is more than one module, do a prepass over globals
1199 // to figure out how the different modules should link together.
Chris Lattner229907c2011-07-18 04:54:35 +00001200 std::map<std::pair<std::string, Type*>,
Chris Lattner0621cae2006-08-16 01:24:12 +00001201 const GlobalValue*> LinkedGlobalsMap;
Misha Brukman835702a2005-04-21 22:36:52 +00001202
Chris Lattner0621cae2006-08-16 01:24:12 +00001203 if (Modules.size() != 1) {
1204 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001205 Module &M = *Modules[m];
Chris Lattner0621cae2006-08-16 01:24:12 +00001206 for (Module::const_global_iterator I = M.global_begin(),
1207 E = M.global_end(); I != E; ++I) {
1208 const GlobalValue *GV = I;
Rafael Espindola6de96a12009-01-15 20:18:42 +00001209 if (GV->hasLocalLinkage() || GV->isDeclaration() ||
Chris Lattner0621cae2006-08-16 01:24:12 +00001210 GV->hasAppendingLinkage() || !GV->hasName())
1211 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001212
1213 const GlobalValue *&GVEntry =
Chris Lattner0621cae2006-08-16 01:24:12 +00001214 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1215
1216 // If this is the first time we've seen this global, it is the canonical
1217 // version.
1218 if (!GVEntry) {
1219 GVEntry = GV;
1220 continue;
1221 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001222
Chris Lattner0621cae2006-08-16 01:24:12 +00001223 // If the existing global is strong, never replace it.
Nico Rieck7157bb72014-01-14 15:22:47 +00001224 if (GVEntry->hasExternalLinkage())
Chris Lattner0621cae2006-08-16 01:24:12 +00001225 continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001226
Chris Lattner0621cae2006-08-16 01:24:12 +00001227 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesence4396b2008-05-14 20:12:51 +00001228 // symbol. FIXME is this right for common?
Anton Korobeynikov12c94942006-12-01 00:25:12 +00001229 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
Chris Lattner0621cae2006-08-16 01:24:12 +00001230 GVEntry = GV;
Chris Lattner9de0d142003-04-23 19:01:49 +00001231 }
Chris Lattner996fe012002-12-24 00:01:05 +00001232 }
Chris Lattner0621cae2006-08-16 01:24:12 +00001233 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001234
Chris Lattner0621cae2006-08-16 01:24:12 +00001235 std::vector<const GlobalValue*> NonCanonicalGlobals;
1236 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001237 Module &M = *Modules[m];
Chris Lattner0621cae2006-08-16 01:24:12 +00001238 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1239 I != E; ++I) {
1240 // In the multi-module case, see what this global maps to.
1241 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001242 if (const GlobalValue *GVEntry =
Chris Lattner0621cae2006-08-16 01:24:12 +00001243 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1244 // If something else is the canonical global, ignore this one.
1245 if (GVEntry != &*I) {
1246 NonCanonicalGlobals.push_back(I);
1247 continue;
1248 }
1249 }
1250 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001251
Reid Spencer5301e7c2007-01-30 20:08:39 +00001252 if (!I->isDeclaration()) {
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001253 addGlobalMapping(I, getMemoryForGV(I));
Chris Lattner0621cae2006-08-16 01:24:12 +00001254 } else {
1255 // External variable reference. Try to use the dynamic loader to
1256 // get a pointer to it.
1257 if (void *SymAddr =
Daniel Dunbar5899e342009-07-21 08:54:24 +00001258 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
Chris Lattner0621cae2006-08-16 01:24:12 +00001259 addGlobalMapping(I, SymAddr);
1260 else {
Chris Lattner2104b8d2010-04-07 22:58:41 +00001261 report_fatal_error("Could not resolve external global address: "
Torok Edwin6c2d2332009-07-07 17:32:34 +00001262 +I->getName());
Chris Lattner0621cae2006-08-16 01:24:12 +00001263 }
1264 }
1265 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001266
Chris Lattner0621cae2006-08-16 01:24:12 +00001267 // If there are multiple modules, map the non-canonical globals to their
1268 // canonical location.
1269 if (!NonCanonicalGlobals.empty()) {
1270 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1271 const GlobalValue *GV = NonCanonicalGlobals[i];
1272 const GlobalValue *CGV =
1273 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1274 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1275 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesa67f06b2008-10-14 10:04:52 +00001276 addGlobalMapping(GV, Ptr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001277 }
1278 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001279
1280 // Now that all of the globals are set up in memory, loop through them all
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001281 // and initialize their contents.
Chris Lattner0621cae2006-08-16 01:24:12 +00001282 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1283 I != E; ++I) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001284 if (!I->isDeclaration()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001285 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001286 if (const GlobalValue *GVEntry =
Chris Lattner0621cae2006-08-16 01:24:12 +00001287 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1288 if (GVEntry != &*I) // Not the canonical variable.
1289 continue;
1290 }
1291 EmitGlobalVariable(I);
1292 }
1293 }
1294 }
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001295}
1296
1297// EmitGlobalVariable - This method emits the specified global variable to the
1298// address specified in GlobalAddresses, or allocates new memory if it's not
1299// already in the map.
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +00001300void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner748e8572003-12-31 20:21:04 +00001301 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattnerdc631732004-02-08 19:33:23 +00001302
Craig Topper2617dcc2014-04-15 06:32:26 +00001303 if (!GA) {
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001304 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001305 GA = getMemoryForGV(GV);
Andrew Kaylor3b442372013-11-15 17:52:54 +00001306
1307 // If we failed to allocate memory for this global, return.
Craig Topper2617dcc2014-04-15 06:32:26 +00001308 if (!GA) return;
Andrew Kaylor3b442372013-11-15 17:52:54 +00001309
Chris Lattner748e8572003-12-31 20:21:04 +00001310 addGlobalMapping(GV, GA);
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001311 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001312
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001313 // Don't initialize if it's thread local, let the client do it.
1314 if (!GV->isThreadLocal())
1315 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001316
Chris Lattner229907c2011-07-18 04:54:35 +00001317 Type *ElTy = GV->getType()->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001318 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattnerdf1f1522005-01-08 20:13:19 +00001319 NumInitBytes += (unsigned)GVSize;
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001320 ++NumGlobals;
Chris Lattner996fe012002-12-24 00:01:05 +00001321}
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001322
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001323ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1324 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001325}
1326
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001327sys::Mutex *
1328ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001329 return &EES->EE.lock;
1330}
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001331
1332void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1333 const GlobalValue *Old) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001334 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1335 EES->GlobalAddressReverseMap.erase(OldVal);
1336}
1337
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001338void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1339 const GlobalValue *,
1340 const GlobalValue *) {
Craig Toppera2886c22012-02-07 05:05:23 +00001341 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1342 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001343}