1 //===-- ExecutionEngine.cpp - Common Implementation shared by EEs ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "Interpreter/Interpreter.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/ExecutionEngine.h"
23 #include "llvm/ExecutionEngine/GenericValue.h"
24 #include "llvm/Target/TargetData.h"
25 #include "Support/Debug.h"
26 #include "Support/Statistic.h"
27 #include "Support/DynamicLinker.h"
28 #include "Config/dlfcn.h"
32 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
35 ExecutionEngine::ExecutionEngine(ModuleProvider *P) :
36 CurMod(*P->getModule()), MP(P) {
37 assert(P && "ModuleProvider is null?");
40 ExecutionEngine::ExecutionEngine(Module *M) : CurMod(*M), MP(0) {
41 assert(M && "Module is null?");
44 ExecutionEngine::~ExecutionEngine() {
48 /// If possible, create a JIT, unless the caller specifically requests an
49 /// Interpreter or there's an error. If even an Interpreter cannot be created,
52 ExecutionEngine *ExecutionEngine::create(ModuleProvider *MP,
53 bool ForceInterpreter) {
54 ExecutionEngine *EE = 0;
56 // Unless the interpreter was explicitly selected, make a JIT.
57 if (!ForceInterpreter)
60 // If we can't make a JIT, make an interpreter instead.
63 EE = Interpreter::create(MP->materializeModule());
70 /// getPointerToGlobal - This returns the address of the specified global
71 /// value. This may involve code generation if it's a function.
73 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
74 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
75 return getPointerToFunction(F);
77 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
78 return GlobalAddress[GV];
83 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
86 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
87 switch (CE->getOpcode()) {
88 case Instruction::GetElementPtr: {
89 Result = getConstantValue(CE->getOperand(0));
90 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
92 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
94 Result.LongVal += Offset;
97 case Instruction::Cast: {
98 // We only need to handle a few cases here. Almost all casts will
99 // automatically fold, just the ones involving pointers won't.
101 Constant *Op = CE->getOperand(0);
103 // Handle cast of pointer to pointer...
104 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
105 return getConstantValue(Op);
107 // Handle a cast of pointer to any integral type...
108 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
109 return getConstantValue(Op);
111 // Handle cast of long to pointer...
112 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
113 Op->getType() == Type::ULongTy))
114 return getConstantValue(Op);
118 case Instruction::Add:
119 if (CE->getOperand(0)->getType() == Type::LongTy ||
120 CE->getOperand(0)->getType() == Type::ULongTy)
121 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
122 getConstantValue(CE->getOperand(1)).LongVal;
130 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
134 switch (C->getType()->getPrimitiveID()) {
135 #define GET_CONST_VAL(TY, CLASS) \
136 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
137 GET_CONST_VAL(Bool , ConstantBool);
138 GET_CONST_VAL(UByte , ConstantUInt);
139 GET_CONST_VAL(SByte , ConstantSInt);
140 GET_CONST_VAL(UShort , ConstantUInt);
141 GET_CONST_VAL(Short , ConstantSInt);
142 GET_CONST_VAL(UInt , ConstantUInt);
143 GET_CONST_VAL(Int , ConstantSInt);
144 GET_CONST_VAL(ULong , ConstantUInt);
145 GET_CONST_VAL(Long , ConstantSInt);
146 GET_CONST_VAL(Float , ConstantFP);
147 GET_CONST_VAL(Double , ConstantFP);
149 case Type::PointerTyID:
150 if (isa<ConstantPointerNull>(C)) {
151 Result.PointerVal = 0;
152 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
153 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
156 assert(0 && "Unknown constant pointer type!");
160 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
168 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
170 if (getTargetData().isLittleEndian()) {
171 switch (Ty->getPrimitiveID()) {
173 case Type::UByteTyID:
174 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
175 case Type::UShortTyID:
176 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
177 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
179 Store4BytesLittleEndian:
180 case Type::FloatTyID:
182 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
183 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
184 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
185 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
187 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
188 goto Store4BytesLittleEndian;
189 case Type::DoubleTyID:
190 case Type::ULongTyID:
191 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
192 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
193 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
194 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
195 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
196 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
197 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
198 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
201 std::cout << "Cannot store value of type " << Ty << "!\n";
204 switch (Ty->getPrimitiveID()) {
206 case Type::UByteTyID:
207 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
208 case Type::UShortTyID:
209 case Type::ShortTyID: Ptr->Untyped[1] = Val.UShortVal & 255;
210 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
212 Store4BytesBigEndian:
213 case Type::FloatTyID:
215 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
216 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
217 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
218 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
220 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
221 goto Store4BytesBigEndian;
222 case Type::DoubleTyID:
223 case Type::ULongTyID:
224 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
225 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
226 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
227 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
228 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
229 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
230 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
231 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
234 std::cout << "Cannot store value of type " << Ty << "!\n";
241 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
244 if (getTargetData().isLittleEndian()) {
245 switch (Ty->getPrimitiveID()) {
247 case Type::UByteTyID:
248 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
249 case Type::UShortTyID:
250 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
251 ((unsigned)Ptr->Untyped[1] << 8);
253 Load4BytesLittleEndian:
254 case Type::FloatTyID:
256 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
257 ((unsigned)Ptr->Untyped[1] << 8) |
258 ((unsigned)Ptr->Untyped[2] << 16) |
259 ((unsigned)Ptr->Untyped[3] << 24);
261 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
262 goto Load4BytesLittleEndian;
263 case Type::DoubleTyID:
264 case Type::ULongTyID:
265 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
266 ((uint64_t)Ptr->Untyped[1] << 8) |
267 ((uint64_t)Ptr->Untyped[2] << 16) |
268 ((uint64_t)Ptr->Untyped[3] << 24) |
269 ((uint64_t)Ptr->Untyped[4] << 32) |
270 ((uint64_t)Ptr->Untyped[5] << 40) |
271 ((uint64_t)Ptr->Untyped[6] << 48) |
272 ((uint64_t)Ptr->Untyped[7] << 56);
275 std::cout << "Cannot load value of type " << *Ty << "!\n";
279 switch (Ty->getPrimitiveID()) {
281 case Type::UByteTyID:
282 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
283 case Type::UShortTyID:
284 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
285 ((unsigned)Ptr->Untyped[0] << 8);
288 case Type::FloatTyID:
290 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
291 ((unsigned)Ptr->Untyped[2] << 8) |
292 ((unsigned)Ptr->Untyped[1] << 16) |
293 ((unsigned)Ptr->Untyped[0] << 24);
295 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
296 goto Load4BytesBigEndian;
297 case Type::DoubleTyID:
298 case Type::ULongTyID:
299 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
300 ((uint64_t)Ptr->Untyped[6] << 8) |
301 ((uint64_t)Ptr->Untyped[5] << 16) |
302 ((uint64_t)Ptr->Untyped[4] << 24) |
303 ((uint64_t)Ptr->Untyped[3] << 32) |
304 ((uint64_t)Ptr->Untyped[2] << 40) |
305 ((uint64_t)Ptr->Untyped[1] << 48) |
306 ((uint64_t)Ptr->Untyped[0] << 56);
309 std::cout << "Cannot load value of type " << *Ty << "!\n";
316 // InitializeMemory - Recursive function to apply a Constant value into the
317 // specified memory location...
319 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
320 if (Init->getType()->isFirstClassType()) {
321 GenericValue Val = getConstantValue(Init);
322 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
326 switch (Init->getType()->getPrimitiveID()) {
327 case Type::ArrayTyID: {
328 const ConstantArray *CPA = cast<ConstantArray>(Init);
329 const std::vector<Use> &Val = CPA->getValues();
330 unsigned ElementSize =
331 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
332 for (unsigned i = 0; i < Val.size(); ++i)
333 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
337 case Type::StructTyID: {
338 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
339 const StructLayout *SL =
340 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
341 const std::vector<Use> &Val = CPS->getValues();
342 for (unsigned i = 0; i < Val.size(); ++i)
343 InitializeMemory(cast<Constant>(Val[i].get()),
344 (char*)Addr+SL->MemberOffsets[i]);
349 std::cerr << "Bad Type: " << Init->getType() << "\n";
350 assert(0 && "Unknown constant type to initialize memory with!");
354 /// EmitGlobals - Emit all of the global variables to memory, storing their
355 /// addresses into GlobalAddress. This must make sure to copy the contents of
356 /// their initializers into the memory.
358 void ExecutionEngine::emitGlobals() {
359 const TargetData &TD = getTargetData();
361 // Loop over all of the global variables in the program, allocating the memory
363 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
365 if (!I->isExternal()) {
366 // Get the type of the global...
367 const Type *Ty = I->getType()->getElementType();
369 // Allocate some memory for it!
370 unsigned Size = TD.getTypeSize(Ty);
371 GlobalAddress[I] = new char[Size];
372 NumInitBytes += Size;
374 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
375 << (void*)GlobalAddress[I] << "\n");
377 // External variable reference. Try to use the dynamic loader to
378 // get a pointer to it.
379 if (void *SymAddr = GetAddressOfSymbol(I->getName().c_str()))
380 GlobalAddress[I] = SymAddr;
382 std::cerr << "Could not resolve external global address: "
383 << I->getName() << "\n";
388 // Now that all of the globals are set up in memory, loop through them all and
389 // initialize their contents.
390 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
392 if (!I->isExternal())
393 InitializeMemory(I->getInitializer(), GlobalAddress[I]);