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