blob: a73d8ca8a2bb9a5ce67498b7e8ff0ca31d06cabd [file] [log] [blame]
Misha Brukman857c21b2003-10-10 17:45:12 +00001//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00009//
Chris Lattner996fe012002-12-24 00:01:05 +000010// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +000015#include "llvm/ExecutionEngine/ExecutionEngine.h"
Daniel Dunbar868e3f02010-11-13 02:48:57 +000016#include "llvm/ADT/SmallString.h"
Chris Lattner390d78b2009-08-23 22:49:13 +000017#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ExecutionEngine/GenericValue.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/ExecutionEngine/JITMemoryManager.h"
20#include "llvm/ExecutionEngine/ObjectCache.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Constants.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Module.h"
25#include "llvm/IR/Operator.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000026#include "llvm/IR/ValueHandle.h"
Rafael Espindoladd396572014-08-01 19:28:15 +000027#include "llvm/Object/Archive.h"
David Blaikie35907d82014-04-29 22:04:55 +000028#include "llvm/Object/ObjectFile.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000029#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/DynamicLibrary.h"
Torok Edwin6c2d2332009-07-07 17:32:34 +000031#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Host.h"
Chris Lattner6d8dd182006-05-08 22:00:52 +000033#include "llvm/Support/MutexGuard.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Support/TargetRegistry.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000035#include "llvm/Support/raw_ostream.h"
Dylan Noblesmith8418fdc2011-05-13 21:51:29 +000036#include "llvm/Target/TargetMachine.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000037#include <cmath>
38#include <cstring>
Chris Lattner29681de2003-11-19 21:08:57 +000039using namespace llvm;
Chris Lattner996fe012002-12-24 00:01:05 +000040
Chandler Carruthf58e3762014-04-22 03:04:17 +000041#define DEBUG_TYPE "jit"
42
Chris Lattnerc346ecd2006-12-19 22:43:32 +000043STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
44STATISTIC(NumGlobals , "Number of global vars initialized");
Chris Lattner996fe012002-12-24 00:01:05 +000045
Juergen Ributzkad12ccbd2013-11-19 00:57:56 +000046// Pin the vtable to this file.
47void ObjectCache::anchor() {}
48void ObjectBuffer::anchor() {}
49void ObjectBufferStream::anchor() {}
50
Eric Christopherb9fd9ed2014-08-07 22:02:54 +000051ExecutionEngine *(*ExecutionEngine::JITCtor)(
Rafael Espindola2a8a2792014-08-19 04:04:25 +000052 std::unique_ptr<Module> M,
Eric Christopherb9fd9ed2014-08-07 22:02:54 +000053 std::string *ErrorStr,
54 JITMemoryManager *JMM,
55 bool GVsWithCode,
56 TargetMachine *TM) = nullptr;
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000057ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
Rafael Espindola2a8a2792014-08-19 04:04:25 +000058 std::unique_ptr<Module >M,
Daniel Dunbar70ff8b02010-11-17 16:06:37 +000059 std::string *ErrorStr,
Filip Pizlo9bc53e82013-05-14 19:29:00 +000060 RTDyldMemoryManager *MCJMM,
Craig Topper2617dcc2014-04-15 06:32:26 +000061 TargetMachine *TM) = nullptr;
Rafael Espindola2a8a2792014-08-19 04:04:25 +000062ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
Craig Topper2617dcc2014-04-15 06:32:26 +000063 std::string *ErrorStr) =nullptr;
Chris Lattner2d52c1b2006-03-22 06:07:50 +000064
Rafael Espindola2a8a2792014-08-19 04:04:25 +000065ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +000066 : EEState(*this),
Craig Topper2617dcc2014-04-15 06:32:26 +000067 LazyFunctionCreator(nullptr) {
Jeffrey Yasskin4567db42009-10-27 20:30:28 +000068 CompilingLazily = false;
Evan Chengcdc00602008-09-24 16:25:55 +000069 GVCompilationDisabled = false;
Evan Cheng84a90552008-06-17 16:49:02 +000070 SymbolSearchingDisabled = false;
Lang Hamesbc876012014-04-18 06:48:23 +000071
72 // IR module verification is enabled by default in debug builds, and disabled
73 // by default in release builds.
74#ifndef NDEBUG
75 VerifyModules = true;
76#else
77 VerifyModules = false;
78#endif
79
Jeffrey Yasskin091217b2010-01-27 20:34:15 +000080 assert(M && "Module is null?");
Rafael Espindola2a8a2792014-08-19 04:04:25 +000081 Modules.push_back(std::move(M));
Misha Brukman260b0c82003-10-16 21:18:05 +000082}
83
Brian Gaeke92f8b302003-09-04 22:57:27 +000084ExecutionEngine::~ExecutionEngine() {
Reid Spencer603682a2007-03-03 18:19:18 +000085 clearAllGlobalMappings();
Brian Gaeke92f8b302003-09-04 22:57:27 +000086}
87
Jeffrey Yasskina4044332010-03-27 04:53:56 +000088namespace {
Daniel Dunbar868e3f02010-11-13 02:48:57 +000089/// \brief Helper class which uses a value handler to automatically deletes the
90/// memory block when the GlobalVariable is destroyed.
Jeffrey Yasskina4044332010-03-27 04:53:56 +000091class GVMemoryBlock : public CallbackVH {
92 GVMemoryBlock(const GlobalVariable *GV)
93 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
94
95public:
Daniel Dunbar868e3f02010-11-13 02:48:57 +000096 /// \brief Returns the address the GlobalVariable should be written into. The
97 /// GVMemoryBlock object prefixes that.
Micah Villmowcdfe20b2012-10-08 16:38:25 +000098 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
Chris Lattner229907c2011-07-18 04:54:35 +000099 Type *ElTy = GV->getType()->getElementType();
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000100 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
101 void *RawMemory = ::operator new(
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000102 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000103 TD.getPreferredAlignment(GV))
104 + GVSize);
105 new(RawMemory) GVMemoryBlock(GV);
106 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
107 }
108
Craig Topperb51ff602014-03-08 07:51:20 +0000109 void deleted() override {
Jeffrey Yasskina4044332010-03-27 04:53:56 +0000110 // We allocated with operator new and with some extra memory hanging off the
111 // end, so don't just delete this. I'm not sure if this is actually
112 // required.
113 this->~GVMemoryBlock();
114 ::operator delete(this);
115 }
116};
117} // anonymous namespace
118
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000119char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000120 return GVMemoryBlock::Create(GV, *getDataLayout());
Nicolas Geoffray5457ce92008-10-25 15:41:43 +0000121}
122
David Blaikie35907d82014-04-29 22:04:55 +0000123void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
124 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
125}
126
Rafael Espindola7271c192014-08-26 21:04:04 +0000127void
128ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
129 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
130}
131
Rafael Espindola48af1c22014-08-19 18:44:46 +0000132void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
Rafael Espindolaacfd6282014-08-01 18:49:24 +0000133 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
134}
135
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000136bool ExecutionEngine::removeModule(Module *M) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000137 for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
138 Module *Found = I->get();
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000139 if (Found == M) {
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000140 I->release();
Devang Patel324fe892007-10-15 19:56:32 +0000141 Modules.erase(I);
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000142 clearGlobalMappingsFromModule(M);
143 return true;
Devang Patel324fe892007-10-15 19:56:32 +0000144 }
145 }
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000146 return false;
Nate Begeman617001d2009-01-23 19:27:28 +0000147}
148
Chris Lattner0621cae2006-08-16 01:24:12 +0000149Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
150 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +0000151 if (Function *F = Modules[i]->getFunction(FnName))
Chris Lattner0621cae2006-08-16 01:24:12 +0000152 return F;
153 }
Craig Topper2617dcc2014-04-15 06:32:26 +0000154 return nullptr;
Chris Lattner0621cae2006-08-16 01:24:12 +0000155}
156
157
Zachary Turner2f825df2014-06-16 20:54:28 +0000158void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000159 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000160 void *OldVal;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000161
162 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
163 // GlobalAddressMap.
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000164 if (I == GlobalAddressMap.end())
Craig Topper2617dcc2014-04-15 06:32:26 +0000165 OldVal = nullptr;
Jeffrey Yasskin307c0532009-10-09 22:10:27 +0000166 else {
167 OldVal = I->second;
168 GlobalAddressMap.erase(I);
169 }
170
171 GlobalAddressReverseMap.erase(OldVal);
172 return OldVal;
173}
174
Chris Lattner6d8dd182006-05-08 22:00:52 +0000175void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000176 MutexGuard locked(lock);
Evan Cheng5cc53c32008-09-18 07:54:21 +0000177
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000178 DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
Daniel Dunbar9813b0b2009-07-26 07:49:05 +0000179 << "\' to [" << Addr << "]\n";);
Zachary Turner2f825df2014-06-16 20:54:28 +0000180 void *&CurVal = EEState.getGlobalAddressMap()[GV];
Craig Topper2617dcc2014-04-15 06:32:26 +0000181 assert((!CurVal || !Addr) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000182 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000183
184 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000185 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000186 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000187 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000188 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000189 V = GV;
190 }
191}
192
Chris Lattner6d8dd182006-05-08 22:00:52 +0000193void ExecutionEngine::clearAllGlobalMappings() {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000194 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000195
Zachary Turner2f825df2014-06-16 20:54:28 +0000196 EEState.getGlobalAddressMap().clear();
197 EEState.getGlobalAddressReverseMap().clear();
Chris Lattner6d8dd182006-05-08 22:00:52 +0000198}
199
Nate Begeman8f83fc42008-05-21 16:34:48 +0000200void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000201 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000202
203 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000204 EEState.RemoveMapping(FI);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000205 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
206 GI != GE; ++GI)
Zachary Turner2f825df2014-06-16 20:54:28 +0000207 EEState.RemoveMapping(GI);
Nate Begeman8f83fc42008-05-21 16:34:48 +0000208}
209
Chris Lattneree181732008-04-04 04:47:41 +0000210void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000211 MutexGuard locked(lock);
Chris Lattneree181732008-04-04 04:47:41 +0000212
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000213 ExecutionEngineState::GlobalAddressMapTy &Map =
Zachary Turner2f825df2014-06-16 20:54:28 +0000214 EEState.getGlobalAddressMap();
Chris Lattneree181732008-04-04 04:47:41 +0000215
Chris Lattner6d8dd182006-05-08 22:00:52 +0000216 // Deleting from the mapping?
Craig Topper2617dcc2014-04-15 06:32:26 +0000217 if (!Addr)
Zachary Turner2f825df2014-06-16 20:54:28 +0000218 return EEState.RemoveMapping(GV);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000219
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000220 void *&CurVal = Map[GV];
Chris Lattneree181732008-04-04 04:47:41 +0000221 void *OldVal = CurVal;
222
Zachary Turner2f825df2014-06-16 20:54:28 +0000223 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
224 EEState.getGlobalAddressReverseMap().erase(CurVal);
Chris Lattner6d8dd182006-05-08 22:00:52 +0000225 CurVal = Addr;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000226
227 // If we are using the reverse mapping, add it too.
Zachary Turner2f825df2014-06-16 20:54:28 +0000228 if (!EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000229 AssertingVH<const GlobalValue> &V =
Zachary Turner2f825df2014-06-16 20:54:28 +0000230 EEState.getGlobalAddressReverseMap()[Addr];
Craig Topper2617dcc2014-04-15 06:32:26 +0000231 assert((!V || !GV) && "GlobalMapping already established!");
Chris Lattner6d8dd182006-05-08 22:00:52 +0000232 V = GV;
233 }
Chris Lattneree181732008-04-04 04:47:41 +0000234 return OldVal;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000235}
236
Chris Lattner6d8dd182006-05-08 22:00:52 +0000237void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000238 MutexGuard locked(lock);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000239
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000240 ExecutionEngineState::GlobalAddressMapTy::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000241 EEState.getGlobalAddressMap().find(GV);
242 return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
Chris Lattner6d8dd182006-05-08 22:00:52 +0000243}
244
Chris Lattner748e8572003-12-31 20:21:04 +0000245const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
Zachary Turnerc04b8922014-06-20 21:07:14 +0000246 MutexGuard locked(lock);
Reid Spencer79876f52005-07-12 15:51:55 +0000247
Chris Lattner748e8572003-12-31 20:21:04 +0000248 // If we haven't computed the reverse mapping yet, do so first.
Zachary Turner2f825df2014-06-16 20:54:28 +0000249 if (EEState.getGlobalAddressReverseMap().empty()) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +0000250 for (ExecutionEngineState::GlobalAddressMapTy::iterator
Zachary Turner2f825df2014-06-16 20:54:28 +0000251 I = EEState.getGlobalAddressMap().begin(),
252 E = EEState.getGlobalAddressMap().end(); I != E; ++I)
253 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
Daniel Dunbare4f47432010-11-13 00:55:42 +0000254 I->second, I->first));
Chris Lattner748e8572003-12-31 20:21:04 +0000255 }
256
Jeffrey Yasskin6bf87df2009-08-07 19:54:29 +0000257 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
Zachary Turner2f825df2014-06-16 20:54:28 +0000258 EEState.getGlobalAddressReverseMap().find(Addr);
259 return I != EEState.getGlobalAddressReverseMap().end() ? I->second : nullptr;
Chris Lattner748e8572003-12-31 20:21:04 +0000260}
Chris Lattner5a0d4822003-12-26 06:50:30 +0000261
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000262namespace {
263class ArgvArray {
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000264 std::unique_ptr<char[]> Array;
265 std::vector<std::unique_ptr<char[]>> Values;
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000266public:
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000267 /// Turn a vector of strings into a nice argv style array of pointers to null
268 /// terminated strings.
269 void *reset(LLVMContext &C, ExecutionEngine *EE,
270 const std::vector<std::string> &InputArgv);
271};
272} // anonymous namespace
273void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
274 const std::vector<std::string> &InputArgv) {
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000275 Values.clear(); // Free the old contents.
276 Values.reserve(InputArgv.size());
Chandler Carruth5da3f052012-11-01 09:14:31 +0000277 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000278 Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000279
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000280 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
Chris Lattner229907c2011-07-18 04:54:35 +0000281 Type *SBytePtr = Type::getInt8PtrTy(C);
Chris Lattner5a0d4822003-12-26 06:50:30 +0000282
283 for (unsigned i = 0; i != InputArgv.size(); ++i) {
284 unsigned Size = InputArgv[i].size()+1;
Dylan Noblesmith4b535d12014-08-26 02:03:28 +0000285 auto Dest = make_unique<char[]>(Size);
286 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
Misha Brukman835702a2005-04-21 22:36:52 +0000287
Dylan Noblesmith4b535d12014-08-26 02:03:28 +0000288 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
Chris Lattner5a0d4822003-12-26 06:50:30 +0000289 Dest[Size-1] = 0;
Misha Brukman835702a2005-04-21 22:36:52 +0000290
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000291 // Endian safe: Array[i] = (PointerTy)Dest;
Dylan Noblesmith4b535d12014-08-26 02:03:28 +0000292 EE->StoreValueToMemory(PTOGV(Dest.get()),
293 (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
294 Values.push_back(std::move(Dest));
Chris Lattner5a0d4822003-12-26 06:50:30 +0000295 }
296
297 // Null terminate it
Craig Topper2617dcc2014-04-15 06:32:26 +0000298 EE->StoreValueToMemory(PTOGV(nullptr),
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000299 (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
Chris Lattner5a0d4822003-12-26 06:50:30 +0000300 SBytePtr);
Dylan Noblesmithc4a99422014-08-25 00:58:18 +0000301 return Array.get();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000302}
303
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000304void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000305 bool isDtors) {
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000306 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000307 GlobalVariable *GV = module.getNamedGlobal(Name);
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000308
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000309 // If this global has internal linkage, or if it has a use, then it must be
310 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
311 // this is the case, don't execute any of the global ctors, __main will do
312 // it.
313 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
314
Nick Lewycky0cbfcb22011-04-06 20:38:44 +0000315 // Should be an array of '{ i32, void ()* }' structs. The first value is
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000316 // the init priority, which we ignore.
Chris Lattner00245f42012-01-24 13:41:11 +0000317 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
Craig Topper2617dcc2014-04-15 06:32:26 +0000318 if (!InitList)
Nick Lewycky0f857892011-04-11 22:11:20 +0000319 return;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000320 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner00245f42012-01-24 13:41:11 +0000321 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
Craig Topper2617dcc2014-04-15 06:32:26 +0000322 if (!CS) continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000323
324 Constant *FP = CS->getOperand(1);
325 if (FP->isNullValue())
Nick Lewycky0f857892011-04-11 22:11:20 +0000326 continue; // Found a sentinal value, ignore.
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000327
328 // Strip off constant expression casts.
329 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
330 if (CE->isCast())
331 FP = CE->getOperand(0);
332
333 // Execute the ctor/dtor function!
334 if (Function *F = dyn_cast<Function>(FP))
335 runFunction(F, std::vector<GenericValue>());
336
337 // FIXME: It is marginally lame that we just do nothing here if we see an
338 // entry we don't recognize. It might not be unreasonable for the verifier
339 // to not even allow this and just assert here.
340 }
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000341}
342
Evan Cheng1a9a0b72008-09-30 15:51:21 +0000343void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
344 // Execute global ctors/dtors for each module in the program.
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000345 for (std::unique_ptr<Module> &M : Modules)
346 runStaticConstructorsDestructors(*M, isDtors);
Chris Lattnerfaae50b2006-03-08 18:42:46 +0000347}
348
Dan Gohmancf3e3012008-08-26 01:38:29 +0000349#ifndef NDEBUG
Duncan Sands1202d1b2007-12-14 19:38:31 +0000350/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
351static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
Chandler Carruth5da3f052012-11-01 09:14:31 +0000352 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000353 for (unsigned i = 0; i < PtrSize; ++i)
354 if (*(i + (uint8_t*)Loc))
355 return false;
356 return true;
357}
Dan Gohmancf3e3012008-08-26 01:38:29 +0000358#endif
Duncan Sands1202d1b2007-12-14 19:38:31 +0000359
Chris Lattner5a0d4822003-12-26 06:50:30 +0000360int ExecutionEngine::runFunctionAsMain(Function *Fn,
361 const std::vector<std::string> &argv,
362 const char * const * envp) {
363 std::vector<GenericValue> GVArgs;
364 GenericValue GVArgc;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000365 GVArgc.IntVal = APInt(32, argv.size());
Anton Korobeynikov8c32c112007-06-03 19:17:35 +0000366
367 // Check main() type
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000368 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
Chris Lattner229907c2011-07-18 04:54:35 +0000369 FunctionType *FTy = Fn->getFunctionType();
370 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000371
372 // Check the argument types.
373 if (NumArgs > 3)
374 report_fatal_error("Invalid number of arguments of main() supplied");
375 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
376 report_fatal_error("Invalid type for third argument of main() supplied");
377 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
378 report_fatal_error("Invalid type for second argument of main() supplied");
379 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
380 report_fatal_error("Invalid type for first argument of main() supplied");
381 if (!FTy->getReturnType()->isIntegerTy() &&
382 !FTy->getReturnType()->isVoidTy())
383 report_fatal_error("Invalid return type of main() supplied");
384
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000385 ArgvArray CArgv;
386 ArgvArray CEnv;
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000387 if (NumArgs) {
388 GVArgs.push_back(GVArgc); // Arg #0 = argc.
389 if (NumArgs > 1) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000390 // Arg #1 = argv.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000391 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
Duncan Sands1202d1b2007-12-14 19:38:31 +0000392 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000393 "argv[0] was null after CreateArgv");
394 if (NumArgs > 2) {
395 std::vector<std::string> EnvVars;
396 for (unsigned i = 0; envp[i]; ++i)
397 EnvVars.push_back(envp[i]);
Owen Anderson55f1c092009-08-13 21:58:54 +0000398 // Arg #2 = envp.
Jeffrey Yasskinbfd38ab2010-03-26 00:59:12 +0000399 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
Chris Lattnerb1cad0b2004-08-16 01:05:35 +0000400 }
401 }
402 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000403
Reid Spencer87aa65f2007-03-06 03:04:04 +0000404 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
Chris Lattner5a0d4822003-12-26 06:50:30 +0000405}
406
Alp Toker322db9e2014-05-31 21:26:17 +0000407void EngineBuilder::InitEngine() {
408 WhichEngine = EngineKind::Either;
409 ErrorStr = nullptr;
410 OptLevel = CodeGenOpt::Default;
411 MCJMM = nullptr;
412 JMM = nullptr;
413 Options = TargetOptions();
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000414 AllocateGVsWithCode = false;
Alp Toker322db9e2014-05-31 21:26:17 +0000415 RelocModel = Reloc::Default;
416 CMModel = CodeModel::JITDefault;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000417 UseMCJIT = false;
Alp Toker322db9e2014-05-31 21:26:17 +0000418
419// IR module verification is enabled by default in debug builds, and disabled
420// by default in release builds.
421#ifndef NDEBUG
422 VerifyModules = true;
423#else
424 VerifyModules = false;
425#endif
426}
427
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000428ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000429 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000430
Nick Lewyckya53414f2008-03-08 02:49:45 +0000431 // Make sure we can resolve symbols in the program as well. The zero arg
432 // to the function tells DynamicLibrary to load the program, not a library.
Craig Topper2617dcc2014-04-15 06:32:26 +0000433 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
434 return nullptr;
Nick Lewyckya53414f2008-03-08 02:49:45 +0000435
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000436 assert(!(JMM && MCJMM));
437
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000438 // If the user specified a memory manager but didn't specify which engine to
439 // create, we assume they only want the JIT, and we fail if they only want
440 // the interpreter.
Filip Pizlo9bc53e82013-05-14 19:29:00 +0000441 if (JMM || MCJMM) {
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000442 if (WhichEngine & EngineKind::JIT)
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000443 WhichEngine = EngineKind::JIT;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000444 else {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000445 if (ErrorStr)
446 *ErrorStr = "Cannot create an interpreter with a memory manager.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000447 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000448 }
449 }
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000450
451 if (MCJMM && ! UseMCJIT) {
452 if (ErrorStr)
453 *ErrorStr =
454 "Cannot create a legacy JIT with a runtime dyld memory "
455 "manager.";
456 return nullptr;
457 }
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000458
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000459 // Unless the interpreter was explicitly selected or the JIT is not linked,
460 // try making a JIT.
Benjamin Kramer25a3d812012-04-08 14:53:14 +0000461 if ((WhichEngine & EngineKind::JIT) && TheTM) {
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000462 Triple TT(M->getTargetTriple());
Owen Andersonadd6f1d2012-03-23 17:40:56 +0000463 if (!TM->getTarget().hasJIT()) {
464 errs() << "WARNING: This target JIT is not designed for the host"
465 << " you are running. If bad things happen, please choose"
466 << " a different -march switch.\n";
467 }
Dylan Noblesmith7f262462011-12-12 04:20:36 +0000468
Lang Hamesbc876012014-04-18 06:48:23 +0000469 ExecutionEngine *EE = nullptr;
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000470 if (UseMCJIT && ExecutionEngine::MCJITCtor)
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000471 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr,
472 MCJMM ? MCJMM : JMM, TheTM.release());
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000473 else if (ExecutionEngine::JITCtor)
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000474 EE = ExecutionEngine::JITCtor(std::move(M), ErrorStr, JMM,
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000475 AllocateGVsWithCode, TheTM.release());
Lang Hamesbc876012014-04-18 06:48:23 +0000476
477 if (EE) {
478 EE->setVerifyModules(VerifyModules);
479 return EE;
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000480 }
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000481 }
482
483 // If we can't make a JIT and we didn't request one specifically, try making
484 // an interpreter instead.
Chris Lattner41fa2bd2009-09-23 01:46:04 +0000485 if (WhichEngine & EngineKind::Interpreter) {
486 if (ExecutionEngine::InterpCtor)
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000487 return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
Chris Lattner8bcc6442009-09-23 02:03:49 +0000488 if (ErrorStr)
489 *ErrorStr = "Interpreter has not been linked in.";
Craig Topper2617dcc2014-04-15 06:32:26 +0000490 return nullptr;
Reid Klecknerfc8a2d52009-07-18 00:42:18 +0000491 }
Chris Lattner8bcc6442009-09-23 02:03:49 +0000492
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000493 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
494 !ExecutionEngine::MCJITCtor) {
Chris Lattner8bcc6442009-09-23 02:03:49 +0000495 if (ErrorStr)
496 *ErrorStr = "JIT has not been linked in.";
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000497 }
498
Craig Topper2617dcc2014-04-15 06:32:26 +0000499 return nullptr;
Brian Gaeke4bd3bd52003-09-03 20:34:19 +0000500}
501
Chris Lattner996fe012002-12-24 00:01:05 +0000502void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
Brian Gaeke1678e852003-08-13 18:16:14 +0000503 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
Chris Lattner996fe012002-12-24 00:01:05 +0000504 return getPointerToFunction(F);
505
Zachary Turnerc04b8922014-06-20 21:07:14 +0000506 MutexGuard locked(lock);
Zachary Turner2f825df2014-06-16 20:54:28 +0000507 if (void *P = EEState.getGlobalAddressMap()[GV])
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000508 return P;
Jeff Cohen69e849012006-02-07 05:11:57 +0000509
510 // Global variable might have been added since interpreter started.
511 if (GlobalVariable *GVar =
512 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
513 EmitGlobalVariable(GVar);
514 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000515 llvm_unreachable("Global hasn't had an address allocated yet!");
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000516
Zachary Turner2f825df2014-06-16 20:54:28 +0000517 return EEState.getGlobalAddressMap()[GV];
Chris Lattner996fe012002-12-24 00:01:05 +0000518}
519
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000520/// \brief Converts a Constant* into a GenericValue, including handling of
521/// ConstantExpr values.
Chris Lattner996fe012002-12-24 00:01:05 +0000522GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000523 // If its undefined, return the garbage.
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000524 if (isa<UndefValue>(C)) {
525 GenericValue Result;
526 switch (C->getType()->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000527 default:
528 break;
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000529 case Type::IntegerTyID:
530 case Type::X86_FP80TyID:
531 case Type::FP128TyID:
532 case Type::PPC_FP128TyID:
533 // Although the value is undefined, we still have to construct an APInt
534 // with the correct bit width.
535 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
536 break;
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000537 case Type::StructTyID: {
538 // if the whole struct is 'undef' just reserve memory for the value.
539 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
540 unsigned int elemNum = STy->getNumElements();
541 Result.AggregateVal.resize(elemNum);
542 for (unsigned int i = 0; i < elemNum; ++i) {
543 Type *ElemTy = STy->getElementType(i);
544 if (ElemTy->isIntegerTy())
545 Result.AggregateVal[i].IntVal =
546 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
547 else if (ElemTy->isAggregateType()) {
548 const Constant *ElemUndef = UndefValue::get(ElemTy);
549 Result.AggregateVal[i] = getConstantValue(ElemUndef);
550 }
551 }
552 }
553 }
554 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000555 case Type::VectorTyID:
556 // if the whole vector is 'undef' just reserve memory for the value.
557 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
558 const Type *ElemTy = VTy->getElementType();
559 unsigned int elemNum = VTy->getNumElements();
560 Result.AggregateVal.resize(elemNum);
561 if (ElemTy->isIntegerTy())
562 for (unsigned int i = 0; i < elemNum; ++i)
Elena Demikhovsky8e97f012013-09-12 10:48:23 +0000563 Result.AggregateVal[i].IntVal =
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000564 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
Jay Foadbcbdbfb2010-01-15 08:32:58 +0000565 break;
566 }
567 return Result;
568 }
Chris Lattner9de0d142003-04-23 19:01:49 +0000569
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000570 // Otherwise, if the value is a ConstantExpr...
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000571 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000572 Constant *Op0 = CE->getOperand(0);
Chris Lattner9de0d142003-04-23 19:01:49 +0000573 switch (CE->getOpcode()) {
574 case Instruction::GetElementPtr: {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000575 // Compute the index
Reid Spencer4fd528f2007-03-06 22:23:15 +0000576 GenericValue Result = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000577 APInt Offset(DL->getPointerSizeInBits(), 0);
578 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
Misha Brukman835702a2005-04-21 22:36:52 +0000579
Reid Spencer87aa65f2007-03-06 03:04:04 +0000580 char* tmp = (char*) Result.PointerVal;
Nuno Lopesb6ad9822012-12-30 16:25:48 +0000581 Result = PTOGV(tmp + Offset.getSExtValue());
Chris Lattner9de0d142003-04-23 19:01:49 +0000582 return Result;
583 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000584 case Instruction::Trunc: {
585 GenericValue GV = getConstantValue(Op0);
586 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
587 GV.IntVal = GV.IntVal.trunc(BitWidth);
588 return GV;
589 }
590 case Instruction::ZExt: {
591 GenericValue GV = getConstantValue(Op0);
592 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
593 GV.IntVal = GV.IntVal.zext(BitWidth);
594 return GV;
595 }
596 case Instruction::SExt: {
597 GenericValue GV = getConstantValue(Op0);
598 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
599 GV.IntVal = GV.IntVal.sext(BitWidth);
600 return GV;
601 }
602 case Instruction::FPTrunc: {
Dale Johannesena1336cf2007-09-17 18:44:13 +0000603 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000604 GenericValue GV = getConstantValue(Op0);
605 GV.FloatVal = float(GV.DoubleVal);
606 return GV;
607 }
608 case Instruction::FPExt:{
Dale Johannesena1336cf2007-09-17 18:44:13 +0000609 // FIXME long double
Reid Spencer4fd528f2007-03-06 22:23:15 +0000610 GenericValue GV = getConstantValue(Op0);
611 GV.DoubleVal = double(GV.FloatVal);
612 return GV;
613 }
614 case Instruction::UIToFP: {
615 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000616 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000617 GV.FloatVal = float(GV.IntVal.roundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000618 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000619 GV.DoubleVal = GV.IntVal.roundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000620 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000621 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000622 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000623 false,
624 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000625 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000626 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000627 return GV;
628 }
629 case Instruction::SIToFP: {
630 GenericValue GV = getConstantValue(Op0);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000631 if (CE->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000632 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
Chris Lattnerfdd87902009-10-05 05:54:46 +0000633 else if (CE->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000634 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000635 else if (CE->getType()->isX86_FP80Ty()) {
Benjamin Kramer31920b02010-12-04 15:28:22 +0000636 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000637 (void)apf.convertFromAPInt(GV.IntVal,
Dan Gohmanca24fd92008-02-29 01:27:13 +0000638 true,
639 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000640 GV.IntVal = apf.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000641 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000642 return GV;
643 }
644 case Instruction::FPToUI: // double->APInt conversion handles sign
645 case Instruction::FPToSI: {
646 GenericValue GV = getConstantValue(Op0);
647 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000648 if (Op0->getType()->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000649 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000650 else if (Op0->getType()->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000651 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
Chris Lattnerfdd87902009-10-05 05:54:46 +0000652 else if (Op0->getType()->isX86_FP80Ty()) {
Tim Northover29178a32013-01-22 09:46:31 +0000653 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000654 uint64_t v;
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000655 bool ignored;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000656 (void)apf.convertToInteger(&v, BitWidth,
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000657 CE->getOpcode()==Instruction::FPToSI,
Dale Johannesen4f0bd682008-10-09 23:00:39 +0000658 APFloat::rmTowardZero, &ignored);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000659 GV.IntVal = v; // endian?
660 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000661 return GV;
662 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000663 case Instruction::PtrToInt: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000664 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000665 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000666 assert(PtrWidth <= 64 && "Bad pointer width");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000667 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000668 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000669 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000670 return GV;
671 }
672 case Instruction::IntToPtr: {
673 GenericValue GV = getConstantValue(Op0);
Rafael Espindola7c68beb2014-02-18 15:33:12 +0000674 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
Eli Friedmanfc1f2cd2012-10-30 22:21:55 +0000675 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000676 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
677 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000678 return GV;
679 }
680 case Instruction::BitCast: {
Reid Spencer4fd528f2007-03-06 22:23:15 +0000681 GenericValue GV = getConstantValue(Op0);
Chris Lattner229907c2011-07-18 04:54:35 +0000682 Type* DestTy = CE->getType();
Reid Spencer4fd528f2007-03-06 22:23:15 +0000683 switch (Op0->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000684 default: llvm_unreachable("Invalid bitcast operand");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000685 case Type::IntegerTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000686 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
Chris Lattnerfdd87902009-10-05 05:54:46 +0000687 if (DestTy->isFloatTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000688 GV.FloatVal = GV.IntVal.bitsToFloat();
Chris Lattnerfdd87902009-10-05 05:54:46 +0000689 else if (DestTy->isDoubleTy())
Reid Spencer4fd528f2007-03-06 22:23:15 +0000690 GV.DoubleVal = GV.IntVal.bitsToDouble();
691 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000692 case Type::FloatTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000693 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000694 GV.IntVal = APInt::floatToBits(GV.FloatVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000695 break;
696 case Type::DoubleTyID:
Duncan Sands9dff9be2010-02-15 16:12:20 +0000697 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
Jay Foad3447fb02010-11-28 21:04:48 +0000698 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
Reid Spencer4fd528f2007-03-06 22:23:15 +0000699 break;
700 case Type::PointerTyID:
Duncan Sands19d0b472010-02-16 11:11:14 +0000701 assert(DestTy->isPointerTy() && "Invalid bitcast");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000702 break; // getConstantValue(Op0) above already converted it
703 }
704 return GV;
Chris Lattner9de0d142003-04-23 19:01:49 +0000705 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000706 case Instruction::Add:
Dan Gohmana5b96452009-06-04 22:49:04 +0000707 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000708 case Instruction::Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +0000709 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000710 case Instruction::Mul:
Dan Gohmana5b96452009-06-04 22:49:04 +0000711 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000712 case Instruction::UDiv:
713 case Instruction::SDiv:
714 case Instruction::URem:
715 case Instruction::SRem:
716 case Instruction::And:
717 case Instruction::Or:
718 case Instruction::Xor: {
719 GenericValue LHS = getConstantValue(Op0);
720 GenericValue RHS = getConstantValue(CE->getOperand(1));
721 GenericValue GV;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000722 switch (CE->getOperand(0)->getType()->getTypeID()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000723 default: llvm_unreachable("Bad add type!");
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000724 case Type::IntegerTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000725 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000726 default: llvm_unreachable("Invalid integer opcode");
Reid Spencer4fd528f2007-03-06 22:23:15 +0000727 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
728 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
729 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
730 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
731 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
732 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
733 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
734 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
735 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
736 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
737 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000738 break;
739 case Type::FloatTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000740 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000741 default: llvm_unreachable("Invalid float opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000742 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000743 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000744 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000745 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000746 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000747 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000748 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000749 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000750 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000751 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000752 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000753 break;
754 case Type::DoubleTyID:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000755 switch (CE->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000756 default: llvm_unreachable("Invalid double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000757 case Instruction::FAdd:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000758 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000759 case Instruction::FSub:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000760 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000761 case Instruction::FMul:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000762 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000763 case Instruction::FDiv:
Reid Spencer4fd528f2007-03-06 22:23:15 +0000764 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000765 case Instruction::FRem:
Chris Lattner93cd0f1c2010-05-15 17:10:24 +0000766 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
Reid Spencer4fd528f2007-03-06 22:23:15 +0000767 }
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000768 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000769 case Type::X86_FP80TyID:
770 case Type::PPC_FP128TyID:
771 case Type::FP128TyID: {
Tim Northover29178a32013-01-22 09:46:31 +0000772 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
773 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
Dale Johannesena1336cf2007-09-17 18:44:13 +0000774 switch (CE->getOpcode()) {
Daniel Dunbare4f47432010-11-13 00:55:42 +0000775 default: llvm_unreachable("Invalid long double opcode");
Dan Gohmana5b96452009-06-04 22:49:04 +0000776 case Instruction::FAdd:
Tim Northover29178a32013-01-22 09:46:31 +0000777 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000778 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000779 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000780 case Instruction::FSub:
Tim Northover29178a32013-01-22 09:46:31 +0000781 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
782 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000783 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000784 break;
Dan Gohmana5b96452009-06-04 22:49:04 +0000785 case Instruction::FMul:
Tim Northover29178a32013-01-22 09:46:31 +0000786 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
787 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000788 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000789 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000790 case Instruction::FDiv:
Tim Northover29178a32013-01-22 09:46:31 +0000791 apfLHS.divide(APFloat(Sem, RHS.IntVal),
792 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000793 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000794 break;
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000795 case Instruction::FRem:
Tim Northover29178a32013-01-22 09:46:31 +0000796 apfLHS.mod(APFloat(Sem, RHS.IntVal),
797 APFloat::rmNearestTiesToEven);
Dale Johannesen54306fe2008-10-09 18:53:47 +0000798 GV.IntVal = apfLHS.bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000799 break;
800 }
801 }
802 break;
Chris Lattnerc4e6bb52004-07-11 08:01:11 +0000803 }
Reid Spencer4fd528f2007-03-06 22:23:15 +0000804 return GV;
805 }
Chris Lattner68cbcc32003-05-14 17:51:49 +0000806 default:
807 break;
808 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000809
810 SmallString<256> Msg;
811 raw_svector_ostream OS(Msg);
812 OS << "ConstantExpr not handled: " << *CE;
813 report_fatal_error(OS.str());
Chris Lattner68cbcc32003-05-14 17:51:49 +0000814 }
Misha Brukman835702a2005-04-21 22:36:52 +0000815
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000816 // Otherwise, we have a simple constant.
Reid Spencer4fd528f2007-03-06 22:23:15 +0000817 GenericValue Result;
Chris Lattner6b727592004-06-17 18:19:28 +0000818 switch (C->getType()->getTypeID()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000819 case Type::FloatTyID:
820 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
Reid Spencer7a9c62b2007-01-12 07:05:14 +0000821 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000822 case Type::DoubleTyID:
Dale Johannesenbed9dc42007-09-06 18:13:44 +0000823 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
Reid Spencer87aa65f2007-03-06 03:04:04 +0000824 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +0000825 case Type::X86_FP80TyID:
826 case Type::FP128TyID:
827 case Type::PPC_FP128TyID:
Dale Johannesen54306fe2008-10-09 18:53:47 +0000828 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
Dale Johannesena1336cf2007-09-17 18:44:13 +0000829 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000830 case Type::IntegerTyID:
831 Result.IntVal = cast<ConstantInt>(C)->getValue();
832 break;
Chris Lattner996fe012002-12-24 00:01:05 +0000833 case Type::PointerTyID:
Reid Spencer6a0fd732004-07-18 00:41:27 +0000834 if (isa<ConstantPointerNull>(C))
Craig Topper2617dcc2014-04-15 06:32:26 +0000835 Result.PointerVal = nullptr;
Reid Spencer6a0fd732004-07-18 00:41:27 +0000836 else if (const Function *F = dyn_cast<Function>(C))
837 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
Chris Lattner0c778f72009-10-29 05:26:09 +0000838 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
Reid Spencer6a0fd732004-07-18 00:41:27 +0000839 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
Eric Christopherb9fd9ed2014-08-07 22:02:54 +0000840 else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
841 Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
842 BA->getBasicBlock())));
Reid Spencer6a0fd732004-07-18 00:41:27 +0000843 else
Torok Edwinfbcc6632009-07-14 16:55:14 +0000844 llvm_unreachable("Unknown constant pointer type!");
Chris Lattner996fe012002-12-24 00:01:05 +0000845 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000846 case Type::VectorTyID: {
847 unsigned elemNum;
848 Type* ElemTy;
849 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
850 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
851 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
852
853 if (CDV) {
854 elemNum = CDV->getNumElements();
855 ElemTy = CDV->getElementType();
856 } else if (CV || CAZ) {
857 VectorType* VTy = dyn_cast<VectorType>(C->getType());
858 elemNum = VTy->getNumElements();
859 ElemTy = VTy->getElementType();
860 } else {
861 llvm_unreachable("Unknown constant vector type!");
862 }
863
864 Result.AggregateVal.resize(elemNum);
865 // Check if vector holds floats.
866 if(ElemTy->isFloatTy()) {
867 if (CAZ) {
868 GenericValue floatZero;
869 floatZero.FloatVal = 0.f;
870 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
871 floatZero);
872 break;
873 }
874 if(CV) {
875 for (unsigned i = 0; i < elemNum; ++i)
876 if (!isa<UndefValue>(CV->getOperand(i)))
877 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
878 CV->getOperand(i))->getValueAPF().convertToFloat();
879 break;
880 }
881 if(CDV)
882 for (unsigned i = 0; i < elemNum; ++i)
883 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
884
885 break;
886 }
887 // Check if vector holds doubles.
888 if (ElemTy->isDoubleTy()) {
889 if (CAZ) {
890 GenericValue doubleZero;
891 doubleZero.DoubleVal = 0.0;
892 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
893 doubleZero);
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].DoubleVal = cast<ConstantFP>(
900 CV->getOperand(i))->getValueAPF().convertToDouble();
901 break;
902 }
903 if(CDV)
904 for (unsigned i = 0; i < elemNum; ++i)
905 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
906
907 break;
908 }
909 // Check if vector holds integers.
910 if (ElemTy->isIntegerTy()) {
911 if (CAZ) {
912 GenericValue intZero;
913 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
914 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
915 intZero);
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].IntVal = cast<ConstantInt>(
922 CV->getOperand(i))->getValue();
923 else {
924 Result.AggregateVal[i].IntVal =
925 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
926 }
927 break;
928 }
929 if(CDV)
930 for (unsigned i = 0; i < elemNum; ++i)
931 Result.AggregateVal[i].IntVal = APInt(
932 CDV->getElementType()->getPrimitiveSizeInBits(),
933 CDV->getElementAsInteger(i));
934
935 break;
936 }
937 llvm_unreachable("Unknown constant pointer type!");
938 }
939 break;
940
Chris Lattner996fe012002-12-24 00:01:05 +0000941 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000942 SmallString<256> Msg;
943 raw_svector_ostream OS(Msg);
944 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
945 report_fatal_error(OS.str());
Chris Lattner996fe012002-12-24 00:01:05 +0000946 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000947
Chris Lattner996fe012002-12-24 00:01:05 +0000948 return Result;
949}
950
Duncan Sands1202d1b2007-12-14 19:38:31 +0000951/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
952/// with the integer held in IntVal.
953static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
954 unsigned StoreBytes) {
955 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
Roman Divackyad06cee2012-09-05 22:26:57 +0000956 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
Duncan Sands1202d1b2007-12-14 19:38:31 +0000957
Rafael Espindola41cb64f2013-04-15 14:44:24 +0000958 if (sys::IsLittleEndianHost) {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000959 // Little-endian host - the source is ordered from LSB to MSB. Order the
960 // destination from LSB to MSB: Do a straight copy.
961 memcpy(Dst, Src, StoreBytes);
Daniel Dunbar868e3f02010-11-13 02:48:57 +0000962 } else {
Duncan Sands1202d1b2007-12-14 19:38:31 +0000963 // Big-endian host - the source is an array of 64 bit words ordered from
964 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
965 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
966 while (StoreBytes > sizeof(uint64_t)) {
967 StoreBytes -= sizeof(uint64_t);
968 // May not be aligned so use memcpy.
969 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
970 Src += sizeof(uint64_t);
971 }
972
973 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
974 }
975}
976
Evan Cheng09053e62008-11-04 06:10:31 +0000977void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
Chris Lattner229907c2011-07-18 04:54:35 +0000978 GenericValue *Ptr, Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000979 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands1202d1b2007-12-14 19:38:31 +0000980
Reid Spencer87aa65f2007-03-06 03:04:04 +0000981 switch (Ty->getTypeID()) {
Nadav Rotembe79a7a2013-04-01 15:53:30 +0000982 default:
983 dbgs() << "Cannot store value of type " << *Ty << "!\n";
984 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +0000985 case Type::IntegerTyID:
986 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
Reid Spencer87aa65f2007-03-06 03:04:04 +0000987 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +0000988 case Type::FloatTyID:
989 *((float*)Ptr) = Val.FloatVal;
990 break;
991 case Type::DoubleTyID:
992 *((double*)Ptr) = Val.DoubleVal;
993 break;
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +0000994 case Type::X86_FP80TyID:
995 memcpy(Ptr, Val.IntVal.getRawData(), 10);
996 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +0000997 case Type::PointerTyID:
998 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
999 if (StoreBytes != sizeof(PointerTy))
Chandler Carruth93da3c82011-04-28 08:37:18 +00001000 memset(&(Ptr->PointerVal), 0, StoreBytes);
Duncan Sands1202d1b2007-12-14 19:38:31 +00001001
Reid Spencer87aa65f2007-03-06 03:04:04 +00001002 *((PointerTy*)Ptr) = Val.PointerVal;
1003 break;
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001004 case Type::VectorTyID:
1005 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1006 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1007 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1008 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1009 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1010 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1011 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1012 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1013 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1014 }
1015 }
1016 break;
Chris Lattner996fe012002-12-24 00:01:05 +00001017 }
Duncan Sands1202d1b2007-12-14 19:38:31 +00001018
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001019 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
Duncan Sands1202d1b2007-12-14 19:38:31 +00001020 // Host and target are different endian - reverse the stored bytes.
1021 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1022}
1023
1024/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1025/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1026static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1027 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
David Greene82b63572013-01-14 21:04:45 +00001028 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1029 const_cast<uint64_t *>(IntVal.getRawData()));
Duncan Sands1202d1b2007-12-14 19:38:31 +00001030
Rafael Espindola41cb64f2013-04-15 14:44:24 +00001031 if (sys::IsLittleEndianHost)
Duncan Sands1202d1b2007-12-14 19:38:31 +00001032 // Little-endian host - the destination must be ordered from LSB to MSB.
1033 // The source is ordered from LSB to MSB: Do a straight copy.
1034 memcpy(Dst, Src, LoadBytes);
1035 else {
1036 // Big-endian - the destination is an array of 64 bit words ordered from
1037 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1038 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1039 // a word.
1040 while (LoadBytes > sizeof(uint64_t)) {
1041 LoadBytes -= sizeof(uint64_t);
1042 // May not be aligned so use memcpy.
1043 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1044 Dst += sizeof(uint64_t);
1045 }
1046
1047 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1048 }
Chris Lattner996fe012002-12-24 00:01:05 +00001049}
1050
Misha Brukman857c21b2003-10-10 17:45:12 +00001051/// FIXME: document
1052///
Duncan Sands1202d1b2007-12-14 19:38:31 +00001053void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
Duncan Sandscd4a6be2008-03-10 16:38:37 +00001054 GenericValue *Ptr,
Chris Lattner229907c2011-07-18 04:54:35 +00001055 Type *Ty) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001056 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
Duncan Sands5c65cb42007-12-10 17:43:13 +00001057
Duncan Sands1202d1b2007-12-14 19:38:31 +00001058 switch (Ty->getTypeID()) {
1059 case Type::IntegerTyID:
1060 // An APInt with all words initially zero.
1061 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1062 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1063 break;
Reid Spencer87aa65f2007-03-06 03:04:04 +00001064 case Type::FloatTyID:
1065 Result.FloatVal = *((float*)Ptr);
1066 break;
1067 case Type::DoubleTyID:
Duncan Sands1202d1b2007-12-14 19:38:31 +00001068 Result.DoubleVal = *((double*)Ptr);
Reid Spencer87aa65f2007-03-06 03:04:04 +00001069 break;
Duncan Sands1202d1b2007-12-14 19:38:31 +00001070 case Type::PointerTyID:
Reid Spencer87aa65f2007-03-06 03:04:04 +00001071 Result.PointerVal = *((PointerTy*)Ptr);
1072 break;
Dale Johannesena1336cf2007-09-17 18:44:13 +00001073 case Type::X86_FP80TyID: {
1074 // This is endian dependent, but it will only work on x86 anyway.
Duncan Sands26d65392007-12-15 17:37:40 +00001075 // FIXME: Will not trap if loading a signaling NaN.
Dale Johannesen4d7e4ee2009-03-24 18:16:17 +00001076 uint64_t y[2];
1077 memcpy(y, Ptr, 10);
Jeffrey Yasskin7a162882011-07-18 21:45:40 +00001078 Result.IntVal = APInt(80, y);
Dale Johannesena1336cf2007-09-17 18:44:13 +00001079 break;
1080 }
Nadav Rotembe79a7a2013-04-01 15:53:30 +00001081 case Type::VectorTyID: {
1082 const VectorType *VT = cast<VectorType>(Ty);
1083 const Type *ElemT = VT->getElementType();
1084 const unsigned numElems = VT->getNumElements();
1085 if (ElemT->isFloatTy()) {
1086 Result.AggregateVal.resize(numElems);
1087 for (unsigned i = 0; i < numElems; ++i)
1088 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1089 }
1090 if (ElemT->isDoubleTy()) {
1091 Result.AggregateVal.resize(numElems);
1092 for (unsigned i = 0; i < numElems; ++i)
1093 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1094 }
1095 if (ElemT->isIntegerTy()) {
1096 GenericValue intZero;
1097 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1098 intZero.IntVal = APInt(elemBitWidth, 0);
1099 Result.AggregateVal.resize(numElems, intZero);
1100 for (unsigned i = 0; i < numElems; ++i)
1101 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1102 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1103 }
1104 break;
1105 }
Reid Spencer87aa65f2007-03-06 03:04:04 +00001106 default:
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001107 SmallString<256> Msg;
1108 raw_svector_ostream OS(Msg);
1109 OS << "Cannot load value of type " << *Ty << "!";
1110 report_fatal_error(OS.str());
Chris Lattner7f389e82003-05-08 16:52:16 +00001111 }
Chris Lattner7f389e82003-05-08 16:52:16 +00001112}
1113
Chris Lattner996fe012002-12-24 00:01:05 +00001114void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
David Greene0967d2d2010-01-05 01:27:39 +00001115 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
Dale Johannesenb086d382008-08-07 01:30:15 +00001116 DEBUG(Init->dump());
Chris Lattner00245f42012-01-24 13:41:11 +00001117 if (isa<UndefValue>(Init))
Chris Lattner61753bf2004-10-16 18:19:26 +00001118 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001119
1120 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
Robert Bocchino69d62132006-01-20 18:18:40 +00001121 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001122 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
Robert Bocchino69d62132006-01-20 18:18:40 +00001123 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1124 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1125 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001126 }
1127
1128 if (isa<ConstantAggregateZero>(Init)) {
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001129 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
Chris Lattner1dd86b12008-02-15 00:57:28 +00001130 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001131 }
1132
1133 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001134 unsigned ElementSize =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001135 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001136 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1137 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1138 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001139 }
1140
1141 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001142 const StructLayout *SL =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001143 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
Dan Gohman69ddfbf2008-05-20 03:20:09 +00001144 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1145 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1146 return;
Chris Lattner00245f42012-01-24 13:41:11 +00001147 }
1148
1149 if (const ConstantDataSequential *CDS =
1150 dyn_cast<ConstantDataSequential>(Init)) {
1151 // CDS is already laid out in host memory order.
1152 StringRef Data = CDS->getRawDataValues();
1153 memcpy(Addr, Data.data(), Data.size());
1154 return;
1155 }
1156
1157 if (Init->getType()->isFirstClassType()) {
Chris Lattner996fe012002-12-24 00:01:05 +00001158 GenericValue Val = getConstantValue(Init);
1159 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1160 return;
1161 }
1162
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001163 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
Torok Edwinfbcc6632009-07-14 16:55:14 +00001164 llvm_unreachable("Unknown constant type to initialize memory with!");
Chris Lattner996fe012002-12-24 00:01:05 +00001165}
1166
Chris Lattner996fe012002-12-24 00:01:05 +00001167/// EmitGlobals - Emit all of the global variables to memory, storing their
1168/// addresses into GlobalAddress. This must make sure to copy the contents of
1169/// their initializers into the memory.
Chris Lattner996fe012002-12-24 00:01:05 +00001170void ExecutionEngine::emitGlobals() {
Chris Lattner996fe012002-12-24 00:01:05 +00001171 // Loop over all of the global variables in the program, allocating the memory
Chris Lattner0621cae2006-08-16 01:24:12 +00001172 // to hold them. If there is more than one module, do a prepass over globals
1173 // to figure out how the different modules should link together.
Chris Lattner229907c2011-07-18 04:54:35 +00001174 std::map<std::pair<std::string, Type*>,
Chris Lattner0621cae2006-08-16 01:24:12 +00001175 const GlobalValue*> LinkedGlobalsMap;
Misha Brukman835702a2005-04-21 22:36:52 +00001176
Chris Lattner0621cae2006-08-16 01:24:12 +00001177 if (Modules.size() != 1) {
1178 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001179 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001180 for (const auto &GV : M.globals()) {
1181 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1182 GV.hasAppendingLinkage() || !GV.hasName())
Chris Lattner0621cae2006-08-16 01:24:12 +00001183 continue;// Ignore external globals and globals with internal linkage.
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001184
1185 const GlobalValue *&GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001186 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
Chris Lattner0621cae2006-08-16 01:24:12 +00001187
1188 // If this is the first time we've seen this global, it is the canonical
1189 // version.
1190 if (!GVEntry) {
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001191 GVEntry = &GV;
Chris Lattner0621cae2006-08-16 01:24:12 +00001192 continue;
1193 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001194
Chris Lattner0621cae2006-08-16 01:24:12 +00001195 // If the existing global is strong, never replace it.
Nico Rieck7157bb72014-01-14 15:22:47 +00001196 if (GVEntry->hasExternalLinkage())
Chris Lattner0621cae2006-08-16 01:24:12 +00001197 continue;
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001198
Chris Lattner0621cae2006-08-16 01:24:12 +00001199 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
Dale Johannesence4396b2008-05-14 20:12:51 +00001200 // symbol. FIXME is this right for common?
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001201 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1202 GVEntry = &GV;
Chris Lattner9de0d142003-04-23 19:01:49 +00001203 }
Chris Lattner996fe012002-12-24 00:01:05 +00001204 }
Chris Lattner0621cae2006-08-16 01:24:12 +00001205 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001206
Chris Lattner0621cae2006-08-16 01:24:12 +00001207 std::vector<const GlobalValue*> NonCanonicalGlobals;
1208 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001209 Module &M = *Modules[m];
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001210 for (const auto &GV : M.globals()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001211 // In the multi-module case, see what this global maps to.
1212 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001213 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001214 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001215 // If something else is the canonical global, ignore this one.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001216 if (GVEntry != &GV) {
1217 NonCanonicalGlobals.push_back(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001218 continue;
1219 }
1220 }
1221 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001222
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001223 if (!GV.isDeclaration()) {
1224 addGlobalMapping(&GV, getMemoryForGV(&GV));
Chris Lattner0621cae2006-08-16 01:24:12 +00001225 } else {
1226 // External variable reference. Try to use the dynamic loader to
1227 // get a pointer to it.
1228 if (void *SymAddr =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001229 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1230 addGlobalMapping(&GV, SymAddr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001231 else {
Chris Lattner2104b8d2010-04-07 22:58:41 +00001232 report_fatal_error("Could not resolve external global address: "
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001233 +GV.getName());
Chris Lattner0621cae2006-08-16 01:24:12 +00001234 }
1235 }
1236 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001237
Chris Lattner0621cae2006-08-16 01:24:12 +00001238 // If there are multiple modules, map the non-canonical globals to their
1239 // canonical location.
1240 if (!NonCanonicalGlobals.empty()) {
1241 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1242 const GlobalValue *GV = NonCanonicalGlobals[i];
1243 const GlobalValue *CGV =
1244 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1245 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1246 assert(Ptr && "Canonical global wasn't codegen'd!");
Nuno Lopesa67f06b2008-10-14 10:04:52 +00001247 addGlobalMapping(GV, Ptr);
Chris Lattner0621cae2006-08-16 01:24:12 +00001248 }
1249 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001250
1251 // Now that all of the globals are set up in memory, loop through them all
Reid Spencer7a9c62b2007-01-12 07:05:14 +00001252 // and initialize their contents.
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001253 for (const auto &GV : M.globals()) {
1254 if (!GV.isDeclaration()) {
Chris Lattner0621cae2006-08-16 01:24:12 +00001255 if (!LinkedGlobalsMap.empty()) {
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001256 if (const GlobalValue *GVEntry =
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001257 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1258 if (GVEntry != &GV) // Not the canonical variable.
Chris Lattner0621cae2006-08-16 01:24:12 +00001259 continue;
1260 }
Rafael Espindola49bb65a2014-05-08 18:17:44 +00001261 EmitGlobalVariable(&GV);
Chris Lattner0621cae2006-08-16 01:24:12 +00001262 }
1263 }
1264 }
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001265}
1266
1267// EmitGlobalVariable - This method emits the specified global variable to the
1268// address specified in GlobalAddresses, or allocates new memory if it's not
1269// already in the map.
Chris Lattnerfbcc0aa2003-12-20 03:36:47 +00001270void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
Chris Lattner748e8572003-12-31 20:21:04 +00001271 void *GA = getPointerToGlobalIfAvailable(GV);
Chris Lattnerdc631732004-02-08 19:33:23 +00001272
Craig Topper2617dcc2014-04-15 06:32:26 +00001273 if (!GA) {
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001274 // If it's not already specified, allocate memory for the global.
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001275 GA = getMemoryForGV(GV);
Andrew Kaylor3b442372013-11-15 17:52:54 +00001276
1277 // If we failed to allocate memory for this global, return.
Craig Topper2617dcc2014-04-15 06:32:26 +00001278 if (!GA) return;
Andrew Kaylor3b442372013-11-15 17:52:54 +00001279
Chris Lattner748e8572003-12-31 20:21:04 +00001280 addGlobalMapping(GV, GA);
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001281 }
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001282
Nicolas Geoffray5457ce92008-10-25 15:41:43 +00001283 // Don't initialize if it's thread local, let the client do it.
1284 if (!GV->isThreadLocal())
1285 InitializeMemory(GV->getInitializer(), GA);
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001286
Chris Lattner229907c2011-07-18 04:54:35 +00001287 Type *ElTy = GV->getType()->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00001288 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
Chris Lattnerdf1f1522005-01-08 20:13:19 +00001289 NumInitBytes += (unsigned)GVSize;
Chris Lattner6bbe3ec2003-12-20 02:45:37 +00001290 ++NumGlobals;
Chris Lattner996fe012002-12-24 00:01:05 +00001291}
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001292
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001293ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1294 : EE(EE), GlobalAddressMap(this) {
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001295}
1296
Zachary Turnerc04b8922014-06-20 21:07:14 +00001297sys::Mutex *
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001298ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001299 return &EES->EE.lock;
1300}
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001301
1302void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1303 const GlobalValue *Old) {
Jeffrey Yasskind0fc8f82009-10-23 22:37:43 +00001304 void *OldVal = EES->GlobalAddressMap.lookup(Old);
1305 EES->GlobalAddressReverseMap.erase(OldVal);
1306}
1307
Daniel Dunbar868e3f02010-11-13 02:48:57 +00001308void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1309 const GlobalValue *,
1310 const GlobalValue *) {
Craig Toppera2886c22012-02-07 05:05:23 +00001311 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1312 " RAUW on a value it has a global mapping for.");
Jeffrey Yasskinf98e9812009-10-13 17:42:08 +00001313}