LLVM API Documentation

ExecutionEngine.cpp
Go to the documentation of this file.
00001 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the common interface used by the various execution engine
00011 // subclasses.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "jit"
00016 #include "llvm/ExecutionEngine/ExecutionEngine.h"
00017 #include "llvm/ADT/SmallString.h"
00018 #include "llvm/ADT/Statistic.h"
00019 #include "llvm/ExecutionEngine/GenericValue.h"
00020 #include "llvm/ExecutionEngine/JITMemoryManager.h"
00021 #include "llvm/ExecutionEngine/ObjectCache.h"
00022 #include "llvm/IR/Constants.h"
00023 #include "llvm/IR/DataLayout.h"
00024 #include "llvm/IR/DerivedTypes.h"
00025 #include "llvm/IR/Module.h"
00026 #include "llvm/IR/Operator.h"
00027 #include "llvm/IR/ValueHandle.h"
00028 #include "llvm/Support/Debug.h"
00029 #include "llvm/Support/DynamicLibrary.h"
00030 #include "llvm/Support/ErrorHandling.h"
00031 #include "llvm/Support/Host.h"
00032 #include "llvm/Support/MutexGuard.h"
00033 #include "llvm/Support/TargetRegistry.h"
00034 #include "llvm/Support/raw_ostream.h"
00035 #include "llvm/Target/TargetMachine.h"
00036 #include <cmath>
00037 #include <cstring>
00038 using namespace llvm;
00039 
00040 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
00041 STATISTIC(NumGlobals  , "Number of global vars initialized");
00042 
00043 // Pin the vtable to this file.
00044 void ObjectCache::anchor() {}
00045 void ObjectBuffer::anchor() {}
00046 void ObjectBufferStream::anchor() {}
00047 
00048 ExecutionEngine *(*ExecutionEngine::JITCtor)(
00049   Module *M,
00050   std::string *ErrorStr,
00051   JITMemoryManager *JMM,
00052   bool GVsWithCode,
00053   TargetMachine *TM) = nullptr;
00054 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
00055   Module *M,
00056   std::string *ErrorStr,
00057   RTDyldMemoryManager *MCJMM,
00058   bool GVsWithCode,
00059   TargetMachine *TM) = nullptr;
00060 ExecutionEngine *(*ExecutionEngine::InterpCtor)(Module *M,
00061                                                 std::string *ErrorStr) =nullptr;
00062 
00063 ExecutionEngine::ExecutionEngine(Module *M)
00064   : EEState(*this),
00065     LazyFunctionCreator(nullptr) {
00066   CompilingLazily         = false;
00067   GVCompilationDisabled   = false;
00068   SymbolSearchingDisabled = false;
00069 
00070   // IR module verification is enabled by default in debug builds, and disabled
00071   // by default in release builds.
00072 #ifndef NDEBUG
00073   VerifyModules = true;
00074 #else
00075   VerifyModules = false;
00076 #endif
00077 
00078   Modules.push_back(M);
00079   assert(M && "Module is null?");
00080 }
00081 
00082 ExecutionEngine::~ExecutionEngine() {
00083   clearAllGlobalMappings();
00084   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
00085     delete Modules[i];
00086 }
00087 
00088 namespace {
00089 /// \brief Helper class which uses a value handler to automatically deletes the
00090 /// memory block when the GlobalVariable is destroyed.
00091 class GVMemoryBlock : public CallbackVH {
00092   GVMemoryBlock(const GlobalVariable *GV)
00093     : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
00094 
00095 public:
00096   /// \brief Returns the address the GlobalVariable should be written into.  The
00097   /// GVMemoryBlock object prefixes that.
00098   static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
00099     Type *ElTy = GV->getType()->getElementType();
00100     size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
00101     void *RawMemory = ::operator new(
00102       DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock),
00103                                    TD.getPreferredAlignment(GV))
00104       + GVSize);
00105     new(RawMemory) GVMemoryBlock(GV);
00106     return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
00107   }
00108 
00109   void deleted() override {
00110     // We allocated with operator new and with some extra memory hanging off the
00111     // end, so don't just delete this.  I'm not sure if this is actually
00112     // required.
00113     this->~GVMemoryBlock();
00114     ::operator delete(this);
00115   }
00116 };
00117 }  // anonymous namespace
00118 
00119 char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
00120   return GVMemoryBlock::Create(GV, *getDataLayout());
00121 }
00122 
00123 bool ExecutionEngine::removeModule(Module *M) {
00124   for(SmallVectorImpl<Module *>::iterator I = Modules.begin(),
00125         E = Modules.end(); I != E; ++I) {
00126     Module *Found = *I;
00127     if (Found == M) {
00128       Modules.erase(I);
00129       clearGlobalMappingsFromModule(M);
00130       return true;
00131     }
00132   }
00133   return false;
00134 }
00135 
00136 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
00137   for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
00138     if (Function *F = Modules[i]->getFunction(FnName))
00139       return F;
00140   }
00141   return nullptr;
00142 }
00143 
00144 
00145 void *ExecutionEngineState::RemoveMapping(const MutexGuard &,
00146                                           const GlobalValue *ToUnmap) {
00147   GlobalAddressMapTy::iterator I = GlobalAddressMap.find(ToUnmap);
00148   void *OldVal;
00149 
00150   // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
00151   // GlobalAddressMap.
00152   if (I == GlobalAddressMap.end())
00153     OldVal = nullptr;
00154   else {
00155     OldVal = I->second;
00156     GlobalAddressMap.erase(I);
00157   }
00158 
00159   GlobalAddressReverseMap.erase(OldVal);
00160   return OldVal;
00161 }
00162 
00163 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
00164   MutexGuard locked(lock);
00165 
00166   DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
00167         << "\' to [" << Addr << "]\n";);
00168   void *&CurVal = EEState.getGlobalAddressMap(locked)[GV];
00169   assert((!CurVal || !Addr) && "GlobalMapping already established!");
00170   CurVal = Addr;
00171 
00172   // If we are using the reverse mapping, add it too.
00173   if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
00174     AssertingVH<const GlobalValue> &V =
00175       EEState.getGlobalAddressReverseMap(locked)[Addr];
00176     assert((!V || !GV) && "GlobalMapping already established!");
00177     V = GV;
00178   }
00179 }
00180 
00181 void ExecutionEngine::clearAllGlobalMappings() {
00182   MutexGuard locked(lock);
00183 
00184   EEState.getGlobalAddressMap(locked).clear();
00185   EEState.getGlobalAddressReverseMap(locked).clear();
00186 }
00187 
00188 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
00189   MutexGuard locked(lock);
00190 
00191   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
00192     EEState.RemoveMapping(locked, FI);
00193   for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
00194        GI != GE; ++GI)
00195     EEState.RemoveMapping(locked, GI);
00196 }
00197 
00198 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
00199   MutexGuard locked(lock);
00200 
00201   ExecutionEngineState::GlobalAddressMapTy &Map =
00202     EEState.getGlobalAddressMap(locked);
00203 
00204   // Deleting from the mapping?
00205   if (!Addr)
00206     return EEState.RemoveMapping(locked, GV);
00207 
00208   void *&CurVal = Map[GV];
00209   void *OldVal = CurVal;
00210 
00211   if (CurVal && !EEState.getGlobalAddressReverseMap(locked).empty())
00212     EEState.getGlobalAddressReverseMap(locked).erase(CurVal);
00213   CurVal = Addr;
00214 
00215   // If we are using the reverse mapping, add it too.
00216   if (!EEState.getGlobalAddressReverseMap(locked).empty()) {
00217     AssertingVH<const GlobalValue> &V =
00218       EEState.getGlobalAddressReverseMap(locked)[Addr];
00219     assert((!V || !GV) && "GlobalMapping already established!");
00220     V = GV;
00221   }
00222   return OldVal;
00223 }
00224 
00225 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
00226   MutexGuard locked(lock);
00227 
00228   ExecutionEngineState::GlobalAddressMapTy::iterator I =
00229     EEState.getGlobalAddressMap(locked).find(GV);
00230   return I != EEState.getGlobalAddressMap(locked).end() ? I->second : nullptr;
00231 }
00232 
00233 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
00234   MutexGuard locked(lock);
00235 
00236   // If we haven't computed the reverse mapping yet, do so first.
00237   if (EEState.getGlobalAddressReverseMap(locked).empty()) {
00238     for (ExecutionEngineState::GlobalAddressMapTy::iterator
00239          I = EEState.getGlobalAddressMap(locked).begin(),
00240          E = EEState.getGlobalAddressMap(locked).end(); I != E; ++I)
00241       EEState.getGlobalAddressReverseMap(locked).insert(std::make_pair(
00242                                                           I->second, I->first));
00243   }
00244 
00245   std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
00246     EEState.getGlobalAddressReverseMap(locked).find(Addr);
00247   return I != EEState.getGlobalAddressReverseMap(locked).end() ? I->second : nullptr;
00248 }
00249 
00250 namespace {
00251 class ArgvArray {
00252   char *Array;
00253   std::vector<char*> Values;
00254 public:
00255   ArgvArray() : Array(nullptr) {}
00256   ~ArgvArray() { clear(); }
00257   void clear() {
00258     delete[] Array;
00259     Array = nullptr;
00260     for (size_t I = 0, E = Values.size(); I != E; ++I) {
00261       delete[] Values[I];
00262     }
00263     Values.clear();
00264   }
00265   /// Turn a vector of strings into a nice argv style array of pointers to null
00266   /// terminated strings.
00267   void *reset(LLVMContext &C, ExecutionEngine *EE,
00268               const std::vector<std::string> &InputArgv);
00269 };
00270 }  // anonymous namespace
00271 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
00272                        const std::vector<std::string> &InputArgv) {
00273   clear();  // Free the old contents.
00274   unsigned PtrSize = EE->getDataLayout()->getPointerSize();
00275   Array = new char[(InputArgv.size()+1)*PtrSize];
00276 
00277   DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array << "\n");
00278   Type *SBytePtr = Type::getInt8PtrTy(C);
00279 
00280   for (unsigned i = 0; i != InputArgv.size(); ++i) {
00281     unsigned Size = InputArgv[i].size()+1;
00282     char *Dest = new char[Size];
00283     Values.push_back(Dest);
00284     DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
00285 
00286     std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
00287     Dest[Size-1] = 0;
00288 
00289     // Endian safe: Array[i] = (PointerTy)Dest;
00290     EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Array+i*PtrSize),
00291                            SBytePtr);
00292   }
00293 
00294   // Null terminate it
00295   EE->StoreValueToMemory(PTOGV(nullptr),
00296                          (GenericValue*)(Array+InputArgv.size()*PtrSize),
00297                          SBytePtr);
00298   return Array;
00299 }
00300 
00301 void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
00302                                                        bool isDtors) {
00303   const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
00304   GlobalVariable *GV = module->getNamedGlobal(Name);
00305 
00306   // If this global has internal linkage, or if it has a use, then it must be
00307   // an old-style (llvmgcc3) static ctor with __main linked in and in use.  If
00308   // this is the case, don't execute any of the global ctors, __main will do
00309   // it.
00310   if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
00311 
00312   // Should be an array of '{ i32, void ()* }' structs.  The first value is
00313   // the init priority, which we ignore.
00314   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
00315   if (!InitList)
00316     return;
00317   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
00318     ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
00319     if (!CS) continue;
00320 
00321     Constant *FP = CS->getOperand(1);
00322     if (FP->isNullValue())
00323       continue;  // Found a sentinal value, ignore.
00324 
00325     // Strip off constant expression casts.
00326     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
00327       if (CE->isCast())
00328         FP = CE->getOperand(0);
00329 
00330     // Execute the ctor/dtor function!
00331     if (Function *F = dyn_cast<Function>(FP))
00332       runFunction(F, std::vector<GenericValue>());
00333 
00334     // FIXME: It is marginally lame that we just do nothing here if we see an
00335     // entry we don't recognize. It might not be unreasonable for the verifier
00336     // to not even allow this and just assert here.
00337   }
00338 }
00339 
00340 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
00341   // Execute global ctors/dtors for each module in the program.
00342   for (unsigned i = 0, e = Modules.size(); i != e; ++i)
00343     runStaticConstructorsDestructors(Modules[i], isDtors);
00344 }
00345 
00346 #ifndef NDEBUG
00347 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
00348 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
00349   unsigned PtrSize = EE->getDataLayout()->getPointerSize();
00350   for (unsigned i = 0; i < PtrSize; ++i)
00351     if (*(i + (uint8_t*)Loc))
00352       return false;
00353   return true;
00354 }
00355 #endif
00356 
00357 int ExecutionEngine::runFunctionAsMain(Function *Fn,
00358                                        const std::vector<std::string> &argv,
00359                                        const char * const * envp) {
00360   std::vector<GenericValue> GVArgs;
00361   GenericValue GVArgc;
00362   GVArgc.IntVal = APInt(32, argv.size());
00363 
00364   // Check main() type
00365   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
00366   FunctionType *FTy = Fn->getFunctionType();
00367   Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
00368 
00369   // Check the argument types.
00370   if (NumArgs > 3)
00371     report_fatal_error("Invalid number of arguments of main() supplied");
00372   if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
00373     report_fatal_error("Invalid type for third argument of main() supplied");
00374   if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
00375     report_fatal_error("Invalid type for second argument of main() supplied");
00376   if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
00377     report_fatal_error("Invalid type for first argument of main() supplied");
00378   if (!FTy->getReturnType()->isIntegerTy() &&
00379       !FTy->getReturnType()->isVoidTy())
00380     report_fatal_error("Invalid return type of main() supplied");
00381 
00382   ArgvArray CArgv;
00383   ArgvArray CEnv;
00384   if (NumArgs) {
00385     GVArgs.push_back(GVArgc); // Arg #0 = argc.
00386     if (NumArgs > 1) {
00387       // Arg #1 = argv.
00388       GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
00389       assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
00390              "argv[0] was null after CreateArgv");
00391       if (NumArgs > 2) {
00392         std::vector<std::string> EnvVars;
00393         for (unsigned i = 0; envp[i]; ++i)
00394           EnvVars.push_back(envp[i]);
00395         // Arg #2 = envp.
00396         GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
00397       }
00398     }
00399   }
00400 
00401   return runFunction(Fn, GVArgs).IntVal.getZExtValue();
00402 }
00403 
00404 ExecutionEngine *ExecutionEngine::create(Module *M,
00405                                          bool ForceInterpreter,
00406                                          std::string *ErrorStr,
00407                                          CodeGenOpt::Level OptLevel,
00408                                          bool GVsWithCode) {
00409   EngineBuilder EB =  EngineBuilder(M)
00410       .setEngineKind(ForceInterpreter
00411                      ? EngineKind::Interpreter
00412                      : EngineKind::JIT)
00413       .setErrorStr(ErrorStr)
00414       .setOptLevel(OptLevel)
00415       .setAllocateGVsWithCode(GVsWithCode);
00416 
00417   return EB.create();
00418 }
00419 
00420 /// createJIT - This is the factory method for creating a JIT for the current
00421 /// machine, it does not fall back to the interpreter.  This takes ownership
00422 /// of the module.
00423 ExecutionEngine *ExecutionEngine::createJIT(Module *M,
00424                                             std::string *ErrorStr,
00425                                             JITMemoryManager *JMM,
00426                                             CodeGenOpt::Level OL,
00427                                             bool GVsWithCode,
00428                                             Reloc::Model RM,
00429                                             CodeModel::Model CMM) {
00430   if (!ExecutionEngine::JITCtor) {
00431     if (ErrorStr)
00432       *ErrorStr = "JIT has not been linked in.";
00433     return nullptr;
00434   }
00435 
00436   // Use the defaults for extra parameters.  Users can use EngineBuilder to
00437   // set them.
00438   EngineBuilder EB(M);
00439   EB.setEngineKind(EngineKind::JIT);
00440   EB.setErrorStr(ErrorStr);
00441   EB.setRelocationModel(RM);
00442   EB.setCodeModel(CMM);
00443   EB.setAllocateGVsWithCode(GVsWithCode);
00444   EB.setOptLevel(OL);
00445   EB.setJITMemoryManager(JMM);
00446 
00447   // TODO: permit custom TargetOptions here
00448   TargetMachine *TM = EB.selectTarget();
00449   if (!TM || (ErrorStr && ErrorStr->length() > 0)) return nullptr;
00450 
00451   return ExecutionEngine::JITCtor(M, ErrorStr, JMM, GVsWithCode, TM);
00452 }
00453 
00454 ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
00455   std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
00456 
00457   // Make sure we can resolve symbols in the program as well. The zero arg
00458   // to the function tells DynamicLibrary to load the program, not a library.
00459   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
00460     return nullptr;
00461 
00462   assert(!(JMM && MCJMM));
00463   
00464   // If the user specified a memory manager but didn't specify which engine to
00465   // create, we assume they only want the JIT, and we fail if they only want
00466   // the interpreter.
00467   if (JMM || MCJMM) {
00468     if (WhichEngine & EngineKind::JIT)
00469       WhichEngine = EngineKind::JIT;
00470     else {
00471       if (ErrorStr)
00472         *ErrorStr = "Cannot create an interpreter with a memory manager.";
00473       return nullptr;
00474     }
00475   }
00476   
00477   if (MCJMM && ! UseMCJIT) {
00478     if (ErrorStr)
00479       *ErrorStr =
00480         "Cannot create a legacy JIT with a runtime dyld memory "
00481         "manager.";
00482     return nullptr;
00483   }
00484 
00485   // Unless the interpreter was explicitly selected or the JIT is not linked,
00486   // try making a JIT.
00487   if ((WhichEngine & EngineKind::JIT) && TheTM) {
00488     Triple TT(M->getTargetTriple());
00489     if (!TM->getTarget().hasJIT()) {
00490       errs() << "WARNING: This target JIT is not designed for the host"
00491              << " you are running.  If bad things happen, please choose"
00492              << " a different -march switch.\n";
00493     }
00494 
00495     ExecutionEngine *EE = nullptr;
00496     if (UseMCJIT && ExecutionEngine::MCJITCtor)
00497       EE = ExecutionEngine::MCJITCtor(M, ErrorStr, MCJMM ? MCJMM : JMM,
00498                                       AllocateGVsWithCode, TheTM.release());
00499     else if (ExecutionEngine::JITCtor)
00500       EE = ExecutionEngine::JITCtor(M, ErrorStr, JMM,
00501                                     AllocateGVsWithCode, TheTM.release());
00502 
00503     if (EE) {
00504       EE->setVerifyModules(VerifyModules);
00505       return EE;
00506     }
00507   }
00508 
00509   // If we can't make a JIT and we didn't request one specifically, try making
00510   // an interpreter instead.
00511   if (WhichEngine & EngineKind::Interpreter) {
00512     if (ExecutionEngine::InterpCtor)
00513       return ExecutionEngine::InterpCtor(M, ErrorStr);
00514     if (ErrorStr)
00515       *ErrorStr = "Interpreter has not been linked in.";
00516     return nullptr;
00517   }
00518 
00519   if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::JITCtor &&
00520       !ExecutionEngine::MCJITCtor) {
00521     if (ErrorStr)
00522       *ErrorStr = "JIT has not been linked in.";
00523   }
00524 
00525   return nullptr;
00526 }
00527 
00528 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
00529   if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
00530     return getPointerToFunction(F);
00531 
00532   MutexGuard locked(lock);
00533   if (void *P = EEState.getGlobalAddressMap(locked)[GV])
00534     return P;
00535 
00536   // Global variable might have been added since interpreter started.
00537   if (GlobalVariable *GVar =
00538           const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
00539     EmitGlobalVariable(GVar);
00540   else
00541     llvm_unreachable("Global hasn't had an address allocated yet!");
00542 
00543   return EEState.getGlobalAddressMap(locked)[GV];
00544 }
00545 
00546 /// \brief Converts a Constant* into a GenericValue, including handling of
00547 /// ConstantExpr values.
00548 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
00549   // If its undefined, return the garbage.
00550   if (isa<UndefValue>(C)) {
00551     GenericValue Result;
00552     switch (C->getType()->getTypeID()) {
00553     default:
00554       break;
00555     case Type::IntegerTyID:
00556     case Type::X86_FP80TyID:
00557     case Type::FP128TyID:
00558     case Type::PPC_FP128TyID:
00559       // Although the value is undefined, we still have to construct an APInt
00560       // with the correct bit width.
00561       Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
00562       break;
00563     case Type::StructTyID: {
00564       // if the whole struct is 'undef' just reserve memory for the value.
00565       if(StructType *STy = dyn_cast<StructType>(C->getType())) {
00566         unsigned int elemNum = STy->getNumElements();
00567         Result.AggregateVal.resize(elemNum);
00568         for (unsigned int i = 0; i < elemNum; ++i) {
00569           Type *ElemTy = STy->getElementType(i);
00570           if (ElemTy->isIntegerTy())
00571             Result.AggregateVal[i].IntVal = 
00572               APInt(ElemTy->getPrimitiveSizeInBits(), 0);
00573           else if (ElemTy->isAggregateType()) {
00574               const Constant *ElemUndef = UndefValue::get(ElemTy);
00575               Result.AggregateVal[i] = getConstantValue(ElemUndef);
00576             }
00577           }
00578         }
00579       }
00580       break;
00581     case Type::VectorTyID:
00582       // if the whole vector is 'undef' just reserve memory for the value.
00583       const VectorType* VTy = dyn_cast<VectorType>(C->getType());
00584       const Type *ElemTy = VTy->getElementType();
00585       unsigned int elemNum = VTy->getNumElements();
00586       Result.AggregateVal.resize(elemNum);
00587       if (ElemTy->isIntegerTy())
00588         for (unsigned int i = 0; i < elemNum; ++i)
00589           Result.AggregateVal[i].IntVal =
00590             APInt(ElemTy->getPrimitiveSizeInBits(), 0);
00591       break;
00592     }
00593     return Result;
00594   }
00595 
00596   // Otherwise, if the value is a ConstantExpr...
00597   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
00598     Constant *Op0 = CE->getOperand(0);
00599     switch (CE->getOpcode()) {
00600     case Instruction::GetElementPtr: {
00601       // Compute the index
00602       GenericValue Result = getConstantValue(Op0);
00603       APInt Offset(DL->getPointerSizeInBits(), 0);
00604       cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
00605 
00606       char* tmp = (char*) Result.PointerVal;
00607       Result = PTOGV(tmp + Offset.getSExtValue());
00608       return Result;
00609     }
00610     case Instruction::Trunc: {
00611       GenericValue GV = getConstantValue(Op0);
00612       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00613       GV.IntVal = GV.IntVal.trunc(BitWidth);
00614       return GV;
00615     }
00616     case Instruction::ZExt: {
00617       GenericValue GV = getConstantValue(Op0);
00618       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00619       GV.IntVal = GV.IntVal.zext(BitWidth);
00620       return GV;
00621     }
00622     case Instruction::SExt: {
00623       GenericValue GV = getConstantValue(Op0);
00624       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00625       GV.IntVal = GV.IntVal.sext(BitWidth);
00626       return GV;
00627     }
00628     case Instruction::FPTrunc: {
00629       // FIXME long double
00630       GenericValue GV = getConstantValue(Op0);
00631       GV.FloatVal = float(GV.DoubleVal);
00632       return GV;
00633     }
00634     case Instruction::FPExt:{
00635       // FIXME long double
00636       GenericValue GV = getConstantValue(Op0);
00637       GV.DoubleVal = double(GV.FloatVal);
00638       return GV;
00639     }
00640     case Instruction::UIToFP: {
00641       GenericValue GV = getConstantValue(Op0);
00642       if (CE->getType()->isFloatTy())
00643         GV.FloatVal = float(GV.IntVal.roundToDouble());
00644       else if (CE->getType()->isDoubleTy())
00645         GV.DoubleVal = GV.IntVal.roundToDouble();
00646       else if (CE->getType()->isX86_FP80Ty()) {
00647         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
00648         (void)apf.convertFromAPInt(GV.IntVal,
00649                                    false,
00650                                    APFloat::rmNearestTiesToEven);
00651         GV.IntVal = apf.bitcastToAPInt();
00652       }
00653       return GV;
00654     }
00655     case Instruction::SIToFP: {
00656       GenericValue GV = getConstantValue(Op0);
00657       if (CE->getType()->isFloatTy())
00658         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
00659       else if (CE->getType()->isDoubleTy())
00660         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
00661       else if (CE->getType()->isX86_FP80Ty()) {
00662         APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
00663         (void)apf.convertFromAPInt(GV.IntVal,
00664                                    true,
00665                                    APFloat::rmNearestTiesToEven);
00666         GV.IntVal = apf.bitcastToAPInt();
00667       }
00668       return GV;
00669     }
00670     case Instruction::FPToUI: // double->APInt conversion handles sign
00671     case Instruction::FPToSI: {
00672       GenericValue GV = getConstantValue(Op0);
00673       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
00674       if (Op0->getType()->isFloatTy())
00675         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
00676       else if (Op0->getType()->isDoubleTy())
00677         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
00678       else if (Op0->getType()->isX86_FP80Ty()) {
00679         APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
00680         uint64_t v;
00681         bool ignored;
00682         (void)apf.convertToInteger(&v, BitWidth,
00683                                    CE->getOpcode()==Instruction::FPToSI,
00684                                    APFloat::rmTowardZero, &ignored);
00685         GV.IntVal = v; // endian?
00686       }
00687       return GV;
00688     }
00689     case Instruction::PtrToInt: {
00690       GenericValue GV = getConstantValue(Op0);
00691       uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
00692       assert(PtrWidth <= 64 && "Bad pointer width");
00693       GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
00694       uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
00695       GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
00696       return GV;
00697     }
00698     case Instruction::IntToPtr: {
00699       GenericValue GV = getConstantValue(Op0);
00700       uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
00701       GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
00702       assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
00703       GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
00704       return GV;
00705     }
00706     case Instruction::BitCast: {
00707       GenericValue GV = getConstantValue(Op0);
00708       Type* DestTy = CE->getType();
00709       switch (Op0->getType()->getTypeID()) {
00710         default: llvm_unreachable("Invalid bitcast operand");
00711         case Type::IntegerTyID:
00712           assert(DestTy->isFloatingPointTy() && "invalid bitcast");
00713           if (DestTy->isFloatTy())
00714             GV.FloatVal = GV.IntVal.bitsToFloat();
00715           else if (DestTy->isDoubleTy())
00716             GV.DoubleVal = GV.IntVal.bitsToDouble();
00717           break;
00718         case Type::FloatTyID:
00719           assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
00720           GV.IntVal = APInt::floatToBits(GV.FloatVal);
00721           break;
00722         case Type::DoubleTyID:
00723           assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
00724           GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
00725           break;
00726         case Type::PointerTyID:
00727           assert(DestTy->isPointerTy() && "Invalid bitcast");
00728           break; // getConstantValue(Op0)  above already converted it
00729       }
00730       return GV;
00731     }
00732     case Instruction::Add:
00733     case Instruction::FAdd:
00734     case Instruction::Sub:
00735     case Instruction::FSub:
00736     case Instruction::Mul:
00737     case Instruction::FMul:
00738     case Instruction::UDiv:
00739     case Instruction::SDiv:
00740     case Instruction::URem:
00741     case Instruction::SRem:
00742     case Instruction::And:
00743     case Instruction::Or:
00744     case Instruction::Xor: {
00745       GenericValue LHS = getConstantValue(Op0);
00746       GenericValue RHS = getConstantValue(CE->getOperand(1));
00747       GenericValue GV;
00748       switch (CE->getOperand(0)->getType()->getTypeID()) {
00749       default: llvm_unreachable("Bad add type!");
00750       case Type::IntegerTyID:
00751         switch (CE->getOpcode()) {
00752           default: llvm_unreachable("Invalid integer opcode");
00753           case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
00754           case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
00755           case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
00756           case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
00757           case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
00758           case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
00759           case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
00760           case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
00761           case Instruction::Or:  GV.IntVal = LHS.IntVal | RHS.IntVal; break;
00762           case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
00763         }
00764         break;
00765       case Type::FloatTyID:
00766         switch (CE->getOpcode()) {
00767           default: llvm_unreachable("Invalid float opcode");
00768           case Instruction::FAdd:
00769             GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
00770           case Instruction::FSub:
00771             GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
00772           case Instruction::FMul:
00773             GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
00774           case Instruction::FDiv:
00775             GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
00776           case Instruction::FRem:
00777             GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
00778         }
00779         break;
00780       case Type::DoubleTyID:
00781         switch (CE->getOpcode()) {
00782           default: llvm_unreachable("Invalid double opcode");
00783           case Instruction::FAdd:
00784             GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
00785           case Instruction::FSub:
00786             GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
00787           case Instruction::FMul:
00788             GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
00789           case Instruction::FDiv:
00790             GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
00791           case Instruction::FRem:
00792             GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
00793         }
00794         break;
00795       case Type::X86_FP80TyID:
00796       case Type::PPC_FP128TyID:
00797       case Type::FP128TyID: {
00798         const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
00799         APFloat apfLHS = APFloat(Sem, LHS.IntVal);
00800         switch (CE->getOpcode()) {
00801           default: llvm_unreachable("Invalid long double opcode");
00802           case Instruction::FAdd:
00803             apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
00804             GV.IntVal = apfLHS.bitcastToAPInt();
00805             break;
00806           case Instruction::FSub:
00807             apfLHS.subtract(APFloat(Sem, RHS.IntVal),
00808                             APFloat::rmNearestTiesToEven);
00809             GV.IntVal = apfLHS.bitcastToAPInt();
00810             break;
00811           case Instruction::FMul:
00812             apfLHS.multiply(APFloat(Sem, RHS.IntVal),
00813                             APFloat::rmNearestTiesToEven);
00814             GV.IntVal = apfLHS.bitcastToAPInt();
00815             break;
00816           case Instruction::FDiv:
00817             apfLHS.divide(APFloat(Sem, RHS.IntVal),
00818                           APFloat::rmNearestTiesToEven);
00819             GV.IntVal = apfLHS.bitcastToAPInt();
00820             break;
00821           case Instruction::FRem:
00822             apfLHS.mod(APFloat(Sem, RHS.IntVal),
00823                        APFloat::rmNearestTiesToEven);
00824             GV.IntVal = apfLHS.bitcastToAPInt();
00825             break;
00826           }
00827         }
00828         break;
00829       }
00830       return GV;
00831     }
00832     default:
00833       break;
00834     }
00835 
00836     SmallString<256> Msg;
00837     raw_svector_ostream OS(Msg);
00838     OS << "ConstantExpr not handled: " << *CE;
00839     report_fatal_error(OS.str());
00840   }
00841 
00842   // Otherwise, we have a simple constant.
00843   GenericValue Result;
00844   switch (C->getType()->getTypeID()) {
00845   case Type::FloatTyID:
00846     Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
00847     break;
00848   case Type::DoubleTyID:
00849     Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
00850     break;
00851   case Type::X86_FP80TyID:
00852   case Type::FP128TyID:
00853   case Type::PPC_FP128TyID:
00854     Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
00855     break;
00856   case Type::IntegerTyID:
00857     Result.IntVal = cast<ConstantInt>(C)->getValue();
00858     break;
00859   case Type::PointerTyID:
00860     if (isa<ConstantPointerNull>(C))
00861       Result.PointerVal = nullptr;
00862     else if (const Function *F = dyn_cast<Function>(C))
00863       Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
00864     else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
00865       Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
00866     else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
00867       Result = PTOGV(getPointerToBasicBlock(const_cast<BasicBlock*>(
00868                                                         BA->getBasicBlock())));
00869     else
00870       llvm_unreachable("Unknown constant pointer type!");
00871     break;
00872   case Type::VectorTyID: {
00873     unsigned elemNum;
00874     Type* ElemTy;
00875     const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
00876     const ConstantVector *CV = dyn_cast<ConstantVector>(C);
00877     const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
00878 
00879     if (CDV) {
00880         elemNum = CDV->getNumElements();
00881         ElemTy = CDV->getElementType();
00882     } else if (CV || CAZ) {
00883         VectorType* VTy = dyn_cast<VectorType>(C->getType());
00884         elemNum = VTy->getNumElements();
00885         ElemTy = VTy->getElementType();
00886     } else {
00887         llvm_unreachable("Unknown constant vector type!");
00888     }
00889 
00890     Result.AggregateVal.resize(elemNum);
00891     // Check if vector holds floats.
00892     if(ElemTy->isFloatTy()) {
00893       if (CAZ) {
00894         GenericValue floatZero;
00895         floatZero.FloatVal = 0.f;
00896         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
00897                   floatZero);
00898         break;
00899       }
00900       if(CV) {
00901         for (unsigned i = 0; i < elemNum; ++i)
00902           if (!isa<UndefValue>(CV->getOperand(i)))
00903             Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
00904               CV->getOperand(i))->getValueAPF().convertToFloat();
00905         break;
00906       }
00907       if(CDV)
00908         for (unsigned i = 0; i < elemNum; ++i)
00909           Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
00910 
00911       break;
00912     }
00913     // Check if vector holds doubles.
00914     if (ElemTy->isDoubleTy()) {
00915       if (CAZ) {
00916         GenericValue doubleZero;
00917         doubleZero.DoubleVal = 0.0;
00918         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
00919                   doubleZero);
00920         break;
00921       }
00922       if(CV) {
00923         for (unsigned i = 0; i < elemNum; ++i)
00924           if (!isa<UndefValue>(CV->getOperand(i)))
00925             Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
00926               CV->getOperand(i))->getValueAPF().convertToDouble();
00927         break;
00928       }
00929       if(CDV)
00930         for (unsigned i = 0; i < elemNum; ++i)
00931           Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
00932 
00933       break;
00934     }
00935     // Check if vector holds integers.
00936     if (ElemTy->isIntegerTy()) {
00937       if (CAZ) {
00938         GenericValue intZero;     
00939         intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
00940         std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
00941                   intZero);
00942         break;
00943       }
00944       if(CV) {
00945         for (unsigned i = 0; i < elemNum; ++i)
00946           if (!isa<UndefValue>(CV->getOperand(i)))
00947             Result.AggregateVal[i].IntVal = cast<ConstantInt>(
00948                                             CV->getOperand(i))->getValue();
00949           else {
00950             Result.AggregateVal[i].IntVal =
00951               APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
00952           }
00953         break;
00954       }
00955       if(CDV)
00956         for (unsigned i = 0; i < elemNum; ++i)
00957           Result.AggregateVal[i].IntVal = APInt(
00958             CDV->getElementType()->getPrimitiveSizeInBits(),
00959             CDV->getElementAsInteger(i));
00960 
00961       break;
00962     }
00963     llvm_unreachable("Unknown constant pointer type!");
00964   }
00965   break;
00966 
00967   default:
00968     SmallString<256> Msg;
00969     raw_svector_ostream OS(Msg);
00970     OS << "ERROR: Constant unimplemented for type: " << *C->getType();
00971     report_fatal_error(OS.str());
00972   }
00973 
00974   return Result;
00975 }
00976 
00977 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
00978 /// with the integer held in IntVal.
00979 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
00980                              unsigned StoreBytes) {
00981   assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
00982   const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
00983 
00984   if (sys::IsLittleEndianHost) {
00985     // Little-endian host - the source is ordered from LSB to MSB.  Order the
00986     // destination from LSB to MSB: Do a straight copy.
00987     memcpy(Dst, Src, StoreBytes);
00988   } else {
00989     // Big-endian host - the source is an array of 64 bit words ordered from
00990     // LSW to MSW.  Each word is ordered from MSB to LSB.  Order the destination
00991     // from MSB to LSB: Reverse the word order, but not the bytes in a word.
00992     while (StoreBytes > sizeof(uint64_t)) {
00993       StoreBytes -= sizeof(uint64_t);
00994       // May not be aligned so use memcpy.
00995       memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
00996       Src += sizeof(uint64_t);
00997     }
00998 
00999     memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
01000   }
01001 }
01002 
01003 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
01004                                          GenericValue *Ptr, Type *Ty) {
01005   const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
01006 
01007   switch (Ty->getTypeID()) {
01008   default:
01009     dbgs() << "Cannot store value of type " << *Ty << "!\n";
01010     break;
01011   case Type::IntegerTyID:
01012     StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
01013     break;
01014   case Type::FloatTyID:
01015     *((float*)Ptr) = Val.FloatVal;
01016     break;
01017   case Type::DoubleTyID:
01018     *((double*)Ptr) = Val.DoubleVal;
01019     break;
01020   case Type::X86_FP80TyID:
01021     memcpy(Ptr, Val.IntVal.getRawData(), 10);
01022     break;
01023   case Type::PointerTyID:
01024     // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
01025     if (StoreBytes != sizeof(PointerTy))
01026       memset(&(Ptr->PointerVal), 0, StoreBytes);
01027 
01028     *((PointerTy*)Ptr) = Val.PointerVal;
01029     break;
01030   case Type::VectorTyID:
01031     for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
01032       if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
01033         *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
01034       if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
01035         *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
01036       if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
01037         unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
01038         StoreIntToMemory(Val.AggregateVal[i].IntVal, 
01039           (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
01040       }
01041     }
01042     break;
01043   }
01044 
01045   if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
01046     // Host and target are different endian - reverse the stored bytes.
01047     std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
01048 }
01049 
01050 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
01051 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
01052 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
01053   assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
01054   uint8_t *Dst = reinterpret_cast<uint8_t *>(
01055                    const_cast<uint64_t *>(IntVal.getRawData()));
01056 
01057   if (sys::IsLittleEndianHost)
01058     // Little-endian host - the destination must be ordered from LSB to MSB.
01059     // The source is ordered from LSB to MSB: Do a straight copy.
01060     memcpy(Dst, Src, LoadBytes);
01061   else {
01062     // Big-endian - the destination is an array of 64 bit words ordered from
01063     // LSW to MSW.  Each word must be ordered from MSB to LSB.  The source is
01064     // ordered from MSB to LSB: Reverse the word order, but not the bytes in
01065     // a word.
01066     while (LoadBytes > sizeof(uint64_t)) {
01067       LoadBytes -= sizeof(uint64_t);
01068       // May not be aligned so use memcpy.
01069       memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
01070       Dst += sizeof(uint64_t);
01071     }
01072 
01073     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
01074   }
01075 }
01076 
01077 /// FIXME: document
01078 ///
01079 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
01080                                           GenericValue *Ptr,
01081                                           Type *Ty) {
01082   const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
01083 
01084   switch (Ty->getTypeID()) {
01085   case Type::IntegerTyID:
01086     // An APInt with all words initially zero.
01087     Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
01088     LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
01089     break;
01090   case Type::FloatTyID:
01091     Result.FloatVal = *((float*)Ptr);
01092     break;
01093   case Type::DoubleTyID:
01094     Result.DoubleVal = *((double*)Ptr);
01095     break;
01096   case Type::PointerTyID:
01097     Result.PointerVal = *((PointerTy*)Ptr);
01098     break;
01099   case Type::X86_FP80TyID: {
01100     // This is endian dependent, but it will only work on x86 anyway.
01101     // FIXME: Will not trap if loading a signaling NaN.
01102     uint64_t y[2];
01103     memcpy(y, Ptr, 10);
01104     Result.IntVal = APInt(80, y);
01105     break;
01106   }
01107   case Type::VectorTyID: {
01108     const VectorType *VT = cast<VectorType>(Ty);
01109     const Type *ElemT = VT->getElementType();
01110     const unsigned numElems = VT->getNumElements();
01111     if (ElemT->isFloatTy()) {
01112       Result.AggregateVal.resize(numElems);
01113       for (unsigned i = 0; i < numElems; ++i)
01114         Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
01115     }
01116     if (ElemT->isDoubleTy()) {
01117       Result.AggregateVal.resize(numElems);
01118       for (unsigned i = 0; i < numElems; ++i)
01119         Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
01120     }
01121     if (ElemT->isIntegerTy()) {
01122       GenericValue intZero;
01123       const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
01124       intZero.IntVal = APInt(elemBitWidth, 0);
01125       Result.AggregateVal.resize(numElems, intZero);
01126       for (unsigned i = 0; i < numElems; ++i)
01127         LoadIntFromMemory(Result.AggregateVal[i].IntVal,
01128           (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
01129     }
01130   break;
01131   }
01132   default:
01133     SmallString<256> Msg;
01134     raw_svector_ostream OS(Msg);
01135     OS << "Cannot load value of type " << *Ty << "!";
01136     report_fatal_error(OS.str());
01137   }
01138 }
01139 
01140 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
01141   DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
01142   DEBUG(Init->dump());
01143   if (isa<UndefValue>(Init))
01144     return;
01145   
01146   if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
01147     unsigned ElementSize =
01148       getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
01149     for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
01150       InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
01151     return;
01152   }
01153   
01154   if (isa<ConstantAggregateZero>(Init)) {
01155     memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
01156     return;
01157   }
01158   
01159   if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
01160     unsigned ElementSize =
01161       getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
01162     for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
01163       InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
01164     return;
01165   }
01166   
01167   if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
01168     const StructLayout *SL =
01169       getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
01170     for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
01171       InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
01172     return;
01173   }
01174 
01175   if (const ConstantDataSequential *CDS =
01176                dyn_cast<ConstantDataSequential>(Init)) {
01177     // CDS is already laid out in host memory order.
01178     StringRef Data = CDS->getRawDataValues();
01179     memcpy(Addr, Data.data(), Data.size());
01180     return;
01181   }
01182 
01183   if (Init->getType()->isFirstClassType()) {
01184     GenericValue Val = getConstantValue(Init);
01185     StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
01186     return;
01187   }
01188 
01189   DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
01190   llvm_unreachable("Unknown constant type to initialize memory with!");
01191 }
01192 
01193 /// EmitGlobals - Emit all of the global variables to memory, storing their
01194 /// addresses into GlobalAddress.  This must make sure to copy the contents of
01195 /// their initializers into the memory.
01196 void ExecutionEngine::emitGlobals() {
01197   // Loop over all of the global variables in the program, allocating the memory
01198   // to hold them.  If there is more than one module, do a prepass over globals
01199   // to figure out how the different modules should link together.
01200   std::map<std::pair<std::string, Type*>,
01201            const GlobalValue*> LinkedGlobalsMap;
01202 
01203   if (Modules.size() != 1) {
01204     for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
01205       Module &M = *Modules[m];
01206       for (Module::const_global_iterator I = M.global_begin(),
01207            E = M.global_end(); I != E; ++I) {
01208         const GlobalValue *GV = I;
01209         if (GV->hasLocalLinkage() || GV->isDeclaration() ||
01210             GV->hasAppendingLinkage() || !GV->hasName())
01211           continue;// Ignore external globals and globals with internal linkage.
01212 
01213         const GlobalValue *&GVEntry =
01214           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
01215 
01216         // If this is the first time we've seen this global, it is the canonical
01217         // version.
01218         if (!GVEntry) {
01219           GVEntry = GV;
01220           continue;
01221         }
01222 
01223         // If the existing global is strong, never replace it.
01224         if (GVEntry->hasExternalLinkage())
01225           continue;
01226 
01227         // Otherwise, we know it's linkonce/weak, replace it if this is a strong
01228         // symbol.  FIXME is this right for common?
01229         if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
01230           GVEntry = GV;
01231       }
01232     }
01233   }
01234 
01235   std::vector<const GlobalValue*> NonCanonicalGlobals;
01236   for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
01237     Module &M = *Modules[m];
01238     for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
01239          I != E; ++I) {
01240       // In the multi-module case, see what this global maps to.
01241       if (!LinkedGlobalsMap.empty()) {
01242         if (const GlobalValue *GVEntry =
01243               LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
01244           // If something else is the canonical global, ignore this one.
01245           if (GVEntry != &*I) {
01246             NonCanonicalGlobals.push_back(I);
01247             continue;
01248           }
01249         }
01250       }
01251 
01252       if (!I->isDeclaration()) {
01253         addGlobalMapping(I, getMemoryForGV(I));
01254       } else {
01255         // External variable reference. Try to use the dynamic loader to
01256         // get a pointer to it.
01257         if (void *SymAddr =
01258             sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
01259           addGlobalMapping(I, SymAddr);
01260         else {
01261           report_fatal_error("Could not resolve external global address: "
01262                             +I->getName());
01263         }
01264       }
01265     }
01266 
01267     // If there are multiple modules, map the non-canonical globals to their
01268     // canonical location.
01269     if (!NonCanonicalGlobals.empty()) {
01270       for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
01271         const GlobalValue *GV = NonCanonicalGlobals[i];
01272         const GlobalValue *CGV =
01273           LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
01274         void *Ptr = getPointerToGlobalIfAvailable(CGV);
01275         assert(Ptr && "Canonical global wasn't codegen'd!");
01276         addGlobalMapping(GV, Ptr);
01277       }
01278     }
01279 
01280     // Now that all of the globals are set up in memory, loop through them all
01281     // and initialize their contents.
01282     for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
01283          I != E; ++I) {
01284       if (!I->isDeclaration()) {
01285         if (!LinkedGlobalsMap.empty()) {
01286           if (const GlobalValue *GVEntry =
01287                 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
01288             if (GVEntry != &*I)  // Not the canonical variable.
01289               continue;
01290         }
01291         EmitGlobalVariable(I);
01292       }
01293     }
01294   }
01295 }
01296 
01297 // EmitGlobalVariable - This method emits the specified global variable to the
01298 // address specified in GlobalAddresses, or allocates new memory if it's not
01299 // already in the map.
01300 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
01301   void *GA = getPointerToGlobalIfAvailable(GV);
01302 
01303   if (!GA) {
01304     // If it's not already specified, allocate memory for the global.
01305     GA = getMemoryForGV(GV);
01306 
01307     // If we failed to allocate memory for this global, return.
01308     if (!GA) return;
01309 
01310     addGlobalMapping(GV, GA);
01311   }
01312 
01313   // Don't initialize if it's thread local, let the client do it.
01314   if (!GV->isThreadLocal())
01315     InitializeMemory(GV->getInitializer(), GA);
01316 
01317   Type *ElTy = GV->getType()->getElementType();
01318   size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
01319   NumInitBytes += (unsigned)GVSize;
01320   ++NumGlobals;
01321 }
01322 
01323 ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
01324   : EE(EE), GlobalAddressMap(this) {
01325 }
01326 
01327 sys::Mutex *
01328 ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
01329   return &EES->EE.lock;
01330 }
01331 
01332 void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState *EES,
01333                                                       const GlobalValue *Old) {
01334   void *OldVal = EES->GlobalAddressMap.lookup(Old);
01335   EES->GlobalAddressReverseMap.erase(OldVal);
01336 }
01337 
01338 void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState *,
01339                                                     const GlobalValue *,
01340                                                     const GlobalValue *) {
01341   llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
01342                    " RAUW on a value it has a global mapping for.");
01343 }