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