ExecutionEngine.cpp revision 2c44a80d991df258a45e2f5fa76d5ada9e99015c
1//===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the common interface used by the various execution engine
11// subclasses.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "jit"
16#include "llvm/ExecutionEngine/ExecutionEngine.h"
17
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Module.h"
21#include "llvm/ExecutionEngine/GenericValue.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/MutexGuard.h"
27#include "llvm/Support/ValueHandle.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/Support/DynamicLibrary.h"
30#include "llvm/Support/Host.h"
31#include "llvm/Target/TargetData.h"
32#include <cmath>
33#include <cstring>
34using namespace llvm;
35
36STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
37STATISTIC(NumGlobals  , "Number of global vars initialized");
38
39ExecutionEngine *(*ExecutionEngine::JITCtor)(
40  Module *M,
41  std::string *ErrorStr,
42  JITMemoryManager *JMM,
43  CodeGenOpt::Level OptLevel,
44  bool GVsWithCode,
45  CodeModel::Model CMM,
46  StringRef MArch,
47  StringRef MCPU,
48  const SmallVectorImpl<std::string>& MAttrs) = 0;
49ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
50  Module *M,
51  std::string *ErrorStr,
52  JITMemoryManager *JMM,
53  CodeGenOpt::Level OptLevel,
54  bool GVsWithCode,
55  CodeModel::Model CMM,
56  StringRef MArch,
57  StringRef MCPU,
58  const SmallVectorImpl<std::string>& MAttrs) = 0;
59ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
60                                                std::string *ErrorStr) = 0;
61
62ExecutionEngine::ExecutionEngine(Module *M)
63  : EEState(*this),
64    LazyFunctionCreator(0),
65    ExceptionTableRegister(0),
66    ExceptionTableDeregister(0) {
67  CompilingLazily         = false;
68  GVCompilationDisabled   = false;
69  SymbolSearchingDisabled = false;
70  Modules.push_back(M);
71  assert(M && "Module is null?");
72}
73
74ExecutionEngine::~ExecutionEngine() {
75  clearAllGlobalMappings();
76  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
77    delete Modules[i];
78}
79
80void ExecutionEngine::DeregisterAllTables() {
81  if (ExceptionTableDeregister) {
82    DenseMap<const Function*, void*>::iterator it = AllExceptionTables.begin();
83    DenseMap<const Function*, void*>::iterator ite = AllExceptionTables.end();
84    for (; it != ite; ++it)
85      ExceptionTableDeregister(it->second);
86    AllExceptionTables.clear();
87  }
88}
89
90namespace {
91/// \brief Helper class which uses a value handler to automatically deletes the
92/// memory block when the GlobalVariable is destroyed.
93class GVMemoryBlock : public CallbackVH {
94  GVMemoryBlock(const GlobalVariable *GV)
95    : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
96
97public:
98  /// \brief Returns the address the GlobalVariable should be written into.  The
99  /// GVMemoryBlock object prefixes that.
100  static char *Create(const GlobalVariable *GV, const TargetData& TD) {
101    const Type *ElTy = GV->getType()->getElementType();
102    size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
103    void *RawMemory = ::operator new(
104      TargetData::RoundUpAlignment(sizeof(GVMemoryBlock),
105                                   TD.getPreferredAlignment(GV))
106      + GVSize);
107    new(RawMemory) GVMemoryBlock(GV);
108    return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
109  }
110
111  virtual void deleted() {
112    // We allocated with operator new and with some extra memory hanging off the
113    // end, so don't just delete this.  I'm not sure if this is actually
114    // required.
115    this->~GVMemoryBlock();
116    ::operator delete(this);
117  }
118};
119}  // anonymous namespace
120
121char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
122  return GVMemoryBlock::Create(GV, *getTargetData());
123}
124
125bool ExecutionEngine::removeModule(Module *M) {
126  for(SmallVector<Module *, 1>::iterator I = Modules.begin(),
127        E = Modules.end(); I != E; ++I) {
128    Module *Found = *I;
129    if (Found == M) {
130      Modules.erase(I);
131      clearGlobalMappingsFromModule(M);
132      return true;
133    }
134  }
135  return false;
136}
137
138Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
139  for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
140    if (Function *F = Modules[i]->getFunction(FnName))
141      return F;
142  }
143  return 0;
144}
145
146
147void *ExecutionEngineState::RemoveMapping(const MutexGuard &,
148                                          const GlobalValue *ToUnmap) {
149  GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
150  void *OldVal;
151
152  // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
153  // GlobalAddressMap.
154  if (I == GlobalAddressMap.end())
155    OldVal = 0;
156  else {
157    OldVal = I->second;
158    GlobalAddressMap.erase(I);
159  }
160
161  GlobalAddressReverseMap.erase(OldVal);
162  return OldVal;
163}
164
165void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
166  MutexGuard locked(lock);
167
168  DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
169        << "\' to [" << Addr << "]\n";);
170  void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
171  assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
172  CurVal = Addr;
173
174  // If we are using the reverse mapping, add it too.
175  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
176    AssertingVH<const GlobalValue> &V =
177      EEState.getGlobalAddressReverseMap(locked)[Addr];
178    assert((V == 0 || GV == 0) && "GlobalMapping already established!");
179    V = GV;
180  }
181}
182
183void ExecutionEngine::clearAllGlobalMappings() {
184  MutexGuard locked(lock);
185
186  EEState.getGlobalAddressMap(locked).clear();
187  EEState.getGlobalAddressReverseMap(locked).clear();
188}
189
190void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
191  MutexGuard locked(lock);
192
193  for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
194    EEState.RemoveMapping(locked, FI);
195  for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
196       GI != GE; ++GI)
197    EEState.RemoveMapping(locked, GI);
198}
199
200void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
201  MutexGuard locked(lock);
202
203  ExecutionEngineState::GlobalAddressMapTy &Map =
204    EEState.getGlobalAddressMap(locked);
205
206  // Deleting from the mapping?
207  if (Addr == 0)
208    return EEState.RemoveMapping(locked, GV);
209
210  void *&CurVal = Map[GV];
211  void *OldVal = CurVal;
212
213  if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
214    EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
215  CurVal = Addr;
216
217  // If we are using the reverse mapping, add it too.
218  if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
219    AssertingVH<const GlobalValue> &V =
220      EEState.getGlobalAddressReverseMap(locked)[Addr];
221    assert((V == 0 || GV == 0) && "GlobalMapping already established!");
222    V = GV;
223  }
224  return OldVal;
225}
226
227void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
228  MutexGuard locked(lock);
229
230  ExecutionEngineState::GlobalAddressMapTy::iterator I =
231    EEState.getGlobalAddressMap(locked).find(GV);
232  return I != EEState.getGlobalAddressMap(locked).end() ? I->second : 0;
233}
234
235const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
236  MutexGuard locked(lock);
237
238  // If we haven't computed the reverse mapping yet, do so first.
239  if (EEState.getGlobalAddressReverseMap(locked).empty()) {
240    for (ExecutionEngineState::GlobalAddressMapTy::iterator
241         I = EEState.getGlobalAddressMap(locked).begin(),
242         E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
243      EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
244                                                          I->second, I->first));
245  }
246
247  std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
248    EEState.getGlobalAddressReverseMap(locked).find(Addr);
249  return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
250}
251
252namespace {
253class ArgvArray {
254  char *Array;
255  std::vector<char*> Values;
256public:
257  ArgvArray() : Array(NULL) {}
258  ~ArgvArray() { clear(); }
259  void clear() {
260    delete[] Array;
261    Array = NULL;
262    for (size_t I = 0, E = Values.size(); I != E; ++I) {
263      delete[] Values[I];
264    }
265    Values.clear();
266  }
267  /// Turn a vector of strings into a nice argv style array of pointers to null
268  /// terminated strings.
269  void *reset(LLVMContext &C, ExecutionEngine *EE,
270              const std::vector<std::string> &InputArgv);
271};
272}  // anonymous namespace
273void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
274                       const std::vector<std::string> &InputArgv) {
275  clear();  // Free the old contents.
276  unsigned PtrSize = EE->getTargetData()->getPointerSize();
277  Array = new char[(InputArgv.size()+1)*PtrSize];
278
279  DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
280  const Type *SBytePtr = Type::getInt8PtrTy(C);
281
282  for (unsigned i = 0; i != InputArgv.size(); ++i) {
283    unsigned Size = InputArgv[i].size()+1;
284    char *Dest = new char[Size];
285    Values.push_back(Dest);
286    DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
287
288    std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
289    Dest[Size-1] = 0;
290
291    // Endian safe: Array[i] = (PointerTy)Dest;
292    EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
293                           SBytePtr);
294  }
295
296  // Null terminate it
297  EE->StoreValueToMemory(PTOGV(0),
298                         (GenericValue*)(Array+InputArgv.size()*PtrSize),
299                         SBytePtr);
300  return Array;
301}
302
303void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
304                                                       bool isDtors) {
305  const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
306  GlobalVariable *GV = module->getNamedGlobal(Name);
307
308  // If this global has internal linkage, or if it has a use, then it must be
309  // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
310  // this is the case, don't execute any of the global ctors, __main will do
311  // it.
312  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
313
314  // Should be an array of '{ i32, void ()* }' structs.  The first value is
315  // the init priority, which we ignore.
316  ConstantArray *InitList = cast<ConstantArray>(GV->getInitializer());
317  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
318    ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(i));
319
320    Constant *FP = CS->getOperand(1);
321    if (FP->isNullValue())
322      break;  // Found a null terminator, exit.
323
324    // Strip off constant expression casts.
325    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
326      if (CE->isCast())
327        FP = CE->getOperand(0);
328
329    // Execute the ctor/dtor function!
330    if (Function *F = dyn_cast<Function>(FP))
331      runFunction(F, std::vector<GenericValue>());
332
333    // FIXME: It is marginally lame that we just do nothing here if we see an
334    // entry we don't recognize. It might not be unreasonable for the verifier
335    // to not even allow this and just assert here.
336  }
337}
338
339void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
340  // Execute global ctors/dtors for each module in the program.
341  for (unsigned i = 0, e = Modules.size(); i != e; ++i)
342    runStaticConstructorsDestructors(Modules[i], isDtors);
343}
344
345#ifndef NDEBUG
346/// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
347static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
348  unsigned PtrSize = EE->getTargetData()->getPointerSize();
349  for (unsigned i = 0; i < PtrSize; ++i)
350    if (*(i + (uint8_t*)Loc))
351      return false;
352  return true;
353}
354#endif
355
356int ExecutionEngine::runFunctionAsMain(Function *Fn,
357                                       const std::vector<std::string> &argv,
358                                       const char * const * envp) {
359  std::vector<GenericValue> GVArgs;
360  GenericValue GVArgc;
361  GVArgc.IntVal = APInt(32, argv.size());
362
363  // Check main() type
364  unsigned NumArgs = Fn->getFunctionType()->getNumParams();
365  const FunctionType *FTy = Fn->getFunctionType();
366  const Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
367
368  // Check the argument types.
369  if (NumArgs > 3)
370    report_fatal_error("Invalid number of arguments of main() supplied");
371  if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
372    report_fatal_error("Invalid type for third argument of main() supplied");
373  if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
374    report_fatal_error("Invalid type for second argument of main() supplied");
375  if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
376    report_fatal_error("Invalid type for first argument of main() supplied");
377  if (!FTy->getReturnType()->isIntegerTy() &&
378      !FTy->getReturnType()->isVoidTy())
379    report_fatal_error("Invalid return type of main() supplied");
380
381  ArgvArray CArgv;
382  ArgvArray CEnv;
383  if (NumArgs) {
384    GVArgs.push_back(GVArgc); // Arg #0 = argc.
385    if (NumArgs > 1) {
386      // Arg #1 = argv.
387      GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
388      assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
389             "argv[0] was null after CreateArgv");
390      if (NumArgs > 2) {
391        std::vector<std::string> EnvVars;
392        for (unsigned i = 0; envp[i]; ++i)
393          EnvVars.push_back(envp[i]);
394        // Arg #2 = envp.
395        GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
396      }
397    }
398  }
399
400  return runFunction(Fn, GVArgs).IntVal.getZExtValue();
401}
402
403ExecutionEngine *ExecutionEngine::create(Module *M,
404                                         bool ForceInterpreter,
405                                         std::string *ErrorStr,
406                                         CodeGenOpt::Level OptLevel,
407                                         bool GVsWithCode) {
408  return EngineBuilder(M)
409      .setEngineKind(ForceInterpreter
410                     ? EngineKind::Interpreter
411                     : EngineKind::JIT)
412      .setErrorStr(ErrorStr)
413      .setOptLevel(OptLevel)
414      .setAllocateGVsWithCode(GVsWithCode)
415      .create();
416}
417
418ExecutionEngine *EngineBuilder::create() {
419  // Make sure we can resolve symbols in the program as well. The zero arg
420  // to the function tells DynamicLibrary to load the program, not a library.
421  if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
422    return 0;
423
424  // If the user specified a memory manager but didn't specify which engine to
425  // create, we assume they only want the JIT, and we fail if they only want
426  // the interpreter.
427  if (JMM) {
428    if (WhichEngine & EngineKind::JIT)
429      WhichEngine = EngineKind::JIT;
430    else {
431      if (ErrorStr)
432        *ErrorStr = "Cannot create an interpreter with a memory manager.";
433      return 0;
434    }
435  }
436
437  // Unless the interpreter was explicitly selected or the JIT is not linked,
438  // try making a JIT.
439  if (WhichEngine & EngineKind::JIT) {
440    if (UseMCJIT && ExecutionEngine::MCJITCtor) {
441      ExecutionEngine *EE =
442        ExecutionEngine::MCJITCtor(M, ErrorStr, JMM, OptLevel,
443                                   AllocateGVsWithCode, CMModel,
444                                   MArch, MCPU, MAttrs);
445      if (EE) return EE;
446    } else if (ExecutionEngine::JITCtor) {
447      ExecutionEngine *EE =
448        ExecutionEngine::JITCtor(M, ErrorStr, JMM, OptLevel,
449                                 AllocateGVsWithCode, CMModel,
450                                 MArch, MCPU, MAttrs);
451      if (EE) return EE;
452    }
453  }
454
455  // If we can't make a JIT and we didn't request one specifically, try making
456  // an interpreter instead.
457  if (WhichEngine & EngineKind::Interpreter) {
458    if (ExecutionEngine::InterpCtor)
459      return ExecutionEngine::InterpCtor(M, ErrorStr);
460    if (ErrorStr)
461      *ErrorStr = "Interpreter has not been linked in.";
462    return 0;
463  }
464
465  if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) {
466    if (ErrorStr)
467      *ErrorStr = "JIT has not been linked in.";
468  }
469
470  return 0;
471}
472
473void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
474  if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
475    return getPointerToFunction(F);
476
477  MutexGuard locked(lock);
478  if (void *P = EEState.getGlobalAddressMap(locked)[GV])
479    return P;
480
481  // Global variable might have been added since interpreter started.
482  if (GlobalVariable *GVar =
483          const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
484    EmitGlobalVariable(GVar);
485  else
486    llvm_unreachable("Global hasn't had an address allocated yet!");
487
488  return EEState.getGlobalAddressMap(locked)[GV];
489}
490
491/// \brief Converts a Constant* into a GenericValue, including handling of
492/// ConstantExpr values.
493GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
494  // If its undefined, return the garbage.
495  if (isa<UndefValue>(C)) {
496    GenericValue Result;
497    switch (C->getType()->getTypeID()) {
498    case Type::IntegerTyID:
499    case Type::X86_FP80TyID:
500    case Type::FP128TyID:
501    case Type::PPC_FP128TyID:
502      // Although the value is undefined, we still have to construct an APInt
503      // with the correct bit width.
504      Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
505      break;
506    default:
507      break;
508    }
509    return Result;
510  }
511
512  // Otherwise, if the value is a ConstantExpr...
513  if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
514    Constant *Op0 = CE->getOperand(0);
515    switch (CE->getOpcode()) {
516    case Instruction::GetElementPtr: {
517      // Compute the index
518      GenericValue Result = getConstantValue(Op0);
519      SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
520      uint64_t Offset =
521        TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size());
522
523      char* tmp = (char*) Result.PointerVal;
524      Result = PTOGV(tmp + Offset);
525      return Result;
526    }
527    case Instruction::Trunc: {
528      GenericValue GV = getConstantValue(Op0);
529      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
530      GV.IntVal = GV.IntVal.trunc(BitWidth);
531      return GV;
532    }
533    case Instruction::ZExt: {
534      GenericValue GV = getConstantValue(Op0);
535      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
536      GV.IntVal = GV.IntVal.zext(BitWidth);
537      return GV;
538    }
539    case Instruction::SExt: {
540      GenericValue GV = getConstantValue(Op0);
541      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
542      GV.IntVal = GV.IntVal.sext(BitWidth);
543      return GV;
544    }
545    case Instruction::FPTrunc: {
546      // FIXME long double
547      GenericValue GV = getConstantValue(Op0);
548      GV.FloatVal = float(GV.DoubleVal);
549      return GV;
550    }
551    case Instruction::FPExt:{
552      // FIXME long double
553      GenericValue GV = getConstantValue(Op0);
554      GV.DoubleVal = double(GV.FloatVal);
555      return GV;
556    }
557    case Instruction::UIToFP: {
558      GenericValue GV = getConstantValue(Op0);
559      if (CE->getType()->isFloatTy())
560        GV.FloatVal = float(GV.IntVal.roundToDouble());
561      else if (CE->getType()->isDoubleTy())
562        GV.DoubleVal = GV.IntVal.roundToDouble();
563      else if (CE->getType()->isX86_FP80Ty()) {
564        APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
565        (void)apf.convertFromAPInt(GV.IntVal,
566                                   false,
567                                   APFloat::rmNearestTiesToEven);
568        GV.IntVal = apf.bitcastToAPInt();
569      }
570      return GV;
571    }
572    case Instruction::SIToFP: {
573      GenericValue GV = getConstantValue(Op0);
574      if (CE->getType()->isFloatTy())
575        GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
576      else if (CE->getType()->isDoubleTy())
577        GV.DoubleVal = GV.IntVal.signedRoundToDouble();
578      else if (CE->getType()->isX86_FP80Ty()) {
579        APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
580        (void)apf.convertFromAPInt(GV.IntVal,
581                                   true,
582                                   APFloat::rmNearestTiesToEven);
583        GV.IntVal = apf.bitcastToAPInt();
584      }
585      return GV;
586    }
587    case Instruction::FPToUI: // double->APInt conversion handles sign
588    case Instruction::FPToSI: {
589      GenericValue GV = getConstantValue(Op0);
590      uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
591      if (Op0->getType()->isFloatTy())
592        GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
593      else if (Op0->getType()->isDoubleTy())
594        GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
595      else if (Op0->getType()->isX86_FP80Ty()) {
596        APFloat apf = APFloat(GV.IntVal);
597        uint64_t v;
598        bool ignored;
599        (void)apf.convertToInteger(&v, BitWidth,
600                                   CE->getOpcode()==Instruction::FPToSI,
601                                   APFloat::rmTowardZero, &ignored);
602        GV.IntVal = v; // endian?
603      }
604      return GV;
605    }
606    case Instruction::PtrToInt: {
607      GenericValue GV = getConstantValue(Op0);
608      uint32_t PtrWidth = TD->getPointerSizeInBits();
609      GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
610      return GV;
611    }
612    case Instruction::IntToPtr: {
613      GenericValue GV = getConstantValue(Op0);
614      uint32_t PtrWidth = TD->getPointerSizeInBits();
615      if (PtrWidth != GV.IntVal.getBitWidth())
616        GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
617      assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
618      GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
619      return GV;
620    }
621    case Instruction::BitCast: {
622      GenericValue GV = getConstantValue(Op0);
623      const Type* DestTy = CE->getType();
624      switch (Op0->getType()->getTypeID()) {
625        default: llvm_unreachable("Invalid bitcast operand");
626        case Type::IntegerTyID:
627          assert(DestTy->isFloatingPointTy() && "invalid bitcast");
628          if (DestTy->isFloatTy())
629            GV.FloatVal = GV.IntVal.bitsToFloat();
630          else if (DestTy->isDoubleTy())
631            GV.DoubleVal = GV.IntVal.bitsToDouble();
632          break;
633        case Type::FloatTyID:
634          assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
635          GV.IntVal = APInt::floatToBits(GV.FloatVal);
636          break;
637        case Type::DoubleTyID:
638          assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
639          GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
640          break;
641        case Type::PointerTyID:
642          assert(DestTy->isPointerTy() && "Invalid bitcast");
643          break; // getConstantValue(Op0)  above already converted it
644      }
645      return GV;
646    }
647    case Instruction::Add:
648    case Instruction::FAdd:
649    case Instruction::Sub:
650    case Instruction::FSub:
651    case Instruction::Mul:
652    case Instruction::FMul:
653    case Instruction::UDiv:
654    case Instruction::SDiv:
655    case Instruction::URem:
656    case Instruction::SRem:
657    case Instruction::And:
658    case Instruction::Or:
659    case Instruction::Xor: {
660      GenericValue LHS = getConstantValue(Op0);
661      GenericValue RHS = getConstantValue(CE->getOperand(1));
662      GenericValue GV;
663      switch (CE->getOperand(0)->getType()->getTypeID()) {
664      default: llvm_unreachable("Bad add type!");
665      case Type::IntegerTyID:
666        switch (CE->getOpcode()) {
667          default: llvm_unreachable("Invalid integer opcode");
668          case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
669          case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
670          case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
671          case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
672          case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
673          case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
674          case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
675          case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
676          case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
677          case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
678        }
679        break;
680      case Type::FloatTyID:
681        switch (CE->getOpcode()) {
682          default: llvm_unreachable("Invalid float opcode");
683          case Instruction::FAdd:
684            GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
685          case Instruction::FSub:
686            GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
687          case Instruction::FMul:
688            GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
689          case Instruction::FDiv:
690            GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
691          case Instruction::FRem:
692            GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
693        }
694        break;
695      case Type::DoubleTyID:
696        switch (CE->getOpcode()) {
697          default: llvm_unreachable("Invalid double opcode");
698          case Instruction::FAdd:
699            GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
700          case Instruction::FSub:
701            GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
702          case Instruction::FMul:
703            GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
704          case Instruction::FDiv:
705            GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
706          case Instruction::FRem:
707            GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
708        }
709        break;
710      case Type::X86_FP80TyID:
711      case Type::PPC_FP128TyID:
712      case Type::FP128TyID: {
713        APFloat apfLHS = APFloat(LHS.IntVal);
714        switch (CE->getOpcode()) {
715          default: llvm_unreachable("Invalid long double opcode");
716          case Instruction::FAdd:
717            apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
718            GV.IntVal = apfLHS.bitcastToAPInt();
719            break;
720          case Instruction::FSub:
721            apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
722            GV.IntVal = apfLHS.bitcastToAPInt();
723            break;
724          case Instruction::FMul:
725            apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
726            GV.IntVal = apfLHS.bitcastToAPInt();
727            break;
728          case Instruction::FDiv:
729            apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
730            GV.IntVal = apfLHS.bitcastToAPInt();
731            break;
732          case Instruction::FRem:
733            apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
734            GV.IntVal = apfLHS.bitcastToAPInt();
735            break;
736          }
737        }
738        break;
739      }
740      return GV;
741    }
742    default:
743      break;
744    }
745
746    SmallString<256> Msg;
747    raw_svector_ostream OS(Msg);
748    OS << "ConstantExpr not handled: " << *CE;
749    report_fatal_error(OS.str());
750  }
751
752  // Otherwise, we have a simple constant.
753  GenericValue Result;
754  switch (C->getType()->getTypeID()) {
755  case Type::FloatTyID:
756    Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
757    break;
758  case Type::DoubleTyID:
759    Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
760    break;
761  case Type::X86_FP80TyID:
762  case Type::FP128TyID:
763  case Type::PPC_FP128TyID:
764    Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
765    break;
766  case Type::IntegerTyID:
767    Result.IntVal = cast<ConstantInt>(C)->getValue();
768    break;
769  case Type::PointerTyID:
770    if (isa<ConstantPointerNull>(C))
771      Result.PointerVal = 0;
772    else if (const Function *F = dyn_cast<Function>(C))
773      Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
774    else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
775      Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
776    else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
777      Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
778                                                        BA->getBasicBlock())));
779    else
780      llvm_unreachable("Unknown constant pointer type!");
781    break;
782  default:
783    SmallString<256> Msg;
784    raw_svector_ostream OS(Msg);
785    OS << "ERROR: Constant unimplemented for type: " << *C->getType();
786    report_fatal_error(OS.str());
787  }
788
789  return Result;
790}
791
792/// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
793/// with the integer held in IntVal.
794static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
795                             unsigned StoreBytes) {
796  assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
797  uint8_t *Src = (uint8_t *)IntVal.getRawData();
798
799  if (sys::isLittleEndianHost()) {
800    // Little-endian host - the source is ordered from LSB to MSB.  Order the
801    // destination from LSB to MSB: Do a straight copy.
802    memcpy(Dst, Src, StoreBytes);
803  } else {
804    // Big-endian host - the source is an array of 64 bit words ordered from
805    // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
806    // from MSB to LSB: Reverse the word order, but not the bytes in a word.
807    while (StoreBytes > sizeof(uint64_t)) {
808      StoreBytes -= sizeof(uint64_t);
809      // May not be aligned so use memcpy.
810      memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
811      Src += sizeof(uint64_t);
812    }
813
814    memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
815  }
816}
817
818void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
819                                         GenericValue *Ptr, const Type *Ty) {
820  const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
821
822  switch (Ty->getTypeID()) {
823  case Type::IntegerTyID:
824    StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
825    break;
826  case Type::FloatTyID:
827    *((float*)Ptr) = Val.FloatVal;
828    break;
829  case Type::DoubleTyID:
830    *((double*)Ptr) = Val.DoubleVal;
831    break;
832  case Type::X86_FP80TyID:
833    memcpy(Ptr, Val.IntVal.getRawData(), 10);
834    break;
835  case Type::PointerTyID:
836    // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
837    if (StoreBytes != sizeof(PointerTy))
838      memset(Ptr, 0, StoreBytes);
839
840    *((PointerTy*)Ptr) = Val.PointerVal;
841    break;
842  default:
843    dbgs() << "Cannot store value of type " << *Ty << "!\n";
844  }
845
846  if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian())
847    // Host and target are different endian - reverse the stored bytes.
848    std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
849}
850
851/// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
852/// from Src into IntVal, which is assumed to be wide enough and to hold zero.
853static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
854  assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
855  uint8_t *Dst = (uint8_t *)IntVal.getRawData();
856
857  if (sys::isLittleEndianHost())
858    // Little-endian host - the destination must be ordered from LSB to MSB.
859    // The source is ordered from LSB to MSB: Do a straight copy.
860    memcpy(Dst, Src, LoadBytes);
861  else {
862    // Big-endian - the destination is an array of 64 bit words ordered from
863    // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
864    // ordered from MSB to LSB: Reverse the word order, but not the bytes in
865    // a word.
866    while (LoadBytes > sizeof(uint64_t)) {
867      LoadBytes -= sizeof(uint64_t);
868      // May not be aligned so use memcpy.
869      memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
870      Dst += sizeof(uint64_t);
871    }
872
873    memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
874  }
875}
876
877/// FIXME: document
878///
879void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
880                                          GenericValue *Ptr,
881                                          const Type *Ty) {
882  const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
883
884  switch (Ty->getTypeID()) {
885  case Type::IntegerTyID:
886    // An APInt with all words initially zero.
887    Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
888    LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
889    break;
890  case Type::FloatTyID:
891    Result.FloatVal = *((float*)Ptr);
892    break;
893  case Type::DoubleTyID:
894    Result.DoubleVal = *((double*)Ptr);
895    break;
896  case Type::PointerTyID:
897    Result.PointerVal = *((PointerTy*)Ptr);
898    break;
899  case Type::X86_FP80TyID: {
900    // This is endian dependent, but it will only work on x86 anyway.
901    // FIXME: Will not trap if loading a signaling NaN.
902    uint64_t y[2];
903    memcpy(y, Ptr, 10);
904    Result.IntVal = APInt(80, 2, y);
905    break;
906  }
907  default:
908    SmallString<256> Msg;
909    raw_svector_ostream OS(Msg);
910    OS << "Cannot load value of type " << *Ty << "!";
911    report_fatal_error(OS.str());
912  }
913}
914
915void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
916  DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
917  DEBUG(Init->dump());
918  if (isa<UndefValue>(Init)) {
919    return;
920  } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
921    unsigned ElementSize =
922      getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
923    for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
924      InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
925    return;
926  } else if (isa<ConstantAggregateZero>(Init)) {
927    memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
928    return;
929  } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
930    unsigned ElementSize =
931      getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
932    for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
933      InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
934    return;
935  } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
936    const StructLayout *SL =
937      getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
938    for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
939      InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
940    return;
941  } else if (Init->getType()->isFirstClassType()) {
942    GenericValue Val = getConstantValue(Init);
943    StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
944    return;
945  }
946
947  DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
948  llvm_unreachable("Unknown constant type to initialize memory with!");
949}
950
951/// EmitGlobals - Emit all of the global variables to memory, storing their
952/// addresses into GlobalAddress.  This must make sure to copy the contents of
953/// their initializers into the memory.
954void ExecutionEngine::emitGlobals() {
955  // Loop over all of the global variables in the program, allocating the memory
956  // to hold them.  If there is more than one module, do a prepass over globals
957  // to figure out how the different modules should link together.
958  std::map<std::pair<std::string, const Type*>,
959           const GlobalValue*> LinkedGlobalsMap;
960
961  if (Modules.size() != 1) {
962    for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
963      Module &M = *Modules[m];
964      for (Module::const_global_iterator I = M.global_begin(),
965           E = M.global_end(); I != E; ++I) {
966        const GlobalValue *GV = I;
967        if (GV->hasLocalLinkage() || GV->isDeclaration() ||
968            GV->hasAppendingLinkage() || !GV->hasName())
969          continue;// Ignore external globals and globals with internal linkage.
970
971        const GlobalValue *&GVEntry =
972          LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
973
974        // If this is the first time we've seen this global, it is the canonical
975        // version.
976        if (!GVEntry) {
977          GVEntry = GV;
978          continue;
979        }
980
981        // If the existing global is strong, never replace it.
982        if (GVEntry->hasExternalLinkage() ||
983            GVEntry->hasDLLImportLinkage() ||
984            GVEntry->hasDLLExportLinkage())
985          continue;
986
987        // Otherwise, we know it's linkonce/weak, replace it if this is a strong
988        // symbol.  FIXME is this right for common?
989        if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
990          GVEntry = GV;
991      }
992    }
993  }
994
995  std::vector<const GlobalValue*> NonCanonicalGlobals;
996  for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
997    Module &M = *Modules[m];
998    for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
999         I != E; ++I) {
1000      // In the multi-module case, see what this global maps to.
1001      if (!LinkedGlobalsMap.empty()) {
1002        if (const GlobalValue *GVEntry =
1003              LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
1004          // If something else is the canonical global, ignore this one.
1005          if (GVEntry != &*I) {
1006            NonCanonicalGlobals.push_back(I);
1007            continue;
1008          }
1009        }
1010      }
1011
1012      if (!I->isDeclaration()) {
1013        addGlobalMapping(I, getMemoryForGV(I));
1014      } else {
1015        // External variable reference. Try to use the dynamic loader to
1016        // get a pointer to it.
1017        if (void *SymAddr =
1018            sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
1019          addGlobalMapping(I, SymAddr);
1020        else {
1021          report_fatal_error("Could not resolve external global address: "
1022                            +I->getName());
1023        }
1024      }
1025    }
1026
1027    // If there are multiple modules, map the non-canonical globals to their
1028    // canonical location.
1029    if (!NonCanonicalGlobals.empty()) {
1030      for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1031        const GlobalValue *GV = NonCanonicalGlobals[i];
1032        const GlobalValue *CGV =
1033          LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1034        void *Ptr = getPointerToGlobalIfAvailable(CGV);
1035        assert(Ptr && "Canonical global wasn't codegen'd!");
1036        addGlobalMapping(GV, Ptr);
1037      }
1038    }
1039
1040    // Now that all of the globals are set up in memory, loop through them all
1041    // and initialize their contents.
1042    for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1043         I != E; ++I) {
1044      if (!I->isDeclaration()) {
1045        if (!LinkedGlobalsMap.empty()) {
1046          if (const GlobalValue *GVEntry =
1047                LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1048            if (GVEntry != &*I)  // Not the canonical variable.
1049              continue;
1050        }
1051        EmitGlobalVariable(I);
1052      }
1053    }
1054  }
1055}
1056
1057// EmitGlobalVariable - This method emits the specified global variable to the
1058// address specified in GlobalAddresses, or allocates new memory if it's not
1059// already in the map.
1060void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1061  void *GA = getPointerToGlobalIfAvailable(GV);
1062
1063  if (GA == 0) {
1064    // If it's not already specified, allocate memory for the global.
1065    GA = getMemoryForGV(GV);
1066    addGlobalMapping(GV, GA);
1067  }
1068
1069  // Don't initialize if it's thread local, let the client do it.
1070  if (!GV->isThreadLocal())
1071    InitializeMemory(GV->getInitializer(), GA);
1072
1073  const Type *ElTy = GV->getType()->getElementType();
1074  size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
1075  NumInitBytes += (unsigned)GVSize;
1076  ++NumGlobals;
1077}
1078
1079ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
1080  : EE(EE), GlobalAddressMap(this) {
1081}
1082
1083sys::Mutex *
1084ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
1085  return &EES->EE.lock;
1086}
1087
1088void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
1089                                                      const GlobalValue *Old) {
1090  void *OldVal = EES->GlobalAddressMap.lookup(Old);
1091  EES->GlobalAddressReverseMap.erase(OldVal);
1092}
1093
1094void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
1095                                                    const GlobalValue *,
1096                                                    const GlobalValue *) {
1097  assert(false && "The ExecutionEngine doesn't know how to handle a"
1098         " RAUW on a value it has a global mapping for.");
1099}
1100