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