blob: 95d5fcb18f263c87141777969c80c797369c1d8f [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"
Filip Pizlo13a3cf12013-05-14 19:29:00 +000017#include "llvm/ExecutionEngine/JITMemoryManager.h"
Daniel Dunbar48dd8752010-11-13 02:48:57 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner9f3ff922009-08-23 22:49:13 +000019#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Operator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/Support/Debug.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Support/DynamicLibrary.h"
Torok Edwin31e24662009-07-07 17:32:34 +000028#include "llvm/Support/ErrorHandling.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Support/Host.h"
Chris Lattnere7fd5532006-05-08 22:00:52 +000030#include "llvm/Support/MutexGuard.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/TargetRegistry.h"
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +000032#include "llvm/Support/ValueHandle.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000033#include "llvm/Support/raw_ostream.h"
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000034#include "llvm/Target/TargetMachine.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000035#include <cmath>
36#include <cstring>
Chris Lattnerc2ee9b92003-11-19 21:08:57 +000037using namespace llvm;
Chris Lattnerbd199fb2002-12-24 00:01:05 +000038
Chris Lattner36343732006-12-19 22:43:32 +000039STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
40STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattnerbd199fb2002-12-24 00:01:05 +000041
Jeffrey Yasskin46882612010-02-05 16:19:36 +000042ExecutionEngine *(*ExecutionEngine::JITCtor)(
43 Module *M,
44 std::string *ErrorStr,
45 JITMemoryManager *JMM,
Jeffrey Yasskin46882612010-02-05 16:19:36 +000046 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000047 TargetMachine *TM) = 0;
Daniel Dunbar6d135972010-11-17 16:06:37 +000048ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
49 Module *M,
50 std::string *ErrorStr,
Filip Pizlo13a3cf12013-05-14 19:29:00 +000051 RTDyldMemoryManager *MCJMM,
Daniel Dunbar6d135972010-11-17 16:06:37 +000052 bool GVsWithCode,
Dylan Noblesmithc5b28582011-05-13 21:51:29 +000053 TargetMachine *TM) = 0;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000054ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
Reid Kleckner4b1511b2009-07-18 00:42:18 +000055 std::string *ErrorStr) = 0;
Chris Lattner2fe4bb02006-03-22 06:07:50 +000056
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000057ExecutionEngine::ExecutionEngine(Module *M)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +000058 : EEState(*this),
Duncan Sandsb35fd442010-10-21 08:57:29 +000059 LazyFunctionCreator(0),
Daniel Dunbar48dd8752010-11-13 02:48:57 +000060 ExceptionTableRegister(0),
Duncan Sandsb35fd442010-10-21 08:57:29 +000061 ExceptionTableDeregister(0) {
Jeffrey Yasskindc857242009-10-27 20:30:28 +000062 CompilingLazily = false;
Evan Cheng446531e2008-09-24 16:25:55 +000063 GVCompilationDisabled = false;
Evan Cheng1b088f32008-06-17 16:49:02 +000064 SymbolSearchingDisabled = false;
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +000065 Modules.push_back(M);
66 assert(M && "Module is null?");
Misha Brukman19684162003-10-16 21:18:05 +000067}
68
Brian Gaeke8e539482003-09-04 22:57:27 +000069ExecutionEngine::~ExecutionEngine() {
Reid Spencerd4c0e622007-03-03 18:19:18 +000070 clearAllGlobalMappings();
Chris Lattnerfe854032006-08-16 01:24:12 +000071 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
72 delete Modules[i];
Brian Gaeke8e539482003-09-04 22:57:27 +000073}
74
Duncan Sandsb35fd442010-10-21 08:57:29 +000075void ExecutionEngine::DeregisterAllTables() {
76 if (ExceptionTableDeregister) {
Eric Christopher515c67e2011-03-04 23:37:39 +000077 DenseMap<const Function*, void*>::iterator it = AllExceptionTables.begin();
78 DenseMap<const Function*, void*>::iterator ite = AllExceptionTables.end();
79 for (; it != ite; ++it)
80 ExceptionTableDeregister(it->second);
Duncan Sandsb35fd442010-10-21 08:57:29 +000081 AllExceptionTables.clear();
82 }
83}
84
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000085namespace {
Daniel Dunbar48dd8752010-11-13 02:48:57 +000086/// \brief Helper class which uses a value handler to automatically deletes the
87/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000088class GVMemoryBlock : public CallbackVH {
89 GVMemoryBlock(const GlobalVariable *GV)
90 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
91
92public:
Daniel Dunbar48dd8752010-11-13 02:48:57 +000093 /// \brief Returns the address the GlobalVariable should be written into. The
94 /// GVMemoryBlock object prefixes that.
Micah Villmow3574eca2012-10-08 16:38:25 +000095 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +000096 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskin47b71122010-03-27 04:53:56 +000097 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
98 void *RawMemory = ::operator new(
Micah Villmow3574eca2012-10-08 16:38:25 +000099 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskin47b71122010-03-27 04:53:56 +0000100 TD.getPreferredAlignment(GV))
101 + GVSize);
102 new(RawMemory) GVMemoryBlock(GV);
103 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
104 }
105
106 virtual void deleted() {
107 // We allocated with operator new and with some extra memory hanging off the
108 // end, so don't just delete this. I'm not sure if this is actually
109 // required.
110 this->~GVMemoryBlock();
111 ::operator delete(this);
112 }
113};
114} // anonymous namespace
115
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000116char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmow3574eca2012-10-08 16:38:25 +0000117 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray46fa1392008-10-25 15:41:43 +0000118}
119
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000120bool ExecutionEngine::removeModule(Module *M) {
Craig Topper6227d5c2013-07-04 01:31:24 +0000121 for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
Devang Patel73d0e212007-10-15 19:56:32 +0000122 E = Modules.end(); I != E; ++I) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000123 Module *Found = *I;
124 if (Found == M) {
Devang Patel73d0e212007-10-15 19:56:32 +0000125 Modules.erase(I);
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000126 clearGlobalMappingsFromModule(M);
127 return true;
Devang Patel73d0e212007-10-15 19:56:32 +0000128 }
129 }
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000130 return false;
Nate Begeman60789e42009-01-23 19:27:28 +0000131}
132
Chris Lattnerfe854032006-08-16 01:24:12 +0000133Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
134 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000135 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattnerfe854032006-08-16 01:24:12 +0000136 return F;
137 }
138 return 0;
139}
140
141
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000142void *ExecutionEngineState::RemoveMapping(const MutexGuard &,
143 const GlobalValue *ToUnmap) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000144 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000145 void *OldVal;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000146
147 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
148 // GlobalAddressMap.
Jeffrey Yasskinc89d27a2009-10-09 22:10:27 +0000149 if (I == GlobalAddressMap.end())
150 OldVal = 0;
151 else {
152 OldVal = I->second;
153 GlobalAddressMap.erase(I);
154 }
155
156 GlobalAddressReverseMap.erase(OldVal);
157 return OldVal;
158}
159
Chris Lattnere7fd5532006-05-08 22:00:52 +0000160void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
161 MutexGuard locked(lock);
Evan Chengbc4707a2008-09-18 07:54:21 +0000162
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000163 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar93b67e42009-07-26 07:49:05 +0000164 << "\' to [" << Addr << "]\n";);
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000165 void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000166 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
167 CurVal = Addr;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000168
169 // If we are using the reverse mapping, add it too.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000170 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000171 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000172 EEState.getGlobalAddressReverseMap(locked)[Addr];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000173 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
174 V = GV;
175 }
176}
177
Chris Lattnere7fd5532006-05-08 22:00:52 +0000178void ExecutionEngine::clearAllGlobalMappings() {
179 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000180
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000181 EEState.getGlobalAddressMap(locked).clear();
182 EEState.getGlobalAddressReverseMap(locked).clear();
Chris Lattnere7fd5532006-05-08 22:00:52 +0000183}
184
Nate Begemanf049e072008-05-21 16:34:48 +0000185void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
186 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000187
188 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000189 EEState.RemoveMapping(locked, FI);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000190 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
191 GI != GE; ++GI)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000192 EEState.RemoveMapping(locked, GI);
Nate Begemanf049e072008-05-21 16:34:48 +0000193}
194
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000195void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Chris Lattnere7fd5532006-05-08 22:00:52 +0000196 MutexGuard locked(lock);
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000197
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000198 ExecutionEngineState::GlobalAddressMapTy &Map =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000199 EEState.getGlobalAddressMap(locked);
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000200
Chris Lattnere7fd5532006-05-08 22:00:52 +0000201 // Deleting from the mapping?
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000202 if (Addr == 0)
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000203 return EEState.RemoveMapping(locked, GV);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000204
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000205 void *&CurVal = Map[GV];
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000206 void *OldVal = CurVal;
207
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000208 if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
209 EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
Chris Lattnere7fd5532006-05-08 22:00:52 +0000210 CurVal = Addr;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000211
212 // If we are using the reverse mapping, add it too.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000213 if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000214 AssertingVH<const GlobalValue> &V =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000215 EEState.getGlobalAddressReverseMap(locked)[Addr];
Chris Lattnere7fd5532006-05-08 22:00:52 +0000216 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
217 V = GV;
218 }
Chris Lattnerf4cc3092008-04-04 04:47:41 +0000219 return OldVal;
Chris Lattnere7fd5532006-05-08 22:00:52 +0000220}
221
Chris Lattnere7fd5532006-05-08 22:00:52 +0000222void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
223 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000224
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000225 ExecutionEngineState::GlobalAddressMapTy::iterator I =
226 EEState.getGlobalAddressMap(locked).find(GV);
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000227 return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
Chris Lattnere7fd5532006-05-08 22:00:52 +0000228}
229
Chris Lattner55d86482003-12-31 20:21:04 +0000230const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Reid Spenceree448632005-07-12 15:51:55 +0000231 MutexGuard locked(lock);
232
Chris Lattner55d86482003-12-31 20:21:04 +0000233 // If we haven't computed the reverse mapping yet, do so first.
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000234 if (EEState.getGlobalAddressReverseMap(locked).empty()) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000235 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000236 I = EEState.getGlobalAddressMap(locked).begin(),
237 E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
Daniel Dunbarab19da42010-11-13 00:55:42 +0000238 EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
239 I->second, I->first));
Chris Lattner55d86482003-12-31 20:21:04 +0000240 }
241
Jeffrey Yasskin0d5bd592009-08-07 19:54:29 +0000242 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +0000243 EEState.getGlobalAddressReverseMap(locked).find(Addr);
244 return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
Chris Lattner55d86482003-12-31 20:21:04 +0000245}
Chris Lattner87f03102003-12-26 06:50:30 +0000246
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000247namespace {
248class ArgvArray {
249 char *Array;
250 std::vector<char*> Values;
251public:
252 ArgvArray() : Array(NULL) {}
253 ~ArgvArray() { clear(); }
254 void clear() {
255 delete[] Array;
256 Array = NULL;
257 for (size_t I = 0, E = Values.size(); I != E; ++I) {
258 delete[] Values[I];
259 }
260 Values.clear();
261 }
262 /// Turn a vector of strings into a nice argv style array of pointers to null
263 /// terminated strings.
264 void *reset(LLVMContext &C, ExecutionEngine *EE,
265 const std::vector<std::string> &InputArgv);
266};
267} // anonymous namespace
268void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
269 const std::vector<std::string> &InputArgv) {
270 clear(); // Free the old contents.
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000271 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000272 Array = new char[(InputArgv.size()+1)*PtrSize];
Chris Lattner87f03102003-12-26 06:50:30 +0000273
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000274 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000275 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner87f03102003-12-26 06:50:30 +0000276
277 for (unsigned i = 0; i != InputArgv.size(); ++i) {
278 unsigned Size = InputArgv[i].size()+1;
279 char *Dest = new char[Size];
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000280 Values.push_back(Dest);
David Greeneae7c59a2010-01-05 01:27:39 +0000281 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000282
Chris Lattner87f03102003-12-26 06:50:30 +0000283 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
284 Dest[Size-1] = 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000285
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000286 // Endian safe: Array[i] = (PointerTy)Dest;
287 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
Chris Lattner87f03102003-12-26 06:50:30 +0000288 SBytePtr);
289 }
290
291 // Null terminate it
292 EE->StoreValueToMemory(PTOGV(0),
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000293 (GenericValue*)(Array+InputArgv.size()*PtrSize),
Chris Lattner87f03102003-12-26 06:50:30 +0000294 SBytePtr);
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000295 return Array;
Chris Lattner87f03102003-12-26 06:50:30 +0000296}
297
Chris Lattnerfbd39762009-09-23 01:46:04 +0000298void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
299 bool isDtors) {
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000300 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000301 GlobalVariable *GV = module->getNamedGlobal(Name);
Evan Cheng18314dc2008-09-30 15:51:21 +0000302
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000303 // If this global has internal linkage, or if it has a use, then it must be
304 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
305 // this is the case, don't execute any of the global ctors, __main will do
306 // it.
307 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
308
Nick Lewyckya040d472011-04-06 20:38:44 +0000309 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000310 // the init priority, which we ignore.
Chris Lattner1ee0ecf2012-01-24 13:41:11 +0000311 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
312 if (InitList == 0)
Nick Lewycky5ea5c612011-04-11 22:11:20 +0000313 return;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000314 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner1ee0ecf2012-01-24 13:41:11 +0000315 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
316 if (CS == 0) continue;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000317
318 Constant *FP = CS->getOperand(1);
319 if (FP->isNullValue())
Nick Lewycky5ea5c612011-04-11 22:11:20 +0000320 continue; // Found a sentinal value, ignore.
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000321
322 // Strip off constant expression casts.
323 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
324 if (CE->isCast())
325 FP = CE->getOperand(0);
326
327 // Execute the ctor/dtor function!
328 if (Function *F = dyn_cast<Function>(FP))
329 runFunction(F, std::vector<GenericValue>());
330
331 // FIXME: It is marginally lame that we just do nothing here if we see an
332 // entry we don't recognize. It might not be unreasonable for the verifier
333 // to not even allow this and just assert here.
334 }
Evan Cheng18314dc2008-09-30 15:51:21 +0000335}
336
Evan Cheng18314dc2008-09-30 15:51:21 +0000337void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
338 // Execute global ctors/dtors for each module in the program.
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000339 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
340 runStaticConstructorsDestructors(Modules[i], isDtors);
Chris Lattner9ca6cda2006-03-08 18:42:46 +0000341}
342
Dan Gohmanb6e3d6c2008-08-26 01:38:29 +0000343#ifndef NDEBUG
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000344/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
345static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000346 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000347 for (unsigned i = 0; i < PtrSize; ++i)
348 if (*(i + (uint8_t*)Loc))
349 return false;
350 return true;
351}
Dan Gohmanb6e3d6c2008-08-26 01:38:29 +0000352#endif
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000353
Chris Lattner87f03102003-12-26 06:50:30 +0000354int ExecutionEngine::runFunctionAsMain(Function *Fn,
355 const std::vector<std::string> &argv,
356 const char * const * envp) {
357 std::vector<GenericValue> GVArgs;
358 GenericValue GVArgc;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000359 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov499d8f02007-06-03 19:17:35 +0000360
361 // Check main() type
Chris Lattnerf24d0992004-08-16 01:05:35 +0000362 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000363 FunctionType *FTy = Fn->getFunctionType();
364 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000365
366 // Check the argument types.
367 if (NumArgs > 3)
368 report_fatal_error("Invalid number of arguments of main() supplied");
369 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
370 report_fatal_error("Invalid type for third argument of main() supplied");
371 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
372 report_fatal_error("Invalid type for second argument of main() supplied");
373 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
374 report_fatal_error("Invalid type for first argument of main() supplied");
375 if (!FTy->getReturnType()->isIntegerTy() &&
376 !FTy->getReturnType()->isVoidTy())
377 report_fatal_error("Invalid return type of main() supplied");
378
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000379 ArgvArray CArgv;
380 ArgvArray CEnv;
Chris Lattnerf24d0992004-08-16 01:05:35 +0000381 if (NumArgs) {
382 GVArgs.push_back(GVArgc); // Arg #0 = argc.
383 if (NumArgs > 1) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000384 // Arg #1 = argv.
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000385 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000386 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerf24d0992004-08-16 01:05:35 +0000387 "argv[0] was null after CreateArgv");
388 if (NumArgs > 2) {
389 std::vector<std::string> EnvVars;
390 for (unsigned i = 0; envp[i]; ++i)
391 EnvVars.push_back(envp[i]);
Owen Anderson1d0be152009-08-13 21:58:54 +0000392 // Arg #2 = envp.
Jeffrey Yasskinb1938382010-03-26 00:59:12 +0000393 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerf24d0992004-08-16 01:05:35 +0000394 }
395 }
396 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000397
Reid Spencer8fb0f192007-03-06 03:04:04 +0000398 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner87f03102003-12-26 06:50:30 +0000399}
400
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000401ExecutionEngine *ExecutionEngine::create(Module *M,
Reid Spencerd4c0e622007-03-03 18:19:18 +0000402 bool ForceInterpreter,
Evan Cheng502f20b2008-08-08 08:11:34 +0000403 std::string *ErrorStr,
Jeffrey Yasskin489393d2009-07-08 21:59:57 +0000404 CodeGenOpt::Level OptLevel,
405 bool GVsWithCode) {
Owen Anderson8e1fc562012-03-23 17:40:56 +0000406 EngineBuilder EB = EngineBuilder(M)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000407 .setEngineKind(ForceInterpreter
408 ? EngineKind::Interpreter
409 : EngineKind::JIT)
410 .setErrorStr(ErrorStr)
411 .setOptLevel(OptLevel)
Owen Anderson8e1fc562012-03-23 17:40:56 +0000412 .setAllocateGVsWithCode(GVsWithCode);
413
414 return EB.create();
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000415}
Brian Gaeke82d82772003-09-03 20:34:19 +0000416
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000417/// createJIT - This is the factory method for creating a JIT for the current
418/// machine, it does not fall back to the interpreter. This takes ownership
419/// of the module.
420ExecutionEngine *ExecutionEngine::createJIT(Module *M,
421 std::string *ErrorStr,
422 JITMemoryManager *JMM,
Dylan Noblesmithd95e67d2011-12-01 21:49:21 +0000423 CodeGenOpt::Level OL,
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000424 bool GVsWithCode,
Evan Cheng43966132011-07-19 06:37:02 +0000425 Reloc::Model RM,
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000426 CodeModel::Model CMM) {
427 if (ExecutionEngine::JITCtor == 0) {
428 if (ErrorStr)
429 *ErrorStr = "JIT has not been linked in.";
430 return 0;
431 }
432
433 // Use the defaults for extra parameters. Users can use EngineBuilder to
434 // set them.
Owen Anderson8e1fc562012-03-23 17:40:56 +0000435 EngineBuilder EB(M);
436 EB.setEngineKind(EngineKind::JIT);
437 EB.setErrorStr(ErrorStr);
438 EB.setRelocationModel(RM);
439 EB.setCodeModel(CMM);
440 EB.setAllocateGVsWithCode(GVsWithCode);
441 EB.setOptLevel(OL);
442 EB.setJITMemoryManager(JMM);
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000443
Peter Collingbourned40e1032011-12-07 23:58:57 +0000444 // TODO: permit custom TargetOptions here
Owen Anderson8e1fc562012-03-23 17:40:56 +0000445 TargetMachine *TM = EB.selectTarget();
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000446 if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000447
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000448 return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
Dylan Noblesmithf5895e92011-05-13 22:02:53 +0000449}
450
Owen Anderson8e1fc562012-03-23 17:40:56 +0000451ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Benjamin Kramer0f554492012-04-08 14:53:14 +0000452 OwningPtr<TargetMachine> TheTM(TM); // Take ownership.
453
Nick Lewycky6456d862008-03-08 02:49:45 +0000454 // Make sure we can resolve symbols in the program as well. The zero arg
455 // to the function tells DynamicLibrary to load the program, not a library.
456 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
457 return 0;
458
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000459 assert(!(JMM && MCJMM));
460
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000461 // If the user specified a memory manager but didn't specify which engine to
462 // create, we assume they only want the JIT, and we fail if they only want
463 // the interpreter.
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000464 if (JMM || MCJMM) {
Chris Lattnerfbd39762009-09-23 01:46:04 +0000465 if (WhichEngine & EngineKind::JIT)
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000466 WhichEngine = EngineKind::JIT;
Chris Lattnerfbd39762009-09-23 01:46:04 +0000467 else {
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000468 if (ErrorStr)
469 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Chris Lattnerfbd39762009-09-23 01:46:04 +0000470 return 0;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000471 }
472 }
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000473
474 if (MCJMM && ! UseMCJIT) {
475 if (ErrorStr)
476 *ErrorStr =
477 "Cannot create a legacy JIT with a runtime dyld memory "
478 "manager.";
479 return 0;
480 }
Brian Gaeke82d82772003-09-03 20:34:19 +0000481
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000482 // Unless the interpreter was explicitly selected or the JIT is not linked,
483 // try making a JIT.
Benjamin Kramer0f554492012-04-08 14:53:14 +0000484 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000485 Triple TT(M->getTargetTriple());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000486 if (!TM->getTarget().hasJIT()) {
487 errs() << "WARNING: This target JIT is not designed for the host"
488 << " you are running. If bad things happen, please choose"
489 << " a different -march switch.\n";
490 }
Dylan Noblesmith9ea47172011-12-12 04:20:36 +0000491
Owen Anderson8e1fc562012-03-23 17:40:56 +0000492 if (UseMCJIT && ExecutionEngine::MCJITCtor) {
493 ExecutionEngine *EE =
Filip Pizlo13a3cf12013-05-14 19:29:00 +0000494 ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
Benjamin Kramer0f554492012-04-08 14:53:14 +0000495 AllocateGVsWithCode, TheTM.take());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000496 if (EE) return EE;
497 } else if (ExecutionEngine::JITCtor) {
498 ExecutionEngine *EE =
499 ExecutionEngine::JITCtor(M, ErrorStr, JMM,
Benjamin Kramer0f554492012-04-08 14:53:14 +0000500 AllocateGVsWithCode, TheTM.take());
Owen Anderson8e1fc562012-03-23 17:40:56 +0000501 if (EE) return EE;
Chris Lattnerfbd39762009-09-23 01:46:04 +0000502 }
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000503 }
504
505 // If we can't make a JIT and we didn't request one specifically, try making
506 // an interpreter instead.
Chris Lattnerfbd39762009-09-23 01:46:04 +0000507 if (WhichEngine & EngineKind::Interpreter) {
508 if (ExecutionEngine::InterpCtor)
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000509 return ExecutionEngine::InterpCtor(M, ErrorStr);
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000510 if (ErrorStr)
511 *ErrorStr = "Interpreter has not been linked in.";
Chris Lattnerfbd39762009-09-23 01:46:04 +0000512 return 0;
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000513 }
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000514
Jim Grosbach8005bcd2012-08-21 15:42:49 +0000515 if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0 &&
516 ExecutionEngine::MCJITCtor == 0) {
Chris Lattnerc72efbe2009-09-23 02:03:49 +0000517 if (ErrorStr)
518 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000519 }
520
Chris Lattnerfbd39762009-09-23 01:46:04 +0000521 return 0;
Brian Gaeke82d82772003-09-03 20:34:19 +0000522}
523
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000524void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke37df4602003-08-13 18:16:14 +0000525 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000526 return getPointerToFunction(F);
527
Reid Spenceree448632005-07-12 15:51:55 +0000528 MutexGuard locked(lock);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000529 if (void *P = EEState.getGlobalAddressMap(locked)[GV])
530 return P;
Jeff Cohen68835dd2006-02-07 05:11:57 +0000531
532 // Global variable might have been added since interpreter started.
533 if (GlobalVariable *GVar =
534 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
535 EmitGlobalVariable(GVar);
536 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000537 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000538
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +0000539 return EEState.getGlobalAddressMap(locked)[GV];
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000540}
541
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000542/// \brief Converts a Constant* into a GenericValue, including handling of
543/// ConstantExpr values.
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000544GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer3da59db2006-11-27 01:05:10 +0000545 // If its undefined, return the garbage.
Jay Foad5b370122010-01-15 08:32:58 +0000546 if (isa<UndefValue>(C)) {
547 GenericValue Result;
548 switch (C->getType()->getTypeID()) {
Nadav Rotem953783e2013-04-01 15:53:30 +0000549 default:
550 break;
Jay Foad5b370122010-01-15 08:32:58 +0000551 case Type::IntegerTyID:
552 case Type::X86_FP80TyID:
553 case Type::FP128TyID:
554 case Type::PPC_FP128TyID:
555 // Although the value is undefined, we still have to construct an APInt
556 // with the correct bit width.
557 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
558 break;
Elena Demikhovsky3c5ce292013-09-12 10:48:23 +0000559 case Type::StructTyID: {
560 // if the whole struct is 'undef' just reserve memory for the value.
561 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
562 unsigned int elemNum = STy->getNumElements();
563 Result.AggregateVal.resize(elemNum);
564 for (unsigned int i = 0; i < elemNum; ++i) {
565 Type *ElemTy = STy->getElementType(i);
566 if (ElemTy->isIntegerTy())
567 Result.AggregateVal[i].IntVal =
568 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
569 else if (ElemTy->isAggregateType()) {
570 const Constant *ElemUndef = UndefValue::get(ElemTy);
571 Result.AggregateVal[i] = getConstantValue(ElemUndef);
572 }
573 }
574 }
575 }
576 break;
Nadav Rotem953783e2013-04-01 15:53:30 +0000577 case Type::VectorTyID:
578 // if the whole vector is 'undef' just reserve memory for the value.
579 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
580 const Type *ElemTy = VTy->getElementType();
581 unsigned int elemNum = VTy->getNumElements();
582 Result.AggregateVal.resize(elemNum);
583 if (ElemTy->isIntegerTy())
584 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky3c5ce292013-09-12 10:48:23 +0000585 Result.AggregateVal[i].IntVal =
Nadav Rotem953783e2013-04-01 15:53:30 +0000586 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foad5b370122010-01-15 08:32:58 +0000587 break;
588 }
589 return Result;
590 }
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000591
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000592 // Otherwise, if the value is a ConstantExpr...
Reid Spencer3da59db2006-11-27 01:05:10 +0000593 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencerbce30f12007-03-06 22:23:15 +0000594 Constant *Op0 = CE->getOperand(0);
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000595 switch (CE->getOpcode()) {
596 case Instruction::GetElementPtr: {
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000597 // Compute the index
Reid Spencerbce30f12007-03-06 22:23:15 +0000598 GenericValue Result = getConstantValue(Op0);
Nuno Lopes98281a22012-12-30 16:25:48 +0000599 APInt Offset(TD->getPointerSizeInBits(), 0);
600 cast<GEPOperator>(CE)->accumulateConstantOffset(*TD, Offset);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000601
Reid Spencer8fb0f192007-03-06 03:04:04 +0000602 char* tmp = (char*) Result.PointerVal;
Nuno Lopes98281a22012-12-30 16:25:48 +0000603 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000604 return Result;
605 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000606 case Instruction::Trunc: {
607 GenericValue GV = getConstantValue(Op0);
608 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
609 GV.IntVal = GV.IntVal.trunc(BitWidth);
610 return GV;
611 }
612 case Instruction::ZExt: {
613 GenericValue GV = getConstantValue(Op0);
614 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
615 GV.IntVal = GV.IntVal.zext(BitWidth);
616 return GV;
617 }
618 case Instruction::SExt: {
619 GenericValue GV = getConstantValue(Op0);
620 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
621 GV.IntVal = GV.IntVal.sext(BitWidth);
622 return GV;
623 }
624 case Instruction::FPTrunc: {
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.FloatVal = float(GV.DoubleVal);
628 return GV;
629 }
630 case Instruction::FPExt:{
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000631 // FIXME long double
Reid Spencerbce30f12007-03-06 22:23:15 +0000632 GenericValue GV = getConstantValue(Op0);
633 GV.DoubleVal = double(GV.FloatVal);
634 return GV;
635 }
636 case Instruction::UIToFP: {
637 GenericValue GV = getConstantValue(Op0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000638 if (CE->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000639 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000640 else if (CE->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000641 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000642 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +0000643 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000644 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohman62824062008-02-29 01:27:13 +0000645 false,
646 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000647 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000648 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000649 return GV;
650 }
651 case Instruction::SIToFP: {
652 GenericValue GV = getConstantValue(Op0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000653 if (CE->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000654 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000655 else if (CE->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000656 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000657 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer3069cbf2010-12-04 15:28:22 +0000658 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000659 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohman62824062008-02-29 01:27:13 +0000660 true,
661 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000662 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000663 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000664 return GV;
665 }
666 case Instruction::FPToUI: // double->APInt conversion handles sign
667 case Instruction::FPToSI: {
668 GenericValue GV = getConstantValue(Op0);
669 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000670 if (Op0->getType()->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000671 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000672 else if (Op0->getType()->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000673 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000674 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover0a29cb02013-01-22 09:46:31 +0000675 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000676 uint64_t v;
Dale Johannesen23a98552008-10-09 23:00:39 +0000677 bool ignored;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000678 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000679 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen23a98552008-10-09 23:00:39 +0000680 APFloat::rmTowardZero, &ignored);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000681 GV.IntVal = v; // endian?
682 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000683 return GV;
684 }
Reid Spencer3da59db2006-11-27 01:05:10 +0000685 case Instruction::PtrToInt: {
Reid Spencerbce30f12007-03-06 22:23:15 +0000686 GenericValue GV = getConstantValue(Op0);
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000687 uint32_t PtrWidth = TD->getTypeSizeInBits(Op0->getType());
688 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencerbce30f12007-03-06 22:23:15 +0000689 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000690 uint32_t IntWidth = TD->getTypeSizeInBits(CE->getType());
691 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencerbce30f12007-03-06 22:23:15 +0000692 return GV;
693 }
694 case Instruction::IntToPtr: {
695 GenericValue GV = getConstantValue(Op0);
Micah Villmowb52fb872012-10-24 18:36:13 +0000696 uint32_t PtrWidth = TD->getTypeSizeInBits(CE->getType());
Eli Friedmanbbc6e672012-10-30 22:21:55 +0000697 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencerbce30f12007-03-06 22:23:15 +0000698 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
699 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer3da59db2006-11-27 01:05:10 +0000700 return GV;
701 }
702 case Instruction::BitCast: {
Reid Spencerbce30f12007-03-06 22:23:15 +0000703 GenericValue GV = getConstantValue(Op0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000704 Type* DestTy = CE->getType();
Reid Spencerbce30f12007-03-06 22:23:15 +0000705 switch (Op0->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000706 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencerbce30f12007-03-06 22:23:15 +0000707 case Type::IntegerTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000708 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000709 if (DestTy->isFloatTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000710 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000711 else if (DestTy->isDoubleTy())
Reid Spencerbce30f12007-03-06 22:23:15 +0000712 GV.DoubleVal = GV.IntVal.bitsToDouble();
713 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000714 case Type::FloatTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000715 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foade4d19c92010-11-28 21:04:48 +0000716 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencerbce30f12007-03-06 22:23:15 +0000717 break;
718 case Type::DoubleTyID:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000719 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foade4d19c92010-11-28 21:04:48 +0000720 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencerbce30f12007-03-06 22:23:15 +0000721 break;
722 case Type::PointerTyID:
Duncan Sands1df98592010-02-16 11:11:14 +0000723 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencerbce30f12007-03-06 22:23:15 +0000724 break; // getConstantValue(Op0) above already converted it
725 }
726 return GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +0000727 }
Chris Lattner9a231222003-05-14 17:51:49 +0000728 case Instruction::Add:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000729 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000730 case Instruction::Sub:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000731 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000732 case Instruction::Mul:
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000733 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000734 case Instruction::UDiv:
735 case Instruction::SDiv:
736 case Instruction::URem:
737 case Instruction::SRem:
738 case Instruction::And:
739 case Instruction::Or:
740 case Instruction::Xor: {
741 GenericValue LHS = getConstantValue(Op0);
742 GenericValue RHS = getConstantValue(CE->getOperand(1));
743 GenericValue GV;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000744 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000745 default: llvm_unreachable("Bad add type!");
Reid Spencera54b7cb2007-01-12 07:05:14 +0000746 case Type::IntegerTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000747 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000748 default: llvm_unreachable("Invalid integer opcode");
Reid Spencerbce30f12007-03-06 22:23:15 +0000749 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
750 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
751 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
752 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
753 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
754 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
755 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
756 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
757 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
758 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
759 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000760 break;
761 case Type::FloatTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000762 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000763 default: llvm_unreachable("Invalid float opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000764 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000765 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000766 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000767 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000768 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000769 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000770 case Instruction::FDiv:
Reid Spencerbce30f12007-03-06 22:23:15 +0000771 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000772 case Instruction::FRem:
Chris Lattner87565c12010-05-15 17:10:24 +0000773 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencerbce30f12007-03-06 22:23:15 +0000774 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000775 break;
776 case Type::DoubleTyID:
Reid Spencerbce30f12007-03-06 22:23:15 +0000777 switch (CE->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000778 default: llvm_unreachable("Invalid double opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000779 case Instruction::FAdd:
Reid Spencerbce30f12007-03-06 22:23:15 +0000780 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000781 case Instruction::FSub:
Reid Spencerbce30f12007-03-06 22:23:15 +0000782 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000783 case Instruction::FMul:
Reid Spencerbce30f12007-03-06 22:23:15 +0000784 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000785 case Instruction::FDiv:
Reid Spencerbce30f12007-03-06 22:23:15 +0000786 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000787 case Instruction::FRem:
Chris Lattner87565c12010-05-15 17:10:24 +0000788 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencerbce30f12007-03-06 22:23:15 +0000789 }
Chris Lattner5f90cb82004-07-11 08:01:11 +0000790 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000791 case Type::X86_FP80TyID:
792 case Type::PPC_FP128TyID:
793 case Type::FP128TyID: {
Tim Northover0a29cb02013-01-22 09:46:31 +0000794 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
795 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000796 switch (CE->getOpcode()) {
Daniel Dunbarab19da42010-11-13 00:55:42 +0000797 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000798 case Instruction::FAdd:
Tim Northover0a29cb02013-01-22 09:46:31 +0000799 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000800 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000801 break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000802 case Instruction::FSub:
Tim Northover0a29cb02013-01-22 09:46:31 +0000803 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
804 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000805 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000806 break;
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000807 case Instruction::FMul:
Tim Northover0a29cb02013-01-22 09:46:31 +0000808 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
809 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000810 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000811 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000812 case Instruction::FDiv:
Tim Northover0a29cb02013-01-22 09:46:31 +0000813 apfLHS.divide(APFloat(Sem, RHS.IntVal),
814 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000815 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000816 break;
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000817 case Instruction::FRem:
Tim Northover0a29cb02013-01-22 09:46:31 +0000818 apfLHS.mod(APFloat(Sem, RHS.IntVal),
819 APFloat::rmNearestTiesToEven);
Dale Johannesen7111b022008-10-09 18:53:47 +0000820 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000821 break;
822 }
823 }
824 break;
Chris Lattner5f90cb82004-07-11 08:01:11 +0000825 }
Reid Spencerbce30f12007-03-06 22:23:15 +0000826 return GV;
827 }
Chris Lattner9a231222003-05-14 17:51:49 +0000828 default:
829 break;
830 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000831
832 SmallString<256> Msg;
833 raw_svector_ostream OS(Msg);
834 OS << "ConstantExpr not handled: " << *CE;
835 report_fatal_error(OS.str());
Chris Lattner9a231222003-05-14 17:51:49 +0000836 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000837
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000838 // Otherwise, we have a simple constant.
Reid Spencerbce30f12007-03-06 22:23:15 +0000839 GenericValue Result;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000840 switch (C->getType()->getTypeID()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000841 case Type::FloatTyID:
842 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencera54b7cb2007-01-12 07:05:14 +0000843 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000844 case Type::DoubleTyID:
Dale Johannesen43421b32007-09-06 18:13:44 +0000845 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer8fb0f192007-03-06 03:04:04 +0000846 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000847 case Type::X86_FP80TyID:
848 case Type::FP128TyID:
849 case Type::PPC_FP128TyID:
Dale Johannesen7111b022008-10-09 18:53:47 +0000850 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesen1abac0d2007-09-17 18:44:13 +0000851 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +0000852 case Type::IntegerTyID:
853 Result.IntVal = cast<ConstantInt>(C)->getValue();
854 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000855 case Type::PointerTyID:
Reid Spencer40cf2f92004-07-18 00:41:27 +0000856 if (isa<ConstantPointerNull>(C))
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000857 Result.PointerVal = 0;
Reid Spencer40cf2f92004-07-18 00:41:27 +0000858 else if (const Function *F = dyn_cast<Function>(C))
859 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000860 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer40cf2f92004-07-18 00:41:27 +0000861 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Chris Lattnerf32a6a32009-10-29 05:26:09 +0000862 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
863 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
864 BA->getBasicBlock())));
Reid Spencer40cf2f92004-07-18 00:41:27 +0000865 else
Torok Edwinc23197a2009-07-14 16:55:14 +0000866 llvm_unreachable("Unknown constant pointer type!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000867 break;
Nadav Rotem953783e2013-04-01 15:53:30 +0000868 case Type::VectorTyID: {
869 unsigned elemNum;
870 Type* ElemTy;
871 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
872 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
873 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
874
875 if (CDV) {
876 elemNum = CDV->getNumElements();
877 ElemTy = CDV->getElementType();
878 } else if (CV || CAZ) {
879 VectorType* VTy = dyn_cast<VectorType>(C->getType());
880 elemNum = VTy->getNumElements();
881 ElemTy = VTy->getElementType();
882 } else {
883 llvm_unreachable("Unknown constant vector type!");
884 }
885
886 Result.AggregateVal.resize(elemNum);
887 // Check if vector holds floats.
888 if(ElemTy->isFloatTy()) {
889 if (CAZ) {
890 GenericValue floatZero;
891 floatZero.FloatVal = 0.f;
892 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
893 floatZero);
894 break;
895 }
896 if(CV) {
897 for (unsigned i = 0; i < elemNum; ++i)
898 if (!isa<UndefValue>(CV->getOperand(i)))
899 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
900 CV->getOperand(i))->getValueAPF().convertToFloat();
901 break;
902 }
903 if(CDV)
904 for (unsigned i = 0; i < elemNum; ++i)
905 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
906
907 break;
908 }
909 // Check if vector holds doubles.
910 if (ElemTy->isDoubleTy()) {
911 if (CAZ) {
912 GenericValue doubleZero;
913 doubleZero.DoubleVal = 0.0;
914 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
915 doubleZero);
916 break;
917 }
918 if(CV) {
919 for (unsigned i = 0; i < elemNum; ++i)
920 if (!isa<UndefValue>(CV->getOperand(i)))
921 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
922 CV->getOperand(i))->getValueAPF().convertToDouble();
923 break;
924 }
925 if(CDV)
926 for (unsigned i = 0; i < elemNum; ++i)
927 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
928
929 break;
930 }
931 // Check if vector holds integers.
932 if (ElemTy->isIntegerTy()) {
933 if (CAZ) {
934 GenericValue intZero;
935 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
936 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
937 intZero);
938 break;
939 }
940 if(CV) {
941 for (unsigned i = 0; i < elemNum; ++i)
942 if (!isa<UndefValue>(CV->getOperand(i)))
943 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
944 CV->getOperand(i))->getValue();
945 else {
946 Result.AggregateVal[i].IntVal =
947 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
948 }
949 break;
950 }
951 if(CDV)
952 for (unsigned i = 0; i < elemNum; ++i)
953 Result.AggregateVal[i].IntVal = APInt(
954 CDV->getElementType()->getPrimitiveSizeInBits(),
955 CDV->getElementAsInteger(i));
956
957 break;
958 }
959 llvm_unreachable("Unknown constant pointer type!");
960 }
961 break;
962
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000963 default:
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000964 SmallString<256> Msg;
965 raw_svector_ostream OS(Msg);
966 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
967 report_fatal_error(OS.str());
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000968 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000969
Chris Lattnerbd199fb2002-12-24 00:01:05 +0000970 return Result;
971}
972
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000973/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
974/// with the integer held in IntVal.
975static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
976 unsigned StoreBytes) {
977 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divacky59324292012-09-05 22:26:57 +0000978 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000979
Rafael Espindola21a01d12013-04-15 14:44:24 +0000980 if (sys::IsLittleEndianHost) {
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000981 // Little-endian host - the source is ordered from LSB to MSB. Order the
982 // destination from LSB to MSB: Do a straight copy.
983 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar48dd8752010-11-13 02:48:57 +0000984 } else {
Duncan Sands8a43e9e2007-12-14 19:38:31 +0000985 // Big-endian host - the source is an array of 64 bit words ordered from
986 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
987 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
988 while (StoreBytes > sizeof(uint64_t)) {
989 StoreBytes -= sizeof(uint64_t);
990 // May not be aligned so use memcpy.
991 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
992 Src += sizeof(uint64_t);
993 }
994
995 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
996 }
997}
998
Evan Cheng89687e32008-11-04 06:10:31 +0000999void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001000 GenericValue *Ptr, Type *Ty) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001001 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001002
Reid Spencer8fb0f192007-03-06 03:04:04 +00001003 switch (Ty->getTypeID()) {
Nadav Rotem953783e2013-04-01 15:53:30 +00001004 default:
1005 dbgs() << "Cannot store value of type " << *Ty << "!\n";
1006 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001007 case Type::IntegerTyID:
1008 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer8fb0f192007-03-06 03:04:04 +00001009 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +00001010 case Type::FloatTyID:
1011 *((float*)Ptr) = Val.FloatVal;
1012 break;
1013 case Type::DoubleTyID:
1014 *((double*)Ptr) = Val.DoubleVal;
1015 break;
Dale Johannesen1f8c5642009-03-24 18:16:17 +00001016 case Type::X86_FP80TyID:
1017 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1018 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001019 case Type::PointerTyID:
1020 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1021 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth80d9e072011-04-28 08:37:18 +00001022 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001023
Reid Spencer8fb0f192007-03-06 03:04:04 +00001024 *((PointerTy*)Ptr) = Val.PointerVal;
1025 break;
Nadav Rotem953783e2013-04-01 15:53:30 +00001026 case Type::VectorTyID:
1027 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1028 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1029 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1030 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1031 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1032 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1033 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1034 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1035 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1036 }
1037 }
1038 break;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001039 }
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001040
Rafael Espindola21a01d12013-04-15 14:44:24 +00001041 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001042 // Host and target are different endian - reverse the stored bytes.
1043 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1044}
1045
1046/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1047/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1048static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1049 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greenec2680be2013-01-14 21:04:45 +00001050 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1051 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001052
Rafael Espindola21a01d12013-04-15 14:44:24 +00001053 if (sys::IsLittleEndianHost)
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001054 // Little-endian host - the destination must be ordered from LSB to MSB.
1055 // The source is ordered from LSB to MSB: Do a straight copy.
1056 memcpy(Dst, Src, LoadBytes);
1057 else {
1058 // Big-endian - the destination is an array of 64 bit words ordered from
1059 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1060 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1061 // a word.
1062 while (LoadBytes > sizeof(uint64_t)) {
1063 LoadBytes -= sizeof(uint64_t);
1064 // May not be aligned so use memcpy.
1065 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1066 Dst += sizeof(uint64_t);
1067 }
1068
1069 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1070 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001071}
1072
Misha Brukman4afac182003-10-10 17:45:12 +00001073/// FIXME: document
1074///
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001075void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sands08bfe262008-03-10 16:38:37 +00001076 GenericValue *Ptr,
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001077 Type *Ty) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001078 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1eff7042007-12-10 17:43:13 +00001079
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001080 switch (Ty->getTypeID()) {
1081 case Type::IntegerTyID:
1082 // An APInt with all words initially zero.
1083 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1084 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1085 break;
Reid Spencer8fb0f192007-03-06 03:04:04 +00001086 case Type::FloatTyID:
1087 Result.FloatVal = *((float*)Ptr);
1088 break;
1089 case Type::DoubleTyID:
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001090 Result.DoubleVal = *((double*)Ptr);
Reid Spencer8fb0f192007-03-06 03:04:04 +00001091 break;
Duncan Sands8a43e9e2007-12-14 19:38:31 +00001092 case Type::PointerTyID:
Reid Spencer8fb0f192007-03-06 03:04:04 +00001093 Result.PointerVal = *((PointerTy*)Ptr);
1094 break;
Dale Johannesen1abac0d2007-09-17 18:44:13 +00001095 case Type::X86_FP80TyID: {
1096 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands9e4635a2007-12-15 17:37:40 +00001097 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen1f8c5642009-03-24 18:16:17 +00001098 uint64_t y[2];
1099 memcpy(y, Ptr, 10);
Jeffrey Yasskin3ba292d2011-07-18 21:45:40 +00001100 Result.IntVal = APInt(80, y);
Dale Johannesen1abac0d2007-09-17 18:44:13 +00001101 break;
1102 }
Nadav Rotem953783e2013-04-01 15:53:30 +00001103 case Type::VectorTyID: {
1104 const VectorType *VT = cast<VectorType>(Ty);
1105 const Type *ElemT = VT->getElementType();
1106 const unsigned numElems = VT->getNumElements();
1107 if (ElemT->isFloatTy()) {
1108 Result.AggregateVal.resize(numElems);
1109 for (unsigned i = 0; i < numElems; ++i)
1110 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1111 }
1112 if (ElemT->isDoubleTy()) {
1113 Result.AggregateVal.resize(numElems);
1114 for (unsigned i = 0; i < numElems; ++i)
1115 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1116 }
1117 if (ElemT->isIntegerTy()) {
1118 GenericValue intZero;
1119 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1120 intZero.IntVal = APInt(elemBitWidth, 0);
1121 Result.AggregateVal.resize(numElems, intZero);
1122 for (unsigned i = 0; i < numElems; ++i)
1123 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1124 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1125 }
1126 break;
1127 }
Reid Spencer8fb0f192007-03-06 03:04:04 +00001128 default:
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001129 SmallString<256> Msg;
1130 raw_svector_ostream OS(Msg);
1131 OS << "Cannot load value of type " << *Ty << "!";
1132 report_fatal_error(OS.str());
Chris Lattnerf88b9a62003-05-08 16:52:16 +00001133 }
Chris Lattnerf88b9a62003-05-08 16:52:16 +00001134}
1135
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001136void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greeneae7c59a2010-01-05 01:27:39 +00001137 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesendd947ea2008-08-07 01:30:15 +00001138 DEBUG(Init->dump());
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001139 if (isa<UndefValue>(Init))
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001140 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001141
1142 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino7c2b7c72006-01-20 18:18:40 +00001143 unsigned ElementSize =
Micah Villmow3574eca2012-10-08 16:38:25 +00001144 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino7c2b7c72006-01-20 18:18:40 +00001145 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1146 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1147 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001148 }
1149
1150 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmow3574eca2012-10-08 16:38:25 +00001151 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattnerb6e1dd72008-02-15 00:57:28 +00001152 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001153 }
1154
1155 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman638e3782008-05-20 03:20:09 +00001156 unsigned ElementSize =
Micah Villmow3574eca2012-10-08 16:38:25 +00001157 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman638e3782008-05-20 03:20:09 +00001158 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1159 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1160 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001161 }
1162
1163 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman638e3782008-05-20 03:20:09 +00001164 const StructLayout *SL =
Micah Villmow3574eca2012-10-08 16:38:25 +00001165 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman638e3782008-05-20 03:20:09 +00001166 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1167 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1168 return;
Chris Lattner1ee0ecf2012-01-24 13:41:11 +00001169 }
1170
1171 if (const ConstantDataSequential *CDS =
1172 dyn_cast<ConstantDataSequential>(Init)) {
1173 // CDS is already laid out in host memory order.
1174 StringRef Data = CDS->getRawDataValues();
1175 memcpy(Addr, Data.data(), Data.size());
1176 return;
1177 }
1178
1179 if (Init->getType()->isFirstClassType()) {
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001180 GenericValue Val = getConstantValue(Init);
1181 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1182 return;
1183 }
1184
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001185 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinc23197a2009-07-14 16:55:14 +00001186 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001187}
1188
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001189/// EmitGlobals - Emit all of the global variables to memory, storing their
1190/// addresses into GlobalAddress. This must make sure to copy the contents of
1191/// their initializers into the memory.
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001192void ExecutionEngine::emitGlobals() {
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001193 // Loop over all of the global variables in the program, allocating the memory
Chris Lattnerfe854032006-08-16 01:24:12 +00001194 // to hold them. If there is more than one module, do a prepass over globals
1195 // to figure out how the different modules should link together.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001196 std::map<std::pair<std::string, Type*>,
Chris Lattnerfe854032006-08-16 01:24:12 +00001197 const GlobalValue*> LinkedGlobalsMap;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001198
Chris Lattnerfe854032006-08-16 01:24:12 +00001199 if (Modules.size() != 1) {
1200 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001201 Module &M = *Modules[m];
Chris Lattnerfe854032006-08-16 01:24:12 +00001202 for (Module::const_global_iterator I = M.global_begin(),
1203 E = M.global_end(); I != E; ++I) {
1204 const GlobalValue *GV = I;
Rafael Espindolabb46f522009-01-15 20:18:42 +00001205 if (GV->hasLocalLinkage() || GV->isDeclaration() ||
Chris Lattnerfe854032006-08-16 01:24:12 +00001206 GV->hasAppendingLinkage() || !GV->hasName())
1207 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001208
1209 const GlobalValue *&GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001210 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1211
1212 // If this is the first time we've seen this global, it is the canonical
1213 // version.
1214 if (!GVEntry) {
1215 GVEntry = GV;
1216 continue;
1217 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001218
Chris Lattnerfe854032006-08-16 01:24:12 +00001219 // If the existing global is strong, never replace it.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001220 if (GVEntry->hasExternalLinkage() ||
1221 GVEntry->hasDLLImportLinkage() ||
1222 GVEntry->hasDLLExportLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +00001223 continue;
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001224
Chris Lattnerfe854032006-08-16 01:24:12 +00001225 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesenaafce772008-05-14 20:12:51 +00001226 // symbol. FIXME is this right for common?
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +00001227 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
Chris Lattnerfe854032006-08-16 01:24:12 +00001228 GVEntry = GV;
Chris Lattnerd8c03bf2003-04-23 19:01:49 +00001229 }
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001230 }
Chris Lattnerfe854032006-08-16 01:24:12 +00001231 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001232
Chris Lattnerfe854032006-08-16 01:24:12 +00001233 std::vector<const GlobalValue*> NonCanonicalGlobals;
1234 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001235 Module &M = *Modules[m];
Chris Lattnerfe854032006-08-16 01:24:12 +00001236 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1237 I != E; ++I) {
1238 // In the multi-module case, see what this global maps to.
1239 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001240 if (const GlobalValue *GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001241 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1242 // If something else is the canonical global, ignore this one.
1243 if (GVEntry != &*I) {
1244 NonCanonicalGlobals.push_back(I);
1245 continue;
1246 }
1247 }
1248 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001249
Reid Spencer5cbf9852007-01-30 20:08:39 +00001250 if (!I->isDeclaration()) {
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001251 addGlobalMapping(I, getMemoryForGV(I));
Chris Lattnerfe854032006-08-16 01:24:12 +00001252 } else {
1253 // External variable reference. Try to use the dynamic loader to
1254 // get a pointer to it.
1255 if (void *SymAddr =
Daniel Dunbarfbee5792009-07-21 08:54:24 +00001256 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
Chris Lattnerfe854032006-08-16 01:24:12 +00001257 addGlobalMapping(I, SymAddr);
1258 else {
Chris Lattner75361b62010-04-07 22:58:41 +00001259 report_fatal_error("Could not resolve external global address: "
Torok Edwin31e24662009-07-07 17:32:34 +00001260 +I->getName());
Chris Lattnerfe854032006-08-16 01:24:12 +00001261 }
1262 }
1263 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001264
Chris Lattnerfe854032006-08-16 01:24:12 +00001265 // If there are multiple modules, map the non-canonical globals to their
1266 // canonical location.
1267 if (!NonCanonicalGlobals.empty()) {
1268 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1269 const GlobalValue *GV = NonCanonicalGlobals[i];
1270 const GlobalValue *CGV =
1271 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1272 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1273 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesc8ed9022008-10-14 10:04:52 +00001274 addGlobalMapping(GV, Ptr);
Chris Lattnerfe854032006-08-16 01:24:12 +00001275 }
1276 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001277
1278 // Now that all of the globals are set up in memory, loop through them all
Reid Spencera54b7cb2007-01-12 07:05:14 +00001279 // and initialize their contents.
Chris Lattnerfe854032006-08-16 01:24:12 +00001280 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1281 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001282 if (!I->isDeclaration()) {
Chris Lattnerfe854032006-08-16 01:24:12 +00001283 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001284 if (const GlobalValue *GVEntry =
Chris Lattnerfe854032006-08-16 01:24:12 +00001285 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1286 if (GVEntry != &*I) // Not the canonical variable.
1287 continue;
1288 }
1289 EmitGlobalVariable(I);
1290 }
1291 }
1292 }
Chris Lattner24b0a182003-12-20 02:45:37 +00001293}
1294
1295// EmitGlobalVariable - This method emits the specified global variable to the
1296// address specified in GlobalAddresses, or allocates new memory if it's not
1297// already in the map.
Chris Lattnerc07ed132003-12-20 03:36:47 +00001298void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner55d86482003-12-31 20:21:04 +00001299 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattner23c47242004-02-08 19:33:23 +00001300
Chris Lattner24b0a182003-12-20 02:45:37 +00001301 if (GA == 0) {
1302 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001303 GA = getMemoryForGV(GV);
Chris Lattner55d86482003-12-31 20:21:04 +00001304 addGlobalMapping(GV, GA);
Chris Lattner24b0a182003-12-20 02:45:37 +00001305 }
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001306
Nicolas Geoffray46fa1392008-10-25 15:41:43 +00001307 // Don't initialize if it's thread local, let the client do it.
1308 if (!GV->isThreadLocal())
1309 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001310
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001311 Type *ElTy = GV->getType()->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +00001312 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattner813c8152005-01-08 20:13:19 +00001313 NumInitBytes += (unsigned)GVSize;
Chris Lattner24b0a182003-12-20 02:45:37 +00001314 ++NumGlobals;
Chris Lattnerbd199fb2002-12-24 00:01:05 +00001315}
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001316
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001317ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1318 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001319}
1320
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001321sys::Mutex *
1322ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001323 return &EES->EE.lock;
1324}
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001325
1326void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1327 const GlobalValue *Old) {
Jeffrey Yasskin23e5fcf2009-10-23 22:37:43 +00001328 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1329 EES->GlobalAddressReverseMap.erase(OldVal);
1330}
1331
Daniel Dunbar48dd8752010-11-13 02:48:57 +00001332void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1333 const GlobalValue *,
1334 const GlobalValue *) {
Craig Topper85814382012-02-07 05:05:23 +00001335 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1336 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskin4c5b23b2009-10-13 17:42:08 +00001337}