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