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
24 Interpreter *TheEE = 0;
27 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
30 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
34 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
37 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
40 AbortOnExceptions("abort-on-exception",
41 cl::desc("Halt execution on a machine exception"));
44 // Create a TargetData structure to handle memory addressing and size/alignment
47 CachedWriter CW; // Object to accelerate printing of LLVM
49 #ifdef PROFILE_STRUCTURE_FIELDS
51 ProfileStructureFields("profilestructfields",
52 cl::desc("Profile Structure Field Accesses"));
54 static std::map<const StructType *, std::vector<unsigned> > FieldAccessCounts;
57 sigjmp_buf SignalRecoverBuffer;
58 static bool InInstruction = false;
61 static void SigHandler(int Signal) {
63 siglongjmp(SignalRecoverBuffer, Signal);
67 static void initializeSignalHandlers() {
68 struct sigaction Action;
69 Action.sa_handler = SigHandler;
70 Action.sa_flags = SA_SIGINFO;
71 sigemptyset(&Action.sa_mask);
72 sigaction(SIGSEGV, &Action, 0);
73 sigaction(SIGBUS, &Action, 0);
74 sigaction(SIGINT, &Action, 0);
75 sigaction(SIGFPE, &Action, 0);
79 //===----------------------------------------------------------------------===//
80 // Value Manipulation code
81 //===----------------------------------------------------------------------===//
83 static unsigned getOperandSlot(Value *V) {
84 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
85 assert(SN && "Operand does not have a slot number annotation!");
89 // Operations used by constant expr implementations...
90 static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
91 ExecutionContext &SF);
92 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
96 static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
97 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
98 switch (CE->getOpcode()) {
99 case Instruction::Cast:
100 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
101 case Instruction::GetElementPtr:
102 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
104 case Instruction::Add:
105 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
106 getOperandValue(CE->getOperand(1), SF),
109 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
111 return GenericValue();
113 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
114 return TheEE->getConstantValue(CPV);
115 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
116 return PTOGV(TheEE->getPointerToGlobal(GV));
118 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
119 unsigned OpSlot = getOperandSlot(V);
120 assert(TyP < SF.Values.size() &&
121 OpSlot < SF.Values[TyP].size() && "Value out of range!");
122 return SF.Values[TyP][getOperandSlot(V)];
126 static void printOperandInfo(Value *V, ExecutionContext &SF) {
127 if (isa<Constant>(V)) {
128 std::cout << "Constant Pool Value\n";
129 } else if (isa<GlobalValue>(V)) {
130 std::cout << "Global Value\n";
132 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
133 unsigned Slot = getOperandSlot(V);
134 std::cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
135 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
138 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
139 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
140 unsigned char Cur = Buf[i];
141 std::cout << ( Cur >= 160?char((Cur>>4)+'A'-10):char((Cur>>4) + '0'))
142 << ((Cur&15) >= 10?char((Cur&15)+'A'-10):char((Cur&15) + '0'));
150 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
151 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
153 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
154 SF.Values[TyP][getOperandSlot(V)] = Val;
158 //===----------------------------------------------------------------------===//
159 // Annotation Wrangling code
160 //===----------------------------------------------------------------------===//
162 void Interpreter::initializeExecutionEngine() {
164 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
165 &MethodInfo::Create);
166 initializeSignalHandlers();
169 //===----------------------------------------------------------------------===//
170 // Binary Instruction Implementations
171 //===----------------------------------------------------------------------===//
173 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
174 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
176 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
179 switch (Ty->getPrimitiveID()) {
180 IMPLEMENT_BINARY_OPERATOR(+, UByte);
181 IMPLEMENT_BINARY_OPERATOR(+, SByte);
182 IMPLEMENT_BINARY_OPERATOR(+, UShort);
183 IMPLEMENT_BINARY_OPERATOR(+, Short);
184 IMPLEMENT_BINARY_OPERATOR(+, UInt);
185 IMPLEMENT_BINARY_OPERATOR(+, Int);
186 IMPLEMENT_BINARY_OPERATOR(+, ULong);
187 IMPLEMENT_BINARY_OPERATOR(+, Long);
188 IMPLEMENT_BINARY_OPERATOR(+, Float);
189 IMPLEMENT_BINARY_OPERATOR(+, Double);
191 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
197 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
200 switch (Ty->getPrimitiveID()) {
201 IMPLEMENT_BINARY_OPERATOR(-, UByte);
202 IMPLEMENT_BINARY_OPERATOR(-, SByte);
203 IMPLEMENT_BINARY_OPERATOR(-, UShort);
204 IMPLEMENT_BINARY_OPERATOR(-, Short);
205 IMPLEMENT_BINARY_OPERATOR(-, UInt);
206 IMPLEMENT_BINARY_OPERATOR(-, Int);
207 IMPLEMENT_BINARY_OPERATOR(-, ULong);
208 IMPLEMENT_BINARY_OPERATOR(-, Long);
209 IMPLEMENT_BINARY_OPERATOR(-, Float);
210 IMPLEMENT_BINARY_OPERATOR(-, Double);
212 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
218 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
221 switch (Ty->getPrimitiveID()) {
222 IMPLEMENT_BINARY_OPERATOR(*, UByte);
223 IMPLEMENT_BINARY_OPERATOR(*, SByte);
224 IMPLEMENT_BINARY_OPERATOR(*, UShort);
225 IMPLEMENT_BINARY_OPERATOR(*, Short);
226 IMPLEMENT_BINARY_OPERATOR(*, UInt);
227 IMPLEMENT_BINARY_OPERATOR(*, Int);
228 IMPLEMENT_BINARY_OPERATOR(*, ULong);
229 IMPLEMENT_BINARY_OPERATOR(*, Long);
230 IMPLEMENT_BINARY_OPERATOR(*, Float);
231 IMPLEMENT_BINARY_OPERATOR(*, Double);
233 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
239 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
242 switch (Ty->getPrimitiveID()) {
243 IMPLEMENT_BINARY_OPERATOR(/, UByte);
244 IMPLEMENT_BINARY_OPERATOR(/, SByte);
245 IMPLEMENT_BINARY_OPERATOR(/, UShort);
246 IMPLEMENT_BINARY_OPERATOR(/, Short);
247 IMPLEMENT_BINARY_OPERATOR(/, UInt);
248 IMPLEMENT_BINARY_OPERATOR(/, Int);
249 IMPLEMENT_BINARY_OPERATOR(/, ULong);
250 IMPLEMENT_BINARY_OPERATOR(/, Long);
251 IMPLEMENT_BINARY_OPERATOR(/, Float);
252 IMPLEMENT_BINARY_OPERATOR(/, Double);
254 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
260 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
263 switch (Ty->getPrimitiveID()) {
264 IMPLEMENT_BINARY_OPERATOR(%, UByte);
265 IMPLEMENT_BINARY_OPERATOR(%, SByte);
266 IMPLEMENT_BINARY_OPERATOR(%, UShort);
267 IMPLEMENT_BINARY_OPERATOR(%, Short);
268 IMPLEMENT_BINARY_OPERATOR(%, UInt);
269 IMPLEMENT_BINARY_OPERATOR(%, Int);
270 IMPLEMENT_BINARY_OPERATOR(%, ULong);
271 IMPLEMENT_BINARY_OPERATOR(%, Long);
272 case Type::FloatTyID:
273 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
275 case Type::DoubleTyID:
276 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
279 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
285 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
288 switch (Ty->getPrimitiveID()) {
289 IMPLEMENT_BINARY_OPERATOR(&, Bool);
290 IMPLEMENT_BINARY_OPERATOR(&, UByte);
291 IMPLEMENT_BINARY_OPERATOR(&, SByte);
292 IMPLEMENT_BINARY_OPERATOR(&, UShort);
293 IMPLEMENT_BINARY_OPERATOR(&, Short);
294 IMPLEMENT_BINARY_OPERATOR(&, UInt);
295 IMPLEMENT_BINARY_OPERATOR(&, Int);
296 IMPLEMENT_BINARY_OPERATOR(&, ULong);
297 IMPLEMENT_BINARY_OPERATOR(&, Long);
299 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
306 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
309 switch (Ty->getPrimitiveID()) {
310 IMPLEMENT_BINARY_OPERATOR(|, Bool);
311 IMPLEMENT_BINARY_OPERATOR(|, UByte);
312 IMPLEMENT_BINARY_OPERATOR(|, SByte);
313 IMPLEMENT_BINARY_OPERATOR(|, UShort);
314 IMPLEMENT_BINARY_OPERATOR(|, Short);
315 IMPLEMENT_BINARY_OPERATOR(|, UInt);
316 IMPLEMENT_BINARY_OPERATOR(|, Int);
317 IMPLEMENT_BINARY_OPERATOR(|, ULong);
318 IMPLEMENT_BINARY_OPERATOR(|, Long);
320 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
327 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
330 switch (Ty->getPrimitiveID()) {
331 IMPLEMENT_BINARY_OPERATOR(^, Bool);
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);
341 std::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 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
373 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
376 switch (Ty->getPrimitiveID()) {
377 IMPLEMENT_SETCC(!=, UByte);
378 IMPLEMENT_SETCC(!=, SByte);
379 IMPLEMENT_SETCC(!=, UShort);
380 IMPLEMENT_SETCC(!=, Short);
381 IMPLEMENT_SETCC(!=, UInt);
382 IMPLEMENT_SETCC(!=, Int);
383 IMPLEMENT_SETCC(!=, ULong);
384 IMPLEMENT_SETCC(!=, Long);
385 IMPLEMENT_SETCC(!=, Float);
386 IMPLEMENT_SETCC(!=, Double);
387 IMPLEMENT_SETCC(!=, Pointer);
390 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
396 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
399 switch (Ty->getPrimitiveID()) {
400 IMPLEMENT_SETCC(<=, UByte);
401 IMPLEMENT_SETCC(<=, SByte);
402 IMPLEMENT_SETCC(<=, UShort);
403 IMPLEMENT_SETCC(<=, Short);
404 IMPLEMENT_SETCC(<=, UInt);
405 IMPLEMENT_SETCC(<=, Int);
406 IMPLEMENT_SETCC(<=, ULong);
407 IMPLEMENT_SETCC(<=, Long);
408 IMPLEMENT_SETCC(<=, Float);
409 IMPLEMENT_SETCC(<=, Double);
410 IMPLEMENT_SETCC(<=, Pointer);
412 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
418 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
421 switch (Ty->getPrimitiveID()) {
422 IMPLEMENT_SETCC(>=, UByte);
423 IMPLEMENT_SETCC(>=, SByte);
424 IMPLEMENT_SETCC(>=, UShort);
425 IMPLEMENT_SETCC(>=, Short);
426 IMPLEMENT_SETCC(>=, UInt);
427 IMPLEMENT_SETCC(>=, Int);
428 IMPLEMENT_SETCC(>=, ULong);
429 IMPLEMENT_SETCC(>=, Long);
430 IMPLEMENT_SETCC(>=, Float);
431 IMPLEMENT_SETCC(>=, Double);
432 IMPLEMENT_SETCC(>=, Pointer);
434 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
440 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
443 switch (Ty->getPrimitiveID()) {
444 IMPLEMENT_SETCC(<, UByte);
445 IMPLEMENT_SETCC(<, SByte);
446 IMPLEMENT_SETCC(<, UShort);
447 IMPLEMENT_SETCC(<, Short);
448 IMPLEMENT_SETCC(<, UInt);
449 IMPLEMENT_SETCC(<, Int);
450 IMPLEMENT_SETCC(<, ULong);
451 IMPLEMENT_SETCC(<, Long);
452 IMPLEMENT_SETCC(<, Float);
453 IMPLEMENT_SETCC(<, Double);
454 IMPLEMENT_SETCC(<, Pointer);
456 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
462 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
465 switch (Ty->getPrimitiveID()) {
466 IMPLEMENT_SETCC(>, UByte);
467 IMPLEMENT_SETCC(>, SByte);
468 IMPLEMENT_SETCC(>, UShort);
469 IMPLEMENT_SETCC(>, Short);
470 IMPLEMENT_SETCC(>, UInt);
471 IMPLEMENT_SETCC(>, Int);
472 IMPLEMENT_SETCC(>, ULong);
473 IMPLEMENT_SETCC(>, Long);
474 IMPLEMENT_SETCC(>, Float);
475 IMPLEMENT_SETCC(>, Double);
476 IMPLEMENT_SETCC(>, Pointer);
478 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
484 static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
485 const Type *Ty = I.getOperand(0)->getType();
486 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
487 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
488 GenericValue R; // Result
490 switch (I.getOpcode()) {
491 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
492 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
493 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
494 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
495 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
496 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
497 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
498 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
499 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
500 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
501 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
502 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
503 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
504 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
506 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
513 //===----------------------------------------------------------------------===//
514 // Terminator Instruction Implementations
515 //===----------------------------------------------------------------------===//
517 static void PerformExitStuff() {
518 #ifdef PROFILE_STRUCTURE_FIELDS
519 // Print out structure field accounting information...
520 if (!FieldAccessCounts.empty()) {
521 CW << "Profile Field Access Counts:\n";
522 std::map<const StructType *, std::vector<unsigned> >::iterator
523 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
524 for (; I != E; ++I) {
525 std::vector<unsigned> &OfC = I->second;
526 CW << " '" << (Value*)I->first << "'\t- Sum=";
529 for (unsigned i = 0; i < OfC.size(); ++i)
533 for (unsigned i = 0; i < OfC.size(); ++i) {
541 CW << "Profile Field Access Percentages:\n";
542 std::cout.precision(3);
543 for (I = FieldAccessCounts.begin(); I != E; ++I) {
544 std::vector<unsigned> &OfC = I->second;
546 for (unsigned i = 0; i < OfC.size(); ++i)
549 CW << " '" << (Value*)I->first << "'\t- ";
550 for (unsigned i = 0; i < OfC.size(); ++i) {
552 CW << double(OfC[i])/Sum;
558 FieldAccessCounts.clear();
563 void Interpreter::exitCalled(GenericValue GV) {
565 std::cout << "Program returned ";
566 print(Type::IntTy, GV);
567 std::cout << " via 'void exit(int)'\n";
570 ExitCode = GV.SByteVal;
575 void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
576 const Type *RetTy = 0;
579 // Save away the return value... (if we are not 'ret void')
580 if (I.getNumOperands()) {
581 RetTy = I.getReturnValue()->getType();
582 Result = getOperandValue(I.getReturnValue(), SF);
585 // Save previously executing meth
586 const Function *M = ECStack.back().CurMethod;
588 // Pop the current stack frame... this invalidates SF
591 if (ECStack.empty()) { // Finished main. Put result into exit code...
592 if (RetTy) { // Nonvoid return type?
594 CW << "Function " << M->getType() << " \"" << M->getName()
596 print(RetTy, Result);
600 if (RetTy->isIntegral())
601 ExitCode = Result.IntVal; // Capture the exit code of the program
610 // If we have a previous stack frame, and we have a previous call, fill in
611 // the return value...
613 ExecutionContext &NewSF = ECStack.back();
615 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
616 SetValue(NewSF.Caller, Result, NewSF);
618 NewSF.Caller = 0; // We returned from the call...
619 } else if (!QuietMode) {
620 // This must be a function that is executing because of a user 'call'
622 CW << "Function " << M->getType() << " \"" << M->getName()
624 print(RetTy, Result);
629 void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
630 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
633 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
634 if (!I.isUnconditional()) {
635 Value *Cond = I.getCondition();
636 GenericValue CondVal = getOperandValue(Cond, SF);
637 if (CondVal.BoolVal == 0) // If false cond...
638 Dest = I.getSuccessor(1);
640 SF.CurBB = Dest; // Update CurBB to branch destination
641 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
644 static void executeSwitch(SwitchInst &I, ExecutionContext &SF) {
645 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
646 const Type *ElTy = I.getOperand(0)->getType();
647 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
648 BasicBlock *Dest = 0;
650 // Check to see if any of the cases match...
651 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) {
652 if (executeSetEQInst(CondVal,
653 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
654 Dest = cast<BasicBlock>(I.getOperand(i+1));
659 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
660 SF.CurBB = Dest; // Update CurBB to branch destination
661 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
665 //===----------------------------------------------------------------------===//
666 // Memory Instruction Implementations
667 //===----------------------------------------------------------------------===//
669 void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
670 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
672 // Get the number of elements being allocated by the array...
673 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
675 // Allocate enough memory to hold the type...
676 // FIXME: Don't use CALLOC, use a tainted malloc.
677 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
679 GenericValue Result = PTOGV(Memory);
680 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
681 SetValue(&I, Result, SF);
683 if (I.getOpcode() == Instruction::Alloca)
684 ECStack.back().Allocas.add(Memory);
687 static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
688 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
689 GenericValue Value = getOperandValue(I.getOperand(0), SF);
690 // TODO: Check to make sure memory is allocated
691 free(GVTOP(Value)); // Free memory
695 // getElementOffset - The workhorse for getelementptr.
697 GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
699 ExecutionContext &SF) {
700 assert(isa<PointerType>(Ptr->getType()) &&
701 "Cannot getElementOffset of a nonpointer type!");
704 const Type *Ty = Ptr->getType();
706 for (; I != E; ++I) {
707 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
708 const StructLayout *SLO = TD.getStructLayout(STy);
710 // Indicies must be ubyte constants...
711 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
712 assert(CPU->getType() == Type::UByteTy);
713 unsigned Index = CPU->getValue();
715 #ifdef PROFILE_STRUCTURE_FIELDS
716 if (ProfileStructureFields) {
717 // Do accounting for this field...
718 std::vector<unsigned> &OfC = FieldAccessCounts[STy];
719 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
724 Total += SLO->MemberOffsets[Index];
725 Ty = STy->getElementTypes()[Index];
726 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
728 // Get the index number for the array... which must be long type...
729 assert((*I)->getType() == Type::LongTy);
730 unsigned Idx = getOperandValue(*I, SF).LongVal;
731 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
732 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
733 std::cerr << "Out of range memory access to element #" << Idx
734 << " of a " << AT->getNumElements() << " element array."
735 << " Subscript #" << *I << "\n";
737 siglongjmp(SignalRecoverBuffer, SIGTRAP);
740 Ty = ST->getElementType();
741 unsigned Size = TD.getTypeSize(Ty);
747 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
751 static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
752 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
753 I.idx_begin(), I.idx_end(), SF), SF);
756 void Interpreter::executeLoadInst(LoadInst &I, ExecutionContext &SF) {
757 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
758 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
761 if (TD.isLittleEndian()) {
762 switch (I.getType()->getPrimitiveID()) {
764 case Type::UByteTyID:
765 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
766 case Type::UShortTyID:
767 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[0] |
768 ((unsigned)Ptr->Untyped[1] << 8);
770 case Type::FloatTyID:
772 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[0] |
773 ((unsigned)Ptr->Untyped[1] << 8) |
774 ((unsigned)Ptr->Untyped[2] << 16) |
775 ((unsigned)Ptr->Untyped[3] << 24);
777 case Type::DoubleTyID:
778 case Type::ULongTyID:
780 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[0] |
781 ((uint64_t)Ptr->Untyped[1] << 8) |
782 ((uint64_t)Ptr->Untyped[2] << 16) |
783 ((uint64_t)Ptr->Untyped[3] << 24) |
784 ((uint64_t)Ptr->Untyped[4] << 32) |
785 ((uint64_t)Ptr->Untyped[5] << 40) |
786 ((uint64_t)Ptr->Untyped[6] << 48) |
787 ((uint64_t)Ptr->Untyped[7] << 56);
790 std::cout << "Cannot load value of type " << *I.getType() << "!\n";
794 switch (I.getType()->getPrimitiveID()) {
796 case Type::UByteTyID:
797 case Type::SByteTyID: Result.UByteVal = Ptr->Untyped[0]; break;
798 case Type::UShortTyID:
799 case Type::ShortTyID: Result.UShortVal = (unsigned)Ptr->Untyped[1] |
800 ((unsigned)Ptr->Untyped[0] << 8);
802 case Type::FloatTyID:
804 case Type::IntTyID: Result.UIntVal = (unsigned)Ptr->Untyped[3] |
805 ((unsigned)Ptr->Untyped[2] << 8) |
806 ((unsigned)Ptr->Untyped[1] << 16) |
807 ((unsigned)Ptr->Untyped[0] << 24);
809 case Type::DoubleTyID:
810 case Type::ULongTyID:
812 case Type::PointerTyID: Result.ULongVal = (uint64_t)Ptr->Untyped[7] |
813 ((uint64_t)Ptr->Untyped[6] << 8) |
814 ((uint64_t)Ptr->Untyped[5] << 16) |
815 ((uint64_t)Ptr->Untyped[4] << 24) |
816 ((uint64_t)Ptr->Untyped[3] << 32) |
817 ((uint64_t)Ptr->Untyped[2] << 40) |
818 ((uint64_t)Ptr->Untyped[1] << 48) |
819 ((uint64_t)Ptr->Untyped[0] << 56);
822 std::cout << "Cannot load value of type " << *I.getType() << "!\n";
827 SetValue(&I, Result, SF);
830 void Interpreter::executeStoreInst(StoreInst &I, ExecutionContext &SF) {
831 GenericValue Val = getOperandValue(I.getOperand(0), SF);
832 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
833 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
834 I.getOperand(0)->getType());
839 //===----------------------------------------------------------------------===//
840 // Miscellaneous Instruction Implementations
841 //===----------------------------------------------------------------------===//
843 void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
844 ECStack.back().Caller = &I;
845 std::vector<GenericValue> ArgVals;
846 ArgVals.reserve(I.getNumOperands()-1);
847 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
848 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
849 // Promote all integral types whose size is < sizeof(int) into ints. We do
850 // this by zero or sign extending the value as appropriate according to the
852 if (I.getOperand(i)->getType()->isIntegral() &&
853 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
854 const Type *Ty = I.getOperand(i)->getType();
855 if (Ty == Type::ShortTy)
856 ArgVals.back().IntVal = ArgVals.back().ShortVal;
857 else if (Ty == Type::UShortTy)
858 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
859 else if (Ty == Type::SByteTy)
860 ArgVals.back().IntVal = ArgVals.back().SByteVal;
861 else if (Ty == Type::UByteTy)
862 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
863 else if (Ty == Type::BoolTy)
864 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
866 assert(0 && "Unknown type!");
870 // To handle indirect calls, we must get the pointer value from the argument
871 // and treat it as a function pointer.
872 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
874 callMethod((Function*)GVTOP(SRC), ArgVals);
877 static void executePHINode(PHINode &I, ExecutionContext &SF) {
878 BasicBlock *PrevBB = SF.PrevBB;
879 Value *IncomingValue = 0;
881 // Search for the value corresponding to this previous bb...
882 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
883 if (I.getIncomingBlock(--i) == PrevBB) {
884 IncomingValue = I.getIncomingValue(i);
888 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
890 // Found the value, set as the result...
891 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
894 #define IMPLEMENT_SHIFT(OP, TY) \
895 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
897 static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
898 const Type *Ty = I.getOperand(0)->getType();
899 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
900 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
903 switch (Ty->getPrimitiveID()) {
904 IMPLEMENT_SHIFT(<<, UByte);
905 IMPLEMENT_SHIFT(<<, SByte);
906 IMPLEMENT_SHIFT(<<, UShort);
907 IMPLEMENT_SHIFT(<<, Short);
908 IMPLEMENT_SHIFT(<<, UInt);
909 IMPLEMENT_SHIFT(<<, Int);
910 IMPLEMENT_SHIFT(<<, ULong);
911 IMPLEMENT_SHIFT(<<, Long);
913 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
915 SetValue(&I, Dest, SF);
918 static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
919 const Type *Ty = I.getOperand(0)->getType();
920 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
921 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
924 switch (Ty->getPrimitiveID()) {
925 IMPLEMENT_SHIFT(>>, UByte);
926 IMPLEMENT_SHIFT(>>, SByte);
927 IMPLEMENT_SHIFT(>>, UShort);
928 IMPLEMENT_SHIFT(>>, Short);
929 IMPLEMENT_SHIFT(>>, UInt);
930 IMPLEMENT_SHIFT(>>, Int);
931 IMPLEMENT_SHIFT(>>, ULong);
932 IMPLEMENT_SHIFT(>>, Long);
934 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
937 SetValue(&I, Dest, SF);
940 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
941 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
943 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
944 case Type::DESTTY##TyID: \
945 switch (SrcTy->getPrimitiveID()) { \
946 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
947 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
948 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
949 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
950 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
951 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
952 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
953 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
954 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
955 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
957 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
958 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
959 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
961 #define IMPLEMENT_CAST_CASE_END() \
962 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
967 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
968 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
969 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
970 IMPLEMENT_CAST_CASE_END()
972 static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
973 ExecutionContext &SF) {
974 const Type *SrcTy = SrcVal->getType();
975 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
977 switch (Ty->getPrimitiveID()) {
978 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
979 IMPLEMENT_CAST_CASE(SByte , ( signed char));
980 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
981 IMPLEMENT_CAST_CASE(Short , ( signed short));
982 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
983 IMPLEMENT_CAST_CASE(Int , ( signed int ));
984 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
985 IMPLEMENT_CAST_CASE(Long , ( int64_t));
986 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
987 IMPLEMENT_CAST_CASE(Float , (float));
988 IMPLEMENT_CAST_CASE(Double , (double));
989 IMPLEMENT_CAST_CASE(Bool , (bool));
991 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
999 static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1000 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
1004 //===----------------------------------------------------------------------===//
1005 // Dispatch and Execution Code
1006 //===----------------------------------------------------------------------===//
1008 MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
1009 // Assign slot numbers to the function arguments...
1010 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1011 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
1013 // Iterate over all of the instructions...
1014 unsigned InstNum = 0;
1015 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1016 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1017 // For each instruction... Add Annote
1018 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
1021 unsigned MethodInfo::getValueSlot(const Value *V) {
1022 unsigned Plane = V->getType()->getUniqueID();
1023 if (Plane >= NumPlaneElements.size())
1024 NumPlaneElements.resize(Plane+1, 0);
1025 return NumPlaneElements[Plane]++;
1029 //===----------------------------------------------------------------------===//
1030 // callMethod - Execute the specified function...
1032 void Interpreter::callMethod(Function *M,
1033 const std::vector<GenericValue> &ArgVals) {
1034 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1035 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1036 "Incorrect number of arguments passed into function call!");
1037 if (M->isExternal()) {
1038 GenericValue Result = callExternalMethod(M, ArgVals);
1039 const Type *RetTy = M->getReturnType();
1041 // Copy the result back into the result variable if we are not returning
1043 if (RetTy != Type::VoidTy) {
1044 if (!ECStack.empty() && ECStack.back().Caller) {
1045 ExecutionContext &SF = ECStack.back();
1046 SetValue(SF.Caller, Result, SF);
1048 SF.Caller = 0; // We returned from the call...
1049 } else if (!QuietMode) {
1051 CW << "Function " << M->getType() << " \"" << M->getName()
1053 print(RetTy, Result);
1056 if (RetTy->isIntegral())
1057 ExitCode = Result.IntVal; // Capture the exit code of the program
1064 // Process the function, assigning instruction numbers to the instructions in
1065 // the function. Also calculate the number of values for each type slot
1068 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
1069 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
1071 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1072 StackFrame.CurMethod = M;
1073 StackFrame.CurBB = M->begin();
1074 StackFrame.CurInst = StackFrame.CurBB->begin();
1075 StackFrame.MethInfo = MethInfo;
1077 // Initialize the values to nothing...
1078 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
1079 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
1080 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1082 // Taint the initial values of stuff
1083 memset(&StackFrame.Values[i][0], 42,
1084 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1087 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1090 // Run through the function arguments and initialize their values...
1091 assert(ArgVals.size() == M->asize() &&
1092 "Invalid number of values passed to function invocation!");
1094 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1095 SetValue(AI, ArgVals[i], StackFrame);
1098 // executeInstruction - Interpret a single instruction, increment the "PC", and
1099 // return true if the next instruction is a breakpoint...
1101 bool Interpreter::executeInstruction() {
1102 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1104 ExecutionContext &SF = ECStack.back(); // Current stack frame
1105 Instruction &I = *SF.CurInst++; // Increment before execute
1110 // Track the number of dynamic instructions executed.
1113 // Set a sigsetjmp buffer so that we can recover if an error happens during
1114 // instruction execution...
1116 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1117 --SF.CurInst; // Back up to erroring instruction
1118 if (SigNo != SIGINT) {
1119 std::cout << "EXCEPTION OCCURRED [" << strsignal(SigNo) << "]:\n";
1121 // If -abort-on-exception was specified, terminate LLI instead of trying
1124 if (AbortOnExceptions) exit(1);
1125 } else if (SigNo == SIGINT) {
1126 std::cout << "CTRL-C Detected, execution halted.\n";
1128 InInstruction = false;
1132 InInstruction = true;
1133 if (I.isBinaryOp()) {
1134 executeBinaryInst(cast<BinaryOperator>(I), SF);
1136 switch (I.getOpcode()) {
1138 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1139 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
1140 case Instruction::Switch: executeSwitch (cast<SwitchInst>(I), SF); break;
1141 // Memory Instructions
1142 case Instruction::Alloca:
1143 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
1144 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1145 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1146 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
1147 case Instruction::GetElementPtr:
1148 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
1150 // Miscellaneous Instructions
1151 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1152 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1153 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1154 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1155 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
1157 std::cout << "Don't know how to execute this instruction!\n-->" << I;
1161 InInstruction = false;
1163 // Reset the current frame location to the top of stack
1164 CurFrame = ECStack.size()-1;
1166 if (CurFrame == -1) return false; // No breakpoint if no code
1168 // Return true if there is a breakpoint annotation on the instruction...
1169 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
1172 void Interpreter::stepInstruction() { // Do the 'step' command
1173 if (ECStack.empty()) {
1174 std::cout << "Error: no program running, cannot step!\n";
1178 // Run an instruction...
1179 executeInstruction();
1181 // Print the next instruction to execute...
1182 printCurrentInstruction();
1186 void Interpreter::nextInstruction() { // Do the 'next' command
1187 if (ECStack.empty()) {
1188 std::cout << "Error: no program running, cannot 'next'!\n";
1192 // If this is a call instruction, step over the call instruction...
1193 // TODO: ICALL, CALL WITH, ...
1194 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
1195 unsigned StackSize = ECStack.size();
1196 // Step into the function...
1197 if (executeInstruction()) {
1198 // Hit a breakpoint, print current instruction, then return to user...
1199 std::cout << "Breakpoint hit!\n";
1200 printCurrentInstruction();
1204 // If we we able to step into the function, finish it now. We might not be
1205 // able the step into a function, if it's external for example.
1206 if (ECStack.size() != StackSize)
1207 finish(); // Finish executing the function...
1209 printCurrentInstruction();
1212 // Normal instruction, just step...
1217 void Interpreter::run() {
1218 if (ECStack.empty()) {
1219 std::cout << "Error: no program running, cannot run!\n";
1223 bool HitBreakpoint = false;
1224 while (!ECStack.empty() && !HitBreakpoint) {
1225 // Run an instruction...
1226 HitBreakpoint = executeInstruction();
1230 std::cout << "Breakpoint hit!\n";
1232 // Print the next instruction to execute...
1233 printCurrentInstruction();
1236 void Interpreter::finish() {
1237 if (ECStack.empty()) {
1238 std::cout << "Error: no program running, cannot run!\n";
1242 unsigned StackSize = ECStack.size();
1243 bool HitBreakpoint = false;
1244 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1245 // Run an instruction...
1246 HitBreakpoint = executeInstruction();
1250 std::cout << "Breakpoint hit!\n";
1252 // Print the next instruction to execute...
1253 printCurrentInstruction();
1258 // printCurrentInstruction - Print out the instruction that the virtual PC is
1259 // at, or fail silently if no program is running.
1261 void Interpreter::printCurrentInstruction() {
1262 if (!ECStack.empty()) {
1263 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1264 WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
1266 Instruction &I = *ECStack.back().CurInst;
1267 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
1268 assert(IN && "Instruction has no numbering annotation!");
1269 std::cout << "#" << IN->InstNum << I;
1273 void Interpreter::printValue(const Type *Ty, GenericValue V) {
1274 switch (Ty->getPrimitiveID()) {
1275 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
1276 case Type::SByteTyID:
1277 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1278 case Type::UByteTyID:
1279 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1280 case Type::ShortTyID: std::cout << V.ShortVal; break;
1281 case Type::UShortTyID: std::cout << V.UShortVal; break;
1282 case Type::IntTyID: std::cout << V.IntVal; break;
1283 case Type::UIntTyID: std::cout << V.UIntVal; break;
1284 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1285 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1286 case Type::FloatTyID: std::cout << V.FloatVal; break;
1287 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1288 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
1290 std::cout << "- Don't know how to print value of this type!";
1295 void Interpreter::print(const Type *Ty, GenericValue V) {
1300 void Interpreter::print(const std::string &Name) {
1301 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1302 if (!PickedVal) return;
1304 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1305 CW << F; // Print the function
1306 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1307 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
1308 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1309 CW << BB; // Print the basic block
1310 } else { // Otherwise there should be an annotation for the slot#
1311 print(PickedVal->getType(),
1312 getOperandValue(PickedVal, ECStack[CurFrame]));
1317 void Interpreter::infoValue(const std::string &Name) {
1318 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1319 if (!PickedVal) return;
1321 std::cout << "Value: ";
1322 print(PickedVal->getType(),
1323 getOperandValue(PickedVal, ECStack[CurFrame]));
1325 printOperandInfo(PickedVal, ECStack[CurFrame]);
1328 // printStackFrame - Print information about the specified stack frame, or -1
1329 // for the default one.
1331 void Interpreter::printStackFrame(int FrameNo) {
1332 if (FrameNo == -1) FrameNo = CurFrame;
1333 Function *F = ECStack[FrameNo].CurMethod;
1334 const Type *RetTy = F->getReturnType();
1336 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1337 << (Value*)RetTy << " \"" << F->getName() << "\"(";
1340 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
1341 if (i != 0) std::cout << ", ";
1344 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
1349 if (FrameNo != int(ECStack.size()-1)) {
1350 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1353 CW << *ECStack[FrameNo].CurInst;