ExecutionEngine.cpp revision 263508
1170754Sdelphij//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2170754Sdelphij//
3170754Sdelphij//                     The LLVM Compiler Infrastructure
4170754Sdelphij//
5170754Sdelphij// This file is distributed under the University of Illinois Open Source
6170754Sdelphij// License. See LICENSE.TXT for details.
7170754Sdelphij//
8170754Sdelphij//===----------------------------------------------------------------------===//
9170754Sdelphij//
10170754Sdelphij// This file defines the common interface used by the various execution engine
11170754Sdelphij// subclasses.
12170754Sdelphij//
13170754Sdelphij//===----------------------------------------------------------------------===//
14170754Sdelphij
15170754Sdelphij#define DEBUG_TYPE "jit"
16170754Sdelphij#include "llvm/ExecutionEngine/ExecutionEngine.h"
17170754Sdelphij#include "llvm/ExecutionEngine/JITMemoryManager.h"
18170754Sdelphij#include "llvm/ExecutionEngine/ObjectCache.h"
19170754Sdelphij#include "llvm/ADT/SmallString.h"
20170754Sdelphij#include "llvm/ADT/Statistic.h"
21170754Sdelphij#include "llvm/ExecutionEngine/GenericValue.h"
22170754Sdelphij#include "llvm/IR/Constants.h"
23170754Sdelphij#include "llvm/IR/DataLayout.h"
24170754Sdelphij#include "llvm/IR/DerivedTypes.h"
25170754Sdelphij#include "llvm/IR/Module.h"
26170754Sdelphij#include "llvm/IR/Operator.h"
27170754Sdelphij#include "llvm/Support/Debug.h"
28170754Sdelphij#include "llvm/Support/DynamicLibrary.h"
29170754Sdelphij#include "llvm/Support/ErrorHandling.h"
30170754Sdelphij#include "llvm/Support/Host.h"
31170754Sdelphij#include "llvm/Support/MutexGuard.h"
32170754Sdelphij#include "llvm/Support/TargetRegistry.h"
33170754Sdelphij#include "llvm/Support/ValueHandle.h"
34170754Sdelphij#include "llvm/Support/raw_ostream.h"
35170754Sdelphij#include "llvm/Target/TargetMachine.h"
36170754Sdelphij#include <cmath>
37170754Sdelphij#include <cstring>
38170754Sdelphijusing namespace llvm;
39170754Sdelphij
40170754SdelphijSTATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
41170754SdelphijSTATISTIC(NumGlobals  , "Number of global vars initialized");
42170754Sdelphij
43170754Sdelphij// Pin the vtable to this file.
44170754Sdelphijvoid ObjectCache::anchor() {}
45170754Sdelphijvoid ObjectBuffer::anchor() {}
46170754Sdelphijvoid ObjectBufferStream::anchor() {}
47170754Sdelphij
48170754SdelphijExecutionEngine *(*ExecutionEngine::JITCtor)(
49170754Sdelphij  Module *M,
50170754Sdelphij  std::string *ErrorStr,
51170754Sdelphij  JITMemoryManager *JMM,
52170754Sdelphij  bool GVsWithCode,
53170754Sdelphij  TargetMachine *TM) = 0;
54170754SdelphijExecutionEngine *(*ExecutionEngine::MCJITCtor)(
55170754Sdelphij  Module *M,
56170754Sdelphij  std::string *ErrorStr,
57170754Sdelphij  RTDyldMemoryManager *MCJMM,
58170754Sdelphij  bool GVsWithCode,
59170754Sdelphij  TargetMachine *TM) = 0;
60170754SdelphijExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
61170754Sdelphij                                                std::string *ErrorStr) = 0;
62170754Sdelphij
63170754SdelphijExecutionEngine::ExecutionEngine(Module *M)
64170754Sdelphij  : EEState(*this),
65170754Sdelphij    LazyFunctionCreator(0) {
66170754Sdelphij  CompilingLazily         = false;
67170754Sdelphij  GVCompilationDisabled   = false;
68170754Sdelphij  SymbolSearchingDisabled = false;
69170754Sdelphij  Modules.push_back(M);
70170754Sdelphij  assert(M && "Module is null?");
71170754Sdelphij}
72170754Sdelphij
73170754SdelphijExecutionEngine::~ExecutionEngine() {
74170754Sdelphij  clearAllGlobalMappings();
75170754Sdelphij  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
76170754Sdelphij    delete Modules[i];
77170754Sdelphij}
78170754Sdelphij
79170754Sdelphijnamespace {
80170754Sdelphij/// \brief Helper class which uses a value handler to automatically deletes the
81170754Sdelphij/// memory block when the GlobalVariable is destroyed.
82170754Sdelphijclass GVMemoryBlock : public CallbackVH {
83170754Sdelphij  GVMemoryBlock(const GlobalVariable *GV)
84170754Sdelphij    : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
85170754Sdelphij
86170754Sdelphijpublic:
87170754Sdelphij  /// \brief Returns the address the GlobalVariable should be written into.  The
88170754Sdelphij  /// GVMemoryBlock object prefixes that.
89170754Sdelphij  static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
90170754Sdelphij    Type *ElTy = GV->getType()->getElementType();
91170754Sdelphij    size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
92170754Sdelphij    void *RawMemory = ::operator new(
93170754Sdelphij      DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
94170754Sdelphij                                   TD.getPreferredAlignment(GV))
95170754Sdelphij      + GVSize);
96170754Sdelphij    new(RawMemory) GVMemoryBlock(GV);
97170754Sdelphij    return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
98170754Sdelphij  }
99170754Sdelphij
100170754Sdelphij  virtual void deleted() {
101170754Sdelphij    // We allocated with operator new and with some extra memory hanging off the
102170754Sdelphij    // end, so don't just delete this.  I'm not sure if this is actually
103170754Sdelphij    // required.
104170754Sdelphij    this->~GVMemoryBlock();
105170754Sdelphij    ::operator delete(this);
106170754Sdelphij  }
107170754Sdelphij};
108170754Sdelphij}  // anonymous namespace
109170754Sdelphij
110170754Sdelphijchar *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
111170754Sdelphij  return GVMemoryBlock::Create(GV, *getDataLayout());
112170754Sdelphij}
113170754Sdelphij
114170754Sdelphijbool ExecutionEngine::removeModule(Module *M) {
115170754Sdelphij  for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
116170754Sdelphij        E = Modules.end(); I != E; ++I) {
117170754Sdelphij    Module *Found = *I;
118170754Sdelphij    if (Found == M) {
119170754Sdelphij      Modules.erase(I);
120170754Sdelphij      clearGlobalMappingsFromModule(M);
121170754Sdelphij      return true;
122170754Sdelphij    }
123170754Sdelphij  }
124170754Sdelphij  return false;
125170754Sdelphij}
126170754Sdelphij
127170754SdelphijFunction *ExecutionEngine::FindFunctionNamed(const char *FnName) {
128170754Sdelphij  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
129170754Sdelphij    if (Function *F = Modules[i]->getFunction(FnName))
130170754Sdelphij      return F;
131170754Sdelphij  }
132170754Sdelphij  return 0;
133170754Sdelphij}
134170754Sdelphij
135170754Sdelphij
136170754Sdelphijvoid *ExecutionEngineState::RemoveMapping(const MutexGuard &,
137170754Sdelphij                                          const GlobalValue *ToUnmap) {
138170754Sdelphij  GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
139170754Sdelphij  void *OldVal;
140170754Sdelphij
141170754Sdelphij  // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
142170754Sdelphij  // GlobalAddressMap.
143170754Sdelphij  if (I == GlobalAddressMap.end())
144170754Sdelphij    OldVal = 0;
145170754Sdelphij  else {
146170754Sdelphij    OldVal = I->second;
147170754Sdelphij    GlobalAddressMap.erase(I);
148170754Sdelphij  }
149170754Sdelphij
150170754Sdelphij  GlobalAddressReverseMap.erase(OldVal);
151170754Sdelphij  return OldVal;
152170754Sdelphij}
153170754Sdelphij
154170754Sdelphijvoid ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
155170754Sdelphij  MutexGuard locked(lock);
156170754Sdelphij
157170754Sdelphij  DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
158170754Sdelphij        << "\' to [" << Addr << "]\n";);
159170754Sdelphij  void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
160170754Sdelphij  assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
161170754Sdelphij  CurVal = Addr;
162170754Sdelphij
163170754Sdelphij  // If we are using the reverse mapping, add it too.
164170754Sdelphij  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
165170754Sdelphij    AssertingVH<const GlobalValue> &V =
166170754Sdelphij      EEState.getGlobalAddressReverseMap(locked)[Addr];
167170754Sdelphij    assert((V == 0 || GV == 0) && "GlobalMapping already established!");
168170754Sdelphij    V = GV;
169170754Sdelphij  }
170170754Sdelphij}
171170754Sdelphij
172170754Sdelphijvoid ExecutionEngine::clearAllGlobalMappings() {
173170754Sdelphij  MutexGuard locked(lock);
174170754Sdelphij
175170754Sdelphij  EEState.getGlobalAddressMap(locked).clear();
176170754Sdelphij  EEState.getGlobalAddressReverseMap(locked).clear();
177170754Sdelphij}
178170754Sdelphij
179170754Sdelphijvoid ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
180170754Sdelphij  MutexGuard locked(lock);
181170754Sdelphij
182170754Sdelphij  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
183170754Sdelphij    EEState.RemoveMapping(locked, FI);
184170754Sdelphij  for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
185170754Sdelphij       GI != GE; ++GI)
186170754Sdelphij    EEState.RemoveMapping(locked, GI);
187170754Sdelphij}
188170754Sdelphij
189170754Sdelphijvoid *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
190170754Sdelphij  MutexGuard locked(lock);
191170754Sdelphij
192170754Sdelphij  ExecutionEngineState::GlobalAddressMapTy &Map =
193170754Sdelphij    EEState.getGlobalAddressMap(locked);
194170754Sdelphij
195170754Sdelphij  // Deleting from the mapping?
196170754Sdelphij  if (Addr == 0)
197170754Sdelphij    return EEState.RemoveMapping(locked, GV);
198170754Sdelphij
199170754Sdelphij  void *&CurVal = Map[GV];
200170754Sdelphij  void *OldVal = CurVal;
201170754Sdelphij
202170754Sdelphij  if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
203170754Sdelphij    EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
204170754Sdelphij  CurVal = Addr;
205170754Sdelphij
206170754Sdelphij  // If we are using the reverse mapping, add it too.
207170754Sdelphij  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
208170754Sdelphij    AssertingVH<const GlobalValue> &V =
209170754Sdelphij      EEState.getGlobalAddressReverseMap(locked)[Addr];
210170754Sdelphij    assert((V == 0 || GV == 0) && "GlobalMapping already established!");
211170754Sdelphij    V = GV;
212170754Sdelphij  }
213170754Sdelphij  return OldVal;
214170754Sdelphij}
215170754Sdelphij
216170754Sdelphijvoid *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
217170754Sdelphij  MutexGuard locked(lock);
218170754Sdelphij
219170754Sdelphij  ExecutionEngineState::GlobalAddressMapTy::iterator I =
220170754Sdelphij    EEState.getGlobalAddressMap(locked).find(GV);
221170754Sdelphij  return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
222170754Sdelphij}
223170754Sdelphij
224170754Sdelphijconst GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
225170754Sdelphij  MutexGuard locked(lock);
226170754Sdelphij
227170754Sdelphij  // If we haven't computed the reverse mapping yet, do so first.
228170754Sdelphij  if (EEState.getGlobalAddressReverseMap(locked).empty()) {
229170754Sdelphij    for (ExecutionEngineState::GlobalAddressMapTy::iterator
230170754Sdelphij         I = EEState.getGlobalAddressMap(locked).begin(),
231170754Sdelphij         E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
232170754Sdelphij      EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
233170754Sdelphij                                                          I->second, I->first));
234170754Sdelphij  }
235170754Sdelphij
236170754Sdelphij  std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
237170754Sdelphij    EEState.getGlobalAddressReverseMap(locked).find(Addr);
238170754Sdelphij  return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
239170754Sdelphij}
240170754Sdelphij
241170754Sdelphijnamespace {
242170754Sdelphijclass ArgvArray {
243170754Sdelphij  char *Array;
244170754Sdelphij  std::vector<char*> Values;
245170754Sdelphijpublic:
246170754Sdelphij  ArgvArray() : Array(NULL) {}
247170754Sdelphij  ~ArgvArray() { clear(); }
248170754Sdelphij  void clear() {
249170754Sdelphij    delete[] Array;
250170754Sdelphij    Array = NULL;
251170754Sdelphij    for (size_t I = 0, E = Values.size(); I != E; ++I) {
252170754Sdelphij      delete[] Values[I];
253170754Sdelphij    }
254170754Sdelphij    Values.clear();
255170754Sdelphij  }
256170754Sdelphij  /// Turn a vector of strings into a nice argv style array of pointers to null
257170754Sdelphij  /// terminated strings.
258170754Sdelphij  void *reset(LLVMContext &C, ExecutionEngine *EE,
259170754Sdelphij              const std::vector<std::string> &InputArgv);
260170754Sdelphij};
261170754Sdelphij}  // anonymous namespace
262170754Sdelphijvoid *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
263170754Sdelphij                       const std::vector<std::string> &InputArgv) {
264170754Sdelphij  clear();  // Free the old contents.
265170754Sdelphij  unsigned PtrSize = EE->getDataLayout()->getPointerSize();
266170754Sdelphij  Array = new char[(InputArgv.size()+1)*PtrSize];
267170754Sdelphij
268170754Sdelphij  DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
269170754Sdelphij  Type *SBytePtr = Type::getInt8PtrTy(C);
270170754Sdelphij
271170754Sdelphij  for (unsigned i = 0; i != InputArgv.size(); ++i) {
272170754Sdelphij    unsigned Size = InputArgv[i].size()+1;
273170754Sdelphij    char *Dest = new char[Size];
274170754Sdelphij    Values.push_back(Dest);
275170754Sdelphij    DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
276170754Sdelphij
277170754Sdelphij    std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
278170754Sdelphij    Dest[Size-1] = 0;
279170754Sdelphij
280170754Sdelphij    // Endian safe: Array[i] = (PointerTy)Dest;
281170754Sdelphij    EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
282170754Sdelphij                           SBytePtr);
283170754Sdelphij  }
284170754Sdelphij
285170754Sdelphij  // Null terminate it
286170754Sdelphij  EE->StoreValueToMemory(PTOGV(0),
287170754Sdelphij                         (GenericValue*)(Array+InputArgv.size()*PtrSize),
288170754Sdelphij                         SBytePtr);
289170754Sdelphij  return Array;
290170754Sdelphij}
291170754Sdelphij
292170754Sdelphijvoid ExecutionEngine::runStaticConstructorsDestructors(Module *module,
293170754Sdelphij                                                       bool isDtors) {
294170754Sdelphij  const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
295170754Sdelphij  GlobalVariable *GV = module->getNamedGlobal(Name);
296170754Sdelphij
297170754Sdelphij  // If this global has internal linkage, or if it has a use, then it must be
298170754Sdelphij  // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
299170754Sdelphij  // this is the case, don't execute any of the global ctors, __main will do
300170754Sdelphij  // it.
301170754Sdelphij  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
302170754Sdelphij
303170754Sdelphij  // Should be an array of '{ i32, void ()* }' structs.  The first value is
304170754Sdelphij  // the init priority, which we ignore.
305170754Sdelphij  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
306170754Sdelphij  if (InitList == 0)
307170754Sdelphij    return;
308170754Sdelphij  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
309170754Sdelphij    ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
310170754Sdelphij    if (CS == 0) continue;
311170754Sdelphij
312170754Sdelphij    Constant *FP = CS->getOperand(1);
313170754Sdelphij    if (FP->isNullValue())
314170754Sdelphij      continue;  // Found a sentinal value, ignore.
315170754Sdelphij
316170754Sdelphij    // Strip off constant expression casts.
317170754Sdelphij    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
318170754Sdelphij      if (CE->isCast())
319170754Sdelphij        FP = CE->getOperand(0);
320170754Sdelphij
321170754Sdelphij    // Execute the ctor/dtor function!
322170754Sdelphij    if (Function *F = dyn_cast<Function>(FP))
323170754Sdelphij      runFunction(F, std::vector<GenericValue>());
324170754Sdelphij
325170754Sdelphij    // FIXME: It is marginally lame that we just do nothing here if we see an
326170754Sdelphij    // entry we don't recognize. It might not be unreasonable for the verifier
327170754Sdelphij    // to not even allow this and just assert here.
328170754Sdelphij  }
329170754Sdelphij}
330170754Sdelphij
331170754Sdelphijvoid ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
332170754Sdelphij  // Execute global ctors/dtors for each module in the program.
333170754Sdelphij  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
334170754Sdelphij    runStaticConstructorsDestructors(Modules[i], isDtors);
335170754Sdelphij}
336170754Sdelphij
337170754Sdelphij#ifndef NDEBUG
338170754Sdelphij/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
339170754Sdelphijstatic bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
340170754Sdelphij  unsigned PtrSize = EE->getDataLayout()->getPointerSize();
341170754Sdelphij  for (unsigned i = 0; i < PtrSize; ++i)
342170754Sdelphij    if (*(i + (uint8_t*)Loc))
343170754Sdelphij      return false;
344170754Sdelphij  return true;
345170754Sdelphij}
346170754Sdelphij#endif
347170754Sdelphij
348170754Sdelphijint ExecutionEngine::runFunctionAsMain(Function *Fn,
349170754Sdelphij                                       const std::vector<std::string> &argv,
350170754Sdelphij                                       const char * const * envp) {
351170754Sdelphij  std::vector<GenericValue> GVArgs;
352170754Sdelphij  GenericValue GVArgc;
353170754Sdelphij  GVArgc.IntVal = APInt(32, argv.size());
354170754Sdelphij
355170754Sdelphij  // Check main() type
356170754Sdelphij  unsigned NumArgs = Fn->getFunctionType()->getNumParams();
357170754Sdelphij  FunctionType *FTy = Fn->getFunctionType();
358170754Sdelphij  Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
359170754Sdelphij
360170754Sdelphij  // Check the argument types.
361170754Sdelphij  if (NumArgs > 3)
362170754Sdelphij    report_fatal_error("Invalid number of arguments of main() supplied");
363170754Sdelphij  if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
364170754Sdelphij    report_fatal_error("Invalid type for third argument of main() supplied");
365170754Sdelphij  if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
366170754Sdelphij    report_fatal_error("Invalid type for second argument of main() supplied");
367170754Sdelphij  if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
368170754Sdelphij    report_fatal_error("Invalid type for first argument of main() supplied");
369170754Sdelphij  if (!FTy->getReturnType()->isIntegerTy() &&
370170754Sdelphij      !FTy->getReturnType()->isVoidTy())
371170754Sdelphij    report_fatal_error("Invalid return type of main() supplied");
372170754Sdelphij
373170754Sdelphij  ArgvArray CArgv;
374170754Sdelphij  ArgvArray CEnv;
375170754Sdelphij  if (NumArgs) {
376170754Sdelphij    GVArgs.push_back(GVArgc); // Arg #0 = argc.
377170754Sdelphij    if (NumArgs > 1) {
378170754Sdelphij      // Arg #1 = argv.
379170754Sdelphij      GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
380170754Sdelphij      assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
381170754Sdelphij             "argv[0] was null after CreateArgv");
382170754Sdelphij      if (NumArgs > 2) {
383170754Sdelphij        std::vector<std::string> EnvVars;
384170754Sdelphij        for (unsigned i = 0; envp[i]; ++i)
385170754Sdelphij          EnvVars.push_back(envp[i]);
386170754Sdelphij        // Arg #2 = envp.
387170754Sdelphij        GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
388170754Sdelphij      }
389170754Sdelphij    }
390170754Sdelphij  }
391170754Sdelphij
392170754Sdelphij  return runFunction(Fn, GVArgs).IntVal.getZExtValue();
393170754Sdelphij}
394170754Sdelphij
395170754SdelphijExecutionEngine *ExecutionEngine::create(Module *M,
396170754Sdelphij                                         bool ForceInterpreter,
397170754Sdelphij                                         std::string *ErrorStr,
398170754Sdelphij                                         CodeGenOpt::Level OptLevel,
399170754Sdelphij                                         bool GVsWithCode) {
400170754Sdelphij  EngineBuilder EB =  EngineBuilder(M)
401170754Sdelphij      .setEngineKind(ForceInterpreter
402170754Sdelphij                     ? EngineKind::Interpreter
403170754Sdelphij                     : EngineKind::JIT)
404170754Sdelphij      .setErrorStr(ErrorStr)
405170754Sdelphij      .setOptLevel(OptLevel)
406170754Sdelphij      .setAllocateGVsWithCode(GVsWithCode);
407170754Sdelphij
408170754Sdelphij  return EB.create();
409170754Sdelphij}
410170754Sdelphij
411170754Sdelphij/// createJIT - This is the factory method for creating a JIT for the current
412170754Sdelphij/// machine, it does not fall back to the interpreter.  This takes ownership
413170754Sdelphij/// of the module.
414170754SdelphijExecutionEngine *ExecutionEngine::createJIT(Module *M,
415170754Sdelphij                                            std::string *ErrorStr,
416170754Sdelphij                                            JITMemoryManager *JMM,
417170754Sdelphij                                            CodeGenOpt::Level OL,
418170754Sdelphij                                            bool GVsWithCode,
419170754Sdelphij                                            Reloc::Model RM,
420170754Sdelphij                                            CodeModel::Model CMM) {
421170754Sdelphij  if (ExecutionEngine::JITCtor == 0) {
422170754Sdelphij    if (ErrorStr)
423170754Sdelphij      *ErrorStr = "JIT has not been linked in.";
424170754Sdelphij    return 0;
425170754Sdelphij  }
426170754Sdelphij
427170754Sdelphij  // Use the defaults for extra parameters.  Users can use EngineBuilder to
428170754Sdelphij  // set them.
429170754Sdelphij  EngineBuilder EB(M);
430170754Sdelphij  EB.setEngineKind(EngineKind::JIT);
431170754Sdelphij  EB.setErrorStr(ErrorStr);
432170754Sdelphij  EB.setRelocationModel(RM);
433170754Sdelphij  EB.setCodeModel(CMM);
434170754Sdelphij  EB.setAllocateGVsWithCode(GVsWithCode);
435170754Sdelphij  EB.setOptLevel(OL);
436170754Sdelphij  EB.setJITMemoryManager(JMM);
437170754Sdelphij
438170754Sdelphij  // TODO: permit custom TargetOptions here
439170754Sdelphij  TargetMachine *TM = EB.selectTarget();
440170754Sdelphij  if (!TM || (ErrorStr && ErrorStr->length() > 0)) return 0;
441170754Sdelphij
442170754Sdelphij  return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
443170754Sdelphij}
444170754Sdelphij
445170754SdelphijExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
446170754Sdelphij  OwningPtr<TargetMachine> TheTM(TM); // Take ownership.
447170754Sdelphij
448170754Sdelphij  // Make sure we can resolve symbols in the program as well. The zero arg
449170754Sdelphij  // to the function tells DynamicLibrary to load the program, not a library.
450170754Sdelphij  if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
451170754Sdelphij    return 0;
452170754Sdelphij
453170754Sdelphij  assert(!(JMM && MCJMM));
454170754Sdelphij
455170754Sdelphij  // If the user specified a memory manager but didn't specify which engine to
456170754Sdelphij  // create, we assume they only want the JIT, and we fail if they only want
457170754Sdelphij  // the interpreter.
458170754Sdelphij  if (JMM || MCJMM) {
459170754Sdelphij    if (WhichEngine & EngineKind::JIT)
460170754Sdelphij      WhichEngine = EngineKind::JIT;
461170754Sdelphij    else {
462170754Sdelphij      if (ErrorStr)
463170754Sdelphij        *ErrorStr = "Cannot create an interpreter with a memory manager.";
464170754Sdelphij      return 0;
465170754Sdelphij    }
466170754Sdelphij  }
467170754Sdelphij
468170754Sdelphij  if (MCJMM && ! UseMCJIT) {
469170754Sdelphij    if (ErrorStr)
470170754Sdelphij      *ErrorStr =
471170754Sdelphij        "Cannot create a legacy JIT with a runtime dyld memory "
472170754Sdelphij        "manager.";
473170754Sdelphij    return 0;
474170754Sdelphij  }
475170754Sdelphij
476170754Sdelphij  // Unless the interpreter was explicitly selected or the JIT is not linked,
477170754Sdelphij  // try making a JIT.
478170754Sdelphij  if ((WhichEngine & EngineKind::JIT) && TheTM) {
479170754Sdelphij    Triple TT(M->getTargetTriple());
480170754Sdelphij    if (!TM->getTarget().hasJIT()) {
481170754Sdelphij      errs() << "WARNING: This target JIT is not designed for the host"
482170754Sdelphij             << " you are running.  If bad things happen, please choose"
483170754Sdelphij             << " a different -march switch.\n";
484170754Sdelphij    }
485170754Sdelphij
486170754Sdelphij    if (UseMCJIT && ExecutionEngine::MCJITCtor) {
487170754Sdelphij      ExecutionEngine *EE =
488170754Sdelphij        ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
489170754Sdelphij                                   AllocateGVsWithCode, TheTM.take());
490170754Sdelphij      if (EE) return EE;
491170754Sdelphij    } else if (ExecutionEngine::JITCtor) {
492170754Sdelphij      ExecutionEngine *EE =
493170754Sdelphij        ExecutionEngine::JITCtor(M, ErrorStr, JMM,
494170754Sdelphij                                 AllocateGVsWithCode, TheTM.take());
495170754Sdelphij      if (EE) return EE;
496170754Sdelphij    }
497170754Sdelphij  }
498170754Sdelphij
499170754Sdelphij  // If we can't make a JIT and we didn't request one specifically, try making
500170754Sdelphij  // an interpreter instead.
501170754Sdelphij  if (WhichEngine & EngineKind::Interpreter) {
502170754Sdelphij    if (ExecutionEngine::InterpCtor)
503170754Sdelphij      return ExecutionEngine::InterpCtor(M, ErrorStr);
504170754Sdelphij    if (ErrorStr)
505170754Sdelphij      *ErrorStr = "Interpreter has not been linked in.";
506170754Sdelphij    return 0;
507170754Sdelphij  }
508170754Sdelphij
509170754Sdelphij  if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0 &&
510170754Sdelphij      ExecutionEngine::MCJITCtor == 0) {
511170754Sdelphij    if (ErrorStr)
512170754Sdelphij      *ErrorStr = "JIT has not been linked in.";
513170754Sdelphij  }
514170754Sdelphij
515170754Sdelphij  return 0;
516170754Sdelphij}
517170754Sdelphij
518170754Sdelphijvoid *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
519170754Sdelphij  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
520170754Sdelphij    return getPointerToFunction(F);
521170754Sdelphij
522170754Sdelphij  MutexGuard locked(lock);
523170754Sdelphij  if (void *P = EEState.getGlobalAddressMap(locked)[GV])
524170754Sdelphij    return P;
525170754Sdelphij
526170754Sdelphij  // Global variable might have been added since interpreter started.
527170754Sdelphij  if (GlobalVariable *GVar =
528170754Sdelphij          const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
529170754Sdelphij    EmitGlobalVariable(GVar);
530170754Sdelphij  else
531170754Sdelphij    llvm_unreachable("Global hasn't had an address allocated yet!");
532170754Sdelphij
533170754Sdelphij  return EEState.getGlobalAddressMap(locked)[GV];
534170754Sdelphij}
535170754Sdelphij
536170754Sdelphij/// \brief Converts a Constant* into a GenericValue, including handling of
537170754Sdelphij/// ConstantExpr values.
538170754SdelphijGenericValue ExecutionEngine::getConstantValue(const Constant *C) {
539170754Sdelphij  // If its undefined, return the garbage.
540170754Sdelphij  if (isa<UndefValue>(C)) {
541170754Sdelphij    GenericValue Result;
542170754Sdelphij    switch (C->getType()->getTypeID()) {
543170754Sdelphij    default:
544170754Sdelphij      break;
545170754Sdelphij    case Type::IntegerTyID:
546170754Sdelphij    case Type::X86_FP80TyID:
547170754Sdelphij    case Type::FP128TyID:
548170754Sdelphij    case Type::PPC_FP128TyID:
549170754Sdelphij      // Although the value is undefined, we still have to construct an APInt
550170754Sdelphij      // with the correct bit width.
551170754Sdelphij      Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
552170754Sdelphij      break;
553170754Sdelphij    case Type::StructTyID: {
554170754Sdelphij      // if the whole struct is 'undef' just reserve memory for the value.
555170754Sdelphij      if(StructType *STy = dyn_cast<StructType>(C->getType())) {
556170754Sdelphij        unsigned int elemNum = STy->getNumElements();
557170754Sdelphij        Result.AggregateVal.resize(elemNum);
558170754Sdelphij        for (unsigned int i = 0; i < elemNum; ++i) {
559170754Sdelphij          Type *ElemTy = STy->getElementType(i);
560170754Sdelphij          if (ElemTy->isIntegerTy())
561170754Sdelphij            Result.AggregateVal[i].IntVal =
562170754Sdelphij              APInt(ElemTy->getPrimitiveSizeInBits(), 0);
563170754Sdelphij          else if (ElemTy->isAggregateType()) {
564170754Sdelphij              const Constant *ElemUndef = UndefValue::get(ElemTy);
565170754Sdelphij              Result.AggregateVal[i] = getConstantValue(ElemUndef);
566170754Sdelphij            }
567170754Sdelphij          }
568170754Sdelphij        }
569170754Sdelphij      }
570170754Sdelphij      break;
571170754Sdelphij    case Type::VectorTyID:
572170754Sdelphij      // if the whole vector is 'undef' just reserve memory for the value.
573170754Sdelphij      const VectorType* VTy = dyn_cast<VectorType>(C->getType());
574170754Sdelphij      const Type *ElemTy = VTy->getElementType();
575170754Sdelphij      unsigned int elemNum = VTy->getNumElements();
576170754Sdelphij      Result.AggregateVal.resize(elemNum);
577170754Sdelphij      if (ElemTy->isIntegerTy())
578170754Sdelphij        for (unsigned int i = 0; i < elemNum; ++i)
579170754Sdelphij          Result.AggregateVal[i].IntVal =
580170754Sdelphij            APInt(ElemTy->getPrimitiveSizeInBits(), 0);
581170754Sdelphij      break;
582170754Sdelphij    }
583170754Sdelphij    return Result;
584170754Sdelphij  }
585170754Sdelphij
586170754Sdelphij  // Otherwise, if the value is a ConstantExpr...
587170754Sdelphij  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
588170754Sdelphij    Constant *Op0 = CE->getOperand(0);
589170754Sdelphij    switch (CE->getOpcode()) {
590170754Sdelphij    case Instruction::GetElementPtr: {
591170754Sdelphij      // Compute the index
592170754Sdelphij      GenericValue Result = getConstantValue(Op0);
593170754Sdelphij      APInt Offset(TD->getPointerSizeInBits(), 0);
594170754Sdelphij      cast<GEPOperator>(CE)->accumulateConstantOffset(*TD, Offset);
595170754Sdelphij
596170754Sdelphij      char* tmp = (char*) Result.PointerVal;
597170754Sdelphij      Result = PTOGV(tmp + Offset.getSExtValue());
598170754Sdelphij      return Result;
599170754Sdelphij    }
600170754Sdelphij    case Instruction::Trunc: {
601170754Sdelphij      GenericValue GV = getConstantValue(Op0);
602170754Sdelphij      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
603170754Sdelphij      GV.IntVal = GV.IntVal.trunc(BitWidth);
604170754Sdelphij      return GV;
605170754Sdelphij    }
606170754Sdelphij    case Instruction::ZExt: {
607170754Sdelphij      GenericValue GV = getConstantValue(Op0);
608170754Sdelphij      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
609170754Sdelphij      GV.IntVal = GV.IntVal.zext(BitWidth);
610170754Sdelphij      return GV;
611170754Sdelphij    }
612170754Sdelphij    case Instruction::SExt: {
613170754Sdelphij      GenericValue GV = getConstantValue(Op0);
614170754Sdelphij      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
615170754Sdelphij      GV.IntVal = GV.IntVal.sext(BitWidth);
616170754Sdelphij      return GV;
617170754Sdelphij    }
618170754Sdelphij    case Instruction::FPTrunc: {
619170754Sdelphij      // FIXME long double
620170754Sdelphij      GenericValue GV = getConstantValue(Op0);
621170754Sdelphij      GV.FloatVal = float(GV.DoubleVal);
622170754Sdelphij      return GV;
623170754Sdelphij    }
624170754Sdelphij    case Instruction::FPExt:{
625170754Sdelphij      // FIXME long double
626170754Sdelphij      GenericValue GV = getConstantValue(Op0);
627170754Sdelphij      GV.DoubleVal = double(GV.FloatVal);
628170754Sdelphij      return GV;
629170754Sdelphij    }
630170754Sdelphij    case Instruction::UIToFP: {
631170754Sdelphij      GenericValue GV = getConstantValue(Op0);
632170754Sdelphij      if (CE->getType()->isFloatTy())
633170754Sdelphij        GV.FloatVal = float(GV.IntVal.roundToDouble());
634170754Sdelphij      else if (CE->getType()->isDoubleTy())
635170754Sdelphij        GV.DoubleVal = GV.IntVal.roundToDouble();
636170754Sdelphij      else if (CE->getType()->isX86_FP80Ty()) {
637170754Sdelphij        APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
638170754Sdelphij        (void)apf.convertFromAPInt(GV.IntVal,
639170754Sdelphij                                   false,
640170754Sdelphij                                   APFloat::rmNearestTiesToEven);
641170754Sdelphij        GV.IntVal = apf.bitcastToAPInt();
642170754Sdelphij      }
643170754Sdelphij      return GV;
644170754Sdelphij    }
645170754Sdelphij    case Instruction::SIToFP: {
646170754Sdelphij      GenericValue GV = getConstantValue(Op0);
647170754Sdelphij      if (CE->getType()->isFloatTy())
648170754Sdelphij        GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
649170754Sdelphij      else if (CE->getType()->isDoubleTy())
650170754Sdelphij        GV.DoubleVal = GV.IntVal.signedRoundToDouble();
651170754Sdelphij      else if (CE->getType()->isX86_FP80Ty()) {
652170754Sdelphij        APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
653170754Sdelphij        (void)apf.convertFromAPInt(GV.IntVal,
654170754Sdelphij                                   true,
655170754Sdelphij                                   APFloat::rmNearestTiesToEven);
656170754Sdelphij        GV.IntVal = apf.bitcastToAPInt();
657170754Sdelphij      }
658170754Sdelphij      return GV;
659170754Sdelphij    }
660170754Sdelphij    case Instruction::FPToUI: // double->APInt conversion handles sign
661170754Sdelphij    case Instruction::FPToSI: {
662170754Sdelphij      GenericValue GV = getConstantValue(Op0);
663170754Sdelphij      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
664170754Sdelphij      if (Op0->getType()->isFloatTy())
665170754Sdelphij        GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
666170754Sdelphij      else if (Op0->getType()->isDoubleTy())
667170754Sdelphij        GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
668170754Sdelphij      else if (Op0->getType()->isX86_FP80Ty()) {
669170754Sdelphij        APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
670170754Sdelphij        uint64_t v;
671170754Sdelphij        bool ignored;
672170754Sdelphij        (void)apf.convertToInteger(&v, BitWidth,
673170754Sdelphij                                   CE->getOpcode()==Instruction::FPToSI,
674170754Sdelphij                                   APFloat::rmTowardZero, &ignored);
675170754Sdelphij        GV.IntVal = v; // endian?
676170754Sdelphij      }
677170754Sdelphij      return GV;
678170754Sdelphij    }
679170754Sdelphij    case Instruction::PtrToInt: {
680170754Sdelphij      GenericValue GV = getConstantValue(Op0);
681170754Sdelphij      uint32_t PtrWidth = TD->getTypeSizeInBits(Op0->getType());
682170754Sdelphij      assert(PtrWidth <= 64 && "Bad pointer width");
683170754Sdelphij      GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
684170754Sdelphij      uint32_t IntWidth = TD->getTypeSizeInBits(CE->getType());
685170754Sdelphij      GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
686170754Sdelphij      return GV;
687170754Sdelphij    }
688170754Sdelphij    case Instruction::IntToPtr: {
689170754Sdelphij      GenericValue GV = getConstantValue(Op0);
690170754Sdelphij      uint32_t PtrWidth = TD->getTypeSizeInBits(CE->getType());
691170754Sdelphij      GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
692170754Sdelphij      assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
693170754Sdelphij      GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
694170754Sdelphij      return GV;
695170754Sdelphij    }
696170754Sdelphij    case Instruction::BitCast: {
697170754Sdelphij      GenericValue GV = getConstantValue(Op0);
698170754Sdelphij      Type* DestTy = CE->getType();
699170754Sdelphij      switch (Op0->getType()->getTypeID()) {
700170754Sdelphij        default: llvm_unreachable("Invalid bitcast operand");
701170754Sdelphij        case Type::IntegerTyID:
702170754Sdelphij          assert(DestTy->isFloatingPointTy() && "invalid bitcast");
703170754Sdelphij          if (DestTy->isFloatTy())
704170754Sdelphij            GV.FloatVal = GV.IntVal.bitsToFloat();
705170754Sdelphij          else if (DestTy->isDoubleTy())
706170754Sdelphij            GV.DoubleVal = GV.IntVal.bitsToDouble();
707170754Sdelphij          break;
708170754Sdelphij        case Type::FloatTyID:
709170754Sdelphij          assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
710170754Sdelphij          GV.IntVal = APInt::floatToBits(GV.FloatVal);
711170754Sdelphij          break;
712170754Sdelphij        case Type::DoubleTyID:
713170754Sdelphij          assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
714170754Sdelphij          GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
715170754Sdelphij          break;
716170754Sdelphij        case Type::PointerTyID:
717170754Sdelphij          assert(DestTy->isPointerTy() && "Invalid bitcast");
718170754Sdelphij          break; // getConstantValue(Op0)  above already converted it
719170754Sdelphij      }
720170754Sdelphij      return GV;
721170754Sdelphij    }
722170754Sdelphij    case Instruction::Add:
723170754Sdelphij    case Instruction::FAdd:
724170754Sdelphij    case Instruction::Sub:
725170754Sdelphij    case Instruction::FSub:
726170754Sdelphij    case Instruction::Mul:
727170754Sdelphij    case Instruction::FMul:
728170754Sdelphij    case Instruction::UDiv:
729170754Sdelphij    case Instruction::SDiv:
730170754Sdelphij    case Instruction::URem:
731170754Sdelphij    case Instruction::SRem:
732170754Sdelphij    case Instruction::And:
733170754Sdelphij    case Instruction::Or:
734170754Sdelphij    case Instruction::Xor: {
735170754Sdelphij      GenericValue LHS = getConstantValue(Op0);
736170754Sdelphij      GenericValue RHS = getConstantValue(CE->getOperand(1));
737170754Sdelphij      GenericValue GV;
738170754Sdelphij      switch (CE->getOperand(0)->getType()->getTypeID()) {
739170754Sdelphij      default: llvm_unreachable("Bad add type!");
740170754Sdelphij      case Type::IntegerTyID:
741170754Sdelphij        switch (CE->getOpcode()) {
742170754Sdelphij          default: llvm_unreachable("Invalid integer opcode");
743170754Sdelphij          case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
744170754Sdelphij          case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
745170754Sdelphij          case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
746170754Sdelphij          case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
747170754Sdelphij          case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
748170754Sdelphij          case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
749170754Sdelphij          case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
750170754Sdelphij          case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
751170754Sdelphij          case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
752170754Sdelphij          case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
753170754Sdelphij        }
754170754Sdelphij        break;
755170754Sdelphij      case Type::FloatTyID:
756170754Sdelphij        switch (CE->getOpcode()) {
757170754Sdelphij          default: llvm_unreachable("Invalid float opcode");
758170754Sdelphij          case Instruction::FAdd:
759170754Sdelphij            GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
760170754Sdelphij          case Instruction::FSub:
761170754Sdelphij            GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
762170754Sdelphij          case Instruction::FMul:
763170754Sdelphij            GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
764170754Sdelphij          case Instruction::FDiv:
765170754Sdelphij            GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
766170754Sdelphij          case Instruction::FRem:
767170754Sdelphij            GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
768170754Sdelphij        }
769        break;
770      case Type::DoubleTyID:
771        switch (CE->getOpcode()) {
772          default: llvm_unreachable("Invalid double opcode");
773          case Instruction::FAdd:
774            GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
775          case Instruction::FSub:
776            GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
777          case Instruction::FMul:
778            GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
779          case Instruction::FDiv:
780            GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
781          case Instruction::FRem:
782            GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
783        }
784        break;
785      case Type::X86_FP80TyID:
786      case Type::PPC_FP128TyID:
787      case Type::FP128TyID: {
788        const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
789        APFloat apfLHS = APFloat(Sem, LHS.IntVal);
790        switch (CE->getOpcode()) {
791          default: llvm_unreachable("Invalid long double opcode");
792          case Instruction::FAdd:
793            apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
794            GV.IntVal = apfLHS.bitcastToAPInt();
795            break;
796          case Instruction::FSub:
797            apfLHS.subtract(APFloat(Sem, RHS.IntVal),
798                            APFloat::rmNearestTiesToEven);
799            GV.IntVal = apfLHS.bitcastToAPInt();
800            break;
801          case Instruction::FMul:
802            apfLHS.multiply(APFloat(Sem, RHS.IntVal),
803                            APFloat::rmNearestTiesToEven);
804            GV.IntVal = apfLHS.bitcastToAPInt();
805            break;
806          case Instruction::FDiv:
807            apfLHS.divide(APFloat(Sem, RHS.IntVal),
808                          APFloat::rmNearestTiesToEven);
809            GV.IntVal = apfLHS.bitcastToAPInt();
810            break;
811          case Instruction::FRem:
812            apfLHS.mod(APFloat(Sem, RHS.IntVal),
813                       APFloat::rmNearestTiesToEven);
814            GV.IntVal = apfLHS.bitcastToAPInt();
815            break;
816          }
817        }
818        break;
819      }
820      return GV;
821    }
822    default:
823      break;
824    }
825
826    SmallString<256> Msg;
827    raw_svector_ostream OS(Msg);
828    OS << "ConstantExpr not handled: " << *CE;
829    report_fatal_error(OS.str());
830  }
831
832  // Otherwise, we have a simple constant.
833  GenericValue Result;
834  switch (C->getType()->getTypeID()) {
835  case Type::FloatTyID:
836    Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
837    break;
838  case Type::DoubleTyID:
839    Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
840    break;
841  case Type::X86_FP80TyID:
842  case Type::FP128TyID:
843  case Type::PPC_FP128TyID:
844    Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
845    break;
846  case Type::IntegerTyID:
847    Result.IntVal = cast<ConstantInt>(C)->getValue();
848    break;
849  case Type::PointerTyID:
850    if (isa<ConstantPointerNull>(C))
851      Result.PointerVal = 0;
852    else if (const Function *F = dyn_cast<Function>(C))
853      Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
854    else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
855      Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
856    else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
857      Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
858                                                        BA->getBasicBlock())));
859    else
860      llvm_unreachable("Unknown constant pointer type!");
861    break;
862  case Type::VectorTyID: {
863    unsigned elemNum;
864    Type* ElemTy;
865    const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
866    const ConstantVector *CV = dyn_cast<ConstantVector>(C);
867    const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
868
869    if (CDV) {
870        elemNum = CDV->getNumElements();
871        ElemTy = CDV->getElementType();
872    } else if (CV || CAZ) {
873        VectorType* VTy = dyn_cast<VectorType>(C->getType());
874        elemNum = VTy->getNumElements();
875        ElemTy = VTy->getElementType();
876    } else {
877        llvm_unreachable("Unknown constant vector type!");
878    }
879
880    Result.AggregateVal.resize(elemNum);
881    // Check if vector holds floats.
882    if(ElemTy->isFloatTy()) {
883      if (CAZ) {
884        GenericValue floatZero;
885        floatZero.FloatVal = 0.f;
886        std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
887                  floatZero);
888        break;
889      }
890      if(CV) {
891        for (unsigned i = 0; i < elemNum; ++i)
892          if (!isa<UndefValue>(CV->getOperand(i)))
893            Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
894              CV->getOperand(i))->getValueAPF().convertToFloat();
895        break;
896      }
897      if(CDV)
898        for (unsigned i = 0; i < elemNum; ++i)
899          Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
900
901      break;
902    }
903    // Check if vector holds doubles.
904    if (ElemTy->isDoubleTy()) {
905      if (CAZ) {
906        GenericValue doubleZero;
907        doubleZero.DoubleVal = 0.0;
908        std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
909                  doubleZero);
910        break;
911      }
912      if(CV) {
913        for (unsigned i = 0; i < elemNum; ++i)
914          if (!isa<UndefValue>(CV->getOperand(i)))
915            Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
916              CV->getOperand(i))->getValueAPF().convertToDouble();
917        break;
918      }
919      if(CDV)
920        for (unsigned i = 0; i < elemNum; ++i)
921          Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
922
923      break;
924    }
925    // Check if vector holds integers.
926    if (ElemTy->isIntegerTy()) {
927      if (CAZ) {
928        GenericValue intZero;
929        intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
930        std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
931                  intZero);
932        break;
933      }
934      if(CV) {
935        for (unsigned i = 0; i < elemNum; ++i)
936          if (!isa<UndefValue>(CV->getOperand(i)))
937            Result.AggregateVal[i].IntVal = cast<ConstantInt>(
938                                            CV->getOperand(i))->getValue();
939          else {
940            Result.AggregateVal[i].IntVal =
941              APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
942          }
943        break;
944      }
945      if(CDV)
946        for (unsigned i = 0; i < elemNum; ++i)
947          Result.AggregateVal[i].IntVal = APInt(
948            CDV->getElementType()->getPrimitiveSizeInBits(),
949            CDV->getElementAsInteger(i));
950
951      break;
952    }
953    llvm_unreachable("Unknown constant pointer type!");
954  }
955  break;
956
957  default:
958    SmallString<256> Msg;
959    raw_svector_ostream OS(Msg);
960    OS << "ERROR: Constant unimplemented for type: " << *C->getType();
961    report_fatal_error(OS.str());
962  }
963
964  return Result;
965}
966
967/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
968/// with the integer held in IntVal.
969static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
970                             unsigned StoreBytes) {
971  assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
972  const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
973
974  if (sys::IsLittleEndianHost) {
975    // Little-endian host - the source is ordered from LSB to MSB.  Order the
976    // destination from LSB to MSB: Do a straight copy.
977    memcpy(Dst, Src, StoreBytes);
978  } else {
979    // Big-endian host - the source is an array of 64 bit words ordered from
980    // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
981    // from MSB to LSB: Reverse the word order, but not the bytes in a word.
982    while (StoreBytes > sizeof(uint64_t)) {
983      StoreBytes -= sizeof(uint64_t);
984      // May not be aligned so use memcpy.
985      memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
986      Src += sizeof(uint64_t);
987    }
988
989    memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
990  }
991}
992
993void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
994                                         GenericValue *Ptr, Type *Ty) {
995  const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
996
997  switch (Ty->getTypeID()) {
998  default:
999    dbgs() << "Cannot store value of type " << *Ty << "!\n";
1000    break;
1001  case Type::IntegerTyID:
1002    StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
1003    break;
1004  case Type::FloatTyID:
1005    *((float*)Ptr) = Val.FloatVal;
1006    break;
1007  case Type::DoubleTyID:
1008    *((double*)Ptr) = Val.DoubleVal;
1009    break;
1010  case Type::X86_FP80TyID:
1011    memcpy(Ptr, Val.IntVal.getRawData(), 10);
1012    break;
1013  case Type::PointerTyID:
1014    // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1015    if (StoreBytes != sizeof(PointerTy))
1016      memset(&(Ptr->PointerVal), 0, StoreBytes);
1017
1018    *((PointerTy*)Ptr) = Val.PointerVal;
1019    break;
1020  case Type::VectorTyID:
1021    for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1022      if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1023        *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1024      if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1025        *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1026      if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1027        unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1028        StoreIntToMemory(Val.AggregateVal[i].IntVal,
1029          (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1030      }
1031    }
1032    break;
1033  }
1034
1035  if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
1036    // Host and target are different endian - reverse the stored bytes.
1037    std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1038}
1039
1040/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1041/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1042static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1043  assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
1044  uint8_t *Dst = reinterpret_cast<uint8_t *>(
1045                   const_cast<uint64_t *>(IntVal.getRawData()));
1046
1047  if (sys::IsLittleEndianHost)
1048    // Little-endian host - the destination must be ordered from LSB to MSB.
1049    // The source is ordered from LSB to MSB: Do a straight copy.
1050    memcpy(Dst, Src, LoadBytes);
1051  else {
1052    // Big-endian - the destination is an array of 64 bit words ordered from
1053    // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
1054    // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1055    // a word.
1056    while (LoadBytes > sizeof(uint64_t)) {
1057      LoadBytes -= sizeof(uint64_t);
1058      // May not be aligned so use memcpy.
1059      memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1060      Dst += sizeof(uint64_t);
1061    }
1062
1063    memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1064  }
1065}
1066
1067/// FIXME: document
1068///
1069void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
1070                                          GenericValue *Ptr,
1071                                          Type *Ty) {
1072  const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
1073
1074  switch (Ty->getTypeID()) {
1075  case Type::IntegerTyID:
1076    // An APInt with all words initially zero.
1077    Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1078    LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1079    break;
1080  case Type::FloatTyID:
1081    Result.FloatVal = *((float*)Ptr);
1082    break;
1083  case Type::DoubleTyID:
1084    Result.DoubleVal = *((double*)Ptr);
1085    break;
1086  case Type::PointerTyID:
1087    Result.PointerVal = *((PointerTy*)Ptr);
1088    break;
1089  case Type::X86_FP80TyID: {
1090    // This is endian dependent, but it will only work on x86 anyway.
1091    // FIXME: Will not trap if loading a signaling NaN.
1092    uint64_t y[2];
1093    memcpy(y, Ptr, 10);
1094    Result.IntVal = APInt(80, y);
1095    break;
1096  }
1097  case Type::VectorTyID: {
1098    const VectorType *VT = cast<VectorType>(Ty);
1099    const Type *ElemT = VT->getElementType();
1100    const unsigned numElems = VT->getNumElements();
1101    if (ElemT->isFloatTy()) {
1102      Result.AggregateVal.resize(numElems);
1103      for (unsigned i = 0; i < numElems; ++i)
1104        Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1105    }
1106    if (ElemT->isDoubleTy()) {
1107      Result.AggregateVal.resize(numElems);
1108      for (unsigned i = 0; i < numElems; ++i)
1109        Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1110    }
1111    if (ElemT->isIntegerTy()) {
1112      GenericValue intZero;
1113      const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1114      intZero.IntVal = APInt(elemBitWidth, 0);
1115      Result.AggregateVal.resize(numElems, intZero);
1116      for (unsigned i = 0; i < numElems; ++i)
1117        LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1118          (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1119    }
1120  break;
1121  }
1122  default:
1123    SmallString<256> Msg;
1124    raw_svector_ostream OS(Msg);
1125    OS << "Cannot load value of type " << *Ty << "!";
1126    report_fatal_error(OS.str());
1127  }
1128}
1129
1130void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
1131  DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1132  DEBUG(Init->dump());
1133  if (isa<UndefValue>(Init))
1134    return;
1135
1136  if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1137    unsigned ElementSize =
1138      getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
1139    for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1140      InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1141    return;
1142  }
1143
1144  if (isa<ConstantAggregateZero>(Init)) {
1145    memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
1146    return;
1147  }
1148
1149  if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1150    unsigned ElementSize =
1151      getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
1152    for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1153      InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1154    return;
1155  }
1156
1157  if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1158    const StructLayout *SL =
1159      getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
1160    for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1161      InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1162    return;
1163  }
1164
1165  if (const ConstantDataSequential *CDS =
1166               dyn_cast<ConstantDataSequential>(Init)) {
1167    // CDS is already laid out in host memory order.
1168    StringRef Data = CDS->getRawDataValues();
1169    memcpy(Addr, Data.data(), Data.size());
1170    return;
1171  }
1172
1173  if (Init->getType()->isFirstClassType()) {
1174    GenericValue Val = getConstantValue(Init);
1175    StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1176    return;
1177  }
1178
1179  DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1180  llvm_unreachable("Unknown constant type to initialize memory with!");
1181}
1182
1183/// EmitGlobals - Emit all of the global variables to memory, storing their
1184/// addresses into GlobalAddress.  This must make sure to copy the contents of
1185/// their initializers into the memory.
1186void ExecutionEngine::emitGlobals() {
1187  // Loop over all of the global variables in the program, allocating the memory
1188  // to hold them.  If there is more than one module, do a prepass over globals
1189  // to figure out how the different modules should link together.
1190  std::map<std::pair<std::string, Type*>,
1191           const GlobalValue*> LinkedGlobalsMap;
1192
1193  if (Modules.size() != 1) {
1194    for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1195      Module &M = *Modules[m];
1196      for (Module::const_global_iterator I = M.global_begin(),
1197           E = M.global_end(); I != E; ++I) {
1198        const GlobalValue *GV = I;
1199        if (GV->hasLocalLinkage() || GV->isDeclaration() ||
1200            GV->hasAppendingLinkage() || !GV->hasName())
1201          continue;// Ignore external globals and globals with internal linkage.
1202
1203        const GlobalValue *&GVEntry =
1204          LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1205
1206        // If this is the first time we've seen this global, it is the canonical
1207        // version.
1208        if (!GVEntry) {
1209          GVEntry = GV;
1210          continue;
1211        }
1212
1213        // If the existing global is strong, never replace it.
1214        if (GVEntry->hasExternalLinkage() ||
1215            GVEntry->hasDLLImportLinkage() ||
1216            GVEntry->hasDLLExportLinkage())
1217          continue;
1218
1219        // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1220        // symbol.  FIXME is this right for common?
1221        if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1222          GVEntry = GV;
1223      }
1224    }
1225  }
1226
1227  std::vector<const GlobalValue*> NonCanonicalGlobals;
1228  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1229    Module &M = *Modules[m];
1230    for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1231         I != E; ++I) {
1232      // In the multi-module case, see what this global maps to.
1233      if (!LinkedGlobalsMap.empty()) {
1234        if (const GlobalValue *GVEntry =
1235              LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1236          // If something else is the canonical global, ignore this one.
1237          if (GVEntry != &*I) {
1238            NonCanonicalGlobals.push_back(I);
1239            continue;
1240          }
1241        }
1242      }
1243
1244      if (!I->isDeclaration()) {
1245        addGlobalMapping(I, getMemoryForGV(I));
1246      } else {
1247        // External variable reference. Try to use the dynamic loader to
1248        // get a pointer to it.
1249        if (void *SymAddr =
1250            sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
1251          addGlobalMapping(I, SymAddr);
1252        else {
1253          report_fatal_error("Could not resolve external global address: "
1254                            +I->getName());
1255        }
1256      }
1257    }
1258
1259    // If there are multiple modules, map the non-canonical globals to their
1260    // canonical location.
1261    if (!NonCanonicalGlobals.empty()) {
1262      for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1263        const GlobalValue *GV = NonCanonicalGlobals[i];
1264        const GlobalValue *CGV =
1265          LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1266        void *Ptr = getPointerToGlobalIfAvailable(CGV);
1267        assert(Ptr && "Canonical global wasn't codegen'd!");
1268        addGlobalMapping(GV, Ptr);
1269      }
1270    }
1271
1272    // Now that all of the globals are set up in memory, loop through them all
1273    // and initialize their contents.
1274    for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1275         I != E; ++I) {
1276      if (!I->isDeclaration()) {
1277        if (!LinkedGlobalsMap.empty()) {
1278          if (const GlobalValue *GVEntry =
1279                LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1280            if (GVEntry != &*I)  // Not the canonical variable.
1281              continue;
1282        }
1283        EmitGlobalVariable(I);
1284      }
1285    }
1286  }
1287}
1288
1289// EmitGlobalVariable - This method emits the specified global variable to the
1290// address specified in GlobalAddresses, or allocates new memory if it's not
1291// already in the map.
1292void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1293  void *GA = getPointerToGlobalIfAvailable(GV);
1294
1295  if (GA == 0) {
1296    // If it's not already specified, allocate memory for the global.
1297    GA = getMemoryForGV(GV);
1298
1299    // If we failed to allocate memory for this global, return.
1300    if (GA == 0) return;
1301
1302    addGlobalMapping(GV, GA);
1303  }
1304
1305  // Don't initialize if it's thread local, let the client do it.
1306  if (!GV->isThreadLocal())
1307    InitializeMemory(GV->getInitializer(), GA);
1308
1309  Type *ElTy = GV->getType()->getElementType();
1310  size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
1311  NumInitBytes += (unsigned)GVSize;
1312  ++NumGlobals;
1313}
1314
1315ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1316  : EE(EE), GlobalAddressMap(this) {
1317}
1318
1319sys::Mutex *
1320ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
1321  return &EES->EE.lock;
1322}
1323
1324void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1325                                                      const GlobalValue *Old) {
1326  void *OldVal = EES->GlobalAddressMap.lookup(Old);
1327  EES->GlobalAddressReverseMap.erase(OldVal);
1328}
1329
1330void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1331                                                    const GlobalValue *,
1332                                                    const GlobalValue *) {
1333  llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1334                   " RAUW on a value it has a global mapping for.");
1335}
1336