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/Module.h"
10 #include "llvm/Instructions.h"
11 #include "llvm/DerivedTypes.h"
12 #include "llvm/Constants.h"
13 #include "llvm/Assembly/Writer.h"
14 #include "Support/CommandLine.h"
15 #include "Support/Statistic.h"
16 #include <math.h> // For fmod
20 Interpreter *TheEE = 0;
23 Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed");
26 QuietMode("quiet", cl::desc("Do not emit any non-program output"),
30 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
33 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
36 AbortOnExceptions("abort-on-exception",
37 cl::desc("Halt execution on a machine exception"));
40 // Create a TargetData structure to handle memory addressing and size/alignment
43 CachedWriter CW; // Object to accelerate printing of LLVM
45 #ifdef PROFILE_STRUCTURE_FIELDS
47 ProfileStructureFields("profilestructfields",
48 cl::desc("Profile Structure Field Accesses"));
50 static std::map<const StructType *, std::vector<unsigned> > FieldAccessCounts;
53 sigjmp_buf SignalRecoverBuffer;
54 static bool InInstruction = false;
57 static void SigHandler(int Signal) {
59 siglongjmp(SignalRecoverBuffer, Signal);
63 static void initializeSignalHandlers() {
64 struct sigaction Action;
65 Action.sa_handler = SigHandler;
66 Action.sa_flags = SA_SIGINFO;
67 sigemptyset(&Action.sa_mask);
68 sigaction(SIGSEGV, &Action, 0);
69 sigaction(SIGBUS, &Action, 0);
70 sigaction(SIGINT, &Action, 0);
71 sigaction(SIGFPE, &Action, 0);
75 //===----------------------------------------------------------------------===//
76 // Value Manipulation code
77 //===----------------------------------------------------------------------===//
79 static unsigned getOperandSlot(Value *V) {
80 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
81 assert(SN && "Operand does not have a slot number annotation!");
85 // Operations used by constant expr implementations...
86 static GenericValue executeCastOperation(Value *Src, const Type *DestTy,
87 ExecutionContext &SF);
88 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
92 static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
93 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
94 switch (CE->getOpcode()) {
95 case Instruction::Cast:
96 return executeCastOperation(CE->getOperand(0), CE->getType(), SF);
97 case Instruction::GetElementPtr:
98 return TheEE->executeGEPOperation(CE->getOperand(0), CE->op_begin()+1,
100 case Instruction::Add:
101 return executeAddInst(getOperandValue(CE->getOperand(0), SF),
102 getOperandValue(CE->getOperand(1), SF),
105 std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
107 return GenericValue();
109 } else if (Constant *CPV = dyn_cast<Constant>(V)) {
110 return TheEE->getConstantValue(CPV);
111 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
112 return PTOGV(TheEE->getPointerToGlobal(GV));
114 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
115 unsigned OpSlot = getOperandSlot(V);
116 assert(TyP < SF.Values.size() &&
117 OpSlot < SF.Values[TyP].size() && "Value out of range!");
118 return SF.Values[TyP][getOperandSlot(V)];
122 static void printOperandInfo(Value *V, ExecutionContext &SF) {
123 if (isa<Constant>(V)) {
124 std::cout << "Constant Pool Value\n";
125 } else if (isa<GlobalValue>(V)) {
126 std::cout << "Global Value\n";
128 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
129 unsigned Slot = getOperandSlot(V);
130 std::cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
131 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
134 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
135 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
136 unsigned char Cur = Buf[i];
137 std::cout << ( Cur >= 160?char((Cur>>4)+'A'-10):char((Cur>>4) + '0'))
138 << ((Cur&15) >= 10?char((Cur&15)+'A'-10):char((Cur&15) + '0'));
146 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
147 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
149 //std::cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)]<< "\n";
150 SF.Values[TyP][getOperandSlot(V)] = Val;
154 //===----------------------------------------------------------------------===//
155 // Annotation Wrangling code
156 //===----------------------------------------------------------------------===//
158 void Interpreter::initializeExecutionEngine() {
160 AnnotationManager::registerAnnotationFactory(FunctionInfoAID,
161 &FunctionInfo::Create);
162 initializeSignalHandlers();
165 //===----------------------------------------------------------------------===//
166 // Binary Instruction Implementations
167 //===----------------------------------------------------------------------===//
169 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
170 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
172 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
175 switch (Ty->getPrimitiveID()) {
176 IMPLEMENT_BINARY_OPERATOR(+, UByte);
177 IMPLEMENT_BINARY_OPERATOR(+, SByte);
178 IMPLEMENT_BINARY_OPERATOR(+, UShort);
179 IMPLEMENT_BINARY_OPERATOR(+, Short);
180 IMPLEMENT_BINARY_OPERATOR(+, UInt);
181 IMPLEMENT_BINARY_OPERATOR(+, Int);
182 IMPLEMENT_BINARY_OPERATOR(+, ULong);
183 IMPLEMENT_BINARY_OPERATOR(+, Long);
184 IMPLEMENT_BINARY_OPERATOR(+, Float);
185 IMPLEMENT_BINARY_OPERATOR(+, Double);
187 std::cout << "Unhandled type for Add instruction: " << *Ty << "\n";
193 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
196 switch (Ty->getPrimitiveID()) {
197 IMPLEMENT_BINARY_OPERATOR(-, UByte);
198 IMPLEMENT_BINARY_OPERATOR(-, SByte);
199 IMPLEMENT_BINARY_OPERATOR(-, UShort);
200 IMPLEMENT_BINARY_OPERATOR(-, Short);
201 IMPLEMENT_BINARY_OPERATOR(-, UInt);
202 IMPLEMENT_BINARY_OPERATOR(-, Int);
203 IMPLEMENT_BINARY_OPERATOR(-, ULong);
204 IMPLEMENT_BINARY_OPERATOR(-, Long);
205 IMPLEMENT_BINARY_OPERATOR(-, Float);
206 IMPLEMENT_BINARY_OPERATOR(-, Double);
208 std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n";
214 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
217 switch (Ty->getPrimitiveID()) {
218 IMPLEMENT_BINARY_OPERATOR(*, UByte);
219 IMPLEMENT_BINARY_OPERATOR(*, SByte);
220 IMPLEMENT_BINARY_OPERATOR(*, UShort);
221 IMPLEMENT_BINARY_OPERATOR(*, Short);
222 IMPLEMENT_BINARY_OPERATOR(*, UInt);
223 IMPLEMENT_BINARY_OPERATOR(*, Int);
224 IMPLEMENT_BINARY_OPERATOR(*, ULong);
225 IMPLEMENT_BINARY_OPERATOR(*, Long);
226 IMPLEMENT_BINARY_OPERATOR(*, Float);
227 IMPLEMENT_BINARY_OPERATOR(*, Double);
229 std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
235 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
238 switch (Ty->getPrimitiveID()) {
239 IMPLEMENT_BINARY_OPERATOR(/, UByte);
240 IMPLEMENT_BINARY_OPERATOR(/, SByte);
241 IMPLEMENT_BINARY_OPERATOR(/, UShort);
242 IMPLEMENT_BINARY_OPERATOR(/, Short);
243 IMPLEMENT_BINARY_OPERATOR(/, UInt);
244 IMPLEMENT_BINARY_OPERATOR(/, Int);
245 IMPLEMENT_BINARY_OPERATOR(/, ULong);
246 IMPLEMENT_BINARY_OPERATOR(/, Long);
247 IMPLEMENT_BINARY_OPERATOR(/, Float);
248 IMPLEMENT_BINARY_OPERATOR(/, Double);
250 std::cout << "Unhandled type for Div instruction: " << *Ty << "\n";
256 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
259 switch (Ty->getPrimitiveID()) {
260 IMPLEMENT_BINARY_OPERATOR(%, UByte);
261 IMPLEMENT_BINARY_OPERATOR(%, SByte);
262 IMPLEMENT_BINARY_OPERATOR(%, UShort);
263 IMPLEMENT_BINARY_OPERATOR(%, Short);
264 IMPLEMENT_BINARY_OPERATOR(%, UInt);
265 IMPLEMENT_BINARY_OPERATOR(%, Int);
266 IMPLEMENT_BINARY_OPERATOR(%, ULong);
267 IMPLEMENT_BINARY_OPERATOR(%, Long);
268 case Type::FloatTyID:
269 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
271 case Type::DoubleTyID:
272 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
275 std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n";
281 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
284 switch (Ty->getPrimitiveID()) {
285 IMPLEMENT_BINARY_OPERATOR(&, Bool);
286 IMPLEMENT_BINARY_OPERATOR(&, UByte);
287 IMPLEMENT_BINARY_OPERATOR(&, SByte);
288 IMPLEMENT_BINARY_OPERATOR(&, UShort);
289 IMPLEMENT_BINARY_OPERATOR(&, Short);
290 IMPLEMENT_BINARY_OPERATOR(&, UInt);
291 IMPLEMENT_BINARY_OPERATOR(&, Int);
292 IMPLEMENT_BINARY_OPERATOR(&, ULong);
293 IMPLEMENT_BINARY_OPERATOR(&, Long);
295 std::cout << "Unhandled type for And instruction: " << *Ty << "\n";
302 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
305 switch (Ty->getPrimitiveID()) {
306 IMPLEMENT_BINARY_OPERATOR(|, Bool);
307 IMPLEMENT_BINARY_OPERATOR(|, UByte);
308 IMPLEMENT_BINARY_OPERATOR(|, SByte);
309 IMPLEMENT_BINARY_OPERATOR(|, UShort);
310 IMPLEMENT_BINARY_OPERATOR(|, Short);
311 IMPLEMENT_BINARY_OPERATOR(|, UInt);
312 IMPLEMENT_BINARY_OPERATOR(|, Int);
313 IMPLEMENT_BINARY_OPERATOR(|, ULong);
314 IMPLEMENT_BINARY_OPERATOR(|, Long);
316 std::cout << "Unhandled type for Or instruction: " << *Ty << "\n";
323 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
326 switch (Ty->getPrimitiveID()) {
327 IMPLEMENT_BINARY_OPERATOR(^, Bool);
328 IMPLEMENT_BINARY_OPERATOR(^, UByte);
329 IMPLEMENT_BINARY_OPERATOR(^, SByte);
330 IMPLEMENT_BINARY_OPERATOR(^, UShort);
331 IMPLEMENT_BINARY_OPERATOR(^, Short);
332 IMPLEMENT_BINARY_OPERATOR(^, UInt);
333 IMPLEMENT_BINARY_OPERATOR(^, Int);
334 IMPLEMENT_BINARY_OPERATOR(^, ULong);
335 IMPLEMENT_BINARY_OPERATOR(^, Long);
337 std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n";
344 #define IMPLEMENT_SETCC(OP, TY) \
345 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
347 // Handle pointers specially because they must be compared with only as much
348 // width as the host has. We _do not_ want to be comparing 64 bit values when
349 // running on a 32-bit target, otherwise the upper 32 bits might mess up
350 // comparisons if they contain garbage.
351 #define IMPLEMENT_POINTERSETCC(OP) \
352 case Type::PointerTyID: \
353 Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \
354 (void*)(intptr_t)Src2.PointerVal; break
356 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
359 switch (Ty->getPrimitiveID()) {
360 IMPLEMENT_SETCC(==, UByte);
361 IMPLEMENT_SETCC(==, SByte);
362 IMPLEMENT_SETCC(==, UShort);
363 IMPLEMENT_SETCC(==, Short);
364 IMPLEMENT_SETCC(==, UInt);
365 IMPLEMENT_SETCC(==, Int);
366 IMPLEMENT_SETCC(==, ULong);
367 IMPLEMENT_SETCC(==, Long);
368 IMPLEMENT_SETCC(==, Float);
369 IMPLEMENT_SETCC(==, Double);
370 IMPLEMENT_POINTERSETCC(==);
372 std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n";
378 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
381 switch (Ty->getPrimitiveID()) {
382 IMPLEMENT_SETCC(!=, UByte);
383 IMPLEMENT_SETCC(!=, SByte);
384 IMPLEMENT_SETCC(!=, UShort);
385 IMPLEMENT_SETCC(!=, Short);
386 IMPLEMENT_SETCC(!=, UInt);
387 IMPLEMENT_SETCC(!=, Int);
388 IMPLEMENT_SETCC(!=, ULong);
389 IMPLEMENT_SETCC(!=, Long);
390 IMPLEMENT_SETCC(!=, Float);
391 IMPLEMENT_SETCC(!=, Double);
392 IMPLEMENT_POINTERSETCC(!=);
395 std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n";
401 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
404 switch (Ty->getPrimitiveID()) {
405 IMPLEMENT_SETCC(<=, UByte);
406 IMPLEMENT_SETCC(<=, SByte);
407 IMPLEMENT_SETCC(<=, UShort);
408 IMPLEMENT_SETCC(<=, Short);
409 IMPLEMENT_SETCC(<=, UInt);
410 IMPLEMENT_SETCC(<=, Int);
411 IMPLEMENT_SETCC(<=, ULong);
412 IMPLEMENT_SETCC(<=, Long);
413 IMPLEMENT_SETCC(<=, Float);
414 IMPLEMENT_SETCC(<=, Double);
415 IMPLEMENT_POINTERSETCC(<=);
417 std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
423 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
426 switch (Ty->getPrimitiveID()) {
427 IMPLEMENT_SETCC(>=, UByte);
428 IMPLEMENT_SETCC(>=, SByte);
429 IMPLEMENT_SETCC(>=, UShort);
430 IMPLEMENT_SETCC(>=, Short);
431 IMPLEMENT_SETCC(>=, UInt);
432 IMPLEMENT_SETCC(>=, Int);
433 IMPLEMENT_SETCC(>=, ULong);
434 IMPLEMENT_SETCC(>=, Long);
435 IMPLEMENT_SETCC(>=, Float);
436 IMPLEMENT_SETCC(>=, Double);
437 IMPLEMENT_POINTERSETCC(>=);
439 std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n";
445 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
448 switch (Ty->getPrimitiveID()) {
449 IMPLEMENT_SETCC(<, UByte);
450 IMPLEMENT_SETCC(<, SByte);
451 IMPLEMENT_SETCC(<, UShort);
452 IMPLEMENT_SETCC(<, Short);
453 IMPLEMENT_SETCC(<, UInt);
454 IMPLEMENT_SETCC(<, Int);
455 IMPLEMENT_SETCC(<, ULong);
456 IMPLEMENT_SETCC(<, Long);
457 IMPLEMENT_SETCC(<, Float);
458 IMPLEMENT_SETCC(<, Double);
459 IMPLEMENT_POINTERSETCC(<);
461 std::cout << "Unhandled type for SetLT instruction: " << *Ty << "\n";
467 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
470 switch (Ty->getPrimitiveID()) {
471 IMPLEMENT_SETCC(>, UByte);
472 IMPLEMENT_SETCC(>, SByte);
473 IMPLEMENT_SETCC(>, UShort);
474 IMPLEMENT_SETCC(>, Short);
475 IMPLEMENT_SETCC(>, UInt);
476 IMPLEMENT_SETCC(>, Int);
477 IMPLEMENT_SETCC(>, ULong);
478 IMPLEMENT_SETCC(>, Long);
479 IMPLEMENT_SETCC(>, Float);
480 IMPLEMENT_SETCC(>, Double);
481 IMPLEMENT_POINTERSETCC(>);
483 std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n";
489 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
490 ExecutionContext &SF = ECStack.back();
491 const Type *Ty = I.getOperand(0)->getType();
492 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
493 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
494 GenericValue R; // Result
496 switch (I.getOpcode()) {
497 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break;
498 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break;
499 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break;
500 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break;
501 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break;
502 case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break;
503 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break;
504 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break;
505 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break;
506 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break;
507 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break;
508 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break;
509 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break;
510 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break;
512 std::cout << "Don't know how to handle this binary operator!\n-->" << I;
519 //===----------------------------------------------------------------------===//
520 // Terminator Instruction Implementations
521 //===----------------------------------------------------------------------===//
523 // PerformExitStuff - Print out counters and profiling information if
525 void Interpreter::PerformExitStuff() {
526 #ifdef PROFILE_STRUCTURE_FIELDS
527 // Print out structure field accounting information...
528 if (!FieldAccessCounts.empty()) {
529 CW << "Profile Field Access Counts:\n";
530 std::map<const StructType *, std::vector<unsigned> >::iterator
531 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
532 for (; I != E; ++I) {
533 std::vector<unsigned> &OfC = I->second;
534 CW << " '" << (Value*)I->first << "'\t- Sum=";
537 for (unsigned i = 0; i < OfC.size(); ++i)
541 for (unsigned i = 0; i < OfC.size(); ++i) {
549 CW << "Profile Field Access Percentages:\n";
550 std::cout.precision(3);
551 for (I = FieldAccessCounts.begin(); I != E; ++I) {
552 std::vector<unsigned> &OfC = I->second;
554 for (unsigned i = 0; i < OfC.size(); ++i)
557 CW << " '" << (Value*)I->first << "'\t- ";
558 for (unsigned i = 0; i < OfC.size(); ++i) {
560 CW << double(OfC[i])/Sum;
566 FieldAccessCounts.clear();
571 void Interpreter::exitCalled(GenericValue GV) {
573 std::cout << "Program returned ";
574 print(Type::IntTy, GV);
575 std::cout << " via 'void exit(int)'\n";
578 ExitCode = GV.SByteVal;
582 void Interpreter::visitReturnInst(ReturnInst &I) {
583 ExecutionContext &SF = ECStack.back();
584 const Type *RetTy = 0;
587 // Save away the return value... (if we are not 'ret void')
588 if (I.getNumOperands()) {
589 RetTy = I.getReturnValue()->getType();
590 Result = getOperandValue(I.getReturnValue(), SF);
593 // Save previously executing meth
594 const Function *M = ECStack.back().CurFunction;
596 // Pop the current stack frame... this invalidates SF
599 if (ECStack.empty()) { // Finished main. Put result into exit code...
600 if (RetTy) { // Nonvoid return type?
602 CW << "Function " << M->getType() << " \"" << M->getName()
604 print(RetTy, Result);
608 if (RetTy->isIntegral())
609 ExitCode = Result.IntVal; // Capture the exit code of the program
616 // If we have a previous stack frame, and we have a previous call, fill in
617 // the return value...
619 ExecutionContext &NewSF = ECStack.back();
621 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
622 SetValue(NewSF.Caller, Result, NewSF);
624 NewSF.Caller = 0; // We returned from the call...
625 } else if (!QuietMode) {
626 // This must be a function that is executing because of a user 'call'
628 CW << "Function " << M->getType() << " \"" << M->getName()
630 print(RetTy, Result);
635 void Interpreter::visitBranchInst(BranchInst &I) {
636 ExecutionContext &SF = ECStack.back();
639 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
640 if (!I.isUnconditional()) {
641 Value *Cond = I.getCondition();
642 if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond...
643 Dest = I.getSuccessor(1);
645 SwitchToNewBasicBlock(Dest, SF);
648 void Interpreter::visitSwitchInst(SwitchInst &I) {
649 ExecutionContext &SF = ECStack.back();
650 GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
651 const Type *ElTy = I.getOperand(0)->getType();
653 // Check to see if any of the cases match...
654 BasicBlock *Dest = 0;
655 for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
656 if (executeSetEQInst(CondVal,
657 getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) {
658 Dest = cast<BasicBlock>(I.getOperand(i+1));
662 if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default
663 SwitchToNewBasicBlock(Dest, SF);
666 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
667 // This function handles the actual updating of block and instruction iterators
668 // as well as execution of all of the PHI nodes in the destination block.
670 // This method does this because all of the PHI nodes must be executed
671 // atomically, reading their inputs before any of the results are updated. Not
672 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
673 // their inputs. If the input PHI node is updated before it is read, incorrect
674 // results can happen. Thus we use a two phase approach.
676 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
677 BasicBlock *PrevBB = SF.CurBB; // Remember where we came from...
678 SF.CurBB = Dest; // Update CurBB to branch destination
679 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
681 if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do
683 // Loop over all of the PHI nodes in the current block, reading their inputs.
684 std::vector<GenericValue> ResultValues;
686 for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
687 if (Trace) CW << "Run:" << PN;
689 // Search for the value corresponding to this previous bb...
690 int i = PN->getBasicBlockIndex(PrevBB);
691 assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
692 Value *IncomingValue = PN->getIncomingValue(i);
694 // Save the incoming value for this PHI node...
695 ResultValues.push_back(getOperandValue(IncomingValue, SF));
698 // Now loop over all of the PHI nodes setting their values...
699 SF.CurInst = SF.CurBB->begin();
700 for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst);
702 SetValue(PN, ResultValues[i], SF);
706 //===----------------------------------------------------------------------===//
707 // Memory Instruction Implementations
708 //===----------------------------------------------------------------------===//
710 void Interpreter::visitAllocationInst(AllocationInst &I) {
711 ExecutionContext &SF = ECStack.back();
713 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
715 // Get the number of elements being allocated by the array...
716 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
718 // Allocate enough memory to hold the type...
719 // FIXME: Don't use CALLOC, use a tainted malloc.
720 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
722 GenericValue Result = PTOGV(Memory);
723 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
724 SetValue(&I, Result, SF);
726 if (I.getOpcode() == Instruction::Alloca)
727 ECStack.back().Allocas.add(Memory);
730 void Interpreter::visitFreeInst(FreeInst &I) {
731 ExecutionContext &SF = ECStack.back();
732 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
733 GenericValue Value = getOperandValue(I.getOperand(0), SF);
734 // TODO: Check to make sure memory is allocated
735 free(GVTOP(Value)); // Free memory
739 // getElementOffset - The workhorse for getelementptr.
741 GenericValue Interpreter::executeGEPOperation(Value *Ptr, User::op_iterator I,
743 ExecutionContext &SF) {
744 assert(isa<PointerType>(Ptr->getType()) &&
745 "Cannot getElementOffset of a nonpointer type!");
748 const Type *Ty = Ptr->getType();
750 for (; I != E; ++I) {
751 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
752 const StructLayout *SLO = TD.getStructLayout(STy);
754 // Indicies must be ubyte constants...
755 const ConstantUInt *CPU = cast<ConstantUInt>(*I);
756 assert(CPU->getType() == Type::UByteTy);
757 unsigned Index = CPU->getValue();
759 #ifdef PROFILE_STRUCTURE_FIELDS
760 if (ProfileStructureFields) {
761 // Do accounting for this field...
762 std::vector<unsigned> &OfC = FieldAccessCounts[STy];
763 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
768 Total += SLO->MemberOffsets[Index];
769 Ty = STy->getElementTypes()[Index];
770 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
772 // Get the index number for the array... which must be long type...
773 assert((*I)->getType() == Type::LongTy);
774 unsigned Idx = getOperandValue(*I, SF).LongVal;
775 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
776 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
777 std::cerr << "Out of range memory access to element #" << Idx
778 << " of a " << AT->getNumElements() << " element array."
779 << " Subscript #" << *I << "\n";
781 siglongjmp(SignalRecoverBuffer, SIGTRAP);
784 Ty = ST->getElementType();
785 unsigned Size = TD.getTypeSize(Ty);
791 Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total;
795 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
796 ExecutionContext &SF = ECStack.back();
797 SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(),
798 I.idx_begin(), I.idx_end(), SF), SF);
801 void Interpreter::visitLoadInst(LoadInst &I) {
802 ExecutionContext &SF = ECStack.back();
803 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
804 GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
805 GenericValue Result = LoadValueFromMemory(Ptr, I.getType());
806 SetValue(&I, Result, SF);
809 void Interpreter::visitStoreInst(StoreInst &I) {
810 ExecutionContext &SF = ECStack.back();
811 GenericValue Val = getOperandValue(I.getOperand(0), SF);
812 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
813 StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
814 I.getOperand(0)->getType());
819 //===----------------------------------------------------------------------===//
820 // Miscellaneous Instruction Implementations
821 //===----------------------------------------------------------------------===//
823 void Interpreter::visitCallInst(CallInst &I) {
824 ExecutionContext &SF = ECStack.back();
826 std::vector<GenericValue> ArgVals;
827 ArgVals.reserve(I.getNumOperands()-1);
828 for (unsigned i = 1; i < I.getNumOperands(); ++i) {
829 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
830 // Promote all integral types whose size is < sizeof(int) into ints. We do
831 // this by zero or sign extending the value as appropriate according to the
833 if (I.getOperand(i)->getType()->isIntegral() &&
834 I.getOperand(i)->getType()->getPrimitiveSize() < 4) {
835 const Type *Ty = I.getOperand(i)->getType();
836 if (Ty == Type::ShortTy)
837 ArgVals.back().IntVal = ArgVals.back().ShortVal;
838 else if (Ty == Type::UShortTy)
839 ArgVals.back().UIntVal = ArgVals.back().UShortVal;
840 else if (Ty == Type::SByteTy)
841 ArgVals.back().IntVal = ArgVals.back().SByteVal;
842 else if (Ty == Type::UByteTy)
843 ArgVals.back().UIntVal = ArgVals.back().UByteVal;
844 else if (Ty == Type::BoolTy)
845 ArgVals.back().UIntVal = ArgVals.back().BoolVal;
847 assert(0 && "Unknown type!");
851 // To handle indirect calls, we must get the pointer value from the argument
852 // and treat it as a function pointer.
853 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
854 callFunction((Function*)GVTOP(SRC), ArgVals);
857 #define IMPLEMENT_SHIFT(OP, TY) \
858 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
860 void Interpreter::visitShl(ShiftInst &I) {
861 ExecutionContext &SF = ECStack.back();
862 const Type *Ty = I.getOperand(0)->getType();
863 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
864 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
867 switch (Ty->getPrimitiveID()) {
868 IMPLEMENT_SHIFT(<<, UByte);
869 IMPLEMENT_SHIFT(<<, SByte);
870 IMPLEMENT_SHIFT(<<, UShort);
871 IMPLEMENT_SHIFT(<<, Short);
872 IMPLEMENT_SHIFT(<<, UInt);
873 IMPLEMENT_SHIFT(<<, Int);
874 IMPLEMENT_SHIFT(<<, ULong);
875 IMPLEMENT_SHIFT(<<, Long);
877 std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n";
879 SetValue(&I, Dest, SF);
882 void Interpreter::visitShr(ShiftInst &I) {
883 ExecutionContext &SF = ECStack.back();
884 const Type *Ty = I.getOperand(0)->getType();
885 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
886 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
889 switch (Ty->getPrimitiveID()) {
890 IMPLEMENT_SHIFT(>>, UByte);
891 IMPLEMENT_SHIFT(>>, SByte);
892 IMPLEMENT_SHIFT(>>, UShort);
893 IMPLEMENT_SHIFT(>>, Short);
894 IMPLEMENT_SHIFT(>>, UInt);
895 IMPLEMENT_SHIFT(>>, Int);
896 IMPLEMENT_SHIFT(>>, ULong);
897 IMPLEMENT_SHIFT(>>, Long);
899 std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n";
902 SetValue(&I, Dest, SF);
905 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
906 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
908 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
909 case Type::DESTTY##TyID: \
910 switch (SrcTy->getPrimitiveID()) { \
911 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
912 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
913 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
914 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
915 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
916 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
917 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
918 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
919 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
920 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
922 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
923 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
924 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
926 #define IMPLEMENT_CAST_CASE_END() \
927 default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
932 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
933 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
934 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
935 IMPLEMENT_CAST_CASE_END()
937 static GenericValue executeCastOperation(Value *SrcVal, const Type *Ty,
938 ExecutionContext &SF) {
939 const Type *SrcTy = SrcVal->getType();
940 GenericValue Dest, Src = getOperandValue(SrcVal, SF);
942 switch (Ty->getPrimitiveID()) {
943 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
944 IMPLEMENT_CAST_CASE(SByte , ( signed char));
945 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
946 IMPLEMENT_CAST_CASE(Short , ( signed short));
947 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
948 IMPLEMENT_CAST_CASE(Int , ( signed int ));
949 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
950 IMPLEMENT_CAST_CASE(Long , ( int64_t));
951 IMPLEMENT_CAST_CASE(Pointer, (PointerTy));
952 IMPLEMENT_CAST_CASE(Float , (float));
953 IMPLEMENT_CAST_CASE(Double , (double));
954 IMPLEMENT_CAST_CASE(Bool , (bool));
956 std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n";
964 void Interpreter::visitCastInst(CastInst &I) {
965 ExecutionContext &SF = ECStack.back();
966 SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF);
969 void Interpreter::visitVarArgInst(VarArgInst &I) {
970 ExecutionContext &SF = ECStack.back();
972 // Get the pointer to the valist element. LLI treats the valist in memory as
974 GenericValue VAListPtr = getOperandValue(I.getOperand(0), SF);
977 GenericValue VAList =
978 TheEE->LoadValueFromMemory((GenericValue *)GVTOP(VAListPtr), Type::UIntTy);
980 unsigned Argument = VAList.IntVal++;
982 // Update the valist to point to the next argument...
983 TheEE->StoreValueToMemory(VAList, (GenericValue *)GVTOP(VAListPtr),
987 assert(Argument < SF.VarArgs.size() &&
988 "Accessing past the last vararg argument!");
989 SetValue(&I, SF.VarArgs[Argument], SF);
992 //===----------------------------------------------------------------------===//
993 // Dispatch and Execution Code
994 //===----------------------------------------------------------------------===//
996 FunctionInfo::FunctionInfo(Function *F) : Annotation(FunctionInfoAID) {
997 // Assign slot numbers to the function arguments...
998 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
999 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
1001 // Iterate over all of the instructions...
1002 unsigned InstNum = 0;
1003 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1004 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1005 // For each instruction... Add Annote
1006 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
1009 unsigned FunctionInfo::getValueSlot(const Value *V) {
1010 unsigned Plane = V->getType()->getUniqueID();
1011 if (Plane >= NumPlaneElements.size())
1012 NumPlaneElements.resize(Plane+1, 0);
1013 return NumPlaneElements[Plane]++;
1017 //===----------------------------------------------------------------------===//
1018 // callFunction - Execute the specified function...
1020 void Interpreter::callFunction(Function *F,
1021 const std::vector<GenericValue> &ArgVals) {
1022 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1023 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1024 "Incorrect number of arguments passed into function call!");
1025 if (F->isExternal()) {
1026 GenericValue Result = callExternalFunction(F, ArgVals);
1027 const Type *RetTy = F->getReturnType();
1029 // Copy the result back into the result variable if we are not returning
1031 if (RetTy != Type::VoidTy) {
1032 if (!ECStack.empty() && ECStack.back().Caller) {
1033 ExecutionContext &SF = ECStack.back();
1034 SetValue(SF.Caller, Result, SF);
1036 SF.Caller = 0; // We returned from the call...
1037 } else if (!QuietMode) {
1039 CW << "Function " << F->getType() << " \"" << F->getName()
1041 print(RetTy, Result);
1044 if (RetTy->isIntegral())
1045 ExitCode = Result.IntVal; // Capture the exit code of the program
1052 // Process the function, assigning instruction numbers to the instructions in
1053 // the function. Also calculate the number of values for each type slot
1056 FunctionInfo *FuncInfo =
1057 (FunctionInfo*)F->getOrCreateAnnotation(FunctionInfoAID);
1058 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
1060 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1061 StackFrame.CurFunction = F;
1062 StackFrame.CurBB = F->begin();
1063 StackFrame.CurInst = StackFrame.CurBB->begin();
1064 StackFrame.FuncInfo = FuncInfo;
1066 // Initialize the values to nothing...
1067 StackFrame.Values.resize(FuncInfo->NumPlaneElements.size());
1068 for (unsigned i = 0; i < FuncInfo->NumPlaneElements.size(); ++i) {
1069 StackFrame.Values[i].resize(FuncInfo->NumPlaneElements[i]);
1071 // Taint the initial values of stuff
1072 memset(&StackFrame.Values[i][0], 42,
1073 FuncInfo->NumPlaneElements[i]*sizeof(GenericValue));
1077 // Run through the function arguments and initialize their values...
1078 assert((ArgVals.size() == F->asize() ||
1079 (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) &&
1080 "Invalid number of values passed to function invocation!");
1082 // Handle non-varargs arguments...
1084 for (Function::aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI, ++i)
1085 SetValue(AI, ArgVals[i], StackFrame);
1087 // Handle varargs arguments...
1088 StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
1091 // executeInstruction - Interpret a single instruction, increment the "PC", and
1092 // return true if the next instruction is a breakpoint...
1094 bool Interpreter::executeInstruction() {
1095 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1097 ExecutionContext &SF = ECStack.back(); // Current stack frame
1098 Instruction &I = *SF.CurInst++; // Increment before execute
1100 if (Trace) CW << "Run:" << I;
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 std::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 std::cout << "CTRL-C Detected, execution halted.\n";
1120 InInstruction = false;
1124 InInstruction = true;
1125 visit(I); // Dispatch to one of the visit* methods...
1126 InInstruction = false;
1128 // Reset the current frame location to the top of stack
1129 CurFrame = ECStack.size()-1;
1131 if (CurFrame == -1) return false; // No breakpoint if no code
1133 // Return true if there is a breakpoint annotation on the instruction...
1134 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
1137 void Interpreter::stepInstruction() { // Do the 'step' command
1138 if (ECStack.empty()) {
1139 std::cout << "Error: no program running, cannot step!\n";
1143 // Run an instruction...
1144 executeInstruction();
1146 // Print the next instruction to execute...
1147 printCurrentInstruction();
1151 void Interpreter::nextInstruction() { // Do the 'next' command
1152 if (ECStack.empty()) {
1153 std::cout << "Error: no program running, cannot 'next'!\n";
1157 // If this is a call instruction, step over the call instruction...
1158 // TODO: ICALL, CALL WITH, ...
1159 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
1160 unsigned StackSize = ECStack.size();
1161 // Step into the function...
1162 if (executeInstruction()) {
1163 // Hit a breakpoint, print current instruction, then return to user...
1164 std::cout << "Breakpoint hit!\n";
1165 printCurrentInstruction();
1169 // If we we able to step into the function, finish it now. We might not be
1170 // able the step into a function, if it's external for example.
1171 if (ECStack.size() != StackSize)
1172 finish(); // Finish executing the function...
1174 printCurrentInstruction();
1177 // Normal instruction, just step...
1182 void Interpreter::run() {
1183 if (ECStack.empty()) {
1184 std::cout << "Error: no program running, cannot run!\n";
1188 bool HitBreakpoint = false;
1189 while (!ECStack.empty() && !HitBreakpoint) {
1190 // Run an instruction...
1191 HitBreakpoint = executeInstruction();
1195 std::cout << "Breakpoint hit!\n";
1197 // Print the next instruction to execute...
1198 printCurrentInstruction();
1201 void Interpreter::finish() {
1202 if (ECStack.empty()) {
1203 std::cout << "Error: no program running, cannot run!\n";
1207 unsigned StackSize = ECStack.size();
1208 bool HitBreakpoint = false;
1209 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1210 // Run an instruction...
1211 HitBreakpoint = executeInstruction();
1215 std::cout << "Breakpoint hit!\n";
1217 // Print the next instruction to execute...
1218 printCurrentInstruction();
1223 // printCurrentInstruction - Print out the instruction that the virtual PC is
1224 // at, or fail silently if no program is running.
1226 void Interpreter::printCurrentInstruction() {
1227 if (!ECStack.empty()) {
1228 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1229 WriteAsOperand(std::cout, ECStack.back().CurBB) << ":\n";
1231 Instruction &I = *ECStack.back().CurInst;
1232 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
1233 assert(IN && "Instruction has no numbering annotation!");
1234 std::cout << "#" << IN->InstNum << I;
1238 void Interpreter::printValue(const Type *Ty, GenericValue V) {
1239 switch (Ty->getPrimitiveID()) {
1240 case Type::BoolTyID: std::cout << (V.BoolVal?"true":"false"); break;
1241 case Type::SByteTyID:
1242 std::cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1243 case Type::UByteTyID:
1244 std::cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1245 case Type::ShortTyID: std::cout << V.ShortVal; break;
1246 case Type::UShortTyID: std::cout << V.UShortVal; break;
1247 case Type::IntTyID: std::cout << V.IntVal; break;
1248 case Type::UIntTyID: std::cout << V.UIntVal; break;
1249 case Type::LongTyID: std::cout << (long)V.LongVal; break;
1250 case Type::ULongTyID: std::cout << (unsigned long)V.ULongVal; break;
1251 case Type::FloatTyID: std::cout << V.FloatVal; break;
1252 case Type::DoubleTyID: std::cout << V.DoubleVal; break;
1253 case Type::PointerTyID:std::cout << (void*)GVTOP(V); break;
1255 std::cout << "- Don't know how to print value of this type!";
1260 void Interpreter::print(const Type *Ty, GenericValue V) {
1265 void Interpreter::print(const std::string &Name) {
1266 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1267 if (!PickedVal) return;
1269 if (const Function *F = dyn_cast<Function>(PickedVal)) {
1270 CW << F; // Print the function
1271 } else if (const Type *Ty = dyn_cast<Type>(PickedVal)) {
1272 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
1273 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(PickedVal)) {
1274 CW << BB; // Print the basic block
1275 } else { // Otherwise there should be an annotation for the slot#
1276 print(PickedVal->getType(),
1277 getOperandValue(PickedVal, ECStack[CurFrame]));
1282 void Interpreter::infoValue(const std::string &Name) {
1283 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1284 if (!PickedVal) return;
1286 std::cout << "Value: ";
1287 print(PickedVal->getType(),
1288 getOperandValue(PickedVal, ECStack[CurFrame]));
1290 printOperandInfo(PickedVal, ECStack[CurFrame]);
1293 // printStackFrame - Print information about the specified stack frame, or -1
1294 // for the default one.
1296 void Interpreter::printStackFrame(int FrameNo) {
1297 if (FrameNo == -1) FrameNo = CurFrame;
1298 Function *F = ECStack[FrameNo].CurFunction;
1299 const Type *RetTy = F->getReturnType();
1301 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1302 << (Value*)RetTy << " \"" << F->getName() << "\"(";
1305 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
1306 if (i != 0) std::cout << ", ";
1309 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
1314 if (FrameNo != int(ECStack.size()-1)) {
1315 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1318 CW << *ECStack[FrameNo].CurInst;