ExecutionEngine.cpp revision 70975eef572b9e132bbaade16ba9edb76f15f287
1//===-- ExecutionEngine.cpp - Common Implementation shared by EE's --------===// 2// 3// This file defines the common interface used by the various execution engine 4// subclasses. 5// 6//===----------------------------------------------------------------------===// 7 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" 18#include "JIT/VM.h" 19#include "Interpreter/Interpreter.h" 20 21Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); 22 23ExecutionEngine::~ExecutionEngine() { 24 delete &CurMod; 25} 26 27ExecutionEngine *ExecutionEngine::create (Module *M, bool ForceInterpreter, 28 bool TraceMode) { 29 ExecutionEngine *EE = 0; 30 31 // If there is nothing that is forcing us to use the interpreter, make a JIT. 32 if (!ForceInterpreter && !TraceMode) 33 EE = VM::create(M); 34 35 // If we can't make a JIT, make an interpreter instead. 36 if (EE == 0) 37 EE = Interpreter::create(M, TraceMode); 38 return EE; 39} 40 41// getPointerToGlobal - This returns the address of the specified global 42// value. This may involve code generation if it's a function. 43// 44void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) { 45 if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV))) 46 return getPointerToFunction(F); 47 48 assert(GlobalAddress[GV] && "Global hasn't had an address allocated yet?"); 49 return GlobalAddress[GV]; 50} 51 52GenericValue ExecutionEngine::getConstantValue(const Constant *C) { 53 GenericValue Result; 54 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()); 60 uint64_t Offset = 61 TD->getIndexedOffset(CE->getOperand(0)->getType(), Indexes); 62 63 Result.LongVal += Offset; 64 return Result; 65 } 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. 69 // 70 Constant *Op = CE->getOperand(0); 71 72 // Handle cast of pointer to pointer... 73 if (Op->getType()->getPrimitiveID() == C->getType()->getPrimitiveID()) 74 return getConstantValue(Op); 75 76 // Handle a cast of pointer to any integral type... 77 if (isa<PointerType>(Op->getType()) && C->getType()->isIntegral()) 78 return getConstantValue(Op); 79 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); 84 break; 85 } 86 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; 92 else 93 break; 94 return Result; 95 96 default: 97 break; 98 } 99 std::cerr << "ConstantExpr not handled as global var init: " << *CE << "\n"; 100 abort(); 101 } 102 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); 117#undef GET_CONST_VAL 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())); 123 124 } else { 125 assert(0 && "Unknown constant pointer type!"); 126 } 127 break; 128 default: 129 std::cout << "ERROR: Constant unimp for type: " << C->getType() << "\n"; 130 abort(); 131 } 132 return Result; 133} 134 135void ExecutionEngine::StoreValueToMemory(GenericValue Val, GenericValue *Ptr, 136 const Type *Ty) { 137 if (getTargetData().isLittleEndian()) { 138 switch (Ty->getPrimitiveID()) { 139 case Type::BoolTyID: 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; 145 break; 146 Store4BytesLittleEndian: 147 case Type::FloatTyID: 148 case Type::UIntTyID: 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; 153 break; 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; 166 break; 167 default: 168 std::cout << "Cannot store value of type " << Ty << "!\n"; 169 } 170 } else { 171 switch (Ty->getPrimitiveID()) { 172 case Type::BoolTyID: 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; 178 break; 179 Store4BytesBigEndian: 180 case Type::FloatTyID: 181 case Type::UIntTyID: 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; 186 break; 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; 199 break; 200 default: 201 std::cout << "Cannot store value of type " << Ty << "!\n"; 202 } 203 } 204} 205 206GenericValue ExecutionEngine::LoadValueFromMemory(GenericValue *Ptr, 207 const Type *Ty) { 208 GenericValue Result; 209 if (getTargetData().isLittleEndian()) { 210 switch (Ty->getPrimitiveID()) { 211 case Type::BoolTyID: 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); 217 break; 218 Load4BytesLittleEndian: 219 case Type::FloatTyID: 220 case Type::UIntTyID: 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); 225 break; 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); 238 break; 239 default: 240 std::cout << "Cannot load value of type " << *Ty << "!\n"; 241 abort(); 242 } 243 } else { 244 switch (Ty->getPrimitiveID()) { 245 case Type::BoolTyID: 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); 251 break; 252 Load4BytesBigEndian: 253 case Type::FloatTyID: 254 case Type::UIntTyID: 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); 259 break; 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); 272 break; 273 default: 274 std::cout << "Cannot load value of type " << *Ty << "!\n"; 275 abort(); 276 } 277 } 278 return Result; 279} 280 281// InitializeMemory - Recursive function to apply a Constant value into the 282// specified memory location... 283// 284void ExecutionEngine::InitializeMemory(const Constant *Init, void *Addr) { 285 if (Init->getType()->isFirstClassType()) { 286 GenericValue Val = getConstantValue(Init); 287 StoreValueToMemory(Val, (GenericValue*)Addr, Init->getType()); 288 return; 289 } 290 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); 299 return; 300 } 301 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]); 310 return; 311 } 312 313 default: 314 std::cerr << "Bad Type: " << Init->getType() << "\n"; 315 assert(0 && "Unknown constant type to initialize memory with!"); 316 } 317} 318 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. 322/// 323void ExecutionEngine::emitGlobals() { 324 const TargetData &TD = getTargetData(); 325 326 // Loop over all of the global variables in the program, allocating the memory 327 // to hold them. 328 for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); 329 I != E; ++I) 330 if (!I->isExternal()) { 331 // Get the type of the global... 332 const Type *Ty = I->getType()->getElementType(); 333 334 // Allocate some memory for it! 335 unsigned Size = TD.getTypeSize(Ty); 336 GlobalAddress[I] = new char[Size]; 337 NumInitBytes += Size; 338 339 DEBUG(std::cerr << "Global '" << I->getName() << "' -> " 340 << (void*)GlobalAddress[I] << "\n"); 341 } else { 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 344#ifndef RTLD_SELF 345#define RTLD_SELF 0 346#endif 347 // External variable reference, try to use dlsym to get a pointer to it in 348 // the LLI image. 349 if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str())) 350 GlobalAddress[I] = SymAddr; 351 else { 352 std::cerr << "Could not resolve external global address: " 353 << I->getName() << "\n"; 354 abort(); 355 } 356 } 357 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(); 361 I != E; ++I) 362 if (!I->isExternal()) 363 InitializeMemory(I->getInitializer(), GlobalAddress[I]); 364} 365 366