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