blob: 4768e67a1e97f872b29b92c4f7dbd55b3cc598c4 [file] [log] [blame]
Misha Brukman4afac182003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattnerbd199fb2002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner3785fad2003-08-05 17:00:32 +000015#define DEBUG_TYPE "jit"
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +000016#include "llvm/ExecutionEngine/ExecutionEngine.h"
Daniel Dunbar48dd8752010-11-13 02:48:57 +000017#include "llvm/ADT/SmallString.h"
Chris Lattner9f3ff922009-08-23 22:49:13 +000018#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/ExecutionEngine/GenericValue.h"
Stephen Hines36b56882014-04-23 16:57:46 -070020#include "llvm/ExecutionEngine/JITMemoryManager.h"
21#include "llvm/ExecutionEngine/ObjectCache.h"
Chandler Carruth0b8c9a82013-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"
Stephen Hines36b56882014-04-23 16:57:46 -070027#include "llvm/IR/ValueHandle.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000028#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/DynamicLibrary.h"
Torok Edwin31e24662009-07-07 17:32:34 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/Host.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000032#include "llvm/Support/MutexGuard.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000033#include "llvm/Support/TargetRegistry.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/raw_ostream.h"
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000035#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000036#include <cmath>
37#include <cstring>
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000038using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000039
Chris Lattner36343732006-12-19 22:43:32 +000040STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
41STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000042
Juergen Ributzka35436252013-11-19 00:57:56 +000043// Pin the vtable to this file.
44void ObjectCache::anchor() {}
45void ObjectBuffer::anchor() {}
46void ObjectBufferStream::anchor() {}
47
Jeffrey Yasskin46882612010-02-05 16:19:36 +000048ExecutionEngine *(*ExecutionEngine::JITCtor)(
49 Module *M,
50 std::string *ErrorStr,
51 JITMemoryManager *JMM,
Jeffrey Yasskin46882612010-02-05 16:19:36 +000052 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000053 TargetMachine *TM) = 0;
Daniel Dunbar6d135972010-11-17 16:06:37 +000054ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
55 Module *M,
56 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +000057 RTDyldMemoryManager *MCJMM,
Daniel Dunbar6d135972010-11-17 16:06:37 +000058 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000059 TargetMachine *TM) = 0;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000060ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
Reid Kleckner4b1511b2009-07-18 00:42:18 +000061 std::string *ErrorStr) = 0;
Chris Lattner2fe4bb02006-03-22 06:07:50 +000062
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000063ExecutionEngine::ExecutionEngine(Module *M)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +000064 : EEState(*this),
Rafael Espindolad0acc842013-10-07 13:54:50 +000065 LazyFunctionCreator(0) {
Jeffrey Yasskindc857242009-10-27 20:30:28 +000066 CompilingLazily = false;
Evan Cheng446531e2008-09-24 16:25:55 +000067 GVCompilationDisabled = false;
Evan Cheng1b088f32008-06-17 16:49:02 +000068 SymbolSearchingDisabled = false;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000069 Modules.push_back(M);
70 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000071}
72
Brian Gaeke8e539482003-09-04 22:57:27 +000073ExecutionEngine::~ExecutionEngine() {
Reid Spencerd4c0e622007-03-03 18:19:18 +000074 clearAllGlobalMappings();
Chris Lattnerfe854032006-08-16 01:24:12 +000075 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
76 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000077}
78
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000079namespace {
Daniel Dunbar48dd8752010-11-13 02:48:57 +000080/// \brief Helper class which uses a value handler to automatically deletes the
81/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000082class GVMemoryBlock : public CallbackVH {
83 GVMemoryBlock(const GlobalVariable *GV)
84 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
85
86public:
Daniel Dunbar48dd8752010-11-13 02:48:57 +000087 /// \brief Returns the address the GlobalVariable should be written into. The
88 /// GVMemoryBlock object prefixes that.
Micah Villmow3574eca2012-10-08 16:38:25 +000089 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000090 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000091 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
92 void *RawMemory = ::operator new(
Micah Villmow3574eca2012-10-08 16:38:25 +000093 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000094 TD.getPreferredAlignment(GV))
95 + GVSize);
96 new(RawMemory) GVMemoryBlock(GV);
97 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
98 }
99
Stephen Hines36b56882014-04-23 16:57:46 -0700100 void deleted() override {
Jeffrey Yasskin47b71122010-03-27 04:53:56 +0000101 // We allocated with operator new and with some extra memory hanging off the
102 // end, so don't just delete this. I'm not sure if this is actually
103 // required.
104 this->~GVMemoryBlock();
105 ::operator delete(this);
106 }
107};
108} // anonymous namespace
109
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000110char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000111 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000112}
113
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000114bool ExecutionEngine::removeModule(Module *M) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000115 for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
Devang Patel73d0e212007-10-15 19:56:32 +0000116 E = Modules.end(); I != E; ++I) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000117 Module *Found = *I;
118 if (Found == M) {
Devang Patel73d0e212007-10-15 19:56:32 +0000119 Modules.erase(I);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000120 clearGlobalMappingsFromModule(M);
121 return true;
Devang Patel73d0e212007-10-15 19:56:32 +0000122 }
123 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000124 return false;
Nate Begeman60789e42009-01-23 19:27:28 +0000125}
126
Chris Lattnerfe854032006-08-16 01:24:12 +0000127Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
128 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000129 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattnerfe854032006-08-16 01:24:12 +0000130 return F;
131 }
132 return 0;
133}
134
135
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000136void *ExecutionEngineState::RemoveMapping(const MutexGuard &,
137 const GlobalValue *ToUnmap) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000138 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000139 void *OldVal;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000140
141 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
142 // GlobalAddressMap.
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000143 if (I == GlobalAddressMap.end())
144 OldVal = 0;
145 else {
146 OldVal = I->second;
147 GlobalAddressMap.erase(I);
148 }
149
150 GlobalAddressReverseMap.erase(OldVal);
151 return OldVal;
152}
153
Chris Lattnere7fd5532006-05-08 22:00:52 +0000154void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
155 MutexGuard locked(lock);
Evan Chengbc4707a2008-09-18 07:54:21 +0000156
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000157 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar93b67e42009-07-26 07:49:05 +0000158 << "\' to [" << Addr << "]\n";);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000159 void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000160 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
161 CurVal = Addr;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000162
163 // If we are using the reverse mapping, add it too.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000164 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000165 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000166 EEState.getGlobalAddressReverseMap(locked)[Addr];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000167 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
168 V = GV;
169 }
170}
171
Chris Lattnere7fd5532006-05-08 22:00:52 +0000172void ExecutionEngine::clearAllGlobalMappings() {
173 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000174
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000175 EEState.getGlobalAddressMap(locked).clear();
176 EEState.getGlobalAddressReverseMap(locked).clear();
Chris Lattnere7fd5532006-05-08 22:00:52 +0000177}
178
Nate Begemanf049e072008-05-21 16:34:48 +0000179void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
180 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000181
182 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000183 EEState.RemoveMapping(locked, FI);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000184 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
185 GI != GE; ++GI)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000186 EEState.RemoveMapping(locked, GI);
Nate Begemanf049e072008-05-21 16:34:48 +0000187}
188
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000189void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000190 MutexGuard locked(lock);
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000191
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000192 ExecutionEngineState::GlobalAddressMapTy &Map =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000193 EEState.getGlobalAddressMap(locked);
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000194
Chris Lattnere7fd5532006-05-08 22:00:52 +0000195 // Deleting from the mapping?
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000196 if (Addr == 0)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000197 return EEState.RemoveMapping(locked, GV);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000198
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000199 void *&CurVal = Map[GV];
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000200 void *OldVal = CurVal;
201
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000202 if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
203 EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
Chris Lattnere7fd5532006-05-08 22:00:52 +0000204 CurVal = Addr;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000205
206 // If we are using the reverse mapping, add it too.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000207 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000208 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000209 EEState.getGlobalAddressReverseMap(locked)[Addr];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000210 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
211 V = GV;
212 }
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000213 return OldVal;
Chris Lattnere7fd5532006-05-08 22:00:52 +0000214}
215
Chris Lattnere7fd5532006-05-08 22:00:52 +0000216void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
217 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000218
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000219 ExecutionEngineState::GlobalAddressMapTy::iterator I =
220 EEState.getGlobalAddressMap(locked).find(GV);
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000221 return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
Chris Lattnere7fd5532006-05-08 22:00:52 +0000222}
223
Chris Lattner55d86482003-12-31 20:21:04 +0000224const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000225 MutexGuard locked(lock);
226
Chris Lattner55d86482003-12-31 20:21:04 +0000227 // If we haven't computed the reverse mapping yet, do so first.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000228 if (EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000229 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000230 I = EEState.getGlobalAddressMap(locked).begin(),
231 E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
Daniel Dunbarab19da42010-11-13 00:55:42 +0000232 EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
233 I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000234 }
235
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000236 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000237 EEState.getGlobalAddressReverseMap(locked).find(Addr);
238 return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000239}
Chris Lattner87f03102003-12-26 06:50:30 +0000240
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000241namespace {
242class ArgvArray {
243 char *Array;
244 std::vector<char*> Values;
245public:
246 ArgvArray() : Array(NULL) {}
247 ~ArgvArray() { clear(); }
248 void clear() {
249 delete[] Array;
250 Array = NULL;
251 for (size_t I = 0, E = Values.size(); I != E; ++I) {
252 delete[] Values[I];
253 }
254 Values.clear();
255 }
256 /// Turn a vector of strings into a nice argv style array of pointers to null
257 /// terminated strings.
258 void *reset(LLVMContext &C, ExecutionEngine *EE,
259 const std::vector<std::string> &InputArgv);
260};
261} // anonymous namespace
262void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
263 const std::vector<std::string> &InputArgv) {
264 clear(); // Free the old contents.
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000265 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000266 Array = new char[(InputArgv.size()+1)*PtrSize];
Chris Lattner87f03102003-12-26 06:50:30 +0000267
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000268 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000269 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner87f03102003-12-26 06:50:30 +0000270
271 for (unsigned i = 0; i != InputArgv.size(); ++i) {
272 unsigned Size = InputArgv[i].size()+1;
273 char *Dest = new char[Size];
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000274 Values.push_back(Dest);
David Greeneae7c59a2010-01-05 01:27:39 +0000275 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000276
Chris Lattner87f03102003-12-26 06:50:30 +0000277 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
278 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000279
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000280 // Endian safe: Array[i] = (PointerTy)Dest;
281 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
Chris Lattner87f03102003-12-26 06:50:30 +0000282 SBytePtr);
283 }
284
285 // Null terminate it
286 EE->StoreValueToMemory(PTOGV(0),
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000287 (GenericValue*)(Array+InputArgv.size()*PtrSize),
Chris Lattner87f03102003-12-26 06:50:30 +0000288 SBytePtr);
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000289 return Array;
Chris Lattner87f03102003-12-26 06:50:30 +0000290}
291
Chris Lattnerfbd39762009-09-23 01:46:04 +0000292void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
293 bool isDtors) {
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000294 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000295 GlobalVariable *GV = module->getNamedGlobal(Name);
Evan Cheng18314dc2008-09-30 15:51:21 +0000296
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000297 // If this global has internal linkage, or if it has a use, then it must be
298 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
299 // this is the case, don't execute any of the global ctors, __main will do
300 // it.
301 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
302
Nick Lewyckya040d472011-04-06 20:38:44 +0000303 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000304 // the init priority, which we ignore.
Chris Lattner1ee0ecf2012-01-24 13:41:11 +0000305 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
306 if (InitList == 0)
Nick Lewycky5ea5c612011-04-11 22:11:20 +0000307 return;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000308 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +0000309 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
310 if (CS == 0) continue;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000311
312 Constant *FP = CS->getOperand(1);
313 if (FP->isNullValue())
Nick Lewycky5ea5c612011-04-11 22:11:20 +0000314 continue; // Found a sentinal value, ignore.
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000315
316 // Strip off constant expression casts.
317 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
318 if (CE->isCast())
319 FP = CE->getOperand(0);
320
321 // Execute the ctor/dtor function!
322 if (Function *F = dyn_cast<Function>(FP))
323 runFunction(F, std::vector<GenericValue>());
324
325 // FIXME: It is marginally lame that we just do nothing here if we see an
326 // entry we don't recognize. It might not be unreasonable for the verifier
327 // to not even allow this and just assert here.
328 }
Evan Cheng18314dc2008-09-30 15:51:21 +0000329}
330
Evan Cheng18314dc2008-09-30 15:51:21 +0000331void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
332 // Execute global ctors/dtors for each module in the program.
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000333 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
334 runStaticConstructorsDestructors(Modules[i], isDtors);
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000335}
336
Dan Gohmanb6e3d6c2008-08-26 01:38:29 +0000337#ifndef NDEBUG
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000338/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
339static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000340 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000341 for (unsigned i = 0; i < PtrSize; ++i)
342 if (*(i + (uint8_t*)Loc))
343 return false;
344 return true;
345}
Dan Gohmanb6e3d6c2008-08-26 01:38:29 +0000346#endif
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000347
Chris Lattner87f03102003-12-26 06:50:30 +0000348int ExecutionEngine::runFunctionAsMain(Function *Fn,
349 const std::vector<std::string> &argv,
350 const char * const * envp) {
351 std::vector<GenericValue> GVArgs;
352 GenericValue GVArgc;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000353 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov499d8f02007-06-03 19:17:35 +0000354
355 // Check main() type
Chris Lattnerf24d0992004-08-16 01:05:35 +0000356 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000357 FunctionType *FTy = Fn->getFunctionType();
358 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000359
360 // Check the argument types.
361 if (NumArgs > 3)
362 report_fatal_error("Invalid number of arguments of main() supplied");
363 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
364 report_fatal_error("Invalid type for third argument of main() supplied");
365 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
366 report_fatal_error("Invalid type for second argument of main() supplied");
367 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
368 report_fatal_error("Invalid type for first argument of main() supplied");
369 if (!FTy->getReturnType()->isIntegerTy() &&
370 !FTy->getReturnType()->isVoidTy())
371 report_fatal_error("Invalid return type of main() supplied");
372
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000373 ArgvArray CArgv;
374 ArgvArray CEnv;
Chris Lattnerf24d0992004-08-16 01:05:35 +0000375 if (NumArgs) {
376 GVArgs.push_back(GVArgc); // Arg #0 = argc.
377 if (NumArgs > 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000378 // Arg #1 = argv.
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000379 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000380 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerf24d0992004-08-16 01:05:35 +0000381 "argv[0] was null after CreateArgv");
382 if (NumArgs > 2) {
383 std::vector<std::string> EnvVars;
384 for (unsigned i = 0; envp[i]; ++i)
385 EnvVars.push_back(envp[i]);
Owen Anderson1d0be152009-08-13 21:58:54 +0000386 // Arg #2 = envp.
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000387 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerf24d0992004-08-16 01:05:35 +0000388 }
389 }
390 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000391
Reid Spencer8fb0f192007-03-06 03:04:04 +0000392 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner87f03102003-12-26 06:50:30 +0000393}
394
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000395ExecutionEngine *ExecutionEngine::create(Module *M,
Reid Spencerd4c0e622007-03-03 18:19:18 +0000396 bool ForceInterpreter,
Evan Cheng502f20b2008-08-08 08:11:34 +0000397 std::string *ErrorStr,
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000398 CodeGenOpt::Level OptLevel,
399 bool GVsWithCode) {
Owen Anderson8e1fc562012-03-23 17:40:56 +0000400 EngineBuilder EB = EngineBuilder(M)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000401 .setEngineKind(ForceInterpreter
402 ? EngineKind::Interpreter
403 : EngineKind::JIT)
404 .setErrorStr(ErrorStr)
405 .setOptLevel(OptLevel)
Owen Anderson8e1fc562012-03-23 17:40:56 +0000406 .setAllocateGVsWithCode(GVsWithCode);
407
408 return EB.create();
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000409}
Brian Gaeke82d82772003-09-03 20:34:19 +0000410
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000411/// createJIT - This is the factory method for creating a JIT for the current
412/// machine, it does not fall back to the interpreter. This takes ownership
413/// of the module.
414ExecutionEngine *ExecutionEngine::createJIT(Module *M,
415 std::string *ErrorStr,
416 JITMemoryManager *JMM,
Dylan Noblesmithd95e67d2011-12-01 21:49:21 +0000417 CodeGenOpt::Level OL,
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000418 bool GVsWithCode,
Evan Cheng43966132011-07-19 06:37:02 +0000419 Reloc::Model RM,
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000420 CodeModel::Model CMM) {
421 if (ExecutionEngine::JITCtor == 0) {
422 if (ErrorStr)
423 *ErrorStr = "JIT has not been linked in.";
424 return 0;
425 }
426
427 // Use the defaults for extra parameters. Users can use EngineBuilder to
428 // set them.
Owen Anderson8e1fc562012-03-23 17:40:56 +0000429 EngineBuilder EB(M);
430 EB.setEngineKind(EngineKind::JIT);
431 EB.setErrorStr(ErrorStr);
432 EB.setRelocationModel(RM);
433 EB.setCodeModel(CMM);
434 EB.setAllocateGVsWithCode(GVsWithCode);
435 EB.setOptLevel(OL);
436 EB.setJITMemoryManager(JMM);
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000437
Peter Collingbourned40e1032011-12-07 23:58:57 +0000438 // TODO: permit custom TargetOptions here
Owen Anderson8e1fc562012-03-23 17:40:56 +0000439 TargetMachine *TM = EB.selectTarget();
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000440 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000441
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000442 return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000443}
444
Owen Anderson8e1fc562012-03-23 17:40:56 +0000445ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Stephen Hines36b56882014-04-23 16:57:46 -0700446 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
Benjamin Kramer0f554492012-04-08 14:53:14 +0000447
Nick Lewycky6456d862008-03-08 02:49:45 +0000448 // Make sure we can resolve symbols in the program as well. The zero arg
449 // to the function tells DynamicLibrary to load the program, not a library.
450 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
451 return 0;
452
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000453 assert(!(JMM && MCJMM));
454
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000455 // If the user specified a memory manager but didn't specify which engine to
456 // create, we assume they only want the JIT, and we fail if they only want
457 // the interpreter.
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000458 if (JMM || MCJMM) {
Chris Lattnerfbd39762009-09-23 01:46:04 +0000459 if (WhichEngine & EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000460 WhichEngine = EngineKind::JIT;
Chris Lattnerfbd39762009-09-23 01:46:04 +0000461 else {
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000462 if (ErrorStr)
463 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Chris Lattnerfbd39762009-09-23 01:46:04 +0000464 return 0;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000465 }
466 }
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000467
468 if (MCJMM && ! UseMCJIT) {
469 if (ErrorStr)
470 *ErrorStr =
471 "Cannot create a legacy JIT with a runtime dyld memory "
472 "manager.";
473 return 0;
474 }
Brian Gaeke82d82772003-09-03 20:34:19 +0000475
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000476 // Unless the interpreter was explicitly selected or the JIT is not linked,
477 // try making a JIT.
Benjamin Kramer0f554492012-04-08 14:53:14 +0000478 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000479 Triple TT(M->getTargetTriple());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000480 if (!TM->getTarget().hasJIT()) {
481 errs() << "WARNING: This target JIT is not designed for the host"
482 << " you are running. If bad things happen, please choose"
483 << " a different -march switch.\n";
484 }
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000485
Owen Anderson8e1fc562012-03-23 17:40:56 +0000486 if (UseMCJIT && ExecutionEngine::MCJITCtor) {
487 ExecutionEngine *EE =
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000488 ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
Stephen Hines36b56882014-04-23 16:57:46 -0700489 AllocateGVsWithCode, TheTM.release());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000490 if (EE) return EE;
491 } else if (ExecutionEngine::JITCtor) {
492 ExecutionEngine *EE =
493 ExecutionEngine::JITCtor(M, ErrorStr, JMM,
Stephen Hines36b56882014-04-23 16:57:46 -0700494 AllocateGVsWithCode, TheTM.release());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000495 if (EE) return EE;
Chris Lattnerfbd39762009-09-23 01:46:04 +0000496 }
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000497 }
498
499 // If we can't make a JIT and we didn't request one specifically, try making
500 // an interpreter instead.
Chris Lattnerfbd39762009-09-23 01:46:04 +0000501 if (WhichEngine & EngineKind::Interpreter) {
502 if (ExecutionEngine::InterpCtor)
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000503 return ExecutionEngine::InterpCtor(M, ErrorStr);
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000504 if (ErrorStr)
505 *ErrorStr = "Interpreter has not been linked in.";
Chris Lattnerfbd39762009-09-23 01:46:04 +0000506 return 0;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000507 }
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000508
Jim Grosbach8005bcd2012-08-21 15:42:49 +0000509 if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0 &&
510 ExecutionEngine::MCJITCtor == 0) {
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000511 if (ErrorStr)
512 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000513 }
514
Chris Lattnerfbd39762009-09-23 01:46:04 +0000515 return 0;
Brian Gaeke82d82772003-09-03 20:34:19 +0000516}
517
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000518void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000519 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000520 return getPointerToFunction(F);
521
Reid Spenceree448632005-07-12 15:51:55 +0000522 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000523 if (void *P = EEState.getGlobalAddressMap(locked)[GV])
524 return P;
Jeff Cohen68835dd2006-02-07 05:11:57 +0000525
526 // Global variable might have been added since interpreter started.
527 if (GlobalVariable *GVar =
528 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
529 EmitGlobalVariable(GVar);
530 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000531 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000532
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000533 return EEState.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000534}
535
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000536/// \brief Converts a Constant* into a GenericValue, including handling of
537/// ConstantExpr values.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000538GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000539 // If its undefined, return the garbage.
Jay Foad5b370122010-01-15 08:32:58 +0000540 if (isa<UndefValue>(C)) {
541 GenericValue Result;
542 switch (C->getType()->getTypeID()) {
Nadav Rotem953783e2013-04-01 15:53:30 +0000543 default:
544 break;
Jay Foad5b370122010-01-15 08:32:58 +0000545 case Type::IntegerTyID:
546 case Type::X86_FP80TyID:
547 case Type::FP128TyID:
548 case Type::PPC_FP128TyID:
549 // Although the value is undefined, we still have to construct an APInt
550 // with the correct bit width.
551 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
552 break;
Elena Demikhovsky3c5ce292013-09-12 10:48:23 +0000553 case Type::StructTyID: {
554 // if the whole struct is 'undef' just reserve memory for the value.
555 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
556 unsigned int elemNum = STy->getNumElements();
557 Result.AggregateVal.resize(elemNum);
558 for (unsigned int i = 0; i < elemNum; ++i) {
559 Type *ElemTy = STy->getElementType(i);
560 if (ElemTy->isIntegerTy())
561 Result.AggregateVal[i].IntVal =
562 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
563 else if (ElemTy->isAggregateType()) {
564 const Constant *ElemUndef = UndefValue::get(ElemTy);
565 Result.AggregateVal[i] = getConstantValue(ElemUndef);
566 }
567 }
568 }
569 }
570 break;
Nadav Rotem953783e2013-04-01 15:53:30 +0000571 case Type::VectorTyID:
572 // if the whole vector is 'undef' just reserve memory for the value.
573 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
574 const Type *ElemTy = VTy->getElementType();
575 unsigned int elemNum = VTy->getNumElements();
576 Result.AggregateVal.resize(elemNum);
577 if (ElemTy->isIntegerTy())
578 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky3c5ce292013-09-12 10:48:23 +0000579 Result.AggregateVal[i].IntVal =
Nadav Rotem953783e2013-04-01 15:53:30 +0000580 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foad5b370122010-01-15 08:32:58 +0000581 break;
582 }
583 return Result;
584 }
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000585
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000586 // Otherwise, if the value is a ConstantExpr...
Reid Spencer3da59db2006-11-27 01:05:10 +0000587 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencerbce30f12007-03-06 22:23:15 +0000588 Constant *Op0 = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000589 switch (CE->getOpcode()) {
590 case Instruction::GetElementPtr: {
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000591 // Compute the index
Reid Spencerbce30f12007-03-06 22:23:15 +0000592 GenericValue Result = getConstantValue(Op0);
Stephen Hines36b56882014-04-23 16:57:46 -0700593 APInt Offset(DL->getPointerSizeInBits(), 0);
594 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000595
Reid Spencer8fb0f192007-03-06 03:04:04 +0000596 char* tmp = (char*) Result.PointerVal;
Nuno Lopes98281a22012-12-30 16:25:48 +0000597 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000598 return Result;
599 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000600 case Instruction::Trunc: {
601 GenericValue GV = getConstantValue(Op0);
602 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
603 GV.IntVal = GV.IntVal.trunc(BitWidth);
604 return GV;
605 }
606 case Instruction::ZExt: {
607 GenericValue GV = getConstantValue(Op0);
608 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
609 GV.IntVal = GV.IntVal.zext(BitWidth);
610 return GV;
611 }
612 case Instruction::SExt: {
613 GenericValue GV = getConstantValue(Op0);
614 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
615 GV.IntVal = GV.IntVal.sext(BitWidth);
616 return GV;
617 }
618 case Instruction::FPTrunc: {
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000619 // FIXME long double
Reid Spencerbce30f12007-03-06 22:23:15 +0000620 GenericValue GV = getConstantValue(Op0);
621 GV.FloatVal = float(GV.DoubleVal);
622 return GV;
623 }
624 case Instruction::FPExt:{
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000625 // FIXME long double
Reid Spencerbce30f12007-03-06 22:23:15 +0000626 GenericValue GV = getConstantValue(Op0);
627 GV.DoubleVal = double(GV.FloatVal);
628 return GV;
629 }
630 case Instruction::UIToFP: {
631 GenericValue GV = getConstantValue(Op0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000632 if (CE->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000633 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000634 else if (CE->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000635 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000636 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +0000637 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000638 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohman62824062008-02-29 01:27:13 +0000639 false,
640 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000641 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000642 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000643 return GV;
644 }
645 case Instruction::SIToFP: {
646 GenericValue GV = getConstantValue(Op0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000647 if (CE->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000648 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000649 else if (CE->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000650 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000651 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +0000652 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000653 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohman62824062008-02-29 01:27:13 +0000654 true,
655 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000656 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000657 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000658 return GV;
659 }
660 case Instruction::FPToUI: // double->APInt conversion handles sign
661 case Instruction::FPToSI: {
662 GenericValue GV = getConstantValue(Op0);
663 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000664 if (Op0->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000665 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000666 else if (Op0->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000667 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000668 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover0a29cb02013-01-22 09:46:31 +0000669 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000670 uint64_t v;
Dale Johannesen23a98552008-10-09 23:00:39 +0000671 bool ignored;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000672 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000673 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen23a98552008-10-09 23:00:39 +0000674 APFloat::rmTowardZero, &ignored);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000675 GV.IntVal = v; // endian?
676 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000677 return GV;
678 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000679 case Instruction::PtrToInt: {
Reid Spencerbce30f12007-03-06 22:23:15 +0000680 GenericValue GV = getConstantValue(Op0);
Stephen Hines36b56882014-04-23 16:57:46 -0700681 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000682 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencerbce30f12007-03-06 22:23:15 +0000683 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Stephen Hines36b56882014-04-23 16:57:46 -0700684 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000685 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencerbce30f12007-03-06 22:23:15 +0000686 return GV;
687 }
688 case Instruction::IntToPtr: {
689 GenericValue GV = getConstantValue(Op0);
Stephen Hines36b56882014-04-23 16:57:46 -0700690 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000691 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencerbce30f12007-03-06 22:23:15 +0000692 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
693 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer3da59db2006-11-27 01:05:10 +0000694 return GV;
695 }
696 case Instruction::BitCast: {
Reid Spencerbce30f12007-03-06 22:23:15 +0000697 GenericValue GV = getConstantValue(Op0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000698 Type* DestTy = CE->getType();
Reid Spencerbce30f12007-03-06 22:23:15 +0000699 switch (Op0->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000700 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencerbce30f12007-03-06 22:23:15 +0000701 case Type::IntegerTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000702 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000703 if (DestTy->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000704 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000705 else if (DestTy->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000706 GV.DoubleVal = GV.IntVal.bitsToDouble();
707 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000708 case Type::FloatTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000709 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foade4d19c92010-11-28 21:04:48 +0000710 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencerbce30f12007-03-06 22:23:15 +0000711 break;
712 case Type::DoubleTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000713 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foade4d19c92010-11-28 21:04:48 +0000714 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencerbce30f12007-03-06 22:23:15 +0000715 break;
716 case Type::PointerTyID:
Duncan Sands1df98592010-02-16 11:11:14 +0000717 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencerbce30f12007-03-06 22:23:15 +0000718 break; // getConstantValue(Op0) above already converted it
719 }
720 return GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000721 }
Chris Lattner9a231222003-05-14 17:51:49 +0000722 case Instruction::Add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000723 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000724 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000725 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000726 case Instruction::Mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000727 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000728 case Instruction::UDiv:
729 case Instruction::SDiv:
730 case Instruction::URem:
731 case Instruction::SRem:
732 case Instruction::And:
733 case Instruction::Or:
734 case Instruction::Xor: {
735 GenericValue LHS = getConstantValue(Op0);
736 GenericValue RHS = getConstantValue(CE->getOperand(1));
737 GenericValue GV;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000738 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000739 default: llvm_unreachable("Bad add type!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000740 case Type::IntegerTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000741 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000742 default: llvm_unreachable("Invalid integer opcode");
Reid Spencerbce30f12007-03-06 22:23:15 +0000743 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
744 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
745 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
746 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
747 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
748 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
749 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
750 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
751 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
752 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
753 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000754 break;
755 case Type::FloatTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000756 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000757 default: llvm_unreachable("Invalid float opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000758 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000759 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000760 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000761 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000762 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000763 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000764 case Instruction::FDiv:
Reid Spencerbce30f12007-03-06 22:23:15 +0000765 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000766 case Instruction::FRem:
Chris Lattner87565c12010-05-15 17:10:24 +0000767 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencerbce30f12007-03-06 22:23:15 +0000768 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000769 break;
770 case Type::DoubleTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000771 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000772 default: llvm_unreachable("Invalid double opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000773 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000774 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000775 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000776 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000777 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000778 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000779 case Instruction::FDiv:
Reid Spencerbce30f12007-03-06 22:23:15 +0000780 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000781 case Instruction::FRem:
Chris Lattner87565c12010-05-15 17:10:24 +0000782 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencerbce30f12007-03-06 22:23:15 +0000783 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000784 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000785 case Type::X86_FP80TyID:
786 case Type::PPC_FP128TyID:
787 case Type::FP128TyID: {
Tim Northover0a29cb02013-01-22 09:46:31 +0000788 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
789 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000790 switch (CE->getOpcode()) {
Daniel Dunbarab19da42010-11-13 00:55:42 +0000791 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000792 case Instruction::FAdd:
Tim Northover0a29cb02013-01-22 09:46:31 +0000793 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000794 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000795 break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000796 case Instruction::FSub:
Tim Northover0a29cb02013-01-22 09:46:31 +0000797 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
798 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000799 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000800 break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000801 case Instruction::FMul:
Tim Northover0a29cb02013-01-22 09:46:31 +0000802 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
803 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000804 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000805 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000806 case Instruction::FDiv:
Tim Northover0a29cb02013-01-22 09:46:31 +0000807 apfLHS.divide(APFloat(Sem, RHS.IntVal),
808 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000809 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000810 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000811 case Instruction::FRem:
Tim Northover0a29cb02013-01-22 09:46:31 +0000812 apfLHS.mod(APFloat(Sem, RHS.IntVal),
813 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000814 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000815 break;
816 }
817 }
818 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000819 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000820 return GV;
821 }
Chris Lattner9a231222003-05-14 17:51:49 +0000822 default:
823 break;
824 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000825
826 SmallString<256> Msg;
827 raw_svector_ostream OS(Msg);
828 OS << "ConstantExpr not handled: " << *CE;
829 report_fatal_error(OS.str());
Chris Lattner9a231222003-05-14 17:51:49 +0000830 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000831
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000832 // Otherwise, we have a simple constant.
Reid Spencerbce30f12007-03-06 22:23:15 +0000833 GenericValue Result;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000834 switch (C->getType()->getTypeID()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000835 case Type::FloatTyID:
836 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000837 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000838 case Type::DoubleTyID:
Dale Johannesen43421b32007-09-06 18:13:44 +0000839 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer8fb0f192007-03-06 03:04:04 +0000840 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000841 case Type::X86_FP80TyID:
842 case Type::FP128TyID:
843 case Type::PPC_FP128TyID:
Dale Johannesen7111b022008-10-09 18:53:47 +0000844 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000845 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000846 case Type::IntegerTyID:
847 Result.IntVal = cast<ConstantInt>(C)->getValue();
848 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000849 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000850 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000851 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000852 else if (const Function *F = dyn_cast<Function>(C))
853 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000854 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer40cf2f92004-07-18 00:41:27 +0000855 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000856 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
857 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
858 BA->getBasicBlock())));
Reid Spencer40cf2f92004-07-18 00:41:27 +0000859 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000860 llvm_unreachable("Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000861 break;
Nadav Rotem953783e2013-04-01 15:53:30 +0000862 case Type::VectorTyID: {
863 unsigned elemNum;
864 Type* ElemTy;
865 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
866 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
867 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
868
869 if (CDV) {
870 elemNum = CDV->getNumElements();
871 ElemTy = CDV->getElementType();
872 } else if (CV || CAZ) {
873 VectorType* VTy = dyn_cast<VectorType>(C->getType());
874 elemNum = VTy->getNumElements();
875 ElemTy = VTy->getElementType();
876 } else {
877 llvm_unreachable("Unknown constant vector type!");
878 }
879
880 Result.AggregateVal.resize(elemNum);
881 // Check if vector holds floats.
882 if(ElemTy->isFloatTy()) {
883 if (CAZ) {
884 GenericValue floatZero;
885 floatZero.FloatVal = 0.f;
886 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
887 floatZero);
888 break;
889 }
890 if(CV) {
891 for (unsigned i = 0; i < elemNum; ++i)
892 if (!isa<UndefValue>(CV->getOperand(i)))
893 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
894 CV->getOperand(i))->getValueAPF().convertToFloat();
895 break;
896 }
897 if(CDV)
898 for (unsigned i = 0; i < elemNum; ++i)
899 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
900
901 break;
902 }
903 // Check if vector holds doubles.
904 if (ElemTy->isDoubleTy()) {
905 if (CAZ) {
906 GenericValue doubleZero;
907 doubleZero.DoubleVal = 0.0;
908 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
909 doubleZero);
910 break;
911 }
912 if(CV) {
913 for (unsigned i = 0; i < elemNum; ++i)
914 if (!isa<UndefValue>(CV->getOperand(i)))
915 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
916 CV->getOperand(i))->getValueAPF().convertToDouble();
917 break;
918 }
919 if(CDV)
920 for (unsigned i = 0; i < elemNum; ++i)
921 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
922
923 break;
924 }
925 // Check if vector holds integers.
926 if (ElemTy->isIntegerTy()) {
927 if (CAZ) {
928 GenericValue intZero;
929 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
930 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
931 intZero);
932 break;
933 }
934 if(CV) {
935 for (unsigned i = 0; i < elemNum; ++i)
936 if (!isa<UndefValue>(CV->getOperand(i)))
937 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
938 CV->getOperand(i))->getValue();
939 else {
940 Result.AggregateVal[i].IntVal =
941 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
942 }
943 break;
944 }
945 if(CDV)
946 for (unsigned i = 0; i < elemNum; ++i)
947 Result.AggregateVal[i].IntVal = APInt(
948 CDV->getElementType()->getPrimitiveSizeInBits(),
949 CDV->getElementAsInteger(i));
950
951 break;
952 }
953 llvm_unreachable("Unknown constant pointer type!");
954 }
955 break;
956
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000957 default:
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000958 SmallString<256> Msg;
959 raw_svector_ostream OS(Msg);
960 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
961 report_fatal_error(OS.str());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000962 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000963
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000964 return Result;
965}
966
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000967/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
968/// with the integer held in IntVal.
969static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
970 unsigned StoreBytes) {
971 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divacky59324292012-09-05 22:26:57 +0000972 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000973
Rafael Espindola21a01d12013-04-15 14:44:24 +0000974 if (sys::IsLittleEndianHost) {
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000975 // Little-endian host - the source is ordered from LSB to MSB. Order the
976 // destination from LSB to MSB: Do a straight copy.
977 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000978 } else {
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000979 // Big-endian host - the source is an array of 64 bit words ordered from
980 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
981 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
982 while (StoreBytes > sizeof(uint64_t)) {
983 StoreBytes -= sizeof(uint64_t);
984 // May not be aligned so use memcpy.
985 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
986 Src += sizeof(uint64_t);
987 }
988
989 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
990 }
991}
992
Evan Cheng89687e32008-11-04 06:10:31 +0000993void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000994 GenericValue *Ptr, Type *Ty) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000995 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000996
Reid Spencer8fb0f192007-03-06 03:04:04 +0000997 switch (Ty->getTypeID()) {
Nadav Rotem953783e2013-04-01 15:53:30 +0000998 default:
999 dbgs() << "Cannot store value of type " << *Ty << "!\n";
1000 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001001 case Type::IntegerTyID:
1002 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer8fb0f192007-03-06 03:04:04 +00001003 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +00001004 case Type::FloatTyID:
1005 *((float*)Ptr) = Val.FloatVal;
1006 break;
1007 case Type::DoubleTyID:
1008 *((double*)Ptr) = Val.DoubleVal;
1009 break;
Dale Johannesen1f8c5642009-03-24 18:16:17 +00001010 case Type::X86_FP80TyID:
1011 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1012 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001013 case Type::PointerTyID:
1014 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1015 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth80d9e072011-04-28 08:37:18 +00001016 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001017
Reid Spencer8fb0f192007-03-06 03:04:04 +00001018 *((PointerTy*)Ptr) = Val.PointerVal;
1019 break;
Nadav Rotem953783e2013-04-01 15:53:30 +00001020 case Type::VectorTyID:
1021 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1022 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1023 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1024 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1025 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1026 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1027 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1028 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1029 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1030 }
1031 }
1032 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001033 }
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001034
Rafael Espindola21a01d12013-04-15 14:44:24 +00001035 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001036 // Host and target are different endian - reverse the stored bytes.
1037 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1038}
1039
1040/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1041/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1042static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1043 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greenec2680be2013-01-14 21:04:45 +00001044 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1045 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001046
Rafael Espindola21a01d12013-04-15 14:44:24 +00001047 if (sys::IsLittleEndianHost)
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001048 // Little-endian host - the destination must be ordered from LSB to MSB.
1049 // The source is ordered from LSB to MSB: Do a straight copy.
1050 memcpy(Dst, Src, LoadBytes);
1051 else {
1052 // Big-endian - the destination is an array of 64 bit words ordered from
1053 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1054 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1055 // a word.
1056 while (LoadBytes > sizeof(uint64_t)) {
1057 LoadBytes -= sizeof(uint64_t);
1058 // May not be aligned so use memcpy.
1059 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1060 Dst += sizeof(uint64_t);
1061 }
1062
1063 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1064 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001065}
1066
Misha Brukman4afac182003-10-10 17:45:12 +00001067/// FIXME: document
1068///
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001069void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sands08bfe262008-03-10 16:38:37 +00001070 GenericValue *Ptr,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001071 Type *Ty) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001072 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1eff7042007-12-10 17:43:13 +00001073
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001074 switch (Ty->getTypeID()) {
1075 case Type::IntegerTyID:
1076 // An APInt with all words initially zero.
1077 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1078 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1079 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +00001080 case Type::FloatTyID:
1081 Result.FloatVal = *((float*)Ptr);
1082 break;
1083 case Type::DoubleTyID:
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001084 Result.DoubleVal = *((double*)Ptr);
Reid Spencer8fb0f192007-03-06 03:04:04 +00001085 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001086 case Type::PointerTyID:
Reid Spencer8fb0f192007-03-06 03:04:04 +00001087 Result.PointerVal = *((PointerTy*)Ptr);
1088 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +00001089 case Type::X86_FP80TyID: {
1090 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands9e4635a2007-12-15 17:37:40 +00001091 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen1f8c5642009-03-24 18:16:17 +00001092 uint64_t y[2];
1093 memcpy(y, Ptr, 10);
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001094 Result.IntVal = APInt(80, y);
Dale Johannesen1abac0d2007-09-17 18:44:13 +00001095 break;
1096 }
Nadav Rotem953783e2013-04-01 15:53:30 +00001097 case Type::VectorTyID: {
1098 const VectorType *VT = cast<VectorType>(Ty);
1099 const Type *ElemT = VT->getElementType();
1100 const unsigned numElems = VT->getNumElements();
1101 if (ElemT->isFloatTy()) {
1102 Result.AggregateVal.resize(numElems);
1103 for (unsigned i = 0; i < numElems; ++i)
1104 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1105 }
1106 if (ElemT->isDoubleTy()) {
1107 Result.AggregateVal.resize(numElems);
1108 for (unsigned i = 0; i < numElems; ++i)
1109 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1110 }
1111 if (ElemT->isIntegerTy()) {
1112 GenericValue intZero;
1113 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1114 intZero.IntVal = APInt(elemBitWidth, 0);
1115 Result.AggregateVal.resize(numElems, intZero);
1116 for (unsigned i = 0; i < numElems; ++i)
1117 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1118 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1119 }
1120 break;
1121 }
Reid Spencer8fb0f192007-03-06 03:04:04 +00001122 default:
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001123 SmallString<256> Msg;
1124 raw_svector_ostream OS(Msg);
1125 OS << "Cannot load value of type " << *Ty << "!";
1126 report_fatal_error(OS.str());
Chris Lattnerf88b9a62003-05-08 16:52:16 +00001127 }
Chris Lattnerf88b9a62003-05-08 16:52:16 +00001128}
1129
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001130void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greeneae7c59a2010-01-05 01:27:39 +00001131 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesendd947ea2008-08-07 01:30:15 +00001132 DEBUG(Init->dump());
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001133 if (isa<UndefValue>(Init))
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001134 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001135
1136 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino7c2b7c72006-01-20 18:18:40 +00001137 unsigned ElementSize =
Micah Villmow3574eca2012-10-08 16:38:25 +00001138 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +00001139 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1140 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1141 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001142 }
1143
1144 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001145 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattnerb6e1dd72008-02-15 00:57:28 +00001146 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001147 }
1148
1149 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman638e3782008-05-20 03:20:09 +00001150 unsigned ElementSize =
Micah Villmow3574eca2012-10-08 16:38:25 +00001151 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman638e3782008-05-20 03:20:09 +00001152 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1153 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1154 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001155 }
1156
1157 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman638e3782008-05-20 03:20:09 +00001158 const StructLayout *SL =
Micah Villmow3574eca2012-10-08 16:38:25 +00001159 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman638e3782008-05-20 03:20:09 +00001160 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1161 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1162 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001163 }
1164
1165 if (const ConstantDataSequential *CDS =
1166 dyn_cast<ConstantDataSequential>(Init)) {
1167 // CDS is already laid out in host memory order.
1168 StringRef Data = CDS->getRawDataValues();
1169 memcpy(Addr, Data.data(), Data.size());
1170 return;
1171 }
1172
1173 if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001174 GenericValue Val = getConstantValue(Init);
1175 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1176 return;
1177 }
1178
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001179 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinc23197a2009-07-14 16:55:14 +00001180 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001181}
1182
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001183/// EmitGlobals - Emit all of the global variables to memory, storing their
1184/// addresses into GlobalAddress. This must make sure to copy the contents of
1185/// their initializers into the memory.
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001186void ExecutionEngine::emitGlobals() {
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001187 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +00001188 // to hold them. If there is more than one module, do a prepass over globals
1189 // to figure out how the different modules should link together.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001190 std::map<std::pair<std::string, Type*>,
Chris Lattnerfe854032006-08-16 01:24:12 +00001191 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001192
Chris Lattnerfe854032006-08-16 01:24:12 +00001193 if (Modules.size() != 1) {
1194 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001195 Module &M = *Modules[m];
Chris Lattnerfe854032006-08-16 01:24:12 +00001196 for (Module::const_global_iterator I = M.global_begin(),
1197 E = M.global_end(); I != E; ++I) {
1198 const GlobalValue *GV = I;
Rafael Espindolabb46f522009-01-15 20:18:42 +00001199 if (GV->hasLocalLinkage() || GV->isDeclaration() ||
Chris Lattnerfe854032006-08-16 01:24:12 +00001200 GV->hasAppendingLinkage() || !GV->hasName())
1201 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001202
1203 const GlobalValue *&GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001204 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1205
1206 // If this is the first time we've seen this global, it is the canonical
1207 // version.
1208 if (!GVEntry) {
1209 GVEntry = GV;
1210 continue;
1211 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001212
Chris Lattnerfe854032006-08-16 01:24:12 +00001213 // If the existing global is strong, never replace it.
Stephen Hines36b56882014-04-23 16:57:46 -07001214 if (GVEntry->hasExternalLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +00001215 continue;
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001216
Chris Lattnerfe854032006-08-16 01:24:12 +00001217 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesenaafce772008-05-14 20:12:51 +00001218 // symbol. FIXME is this right for common?
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001219 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +00001220 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +00001221 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001222 }
Chris Lattnerfe854032006-08-16 01:24:12 +00001223 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001224
Chris Lattnerfe854032006-08-16 01:24:12 +00001225 std::vector<const GlobalValue*> NonCanonicalGlobals;
1226 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001227 Module &M = *Modules[m];
Chris Lattnerfe854032006-08-16 01:24:12 +00001228 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1229 I != E; ++I) {
1230 // In the multi-module case, see what this global maps to.
1231 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001232 if (const GlobalValue *GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001233 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1234 // If something else is the canonical global, ignore this one.
1235 if (GVEntry != &*I) {
1236 NonCanonicalGlobals.push_back(I);
1237 continue;
1238 }
1239 }
1240 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001241
Reid Spencer5cbf9852007-01-30 20:08:39 +00001242 if (!I->isDeclaration()) {
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001243 addGlobalMapping(I, getMemoryForGV(I));
Chris Lattnerfe854032006-08-16 01:24:12 +00001244 } else {
1245 // External variable reference. Try to use the dynamic loader to
1246 // get a pointer to it.
1247 if (void *SymAddr =
Daniel Dunbarfbee5792009-07-21 08:54:24 +00001248 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
Chris Lattnerfe854032006-08-16 01:24:12 +00001249 addGlobalMapping(I, SymAddr);
1250 else {
Chris Lattner75361b62010-04-07 22:58:41 +00001251 report_fatal_error("Could not resolve external global address: "
Torok Edwin31e24662009-07-07 17:32:34 +00001252 +I->getName());
Chris Lattnerfe854032006-08-16 01:24:12 +00001253 }
1254 }
1255 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001256
Chris Lattnerfe854032006-08-16 01:24:12 +00001257 // If there are multiple modules, map the non-canonical globals to their
1258 // canonical location.
1259 if (!NonCanonicalGlobals.empty()) {
1260 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1261 const GlobalValue *GV = NonCanonicalGlobals[i];
1262 const GlobalValue *CGV =
1263 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1264 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1265 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesc8ed9022008-10-14 10:04:52 +00001266 addGlobalMapping(GV, Ptr);
Chris Lattnerfe854032006-08-16 01:24:12 +00001267 }
1268 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001269
1270 // Now that all of the globals are set up in memory, loop through them all
Reid Spencera54b7cb2007-01-12 07:05:14 +00001271 // and initialize their contents.
Chris Lattnerfe854032006-08-16 01:24:12 +00001272 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1273 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001274 if (!I->isDeclaration()) {
Chris Lattnerfe854032006-08-16 01:24:12 +00001275 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001276 if (const GlobalValue *GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001277 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1278 if (GVEntry != &*I) // Not the canonical variable.
1279 continue;
1280 }
1281 EmitGlobalVariable(I);
1282 }
1283 }
1284 }
Chris Lattner24b0a182003-12-20 02:45:37 +00001285}
1286
1287// EmitGlobalVariable - This method emits the specified global variable to the
1288// address specified in GlobalAddresses, or allocates new memory if it's not
1289// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +00001290void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +00001291 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +00001292
Chris Lattner24b0a182003-12-20 02:45:37 +00001293 if (GA == 0) {
1294 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001295 GA = getMemoryForGV(GV);
Andrew Kaylor48079e02013-11-15 17:52:54 +00001296
1297 // If we failed to allocate memory for this global, return.
1298 if (GA == 0) return;
1299
Chris Lattner55d86482003-12-31 20:21:04 +00001300 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +00001301 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001302
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001303 // Don't initialize if it's thread local, let the client do it.
1304 if (!GV->isThreadLocal())
1305 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001306
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001307 Type *ElTy = GV->getType()->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00001308 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattner813c8152005-01-08 20:13:19 +00001309 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +00001310 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001311}
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001312
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001313ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1314 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001315}
1316
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001317sys::Mutex *
1318ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001319 return &EES->EE.lock;
1320}
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001321
1322void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1323 const GlobalValue *Old) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001324 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1325 EES->GlobalAddressReverseMap.erase(OldVal);
1326}
1327
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001328void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1329 const GlobalValue *,
1330 const GlobalValue *) {
Craig Topper85814382012-02-07 05:05:23 +00001331 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1332 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001333}