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