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
27 cl::Flag QuietMode ("quiet" , "Do not emit any non-program output");
28 cl::Alias QuietModeA("q" , "Alias for -quiet", cl::NoFlags, QuietMode);
29 cl::Flag ArrayChecksEnabled("array-checks", "Enable array bound checks");
30 cl::Flag AbortOnExceptions("abort-on-exception", "Halt execution on a machine exception");
32 // Create a TargetData structure to handle memory addressing and size/alignment
35 static TargetData TD("lli Interpreter");
36 CachedWriter CW; // Object to accelerate printing of LLVM
39 #ifdef PROFILE_STRUCTURE_FIELDS
40 static cl::Flag ProfileStructureFields("profilestructfields",
41 "Profile Structure Field Accesses");
43 static std::map<const StructType *, vector<unsigned> > FieldAccessCounts;
46 sigjmp_buf SignalRecoverBuffer;
47 static bool InInstruction = false;
50 static void SigHandler(int Signal) {
52 siglongjmp(SignalRecoverBuffer, Signal);
56 static void initializeSignalHandlers() {
57 struct sigaction Action;
58 Action.sa_handler = SigHandler;
59 Action.sa_flags = SA_SIGINFO;
60 sigemptyset(&Action.sa_mask);
61 sigaction(SIGSEGV, &Action, 0);
62 sigaction(SIGBUS, &Action, 0);
63 sigaction(SIGINT, &Action, 0);
64 sigaction(SIGFPE, &Action, 0);
68 //===----------------------------------------------------------------------===//
69 // Value Manipulation code
70 //===----------------------------------------------------------------------===//
72 static unsigned getOperandSlot(Value *V) {
73 SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID);
74 assert(SN && "Operand does not have a slot number annotation!");
78 #define GET_CONST_VAL(TY, CLASS) \
79 case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break
81 static GenericValue getOperandValue(Value *V, ExecutionContext &SF) {
82 if (Constant *CPV = dyn_cast<Constant>(V)) {
84 switch (CPV->getType()->getPrimitiveID()) {
85 GET_CONST_VAL(Bool , ConstantBool);
86 GET_CONST_VAL(UByte , ConstantUInt);
87 GET_CONST_VAL(SByte , ConstantSInt);
88 GET_CONST_VAL(UShort , ConstantUInt);
89 GET_CONST_VAL(Short , ConstantSInt);
90 GET_CONST_VAL(UInt , ConstantUInt);
91 GET_CONST_VAL(Int , ConstantSInt);
92 GET_CONST_VAL(ULong , ConstantUInt);
93 GET_CONST_VAL(Long , ConstantSInt);
94 GET_CONST_VAL(Float , ConstantFP);
95 GET_CONST_VAL(Double , ConstantFP);
96 case Type::PointerTyID:
97 if (isa<ConstantPointerNull>(CPV)) {
98 Result.PointerVal = 0;
99 } else if (isa<ConstantPointerRef>(CPV)) {
100 assert(0 && "Not implemented!");
102 assert(0 && "Unknown constant pointer type!");
106 cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n";
109 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
110 GlobalAddress *Address =
111 (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID);
113 Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr;
116 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
117 unsigned OpSlot = getOperandSlot(V);
118 assert(TyP < SF.Values.size() &&
119 OpSlot < SF.Values[TyP].size() && "Value out of range!");
120 return SF.Values[TyP][getOperandSlot(V)];
124 static void printOperandInfo(Value *V, ExecutionContext &SF) {
125 if (isa<Constant>(V)) {
126 cout << "Constant Pool Value\n";
127 } else if (isa<GlobalValue>(V)) {
128 cout << "Global Value\n";
130 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
131 unsigned Slot = getOperandSlot(V);
132 cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot
133 << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF
136 const unsigned char *Buf = (const unsigned char*)&SF.Values[TyP][Slot];
137 for (unsigned i = 0; i < sizeof(GenericValue); ++i) {
138 unsigned char Cur = Buf[i];
139 cout << ( Cur >= 160? char((Cur>>4)+'A'-10) : char((Cur>>4) + '0'))
140 << ((Cur&15) >= 10? char((Cur&15)+'A'-10) : char((Cur&15) + '0'));
148 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
149 unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value
151 //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n";
152 SF.Values[TyP][getOperandSlot(V)] = Val;
156 //===----------------------------------------------------------------------===//
157 // Annotation Wrangling code
158 //===----------------------------------------------------------------------===//
160 void Interpreter::initializeExecutionEngine() {
161 AnnotationManager::registerAnnotationFactory(MethodInfoAID,
162 &MethodInfo::Create);
163 AnnotationManager::registerAnnotationFactory(GlobalAddressAID,
164 &GlobalAddress::Create);
165 initializeSignalHandlers();
168 // InitializeMemory - Recursive function to apply a Constant value into the
169 // specified memory location...
171 static void InitializeMemory(const Constant *Init, char *Addr) {
172 #define INITIALIZE_MEMORY(TYID, CLASS, TY) \
173 case Type::TYID##TyID: { \
174 TY Tmp = cast<CLASS>(Init)->getValue(); \
175 memcpy(Addr, &Tmp, sizeof(TY)); \
178 switch (Init->getType()->getPrimitiveID()) {
179 INITIALIZE_MEMORY(Bool , ConstantBool, bool);
180 INITIALIZE_MEMORY(UByte , ConstantUInt, unsigned char);
181 INITIALIZE_MEMORY(SByte , ConstantSInt, signed char);
182 INITIALIZE_MEMORY(UShort , ConstantUInt, unsigned short);
183 INITIALIZE_MEMORY(Short , ConstantSInt, signed short);
184 INITIALIZE_MEMORY(UInt , ConstantUInt, unsigned int);
185 INITIALIZE_MEMORY(Int , ConstantSInt, signed int);
186 INITIALIZE_MEMORY(ULong , ConstantUInt, uint64_t);
187 INITIALIZE_MEMORY(Long , ConstantSInt, int64_t);
188 INITIALIZE_MEMORY(Float , ConstantFP , float);
189 INITIALIZE_MEMORY(Double , ConstantFP , double);
190 #undef INITIALIZE_MEMORY
192 case Type::ArrayTyID: {
193 const ConstantArray *CPA = cast<ConstantArray>(Init);
194 const vector<Use> &Val = CPA->getValues();
195 unsigned ElementSize =
196 TD.getTypeSize(cast<ArrayType>(CPA->getType())->getElementType());
197 for (unsigned i = 0; i < Val.size(); ++i)
198 InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize);
202 case Type::StructTyID: {
203 const ConstantStruct *CPS = cast<ConstantStruct>(Init);
204 const StructLayout *SL=TD.getStructLayout(cast<StructType>(CPS->getType()));
205 const vector<Use> &Val = CPS->getValues();
206 for (unsigned i = 0; i < Val.size(); ++i)
207 InitializeMemory(cast<Constant>(Val[i].get()),
208 Addr+SL->MemberOffsets[i]);
212 case Type::PointerTyID:
213 if (isa<ConstantPointerNull>(Init)) {
215 } else if (const ConstantPointerRef *CPR =
216 dyn_cast<ConstantPointerRef>(Init)) {
217 GlobalAddress *Address =
218 (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID);
219 *(void**)Addr = (GenericValue*)Address->Ptr;
221 assert(0 && "Unknown Constant pointer type!");
226 CW << "Bad Type: " << Init->getType() << "\n";
227 assert(0 && "Unknown constant type to initialize memory with!");
231 Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){
232 assert(AID == GlobalAddressAID);
234 // This annotation will only be created on GlobalValue objects...
235 GlobalValue *GVal = cast<GlobalValue>((Value*)O);
237 if (isa<Function>(GVal)) {
238 // The GlobalAddress object for a function is just a pointer to function
239 // itself. Don't delete it when the annotation is gone though!
240 return new GlobalAddress(GVal, false);
243 // Handle the case of a global variable...
244 assert(isa<GlobalVariable>(GVal) &&
245 "Global value found that isn't a function or global variable!");
246 GlobalVariable *GV = cast<GlobalVariable>(GVal);
248 // First off, we must allocate space for the global variable to point at...
249 const Type *Ty = GV->getType()->getElementType(); // Type to be allocated
251 // Allocate enough memory to hold the type...
252 void *Addr = calloc(1, TD.getTypeSize(Ty));
253 assert(Addr != 0 && "Null pointer returned by malloc!");
255 // Initialize the memory if there is an initializer...
256 if (GV->hasInitializer())
257 InitializeMemory(GV->getInitializer(), (char*)Addr);
259 return new GlobalAddress(Addr, true); // Simply invoke the ctor
263 //===----------------------------------------------------------------------===//
264 // Unary Instruction Implementations
265 //===----------------------------------------------------------------------===//
267 #define IMPLEMENT_UNARY_OPERATOR(OP, TY) \
268 case Type::TY##TyID: Dest.TY##Val = OP Src.TY##Val; break
270 static void executeNotInst(UnaryOperator &I, ExecutionContext &SF) {
271 const Type *Ty = I.getOperand(0)->getType();
272 GenericValue Src = getOperandValue(I.getOperand(0), SF);
274 switch (Ty->getPrimitiveID()) {
275 IMPLEMENT_UNARY_OPERATOR(~, UByte);
276 IMPLEMENT_UNARY_OPERATOR(~, SByte);
277 IMPLEMENT_UNARY_OPERATOR(~, UShort);
278 IMPLEMENT_UNARY_OPERATOR(~, Short);
279 IMPLEMENT_UNARY_OPERATOR(~, UInt);
280 IMPLEMENT_UNARY_OPERATOR(~, Int);
281 IMPLEMENT_UNARY_OPERATOR(~, ULong);
282 IMPLEMENT_UNARY_OPERATOR(~, Long);
283 IMPLEMENT_UNARY_OPERATOR(~, Pointer);
285 cout << "Unhandled type for Not instruction: " << Ty << "\n";
287 SetValue(&I, Dest, SF);
290 //===----------------------------------------------------------------------===//
291 // Binary Instruction Implementations
292 //===----------------------------------------------------------------------===//
294 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
295 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break
297 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
298 const Type *Ty, ExecutionContext &SF) {
300 switch (Ty->getPrimitiveID()) {
301 IMPLEMENT_BINARY_OPERATOR(+, UByte);
302 IMPLEMENT_BINARY_OPERATOR(+, SByte);
303 IMPLEMENT_BINARY_OPERATOR(+, UShort);
304 IMPLEMENT_BINARY_OPERATOR(+, Short);
305 IMPLEMENT_BINARY_OPERATOR(+, UInt);
306 IMPLEMENT_BINARY_OPERATOR(+, Int);
307 IMPLEMENT_BINARY_OPERATOR(+, ULong);
308 IMPLEMENT_BINARY_OPERATOR(+, Long);
309 IMPLEMENT_BINARY_OPERATOR(+, Float);
310 IMPLEMENT_BINARY_OPERATOR(+, Double);
311 IMPLEMENT_BINARY_OPERATOR(+, Pointer);
313 cout << "Unhandled type for Add instruction: " << Ty << "\n";
318 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
319 const Type *Ty, ExecutionContext &SF) {
321 switch (Ty->getPrimitiveID()) {
322 IMPLEMENT_BINARY_OPERATOR(-, UByte);
323 IMPLEMENT_BINARY_OPERATOR(-, SByte);
324 IMPLEMENT_BINARY_OPERATOR(-, UShort);
325 IMPLEMENT_BINARY_OPERATOR(-, Short);
326 IMPLEMENT_BINARY_OPERATOR(-, UInt);
327 IMPLEMENT_BINARY_OPERATOR(-, Int);
328 IMPLEMENT_BINARY_OPERATOR(-, ULong);
329 IMPLEMENT_BINARY_OPERATOR(-, Long);
330 IMPLEMENT_BINARY_OPERATOR(-, Float);
331 IMPLEMENT_BINARY_OPERATOR(-, Double);
332 IMPLEMENT_BINARY_OPERATOR(-, Pointer);
334 cout << "Unhandled type for Sub instruction: " << Ty << "\n";
339 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
340 const Type *Ty, ExecutionContext &SF) {
342 switch (Ty->getPrimitiveID()) {
343 IMPLEMENT_BINARY_OPERATOR(*, UByte);
344 IMPLEMENT_BINARY_OPERATOR(*, SByte);
345 IMPLEMENT_BINARY_OPERATOR(*, UShort);
346 IMPLEMENT_BINARY_OPERATOR(*, Short);
347 IMPLEMENT_BINARY_OPERATOR(*, UInt);
348 IMPLEMENT_BINARY_OPERATOR(*, Int);
349 IMPLEMENT_BINARY_OPERATOR(*, ULong);
350 IMPLEMENT_BINARY_OPERATOR(*, Long);
351 IMPLEMENT_BINARY_OPERATOR(*, Float);
352 IMPLEMENT_BINARY_OPERATOR(*, Double);
353 IMPLEMENT_BINARY_OPERATOR(*, Pointer);
355 cout << "Unhandled type for Mul instruction: " << Ty << "\n";
360 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
361 const Type *Ty, ExecutionContext &SF) {
363 switch (Ty->getPrimitiveID()) {
364 IMPLEMENT_BINARY_OPERATOR(/, UByte);
365 IMPLEMENT_BINARY_OPERATOR(/, SByte);
366 IMPLEMENT_BINARY_OPERATOR(/, UShort);
367 IMPLEMENT_BINARY_OPERATOR(/, Short);
368 IMPLEMENT_BINARY_OPERATOR(/, UInt);
369 IMPLEMENT_BINARY_OPERATOR(/, Int);
370 IMPLEMENT_BINARY_OPERATOR(/, ULong);
371 IMPLEMENT_BINARY_OPERATOR(/, Long);
372 IMPLEMENT_BINARY_OPERATOR(/, Float);
373 IMPLEMENT_BINARY_OPERATOR(/, Double);
374 IMPLEMENT_BINARY_OPERATOR(/, Pointer);
376 cout << "Unhandled type for Div instruction: " << Ty << "\n";
381 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
382 const Type *Ty, ExecutionContext &SF) {
384 switch (Ty->getPrimitiveID()) {
385 IMPLEMENT_BINARY_OPERATOR(%, UByte);
386 IMPLEMENT_BINARY_OPERATOR(%, SByte);
387 IMPLEMENT_BINARY_OPERATOR(%, UShort);
388 IMPLEMENT_BINARY_OPERATOR(%, Short);
389 IMPLEMENT_BINARY_OPERATOR(%, UInt);
390 IMPLEMENT_BINARY_OPERATOR(%, Int);
391 IMPLEMENT_BINARY_OPERATOR(%, ULong);
392 IMPLEMENT_BINARY_OPERATOR(%, Long);
393 IMPLEMENT_BINARY_OPERATOR(%, Pointer);
394 case Type::FloatTyID:
395 Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
397 case Type::DoubleTyID:
398 Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
401 cout << "Unhandled type for Rem instruction: " << Ty << "\n";
406 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
407 const Type *Ty, ExecutionContext &SF) {
409 switch (Ty->getPrimitiveID()) {
410 IMPLEMENT_BINARY_OPERATOR(&, UByte);
411 IMPLEMENT_BINARY_OPERATOR(&, SByte);
412 IMPLEMENT_BINARY_OPERATOR(&, UShort);
413 IMPLEMENT_BINARY_OPERATOR(&, Short);
414 IMPLEMENT_BINARY_OPERATOR(&, UInt);
415 IMPLEMENT_BINARY_OPERATOR(&, Int);
416 IMPLEMENT_BINARY_OPERATOR(&, ULong);
417 IMPLEMENT_BINARY_OPERATOR(&, Long);
418 IMPLEMENT_BINARY_OPERATOR(&, Pointer);
420 cout << "Unhandled type for And instruction: " << Ty << "\n";
426 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
427 const Type *Ty, ExecutionContext &SF) {
429 switch (Ty->getPrimitiveID()) {
430 IMPLEMENT_BINARY_OPERATOR(|, UByte);
431 IMPLEMENT_BINARY_OPERATOR(|, SByte);
432 IMPLEMENT_BINARY_OPERATOR(|, UShort);
433 IMPLEMENT_BINARY_OPERATOR(|, Short);
434 IMPLEMENT_BINARY_OPERATOR(|, UInt);
435 IMPLEMENT_BINARY_OPERATOR(|, Int);
436 IMPLEMENT_BINARY_OPERATOR(|, ULong);
437 IMPLEMENT_BINARY_OPERATOR(|, Long);
438 IMPLEMENT_BINARY_OPERATOR(|, Pointer);
440 cout << "Unhandled type for Or instruction: " << Ty << "\n";
446 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
447 const Type *Ty, ExecutionContext &SF) {
449 switch (Ty->getPrimitiveID()) {
450 IMPLEMENT_BINARY_OPERATOR(^, UByte);
451 IMPLEMENT_BINARY_OPERATOR(^, SByte);
452 IMPLEMENT_BINARY_OPERATOR(^, UShort);
453 IMPLEMENT_BINARY_OPERATOR(^, Short);
454 IMPLEMENT_BINARY_OPERATOR(^, UInt);
455 IMPLEMENT_BINARY_OPERATOR(^, Int);
456 IMPLEMENT_BINARY_OPERATOR(^, ULong);
457 IMPLEMENT_BINARY_OPERATOR(^, Long);
458 IMPLEMENT_BINARY_OPERATOR(^, Pointer);
460 cout << "Unhandled type for Xor instruction: " << Ty << "\n";
466 #define IMPLEMENT_SETCC(OP, TY) \
467 case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break
469 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
470 const Type *Ty, ExecutionContext &SF) {
472 switch (Ty->getPrimitiveID()) {
473 IMPLEMENT_SETCC(==, UByte);
474 IMPLEMENT_SETCC(==, SByte);
475 IMPLEMENT_SETCC(==, UShort);
476 IMPLEMENT_SETCC(==, Short);
477 IMPLEMENT_SETCC(==, UInt);
478 IMPLEMENT_SETCC(==, Int);
479 IMPLEMENT_SETCC(==, ULong);
480 IMPLEMENT_SETCC(==, Long);
481 IMPLEMENT_SETCC(==, Float);
482 IMPLEMENT_SETCC(==, Double);
483 IMPLEMENT_SETCC(==, Pointer);
485 cout << "Unhandled type for SetEQ instruction: " << Ty << "\n";
490 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
491 const Type *Ty, ExecutionContext &SF) {
493 switch (Ty->getPrimitiveID()) {
494 IMPLEMENT_SETCC(!=, UByte);
495 IMPLEMENT_SETCC(!=, SByte);
496 IMPLEMENT_SETCC(!=, UShort);
497 IMPLEMENT_SETCC(!=, Short);
498 IMPLEMENT_SETCC(!=, UInt);
499 IMPLEMENT_SETCC(!=, Int);
500 IMPLEMENT_SETCC(!=, ULong);
501 IMPLEMENT_SETCC(!=, Long);
502 IMPLEMENT_SETCC(!=, Float);
503 IMPLEMENT_SETCC(!=, Double);
504 IMPLEMENT_SETCC(!=, Pointer);
507 cout << "Unhandled type for SetNE instruction: " << Ty << "\n";
512 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
513 const Type *Ty, ExecutionContext &SF) {
515 switch (Ty->getPrimitiveID()) {
516 IMPLEMENT_SETCC(<=, UByte);
517 IMPLEMENT_SETCC(<=, SByte);
518 IMPLEMENT_SETCC(<=, UShort);
519 IMPLEMENT_SETCC(<=, Short);
520 IMPLEMENT_SETCC(<=, UInt);
521 IMPLEMENT_SETCC(<=, Int);
522 IMPLEMENT_SETCC(<=, ULong);
523 IMPLEMENT_SETCC(<=, Long);
524 IMPLEMENT_SETCC(<=, Float);
525 IMPLEMENT_SETCC(<=, Double);
526 IMPLEMENT_SETCC(<=, Pointer);
528 cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
533 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
534 const Type *Ty, ExecutionContext &SF) {
536 switch (Ty->getPrimitiveID()) {
537 IMPLEMENT_SETCC(>=, UByte);
538 IMPLEMENT_SETCC(>=, SByte);
539 IMPLEMENT_SETCC(>=, UShort);
540 IMPLEMENT_SETCC(>=, Short);
541 IMPLEMENT_SETCC(>=, UInt);
542 IMPLEMENT_SETCC(>=, Int);
543 IMPLEMENT_SETCC(>=, ULong);
544 IMPLEMENT_SETCC(>=, Long);
545 IMPLEMENT_SETCC(>=, Float);
546 IMPLEMENT_SETCC(>=, Double);
547 IMPLEMENT_SETCC(>=, Pointer);
549 cout << "Unhandled type for SetGE instruction: " << Ty << "\n";
554 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
555 const Type *Ty, ExecutionContext &SF) {
557 switch (Ty->getPrimitiveID()) {
558 IMPLEMENT_SETCC(<, UByte);
559 IMPLEMENT_SETCC(<, SByte);
560 IMPLEMENT_SETCC(<, UShort);
561 IMPLEMENT_SETCC(<, Short);
562 IMPLEMENT_SETCC(<, UInt);
563 IMPLEMENT_SETCC(<, Int);
564 IMPLEMENT_SETCC(<, ULong);
565 IMPLEMENT_SETCC(<, Long);
566 IMPLEMENT_SETCC(<, Float);
567 IMPLEMENT_SETCC(<, Double);
568 IMPLEMENT_SETCC(<, Pointer);
570 cout << "Unhandled type for SetLT instruction: " << Ty << "\n";
575 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2,
576 const Type *Ty, ExecutionContext &SF) {
578 switch (Ty->getPrimitiveID()) {
579 IMPLEMENT_SETCC(>, UByte);
580 IMPLEMENT_SETCC(>, SByte);
581 IMPLEMENT_SETCC(>, UShort);
582 IMPLEMENT_SETCC(>, Short);
583 IMPLEMENT_SETCC(>, UInt);
584 IMPLEMENT_SETCC(>, Int);
585 IMPLEMENT_SETCC(>, ULong);
586 IMPLEMENT_SETCC(>, Long);
587 IMPLEMENT_SETCC(>, Float);
588 IMPLEMENT_SETCC(>, Double);
589 IMPLEMENT_SETCC(>, Pointer);
591 cout << "Unhandled type for SetGT instruction: " << Ty << "\n";
596 static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) {
597 const Type *Ty = I.getOperand(0)->getType();
598 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
599 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
600 GenericValue R; // Result
602 switch (I.getOpcode()) {
603 case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break;
604 case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break;
605 case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break;
606 case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break;
607 case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break;
608 case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break;
609 case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break;
610 case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break;
611 case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break;
612 case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break;
613 case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break;
614 case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break;
615 case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break;
616 case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break;
618 cout << "Don't know how to handle this binary operator!\n-->" << I;
625 //===----------------------------------------------------------------------===//
626 // Terminator Instruction Implementations
627 //===----------------------------------------------------------------------===//
629 static void PerformExitStuff() {
630 #ifdef PROFILE_STRUCTURE_FIELDS
631 // Print out structure field accounting information...
632 if (!FieldAccessCounts.empty()) {
633 CW << "Profile Field Access Counts:\n";
634 std::map<const StructType *, vector<unsigned> >::iterator
635 I = FieldAccessCounts.begin(), E = FieldAccessCounts.end();
636 for (; I != E; ++I) {
637 vector<unsigned> &OfC = I->second;
638 CW << " '" << (Value*)I->first << "'\t- Sum=";
641 for (unsigned i = 0; i < OfC.size(); ++i)
645 for (unsigned i = 0; i < OfC.size(); ++i) {
653 CW << "Profile Field Access Percentages:\n";
655 for (I = FieldAccessCounts.begin(); I != E; ++I) {
656 vector<unsigned> &OfC = I->second;
658 for (unsigned i = 0; i < OfC.size(); ++i)
661 CW << " '" << (Value*)I->first << "'\t- ";
662 for (unsigned i = 0; i < OfC.size(); ++i) {
664 CW << double(OfC[i])/Sum;
670 FieldAccessCounts.clear();
675 void Interpreter::exitCalled(GenericValue GV) {
677 cout << "Program returned ";
678 print(Type::IntTy, GV);
679 cout << " via 'void exit(int)'\n";
682 ExitCode = GV.SByteVal;
687 void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) {
688 const Type *RetTy = 0;
691 // Save away the return value... (if we are not 'ret void')
692 if (I.getNumOperands()) {
693 RetTy = I.getReturnValue()->getType();
694 Result = getOperandValue(I.getReturnValue(), SF);
697 // Save previously executing meth
698 const Function *M = ECStack.back().CurMethod;
700 // Pop the current stack frame... this invalidates SF
703 if (ECStack.empty()) { // Finished main. Put result into exit code...
704 if (RetTy) { // Nonvoid return type?
706 CW << "Function " << M->getType() << " \"" << M->getName()
708 print(RetTy, Result);
712 if (RetTy->isIntegral())
713 ExitCode = Result.IntVal; // Capture the exit code of the program
722 // If we have a previous stack frame, and we have a previous call, fill in
723 // the return value...
725 ExecutionContext &NewSF = ECStack.back();
727 if (NewSF.Caller->getType() != Type::VoidTy) // Save result...
728 SetValue(NewSF.Caller, Result, NewSF);
730 NewSF.Caller = 0; // We returned from the call...
731 } else if (!QuietMode) {
732 // This must be a function that is executing because of a user 'call'
734 CW << "Function " << M->getType() << " \"" << M->getName()
736 print(RetTy, Result);
741 void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) {
742 SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work...
745 Dest = I.getSuccessor(0); // Uncond branches have a fixed dest...
746 if (!I.isUnconditional()) {
747 Value *Cond = I.getCondition();
748 GenericValue CondVal = getOperandValue(Cond, SF);
749 if (CondVal.BoolVal == 0) // If false cond...
750 Dest = I.getSuccessor(1);
752 SF.CurBB = Dest; // Update CurBB to branch destination
753 SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr...
756 //===----------------------------------------------------------------------===//
757 // Memory Instruction Implementations
758 //===----------------------------------------------------------------------===//
760 void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) {
761 const Type *Ty = I.getType()->getElementType(); // Type to be allocated
763 // Get the number of elements being allocated by the array...
764 unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal;
766 // Allocate enough memory to hold the type...
767 // FIXME: Don't use CALLOC, use a tainted malloc.
768 void *Memory = calloc(NumElements, TD.getTypeSize(Ty));
771 Result.PointerVal = (PointerTy)Memory;
772 assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
773 SetValue(&I, Result, SF);
775 if (I.getOpcode() == Instruction::Alloca)
776 ECStack.back().Allocas.add(Memory);
779 static void executeFreeInst(FreeInst &I, ExecutionContext &SF) {
780 assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?");
781 GenericValue Value = getOperandValue(I.getOperand(0), SF);
782 // TODO: Check to make sure memory is allocated
783 free((void*)Value.PointerVal); // Free memory
787 // getElementOffset - The workhorse for getelementptr, load and store. This
788 // function returns the offset that arguments ArgOff+1 -> NumArgs specify for
789 // the pointer type specified by argument Arg.
791 static PointerTy getElementOffset(MemAccessInst &I, ExecutionContext &SF) {
792 assert(isa<PointerType>(I.getPointerOperand()->getType()) &&
793 "Cannot getElementOffset of a nonpointer type!");
796 const Type *Ty = I.getPointerOperand()->getType();
798 unsigned ArgOff = I.getFirstIndexOperandNumber();
799 while (ArgOff < I.getNumOperands()) {
800 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
801 const StructLayout *SLO = TD.getStructLayout(STy);
803 // Indicies must be ubyte constants...
804 const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand(ArgOff++));
805 assert(CPU->getType() == Type::UByteTy);
806 unsigned Index = CPU->getValue();
808 #ifdef PROFILE_STRUCTURE_FIELDS
809 if (ProfileStructureFields) {
810 // Do accounting for this field...
811 vector<unsigned> &OfC = FieldAccessCounts[STy];
812 if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size());
817 Total += SLO->MemberOffsets[Index];
818 Ty = STy->getElementTypes()[Index];
819 } else if (const SequentialType *ST = cast<SequentialType>(Ty)) {
821 // Get the index number for the array... which must be uint type...
822 assert(I.getOperand(ArgOff)->getType() == Type::UIntTy);
823 unsigned Idx = getOperandValue(I.getOperand(ArgOff++), SF).UIntVal;
824 if (const ArrayType *AT = dyn_cast<ArrayType>(ST))
825 if (Idx >= AT->getNumElements() && ArrayChecksEnabled) {
826 cerr << "Out of range memory access to element #" << Idx
827 << " of a " << AT->getNumElements() << " element array."
828 << " Subscript #" << (ArgOff-I.getFirstIndexOperandNumber())
831 siglongjmp(SignalRecoverBuffer, SIGTRAP);
834 Ty = ST->getElementType();
835 unsigned Size = TD.getTypeSize(Ty);
843 static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) {
844 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
845 PointerTy SrcPtr = SRC.PointerVal;
848 Result.PointerVal = SrcPtr + getElementOffset(I, SF);
849 SetValue(&I, Result, SF);
852 static void executeLoadInst(LoadInst &I, ExecutionContext &SF) {
853 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
854 PointerTy SrcPtr = SRC.PointerVal;
855 PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices
858 GenericValue *Ptr = (GenericValue*)SrcPtr;
861 switch (I.getType()->getPrimitiveID()) {
863 case Type::UByteTyID:
864 case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break;
865 case Type::UShortTyID:
866 case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break;
868 case Type::IntTyID: Result.IntVal = Ptr->IntVal; break;
869 case Type::ULongTyID:
870 case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break;
871 case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break;
872 case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break;
873 case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break;
875 cout << "Cannot load value of type " << I.getType() << "!\n";
878 SetValue(&I, Result, SF);
881 static void executeStoreInst(StoreInst &I, ExecutionContext &SF) {
882 GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
883 PointerTy SrcPtr = SRC.PointerVal;
884 SrcPtr += getElementOffset(I, SF); // Handle any structure indices
886 GenericValue *Ptr = (GenericValue *)SrcPtr;
887 GenericValue Val = getOperandValue(I.getOperand(0), SF);
889 switch (I.getOperand(0)->getType()->getPrimitiveID()) {
891 case Type::UByteTyID:
892 case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break;
893 case Type::UShortTyID:
894 case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break;
896 case Type::IntTyID: Ptr->IntVal = Val.IntVal; break;
897 case Type::ULongTyID:
898 case Type::LongTyID: Ptr->LongVal = Val.LongVal; break;
899 case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break;
900 case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break;
901 case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break;
903 cout << "Cannot store value of type " << I.getType() << "!\n";
908 //===----------------------------------------------------------------------===//
909 // Miscellaneous Instruction Implementations
910 //===----------------------------------------------------------------------===//
912 void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) {
913 ECStack.back().Caller = &I;
914 vector<GenericValue> ArgVals;
915 ArgVals.reserve(I.getNumOperands()-1);
916 for (unsigned i = 1; i < I.getNumOperands(); ++i)
917 ArgVals.push_back(getOperandValue(I.getOperand(i), SF));
919 // To handle indirect calls, we must get the pointer value from the argument
920 // and treat it as a function pointer.
921 GenericValue SRC = getOperandValue(I.getCalledValue(), SF);
923 callMethod((Function*)SRC.PointerVal, ArgVals);
926 static void executePHINode(PHINode &I, ExecutionContext &SF) {
927 BasicBlock *PrevBB = SF.PrevBB;
928 Value *IncomingValue = 0;
930 // Search for the value corresponding to this previous bb...
931 for (unsigned i = I.getNumIncomingValues(); i > 0;) {
932 if (I.getIncomingBlock(--i) == PrevBB) {
933 IncomingValue = I.getIncomingValue(i);
937 assert(IncomingValue && "No PHI node predecessor for current PrevBB!");
939 // Found the value, set as the result...
940 SetValue(&I, getOperandValue(IncomingValue, SF), SF);
943 #define IMPLEMENT_SHIFT(OP, TY) \
944 case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break
946 static void executeShlInst(ShiftInst &I, ExecutionContext &SF) {
947 const Type *Ty = I.getOperand(0)->getType();
948 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
949 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
952 switch (Ty->getPrimitiveID()) {
953 IMPLEMENT_SHIFT(<<, UByte);
954 IMPLEMENT_SHIFT(<<, SByte);
955 IMPLEMENT_SHIFT(<<, UShort);
956 IMPLEMENT_SHIFT(<<, Short);
957 IMPLEMENT_SHIFT(<<, UInt);
958 IMPLEMENT_SHIFT(<<, Int);
959 IMPLEMENT_SHIFT(<<, ULong);
960 IMPLEMENT_SHIFT(<<, Long);
962 cout << "Unhandled type for Shl instruction: " << Ty << "\n";
964 SetValue(&I, Dest, SF);
967 static void executeShrInst(ShiftInst &I, ExecutionContext &SF) {
968 const Type *Ty = I.getOperand(0)->getType();
969 GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
970 GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
973 switch (Ty->getPrimitiveID()) {
974 IMPLEMENT_SHIFT(>>, UByte);
975 IMPLEMENT_SHIFT(>>, SByte);
976 IMPLEMENT_SHIFT(>>, UShort);
977 IMPLEMENT_SHIFT(>>, Short);
978 IMPLEMENT_SHIFT(>>, UInt);
979 IMPLEMENT_SHIFT(>>, Int);
980 IMPLEMENT_SHIFT(>>, ULong);
981 IMPLEMENT_SHIFT(>>, Long);
983 cout << "Unhandled type for Shr instruction: " << Ty << "\n";
985 SetValue(&I, Dest, SF);
988 #define IMPLEMENT_CAST(DTY, DCTY, STY) \
989 case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break;
991 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \
992 case Type::DESTTY##TyID: \
993 switch (SrcTy->getPrimitiveID()) { \
994 IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \
995 IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \
996 IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \
997 IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \
998 IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \
999 IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \
1000 IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \
1001 IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \
1002 IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \
1003 IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer);
1005 #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \
1006 IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \
1007 IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
1009 #define IMPLEMENT_CAST_CASE_END() \
1010 default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
1015 #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \
1016 IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \
1017 IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \
1018 IMPLEMENT_CAST_CASE_END()
1020 static void executeCastInst(CastInst &I, ExecutionContext &SF) {
1021 const Type *Ty = I.getType();
1022 const Type *SrcTy = I.getOperand(0)->getType();
1023 GenericValue Src = getOperandValue(I.getOperand(0), SF);
1026 switch (Ty->getPrimitiveID()) {
1027 IMPLEMENT_CAST_CASE(UByte , (unsigned char));
1028 IMPLEMENT_CAST_CASE(SByte , ( signed char));
1029 IMPLEMENT_CAST_CASE(UShort , (unsigned short));
1030 IMPLEMENT_CAST_CASE(Short , ( signed char));
1031 IMPLEMENT_CAST_CASE(UInt , (unsigned int ));
1032 IMPLEMENT_CAST_CASE(Int , ( signed int ));
1033 IMPLEMENT_CAST_CASE(ULong , (uint64_t));
1034 IMPLEMENT_CAST_CASE(Long , ( int64_t));
1035 IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t));
1036 IMPLEMENT_CAST_CASE(Float , (float));
1037 IMPLEMENT_CAST_CASE(Double , (double));
1039 cout << "Unhandled dest type for cast instruction: " << Ty << "\n";
1041 SetValue(&I, Dest, SF);
1047 //===----------------------------------------------------------------------===//
1048 // Dispatch and Execution Code
1049 //===----------------------------------------------------------------------===//
1051 MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) {
1052 // Assign slot numbers to the function arguments...
1053 for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI)
1054 AI->addAnnotation(new SlotNumber(getValueSlot(AI)));
1056 // Iterate over all of the instructions...
1057 unsigned InstNum = 0;
1058 for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB)
1059 for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II)
1060 // For each instruction... Add Annote
1061 II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II)));
1064 unsigned MethodInfo::getValueSlot(const Value *V) {
1065 unsigned Plane = V->getType()->getUniqueID();
1066 if (Plane >= NumPlaneElements.size())
1067 NumPlaneElements.resize(Plane+1, 0);
1068 return NumPlaneElements[Plane]++;
1072 //===----------------------------------------------------------------------===//
1073 // callMethod - Execute the specified function...
1075 void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) {
1076 assert((ECStack.empty() || ECStack.back().Caller == 0 ||
1077 ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) &&
1078 "Incorrect number of arguments passed into function call!");
1079 if (M->isExternal()) {
1080 GenericValue Result = callExternalMethod(M, ArgVals);
1081 const Type *RetTy = M->getReturnType();
1083 // Copy the result back into the result variable if we are not returning
1085 if (RetTy != Type::VoidTy) {
1086 if (!ECStack.empty() && ECStack.back().Caller) {
1087 ExecutionContext &SF = ECStack.back();
1088 SetValue(SF.Caller, Result, SF);
1090 SF.Caller = 0; // We returned from the call...
1091 } else if (!QuietMode) {
1093 CW << "Function " << M->getType() << " \"" << M->getName()
1095 print(RetTy, Result);
1098 if (RetTy->isIntegral())
1099 ExitCode = Result.SByteVal; // Capture the exit code of the program
1106 // Process the function, assigning instruction numbers to the instructions in
1107 // the function. Also calculate the number of values for each type slot
1110 MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID);
1111 ECStack.push_back(ExecutionContext()); // Make a new stack frame...
1113 ExecutionContext &StackFrame = ECStack.back(); // Fill it in...
1114 StackFrame.CurMethod = M;
1115 StackFrame.CurBB = M->begin();
1116 StackFrame.CurInst = StackFrame.CurBB->begin();
1117 StackFrame.MethInfo = MethInfo;
1119 // Initialize the values to nothing...
1120 StackFrame.Values.resize(MethInfo->NumPlaneElements.size());
1121 for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) {
1122 StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]);
1124 // Taint the initial values of stuff
1125 memset(&StackFrame.Values[i][0], 42,
1126 MethInfo->NumPlaneElements[i]*sizeof(GenericValue));
1129 StackFrame.PrevBB = 0; // No previous BB for PHI nodes...
1132 // Run through the function arguments and initialize their values...
1133 assert(ArgVals.size() == M->asize() &&
1134 "Invalid number of values passed to function invocation!");
1136 for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i)
1137 SetValue(AI, ArgVals[i], StackFrame);
1140 // executeInstruction - Interpret a single instruction, increment the "PC", and
1141 // return true if the next instruction is a breakpoint...
1143 bool Interpreter::executeInstruction() {
1144 assert(!ECStack.empty() && "No program running, cannot execute inst!");
1146 ExecutionContext &SF = ECStack.back(); // Current stack frame
1147 Instruction &I = *SF.CurInst++; // Increment before execute
1152 // Set a sigsetjmp buffer so that we can recover if an error happens during
1153 // instruction execution...
1155 if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) {
1156 --SF.CurInst; // Back up to erroring instruction
1157 if (SigNo != SIGINT) {
1158 cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n";
1160 // If -abort-on-exception was specified, terminate LLI instead of trying
1163 if (AbortOnExceptions) exit(1);
1164 } else if (SigNo == SIGINT) {
1165 cout << "CTRL-C Detected, execution halted.\n";
1167 InInstruction = false;
1171 InInstruction = true;
1172 if (I.isBinaryOp()) {
1173 executeBinaryInst(cast<BinaryOperator>(I), SF);
1175 switch (I.getOpcode()) {
1176 case Instruction::Not: executeNotInst(cast<UnaryOperator>(I),SF); break;
1178 case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break;
1179 case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break;
1180 // Memory Instructions
1181 case Instruction::Alloca:
1182 case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break;
1183 case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break;
1184 case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break;
1185 case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break;
1186 case Instruction::GetElementPtr:
1187 executeGEPInst(cast<GetElementPtrInst>(I), SF); break;
1189 // Miscellaneous Instructions
1190 case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break;
1191 case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break;
1192 case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break;
1193 case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break;
1194 case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break;
1196 cout << "Don't know how to execute this instruction!\n-->" << I;
1199 InInstruction = false;
1201 // Reset the current frame location to the top of stack
1202 CurFrame = ECStack.size()-1;
1204 if (CurFrame == -1) return false; // No breakpoint if no code
1206 // Return true if there is a breakpoint annotation on the instruction...
1207 return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0;
1210 void Interpreter::stepInstruction() { // Do the 'step' command
1211 if (ECStack.empty()) {
1212 cout << "Error: no program running, cannot step!\n";
1216 // Run an instruction...
1217 executeInstruction();
1219 // Print the next instruction to execute...
1220 printCurrentInstruction();
1224 void Interpreter::nextInstruction() { // Do the 'next' command
1225 if (ECStack.empty()) {
1226 cout << "Error: no program running, cannot 'next'!\n";
1230 // If this is a call instruction, step over the call instruction...
1231 // TODO: ICALL, CALL WITH, ...
1232 if (ECStack.back().CurInst->getOpcode() == Instruction::Call) {
1233 unsigned StackSize = ECStack.size();
1234 // Step into the function...
1235 if (executeInstruction()) {
1236 // Hit a breakpoint, print current instruction, then return to user...
1237 cout << "Breakpoint hit!\n";
1238 printCurrentInstruction();
1242 // If we we able to step into the function, finish it now. We might not be
1243 // able the step into a function, if it's external for example.
1244 if (ECStack.size() != StackSize)
1245 finish(); // Finish executing the function...
1247 printCurrentInstruction();
1250 // Normal instruction, just step...
1255 void Interpreter::run() {
1256 if (ECStack.empty()) {
1257 cout << "Error: no program running, cannot run!\n";
1261 bool HitBreakpoint = false;
1262 while (!ECStack.empty() && !HitBreakpoint) {
1263 // Run an instruction...
1264 HitBreakpoint = executeInstruction();
1267 if (HitBreakpoint) {
1268 cout << "Breakpoint hit!\n";
1270 // Print the next instruction to execute...
1271 printCurrentInstruction();
1274 void Interpreter::finish() {
1275 if (ECStack.empty()) {
1276 cout << "Error: no program running, cannot run!\n";
1280 unsigned StackSize = ECStack.size();
1281 bool HitBreakpoint = false;
1282 while (ECStack.size() >= StackSize && !HitBreakpoint) {
1283 // Run an instruction...
1284 HitBreakpoint = executeInstruction();
1287 if (HitBreakpoint) {
1288 cout << "Breakpoint hit!\n";
1291 // Print the next instruction to execute...
1292 printCurrentInstruction();
1297 // printCurrentInstruction - Print out the instruction that the virtual PC is
1298 // at, or fail silently if no program is running.
1300 void Interpreter::printCurrentInstruction() {
1301 if (!ECStack.empty()) {
1302 if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label
1303 WriteAsOperand(cout, ECStack.back().CurBB) << ":\n";
1305 Instruction &I = *ECStack.back().CurInst;
1306 InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID);
1307 assert(IN && "Instruction has no numbering annotation!");
1308 cout << "#" << IN->InstNum << I;
1312 void Interpreter::printValue(const Type *Ty, GenericValue V) {
1313 switch (Ty->getPrimitiveID()) {
1314 case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break;
1315 case Type::SByteTyID: cout << V.SByteVal; break;
1316 case Type::UByteTyID: cout << V.UByteVal; break;
1317 case Type::ShortTyID: cout << V.ShortVal; break;
1318 case Type::UShortTyID: cout << V.UShortVal; break;
1319 case Type::IntTyID: cout << V.IntVal; break;
1320 case Type::UIntTyID: cout << V.UIntVal; break;
1321 case Type::LongTyID: cout << (long)V.LongVal; break;
1322 case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break;
1323 case Type::FloatTyID: cout << V.FloatVal; break;
1324 case Type::DoubleTyID: cout << V.DoubleVal; break;
1325 case Type::PointerTyID:cout << (void*)V.PointerVal; break;
1327 cout << "- Don't know how to print value of this type!";
1332 void Interpreter::print(const Type *Ty, GenericValue V) {
1337 void Interpreter::print(const std::string &Name) {
1338 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1339 if (!PickedVal) return;
1341 if (const Function *F = dyn_cast<const Function>(PickedVal)) {
1342 CW << F; // Print the function
1343 } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) {
1344 CW << "type %" << Name << " = " << Ty->getDescription() << "\n";
1345 } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) {
1346 CW << BB; // Print the basic block
1347 } else { // Otherwise there should be an annotation for the slot#
1348 print(PickedVal->getType(),
1349 getOperandValue(PickedVal, ECStack[CurFrame]));
1354 void Interpreter::infoValue(const std::string &Name) {
1355 Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name));
1356 if (!PickedVal) return;
1359 print(PickedVal->getType(),
1360 getOperandValue(PickedVal, ECStack[CurFrame]));
1362 printOperandInfo(PickedVal, ECStack[CurFrame]);
1365 // printStackFrame - Print information about the specified stack frame, or -1
1366 // for the default one.
1368 void Interpreter::printStackFrame(int FrameNo = -1) {
1369 if (FrameNo == -1) FrameNo = CurFrame;
1370 Function *F = ECStack[FrameNo].CurMethod;
1371 const Type *RetTy = F->getReturnType();
1373 CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". "
1374 << (Value*)RetTy << " \"" << F->getName() << "\"(";
1377 for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) {
1378 if (i != 0) cout << ", ";
1381 printValue(I->getType(), getOperandValue(I, ECStack[FrameNo]));
1386 if (FrameNo != int(ECStack.size()-1)) {
1387 BasicBlock::iterator I = ECStack[FrameNo].CurInst;
1390 CW << *ECStack[FrameNo].CurInst;