1 //===-- Execution.cpp - Implement code to simulate the program ------------===// 2 // 3 // This file contains the actual instruction interpreter. 4 // 5 //===----------------------------------------------------------------------===// 6 7 #include "Interpreter.h" 8 #include "ExecutionAnnotations.h" 9 #include "llvm/GlobalVariable.h" 10 #include "llvm/Function.h" 11 #include "llvm/iPHINode.h" 12 #include "llvm/iOther.h" 13 #include "llvm/iTerminators.h" 14 #include "llvm/iMemory.h" 15 #include "llvm/DerivedTypes.h" 16 #include "llvm/Constants.h" 17 #include "llvm/Assembly/Writer.h" 18 #include "Support/CommandLine.h" 19 #include "Support/Statistic.h" 20 #include <math.h> // For fmod 21 #include <signal.h> 22 #include <setjmp.h> 23 using std::vector; 24 using std::cout; 25 using std::cerr; 26 27 Interpreter *TheEE = 0; 28 29 namespace { 30 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed"); 31 32 cl::opt<bool> 33 QuietMode("quiet", cl::desc("Do not emit any non-program output"), 34 cl::init(true)); 35 36 cl::alias 37 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode)); 38 39 cl::opt<bool> 40 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks")); 41 42 cl::opt<bool> 43 AbortOnExceptions("abort-on-exception", 44 cl::desc("Halt execution on a machine exception")); 45 } 46 47 // Create a TargetData structure to handle memory addressing and size/alignment 48 // computations 49 // 50 CachedWriter CW; // Object to accelerate printing of LLVM 51 52 #ifdef PROFILE_STRUCTURE_FIELDS 53 static cl::opt<bool> 54 ProfileStructureFields("profilestructfields", 55 cl::desc("Profile Structure Field Accesses")); 56 #include <map> 57 static std::map<const StructType *, vector<unsigned> > FieldAccessCounts; 58 #endif 59 60 sigjmp_buf SignalRecoverBuffer; 61 static bool InInstruction = false; 62 63 extern "C" { 64 static void SigHandler(int Signal) { 65 if (InInstruction) 66 siglongjmp(SignalRecoverBuffer, Signal); 67 } 68 } 69 70 static void initializeSignalHandlers() { 71 struct sigaction Action; 72 Action.sa_handler = SigHandler; 73 Action.sa_flags = SA_SIGINFO; 74 sigemptyset(&Action.sa_mask); 75 sigaction(SIGSEGV, &Action, 0); 76 sigaction(SIGBUS, &Action, 0); 77 sigaction(SIGINT, &Action, 0); 78 sigaction(SIGFPE, &Action, 0); 79 } 80 81 82 //===----------------------------------------------------------------------===// 83 // Value Manipulation code 84 //===----------------------------------------------------------------------===// 85 86 static unsigned getOperandSlot(Value *V) { 87 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID); 88 assert(SN && "Operand does not have a slot number annotation!"); 89 return SN->SlotNum; 90 } 91 92 // Operations used by constant expr implementations... 93 static GenericValue executeCastOperation(Value *Src, const Type *DestTy, 94 ExecutionContext &SF); 95 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, 96 const Type *Ty, ExecutionContext &SF); 97 98 99 static GenericValue getOperandValue(Value *V, ExecutionContext &SF) { 100 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 101 switch (CE->getOpcode()) { 102 case Instruction::Cast: 103 return executeCastOperation(CE->getOperand(0), CE->getType(), SF); 104 case Instruction::GetElementPtr: 105 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1, 106 CE->op_end(), SF); 107 case Instruction::Add: 108 return executeAddInst(getOperandValue(CE->getOperand(0), SF), 109 getOperandValue(CE->getOperand(1), SF), 110 CE->getType(), SF); 111 default: 112 cerr << "Unhandled ConstantExpr: " << CE << "\n"; 113 abort(); 114 { GenericValue V; return V; } 115 } 116 } else if (Constant *CPV = dyn_cast<Constant>(V)) { 117 return TheEE->getConstantValue(CPV); 118 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 119 return PTOGV(TheEE->getPointerToGlobal(GV)); 120 } else { 121 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value 122 unsigned OpSlot = getOperandSlot(V); 123 assert(TyP < SF.Values.size() && 124 OpSlot < SF.Values[TyP].size() && "Value out of range!"); 125 return SF.Values[TyP][getOperandSlot(V)]; 126 } 127 } 128 129 static void printOperandInfo(Value *V, ExecutionContext &SF) { 130 if (isa<Constant>(V)) { 131 cout << "Constant Pool Value\n"; 132 } else if (isa<GlobalValue>(V)) { 133 cout << "Global Value\n"; 134 } else { 135 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value 136 unsigned Slot = getOperandSlot(V); 137 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot 138 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF 139 << " Contents=0x"; 140 141 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot]; 142 for (unsigned i = 0; i < sizeof(GenericValue); ++i) { 143 unsigned char Cur = Buf[i]; 144 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0')) 145 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0')); 146 } 147 cout << "\n"; 148 } 149 } 150 151 152 153 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { 154 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value 155 156 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n"; 157 SF.Values[TyP][getOperandSlot(V)] = Val; 158 } 159 160 161 //===----------------------------------------------------------------------===// 162 // Annotation Wrangling code 163 //===----------------------------------------------------------------------===// 164 165 void Interpreter::initializeExecutionEngine() { 166 TheEE = this; 167 AnnotationManager::registerAnnotationFactory(MethodInfoAID, 168 &MethodInfo::Create); 169 initializeSignalHandlers(); 170 } 171 172 //===----------------------------------------------------------------------===// 173 // Binary Instruction Implementations 174 //===----------------------------------------------------------------------===// 175 176 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ 177 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break 178 179 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, 180 const Type *Ty, ExecutionContext &SF) { 181 GenericValue Dest; 182 switch (Ty->getPrimitiveID()) { 183 IMPLEMENT_BINARY_OPERATOR(+, UByte); 184 IMPLEMENT_BINARY_OPERATOR(+, SByte); 185 IMPLEMENT_BINARY_OPERATOR(+, UShort); 186 IMPLEMENT_BINARY_OPERATOR(+, Short); 187 IMPLEMENT_BINARY_OPERATOR(+, UInt); 188 IMPLEMENT_BINARY_OPERATOR(+, Int); 189 IMPLEMENT_BINARY_OPERATOR(+, ULong); 190 IMPLEMENT_BINARY_OPERATOR(+, Long); 191 IMPLEMENT_BINARY_OPERATOR(+, Float); 192 IMPLEMENT_BINARY_OPERATOR(+, Double); 193 IMPLEMENT_BINARY_OPERATOR(+, Pointer); 194 default: 195 cout << "Unhandled type for Add instruction: " << Ty << "\n"; 196 } 197 return Dest; 198 } 199 200 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, 201 const Type *Ty, ExecutionContext &SF) { 202 GenericValue Dest; 203 switch (Ty->getPrimitiveID()) { 204 IMPLEMENT_BINARY_OPERATOR(-, UByte); 205 IMPLEMENT_BINARY_OPERATOR(-, SByte); 206 IMPLEMENT_BINARY_OPERATOR(-, UShort); 207 IMPLEMENT_BINARY_OPERATOR(-, Short); 208 IMPLEMENT_BINARY_OPERATOR(-, UInt); 209 IMPLEMENT_BINARY_OPERATOR(-, Int); 210 IMPLEMENT_BINARY_OPERATOR(-, ULong); 211 IMPLEMENT_BINARY_OPERATOR(-, Long); 212 IMPLEMENT_BINARY_OPERATOR(-, Float); 213 IMPLEMENT_BINARY_OPERATOR(-, Double); 214 IMPLEMENT_BINARY_OPERATOR(-, Pointer); 215 default: 216 cout << "Unhandled type for Sub instruction: " << Ty << "\n"; 217 } 218 return Dest; 219 } 220 221 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, 222 const Type *Ty, ExecutionContext &SF) { 223 GenericValue Dest; 224 switch (Ty->getPrimitiveID()) { 225 IMPLEMENT_BINARY_OPERATOR(*, UByte); 226 IMPLEMENT_BINARY_OPERATOR(*, SByte); 227 IMPLEMENT_BINARY_OPERATOR(*, UShort); 228 IMPLEMENT_BINARY_OPERATOR(*, Short); 229 IMPLEMENT_BINARY_OPERATOR(*, UInt); 230 IMPLEMENT_BINARY_OPERATOR(*, Int); 231 IMPLEMENT_BINARY_OPERATOR(*, ULong); 232 IMPLEMENT_BINARY_OPERATOR(*, Long); 233 IMPLEMENT_BINARY_OPERATOR(*, Float); 234 IMPLEMENT_BINARY_OPERATOR(*, Double); 235 IMPLEMENT_BINARY_OPERATOR(*, Pointer); 236 default: 237 cout << "Unhandled type for Mul instruction: " << Ty << "\n"; 238 } 239 return Dest; 240 } 241 242 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2, 243 const Type *Ty, ExecutionContext &SF) { 244 GenericValue Dest; 245 switch (Ty->getPrimitiveID()) { 246 IMPLEMENT_BINARY_OPERATOR(/, UByte); 247 IMPLEMENT_BINARY_OPERATOR(/, SByte); 248 IMPLEMENT_BINARY_OPERATOR(/, UShort); 249 IMPLEMENT_BINARY_OPERATOR(/, Short); 250 IMPLEMENT_BINARY_OPERATOR(/, UInt); 251 IMPLEMENT_BINARY_OPERATOR(/, Int); 252 IMPLEMENT_BINARY_OPERATOR(/, ULong); 253 IMPLEMENT_BINARY_OPERATOR(/, Long); 254 IMPLEMENT_BINARY_OPERATOR(/, Float); 255 IMPLEMENT_BINARY_OPERATOR(/, Double); 256 IMPLEMENT_BINARY_OPERATOR(/, Pointer); 257 default: 258 cout << "Unhandled type for Div instruction: " << Ty << "\n"; 259 } 260 return Dest; 261 } 262 263 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2, 264 const Type *Ty, ExecutionContext &SF) { 265 GenericValue Dest; 266 switch (Ty->getPrimitiveID()) { 267 IMPLEMENT_BINARY_OPERATOR(%, UByte); 268 IMPLEMENT_BINARY_OPERATOR(%, SByte); 269 IMPLEMENT_BINARY_OPERATOR(%, UShort); 270 IMPLEMENT_BINARY_OPERATOR(%, Short); 271 IMPLEMENT_BINARY_OPERATOR(%, UInt); 272 IMPLEMENT_BINARY_OPERATOR(%, Int); 273 IMPLEMENT_BINARY_OPERATOR(%, ULong); 274 IMPLEMENT_BINARY_OPERATOR(%, Long); 275 IMPLEMENT_BINARY_OPERATOR(%, Pointer); 276 case Type::FloatTyID: 277 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); 278 break; 279 case Type::DoubleTyID: 280 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); 281 break; 282 default: 283 cout << "Unhandled type for Rem instruction: " << Ty << "\n"; 284 } 285 return Dest; 286 } 287 288 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, 289 const Type *Ty, ExecutionContext &SF) { 290 GenericValue Dest; 291 switch (Ty->getPrimitiveID()) { 292 IMPLEMENT_BINARY_OPERATOR(&, UByte); 293 IMPLEMENT_BINARY_OPERATOR(&, SByte); 294 IMPLEMENT_BINARY_OPERATOR(&, UShort); 295 IMPLEMENT_BINARY_OPERATOR(&, Short); 296 IMPLEMENT_BINARY_OPERATOR(&, UInt); 297 IMPLEMENT_BINARY_OPERATOR(&, Int); 298 IMPLEMENT_BINARY_OPERATOR(&, ULong); 299 IMPLEMENT_BINARY_OPERATOR(&, Long); 300 IMPLEMENT_BINARY_OPERATOR(&, Pointer); 301 default: 302 cout << "Unhandled type for And instruction: " << Ty << "\n"; 303 } 304 return Dest; 305 } 306 307 308 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, 309 const Type *Ty, ExecutionContext &SF) { 310 GenericValue Dest; 311 switch (Ty->getPrimitiveID()) { 312 IMPLEMENT_BINARY_OPERATOR(|, UByte); 313 IMPLEMENT_BINARY_OPERATOR(|, SByte); 314 IMPLEMENT_BINARY_OPERATOR(|, UShort); 315 IMPLEMENT_BINARY_OPERATOR(|, Short); 316 IMPLEMENT_BINARY_OPERATOR(|, UInt); 317 IMPLEMENT_BINARY_OPERATOR(|, Int); 318 IMPLEMENT_BINARY_OPERATOR(|, ULong); 319 IMPLEMENT_BINARY_OPERATOR(|, Long); 320 IMPLEMENT_BINARY_OPERATOR(|, Pointer); 321 default: 322 cout << "Unhandled type for Or instruction: " << Ty << "\n"; 323 } 324 return Dest; 325 } 326 327 328 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2, 329 const Type *Ty, ExecutionContext &SF) { 330 GenericValue Dest; 331 switch (Ty->getPrimitiveID()) { 332 IMPLEMENT_BINARY_OPERATOR(^, UByte); 333 IMPLEMENT_BINARY_OPERATOR(^, SByte); 334 IMPLEMENT_BINARY_OPERATOR(^, UShort); 335 IMPLEMENT_BINARY_OPERATOR(^, Short); 336 IMPLEMENT_BINARY_OPERATOR(^, UInt); 337 IMPLEMENT_BINARY_OPERATOR(^, Int); 338 IMPLEMENT_BINARY_OPERATOR(^, ULong); 339 IMPLEMENT_BINARY_OPERATOR(^, Long); 340 IMPLEMENT_BINARY_OPERATOR(^, Pointer); 341 default: 342 cout << "Unhandled type for Xor instruction: " << Ty << "\n"; 343 } 344 return Dest; 345 } 346 347 348 #define IMPLEMENT_SETCC(OP, TY) \ 349 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break 350 351 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2, 352 const Type *Ty, ExecutionContext &SF) { 353 GenericValue Dest; 354 switch (Ty->getPrimitiveID()) { 355 IMPLEMENT_SETCC(==, UByte); 356 IMPLEMENT_SETCC(==, SByte); 357 IMPLEMENT_SETCC(==, UShort); 358 IMPLEMENT_SETCC(==, Short); 359 IMPLEMENT_SETCC(==, UInt); 360 IMPLEMENT_SETCC(==, Int); 361 IMPLEMENT_SETCC(==, ULong); 362 IMPLEMENT_SETCC(==, Long); 363 IMPLEMENT_SETCC(==, Float); 364 IMPLEMENT_SETCC(==, Double); 365 IMPLEMENT_SETCC(==, Pointer); 366 default: 367 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n"; 368 } 369 return Dest; 370 } 371 372 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2, 373 const Type *Ty, ExecutionContext &SF) { 374 GenericValue Dest; 375 switch (Ty->getPrimitiveID()) { 376 IMPLEMENT_SETCC(!=, UByte); 377 IMPLEMENT_SETCC(!=, SByte); 378 IMPLEMENT_SETCC(!=, UShort); 379 IMPLEMENT_SETCC(!=, Short); 380 IMPLEMENT_SETCC(!=, UInt); 381 IMPLEMENT_SETCC(!=, Int); 382 IMPLEMENT_SETCC(!=, ULong); 383 IMPLEMENT_SETCC(!=, Long); 384 IMPLEMENT_SETCC(!=, Float); 385 IMPLEMENT_SETCC(!=, Double); 386 IMPLEMENT_SETCC(!=, Pointer); 387 388 default: 389 cout << "Unhandled type for SetNE instruction: " << Ty << "\n"; 390 } 391 return Dest; 392 } 393 394 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2, 395 const Type *Ty, ExecutionContext &SF) { 396 GenericValue Dest; 397 switch (Ty->getPrimitiveID()) { 398 IMPLEMENT_SETCC(<=, UByte); 399 IMPLEMENT_SETCC(<=, SByte); 400 IMPLEMENT_SETCC(<=, UShort); 401 IMPLEMENT_SETCC(<=, Short); 402 IMPLEMENT_SETCC(<=, UInt); 403 IMPLEMENT_SETCC(<=, Int); 404 IMPLEMENT_SETCC(<=, ULong); 405 IMPLEMENT_SETCC(<=, Long); 406 IMPLEMENT_SETCC(<=, Float); 407 IMPLEMENT_SETCC(<=, Double); 408 IMPLEMENT_SETCC(<=, Pointer); 409 default: 410 cout << "Unhandled type for SetLE instruction: " << Ty << "\n"; 411 } 412 return Dest; 413 } 414 415 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2, 416 const Type *Ty, ExecutionContext &SF) { 417 GenericValue Dest; 418 switch (Ty->getPrimitiveID()) { 419 IMPLEMENT_SETCC(>=, UByte); 420 IMPLEMENT_SETCC(>=, SByte); 421 IMPLEMENT_SETCC(>=, UShort); 422 IMPLEMENT_SETCC(>=, Short); 423 IMPLEMENT_SETCC(>=, UInt); 424 IMPLEMENT_SETCC(>=, Int); 425 IMPLEMENT_SETCC(>=, ULong); 426 IMPLEMENT_SETCC(>=, Long); 427 IMPLEMENT_SETCC(>=, Float); 428 IMPLEMENT_SETCC(>=, Double); 429 IMPLEMENT_SETCC(>=, Pointer); 430 default: 431 cout << "Unhandled type for SetGE instruction: " << Ty << "\n"; 432 } 433 return Dest; 434 } 435 436 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2, 437 const Type *Ty, ExecutionContext &SF) { 438 GenericValue Dest; 439 switch (Ty->getPrimitiveID()) { 440 IMPLEMENT_SETCC(<, UByte); 441 IMPLEMENT_SETCC(<, SByte); 442 IMPLEMENT_SETCC(<, UShort); 443 IMPLEMENT_SETCC(<, Short); 444 IMPLEMENT_SETCC(<, UInt); 445 IMPLEMENT_SETCC(<, Int); 446 IMPLEMENT_SETCC(<, ULong); 447 IMPLEMENT_SETCC(<, Long); 448 IMPLEMENT_SETCC(<, Float); 449 IMPLEMENT_SETCC(<, Double); 450 IMPLEMENT_SETCC(<, Pointer); 451 default: 452 cout << "Unhandled type for SetLT instruction: " << Ty << "\n"; 453 } 454 return Dest; 455 } 456 457 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2, 458 const Type *Ty, ExecutionContext &SF) { 459 GenericValue Dest; 460 switch (Ty->getPrimitiveID()) { 461 IMPLEMENT_SETCC(>, UByte); 462 IMPLEMENT_SETCC(>, SByte); 463 IMPLEMENT_SETCC(>, UShort); 464 IMPLEMENT_SETCC(>, Short); 465 IMPLEMENT_SETCC(>, UInt); 466 IMPLEMENT_SETCC(>, Int); 467 IMPLEMENT_SETCC(>, ULong); 468 IMPLEMENT_SETCC(>, Long); 469 IMPLEMENT_SETCC(>, Float); 470 IMPLEMENT_SETCC(>, Double); 471 IMPLEMENT_SETCC(>, Pointer); 472 default: 473 cout << "Unhandled type for SetGT instruction: " << Ty << "\n"; 474 } 475 return Dest; 476 } 477 478 static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) { 479 const Type *Ty = I.getOperand(0)->getType(); 480 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 481 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 482 GenericValue R; // Result 483 484 switch (I.getOpcode()) { 485 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break; 486 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break; 487 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break; 488 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break; 489 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break; 490 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break; 491 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break; 492 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break; 493 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break; 494 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break; 495 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break; 496 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break; 497 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break; 498 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break; 499 default: 500 cout << "Don't know how to handle this binary operator!\n-->" << I; 501 R = Src1; 502 } 503 504 SetValue(&I, R, SF); 505 } 506 507 //===----------------------------------------------------------------------===// 508 // Terminator Instruction Implementations 509 //===----------------------------------------------------------------------===// 510 511 static void PerformExitStuff() { 512 #ifdef PROFILE_STRUCTURE_FIELDS 513 // Print out structure field accounting information... 514 if (!FieldAccessCounts.empty()) { 515 CW << "Profile Field Access Counts:\n"; 516 std::map<const StructType *, vector<unsigned> >::iterator 517 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end(); 518 for (; I != E; ++I) { 519 vector<unsigned> &OfC = I->second; 520 CW << " '" << (Value*)I->first << "'\t- Sum="; 521 522 unsigned Sum = 0; 523 for (unsigned i = 0; i < OfC.size(); ++i) 524 Sum += OfC[i]; 525 CW << Sum << " - "; 526 527 for (unsigned i = 0; i < OfC.size(); ++i) { 528 if (i) CW << ", "; 529 CW << OfC[i]; 530 } 531 CW << "\n"; 532 } 533 CW << "\n"; 534 535 CW << "Profile Field Access Percentages:\n"; 536 cout.precision(3); 537 for (I = FieldAccessCounts.begin(); I != E; ++I) { 538 vector<unsigned> &OfC = I->second; 539 unsigned Sum = 0; 540 for (unsigned i = 0; i < OfC.size(); ++i) 541 Sum += OfC[i]; 542 543 CW << " '" << (Value*)I->first << "'\t- "; 544 for (unsigned i = 0; i < OfC.size(); ++i) { 545 if (i) CW << ", "; 546 CW << double(OfC[i])/Sum; 547 } 548 CW << "\n"; 549 } 550 CW << "\n"; 551 552 FieldAccessCounts.clear(); 553 } 554 #endif 555 } 556 557 void Interpreter::exitCalled(GenericValue GV) { 558 if (!QuietMode) { 559 cout << "Program returned "; 560 print(Type::IntTy, GV); 561 cout << " via 'void exit(int)'\n"; 562 } 563 564 ExitCode = GV.SByteVal; 565 ECStack.clear(); 566 PerformExitStuff(); 567 } 568 569 void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) { 570 const Type *RetTy = 0; 571 GenericValue Result; 572 573 // Save away the return value... (if we are not 'ret void') 574 if (I.getNumOperands()) { 575 RetTy = I.getReturnValue()->getType(); 576 Result = getOperandValue(I.getReturnValue(), SF); 577 } 578 579 // Save previously executing meth 580 const Function *M = ECStack.back().CurMethod; 581 582 // Pop the current stack frame... this invalidates SF 583 ECStack.pop_back(); 584 585 if (ECStack.empty()) { // Finished main. Put result into exit code... 586 if (RetTy) { // Nonvoid return type? 587 if (!QuietMode) { 588 CW << "Function " << M->getType() << " \"" << M->getName() 589 << "\" returned "; 590 print(RetTy, Result); 591 cout << "\n"; 592 } 593 594 if (RetTy->isIntegral()) 595 ExitCode = Result.IntVal; // Capture the exit code of the program 596 } else { 597 ExitCode = 0; 598 } 599 600 PerformExitStuff(); 601 return; 602 } 603 604 // If we have a previous stack frame, and we have a previous call, fill in 605 // the return value... 606 // 607 ExecutionContext &NewSF = ECStack.back(); 608 if (NewSF.Caller) { 609 if (NewSF.Caller->getType() != Type::VoidTy) // Save result... 610 SetValue(NewSF.Caller, Result, NewSF); 611 612 NewSF.Caller = 0; // We returned from the call... 613 } else if (!QuietMode) { 614 // This must be a function that is executing because of a user 'call' 615 // instruction. 616 CW << "Function " << M->getType() << " \"" << M->getName() 617 << "\" returned "; 618 print(RetTy, Result); 619 cout << "\n"; 620 } 621 } 622 623 void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) { 624 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work... 625 BasicBlock *Dest; 626 627 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... 628 if (!I.isUnconditional()) { 629 Value *Cond = I.getCondition(); 630 GenericValue CondVal = getOperandValue(Cond, SF); 631 if (CondVal.BoolVal == 0) // If false cond... 632 Dest = I.getSuccessor(1); 633 } 634 SF.CurBB = Dest; // Update CurBB to branch destination 635 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... 636 } 637 638 //===----------------------------------------------------------------------===// 639 // Memory Instruction Implementations 640 //===----------------------------------------------------------------------===// 641 642 void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) { 643 const Type *Ty = I.getType()->getElementType(); // Type to be allocated 644 645 // Get the number of elements being allocated by the array... 646 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal; 647 648 // Allocate enough memory to hold the type... 649 // FIXME: Don't use CALLOC, use a tainted malloc. 650 void *Memory = calloc(NumElements, TD.getTypeSize(Ty)); 651 652 GenericValue Result = PTOGV(Memory); 653 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); 654 SetValue(&I, Result, SF); 655 656 if (I.getOpcode() == Instruction::Alloca) 657 ECStack.back().Allocas.add(Memory); 658 } 659 660 static void executeFreeInst(FreeInst &I, ExecutionContext &SF) { 661 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?"); 662 GenericValue Value = getOperandValue(I.getOperand(0), SF); 663 // TODO: Check to make sure memory is allocated 664 free(GVTOP(Value)); // Free memory 665 } 666 667 668 // getElementOffset - The workhorse for getelementptr. 669 // 670 GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I, 671 User::op_iterator E, 672 ExecutionContext &SF) { 673 assert(isa<PointerType>(Ptr->getType()) && 674 "Cannot getElementOffset of a nonpointer type!"); 675 676 PointerTy Total = 0; 677 const Type *Ty = Ptr->getType(); 678 679 for (; I != E; ++I) { 680 if (const StructType *STy = dyn_cast<StructType>(Ty)) { 681 const StructLayout *SLO = TD.getStructLayout(STy); 682 683 // Indicies must be ubyte constants... 684 const ConstantUInt *CPU = cast<ConstantUInt>(*I); 685 assert(CPU->getType() == Type::UByteTy); 686 unsigned Index = CPU->getValue(); 687 688 #ifdef PROFILE_STRUCTURE_FIELDS 689 if (ProfileStructureFields) { 690 // Do accounting for this field... 691 vector<unsigned> &OfC = FieldAccessCounts[STy]; 692 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size()); 693 OfC[Index]++; 694 } 695 #endif 696 697 Total += SLO->MemberOffsets[Index]; 698 Ty = STy->getElementTypes()[Index]; 699 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) { 700 701 // Get the index number for the array... which must be uint type... 702 assert((*I)->getType() == Type::LongTy); 703 unsigned Idx = getOperandValue(*I, SF).LongVal; 704 if (const ArrayType *AT = dyn_cast<ArrayType>(ST)) 705 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) { 706 cerr << "Out of range memory access to element #" << Idx 707 << " of a " << AT->getNumElements() << " element array." 708 << " Subscript #" << *I << "\n"; 709 // Get outta here!!! 710 siglongjmp(SignalRecoverBuffer, SIGTRAP); 711 } 712 713 Ty = ST->getElementType(); 714 unsigned Size = TD.getTypeSize(Ty); 715 Total += Size*Idx; 716 } 717 } 718 719 GenericValue Result; 720 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total; 721 return Result; 722 } 723 724 static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) { 725 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(), 726 I.idx_begin(), I.idx_end(), SF), SF); 727 } 728 729 void Interpreter::executeLoadInst(LoadInst &I, ExecutionContext &SF) { 730 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); 731 GenericValue *Ptr = (GenericValue*)GVTOP(SRC); 732 GenericValue Result; 733 734 if (TD.isLittleEndian()) { 735 switch (I.getType()->getPrimitiveID()) { 736 case Type::BoolTyID: 737 case Type::UByteTyID: 738 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 739 case Type::UShortTyID: 740 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] | 741 ((unsigned)Ptr->Untyped[1] << 8); 742 break; 743 case Type::FloatTyID: 744 case Type::UIntTyID: 745 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] | 746 ((unsigned)Ptr->Untyped[1] << 8) | 747 ((unsigned)Ptr->Untyped[2] << 16) | 748 ((unsigned)Ptr->Untyped[3] << 24); 749 break; 750 case Type::DoubleTyID: 751 case Type::ULongTyID: 752 case Type::LongTyID: 753 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] | 754 ((uint64_t)Ptr->Untyped[1] << 8) | 755 ((uint64_t)Ptr->Untyped[2] << 16) | 756 ((uint64_t)Ptr->Untyped[3] << 24) | 757 ((uint64_t)Ptr->Untyped[4] << 32) | 758 ((uint64_t)Ptr->Untyped[5] << 40) | 759 ((uint64_t)Ptr->Untyped[6] << 48) | 760 ((uint64_t)Ptr->Untyped[7] << 56); 761 break; 762 default: 763 cout << "Cannot load value of type " << I.getType() << "!\n"; 764 } 765 } else { 766 switch (I.getType()->getPrimitiveID()) { 767 case Type::BoolTyID: 768 case Type::UByteTyID: 769 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break; 770 case Type::UShortTyID: 771 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] | 772 ((unsigned)Ptr->Untyped[0] << 8); 773 break; 774 case Type::FloatTyID: 775 case Type::UIntTyID: 776 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] | 777 ((unsigned)Ptr->Untyped[2] << 8) | 778 ((unsigned)Ptr->Untyped[1] << 16) | 779 ((unsigned)Ptr->Untyped[0] << 24); 780 break; 781 case Type::DoubleTyID: 782 case Type::ULongTyID: 783 case Type::LongTyID: 784 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] | 785 ((uint64_t)Ptr->Untyped[6] << 8) | 786 ((uint64_t)Ptr->Untyped[5] << 16) | 787 ((uint64_t)Ptr->Untyped[4] << 24) | 788 ((uint64_t)Ptr->Untyped[3] << 32) | 789 ((uint64_t)Ptr->Untyped[2] << 40) | 790 ((uint64_t)Ptr->Untyped[1] << 48) | 791 ((uint64_t)Ptr->Untyped[0] << 56); 792 break; 793 default: 794 cout << "Cannot load value of type " << I.getType() << "!\n"; 795 } 796 } 797 798 SetValue(&I, Result, SF); 799 } 800 801 void Interpreter::executeStoreInst(StoreInst &I, ExecutionContext &SF) { 802 GenericValue Val = getOperandValue(I.getOperand(0), SF); 803 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); 804 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC), 805 I.getOperand(0)->getType()); 806 } 807 808 809 810 //===----------------------------------------------------------------------===// 811 // Miscellaneous Instruction Implementations 812 //===----------------------------------------------------------------------===// 813 814 void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) { 815 ECStack.back().Caller = &I; 816 vector<GenericValue> ArgVals; 817 ArgVals.reserve(I.getNumOperands()-1); 818 for (unsigned i = 1; i < I.getNumOperands(); ++i) { 819 ArgVals.push_back(getOperandValue(I.getOperand(i), SF)); 820 // Promote all integral types whose size is < sizeof(int) into ints. We do 821 // this by zero or sign extending the value as appropriate according to the 822 // source type. 823 if (I.getOperand(i)->getType()->isIntegral() && 824 I.getOperand(i)->getType()->getPrimitiveSize() < 4) { 825 const Type *Ty = I.getOperand(i)->getType(); 826 if (Ty == Type::ShortTy) 827 ArgVals.back().IntVal = ArgVals.back().ShortVal; 828 else if (Ty == Type::UShortTy) 829 ArgVals.back().UIntVal = ArgVals.back().UShortVal; 830 else if (Ty == Type::SByteTy) 831 ArgVals.back().IntVal = ArgVals.back().SByteVal; 832 else if (Ty == Type::UByteTy) 833 ArgVals.back().UIntVal = ArgVals.back().UByteVal; 834 else if (Ty == Type::BoolTy) 835 ArgVals.back().UIntVal = ArgVals.back().BoolVal; 836 else 837 assert(0 && "Unknown type!"); 838 } 839 } 840 841 // To handle indirect calls, we must get the pointer value from the argument 842 // and treat it as a function pointer. 843 GenericValue SRC = getOperandValue(I.getCalledValue(), SF); 844 845 callMethod((Function*)GVTOP(SRC), ArgVals); 846 } 847 848 static void executePHINode(PHINode &I, ExecutionContext &SF) { 849 BasicBlock *PrevBB = SF.PrevBB; 850 Value *IncomingValue = 0; 851 852 // Search for the value corresponding to this previous bb... 853 for (unsigned i = I.getNumIncomingValues(); i > 0;) { 854 if (I.getIncomingBlock(--i) == PrevBB) { 855 IncomingValue = I.getIncomingValue(i); 856 break; 857 } 858 } 859 assert(IncomingValue && "No PHI node predecessor for current PrevBB!"); 860 861 // Found the value, set as the result... 862 SetValue(&I, getOperandValue(IncomingValue, SF), SF); 863 } 864 865 #define IMPLEMENT_SHIFT(OP, TY) \ 866 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break 867 868 static void executeShlInst(ShiftInst &I, ExecutionContext &SF) { 869 const Type *Ty = I.getOperand(0)->getType(); 870 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 871 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 872 GenericValue Dest; 873 874 switch (Ty->getPrimitiveID()) { 875 IMPLEMENT_SHIFT(<<, UByte); 876 IMPLEMENT_SHIFT(<<, SByte); 877 IMPLEMENT_SHIFT(<<, UShort); 878 IMPLEMENT_SHIFT(<<, Short); 879 IMPLEMENT_SHIFT(<<, UInt); 880 IMPLEMENT_SHIFT(<<, Int); 881 IMPLEMENT_SHIFT(<<, ULong); 882 IMPLEMENT_SHIFT(<<, Long); 883 IMPLEMENT_SHIFT(<<, Pointer); 884 default: 885 cout << "Unhandled type for Shl instruction: " << Ty << "\n"; 886 } 887 SetValue(&I, Dest, SF); 888 } 889 890 static void executeShrInst(ShiftInst &I, ExecutionContext &SF) { 891 const Type *Ty = I.getOperand(0)->getType(); 892 GenericValue Src1 = getOperandValue(I.getOperand(0), SF); 893 GenericValue Src2 = getOperandValue(I.getOperand(1), SF); 894 GenericValue Dest; 895 896 switch (Ty->getPrimitiveID()) { 897 IMPLEMENT_SHIFT(>>, UByte); 898 IMPLEMENT_SHIFT(>>, SByte); 899 IMPLEMENT_SHIFT(>>, UShort); 900 IMPLEMENT_SHIFT(>>, Short); 901 IMPLEMENT_SHIFT(>>, UInt); 902 IMPLEMENT_SHIFT(>>, Int); 903 IMPLEMENT_SHIFT(>>, ULong); 904 IMPLEMENT_SHIFT(>>, Long); 905 IMPLEMENT_SHIFT(>>, Pointer); 906 default: 907 cout << "Unhandled type for Shr instruction: " << Ty << "\n"; 908 } 909 SetValue(&I, Dest, SF); 910 } 911 912 #define IMPLEMENT_CAST(DTY, DCTY, STY) \ 913 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break; 914 915 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \ 916 case Type::DESTTY##TyID: \ 917 switch (SrcTy->getPrimitiveID()) { \ 918 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \ 919 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \ 920 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \ 921 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \ 922 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \ 923 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \ 924 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \ 925 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \ 926 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \ 927 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); 928 929 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \ 930 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \ 931 IMPLEMENT_CAST(DESTTY, DESTCTY, Double) 932 933 #define IMPLEMENT_CAST_CASE_END() \ 934 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \ 935 break; \ 936 } \ 937 break 938 939 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \ 940 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \ 941 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \ 942 IMPLEMENT_CAST_CASE_END() 943 944 static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty, 945 ExecutionContext &SF) { 946 const Type *SrcTy = SrcVal->getType(); 947 GenericValue Dest, Src = getOperandValue(SrcVal, SF); 948 949 switch (Ty->getPrimitiveID()) { 950 IMPLEMENT_CAST_CASE(UByte , (unsigned char)); 951 IMPLEMENT_CAST_CASE(SByte , ( signed char)); 952 IMPLEMENT_CAST_CASE(UShort , (unsigned short)); 953 IMPLEMENT_CAST_CASE(Short , ( signed short)); 954 IMPLEMENT_CAST_CASE(UInt , (unsigned int )); 955 IMPLEMENT_CAST_CASE(Int , ( signed int )); 956 IMPLEMENT_CAST_CASE(ULong , (uint64_t)); 957 IMPLEMENT_CAST_CASE(Long , ( int64_t)); 958 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)); 959 IMPLEMENT_CAST_CASE(Float , (float)); 960 IMPLEMENT_CAST_CASE(Double , (double)); 961 default: 962 cout << "Unhandled dest type for cast instruction: " << Ty << "\n"; 963 } 964 965 return Dest; 966 } 967 968 969 static void executeCastInst(CastInst &I, ExecutionContext &SF) { 970 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF); 971 } 972 973 974 //===----------------------------------------------------------------------===// 975 // Dispatch and Execution Code 976 //===----------------------------------------------------------------------===// 977 978 MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) { 979 // Assign slot numbers to the function arguments... 980 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI) 981 AI->addAnnotation(new SlotNumber(getValueSlot(AI))); 982 983 // Iterate over all of the instructions... 984 unsigned InstNum = 0; 985 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) 986 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II) 987 // For each instruction... Add Annote 988 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II))); 989 } 990 991 unsigned MethodInfo::getValueSlot(const Value *V) { 992 unsigned Plane = V->getType()->getUniqueID(); 993 if (Plane >= NumPlaneElements.size()) 994 NumPlaneElements.resize(Plane+1, 0); 995 return NumPlaneElements[Plane]++; 996 } 997 998 999 //===----------------------------------------------------------------------===// 1000 // callMethod - Execute the specified function... 1001 // 1002 void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) { 1003 assert((ECStack.empty() || ECStack.back().Caller == 0 || 1004 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) && 1005 "Incorrect number of arguments passed into function call!"); 1006 if (M->isExternal()) { 1007 GenericValue Result = callExternalMethod(M, ArgVals); 1008 const Type *RetTy = M->getReturnType(); 1009 1010 // Copy the result back into the result variable if we are not returning 1011 // void. 1012 if (RetTy != Type::VoidTy) { 1013 if (!ECStack.empty() && ECStack.back().Caller) { 1014 ExecutionContext &SF = ECStack.back(); 1015 SetValue(SF.Caller, Result, SF); 1016 1017 SF.Caller = 0; // We returned from the call... 1018 } else if (!QuietMode) { 1019 // print it. 1020 CW << "Function " << M->getType() << " \"" << M->getName() 1021 << "\" returned "; 1022 print(RetTy, Result); 1023 cout << "\n"; 1024 1025 if (RetTy->isIntegral()) 1026 ExitCode = Result.IntVal; // Capture the exit code of the program 1027 } 1028 } 1029 1030 return; 1031 } 1032 1033 // Process the function, assigning instruction numbers to the instructions in 1034 // the function. Also calculate the number of values for each type slot 1035 // active. 1036 // 1037 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID); 1038 ECStack.push_back(ExecutionContext()); // Make a new stack frame... 1039 1040 ExecutionContext &StackFrame = ECStack.back(); // Fill it in... 1041 StackFrame.CurMethod = M; 1042 StackFrame.CurBB = M->begin(); 1043 StackFrame.CurInst = StackFrame.CurBB->begin(); 1044 StackFrame.MethInfo = MethInfo; 1045 1046 // Initialize the values to nothing... 1047 StackFrame.Values.resize(MethInfo->NumPlaneElements.size()); 1048 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) { 1049 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]); 1050 1051 // Taint the initial values of stuff 1052 memset(&StackFrame.Values[i][0], 42, 1053 MethInfo->NumPlaneElements[i]*sizeof(GenericValue)); 1054 } 1055 1056 StackFrame.PrevBB = 0; // No previous BB for PHI nodes... 1057 1058 1059 // Run through the function arguments and initialize their values... 1060 assert(ArgVals.size() == M->asize() && 1061 "Invalid number of values passed to function invocation!"); 1062 unsigned i = 0; 1063 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i) 1064 SetValue(AI, ArgVals[i], StackFrame); 1065 } 1066 1067 // executeInstruction - Interpret a single instruction, increment the "PC", and 1068 // return true if the next instruction is a breakpoint... 1069 // 1070 bool Interpreter::executeInstruction() { 1071 assert(!ECStack.empty() && "No program running, cannot execute inst!"); 1072 1073 ExecutionContext &SF = ECStack.back(); // Current stack frame 1074 Instruction &I = *SF.CurInst++; // Increment before execute 1075 1076 if (Trace) 1077 CW << "Run:" << I; 1078 1079 // Track the number of dynamic instructions executed. 1080 ++NumDynamicInsts; 1081 1082 // Set a sigsetjmp buffer so that we can recover if an error happens during 1083 // instruction execution... 1084 // 1085 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) { 1086 --SF.CurInst; // Back up to erroring instruction 1087 if (SigNo != SIGINT) { 1088 cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n"; 1089 printStackTrace(); 1090 // If -abort-on-exception was specified, terminate LLI instead of trying 1091 // to debug it. 1092 // 1093 if (AbortOnExceptions) exit(1); 1094 } else if (SigNo == SIGINT) { 1095 cout << "CTRL-C Detected, execution halted.\n"; 1096 } 1097 InInstruction = false; 1098 return true; 1099 } 1100 1101 InInstruction = true; 1102 if (I.isBinaryOp()) { 1103 executeBinaryInst(cast<BinaryOperator>(I), SF); 1104 } else { 1105 switch (I.getOpcode()) { 1106 // Terminators 1107 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break; 1108 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break; 1109 // Memory Instructions 1110 case Instruction::Alloca: 1111 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break; 1112 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break; 1113 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break; 1114 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break; 1115 case Instruction::GetElementPtr: 1116 executeGEPInst(cast<GetElementPtrInst>(I), SF); break; 1117 1118 // Miscellaneous Instructions 1119 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break; 1120 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break; 1121 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break; 1122 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break; 1123 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break; 1124 default: 1125 cout << "Don't know how to execute this instruction!\n-->" << I; 1126 } 1127 } 1128 InInstruction = false; 1129 1130 // Reset the current frame location to the top of stack 1131 CurFrame = ECStack.size()-1; 1132 1133 if (CurFrame == -1) return false; // No breakpoint if no code 1134 1135 // Return true if there is a breakpoint annotation on the instruction... 1136 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0; 1137 } 1138 1139 void Interpreter::stepInstruction() { // Do the 'step' command 1140 if (ECStack.empty()) { 1141 cout << "Error: no program running, cannot step!\n"; 1142 return; 1143 } 1144 1145 // Run an instruction... 1146 executeInstruction(); 1147 1148 // Print the next instruction to execute... 1149 printCurrentInstruction(); 1150 } 1151 1152 // --- UI Stuff... 1153 void Interpreter::nextInstruction() { // Do the 'next' command 1154 if (ECStack.empty()) { 1155 cout << "Error: no program running, cannot 'next'!\n"; 1156 return; 1157 } 1158 1159 // If this is a call instruction, step over the call instruction... 1160 // TODO: ICALL, CALL WITH, ... 1161 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) { 1162 unsigned StackSize = ECStack.size(); 1163 // Step into the function... 1164 if (executeInstruction()) { 1165 // Hit a breakpoint, print current instruction, then return to user... 1166 cout << "Breakpoint hit!\n"; 1167 printCurrentInstruction(); 1168 return; 1169 } 1170 1171 // If we we able to step into the function, finish it now. We might not be 1172 // able the step into a function, if it's external for example. 1173 if (ECStack.size() != StackSize) 1174 finish(); // Finish executing the function... 1175 else 1176 printCurrentInstruction(); 1177 1178 } else { 1179 // Normal instruction, just step... 1180 stepInstruction(); 1181 } 1182 } 1183 1184 void Interpreter::run() { 1185 if (ECStack.empty()) { 1186 cout << "Error: no program running, cannot run!\n"; 1187 return; 1188 } 1189 1190 bool HitBreakpoint = false; 1191 while (!ECStack.empty() && !HitBreakpoint) { 1192 // Run an instruction... 1193 HitBreakpoint = executeInstruction(); 1194 } 1195 1196 if (HitBreakpoint) { 1197 cout << "Breakpoint hit!\n"; 1198 } 1199 // Print the next instruction to execute... 1200 printCurrentInstruction(); 1201 } 1202 1203 void Interpreter::finish() { 1204 if (ECStack.empty()) { 1205 cout << "Error: no program running, cannot run!\n"; 1206 return; 1207 } 1208 1209 unsigned StackSize = ECStack.size(); 1210 bool HitBreakpoint = false; 1211 while (ECStack.size() >= StackSize && !HitBreakpoint) { 1212 // Run an instruction... 1213 HitBreakpoint = executeInstruction(); 1214 } 1215 1216 if (HitBreakpoint) { 1217 cout << "Breakpoint hit!\n"; 1218 } 1219 1220 // Print the next instruction to execute... 1221 printCurrentInstruction(); 1222 } 1223 1224 1225 1226 // printCurrentInstruction - Print out the instruction that the virtual PC is 1227 // at, or fail silently if no program is running. 1228 // 1229 void Interpreter::printCurrentInstruction() { 1230 if (!ECStack.empty()) { 1231 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label 1232 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n"; 1233 1234 Instruction &I = *ECStack.back().CurInst; 1235 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID); 1236 assert(IN && "Instruction has no numbering annotation!"); 1237 cout << "#" << IN->InstNum << I; 1238 } 1239 } 1240 1241 void Interpreter::printValue(const Type *Ty, GenericValue V) { 1242 switch (Ty->getPrimitiveID()) { 1243 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break; 1244 case Type::SByteTyID: 1245 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break; 1246 case Type::UByteTyID: 1247 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break; 1248 case Type::ShortTyID: cout << V.ShortVal; break; 1249 case Type::UShortTyID: cout << V.UShortVal; break; 1250 case Type::IntTyID: cout << V.IntVal; break; 1251 case Type::UIntTyID: cout << V.UIntVal; break; 1252 case Type::LongTyID: cout << (long)V.LongVal; break; 1253 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break; 1254 case Type::FloatTyID: cout << V.FloatVal; break; 1255 case Type::DoubleTyID: cout << V.DoubleVal; break; 1256 case Type::PointerTyID:cout << (void*)GVTOP(V); break; 1257 default: 1258 cout << "- Don't know how to print value of this type!"; 1259 break; 1260 } 1261 } 1262 1263 void Interpreter::print(const Type *Ty, GenericValue V) { 1264 CW << Ty << " "; 1265 printValue(Ty, V); 1266 } 1267 1268 void Interpreter::print(const std::string &Name) { 1269 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); 1270 if (!PickedVal) return; 1271 1272 if (const Function *F = dyn_cast<const Function>(PickedVal)) { 1273 CW << F; // Print the function 1274 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) { 1275 CW << "type %" << Name << " = " << Ty->getDescription() << "\n"; 1276 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) { 1277 CW << BB; // Print the basic block 1278 } else { // Otherwise there should be an annotation for the slot# 1279 print(PickedVal->getType(), 1280 getOperandValue(PickedVal, ECStack[CurFrame])); 1281 cout << "\n"; 1282 } 1283 } 1284 1285 void Interpreter::infoValue(const std::string &Name) { 1286 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); 1287 if (!PickedVal) return; 1288 1289 cout << "Value: "; 1290 print(PickedVal->getType(), 1291 getOperandValue(PickedVal, ECStack[CurFrame])); 1292 cout << "\n"; 1293 printOperandInfo(PickedVal, ECStack[CurFrame]); 1294 } 1295 1296 // printStackFrame - Print information about the specified stack frame, or -1 1297 // for the default one. 1298 // 1299 void Interpreter::printStackFrame(int FrameNo) { 1300 if (FrameNo == -1) FrameNo = CurFrame; 1301 Function *F = ECStack[FrameNo].CurMethod; 1302 const Type *RetTy = F->getReturnType(); 1303 1304 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". " 1305 << (Value*)RetTy << " \"" << F->getName() << "\"("; 1306 1307 unsigned i = 0; 1308 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) { 1309 if (i != 0) cout << ", "; 1310 CW << *I << "="; 1311 1312 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo])); 1313 } 1314 1315 cout << ")\n"; 1316 1317 if (FrameNo != int(ECStack.size()-1)) { 1318 BasicBlock::iterator I = ECStack[FrameNo].CurInst; 1319 CW << --I; 1320 } else { 1321 CW << *ECStack[FrameNo].CurInst; 1322 } 1323 } 1324 1325