1 //===-- ExecutionEngine.cpp - Common Implementation shared by EE's --------===//
3 // This file defines the common interface used by the various execution engine
6 //===----------------------------------------------------------------------===//
8 #define DEBUG_TYPE "jit"
9 #include "ExecutionEngine.h"
10 #include "GenericValue.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Module.h"
14 #include "llvm/Target/TargetData.h"
15 #include "Support/Debug.h"
16 #include "Support/Statistic.h"
17 #include "Config/dlfcn.h"
19 #include "Interpreter/Interpreter.h"
21 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized");
23 ExecutionEngine::~ExecutionEngine() {
27 ExecutionEngine *ExecutionEngine::create (Module *M, bool ForceInterpreter,
29 ExecutionEngine *EE = 0;
31 // If there is nothing that is forcing us to use the interpreter, make a JIT.
32 if (!ForceInterpreter && !TraceMode)
35 // If we can't make a JIT, make an interpreter instead.
37 EE = Interpreter::create(M, TraceMode);
41 // getPointerToGlobal - This returns the address of the specified global
42 // value. This may involve code generation if it's a function.
44 void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
45 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
46 return getPointerToFunction(F);
48 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?");
49 return GlobalAddress[GV];
52 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
55 if (ConstantExpr *CE = const_cast<ConstantExpr*>(dyn_cast<ConstantExpr>(C))) {
56 switch (CE->getOpcode()) {
57 case Instruction::GetElementPtr: {
58 Result = getConstantValue(CE->getOperand(0));
59 std::vector<Value*> Indexes(CE->op_begin()+1, CE->op_end());
61 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes);
63 Result.LongVal += Offset;
66 case Instruction::Cast: {
67 // We only need to handle a few cases here. Almost all casts will
68 // automatically fold, just the ones involving pointers won't.
70 Constant *Op = CE->getOperand(0);
72 // Handle cast of pointer to pointer...
73 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID())
74 return getConstantValue(Op);
76 // Handle a cast of pointer to any integral type...
77 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral())
78 return getConstantValue(Op);
80 // Handle cast of long to pointer...
81 if (isa<PointerType>(C->getType()) && (Op->getType() == Type::LongTy ||
82 Op->getType() == Type::ULongTy))
83 return getConstantValue(Op);
87 case Instruction::Add:
88 if (CE->getOperand(0)->getType() == Type::LongTy ||
89 CE->getOperand(0)->getType() == Type::ULongTy)
90 Result.LongVal = getConstantValue(CE->getOperand(0)).LongVal +
91 getConstantValue(CE->getOperand(1)).LongVal;
99 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n";
103 switch (C->getType()->getPrimitiveID()) {
104 #define GET_CONST_VAL(TY, CLASS) \
105 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(C)->getValue(); break
106 GET_CONST_VAL(Bool , ConstantBool);
107 GET_CONST_VAL(UByte , ConstantUInt);
108 GET_CONST_VAL(SByte , ConstantSInt);
109 GET_CONST_VAL(UShort , ConstantUInt);
110 GET_CONST_VAL(Short , ConstantSInt);
111 GET_CONST_VAL(UInt , ConstantUInt);
112 GET_CONST_VAL(Int , ConstantSInt);
113 GET_CONST_VAL(ULong , ConstantUInt);
114 GET_CONST_VAL(Long , ConstantSInt);
115 GET_CONST_VAL(Float , ConstantFP);
116 GET_CONST_VAL(Double , ConstantFP);
118 case Type::PointerTyID:
119 if (isa<ConstantPointerNull>(C)) {
120 Result.PointerVal = 0;
121 } else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C)){
122 Result = PTOGV(getPointerToGlobal(CPR->getValue()));
125 assert(0 && "Unknown constant pointer type!");
129 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n";
135 void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr,
137 if (getTargetData().isLittleEndian()) {
138 switch (Ty->getPrimitiveID()) {
140 case Type::UByteTyID:
141 case Type::SByteTyID: Ptr->Untyped[0] = Val.UByteVal; break;
142 case Type::UShortTyID:
143 case Type::ShortTyID: Ptr->Untyped[0] = Val.UShortVal & 255;
144 Ptr->Untyped[1] = (Val.UShortVal >> 8) & 255;
146 Store4BytesLittleEndian:
147 case Type::FloatTyID:
149 case Type::IntTyID: Ptr->Untyped[0] = Val.UIntVal & 255;
150 Ptr->Untyped[1] = (Val.UIntVal >> 8) & 255;
151 Ptr->Untyped[2] = (Val.UIntVal >> 16) & 255;
152 Ptr->Untyped[3] = (Val.UIntVal >> 24) & 255;
154 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
155 goto Store4BytesLittleEndian;
156 case Type::DoubleTyID:
157 case Type::ULongTyID:
158 case Type::LongTyID: Ptr->Untyped[0] = Val.ULongVal & 255;
159 Ptr->Untyped[1] = (Val.ULongVal >> 8) & 255;
160 Ptr->Untyped[2] = (Val.ULongVal >> 16) & 255;
161 Ptr->Untyped[3] = (Val.ULongVal >> 24) & 255;
162 Ptr->Untyped[4] = (Val.ULongVal >> 32) & 255;
163 Ptr->Untyped[5] = (Val.ULongVal >> 40) & 255;
164 Ptr->Untyped[6] = (Val.ULongVal >> 48) & 255;
165 Ptr->Untyped[7] = (Val.ULongVal >> 56) & 255;
168 std::cout << "Cannot store value of type " << Ty << "!\n";
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[1] = Val.UShortVal & 255;
177 Ptr->Untyped[0] = (Val.UShortVal >> 8) & 255;
179 Store4BytesBigEndian:
180 case Type::FloatTyID:
182 case Type::IntTyID: Ptr->Untyped[3] = Val.UIntVal & 255;
183 Ptr->Untyped[2] = (Val.UIntVal >> 8) & 255;
184 Ptr->Untyped[1] = (Val.UIntVal >> 16) & 255;
185 Ptr->Untyped[0] = (Val.UIntVal >> 24) & 255;
187 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
188 goto Store4BytesBigEndian;
189 case Type::DoubleTyID:
190 case Type::ULongTyID:
191 case Type::LongTyID: Ptr->Untyped[7] = Val.ULongVal & 255;
192 Ptr->Untyped[6] = (Val.ULongVal >> 8) & 255;
193 Ptr->Untyped[5] = (Val.ULongVal >> 16) & 255;
194 Ptr->Untyped[4] = (Val.ULongVal >> 24) & 255;
195 Ptr->Untyped[3] = (Val.ULongVal >> 32) & 255;
196 Ptr->Untyped[2] = (Val.ULongVal >> 40) & 255;
197 Ptr->Untyped[1] = (Val.ULongVal >> 48) & 255;
198 Ptr->Untyped[0] = (Val.ULongVal >> 56) & 255;
201 std::cout << "Cannot store value of type " << Ty << "!\n";
206 GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr,
209 if (getTargetData().isLittleEndian()) {
210 switch (Ty->getPrimitiveID()) {
212 case Type::UByteTyID:
213 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
214 case Type::UShortTyID:
215 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
216 ((unsigned)Ptr->Untyped[1] << 8);
218 Load4BytesLittleEndian:
219 case Type::FloatTyID:
221 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
222 ((unsigned)Ptr->Untyped[1] << 8) |
223 ((unsigned)Ptr->Untyped[2] << 16) |
224 ((unsigned)Ptr->Untyped[3] << 24);
226 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
227 goto Load4BytesLittleEndian;
228 case Type::DoubleTyID:
229 case Type::ULongTyID:
230 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
231 ((uint64_t)Ptr->Untyped[1] << 8) |
232 ((uint64_t)Ptr->Untyped[2] << 16) |
233 ((uint64_t)Ptr->Untyped[3] << 24) |
234 ((uint64_t)Ptr->Untyped[4] << 32) |
235 ((uint64_t)Ptr->Untyped[5] << 40) |
236 ((uint64_t)Ptr->Untyped[6] << 48) |
237 ((uint64_t)Ptr->Untyped[7] << 56);
240 std::cout << "Cannot load value of type " << *Ty << "!\n";
244 switch (Ty->getPrimitiveID()) {
246 case Type::UByteTyID:
247 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
248 case Type::UShortTyID:
249 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
250 ((unsigned)Ptr->Untyped[0] << 8);
253 case Type::FloatTyID:
255 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
256 ((unsigned)Ptr->Untyped[2] << 8) |
257 ((unsigned)Ptr->Untyped[1] << 16) |
258 ((unsigned)Ptr->Untyped[0] << 24);
260 case Type::PointerTyID: if (getTargetData().getPointerSize() == 4)
261 goto Load4BytesBigEndian;
262 case Type::DoubleTyID:
263 case Type::ULongTyID:
264 case Type::LongTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
265 ((uint64_t)Ptr->Untyped[6] << 8) |
266 ((uint64_t)Ptr->Untyped[5] << 16) |
267 ((uint64_t)Ptr->Untyped[4] << 24) |
268 ((uint64_t)Ptr->Untyped[3] << 32) |
269 ((uint64_t)Ptr->Untyped[2] << 40) |
270 ((uint64_t)Ptr->Untyped[1] << 48) |
271 ((uint64_t)Ptr->Untyped[0] << 56);
274 std::cout << "Cannot load value of type " << *Ty << "!\n";
281 // InitializeMemory - Recursive function to apply a Constant value into the
282 // specified memory location...
284 void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) {
285 if (Init->getType()->isFirstClassType()) {
286 GenericValue Val = getConstantValue(Init);
287 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType());
291 switch (Init->getType()->getPrimitiveID()) {
292 case Type::ArrayTyID: {
293 const ConstantArray *CPA = cast<ConstantArray>(Init);
294 const std::vector<Use> &Val = CPA->getValues();
295 unsigned ElementSize =
296 getTargetData().getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
297 for (unsigned i = 0; i < Val.size(); ++i)
298 InitializeMemory(cast<Constant>(Val[i].get()), (char*)Addr+i*ElementSize);
302 case Type::StructTyID: {
303 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
304 const StructLayout *SL =
305 getTargetData().getStructLayout(cast<StructType>(CPS->getType()));
306 const std::vector<Use> &Val = CPS->getValues();
307 for (unsigned i = 0; i < Val.size(); ++i)
308 InitializeMemory(cast<Constant>(Val[i].get()),
309 (char*)Addr+SL->MemberOffsets[i]);
314 std::cerr << "Bad Type: " << Init->getType() << "\n";
315 assert(0 && "Unknown constant type to initialize memory with!");
319 /// EmitGlobals - Emit all of the global variables to memory, storing their
320 /// addresses into GlobalAddress. This must make sure to copy the contents of
321 /// their initializers into the memory.
323 void ExecutionEngine::emitGlobals() {
324 const TargetData &TD = getTargetData();
326 // Loop over all of the global variables in the program, allocating the memory
328 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
330 if (!I->isExternal()) {
331 // Get the type of the global...
332 const Type *Ty = I->getType()->getElementType();
334 // Allocate some memory for it!
335 unsigned Size = TD.getTypeSize(Ty);
336 GlobalAddress[I] = new char[Size];
337 NumInitBytes += Size;
339 DEBUG(std::cerr << "Global '" << I->getName() << "' -> "
340 << (void*)GlobalAddress[I] << "\n");
342 // On Sparc, RTLD_SELF is already defined and it's not zero
343 // Linux/x86 wants to use a 0, other systems may differ
347 // External variable reference, try to use dlsym to get a pointer to it in
349 if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str()))
350 GlobalAddress[I] = SymAddr;
352 std::cerr << "Could not resolve external global address: "
353 << I->getName() << "\n";
358 // Now that all of the globals are set up in memory, loop through them all and
359 // initialize their contents.
360 for (Module::giterator I = getModule().gbegin(), E = getModule().gend();
362 if (!I->isExternal())
363 InitializeMemory(I->getInitializer(), GlobalAddress[I]);