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/iPHINode.h"
10 #include "llvm/iOther.h"
11 #include "llvm/iTerminators.h"
12 #include "llvm/iMemory.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/Constants.h"
15 #include "llvm/Assembly/Writer.h"
16 #include "llvm/Target/TargetData.h"
17 #include "llvm/GlobalVariable.h"
18 #include "Support/CommandLine.h"
19 #include <math.h> // For fmod
28 QuietMode("quiet", cl::desc("Do not emit any non-program output"));
31 QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode));
34 ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks"));
37 AbortOnExceptions("abort-on-exception",
38 cl::desc("Halt execution on a machine exception"));
40 // Create a TargetData structure to handle memory addressing and size/alignment
43 static TargetData TD("lli Interpreter");
44 CachedWriter CW; // Object to accelerate printing of LLVM
47 #ifdef PROFILE_STRUCTURE_FIELDS
49 ProfileStructureFields("profilestructfields",
50 cl::desc("Profile Structure Field Accesses"));
52 static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
55 sigjmp_buf SignalRecoverBuffer;
56 static bool InInstruction = false;
59 static void SigHandler(int Signal) {
61 siglongjmp(SignalRecoverBuffer, Signal);
65 static void initializeSignalHandlers() {
66 struct sigaction Action;
67 Action.sa_handler = SigHandler;
68 Action.sa_flags = SA_SIGINFO;
69 sigemptyset(&Action.sa_mask);
70 sigaction(SIGSEGV, &Action, 0);
71 sigaction(SIGBUS, &Action, 0);
72 sigaction(SIGINT, &Action, 0);
73 sigaction(SIGFPE, &Action, 0);
77 //===----------------------------------------------------------------------===//
78 // Value Manipulation code
79 //===----------------------------------------------------------------------===//
81 static unsigned getOperandSlot(Value *V) {
82 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
83 assert(SN && "Operand does not have a slot number annotation!");
87 #define GET_CONST_VAL(TY, CLASS) \
88 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
90 static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
91 if (Constant *CPV = dyn_cast<Constant>(V)) {
93 switch (CPV->getType()->getPrimitiveID()) {
94 GET_CONST_VAL(Bool , ConstantBool);
95 GET_CONST_VAL(UByte , ConstantUInt);
96 GET_CONST_VAL(SByte , ConstantSInt);
97 GET_CONST_VAL(UShort , ConstantUInt);
98 GET_CONST_VAL(Short , ConstantSInt);
99 GET_CONST_VAL(UInt , ConstantUInt);
100 GET_CONST_VAL(Int , ConstantSInt);
101 GET_CONST_VAL(ULong , ConstantUInt);
102 GET_CONST_VAL(Long , ConstantSInt);
103 GET_CONST_VAL(Float , ConstantFP);
104 GET_CONST_VAL(Double , ConstantFP);
105 case Type::PointerTyID:
106 if (isa<ConstantPointerNull>(CPV)) {
107 Result.PointerVal = 0;
108 } else if (isa<ConstantPointerRef>(CPV)) {
109 assert(0 && "Not implemented!");
111 assert(0 && "Unknown constant pointer type!");
115 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
118 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
119 GlobalAddress *Address =
120 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
122 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
125 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
126 unsigned OpSlot = getOperandSlot(V);
127 assert(TyP < SF.Values.size() &&
128 OpSlot < SF.Values[TyP].size() && "Value out of range!");
129 return SF.Values[TyP][getOperandSlot(V)];
133 static void printOperandInfo(Value *V, ExecutionContext &SF) {
134 if (isa<Constant>(V)) {
135 cout << "Constant Pool Value\n";
136 } else if (isa<GlobalValue>(V)) {
137 cout << "Global Value\n";
139 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
140 unsigned Slot = getOperandSlot(V);
141 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
142 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
145 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
146 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
147 unsigned char Cur = Buf[i];
148 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
149 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
157 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
158 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
160 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
161 SF.Values[TyP][getOperandSlot(V)] = Val;
165 //===----------------------------------------------------------------------===//
166 // Annotation Wrangling code
167 //===----------------------------------------------------------------------===//
169 void Interpreter::initializeExecutionEngine() {
170 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
171 &MethodInfo::Create);
172 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
173 &GlobalAddress::Create);
174 initializeSignalHandlers();
177 // InitializeMemory - Recursive function to apply a Constant value into the
178 // specified memory location...
180 static void InitializeMemory(const Constant *Init, char *Addr) {
181 #define INITIALIZE_MEMORY(TYID, CLASS, TY) \
182 case Type::TYID##TyID: { \
183 TY Tmp = cast<CLASS>(Init)->getValue(); \
184 memcpy(Addr, &Tmp, sizeof(TY)); \
187 switch (Init->getType()->getPrimitiveID()) {
188 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
189 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
190 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
191 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
192 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
193 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
194 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
195 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
196 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
197 INITIALIZE_MEMORY(Float , ConstantFP , float);
198 INITIALIZE_MEMORY(Double , ConstantFP , double);
199 #undef INITIALIZE_MEMORY
201 case Type::ArrayTyID: {
202 const ConstantArray *CPA = cast<ConstantArray>(Init);
203 const vector<Use> &Val = CPA->getValues();
204 unsigned ElementSize =
205 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
206 for (unsigned i = 0; i < Val.size(); ++i)
207 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
211 case Type::StructTyID: {
212 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
213 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
214 const vector<Use> &Val = CPS->getValues();
215 for (unsigned i = 0; i < Val.size(); ++i)
216 InitializeMemory(cast<Constant>(Val[i].get()),
217 Addr+SL->MemberOffsets[i]);
221 case Type::PointerTyID:
222 if (isa<ConstantPointerNull>(Init)) {
224 } else if (const ConstantPointerRef *CPR =
225 dyn_cast<ConstantPointerRef>(Init)) {
226 GlobalAddress *Address =
227 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
228 *(void**)Addr = (GenericValue*)Address->Ptr;
230 assert(0 && "Unknown Constant pointer type!");
235 CW << "Bad Type: " << Init->getType() << "\n";
236 assert(0 && "Unknown constant type to initialize memory with!");
240 Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
241 assert(AID == GlobalAddressAID);
243 // This annotation will only be created on GlobalValue objects...
244 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
246 if (isa<Function>(GVal)) {
247 // The GlobalAddress object for a function is just a pointer to function
248 // itself. Don't delete it when the annotation is gone though!
249 return new GlobalAddress(GVal, false);
252 // Handle the case of a global variable...
253 assert(isa<GlobalVariable>(GVal) &&
254 "Global value found that isn't a function or global variable!");
255 GlobalVariable *GV = cast<GlobalVariable>(GVal);
257 // First off, we must allocate space for the global variable to point at...
258 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
260 // Allocate enough memory to hold the type...
261 void *Addr = calloc(1, TD.getTypeSize(Ty));
262 assert(Addr != 0 && "Null pointer returned by malloc!");
264 // Initialize the memory if there is an initializer...
265 if (GV->hasInitializer())
266 InitializeMemory(GV->getInitializer(), (char*)Addr);
268 return new GlobalAddress(Addr, true); // Simply invoke the ctor
271 //===----------------------------------------------------------------------===//
272 // Binary Instruction Implementations
273 //===----------------------------------------------------------------------===//
275 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
276 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
278 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
279 const Type *Ty, ExecutionContext &SF) {
281 switch (Ty->getPrimitiveID()) {
282 IMPLEMENT_BINARY_OPERATOR(+, UByte);
283 IMPLEMENT_BINARY_OPERATOR(+, SByte);
284 IMPLEMENT_BINARY_OPERATOR(+, UShort);
285 IMPLEMENT_BINARY_OPERATOR(+, Short);
286 IMPLEMENT_BINARY_OPERATOR(+, UInt);
287 IMPLEMENT_BINARY_OPERATOR(+, Int);
288 IMPLEMENT_BINARY_OPERATOR(+, ULong);
289 IMPLEMENT_BINARY_OPERATOR(+, Long);
290 IMPLEMENT_BINARY_OPERATOR(+, Float);
291 IMPLEMENT_BINARY_OPERATOR(+, Double);
292 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
294 cout << "Unhandled type for Add instruction: " << Ty << "\n";
299 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
300 const Type *Ty, ExecutionContext &SF) {
302 switch (Ty->getPrimitiveID()) {
303 IMPLEMENT_BINARY_OPERATOR(-, UByte);
304 IMPLEMENT_BINARY_OPERATOR(-, SByte);
305 IMPLEMENT_BINARY_OPERATOR(-, UShort);
306 IMPLEMENT_BINARY_OPERATOR(-, Short);
307 IMPLEMENT_BINARY_OPERATOR(-, UInt);
308 IMPLEMENT_BINARY_OPERATOR(-, Int);
309 IMPLEMENT_BINARY_OPERATOR(-, ULong);
310 IMPLEMENT_BINARY_OPERATOR(-, Long);
311 IMPLEMENT_BINARY_OPERATOR(-, Float);
312 IMPLEMENT_BINARY_OPERATOR(-, Double);
313 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
315 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
320 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
321 const Type *Ty, ExecutionContext &SF) {
323 switch (Ty->getPrimitiveID()) {
324 IMPLEMENT_BINARY_OPERATOR(*, UByte);
325 IMPLEMENT_BINARY_OPERATOR(*, SByte);
326 IMPLEMENT_BINARY_OPERATOR(*, UShort);
327 IMPLEMENT_BINARY_OPERATOR(*, Short);
328 IMPLEMENT_BINARY_OPERATOR(*, UInt);
329 IMPLEMENT_BINARY_OPERATOR(*, Int);
330 IMPLEMENT_BINARY_OPERATOR(*, ULong);
331 IMPLEMENT_BINARY_OPERATOR(*, Long);
332 IMPLEMENT_BINARY_OPERATOR(*, Float);
333 IMPLEMENT_BINARY_OPERATOR(*, Double);
334 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
336 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
341 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
342 const Type *Ty, ExecutionContext &SF) {
344 switch (Ty->getPrimitiveID()) {
345 IMPLEMENT_BINARY_OPERATOR(/, UByte);
346 IMPLEMENT_BINARY_OPERATOR(/, SByte);
347 IMPLEMENT_BINARY_OPERATOR(/, UShort);
348 IMPLEMENT_BINARY_OPERATOR(/, Short);
349 IMPLEMENT_BINARY_OPERATOR(/, UInt);
350 IMPLEMENT_BINARY_OPERATOR(/, Int);
351 IMPLEMENT_BINARY_OPERATOR(/, ULong);
352 IMPLEMENT_BINARY_OPERATOR(/, Long);
353 IMPLEMENT_BINARY_OPERATOR(/, Float);
354 IMPLEMENT_BINARY_OPERATOR(/, Double);
355 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
357 cout << "Unhandled type for Div instruction: " << Ty << "\n";
362 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
363 const Type *Ty, ExecutionContext &SF) {
365 switch (Ty->getPrimitiveID()) {
366 IMPLEMENT_BINARY_OPERATOR(%, UByte);
367 IMPLEMENT_BINARY_OPERATOR(%, SByte);
368 IMPLEMENT_BINARY_OPERATOR(%, UShort);
369 IMPLEMENT_BINARY_OPERATOR(%, Short);
370 IMPLEMENT_BINARY_OPERATOR(%, UInt);
371 IMPLEMENT_BINARY_OPERATOR(%, Int);
372 IMPLEMENT_BINARY_OPERATOR(%, ULong);
373 IMPLEMENT_BINARY_OPERATOR(%, Long);
374 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
375 case Type::FloatTyID:
376 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
378 case Type::DoubleTyID:
379 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
382 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
387 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
388 const Type *Ty, ExecutionContext &SF) {
390 switch (Ty->getPrimitiveID()) {
391 IMPLEMENT_BINARY_OPERATOR(&, UByte);
392 IMPLEMENT_BINARY_OPERATOR(&, SByte);
393 IMPLEMENT_BINARY_OPERATOR(&, UShort);
394 IMPLEMENT_BINARY_OPERATOR(&, Short);
395 IMPLEMENT_BINARY_OPERATOR(&, UInt);
396 IMPLEMENT_BINARY_OPERATOR(&, Int);
397 IMPLEMENT_BINARY_OPERATOR(&, ULong);
398 IMPLEMENT_BINARY_OPERATOR(&, Long);
399 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
401 cout << "Unhandled type for And instruction: " << Ty << "\n";
407 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
408 const Type *Ty, ExecutionContext &SF) {
410 switch (Ty->getPrimitiveID()) {
411 IMPLEMENT_BINARY_OPERATOR(|, UByte);
412 IMPLEMENT_BINARY_OPERATOR(|, SByte);
413 IMPLEMENT_BINARY_OPERATOR(|, UShort);
414 IMPLEMENT_BINARY_OPERATOR(|, Short);
415 IMPLEMENT_BINARY_OPERATOR(|, UInt);
416 IMPLEMENT_BINARY_OPERATOR(|, Int);
417 IMPLEMENT_BINARY_OPERATOR(|, ULong);
418 IMPLEMENT_BINARY_OPERATOR(|, Long);
419 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
421 cout << "Unhandled type for Or instruction: " << Ty << "\n";
427 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
428 const Type *Ty, ExecutionContext &SF) {
430 switch (Ty->getPrimitiveID()) {
431 IMPLEMENT_BINARY_OPERATOR(^, UByte);
432 IMPLEMENT_BINARY_OPERATOR(^, SByte);
433 IMPLEMENT_BINARY_OPERATOR(^, UShort);
434 IMPLEMENT_BINARY_OPERATOR(^, Short);
435 IMPLEMENT_BINARY_OPERATOR(^, UInt);
436 IMPLEMENT_BINARY_OPERATOR(^, Int);
437 IMPLEMENT_BINARY_OPERATOR(^, ULong);
438 IMPLEMENT_BINARY_OPERATOR(^, Long);
439 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
441 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
447 #define IMPLEMENT_SETCC(OP, TY) \
448 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
450 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
451 const Type *Ty, ExecutionContext &SF) {
453 switch (Ty->getPrimitiveID()) {
454 IMPLEMENT_SETCC(==, UByte);
455 IMPLEMENT_SETCC(==, SByte);
456 IMPLEMENT_SETCC(==, UShort);
457 IMPLEMENT_SETCC(==, Short);
458 IMPLEMENT_SETCC(==, UInt);
459 IMPLEMENT_SETCC(==, Int);
460 IMPLEMENT_SETCC(==, ULong);
461 IMPLEMENT_SETCC(==, Long);
462 IMPLEMENT_SETCC(==, Float);
463 IMPLEMENT_SETCC(==, Double);
464 IMPLEMENT_SETCC(==, Pointer);
466 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
471 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
472 const Type *Ty, ExecutionContext &SF) {
474 switch (Ty->getPrimitiveID()) {
475 IMPLEMENT_SETCC(!=, UByte);
476 IMPLEMENT_SETCC(!=, SByte);
477 IMPLEMENT_SETCC(!=, UShort);
478 IMPLEMENT_SETCC(!=, Short);
479 IMPLEMENT_SETCC(!=, UInt);
480 IMPLEMENT_SETCC(!=, Int);
481 IMPLEMENT_SETCC(!=, ULong);
482 IMPLEMENT_SETCC(!=, Long);
483 IMPLEMENT_SETCC(!=, Float);
484 IMPLEMENT_SETCC(!=, Double);
485 IMPLEMENT_SETCC(!=, Pointer);
488 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
493 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
494 const Type *Ty, ExecutionContext &SF) {
496 switch (Ty->getPrimitiveID()) {
497 IMPLEMENT_SETCC(<=, UByte);
498 IMPLEMENT_SETCC(<=, SByte);
499 IMPLEMENT_SETCC(<=, UShort);
500 IMPLEMENT_SETCC(<=, Short);
501 IMPLEMENT_SETCC(<=, UInt);
502 IMPLEMENT_SETCC(<=, Int);
503 IMPLEMENT_SETCC(<=, ULong);
504 IMPLEMENT_SETCC(<=, Long);
505 IMPLEMENT_SETCC(<=, Float);
506 IMPLEMENT_SETCC(<=, Double);
507 IMPLEMENT_SETCC(<=, Pointer);
509 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
514 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
515 const Type *Ty, ExecutionContext &SF) {
517 switch (Ty->getPrimitiveID()) {
518 IMPLEMENT_SETCC(>=, UByte);
519 IMPLEMENT_SETCC(>=, SByte);
520 IMPLEMENT_SETCC(>=, UShort);
521 IMPLEMENT_SETCC(>=, Short);
522 IMPLEMENT_SETCC(>=, UInt);
523 IMPLEMENT_SETCC(>=, Int);
524 IMPLEMENT_SETCC(>=, ULong);
525 IMPLEMENT_SETCC(>=, Long);
526 IMPLEMENT_SETCC(>=, Float);
527 IMPLEMENT_SETCC(>=, Double);
528 IMPLEMENT_SETCC(>=, Pointer);
530 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
535 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
536 const Type *Ty, ExecutionContext &SF) {
538 switch (Ty->getPrimitiveID()) {
539 IMPLEMENT_SETCC(<, UByte);
540 IMPLEMENT_SETCC(<, SByte);
541 IMPLEMENT_SETCC(<, UShort);
542 IMPLEMENT_SETCC(<, Short);
543 IMPLEMENT_SETCC(<, UInt);
544 IMPLEMENT_SETCC(<, Int);
545 IMPLEMENT_SETCC(<, ULong);
546 IMPLEMENT_SETCC(<, Long);
547 IMPLEMENT_SETCC(<, Float);
548 IMPLEMENT_SETCC(<, Double);
549 IMPLEMENT_SETCC(<, Pointer);
551 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
556 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
557 const Type *Ty, ExecutionContext &SF) {
559 switch (Ty->getPrimitiveID()) {
560 IMPLEMENT_SETCC(>, UByte);
561 IMPLEMENT_SETCC(>, SByte);
562 IMPLEMENT_SETCC(>, UShort);
563 IMPLEMENT_SETCC(>, Short);
564 IMPLEMENT_SETCC(>, UInt);
565 IMPLEMENT_SETCC(>, Int);
566 IMPLEMENT_SETCC(>, ULong);
567 IMPLEMENT_SETCC(>, Long);
568 IMPLEMENT_SETCC(>, Float);
569 IMPLEMENT_SETCC(>, Double);
570 IMPLEMENT_SETCC(>, Pointer);
572 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
577 static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
578 const Type *Ty = I.getOperand(0)->getType();
579 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
580 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
581 GenericValue R; // Result
583 switch (I.getOpcode()) {
584 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
585 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
586 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
587 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
588 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
589 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
590 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
591 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
592 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
593 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
594 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
595 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
596 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
597 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
599 cout << "Don't know how to handle this binary operator!\n-->" << I;
606 //===----------------------------------------------------------------------===//
607 // Terminator Instruction Implementations
608 //===----------------------------------------------------------------------===//
610 static void PerformExitStuff() {
611 #ifdef PROFILE_STRUCTURE_FIELDS
612 // Print out structure field accounting information...
613 if (!FieldAccessCounts.empty()) {
614 CW << "Profile Field Access Counts:\n";
615 std::map<const StructType *, vector<unsigned> >::iterator
616 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
617 for (; I != E; ++I) {
618 vector<unsigned> &OfC = I->second;
619 CW << " '" << (Value*)I->first << "'\t- Sum=";
622 for (unsigned i = 0; i < OfC.size(); ++i)
626 for (unsigned i = 0; i < OfC.size(); ++i) {
634 CW << "Profile Field Access Percentages:\n";
636 for (I = FieldAccessCounts.begin(); I != E; ++I) {
637 vector<unsigned> &OfC = I->second;
639 for (unsigned i = 0; i < OfC.size(); ++i)
642 CW << " '" << (Value*)I->first << "'\t- ";
643 for (unsigned i = 0; i < OfC.size(); ++i) {
645 CW << double(OfC[i])/Sum;
651 FieldAccessCounts.clear();
656 void Interpreter::exitCalled(GenericValue GV) {
658 cout << "Program returned ";
659 print(Type::IntTy, GV);
660 cout << " via 'void exit(int)'\n";
663 ExitCode = GV.SByteVal;
668 void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
669 const Type *RetTy = 0;
672 // Save away the return value... (if we are not 'ret void')
673 if (I.getNumOperands()) {
674 RetTy = I.getReturnValue()->getType();
675 Result = getOperandValue(I.getReturnValue(), SF);
678 // Save previously executing meth
679 const Function *M = ECStack.back().CurMethod;
681 // Pop the current stack frame... this invalidates SF
684 if (ECStack.empty()) { // Finished main. Put result into exit code...
685 if (RetTy) { // Nonvoid return type?
687 CW << "Function " << M->getType() << " \"" << M->getName()
689 print(RetTy, Result);
693 if (RetTy->isIntegral())
694 ExitCode = Result.IntVal; // Capture the exit code of the program
703 // If we have a previous stack frame, and we have a previous call, fill in
704 // the return value...
706 ExecutionContext &NewSF = ECStack.back();
708 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
709 SetValue(NewSF.Caller, Result, NewSF);
711 NewSF.Caller = 0; // We returned from the call...
712 } else if (!QuietMode) {
713 // This must be a function that is executing because of a user 'call'
715 CW << "Function " << M->getType() << " \"" << M->getName()
717 print(RetTy, Result);
722 void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
723 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
726 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
727 if (!I.isUnconditional()) {
728 Value *Cond = I.getCondition();
729 GenericValue CondVal = getOperandValue(Cond, SF);
730 if (CondVal.BoolVal == 0) // If false cond...
731 Dest = I.getSuccessor(1);
733 SF.CurBB = Dest; // Update CurBB to branch destination
734 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
737 //===----------------------------------------------------------------------===//
738 // Memory Instruction Implementations
739 //===----------------------------------------------------------------------===//
741 void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
742 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
744 // Get the number of elements being allocated by the array...
745 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
747 // Allocate enough memory to hold the type...
748 // FIXME: Don't use CALLOC, use a tainted malloc.
749 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
752 Result.PointerVal = (PointerTy)Memory;
753 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
754 SetValue(&I, Result, SF);
756 if (I.getOpcode() == Instruction::Alloca)
757 ECStack.back().Allocas.add(Memory);
760 static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
761 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
762 GenericValue Value = getOperandValue(I.getOperand(0), SF);
763 // TODO: Check to make sure memory is allocated
764 free((void*)Value.PointerVal); // Free memory
768 // getElementOffset - The workhorse for getelementptr, load and store. This
769 // function returns the offset that arguments ArgOff+1 -> NumArgs specify for
770 // the pointer type specified by argument Arg.
772 static PointerTy getElementOffset(MemAccessInst &I, ExecutionContext &SF) {
773 assert(isa<PointerType>(I.getPointerOperand()->getType()) &&
774 "Cannot getElementOffset of a nonpointer type!");
777 const Type *Ty = I.getPointerOperand()->getType();
779 unsigned ArgOff = I.getFirstIndexOperandNumber();
780 while (ArgOff < I.getNumOperands()) {
781 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
782 const StructLayout *SLO = TD.getStructLayout(STy);
784 // Indicies must be ubyte constants...
785 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand(ArgOff++));
786 assert(CPU->getType() == Type::UByteTy);
787 unsigned Index = CPU->getValue();
789 #ifdef PROFILE_STRUCTURE_FIELDS
790 if (ProfileStructureFields) {
791 // Do accounting for this field...
792 vector<unsigned> &OfC = FieldAccessCounts[STy];
793 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
798 Total += SLO->MemberOffsets[Index];
799 Ty = STy->getElementTypes()[Index];
800 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
802 // Get the index number for the array... which must be uint type...
803 assert(I.getOperand(ArgOff)->getType() == Type::UIntTy);
804 unsigned Idx = getOperandValue(I.getOperand(ArgOff++), SF).UIntVal;
805 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
806 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
807 cerr << "Out of range memory access to element #" << Idx
808 << " of a " << AT->getNumElements() << " element array."
809 << " Subscript #" << (ArgOff-I.getFirstIndexOperandNumber())
812 siglongjmp(SignalRecoverBuffer, SIGTRAP);
815 Ty = ST->getElementType();
816 unsigned Size = TD.getTypeSize(Ty);
824 static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
825 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
826 PointerTy SrcPtr = SRC.PointerVal;
829 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
830 SetValue(&I, Result, SF);
833 static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
834 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
835 PointerTy SrcPtr = SRC.PointerVal;
836 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
839 GenericValue *Ptr = (GenericValue*)SrcPtr;
842 switch (I.getType()->getPrimitiveID()) {
844 case Type::UByteTyID:
845 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
846 case Type::UShortTyID:
847 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
849 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
850 case Type::ULongTyID:
851 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
852 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
853 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
854 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
856 cout << "Cannot load value of type " << I.getType() << "!\n";
859 SetValue(&I, Result, SF);
862 static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
863 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
864 PointerTy SrcPtr = SRC.PointerVal;
865 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
867 GenericValue *Ptr = (GenericValue *)SrcPtr;
868 GenericValue Val = getOperandValue(I.getOperand(0), SF);
870 switch (I.getOperand(0)->getType()->getPrimitiveID()) {
872 case Type::UByteTyID:
873 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
874 case Type::UShortTyID:
875 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
877 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
878 case Type::ULongTyID:
879 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
880 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
881 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
882 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
884 cout << "Cannot store value of type " << I.getType() << "!\n";
889 //===----------------------------------------------------------------------===//
890 // Miscellaneous Instruction Implementations
891 //===----------------------------------------------------------------------===//
893 void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
894 ECStack.back().Caller = &I;
895 vector<GenericValue> ArgVals;
896 ArgVals.reserve(I.getNumOperands()-1);
897 for (unsigned i = 1; i < I.getNumOperands(); ++i)
898 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
900 // To handle indirect calls, we must get the pointer value from the argument
901 // and treat it as a function pointer.
902 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
904 callMethod((Function*)SRC.PointerVal, ArgVals);
907 static void executePHINode(PHINode &I, ExecutionContext &SF) {
908 BasicBlock *PrevBB = SF.PrevBB;
909 Value *IncomingValue = 0;
911 // Search for the value corresponding to this previous bb...
912 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
913 if (I.getIncomingBlock(--i) == PrevBB) {
914 IncomingValue = I.getIncomingValue(i);
918 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
920 // Found the value, set as the result...
921 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
924 #define IMPLEMENT_SHIFT(OP, TY) \
925 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
927 static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
928 const Type *Ty = I.getOperand(0)->getType();
929 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
930 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
933 switch (Ty->getPrimitiveID()) {
934 IMPLEMENT_SHIFT(<<, UByte);
935 IMPLEMENT_SHIFT(<<, SByte);
936 IMPLEMENT_SHIFT(<<, UShort);
937 IMPLEMENT_SHIFT(<<, Short);
938 IMPLEMENT_SHIFT(<<, UInt);
939 IMPLEMENT_SHIFT(<<, Int);
940 IMPLEMENT_SHIFT(<<, ULong);
941 IMPLEMENT_SHIFT(<<, Long);
942 IMPLEMENT_SHIFT(<<, Pointer);
944 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
946 SetValue(&I, Dest, SF);
949 static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
950 const Type *Ty = I.getOperand(0)->getType();
951 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
952 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
955 switch (Ty->getPrimitiveID()) {
956 IMPLEMENT_SHIFT(>>, UByte);
957 IMPLEMENT_SHIFT(>>, SByte);
958 IMPLEMENT_SHIFT(>>, UShort);
959 IMPLEMENT_SHIFT(>>, Short);
960 IMPLEMENT_SHIFT(>>, UInt);
961 IMPLEMENT_SHIFT(>>, Int);
962 IMPLEMENT_SHIFT(>>, ULong);
963 IMPLEMENT_SHIFT(>>, Long);
964 IMPLEMENT_SHIFT(>>, Pointer);
966 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
968 SetValue(&I, Dest, SF);
971 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
972 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
974 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
975 case Type::DESTTY##TyID: \
976 switch (SrcTy->getPrimitiveID()) { \
977 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
978 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
979 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
980 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
981 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
982 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
983 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
984 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
985 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
986 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
988 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
989 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
990 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
992 #define IMPLEMENT_CAST_CASE_END() \
993 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
998 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
999 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1000 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
1001 IMPLEMENT_CAST_CASE_END()
1003 static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1004 const Type *Ty = I.getType();
1005 const Type *SrcTy = I.getOperand(0)->getType();
1006 GenericValue Src = getOperandValue(I.getOperand(0), SF);
1009 switch (Ty->getPrimitiveID()) {
1010 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1011 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1012 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1013 IMPLEMENT_CAST_CASE(Short , ( signed short));
1014 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1015 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1016 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1017 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1018 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1019 IMPLEMENT_CAST_CASE(Float , (float));
1020 IMPLEMENT_CAST_CASE(Double , (double));
1022 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
1024 SetValue(&I, Dest, SF);
1030 //===----------------------------------------------------------------------===//
1031 // Dispatch and Execution Code
1032 //===----------------------------------------------------------------------===//
1034 MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
1035 // Assign slot numbers to the function arguments...
1036 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1037 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
1039 // Iterate over all of the instructions...
1040 unsigned InstNum = 0;
1041 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1042 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1043 // For each instruction... Add Annote
1044 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
1047 unsigned MethodInfo::getValueSlot(const Value *V) {
1048 unsigned Plane = V->getType()->getUniqueID();
1049 if (Plane >= NumPlaneElements.size())
1050 NumPlaneElements.resize(Plane+1, 0);
1051 return NumPlaneElements[Plane]++;
1055 //===----------------------------------------------------------------------===//
1056 // callMethod - Execute the specified function...
1058 void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
1059 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1060 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1061 "Incorrect number of arguments passed into function call!");
1062 if (M->isExternal()) {
1063 GenericValue Result = callExternalMethod(M, ArgVals);
1064 const Type *RetTy = M->getReturnType();
1066 // Copy the result back into the result variable if we are not returning
1068 if (RetTy != Type::VoidTy) {
1069 if (!ECStack.empty() && ECStack.back().Caller) {
1070 ExecutionContext &SF = ECStack.back();
1071 SetValue(SF.Caller, Result, SF);
1073 SF.Caller = 0; // We returned from the call...
1074 } else if (!QuietMode) {
1076 CW << "Function " << M->getType() << " \"" << M->getName()
1078 print(RetTy, Result);
1081 if (RetTy->isIntegral())
1082 ExitCode = Result.SByteVal; // Capture the exit code of the program
1089 // Process the function, assigning instruction numbers to the instructions in
1090 // the function. Also calculate the number of values for each type slot
1093 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
1094 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
1096 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1097 StackFrame.CurMethod = M;
1098 StackFrame.CurBB = M->begin();
1099 StackFrame.CurInst = StackFrame.CurBB->begin();
1100 StackFrame.MethInfo = MethInfo;
1102 // Initialize the values to nothing...
1103 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
1104 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
1105 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1107 // Taint the initial values of stuff
1108 memset(&StackFrame.Values[i][0], 42,
1109 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1112 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1115 // Run through the function arguments and initialize their values...
1116 assert(ArgVals.size() == M->asize() &&
1117 "Invalid number of values passed to function invocation!");
1119 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1120 SetValue(AI, ArgVals[i], StackFrame);
1123 // executeInstruction - Interpret a single instruction, increment the "PC", and
1124 // return true if the next instruction is a breakpoint...
1126 bool Interpreter::executeInstruction() {
1127 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1129 ExecutionContext &SF = ECStack.back(); // Current stack frame
1130 Instruction &I = *SF.CurInst++; // Increment before execute
1135 // Set a sigsetjmp buffer so that we can recover if an error happens during
1136 // instruction execution...
1138 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1139 --SF.CurInst; // Back up to erroring instruction
1140 if (SigNo != SIGINT) {
1141 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
1143 // If -abort-on-exception was specified, terminate LLI instead of trying
1146 if (AbortOnExceptions) exit(1);
1147 } else if (SigNo == SIGINT) {
1148 cout << "CTRL-C Detected, execution halted.\n";
1150 InInstruction = false;
1154 InInstruction = true;
1155 if (I.isBinaryOp()) {
1156 executeBinaryInst(cast<BinaryOperator>(I), SF);
1158 switch (I.getOpcode()) {
1160 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1161 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
1162 // Memory Instructions
1163 case Instruction::Alloca:
1164 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
1165 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1166 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1167 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
1168 case Instruction::GetElementPtr:
1169 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
1171 // Miscellaneous Instructions
1172 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1173 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1174 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1175 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1176 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
1178 cout << "Don't know how to execute this instruction!\n-->" << I;
1181 InInstruction = false;
1183 // Reset the current frame location to the top of stack
1184 CurFrame = ECStack.size()-1;
1186 if (CurFrame == -1) return false; // No breakpoint if no code
1188 // Return true if there is a breakpoint annotation on the instruction...
1189 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
1192 void Interpreter::stepInstruction() { // Do the 'step' command
1193 if (ECStack.empty()) {
1194 cout << "Error: no program running, cannot step!\n";
1198 // Run an instruction...
1199 executeInstruction();
1201 // Print the next instruction to execute...
1202 printCurrentInstruction();
1206 void Interpreter::nextInstruction() { // Do the 'next' command
1207 if (ECStack.empty()) {
1208 cout << "Error: no program running, cannot 'next'!\n";
1212 // If this is a call instruction, step over the call instruction...
1213 // TODO: ICALL, CALL WITH, ...
1214 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
1215 unsigned StackSize = ECStack.size();
1216 // Step into the function...
1217 if (executeInstruction()) {
1218 // Hit a breakpoint, print current instruction, then return to user...
1219 cout << "Breakpoint hit!\n";
1220 printCurrentInstruction();
1224 // If we we able to step into the function, finish it now. We might not be
1225 // able the step into a function, if it's external for example.
1226 if (ECStack.size() != StackSize)
1227 finish(); // Finish executing the function...
1229 printCurrentInstruction();
1232 // Normal instruction, just step...
1237 void Interpreter::run() {
1238 if (ECStack.empty()) {
1239 cout << "Error: no program running, cannot run!\n";
1243 bool HitBreakpoint = false;
1244 while (!ECStack.empty() && !HitBreakpoint) {
1245 // Run an instruction...
1246 HitBreakpoint = executeInstruction();
1249 if (HitBreakpoint) {
1250 cout << "Breakpoint hit!\n";
1252 // Print the next instruction to execute...
1253 printCurrentInstruction();
1256 void Interpreter::finish() {
1257 if (ECStack.empty()) {
1258 cout << "Error: no program running, cannot run!\n";
1262 unsigned StackSize = ECStack.size();
1263 bool HitBreakpoint = false;
1264 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1265 // Run an instruction...
1266 HitBreakpoint = executeInstruction();
1269 if (HitBreakpoint) {
1270 cout << "Breakpoint hit!\n";
1273 // Print the next instruction to execute...
1274 printCurrentInstruction();
1279 // printCurrentInstruction - Print out the instruction that the virtual PC is
1280 // at, or fail silently if no program is running.
1282 void Interpreter::printCurrentInstruction() {
1283 if (!ECStack.empty()) {
1284 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1285 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1287 Instruction &I = *ECStack.back().CurInst;
1288 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
1289 assert(IN && "Instruction has no numbering annotation!");
1290 cout << "#" << IN->InstNum << I;
1294 void Interpreter::printValue(const Type *Ty, GenericValue V) {
1295 switch (Ty->getPrimitiveID()) {
1296 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1297 case Type::SByteTyID:
1298 cout << (int)V.SByteVal << " '" << V.SByteVal << "'"; break;
1299 case Type::UByteTyID:
1300 cout << (unsigned)V.UByteVal << " '" << V.UByteVal << "'"; break;
1301 case Type::ShortTyID: cout << V.ShortVal; break;
1302 case Type::UShortTyID: cout << V.UShortVal; break;
1303 case Type::IntTyID: cout << V.IntVal; break;
1304 case Type::UIntTyID: cout << V.UIntVal; break;
1305 case Type::LongTyID: cout << (long)V.LongVal; break;
1306 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
1307 case Type::FloatTyID: cout << V.FloatVal; break;
1308 case Type::DoubleTyID: cout << V.DoubleVal; break;
1309 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
1311 cout << "- Don't know how to print value of this type!";
1316 void Interpreter::print(const Type *Ty, GenericValue V) {
1321 void Interpreter::print(const std::string &Name) {
1322 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1323 if (!PickedVal) return;
1325 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1326 CW << F; // Print the function
1327 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1328 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
1329 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1330 CW << BB; // Print the basic block
1331 } else { // Otherwise there should be an annotation for the slot#
1332 print(PickedVal->getType(),
1333 getOperandValue(PickedVal, ECStack[CurFrame]));
1338 void Interpreter::infoValue(const std::string &Name) {
1339 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1340 if (!PickedVal) return;
1343 print(PickedVal->getType(),
1344 getOperandValue(PickedVal, ECStack[CurFrame]));
1346 printOperandInfo(PickedVal, ECStack[CurFrame]);
1349 // printStackFrame - Print information about the specified stack frame, or -1
1350 // for the default one.
1352 void Interpreter::printStackFrame(int FrameNo) {
1353 if (FrameNo == -1) FrameNo = CurFrame;
1354 Function *F = ECStack[FrameNo].CurMethod;
1355 const Type *RetTy = F->getReturnType();
1357 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1358 << (Value*)RetTy << " \"" << F->getName() << "\"(";
1361 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
1362 if (i != 0) cout << ", ";
1365 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
1370 if (FrameNo != int(ECStack.size()-1)) {
1371 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1374 CW << *ECStack[FrameNo].CurInst;