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