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/iOther.h"
10 #include "llvm/iTerminators.h"
11 #include "llvm/iMemory.h"
12 #include "llvm/Type.h"
13 #include "llvm/ConstPoolVals.h"
14 #include "llvm/Assembly/Writer.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Target/TargetData.h"
18 static unsigned getOperandSlot(Value *V) {
19 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
20 assert(SN && "Operand does not have a slot number annotation!");
24 #define GET_CONST_VAL(TY, CLASS) \
25 case Type::TY##TyID: Result.TY##Val = ((CLASS*)CPV)->getValue(); break
27 static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
28 if (ConstPoolVal *CPV = V->castConstant()) {
30 switch (CPV->getType()->getPrimitiveID()) {
31 GET_CONST_VAL(Bool , ConstPoolBool);
32 GET_CONST_VAL(UByte , ConstPoolUInt);
33 GET_CONST_VAL(SByte , ConstPoolSInt);
34 GET_CONST_VAL(UShort , ConstPoolUInt);
35 GET_CONST_VAL(Short , ConstPoolSInt);
36 GET_CONST_VAL(UInt , ConstPoolUInt);
37 GET_CONST_VAL(Int , ConstPoolSInt);
38 GET_CONST_VAL(Float , ConstPoolFP);
39 GET_CONST_VAL(Double , ConstPoolFP);
41 cout << "ERROR: Constant unimp for type: " << CPV->getType() << endl;
45 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
46 return SF.Values[TyP][getOperandSlot(V)];
50 static void printOperandInfo(Value *V, ExecutionContext &SF) {
51 if (!V->isConstant()) {
52 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
53 unsigned Slot = getOperandSlot(V);
54 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
55 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF << endl;
61 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
62 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
64 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << endl;
65 SF.Values[TyP][getOperandSlot(V)] = Val;
70 //===----------------------------------------------------------------------===//
71 // Binary Instruction Implementations
72 //===----------------------------------------------------------------------===//
74 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
75 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
76 #define IMPLEMENT_BINARY_PTR_OPERATOR(OP) \
77 case Type::PointerTyID: Dest.PointerVal = \
78 (GenericValue*)((unsigned long)Src1.PointerVal OP (unsigned long)Src2.PointerVal); break
80 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
81 const Type *Ty, ExecutionContext &SF) {
83 switch (Ty->getPrimitiveID()) {
84 IMPLEMENT_BINARY_OPERATOR(+, UByte);
85 IMPLEMENT_BINARY_OPERATOR(+, SByte);
86 IMPLEMENT_BINARY_OPERATOR(+, UShort);
87 IMPLEMENT_BINARY_OPERATOR(+, Short);
88 IMPLEMENT_BINARY_OPERATOR(+, UInt);
89 IMPLEMENT_BINARY_OPERATOR(+, Int);
90 IMPLEMENT_BINARY_OPERATOR(+, Float);
91 IMPLEMENT_BINARY_OPERATOR(+, Double);
92 IMPLEMENT_BINARY_PTR_OPERATOR(+);
96 cout << "Unhandled type for Add instruction: " << Ty << endl;
101 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
102 const Type *Ty, ExecutionContext &SF) {
104 switch (Ty->getPrimitiveID()) {
105 IMPLEMENT_BINARY_OPERATOR(-, UByte);
106 IMPLEMENT_BINARY_OPERATOR(-, SByte);
107 IMPLEMENT_BINARY_OPERATOR(-, UShort);
108 IMPLEMENT_BINARY_OPERATOR(-, Short);
109 IMPLEMENT_BINARY_OPERATOR(-, UInt);
110 IMPLEMENT_BINARY_OPERATOR(-, Int);
111 IMPLEMENT_BINARY_OPERATOR(-, Float);
112 IMPLEMENT_BINARY_OPERATOR(-, Double);
113 IMPLEMENT_BINARY_PTR_OPERATOR(-);
114 case Type::ULongTyID:
117 cout << "Unhandled type for Sub instruction: " << Ty << endl;
122 #define IMPLEMENT_SETCC(OP, TY) \
123 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
125 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
126 const Type *Ty, ExecutionContext &SF) {
128 switch (Ty->getPrimitiveID()) {
129 IMPLEMENT_SETCC(==, UByte);
130 IMPLEMENT_SETCC(==, SByte);
131 IMPLEMENT_SETCC(==, UShort);
132 IMPLEMENT_SETCC(==, Short);
133 IMPLEMENT_SETCC(==, UInt);
134 IMPLEMENT_SETCC(==, Int);
135 IMPLEMENT_SETCC(==, Float);
136 IMPLEMENT_SETCC(==, Double);
137 IMPLEMENT_SETCC(==, Pointer);
138 case Type::ULongTyID:
141 cout << "Unhandled type for SetEQ instruction: " << Ty << endl;
146 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
147 const Type *Ty, ExecutionContext &SF) {
149 switch (Ty->getPrimitiveID()) {
150 IMPLEMENT_SETCC(!=, UByte);
151 IMPLEMENT_SETCC(!=, SByte);
152 IMPLEMENT_SETCC(!=, UShort);
153 IMPLEMENT_SETCC(!=, Short);
154 IMPLEMENT_SETCC(!=, UInt);
155 IMPLEMENT_SETCC(!=, Int);
156 IMPLEMENT_SETCC(!=, Float);
157 IMPLEMENT_SETCC(!=, Double);
158 IMPLEMENT_SETCC(!=, Pointer);
159 case Type::ULongTyID:
162 cout << "Unhandled type for SetNE instruction: " << Ty << endl;
167 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
168 const Type *Ty, ExecutionContext &SF) {
170 switch (Ty->getPrimitiveID()) {
171 IMPLEMENT_SETCC(<=, UByte);
172 IMPLEMENT_SETCC(<=, SByte);
173 IMPLEMENT_SETCC(<=, UShort);
174 IMPLEMENT_SETCC(<=, Short);
175 IMPLEMENT_SETCC(<=, UInt);
176 IMPLEMENT_SETCC(<=, Int);
177 IMPLEMENT_SETCC(<=, Float);
178 IMPLEMENT_SETCC(<=, Double);
179 IMPLEMENT_SETCC(<=, Pointer);
180 case Type::ULongTyID:
183 cout << "Unhandled type for SetLE instruction: " << Ty << endl;
188 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
189 const Type *Ty, ExecutionContext &SF) {
191 switch (Ty->getPrimitiveID()) {
192 IMPLEMENT_SETCC(>=, UByte);
193 IMPLEMENT_SETCC(>=, SByte);
194 IMPLEMENT_SETCC(>=, UShort);
195 IMPLEMENT_SETCC(>=, Short);
196 IMPLEMENT_SETCC(>=, UInt);
197 IMPLEMENT_SETCC(>=, Int);
198 IMPLEMENT_SETCC(>=, Float);
199 IMPLEMENT_SETCC(>=, Double);
200 IMPLEMENT_SETCC(>=, Pointer);
201 case Type::ULongTyID:
204 cout << "Unhandled type for SetGE instruction: " << Ty << endl;
209 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
210 const Type *Ty, ExecutionContext &SF) {
212 switch (Ty->getPrimitiveID()) {
213 IMPLEMENT_SETCC(<, UByte);
214 IMPLEMENT_SETCC(<, SByte);
215 IMPLEMENT_SETCC(<, UShort);
216 IMPLEMENT_SETCC(<, Short);
217 IMPLEMENT_SETCC(<, UInt);
218 IMPLEMENT_SETCC(<, Int);
219 IMPLEMENT_SETCC(<, Float);
220 IMPLEMENT_SETCC(<, Double);
221 IMPLEMENT_SETCC(<, Pointer);
222 case Type::ULongTyID:
225 cout << "Unhandled type for SetLT instruction: " << Ty << endl;
230 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
231 const Type *Ty, ExecutionContext &SF) {
233 switch (Ty->getPrimitiveID()) {
234 IMPLEMENT_SETCC(>, UByte);
235 IMPLEMENT_SETCC(>, SByte);
236 IMPLEMENT_SETCC(>, UShort);
237 IMPLEMENT_SETCC(>, Short);
238 IMPLEMENT_SETCC(>, UInt);
239 IMPLEMENT_SETCC(>, Int);
240 IMPLEMENT_SETCC(>, Float);
241 IMPLEMENT_SETCC(>, Double);
242 IMPLEMENT_SETCC(>, Pointer);
243 case Type::ULongTyID:
246 cout << "Unhandled type for SetGT instruction: " << Ty << endl;
251 static void executeBinaryInst(BinaryOperator *I, ExecutionContext &SF) {
252 const Type *Ty = I->getOperand(0)->getType();
253 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
254 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
255 GenericValue R; // Result
257 switch (I->getOpcode()) {
258 case Instruction::Add: R = executeAddInst(Src1, Src2, Ty, SF); break;
259 case Instruction::Sub: R = executeSubInst(Src1, Src2, Ty, SF); break;
260 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
261 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
262 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
263 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
264 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
265 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
267 cout << "Don't know how to handle this binary operator!\n-->" << I;
273 //===----------------------------------------------------------------------===//
274 // Terminator Instruction Implementations
275 //===----------------------------------------------------------------------===//
277 void Interpreter::executeRetInst(ReturnInst *I, ExecutionContext &SF) {
278 const Type *RetTy = 0;
281 // Save away the return value... (if we are not 'ret void')
282 if (I->getNumOperands()) {
283 RetTy = I->getReturnValue()->getType();
284 Result = getOperandValue(I->getReturnValue(), SF);
287 // Save previously executing meth
288 const Method *M = ECStack.back().CurMethod;
290 // Pop the current stack frame... this invalidates SF
293 if (ECStack.empty()) { // Finished main. Put result into exit code...
294 if (RetTy) { // Nonvoid return type?
295 cout << "Method " << M->getType() << " \"" << M->getName()
297 printValue(RetTy, Result);
300 if (RetTy->isIntegral())
301 ExitCode = Result.SByteVal; // Capture the exit code of the program
308 // If we have a previous stack frame, and we have a previous call, fill in
309 // the return value...
311 ExecutionContext &NewSF = ECStack.back();
313 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
314 SetValue(NewSF.Caller, Result, NewSF);
316 NewSF.Caller = 0; // We returned from the call...
318 // This must be a function that is executing because of a user 'call'
320 cout << "Method " << M->getType() << " \"" << M->getName()
322 printValue(RetTy, Result);
327 void Interpreter::executeBrInst(BranchInst *I, ExecutionContext &SF) {
328 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
331 Dest = I->getSuccessor(0); // Uncond branches have a fixed dest...
332 if (!I->isUnconditional()) {
333 if (getOperandValue(I->getCondition(), SF).BoolVal == 0) // If false cond...
334 Dest = I->getSuccessor(1);
336 SF.CurBB = Dest; // Update CurBB to branch destination
337 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
340 //===----------------------------------------------------------------------===//
341 // Memory Instruction Implementations
342 //===----------------------------------------------------------------------===//
344 // Create a TargetData structure to handle memory addressing and size/alignment
347 static TargetData TD("lli Interpreter");
349 void Interpreter::executeAllocInst(AllocationInst *I, ExecutionContext &SF) {
350 const Type *Ty = I->getType()->getValueType(); // Type to be allocated
351 unsigned NumElements = 1;
353 if (I->getNumOperands()) { // Allocating a unsized array type?
354 assert(Ty->isArrayType() && Ty->castArrayType()->isUnsized() &&
355 "Allocation inst with size operand for !unsized array type???");
356 Ty = ((const ArrayType*)Ty)->getElementType(); // Get the actual type...
358 // Get the number of elements being allocated by the array...
359 GenericValue NumEl = getOperandValue(I->getOperand(0), SF);
360 NumElements = NumEl.UIntVal;
363 // Allocate enough memory to hold the type...
365 Result.PointerVal = (GenericValue*)malloc(NumElements * TD.getTypeSize(Ty));
366 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
367 SetValue(I, Result, SF);
369 if (I->getOpcode() == Instruction::Alloca) {
370 // Keep track to free it later...
374 static void executeFreeInst(FreeInst *I, ExecutionContext &SF) {
375 assert(I->getOperand(0)->getType()->isPointerType() && "Freeing nonptr?");
376 GenericValue Value = getOperandValue(I->getOperand(0), SF);
377 // TODO: Check to make sure memory is allocated
378 free(Value.PointerVal); // Free memory
381 static void executeLoadInst(LoadInst *I, ExecutionContext &SF) {
382 assert(I->getNumOperands() == 1 && "NI!");
383 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
386 switch (I->getType()->getPrimitiveID()) {
388 case Type::UByteTyID:
389 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
390 case Type::UShortTyID:
391 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
393 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
394 //case Type::ULongTyID:
395 //case Type::LongTyID: Result.LongVal = Ptr->LongVal; break;
396 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
397 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
398 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
400 cout << "Cannot load value of type " << I->getType() << "!\n";
403 SetValue(I, Result, SF);
406 static void executeStoreInst(StoreInst *I, ExecutionContext &SF) {
407 GenericValue *Ptr = getOperandValue(I->getPtrOperand(), SF).PointerVal;
408 GenericValue Val = getOperandValue(I->getOperand(0), SF);
409 assert(I->getNumOperands() == 2 && "NI!");
411 switch (I->getOperand(0)->getType()->getPrimitiveID()) {
413 case Type::UByteTyID:
414 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
415 case Type::UShortTyID:
416 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
418 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
419 //case Type::ULongTyID:
420 //case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
421 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
422 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
423 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
425 cout << "Cannot store value of type " << I->getType() << "!\n";
430 //===----------------------------------------------------------------------===//
431 // Miscellaneous Instruction Implementations
432 //===----------------------------------------------------------------------===//
434 void Interpreter::executeCallInst(CallInst *I, ExecutionContext &SF) {
435 ECStack.back().Caller = I;
436 vector<GenericValue> ArgVals;
437 ArgVals.reserve(I->getNumOperands()-1);
438 for (unsigned i = 1; i < I->getNumOperands(); ++i)
439 ArgVals.push_back(getOperandValue(I->getOperand(i), SF));
441 callMethod(I->getCalledMethod(), ArgVals);
444 static void executePHINode(PHINode *I, ExecutionContext &SF) {
445 BasicBlock *PrevBB = SF.PrevBB;
446 Value *IncomingValue = 0;
448 // Search for the value corresponding to this previous bb...
449 for (unsigned i = I->getNumIncomingValues(); i > 0;) {
450 if (I->getIncomingBlock(--i) == PrevBB) {
451 IncomingValue = I->getIncomingValue(i);
455 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
457 // Found the value, set as the result...
458 SetValue(I, getOperandValue(IncomingValue, SF), SF);
461 #define IMPLEMENT_SHIFT(OP, TY) \
462 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
464 static void executeShlInst(ShiftInst *I, ExecutionContext &SF) {
465 const Type *Ty = I->getOperand(0)->getType();
466 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
467 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
470 switch (Ty->getPrimitiveID()) {
471 IMPLEMENT_SHIFT(<<, UByte);
472 IMPLEMENT_SHIFT(<<, SByte);
473 IMPLEMENT_SHIFT(<<, UShort);
474 IMPLEMENT_SHIFT(<<, Short);
475 IMPLEMENT_SHIFT(<<, UInt);
476 IMPLEMENT_SHIFT(<<, Int);
477 case Type::ULongTyID:
480 cout << "Unhandled type for Shl instruction: " << Ty << endl;
482 SetValue(I, Dest, SF);
485 static void executeShrInst(ShiftInst *I, ExecutionContext &SF) {
486 const Type *Ty = I->getOperand(0)->getType();
487 GenericValue Src1 = getOperandValue(I->getOperand(0), SF);
488 GenericValue Src2 = getOperandValue(I->getOperand(1), SF);
491 switch (Ty->getPrimitiveID()) {
492 IMPLEMENT_SHIFT(>>, UByte);
493 IMPLEMENT_SHIFT(>>, SByte);
494 IMPLEMENT_SHIFT(>>, UShort);
495 IMPLEMENT_SHIFT(>>, Short);
496 IMPLEMENT_SHIFT(>>, UInt);
497 IMPLEMENT_SHIFT(>>, Int);
498 case Type::ULongTyID:
501 cout << "Unhandled type for Shr instruction: " << Ty << endl;
503 SetValue(I, Dest, SF);
506 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
507 case Type::STY##TyID: Dest.DTY##Val = (DCTY)Src.STY##Val; break;
509 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
510 case Type::DESTTY##TyID: \
511 switch (SrcTy->getPrimitiveID()) { \
512 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
513 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
514 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
515 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
516 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
517 IMPLEMENT_CAST(DESTTY, DESTCTY, Int);
519 #define IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY) \
520 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer)
522 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
523 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
524 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
526 #define IMPLEMENT_CAST_CASE_END() \
527 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << endl; \
532 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
533 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
534 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
535 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
536 IMPLEMENT_CAST_CASE_END()
538 #define IMPLEMENT_CAST_CASE_FP(DESTTY, DESTCTY) \
539 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
540 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
541 IMPLEMENT_CAST_CASE_END()
543 #define IMPLEMENT_CAST_CASE_PTR(DESTTY, DESTCTY) \
544 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
545 IMPLEMENT_CAST_CASE_PTR_IMP(DESTTY, DESTCTY); \
546 IMPLEMENT_CAST_CASE_END()
548 static void executeCastInst(CastInst *I, ExecutionContext &SF) {
549 const Type *Ty = I->getType();
550 const Type *SrcTy = I->getOperand(0)->getType();
551 GenericValue Src = getOperandValue(I->getOperand(0), SF);
554 switch (Ty->getPrimitiveID()) {
555 IMPLEMENT_CAST_CASE(UByte , unsigned char);
556 IMPLEMENT_CAST_CASE(SByte , signed char);
557 IMPLEMENT_CAST_CASE(UShort, unsigned short);
558 IMPLEMENT_CAST_CASE(Short , signed char);
559 IMPLEMENT_CAST_CASE(UInt , unsigned int );
560 IMPLEMENT_CAST_CASE(Int , signed int );
561 IMPLEMENT_CAST_CASE_FP(Float , float);
562 IMPLEMENT_CAST_CASE_FP(Double, double);
563 IMPLEMENT_CAST_CASE_PTR(Pointer, GenericValue *);
564 case Type::ULongTyID:
567 cout << "Unhandled dest type for cast instruction: " << Ty << endl;
569 SetValue(I, Dest, SF);
575 //===----------------------------------------------------------------------===//
576 // Dispatch and Execution Code
577 //===----------------------------------------------------------------------===//
579 MethodInfo::MethodInfo(Method *M) : Annotation(MethodInfoAID) {
580 // Assign slot numbers to the method arguments...
581 const Method::ArgumentListType &ArgList = M->getArgumentList();
582 for (Method::ArgumentListType::const_iterator AI = ArgList.begin(),
583 AE = ArgList.end(); AI != AE; ++AI) {
584 MethodArgument *MA = *AI;
585 MA->addAnnotation(new SlotNumber(getValueSlot(MA)));
588 // Iterate over all of the instructions...
589 unsigned InstNum = 0;
590 for (Method::inst_iterator MI = M->inst_begin(), ME = M->inst_end();
592 Instruction *I = *MI; // For each instruction...
593 I->addAnnotation(new InstNumber(++InstNum, getValueSlot(I))); // Add Annote
597 unsigned MethodInfo::getValueSlot(const Value *V) {
598 unsigned Plane = V->getType()->getUniqueID();
599 if (Plane >= NumPlaneElements.size())
600 NumPlaneElements.resize(Plane+1, 0);
601 return NumPlaneElements[Plane]++;
605 void Interpreter::initializeExecutionEngine() {
606 AnnotationManager::registerAnnotationFactory(MethodInfoAID, CreateMethodInfo);
609 //===----------------------------------------------------------------------===//
610 // callMethod - Execute the specified method...
612 void Interpreter::callMethod(Method *M, const vector<GenericValue> &ArgVals) {
613 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
614 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
615 "Incorrect number of arguments passed into function call!");
616 if (M->isExternal()) {
617 callExternalMethod(M, ArgVals);
621 // Process the method, assigning instruction numbers to the instructions in
622 // the method. Also calculate the number of values for each type slot active.
624 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
625 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
627 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
628 StackFrame.CurMethod = M;
629 StackFrame.CurBB = M->front();
630 StackFrame.CurInst = StackFrame.CurBB->begin();
631 StackFrame.MethInfo = MethInfo;
633 // Initialize the values to nothing...
634 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
635 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i)
636 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
638 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
641 // Run through the method arguments and initialize their values...
643 for (Method::ArgumentListType::iterator MI = M->getArgumentList().begin(),
644 ME = M->getArgumentList().end(); MI != ME; ++MI, ++i) {
645 SetValue(*MI, ArgVals[i], StackFrame);
649 // executeInstruction - Interpret a single instruction, increment the "PC", and
650 // return true if the next instruction is a breakpoint...
652 bool Interpreter::executeInstruction() {
653 assert(!ECStack.empty() && "No program running, cannot execute inst!");
655 ExecutionContext &SF = ECStack.back(); // Current stack frame
656 Instruction *I = *SF.CurInst++; // Increment before execute
658 if (I->isBinaryOp()) {
659 executeBinaryInst((BinaryOperator*)I, SF);
661 switch (I->getOpcode()) {
663 case Instruction::Ret: executeRetInst ((ReturnInst*)I, SF); break;
664 case Instruction::Br: executeBrInst ((BranchInst*)I, SF); break;
665 // Memory Instructions
666 case Instruction::Alloca:
667 case Instruction::Malloc: executeAllocInst ((AllocationInst*)I, SF); break;
668 case Instruction::Free: executeFreeInst ((FreeInst*) I, SF); break;
669 case Instruction::Load: executeLoadInst ((LoadInst*) I, SF); break;
670 case Instruction::Store: executeStoreInst ((StoreInst*) I, SF); break;
672 // Miscellaneous Instructions
673 case Instruction::Call: executeCallInst ((CallInst*) I, SF); break;
674 case Instruction::PHINode: executePHINode ((PHINode*) I, SF); break;
675 case Instruction::Shl: executeShlInst ((ShiftInst*) I, SF); break;
676 case Instruction::Shr: executeShrInst ((ShiftInst*) I, SF); break;
677 case Instruction::Cast: executeCastInst ((CastInst*) I, SF); break;
679 cout << "Don't know how to execute this instruction!\n-->" << I;
683 // Reset the current frame location to the top of stack
684 CurFrame = ECStack.size()-1;
686 if (CurFrame == -1) return false; // No breakpoint if no code
688 // Return true if there is a breakpoint annotation on the instruction...
689 return (*ECStack[CurFrame].CurInst)->getAnnotation(BreakpointAID) != 0;
692 void Interpreter::stepInstruction() { // Do the 'step' command
693 if (ECStack.empty()) {
694 cout << "Error: no program running, cannot step!\n";
698 // Run an instruction...
699 executeInstruction();
701 // Print the next instruction to execute...
702 printCurrentInstruction();
709 void Interpreter::nextInstruction() { // Do the 'next' command
710 if (ECStack.empty()) {
711 cout << "Error: no program running, cannot 'next'!\n";
715 // If this is a call instruction, step over the call instruction...
716 // TODO: ICALL, CALL WITH, ...
717 if ((*ECStack.back().CurInst)->getOpcode() == Instruction::Call) {
718 // Step into the function...
719 if (executeInstruction()) {
720 // Hit a breakpoint, print current instruction, then return to user...
721 cout << "Breakpoint hit!\n";
722 printCurrentInstruction();
726 // Finish executing the function...
729 // Normal instruction, just step...
734 void Interpreter::run() {
735 if (ECStack.empty()) {
736 cout << "Error: no program running, cannot run!\n";
740 bool HitBreakpoint = false;
741 while (!ECStack.empty() && !HitBreakpoint) {
742 // Run an instruction...
743 HitBreakpoint = executeInstruction();
747 cout << "Breakpoint hit!\n";
750 // Print the next instruction to execute...
751 printCurrentInstruction();
754 void Interpreter::finish() {
755 if (ECStack.empty()) {
756 cout << "Error: no program running, cannot run!\n";
760 unsigned StackSize = ECStack.size();
761 bool HitBreakpoint = false;
762 while (ECStack.size() >= StackSize && !HitBreakpoint) {
763 // Run an instruction...
764 HitBreakpoint = executeInstruction();
768 cout << "Breakpoint hit!\n";
771 // Print the next instruction to execute...
772 printCurrentInstruction();
777 // printCurrentInstruction - Print out the instruction that the virtual PC is
778 // at, or fail silently if no program is running.
780 void Interpreter::printCurrentInstruction() {
781 if (!ECStack.empty()) {
782 Instruction *I = *ECStack.back().CurInst;
783 InstNumber *IN = (InstNumber*)I->getAnnotation(SlotNumberAID);
784 assert(IN && "Instruction has no numbering annotation!");
785 cout << "#" << IN->InstNum << I;
789 void Interpreter::printValue(const Type *Ty, GenericValue V) {
792 switch (Ty->getPrimitiveID()) {
793 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
794 case Type::SByteTyID: cout << V.SByteVal; break;
795 case Type::UByteTyID: cout << V.UByteVal; break;
796 case Type::ShortTyID: cout << V.ShortVal; break;
797 case Type::UShortTyID: cout << V.UShortVal; break;
798 case Type::IntTyID: cout << V.IntVal; break;
799 case Type::UIntTyID: cout << V.UIntVal; break;
800 case Type::FloatTyID: cout << V.FloatVal; break;
801 case Type::DoubleTyID: cout << V.DoubleVal; break;
802 case Type::PointerTyID:cout << V.PointerVal; break;
804 cout << "- Don't know how to print value of this type!";
809 void Interpreter::printValue(const string &Name) {
810 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
811 if (!PickedVal) return;
813 if (const Method *M = dyn_cast<const Method>(PickedVal)) {
814 cout << M; // Print the method
815 } else { // Otherwise there should be an annotation for the slot#
816 printValue(PickedVal->getType(),
817 getOperandValue(PickedVal, ECStack[CurFrame]));
823 void Interpreter::infoValue(const string &Name) {
824 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
825 if (!PickedVal) return;
828 printValue(PickedVal->getType(),
829 getOperandValue(PickedVal, ECStack[CurFrame]));
831 printOperandInfo(PickedVal, ECStack[CurFrame]);
834 void Interpreter::list() {
836 cout << "Error: No program executing!\n";
838 cout << ECStack[CurFrame].CurMethod; // Just print the method out...
841 void Interpreter::printStackTrace() {
842 if (ECStack.empty()) cout << "No program executing!\n";
844 for (unsigned i = 0; i < ECStack.size(); ++i) {
845 cout << (((int)i == CurFrame) ? '>' : '-');
846 cout << "#" << i << ". " << ECStack[i].CurMethod->getType() << " \""
847 << ECStack[i].CurMethod->getName() << "\"(";
850 cout << *ECStack[i].CurInst;