1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the common interface used by the various execution engine
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ExecutionEngine/ExecutionEngine.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/ExecutionEngine/JITEventListener.h"
21 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
22 #include "llvm/IR/Constants.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Mangler.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/ValueHandle.h"
29 #include "llvm/Object/Archive.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/DynamicLibrary.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/Host.h"
35 #include "llvm/Support/MutexGuard.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include "llvm/Target/TargetMachine.h"
43 #define DEBUG_TYPE "jit"
45 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
46 STATISTIC(NumGlobals , "Number of global vars initialized");
48 ExecutionEngine *(*ExecutionEngine::MCJITCtor)(
49 std::unique_ptr<Module> M, std::string *ErrorStr,
50 std::shared_ptr<MCJITMemoryManager> MemMgr,
51 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
52 std::unique_ptr<TargetMachine> TM) = nullptr;
54 ExecutionEngine *(*ExecutionEngine::OrcMCJITReplacementCtor)(
55 std::string *ErrorStr, std::shared_ptr<MCJITMemoryManager> MemMgr,
56 std::shared_ptr<RuntimeDyld::SymbolResolver> Resolver,
57 std::unique_ptr<TargetMachine> TM) = nullptr;
59 ExecutionEngine *(*ExecutionEngine::InterpCtor)(std::unique_ptr<Module> M,
60 std::string *ErrorStr) =nullptr;
62 void JITEventListener::anchor() {}
64 ExecutionEngine::ExecutionEngine(std::unique_ptr<Module> M)
65 : LazyFunctionCreator(nullptr) {
66 CompilingLazily = false;
67 GVCompilationDisabled = false;
68 SymbolSearchingDisabled = false;
70 // IR module verification is enabled by default in debug builds, and disabled
71 // by default in release builds.
75 VerifyModules = false;
78 assert(M && "Module is null?");
79 Modules.push_back(std::move(M));
82 ExecutionEngine::~ExecutionEngine() {
83 clearAllGlobalMappings();
87 /// \brief Helper class which uses a value handler to automatically deletes the
88 /// memory block when the GlobalVariable is destroyed.
89 class GVMemoryBlock : public CallbackVH {
90 GVMemoryBlock(const GlobalVariable *GV)
91 : CallbackVH(const_cast<GlobalVariable*>(GV)) {}
94 /// \brief Returns the address the GlobalVariable should be written into. The
95 /// GVMemoryBlock object prefixes that.
96 static char *Create(const GlobalVariable *GV, const DataLayout& TD) {
97 Type *ElTy = GV->getType()->getElementType();
98 size_t GVSize = (size_t)TD.getTypeAllocSize(ElTy);
99 void *RawMemory = ::operator new(
100 RoundUpToAlignment(sizeof(GVMemoryBlock),
101 TD.getPreferredAlignment(GV))
103 new(RawMemory) GVMemoryBlock(GV);
104 return static_cast<char*>(RawMemory) + sizeof(GVMemoryBlock);
107 void deleted() override {
108 // We allocated with operator new and with some extra memory hanging off the
109 // end, so don't just delete this. I'm not sure if this is actually
111 this->~GVMemoryBlock();
112 ::operator delete(this);
115 } // anonymous namespace
117 char *ExecutionEngine::getMemoryForGV(const GlobalVariable *GV) {
118 return GVMemoryBlock::Create(GV, *getDataLayout());
121 void ExecutionEngine::addObjectFile(std::unique_ptr<object::ObjectFile> O) {
122 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
126 ExecutionEngine::addObjectFile(object::OwningBinary<object::ObjectFile> O) {
127 llvm_unreachable("ExecutionEngine subclass doesn't implement addObjectFile.");
130 void ExecutionEngine::addArchive(object::OwningBinary<object::Archive> A) {
131 llvm_unreachable("ExecutionEngine subclass doesn't implement addArchive.");
134 bool ExecutionEngine::removeModule(Module *M) {
135 for (auto I = Modules.begin(), E = Modules.end(); I != E; ++I) {
136 Module *Found = I->get();
140 clearGlobalMappingsFromModule(M);
147 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
148 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
149 Function *F = Modules[i]->getFunction(FnName);
150 if (F && !F->isDeclaration())
157 uint64_t ExecutionEngineState::RemoveMapping(StringRef Name) {
158 GlobalAddressMapTy::iterator I = GlobalAddressMap.find(Name);
161 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
163 if (I == GlobalAddressMap.end())
166 GlobalAddressReverseMap.erase(I->second);
168 GlobalAddressMap.erase(I);
174 std::string ExecutionEngine::getMangledName(const GlobalValue *GV) {
175 MutexGuard locked(lock);
177 SmallString<128> FullName;
178 Mang.getNameWithPrefix(FullName, GV->getName());
179 return FullName.str();
182 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
183 MutexGuard locked(lock);
184 addGlobalMapping(getMangledName(GV), (uint64_t) Addr);
187 void ExecutionEngine::addGlobalMapping(StringRef Name, uint64_t Addr) {
188 MutexGuard locked(lock);
190 assert(!Name.empty() && "Empty GlobalMapping symbol name!");
192 DEBUG(dbgs() << "JIT: Map \'" << Name << "\' to [" << Addr << "]\n";);
193 uint64_t &CurVal = EEState.getGlobalAddressMap()[Name];
194 assert((!CurVal || !Addr) && "GlobalMapping already established!");
197 // If we are using the reverse mapping, add it too.
198 if (!EEState.getGlobalAddressReverseMap().empty()) {
199 std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
200 assert((!V.empty() || !Name.empty()) &&
201 "GlobalMapping already established!");
206 void ExecutionEngine::clearAllGlobalMappings() {
207 MutexGuard locked(lock);
209 EEState.getGlobalAddressMap().clear();
210 EEState.getGlobalAddressReverseMap().clear();
213 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
214 MutexGuard locked(lock);
216 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
217 EEState.RemoveMapping(getMangledName(FI));
218 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
220 EEState.RemoveMapping(getMangledName(GI));
223 uint64_t ExecutionEngine::updateGlobalMapping(const GlobalValue *GV,
225 MutexGuard locked(lock);
226 return updateGlobalMapping(getMangledName(GV), (uint64_t) Addr);
229 uint64_t ExecutionEngine::updateGlobalMapping(StringRef Name, uint64_t Addr) {
230 MutexGuard locked(lock);
232 ExecutionEngineState::GlobalAddressMapTy &Map =
233 EEState.getGlobalAddressMap();
235 // Deleting from the mapping?
237 return EEState.RemoveMapping(Name);
239 uint64_t &CurVal = Map[Name];
240 uint64_t OldVal = CurVal;
242 if (CurVal && !EEState.getGlobalAddressReverseMap().empty())
243 EEState.getGlobalAddressReverseMap().erase(CurVal);
246 // If we are using the reverse mapping, add it too.
247 if (!EEState.getGlobalAddressReverseMap().empty()) {
248 std::string &V = EEState.getGlobalAddressReverseMap()[CurVal];
249 assert((!V.empty() || !Name.empty()) &&
250 "GlobalMapping already established!");
256 uint64_t ExecutionEngine::getAddressToGlobalIfAvailable(StringRef S) {
257 MutexGuard locked(lock);
258 uint64_t Address = 0;
259 ExecutionEngineState::GlobalAddressMapTy::iterator I =
260 EEState.getGlobalAddressMap().find(S);
261 if (I != EEState.getGlobalAddressMap().end())
267 void *ExecutionEngine::getPointerToGlobalIfAvailable(StringRef S) {
268 MutexGuard locked(lock);
269 if (void* Address = (void *) getAddressToGlobalIfAvailable(S))
274 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
275 MutexGuard locked(lock);
276 return getPointerToGlobalIfAvailable(getMangledName(GV));
279 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
280 MutexGuard locked(lock);
282 // If we haven't computed the reverse mapping yet, do so first.
283 if (EEState.getGlobalAddressReverseMap().empty()) {
284 for (ExecutionEngineState::GlobalAddressMapTy::iterator
285 I = EEState.getGlobalAddressMap().begin(),
286 E = EEState.getGlobalAddressMap().end(); I != E; ++I) {
287 StringRef Name = I->first();
288 uint64_t Addr = I->second;
289 EEState.getGlobalAddressReverseMap().insert(std::make_pair(
294 std::map<uint64_t, std::string>::iterator I =
295 EEState.getGlobalAddressReverseMap().find((uint64_t) Addr);
297 if (I != EEState.getGlobalAddressReverseMap().end()) {
298 StringRef Name = I->second;
299 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
300 if (GlobalValue *GV = Modules[i]->getNamedValue(Name))
308 std::unique_ptr<char[]> Array;
309 std::vector<std::unique_ptr<char[]>> Values;
311 /// Turn a vector of strings into a nice argv style array of pointers to null
312 /// terminated strings.
313 void *reset(LLVMContext &C, ExecutionEngine *EE,
314 const std::vector<std::string> &InputArgv);
316 } // anonymous namespace
317 void *ArgvArray::reset(LLVMContext &C, ExecutionEngine *EE,
318 const std::vector<std::string> &InputArgv) {
319 Values.clear(); // Free the old contents.
320 Values.reserve(InputArgv.size());
321 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
322 Array = make_unique<char[]>((InputArgv.size()+1)*PtrSize);
324 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array.get() << "\n");
325 Type *SBytePtr = Type::getInt8PtrTy(C);
327 for (unsigned i = 0; i != InputArgv.size(); ++i) {
328 unsigned Size = InputArgv[i].size()+1;
329 auto Dest = make_unique<char[]>(Size);
330 DEBUG(dbgs() << "JIT: ARGV[" << i << "] = " << (void*)Dest.get() << "\n");
332 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest.get());
335 // Endian safe: Array[i] = (PointerTy)Dest;
336 EE->StoreValueToMemory(PTOGV(Dest.get()),
337 (GenericValue*)(&Array[i*PtrSize]), SBytePtr);
338 Values.push_back(std::move(Dest));
342 EE->StoreValueToMemory(PTOGV(nullptr),
343 (GenericValue*)(&Array[InputArgv.size()*PtrSize]),
348 void ExecutionEngine::runStaticConstructorsDestructors(Module &module,
350 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
351 GlobalVariable *GV = module.getNamedGlobal(Name);
353 // If this global has internal linkage, or if it has a use, then it must be
354 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
355 // this is the case, don't execute any of the global ctors, __main will do
357 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
359 // Should be an array of '{ i32, void ()* }' structs. The first value is
360 // the init priority, which we ignore.
361 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
364 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
365 ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i));
368 Constant *FP = CS->getOperand(1);
369 if (FP->isNullValue())
370 continue; // Found a sentinal value, ignore.
372 // Strip off constant expression casts.
373 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
375 FP = CE->getOperand(0);
377 // Execute the ctor/dtor function!
378 if (Function *F = dyn_cast<Function>(FP))
379 runFunction(F, std::vector<GenericValue>());
381 // FIXME: It is marginally lame that we just do nothing here if we see an
382 // entry we don't recognize. It might not be unreasonable for the verifier
383 // to not even allow this and just assert here.
387 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
388 // Execute global ctors/dtors for each module in the program.
389 for (std::unique_ptr<Module> &M : Modules)
390 runStaticConstructorsDestructors(*M, isDtors);
394 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
395 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
396 unsigned PtrSize = EE->getDataLayout()->getPointerSize();
397 for (unsigned i = 0; i < PtrSize; ++i)
398 if (*(i + (uint8_t*)Loc))
404 int ExecutionEngine::runFunctionAsMain(Function *Fn,
405 const std::vector<std::string> &argv,
406 const char * const * envp) {
407 std::vector<GenericValue> GVArgs;
409 GVArgc.IntVal = APInt(32, argv.size());
412 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
413 FunctionType *FTy = Fn->getFunctionType();
414 Type* PPInt8Ty = Type::getInt8PtrTy(Fn->getContext())->getPointerTo();
416 // Check the argument types.
418 report_fatal_error("Invalid number of arguments of main() supplied");
419 if (NumArgs >= 3 && FTy->getParamType(2) != PPInt8Ty)
420 report_fatal_error("Invalid type for third argument of main() supplied");
421 if (NumArgs >= 2 && FTy->getParamType(1) != PPInt8Ty)
422 report_fatal_error("Invalid type for second argument of main() supplied");
423 if (NumArgs >= 1 && !FTy->getParamType(0)->isIntegerTy(32))
424 report_fatal_error("Invalid type for first argument of main() supplied");
425 if (!FTy->getReturnType()->isIntegerTy() &&
426 !FTy->getReturnType()->isVoidTy())
427 report_fatal_error("Invalid return type of main() supplied");
432 GVArgs.push_back(GVArgc); // Arg #0 = argc.
435 GVArgs.push_back(PTOGV(CArgv.reset(Fn->getContext(), this, argv)));
436 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
437 "argv[0] was null after CreateArgv");
439 std::vector<std::string> EnvVars;
440 for (unsigned i = 0; envp[i]; ++i)
441 EnvVars.push_back(envp[i]);
443 GVArgs.push_back(PTOGV(CEnv.reset(Fn->getContext(), this, EnvVars)));
448 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
451 EngineBuilder::EngineBuilder() : EngineBuilder(nullptr) {}
453 EngineBuilder::EngineBuilder(std::unique_ptr<Module> M)
454 : M(std::move(M)), WhichEngine(EngineKind::Either), ErrorStr(nullptr),
455 OptLevel(CodeGenOpt::Default), MemMgr(nullptr), Resolver(nullptr),
456 RelocModel(Reloc::Default), CMModel(CodeModel::JITDefault),
457 UseOrcMCJITReplacement(false) {
458 // IR module verification is enabled by default in debug builds, and disabled
459 // by default in release builds.
461 VerifyModules = true;
463 VerifyModules = false;
467 EngineBuilder::~EngineBuilder() = default;
469 EngineBuilder &EngineBuilder::setMCJITMemoryManager(
470 std::unique_ptr<RTDyldMemoryManager> mcjmm) {
471 auto SharedMM = std::shared_ptr<RTDyldMemoryManager>(std::move(mcjmm));
478 EngineBuilder::setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM) {
479 MemMgr = std::shared_ptr<MCJITMemoryManager>(std::move(MM));
484 EngineBuilder::setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR) {
485 Resolver = std::shared_ptr<RuntimeDyld::SymbolResolver>(std::move(SR));
489 ExecutionEngine *EngineBuilder::create(TargetMachine *TM) {
490 std::unique_ptr<TargetMachine> TheTM(TM); // Take ownership.
492 // Make sure we can resolve symbols in the program as well. The zero arg
493 // to the function tells DynamicLibrary to load the program, not a library.
494 if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr, ErrorStr))
497 // If the user specified a memory manager but didn't specify which engine to
498 // create, we assume they only want the JIT, and we fail if they only want
501 if (WhichEngine & EngineKind::JIT)
502 WhichEngine = EngineKind::JIT;
505 *ErrorStr = "Cannot create an interpreter with a memory manager.";
510 // Unless the interpreter was explicitly selected or the JIT is not linked,
512 if ((WhichEngine & EngineKind::JIT) && TheTM) {
513 Triple TT(M->getTargetTriple());
514 if (!TM->getTarget().hasJIT()) {
515 errs() << "WARNING: This target JIT is not designed for the host"
516 << " you are running. If bad things happen, please choose"
517 << " a different -march switch.\n";
520 ExecutionEngine *EE = nullptr;
521 if (ExecutionEngine::OrcMCJITReplacementCtor && UseOrcMCJITReplacement) {
522 EE = ExecutionEngine::OrcMCJITReplacementCtor(ErrorStr, std::move(MemMgr),
525 EE->addModule(std::move(M));
526 } else if (ExecutionEngine::MCJITCtor)
527 EE = ExecutionEngine::MCJITCtor(std::move(M), ErrorStr, std::move(MemMgr),
528 std::move(Resolver), std::move(TheTM));
531 EE->setVerifyModules(VerifyModules);
536 // If we can't make a JIT and we didn't request one specifically, try making
537 // an interpreter instead.
538 if (WhichEngine & EngineKind::Interpreter) {
539 if (ExecutionEngine::InterpCtor)
540 return ExecutionEngine::InterpCtor(std::move(M), ErrorStr);
542 *ErrorStr = "Interpreter has not been linked in.";
546 if ((WhichEngine & EngineKind::JIT) && !ExecutionEngine::MCJITCtor) {
548 *ErrorStr = "JIT has not been linked in.";
554 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
555 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
556 return getPointerToFunction(F);
558 MutexGuard locked(lock);
559 if (void* P = getPointerToGlobalIfAvailable(GV))
562 // Global variable might have been added since interpreter started.
563 if (GlobalVariable *GVar =
564 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
565 EmitGlobalVariable(GVar);
567 llvm_unreachable("Global hasn't had an address allocated yet!");
569 return getPointerToGlobalIfAvailable(GV);
572 /// \brief Converts a Constant* into a GenericValue, including handling of
573 /// ConstantExpr values.
574 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
575 // If its undefined, return the garbage.
576 if (isa<UndefValue>(C)) {
578 switch (C->getType()->getTypeID()) {
581 case Type::IntegerTyID:
582 case Type::X86_FP80TyID:
583 case Type::FP128TyID:
584 case Type::PPC_FP128TyID:
585 // Although the value is undefined, we still have to construct an APInt
586 // with the correct bit width.
587 Result.IntVal = APInt(C->getType()->getPrimitiveSizeInBits(), 0);
589 case Type::StructTyID: {
590 // if the whole struct is 'undef' just reserve memory for the value.
591 if(StructType *STy = dyn_cast<StructType>(C->getType())) {
592 unsigned int elemNum = STy->getNumElements();
593 Result.AggregateVal.resize(elemNum);
594 for (unsigned int i = 0; i < elemNum; ++i) {
595 Type *ElemTy = STy->getElementType(i);
596 if (ElemTy->isIntegerTy())
597 Result.AggregateVal[i].IntVal =
598 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
599 else if (ElemTy->isAggregateType()) {
600 const Constant *ElemUndef = UndefValue::get(ElemTy);
601 Result.AggregateVal[i] = getConstantValue(ElemUndef);
607 case Type::VectorTyID:
608 // if the whole vector is 'undef' just reserve memory for the value.
609 const VectorType* VTy = dyn_cast<VectorType>(C->getType());
610 const Type *ElemTy = VTy->getElementType();
611 unsigned int elemNum = VTy->getNumElements();
612 Result.AggregateVal.resize(elemNum);
613 if (ElemTy->isIntegerTy())
614 for (unsigned int i = 0; i < elemNum; ++i)
615 Result.AggregateVal[i].IntVal =
616 APInt(ElemTy->getPrimitiveSizeInBits(), 0);
622 // Otherwise, if the value is a ConstantExpr...
623 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
624 Constant *Op0 = CE->getOperand(0);
625 switch (CE->getOpcode()) {
626 case Instruction::GetElementPtr: {
628 GenericValue Result = getConstantValue(Op0);
629 APInt Offset(DL->getPointerSizeInBits(), 0);
630 cast<GEPOperator>(CE)->accumulateConstantOffset(*DL, Offset);
632 char* tmp = (char*) Result.PointerVal;
633 Result = PTOGV(tmp + Offset.getSExtValue());
636 case Instruction::Trunc: {
637 GenericValue GV = getConstantValue(Op0);
638 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
639 GV.IntVal = GV.IntVal.trunc(BitWidth);
642 case Instruction::ZExt: {
643 GenericValue GV = getConstantValue(Op0);
644 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
645 GV.IntVal = GV.IntVal.zext(BitWidth);
648 case Instruction::SExt: {
649 GenericValue GV = getConstantValue(Op0);
650 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
651 GV.IntVal = GV.IntVal.sext(BitWidth);
654 case Instruction::FPTrunc: {
656 GenericValue GV = getConstantValue(Op0);
657 GV.FloatVal = float(GV.DoubleVal);
660 case Instruction::FPExt:{
662 GenericValue GV = getConstantValue(Op0);
663 GV.DoubleVal = double(GV.FloatVal);
666 case Instruction::UIToFP: {
667 GenericValue GV = getConstantValue(Op0);
668 if (CE->getType()->isFloatTy())
669 GV.FloatVal = float(GV.IntVal.roundToDouble());
670 else if (CE->getType()->isDoubleTy())
671 GV.DoubleVal = GV.IntVal.roundToDouble();
672 else if (CE->getType()->isX86_FP80Ty()) {
673 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
674 (void)apf.convertFromAPInt(GV.IntVal,
676 APFloat::rmNearestTiesToEven);
677 GV.IntVal = apf.bitcastToAPInt();
681 case Instruction::SIToFP: {
682 GenericValue GV = getConstantValue(Op0);
683 if (CE->getType()->isFloatTy())
684 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
685 else if (CE->getType()->isDoubleTy())
686 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
687 else if (CE->getType()->isX86_FP80Ty()) {
688 APFloat apf = APFloat::getZero(APFloat::x87DoubleExtended);
689 (void)apf.convertFromAPInt(GV.IntVal,
691 APFloat::rmNearestTiesToEven);
692 GV.IntVal = apf.bitcastToAPInt();
696 case Instruction::FPToUI: // double->APInt conversion handles sign
697 case Instruction::FPToSI: {
698 GenericValue GV = getConstantValue(Op0);
699 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
700 if (Op0->getType()->isFloatTy())
701 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
702 else if (Op0->getType()->isDoubleTy())
703 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
704 else if (Op0->getType()->isX86_FP80Ty()) {
705 APFloat apf = APFloat(APFloat::x87DoubleExtended, GV.IntVal);
708 (void)apf.convertToInteger(&v, BitWidth,
709 CE->getOpcode()==Instruction::FPToSI,
710 APFloat::rmTowardZero, &ignored);
711 GV.IntVal = v; // endian?
715 case Instruction::PtrToInt: {
716 GenericValue GV = getConstantValue(Op0);
717 uint32_t PtrWidth = DL->getTypeSizeInBits(Op0->getType());
718 assert(PtrWidth <= 64 && "Bad pointer width");
719 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
720 uint32_t IntWidth = DL->getTypeSizeInBits(CE->getType());
721 GV.IntVal = GV.IntVal.zextOrTrunc(IntWidth);
724 case Instruction::IntToPtr: {
725 GenericValue GV = getConstantValue(Op0);
726 uint32_t PtrWidth = DL->getTypeSizeInBits(CE->getType());
727 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
728 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
729 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
732 case Instruction::BitCast: {
733 GenericValue GV = getConstantValue(Op0);
734 Type* DestTy = CE->getType();
735 switch (Op0->getType()->getTypeID()) {
736 default: llvm_unreachable("Invalid bitcast operand");
737 case Type::IntegerTyID:
738 assert(DestTy->isFloatingPointTy() && "invalid bitcast");
739 if (DestTy->isFloatTy())
740 GV.FloatVal = GV.IntVal.bitsToFloat();
741 else if (DestTy->isDoubleTy())
742 GV.DoubleVal = GV.IntVal.bitsToDouble();
744 case Type::FloatTyID:
745 assert(DestTy->isIntegerTy(32) && "Invalid bitcast");
746 GV.IntVal = APInt::floatToBits(GV.FloatVal);
748 case Type::DoubleTyID:
749 assert(DestTy->isIntegerTy(64) && "Invalid bitcast");
750 GV.IntVal = APInt::doubleToBits(GV.DoubleVal);
752 case Type::PointerTyID:
753 assert(DestTy->isPointerTy() && "Invalid bitcast");
754 break; // getConstantValue(Op0) above already converted it
758 case Instruction::Add:
759 case Instruction::FAdd:
760 case Instruction::Sub:
761 case Instruction::FSub:
762 case Instruction::Mul:
763 case Instruction::FMul:
764 case Instruction::UDiv:
765 case Instruction::SDiv:
766 case Instruction::URem:
767 case Instruction::SRem:
768 case Instruction::And:
769 case Instruction::Or:
770 case Instruction::Xor: {
771 GenericValue LHS = getConstantValue(Op0);
772 GenericValue RHS = getConstantValue(CE->getOperand(1));
774 switch (CE->getOperand(0)->getType()->getTypeID()) {
775 default: llvm_unreachable("Bad add type!");
776 case Type::IntegerTyID:
777 switch (CE->getOpcode()) {
778 default: llvm_unreachable("Invalid integer opcode");
779 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
780 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
781 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
782 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
783 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
784 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
785 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
786 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
787 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
788 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
791 case Type::FloatTyID:
792 switch (CE->getOpcode()) {
793 default: llvm_unreachable("Invalid float opcode");
794 case Instruction::FAdd:
795 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
796 case Instruction::FSub:
797 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
798 case Instruction::FMul:
799 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
800 case Instruction::FDiv:
801 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
802 case Instruction::FRem:
803 GV.FloatVal = std::fmod(LHS.FloatVal,RHS.FloatVal); break;
806 case Type::DoubleTyID:
807 switch (CE->getOpcode()) {
808 default: llvm_unreachable("Invalid double opcode");
809 case Instruction::FAdd:
810 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
811 case Instruction::FSub:
812 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
813 case Instruction::FMul:
814 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
815 case Instruction::FDiv:
816 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
817 case Instruction::FRem:
818 GV.DoubleVal = std::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
821 case Type::X86_FP80TyID:
822 case Type::PPC_FP128TyID:
823 case Type::FP128TyID: {
824 const fltSemantics &Sem = CE->getOperand(0)->getType()->getFltSemantics();
825 APFloat apfLHS = APFloat(Sem, LHS.IntVal);
826 switch (CE->getOpcode()) {
827 default: llvm_unreachable("Invalid long double opcode");
828 case Instruction::FAdd:
829 apfLHS.add(APFloat(Sem, RHS.IntVal), APFloat::rmNearestTiesToEven);
830 GV.IntVal = apfLHS.bitcastToAPInt();
832 case Instruction::FSub:
833 apfLHS.subtract(APFloat(Sem, RHS.IntVal),
834 APFloat::rmNearestTiesToEven);
835 GV.IntVal = apfLHS.bitcastToAPInt();
837 case Instruction::FMul:
838 apfLHS.multiply(APFloat(Sem, RHS.IntVal),
839 APFloat::rmNearestTiesToEven);
840 GV.IntVal = apfLHS.bitcastToAPInt();
842 case Instruction::FDiv:
843 apfLHS.divide(APFloat(Sem, RHS.IntVal),
844 APFloat::rmNearestTiesToEven);
845 GV.IntVal = apfLHS.bitcastToAPInt();
847 case Instruction::FRem:
848 apfLHS.mod(APFloat(Sem, RHS.IntVal),
849 APFloat::rmNearestTiesToEven);
850 GV.IntVal = apfLHS.bitcastToAPInt();
862 SmallString<256> Msg;
863 raw_svector_ostream OS(Msg);
864 OS << "ConstantExpr not handled: " << *CE;
865 report_fatal_error(OS.str());
868 // Otherwise, we have a simple constant.
870 switch (C->getType()->getTypeID()) {
871 case Type::FloatTyID:
872 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
874 case Type::DoubleTyID:
875 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
877 case Type::X86_FP80TyID:
878 case Type::FP128TyID:
879 case Type::PPC_FP128TyID:
880 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
882 case Type::IntegerTyID:
883 Result.IntVal = cast<ConstantInt>(C)->getValue();
885 case Type::PointerTyID:
886 if (isa<ConstantPointerNull>(C))
887 Result.PointerVal = nullptr;
888 else if (const Function *F = dyn_cast<Function>(C))
889 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
890 else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
891 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
893 llvm_unreachable("Unknown constant pointer type!");
895 case Type::VectorTyID: {
898 const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(C);
899 const ConstantVector *CV = dyn_cast<ConstantVector>(C);
900 const ConstantAggregateZero *CAZ = dyn_cast<ConstantAggregateZero>(C);
903 elemNum = CDV->getNumElements();
904 ElemTy = CDV->getElementType();
905 } else if (CV || CAZ) {
906 VectorType* VTy = dyn_cast<VectorType>(C->getType());
907 elemNum = VTy->getNumElements();
908 ElemTy = VTy->getElementType();
910 llvm_unreachable("Unknown constant vector type!");
913 Result.AggregateVal.resize(elemNum);
914 // Check if vector holds floats.
915 if(ElemTy->isFloatTy()) {
917 GenericValue floatZero;
918 floatZero.FloatVal = 0.f;
919 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
924 for (unsigned i = 0; i < elemNum; ++i)
925 if (!isa<UndefValue>(CV->getOperand(i)))
926 Result.AggregateVal[i].FloatVal = cast<ConstantFP>(
927 CV->getOperand(i))->getValueAPF().convertToFloat();
931 for (unsigned i = 0; i < elemNum; ++i)
932 Result.AggregateVal[i].FloatVal = CDV->getElementAsFloat(i);
936 // Check if vector holds doubles.
937 if (ElemTy->isDoubleTy()) {
939 GenericValue doubleZero;
940 doubleZero.DoubleVal = 0.0;
941 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
946 for (unsigned i = 0; i < elemNum; ++i)
947 if (!isa<UndefValue>(CV->getOperand(i)))
948 Result.AggregateVal[i].DoubleVal = cast<ConstantFP>(
949 CV->getOperand(i))->getValueAPF().convertToDouble();
953 for (unsigned i = 0; i < elemNum; ++i)
954 Result.AggregateVal[i].DoubleVal = CDV->getElementAsDouble(i);
958 // Check if vector holds integers.
959 if (ElemTy->isIntegerTy()) {
961 GenericValue intZero;
962 intZero.IntVal = APInt(ElemTy->getScalarSizeInBits(), 0ull);
963 std::fill(Result.AggregateVal.begin(), Result.AggregateVal.end(),
968 for (unsigned i = 0; i < elemNum; ++i)
969 if (!isa<UndefValue>(CV->getOperand(i)))
970 Result.AggregateVal[i].IntVal = cast<ConstantInt>(
971 CV->getOperand(i))->getValue();
973 Result.AggregateVal[i].IntVal =
974 APInt(CV->getOperand(i)->getType()->getPrimitiveSizeInBits(), 0);
979 for (unsigned i = 0; i < elemNum; ++i)
980 Result.AggregateVal[i].IntVal = APInt(
981 CDV->getElementType()->getPrimitiveSizeInBits(),
982 CDV->getElementAsInteger(i));
986 llvm_unreachable("Unknown constant pointer type!");
991 SmallString<256> Msg;
992 raw_svector_ostream OS(Msg);
993 OS << "ERROR: Constant unimplemented for type: " << *C->getType();
994 report_fatal_error(OS.str());
1000 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
1001 /// with the integer held in IntVal.
1002 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
1003 unsigned StoreBytes) {
1004 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
1005 const uint8_t *Src = (const uint8_t *)IntVal.getRawData();
1007 if (sys::IsLittleEndianHost) {
1008 // Little-endian host - the source is ordered from LSB to MSB. Order the
1009 // destination from LSB to MSB: Do a straight copy.
1010 memcpy(Dst, Src, StoreBytes);
1012 // Big-endian host - the source is an array of 64 bit words ordered from
1013 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
1014 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
1015 while (StoreBytes > sizeof(uint64_t)) {
1016 StoreBytes -= sizeof(uint64_t);
1017 // May not be aligned so use memcpy.
1018 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
1019 Src += sizeof(uint64_t);
1022 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
1026 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
1027 GenericValue *Ptr, Type *Ty) {
1028 const unsigned StoreBytes = getDataLayout()->getTypeStoreSize(Ty);
1030 switch (Ty->getTypeID()) {
1032 dbgs() << "Cannot store value of type " << *Ty << "!\n";
1034 case Type::IntegerTyID:
1035 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
1037 case Type::FloatTyID:
1038 *((float*)Ptr) = Val.FloatVal;
1040 case Type::DoubleTyID:
1041 *((double*)Ptr) = Val.DoubleVal;
1043 case Type::X86_FP80TyID:
1044 memcpy(Ptr, Val.IntVal.getRawData(), 10);
1046 case Type::PointerTyID:
1047 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
1048 if (StoreBytes != sizeof(PointerTy))
1049 memset(&(Ptr->PointerVal), 0, StoreBytes);
1051 *((PointerTy*)Ptr) = Val.PointerVal;
1053 case Type::VectorTyID:
1054 for (unsigned i = 0; i < Val.AggregateVal.size(); ++i) {
1055 if (cast<VectorType>(Ty)->getElementType()->isDoubleTy())
1056 *(((double*)Ptr)+i) = Val.AggregateVal[i].DoubleVal;
1057 if (cast<VectorType>(Ty)->getElementType()->isFloatTy())
1058 *(((float*)Ptr)+i) = Val.AggregateVal[i].FloatVal;
1059 if (cast<VectorType>(Ty)->getElementType()->isIntegerTy()) {
1060 unsigned numOfBytes =(Val.AggregateVal[i].IntVal.getBitWidth()+7)/8;
1061 StoreIntToMemory(Val.AggregateVal[i].IntVal,
1062 (uint8_t*)Ptr + numOfBytes*i, numOfBytes);
1068 if (sys::IsLittleEndianHost != getDataLayout()->isLittleEndian())
1069 // Host and target are different endian - reverse the stored bytes.
1070 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
1073 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
1074 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
1075 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
1076 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
1077 uint8_t *Dst = reinterpret_cast<uint8_t *>(
1078 const_cast<uint64_t *>(IntVal.getRawData()));
1080 if (sys::IsLittleEndianHost)
1081 // Little-endian host - the destination must be ordered from LSB to MSB.
1082 // The source is ordered from LSB to MSB: Do a straight copy.
1083 memcpy(Dst, Src, LoadBytes);
1085 // Big-endian - the destination is an array of 64 bit words ordered from
1086 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
1087 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
1089 while (LoadBytes > sizeof(uint64_t)) {
1090 LoadBytes -= sizeof(uint64_t);
1091 // May not be aligned so use memcpy.
1092 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
1093 Dst += sizeof(uint64_t);
1096 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
1102 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
1105 const unsigned LoadBytes = getDataLayout()->getTypeStoreSize(Ty);
1107 switch (Ty->getTypeID()) {
1108 case Type::IntegerTyID:
1109 // An APInt with all words initially zero.
1110 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
1111 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
1113 case Type::FloatTyID:
1114 Result.FloatVal = *((float*)Ptr);
1116 case Type::DoubleTyID:
1117 Result.DoubleVal = *((double*)Ptr);
1119 case Type::PointerTyID:
1120 Result.PointerVal = *((PointerTy*)Ptr);
1122 case Type::X86_FP80TyID: {
1123 // This is endian dependent, but it will only work on x86 anyway.
1124 // FIXME: Will not trap if loading a signaling NaN.
1127 Result.IntVal = APInt(80, y);
1130 case Type::VectorTyID: {
1131 const VectorType *VT = cast<VectorType>(Ty);
1132 const Type *ElemT = VT->getElementType();
1133 const unsigned numElems = VT->getNumElements();
1134 if (ElemT->isFloatTy()) {
1135 Result.AggregateVal.resize(numElems);
1136 for (unsigned i = 0; i < numElems; ++i)
1137 Result.AggregateVal[i].FloatVal = *((float*)Ptr+i);
1139 if (ElemT->isDoubleTy()) {
1140 Result.AggregateVal.resize(numElems);
1141 for (unsigned i = 0; i < numElems; ++i)
1142 Result.AggregateVal[i].DoubleVal = *((double*)Ptr+i);
1144 if (ElemT->isIntegerTy()) {
1145 GenericValue intZero;
1146 const unsigned elemBitWidth = cast<IntegerType>(ElemT)->getBitWidth();
1147 intZero.IntVal = APInt(elemBitWidth, 0);
1148 Result.AggregateVal.resize(numElems, intZero);
1149 for (unsigned i = 0; i < numElems; ++i)
1150 LoadIntFromMemory(Result.AggregateVal[i].IntVal,
1151 (uint8_t*)Ptr+((elemBitWidth+7)/8)*i, (elemBitWidth+7)/8);
1156 SmallString<256> Msg;
1157 raw_svector_ostream OS(Msg);
1158 OS << "Cannot load value of type " << *Ty << "!";
1159 report_fatal_error(OS.str());
1163 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
1164 DEBUG(dbgs() << "JIT: Initializing " << Addr << " ");
1165 DEBUG(Init->dump());
1166 if (isa<UndefValue>(Init))
1169 if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
1170 unsigned ElementSize =
1171 getDataLayout()->getTypeAllocSize(CP->getType()->getElementType());
1172 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
1173 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
1177 if (isa<ConstantAggregateZero>(Init)) {
1178 memset(Addr, 0, (size_t)getDataLayout()->getTypeAllocSize(Init->getType()));
1182 if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
1183 unsigned ElementSize =
1184 getDataLayout()->getTypeAllocSize(CPA->getType()->getElementType());
1185 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
1186 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
1190 if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
1191 const StructLayout *SL =
1192 getDataLayout()->getStructLayout(cast<StructType>(CPS->getType()));
1193 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
1194 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
1198 if (const ConstantDataSequential *CDS =
1199 dyn_cast<ConstantDataSequential>(Init)) {
1200 // CDS is already laid out in host memory order.
1201 StringRef Data = CDS->getRawDataValues();
1202 memcpy(Addr, Data.data(), Data.size());
1206 if (Init->getType()->isFirstClassType()) {
1207 GenericValue Val = getConstantValue(Init);
1208 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
1212 DEBUG(dbgs() << "Bad Type: " << *Init->getType() << "\n");
1213 llvm_unreachable("Unknown constant type to initialize memory with!");
1216 /// EmitGlobals - Emit all of the global variables to memory, storing their
1217 /// addresses into GlobalAddress. This must make sure to copy the contents of
1218 /// their initializers into the memory.
1219 void ExecutionEngine::emitGlobals() {
1220 // Loop over all of the global variables in the program, allocating the memory
1221 // to hold them. If there is more than one module, do a prepass over globals
1222 // to figure out how the different modules should link together.
1223 std::map<std::pair<std::string, Type*>,
1224 const GlobalValue*> LinkedGlobalsMap;
1226 if (Modules.size() != 1) {
1227 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1228 Module &M = *Modules[m];
1229 for (const auto &GV : M.globals()) {
1230 if (GV.hasLocalLinkage() || GV.isDeclaration() ||
1231 GV.hasAppendingLinkage() || !GV.hasName())
1232 continue;// Ignore external globals and globals with internal linkage.
1234 const GlobalValue *&GVEntry =
1235 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())];
1237 // If this is the first time we've seen this global, it is the canonical
1244 // If the existing global is strong, never replace it.
1245 if (GVEntry->hasExternalLinkage())
1248 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1249 // symbol. FIXME is this right for common?
1250 if (GV.hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
1256 std::vector<const GlobalValue*> NonCanonicalGlobals;
1257 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
1258 Module &M = *Modules[m];
1259 for (const auto &GV : M.globals()) {
1260 // In the multi-module case, see what this global maps to.
1261 if (!LinkedGlobalsMap.empty()) {
1262 if (const GlobalValue *GVEntry =
1263 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())]) {
1264 // If something else is the canonical global, ignore this one.
1265 if (GVEntry != &GV) {
1266 NonCanonicalGlobals.push_back(&GV);
1272 if (!GV.isDeclaration()) {
1273 addGlobalMapping(&GV, getMemoryForGV(&GV));
1275 // External variable reference. Try to use the dynamic loader to
1276 // get a pointer to it.
1278 sys::DynamicLibrary::SearchForAddressOfSymbol(GV.getName()))
1279 addGlobalMapping(&GV, SymAddr);
1281 report_fatal_error("Could not resolve external global address: "
1287 // If there are multiple modules, map the non-canonical globals to their
1288 // canonical location.
1289 if (!NonCanonicalGlobals.empty()) {
1290 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1291 const GlobalValue *GV = NonCanonicalGlobals[i];
1292 const GlobalValue *CGV =
1293 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1294 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1295 assert(Ptr && "Canonical global wasn't codegen'd!");
1296 addGlobalMapping(GV, Ptr);
1300 // Now that all of the globals are set up in memory, loop through them all
1301 // and initialize their contents.
1302 for (const auto &GV : M.globals()) {
1303 if (!GV.isDeclaration()) {
1304 if (!LinkedGlobalsMap.empty()) {
1305 if (const GlobalValue *GVEntry =
1306 LinkedGlobalsMap[std::make_pair(GV.getName(), GV.getType())])
1307 if (GVEntry != &GV) // Not the canonical variable.
1310 EmitGlobalVariable(&GV);
1316 // EmitGlobalVariable - This method emits the specified global variable to the
1317 // address specified in GlobalAddresses, or allocates new memory if it's not
1318 // already in the map.
1319 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1320 void *GA = getPointerToGlobalIfAvailable(GV);
1323 // If it's not already specified, allocate memory for the global.
1324 GA = getMemoryForGV(GV);
1326 // If we failed to allocate memory for this global, return.
1329 addGlobalMapping(GV, GA);
1332 // Don't initialize if it's thread local, let the client do it.
1333 if (!GV->isThreadLocal())
1334 InitializeMemory(GV->getInitializer(), GA);
1336 Type *ElTy = GV->getType()->getElementType();
1337 size_t GVSize = (size_t)getDataLayout()->getTypeAllocSize(ElTy);
1338 NumInitBytes += (unsigned)GVSize;