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 #define DEBUG_TYPE "jit"
16 #include "llvm/ExecutionEngine/ExecutionEngine.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Module.h"
21 #include "llvm/ModuleProvider.h"
22 #include "llvm/ExecutionEngine/GenericValue.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/MutexGuard.h"
27 #include "llvm/Support/ValueHandle.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/System/DynamicLibrary.h"
30 #include "llvm/System/Host.h"
31 #include "llvm/Target/TargetData.h"
36 STATISTIC(NumInitBytes, "Number of bytes of global vars initialized");
37 STATISTIC(NumGlobals , "Number of global vars initialized");
39 ExecutionEngine *(*ExecutionEngine::JITCtor)(ModuleProvider *MP,
40 std::string *ErrorStr,
41 JITMemoryManager *JMM,
42 CodeGenOpt::Level OptLevel,
43 bool GVsWithCode) = 0;
44 ExecutionEngine *(*ExecutionEngine::InterpCtor)(ModuleProvider *MP,
45 std::string *ErrorStr) = 0;
46 ExecutionEngine::EERegisterFn ExecutionEngine::ExceptionTableRegister = 0;
49 ExecutionEngine::ExecutionEngine(ModuleProvider *P) : LazyFunctionCreator(0) {
50 LazyCompilationDisabled = false;
51 GVCompilationDisabled = false;
52 SymbolSearchingDisabled = false;
53 DlsymStubsEnabled = false;
55 assert(P && "ModuleProvider is null?");
58 ExecutionEngine::~ExecutionEngine() {
59 clearAllGlobalMappings();
60 for (unsigned i = 0, e = Modules.size(); i != e; ++i)
64 char* ExecutionEngine::getMemoryForGV(const GlobalVariable* GV) {
65 const Type *ElTy = GV->getType()->getElementType();
66 size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
67 return new char[GVSize];
70 /// removeModuleProvider - Remove a ModuleProvider from the list of modules.
71 /// Relases the Module from the ModuleProvider, materializing it in the
72 /// process, and returns the materialized Module.
73 Module* ExecutionEngine::removeModuleProvider(ModuleProvider *P,
74 std::string *ErrInfo) {
75 for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(),
76 E = Modules.end(); I != E; ++I) {
77 ModuleProvider *MP = *I;
80 clearGlobalMappingsFromModule(MP->getModule());
81 return MP->releaseModule(ErrInfo);
87 /// deleteModuleProvider - Remove a ModuleProvider from the list of modules,
88 /// and deletes the ModuleProvider and owned Module. Avoids materializing
89 /// the underlying module.
90 void ExecutionEngine::deleteModuleProvider(ModuleProvider *P,
91 std::string *ErrInfo) {
92 for(SmallVector<ModuleProvider *, 1>::iterator I = Modules.begin(),
93 E = Modules.end(); I != E; ++I) {
94 ModuleProvider *MP = *I;
97 clearGlobalMappingsFromModule(MP->getModule());
104 /// FindFunctionNamed - Search all of the active modules to find the one that
105 /// defines FnName. This is very slow operation and shouldn't be used for
107 Function *ExecutionEngine::FindFunctionNamed(const char *FnName) {
108 for (unsigned i = 0, e = Modules.size(); i != e; ++i) {
109 if (Function *F = Modules[i]->getModule()->getFunction(FnName))
116 /// addGlobalMapping - Tell the execution engine that the specified global is
117 /// at the specified location. This is used internally as functions are JIT'd
118 /// and as global variables are laid out in memory. It can and should also be
119 /// used by clients of the EE that want to have an LLVM global overlay
120 /// existing data in memory.
121 void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
122 MutexGuard locked(lock);
124 DEBUG(errs() << "JIT: Map \'" << GV->getName()
125 << "\' to [" << Addr << "]\n";);
126 void *&CurVal = state.getGlobalAddressMap(locked)[GV];
127 assert((CurVal == 0 || Addr == 0) && "GlobalMapping already established!");
130 // If we are using the reverse mapping, add it too
131 if (!state.getGlobalAddressReverseMap(locked).empty()) {
132 AssertingVH<const GlobalValue> &V =
133 state.getGlobalAddressReverseMap(locked)[Addr];
134 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
139 /// clearAllGlobalMappings - Clear all global mappings and start over again
140 /// use in dynamic compilation scenarios when you want to move globals
141 void ExecutionEngine::clearAllGlobalMappings() {
142 MutexGuard locked(lock);
144 state.getGlobalAddressMap(locked).clear();
145 state.getGlobalAddressReverseMap(locked).clear();
148 /// clearGlobalMappingsFromModule - Clear all global mappings that came from a
149 /// particular module, because it has been removed from the JIT.
150 void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
151 MutexGuard locked(lock);
153 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) {
154 state.getGlobalAddressMap(locked).erase(&*FI);
155 state.getGlobalAddressReverseMap(locked).erase(&*FI);
157 for (Module::global_iterator GI = M->global_begin(), GE = M->global_end();
159 state.getGlobalAddressMap(locked).erase(&*GI);
160 state.getGlobalAddressReverseMap(locked).erase(&*GI);
164 /// updateGlobalMapping - Replace an existing mapping for GV with a new
165 /// address. This updates both maps as required. If "Addr" is null, the
166 /// entry for the global is removed from the mappings.
167 void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
168 MutexGuard locked(lock);
170 std::map<AssertingVH<const GlobalValue>, void *> &Map =
171 state.getGlobalAddressMap(locked);
173 // Deleting from the mapping?
175 std::map<AssertingVH<const GlobalValue>, void *>::iterator I = Map.find(GV);
184 if (!state.getGlobalAddressReverseMap(locked).empty())
185 state.getGlobalAddressReverseMap(locked).erase(OldVal);
189 void *&CurVal = Map[GV];
190 void *OldVal = CurVal;
192 if (CurVal && !state.getGlobalAddressReverseMap(locked).empty())
193 state.getGlobalAddressReverseMap(locked).erase(CurVal);
196 // If we are using the reverse mapping, add it too
197 if (!state.getGlobalAddressReverseMap(locked).empty()) {
198 AssertingVH<const GlobalValue> &V =
199 state.getGlobalAddressReverseMap(locked)[Addr];
200 assert((V == 0 || GV == 0) && "GlobalMapping already established!");
206 /// getPointerToGlobalIfAvailable - This returns the address of the specified
207 /// global value if it is has already been codegen'd, otherwise it returns null.
209 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
210 MutexGuard locked(lock);
212 std::map<AssertingVH<const GlobalValue>, void*>::iterator I =
213 state.getGlobalAddressMap(locked).find(GV);
214 return I != state.getGlobalAddressMap(locked).end() ? I->second : 0;
217 /// getGlobalValueAtAddress - Return the LLVM global value object that starts
218 /// at the specified address.
220 const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
221 MutexGuard locked(lock);
223 // If we haven't computed the reverse mapping yet, do so first.
224 if (state.getGlobalAddressReverseMap(locked).empty()) {
225 for (std::map<AssertingVH<const GlobalValue>, void *>::iterator
226 I = state.getGlobalAddressMap(locked).begin(),
227 E = state.getGlobalAddressMap(locked).end(); I != E; ++I)
228 state.getGlobalAddressReverseMap(locked).insert(std::make_pair(I->second,
232 std::map<void *, AssertingVH<const GlobalValue> >::iterator I =
233 state.getGlobalAddressReverseMap(locked).find(Addr);
234 return I != state.getGlobalAddressReverseMap(locked).end() ? I->second : 0;
237 // CreateArgv - Turn a vector of strings into a nice argv style array of
238 // pointers to null terminated strings.
240 static void *CreateArgv(LLVMContext &C, ExecutionEngine *EE,
241 const std::vector<std::string> &InputArgv) {
242 unsigned PtrSize = EE->getTargetData()->getPointerSize();
243 char *Result = new char[(InputArgv.size()+1)*PtrSize];
245 DEBUG(errs() << "JIT: ARGV = " << (void*)Result << "\n");
246 const Type *SBytePtr = Type::getInt8PtrTy(C);
248 for (unsigned i = 0; i != InputArgv.size(); ++i) {
249 unsigned Size = InputArgv[i].size()+1;
250 char *Dest = new char[Size];
251 DEBUG(errs() << "JIT: ARGV[" << i << "] = " << (void*)Dest << "\n");
253 std::copy(InputArgv[i].begin(), InputArgv[i].end(), Dest);
256 // Endian safe: Result[i] = (PointerTy)Dest;
257 EE->StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i*PtrSize),
262 EE->StoreValueToMemory(PTOGV(0),
263 (GenericValue*)(Result+InputArgv.size()*PtrSize),
269 /// runStaticConstructorsDestructors - This method is used to execute all of
270 /// the static constructors or destructors for a module, depending on the
271 /// value of isDtors.
272 void ExecutionEngine::runStaticConstructorsDestructors(Module *module,
274 const char *Name = isDtors ? "llvm.global_dtors" : "llvm.global_ctors";
276 // Execute global ctors/dtors for each module in the program.
278 GlobalVariable *GV = module->getNamedGlobal(Name);
280 // If this global has internal linkage, or if it has a use, then it must be
281 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
282 // this is the case, don't execute any of the global ctors, __main will do
284 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage()) return;
286 // Should be an array of '{ int, void ()* }' structs. The first value is
287 // the init priority, which we ignore.
288 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
289 if (!InitList) return;
290 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
291 if (ConstantStruct *CS =
292 dyn_cast<ConstantStruct>(InitList->getOperand(i))) {
293 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
295 Constant *FP = CS->getOperand(1);
296 if (FP->isNullValue())
297 break; // Found a null terminator, exit.
299 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
301 FP = CE->getOperand(0);
302 if (Function *F = dyn_cast<Function>(FP)) {
303 // Execute the ctor/dtor function!
304 runFunction(F, std::vector<GenericValue>());
309 /// runStaticConstructorsDestructors - This method is used to execute all of
310 /// the static constructors or destructors for a program, depending on the
311 /// value of isDtors.
312 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors) {
313 // Execute global ctors/dtors for each module in the program.
314 for (unsigned m = 0, e = Modules.size(); m != e; ++m)
315 runStaticConstructorsDestructors(Modules[m]->getModule(), isDtors);
319 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
320 static bool isTargetNullPtr(ExecutionEngine *EE, void *Loc) {
321 unsigned PtrSize = EE->getTargetData()->getPointerSize();
322 for (unsigned i = 0; i < PtrSize; ++i)
323 if (*(i + (uint8_t*)Loc))
329 /// runFunctionAsMain - This is a helper function which wraps runFunction to
330 /// handle the common task of starting up main with the specified argc, argv,
331 /// and envp parameters.
332 int ExecutionEngine::runFunctionAsMain(Function *Fn,
333 const std::vector<std::string> &argv,
334 const char * const * envp) {
335 std::vector<GenericValue> GVArgs;
337 GVArgc.IntVal = APInt(32, argv.size());
340 unsigned NumArgs = Fn->getFunctionType()->getNumParams();
341 const FunctionType *FTy = Fn->getFunctionType();
342 const Type* PPInt8Ty =
343 PointerType::getUnqual(PointerType::getUnqual(
344 Type::getInt8Ty(Fn->getContext())));
347 if (FTy->getParamType(2) != PPInt8Ty) {
348 llvm_report_error("Invalid type for third argument of main() supplied");
352 if (FTy->getParamType(1) != PPInt8Ty) {
353 llvm_report_error("Invalid type for second argument of main() supplied");
357 if (FTy->getParamType(0) != Type::getInt32Ty(Fn->getContext())) {
358 llvm_report_error("Invalid type for first argument of main() supplied");
362 if (!isa<IntegerType>(FTy->getReturnType()) &&
363 FTy->getReturnType() != Type::getVoidTy(FTy->getContext())) {
364 llvm_report_error("Invalid return type of main() supplied");
368 llvm_report_error("Invalid number of arguments of main() supplied");
372 GVArgs.push_back(GVArgc); // Arg #0 = argc.
375 GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, argv)));
376 assert(!isTargetNullPtr(this, GVTOP(GVArgs[1])) &&
377 "argv[0] was null after CreateArgv");
379 std::vector<std::string> EnvVars;
380 for (unsigned i = 0; envp[i]; ++i)
381 EnvVars.push_back(envp[i]);
383 GVArgs.push_back(PTOGV(CreateArgv(Fn->getContext(), this, EnvVars)));
387 return runFunction(Fn, GVArgs).IntVal.getZExtValue();
390 /// If possible, create a JIT, unless the caller specifically requests an
391 /// Interpreter or there's an error. If even an Interpreter cannot be created,
392 /// NULL is returned.
394 ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
395 bool ForceInterpreter,
396 std::string *ErrorStr,
397 CodeGenOpt::Level OptLevel,
399 return EngineBuilder(MP)
400 .setEngineKind(ForceInterpreter
401 ? EngineKind::Interpreter
403 .setErrorStr(ErrorStr)
404 .setOptLevel(OptLevel)
405 .setAllocateGVsWithCode(GVsWithCode)
409 ExecutionEngine *ExecutionEngine::create(Module *M) {
410 return EngineBuilder(M).create();
413 /// EngineBuilder - Overloaded constructor that automatically creates an
414 /// ExistingModuleProvider for an existing module.
415 EngineBuilder::EngineBuilder(Module *m) : MP(new ExistingModuleProvider(m)) {
419 ExecutionEngine *EngineBuilder::create() {
420 // Make sure we can resolve symbols in the program as well. The zero arg
421 // to the function tells DynamicLibrary to load the program, not a library.
422 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr))
425 // If the user specified a memory manager but didn't specify which engine to
426 // create, we assume they only want the JIT, and we fail if they only want
429 if (WhichEngine & EngineKind::JIT)
430 WhichEngine = EngineKind::JIT;
433 *ErrorStr = "Cannot create an interpreter with a memory manager.";
438 // Unless the interpreter was explicitly selected or the JIT is not linked,
440 if (WhichEngine & EngineKind::JIT) {
441 if (ExecutionEngine::JITCtor) {
442 ExecutionEngine *EE =
443 ExecutionEngine::JITCtor(MP, ErrorStr, JMM, OptLevel,
444 AllocateGVsWithCode);
449 // If we can't make a JIT and we didn't request one specifically, try making
450 // an interpreter instead.
451 if (WhichEngine & EngineKind::Interpreter) {
452 if (ExecutionEngine::InterpCtor)
453 return ExecutionEngine::InterpCtor(MP, ErrorStr);
455 *ErrorStr = "Interpreter has not been linked in.";
459 if ((WhichEngine & EngineKind::JIT) && ExecutionEngine::JITCtor == 0) {
461 *ErrorStr = "JIT has not been linked in.";
466 /// getPointerToGlobal - This returns the address of the specified global
467 /// value. This may involve code generation if it's a function.
469 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
470 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
471 return getPointerToFunction(F);
473 MutexGuard locked(lock);
474 void *p = state.getGlobalAddressMap(locked)[GV];
478 // Global variable might have been added since interpreter started.
479 if (GlobalVariable *GVar =
480 const_cast<GlobalVariable *>(dyn_cast<GlobalVariable>(GV)))
481 EmitGlobalVariable(GVar);
483 llvm_unreachable("Global hasn't had an address allocated yet!");
484 return state.getGlobalAddressMap(locked)[GV];
487 /// This function converts a Constant* into a GenericValue. The interesting
488 /// part is if C is a ConstantExpr.
489 /// @brief Get a GenericValue for a Constant*
490 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
491 // If its undefined, return the garbage.
492 if (isa<UndefValue>(C))
493 return GenericValue();
495 // If the value is a ConstantExpr
496 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
497 Constant *Op0 = CE->getOperand(0);
498 switch (CE->getOpcode()) {
499 case Instruction::GetElementPtr: {
501 GenericValue Result = getConstantValue(Op0);
502 SmallVector<Value*, 8> Indices(CE->op_begin()+1, CE->op_end());
504 TD->getIndexedOffset(Op0->getType(), &Indices[0], Indices.size());
506 char* tmp = (char*) Result.PointerVal;
507 Result = PTOGV(tmp + Offset);
510 case Instruction::Trunc: {
511 GenericValue GV = getConstantValue(Op0);
512 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
513 GV.IntVal = GV.IntVal.trunc(BitWidth);
516 case Instruction::ZExt: {
517 GenericValue GV = getConstantValue(Op0);
518 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
519 GV.IntVal = GV.IntVal.zext(BitWidth);
522 case Instruction::SExt: {
523 GenericValue GV = getConstantValue(Op0);
524 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
525 GV.IntVal = GV.IntVal.sext(BitWidth);
528 case Instruction::FPTrunc: {
530 GenericValue GV = getConstantValue(Op0);
531 GV.FloatVal = float(GV.DoubleVal);
534 case Instruction::FPExt:{
536 GenericValue GV = getConstantValue(Op0);
537 GV.DoubleVal = double(GV.FloatVal);
540 case Instruction::UIToFP: {
541 GenericValue GV = getConstantValue(Op0);
542 if (CE->getType()->isFloatTy())
543 GV.FloatVal = float(GV.IntVal.roundToDouble());
544 else if (CE->getType()->isDoubleTy())
545 GV.DoubleVal = GV.IntVal.roundToDouble();
546 else if (CE->getType()->isX86_FP80Ty()) {
547 const uint64_t zero[] = {0, 0};
548 APFloat apf = APFloat(APInt(80, 2, zero));
549 (void)apf.convertFromAPInt(GV.IntVal,
551 APFloat::rmNearestTiesToEven);
552 GV.IntVal = apf.bitcastToAPInt();
556 case Instruction::SIToFP: {
557 GenericValue GV = getConstantValue(Op0);
558 if (CE->getType()->isFloatTy())
559 GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
560 else if (CE->getType()->isDoubleTy())
561 GV.DoubleVal = GV.IntVal.signedRoundToDouble();
562 else if (CE->getType()->isX86_FP80Ty()) {
563 const uint64_t zero[] = { 0, 0};
564 APFloat apf = APFloat(APInt(80, 2, zero));
565 (void)apf.convertFromAPInt(GV.IntVal,
567 APFloat::rmNearestTiesToEven);
568 GV.IntVal = apf.bitcastToAPInt();
572 case Instruction::FPToUI: // double->APInt conversion handles sign
573 case Instruction::FPToSI: {
574 GenericValue GV = getConstantValue(Op0);
575 uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
576 if (Op0->getType()->isFloatTy())
577 GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
578 else if (Op0->getType()->isDoubleTy())
579 GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
580 else if (Op0->getType()->isX86_FP80Ty()) {
581 APFloat apf = APFloat(GV.IntVal);
584 (void)apf.convertToInteger(&v, BitWidth,
585 CE->getOpcode()==Instruction::FPToSI,
586 APFloat::rmTowardZero, &ignored);
587 GV.IntVal = v; // endian?
591 case Instruction::PtrToInt: {
592 GenericValue GV = getConstantValue(Op0);
593 uint32_t PtrWidth = TD->getPointerSizeInBits();
594 GV.IntVal = APInt(PtrWidth, uintptr_t(GV.PointerVal));
597 case Instruction::IntToPtr: {
598 GenericValue GV = getConstantValue(Op0);
599 uint32_t PtrWidth = TD->getPointerSizeInBits();
600 if (PtrWidth != GV.IntVal.getBitWidth())
601 GV.IntVal = GV.IntVal.zextOrTrunc(PtrWidth);
602 assert(GV.IntVal.getBitWidth() <= 64 && "Bad pointer width");
603 GV.PointerVal = PointerTy(uintptr_t(GV.IntVal.getZExtValue()));
606 case Instruction::BitCast: {
607 GenericValue GV = getConstantValue(Op0);
608 const Type* DestTy = CE->getType();
609 switch (Op0->getType()->getTypeID()) {
610 default: llvm_unreachable("Invalid bitcast operand");
611 case Type::IntegerTyID:
612 assert(DestTy->isFloatingPoint() && "invalid bitcast");
613 if (DestTy->isFloatTy())
614 GV.FloatVal = GV.IntVal.bitsToFloat();
615 else if (DestTy->isDoubleTy())
616 GV.DoubleVal = GV.IntVal.bitsToDouble();
618 case Type::FloatTyID:
619 assert(DestTy == Type::getInt32Ty(DestTy->getContext()) &&
621 GV.IntVal.floatToBits(GV.FloatVal);
623 case Type::DoubleTyID:
624 assert(DestTy == Type::getInt64Ty(DestTy->getContext()) &&
626 GV.IntVal.doubleToBits(GV.DoubleVal);
628 case Type::PointerTyID:
629 assert(isa<PointerType>(DestTy) && "Invalid bitcast");
630 break; // getConstantValue(Op0) above already converted it
634 case Instruction::Add:
635 case Instruction::FAdd:
636 case Instruction::Sub:
637 case Instruction::FSub:
638 case Instruction::Mul:
639 case Instruction::FMul:
640 case Instruction::UDiv:
641 case Instruction::SDiv:
642 case Instruction::URem:
643 case Instruction::SRem:
644 case Instruction::And:
645 case Instruction::Or:
646 case Instruction::Xor: {
647 GenericValue LHS = getConstantValue(Op0);
648 GenericValue RHS = getConstantValue(CE->getOperand(1));
650 switch (CE->getOperand(0)->getType()->getTypeID()) {
651 default: llvm_unreachable("Bad add type!");
652 case Type::IntegerTyID:
653 switch (CE->getOpcode()) {
654 default: llvm_unreachable("Invalid integer opcode");
655 case Instruction::Add: GV.IntVal = LHS.IntVal + RHS.IntVal; break;
656 case Instruction::Sub: GV.IntVal = LHS.IntVal - RHS.IntVal; break;
657 case Instruction::Mul: GV.IntVal = LHS.IntVal * RHS.IntVal; break;
658 case Instruction::UDiv:GV.IntVal = LHS.IntVal.udiv(RHS.IntVal); break;
659 case Instruction::SDiv:GV.IntVal = LHS.IntVal.sdiv(RHS.IntVal); break;
660 case Instruction::URem:GV.IntVal = LHS.IntVal.urem(RHS.IntVal); break;
661 case Instruction::SRem:GV.IntVal = LHS.IntVal.srem(RHS.IntVal); break;
662 case Instruction::And: GV.IntVal = LHS.IntVal & RHS.IntVal; break;
663 case Instruction::Or: GV.IntVal = LHS.IntVal | RHS.IntVal; break;
664 case Instruction::Xor: GV.IntVal = LHS.IntVal ^ RHS.IntVal; break;
667 case Type::FloatTyID:
668 switch (CE->getOpcode()) {
669 default: llvm_unreachable("Invalid float opcode");
670 case Instruction::FAdd:
671 GV.FloatVal = LHS.FloatVal + RHS.FloatVal; break;
672 case Instruction::FSub:
673 GV.FloatVal = LHS.FloatVal - RHS.FloatVal; break;
674 case Instruction::FMul:
675 GV.FloatVal = LHS.FloatVal * RHS.FloatVal; break;
676 case Instruction::FDiv:
677 GV.FloatVal = LHS.FloatVal / RHS.FloatVal; break;
678 case Instruction::FRem:
679 GV.FloatVal = ::fmodf(LHS.FloatVal,RHS.FloatVal); break;
682 case Type::DoubleTyID:
683 switch (CE->getOpcode()) {
684 default: llvm_unreachable("Invalid double opcode");
685 case Instruction::FAdd:
686 GV.DoubleVal = LHS.DoubleVal + RHS.DoubleVal; break;
687 case Instruction::FSub:
688 GV.DoubleVal = LHS.DoubleVal - RHS.DoubleVal; break;
689 case Instruction::FMul:
690 GV.DoubleVal = LHS.DoubleVal * RHS.DoubleVal; break;
691 case Instruction::FDiv:
692 GV.DoubleVal = LHS.DoubleVal / RHS.DoubleVal; break;
693 case Instruction::FRem:
694 GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
697 case Type::X86_FP80TyID:
698 case Type::PPC_FP128TyID:
699 case Type::FP128TyID: {
700 APFloat apfLHS = APFloat(LHS.IntVal);
701 switch (CE->getOpcode()) {
702 default: llvm_unreachable("Invalid long double opcode");llvm_unreachable(0);
703 case Instruction::FAdd:
704 apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
705 GV.IntVal = apfLHS.bitcastToAPInt();
707 case Instruction::FSub:
708 apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
709 GV.IntVal = apfLHS.bitcastToAPInt();
711 case Instruction::FMul:
712 apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
713 GV.IntVal = apfLHS.bitcastToAPInt();
715 case Instruction::FDiv:
716 apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
717 GV.IntVal = apfLHS.bitcastToAPInt();
719 case Instruction::FRem:
720 apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
721 GV.IntVal = apfLHS.bitcastToAPInt();
733 raw_string_ostream Msg(msg);
734 Msg << "ConstantExpr not handled: " << *CE;
735 llvm_report_error(Msg.str());
739 switch (C->getType()->getTypeID()) {
740 case Type::FloatTyID:
741 Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat();
743 case Type::DoubleTyID:
744 Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
746 case Type::X86_FP80TyID:
747 case Type::FP128TyID:
748 case Type::PPC_FP128TyID:
749 Result.IntVal = cast <ConstantFP>(C)->getValueAPF().bitcastToAPInt();
751 case Type::IntegerTyID:
752 Result.IntVal = cast<ConstantInt>(C)->getValue();
754 case Type::PointerTyID:
755 if (isa<ConstantPointerNull>(C))
756 Result.PointerVal = 0;
757 else if (const Function *F = dyn_cast<Function>(C))
758 Result = PTOGV(getPointerToFunctionOrStub(const_cast<Function*>(F)));
759 else if (const GlobalVariable* GV = dyn_cast<GlobalVariable>(C))
760 Result = PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable*>(GV)));
762 llvm_unreachable("Unknown constant pointer type!");
766 raw_string_ostream Msg(msg);
767 Msg << "ERROR: Constant unimplemented for type: " << *C->getType();
768 llvm_report_error(Msg.str());
773 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
774 /// with the integer held in IntVal.
775 static void StoreIntToMemory(const APInt &IntVal, uint8_t *Dst,
776 unsigned StoreBytes) {
777 assert((IntVal.getBitWidth()+7)/8 >= StoreBytes && "Integer too small!");
778 uint8_t *Src = (uint8_t *)IntVal.getRawData();
780 if (sys::isLittleEndianHost())
781 // Little-endian host - the source is ordered from LSB to MSB. Order the
782 // destination from LSB to MSB: Do a straight copy.
783 memcpy(Dst, Src, StoreBytes);
785 // Big-endian host - the source is an array of 64 bit words ordered from
786 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
787 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
788 while (StoreBytes > sizeof(uint64_t)) {
789 StoreBytes -= sizeof(uint64_t);
790 // May not be aligned so use memcpy.
791 memcpy(Dst + StoreBytes, Src, sizeof(uint64_t));
792 Src += sizeof(uint64_t);
795 memcpy(Dst, Src + sizeof(uint64_t) - StoreBytes, StoreBytes);
799 /// StoreValueToMemory - Stores the data in Val of type Ty at address Ptr. Ptr
800 /// is the address of the memory at which to store Val, cast to GenericValue *.
801 /// It is not a pointer to a GenericValue containing the address at which to
803 void ExecutionEngine::StoreValueToMemory(const GenericValue &Val,
804 GenericValue *Ptr, const Type *Ty) {
805 const unsigned StoreBytes = getTargetData()->getTypeStoreSize(Ty);
807 switch (Ty->getTypeID()) {
808 case Type::IntegerTyID:
809 StoreIntToMemory(Val.IntVal, (uint8_t*)Ptr, StoreBytes);
811 case Type::FloatTyID:
812 *((float*)Ptr) = Val.FloatVal;
814 case Type::DoubleTyID:
815 *((double*)Ptr) = Val.DoubleVal;
817 case Type::X86_FP80TyID:
818 memcpy(Ptr, Val.IntVal.getRawData(), 10);
820 case Type::PointerTyID:
821 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
822 if (StoreBytes != sizeof(PointerTy))
823 memset(Ptr, 0, StoreBytes);
825 *((PointerTy*)Ptr) = Val.PointerVal;
828 errs() << "Cannot store value of type " << *Ty << "!\n";
831 if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian())
832 // Host and target are different endian - reverse the stored bytes.
833 std::reverse((uint8_t*)Ptr, StoreBytes + (uint8_t*)Ptr);
836 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
837 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
838 static void LoadIntFromMemory(APInt &IntVal, uint8_t *Src, unsigned LoadBytes) {
839 assert((IntVal.getBitWidth()+7)/8 >= LoadBytes && "Integer too small!");
840 uint8_t *Dst = (uint8_t *)IntVal.getRawData();
842 if (sys::isLittleEndianHost())
843 // Little-endian host - the destination must be ordered from LSB to MSB.
844 // The source is ordered from LSB to MSB: Do a straight copy.
845 memcpy(Dst, Src, LoadBytes);
847 // Big-endian - the destination is an array of 64 bit words ordered from
848 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
849 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
851 while (LoadBytes > sizeof(uint64_t)) {
852 LoadBytes -= sizeof(uint64_t);
853 // May not be aligned so use memcpy.
854 memcpy(Dst, Src + LoadBytes, sizeof(uint64_t));
855 Dst += sizeof(uint64_t);
858 memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
864 void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
867 const unsigned LoadBytes = getTargetData()->getTypeStoreSize(Ty);
869 switch (Ty->getTypeID()) {
870 case Type::IntegerTyID:
871 // An APInt with all words initially zero.
872 Result.IntVal = APInt(cast<IntegerType>(Ty)->getBitWidth(), 0);
873 LoadIntFromMemory(Result.IntVal, (uint8_t*)Ptr, LoadBytes);
875 case Type::FloatTyID:
876 Result.FloatVal = *((float*)Ptr);
878 case Type::DoubleTyID:
879 Result.DoubleVal = *((double*)Ptr);
881 case Type::PointerTyID:
882 Result.PointerVal = *((PointerTy*)Ptr);
884 case Type::X86_FP80TyID: {
885 // This is endian dependent, but it will only work on x86 anyway.
886 // FIXME: Will not trap if loading a signaling NaN.
889 Result.IntVal = APInt(80, 2, y);
894 raw_string_ostream Msg(msg);
895 Msg << "Cannot load value of type " << *Ty << "!";
896 llvm_report_error(Msg.str());
900 // InitializeMemory - Recursive function to apply a Constant value into the
901 // specified memory location...
903 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
904 DEBUG(errs() << "JIT: Initializing " << Addr << " ");
906 if (isa<UndefValue>(Init)) {
908 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(Init)) {
909 unsigned ElementSize =
910 getTargetData()->getTypeAllocSize(CP->getType()->getElementType());
911 for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i)
912 InitializeMemory(CP->getOperand(i), (char*)Addr+i*ElementSize);
914 } else if (isa<ConstantAggregateZero>(Init)) {
915 memset(Addr, 0, (size_t)getTargetData()->getTypeAllocSize(Init->getType()));
917 } else if (const ConstantArray *CPA = dyn_cast<ConstantArray>(Init)) {
918 unsigned ElementSize =
919 getTargetData()->getTypeAllocSize(CPA->getType()->getElementType());
920 for (unsigned i = 0, e = CPA->getNumOperands(); i != e; ++i)
921 InitializeMemory(CPA->getOperand(i), (char*)Addr+i*ElementSize);
923 } else if (const ConstantStruct *CPS = dyn_cast<ConstantStruct>(Init)) {
924 const StructLayout *SL =
925 getTargetData()->getStructLayout(cast<StructType>(CPS->getType()));
926 for (unsigned i = 0, e = CPS->getNumOperands(); i != e; ++i)
927 InitializeMemory(CPS->getOperand(i), (char*)Addr+SL->getElementOffset(i));
929 } else if (Init->getType()->isFirstClassType()) {
930 GenericValue Val = getConstantValue(Init);
931 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
935 errs() << "Bad Type: " << *Init->getType() << "\n";
936 llvm_unreachable("Unknown constant type to initialize memory with!");
939 /// EmitGlobals - Emit all of the global variables to memory, storing their
940 /// addresses into GlobalAddress. This must make sure to copy the contents of
941 /// their initializers into the memory.
943 void ExecutionEngine::emitGlobals() {
945 // Loop over all of the global variables in the program, allocating the memory
946 // to hold them. If there is more than one module, do a prepass over globals
947 // to figure out how the different modules should link together.
949 std::map<std::pair<std::string, const Type*>,
950 const GlobalValue*> LinkedGlobalsMap;
952 if (Modules.size() != 1) {
953 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
954 Module &M = *Modules[m]->getModule();
955 for (Module::const_global_iterator I = M.global_begin(),
956 E = M.global_end(); I != E; ++I) {
957 const GlobalValue *GV = I;
958 if (GV->hasLocalLinkage() || GV->isDeclaration() ||
959 GV->hasAppendingLinkage() || !GV->hasName())
960 continue;// Ignore external globals and globals with internal linkage.
962 const GlobalValue *&GVEntry =
963 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
965 // If this is the first time we've seen this global, it is the canonical
972 // If the existing global is strong, never replace it.
973 if (GVEntry->hasExternalLinkage() ||
974 GVEntry->hasDLLImportLinkage() ||
975 GVEntry->hasDLLExportLinkage())
978 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
979 // symbol. FIXME is this right for common?
980 if (GV->hasExternalLinkage() || GVEntry->hasExternalWeakLinkage())
986 std::vector<const GlobalValue*> NonCanonicalGlobals;
987 for (unsigned m = 0, e = Modules.size(); m != e; ++m) {
988 Module &M = *Modules[m]->getModule();
989 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
991 // In the multi-module case, see what this global maps to.
992 if (!LinkedGlobalsMap.empty()) {
993 if (const GlobalValue *GVEntry =
994 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())]) {
995 // If something else is the canonical global, ignore this one.
996 if (GVEntry != &*I) {
997 NonCanonicalGlobals.push_back(I);
1003 if (!I->isDeclaration()) {
1004 addGlobalMapping(I, getMemoryForGV(I));
1006 // External variable reference. Try to use the dynamic loader to
1007 // get a pointer to it.
1009 sys::DynamicLibrary::SearchForAddressOfSymbol(I->getName()))
1010 addGlobalMapping(I, SymAddr);
1012 llvm_report_error("Could not resolve external global address: "
1018 // If there are multiple modules, map the non-canonical globals to their
1019 // canonical location.
1020 if (!NonCanonicalGlobals.empty()) {
1021 for (unsigned i = 0, e = NonCanonicalGlobals.size(); i != e; ++i) {
1022 const GlobalValue *GV = NonCanonicalGlobals[i];
1023 const GlobalValue *CGV =
1024 LinkedGlobalsMap[std::make_pair(GV->getName(), GV->getType())];
1025 void *Ptr = getPointerToGlobalIfAvailable(CGV);
1026 assert(Ptr && "Canonical global wasn't codegen'd!");
1027 addGlobalMapping(GV, Ptr);
1031 // Now that all of the globals are set up in memory, loop through them all
1032 // and initialize their contents.
1033 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
1035 if (!I->isDeclaration()) {
1036 if (!LinkedGlobalsMap.empty()) {
1037 if (const GlobalValue *GVEntry =
1038 LinkedGlobalsMap[std::make_pair(I->getName(), I->getType())])
1039 if (GVEntry != &*I) // Not the canonical variable.
1042 EmitGlobalVariable(I);
1048 // EmitGlobalVariable - This method emits the specified global variable to the
1049 // address specified in GlobalAddresses, or allocates new memory if it's not
1050 // already in the map.
1051 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable *GV) {
1052 void *GA = getPointerToGlobalIfAvailable(GV);
1055 // If it's not already specified, allocate memory for the global.
1056 GA = getMemoryForGV(GV);
1057 addGlobalMapping(GV, GA);
1060 // Don't initialize if it's thread local, let the client do it.
1061 if (!GV->isThreadLocal())
1062 InitializeMemory(GV->getInitializer(), GA);
1064 const Type *ElTy = GV->getType()->getElementType();
1065 size_t GVSize = (size_t)getTargetData()->getTypeAllocSize(ElTy);
1066 NumInitBytes += (unsigned)GVSize;