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