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