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