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