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