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