Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- Execution.cpp - Implement code to simulate the program ------------===// |
| 2 | // |
| 3 | // This file contains the actual instruction interpreter. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "Interpreter.h" |
| 8 | #include "ExecutionAnnotations.h" |
Chris Lattner | 7061dc5 | 2001-12-03 18:02:31 +0000 | [diff] [blame] | 9 | #include "llvm/iPHINode.h" |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 10 | #include "llvm/iOther.h" |
| 11 | #include "llvm/iTerminators.h" |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 12 | #include "llvm/iMemory.h" |
Chris Lattner | e2cbbce | 2002-04-29 18:56:45 +0000 | [diff] [blame] | 13 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 15 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 41c2e5c | 2001-09-28 22:56:43 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 17 | #include "llvm/GlobalVariable.h" |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 18 | #include "Support/CommandLine.h" |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 19 | #include <math.h> // For fmod |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 20 | #include <signal.h> |
| 21 | #include <setjmp.h> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 22 | #include <iostream> |
| 23 | using std::vector; |
| 24 | using std::cout; |
| 25 | using std::cerr; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 27 | static cl::opt<bool> |
| 28 | QuietMode("quiet", cl::desc("Do not emit any non-program output")); |
| 29 | |
| 30 | static cl::alias |
| 31 | QuietModeA("q", cl::desc("Alias for -quiet"), cl::aliasopt(QuietMode)); |
| 32 | |
| 33 | static cl::opt<bool> |
| 34 | ArrayChecksEnabled("array-checks", cl::desc("Enable array bound checks")); |
| 35 | |
| 36 | static cl::opt<bool> |
| 37 | AbortOnExceptions("abort-on-exception", |
| 38 | cl::desc("Halt execution on a machine exception")); |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 40 | // Create a TargetData structure to handle memory addressing and size/alignment |
| 41 | // computations |
| 42 | // |
| 43 | static TargetData TD("lli Interpreter"); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 44 | CachedWriter CW; // Object to accelerate printing of LLVM |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 45 | |
| 46 | |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 47 | #ifdef PROFILE_STRUCTURE_FIELDS |
Chris Lattner | 5ff62e9 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 48 | static cl::opt<bool> |
| 49 | ProfileStructureFields("profilestructfields", |
| 50 | cl::desc("Profile Structure Field Accesses")); |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 51 | #include <map> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 52 | static std::map<const StructType *, vector<unsigned> > FieldAccessCounts; |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 55 | sigjmp_buf SignalRecoverBuffer; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 56 | static bool InInstruction = false; |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 57 | |
| 58 | extern "C" { |
| 59 | static void SigHandler(int Signal) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 60 | if (InInstruction) |
| 61 | siglongjmp(SignalRecoverBuffer, Signal); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 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); |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 72 | sigaction(SIGINT, &Action, 0); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 73 | sigaction(SIGFPE, &Action, 0); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 76 | |
| 77 | //===----------------------------------------------------------------------===// |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 78 | // Value Manipulation code |
| 79 | //===----------------------------------------------------------------------===// |
| 80 | |
| 81 | static unsigned getOperandSlot(Value *V) { |
| 82 | SlotNumber *SN = (SlotNumber*)V->getAnnotation(SlotNumberAID); |
| 83 | assert(SN && "Operand does not have a slot number annotation!"); |
| 84 | return SN->SlotNum; |
| 85 | } |
| 86 | |
| 87 | #define GET_CONST_VAL(TY, CLASS) \ |
| 88 | case Type::TY##TyID: Result.TY##Val = cast<CLASS>(CPV)->getValue(); break |
| 89 | |
| 90 | static GenericValue getOperandValue(Value *V, ExecutionContext &SF) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 91 | if (Constant *CPV = dyn_cast<Constant>(V)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 92 | GenericValue Result; |
| 93 | switch (CPV->getType()->getPrimitiveID()) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 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); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 105 | case Type::PointerTyID: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 106 | if (isa<ConstantPointerNull>(CPV)) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 107 | Result.PointerVal = 0; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 108 | } else if (isa<ConstantPointerRef>(CPV)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 109 | assert(0 && "Not implemented!"); |
| 110 | } else { |
| 111 | assert(0 && "Unknown constant pointer type!"); |
| 112 | } |
| 113 | break; |
| 114 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 115 | cout << "ERROR: Constant unimp for type: " << CPV->getType() << "\n"; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 116 | } |
| 117 | return Result; |
| 118 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 119 | GlobalAddress *Address = |
| 120 | (GlobalAddress*)GV->getOrCreateAnnotation(GlobalAddressAID); |
| 121 | GenericValue Result; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 122 | Result.PointerVal = (PointerTy)(GenericValue*)Address->Ptr; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 123 | return Result; |
| 124 | } else { |
| 125 | unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 126 | unsigned OpSlot = getOperandSlot(V); |
| 127 | assert(TyP < SF.Values.size() && |
| 128 | OpSlot < SF.Values[TyP].size() && "Value out of range!"); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 129 | return SF.Values[TyP][getOperandSlot(V)]; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | static void printOperandInfo(Value *V, ExecutionContext &SF) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 134 | if (isa<Constant>(V)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 135 | cout << "Constant Pool Value\n"; |
| 136 | } else if (isa<GlobalValue>(V)) { |
| 137 | cout << "Global Value\n"; |
| 138 | } else { |
| 139 | unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value |
| 140 | unsigned Slot = getOperandSlot(V); |
| 141 | cout << "Value=" << (void*)V << " TypeID=" << TyP << " Slot=" << Slot |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 142 | << " Addr=" << &SF.Values[TyP][Slot] << " SF=" << &SF |
| 143 | << " Contents=0x"; |
| 144 | |
| 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')); |
| 150 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 151 | cout << "\n"; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { |
| 158 | unsigned TyP = V->getType()->getUniqueID(); // TypePlane for value |
| 159 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 160 | //cout << "Setting value: " << &SF.Values[TyP][getOperandSlot(V)] << "\n"; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 161 | SF.Values[TyP][getOperandSlot(V)] = Val; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 166 | // Annotation Wrangling code |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | |
| 169 | void Interpreter::initializeExecutionEngine() { |
| 170 | AnnotationManager::registerAnnotationFactory(MethodInfoAID, |
| 171 | &MethodInfo::Create); |
| 172 | AnnotationManager::registerAnnotationFactory(GlobalAddressAID, |
| 173 | &GlobalAddress::Create); |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 174 | initializeSignalHandlers(); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 177 | // InitializeMemory - Recursive function to apply a Constant value into the |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 178 | // specified memory location... |
| 179 | // |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 180 | static void InitializeMemory(const Constant *Init, char *Addr) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 181 | #define INITIALIZE_MEMORY(TYID, CLASS, TY) \ |
| 182 | case Type::TYID##TyID: { \ |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 183 | TY Tmp = cast<CLASS>(Init)->getValue(); \ |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 184 | memcpy(Addr, &Tmp, sizeof(TY)); \ |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 185 | } return |
| 186 | |
| 187 | switch (Init->getType()->getPrimitiveID()) { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 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); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 199 | #undef INITIALIZE_MEMORY |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 200 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 201 | case Type::ArrayTyID: { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 202 | const ConstantArray *CPA = cast<ConstantArray>(Init); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 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) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 207 | InitializeMemory(cast<Constant>(Val[i].get()), Addr+i*ElementSize); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 210 | |
| 211 | case Type::StructTyID: { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 212 | const ConstantStruct *CPS = cast<ConstantStruct>(Init); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 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) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 216 | InitializeMemory(cast<Constant>(Val[i].get()), |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 217 | Addr+SL->MemberOffsets[i]); |
| 218 | return; |
| 219 | } |
| 220 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 221 | case Type::PointerTyID: |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 222 | if (isa<ConstantPointerNull>(Init)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 223 | *(void**)Addr = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 224 | } else if (const ConstantPointerRef *CPR = |
| 225 | dyn_cast<ConstantPointerRef>(Init)) { |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 226 | GlobalAddress *Address = |
| 227 | (GlobalAddress*)CPR->getValue()->getOrCreateAnnotation(GlobalAddressAID); |
| 228 | *(void**)Addr = (GenericValue*)Address->Ptr; |
| 229 | } else { |
| 230 | assert(0 && "Unknown Constant pointer type!"); |
| 231 | } |
| 232 | return; |
| 233 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 234 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 235 | CW << "Bad Type: " << Init->getType() << "\n"; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 236 | assert(0 && "Unknown constant type to initialize memory with!"); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | Annotation *GlobalAddress::Create(AnnotationID AID, const Annotable *O, void *){ |
| 241 | assert(AID == GlobalAddressAID); |
| 242 | |
| 243 | // This annotation will only be created on GlobalValue objects... |
| 244 | GlobalValue *GVal = cast<GlobalValue>((Value*)O); |
| 245 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 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! |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 249 | return new GlobalAddress(GVal, false); |
| 250 | } |
| 251 | |
| 252 | // Handle the case of a global variable... |
| 253 | assert(isa<GlobalVariable>(GVal) && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 254 | "Global value found that isn't a function or global variable!"); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 255 | GlobalVariable *GV = cast<GlobalVariable>(GVal); |
| 256 | |
| 257 | // First off, we must allocate space for the global variable to point at... |
Chris Lattner | 7a17675 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 258 | const Type *Ty = GV->getType()->getElementType(); // Type to be allocated |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 259 | |
| 260 | // Allocate enough memory to hold the type... |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 261 | void *Addr = calloc(1, TD.getTypeSize(Ty)); |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 262 | assert(Addr != 0 && "Null pointer returned by malloc!"); |
| 263 | |
| 264 | // Initialize the memory if there is an initializer... |
| 265 | if (GV->hasInitializer()) |
| 266 | InitializeMemory(GV->getInitializer(), (char*)Addr); |
| 267 | |
| 268 | return new GlobalAddress(Addr, true); // Simply invoke the ctor |
| 269 | } |
| 270 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 271 | |
| 272 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 273 | // Unary Instruction Implementations |
| 274 | //===----------------------------------------------------------------------===// |
| 275 | |
| 276 | #define IMPLEMENT_UNARY_OPERATOR(OP, TY) \ |
| 277 | case Type::TY##TyID: Dest.TY##Val = OP Src.TY##Val; break |
| 278 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 279 | static void executeNotInst(UnaryOperator &I, ExecutionContext &SF) { |
| 280 | const Type *Ty = I.getOperand(0)->getType(); |
| 281 | GenericValue Src = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 282 | GenericValue Dest; |
| 283 | switch (Ty->getPrimitiveID()) { |
| 284 | IMPLEMENT_UNARY_OPERATOR(~, UByte); |
| 285 | IMPLEMENT_UNARY_OPERATOR(~, SByte); |
| 286 | IMPLEMENT_UNARY_OPERATOR(~, UShort); |
| 287 | IMPLEMENT_UNARY_OPERATOR(~, Short); |
| 288 | IMPLEMENT_UNARY_OPERATOR(~, UInt); |
| 289 | IMPLEMENT_UNARY_OPERATOR(~, Int); |
| 290 | IMPLEMENT_UNARY_OPERATOR(~, ULong); |
| 291 | IMPLEMENT_UNARY_OPERATOR(~, Long); |
| 292 | IMPLEMENT_UNARY_OPERATOR(~, Pointer); |
| 293 | default: |
| 294 | cout << "Unhandled type for Not instruction: " << Ty << "\n"; |
| 295 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 296 | SetValue(&I, Dest, SF); |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 300 | // Binary Instruction Implementations |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | |
| 303 | #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ |
| 304 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break |
| 305 | |
| 306 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
| 307 | const Type *Ty, ExecutionContext &SF) { |
| 308 | GenericValue Dest; |
| 309 | switch (Ty->getPrimitiveID()) { |
| 310 | IMPLEMENT_BINARY_OPERATOR(+, UByte); |
| 311 | IMPLEMENT_BINARY_OPERATOR(+, SByte); |
| 312 | IMPLEMENT_BINARY_OPERATOR(+, UShort); |
| 313 | IMPLEMENT_BINARY_OPERATOR(+, Short); |
| 314 | IMPLEMENT_BINARY_OPERATOR(+, UInt); |
| 315 | IMPLEMENT_BINARY_OPERATOR(+, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 316 | IMPLEMENT_BINARY_OPERATOR(+, ULong); |
| 317 | IMPLEMENT_BINARY_OPERATOR(+, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 318 | IMPLEMENT_BINARY_OPERATOR(+, Float); |
| 319 | IMPLEMENT_BINARY_OPERATOR(+, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 320 | IMPLEMENT_BINARY_OPERATOR(+, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 321 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 322 | cout << "Unhandled type for Add instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 323 | } |
| 324 | return Dest; |
| 325 | } |
| 326 | |
| 327 | static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, |
| 328 | const Type *Ty, ExecutionContext &SF) { |
| 329 | GenericValue Dest; |
| 330 | switch (Ty->getPrimitiveID()) { |
| 331 | IMPLEMENT_BINARY_OPERATOR(-, UByte); |
| 332 | IMPLEMENT_BINARY_OPERATOR(-, SByte); |
| 333 | IMPLEMENT_BINARY_OPERATOR(-, UShort); |
| 334 | IMPLEMENT_BINARY_OPERATOR(-, Short); |
| 335 | IMPLEMENT_BINARY_OPERATOR(-, UInt); |
| 336 | IMPLEMENT_BINARY_OPERATOR(-, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 337 | IMPLEMENT_BINARY_OPERATOR(-, ULong); |
| 338 | IMPLEMENT_BINARY_OPERATOR(-, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 339 | IMPLEMENT_BINARY_OPERATOR(-, Float); |
| 340 | IMPLEMENT_BINARY_OPERATOR(-, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 341 | IMPLEMENT_BINARY_OPERATOR(-, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 342 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 343 | cout << "Unhandled type for Sub instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 344 | } |
| 345 | return Dest; |
| 346 | } |
| 347 | |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 348 | static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, |
| 349 | const Type *Ty, ExecutionContext &SF) { |
| 350 | GenericValue Dest; |
| 351 | switch (Ty->getPrimitiveID()) { |
| 352 | IMPLEMENT_BINARY_OPERATOR(*, UByte); |
| 353 | IMPLEMENT_BINARY_OPERATOR(*, SByte); |
| 354 | IMPLEMENT_BINARY_OPERATOR(*, UShort); |
| 355 | IMPLEMENT_BINARY_OPERATOR(*, Short); |
| 356 | IMPLEMENT_BINARY_OPERATOR(*, UInt); |
| 357 | IMPLEMENT_BINARY_OPERATOR(*, Int); |
| 358 | IMPLEMENT_BINARY_OPERATOR(*, ULong); |
| 359 | IMPLEMENT_BINARY_OPERATOR(*, Long); |
| 360 | IMPLEMENT_BINARY_OPERATOR(*, Float); |
| 361 | IMPLEMENT_BINARY_OPERATOR(*, Double); |
| 362 | IMPLEMENT_BINARY_OPERATOR(*, Pointer); |
| 363 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 364 | cout << "Unhandled type for Mul instruction: " << Ty << "\n"; |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 365 | } |
| 366 | return Dest; |
| 367 | } |
| 368 | |
| 369 | static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2, |
| 370 | const Type *Ty, ExecutionContext &SF) { |
| 371 | GenericValue Dest; |
| 372 | switch (Ty->getPrimitiveID()) { |
| 373 | IMPLEMENT_BINARY_OPERATOR(/, UByte); |
| 374 | IMPLEMENT_BINARY_OPERATOR(/, SByte); |
| 375 | IMPLEMENT_BINARY_OPERATOR(/, UShort); |
| 376 | IMPLEMENT_BINARY_OPERATOR(/, Short); |
| 377 | IMPLEMENT_BINARY_OPERATOR(/, UInt); |
| 378 | IMPLEMENT_BINARY_OPERATOR(/, Int); |
| 379 | IMPLEMENT_BINARY_OPERATOR(/, ULong); |
| 380 | IMPLEMENT_BINARY_OPERATOR(/, Long); |
| 381 | IMPLEMENT_BINARY_OPERATOR(/, Float); |
| 382 | IMPLEMENT_BINARY_OPERATOR(/, Double); |
| 383 | IMPLEMENT_BINARY_OPERATOR(/, Pointer); |
| 384 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 385 | cout << "Unhandled type for Div instruction: " << Ty << "\n"; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 386 | } |
| 387 | return Dest; |
| 388 | } |
| 389 | |
| 390 | static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2, |
| 391 | const Type *Ty, ExecutionContext &SF) { |
| 392 | GenericValue Dest; |
| 393 | switch (Ty->getPrimitiveID()) { |
| 394 | IMPLEMENT_BINARY_OPERATOR(%, UByte); |
| 395 | IMPLEMENT_BINARY_OPERATOR(%, SByte); |
| 396 | IMPLEMENT_BINARY_OPERATOR(%, UShort); |
| 397 | IMPLEMENT_BINARY_OPERATOR(%, Short); |
| 398 | IMPLEMENT_BINARY_OPERATOR(%, UInt); |
| 399 | IMPLEMENT_BINARY_OPERATOR(%, Int); |
| 400 | IMPLEMENT_BINARY_OPERATOR(%, ULong); |
| 401 | IMPLEMENT_BINARY_OPERATOR(%, Long); |
| 402 | IMPLEMENT_BINARY_OPERATOR(%, Pointer); |
| 403 | case Type::FloatTyID: |
| 404 | Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); |
| 405 | break; |
| 406 | case Type::DoubleTyID: |
| 407 | Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); |
| 408 | break; |
| 409 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 410 | cout << "Unhandled type for Rem instruction: " << Ty << "\n"; |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 411 | } |
| 412 | return Dest; |
| 413 | } |
| 414 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 415 | static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 416 | const Type *Ty, ExecutionContext &SF) { |
| 417 | GenericValue Dest; |
| 418 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 419 | IMPLEMENT_BINARY_OPERATOR(&, UByte); |
| 420 | IMPLEMENT_BINARY_OPERATOR(&, SByte); |
| 421 | IMPLEMENT_BINARY_OPERATOR(&, UShort); |
| 422 | IMPLEMENT_BINARY_OPERATOR(&, Short); |
| 423 | IMPLEMENT_BINARY_OPERATOR(&, UInt); |
| 424 | IMPLEMENT_BINARY_OPERATOR(&, Int); |
| 425 | IMPLEMENT_BINARY_OPERATOR(&, ULong); |
| 426 | IMPLEMENT_BINARY_OPERATOR(&, Long); |
| 427 | IMPLEMENT_BINARY_OPERATOR(&, Pointer); |
| 428 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 429 | cout << "Unhandled type for And instruction: " << Ty << "\n"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 430 | } |
| 431 | return Dest; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, |
| 436 | const Type *Ty, ExecutionContext &SF) { |
| 437 | GenericValue Dest; |
| 438 | switch (Ty->getPrimitiveID()) { |
| 439 | IMPLEMENT_BINARY_OPERATOR(|, UByte); |
| 440 | IMPLEMENT_BINARY_OPERATOR(|, SByte); |
| 441 | IMPLEMENT_BINARY_OPERATOR(|, UShort); |
| 442 | IMPLEMENT_BINARY_OPERATOR(|, Short); |
| 443 | IMPLEMENT_BINARY_OPERATOR(|, UInt); |
| 444 | IMPLEMENT_BINARY_OPERATOR(|, Int); |
| 445 | IMPLEMENT_BINARY_OPERATOR(|, ULong); |
| 446 | IMPLEMENT_BINARY_OPERATOR(|, Long); |
| 447 | IMPLEMENT_BINARY_OPERATOR(|, Pointer); |
| 448 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 449 | cout << "Unhandled type for Or instruction: " << Ty << "\n"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 450 | } |
| 451 | return Dest; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2, |
| 456 | const Type *Ty, ExecutionContext &SF) { |
| 457 | GenericValue Dest; |
| 458 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 459 | IMPLEMENT_BINARY_OPERATOR(^, UByte); |
| 460 | IMPLEMENT_BINARY_OPERATOR(^, SByte); |
| 461 | IMPLEMENT_BINARY_OPERATOR(^, UShort); |
| 462 | IMPLEMENT_BINARY_OPERATOR(^, Short); |
| 463 | IMPLEMENT_BINARY_OPERATOR(^, UInt); |
| 464 | IMPLEMENT_BINARY_OPERATOR(^, Int); |
| 465 | IMPLEMENT_BINARY_OPERATOR(^, ULong); |
| 466 | IMPLEMENT_BINARY_OPERATOR(^, Long); |
| 467 | IMPLEMENT_BINARY_OPERATOR(^, Pointer); |
| 468 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 469 | cout << "Unhandled type for Xor instruction: " << Ty << "\n"; |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 470 | } |
| 471 | return Dest; |
| 472 | } |
| 473 | |
| 474 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 475 | #define IMPLEMENT_SETCC(OP, TY) \ |
| 476 | case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break |
| 477 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 478 | static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2, |
| 479 | const Type *Ty, ExecutionContext &SF) { |
| 480 | GenericValue Dest; |
| 481 | switch (Ty->getPrimitiveID()) { |
| 482 | IMPLEMENT_SETCC(==, UByte); |
| 483 | IMPLEMENT_SETCC(==, SByte); |
| 484 | IMPLEMENT_SETCC(==, UShort); |
| 485 | IMPLEMENT_SETCC(==, Short); |
| 486 | IMPLEMENT_SETCC(==, UInt); |
| 487 | IMPLEMENT_SETCC(==, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 488 | IMPLEMENT_SETCC(==, ULong); |
| 489 | IMPLEMENT_SETCC(==, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 490 | IMPLEMENT_SETCC(==, Float); |
| 491 | IMPLEMENT_SETCC(==, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 492 | IMPLEMENT_SETCC(==, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 493 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 494 | cout << "Unhandled type for SetEQ instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 495 | } |
| 496 | return Dest; |
| 497 | } |
| 498 | |
| 499 | static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2, |
| 500 | const Type *Ty, ExecutionContext &SF) { |
| 501 | GenericValue Dest; |
| 502 | switch (Ty->getPrimitiveID()) { |
| 503 | IMPLEMENT_SETCC(!=, UByte); |
| 504 | IMPLEMENT_SETCC(!=, SByte); |
| 505 | IMPLEMENT_SETCC(!=, UShort); |
| 506 | IMPLEMENT_SETCC(!=, Short); |
| 507 | IMPLEMENT_SETCC(!=, UInt); |
| 508 | IMPLEMENT_SETCC(!=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 509 | IMPLEMENT_SETCC(!=, ULong); |
| 510 | IMPLEMENT_SETCC(!=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 511 | IMPLEMENT_SETCC(!=, Float); |
| 512 | IMPLEMENT_SETCC(!=, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 513 | IMPLEMENT_SETCC(!=, Pointer); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 514 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 515 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 516 | cout << "Unhandled type for SetNE instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 517 | } |
| 518 | return Dest; |
| 519 | } |
| 520 | |
| 521 | static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2, |
| 522 | const Type *Ty, ExecutionContext &SF) { |
| 523 | GenericValue Dest; |
| 524 | switch (Ty->getPrimitiveID()) { |
| 525 | IMPLEMENT_SETCC(<=, UByte); |
| 526 | IMPLEMENT_SETCC(<=, SByte); |
| 527 | IMPLEMENT_SETCC(<=, UShort); |
| 528 | IMPLEMENT_SETCC(<=, Short); |
| 529 | IMPLEMENT_SETCC(<=, UInt); |
| 530 | IMPLEMENT_SETCC(<=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 531 | IMPLEMENT_SETCC(<=, ULong); |
| 532 | IMPLEMENT_SETCC(<=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 533 | IMPLEMENT_SETCC(<=, Float); |
| 534 | IMPLEMENT_SETCC(<=, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 535 | IMPLEMENT_SETCC(<=, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 536 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 537 | cout << "Unhandled type for SetLE instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 538 | } |
| 539 | return Dest; |
| 540 | } |
| 541 | |
| 542 | static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2, |
| 543 | const Type *Ty, ExecutionContext &SF) { |
| 544 | GenericValue Dest; |
| 545 | switch (Ty->getPrimitiveID()) { |
| 546 | IMPLEMENT_SETCC(>=, UByte); |
| 547 | IMPLEMENT_SETCC(>=, SByte); |
| 548 | IMPLEMENT_SETCC(>=, UShort); |
| 549 | IMPLEMENT_SETCC(>=, Short); |
| 550 | IMPLEMENT_SETCC(>=, UInt); |
| 551 | IMPLEMENT_SETCC(>=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 552 | IMPLEMENT_SETCC(>=, ULong); |
| 553 | IMPLEMENT_SETCC(>=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 554 | IMPLEMENT_SETCC(>=, Float); |
| 555 | IMPLEMENT_SETCC(>=, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 556 | IMPLEMENT_SETCC(>=, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 557 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 558 | cout << "Unhandled type for SetGE instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 559 | } |
| 560 | return Dest; |
| 561 | } |
| 562 | |
| 563 | static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2, |
| 564 | const Type *Ty, ExecutionContext &SF) { |
| 565 | GenericValue Dest; |
| 566 | switch (Ty->getPrimitiveID()) { |
| 567 | IMPLEMENT_SETCC(<, UByte); |
| 568 | IMPLEMENT_SETCC(<, SByte); |
| 569 | IMPLEMENT_SETCC(<, UShort); |
| 570 | IMPLEMENT_SETCC(<, Short); |
| 571 | IMPLEMENT_SETCC(<, UInt); |
| 572 | IMPLEMENT_SETCC(<, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 573 | IMPLEMENT_SETCC(<, ULong); |
| 574 | IMPLEMENT_SETCC(<, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 575 | IMPLEMENT_SETCC(<, Float); |
| 576 | IMPLEMENT_SETCC(<, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 577 | IMPLEMENT_SETCC(<, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 578 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 579 | cout << "Unhandled type for SetLT instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 580 | } |
| 581 | return Dest; |
| 582 | } |
| 583 | |
| 584 | static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2, |
| 585 | const Type *Ty, ExecutionContext &SF) { |
| 586 | GenericValue Dest; |
| 587 | switch (Ty->getPrimitiveID()) { |
| 588 | IMPLEMENT_SETCC(>, UByte); |
| 589 | IMPLEMENT_SETCC(>, SByte); |
| 590 | IMPLEMENT_SETCC(>, UShort); |
| 591 | IMPLEMENT_SETCC(>, Short); |
| 592 | IMPLEMENT_SETCC(>, UInt); |
| 593 | IMPLEMENT_SETCC(>, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 594 | IMPLEMENT_SETCC(>, ULong); |
| 595 | IMPLEMENT_SETCC(>, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 596 | IMPLEMENT_SETCC(>, Float); |
| 597 | IMPLEMENT_SETCC(>, Double); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 598 | IMPLEMENT_SETCC(>, Pointer); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 599 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 600 | cout << "Unhandled type for SetGT instruction: " << Ty << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 601 | } |
| 602 | return Dest; |
| 603 | } |
| 604 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 605 | static void executeBinaryInst(BinaryOperator &I, ExecutionContext &SF) { |
| 606 | const Type *Ty = I.getOperand(0)->getType(); |
| 607 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 608 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 609 | GenericValue R; // Result |
| 610 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 611 | switch (I.getOpcode()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 612 | case Instruction::Add: R = executeAddInst (Src1, Src2, Ty, SF); break; |
| 613 | case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty, SF); break; |
| 614 | case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty, SF); break; |
| 615 | case Instruction::Div: R = executeDivInst (Src1, Src2, Ty, SF); break; |
| 616 | case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty, SF); break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 617 | case Instruction::And: R = executeAndInst (Src1, Src2, Ty, SF); break; |
| 618 | case Instruction::Or: R = executeOrInst (Src1, Src2, Ty, SF); break; |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 619 | case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty, SF); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 620 | case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty, SF); break; |
| 621 | case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty, SF); break; |
| 622 | case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty, SF); break; |
| 623 | case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty, SF); break; |
| 624 | case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty, SF); break; |
| 625 | case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty, SF); break; |
| 626 | default: |
| 627 | cout << "Don't know how to handle this binary operator!\n-->" << I; |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 628 | R = Src1; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 631 | SetValue(&I, R, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 634 | //===----------------------------------------------------------------------===// |
| 635 | // Terminator Instruction Implementations |
| 636 | //===----------------------------------------------------------------------===// |
| 637 | |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 638 | static void PerformExitStuff() { |
| 639 | #ifdef PROFILE_STRUCTURE_FIELDS |
| 640 | // Print out structure field accounting information... |
| 641 | if (!FieldAccessCounts.empty()) { |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 642 | CW << "Profile Field Access Counts:\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 643 | std::map<const StructType *, vector<unsigned> >::iterator |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 644 | I = FieldAccessCounts.begin(), E = FieldAccessCounts.end(); |
| 645 | for (; I != E; ++I) { |
| 646 | vector<unsigned> &OfC = I->second; |
| 647 | CW << " '" << (Value*)I->first << "'\t- Sum="; |
| 648 | |
| 649 | unsigned Sum = 0; |
| 650 | for (unsigned i = 0; i < OfC.size(); ++i) |
| 651 | Sum += OfC[i]; |
| 652 | CW << Sum << " - "; |
| 653 | |
| 654 | for (unsigned i = 0; i < OfC.size(); ++i) { |
| 655 | if (i) CW << ", "; |
| 656 | CW << OfC[i]; |
| 657 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 658 | CW << "\n"; |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 659 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 660 | CW << "\n"; |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 661 | |
| 662 | CW << "Profile Field Access Percentages:\n"; |
| 663 | cout.precision(3); |
| 664 | for (I = FieldAccessCounts.begin(); I != E; ++I) { |
| 665 | vector<unsigned> &OfC = I->second; |
| 666 | unsigned Sum = 0; |
| 667 | for (unsigned i = 0; i < OfC.size(); ++i) |
| 668 | Sum += OfC[i]; |
| 669 | |
| 670 | CW << " '" << (Value*)I->first << "'\t- "; |
| 671 | for (unsigned i = 0; i < OfC.size(); ++i) { |
| 672 | if (i) CW << ", "; |
| 673 | CW << double(OfC[i])/Sum; |
| 674 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 675 | CW << "\n"; |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 676 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 677 | CW << "\n"; |
Chris Lattner | 84efe09 | 2001-11-12 20:13:14 +0000 | [diff] [blame] | 678 | |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 679 | FieldAccessCounts.clear(); |
| 680 | } |
| 681 | #endif |
| 682 | } |
| 683 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 684 | void Interpreter::exitCalled(GenericValue GV) { |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 685 | if (!QuietMode) { |
| 686 | cout << "Program returned "; |
| 687 | print(Type::IntTy, GV); |
| 688 | cout << " via 'void exit(int)'\n"; |
| 689 | } |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 690 | |
| 691 | ExitCode = GV.SByteVal; |
| 692 | ECStack.clear(); |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 693 | PerformExitStuff(); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 696 | void Interpreter::executeRetInst(ReturnInst &I, ExecutionContext &SF) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 697 | const Type *RetTy = 0; |
| 698 | GenericValue Result; |
| 699 | |
| 700 | // Save away the return value... (if we are not 'ret void') |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 701 | if (I.getNumOperands()) { |
| 702 | RetTy = I.getReturnValue()->getType(); |
| 703 | Result = getOperandValue(I.getReturnValue(), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | // Save previously executing meth |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 707 | const Function *M = ECStack.back().CurMethod; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 708 | |
| 709 | // Pop the current stack frame... this invalidates SF |
| 710 | ECStack.pop_back(); |
| 711 | |
| 712 | if (ECStack.empty()) { // Finished main. Put result into exit code... |
| 713 | if (RetTy) { // Nonvoid return type? |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 714 | if (!QuietMode) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 715 | CW << "Function " << M->getType() << " \"" << M->getName() |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 716 | << "\" returned "; |
| 717 | print(RetTy, Result); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 718 | cout << "\n"; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 719 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 720 | |
| 721 | if (RetTy->isIntegral()) |
Chris Lattner | f4dca80 | 2002-05-02 19:28:45 +0000 | [diff] [blame] | 722 | ExitCode = Result.IntVal; // Capture the exit code of the program |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 723 | } else { |
| 724 | ExitCode = 0; |
| 725 | } |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 726 | |
Chris Lattner | a95c699 | 2001-11-12 16:28:48 +0000 | [diff] [blame] | 727 | PerformExitStuff(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | |
| 731 | // If we have a previous stack frame, and we have a previous call, fill in |
| 732 | // the return value... |
| 733 | // |
| 734 | ExecutionContext &NewSF = ECStack.back(); |
| 735 | if (NewSF.Caller) { |
| 736 | if (NewSF.Caller->getType() != Type::VoidTy) // Save result... |
| 737 | SetValue(NewSF.Caller, Result, NewSF); |
| 738 | |
| 739 | NewSF.Caller = 0; // We returned from the call... |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 740 | } else if (!QuietMode) { |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 741 | // This must be a function that is executing because of a user 'call' |
| 742 | // instruction. |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 743 | CW << "Function " << M->getType() << " \"" << M->getName() |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 744 | << "\" returned "; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 745 | print(RetTy, Result); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 746 | cout << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 750 | void Interpreter::executeBrInst(BranchInst &I, ExecutionContext &SF) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 751 | SF.PrevBB = SF.CurBB; // Update PrevBB so that PHI nodes work... |
| 752 | BasicBlock *Dest; |
| 753 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 754 | Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... |
| 755 | if (!I.isUnconditional()) { |
| 756 | Value *Cond = I.getCondition(); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 757 | GenericValue CondVal = getOperandValue(Cond, SF); |
| 758 | if (CondVal.BoolVal == 0) // If false cond... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 759 | Dest = I.getSuccessor(1); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 760 | } |
| 761 | SF.CurBB = Dest; // Update CurBB to branch destination |
| 762 | SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... |
| 763 | } |
| 764 | |
| 765 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 766 | // Memory Instruction Implementations |
| 767 | //===----------------------------------------------------------------------===// |
| 768 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 769 | void Interpreter::executeAllocInst(AllocationInst &I, ExecutionContext &SF) { |
| 770 | const Type *Ty = I.getType()->getElementType(); // Type to be allocated |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 771 | |
Chris Lattner | cc82cc1 | 2002-04-28 21:57:33 +0000 | [diff] [blame] | 772 | // Get the number of elements being allocated by the array... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 773 | unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 774 | |
| 775 | // Allocate enough memory to hold the type... |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 776 | // FIXME: Don't use CALLOC, use a tainted malloc. |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 777 | void *Memory = calloc(NumElements, TD.getTypeSize(Ty)); |
| 778 | |
| 779 | GenericValue Result; |
| 780 | Result.PointerVal = (PointerTy)Memory; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 781 | assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 782 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 783 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 784 | if (I.getOpcode() == Instruction::Alloca) |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 785 | ECStack.back().Allocas.add(Memory); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 788 | static void executeFreeInst(FreeInst &I, ExecutionContext &SF) { |
| 789 | assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?"); |
| 790 | GenericValue Value = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 791 | // TODO: Check to make sure memory is allocated |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 792 | free((void*)Value.PointerVal); // Free memory |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 795 | |
| 796 | // getElementOffset - The workhorse for getelementptr, load and store. This |
| 797 | // function returns the offset that arguments ArgOff+1 -> NumArgs specify for |
| 798 | // the pointer type specified by argument Arg. |
| 799 | // |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 800 | static PointerTy getElementOffset(MemAccessInst &I, ExecutionContext &SF) { |
| 801 | assert(isa<PointerType>(I.getPointerOperand()->getType()) && |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 802 | "Cannot getElementOffset of a nonpointer type!"); |
| 803 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 804 | PointerTy Total = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 805 | const Type *Ty = I.getPointerOperand()->getType(); |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 806 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 807 | unsigned ArgOff = I.getFirstIndexOperandNumber(); |
| 808 | while (ArgOff < I.getNumOperands()) { |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 809 | if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| 810 | const StructLayout *SLO = TD.getStructLayout(STy); |
| 811 | |
| 812 | // Indicies must be ubyte constants... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 813 | const ConstantUInt *CPU = cast<ConstantUInt>(I.getOperand(ArgOff++)); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 814 | assert(CPU->getType() == Type::UByteTy); |
| 815 | unsigned Index = CPU->getValue(); |
| 816 | |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 817 | #ifdef PROFILE_STRUCTURE_FIELDS |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 818 | if (ProfileStructureFields) { |
| 819 | // Do accounting for this field... |
| 820 | vector<unsigned> &OfC = FieldAccessCounts[STy]; |
| 821 | if (OfC.size() == 0) OfC.resize(STy->getElementTypes().size()); |
| 822 | OfC[Index]++; |
| 823 | } |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 824 | #endif |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 825 | |
| 826 | Total += SLO->MemberOffsets[Index]; |
| 827 | Ty = STy->getElementTypes()[Index]; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 828 | } else if (const SequentialType *ST = cast<SequentialType>(Ty)) { |
Chris Lattner | e240906 | 2001-11-12 16:19:45 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 830 | // Get the index number for the array... which must be uint type... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 831 | assert(I.getOperand(ArgOff)->getType() == Type::UIntTy); |
| 832 | unsigned Idx = getOperandValue(I.getOperand(ArgOff++), SF).UIntVal; |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 833 | if (const ArrayType *AT = dyn_cast<ArrayType>(ST)) |
Chris Lattner | c0fbd57 | 2002-02-11 20:19:16 +0000 | [diff] [blame] | 834 | if (Idx >= AT->getNumElements() && ArrayChecksEnabled) { |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 835 | cerr << "Out of range memory access to element #" << Idx |
| 836 | << " of a " << AT->getNumElements() << " element array." |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 837 | << " Subscript #" << (ArgOff-I.getFirstIndexOperandNumber()) |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 838 | << "\n"; |
| 839 | // Get outta here!!! |
Chris Lattner | 7403025 | 2002-02-12 15:47:23 +0000 | [diff] [blame] | 840 | siglongjmp(SignalRecoverBuffer, SIGTRAP); |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 841 | } |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 842 | |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 843 | Ty = ST->getElementType(); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 844 | unsigned Size = TD.getTypeSize(Ty); |
| 845 | Total += Size*Idx; |
| 846 | } |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return Total; |
| 850 | } |
| 851 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 852 | static void executeGEPInst(GetElementPtrInst &I, ExecutionContext &SF) { |
| 853 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 854 | PointerTy SrcPtr = SRC.PointerVal; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 855 | |
| 856 | GenericValue Result; |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 857 | Result.PointerVal = SrcPtr + getElementOffset(I, SF); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 858 | SetValue(&I, Result, SF); |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 861 | static void executeLoadInst(LoadInst &I, ExecutionContext &SF) { |
| 862 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 863 | PointerTy SrcPtr = SRC.PointerVal; |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 864 | PointerTy Offset = getElementOffset(I, SF); // Handle any structure indices |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 865 | SrcPtr += Offset; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 866 | |
| 867 | GenericValue *Ptr = (GenericValue*)SrcPtr; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 868 | GenericValue Result; |
| 869 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 870 | switch (I.getType()->getPrimitiveID()) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 871 | case Type::BoolTyID: |
| 872 | case Type::UByteTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 873 | case Type::SByteTyID: Result.SByteVal = Ptr->SByteVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 874 | case Type::UShortTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 875 | case Type::ShortTyID: Result.ShortVal = Ptr->ShortVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 876 | case Type::UIntTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 877 | case Type::IntTyID: Result.IntVal = Ptr->IntVal; break; |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 878 | case Type::ULongTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 879 | case Type::LongTyID: Result.ULongVal = Ptr->ULongVal; break; |
| 880 | case Type::PointerTyID: Result.PointerVal = Ptr->PointerVal; break; |
| 881 | case Type::FloatTyID: Result.FloatVal = Ptr->FloatVal; break; |
| 882 | case Type::DoubleTyID: Result.DoubleVal = Ptr->DoubleVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 883 | default: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 884 | cout << "Cannot load value of type " << I.getType() << "!\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 887 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 890 | static void executeStoreInst(StoreInst &I, ExecutionContext &SF) { |
| 891 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 892 | PointerTy SrcPtr = SRC.PointerVal; |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 893 | SrcPtr += getElementOffset(I, SF); // Handle any structure indices |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 894 | |
| 895 | GenericValue *Ptr = (GenericValue *)SrcPtr; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 896 | GenericValue Val = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 897 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 898 | switch (I.getOperand(0)->getType()->getPrimitiveID()) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 899 | case Type::BoolTyID: |
| 900 | case Type::UByteTyID: |
| 901 | case Type::SByteTyID: Ptr->SByteVal = Val.SByteVal; break; |
| 902 | case Type::UShortTyID: |
| 903 | case Type::ShortTyID: Ptr->ShortVal = Val.ShortVal; break; |
| 904 | case Type::UIntTyID: |
| 905 | case Type::IntTyID: Ptr->IntVal = Val.IntVal; break; |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 906 | case Type::ULongTyID: |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 907 | case Type::LongTyID: Ptr->LongVal = Val.LongVal; break; |
| 908 | case Type::PointerTyID: Ptr->PointerVal = Val.PointerVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 909 | case Type::FloatTyID: Ptr->FloatVal = Val.FloatVal; break; |
| 910 | case Type::DoubleTyID: Ptr->DoubleVal = Val.DoubleVal; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 911 | default: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 912 | cout << "Cannot store value of type " << I.getType() << "!\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 913 | } |
| 914 | } |
| 915 | |
| 916 | |
| 917 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 918 | // Miscellaneous Instruction Implementations |
| 919 | //===----------------------------------------------------------------------===// |
| 920 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 921 | void Interpreter::executeCallInst(CallInst &I, ExecutionContext &SF) { |
| 922 | ECStack.back().Caller = &I; |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 923 | vector<GenericValue> ArgVals; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 924 | ArgVals.reserve(I.getNumOperands()-1); |
| 925 | for (unsigned i = 1; i < I.getNumOperands(); ++i) |
| 926 | ArgVals.push_back(getOperandValue(I.getOperand(i), SF)); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 927 | |
Chris Lattner | 070cf5e | 2001-11-07 20:12:30 +0000 | [diff] [blame] | 928 | // To handle indirect calls, we must get the pointer value from the argument |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 929 | // and treat it as a function pointer. |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 930 | GenericValue SRC = getOperandValue(I.getCalledValue(), SF); |
Chris Lattner | 070cf5e | 2001-11-07 20:12:30 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 932 | callMethod((Function*)SRC.PointerVal, ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 935 | static void executePHINode(PHINode &I, ExecutionContext &SF) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 936 | BasicBlock *PrevBB = SF.PrevBB; |
| 937 | Value *IncomingValue = 0; |
| 938 | |
| 939 | // Search for the value corresponding to this previous bb... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 940 | for (unsigned i = I.getNumIncomingValues(); i > 0;) { |
| 941 | if (I.getIncomingBlock(--i) == PrevBB) { |
| 942 | IncomingValue = I.getIncomingValue(i); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 943 | break; |
| 944 | } |
| 945 | } |
| 946 | assert(IncomingValue && "No PHI node predecessor for current PrevBB!"); |
| 947 | |
| 948 | // Found the value, set as the result... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 949 | SetValue(&I, getOperandValue(IncomingValue, SF), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 952 | #define IMPLEMENT_SHIFT(OP, TY) \ |
| 953 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break |
| 954 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 955 | static void executeShlInst(ShiftInst &I, ExecutionContext &SF) { |
| 956 | const Type *Ty = I.getOperand(0)->getType(); |
| 957 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 958 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 959 | GenericValue Dest; |
| 960 | |
| 961 | switch (Ty->getPrimitiveID()) { |
| 962 | IMPLEMENT_SHIFT(<<, UByte); |
| 963 | IMPLEMENT_SHIFT(<<, SByte); |
| 964 | IMPLEMENT_SHIFT(<<, UShort); |
| 965 | IMPLEMENT_SHIFT(<<, Short); |
| 966 | IMPLEMENT_SHIFT(<<, UInt); |
| 967 | IMPLEMENT_SHIFT(<<, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 968 | IMPLEMENT_SHIFT(<<, ULong); |
| 969 | IMPLEMENT_SHIFT(<<, Long); |
Chris Lattner | 743cd3e | 2002-07-09 18:42:36 +0000 | [diff] [blame] | 970 | IMPLEMENT_SHIFT(<<, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 971 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 972 | cout << "Unhandled type for Shl instruction: " << Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 973 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 974 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 977 | static void executeShrInst(ShiftInst &I, ExecutionContext &SF) { |
| 978 | const Type *Ty = I.getOperand(0)->getType(); |
| 979 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 980 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 981 | GenericValue Dest; |
| 982 | |
| 983 | switch (Ty->getPrimitiveID()) { |
| 984 | IMPLEMENT_SHIFT(>>, UByte); |
| 985 | IMPLEMENT_SHIFT(>>, SByte); |
| 986 | IMPLEMENT_SHIFT(>>, UShort); |
| 987 | IMPLEMENT_SHIFT(>>, Short); |
| 988 | IMPLEMENT_SHIFT(>>, UInt); |
| 989 | IMPLEMENT_SHIFT(>>, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 990 | IMPLEMENT_SHIFT(>>, ULong); |
| 991 | IMPLEMENT_SHIFT(>>, Long); |
Chris Lattner | 743cd3e | 2002-07-09 18:42:36 +0000 | [diff] [blame] | 992 | IMPLEMENT_SHIFT(>>, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 993 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 994 | cout << "Unhandled type for Shr instruction: " << Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 995 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 996 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | #define IMPLEMENT_CAST(DTY, DCTY, STY) \ |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1000 | case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1001 | |
| 1002 | #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \ |
| 1003 | case Type::DESTTY##TyID: \ |
| 1004 | switch (SrcTy->getPrimitiveID()) { \ |
Chris Lattner | f4dca80 | 2002-05-02 19:28:45 +0000 | [diff] [blame] | 1005 | IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1006 | IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \ |
| 1007 | IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \ |
| 1008 | IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \ |
| 1009 | IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \ |
| 1010 | IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \ |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 1011 | IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \ |
| 1012 | IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \ |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 1013 | IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \ |
| 1014 | IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1015 | |
| 1016 | #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \ |
| 1017 | IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \ |
| 1018 | IMPLEMENT_CAST(DESTTY, DESTCTY, Double) |
| 1019 | |
| 1020 | #define IMPLEMENT_CAST_CASE_END() \ |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1021 | default: cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1022 | break; \ |
| 1023 | } \ |
| 1024 | break |
| 1025 | |
| 1026 | #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \ |
| 1027 | IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \ |
| 1028 | IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1029 | IMPLEMENT_CAST_CASE_END() |
| 1030 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1031 | static void executeCastInst(CastInst &I, ExecutionContext &SF) { |
| 1032 | const Type *Ty = I.getType(); |
| 1033 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 1034 | GenericValue Src = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1035 | GenericValue Dest; |
| 1036 | |
| 1037 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1038 | IMPLEMENT_CAST_CASE(UByte , (unsigned char)); |
| 1039 | IMPLEMENT_CAST_CASE(SByte , ( signed char)); |
| 1040 | IMPLEMENT_CAST_CASE(UShort , (unsigned short)); |
Chris Lattner | 1bbd361 | 2002-08-02 22:06:04 +0000 | [diff] [blame^] | 1041 | IMPLEMENT_CAST_CASE(Short , ( signed short)); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1042 | IMPLEMENT_CAST_CASE(UInt , (unsigned int )); |
| 1043 | IMPLEMENT_CAST_CASE(Int , ( signed int )); |
| 1044 | IMPLEMENT_CAST_CASE(ULong , (uint64_t)); |
| 1045 | IMPLEMENT_CAST_CASE(Long , ( int64_t)); |
| 1046 | IMPLEMENT_CAST_CASE(Pointer, (PointerTy)(uint32_t)); |
| 1047 | IMPLEMENT_CAST_CASE(Float , (float)); |
| 1048 | IMPLEMENT_CAST_CASE(Double , (double)); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1049 | default: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1050 | cout << "Unhandled dest type for cast instruction: " << Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1051 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1052 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1053 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | //===----------------------------------------------------------------------===// |
| 1059 | // Dispatch and Execution Code |
| 1060 | //===----------------------------------------------------------------------===// |
| 1061 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1062 | MethodInfo::MethodInfo(Function *F) : Annotation(MethodInfoAID) { |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1063 | // Assign slot numbers to the function arguments... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1064 | for (Function::const_aiterator AI = F->abegin(), E = F->aend(); AI != E; ++AI) |
| 1065 | AI->addAnnotation(new SlotNumber(getValueSlot(AI))); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1066 | |
| 1067 | // Iterate over all of the instructions... |
| 1068 | unsigned InstNum = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1069 | for (Function::iterator BB = F->begin(), BBE = F->end(); BB != BBE; ++BB) |
| 1070 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II) |
| 1071 | // For each instruction... Add Annote |
| 1072 | II->addAnnotation(new InstNumber(++InstNum, getValueSlot(II))); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | unsigned MethodInfo::getValueSlot(const Value *V) { |
| 1076 | unsigned Plane = V->getType()->getUniqueID(); |
| 1077 | if (Plane >= NumPlaneElements.size()) |
| 1078 | NumPlaneElements.resize(Plane+1, 0); |
| 1079 | return NumPlaneElements[Plane]++; |
| 1080 | } |
| 1081 | |
| 1082 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1083 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1084 | // callMethod - Execute the specified function... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1085 | // |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1086 | void Interpreter::callMethod(Function *M, const vector<GenericValue> &ArgVals) { |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1087 | assert((ECStack.empty() || ECStack.back().Caller == 0 || |
| 1088 | ECStack.back().Caller->getNumOperands()-1 == ArgVals.size()) && |
| 1089 | "Incorrect number of arguments passed into function call!"); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1090 | if (M->isExternal()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1091 | GenericValue Result = callExternalMethod(M, ArgVals); |
| 1092 | const Type *RetTy = M->getReturnType(); |
| 1093 | |
| 1094 | // Copy the result back into the result variable if we are not returning |
| 1095 | // void. |
| 1096 | if (RetTy != Type::VoidTy) { |
| 1097 | if (!ECStack.empty() && ECStack.back().Caller) { |
| 1098 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1099 | SetValue(SF.Caller, Result, SF); |
| 1100 | |
| 1101 | SF.Caller = 0; // We returned from the call... |
Chris Lattner | f23eb85 | 2001-12-14 16:49:29 +0000 | [diff] [blame] | 1102 | } else if (!QuietMode) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1103 | // print it. |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1104 | CW << "Function " << M->getType() << " \"" << M->getName() |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1105 | << "\" returned "; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1106 | print(RetTy, Result); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1107 | cout << "\n"; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1108 | |
| 1109 | if (RetTy->isIntegral()) |
| 1110 | ExitCode = Result.SByteVal; // Capture the exit code of the program |
| 1111 | } |
| 1112 | } |
| 1113 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1114 | return; |
| 1115 | } |
| 1116 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1117 | // Process the function, assigning instruction numbers to the instructions in |
| 1118 | // the function. Also calculate the number of values for each type slot |
| 1119 | // active. |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1120 | // |
| 1121 | MethodInfo *MethInfo = (MethodInfo*)M->getOrCreateAnnotation(MethodInfoAID); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1122 | ECStack.push_back(ExecutionContext()); // Make a new stack frame... |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1123 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1124 | ExecutionContext &StackFrame = ECStack.back(); // Fill it in... |
| 1125 | StackFrame.CurMethod = M; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1126 | StackFrame.CurBB = M->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1127 | StackFrame.CurInst = StackFrame.CurBB->begin(); |
| 1128 | StackFrame.MethInfo = MethInfo; |
| 1129 | |
| 1130 | // Initialize the values to nothing... |
| 1131 | StackFrame.Values.resize(MethInfo->NumPlaneElements.size()); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1132 | for (unsigned i = 0; i < MethInfo->NumPlaneElements.size(); ++i) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1133 | StackFrame.Values[i].resize(MethInfo->NumPlaneElements[i]); |
| 1134 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1135 | // Taint the initial values of stuff |
| 1136 | memset(&StackFrame.Values[i][0], 42, |
| 1137 | MethInfo->NumPlaneElements[i]*sizeof(GenericValue)); |
| 1138 | } |
| 1139 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1140 | StackFrame.PrevBB = 0; // No previous BB for PHI nodes... |
| 1141 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1143 | // Run through the function arguments and initialize their values... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1144 | assert(ArgVals.size() == M->asize() && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1145 | "Invalid number of values passed to function invocation!"); |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1146 | unsigned i = 0; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1147 | for (Function::aiterator AI = M->abegin(), E = M->aend(); AI != E; ++AI, ++i) |
| 1148 | SetValue(AI, ArgVals[i], StackFrame); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | // executeInstruction - Interpret a single instruction, increment the "PC", and |
| 1152 | // return true if the next instruction is a breakpoint... |
| 1153 | // |
| 1154 | bool Interpreter::executeInstruction() { |
| 1155 | assert(!ECStack.empty() && "No program running, cannot execute inst!"); |
| 1156 | |
| 1157 | ExecutionContext &SF = ECStack.back(); // Current stack frame |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1158 | Instruction &I = *SF.CurInst++; // Increment before execute |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 1160 | if (Trace) |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1161 | CW << "Run:" << I; |
| 1162 | |
| 1163 | // Set a sigsetjmp buffer so that we can recover if an error happens during |
| 1164 | // instruction execution... |
| 1165 | // |
| 1166 | if (int SigNo = sigsetjmp(SignalRecoverBuffer, 1)) { |
| 1167 | --SF.CurInst; // Back up to erroring instruction |
Chris Lattner | 7403025 | 2002-02-12 15:47:23 +0000 | [diff] [blame] | 1168 | if (SigNo != SIGINT) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1169 | cout << "EXCEPTION OCCURRED [" << _sys_siglistp[SigNo] << "]:\n"; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1170 | printStackTrace(); |
Chris Lattner | 7403025 | 2002-02-12 15:47:23 +0000 | [diff] [blame] | 1171 | // If -abort-on-exception was specified, terminate LLI instead of trying |
| 1172 | // to debug it. |
| 1173 | // |
| 1174 | if (AbortOnExceptions) exit(1); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 1175 | } else if (SigNo == SIGINT) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1176 | cout << "CTRL-C Detected, execution halted.\n"; |
| 1177 | } |
| 1178 | InInstruction = false; |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1179 | return true; |
| 1180 | } |
Chris Lattner | 43e3f7c | 2001-10-27 08:43:52 +0000 | [diff] [blame] | 1181 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1182 | InInstruction = true; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1183 | if (I.isBinaryOp()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1184 | executeBinaryInst(cast<BinaryOperator>(I), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1185 | } else { |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1186 | switch (I.getOpcode()) { |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 1187 | case Instruction::Not: executeNotInst(cast<UnaryOperator>(I),SF); break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1188 | // Terminators |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1189 | case Instruction::Ret: executeRetInst (cast<ReturnInst>(I), SF); break; |
| 1190 | case Instruction::Br: executeBrInst (cast<BranchInst>(I), SF); break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1191 | // Memory Instructions |
| 1192 | case Instruction::Alloca: |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1193 | case Instruction::Malloc: executeAllocInst((AllocationInst&)I, SF); break; |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1194 | case Instruction::Free: executeFreeInst (cast<FreeInst> (I), SF); break; |
| 1195 | case Instruction::Load: executeLoadInst (cast<LoadInst> (I), SF); break; |
| 1196 | case Instruction::Store: executeStoreInst(cast<StoreInst>(I), SF); break; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1197 | case Instruction::GetElementPtr: |
| 1198 | executeGEPInst(cast<GetElementPtrInst>(I), SF); break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1199 | |
| 1200 | // Miscellaneous Instructions |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 1201 | case Instruction::Call: executeCallInst (cast<CallInst> (I), SF); break; |
| 1202 | case Instruction::PHINode: executePHINode (cast<PHINode> (I), SF); break; |
| 1203 | case Instruction::Shl: executeShlInst (cast<ShiftInst>(I), SF); break; |
| 1204 | case Instruction::Shr: executeShrInst (cast<ShiftInst>(I), SF); break; |
| 1205 | case Instruction::Cast: executeCastInst (cast<CastInst> (I), SF); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1206 | default: |
| 1207 | cout << "Don't know how to execute this instruction!\n-->" << I; |
| 1208 | } |
| 1209 | } |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1210 | InInstruction = false; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Reset the current frame location to the top of stack |
| 1213 | CurFrame = ECStack.size()-1; |
| 1214 | |
| 1215 | if (CurFrame == -1) return false; // No breakpoint if no code |
| 1216 | |
| 1217 | // Return true if there is a breakpoint annotation on the instruction... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1218 | return ECStack[CurFrame].CurInst->getAnnotation(BreakpointAID) != 0; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | void Interpreter::stepInstruction() { // Do the 'step' command |
| 1222 | if (ECStack.empty()) { |
| 1223 | cout << "Error: no program running, cannot step!\n"; |
| 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | // Run an instruction... |
| 1228 | executeInstruction(); |
| 1229 | |
| 1230 | // Print the next instruction to execute... |
| 1231 | printCurrentInstruction(); |
| 1232 | } |
| 1233 | |
| 1234 | // --- UI Stuff... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1235 | void Interpreter::nextInstruction() { // Do the 'next' command |
| 1236 | if (ECStack.empty()) { |
| 1237 | cout << "Error: no program running, cannot 'next'!\n"; |
| 1238 | return; |
| 1239 | } |
| 1240 | |
| 1241 | // If this is a call instruction, step over the call instruction... |
| 1242 | // TODO: ICALL, CALL WITH, ... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1243 | if (ECStack.back().CurInst->getOpcode() == Instruction::Call) { |
Chris Lattner | a74a6b5 | 2001-10-29 14:08:33 +0000 | [diff] [blame] | 1244 | unsigned StackSize = ECStack.size(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1245 | // Step into the function... |
| 1246 | if (executeInstruction()) { |
| 1247 | // Hit a breakpoint, print current instruction, then return to user... |
| 1248 | cout << "Breakpoint hit!\n"; |
| 1249 | printCurrentInstruction(); |
| 1250 | return; |
| 1251 | } |
| 1252 | |
Chris Lattner | a74a6b5 | 2001-10-29 14:08:33 +0000 | [diff] [blame] | 1253 | // If we we able to step into the function, finish it now. We might not be |
| 1254 | // able the step into a function, if it's external for example. |
| 1255 | if (ECStack.size() != StackSize) |
| 1256 | finish(); // Finish executing the function... |
Chris Lattner | 069aa25 | 2001-10-29 16:05:19 +0000 | [diff] [blame] | 1257 | else |
| 1258 | printCurrentInstruction(); |
Chris Lattner | a74a6b5 | 2001-10-29 14:08:33 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1260 | } else { |
| 1261 | // Normal instruction, just step... |
| 1262 | stepInstruction(); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | void Interpreter::run() { |
| 1267 | if (ECStack.empty()) { |
| 1268 | cout << "Error: no program running, cannot run!\n"; |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | bool HitBreakpoint = false; |
| 1273 | while (!ECStack.empty() && !HitBreakpoint) { |
| 1274 | // Run an instruction... |
| 1275 | HitBreakpoint = executeInstruction(); |
| 1276 | } |
| 1277 | |
| 1278 | if (HitBreakpoint) { |
| 1279 | cout << "Breakpoint hit!\n"; |
| 1280 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1281 | // Print the next instruction to execute... |
| 1282 | printCurrentInstruction(); |
| 1283 | } |
| 1284 | |
| 1285 | void Interpreter::finish() { |
| 1286 | if (ECStack.empty()) { |
| 1287 | cout << "Error: no program running, cannot run!\n"; |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | unsigned StackSize = ECStack.size(); |
| 1292 | bool HitBreakpoint = false; |
| 1293 | while (ECStack.size() >= StackSize && !HitBreakpoint) { |
| 1294 | // Run an instruction... |
| 1295 | HitBreakpoint = executeInstruction(); |
| 1296 | } |
| 1297 | |
| 1298 | if (HitBreakpoint) { |
| 1299 | cout << "Breakpoint hit!\n"; |
| 1300 | } |
| 1301 | |
| 1302 | // Print the next instruction to execute... |
| 1303 | printCurrentInstruction(); |
| 1304 | } |
| 1305 | |
| 1306 | |
| 1307 | |
| 1308 | // printCurrentInstruction - Print out the instruction that the virtual PC is |
| 1309 | // at, or fail silently if no program is running. |
| 1310 | // |
| 1311 | void Interpreter::printCurrentInstruction() { |
| 1312 | if (!ECStack.empty()) { |
Chris Lattner | f5b2ec1 | 2001-10-29 20:44:34 +0000 | [diff] [blame] | 1313 | if (ECStack.back().CurBB->begin() == ECStack.back().CurInst) // print label |
| 1314 | WriteAsOperand(cout, ECStack.back().CurBB) << ":\n"; |
| 1315 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1316 | Instruction &I = *ECStack.back().CurInst; |
| 1317 | InstNumber *IN = (InstNumber*)I.getAnnotation(SlotNumberAID); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1318 | assert(IN && "Instruction has no numbering annotation!"); |
| 1319 | cout << "#" << IN->InstNum << I; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | void Interpreter::printValue(const Type *Ty, GenericValue V) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1324 | switch (Ty->getPrimitiveID()) { |
| 1325 | case Type::BoolTyID: cout << (V.BoolVal?"true":"false"); break; |
| 1326 | case Type::SByteTyID: cout << V.SByteVal; break; |
| 1327 | case Type::UByteTyID: cout << V.UByteVal; break; |
| 1328 | case Type::ShortTyID: cout << V.ShortVal; break; |
| 1329 | case Type::UShortTyID: cout << V.UShortVal; break; |
| 1330 | case Type::IntTyID: cout << V.IntVal; break; |
| 1331 | case Type::UIntTyID: cout << V.UIntVal; break; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1332 | case Type::LongTyID: cout << (long)V.LongVal; break; |
| 1333 | case Type::ULongTyID: cout << (unsigned long)V.ULongVal; break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1334 | case Type::FloatTyID: cout << V.FloatVal; break; |
| 1335 | case Type::DoubleTyID: cout << V.DoubleVal; break; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1336 | case Type::PointerTyID:cout << (void*)V.PointerVal; break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1337 | default: |
| 1338 | cout << "- Don't know how to print value of this type!"; |
| 1339 | break; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1343 | void Interpreter::print(const Type *Ty, GenericValue V) { |
Chris Lattner | 5af0c48 | 2001-11-07 04:23:00 +0000 | [diff] [blame] | 1344 | CW << Ty << " "; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1345 | printValue(Ty, V); |
| 1346 | } |
| 1347 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1348 | void Interpreter::print(const std::string &Name) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1349 | Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); |
| 1350 | if (!PickedVal) return; |
| 1351 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1352 | if (const Function *F = dyn_cast<const Function>(PickedVal)) { |
| 1353 | CW << F; // Print the function |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1354 | } else if (const Type *Ty = dyn_cast<const Type>(PickedVal)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1355 | CW << "type %" << Name << " = " << Ty->getDescription() << "\n"; |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1356 | } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(PickedVal)) { |
| 1357 | CW << BB; // Print the basic block |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1358 | } else { // Otherwise there should be an annotation for the slot# |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1359 | print(PickedVal->getType(), |
| 1360 | getOperandValue(PickedVal, ECStack[CurFrame])); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1361 | cout << "\n"; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1362 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1365 | void Interpreter::infoValue(const std::string &Name) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1366 | Value *PickedVal = ChooseOneOption(Name, LookupMatchingNames(Name)); |
| 1367 | if (!PickedVal) return; |
| 1368 | |
| 1369 | cout << "Value: "; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 1370 | print(PickedVal->getType(), |
| 1371 | getOperandValue(PickedVal, ECStack[CurFrame])); |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1372 | cout << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1373 | printOperandInfo(PickedVal, ECStack[CurFrame]); |
| 1374 | } |
| 1375 | |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1376 | // printStackFrame - Print information about the specified stack frame, or -1 |
| 1377 | // for the default one. |
| 1378 | // |
Chris Lattner | 601d715 | 2002-07-25 17:37:05 +0000 | [diff] [blame] | 1379 | void Interpreter::printStackFrame(int FrameNo) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1380 | if (FrameNo == -1) FrameNo = CurFrame; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1381 | Function *F = ECStack[FrameNo].CurMethod; |
| 1382 | const Type *RetTy = F->getReturnType(); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1383 | |
| 1384 | CW << ((FrameNo == CurFrame) ? '>' : '-') << "#" << FrameNo << ". " |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1385 | << (Value*)RetTy << " \"" << F->getName() << "\"("; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1386 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1387 | unsigned i = 0; |
| 1388 | for (Function::aiterator I = F->abegin(), E = F->aend(); I != E; ++I, ++i) { |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1389 | if (i != 0) cout << ", "; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1390 | CW << *I << "="; |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1391 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1392 | printValue(I->getType(), getOperandValue(I, ECStack[FrameNo])); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1393 | } |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1394 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 1395 | cout << ")\n"; |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1396 | |
| 1397 | if (FrameNo != int(ECStack.size()-1)) { |
| 1398 | BasicBlock::iterator I = ECStack[FrameNo].CurInst; |
| 1399 | CW << --I; |
| 1400 | } else { |
| 1401 | CW << *ECStack[FrameNo].CurInst; |
| 1402 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1403 | } |
Chris Lattner | 461f02f | 2001-11-07 05:31:27 +0000 | [diff] [blame] | 1404 | |