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 21 Statistic<> NumInitBytes("lli", "Number of bytes of global vars initialized"); 22 23 ExecutionEngine::~ExecutionEngine() { 24 delete &CurMod; 25 } 26 27 ExecutionEngine *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 // 44 void *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 52 GenericValue 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 135 void 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 206 GenericValue 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 // 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()); 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 void *ExecutionEngine::CreateArgv(const std::vector<std::string> &InputArgv) { 320 if (getTargetData().getPointerSize() == 8) { // 64 bit target? 321 PointerTy *Result = new PointerTy[InputArgv.size()+1]; 322 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 323 324 for (unsigned i = 0; i < InputArgv.size(); ++i) { 325 unsigned Size = InputArgv[i].size()+1; 326 char *Dest = new char[Size]; 327 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 328 329 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 330 Dest[Size-1] = 0; 331 332 // Endian safe: Result[i] = (PointerTy)Dest; 333 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::LongTy); 334 } 335 Result[InputArgv.size()] = 0; 336 return Result; 337 338 } else { // 32 bit target? 339 int *Result = new int[InputArgv.size()+1]; 340 DEBUG(std::cerr << "ARGV = " << (void*)Result << "\n"); 341 342 for (unsigned i = 0; i < InputArgv.size(); ++i) { 343 unsigned Size = InputArgv[i].size()+1; 344 char *Dest = new char[Size]; 345 DEBUG(std::cerr << "ARGV[" << i << "] = " << (void*)Dest << "\n"); 346 347 copy(InputArgv[i].begin(), InputArgv[i].end(), Dest); 348 Dest[Size-1] = 0; 349 350 // Endian safe: Result[i] = (PointerTy)Dest; 351 StoreValueToMemory(PTOGV(Dest), (GenericValue*)(Result+i), Type::IntTy); 352 } 353 Result[InputArgv.size()] = 0; // null terminate it 354 return Result; 355 } 356 } 357 358 /// EmitGlobals - Emit all of the global variables to memory, storing their 359 /// addresses into GlobalAddress. This must make sure to copy the contents of 360 /// their initializers into the memory. 361 /// 362 void ExecutionEngine::emitGlobals() { 363 const TargetData &TD = getTargetData(); 364 365 // Loop over all of the global variables in the program, allocating the memory 366 // to hold them. 367 for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); 368 I != E; ++I) 369 if (!I->isExternal()) { 370 // Get the type of the global... 371 const Type *Ty = I->getType()->getElementType(); 372 373 // Allocate some memory for it! 374 unsigned Size = TD.getTypeSize(Ty); 375 GlobalAddress[I] = new char[Size]; 376 NumInitBytes += Size; 377 378 DEBUG(std::cerr << "Global '" << I->getName() << "' -> " 379 << (void*)GlobalAddress[I] << "\n"); 380 } else { 381 // On Sparc, RTLD_SELF is already defined and it's not zero 382 // Linux/x86 wants to use a 0, other systems may differ 383 #ifndef RTLD_SELF 384 #define RTLD_SELF 0 385 #endif 386 // External variable reference, try to use dlsym to get a pointer to it in 387 // the LLI image. 388 if (void *SymAddr = dlsym(RTLD_SELF, I->getName().c_str())) 389 GlobalAddress[I] = SymAddr; 390 else { 391 std::cerr << "Could not resolve external global address: " 392 << I->getName() << "\n"; 393 abort(); 394 } 395 } 396 397 // Now that all of the globals are set up in memory, loop through them all and 398 // initialize their contents. 399 for (Module::giterator I = getModule().gbegin(), E = getModule().gend(); 400 I != E; ++I) 401 if (!I->isExternal()) 402 InitializeMemory(I->getInitializer(), GlobalAddress[I]); 403 } 404 405