]>
git.proxmox.com Git - rustc.git/blob - src/llvm/lib/ExecutionEngine/ExecutionEngine.cpp
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"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/ExecutionEngine/GenericValue.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/DerivedTypes.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/DynamicLibrary.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/Host.h"
29 #include "llvm/Support/MutexGuard.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/ValueHandle.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Target/TargetMachine.h"
38 STATISTIC(NumInitBytes
, "Number of bytes of global vars initialized");
39 STATISTIC(NumGlobals
, "Number of global vars initialized");
41 ExecutionEngine
*(*ExecutionEngine::JITCtor
)(
43 std::string
*ErrorStr
,
44 JITMemoryManager
*JMM
,
46 TargetMachine
*TM
) = 0;
47 ExecutionEngine
*(*ExecutionEngine::MCJITCtor
)(
49 std::string
*ErrorStr
,
50 JITMemoryManager
*JMM
,
52 TargetMachine
*TM
) = 0;
53 ExecutionEngine
*(*ExecutionEngine::InterpCtor
)(Module
*M
,
54 std::string
*ErrorStr
) = 0;
56 ExecutionEngine::ExecutionEngine(Module
*M
)
58 LazyFunctionCreator(0),
59 ExceptionTableRegister(0),
60 ExceptionTableDeregister(0) {
61 CompilingLazily
= false;
62 GVCompilationDisabled
= false;
63 SymbolSearchingDisabled
= false;
65 assert(M
&& "Module is null?");
68 ExecutionEngine::~ExecutionEngine() {
69 clearAllGlobalMappings();
70 for (unsigned i
= 0, e
= Modules
.size(); i
!= e
; ++i
)
74 void ExecutionEngine::DeregisterAllTables() {
75 if (ExceptionTableDeregister
) {
76 DenseMap
<const Function
*, void*>::iterator it
= AllExceptionTables
.begin();
77 DenseMap
<const Function
*, void*>::iterator ite
= AllExceptionTables
.end();
78 for (; it
!= ite
; ++it
)
79 ExceptionTableDeregister(it
->second
);
80 AllExceptionTables
.clear();
85 /// \brief Helper class which uses a value handler to automatically deletes the
86 /// memory block when the GlobalVariable is destroyed.
87 class GVMemoryBlock
: public CallbackVH
{
88 GVMemoryBlock(const GlobalVariable
*GV
)
89 : CallbackVH(const_cast<GlobalVariable
*>(GV
)) {}
92 /// \brief Returns the address the GlobalVariable should be written into. The
93 /// GVMemoryBlock object prefixes that.
94 static char *Create(const GlobalVariable
*GV
, const DataLayout
& TD
) {
95 Type
*ElTy
= GV
->getType()->getElementType();
96 size_t GVSize
= (size_t)TD
.getTypeAllocSize(ElTy
);
97 void *RawMemory
= ::operator new(
98 DataLayout::RoundUpAlignment(sizeof(GVMemoryBlock
),
99 TD
.getPreferredAlignment(GV
))
101 new(RawMemory
) GVMemoryBlock(GV
);
102 return static_cast<char*>(RawMemory
) + sizeof(GVMemoryBlock
);
105 virtual void deleted() {
106 // We allocated with operator new and with some extra memory hanging off the
107 // end, so don't just delete this. I'm not sure if this is actually
109 this->~GVMemoryBlock();
110 ::operator delete(this);
113 } // anonymous namespace
115 char *ExecutionEngine::getMemoryForGV(const GlobalVariable
*GV
) {
116 return GVMemoryBlock::Create(GV
, *getDataLayout());
119 bool ExecutionEngine::removeModule(Module
*M
) {
120 for(SmallVector
<Module
*, 1>::iterator I
= Modules
.begin(),
121 E
= Modules
.end(); I
!= E
; ++I
) {
125 clearGlobalMappingsFromModule(M
);
132 Function
*ExecutionEngine::FindFunctionNamed(const char *FnName
) {
133 for (unsigned i
= 0, e
= Modules
.size(); i
!= e
; ++i
) {
134 if (Function
*F
= Modules
[i
]->getFunction(FnName
))
141 void *ExecutionEngineState::RemoveMapping(const MutexGuard
&,
142 const GlobalValue
*ToUnmap
) {
143 GlobalAddressMapTy::iterator I
= GlobalAddressMap
.find(ToUnmap
);
146 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
148 if (I
== GlobalAddressMap
.end())
152 GlobalAddressMap
.erase(I
);
155 GlobalAddressReverseMap
.erase(OldVal
);
159 void ExecutionEngine::addGlobalMapping(const GlobalValue
*GV
, void *Addr
) {
160 MutexGuard
locked(lock
);
162 DEBUG(dbgs() << "JIT: Map \'" << GV
->getName()
163 << "\' to [" << Addr
<< "]\n";);
164 void *&CurVal
= EEState
.getGlobalAddressMap(locked
)[GV
];
165 assert((CurVal
== 0 || Addr
== 0) && "GlobalMapping already established!");
168 // If we are using the reverse mapping, add it too.
169 if (!EEState
.getGlobalAddressReverseMap(locked
).empty()) {
170 AssertingVH
<const GlobalValue
> &V
=
171 EEState
.getGlobalAddressReverseMap(locked
)[Addr
];
172 assert((V
== 0 || GV
== 0) && "GlobalMapping already established!");
177 void ExecutionEngine::clearAllGlobalMappings() {
178 MutexGuard
locked(lock
);
180 EEState
.getGlobalAddressMap(locked
).clear();
181 EEState
.getGlobalAddressReverseMap(locked
).clear();
184 void ExecutionEngine::clearGlobalMappingsFromModule(Module
*M
) {
185 MutexGuard
locked(lock
);
187 for (Module::iterator FI
= M
->begin(), FE
= M
->end(); FI
!= FE
; ++FI
)
188 EEState
.RemoveMapping(locked
, FI
);
189 for (Module::global_iterator GI
= M
->global_begin(), GE
= M
->global_end();
191 EEState
.RemoveMapping(locked
, GI
);
194 void *ExecutionEngine::updateGlobalMapping(const GlobalValue
*GV
, void *Addr
) {
195 MutexGuard
locked(lock
);
197 ExecutionEngineState::GlobalAddressMapTy
&Map
=
198 EEState
.getGlobalAddressMap(locked
);
200 // Deleting from the mapping?
202 return EEState
.RemoveMapping(locked
, GV
);
204 void *&CurVal
= Map
[GV
];
205 void *OldVal
= CurVal
;
207 if (CurVal
&& !EEState
.getGlobalAddressReverseMap(locked
).empty())
208 EEState
.getGlobalAddressReverseMap(locked
).erase(CurVal
);
211 // If we are using the reverse mapping, add it too.
212 if (!EEState
.getGlobalAddressReverseMap(locked
).empty()) {
213 AssertingVH
<const GlobalValue
> &V
=
214 EEState
.getGlobalAddressReverseMap(locked
)[Addr
];
215 assert((V
== 0 || GV
== 0) && "GlobalMapping already established!");
221 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue
*GV
) {
222 MutexGuard
locked(lock
);
224 ExecutionEngineState::GlobalAddressMapTy::iterator I
=
225 EEState
.getGlobalAddressMap(locked
).find(GV
);
226 return I
!= EEState
.getGlobalAddressMap(locked
).end() ? I
->second
: 0;
229 const GlobalValue
*ExecutionEngine::getGlobalValueAtAddress(void *Addr
) {
230 MutexGuard
locked(lock
);
232 // If we haven't computed the reverse mapping yet, do so first.
233 if (EEState
.getGlobalAddressReverseMap(locked
).empty()) {
234 for (ExecutionEngineState::GlobalAddressMapTy::iterator
235 I
= EEState
.getGlobalAddressMap(locked
).begin(),
236 E
= EEState
.getGlobalAddressMap(locked
).end(); I
!= E
; ++I
)
237 EEState
.getGlobalAddressReverseMap(locked
).insert(std::make_pair(
238 I
->second
, I
->first
));
241 std::map
<void *, AssertingVH
<const GlobalValue
> >::iterator I
=
242 EEState
.getGlobalAddressReverseMap(locked
).find(Addr
);
243 return I
!= EEState
.getGlobalAddressReverseMap(locked
).end() ? I
->second
: 0;
249 std::vector
<char*> Values
;
251 ArgvArray() : Array(NULL
) {}
252 ~ArgvArray() { clear(); }
256 for (size_t I
= 0, E
= Values
.size(); I
!= E
; ++I
) {
261 /// Turn a vector of strings into a nice argv style array of pointers to null
262 /// terminated strings.
263 void *reset(LLVMContext
&C
, ExecutionEngine
*EE
,
264 const std::vector
<std::string
> &InputArgv
);
266 } // anonymous namespace
267 void *ArgvArray::reset(LLVMContext
&C
, ExecutionEngine
*EE
,
268 const std::vector
<std::string
> &InputArgv
) {
269 clear(); // Free the old contents.
270 unsigned PtrSize
= EE
->getDataLayout()->getPointerSize();
271 Array
= new char[(InputArgv
.size()+1)*PtrSize
];
273 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array
<< "\n");
274 Type
*SBytePtr
= Type::getInt8PtrTy(C
);
276 for (unsigned i
= 0; i
!= InputArgv
.size(); ++i
) {
277 unsigned Size
= InputArgv
[i
].size()+1;
278 char *Dest
= new char[Size
];
279 Values
.push_back(Dest
);
280 DEBUG(dbgs() << "JIT: ARGV[" << i
<< "] = " << (void*)Dest
<< "\n");
282 std::copy(InputArgv
[i
].begin(), InputArgv
[i
].end(), Dest
);
285 // Endian safe: Array[i] = (PointerTy)Dest;
286 EE
->StoreValueToMemory(PTOGV(Dest
), (GenericValue
*)(Array
+i
*PtrSize
),
291 EE
->StoreValueToMemory(PTOGV(0),
292 (GenericValue
*)(Array
+InputArgv
.size()*PtrSize
),
297 void ExecutionEngine::runStaticConstructorsDestructors(Module
*module
,
299 const char *Name
= isDtors
? "llvm.global_dtors" : "llvm.global_ctors";
300 GlobalVariable
*GV
= module
->getNamedGlobal(Name
);
302 // If this global has internal linkage, or if it has a use, then it must be
303 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
304 // this is the case, don't execute any of the global ctors, __main will do
306 if (!GV
|| GV
->isDeclaration() || GV
->hasLocalLinkage()) return;
308 // Should be an array of '{ i32, void ()* }' structs. The first value is
309 // the init priority, which we ignore.
310 ConstantArray
*InitList
= dyn_cast
<ConstantArray
>(GV
->getInitializer());
313 for (unsigned i
= 0, e
= InitList
->getNumOperands(); i
!= e
; ++i
) {
314 ConstantStruct
*CS
= dyn_cast
<ConstantStruct
>(InitList
->getOperand(i
));
315 if (CS
== 0) continue;
317 Constant
*FP
= CS
->getOperand(1);
318 if (FP
->isNullValue())
319 continue; // Found a sentinal value, ignore.
321 // Strip off constant expression casts.
322 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(FP
))
324 FP
= CE
->getOperand(0);
326 // Execute the ctor/dtor function!
327 if (Function
*F
= dyn_cast
<Function
>(FP
))
328 runFunction(F
, std::vector
<GenericValue
>());
330 // FIXME: It is marginally lame that we just do nothing here if we see an
331 // entry we don't recognize. It might not be unreasonable for the verifier
332 // to not even allow this and just assert here.
336 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors
) {
337 // Execute global ctors/dtors for each module in the program.
338 for (unsigned i
= 0, e
= Modules
.size(); i
!= e
; ++i
)
339 runStaticConstructorsDestructors(Modules
[i
], isDtors
);
343 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
344 static bool isTargetNullPtr(ExecutionEngine
*EE
, void *Loc
) {
345 unsigned PtrSize
= EE
->getDataLayout()->getPointerSize();
346 for (unsigned i
= 0; i
< PtrSize
; ++i
)
347 if (*(i
+ (uint8_t*)Loc
))
353 int ExecutionEngine::runFunctionAsMain(Function
*Fn
,
354 const std::vector
<std::string
> &argv
,
355 const char * const * envp
) {
356 std::vector
<GenericValue
> GVArgs
;
358 GVArgc
.IntVal
= APInt(32, argv
.size());
361 unsigned NumArgs
= Fn
->getFunctionType()->getNumParams();
362 FunctionType
*FTy
= Fn
->getFunctionType();
363 Type
* PPInt8Ty
= Type::getInt8PtrTy(Fn
->getContext())->getPointerTo();
365 // Check the argument types.
367 report_fatal_error("Invalid number of arguments of main() supplied");
368 if (NumArgs
>= 3 && FTy
->getParamType(2) != PPInt8Ty
)
369 report_fatal_error("Invalid type for third argument of main() supplied");
370 if (NumArgs
>= 2 && FTy
->getParamType(1) != PPInt8Ty
)
371 report_fatal_error("Invalid type for second argument of main() supplied");
372 if (NumArgs
>= 1 && !FTy
->getParamType(0)->isIntegerTy(32))
373 report_fatal_error("Invalid type for first argument of main() supplied");
374 if (!FTy
->getReturnType()->isIntegerTy() &&
375 !FTy
->getReturnType()->isVoidTy())
376 report_fatal_error("Invalid return type of main() supplied");
381 GVArgs
.push_back(GVArgc
); // Arg #0 = argc.
384 GVArgs
.push_back(PTOGV(CArgv
.reset(Fn
->getContext(), this, argv
)));
385 assert(!isTargetNullPtr(this, GVTOP(GVArgs
[1])) &&
386 "argv[0] was null after CreateArgv");
388 std::vector
<std::string
> EnvVars
;
389 for (unsigned i
= 0; envp
[i
]; ++i
)
390 EnvVars
.push_back(envp
[i
]);
392 GVArgs
.push_back(PTOGV(CEnv
.reset(Fn
->getContext(), this, EnvVars
)));
397 return runFunction(Fn
, GVArgs
).IntVal
.getZExtValue();
400 ExecutionEngine
*ExecutionEngine::create(Module
*M
,
401 bool ForceInterpreter
,
402 std::string
*ErrorStr
,
403 CodeGenOpt::Level OptLevel
,
405 EngineBuilder EB
= EngineBuilder(M
)
406 .setEngineKind(ForceInterpreter
407 ? EngineKind::Interpreter
409 .setErrorStr(ErrorStr
)
410 .setOptLevel(OptLevel
)
411 .setAllocateGVsWithCode(GVsWithCode
);
416 /// createJIT - This is the factory method for creating a JIT for the current
417 /// machine, it does not fall back to the interpreter. This takes ownership
419 ExecutionEngine
*ExecutionEngine::createJIT(Module
*M
,
420 std::string
*ErrorStr
,
421 JITMemoryManager
*JMM
,
422 CodeGenOpt::Level OL
,
425 CodeModel::Model CMM
) {
426 if (ExecutionEngine::JITCtor
== 0) {
428 *ErrorStr
= "JIT has not been linked in.";
432 // Use the defaults for extra parameters. Users can use EngineBuilder to
435 EB
.setEngineKind(EngineKind::JIT
);
436 EB
.setErrorStr(ErrorStr
);
437 EB
.setRelocationModel(RM
);
438 EB
.setCodeModel(CMM
);
439 EB
.setAllocateGVsWithCode(GVsWithCode
);
441 EB
.setJITMemoryManager(JMM
);
443 // TODO: permit custom TargetOptions here
444 TargetMachine
*TM
= EB
.selectTarget();
445 if (!TM
|| (ErrorStr
&& ErrorStr
->length() > 0)) return 0;
447 return ExecutionEngine::JITCtor(M
, ErrorStr
, JMM
, GVsWithCode
, TM
);
450 ExecutionEngine
*EngineBuilder::create(TargetMachine
*TM
) {
451 OwningPtr
<TargetMachine
> TheTM(TM
); // Take ownership.
453 // Make sure we can resolve symbols in the program as well. The zero arg
454 // to the function tells DynamicLibrary to load the program, not a library.
455 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr
))
458 // If the user specified a memory manager but didn't specify which engine to
459 // create, we assume they only want the JIT, and we fail if they only want
462 if (WhichEngine
& EngineKind::JIT
)
463 WhichEngine
= EngineKind::JIT
;
466 *ErrorStr
= "Cannot create an interpreter with a memory manager.";
471 // Unless the interpreter was explicitly selected or the JIT is not linked,
473 if ((WhichEngine
& EngineKind::JIT
) && TheTM
) {
474 Triple
TT(M
->getTargetTriple());
475 if (!TM
->getTarget().hasJIT()) {
476 errs() << "WARNING: This target JIT is not designed for the host"
477 << " you are running. If bad things happen, please choose"
478 << " a different -march switch.\n";
481 if (UseMCJIT
&& ExecutionEngine::MCJITCtor
) {
482 ExecutionEngine
*EE
=
483 ExecutionEngine::MCJITCtor(M
, ErrorStr
, JMM
,
484 AllocateGVsWithCode
, TheTM
.take());
486 } else if (ExecutionEngine::JITCtor
) {
487 ExecutionEngine
*EE
=
488 ExecutionEngine::JITCtor(M
, ErrorStr
, JMM
,
489 AllocateGVsWithCode
, TheTM
.take());
494 // If we can't make a JIT and we didn't request one specifically, try making
495 // an interpreter instead.
496 if (WhichEngine
& EngineKind::Interpreter
) {
497 if (ExecutionEngine::InterpCtor
)
498 return ExecutionEngine::InterpCtor(M
, ErrorStr
);
500 *ErrorStr
= "Interpreter has not been linked in.";
504 if ((WhichEngine
& EngineKind::JIT
) && ExecutionEngine::JITCtor
== 0 &&
505 ExecutionEngine::MCJITCtor
== 0) {
507 *ErrorStr
= "JIT has not been linked in.";
513 void *ExecutionEngine::getPointerToGlobal(const GlobalValue
*GV
) {
514 if (Function
*F
= const_cast<Function
*>(dyn_cast
<Function
>(GV
)))
515 return getPointerToFunction(F
);
517 MutexGuard
locked(lock
);
518 if (void *P
= EEState
.getGlobalAddressMap(locked
)[GV
])
521 // Global variable might have been added since interpreter started.
522 if (GlobalVariable
*GVar
=
523 const_cast<GlobalVariable
*>(dyn_cast
<GlobalVariable
>(GV
)))
524 EmitGlobalVariable(GVar
);
526 llvm_unreachable("Global hasn't had an address allocated yet!");
528 return EEState
.getGlobalAddressMap(locked
)[GV
];
531 /// \brief Converts a Constant* into a GenericValue, including handling of
532 /// ConstantExpr values.
533 GenericValue
ExecutionEngine::getConstantValue(const Constant
*C
) {
534 // If its undefined, return the garbage.
535 if (isa
<UndefValue
>(C
)) {
537 switch (C
->getType()->getTypeID()) {
538 case Type::IntegerTyID
:
539 case Type::X86_FP80TyID
:
540 case Type::FP128TyID
:
541 case Type::PPC_FP128TyID
:
542 // Although the value is undefined, we still have to construct an APInt
543 // with the correct bit width.
544 Result
.IntVal
= APInt(C
->getType()->getPrimitiveSizeInBits(), 0);
552 // Otherwise, if the value is a ConstantExpr...
553 if (const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
)) {
554 Constant
*Op0
= CE
->getOperand(0);
555 switch (CE
->getOpcode()) {
556 case Instruction::GetElementPtr
: {
558 GenericValue Result
= getConstantValue(Op0
);
559 APInt
Offset(TD
->getPointerSizeInBits(), 0);
560 cast
<GEPOperator
>(CE
)->accumulateConstantOffset(*TD
, Offset
);
562 char* tmp
= (char*) Result
.PointerVal
;
563 Result
= PTOGV(tmp
+ Offset
.getSExtValue());
566 case Instruction::Trunc
: {
567 GenericValue GV
= getConstantValue(Op0
);
568 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
569 GV
.IntVal
= GV
.IntVal
.trunc(BitWidth
);
572 case Instruction::ZExt
: {
573 GenericValue GV
= getConstantValue(Op0
);
574 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
575 GV
.IntVal
= GV
.IntVal
.zext(BitWidth
);
578 case Instruction::SExt
: {
579 GenericValue GV
= getConstantValue(Op0
);
580 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
581 GV
.IntVal
= GV
.IntVal
.sext(BitWidth
);
584 case Instruction::FPTrunc
: {
586 GenericValue GV
= getConstantValue(Op0
);
587 GV
.FloatVal
= float(GV
.DoubleVal
);
590 case Instruction::FPExt
:{
592 GenericValue GV
= getConstantValue(Op0
);
593 GV
.DoubleVal
= double(GV
.FloatVal
);
596 case Instruction::UIToFP
: {
597 GenericValue GV
= getConstantValue(Op0
);
598 if (CE
->getType()->isFloatTy())
599 GV
.FloatVal
= float(GV
.IntVal
.roundToDouble());
600 else if (CE
->getType()->isDoubleTy())
601 GV
.DoubleVal
= GV
.IntVal
.roundToDouble();
602 else if (CE
->getType()->isX86_FP80Ty()) {
603 APFloat apf
= APFloat::getZero(APFloat::x87DoubleExtended
);
604 (void)apf
.convertFromAPInt(GV
.IntVal
,
606 APFloat::rmNearestTiesToEven
);
607 GV
.IntVal
= apf
.bitcastToAPInt();
611 case Instruction::SIToFP
: {
612 GenericValue GV
= getConstantValue(Op0
);
613 if (CE
->getType()->isFloatTy())
614 GV
.FloatVal
= float(GV
.IntVal
.signedRoundToDouble());
615 else if (CE
->getType()->isDoubleTy())
616 GV
.DoubleVal
= GV
.IntVal
.signedRoundToDouble();
617 else if (CE
->getType()->isX86_FP80Ty()) {
618 APFloat apf
= APFloat::getZero(APFloat::x87DoubleExtended
);
619 (void)apf
.convertFromAPInt(GV
.IntVal
,
621 APFloat::rmNearestTiesToEven
);
622 GV
.IntVal
= apf
.bitcastToAPInt();
626 case Instruction::FPToUI
: // double->APInt conversion handles sign
627 case Instruction::FPToSI
: {
628 GenericValue GV
= getConstantValue(Op0
);
629 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
630 if (Op0
->getType()->isFloatTy())
631 GV
.IntVal
= APIntOps::RoundFloatToAPInt(GV
.FloatVal
, BitWidth
);
632 else if (Op0
->getType()->isDoubleTy())
633 GV
.IntVal
= APIntOps::RoundDoubleToAPInt(GV
.DoubleVal
, BitWidth
);
634 else if (Op0
->getType()->isX86_FP80Ty()) {
635 APFloat apf
= APFloat(APFloat::x87DoubleExtended
, GV
.IntVal
);
638 (void)apf
.convertToInteger(&v
, BitWidth
,
639 CE
->getOpcode()==Instruction::FPToSI
,
640 APFloat::rmTowardZero
, &ignored
);
641 GV
.IntVal
= v
; // endian?
645 case Instruction::PtrToInt
: {
646 GenericValue GV
= getConstantValue(Op0
);
647 uint32_t PtrWidth
= TD
->getTypeSizeInBits(Op0
->getType());
648 assert(PtrWidth
<= 64 && "Bad pointer width");
649 GV
.IntVal
= APInt(PtrWidth
, uintptr_t(GV
.PointerVal
));
650 uint32_t IntWidth
= TD
->getTypeSizeInBits(CE
->getType());
651 GV
.IntVal
= GV
.IntVal
.zextOrTrunc(IntWidth
);
654 case Instruction::IntToPtr
: {
655 GenericValue GV
= getConstantValue(Op0
);
656 uint32_t PtrWidth
= TD
->getTypeSizeInBits(CE
->getType());
657 GV
.IntVal
= GV
.IntVal
.zextOrTrunc(PtrWidth
);
658 assert(GV
.IntVal
.getBitWidth() <= 64 && "Bad pointer width");
659 GV
.PointerVal
= PointerTy(uintptr_t(GV
.IntVal
.getZExtValue()));
662 case Instruction::BitCast
: {
663 GenericValue GV
= getConstantValue(Op0
);
664 Type
* DestTy
= CE
->getType();
665 switch (Op0
->getType()->getTypeID()) {
666 default: llvm_unreachable("Invalid bitcast operand");
667 case Type::IntegerTyID
:
668 assert(DestTy
->isFloatingPointTy() && "invalid bitcast");
669 if (DestTy
->isFloatTy())
670 GV
.FloatVal
= GV
.IntVal
.bitsToFloat();
671 else if (DestTy
->isDoubleTy())
672 GV
.DoubleVal
= GV
.IntVal
.bitsToDouble();
674 case Type::FloatTyID
:
675 assert(DestTy
->isIntegerTy(32) && "Invalid bitcast");
676 GV
.IntVal
= APInt::floatToBits(GV
.FloatVal
);
678 case Type::DoubleTyID
:
679 assert(DestTy
->isIntegerTy(64) && "Invalid bitcast");
680 GV
.IntVal
= APInt::doubleToBits(GV
.DoubleVal
);
682 case Type::PointerTyID
:
683 assert(DestTy
->isPointerTy() && "Invalid bitcast");
684 break; // getConstantValue(Op0) above already converted it
688 case Instruction::Add
:
689 case Instruction::FAdd
:
690 case Instruction::Sub
:
691 case Instruction::FSub
:
692 case Instruction::Mul
:
693 case Instruction::FMul
:
694 case Instruction::UDiv
:
695 case Instruction::SDiv
:
696 case Instruction::URem
:
697 case Instruction::SRem
:
698 case Instruction::And
:
699 case Instruction::Or
:
700 case Instruction::Xor
: {
701 GenericValue LHS
= getConstantValue(Op0
);
702 GenericValue RHS
= getConstantValue(CE
->getOperand(1));
704 switch (CE
->getOperand(0)->getType()->getTypeID()) {
705 default: llvm_unreachable("Bad add type!");
706 case Type::IntegerTyID
:
707 switch (CE
->getOpcode()) {
708 default: llvm_unreachable("Invalid integer opcode");
709 case Instruction::Add
: GV
.IntVal
= LHS
.IntVal
+ RHS
.IntVal
; break;
710 case Instruction::Sub
: GV
.IntVal
= LHS
.IntVal
- RHS
.IntVal
; break;
711 case Instruction::Mul
: GV
.IntVal
= LHS
.IntVal
* RHS
.IntVal
; break;
712 case Instruction::UDiv
:GV
.IntVal
= LHS
.IntVal
.udiv(RHS
.IntVal
); break;
713 case Instruction::SDiv
:GV
.IntVal
= LHS
.IntVal
.sdiv(RHS
.IntVal
); break;
714 case Instruction::URem
:GV
.IntVal
= LHS
.IntVal
.urem(RHS
.IntVal
); break;
715 case Instruction::SRem
:GV
.IntVal
= LHS
.IntVal
.srem(RHS
.IntVal
); break;
716 case Instruction::And
: GV
.IntVal
= LHS
.IntVal
& RHS
.IntVal
; break;
717 case Instruction::Or
: GV
.IntVal
= LHS
.IntVal
| RHS
.IntVal
; break;
718 case Instruction::Xor
: GV
.IntVal
= LHS
.IntVal
^ RHS
.IntVal
; break;
721 case Type::FloatTyID
:
722 switch (CE
->getOpcode()) {
723 default: llvm_unreachable("Invalid float opcode");
724 case Instruction::FAdd
:
725 GV
.FloatVal
= LHS
.FloatVal
+ RHS
.FloatVal
; break;
726 case Instruction::FSub
:
727 GV
.FloatVal
= LHS
.FloatVal
- RHS
.FloatVal
; break;
728 case Instruction::FMul
:
729 GV
.FloatVal
= LHS
.FloatVal
* RHS
.FloatVal
; break;
730 case Instruction::FDiv
:
731 GV
.FloatVal
= LHS
.FloatVal
/ RHS
.FloatVal
; break;
732 case Instruction::FRem
:
733 GV
.FloatVal
= std::fmod(LHS
.FloatVal
,RHS
.FloatVal
); break;
736 case Type::DoubleTyID
:
737 switch (CE
->getOpcode()) {
738 default: llvm_unreachable("Invalid double opcode");
739 case Instruction::FAdd
:
740 GV
.DoubleVal
= LHS
.DoubleVal
+ RHS
.DoubleVal
; break;
741 case Instruction::FSub
:
742 GV
.DoubleVal
= LHS
.DoubleVal
- RHS
.DoubleVal
; break;
743 case Instruction::FMul
:
744 GV
.DoubleVal
= LHS
.DoubleVal
* RHS
.DoubleVal
; break;
745 case Instruction::FDiv
:
746 GV
.DoubleVal
= LHS
.DoubleVal
/ RHS
.DoubleVal
; break;
747 case Instruction::FRem
:
748 GV
.DoubleVal
= std::fmod(LHS
.DoubleVal
,RHS
.DoubleVal
); break;
751 case Type::X86_FP80TyID
:
752 case Type::PPC_FP128TyID
:
753 case Type::FP128TyID
: {
754 const fltSemantics
&Sem
= CE
->getOperand(0)->getType()->getFltSemantics();
755 APFloat apfLHS
= APFloat(Sem
, LHS
.IntVal
);
756 switch (CE
->getOpcode()) {
757 default: llvm_unreachable("Invalid long double opcode");
758 case Instruction::FAdd
:
759 apfLHS
.add(APFloat(Sem
, RHS
.IntVal
), APFloat::rmNearestTiesToEven
);
760 GV
.IntVal
= apfLHS
.bitcastToAPInt();
762 case Instruction::FSub
:
763 apfLHS
.subtract(APFloat(Sem
, RHS
.IntVal
),
764 APFloat::rmNearestTiesToEven
);
765 GV
.IntVal
= apfLHS
.bitcastToAPInt();
767 case Instruction::FMul
:
768 apfLHS
.multiply(APFloat(Sem
, RHS
.IntVal
),
769 APFloat::rmNearestTiesToEven
);
770 GV
.IntVal
= apfLHS
.bitcastToAPInt();
772 case Instruction::FDiv
:
773 apfLHS
.divide(APFloat(Sem
, RHS
.IntVal
),
774 APFloat::rmNearestTiesToEven
);
775 GV
.IntVal
= apfLHS
.bitcastToAPInt();
777 case Instruction::FRem
:
778 apfLHS
.mod(APFloat(Sem
, RHS
.IntVal
),
779 APFloat::rmNearestTiesToEven
);
780 GV
.IntVal
= apfLHS
.bitcastToAPInt();
792 SmallString
<256> Msg
;
793 raw_svector_ostream
OS(Msg
);
794 OS
<< "ConstantExpr not handled: " << *CE
;
795 report_fatal_error(OS
.str());
798 // Otherwise, we have a simple constant.
800 switch (C
->getType()->getTypeID()) {
801 case Type::FloatTyID
:
802 Result
.FloatVal
= cast
<ConstantFP
>(C
)->getValueAPF().convertToFloat();
804 case Type::DoubleTyID
:
805 Result
.DoubleVal
= cast
<ConstantFP
>(C
)->getValueAPF().convertToDouble();
807 case Type::X86_FP80TyID
:
808 case Type::FP128TyID
:
809 case Type::PPC_FP128TyID
:
810 Result
.IntVal
= cast
<ConstantFP
>(C
)->getValueAPF().bitcastToAPInt();
812 case Type::IntegerTyID
:
813 Result
.IntVal
= cast
<ConstantInt
>(C
)->getValue();
815 case Type::PointerTyID
:
816 if (isa
<ConstantPointerNull
>(C
))
817 Result
.PointerVal
= 0;
818 else if (const Function
*F
= dyn_cast
<Function
>(C
))
819 Result
= PTOGV(getPointerToFunctionOrStub(const_cast<Function
*>(F
)));
820 else if (const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(C
))
821 Result
= PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable
*>(GV
)));
822 else if (const BlockAddress
*BA
= dyn_cast
<BlockAddress
>(C
))
823 Result
= PTOGV(getPointerToBasicBlock(const_cast<BasicBlock
*>(
824 BA
->getBasicBlock())));
826 llvm_unreachable("Unknown constant pointer type!");
829 SmallString
<256> Msg
;
830 raw_svector_ostream
OS(Msg
);
831 OS
<< "ERROR: Constant unimplemented for type: " << *C
->getType();
832 report_fatal_error(OS
.str());
838 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
839 /// with the integer held in IntVal.
840 static void StoreIntToMemory(const APInt
&IntVal
, uint8_t *Dst
,
841 unsigned StoreBytes
) {
842 assert((IntVal
.getBitWidth()+7)/8 >= StoreBytes
&& "Integer too small!");
843 const uint8_t *Src
= (const uint8_t *)IntVal
.getRawData();
845 if (sys::isLittleEndianHost()) {
846 // Little-endian host - the source is ordered from LSB to MSB. Order the
847 // destination from LSB to MSB: Do a straight copy.
848 memcpy(Dst
, Src
, StoreBytes
);
850 // Big-endian host - the source is an array of 64 bit words ordered from
851 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
852 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
853 while (StoreBytes
> sizeof(uint64_t)) {
854 StoreBytes
-= sizeof(uint64_t);
855 // May not be aligned so use memcpy.
856 memcpy(Dst
+ StoreBytes
, Src
, sizeof(uint64_t));
857 Src
+= sizeof(uint64_t);
860 memcpy(Dst
, Src
+ sizeof(uint64_t) - StoreBytes
, StoreBytes
);
864 void ExecutionEngine::StoreValueToMemory(const GenericValue
&Val
,
865 GenericValue
*Ptr
, Type
*Ty
) {
866 const unsigned StoreBytes
= getDataLayout()->getTypeStoreSize(Ty
);
868 switch (Ty
->getTypeID()) {
869 case Type::IntegerTyID
:
870 StoreIntToMemory(Val
.IntVal
, (uint8_t*)Ptr
, StoreBytes
);
872 case Type::FloatTyID
:
873 *((float*)Ptr
) = Val
.FloatVal
;
875 case Type::DoubleTyID
:
876 *((double*)Ptr
) = Val
.DoubleVal
;
878 case Type::X86_FP80TyID
:
879 memcpy(Ptr
, Val
.IntVal
.getRawData(), 10);
881 case Type::PointerTyID
:
882 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
883 if (StoreBytes
!= sizeof(PointerTy
))
884 memset(&(Ptr
->PointerVal
), 0, StoreBytes
);
886 *((PointerTy
*)Ptr
) = Val
.PointerVal
;
889 dbgs() << "Cannot store value of type " << *Ty
<< "!\n";
892 if (sys::isLittleEndianHost() != getDataLayout()->isLittleEndian())
893 // Host and target are different endian - reverse the stored bytes.
894 std::reverse((uint8_t*)Ptr
, StoreBytes
+ (uint8_t*)Ptr
);
897 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
898 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
899 static void LoadIntFromMemory(APInt
&IntVal
, uint8_t *Src
, unsigned LoadBytes
) {
900 assert((IntVal
.getBitWidth()+7)/8 >= LoadBytes
&& "Integer too small!");
901 uint8_t *Dst
= reinterpret_cast<uint8_t *>(
902 const_cast<uint64_t *>(IntVal
.getRawData()));
904 if (sys::isLittleEndianHost())
905 // Little-endian host - the destination must be ordered from LSB to MSB.
906 // The source is ordered from LSB to MSB: Do a straight copy.
907 memcpy(Dst
, Src
, LoadBytes
);
909 // Big-endian - the destination is an array of 64 bit words ordered from
910 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
911 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
913 while (LoadBytes
> sizeof(uint64_t)) {
914 LoadBytes
-= sizeof(uint64_t);
915 // May not be aligned so use memcpy.
916 memcpy(Dst
, Src
+ LoadBytes
, sizeof(uint64_t));
917 Dst
+= sizeof(uint64_t);
920 memcpy(Dst
+ sizeof(uint64_t) - LoadBytes
, Src
, LoadBytes
);
926 void ExecutionEngine::LoadValueFromMemory(GenericValue
&Result
,
929 const unsigned LoadBytes
= getDataLayout()->getTypeStoreSize(Ty
);
931 switch (Ty
->getTypeID()) {
932 case Type::IntegerTyID
:
933 // An APInt with all words initially zero.
934 Result
.IntVal
= APInt(cast
<IntegerType
>(Ty
)->getBitWidth(), 0);
935 LoadIntFromMemory(Result
.IntVal
, (uint8_t*)Ptr
, LoadBytes
);
937 case Type::FloatTyID
:
938 Result
.FloatVal
= *((float*)Ptr
);
940 case Type::DoubleTyID
:
941 Result
.DoubleVal
= *((double*)Ptr
);
943 case Type::PointerTyID
:
944 Result
.PointerVal
= *((PointerTy
*)Ptr
);
946 case Type::X86_FP80TyID
: {
947 // This is endian dependent, but it will only work on x86 anyway.
948 // FIXME: Will not trap if loading a signaling NaN.
951 Result
.IntVal
= APInt(80, y
);
955 SmallString
<256> Msg
;
956 raw_svector_ostream
OS(Msg
);
957 OS
<< "Cannot load value of type " << *Ty
<< "!";
958 report_fatal_error(OS
.str());
962 void ExecutionEngine::InitializeMemory(const Constant
*Init
, void *Addr
) {
963 DEBUG(dbgs() << "JIT: Initializing " << Addr
<< " ");
965 if (isa
<UndefValue
>(Init
))
968 if (const ConstantVector
*CP
= dyn_cast
<ConstantVector
>(Init
)) {
969 unsigned ElementSize
=
970 getDataLayout()->getTypeAllocSize(CP
->getType()->getElementType());
971 for (unsigned i
= 0, e
= CP
->getNumOperands(); i
!= e
; ++i
)
972 InitializeMemory(CP
->getOperand(i
), (char*)Addr
+i
*ElementSize
);
976 if (isa
<ConstantAggregateZero
>(Init
)) {
977 memset(Addr
, 0, (size_t)getDataLayout()->getTypeAllocSize(Init
->getType()));
981 if (const ConstantArray
*CPA
= dyn_cast
<ConstantArray
>(Init
)) {
982 unsigned ElementSize
=
983 getDataLayout()->getTypeAllocSize(CPA
->getType()->getElementType());
984 for (unsigned i
= 0, e
= CPA
->getNumOperands(); i
!= e
; ++i
)
985 InitializeMemory(CPA
->getOperand(i
), (char*)Addr
+i
*ElementSize
);
989 if (const ConstantStruct
*CPS
= dyn_cast
<ConstantStruct
>(Init
)) {
990 const StructLayout
*SL
=
991 getDataLayout()->getStructLayout(cast
<StructType
>(CPS
->getType()));
992 for (unsigned i
= 0, e
= CPS
->getNumOperands(); i
!= e
; ++i
)
993 InitializeMemory(CPS
->getOperand(i
), (char*)Addr
+SL
->getElementOffset(i
));
997 if (const ConstantDataSequential
*CDS
=
998 dyn_cast
<ConstantDataSequential
>(Init
)) {
999 // CDS is already laid out in host memory order.
1000 StringRef Data
= CDS
->getRawDataValues();
1001 memcpy(Addr
, Data
.data(), Data
.size());
1005 if (Init
->getType()->isFirstClassType()) {
1006 GenericValue Val
= getConstantValue(Init
);
1007 StoreValueToMemory(Val
, (GenericValue
*)Addr
, Init
->getType());
1011 DEBUG(dbgs() << "Bad Type: " << *Init
->getType() << "\n");
1012 llvm_unreachable("Unknown constant type to initialize memory with!");
1015 /// EmitGlobals - Emit all of the global variables to memory, storing their
1016 /// addresses into GlobalAddress. This must make sure to copy the contents of
1017 /// their initializers into the memory.
1018 void ExecutionEngine::emitGlobals() {
1019 // Loop over all of the global variables in the program, allocating the memory
1020 // to hold them. If there is more than one module, do a prepass over globals
1021 // to figure out how the different modules should link together.
1022 std::map
<std::pair
<std::string
, Type
*>,
1023 const GlobalValue
*> LinkedGlobalsMap
;
1025 if (Modules
.size() != 1) {
1026 for (unsigned m
= 0, e
= Modules
.size(); m
!= e
; ++m
) {
1027 Module
&M
= *Modules
[m
];
1028 for (Module::const_global_iterator I
= M
.global_begin(),
1029 E
= M
.global_end(); I
!= E
; ++I
) {
1030 const GlobalValue
*GV
= I
;
1031 if (GV
->hasLocalLinkage() || GV
->isDeclaration() ||
1032 GV
->hasAppendingLinkage() || !GV
->hasName())
1033 continue;// Ignore external globals and globals with internal linkage.
1035 const GlobalValue
*&GVEntry
=
1036 LinkedGlobalsMap
[std::make_pair(GV
->getName(), GV
->getType())];
1038 // If this is the first time we've seen this global, it is the canonical
1045 // If the existing global is strong, never replace it.
1046 if (GVEntry
->hasExternalLinkage() ||
1047 GVEntry
->hasDLLImportLinkage() ||
1048 GVEntry
->hasDLLExportLinkage())
1051 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
1052 // symbol. FIXME is this right for common?
1053 if (GV
->hasExternalLinkage() || GVEntry
->hasExternalWeakLinkage())
1059 std::vector
<const GlobalValue
*> NonCanonicalGlobals
;
1060 for (unsigned m
= 0, e
= Modules
.size(); m
!= e
; ++m
) {
1061 Module
&M
= *Modules
[m
];
1062 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
1064 // In the multi-module case, see what this global maps to.
1065 if (!LinkedGlobalsMap
.empty()) {
1066 if (const GlobalValue
*GVEntry
=
1067 LinkedGlobalsMap
[std::make_pair(I
->getName(), I
->getType())]) {
1068 // If something else is the canonical global, ignore this one.
1069 if (GVEntry
!= &*I
) {
1070 NonCanonicalGlobals
.push_back(I
);
1076 if (!I
->isDeclaration()) {
1077 addGlobalMapping(I
, getMemoryForGV(I
));
1079 // External variable reference. Try to use the dynamic loader to
1080 // get a pointer to it.
1082 sys::DynamicLibrary::SearchForAddressOfSymbol(I
->getName()))
1083 addGlobalMapping(I
, SymAddr
);
1085 report_fatal_error("Could not resolve external global address: "
1091 // If there are multiple modules, map the non-canonical globals to their
1092 // canonical location.
1093 if (!NonCanonicalGlobals
.empty()) {
1094 for (unsigned i
= 0, e
= NonCanonicalGlobals
.size(); i
!= e
; ++i
) {
1095 const GlobalValue
*GV
= NonCanonicalGlobals
[i
];
1096 const GlobalValue
*CGV
=
1097 LinkedGlobalsMap
[std::make_pair(GV
->getName(), GV
->getType())];
1098 void *Ptr
= getPointerToGlobalIfAvailable(CGV
);
1099 assert(Ptr
&& "Canonical global wasn't codegen'd!");
1100 addGlobalMapping(GV
, Ptr
);
1104 // Now that all of the globals are set up in memory, loop through them all
1105 // and initialize their contents.
1106 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
1108 if (!I
->isDeclaration()) {
1109 if (!LinkedGlobalsMap
.empty()) {
1110 if (const GlobalValue
*GVEntry
=
1111 LinkedGlobalsMap
[std::make_pair(I
->getName(), I
->getType())])
1112 if (GVEntry
!= &*I
) // Not the canonical variable.
1115 EmitGlobalVariable(I
);
1121 // EmitGlobalVariable - This method emits the specified global variable to the
1122 // address specified in GlobalAddresses, or allocates new memory if it's not
1123 // already in the map.
1124 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable
*GV
) {
1125 void *GA
= getPointerToGlobalIfAvailable(GV
);
1128 // If it's not already specified, allocate memory for the global.
1129 GA
= getMemoryForGV(GV
);
1130 addGlobalMapping(GV
, GA
);
1133 // Don't initialize if it's thread local, let the client do it.
1134 if (!GV
->isThreadLocal())
1135 InitializeMemory(GV
->getInitializer(), GA
);
1137 Type
*ElTy
= GV
->getType()->getElementType();
1138 size_t GVSize
= (size_t)getDataLayout()->getTypeAllocSize(ElTy
);
1139 NumInitBytes
+= (unsigned)GVSize
;
1143 ExecutionEngineState::ExecutionEngineState(ExecutionEngine
&EE
)
1144 : EE(EE
), GlobalAddressMap(this) {
1148 ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState
*EES
) {
1149 return &EES
->EE
.lock
;
1152 void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState
*EES
,
1153 const GlobalValue
*Old
) {
1154 void *OldVal
= EES
->GlobalAddressMap
.lookup(Old
);
1155 EES
->GlobalAddressReverseMap
.erase(OldVal
);
1158 void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState
*,
1159 const GlobalValue
*,
1160 const GlobalValue
*) {
1161 llvm_unreachable("The ExecutionEngine doesn't know how to handle a"
1162 " RAUW on a value it has a global mapping for.");