1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines the common interface used by the various execution engine
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// subclasses.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define DEBUG_TYPE "jit"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ExecutionEngine/ExecutionEngine.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Constants.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/DerivedTypes.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Module.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ExecutionEngine/GenericValue.h"
2219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/SmallString.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/Statistic.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/Debug.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/MutexGuard.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ValueHandle.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
2919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/DynamicLibrary.h"
3019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/Host.h"
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Target/TargetData.h"
3219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Target/TargetMachine.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cmath>
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <cstring>
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSTATISTIC(NumGlobals  , "Number of global vars initialized");
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngine *(*ExecutionEngine::JITCtor)(
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Module *M,
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::string *ErrorStr,
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  JITMemoryManager *JMM,
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CodeGenOpt::Level OptLevel,
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool GVsWithCode,
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TargetMachine *TM) = 0;
4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanExecutionEngine *(*ExecutionEngine::MCJITCtor)(
4819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Module *M,
4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::string *ErrorStr,
5019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  JITMemoryManager *JMM,
5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CodeGenOpt::Level OptLevel,
5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool GVsWithCode,
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TargetMachine *TM) = 0;
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                std::string *ErrorStr) = 0;
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngine::ExecutionEngine(Module *M)
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : EEState(*this),
5919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    LazyFunctionCreator(0),
6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ExceptionTableRegister(0),
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ExceptionTableDeregister(0) {
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CompilingLazily         = false;
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GVCompilationDisabled   = false;
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SymbolSearchingDisabled = false;
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Modules.push_back(M);
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(M && "Module is null?");
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngine::~ExecutionEngine() {
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  clearAllGlobalMappings();
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    delete Modules[i];
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid ExecutionEngine::DeregisterAllTables() {
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ExceptionTableDeregister) {
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DenseMap<const Function*, void*>::iterator it = AllExceptionTables.begin();
7819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DenseMap<const Function*, void*>::iterator ite = AllExceptionTables.end();
7919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (; it != ite; ++it)
8019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ExceptionTableDeregister(it->second);
8119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    AllExceptionTables.clear();
8219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
8319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
8419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
8619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// \brief Helper class which uses a value handler to automatically deletes the
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// memory block when the GlobalVariable is destroyed.
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass GVMemoryBlock : public CallbackVH {
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GVMemoryBlock(const GlobalVariable *GV)
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// \brief Returns the address the GlobalVariable should be written into.  The
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// GVMemoryBlock object prefixes that.
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static char *Create(const GlobalVariable *GV, const TargetData& TD) {
9619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Type *ElTy = GV->getType()->getElementType();
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void *RawMemory = ::operator new(
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      TargetData::RoundUpAlignment(sizeof(GVMemoryBlock),
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   TD.getPreferredAlignment(GV))
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      + GVSize);
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    new(RawMemory) GVMemoryBlock(GV);
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void deleted() {
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // We allocated with operator new and with some extra memory hanging off the
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // end, so don't just delete this.  I'm not sure if this is actually
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // required.
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    this->~GVMemoryBlock();
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ::operator delete(this);
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}  // anonymous namespace
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanchar *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return GVMemoryBlock::Create(GV, *getTargetData());
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool ExecutionEngine::removeModule(Module *M) {
12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for(SmallVector<Module *, 1>::iterator I = Modules.begin(),
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        E = Modules.end(); I != E; ++I) {
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Module *Found = *I;
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Found == M) {
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Modules.erase(I);
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      clearGlobalMappingsFromModule(M);
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanFunction *ExecutionEngine::FindFunctionNamed(const char *FnName) {
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Function *F = Modules[i]->getFunction(FnName))
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return F;
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid *ExecutionEngineState::RemoveMapping(const MutexGuard &,
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          const GlobalValue *ToUnmap) {
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *OldVal;
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // GlobalAddressMap.
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (I == GlobalAddressMap.end())
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OldVal = 0;
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else {
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    OldVal = I->second;
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GlobalAddressMap.erase(I);
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GlobalAddressReverseMap.erase(OldVal);
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return OldVal;
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        << "\' to [" << Addr << "]\n";);
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CurVal = Addr;
16819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we are using the reverse mapping, add it too.
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AssertingVH<const GlobalValue> &V =
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EEState.getGlobalAddressReverseMap(locked)[Addr];
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert((V == 0 || GV == 0) && "GlobalMapping already established!");
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V = GV;
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::clearAllGlobalMappings() {
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EEState.getGlobalAddressMap(locked).clear();
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EEState.getGlobalAddressReverseMap(locked).clear();
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EEState.RemoveMapping(locked, FI);
19019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       GI != GE; ++GI)
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EEState.RemoveMapping(locked, GI);
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ExecutionEngineState::GlobalAddressMapTy &Map =
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EEState.getGlobalAddressMap(locked);
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Deleting from the mapping?
20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (Addr == 0)
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return EEState.RemoveMapping(locked, GV);
20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *&CurVal = Map[GV];
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *OldVal = CurVal;
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CurVal = Addr;
21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If we are using the reverse mapping, add it too.
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    AssertingVH<const GlobalValue> &V =
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      EEState.getGlobalAddressReverseMap(locked)[Addr];
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert((V == 0 || GV == 0) && "GlobalMapping already established!");
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V = GV;
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return OldVal;
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ExecutionEngineState::GlobalAddressMapTy::iterator I =
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EEState.getGlobalAddressMap(locked).find(GV);
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we haven't computed the reverse mapping yet, do so first.
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (EEState.getGlobalAddressReverseMap(locked).empty()) {
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (ExecutionEngineState::GlobalAddressMapTy::iterator
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I = EEState.getGlobalAddressMap(locked).begin(),
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                          I->second, I->first));
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EEState.getGlobalAddressReverseMap(locked).find(Addr);
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass ArgvArray {
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  char *Array;
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<char*> Values;
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgvArray() : Array(NULL) {}
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ~ArgvArray() { clear(); }
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void clear() {
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    delete[] Array;
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Array = NULL;
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (size_t I = 0, E = Values.size(); I != E; ++I) {
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      delete[] Values[I];
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Values.clear();
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Turn a vector of strings into a nice argv style array of pointers to null
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// terminated strings.
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *reset(LLVMContext &C, ExecutionEngine *EE,
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              const std::vector<std::string> &InputArgv);
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}  // anonymous namespace
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                       const std::vector<std::string> &InputArgv) {
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  clear();  // Free the old contents.
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned PtrSize = EE->getTargetData()->getPointerSize();
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Array = new char[(InputArgv.size()+1)*PtrSize];
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *SBytePtr = Type::getInt8PtrTy(C);
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != InputArgv.size(); ++i) {
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Size = InputArgv[i].size()+1;
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    char *Dest = new char[Size];
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Values.push_back(Dest);
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Dest[Size-1] = 0;
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Endian safe: Array[i] = (PointerTy)Dest;
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                           SBytePtr);
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Null terminate it
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EE->StoreValueToMemory(PTOGV(0),
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         (GenericValue*)(Array+InputArgv.size()*PtrSize),
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                         SBytePtr);
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Array;
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::runStaticConstructorsDestructors(Module *module,
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                       bool isDtors) {
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
30119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  GlobalVariable *GV = module->getNamedGlobal(Name);
30219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
30319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If this global has internal linkage, or if it has a use, then it must be
30419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // this is the case, don't execute any of the global ctors, __main will do
30619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // it.
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
30819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Should be an array of '{ i32, void ()* }' structs.  The first value is
31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // the init priority, which we ignore.
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (isa<ConstantAggregateZero>(GV->getInitializer()))
31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return;
31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (isa<ConstantAggregateZero>(InitList->getOperand(i)))
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(i));
31819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
31919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Constant *FP = CS->getOperand(1);
32019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (FP->isNullValue())
32119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;  // Found a sentinal value, ignore.
32219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Strip off constant expression casts.
32419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
32519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (CE->isCast())
32619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        FP = CE->getOperand(0);
32719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
32819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Execute the ctor/dtor function!
32919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Function *F = dyn_cast<Function>(FP))
33019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      runFunction(F, std::vector<GenericValue>());
33119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // FIXME: It is marginally lame that we just do nothing here if we see an
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // entry we don't recognize. It might not be unreasonable for the verifier
33419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // to not even allow this and just assert here.
33519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Execute global ctors/dtors for each module in the program.
34019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
34119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    runStaticConstructorsDestructors(Modules[i], isDtors);
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef NDEBUG
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned PtrSize = EE->getTargetData()->getPointerSize();
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i < PtrSize; ++i)
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (*(i + (uint8_t*)Loc))
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return true;
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanint ExecutionEngine::runFunctionAsMain(Function *Fn,
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       const std::vector<std::string> &argv,
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       const char * const * envp) {
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<GenericValue> GVArgs;
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenericValue GVArgc;
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GVArgc.IntVal = APInt(32, argv.size());
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Check main() type
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned NumArgs = Fn->getFunctionType()->getNumParams();
36419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  FunctionType *FTy = Fn->getFunctionType();
36519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
36619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
36719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Check the argument types.
36819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumArgs > 3)
36919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error("Invalid number of arguments of main() supplied");
37019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
37119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error("Invalid type for third argument of main() supplied");
37219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
37319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error("Invalid type for second argument of main() supplied");
37419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error("Invalid type for first argument of main() supplied");
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!FTy->getReturnType()->isIntegerTy() &&
37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      !FTy->getReturnType()->isVoidTy())
37819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error("Invalid return type of main() supplied");
37919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgvArray CArgv;
381894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgvArray CEnv;
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (NumArgs) {
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GVArgs.push_back(GVArgc); // Arg #0 = argc.
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (NumArgs > 1) {
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Arg #1 = argv.
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman             "argv[0] was null after CreateArgv");
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (NumArgs > 2) {
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        std::vector<std::string> EnvVars;
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        for (unsigned i = 0; envp[i]; ++i)
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          EnvVars.push_back(envp[i]);
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Arg #2 = envp.
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
39819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return runFunction(Fn, GVArgs).IntVal.getZExtValue();
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngine *ExecutionEngine::create(Module *M,
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         bool ForceInterpreter,
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         std::string *ErrorStr,
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         CodeGenOpt::Level OptLevel,
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                         bool GVsWithCode) {
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return EngineBuilder(M)
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .setEngineKind(ForceInterpreter
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     ? EngineKind::Interpreter
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     : EngineKind::JIT)
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .setErrorStr(ErrorStr)
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .setOptLevel(OptLevel)
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .setAllocateGVsWithCode(GVsWithCode)
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      .create();
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// createJIT - This is the factory method for creating a JIT for the current
41819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// machine, it does not fall back to the interpreter.  This takes ownership
41919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// of the module.
42019bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanExecutionEngine *ExecutionEngine::createJIT(Module *M,
42119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            std::string *ErrorStr,
42219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            JITMemoryManager *JMM,
42319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            CodeGenOpt::Level OptLevel,
42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            bool GVsWithCode,
42519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            Reloc::Model RM,
42619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                            CodeModel::Model CMM) {
42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (ExecutionEngine::JITCtor == 0) {
42819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (ErrorStr)
42919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      *ErrorStr = "JIT has not been linked in.";
43019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 0;
43119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
43219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
43319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Use the defaults for extra parameters.  Users can use EngineBuilder to
43419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // set them.
43519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  StringRef MArch = "";
43619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  StringRef MCPU = "";
43719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<std::string, 1> MAttrs;
43819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
43919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  TargetMachine *TM =
44019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EngineBuilder::selectTarget(M, MArch, MCPU, MAttrs, RM, CMM, ErrorStr);
44119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
44219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
44319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel, GVsWithCode, TM);
44419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
44519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngine *EngineBuilder::create() {
447894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Make sure we can resolve symbols in the program as well. The zero arg
448894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to the function tells DynamicLibrary to load the program, not a library.
449894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
450894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
451894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
452894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the user specified a memory manager but didn't specify which engine to
453894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // create, we assume they only want the JIT, and we fail if they only want
454894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the interpreter.
455894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (JMM) {
456894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (WhichEngine & EngineKind::JIT)
457894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      WhichEngine = EngineKind::JIT;
458894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else {
459894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (ErrorStr)
460894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        *ErrorStr = "Cannot create an interpreter with a memory manager.";
461894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return 0;
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
465894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Unless the interpreter was explicitly selected or the JIT is not linked,
466894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // try making a JIT.
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (WhichEngine & EngineKind::JIT) {
46819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (TargetMachine *TM = EngineBuilder::selectTarget(M, MArch, MCPU, MAttrs,
46919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                        RelocModel, CMModel,
47019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                        ErrorStr)) {
47119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (UseMCJIT && ExecutionEngine::MCJITCtor) {
47219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ExecutionEngine *EE =
47319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ExecutionEngine::MCJITCtor(M, ErrorStr, JMM, OptLevel,
47419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     AllocateGVsWithCode, TM);
47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (EE) return EE;
47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      } else if (ExecutionEngine::JITCtor) {
47719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        ExecutionEngine *EE =
47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel,
47919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   AllocateGVsWithCode, TM);
48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (EE) return EE;
48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
482894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
483894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
484894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
485894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If we can't make a JIT and we didn't request one specifically, try making
486894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // an interpreter instead.
487894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (WhichEngine & EngineKind::Interpreter) {
488894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ExecutionEngine::InterpCtor)
489894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return ExecutionEngine::InterpCtor(M, ErrorStr);
490894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ErrorStr)
491894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      *ErrorStr = "Interpreter has not been linked in.";
492894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return 0;
493894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
495894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) {
496894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (ErrorStr)
497894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      *ErrorStr = "JIT has not been linked in.";
49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
500894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return 0;
501894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
502894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
503894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
504894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
505894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getPointerToFunction(F);
506894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
507894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MutexGuard locked(lock);
50819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (void *P = EEState.getGlobalAddressMap(locked)[GV])
50919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return P;
510894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Global variable might have been added since interpreter started.
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GlobalVariable *GVar =
513894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
514894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    EmitGlobalVariable(GVar);
515894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else
516894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("Global hasn't had an address allocated yet!");
51719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
518894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return EEState.getGlobalAddressMap(locked)[GV];
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
520894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// \brief Converts a Constant* into a GenericValue, including handling of
52219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman/// ConstantExpr values.
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanGenericValue ExecutionEngine::getConstantValue(const Constant *C) {
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If its undefined, return the garbage.
525894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<UndefValue>(C)) {
526894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GenericValue Result;
527894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (C->getType()->getTypeID()) {
528894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::IntegerTyID:
529894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::X86_FP80TyID:
530894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::FP128TyID:
531894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Type::PPC_FP128TyID:
532894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Although the value is undefined, we still have to construct an APInt
533894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // with the correct bit width.
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
535894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default:
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return Result;
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
54219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Otherwise, if the value is a ConstantExpr...
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
544894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Constant *Op0 = CE->getOperand(0);
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (CE->getOpcode()) {
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::GetElementPtr: {
54719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Compute the index
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue Result = getConstantValue(Op0);
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
55019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      uint64_t Offset = TD->getIndexedOffset(Op0->getType(), Indices);
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      char* tmp = (char*) Result.PointerVal;
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result = PTOGV(tmp + Offset);
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return Result;
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Trunc: {
557894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
558894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.IntVal = GV.IntVal.trunc(BitWidth);
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::ZExt: {
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.IntVal = GV.IntVal.zext(BitWidth);
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
567894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
568894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SExt: {
569894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
570894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
571894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.IntVal = GV.IntVal.sext(BitWidth);
572894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
573894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
574894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPTrunc: {
575894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME long double
576894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
577894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.FloatVal = float(GV.DoubleVal);
578894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
579894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
580894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPExt:{
581894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // FIXME long double
582894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
583894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.DoubleVal = double(GV.FloatVal);
584894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::UIToFP: {
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CE->getType()->isFloatTy())
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.FloatVal = float(GV.IntVal.roundToDouble());
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (CE->getType()->isDoubleTy())
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.DoubleVal = GV.IntVal.roundToDouble();
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (CE->getType()->isX86_FP80Ty()) {
59319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
59419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (void)apf.convertFromAPInt(GV.IntVal,
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   false,
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   APFloat::rmNearestTiesToEven);
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.IntVal = apf.bitcastToAPInt();
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SIToFP: {
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (CE->getType()->isFloatTy())
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (CE->getType()->isDoubleTy())
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.DoubleVal = GV.IntVal.signedRoundToDouble();
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (CE->getType()->isX86_FP80Ty()) {
60819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
60919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        (void)apf.convertFromAPInt(GV.IntVal,
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   true,
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   APFloat::rmNearestTiesToEven);
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.IntVal = apf.bitcastToAPInt();
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
615894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
616894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPToUI: // double->APInt conversion handles sign
617894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FPToSI: {
618894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
619894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
620894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (Op0->getType()->isFloatTy())
621894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
622894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (Op0->getType()->isDoubleTy())
623894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
624894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      else if (Op0->getType()->isX86_FP80Ty()) {
625894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        APFloat apf = APFloat(GV.IntVal);
626894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        uint64_t v;
627894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        bool ignored;
628894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        (void)apf.convertToInteger(&v, BitWidth,
62919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   CE->getOpcode()==Instruction::FPToSI,
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   APFloat::rmTowardZero, &ignored);
631894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.IntVal = v; // endian?
632894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
633894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
634894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
635894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::PtrToInt: {
636894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
637894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint32_t PtrWidth = TD->getPointerSizeInBits();
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::IntToPtr: {
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      uint32_t PtrWidth = TD->getPointerSizeInBits();
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (PtrWidth != GV.IntVal.getBitWidth())
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::BitCast: {
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV = getConstantValue(Op0);
65219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Type* DestTy = CE->getType();
653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (Op0->getType()->getTypeID()) {
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        default: llvm_unreachable("Invalid bitcast operand");
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case Type::IntegerTyID:
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          assert(DestTy->isFloatingPointTy() && "invalid bitcast");
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (DestTy->isFloatTy())
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.FloatVal = GV.IntVal.bitsToFloat();
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          else if (DestTy->isDoubleTy())
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.DoubleVal = GV.IntVal.bitsToDouble();
661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
66219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        case Type::FloatTyID:
663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
66419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          GV.IntVal = APInt::floatToBits(GV.FloatVal);
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case Type::DoubleTyID:
667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
66819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break;
670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        case Type::PointerTyID:
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          assert(DestTy->isPointerTy() && "Invalid bitcast");
672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          break; // getConstantValue(Op0)  above already converted it
673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Add:
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FAdd:
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Sub:
679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FSub:
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Mul:
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::FMul:
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::UDiv:
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SDiv:
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::URem:
685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::SRem:
686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::And:
687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Or:
688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case Instruction::Xor: {
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue LHS = getConstantValue(Op0);
690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue RHS = getConstantValue(CE->getOperand(1));
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      GenericValue GV;
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      switch (CE->getOperand(0)->getType()->getTypeID()) {
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      default: llvm_unreachable("Bad add type!");
694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Type::IntegerTyID:
695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (CE->getOpcode()) {
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          default: llvm_unreachable("Invalid integer opcode");
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Type::FloatTyID:
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (CE->getOpcode()) {
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          default: llvm_unreachable("Invalid float opcode");
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FAdd:
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FSub:
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FMul:
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
71819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case Instruction::FDiv:
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
72019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case Instruction::FRem:
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Type::DoubleTyID:
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (CE->getOpcode()) {
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          default: llvm_unreachable("Invalid double opcode");
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FAdd:
728894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
729894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FSub:
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FMul:
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
73319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case Instruction::FDiv:
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
73519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case Instruction::FRem:
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Type::X86_FP80TyID:
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Type::PPC_FP128TyID:
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      case Type::FP128TyID: {
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        APFloat apfLHS = APFloat(LHS.IntVal);
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        switch (CE->getOpcode()) {
74419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          default: llvm_unreachable("Invalid long double opcode");
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FAdd:
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.IntVal = apfLHS.bitcastToAPInt();
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FSub:
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
751894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.IntVal = apfLHS.bitcastToAPInt();
752894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          case Instruction::FMul:
754894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.IntVal = apfLHS.bitcastToAPInt();
756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
75719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case Instruction::FDiv:
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
759894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.IntVal = apfLHS.bitcastToAPInt();
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
76119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          case Instruction::FRem:
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
763894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV.IntVal = apfLHS.bitcastToAPInt();
764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            break;
765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return GV;
770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default:
772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
77419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
77519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallString<256> Msg;
77619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    raw_svector_ostream OS(Msg);
77719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "ConstantExpr not handled: " << *CE;
77819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error(OS.str());
779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
78119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Otherwise, we have a simple constant.
782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  GenericValue Result;
783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (C->getType()->getTypeID()) {
78419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case Type::FloatTyID:
78519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID:
788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::X86_FP80TyID:
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FP128TyID:
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PPC_FP128TyID:
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::IntegerTyID:
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.IntVal = cast<ConstantInt>(C)->getValue();
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID:
799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (isa<ConstantPointerNull>(C))
800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result.PointerVal = 0;
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (const Function *F = dyn_cast<Function>(C))
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
805894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
806894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                        BA->getBasicBlock())));
808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    else
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      llvm_unreachable("Unknown constant pointer type!");
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
81219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallString<256> Msg;
81319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    raw_svector_ostream OS(Msg);
81419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "ERROR: Constant unimplemented for type: " << *C->getType();
81519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error(OS.str());
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
81719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Result;
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// with the integer held in IntVal.
823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             unsigned StoreBytes) {
825894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
826894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint8_t *Src = (uint8_t *)IntVal.getRawData();
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
82819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (sys::isLittleEndianHost()) {
829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Little-endian host - the source is ordered from LSB to MSB.  Order the
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // destination from LSB to MSB: Do a straight copy.
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(Dst, Src, StoreBytes);
83219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Big-endian host - the source is an array of 64 bit words ordered from
834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // from MSB to LSB: Reverse the word order, but not the bytes in a word.
836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    while (StoreBytes > sizeof(uint64_t)) {
837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      StoreBytes -= sizeof(uint64_t);
838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // May not be aligned so use memcpy.
839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Src += sizeof(uint64_t);
841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
84819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         GenericValue *Ptr, Type *Ty) {
849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::IntegerTyID:
853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FloatTyID:
856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    *((float*)Ptr) = Val.FloatVal;
857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID:
859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    *((double*)Ptr) = Val.DoubleVal;
860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::X86_FP80TyID:
862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(Ptr, Val.IntVal.getRawData(), 10);
863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID:
865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (StoreBytes != sizeof(PointerTy))
86719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      memset(&(Ptr->PointerVal), 0, StoreBytes);
868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    *((PointerTy*)Ptr) = Val.PointerVal;
870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    dbgs() << "Cannot store value of type " << *Ty << "!\n";
873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian())
876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Host and target are different endian - reverse the stored bytes.
877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
882894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  uint8_t *Dst = (uint8_t *)IntVal.getRawData();
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (sys::isLittleEndianHost())
887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Little-endian host - the destination must be ordered from LSB to MSB.
888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // The source is ordered from LSB to MSB: Do a straight copy.
889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(Dst, Src, LoadBytes);
890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else {
891894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Big-endian - the destination is an array of 64 bit words ordered from
892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
893894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // ordered from MSB to LSB: Reverse the word order, but not the bytes in
894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // a word.
895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    while (LoadBytes > sizeof(uint64_t)) {
896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      LoadBytes -= sizeof(uint64_t);
897894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // May not be aligned so use memcpy.
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
899894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Dst += sizeof(uint64_t);
900894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FIXME: document
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman///
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          GenericValue *Ptr,
91019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          Type *Ty) {
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
912894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Ty->getTypeID()) {
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::IntegerTyID:
915894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // An APInt with all words initially zero.
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::FloatTyID:
920894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.FloatVal = *((float*)Ptr);
921894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::DoubleTyID:
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.DoubleVal = *((double*)Ptr);
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::PointerTyID:
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Result.PointerVal = *((PointerTy*)Ptr);
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case Type::X86_FP80TyID: {
929894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This is endian dependent, but it will only work on x86 anyway.
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Will not trap if loading a signaling NaN.
931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    uint64_t y[2];
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memcpy(y, Ptr, 10);
93319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Result.IntVal = APInt(80, y);
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
935894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default:
93719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SmallString<256> Msg;
93819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    raw_svector_ostream OS(Msg);
93919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    OS << "Cannot load value of type " << *Ty << "!";
94019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    report_fatal_error(OS.str());
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DEBUG(Init->dump());
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<UndefValue>(Init)) {
948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned ElementSize =
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (isa<ConstantAggregateZero>(Init)) {
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
958894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned ElementSize =
960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
964894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    const StructLayout *SL =
966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else if (Init->getType()->isFirstClassType()) {
971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GenericValue Val = getConstantValue(Init);
972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  llvm_unreachable("Unknown constant type to initialize memory with!");
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// EmitGlobals - Emit all of the global variables to memory, storing their
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// addresses into GlobalAddress.  This must make sure to copy the contents of
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// their initializers into the memory.
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::emitGlobals() {
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Loop over all of the global variables in the program, allocating the memory
985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to hold them.  If there is more than one module, do a prepass over globals
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to figure out how the different modules should link together.
98719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  std::map<std::pair<std::string, Type*>,
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           const GlobalValue*> LinkedGlobalsMap;
989894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Modules.size() != 1) {
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Module &M = *Modules[m];
993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (Module::const_global_iterator I = M.global_begin(),
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           E = M.global_end(); I != E; ++I) {
995894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        const GlobalValue *GV = I;
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (GV->hasLocalLinkage() || GV->isDeclaration() ||
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GV->hasAppendingLinkage() || !GV->hasName())
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;// Ignore external globals and globals with internal linkage.
99919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
100019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        const GlobalValue *&GVEntry =
1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If this is the first time we've seen this global, it is the canonical
1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // version.
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!GVEntry) {
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          GVEntry = GV;
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
100919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // If the existing global is strong, never replace it.
1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (GVEntry->hasExternalLinkage() ||
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GVEntry->hasDLLImportLinkage() ||
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            GVEntry->hasDLLExportLinkage())
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          continue;
101519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1017894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // symbol.  FIXME is this right for common?
1018894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          GVEntry = GV;
1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
102319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  std::vector<const GlobalValue*> NonCanonicalGlobals;
1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Module &M = *Modules[m];
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1028894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I != E; ++I) {
1029894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // In the multi-module case, see what this global maps to.
1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!LinkedGlobalsMap.empty()) {
103119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (const GlobalValue *GVEntry =
1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          // If something else is the canonical global, ignore this one.
1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          if (GVEntry != &*I) {
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            NonCanonicalGlobals.push_back(I);
1036894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            continue;
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          }
1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
104019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1041894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!I->isDeclaration()) {
1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        addGlobalMapping(I, getMemoryForGV(I));
1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
1044894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // External variable reference. Try to use the dynamic loader to
1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        // get a pointer to it.
1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (void *SymAddr =
1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          addGlobalMapping(I, SymAddr);
1049894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        else {
1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          report_fatal_error("Could not resolve external global address: "
1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            +I->getName());
1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
105519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If there are multiple modules, map the non-canonical globals to their
1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // canonical location.
1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!NonCanonicalGlobals.empty()) {
1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        const GlobalValue *GV = NonCanonicalGlobals[i];
1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        const GlobalValue *CGV =
1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        void *Ptr = getPointerToGlobalIfAvailable(CGV);
1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        assert(Ptr && "Canonical global wasn't codegen'd!");
1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        addGlobalMapping(GV, Ptr);
1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
106819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
106919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Now that all of the globals are set up in memory, loop through them all
1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // and initialize their contents.
1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         I != E; ++I) {
1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (!I->isDeclaration()) {
1074894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        if (!LinkedGlobalsMap.empty()) {
107519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          if (const GlobalValue *GVEntry =
1076894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1077894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman            if (GVEntry != &*I)  // Not the canonical variable.
1078894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman              continue;
1079894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
1080894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        EmitGlobalVariable(I);
1081894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
1082894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1083894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1084894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1085894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1086894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// EmitGlobalVariable - This method emits the specified global variable to the
1087894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// address specified in GlobalAddresses, or allocates new memory if it's not
1088894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// already in the map.
1089894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1090894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *GA = getPointerToGlobalIfAvailable(GV);
1091894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1092894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GA == 0) {
1093894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // If it's not already specified, allocate memory for the global.
1094894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    GA = getMemoryForGV(GV);
1095894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    addGlobalMapping(GV, GA);
1096894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
109719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1098894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Don't initialize if it's thread local, let the client do it.
1099894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!GV->isThreadLocal())
1100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InitializeMemory(GV->getInitializer(), GA);
110119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
110219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ElTy = GV->getType()->getElementType();
1103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
1104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  NumInitBytes += (unsigned)GVSize;
1105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++NumGlobals;
1106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : EE(EE), GlobalAddressMap(this) {
1110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumansys::Mutex *
111319bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
1114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return &EES->EE.lock;
1115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
111619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
111719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
111819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                      const GlobalValue *Old) {
1119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void *OldVal = EES->GlobalAddressMap.lookup(Old);
1120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EES->GlobalAddressReverseMap.erase(OldVal);
1121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
112319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
112419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                    const GlobalValue *,
112519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                    const GlobalValue *) {
1126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(false && "The ExecutionEngine doesn't know how to handle a"
1127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         " RAUW on a value it has a global mapping for.");
1128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1129