Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- Execution.cpp - Implement code to simulate the program ------------===// |
| 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 10 | // This file contains the actual instruction interpreter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Interpreter.h" |
Chris Lattner | d5bc41a | 2003-04-25 04:21:19 +0000 | [diff] [blame] | 15 | #include "llvm/Instructions.h" |
Chris Lattner | e2cbbce | 2002-04-29 18:56:45 +0000 | [diff] [blame] | 16 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 18 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Chris Lattner | bbdabce | 2002-12-08 05:51:08 +0000 | [diff] [blame] | 19 | #include "Support/Statistic.h" |
Brian Gaeke | 235b200 | 2003-10-10 17:03:22 +0000 | [diff] [blame] | 20 | #include <cmath> // For fmod |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 22 | |
Chris Lattner | bbdabce | 2002-12-08 05:51:08 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | Statistic<> NumDynamicInsts("lli", "Number of dynamic instructions executed"); |
| 25 | } |
| 26 | |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 27 | namespace llvm { |
| 28 | Interpreter *TheEE = 0; |
| 29 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 32 | // Value Manipulation code |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 35 | // Operations used by constant expr implementations... |
| 36 | static GenericValue executeCastOperation(Value *Src, const Type *DestTy, |
| 37 | ExecutionContext &SF); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 38 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 39 | const Type *Ty); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 40 | |
Brian Gaeke | 29794cb | 2003-09-05 18:55:03 +0000 | [diff] [blame] | 41 | GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 42 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 43 | switch (CE->getOpcode()) { |
| 44 | case Instruction::Cast: |
| 45 | return executeCastOperation(CE->getOperand(0), CE->getType(), SF); |
| 46 | case Instruction::GetElementPtr: |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 47 | return TheEE->executeGEPOperation(CE->getOperand(0), gep_type_begin(CE), |
| 48 | gep_type_end(CE), SF); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 49 | case Instruction::Add: |
| 50 | return executeAddInst(getOperandValue(CE->getOperand(0), SF), |
| 51 | getOperandValue(CE->getOperand(1), SF), |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 52 | CE->getType()); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 53 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 54 | std::cerr << "Unhandled ConstantExpr: " << CE << "\n"; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 55 | abort(); |
Chris Lattner | 04e2ad7 | 2003-04-21 22:43:32 +0000 | [diff] [blame] | 56 | return GenericValue(); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 57 | } |
| 58 | } else if (Constant *CPV = dyn_cast<Constant>(V)) { |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 59 | return TheEE->getConstantValue(CPV); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 60 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 61 | return PTOGV(TheEE->getPointerToGlobal(GV)); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 62 | } else { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 63 | return SF.Values[V]; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 67 | static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 68 | SF.Values[V] = Val; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 71 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 72 | // Annotation Wrangling code |
| 73 | //===----------------------------------------------------------------------===// |
| 74 | |
| 75 | void Interpreter::initializeExecutionEngine() { |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 76 | TheEE = this; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 79 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 80 | // Binary Instruction Implementations |
| 81 | //===----------------------------------------------------------------------===// |
| 82 | |
| 83 | #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ |
| 84 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break |
| 85 | |
| 86 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 87 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 88 | GenericValue Dest; |
| 89 | switch (Ty->getPrimitiveID()) { |
| 90 | IMPLEMENT_BINARY_OPERATOR(+, UByte); |
| 91 | IMPLEMENT_BINARY_OPERATOR(+, SByte); |
| 92 | IMPLEMENT_BINARY_OPERATOR(+, UShort); |
| 93 | IMPLEMENT_BINARY_OPERATOR(+, Short); |
| 94 | IMPLEMENT_BINARY_OPERATOR(+, UInt); |
| 95 | IMPLEMENT_BINARY_OPERATOR(+, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 96 | IMPLEMENT_BINARY_OPERATOR(+, ULong); |
| 97 | IMPLEMENT_BINARY_OPERATOR(+, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 98 | IMPLEMENT_BINARY_OPERATOR(+, Float); |
| 99 | IMPLEMENT_BINARY_OPERATOR(+, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 100 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 101 | std::cout << "Unhandled type for Add instruction: " << *Ty << "\n"; |
| 102 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 103 | } |
| 104 | return Dest; |
| 105 | } |
| 106 | |
| 107 | static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 108 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 109 | GenericValue Dest; |
| 110 | switch (Ty->getPrimitiveID()) { |
| 111 | IMPLEMENT_BINARY_OPERATOR(-, UByte); |
| 112 | IMPLEMENT_BINARY_OPERATOR(-, SByte); |
| 113 | IMPLEMENT_BINARY_OPERATOR(-, UShort); |
| 114 | IMPLEMENT_BINARY_OPERATOR(-, Short); |
| 115 | IMPLEMENT_BINARY_OPERATOR(-, UInt); |
| 116 | IMPLEMENT_BINARY_OPERATOR(-, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 117 | IMPLEMENT_BINARY_OPERATOR(-, ULong); |
| 118 | IMPLEMENT_BINARY_OPERATOR(-, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 119 | IMPLEMENT_BINARY_OPERATOR(-, Float); |
| 120 | IMPLEMENT_BINARY_OPERATOR(-, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 121 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 122 | std::cout << "Unhandled type for Sub instruction: " << *Ty << "\n"; |
| 123 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 124 | } |
| 125 | return Dest; |
| 126 | } |
| 127 | |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 128 | static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 129 | const Type *Ty) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 130 | GenericValue Dest; |
| 131 | switch (Ty->getPrimitiveID()) { |
| 132 | IMPLEMENT_BINARY_OPERATOR(*, UByte); |
| 133 | IMPLEMENT_BINARY_OPERATOR(*, SByte); |
| 134 | IMPLEMENT_BINARY_OPERATOR(*, UShort); |
| 135 | IMPLEMENT_BINARY_OPERATOR(*, Short); |
| 136 | IMPLEMENT_BINARY_OPERATOR(*, UInt); |
| 137 | IMPLEMENT_BINARY_OPERATOR(*, Int); |
| 138 | IMPLEMENT_BINARY_OPERATOR(*, ULong); |
| 139 | IMPLEMENT_BINARY_OPERATOR(*, Long); |
| 140 | IMPLEMENT_BINARY_OPERATOR(*, Float); |
| 141 | IMPLEMENT_BINARY_OPERATOR(*, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 142 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 143 | std::cout << "Unhandled type for Mul instruction: " << Ty << "\n"; |
| 144 | abort(); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 145 | } |
| 146 | return Dest; |
| 147 | } |
| 148 | |
| 149 | static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 150 | const Type *Ty) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 151 | GenericValue Dest; |
| 152 | switch (Ty->getPrimitiveID()) { |
| 153 | IMPLEMENT_BINARY_OPERATOR(/, UByte); |
| 154 | IMPLEMENT_BINARY_OPERATOR(/, SByte); |
| 155 | IMPLEMENT_BINARY_OPERATOR(/, UShort); |
| 156 | IMPLEMENT_BINARY_OPERATOR(/, Short); |
| 157 | IMPLEMENT_BINARY_OPERATOR(/, UInt); |
| 158 | IMPLEMENT_BINARY_OPERATOR(/, Int); |
| 159 | IMPLEMENT_BINARY_OPERATOR(/, ULong); |
| 160 | IMPLEMENT_BINARY_OPERATOR(/, Long); |
| 161 | IMPLEMENT_BINARY_OPERATOR(/, Float); |
| 162 | IMPLEMENT_BINARY_OPERATOR(/, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 163 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 164 | std::cout << "Unhandled type for Div instruction: " << *Ty << "\n"; |
| 165 | abort(); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 166 | } |
| 167 | return Dest; |
| 168 | } |
| 169 | |
| 170 | static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 171 | const Type *Ty) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 172 | GenericValue Dest; |
| 173 | switch (Ty->getPrimitiveID()) { |
| 174 | IMPLEMENT_BINARY_OPERATOR(%, UByte); |
| 175 | IMPLEMENT_BINARY_OPERATOR(%, SByte); |
| 176 | IMPLEMENT_BINARY_OPERATOR(%, UShort); |
| 177 | IMPLEMENT_BINARY_OPERATOR(%, Short); |
| 178 | IMPLEMENT_BINARY_OPERATOR(%, UInt); |
| 179 | IMPLEMENT_BINARY_OPERATOR(%, Int); |
| 180 | IMPLEMENT_BINARY_OPERATOR(%, ULong); |
| 181 | IMPLEMENT_BINARY_OPERATOR(%, Long); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 182 | case Type::FloatTyID: |
| 183 | Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); |
| 184 | break; |
| 185 | case Type::DoubleTyID: |
| 186 | Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); |
| 187 | break; |
| 188 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 189 | std::cout << "Unhandled type for Rem instruction: " << *Ty << "\n"; |
| 190 | abort(); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 191 | } |
| 192 | return Dest; |
| 193 | } |
| 194 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 195 | static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 196 | const Type *Ty) { |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 197 | GenericValue Dest; |
| 198 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 669b76a | 2003-04-23 19:21:00 +0000 | [diff] [blame] | 199 | IMPLEMENT_BINARY_OPERATOR(&, Bool); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 200 | IMPLEMENT_BINARY_OPERATOR(&, UByte); |
| 201 | IMPLEMENT_BINARY_OPERATOR(&, SByte); |
| 202 | IMPLEMENT_BINARY_OPERATOR(&, UShort); |
| 203 | IMPLEMENT_BINARY_OPERATOR(&, Short); |
| 204 | IMPLEMENT_BINARY_OPERATOR(&, UInt); |
| 205 | IMPLEMENT_BINARY_OPERATOR(&, Int); |
| 206 | IMPLEMENT_BINARY_OPERATOR(&, ULong); |
| 207 | IMPLEMENT_BINARY_OPERATOR(&, Long); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 208 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 209 | std::cout << "Unhandled type for And instruction: " << *Ty << "\n"; |
| 210 | abort(); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 211 | } |
| 212 | return Dest; |
| 213 | } |
| 214 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 215 | static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 216 | const Type *Ty) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 217 | GenericValue Dest; |
| 218 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 669b76a | 2003-04-23 19:21:00 +0000 | [diff] [blame] | 219 | IMPLEMENT_BINARY_OPERATOR(|, Bool); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 220 | IMPLEMENT_BINARY_OPERATOR(|, UByte); |
| 221 | IMPLEMENT_BINARY_OPERATOR(|, SByte); |
| 222 | IMPLEMENT_BINARY_OPERATOR(|, UShort); |
| 223 | IMPLEMENT_BINARY_OPERATOR(|, Short); |
| 224 | IMPLEMENT_BINARY_OPERATOR(|, UInt); |
| 225 | IMPLEMENT_BINARY_OPERATOR(|, Int); |
| 226 | IMPLEMENT_BINARY_OPERATOR(|, ULong); |
| 227 | IMPLEMENT_BINARY_OPERATOR(|, Long); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 228 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 229 | std::cout << "Unhandled type for Or instruction: " << *Ty << "\n"; |
| 230 | abort(); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 231 | } |
| 232 | return Dest; |
| 233 | } |
| 234 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 235 | static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 236 | const Type *Ty) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 237 | GenericValue Dest; |
| 238 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | 669b76a | 2003-04-23 19:21:00 +0000 | [diff] [blame] | 239 | IMPLEMENT_BINARY_OPERATOR(^, Bool); |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 240 | IMPLEMENT_BINARY_OPERATOR(^, UByte); |
| 241 | IMPLEMENT_BINARY_OPERATOR(^, SByte); |
| 242 | IMPLEMENT_BINARY_OPERATOR(^, UShort); |
| 243 | IMPLEMENT_BINARY_OPERATOR(^, Short); |
| 244 | IMPLEMENT_BINARY_OPERATOR(^, UInt); |
| 245 | IMPLEMENT_BINARY_OPERATOR(^, Int); |
| 246 | IMPLEMENT_BINARY_OPERATOR(^, ULong); |
| 247 | IMPLEMENT_BINARY_OPERATOR(^, Long); |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 248 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 249 | std::cout << "Unhandled type for Xor instruction: " << *Ty << "\n"; |
| 250 | abort(); |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 251 | } |
| 252 | return Dest; |
| 253 | } |
| 254 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 255 | #define IMPLEMENT_SETCC(OP, TY) \ |
| 256 | case Type::TY##TyID: Dest.BoolVal = Src1.TY##Val OP Src2.TY##Val; break |
| 257 | |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 258 | // Handle pointers specially because they must be compared with only as much |
| 259 | // width as the host has. We _do not_ want to be comparing 64 bit values when |
| 260 | // running on a 32-bit target, otherwise the upper 32 bits might mess up |
| 261 | // comparisons if they contain garbage. |
| 262 | #define IMPLEMENT_POINTERSETCC(OP) \ |
| 263 | case Type::PointerTyID: \ |
| 264 | Dest.BoolVal = (void*)(intptr_t)Src1.PointerVal OP \ |
| 265 | (void*)(intptr_t)Src2.PointerVal; break |
| 266 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 267 | static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 268 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 269 | GenericValue Dest; |
| 270 | switch (Ty->getPrimitiveID()) { |
| 271 | IMPLEMENT_SETCC(==, UByte); |
| 272 | IMPLEMENT_SETCC(==, SByte); |
| 273 | IMPLEMENT_SETCC(==, UShort); |
| 274 | IMPLEMENT_SETCC(==, Short); |
| 275 | IMPLEMENT_SETCC(==, UInt); |
| 276 | IMPLEMENT_SETCC(==, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 277 | IMPLEMENT_SETCC(==, ULong); |
| 278 | IMPLEMENT_SETCC(==, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 279 | IMPLEMENT_SETCC(==, Float); |
| 280 | IMPLEMENT_SETCC(==, Double); |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 281 | IMPLEMENT_POINTERSETCC(==); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 282 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 283 | std::cout << "Unhandled type for SetEQ instruction: " << *Ty << "\n"; |
| 284 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 285 | } |
| 286 | return Dest; |
| 287 | } |
| 288 | |
| 289 | static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 290 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 291 | GenericValue Dest; |
| 292 | switch (Ty->getPrimitiveID()) { |
| 293 | IMPLEMENT_SETCC(!=, UByte); |
| 294 | IMPLEMENT_SETCC(!=, SByte); |
| 295 | IMPLEMENT_SETCC(!=, UShort); |
| 296 | IMPLEMENT_SETCC(!=, Short); |
| 297 | IMPLEMENT_SETCC(!=, UInt); |
| 298 | IMPLEMENT_SETCC(!=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 299 | IMPLEMENT_SETCC(!=, ULong); |
| 300 | IMPLEMENT_SETCC(!=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 301 | IMPLEMENT_SETCC(!=, Float); |
| 302 | IMPLEMENT_SETCC(!=, Double); |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 303 | IMPLEMENT_POINTERSETCC(!=); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 304 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 305 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 306 | std::cout << "Unhandled type for SetNE instruction: " << *Ty << "\n"; |
| 307 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 308 | } |
| 309 | return Dest; |
| 310 | } |
| 311 | |
| 312 | static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 313 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 314 | GenericValue Dest; |
| 315 | switch (Ty->getPrimitiveID()) { |
| 316 | IMPLEMENT_SETCC(<=, UByte); |
| 317 | IMPLEMENT_SETCC(<=, SByte); |
| 318 | IMPLEMENT_SETCC(<=, UShort); |
| 319 | IMPLEMENT_SETCC(<=, Short); |
| 320 | IMPLEMENT_SETCC(<=, UInt); |
| 321 | IMPLEMENT_SETCC(<=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 322 | IMPLEMENT_SETCC(<=, ULong); |
| 323 | IMPLEMENT_SETCC(<=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 324 | IMPLEMENT_SETCC(<=, Float); |
| 325 | IMPLEMENT_SETCC(<=, Double); |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 326 | IMPLEMENT_POINTERSETCC(<=); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 327 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 328 | std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n"; |
| 329 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 330 | } |
| 331 | return Dest; |
| 332 | } |
| 333 | |
| 334 | static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 335 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 336 | GenericValue Dest; |
| 337 | switch (Ty->getPrimitiveID()) { |
| 338 | IMPLEMENT_SETCC(>=, UByte); |
| 339 | IMPLEMENT_SETCC(>=, SByte); |
| 340 | IMPLEMENT_SETCC(>=, UShort); |
| 341 | IMPLEMENT_SETCC(>=, Short); |
| 342 | IMPLEMENT_SETCC(>=, UInt); |
| 343 | IMPLEMENT_SETCC(>=, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 344 | IMPLEMENT_SETCC(>=, ULong); |
| 345 | IMPLEMENT_SETCC(>=, Long); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 346 | IMPLEMENT_SETCC(>=, Float); |
| 347 | IMPLEMENT_SETCC(>=, Double); |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 348 | IMPLEMENT_POINTERSETCC(>=); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 349 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 350 | std::cout << "Unhandled type for SetGE instruction: " << *Ty << "\n"; |
| 351 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 352 | } |
| 353 | return Dest; |
| 354 | } |
| 355 | |
| 356 | static GenericValue executeSetLTInst(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 SetLT 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 executeSetGTInst(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 | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 393 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 394 | std::cout << "Unhandled type for SetGT instruction: " << *Ty << "\n"; |
| 395 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 396 | } |
| 397 | return Dest; |
| 398 | } |
| 399 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 400 | void Interpreter::visitBinaryOperator(BinaryOperator &I) { |
| 401 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 402 | const Type *Ty = I.getOperand(0)->getType(); |
| 403 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 404 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 405 | GenericValue R; // Result |
| 406 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 407 | switch (I.getOpcode()) { |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 408 | case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break; |
| 409 | case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break; |
| 410 | case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break; |
| 411 | case Instruction::Div: R = executeDivInst (Src1, Src2, Ty); break; |
| 412 | case Instruction::Rem: R = executeRemInst (Src1, Src2, Ty); break; |
| 413 | case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break; |
| 414 | case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break; |
| 415 | case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break; |
| 416 | case Instruction::SetEQ: R = executeSetEQInst(Src1, Src2, Ty); break; |
| 417 | case Instruction::SetNE: R = executeSetNEInst(Src1, Src2, Ty); break; |
| 418 | case Instruction::SetLE: R = executeSetLEInst(Src1, Src2, Ty); break; |
| 419 | case Instruction::SetGE: R = executeSetGEInst(Src1, Src2, Ty); break; |
| 420 | case Instruction::SetLT: R = executeSetLTInst(Src1, Src2, Ty); break; |
| 421 | case Instruction::SetGT: R = executeSetGTInst(Src1, Src2, Ty); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 422 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 423 | std::cout << "Don't know how to handle this binary operator!\n-->" << I; |
| 424 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 427 | SetValue(&I, R, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 430 | //===----------------------------------------------------------------------===// |
| 431 | // Terminator Instruction Implementations |
| 432 | //===----------------------------------------------------------------------===// |
| 433 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 434 | void Interpreter::exitCalled(GenericValue GV) { |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 435 | ExitCode = GV.SByteVal; |
| 436 | ECStack.clear(); |
| 437 | } |
| 438 | |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 439 | /// Pop the last stack frame off of ECStack and then copy the result |
| 440 | /// back into the result variable if we are not returning void. The |
| 441 | /// result variable may be the ExitCode, or the Value of the calling |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 442 | /// CallInst if there was a previous stack frame. This method may |
| 443 | /// invalidate any ECStack iterators you have. This method also takes |
| 444 | /// care of switching to the normal destination BB, if we are returning |
| 445 | /// from an invoke. |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 446 | /// |
| 447 | void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy, |
| 448 | GenericValue Result) { |
| 449 | // Pop the current stack frame. |
| 450 | ECStack.pop_back(); |
| 451 | |
| 452 | if (ECStack.empty()) { // Finished main. Put result into exit code... |
| 453 | if (RetTy && RetTy->isIntegral()) { // Nonvoid return type? |
| 454 | ExitCode = Result.IntVal; // Capture the exit code of the program |
| 455 | } else { |
| 456 | ExitCode = 0; |
| 457 | } |
| 458 | } else { |
| 459 | // If we have a previous stack frame, and we have a previous call, |
| 460 | // fill in the return value... |
| 461 | ExecutionContext &CallingSF = ECStack.back(); |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 462 | if (Instruction *I = CallingSF.Caller.getInstruction()) { |
Brian Gaeke | 2cb474c | 2003-11-07 19:26:23 +0000 | [diff] [blame] | 463 | if (CallingSF.Caller.getType() != Type::VoidTy) // Save result... |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 464 | SetValue(I, Result, CallingSF); |
| 465 | if (InvokeInst *II = dyn_cast<InvokeInst> (I)) |
| 466 | SwitchToNewBasicBlock (II->getNormalDest (), CallingSF); |
Brian Gaeke | 2cb474c | 2003-11-07 19:26:23 +0000 | [diff] [blame] | 467 | CallingSF.Caller = CallSite(); // We returned from the call... |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 472 | void Interpreter::visitReturnInst(ReturnInst &I) { |
| 473 | ExecutionContext &SF = ECStack.back(); |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 474 | const Type *RetTy = Type::VoidTy; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 475 | GenericValue Result; |
| 476 | |
| 477 | // Save away the return value... (if we are not 'ret void') |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 478 | if (I.getNumOperands()) { |
| 479 | RetTy = I.getReturnValue()->getType(); |
| 480 | Result = getOperandValue(I.getReturnValue(), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 483 | popStackAndReturnValueToCaller(RetTy, Result); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Brian Gaeke | 9bf06b1 | 2003-11-07 20:07:06 +0000 | [diff] [blame] | 486 | void Interpreter::visitUnwindInst(UnwindInst &I) { |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 487 | // Unwind stack |
| 488 | Instruction *Inst; |
| 489 | do { |
| 490 | ECStack.pop_back (); |
| 491 | if (ECStack.empty ()) |
| 492 | abort (); |
| 493 | Inst = ECStack.back ().Caller.getInstruction (); |
| 494 | } while (!(Inst && isa<InvokeInst> (Inst))); |
| 495 | |
| 496 | // Return from invoke |
| 497 | ExecutionContext &InvokingSF = ECStack.back (); |
| 498 | InvokingSF.Caller = CallSite (); |
| 499 | |
| 500 | // Go to exceptional destination BB of invoke instruction |
| 501 | SwitchToNewBasicBlock (cast<InvokeInst> (Inst)->getExceptionalDest (), |
| 502 | InvokingSF); |
Brian Gaeke | 9bf06b1 | 2003-11-07 20:07:06 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 505 | void Interpreter::visitBranchInst(BranchInst &I) { |
| 506 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 507 | BasicBlock *Dest; |
| 508 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 509 | Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... |
| 510 | if (!I.isUnconditional()) { |
| 511 | Value *Cond = I.getCondition(); |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 512 | if (getOperandValue(Cond, SF).BoolVal == 0) // If false cond... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 513 | Dest = I.getSuccessor(1); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 514 | } |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 515 | SwitchToNewBasicBlock(Dest, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 518 | void Interpreter::visitSwitchInst(SwitchInst &I) { |
| 519 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 520 | GenericValue CondVal = getOperandValue(I.getOperand(0), SF); |
| 521 | const Type *ElTy = I.getOperand(0)->getType(); |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 522 | |
| 523 | // Check to see if any of the cases match... |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 524 | BasicBlock *Dest = 0; |
| 525 | for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 526 | if (executeSetEQInst(CondVal, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 527 | getOperandValue(I.getOperand(i), SF), ElTy).BoolVal) { |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 528 | Dest = cast<BasicBlock>(I.getOperand(i+1)); |
| 529 | break; |
| 530 | } |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 531 | |
| 532 | if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 533 | SwitchToNewBasicBlock(Dest, SF); |
| 534 | } |
| 535 | |
| 536 | // SwitchToNewBasicBlock - This method is used to jump to a new basic block. |
| 537 | // This function handles the actual updating of block and instruction iterators |
| 538 | // as well as execution of all of the PHI nodes in the destination block. |
| 539 | // |
| 540 | // This method does this because all of the PHI nodes must be executed |
| 541 | // atomically, reading their inputs before any of the results are updated. Not |
| 542 | // doing this can cause problems if the PHI nodes depend on other PHI nodes for |
| 543 | // their inputs. If the input PHI node is updated before it is read, incorrect |
| 544 | // results can happen. Thus we use a two phase approach. |
| 545 | // |
| 546 | void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){ |
| 547 | BasicBlock *PrevBB = SF.CurBB; // Remember where we came from... |
| 548 | SF.CurBB = Dest; // Update CurBB to branch destination |
| 549 | SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... |
| 550 | |
| 551 | if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do |
| 552 | |
| 553 | // Loop over all of the PHI nodes in the current block, reading their inputs. |
| 554 | std::vector<GenericValue> ResultValues; |
| 555 | |
| 556 | for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) { |
| 557 | // Search for the value corresponding to this previous bb... |
| 558 | int i = PN->getBasicBlockIndex(PrevBB); |
| 559 | assert(i != -1 && "PHINode doesn't contain entry for predecessor??"); |
| 560 | Value *IncomingValue = PN->getIncomingValue(i); |
| 561 | |
| 562 | // Save the incoming value for this PHI node... |
| 563 | ResultValues.push_back(getOperandValue(IncomingValue, SF)); |
| 564 | } |
| 565 | |
| 566 | // Now loop over all of the PHI nodes setting their values... |
| 567 | SF.CurInst = SF.CurBB->begin(); |
| 568 | for (unsigned i = 0; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); |
| 569 | ++SF.CurInst, ++i) |
| 570 | SetValue(PN, ResultValues[i], SF); |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 573 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 574 | // Memory Instruction Implementations |
| 575 | //===----------------------------------------------------------------------===// |
| 576 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 577 | void Interpreter::visitAllocationInst(AllocationInst &I) { |
| 578 | ExecutionContext &SF = ECStack.back(); |
| 579 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 580 | const Type *Ty = I.getType()->getElementType(); // Type to be allocated |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 581 | |
Chris Lattner | cc82cc1 | 2002-04-28 21:57:33 +0000 | [diff] [blame] | 582 | // Get the number of elements being allocated by the array... |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 583 | unsigned NumElements = getOperandValue(I.getOperand(0), SF).UIntVal; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 584 | |
| 585 | // Allocate enough memory to hold the type... |
Brian Gaeke | 2fa8212 | 2003-11-05 01:02:14 +0000 | [diff] [blame] | 586 | void *Memory = malloc(NumElements * TD.getTypeSize(Ty)); |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 587 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 588 | GenericValue Result = PTOGV(Memory); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 589 | assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 590 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 591 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 592 | if (I.getOpcode() == Instruction::Alloca) |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 593 | ECStack.back().Allocas.add(Memory); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 596 | void Interpreter::visitFreeInst(FreeInst &I) { |
| 597 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 598 | assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?"); |
| 599 | GenericValue Value = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 600 | // TODO: Check to make sure memory is allocated |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 601 | free(GVTOP(Value)); // Free memory |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 604 | // getElementOffset - The workhorse for getelementptr. |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 605 | // |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 606 | GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I, |
| 607 | gep_type_iterator E, |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 608 | ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 609 | assert(isa<PointerType>(Ptr->getType()) && |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 610 | "Cannot getElementOffset of a nonpointer type!"); |
| 611 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 612 | PointerTy Total = 0; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 613 | |
| 614 | for (; I != E; ++I) { |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 615 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 616 | const StructLayout *SLO = TD.getStructLayout(STy); |
| 617 | |
Misha Brukman | d5d96b9 | 2003-10-10 17:42:19 +0000 | [diff] [blame] | 618 | // Indices must be ubyte constants... |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 619 | const ConstantUInt *CPU = cast<ConstantUInt>(*I); |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 620 | unsigned Index = CPU->getValue(); |
| 621 | |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 622 | Total += SLO->MemberOffsets[Index]; |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 623 | } else { |
| 624 | const SequentialType *ST = cast<SequentialType>(*I); |
Chris Lattner | 006a4a5 | 2003-02-25 21:14:59 +0000 | [diff] [blame] | 625 | // Get the index number for the array... which must be long type... |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 626 | GenericValue IdxGV = getOperandValue(I.getOperand(), SF); |
| 627 | |
| 628 | uint64_t Idx; |
| 629 | switch (I.getOperand()->getType()->getPrimitiveID()) { |
| 630 | default: assert(0 && "Illegal getelementptr index for sequential type!"); |
| 631 | case Type::SByteTyID: Idx = IdxGV.SByteVal; break; |
| 632 | case Type::ShortTyID: Idx = IdxGV.ShortVal; break; |
| 633 | case Type::IntTyID: Idx = IdxGV.IntVal; break; |
| 634 | case Type::LongTyID: Idx = IdxGV.LongVal; break; |
| 635 | case Type::UByteTyID: Idx = IdxGV.UByteVal; break; |
| 636 | case Type::UShortTyID: Idx = IdxGV.UShortVal; break; |
| 637 | case Type::UIntTyID: Idx = IdxGV.UIntVal; break; |
| 638 | case Type::ULongTyID: Idx = IdxGV.ULongVal; break; |
| 639 | } |
| 640 | Total += TD.getTypeSize(ST->getElementType())*Idx; |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 641 | } |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 644 | GenericValue Result; |
| 645 | Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total; |
| 646 | return Result; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 649 | void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 650 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 651 | SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(), |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 652 | gep_type_begin(I), gep_type_end(I), SF), SF); |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 655 | void Interpreter::visitLoadInst(LoadInst &I) { |
| 656 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 657 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 658 | GenericValue *Ptr = (GenericValue*)GVTOP(SRC); |
Chris Lattner | 374344c | 2003-05-08 16:52:43 +0000 | [diff] [blame] | 659 | GenericValue Result = LoadValueFromMemory(Ptr, I.getType()); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 660 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 663 | void Interpreter::visitStoreInst(StoreInst &I) { |
| 664 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | fddc755 | 2002-10-15 20:34:05 +0000 | [diff] [blame] | 665 | GenericValue Val = getOperandValue(I.getOperand(0), SF); |
| 666 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 667 | StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC), |
Chris Lattner | 683d5da9 | 2002-10-26 01:57:15 +0000 | [diff] [blame] | 668 | I.getOperand(0)->getType()); |
Chris Lattner | fddc755 | 2002-10-15 20:34:05 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 671 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 672 | // Miscellaneous Instruction Implementations |
| 673 | //===----------------------------------------------------------------------===// |
| 674 | |
Brian Gaeke | fea483d | 2003-11-07 20:04:22 +0000 | [diff] [blame] | 675 | void Interpreter::visitCallSite(CallSite CS) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 676 | ExecutionContext &SF = ECStack.back(); |
Brian Gaeke | fea483d | 2003-11-07 20:04:22 +0000 | [diff] [blame] | 677 | SF.Caller = CS; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 678 | std::vector<GenericValue> ArgVals; |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 679 | const unsigned NumArgs = SF.Caller.arg_size(); |
| 680 | ArgVals.reserve(NumArgs); |
| 681 | for (CallSite::arg_iterator i = SF.Caller.arg_begin(), |
| 682 | e = SF.Caller.arg_end(); i != e; ++i) { |
| 683 | Value *V = *i; |
| 684 | ArgVals.push_back(getOperandValue(V, SF)); |
Chris Lattner | 9378013 | 2003-01-13 00:58:52 +0000 | [diff] [blame] | 685 | // Promote all integral types whose size is < sizeof(int) into ints. We do |
| 686 | // this by zero or sign extending the value as appropriate according to the |
| 687 | // source type. |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 688 | const Type *Ty = V->getType(); |
| 689 | if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) { |
Chris Lattner | 9378013 | 2003-01-13 00:58:52 +0000 | [diff] [blame] | 690 | if (Ty == Type::ShortTy) |
| 691 | ArgVals.back().IntVal = ArgVals.back().ShortVal; |
| 692 | else if (Ty == Type::UShortTy) |
| 693 | ArgVals.back().UIntVal = ArgVals.back().UShortVal; |
| 694 | else if (Ty == Type::SByteTy) |
| 695 | ArgVals.back().IntVal = ArgVals.back().SByteVal; |
| 696 | else if (Ty == Type::UByteTy) |
| 697 | ArgVals.back().UIntVal = ArgVals.back().UByteVal; |
| 698 | else if (Ty == Type::BoolTy) |
| 699 | ArgVals.back().UIntVal = ArgVals.back().BoolVal; |
| 700 | else |
| 701 | assert(0 && "Unknown type!"); |
| 702 | } |
| 703 | } |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 704 | |
Chris Lattner | 070cf5e | 2001-11-07 20:12:30 +0000 | [diff] [blame] | 705 | // 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] | 706 | // and treat it as a function pointer. |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 707 | GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 708 | callFunction((Function*)GVTOP(SRC), ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 711 | #define IMPLEMENT_SHIFT(OP, TY) \ |
| 712 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.UByteVal; break |
| 713 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 714 | void Interpreter::visitShl(ShiftInst &I) { |
| 715 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 716 | const Type *Ty = I.getOperand(0)->getType(); |
| 717 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 718 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 719 | GenericValue Dest; |
| 720 | |
| 721 | switch (Ty->getPrimitiveID()) { |
| 722 | IMPLEMENT_SHIFT(<<, UByte); |
| 723 | IMPLEMENT_SHIFT(<<, SByte); |
| 724 | IMPLEMENT_SHIFT(<<, UShort); |
| 725 | IMPLEMENT_SHIFT(<<, Short); |
| 726 | IMPLEMENT_SHIFT(<<, UInt); |
| 727 | IMPLEMENT_SHIFT(<<, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 728 | IMPLEMENT_SHIFT(<<, ULong); |
| 729 | IMPLEMENT_SHIFT(<<, Long); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 730 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 731 | std::cout << "Unhandled type for Shl instruction: " << *Ty << "\n"; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 732 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 733 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 736 | void Interpreter::visitShr(ShiftInst &I) { |
| 737 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 738 | const Type *Ty = I.getOperand(0)->getType(); |
| 739 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 740 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 741 | GenericValue Dest; |
| 742 | |
| 743 | switch (Ty->getPrimitiveID()) { |
| 744 | IMPLEMENT_SHIFT(>>, UByte); |
| 745 | IMPLEMENT_SHIFT(>>, SByte); |
| 746 | IMPLEMENT_SHIFT(>>, UShort); |
| 747 | IMPLEMENT_SHIFT(>>, Short); |
| 748 | IMPLEMENT_SHIFT(>>, UInt); |
| 749 | IMPLEMENT_SHIFT(>>, Int); |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 750 | IMPLEMENT_SHIFT(>>, ULong); |
| 751 | IMPLEMENT_SHIFT(>>, Long); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 752 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 753 | std::cout << "Unhandled type for Shr instruction: " << *Ty << "\n"; |
| 754 | abort(); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 755 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 756 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | #define IMPLEMENT_CAST(DTY, DCTY, STY) \ |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 760 | case Type::STY##TyID: Dest.DTY##Val = DCTY Src.STY##Val; break; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 761 | |
| 762 | #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY) \ |
| 763 | case Type::DESTTY##TyID: \ |
| 764 | switch (SrcTy->getPrimitiveID()) { \ |
Chris Lattner | f4dca80 | 2002-05-02 19:28:45 +0000 | [diff] [blame] | 765 | IMPLEMENT_CAST(DESTTY, DESTCTY, Bool); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 766 | IMPLEMENT_CAST(DESTTY, DESTCTY, UByte); \ |
| 767 | IMPLEMENT_CAST(DESTTY, DESTCTY, SByte); \ |
| 768 | IMPLEMENT_CAST(DESTTY, DESTCTY, UShort); \ |
| 769 | IMPLEMENT_CAST(DESTTY, DESTCTY, Short); \ |
| 770 | IMPLEMENT_CAST(DESTTY, DESTCTY, UInt); \ |
Chris Lattner | 7b851ab | 2001-10-15 19:18:26 +0000 | [diff] [blame] | 771 | IMPLEMENT_CAST(DESTTY, DESTCTY, Int); \ |
| 772 | IMPLEMENT_CAST(DESTTY, DESTCTY, ULong); \ |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 773 | IMPLEMENT_CAST(DESTTY, DESTCTY, Long); \ |
| 774 | IMPLEMENT_CAST(DESTTY, DESTCTY, Pointer); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 775 | |
| 776 | #define IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY) \ |
| 777 | IMPLEMENT_CAST(DESTTY, DESTCTY, Float); \ |
| 778 | IMPLEMENT_CAST(DESTTY, DESTCTY, Double) |
| 779 | |
| 780 | #define IMPLEMENT_CAST_CASE_END() \ |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 781 | default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \ |
| 782 | abort(); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 783 | } \ |
| 784 | break |
| 785 | |
| 786 | #define IMPLEMENT_CAST_CASE(DESTTY, DESTCTY) \ |
| 787 | IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY); \ |
| 788 | IMPLEMENT_CAST_CASE_FP_IMP(DESTTY, DESTCTY); \ |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 789 | IMPLEMENT_CAST_CASE_END() |
| 790 | |
Brian Gaeke | 29794cb | 2003-09-05 18:55:03 +0000 | [diff] [blame] | 791 | GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty, |
| 792 | ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 793 | const Type *SrcTy = SrcVal->getType(); |
| 794 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 795 | |
| 796 | switch (Ty->getPrimitiveID()) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 797 | IMPLEMENT_CAST_CASE(UByte , (unsigned char)); |
| 798 | IMPLEMENT_CAST_CASE(SByte , ( signed char)); |
| 799 | IMPLEMENT_CAST_CASE(UShort , (unsigned short)); |
Chris Lattner | 1bbd361 | 2002-08-02 22:06:04 +0000 | [diff] [blame] | 800 | IMPLEMENT_CAST_CASE(Short , ( signed short)); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 801 | IMPLEMENT_CAST_CASE(UInt , (unsigned int )); |
| 802 | IMPLEMENT_CAST_CASE(Int , ( signed int )); |
| 803 | IMPLEMENT_CAST_CASE(ULong , (uint64_t)); |
| 804 | IMPLEMENT_CAST_CASE(Long , ( int64_t)); |
Chris Lattner | 2fdaddf | 2002-10-30 21:47:57 +0000 | [diff] [blame] | 805 | IMPLEMENT_CAST_CASE(Pointer, (PointerTy)); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 806 | IMPLEMENT_CAST_CASE(Float , (float)); |
| 807 | IMPLEMENT_CAST_CASE(Double , (double)); |
Chris Lattner | 5bff50d | 2003-04-22 21:15:56 +0000 | [diff] [blame] | 808 | IMPLEMENT_CAST_CASE(Bool , (bool)); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 809 | default: |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 810 | std::cout << "Unhandled dest type for cast instruction: " << *Ty << "\n"; |
Chris Lattner | 5bff50d | 2003-04-22 21:15:56 +0000 | [diff] [blame] | 811 | abort(); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 812 | } |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 813 | |
| 814 | return Dest; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 815 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 816 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 817 | void Interpreter::visitCastInst(CastInst &I) { |
| 818 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 819 | SetValue(&I, executeCastOperation(I.getOperand(0), I.getType(), SF), SF); |
| 820 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 821 | |
Chris Lattner | 4c66549 | 2003-10-18 05:55:25 +0000 | [diff] [blame] | 822 | void Interpreter::visitVANextInst(VANextInst &I) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 823 | ExecutionContext &SF = ECStack.back(); |
| 824 | |
Brian Gaeke | 8da1748 | 2003-11-13 06:06:01 +0000 | [diff] [blame] | 825 | // Get the incoming valist parameter. LLI treats the valist as a pointer |
| 826 | // to the next argument. |
Chris Lattner | 4c66549 | 2003-10-18 05:55:25 +0000 | [diff] [blame] | 827 | GenericValue VAList = getOperandValue(I.getOperand(0), SF); |
| 828 | |
Brian Gaeke | 8da1748 | 2003-11-13 06:06:01 +0000 | [diff] [blame] | 829 | // Move the pointer to the next vararg. |
| 830 | GenericValue *ArgPtr = (GenericValue *) GVTOP (VAList); |
| 831 | ++ArgPtr; |
| 832 | VAList = PTOGV (ArgPtr); |
Chris Lattner | 4c66549 | 2003-10-18 05:55:25 +0000 | [diff] [blame] | 833 | SetValue(&I, VAList, SF); |
Chris Lattner | 374344c | 2003-05-08 16:52:43 +0000 | [diff] [blame] | 834 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 835 | |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 836 | #define IMPLEMENT_VAARG(TY) \ |
| 837 | case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break |
| 838 | |
| 839 | void Interpreter::visitVAArgInst(VAArgInst &I) { |
| 840 | ExecutionContext &SF = ECStack.back(); |
| 841 | |
Brian Gaeke | 8da1748 | 2003-11-13 06:06:01 +0000 | [diff] [blame] | 842 | // Get the incoming valist parameter. LLI treats the valist as a pointer |
| 843 | // to the next argument. |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 844 | GenericValue VAList = getOperandValue(I.getOperand(0), SF); |
Brian Gaeke | 8da1748 | 2003-11-13 06:06:01 +0000 | [diff] [blame] | 845 | assert (GVTOP (VAList) != 0 && "VAList was null in vaarg instruction"); |
| 846 | GenericValue Dest, Src = *(GenericValue *) GVTOP (VAList); |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 847 | const Type *Ty = I.getType(); |
| 848 | switch (Ty->getPrimitiveID()) { |
| 849 | IMPLEMENT_VAARG(UByte); |
| 850 | IMPLEMENT_VAARG(SByte); |
| 851 | IMPLEMENT_VAARG(UShort); |
| 852 | IMPLEMENT_VAARG(Short); |
| 853 | IMPLEMENT_VAARG(UInt); |
| 854 | IMPLEMENT_VAARG(Int); |
| 855 | IMPLEMENT_VAARG(ULong); |
| 856 | IMPLEMENT_VAARG(Long); |
| 857 | IMPLEMENT_VAARG(Pointer); |
| 858 | IMPLEMENT_VAARG(Float); |
| 859 | IMPLEMENT_VAARG(Double); |
| 860 | IMPLEMENT_VAARG(Bool); |
| 861 | default: |
| 862 | std::cout << "Unhandled dest type for vaarg instruction: " << *Ty << "\n"; |
| 863 | abort(); |
| 864 | } |
| 865 | |
| 866 | // Set the Value of this Instruction. |
| 867 | SetValue(&I, Dest, SF); |
| 868 | } |
| 869 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 870 | //===----------------------------------------------------------------------===// |
| 871 | // Dispatch and Execution Code |
| 872 | //===----------------------------------------------------------------------===// |
| 873 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 874 | //===----------------------------------------------------------------------===// |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 875 | // callFunction - Execute the specified function... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 876 | // |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 877 | void Interpreter::callFunction(Function *F, |
| 878 | const std::vector<GenericValue> &ArgVals) { |
Brian Gaeke | 2cb474c | 2003-11-07 19:26:23 +0000 | [diff] [blame] | 879 | assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 || |
| 880 | ECStack.back().Caller.arg_size() == ArgVals.size()) && |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 881 | "Incorrect number of arguments passed into function call!"); |
Chris Lattner | 63bd613 | 2003-09-17 17:26:22 +0000 | [diff] [blame] | 882 | // Make a new stack frame... and fill it in. |
| 883 | ECStack.push_back(ExecutionContext()); |
| 884 | ExecutionContext &StackFrame = ECStack.back(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 885 | StackFrame.CurFunction = F; |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 886 | |
| 887 | // Special handling for external functions. |
| 888 | if (F->isExternal()) { |
| 889 | GenericValue Result = callExternalFunction (F, ArgVals); |
| 890 | // Simulate a 'ret' instruction of the appropriate type. |
| 891 | popStackAndReturnValueToCaller (F->getReturnType (), Result); |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | // Get pointers to first LLVM BB & Instruction in function. |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 896 | StackFrame.CurBB = F->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 897 | StackFrame.CurInst = StackFrame.CurBB->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 899 | // Run through the function arguments and initialize their values... |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 900 | assert((ArgVals.size() == F->asize() || |
| 901 | (ArgVals.size() > F->asize() && F->getFunctionType()->isVarArg())) && |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 902 | "Invalid number of values passed to function invocation!"); |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 903 | |
| 904 | // Handle non-varargs arguments... |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 905 | unsigned i = 0; |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 906 | 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] | 907 | SetValue(AI, ArgVals[i], StackFrame); |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 908 | |
| 909 | // Handle varargs arguments... |
| 910 | StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end()); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 913 | void Interpreter::run() { |
Brian Gaeke | 9ad671d | 2003-09-04 23:15:40 +0000 | [diff] [blame] | 914 | while (!ECStack.empty()) { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 915 | // Interpret a single instruction & increment the "PC". |
| 916 | ExecutionContext &SF = ECStack.back(); // Current stack frame |
| 917 | Instruction &I = *SF.CurInst++; // Increment before execute |
| 918 | |
| 919 | // Track the number of dynamic instructions executed. |
| 920 | ++NumDynamicInsts; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 921 | |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 922 | visit(I); // Dispatch to one of the visit* methods... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 923 | } |
| 924 | } |