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