• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the common interface used by the various execution engine
11 // subclasses.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/ExecutionEngine/JITEventListener.h"
21 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/ValueHandle.h"
29 #include "llvm/Object/Archive.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/DynamicLibrary.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/Host.h"
35 #include "llvm/Support/MutexGuard.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include <cmath>
40 #include <cstring>
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "jit"
44 
45 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
46 STATISTIC(NumGlobals  , "Number of global vars initialized");
47 
48 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
49     std::unique_ptr<Module> M, std::string *ErrorStr,
50     std::shared_ptr<MCJITMemoryManager> MemMgr,
51     std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
52     std::unique_ptr<TargetMachine> TM) = nullptr;
53 
54 ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
55   std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MemMgr,
56   std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
57   std::unique_ptr<TargetMachine> TM) = nullptr;
58 
59 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
60                                                 std::string *ErrorStr) =nullptr;
61 
anchor()62 void JITEventListener::anchor() {}
63 
Init(std::unique_ptr<Module> M)64 void ExecutionEngine::Init(std::unique_ptr<Module> M) {
65   CompilingLazily         = false;
66   GVCompilationDisabled   = false;
67   SymbolSearchingDisabled = false;
68 
69   // IR module verification is enabled by default in debug builds, and disabled
70   // by default in release builds.
71 #ifndef NDEBUG
72   VerifyModules = true;
73 #else
74   VerifyModules = false;
75 #endif
76 
77   assert(M && "Module is null?");
78   Modules.push_back(std::move(M));
79 }
80 
ExecutionEngine(std::unique_ptr<Module> M)81 ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
82     : DL(M->getDataLayout()), LazyFunctionCreator(nullptr) {
83   Init(std::move(M));
84 }
85 
ExecutionEngine(DataLayout DL,std::unique_ptr<Module> M)86 ExecutionEngine::ExecutionEngine(DataLayout DL, std::unique_ptr<Module> M)
87     : DL(std::move(DL)), LazyFunctionCreator(nullptr) {
88   Init(std::move(M));
89 }
90 
~ExecutionEngine()91 ExecutionEngine::~ExecutionEngine() {
92   clearAllGlobalMappings();
93 }
94 
95 namespace {
96 /// \brief Helper class which uses a value handler to automatically deletes the
97 /// memory block when the GlobalVariable is destroyed.
98 class GVMemoryBlock final : public CallbackVH {
GVMemoryBlock(const GlobalVariable * GV)99   GVMemoryBlock(const GlobalVariable *GV)
100     : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
101 
102 public:
103   /// \brief Returns the address the GlobalVariable should be written into.  The
104   /// GVMemoryBlock object prefixes that.
Create(const GlobalVariable * GV,const DataLayout & TD)105   static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
106     Type *ElTy = GV->getType()->getElementType();
107     size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
108     void *RawMemory = ::operator new(
109       RoundUpToAlignment(sizeof(GVMemoryBlock),
110                          TD.getPreferredAlignment(GV))
111       + GVSize);
112     new(RawMemory) GVMemoryBlock(GV);
113     return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
114   }
115 
deleted()116   void deleted() override {
117     // We allocated with operator new and with some extra memory hanging off the
118     // end, so don't just delete this.  I'm not sure if this is actually
119     // required.
120     this->~GVMemoryBlock();
121     ::operator delete(this);
122   }
123 };
124 }  // anonymous namespace
125 
getMemoryForGV(const GlobalVariable * GV)126 char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
127   return GVMemoryBlock::Create(GV, getDataLayout());
128 }
129 
addObjectFile(std::unique_ptr<object::ObjectFile> O)130 void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
131   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
132 }
133 
134 void
addObjectFile(object::OwningBinary<object::ObjectFile> O)135 ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
136   llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
137 }
138 
addArchive(object::OwningBinary<object::Archive> A)139 void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
140   llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
141 }
142 
removeModule(Module * M)143 bool ExecutionEngine::removeModule(Module *M) {
144   for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
145     Module *Found = I->get();
146     if (Found == M) {
147       I->release();
148       Modules.erase(I);
149       clearGlobalMappingsFromModule(M);
150       return true;
151     }
152   }
153   return false;
154 }
155 
FindFunctionNamed(const char * FnName)156 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
157   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
158     Function *F = Modules[i]->getFunction(FnName);
159     if (F && !F->isDeclaration())
160       return F;
161   }
162   return nullptr;
163 }
164 
FindGlobalVariableNamed(const char * Name,bool AllowInternal)165 GlobalVariable *ExecutionEngine::FindGlobalVariableNamed(const char *Name, bool AllowInternal) {
166   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
167     GlobalVariable *GV = Modules[i]->getGlobalVariable(Name,AllowInternal);
168     if (GV && !GV->isDeclaration())
169       return GV;
170   }
171   return nullptr;
172 }
173 
RemoveMapping(StringRef Name)174 uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) {
175   GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
176   uint64_t OldVal;
177 
178   // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
179   // GlobalAddressMap.
180   if (I == GlobalAddressMap.end())
181     OldVal = 0;
182   else {
183     GlobalAddressReverseMap.erase(I->second);
184     OldVal = I->second;
185     GlobalAddressMap.erase(I);
186   }
187 
188   return OldVal;
189 }
190 
getMangledName(const GlobalValue * GV)191 std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
192   assert(GV->hasName() && "Global must have name.");
193 
194   MutexGuard locked(lock);
195   SmallString<128> FullName;
196 
197   const DataLayout &DL =
198     GV->getParent()->getDataLayout().isDefault()
199       ? getDataLayout()
200       : GV->getParent()->getDataLayout();
201 
202   Mangler::getNameWithPrefix(FullName, GV->getName(), DL);
203   return FullName.str();
204 }
205 
addGlobalMapping(const GlobalValue * GV,void * Addr)206 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
207   MutexGuard locked(lock);
208   addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
209 }
210 
addGlobalMapping(StringRef Name,uint64_t Addr)211 void ExecutionEngine::addGlobalMapping(StringRef Name, uint64_t Addr) {
212   MutexGuard locked(lock);
213 
214   assert(!Name.empty() && "Empty GlobalMapping symbol name!");
215 
216   DEBUG(dbgs() << "JIT: Map \'" << Name  << "\' to [" << Addr << "]\n";);
217   uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
218   assert((!CurVal || !Addr) && "GlobalMapping already established!");
219   CurVal = Addr;
220 
221   // If we are using the reverse mapping, add it too.
222   if (!EEState.getGlobalAddressReverseMap().empty()) {
223     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
224     assert((!V.empty() || !Name.empty()) &&
225            "GlobalMapping already established!");
226     V = Name;
227   }
228 }
229 
clearAllGlobalMappings()230 void ExecutionEngine::clearAllGlobalMappings() {
231   MutexGuard locked(lock);
232 
233   EEState.getGlobalAddressMap().clear();
234   EEState.getGlobalAddressReverseMap().clear();
235 }
236 
clearGlobalMappingsFromModule(Module * M)237 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
238   MutexGuard locked(lock);
239 
240   for (Function &FI : *M)
241     EEState.RemoveMapping(getMangledName(&FI));
242   for (GlobalVariable &GI : M->globals())
243     EEState.RemoveMapping(getMangledName(&GI));
244 }
245 
updateGlobalMapping(const GlobalValue * GV,void * Addr)246 uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
247                                               void *Addr) {
248   MutexGuard locked(lock);
249   return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
250 }
251 
updateGlobalMapping(StringRef Name,uint64_t Addr)252 uint64_t ExecutionEngine::updateGlobalMapping(StringRef Name, uint64_t Addr) {
253   MutexGuard locked(lock);
254 
255   ExecutionEngineState::GlobalAddressMapTy &Map =
256     EEState.getGlobalAddressMap();
257 
258   // Deleting from the mapping?
259   if (!Addr)
260     return EEState.RemoveMapping(Name);
261 
262   uint64_t &CurVal = Map[Name];
263   uint64_t OldVal = CurVal;
264 
265   if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
266     EEState.getGlobalAddressReverseMap().erase(CurVal);
267   CurVal = Addr;
268 
269   // If we are using the reverse mapping, add it too.
270   if (!EEState.getGlobalAddressReverseMap().empty()) {
271     std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
272     assert((!V.empty() || !Name.empty()) &&
273            "GlobalMapping already established!");
274     V = Name;
275   }
276   return OldVal;
277 }
278 
getAddressToGlobalIfAvailable(StringRef S)279 uint64_t ExecutionEngine::getAddressToGlobalIfAvailable(StringRef S) {
280   MutexGuard locked(lock);
281   uint64_t Address = 0;
282   ExecutionEngineState::GlobalAddressMapTy::iterator I =
283     EEState.getGlobalAddressMap().find(S);
284   if (I != EEState.getGlobalAddressMap().end())
285     Address = I->second;
286   return Address;
287 }
288 
289 
getPointerToGlobalIfAvailable(StringRef S)290 void *ExecutionEngine::getPointerToGlobalIfAvailable(StringRef S) {
291   MutexGuard locked(lock);
292   if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
293     return Address;
294   return nullptr;
295 }
296 
getPointerToGlobalIfAvailable(const GlobalValue * GV)297 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
298   MutexGuard locked(lock);
299   return getPointerToGlobalIfAvailable(getMangledName(GV));
300 }
301 
getGlobalValueAtAddress(void * Addr)302 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
303   MutexGuard locked(lock);
304 
305   // If we haven't computed the reverse mapping yet, do so first.
306   if (EEState.getGlobalAddressReverseMap().empty()) {
307     for (ExecutionEngineState::GlobalAddressMapTy::iterator
308            I = EEState.getGlobalAddressMap().begin(),
309            E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
310       StringRef Name = I->first();
311       uint64_t Addr = I->second;
312       EEState.getGlobalAddressReverseMap().insert(std::make_pair(
313                                                           Addr, Name));
314     }
315   }
316 
317   std::map<uint64_t, std::string>::iterator I =
318     EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
319 
320   if (I != EEState.getGlobalAddressReverseMap().end()) {
321     StringRef Name = I->second;
322     for (unsigned i = 0, e = Modules.size(); i != e; ++i)
323       if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
324         return GV;
325   }
326   return nullptr;
327 }
328 
329 namespace {
330 class ArgvArray {
331   std::unique_ptr<char[]> Array;
332   std::vector<std::unique_ptr<char[]>> Values;
333 public:
334   /// Turn a vector of strings into a nice argv style array of pointers to null
335   /// terminated strings.
336   void *reset(LLVMContext &C, ExecutionEngine *EE,
337               const std::vector<std::string> &InputArgv);
338 };
339 }  // anonymous namespace
reset(LLVMContext & C,ExecutionEngine * EE,const std::vector<std::string> & InputArgv)340 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
341                        const std::vector<std::string> &InputArgv) {
342   Values.clear();  // Free the old contents.
343   Values.reserve(InputArgv.size());
344   unsigned PtrSize = EE->getDataLayout().getPointerSize();
345   Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
346 
347   DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
348   Type *SBytePtr = Type::getInt8PtrTy(C);
349 
350   for (unsigned i = 0; i != InputArgv.size(); ++i) {
351     unsigned Size = InputArgv[i].size()+1;
352     auto Dest = make_unique<char[]>(Size);
353     DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
354 
355     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
356     Dest[Size-1] = 0;
357 
358     // Endian safe: Array[i] = (PointerTy)Dest;
359     EE->StoreValueToMemory(PTOGV(Dest.get()),
360                            (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
361     Values.push_back(std::move(Dest));
362   }
363 
364   // Null terminate it
365   EE->StoreValueToMemory(PTOGV(nullptr),
366                          (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
367                          SBytePtr);
368   return Array.get();
369 }
370 
runStaticConstructorsDestructors(Module & module,bool isDtors)371 void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
372                                                        bool isDtors) {
373   const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
374   GlobalVariable *GV = module.getNamedGlobal(Name);
375 
376   // If this global has internal linkage, or if it has a use, then it must be
377   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
378   // this is the case, don't execute any of the global ctors, __main will do
379   // it.
380   if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
381 
382   // Should be an array of '{ i32, void ()* }' structs.  The first value is
383   // the init priority, which we ignore.
384   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
385   if (!InitList)
386     return;
387   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
388     ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
389     if (!CS) continue;
390 
391     Constant *FP = CS->getOperand(1);
392     if (FP->isNullValue())
393       continue;  // Found a sentinal value, ignore.
394 
395     // Strip off constant expression casts.
396     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
397       if (CE->isCast())
398         FP = CE->getOperand(0);
399 
400     // Execute the ctor/dtor function!
401     if (Function *F = dyn_cast<Function>(FP))
402       runFunction(F, None);
403 
404     // FIXME: It is marginally lame that we just do nothing here if we see an
405     // entry we don't recognize. It might not be unreasonable for the verifier
406     // to not even allow this and just assert here.
407   }
408 }
409 
runStaticConstructorsDestructors(bool isDtors)410 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
411   // Execute global ctors/dtors for each module in the program.
412   for (std::unique_ptr<Module> &M : Modules)
413     runStaticConstructorsDestructors(*M, isDtors);
414 }
415 
416 #ifndef NDEBUG
417 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
isTargetNullPtr(ExecutionEngine * EE,void * Loc)418 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
419   unsigned PtrSize = EE->getDataLayout().getPointerSize();
420   for (unsigned i = 0; i < PtrSize; ++i)
421     if (*(i + (uint8_t*)Loc))
422       return false;
423   return true;
424 }
425 #endif
426 
runFunctionAsMain(Function * Fn,const std::vector<std::string> & argv,const char * const * envp)427 int ExecutionEngine::runFunctionAsMain(Function *Fn,
428                                        const std::vector<std::string> &argv,
429                                        const char * const * envp) {
430   std::vector<GenericValue> GVArgs;
431   GenericValue GVArgc;
432   GVArgc.IntVal = APInt(32, argv.size());
433 
434   // Check main() type
435   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
436   FunctionType *FTy = Fn->getFunctionType();
437   Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
438 
439   // Check the argument types.
440   if (NumArgs > 3)
441     report_fatal_error("Invalid number of arguments of main() supplied");
442   if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
443     report_fatal_error("Invalid type for third argument of main() supplied");
444   if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
445     report_fatal_error("Invalid type for second argument of main() supplied");
446   if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
447     report_fatal_error("Invalid type for first argument of main() supplied");
448   if (!FTy->getReturnType()->isIntegerTy() &&
449       !FTy->getReturnType()->isVoidTy())
450     report_fatal_error("Invalid return type of main() supplied");
451 
452   ArgvArray CArgv;
453   ArgvArray CEnv;
454   if (NumArgs) {
455     GVArgs.push_back(GVArgc); // Arg #0 = argc.
456     if (NumArgs > 1) {
457       // Arg #1 = argv.
458       GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
459       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
460              "argv[0] was null after CreateArgv");
461       if (NumArgs > 2) {
462         std::vector<std::string> EnvVars;
463         for (unsigned i = 0; envp[i]; ++i)
464           EnvVars.emplace_back(envp[i]);
465         // Arg #2 = envp.
466         GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
467       }
468     }
469   }
470 
471   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
472 }
473 
EngineBuilder()474 EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
475 
EngineBuilder(std::unique_ptr<Module> M)476 EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
477     : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
478       OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
479       RelocModel(Reloc::Default), CMModel(CodeModel::JITDefault),
480       UseOrcMCJITReplacement(false) {
481 // IR module verification is enabled by default in debug builds, and disabled
482 // by default in release builds.
483 #ifndef NDEBUG
484   VerifyModules = true;
485 #else
486   VerifyModules = false;
487 #endif
488 }
489 
490 EngineBuilder::~EngineBuilder() = default;
491 
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm)492 EngineBuilder &EngineBuilder::setMCJITMemoryManager(
493                                    std::unique_ptr<RTDyldMemoryManager> mcjmm) {
494   auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
495   MemMgr = SharedMM;
496   Resolver = SharedMM;
497   return *this;
498 }
499 
500 EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM)501 EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
502   MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
503   return *this;
504 }
505 
506 EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR)507 EngineBuilder::setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR) {
508   Resolver = std::shared_ptr<RuntimeDyld::SymbolResolver>(std::move(SR));
509   return *this;
510 }
511 
create(TargetMachine * TM)512 ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
513   std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
514 
515   // Make sure we can resolve symbols in the program as well. The zero arg
516   // to the function tells DynamicLibrary to load the program, not a library.
517   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
518     return nullptr;
519 
520   // If the user specified a memory manager but didn't specify which engine to
521   // create, we assume they only want the JIT, and we fail if they only want
522   // the interpreter.
523   if (MemMgr) {
524     if (WhichEngine & EngineKind::JIT)
525       WhichEngine = EngineKind::JIT;
526     else {
527       if (ErrorStr)
528         *ErrorStr = "Cannot create an interpreter with a memory manager.";
529       return nullptr;
530     }
531   }
532 
533   // Unless the interpreter was explicitly selected or the JIT is not linked,
534   // try making a JIT.
535   if ((WhichEngine & EngineKind::JIT) && TheTM) {
536     Triple TT(M->getTargetTriple());
537     if (!TM->getTarget().hasJIT()) {
538       errs() << "WARNING: This target JIT is not designed for the host"
539              << " you are running.  If bad things happen, please choose"
540              << " a different -march switch.\n";
541     }
542 
543     ExecutionEngine *EE = nullptr;
544     if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
545       EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
546                                                     std::move(Resolver),
547                                                     std::move(TheTM));
548       EE->addModule(std::move(M));
549     } else if (ExecutionEngine::MCJITCtor)
550       EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
551                                       std::move(Resolver), std::move(TheTM));
552 
553     if (EE) {
554       EE->setVerifyModules(VerifyModules);
555       return EE;
556     }
557   }
558 
559   // If we can't make a JIT and we didn't request one specifically, try making
560   // an interpreter instead.
561   if (WhichEngine & EngineKind::Interpreter) {
562     if (ExecutionEngine::InterpCtor)
563       return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
564     if (ErrorStr)
565       *ErrorStr = "Interpreter has not been linked in.";
566     return nullptr;
567   }
568 
569   if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
570     if (ErrorStr)
571       *ErrorStr = "JIT has not been linked in.";
572   }
573 
574   return nullptr;
575 }
576 
getPointerToGlobal(const GlobalValue * GV)577 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
578   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
579     return getPointerToFunction(F);
580 
581   MutexGuard locked(lock);
582   if (void* P = getPointerToGlobalIfAvailable(GV))
583     return P;
584 
585   // Global variable might have been added since interpreter started.
586   if (GlobalVariable *GVar =
587           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
588     EmitGlobalVariable(GVar);
589   else
590     llvm_unreachable("Global hasn't had an address allocated yet!");
591 
592   return getPointerToGlobalIfAvailable(GV);
593 }
594 
595 /// \brief Converts a Constant* into a GenericValue, including handling of
596 /// ConstantExpr values.
getConstantValue(const Constant * C)597 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
598   // If its undefined, return the garbage.
599   if (isa<UndefValue>(C)) {
600     GenericValue Result;
601     switch (C->getType()->getTypeID()) {
602     default:
603       break;
604     case Type::IntegerTyID:
605     case Type::X86_FP80TyID:
606     case Type::FP128TyID:
607     case Type::PPC_FP128TyID:
608       // Although the value is undefined, we still have to construct an APInt
609       // with the correct bit width.
610       Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
611       break;
612     case Type::StructTyID: {
613       // if the whole struct is 'undef' just reserve memory for the value.
614       if(StructType *STy = dyn_cast<StructType>(C->getType())) {
615         unsigned int elemNum = STy->getNumElements();
616         Result.AggregateVal.resize(elemNum);
617         for (unsigned int i = 0; i < elemNum; ++i) {
618           Type *ElemTy = STy->getElementType(i);
619           if (ElemTy->isIntegerTy())
620             Result.AggregateVal[i].IntVal =
621               APInt(ElemTy->getPrimitiveSizeInBits(), 0);
622           else if (ElemTy->isAggregateType()) {
623               const Constant *ElemUndef = UndefValue::get(ElemTy);
624               Result.AggregateVal[i] = getConstantValue(ElemUndef);
625             }
626           }
627         }
628       }
629       break;
630     case Type::VectorTyID:
631       // if the whole vector is 'undef' just reserve memory for the value.
632       auto* VTy = dyn_cast<VectorType>(C->getType());
633       Type *ElemTy = VTy->getElementType();
634       unsigned int elemNum = VTy->getNumElements();
635       Result.AggregateVal.resize(elemNum);
636       if (ElemTy->isIntegerTy())
637         for (unsigned int i = 0; i < elemNum; ++i)
638           Result.AggregateVal[i].IntVal =
639             APInt(ElemTy->getPrimitiveSizeInBits(), 0);
640       break;
641     }
642     return Result;
643   }
644 
645   // Otherwise, if the value is a ConstantExpr...
646   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
647     Constant *Op0 = CE->getOperand(0);
648     switch (CE->getOpcode()) {
649     case Instruction::GetElementPtr: {
650       // Compute the index
651       GenericValue Result = getConstantValue(Op0);
652       APInt Offset(DL.getPointerSizeInBits(), 0);
653       cast<GEPOperator>(CE)->accumulateConstantOffset(DL, Offset);
654 
655       char* tmp = (char*) Result.PointerVal;
656       Result = PTOGV(tmp + Offset.getSExtValue());
657       return Result;
658     }
659     case Instruction::Trunc: {
660       GenericValue GV = getConstantValue(Op0);
661       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
662       GV.IntVal = GV.IntVal.trunc(BitWidth);
663       return GV;
664     }
665     case Instruction::ZExt: {
666       GenericValue GV = getConstantValue(Op0);
667       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
668       GV.IntVal = GV.IntVal.zext(BitWidth);
669       return GV;
670     }
671     case Instruction::SExt: {
672       GenericValue GV = getConstantValue(Op0);
673       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
674       GV.IntVal = GV.IntVal.sext(BitWidth);
675       return GV;
676     }
677     case Instruction::FPTrunc: {
678       // FIXME long double
679       GenericValue GV = getConstantValue(Op0);
680       GV.FloatVal = float(GV.DoubleVal);
681       return GV;
682     }
683     case Instruction::FPExt:{
684       // FIXME long double
685       GenericValue GV = getConstantValue(Op0);
686       GV.DoubleVal = double(GV.FloatVal);
687       return GV;
688     }
689     case Instruction::UIToFP: {
690       GenericValue GV = getConstantValue(Op0);
691       if (CE->getType()->isFloatTy())
692         GV.FloatVal = float(GV.IntVal.roundToDouble());
693       else if (CE->getType()->isDoubleTy())
694         GV.DoubleVal = GV.IntVal.roundToDouble();
695       else if (CE->getType()->isX86_FP80Ty()) {
696         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
697         (void)apf.convertFromAPInt(GV.IntVal,
698                                    false,
699                                    APFloat::rmNearestTiesToEven);
700         GV.IntVal = apf.bitcastToAPInt();
701       }
702       return GV;
703     }
704     case Instruction::SIToFP: {
705       GenericValue GV = getConstantValue(Op0);
706       if (CE->getType()->isFloatTy())
707         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
708       else if (CE->getType()->isDoubleTy())
709         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
710       else if (CE->getType()->isX86_FP80Ty()) {
711         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
712         (void)apf.convertFromAPInt(GV.IntVal,
713                                    true,
714                                    APFloat::rmNearestTiesToEven);
715         GV.IntVal = apf.bitcastToAPInt();
716       }
717       return GV;
718     }
719     case Instruction::FPToUI: // double->APInt conversion handles sign
720     case Instruction::FPToSI: {
721       GenericValue GV = getConstantValue(Op0);
722       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
723       if (Op0->getType()->isFloatTy())
724         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
725       else if (Op0->getType()->isDoubleTy())
726         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
727       else if (Op0->getType()->isX86_FP80Ty()) {
728         APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
729         uint64_t v;
730         bool ignored;
731         (void)apf.convertToInteger(&v, BitWidth,
732                                    CE->getOpcode()==Instruction::FPToSI,
733                                    APFloat::rmTowardZero, &ignored);
734         GV.IntVal = v; // endian?
735       }
736       return GV;
737     }
738     case Instruction::PtrToInt: {
739       GenericValue GV = getConstantValue(Op0);
740       uint32_t PtrWidth = DL.getTypeSizeInBits(Op0->getType());
741       assert(PtrWidth <= 64 && "Bad pointer width");
742       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
743       uint32_t IntWidth = DL.getTypeSizeInBits(CE->getType());
744       GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
745       return GV;
746     }
747     case Instruction::IntToPtr: {
748       GenericValue GV = getConstantValue(Op0);
749       uint32_t PtrWidth = DL.getTypeSizeInBits(CE->getType());
750       GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
751       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
752       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
753       return GV;
754     }
755     case Instruction::BitCast: {
756       GenericValue GV = getConstantValue(Op0);
757       Type* DestTy = CE->getType();
758       switch (Op0->getType()->getTypeID()) {
759         default: llvm_unreachable("Invalid bitcast operand");
760         case Type::IntegerTyID:
761           assert(DestTy->isFloatingPointTy() && "invalid bitcast");
762           if (DestTy->isFloatTy())
763             GV.FloatVal = GV.IntVal.bitsToFloat();
764           else if (DestTy->isDoubleTy())
765             GV.DoubleVal = GV.IntVal.bitsToDouble();
766           break;
767         case Type::FloatTyID:
768           assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
769           GV.IntVal = APInt::floatToBits(GV.FloatVal);
770           break;
771         case Type::DoubleTyID:
772           assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
773           GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
774           break;
775         case Type::PointerTyID:
776           assert(DestTy->isPointerTy() && "Invalid bitcast");
777           break; // getConstantValue(Op0)  above already converted it
778       }
779       return GV;
780     }
781     case Instruction::Add:
782     case Instruction::FAdd:
783     case Instruction::Sub:
784     case Instruction::FSub:
785     case Instruction::Mul:
786     case Instruction::FMul:
787     case Instruction::UDiv:
788     case Instruction::SDiv:
789     case Instruction::URem:
790     case Instruction::SRem:
791     case Instruction::And:
792     case Instruction::Or:
793     case Instruction::Xor: {
794       GenericValue LHS = getConstantValue(Op0);
795       GenericValue RHS = getConstantValue(CE->getOperand(1));
796       GenericValue GV;
797       switch (CE->getOperand(0)->getType()->getTypeID()) {
798       default: llvm_unreachable("Bad add type!");
799       case Type::IntegerTyID:
800         switch (CE->getOpcode()) {
801           default: llvm_unreachable("Invalid integer opcode");
802           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
803           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
804           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
805           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
806           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
807           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
808           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
809           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
810           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
811           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
812         }
813         break;
814       case Type::FloatTyID:
815         switch (CE->getOpcode()) {
816           default: llvm_unreachable("Invalid float opcode");
817           case Instruction::FAdd:
818             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
819           case Instruction::FSub:
820             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
821           case Instruction::FMul:
822             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
823           case Instruction::FDiv:
824             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
825           case Instruction::FRem:
826             GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
827         }
828         break;
829       case Type::DoubleTyID:
830         switch (CE->getOpcode()) {
831           default: llvm_unreachable("Invalid double opcode");
832           case Instruction::FAdd:
833             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
834           case Instruction::FSub:
835             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
836           case Instruction::FMul:
837             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
838           case Instruction::FDiv:
839             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
840           case Instruction::FRem:
841             GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
842         }
843         break;
844       case Type::X86_FP80TyID:
845       case Type::PPC_FP128TyID:
846       case Type::FP128TyID: {
847         const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
848         APFloat apfLHS = APFloat(Sem, LHS.IntVal);
849         switch (CE->getOpcode()) {
850           default: llvm_unreachable("Invalid long double opcode");
851           case Instruction::FAdd:
852             apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
853             GV.IntVal = apfLHS.bitcastToAPInt();
854             break;
855           case Instruction::FSub:
856             apfLHS.subtract(APFloat(Sem, RHS.IntVal),
857                             APFloat::rmNearestTiesToEven);
858             GV.IntVal = apfLHS.bitcastToAPInt();
859             break;
860           case Instruction::FMul:
861             apfLHS.multiply(APFloat(Sem, RHS.IntVal),
862                             APFloat::rmNearestTiesToEven);
863             GV.IntVal = apfLHS.bitcastToAPInt();
864             break;
865           case Instruction::FDiv:
866             apfLHS.divide(APFloat(Sem, RHS.IntVal),
867                           APFloat::rmNearestTiesToEven);
868             GV.IntVal = apfLHS.bitcastToAPInt();
869             break;
870           case Instruction::FRem:
871             apfLHS.mod(APFloat(Sem, RHS.IntVal));
872             GV.IntVal = apfLHS.bitcastToAPInt();
873             break;
874           }
875         }
876         break;
877       }
878       return GV;
879     }
880     default:
881       break;
882     }
883 
884     SmallString<256> Msg;
885     raw_svector_ostream OS(Msg);
886     OS << "ConstantExpr not handled: " << *CE;
887     report_fatal_error(OS.str());
888   }
889 
890   // Otherwise, we have a simple constant.
891   GenericValue Result;
892   switch (C->getType()->getTypeID()) {
893   case Type::FloatTyID:
894     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
895     break;
896   case Type::DoubleTyID:
897     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
898     break;
899   case Type::X86_FP80TyID:
900   case Type::FP128TyID:
901   case Type::PPC_FP128TyID:
902     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
903     break;
904   case Type::IntegerTyID:
905     Result.IntVal = cast<ConstantInt>(C)->getValue();
906     break;
907   case Type::PointerTyID:
908     if (isa<ConstantPointerNull>(C))
909       Result.PointerVal = nullptr;
910     else if (const Function *F = dyn_cast<Function>(C))
911       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
912     else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
913       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
914     else
915       llvm_unreachable("Unknown constant pointer type!");
916     break;
917   case Type::VectorTyID: {
918     unsigned elemNum;
919     Type* ElemTy;
920     const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
921     const ConstantVector *CV = dyn_cast<ConstantVector>(C);
922     const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
923 
924     if (CDV) {
925         elemNum = CDV->getNumElements();
926         ElemTy = CDV->getElementType();
927     } else if (CV || CAZ) {
928         VectorType* VTy = dyn_cast<VectorType>(C->getType());
929         elemNum = VTy->getNumElements();
930         ElemTy = VTy->getElementType();
931     } else {
932         llvm_unreachable("Unknown constant vector type!");
933     }
934 
935     Result.AggregateVal.resize(elemNum);
936     // Check if vector holds floats.
937     if(ElemTy->isFloatTy()) {
938       if (CAZ) {
939         GenericValue floatZero;
940         floatZero.FloatVal = 0.f;
941         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
942                   floatZero);
943         break;
944       }
945       if(CV) {
946         for (unsigned i = 0; i < elemNum; ++i)
947           if (!isa<UndefValue>(CV->getOperand(i)))
948             Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
949               CV->getOperand(i))->getValueAPF().convertToFloat();
950         break;
951       }
952       if(CDV)
953         for (unsigned i = 0; i < elemNum; ++i)
954           Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
955 
956       break;
957     }
958     // Check if vector holds doubles.
959     if (ElemTy->isDoubleTy()) {
960       if (CAZ) {
961         GenericValue doubleZero;
962         doubleZero.DoubleVal = 0.0;
963         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
964                   doubleZero);
965         break;
966       }
967       if(CV) {
968         for (unsigned i = 0; i < elemNum; ++i)
969           if (!isa<UndefValue>(CV->getOperand(i)))
970             Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
971               CV->getOperand(i))->getValueAPF().convertToDouble();
972         break;
973       }
974       if(CDV)
975         for (unsigned i = 0; i < elemNum; ++i)
976           Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
977 
978       break;
979     }
980     // Check if vector holds integers.
981     if (ElemTy->isIntegerTy()) {
982       if (CAZ) {
983         GenericValue intZero;
984         intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
985         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
986                   intZero);
987         break;
988       }
989       if(CV) {
990         for (unsigned i = 0; i < elemNum; ++i)
991           if (!isa<UndefValue>(CV->getOperand(i)))
992             Result.AggregateVal[i].IntVal = cast<ConstantInt>(
993                                             CV->getOperand(i))->getValue();
994           else {
995             Result.AggregateVal[i].IntVal =
996               APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
997           }
998         break;
999       }
1000       if(CDV)
1001         for (unsigned i = 0; i < elemNum; ++i)
1002           Result.AggregateVal[i].IntVal = APInt(
1003             CDV->getElementType()->getPrimitiveSizeInBits(),
1004             CDV->getElementAsInteger(i));
1005 
1006       break;
1007     }
1008     llvm_unreachable("Unknown constant pointer type!");
1009   }
1010   break;
1011 
1012   default:
1013     SmallString<256> Msg;
1014     raw_svector_ostream OS(Msg);
1015     OS << "ERROR: Constant unimplemented for type: " << *C->getType();
1016     report_fatal_error(OS.str());
1017   }
1018 
1019   return Result;
1020 }
1021 
1022 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
1023 /// with the integer held in IntVal.
StoreIntToMemory(const APInt & IntVal,uint8_t * Dst,unsigned StoreBytes)1024 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
1025                              unsigned StoreBytes) {
1026   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
1027   const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
1028 
1029   if (sys::IsLittleEndianHost) {
1030     // Little-endian host - the source is ordered from LSB to MSB.  Order the
1031     // destination from LSB to MSB: Do a straight copy.
1032     memcpy(Dst, Src, StoreBytes);
1033   } else {
1034     // Big-endian host - the source is an array of 64 bit words ordered from
1035     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
1036     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
1037     while (StoreBytes > sizeof(uint64_t)) {
1038       StoreBytes -= sizeof(uint64_t);
1039       // May not be aligned so use memcpy.
1040       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
1041       Src += sizeof(uint64_t);
1042     }
1043 
1044     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
1045   }
1046 }
1047 
StoreValueToMemory(const GenericValue & Val,GenericValue * Ptr,Type * Ty)1048 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
1049                                          GenericValue *Ptr, Type *Ty) {
1050   const unsigned StoreBytes = getDataLayout().getTypeStoreSize(Ty);
1051 
1052   switch (Ty->getTypeID()) {
1053   default:
1054     dbgs() << "Cannot store value of type " << *Ty << "!\n";
1055     break;
1056   case Type::IntegerTyID:
1057     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
1058     break;
1059   case Type::FloatTyID:
1060     *((float*)Ptr) = Val.FloatVal;
1061     break;
1062   case Type::DoubleTyID:
1063     *((double*)Ptr) = Val.DoubleVal;
1064     break;
1065   case Type::X86_FP80TyID:
1066     memcpy(Ptr, Val.IntVal.getRawData(), 10);
1067     break;
1068   case Type::PointerTyID:
1069     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1070     if (StoreBytes != sizeof(PointerTy))
1071       memset(&(Ptr->PointerVal), 0, StoreBytes);
1072 
1073     *((PointerTy*)Ptr) = Val.PointerVal;
1074     break;
1075   case Type::VectorTyID:
1076     for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1077       if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1078         *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1079       if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1080         *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1081       if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1082         unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1083         StoreIntToMemory(Val.AggregateVal[i].IntVal,
1084           (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1085       }
1086     }
1087     break;
1088   }
1089 
1090   if (sys::IsLittleEndianHost != getDataLayout().isLittleEndian())
1091     // Host and target are different endian - reverse the stored bytes.
1092     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1093 }
1094 
1095 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1096 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
LoadIntFromMemory(APInt & IntVal,uint8_t * Src,unsigned LoadBytes)1097 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1098   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
1099   uint8_t *Dst = reinterpret_cast<uint8_t *>(
1100                    const_cast<uint64_t *>(IntVal.getRawData()));
1101 
1102   if (sys::IsLittleEndianHost)
1103     // Little-endian host - the destination must be ordered from LSB to MSB.
1104     // The source is ordered from LSB to MSB: Do a straight copy.
1105     memcpy(Dst, Src, LoadBytes);
1106   else {
1107     // Big-endian - the destination is an array of 64 bit words ordered from
1108     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
1109     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1110     // a word.
1111     while (LoadBytes > sizeof(uint64_t)) {
1112       LoadBytes -= sizeof(uint64_t);
1113       // May not be aligned so use memcpy.
1114       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1115       Dst += sizeof(uint64_t);
1116     }
1117 
1118     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1119   }
1120 }
1121 
1122 /// FIXME: document
1123 ///
LoadValueFromMemory(GenericValue & Result,GenericValue * Ptr,Type * Ty)1124 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
1125                                           GenericValue *Ptr,
1126                                           Type *Ty) {
1127   const unsigned LoadBytes = getDataLayout().getTypeStoreSize(Ty);
1128 
1129   switch (Ty->getTypeID()) {
1130   case Type::IntegerTyID:
1131     // An APInt with all words initially zero.
1132     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1133     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1134     break;
1135   case Type::FloatTyID:
1136     Result.FloatVal = *((float*)Ptr);
1137     break;
1138   case Type::DoubleTyID:
1139     Result.DoubleVal = *((double*)Ptr);
1140     break;
1141   case Type::PointerTyID:
1142     Result.PointerVal = *((PointerTy*)Ptr);
1143     break;
1144   case Type::X86_FP80TyID: {
1145     // This is endian dependent, but it will only work on x86 anyway.
1146     // FIXME: Will not trap if loading a signaling NaN.
1147     uint64_t y[2];
1148     memcpy(y, Ptr, 10);
1149     Result.IntVal = APInt(80, y);
1150     break;
1151   }
1152   case Type::VectorTyID: {
1153     auto *VT = cast<VectorType>(Ty);
1154     Type *ElemT = VT->getElementType();
1155     const unsigned numElems = VT->getNumElements();
1156     if (ElemT->isFloatTy()) {
1157       Result.AggregateVal.resize(numElems);
1158       for (unsigned i = 0; i < numElems; ++i)
1159         Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1160     }
1161     if (ElemT->isDoubleTy()) {
1162       Result.AggregateVal.resize(numElems);
1163       for (unsigned i = 0; i < numElems; ++i)
1164         Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1165     }
1166     if (ElemT->isIntegerTy()) {
1167       GenericValue intZero;
1168       const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1169       intZero.IntVal = APInt(elemBitWidth, 0);
1170       Result.AggregateVal.resize(numElems, intZero);
1171       for (unsigned i = 0; i < numElems; ++i)
1172         LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1173           (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1174     }
1175   break;
1176   }
1177   default:
1178     SmallString<256> Msg;
1179     raw_svector_ostream OS(Msg);
1180     OS << "Cannot load value of type " << *Ty << "!";
1181     report_fatal_error(OS.str());
1182   }
1183 }
1184 
InitializeMemory(const Constant * Init,void * Addr)1185 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
1186   DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1187   DEBUG(Init->dump());
1188   if (isa<UndefValue>(Init))
1189     return;
1190 
1191   if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1192     unsigned ElementSize =
1193         getDataLayout().getTypeAllocSize(CP->getType()->getElementType());
1194     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1195       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1196     return;
1197   }
1198 
1199   if (isa<ConstantAggregateZero>(Init)) {
1200     memset(Addr, 0, (size_t)getDataLayout().getTypeAllocSize(Init->getType()));
1201     return;
1202   }
1203 
1204   if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1205     unsigned ElementSize =
1206         getDataLayout().getTypeAllocSize(CPA->getType()->getElementType());
1207     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1208       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1209     return;
1210   }
1211 
1212   if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1213     const StructLayout *SL =
1214         getDataLayout().getStructLayout(cast<StructType>(CPS->getType()));
1215     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1216       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1217     return;
1218   }
1219 
1220   if (const ConstantDataSequential *CDS =
1221                dyn_cast<ConstantDataSequential>(Init)) {
1222     // CDS is already laid out in host memory order.
1223     StringRef Data = CDS->getRawDataValues();
1224     memcpy(Addr, Data.data(), Data.size());
1225     return;
1226   }
1227 
1228   if (Init->getType()->isFirstClassType()) {
1229     GenericValue Val = getConstantValue(Init);
1230     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1231     return;
1232   }
1233 
1234   DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1235   llvm_unreachable("Unknown constant type to initialize memory with!");
1236 }
1237 
1238 /// EmitGlobals - Emit all of the global variables to memory, storing their
1239 /// addresses into GlobalAddress.  This must make sure to copy the contents of
1240 /// their initializers into the memory.
emitGlobals()1241 void ExecutionEngine::emitGlobals() {
1242   // Loop over all of the global variables in the program, allocating the memory
1243   // to hold them.  If there is more than one module, do a prepass over globals
1244   // to figure out how the different modules should link together.
1245   std::map<std::pair<std::string, Type*>,
1246            const GlobalValue*> LinkedGlobalsMap;
1247 
1248   if (Modules.size() != 1) {
1249     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1250       Module &M = *Modules[m];
1251       for (const auto &GV : M.globals()) {
1252         if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1253             GV.hasAppendingLinkage() || !GV.hasName())
1254           continue;// Ignore external globals and globals with internal linkage.
1255 
1256         const GlobalValue *&GVEntry =
1257           LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
1258 
1259         // If this is the first time we've seen this global, it is the canonical
1260         // version.
1261         if (!GVEntry) {
1262           GVEntry = &GV;
1263           continue;
1264         }
1265 
1266         // If the existing global is strong, never replace it.
1267         if (GVEntry->hasExternalLinkage())
1268           continue;
1269 
1270         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1271         // symbol.  FIXME is this right for common?
1272         if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1273           GVEntry = &GV;
1274       }
1275     }
1276   }
1277 
1278   std::vector<const GlobalValue*> NonCanonicalGlobals;
1279   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1280     Module &M = *Modules[m];
1281     for (const auto &GV : M.globals()) {
1282       // In the multi-module case, see what this global maps to.
1283       if (!LinkedGlobalsMap.empty()) {
1284         if (const GlobalValue *GVEntry =
1285               LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
1286           // If something else is the canonical global, ignore this one.
1287           if (GVEntry != &GV) {
1288             NonCanonicalGlobals.push_back(&GV);
1289             continue;
1290           }
1291         }
1292       }
1293 
1294       if (!GV.isDeclaration()) {
1295         addGlobalMapping(&GV, getMemoryForGV(&GV));
1296       } else {
1297         // External variable reference. Try to use the dynamic loader to
1298         // get a pointer to it.
1299         if (void *SymAddr =
1300             sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1301           addGlobalMapping(&GV, SymAddr);
1302         else {
1303           report_fatal_error("Could not resolve external global address: "
1304                             +GV.getName());
1305         }
1306       }
1307     }
1308 
1309     // If there are multiple modules, map the non-canonical globals to their
1310     // canonical location.
1311     if (!NonCanonicalGlobals.empty()) {
1312       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1313         const GlobalValue *GV = NonCanonicalGlobals[i];
1314         const GlobalValue *CGV =
1315           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1316         void *Ptr = getPointerToGlobalIfAvailable(CGV);
1317         assert(Ptr && "Canonical global wasn't codegen'd!");
1318         addGlobalMapping(GV, Ptr);
1319       }
1320     }
1321 
1322     // Now that all of the globals are set up in memory, loop through them all
1323     // and initialize their contents.
1324     for (const auto &GV : M.globals()) {
1325       if (!GV.isDeclaration()) {
1326         if (!LinkedGlobalsMap.empty()) {
1327           if (const GlobalValue *GVEntry =
1328                 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1329             if (GVEntry != &GV)  // Not the canonical variable.
1330               continue;
1331         }
1332         EmitGlobalVariable(&GV);
1333       }
1334     }
1335   }
1336 }
1337 
1338 // EmitGlobalVariable - This method emits the specified global variable to the
1339 // address specified in GlobalAddresses, or allocates new memory if it's not
1340 // already in the map.
EmitGlobalVariable(const GlobalVariable * GV)1341 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1342   void *GA = getPointerToGlobalIfAvailable(GV);
1343 
1344   if (!GA) {
1345     // If it's not already specified, allocate memory for the global.
1346     GA = getMemoryForGV(GV);
1347 
1348     // If we failed to allocate memory for this global, return.
1349     if (!GA) return;
1350 
1351     addGlobalMapping(GV, GA);
1352   }
1353 
1354   // Don't initialize if it's thread local, let the client do it.
1355   if (!GV->isThreadLocal())
1356     InitializeMemory(GV->getInitializer(), GA);
1357 
1358   Type *ElTy = GV->getType()->getElementType();
1359   size_t GVSize = (size_t)getDataLayout().getTypeAllocSize(ElTy);
1360   NumInitBytes += (unsigned)GVSize;
1361   ++NumGlobals;
1362 }
1363