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/ExecutionEngine/GenericValue.h"
22 #include "llvm/ADT/SmallString.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/Support/DynamicLibrary.h"
30 #include "llvm/Support/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
)(
41 std::string
*ErrorStr
,
42 JITMemoryManager
*JMM
,
43 CodeGenOpt::Level OptLevel
,
48 const SmallVectorImpl
<std::string
>& MAttrs
) = 0;
49 ExecutionEngine
*(*ExecutionEngine::MCJITCtor
)(
51 std::string
*ErrorStr
,
52 JITMemoryManager
*JMM
,
53 CodeGenOpt::Level OptLevel
,
58 const SmallVectorImpl
<std::string
>& MAttrs
) = 0;
59 ExecutionEngine
*(*ExecutionEngine::InterpCtor
)(Module
*M
,
60 std::string
*ErrorStr
) = 0;
62 ExecutionEngine::ExecutionEngine(Module
*M
)
64 LazyFunctionCreator(0),
65 ExceptionTableRegister(0),
66 ExceptionTableDeregister(0) {
67 CompilingLazily
= false;
68 GVCompilationDisabled
= false;
69 SymbolSearchingDisabled
= false;
71 assert(M
&& "Module is null?");
74 ExecutionEngine::~ExecutionEngine() {
75 clearAllGlobalMappings();
76 for (unsigned i
= 0, e
= Modules
.size(); i
!= e
; ++i
)
80 void ExecutionEngine::DeregisterAllTables() {
81 if (ExceptionTableDeregister
) {
82 DenseMap
<const Function
*, void*>::iterator it
= AllExceptionTables
.begin();
83 DenseMap
<const Function
*, void*>::iterator ite
= AllExceptionTables
.end();
84 for (; it
!= ite
; ++it
)
85 ExceptionTableDeregister(it
->second
);
86 AllExceptionTables
.clear();
91 /// \brief Helper class which uses a value handler to automatically deletes the
92 /// memory block when the GlobalVariable is destroyed.
93 class GVMemoryBlock
: public CallbackVH
{
94 GVMemoryBlock(const GlobalVariable
*GV
)
95 : CallbackVH(const_cast<GlobalVariable
*>(GV
)) {}
98 /// \brief Returns the address the GlobalVariable should be written into. The
99 /// GVMemoryBlock object prefixes that.
100 static char *Create(const GlobalVariable
*GV
, const TargetData
& TD
) {
101 const Type
*ElTy
= GV
->getType()->getElementType();
102 size_t GVSize
= (size_t)TD
.getTypeAllocSize(ElTy
);
103 void *RawMemory
= ::operator new(
104 TargetData::RoundUpAlignment(sizeof(GVMemoryBlock
),
105 TD
.getPreferredAlignment(GV
))
107 new(RawMemory
) GVMemoryBlock(GV
);
108 return static_cast<char*>(RawMemory
) + sizeof(GVMemoryBlock
);
111 virtual void deleted() {
112 // We allocated with operator new and with some extra memory hanging off the
113 // end, so don't just delete this. I'm not sure if this is actually
115 this->~GVMemoryBlock();
116 ::operator delete(this);
119 } // anonymous namespace
121 char *ExecutionEngine::getMemoryForGV(const GlobalVariable
*GV
) {
122 return GVMemoryBlock::Create(GV
, *getTargetData());
125 bool ExecutionEngine::removeModule(Module
*M
) {
126 for(SmallVector
<Module
*, 1>::iterator I
= Modules
.begin(),
127 E
= Modules
.end(); I
!= E
; ++I
) {
131 clearGlobalMappingsFromModule(M
);
138 Function
*ExecutionEngine::FindFunctionNamed(const char *FnName
) {
139 for (unsigned i
= 0, e
= Modules
.size(); i
!= e
; ++i
) {
140 if (Function
*F
= Modules
[i
]->getFunction(FnName
))
147 void *ExecutionEngineState::RemoveMapping(const MutexGuard
&,
148 const GlobalValue
*ToUnmap
) {
149 GlobalAddressMapTy::iterator I
= GlobalAddressMap
.find(ToUnmap
);
152 // FIXME: This is silly, we shouldn't end up with a mapping -> 0 in the
154 if (I
== GlobalAddressMap
.end())
158 GlobalAddressMap
.erase(I
);
161 GlobalAddressReverseMap
.erase(OldVal
);
165 void ExecutionEngine::addGlobalMapping(const GlobalValue
*GV
, void *Addr
) {
166 MutexGuard
locked(lock
);
168 DEBUG(dbgs() << "JIT: Map \'" << GV
->getName()
169 << "\' to [" << Addr
<< "]\n";);
170 void *&CurVal
= EEState
.getGlobalAddressMap(locked
)[GV
];
171 assert((CurVal
== 0 || Addr
== 0) && "GlobalMapping already established!");
174 // If we are using the reverse mapping, add it too.
175 if (!EEState
.getGlobalAddressReverseMap(locked
).empty()) {
176 AssertingVH
<const GlobalValue
> &V
=
177 EEState
.getGlobalAddressReverseMap(locked
)[Addr
];
178 assert((V
== 0 || GV
== 0) && "GlobalMapping already established!");
183 void ExecutionEngine::clearAllGlobalMappings() {
184 MutexGuard
locked(lock
);
186 EEState
.getGlobalAddressMap(locked
).clear();
187 EEState
.getGlobalAddressReverseMap(locked
).clear();
190 void ExecutionEngine::clearGlobalMappingsFromModule(Module
*M
) {
191 MutexGuard
locked(lock
);
193 for (Module::iterator FI
= M
->begin(), FE
= M
->end(); FI
!= FE
; ++FI
)
194 EEState
.RemoveMapping(locked
, FI
);
195 for (Module::global_iterator GI
= M
->global_begin(), GE
= M
->global_end();
197 EEState
.RemoveMapping(locked
, GI
);
200 void *ExecutionEngine::updateGlobalMapping(const GlobalValue
*GV
, void *Addr
) {
201 MutexGuard
locked(lock
);
203 ExecutionEngineState::GlobalAddressMapTy
&Map
=
204 EEState
.getGlobalAddressMap(locked
);
206 // Deleting from the mapping?
208 return EEState
.RemoveMapping(locked
, GV
);
210 void *&CurVal
= Map
[GV
];
211 void *OldVal
= CurVal
;
213 if (CurVal
&& !EEState
.getGlobalAddressReverseMap(locked
).empty())
214 EEState
.getGlobalAddressReverseMap(locked
).erase(CurVal
);
217 // If we are using the reverse mapping, add it too.
218 if (!EEState
.getGlobalAddressReverseMap(locked
).empty()) {
219 AssertingVH
<const GlobalValue
> &V
=
220 EEState
.getGlobalAddressReverseMap(locked
)[Addr
];
221 assert((V
== 0 || GV
== 0) && "GlobalMapping already established!");
227 void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue
*GV
) {
228 MutexGuard
locked(lock
);
230 ExecutionEngineState::GlobalAddressMapTy::iterator I
=
231 EEState
.getGlobalAddressMap(locked
).find(GV
);
232 return I
!= EEState
.getGlobalAddressMap(locked
).end() ? I
->second
: 0;
235 const GlobalValue
*ExecutionEngine::getGlobalValueAtAddress(void *Addr
) {
236 MutexGuard
locked(lock
);
238 // If we haven't computed the reverse mapping yet, do so first.
239 if (EEState
.getGlobalAddressReverseMap(locked
).empty()) {
240 for (ExecutionEngineState::GlobalAddressMapTy::iterator
241 I
= EEState
.getGlobalAddressMap(locked
).begin(),
242 E
= EEState
.getGlobalAddressMap(locked
).end(); I
!= E
; ++I
)
243 EEState
.getGlobalAddressReverseMap(locked
).insert(std::make_pair(
244 I
->second
, I
->first
));
247 std::map
<void *, AssertingVH
<const GlobalValue
> >::iterator I
=
248 EEState
.getGlobalAddressReverseMap(locked
).find(Addr
);
249 return I
!= EEState
.getGlobalAddressReverseMap(locked
).end() ? I
->second
: 0;
255 std::vector
<char*> Values
;
257 ArgvArray() : Array(NULL
) {}
258 ~ArgvArray() { clear(); }
262 for (size_t I
= 0, E
= Values
.size(); I
!= E
; ++I
) {
267 /// Turn a vector of strings into a nice argv style array of pointers to null
268 /// terminated strings.
269 void *reset(LLVMContext
&C
, ExecutionEngine
*EE
,
270 const std::vector
<std::string
> &InputArgv
);
272 } // anonymous namespace
273 void *ArgvArray::reset(LLVMContext
&C
, ExecutionEngine
*EE
,
274 const std::vector
<std::string
> &InputArgv
) {
275 clear(); // Free the old contents.
276 unsigned PtrSize
= EE
->getTargetData()->getPointerSize();
277 Array
= new char[(InputArgv
.size()+1)*PtrSize
];
279 DEBUG(dbgs() << "JIT: ARGV = " << (void*)Array
<< "\n");
280 const Type
*SBytePtr
= Type::getInt8PtrTy(C
);
282 for (unsigned i
= 0; i
!= InputArgv
.size(); ++i
) {
283 unsigned Size
= InputArgv
[i
].size()+1;
284 char *Dest
= new char[Size
];
285 Values
.push_back(Dest
);
286 DEBUG(dbgs() << "JIT: ARGV[" << i
<< "] = " << (void*)Dest
<< "\n");
288 std::copy(InputArgv
[i
].begin(), InputArgv
[i
].end(), Dest
);
291 // Endian safe: Array[i] = (PointerTy)Dest;
292 EE
->StoreValueToMemory(PTOGV(Dest
), (GenericValue
*)(Array
+i
*PtrSize
),
297 EE
->StoreValueToMemory(PTOGV(0),
298 (GenericValue
*)(Array
+InputArgv
.size()*PtrSize
),
303 void ExecutionEngine::runStaticConstructorsDestructors(Module
*module
,
305 const char *Name
= isDtors
? "llvm.global_dtors" : "llvm.global_ctors";
306 GlobalVariable
*GV
= module
->getNamedGlobal(Name
);
308 // If this global has internal linkage, or if it has a use, then it must be
309 // an old-style (llvmgcc3) static ctor with __main linked in and in use. If
310 // this is the case, don't execute any of the global ctors, __main will do
312 if (!GV
|| GV
->isDeclaration() || GV
->hasLocalLinkage()) return;
314 // Should be an array of '{ i32, void ()* }' structs. The first value is
315 // the init priority, which we ignore.
316 if (isa
<ConstantAggregateZero
>(GV
->getInitializer()))
318 ConstantArray
*InitList
= cast
<ConstantArray
>(GV
->getInitializer());
319 for (unsigned i
= 0, e
= InitList
->getNumOperands(); i
!= e
; ++i
) {
320 if (isa
<ConstantAggregateZero
>(InitList
->getOperand(i
)))
322 ConstantStruct
*CS
= cast
<ConstantStruct
>(InitList
->getOperand(i
));
324 Constant
*FP
= CS
->getOperand(1);
325 if (FP
->isNullValue())
326 continue; // Found a sentinal value, ignore.
328 // Strip off constant expression casts.
329 if (ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(FP
))
331 FP
= CE
->getOperand(0);
333 // Execute the ctor/dtor function!
334 if (Function
*F
= dyn_cast
<Function
>(FP
))
335 runFunction(F
, std::vector
<GenericValue
>());
337 // FIXME: It is marginally lame that we just do nothing here if we see an
338 // entry we don't recognize. It might not be unreasonable for the verifier
339 // to not even allow this and just assert here.
343 void ExecutionEngine::runStaticConstructorsDestructors(bool isDtors
) {
344 // Execute global ctors/dtors for each module in the program.
345 for (unsigned i
= 0, e
= Modules
.size(); i
!= e
; ++i
)
346 runStaticConstructorsDestructors(Modules
[i
], isDtors
);
350 /// isTargetNullPtr - Return whether the target pointer stored at Loc is null.
351 static bool isTargetNullPtr(ExecutionEngine
*EE
, void *Loc
) {
352 unsigned PtrSize
= EE
->getTargetData()->getPointerSize();
353 for (unsigned i
= 0; i
< PtrSize
; ++i
)
354 if (*(i
+ (uint8_t*)Loc
))
360 int ExecutionEngine::runFunctionAsMain(Function
*Fn
,
361 const std::vector
<std::string
> &argv
,
362 const char * const * envp
) {
363 std::vector
<GenericValue
> GVArgs
;
365 GVArgc
.IntVal
= APInt(32, argv
.size());
368 unsigned NumArgs
= Fn
->getFunctionType()->getNumParams();
369 const FunctionType
*FTy
= Fn
->getFunctionType();
370 const Type
* PPInt8Ty
= Type::getInt8PtrTy(Fn
->getContext())->getPointerTo();
372 // Check the argument types.
374 report_fatal_error("Invalid number of arguments of main() supplied");
375 if (NumArgs
>= 3 && FTy
->getParamType(2) != PPInt8Ty
)
376 report_fatal_error("Invalid type for third argument of main() supplied");
377 if (NumArgs
>= 2 && FTy
->getParamType(1) != PPInt8Ty
)
378 report_fatal_error("Invalid type for second argument of main() supplied");
379 if (NumArgs
>= 1 && !FTy
->getParamType(0)->isIntegerTy(32))
380 report_fatal_error("Invalid type for first argument of main() supplied");
381 if (!FTy
->getReturnType()->isIntegerTy() &&
382 !FTy
->getReturnType()->isVoidTy())
383 report_fatal_error("Invalid return type of main() supplied");
388 GVArgs
.push_back(GVArgc
); // Arg #0 = argc.
391 GVArgs
.push_back(PTOGV(CArgv
.reset(Fn
->getContext(), this, argv
)));
392 assert(!isTargetNullPtr(this, GVTOP(GVArgs
[1])) &&
393 "argv[0] was null after CreateArgv");
395 std::vector
<std::string
> EnvVars
;
396 for (unsigned i
= 0; envp
[i
]; ++i
)
397 EnvVars
.push_back(envp
[i
]);
399 GVArgs
.push_back(PTOGV(CEnv
.reset(Fn
->getContext(), this, EnvVars
)));
404 return runFunction(Fn
, GVArgs
).IntVal
.getZExtValue();
407 ExecutionEngine
*ExecutionEngine::create(Module
*M
,
408 bool ForceInterpreter
,
409 std::string
*ErrorStr
,
410 CodeGenOpt::Level OptLevel
,
412 return EngineBuilder(M
)
413 .setEngineKind(ForceInterpreter
414 ? EngineKind::Interpreter
416 .setErrorStr(ErrorStr
)
417 .setOptLevel(OptLevel
)
418 .setAllocateGVsWithCode(GVsWithCode
)
422 ExecutionEngine
*EngineBuilder::create() {
423 // Make sure we can resolve symbols in the program as well. The zero arg
424 // to the function tells DynamicLibrary to load the program, not a library.
425 if (sys::DynamicLibrary::LoadLibraryPermanently(0, ErrorStr
))
428 // If the user specified a memory manager but didn't specify which engine to
429 // create, we assume they only want the JIT, and we fail if they only want
432 if (WhichEngine
& EngineKind::JIT
)
433 WhichEngine
= EngineKind::JIT
;
436 *ErrorStr
= "Cannot create an interpreter with a memory manager.";
441 // Unless the interpreter was explicitly selected or the JIT is not linked,
443 if (WhichEngine
& EngineKind::JIT
) {
444 if (UseMCJIT
&& ExecutionEngine::MCJITCtor
) {
445 ExecutionEngine
*EE
=
446 ExecutionEngine::MCJITCtor(M
, ErrorStr
, JMM
, OptLevel
,
447 AllocateGVsWithCode
, CMModel
,
448 MArch
, MCPU
, MAttrs
);
450 } else if (ExecutionEngine::JITCtor
) {
451 ExecutionEngine
*EE
=
452 ExecutionEngine::JITCtor(M
, ErrorStr
, JMM
, OptLevel
,
453 AllocateGVsWithCode
, CMModel
,
454 MArch
, MCPU
, MAttrs
);
459 // If we can't make a JIT and we didn't request one specifically, try making
460 // an interpreter instead.
461 if (WhichEngine
& EngineKind::Interpreter
) {
462 if (ExecutionEngine::InterpCtor
)
463 return ExecutionEngine::InterpCtor(M
, ErrorStr
);
465 *ErrorStr
= "Interpreter has not been linked in.";
469 if ((WhichEngine
& EngineKind::JIT
) && ExecutionEngine::JITCtor
== 0) {
471 *ErrorStr
= "JIT has not been linked in.";
477 void *ExecutionEngine::getPointerToGlobal(const GlobalValue
*GV
) {
478 if (Function
*F
= const_cast<Function
*>(dyn_cast
<Function
>(GV
)))
479 return getPointerToFunction(F
);
481 MutexGuard
locked(lock
);
482 if (void *P
= EEState
.getGlobalAddressMap(locked
)[GV
])
485 // Global variable might have been added since interpreter started.
486 if (GlobalVariable
*GVar
=
487 const_cast<GlobalVariable
*>(dyn_cast
<GlobalVariable
>(GV
)))
488 EmitGlobalVariable(GVar
);
490 llvm_unreachable("Global hasn't had an address allocated yet!");
492 return EEState
.getGlobalAddressMap(locked
)[GV
];
495 /// \brief Converts a Constant* into a GenericValue, including handling of
496 /// ConstantExpr values.
497 GenericValue
ExecutionEngine::getConstantValue(const Constant
*C
) {
498 // If its undefined, return the garbage.
499 if (isa
<UndefValue
>(C
)) {
501 switch (C
->getType()->getTypeID()) {
502 case Type::IntegerTyID
:
503 case Type::X86_FP80TyID
:
504 case Type::FP128TyID
:
505 case Type::PPC_FP128TyID
:
506 // Although the value is undefined, we still have to construct an APInt
507 // with the correct bit width.
508 Result
.IntVal
= APInt(C
->getType()->getPrimitiveSizeInBits(), 0);
516 // Otherwise, if the value is a ConstantExpr...
517 if (const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(C
)) {
518 Constant
*Op0
= CE
->getOperand(0);
519 switch (CE
->getOpcode()) {
520 case Instruction::GetElementPtr
: {
522 GenericValue Result
= getConstantValue(Op0
);
523 SmallVector
<Value
*, 8> Indices(CE
->op_begin()+1, CE
->op_end());
525 TD
->getIndexedOffset(Op0
->getType(), &Indices
[0], Indices
.size());
527 char* tmp
= (char*) Result
.PointerVal
;
528 Result
= PTOGV(tmp
+ Offset
);
531 case Instruction::Trunc
: {
532 GenericValue GV
= getConstantValue(Op0
);
533 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
534 GV
.IntVal
= GV
.IntVal
.trunc(BitWidth
);
537 case Instruction::ZExt
: {
538 GenericValue GV
= getConstantValue(Op0
);
539 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
540 GV
.IntVal
= GV
.IntVal
.zext(BitWidth
);
543 case Instruction::SExt
: {
544 GenericValue GV
= getConstantValue(Op0
);
545 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
546 GV
.IntVal
= GV
.IntVal
.sext(BitWidth
);
549 case Instruction::FPTrunc
: {
551 GenericValue GV
= getConstantValue(Op0
);
552 GV
.FloatVal
= float(GV
.DoubleVal
);
555 case Instruction::FPExt
:{
557 GenericValue GV
= getConstantValue(Op0
);
558 GV
.DoubleVal
= double(GV
.FloatVal
);
561 case Instruction::UIToFP
: {
562 GenericValue GV
= getConstantValue(Op0
);
563 if (CE
->getType()->isFloatTy())
564 GV
.FloatVal
= float(GV
.IntVal
.roundToDouble());
565 else if (CE
->getType()->isDoubleTy())
566 GV
.DoubleVal
= GV
.IntVal
.roundToDouble();
567 else if (CE
->getType()->isX86_FP80Ty()) {
568 APFloat apf
= APFloat::getZero(APFloat::x87DoubleExtended
);
569 (void)apf
.convertFromAPInt(GV
.IntVal
,
571 APFloat::rmNearestTiesToEven
);
572 GV
.IntVal
= apf
.bitcastToAPInt();
576 case Instruction::SIToFP
: {
577 GenericValue GV
= getConstantValue(Op0
);
578 if (CE
->getType()->isFloatTy())
579 GV
.FloatVal
= float(GV
.IntVal
.signedRoundToDouble());
580 else if (CE
->getType()->isDoubleTy())
581 GV
.DoubleVal
= GV
.IntVal
.signedRoundToDouble();
582 else if (CE
->getType()->isX86_FP80Ty()) {
583 APFloat apf
= APFloat::getZero(APFloat::x87DoubleExtended
);
584 (void)apf
.convertFromAPInt(GV
.IntVal
,
586 APFloat::rmNearestTiesToEven
);
587 GV
.IntVal
= apf
.bitcastToAPInt();
591 case Instruction::FPToUI
: // double->APInt conversion handles sign
592 case Instruction::FPToSI
: {
593 GenericValue GV
= getConstantValue(Op0
);
594 uint32_t BitWidth
= cast
<IntegerType
>(CE
->getType())->getBitWidth();
595 if (Op0
->getType()->isFloatTy())
596 GV
.IntVal
= APIntOps::RoundFloatToAPInt(GV
.FloatVal
, BitWidth
);
597 else if (Op0
->getType()->isDoubleTy())
598 GV
.IntVal
= APIntOps::RoundDoubleToAPInt(GV
.DoubleVal
, BitWidth
);
599 else if (Op0
->getType()->isX86_FP80Ty()) {
600 APFloat apf
= APFloat(GV
.IntVal
);
603 (void)apf
.convertToInteger(&v
, BitWidth
,
604 CE
->getOpcode()==Instruction::FPToSI
,
605 APFloat::rmTowardZero
, &ignored
);
606 GV
.IntVal
= v
; // endian?
610 case Instruction::PtrToInt
: {
611 GenericValue GV
= getConstantValue(Op0
);
612 uint32_t PtrWidth
= TD
->getPointerSizeInBits();
613 GV
.IntVal
= APInt(PtrWidth
, uintptr_t(GV
.PointerVal
));
616 case Instruction::IntToPtr
: {
617 GenericValue GV
= getConstantValue(Op0
);
618 uint32_t PtrWidth
= TD
->getPointerSizeInBits();
619 if (PtrWidth
!= GV
.IntVal
.getBitWidth())
620 GV
.IntVal
= GV
.IntVal
.zextOrTrunc(PtrWidth
);
621 assert(GV
.IntVal
.getBitWidth() <= 64 && "Bad pointer width");
622 GV
.PointerVal
= PointerTy(uintptr_t(GV
.IntVal
.getZExtValue()));
625 case Instruction::BitCast
: {
626 GenericValue GV
= getConstantValue(Op0
);
627 const Type
* DestTy
= CE
->getType();
628 switch (Op0
->getType()->getTypeID()) {
629 default: llvm_unreachable("Invalid bitcast operand");
630 case Type::IntegerTyID
:
631 assert(DestTy
->isFloatingPointTy() && "invalid bitcast");
632 if (DestTy
->isFloatTy())
633 GV
.FloatVal
= GV
.IntVal
.bitsToFloat();
634 else if (DestTy
->isDoubleTy())
635 GV
.DoubleVal
= GV
.IntVal
.bitsToDouble();
637 case Type::FloatTyID
:
638 assert(DestTy
->isIntegerTy(32) && "Invalid bitcast");
639 GV
.IntVal
= APInt::floatToBits(GV
.FloatVal
);
641 case Type::DoubleTyID
:
642 assert(DestTy
->isIntegerTy(64) && "Invalid bitcast");
643 GV
.IntVal
= APInt::doubleToBits(GV
.DoubleVal
);
645 case Type::PointerTyID
:
646 assert(DestTy
->isPointerTy() && "Invalid bitcast");
647 break; // getConstantValue(Op0) above already converted it
651 case Instruction::Add
:
652 case Instruction::FAdd
:
653 case Instruction::Sub
:
654 case Instruction::FSub
:
655 case Instruction::Mul
:
656 case Instruction::FMul
:
657 case Instruction::UDiv
:
658 case Instruction::SDiv
:
659 case Instruction::URem
:
660 case Instruction::SRem
:
661 case Instruction::And
:
662 case Instruction::Or
:
663 case Instruction::Xor
: {
664 GenericValue LHS
= getConstantValue(Op0
);
665 GenericValue RHS
= getConstantValue(CE
->getOperand(1));
667 switch (CE
->getOperand(0)->getType()->getTypeID()) {
668 default: llvm_unreachable("Bad add type!");
669 case Type::IntegerTyID
:
670 switch (CE
->getOpcode()) {
671 default: llvm_unreachable("Invalid integer opcode");
672 case Instruction::Add
: GV
.IntVal
= LHS
.IntVal
+ RHS
.IntVal
; break;
673 case Instruction::Sub
: GV
.IntVal
= LHS
.IntVal
- RHS
.IntVal
; break;
674 case Instruction::Mul
: GV
.IntVal
= LHS
.IntVal
* RHS
.IntVal
; break;
675 case Instruction::UDiv
:GV
.IntVal
= LHS
.IntVal
.udiv(RHS
.IntVal
); break;
676 case Instruction::SDiv
:GV
.IntVal
= LHS
.IntVal
.sdiv(RHS
.IntVal
); break;
677 case Instruction::URem
:GV
.IntVal
= LHS
.IntVal
.urem(RHS
.IntVal
); break;
678 case Instruction::SRem
:GV
.IntVal
= LHS
.IntVal
.srem(RHS
.IntVal
); break;
679 case Instruction::And
: GV
.IntVal
= LHS
.IntVal
& RHS
.IntVal
; break;
680 case Instruction::Or
: GV
.IntVal
= LHS
.IntVal
| RHS
.IntVal
; break;
681 case Instruction::Xor
: GV
.IntVal
= LHS
.IntVal
^ RHS
.IntVal
; break;
684 case Type::FloatTyID
:
685 switch (CE
->getOpcode()) {
686 default: llvm_unreachable("Invalid float opcode");
687 case Instruction::FAdd
:
688 GV
.FloatVal
= LHS
.FloatVal
+ RHS
.FloatVal
; break;
689 case Instruction::FSub
:
690 GV
.FloatVal
= LHS
.FloatVal
- RHS
.FloatVal
; break;
691 case Instruction::FMul
:
692 GV
.FloatVal
= LHS
.FloatVal
* RHS
.FloatVal
; break;
693 case Instruction::FDiv
:
694 GV
.FloatVal
= LHS
.FloatVal
/ RHS
.FloatVal
; break;
695 case Instruction::FRem
:
696 GV
.FloatVal
= std::fmod(LHS
.FloatVal
,RHS
.FloatVal
); break;
699 case Type::DoubleTyID
:
700 switch (CE
->getOpcode()) {
701 default: llvm_unreachable("Invalid double opcode");
702 case Instruction::FAdd
:
703 GV
.DoubleVal
= LHS
.DoubleVal
+ RHS
.DoubleVal
; break;
704 case Instruction::FSub
:
705 GV
.DoubleVal
= LHS
.DoubleVal
- RHS
.DoubleVal
; break;
706 case Instruction::FMul
:
707 GV
.DoubleVal
= LHS
.DoubleVal
* RHS
.DoubleVal
; break;
708 case Instruction::FDiv
:
709 GV
.DoubleVal
= LHS
.DoubleVal
/ RHS
.DoubleVal
; break;
710 case Instruction::FRem
:
711 GV
.DoubleVal
= std::fmod(LHS
.DoubleVal
,RHS
.DoubleVal
); break;
714 case Type::X86_FP80TyID
:
715 case Type::PPC_FP128TyID
:
716 case Type::FP128TyID
: {
717 APFloat apfLHS
= APFloat(LHS
.IntVal
);
718 switch (CE
->getOpcode()) {
719 default: llvm_unreachable("Invalid long double opcode");
720 case Instruction::FAdd
:
721 apfLHS
.add(APFloat(RHS
.IntVal
), APFloat::rmNearestTiesToEven
);
722 GV
.IntVal
= apfLHS
.bitcastToAPInt();
724 case Instruction::FSub
:
725 apfLHS
.subtract(APFloat(RHS
.IntVal
), APFloat::rmNearestTiesToEven
);
726 GV
.IntVal
= apfLHS
.bitcastToAPInt();
728 case Instruction::FMul
:
729 apfLHS
.multiply(APFloat(RHS
.IntVal
), APFloat::rmNearestTiesToEven
);
730 GV
.IntVal
= apfLHS
.bitcastToAPInt();
732 case Instruction::FDiv
:
733 apfLHS
.divide(APFloat(RHS
.IntVal
), APFloat::rmNearestTiesToEven
);
734 GV
.IntVal
= apfLHS
.bitcastToAPInt();
736 case Instruction::FRem
:
737 apfLHS
.mod(APFloat(RHS
.IntVal
), APFloat::rmNearestTiesToEven
);
738 GV
.IntVal
= apfLHS
.bitcastToAPInt();
750 SmallString
<256> Msg
;
751 raw_svector_ostream
OS(Msg
);
752 OS
<< "ConstantExpr not handled: " << *CE
;
753 report_fatal_error(OS
.str());
756 // Otherwise, we have a simple constant.
758 switch (C
->getType()->getTypeID()) {
759 case Type::FloatTyID
:
760 Result
.FloatVal
= cast
<ConstantFP
>(C
)->getValueAPF().convertToFloat();
762 case Type::DoubleTyID
:
763 Result
.DoubleVal
= cast
<ConstantFP
>(C
)->getValueAPF().convertToDouble();
765 case Type::X86_FP80TyID
:
766 case Type::FP128TyID
:
767 case Type::PPC_FP128TyID
:
768 Result
.IntVal
= cast
<ConstantFP
>(C
)->getValueAPF().bitcastToAPInt();
770 case Type::IntegerTyID
:
771 Result
.IntVal
= cast
<ConstantInt
>(C
)->getValue();
773 case Type::PointerTyID
:
774 if (isa
<ConstantPointerNull
>(C
))
775 Result
.PointerVal
= 0;
776 else if (const Function
*F
= dyn_cast
<Function
>(C
))
777 Result
= PTOGV(getPointerToFunctionOrStub(const_cast<Function
*>(F
)));
778 else if (const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(C
))
779 Result
= PTOGV(getOrEmitGlobalVariable(const_cast<GlobalVariable
*>(GV
)));
780 else if (const BlockAddress
*BA
= dyn_cast
<BlockAddress
>(C
))
781 Result
= PTOGV(getPointerToBasicBlock(const_cast<BasicBlock
*>(
782 BA
->getBasicBlock())));
784 llvm_unreachable("Unknown constant pointer type!");
787 SmallString
<256> Msg
;
788 raw_svector_ostream
OS(Msg
);
789 OS
<< "ERROR: Constant unimplemented for type: " << *C
->getType();
790 report_fatal_error(OS
.str());
796 /// StoreIntToMemory - Fills the StoreBytes bytes of memory starting from Dst
797 /// with the integer held in IntVal.
798 static void StoreIntToMemory(const APInt
&IntVal
, uint8_t *Dst
,
799 unsigned StoreBytes
) {
800 assert((IntVal
.getBitWidth()+7)/8 >= StoreBytes
&& "Integer too small!");
801 uint8_t *Src
= (uint8_t *)IntVal
.getRawData();
803 if (sys::isLittleEndianHost()) {
804 // Little-endian host - the source is ordered from LSB to MSB. Order the
805 // destination from LSB to MSB: Do a straight copy.
806 memcpy(Dst
, Src
, StoreBytes
);
808 // Big-endian host - the source is an array of 64 bit words ordered from
809 // LSW to MSW. Each word is ordered from MSB to LSB. Order the destination
810 // from MSB to LSB: Reverse the word order, but not the bytes in a word.
811 while (StoreBytes
> sizeof(uint64_t)) {
812 StoreBytes
-= sizeof(uint64_t);
813 // May not be aligned so use memcpy.
814 memcpy(Dst
+ StoreBytes
, Src
, sizeof(uint64_t));
815 Src
+= sizeof(uint64_t);
818 memcpy(Dst
, Src
+ sizeof(uint64_t) - StoreBytes
, StoreBytes
);
822 void ExecutionEngine::StoreValueToMemory(const GenericValue
&Val
,
823 GenericValue
*Ptr
, const Type
*Ty
) {
824 const unsigned StoreBytes
= getTargetData()->getTypeStoreSize(Ty
);
826 switch (Ty
->getTypeID()) {
827 case Type::IntegerTyID
:
828 StoreIntToMemory(Val
.IntVal
, (uint8_t*)Ptr
, StoreBytes
);
830 case Type::FloatTyID
:
831 *((float*)Ptr
) = Val
.FloatVal
;
833 case Type::DoubleTyID
:
834 *((double*)Ptr
) = Val
.DoubleVal
;
836 case Type::X86_FP80TyID
:
837 memcpy(Ptr
, Val
.IntVal
.getRawData(), 10);
839 case Type::PointerTyID
:
840 // Ensure 64 bit target pointers are fully initialized on 32 bit hosts.
841 if (StoreBytes
!= sizeof(PointerTy
))
842 memset(Ptr
, 0, StoreBytes
);
844 *((PointerTy
*)Ptr
) = Val
.PointerVal
;
847 dbgs() << "Cannot store value of type " << *Ty
<< "!\n";
850 if (sys::isLittleEndianHost() != getTargetData()->isLittleEndian())
851 // Host and target are different endian - reverse the stored bytes.
852 std::reverse((uint8_t*)Ptr
, StoreBytes
+ (uint8_t*)Ptr
);
855 /// LoadIntFromMemory - Loads the integer stored in the LoadBytes bytes starting
856 /// from Src into IntVal, which is assumed to be wide enough and to hold zero.
857 static void LoadIntFromMemory(APInt
&IntVal
, uint8_t *Src
, unsigned LoadBytes
) {
858 assert((IntVal
.getBitWidth()+7)/8 >= LoadBytes
&& "Integer too small!");
859 uint8_t *Dst
= (uint8_t *)IntVal
.getRawData();
861 if (sys::isLittleEndianHost())
862 // Little-endian host - the destination must be ordered from LSB to MSB.
863 // The source is ordered from LSB to MSB: Do a straight copy.
864 memcpy(Dst
, Src
, LoadBytes
);
866 // Big-endian - the destination is an array of 64 bit words ordered from
867 // LSW to MSW. Each word must be ordered from MSB to LSB. The source is
868 // ordered from MSB to LSB: Reverse the word order, but not the bytes in
870 while (LoadBytes
> sizeof(uint64_t)) {
871 LoadBytes
-= sizeof(uint64_t);
872 // May not be aligned so use memcpy.
873 memcpy(Dst
, Src
+ LoadBytes
, sizeof(uint64_t));
874 Dst
+= sizeof(uint64_t);
877 memcpy(Dst
+ sizeof(uint64_t) - LoadBytes
, Src
, LoadBytes
);
883 void ExecutionEngine::LoadValueFromMemory(GenericValue
&Result
,
886 const unsigned LoadBytes
= getTargetData()->getTypeStoreSize(Ty
);
888 switch (Ty
->getTypeID()) {
889 case Type::IntegerTyID
:
890 // An APInt with all words initially zero.
891 Result
.IntVal
= APInt(cast
<IntegerType
>(Ty
)->getBitWidth(), 0);
892 LoadIntFromMemory(Result
.IntVal
, (uint8_t*)Ptr
, LoadBytes
);
894 case Type::FloatTyID
:
895 Result
.FloatVal
= *((float*)Ptr
);
897 case Type::DoubleTyID
:
898 Result
.DoubleVal
= *((double*)Ptr
);
900 case Type::PointerTyID
:
901 Result
.PointerVal
= *((PointerTy
*)Ptr
);
903 case Type::X86_FP80TyID
: {
904 // This is endian dependent, but it will only work on x86 anyway.
905 // FIXME: Will not trap if loading a signaling NaN.
908 Result
.IntVal
= APInt(80, 2, y
);
912 SmallString
<256> Msg
;
913 raw_svector_ostream
OS(Msg
);
914 OS
<< "Cannot load value of type " << *Ty
<< "!";
915 report_fatal_error(OS
.str());
919 void ExecutionEngine::InitializeMemory(const Constant
*Init
, void *Addr
) {
920 DEBUG(dbgs() << "JIT: Initializing " << Addr
<< " ");
922 if (isa
<UndefValue
>(Init
)) {
924 } else if (const ConstantVector
*CP
= dyn_cast
<ConstantVector
>(Init
)) {
925 unsigned ElementSize
=
926 getTargetData()->getTypeAllocSize(CP
->getType()->getElementType());
927 for (unsigned i
= 0, e
= CP
->getNumOperands(); i
!= e
; ++i
)
928 InitializeMemory(CP
->getOperand(i
), (char*)Addr
+i
*ElementSize
);
930 } else if (isa
<ConstantAggregateZero
>(Init
)) {
931 memset(Addr
, 0, (size_t)getTargetData()->getTypeAllocSize(Init
->getType()));
933 } else if (const ConstantArray
*CPA
= dyn_cast
<ConstantArray
>(Init
)) {
934 unsigned ElementSize
=
935 getTargetData()->getTypeAllocSize(CPA
->getType()->getElementType());
936 for (unsigned i
= 0, e
= CPA
->getNumOperands(); i
!= e
; ++i
)
937 InitializeMemory(CPA
->getOperand(i
), (char*)Addr
+i
*ElementSize
);
939 } else if (const ConstantStruct
*CPS
= dyn_cast
<ConstantStruct
>(Init
)) {
940 const StructLayout
*SL
=
941 getTargetData()->getStructLayout(cast
<StructType
>(CPS
->getType()));
942 for (unsigned i
= 0, e
= CPS
->getNumOperands(); i
!= e
; ++i
)
943 InitializeMemory(CPS
->getOperand(i
), (char*)Addr
+SL
->getElementOffset(i
));
945 } else if (Init
->getType()->isFirstClassType()) {
946 GenericValue Val
= getConstantValue(Init
);
947 StoreValueToMemory(Val
, (GenericValue
*)Addr
, Init
->getType());
951 DEBUG(dbgs() << "Bad Type: " << *Init
->getType() << "\n");
952 llvm_unreachable("Unknown constant type to initialize memory with!");
955 /// EmitGlobals - Emit all of the global variables to memory, storing their
956 /// addresses into GlobalAddress. This must make sure to copy the contents of
957 /// their initializers into the memory.
958 void ExecutionEngine::emitGlobals() {
959 // Loop over all of the global variables in the program, allocating the memory
960 // to hold them. If there is more than one module, do a prepass over globals
961 // to figure out how the different modules should link together.
962 std::map
<std::pair
<std::string
, const Type
*>,
963 const GlobalValue
*> LinkedGlobalsMap
;
965 if (Modules
.size() != 1) {
966 for (unsigned m
= 0, e
= Modules
.size(); m
!= e
; ++m
) {
967 Module
&M
= *Modules
[m
];
968 for (Module::const_global_iterator I
= M
.global_begin(),
969 E
= M
.global_end(); I
!= E
; ++I
) {
970 const GlobalValue
*GV
= I
;
971 if (GV
->hasLocalLinkage() || GV
->isDeclaration() ||
972 GV
->hasAppendingLinkage() || !GV
->hasName())
973 continue;// Ignore external globals and globals with internal linkage.
975 const GlobalValue
*&GVEntry
=
976 LinkedGlobalsMap
[std::make_pair(GV
->getName(), GV
->getType())];
978 // If this is the first time we've seen this global, it is the canonical
985 // If the existing global is strong, never replace it.
986 if (GVEntry
->hasExternalLinkage() ||
987 GVEntry
->hasDLLImportLinkage() ||
988 GVEntry
->hasDLLExportLinkage())
991 // Otherwise, we know it's linkonce/weak, replace it if this is a strong
992 // symbol. FIXME is this right for common?
993 if (GV
->hasExternalLinkage() || GVEntry
->hasExternalWeakLinkage())
999 std::vector
<const GlobalValue
*> NonCanonicalGlobals
;
1000 for (unsigned m
= 0, e
= Modules
.size(); m
!= e
; ++m
) {
1001 Module
&M
= *Modules
[m
];
1002 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
1004 // In the multi-module case, see what this global maps to.
1005 if (!LinkedGlobalsMap
.empty()) {
1006 if (const GlobalValue
*GVEntry
=
1007 LinkedGlobalsMap
[std::make_pair(I
->getName(), I
->getType())]) {
1008 // If something else is the canonical global, ignore this one.
1009 if (GVEntry
!= &*I
) {
1010 NonCanonicalGlobals
.push_back(I
);
1016 if (!I
->isDeclaration()) {
1017 addGlobalMapping(I
, getMemoryForGV(I
));
1019 // External variable reference. Try to use the dynamic loader to
1020 // get a pointer to it.
1022 sys::DynamicLibrary::SearchForAddressOfSymbol(I
->getName()))
1023 addGlobalMapping(I
, SymAddr
);
1025 report_fatal_error("Could not resolve external global address: "
1031 // If there are multiple modules, map the non-canonical globals to their
1032 // canonical location.
1033 if (!NonCanonicalGlobals
.empty()) {
1034 for (unsigned i
= 0, e
= NonCanonicalGlobals
.size(); i
!= e
; ++i
) {
1035 const GlobalValue
*GV
= NonCanonicalGlobals
[i
];
1036 const GlobalValue
*CGV
=
1037 LinkedGlobalsMap
[std::make_pair(GV
->getName(), GV
->getType())];
1038 void *Ptr
= getPointerToGlobalIfAvailable(CGV
);
1039 assert(Ptr
&& "Canonical global wasn't codegen'd!");
1040 addGlobalMapping(GV
, Ptr
);
1044 // Now that all of the globals are set up in memory, loop through them all
1045 // and initialize their contents.
1046 for (Module::const_global_iterator I
= M
.global_begin(), E
= M
.global_end();
1048 if (!I
->isDeclaration()) {
1049 if (!LinkedGlobalsMap
.empty()) {
1050 if (const GlobalValue
*GVEntry
=
1051 LinkedGlobalsMap
[std::make_pair(I
->getName(), I
->getType())])
1052 if (GVEntry
!= &*I
) // Not the canonical variable.
1055 EmitGlobalVariable(I
);
1061 // EmitGlobalVariable - This method emits the specified global variable to the
1062 // address specified in GlobalAddresses, or allocates new memory if it's not
1063 // already in the map.
1064 void ExecutionEngine::EmitGlobalVariable(const GlobalVariable
*GV
) {
1065 void *GA
= getPointerToGlobalIfAvailable(GV
);
1068 // If it's not already specified, allocate memory for the global.
1069 GA
= getMemoryForGV(GV
);
1070 addGlobalMapping(GV
, GA
);
1073 // Don't initialize if it's thread local, let the client do it.
1074 if (!GV
->isThreadLocal())
1075 InitializeMemory(GV
->getInitializer(), GA
);
1077 const Type
*ElTy
= GV
->getType()->getElementType();
1078 size_t GVSize
= (size_t)getTargetData()->getTypeAllocSize(ElTy
);
1079 NumInitBytes
+= (unsigned)GVSize
;
1083 ExecutionEngineState::ExecutionEngineState(ExecutionEngine
&EE
)
1084 : EE(EE
), GlobalAddressMap(this) {
1088 ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState
*EES
) {
1089 return &EES
->EE
.lock
;
1092 void ExecutionEngineState::AddressMapConfig::onDelete(ExecutionEngineState
*EES
,
1093 const GlobalValue
*Old
) {
1094 void *OldVal
= EES
->GlobalAddressMap
.lookup(Old
);
1095 EES
->GlobalAddressReverseMap
.erase(OldVal
);
1098 void ExecutionEngineState::AddressMapConfig::onRAUW(ExecutionEngineState
*,
1099 const GlobalValue
*,
1100 const GlobalValue
*) {
1101 assert(false && "The ExecutionEngine doesn't know how to handle a"
1102 " RAUW on a value it has a global mapping for.");