1 //===-- Execution.cpp - Implement code to simulate the program ------------===//
3 // This file contains the actual instruction interpreter.
5 //===----------------------------------------------------------------------===//
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
27 Interpreter *TheEE = 0;
30 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
33 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
37 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
40 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
43 AbortOnExceptions("abort-on-exception",
44 cl::desc("Halt execution on a machine exception"));
47 // Create a TargetData structure to handle memory addressing and size/alignment
50 CachedWriter CW; // Object to accelerate printing of LLVM
52 #ifdef PROFILE_STRUCTURE_FIELDS
54 ProfileStructureFields("profilestructfields",
55 cl::desc("Profile Structure Field Accesses"));
57 static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
60 sigjmp_buf SignalRecoverBuffer;
61 static bool InInstruction = false;
64 static void SigHandler(int Signal) {
66 siglongjmp(SignalRecoverBuffer, Signal);
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);
82 //===----------------------------------------------------------------------===//
83 // Value Manipulation code
84 //===----------------------------------------------------------------------===//
86 static unsigned getOperandSlot(Value *V) {
87 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
88 assert(SN && "Operand does not have a slot number annotation!");
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,
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,
107 case Instruction::Add:
108 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
109 getOperandValue(CE->getOperand(1), SF),
112 cerr << "Unhandled ConstantExpr: " << CE << "\n";
114 return GenericValue();
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));
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)];
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";
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
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'));
153 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
154 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
156 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
157 SF.Values[TyP][getOperandSlot(V)] = Val;
161 //===----------------------------------------------------------------------===//
162 // Annotation Wrangling code
163 //===----------------------------------------------------------------------===//
165 void Interpreter::initializeExecutionEngine() {
167 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
168 &MethodInfo::Create);
169 initializeSignalHandlers();
172 //===----------------------------------------------------------------------===//
173 // Binary Instruction Implementations
174 //===----------------------------------------------------------------------===//
176 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
177 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
179 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
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);
195 cout << "Unhandled type for Add instruction: " << Ty << "\n";
200 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
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);
216 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
221 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
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);
237 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
242 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
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);
258 cout << "Unhandled type for Div instruction: " << Ty << "\n";
263 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
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);
279 case Type::DoubleTyID:
280 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
283 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
288 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
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);
302 cout << "Unhandled type for And instruction: " << Ty << "\n";
308 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
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);
322 cout << "Unhandled type for Or instruction: " << Ty << "\n";
328 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
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);
342 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
348 #define IMPLEMENT_SETCC(OP, TY) \
349 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
351 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
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);
367 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
372 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
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);
389 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
394 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
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);
410 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
415 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
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);
431 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
436 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
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);
452 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
457 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
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);
473 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
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
484 switch (I.getOpcode()) {
485 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
486 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
487 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
488 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
489 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
490 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
491 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
492 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
493 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
494 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
495 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
496 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
497 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
498 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
500 cout << "Don't know how to handle this binary operator!\n-->" << I;
507 //===----------------------------------------------------------------------===//
508 // Terminator Instruction Implementations
509 //===----------------------------------------------------------------------===//
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=";
523 for (unsigned i = 0; i < OfC.size(); ++i)
527 for (unsigned i = 0; i < OfC.size(); ++i) {
535 CW << "Profile Field Access Percentages:\n";
537 for (I = FieldAccessCounts.begin(); I != E; ++I) {
538 vector<unsigned> &OfC = I->second;
540 for (unsigned i = 0; i < OfC.size(); ++i)
543 CW << " '" << (Value*)I->first << "'\t- ";
544 for (unsigned i = 0; i < OfC.size(); ++i) {
546 CW << double(OfC[i])/Sum;
552 FieldAccessCounts.clear();
557 void Interpreter::exitCalled(GenericValue GV) {
559 cout << "Program returned ";
560 print(Type::IntTy, GV);
561 cout << " via 'void exit(int)'\n";
564 ExitCode = GV.SByteVal;
569 void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
570 const Type *RetTy = 0;
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);
579 // Save previously executing meth
580 const Function *M = ECStack.back().CurMethod;
582 // Pop the current stack frame... this invalidates SF
585 if (ECStack.empty()) { // Finished main. Put result into exit code...
586 if (RetTy) { // Nonvoid return type?
588 CW << "Function " << M->getType() << " \"" << M->getName()
590 print(RetTy, Result);
594 if (RetTy->isIntegral())
595 ExitCode = Result.IntVal; // Capture the exit code of the program
604 // If we have a previous stack frame, and we have a previous call, fill in
605 // the return value...
607 ExecutionContext &NewSF = ECStack.back();
609 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
610 SetValue(NewSF.Caller, Result, NewSF);
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'
616 CW << "Function " << M->getType() << " \"" << M->getName()
618 print(RetTy, Result);
623 void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
624 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
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);
634 SF.CurBB = Dest; // Update CurBB to branch destination
635 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
638 static void executeSwitch(SwitchInst &I, ExecutionContext &SF) {
639 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
640 const Type *ElTy = I.getOperand(0)->getType();
641 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
642 BasicBlock *Dest = 0;
644 // Check to see if any of the cases match...
645 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) {
646 if (executeSetEQInst(CondVal,
647 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
648 Dest = cast<BasicBlock>(I.getOperand(i+1));
653 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
654 SF.CurBB = Dest; // Update CurBB to branch destination
655 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
659 //===----------------------------------------------------------------------===//
660 // Memory Instruction Implementations
661 //===----------------------------------------------------------------------===//
663 void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
664 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
666 // Get the number of elements being allocated by the array...
667 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
669 // Allocate enough memory to hold the type...
670 // FIXME: Don't use CALLOC, use a tainted malloc.
671 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
673 GenericValue Result = PTOGV(Memory);
674 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
675 SetValue(&I, Result, SF);
677 if (I.getOpcode() == Instruction::Alloca)
678 ECStack.back().Allocas.add(Memory);
681 static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
682 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
683 GenericValue Value = getOperandValue(I.getOperand(0), SF);
684 // TODO: Check to make sure memory is allocated
685 free(GVTOP(Value)); // Free memory
689 // getElementOffset - The workhorse for getelementptr.
691 GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
693 ExecutionContext &SF) {
694 assert(isa<PointerType>(Ptr->getType()) &&
695 "Cannot getElementOffset of a nonpointer type!");
698 const Type *Ty = Ptr->getType();
700 for (; I != E; ++I) {
701 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
702 const StructLayout *SLO = TD.getStructLayout(STy);
704 // Indicies must be ubyte constants...
705 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
706 assert(CPU->getType() == Type::UByteTy);
707 unsigned Index = CPU->getValue();
709 #ifdef PROFILE_STRUCTURE_FIELDS
710 if (ProfileStructureFields) {
711 // Do accounting for this field...
712 vector<unsigned> &OfC = FieldAccessCounts[STy];
713 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
718 Total += SLO->MemberOffsets[Index];
719 Ty = STy->getElementTypes()[Index];
720 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
722 // Get the index number for the array... which must be long type...
723 assert((*I)->getType() == Type::LongTy);
724 unsigned Idx = getOperandValue(*I, SF).LongVal;
725 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
726 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
727 cerr << "Out of range memory access to element #" << Idx
728 << " of a " << AT->getNumElements() << " element array."
729 << " Subscript #" << *I << "\n";
731 siglongjmp(SignalRecoverBuffer, SIGTRAP);
734 Ty = ST->getElementType();
735 unsigned Size = TD.getTypeSize(Ty);
741 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
745 static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
746 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
747 I.idx_begin(), I.idx_end(), SF), SF);
750 void Interpreter::executeLoadInst(LoadInst &I, ExecutionContext &SF) {
751 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
752 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
755 if (TD.isLittleEndian()) {
756 switch (I.getType()->getPrimitiveID()) {
758 case Type::UByteTyID:
759 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
760 case Type::UShortTyID:
761 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
762 ((unsigned)Ptr->Untyped[1] << 8);
764 case Type::FloatTyID:
766 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
767 ((unsigned)Ptr->Untyped[1] << 8) |
768 ((unsigned)Ptr->Untyped[2] << 16) |
769 ((unsigned)Ptr->Untyped[3] << 24);
771 case Type::DoubleTyID:
772 case Type::ULongTyID:
774 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
775 ((uint64_t)Ptr->Untyped[1] << 8) |
776 ((uint64_t)Ptr->Untyped[2] << 16) |
777 ((uint64_t)Ptr->Untyped[3] << 24) |
778 ((uint64_t)Ptr->Untyped[4] << 32) |
779 ((uint64_t)Ptr->Untyped[5] << 40) |
780 ((uint64_t)Ptr->Untyped[6] << 48) |
781 ((uint64_t)Ptr->Untyped[7] << 56);
784 cout << "Cannot load value of type " << I.getType() << "!\n";
787 switch (I.getType()->getPrimitiveID()) {
789 case Type::UByteTyID:
790 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
791 case Type::UShortTyID:
792 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
793 ((unsigned)Ptr->Untyped[0] << 8);
795 case Type::FloatTyID:
797 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
798 ((unsigned)Ptr->Untyped[2] << 8) |
799 ((unsigned)Ptr->Untyped[1] << 16) |
800 ((unsigned)Ptr->Untyped[0] << 24);
802 case Type::DoubleTyID:
803 case Type::ULongTyID:
805 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
806 ((uint64_t)Ptr->Untyped[6] << 8) |
807 ((uint64_t)Ptr->Untyped[5] << 16) |
808 ((uint64_t)Ptr->Untyped[4] << 24) |
809 ((uint64_t)Ptr->Untyped[3] << 32) |
810 ((uint64_t)Ptr->Untyped[2] << 40) |
811 ((uint64_t)Ptr->Untyped[1] << 48) |
812 ((uint64_t)Ptr->Untyped[0] << 56);
815 cout << "Cannot load value of type " << I.getType() << "!\n";
819 SetValue(&I, Result, SF);
822 void Interpreter::executeStoreInst(StoreInst &I, ExecutionContext &SF) {
823 GenericValue Val = getOperandValue(I.getOperand(0), SF);
824 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
825 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
826 I.getOperand(0)->getType());
831 //===----------------------------------------------------------------------===//
832 // Miscellaneous Instruction Implementations
833 //===----------------------------------------------------------------------===//
835 void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
836 ECStack.back().Caller = &I;
837 vector<GenericValue> ArgVals;
838 ArgVals.reserve(I.getNumOperands()-1);
839 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
840 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
841 // Promote all integral types whose size is < sizeof(int) into ints. We do
842 // this by zero or sign extending the value as appropriate according to the
844 if (I.getOperand(i)->getType()->isIntegral() &&
845 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
846 const Type *Ty = I.getOperand(i)->getType();
847 if (Ty == Type::ShortTy)
848 ArgVals.back().IntVal = ArgVals.back().ShortVal;
849 else if (Ty == Type::UShortTy)
850 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
851 else if (Ty == Type::SByteTy)
852 ArgVals.back().IntVal = ArgVals.back().SByteVal;
853 else if (Ty == Type::UByteTy)
854 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
855 else if (Ty == Type::BoolTy)
856 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
858 assert(0 && "Unknown type!");
862 // To handle indirect calls, we must get the pointer value from the argument
863 // and treat it as a function pointer.
864 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
866 callMethod((Function*)GVTOP(SRC), ArgVals);
869 static void executePHINode(PHINode &I, ExecutionContext &SF) {
870 BasicBlock *PrevBB = SF.PrevBB;
871 Value *IncomingValue = 0;
873 // Search for the value corresponding to this previous bb...
874 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
875 if (I.getIncomingBlock(--i) == PrevBB) {
876 IncomingValue = I.getIncomingValue(i);
880 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
882 // Found the value, set as the result...
883 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
886 #define IMPLEMENT_SHIFT(OP, TY) \
887 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
889 static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
890 const Type *Ty = I.getOperand(0)->getType();
891 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
892 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
895 switch (Ty->getPrimitiveID()) {
896 IMPLEMENT_SHIFT(<<, UByte);
897 IMPLEMENT_SHIFT(<<, SByte);
898 IMPLEMENT_SHIFT(<<, UShort);
899 IMPLEMENT_SHIFT(<<, Short);
900 IMPLEMENT_SHIFT(<<, UInt);
901 IMPLEMENT_SHIFT(<<, Int);
902 IMPLEMENT_SHIFT(<<, ULong);
903 IMPLEMENT_SHIFT(<<, Long);
904 IMPLEMENT_SHIFT(<<, Pointer);
906 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
908 SetValue(&I, Dest, SF);
911 static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
912 const Type *Ty = I.getOperand(0)->getType();
913 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
914 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
917 switch (Ty->getPrimitiveID()) {
918 IMPLEMENT_SHIFT(>>, UByte);
919 IMPLEMENT_SHIFT(>>, SByte);
920 IMPLEMENT_SHIFT(>>, UShort);
921 IMPLEMENT_SHIFT(>>, Short);
922 IMPLEMENT_SHIFT(>>, UInt);
923 IMPLEMENT_SHIFT(>>, Int);
924 IMPLEMENT_SHIFT(>>, ULong);
925 IMPLEMENT_SHIFT(>>, Long);
926 IMPLEMENT_SHIFT(>>, Pointer);
928 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
930 SetValue(&I, Dest, SF);
933 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
934 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
936 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
937 case Type::DESTTY##TyID: \
938 switch (SrcTy->getPrimitiveID()) { \
939 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
940 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
941 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
942 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
943 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
944 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
945 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
946 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
947 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
948 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
950 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
951 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
952 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
954 #define IMPLEMENT_CAST_CASE_END() \
955 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
960 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
961 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
962 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
963 IMPLEMENT_CAST_CASE_END()
965 static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
966 ExecutionContext &SF) {
967 const Type *SrcTy = SrcVal->getType();
968 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
970 switch (Ty->getPrimitiveID()) {
971 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
972 IMPLEMENT_CAST_CASE(SByte , ( signed char));
973 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
974 IMPLEMENT_CAST_CASE(Short , ( signed short));
975 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
976 IMPLEMENT_CAST_CASE(Int , ( signed int ));
977 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
978 IMPLEMENT_CAST_CASE(Long , ( int64_t));
979 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
980 IMPLEMENT_CAST_CASE(Float , (float));
981 IMPLEMENT_CAST_CASE(Double , (double));
982 IMPLEMENT_CAST_CASE(Bool , (bool));
984 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
992 static void executeCastInst(CastInst &I, ExecutionContext &SF) {
993 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
997 //===----------------------------------------------------------------------===//
998 // Dispatch and Execution Code
999 //===----------------------------------------------------------------------===//
1001 MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
1002 // Assign slot numbers to the function arguments...
1003 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1004 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
1006 // Iterate over all of the instructions...
1007 unsigned InstNum = 0;
1008 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1009 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1010 // For each instruction... Add Annote
1011 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
1014 unsigned MethodInfo::getValueSlot(const Value *V) {
1015 unsigned Plane = V->getType()->getUniqueID();
1016 if (Plane >= NumPlaneElements.size())
1017 NumPlaneElements.resize(Plane+1, 0);
1018 return NumPlaneElements[Plane]++;
1022 //===----------------------------------------------------------------------===//
1023 // callMethod - Execute the specified function...
1025 void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
1026 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1027 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1028 "Incorrect number of arguments passed into function call!");
1029 if (M->isExternal()) {
1030 GenericValue Result = callExternalMethod(M, ArgVals);
1031 const Type *RetTy = M->getReturnType();
1033 // Copy the result back into the result variable if we are not returning
1035 if (RetTy != Type::VoidTy) {
1036 if (!ECStack.empty() && ECStack.back().Caller) {
1037 ExecutionContext &SF = ECStack.back();
1038 SetValue(SF.Caller, Result, SF);
1040 SF.Caller = 0; // We returned from the call...
1041 } else if (!QuietMode) {
1043 CW << "Function " << M->getType() << " \"" << M->getName()
1045 print(RetTy, Result);
1048 if (RetTy->isIntegral())
1049 ExitCode = Result.IntVal; // Capture the exit code of the program
1056 // Process the function, assigning instruction numbers to the instructions in
1057 // the function. Also calculate the number of values for each type slot
1060 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
1061 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
1063 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1064 StackFrame.CurMethod = M;
1065 StackFrame.CurBB = M->begin();
1066 StackFrame.CurInst = StackFrame.CurBB->begin();
1067 StackFrame.MethInfo = MethInfo;
1069 // Initialize the values to nothing...
1070 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
1071 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
1072 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1074 // Taint the initial values of stuff
1075 memset(&StackFrame.Values[i][0], 42,
1076 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1079 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1082 // Run through the function arguments and initialize their values...
1083 assert(ArgVals.size() == M->asize() &&
1084 "Invalid number of values passed to function invocation!");
1086 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1087 SetValue(AI, ArgVals[i], StackFrame);
1090 // executeInstruction - Interpret a single instruction, increment the "PC", and
1091 // return true if the next instruction is a breakpoint...
1093 bool Interpreter::executeInstruction() {
1094 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1096 ExecutionContext &SF = ECStack.back(); // Current stack frame
1097 Instruction &I = *SF.CurInst++; // Increment before execute
1102 // Track the number of dynamic instructions executed.
1105 // Set a sigsetjmp buffer so that we can recover if an error happens during
1106 // instruction execution...
1108 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1109 --SF.CurInst; // Back up to erroring instruction
1110 if (SigNo != SIGINT) {
1111 cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
1113 // If -abort-on-exception was specified, terminate LLI instead of trying
1116 if (AbortOnExceptions) exit(1);
1117 } else if (SigNo == SIGINT) {
1118 cout << "CTRL-C Detected, execution halted.\n";
1120 InInstruction = false;
1124 InInstruction = true;
1125 if (I.isBinaryOp()) {
1126 executeBinaryInst(cast<BinaryOperator>(I), SF);
1128 switch (I.getOpcode()) {
1130 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1131 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
1132 case Instruction::Switch: executeSwitch (cast<SwitchInst>(I), SF); break;
1133 // Memory Instructions
1134 case Instruction::Alloca:
1135 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
1136 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1137 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1138 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
1139 case Instruction::GetElementPtr:
1140 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
1142 // Miscellaneous Instructions
1143 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1144 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1145 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1146 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1147 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
1149 cout << "Don't know how to execute this instruction!\n-->" << I;
1152 InInstruction = false;
1154 // Reset the current frame location to the top of stack
1155 CurFrame = ECStack.size()-1;
1157 if (CurFrame == -1) return false; // No breakpoint if no code
1159 // Return true if there is a breakpoint annotation on the instruction...
1160 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
1163 void Interpreter::stepInstruction() { // Do the 'step' command
1164 if (ECStack.empty()) {
1165 cout << "Error: no program running, cannot step!\n";
1169 // Run an instruction...
1170 executeInstruction();
1172 // Print the next instruction to execute...
1173 printCurrentInstruction();
1177 void Interpreter::nextInstruction() { // Do the 'next' command
1178 if (ECStack.empty()) {
1179 cout << "Error: no program running, cannot 'next'!\n";
1183 // If this is a call instruction, step over the call instruction...
1184 // TODO: ICALL, CALL WITH, ...
1185 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
1186 unsigned StackSize = ECStack.size();
1187 // Step into the function...
1188 if (executeInstruction()) {
1189 // Hit a breakpoint, print current instruction, then return to user...
1190 cout << "Breakpoint hit!\n";
1191 printCurrentInstruction();
1195 // If we we able to step into the function, finish it now. We might not be
1196 // able the step into a function, if it's external for example.
1197 if (ECStack.size() != StackSize)
1198 finish(); // Finish executing the function...
1200 printCurrentInstruction();
1203 // Normal instruction, just step...
1208 void Interpreter::run() {
1209 if (ECStack.empty()) {
1210 cout << "Error: no program running, cannot run!\n";
1214 bool HitBreakpoint = false;
1215 while (!ECStack.empty() && !HitBreakpoint) {
1216 // Run an instruction...
1217 HitBreakpoint = executeInstruction();
1220 if (HitBreakpoint) {
1221 cout << "Breakpoint hit!\n";
1223 // Print the next instruction to execute...
1224 printCurrentInstruction();
1227 void Interpreter::finish() {
1228 if (ECStack.empty()) {
1229 cout << "Error: no program running, cannot run!\n";
1233 unsigned StackSize = ECStack.size();
1234 bool HitBreakpoint = false;
1235 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1236 // Run an instruction...
1237 HitBreakpoint = executeInstruction();
1240 if (HitBreakpoint) {
1241 cout << "Breakpoint hit!\n";
1244 // Print the next instruction to execute...
1245 printCurrentInstruction();
1250 // printCurrentInstruction - Print out the instruction that the virtual PC is
1251 // at, or fail silently if no program is running.
1253 void Interpreter::printCurrentInstruction() {
1254 if (!ECStack.empty()) {
1255 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1256 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1258 Instruction &I = *ECStack.back().CurInst;
1259 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
1260 assert(IN && "Instruction has no numbering annotation!");
1261 cout << "#" << IN->InstNum << I;
1265 void Interpreter::printValue(const Type *Ty, GenericValue V) {
1266 switch (Ty->getPrimitiveID()) {
1267 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1268 case Type::SByteTyID:
1269 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1270 case Type::UByteTyID:
1271 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1272 case Type::ShortTyID: cout << V.ShortVal; break;
1273 case Type::UShortTyID: cout << V.UShortVal; break;
1274 case Type::IntTyID: cout << V.IntVal; break;
1275 case Type::UIntTyID: cout << V.UIntVal; break;
1276 case Type::LongTyID: cout << (long)V.LongVal; break;
1277 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
1278 case Type::FloatTyID: cout << V.FloatVal; break;
1279 case Type::DoubleTyID: cout << V.DoubleVal; break;
1280 case Type::PointerTyID:cout << (void*)GVTOP(V); break;
1282 cout << "- Don't know how to print value of this type!";
1287 void Interpreter::print(const Type *Ty, GenericValue V) {
1292 void Interpreter::print(const std::string &Name) {
1293 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1294 if (!PickedVal) return;
1296 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1297 CW << F; // Print the function
1298 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1299 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
1300 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1301 CW << BB; // Print the basic block
1302 } else { // Otherwise there should be an annotation for the slot#
1303 print(PickedVal->getType(),
1304 getOperandValue(PickedVal, ECStack[CurFrame]));
1309 void Interpreter::infoValue(const std::string &Name) {
1310 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1311 if (!PickedVal) return;
1314 print(PickedVal->getType(),
1315 getOperandValue(PickedVal, ECStack[CurFrame]));
1317 printOperandInfo(PickedVal, ECStack[CurFrame]);
1320 // printStackFrame - Print information about the specified stack frame, or -1
1321 // for the default one.
1323 void Interpreter::printStackFrame(int FrameNo) {
1324 if (FrameNo == -1) FrameNo = CurFrame;
1325 Function *F = ECStack[FrameNo].CurMethod;
1326 const Type *RetTy = F->getReturnType();
1328 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1329 << (Value*)RetTy << " \"" << F->getName() << "\"(";
1332 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
1333 if (i != 0) cout << ", ";
1336 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
1341 if (FrameNo != int(ECStack.size()-1)) {
1342 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1345 CW << *ECStack[FrameNo].CurInst;