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