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