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" |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 24 | #include <cmath> |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 26 | |
Chris Lattner | cecf56b | 2006-12-19 22:56:53 +0000 | [diff] [blame] | 27 | STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed"); |
| 28 | static Interpreter *TheEE = 0; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 32 | // Value Manipulation code |
| 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 34 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 35 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
| 36 | const Type *Ty); |
| 37 | static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, |
| 38 | const Type *Ty); |
| 39 | static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, |
| 40 | const Type *Ty); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 41 | static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2, |
| 42 | const Type *Ty); |
| 43 | static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2, |
| 44 | const Type *Ty); |
| 45 | static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2, |
| 46 | const Type *Ty); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 47 | static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2, |
| 48 | const Type *Ty); |
| 49 | static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2, |
| 50 | const Type *Ty); |
| 51 | static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2, |
| 52 | const Type *Ty); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 53 | static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, |
| 54 | const Type *Ty); |
| 55 | static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, |
| 56 | const Type *Ty); |
| 57 | static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2, |
| 58 | const Type *Ty); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 59 | static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, |
| 60 | GenericValue Src2, const Type *Ty); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 61 | static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2, |
| 62 | const Type *Ty); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 63 | static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2, |
| 64 | const Type *Ty); |
| 65 | static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2, |
| 66 | const Type *Ty); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 67 | static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 68 | GenericValue Src3); |
| 69 | |
Reid Spencer | a42c7fd | 2007-01-20 20:12:29 +0000 | [diff] [blame] | 70 | inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) { |
| 71 | // Determine if the value is signed or not |
| 72 | bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0; |
| 73 | // If its signed, extend the sign bits |
| 74 | if (isSigned) |
| 75 | Val |= ~ITy->getBitMask(); |
| 76 | return Val; |
| 77 | } |
| 78 | |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 79 | GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE, |
| 80 | ExecutionContext &SF) { |
| 81 | switch (CE->getOpcode()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 82 | case Instruction::Trunc: |
| 83 | return executeTruncInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 84 | case Instruction::ZExt: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 85 | return executeZExtInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 86 | case Instruction::SExt: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 87 | return executeSExtInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 88 | case Instruction::FPTrunc: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 89 | return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 90 | case Instruction::FPExt: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 91 | return executeFPExtInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 92 | case Instruction::UIToFP: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 93 | return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 94 | case Instruction::SIToFP: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 95 | return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 96 | case Instruction::FPToUI: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 97 | return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 98 | case Instruction::FPToSI: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 99 | return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 100 | case Instruction::PtrToInt: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 101 | return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 102 | case Instruction::IntToPtr: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 103 | return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 104 | case Instruction::BitCast: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 105 | return executeBitCastInst(CE->getOperand(0), CE->getType(), SF); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 106 | case Instruction::GetElementPtr: |
| 107 | return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE), |
| 108 | gep_type_end(CE), SF); |
| 109 | case Instruction::Add: |
| 110 | return executeAddInst(getOperandValue(CE->getOperand(0), SF), |
| 111 | getOperandValue(CE->getOperand(1), SF), |
| 112 | CE->getOperand(0)->getType()); |
| 113 | case Instruction::Sub: |
| 114 | return executeSubInst(getOperandValue(CE->getOperand(0), SF), |
| 115 | getOperandValue(CE->getOperand(1), SF), |
| 116 | CE->getOperand(0)->getType()); |
| 117 | case Instruction::Mul: |
| 118 | return executeMulInst(getOperandValue(CE->getOperand(0), SF), |
| 119 | getOperandValue(CE->getOperand(1), SF), |
| 120 | CE->getOperand(0)->getType()); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 121 | case Instruction::SDiv: |
| 122 | return executeSDivInst(getOperandValue(CE->getOperand(0), SF), |
| 123 | getOperandValue(CE->getOperand(1), SF), |
| 124 | CE->getOperand(0)->getType()); |
| 125 | case Instruction::UDiv: |
| 126 | return executeUDivInst(getOperandValue(CE->getOperand(0), SF), |
| 127 | getOperandValue(CE->getOperand(1), SF), |
| 128 | CE->getOperand(0)->getType()); |
| 129 | case Instruction::FDiv: |
| 130 | return executeFDivInst(getOperandValue(CE->getOperand(0), SF), |
| 131 | getOperandValue(CE->getOperand(1), SF), |
| 132 | CE->getOperand(0)->getType()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 133 | case Instruction::URem: |
| 134 | return executeURemInst(getOperandValue(CE->getOperand(0), SF), |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 135 | getOperandValue(CE->getOperand(1), SF), |
| 136 | CE->getOperand(0)->getType()); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 137 | case Instruction::SRem: |
| 138 | return executeSRemInst(getOperandValue(CE->getOperand(0), SF), |
| 139 | getOperandValue(CE->getOperand(1), SF), |
| 140 | CE->getOperand(0)->getType()); |
| 141 | case Instruction::FRem: |
| 142 | return executeFRemInst(getOperandValue(CE->getOperand(0), SF), |
| 143 | getOperandValue(CE->getOperand(1), SF), |
| 144 | CE->getOperand(0)->getType()); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 145 | case Instruction::And: |
| 146 | return executeAndInst(getOperandValue(CE->getOperand(0), SF), |
| 147 | getOperandValue(CE->getOperand(1), SF), |
| 148 | CE->getOperand(0)->getType()); |
| 149 | case Instruction::Or: |
| 150 | return executeOrInst(getOperandValue(CE->getOperand(0), SF), |
| 151 | getOperandValue(CE->getOperand(1), SF), |
| 152 | CE->getOperand(0)->getType()); |
| 153 | case Instruction::Xor: |
| 154 | return executeXorInst(getOperandValue(CE->getOperand(0), SF), |
| 155 | getOperandValue(CE->getOperand(1), SF), |
| 156 | CE->getOperand(0)->getType()); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 157 | case Instruction::FCmp: |
| 158 | case Instruction::ICmp: |
| 159 | return executeCmpInst(CE->getPredicate(), |
| 160 | getOperandValue(CE->getOperand(0), SF), |
| 161 | getOperandValue(CE->getOperand(1), SF), |
| 162 | CE->getOperand(0)->getType()); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 163 | case Instruction::Shl: |
| 164 | return executeShlInst(getOperandValue(CE->getOperand(0), SF), |
| 165 | getOperandValue(CE->getOperand(1), SF), |
| 166 | CE->getOperand(0)->getType()); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 167 | case Instruction::LShr: |
| 168 | return executeLShrInst(getOperandValue(CE->getOperand(0), SF), |
| 169 | getOperandValue(CE->getOperand(1), SF), |
| 170 | CE->getOperand(0)->getType()); |
| 171 | case Instruction::AShr: |
| 172 | return executeAShrInst(getOperandValue(CE->getOperand(0), SF), |
| 173 | getOperandValue(CE->getOperand(1), SF), |
| 174 | CE->getOperand(0)->getType()); |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 175 | case Instruction::Select: |
| 176 | return executeSelectInst(getOperandValue(CE->getOperand(0), SF), |
| 177 | getOperandValue(CE->getOperand(1), SF), |
| 178 | getOperandValue(CE->getOperand(2), SF)); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 179 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 180 | cerr << "Unhandled ConstantExpr: " << *CE << "\n"; |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 181 | abort(); |
| 182 | return GenericValue(); |
| 183 | } |
| 184 | } |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 185 | |
Brian Gaeke | 29794cb | 2003-09-05 18:55:03 +0000 | [diff] [blame] | 186 | GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 187 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 188 | return getConstantExprValue(CE, SF); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 189 | } else if (Constant *CPV = dyn_cast<Constant>(V)) { |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 190 | return getConstantValue(CPV); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 191 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 192 | return PTOGV(getPointerToGlobal(GV)); |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 193 | } else { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 194 | return SF.Values[V]; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 198 | static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 199 | SF.Values[V] = Val; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 202 | void Interpreter::initializeExecutionEngine() { |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 203 | TheEE = this; |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 207 | // Binary Instruction Implementations |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | |
| 210 | #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ |
| 211 | case Type::TY##TyID: Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; break |
| 212 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 213 | #define IMPLEMENT_INTEGER_BINOP(OP, TY) \ |
| 214 | case Type::IntegerTyID: { \ |
| 215 | unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \ |
| 216 | if (BitWidth == 1) \ |
| 217 | Dest.Int1Val = Src1.Int1Val OP Src2.Int1Val; \ |
| 218 | else if (BitWidth <= 8) \ |
| 219 | Dest.Int8Val = Src1.Int8Val OP Src2.Int8Val; \ |
| 220 | else if (BitWidth <= 16) \ |
| 221 | Dest.Int16Val = Src1.Int16Val OP Src2.Int16Val; \ |
| 222 | else if (BitWidth <= 32) \ |
| 223 | Dest.Int32Val = Src1.Int32Val OP Src2.Int32Val; \ |
| 224 | else if (BitWidth <= 64) \ |
| 225 | Dest.Int64Val = Src1.Int64Val OP Src2.Int64Val; \ |
| 226 | else \ |
| 227 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \ |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 228 | maskToBitWidth(Dest, BitWidth); \ |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 229 | break; \ |
| 230 | } |
| 231 | |
| 232 | #define IMPLEMENT_SIGNED_BINOP(OP, TY) \ |
| 233 | if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \ |
| 234 | unsigned BitWidth = ITy->getBitWidth(); \ |
| 235 | if (BitWidth <= 8) \ |
| 236 | Dest.Int8Val = ((int8_t)Src1.Int8Val) OP ((int8_t)Src2.Int8Val); \ |
| 237 | else if (BitWidth <= 16) \ |
| 238 | Dest.Int16Val = ((int16_t)Src1.Int16Val) OP ((int16_t)Src2.Int16Val); \ |
| 239 | else if (BitWidth <= 32) \ |
| 240 | Dest.Int32Val = ((int32_t)Src1.Int32Val) OP ((int32_t)Src2.Int32Val); \ |
| 241 | else if (BitWidth <= 64) \ |
| 242 | Dest.Int64Val = ((int64_t)Src1.Int64Val) OP ((int64_t)Src2.Int64Val); \ |
| 243 | else { \ |
| 244 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \ |
| 245 | abort(); \ |
| 246 | } \ |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 247 | maskToBitWidth(Dest, BitWidth); \ |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 248 | } else { \ |
| 249 | cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \ |
| 250 | abort(); \ |
| 251 | } |
| 252 | |
| 253 | #define IMPLEMENT_UNSIGNED_BINOP(OP, TY) \ |
| 254 | if (const IntegerType *ITy = dyn_cast<IntegerType>(TY)) { \ |
| 255 | unsigned BitWidth = ITy->getBitWidth(); \ |
| 256 | if (BitWidth <= 8) \ |
| 257 | Dest.Int8Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \ |
| 258 | else if (BitWidth <= 16) \ |
| 259 | Dest.Int16Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \ |
| 260 | else if (BitWidth <= 32) \ |
| 261 | Dest.Int32Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \ |
| 262 | else if (BitWidth <= 64) \ |
| 263 | Dest.Int64Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \ |
| 264 | else { \ |
| 265 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \ |
| 266 | abort(); \ |
| 267 | } \ |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 268 | maskToBitWidth(Dest, BitWidth); \ |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 269 | } else { \ |
| 270 | cerr << "Unhandled type for " #OP " operator: " << *Ty << "\n"; \ |
| 271 | abort(); \ |
| 272 | } |
| 273 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 274 | static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, |
| 275 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 276 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 277 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 278 | IMPLEMENT_INTEGER_BINOP(+, Ty); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 279 | IMPLEMENT_BINARY_OPERATOR(+, Float); |
| 280 | IMPLEMENT_BINARY_OPERATOR(+, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 281 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 282 | cerr << "Unhandled type for Add instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 283 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 284 | } |
| 285 | return Dest; |
| 286 | } |
| 287 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 288 | static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, |
| 289 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 290 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 291 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 292 | IMPLEMENT_INTEGER_BINOP(-, Ty); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 293 | IMPLEMENT_BINARY_OPERATOR(-, Float); |
| 294 | IMPLEMENT_BINARY_OPERATOR(-, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 295 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 296 | cerr << "Unhandled type for Sub instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 297 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 298 | } |
| 299 | return Dest; |
| 300 | } |
| 301 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 302 | static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, |
| 303 | const Type *Ty) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 304 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 305 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 306 | IMPLEMENT_INTEGER_BINOP(*, Ty); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 307 | IMPLEMENT_BINARY_OPERATOR(*, Float); |
| 308 | IMPLEMENT_BINARY_OPERATOR(*, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 309 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 310 | cerr << "Unhandled type for Mul instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 311 | abort(); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 312 | } |
| 313 | return Dest; |
| 314 | } |
| 315 | |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 316 | static GenericValue executeUDivInst(GenericValue Src1, GenericValue Src2, |
| 317 | const Type *Ty) { |
| 318 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 319 | IMPLEMENT_UNSIGNED_BINOP(/,Ty) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 320 | return Dest; |
| 321 | } |
| 322 | |
| 323 | static GenericValue executeSDivInst(GenericValue Src1, GenericValue Src2, |
| 324 | const Type *Ty) { |
| 325 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 326 | IMPLEMENT_SIGNED_BINOP(/,Ty) |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 327 | return Dest; |
| 328 | } |
| 329 | |
| 330 | static GenericValue executeFDivInst(GenericValue Src1, GenericValue Src2, |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 331 | const Type *Ty) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 332 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 333 | switch (Ty->getTypeID()) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 334 | IMPLEMENT_BINARY_OPERATOR(/, Float); |
| 335 | IMPLEMENT_BINARY_OPERATOR(/, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 336 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 337 | cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 338 | abort(); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 339 | } |
| 340 | return Dest; |
| 341 | } |
| 342 | |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 343 | static GenericValue executeURemInst(GenericValue Src1, GenericValue Src2, |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 344 | const Type *Ty) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 345 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 346 | IMPLEMENT_UNSIGNED_BINOP(%, Ty) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 347 | return Dest; |
| 348 | } |
| 349 | |
| 350 | static GenericValue executeSRemInst(GenericValue Src1, GenericValue Src2, |
| 351 | const Type *Ty) { |
| 352 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 353 | IMPLEMENT_SIGNED_BINOP(%, Ty) |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 354 | return Dest; |
| 355 | } |
| 356 | |
| 357 | static GenericValue executeFRemInst(GenericValue Src1, GenericValue Src2, |
| 358 | const Type *Ty) { |
| 359 | GenericValue Dest; |
| 360 | switch (Ty->getTypeID()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 361 | case Type::FloatTyID: |
| 362 | Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); |
| 363 | break; |
| 364 | case Type::DoubleTyID: |
| 365 | Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); |
| 366 | break; |
| 367 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 368 | cerr << "Unhandled type for Rem instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 369 | abort(); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 370 | } |
| 371 | return Dest; |
| 372 | } |
| 373 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 374 | static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, |
| 375 | const Type *Ty) { |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 376 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 377 | IMPLEMENT_UNSIGNED_BINOP(&, Ty) |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 378 | return Dest; |
| 379 | } |
| 380 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 381 | static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 382 | const Type *Ty) { |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 383 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 384 | IMPLEMENT_UNSIGNED_BINOP(|, Ty) |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 385 | return Dest; |
| 386 | } |
| 387 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 388 | static GenericValue executeXorInst(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; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 391 | IMPLEMENT_UNSIGNED_BINOP(^, Ty) |
Chris Lattner | 4d0e1f9 | 2001-10-30 20:54:36 +0000 | [diff] [blame] | 392 | return Dest; |
| 393 | } |
| 394 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 395 | #define IMPLEMENT_SIGNED_ICMP(OP, TY) \ |
| 396 | case Type::IntegerTyID: { \ |
Reid Spencer | a42c7fd | 2007-01-20 20:12:29 +0000 | [diff] [blame] | 397 | const IntegerType* ITy = cast<IntegerType>(TY); \ |
| 398 | unsigned BitWidth = ITy->getBitWidth(); \ |
| 399 | int64_t LHS = 0, RHS = 0; \ |
| 400 | if (BitWidth <= 8) { \ |
| 401 | LHS = int64_t(doSignExtension(uint64_t(Src1.Int8Val), ITy)); \ |
| 402 | RHS = int64_t(doSignExtension(uint64_t(Src2.Int8Val), ITy)); \ |
| 403 | } else if (BitWidth <= 16) { \ |
| 404 | LHS = int64_t(doSignExtension(uint64_t(Src1.Int16Val), ITy)); \ |
| 405 | RHS = int64_t(doSignExtension(uint64_t(Src2.Int16Val), ITy)); \ |
| 406 | } else if (BitWidth <= 32) { \ |
| 407 | LHS = int64_t(doSignExtension(uint64_t(Src1.Int32Val), ITy)); \ |
| 408 | RHS = int64_t(doSignExtension(uint64_t(Src2.Int32Val), ITy)); \ |
| 409 | } else if (BitWidth <= 64) { \ |
| 410 | LHS = int64_t(doSignExtension(uint64_t(Src1.Int64Val), ITy)); \ |
| 411 | RHS = int64_t(doSignExtension(uint64_t(Src2.Int64Val), ITy)); \ |
| 412 | } else { \ |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 413 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \ |
| 414 | abort(); \ |
| 415 | } \ |
Reid Spencer | a42c7fd | 2007-01-20 20:12:29 +0000 | [diff] [blame] | 416 | Dest.Int1Val = LHS OP RHS; \ |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 417 | break; \ |
| 418 | } |
| 419 | |
| 420 | #define IMPLEMENT_UNSIGNED_ICMP(OP, TY) \ |
| 421 | case Type::IntegerTyID: { \ |
| 422 | unsigned BitWidth = cast<IntegerType>(TY)->getBitWidth(); \ |
| 423 | if (BitWidth == 1) \ |
| 424 | Dest.Int1Val = ((uint8_t)Src1.Int1Val) OP ((uint8_t)Src2.Int1Val); \ |
| 425 | else if (BitWidth <= 8) \ |
| 426 | Dest.Int1Val = ((uint8_t)Src1.Int8Val) OP ((uint8_t)Src2.Int8Val); \ |
| 427 | else if (BitWidth <= 16) \ |
| 428 | Dest.Int1Val = ((uint16_t)Src1.Int16Val) OP ((uint16_t)Src2.Int16Val); \ |
| 429 | else if (BitWidth <= 32) \ |
| 430 | Dest.Int1Val = ((uint32_t)Src1.Int32Val) OP ((uint32_t)Src2.Int32Val); \ |
| 431 | else if (BitWidth <= 64) \ |
| 432 | Dest.Int1Val = ((uint64_t)Src1.Int64Val) OP ((uint64_t)Src2.Int64Val); \ |
| 433 | else { \ |
| 434 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \ |
| 435 | abort(); \ |
| 436 | } \ |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 437 | maskToBitWidth(Dest, BitWidth); \ |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 438 | break; \ |
| 439 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 440 | |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 441 | // Handle pointers specially because they must be compared with only as much |
| 442 | // width as the host has. We _do not_ want to be comparing 64 bit values when |
| 443 | // running on a 32-bit target, otherwise the upper 32 bits might mess up |
| 444 | // comparisons if they contain garbage. |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 445 | #define IMPLEMENT_POINTER_ICMP(OP) \ |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 446 | case Type::PointerTyID: \ |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 447 | Dest.Int1Val = (void*)(intptr_t)Src1.PointerVal OP \ |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 448 | (void*)(intptr_t)Src2.PointerVal; break |
| 449 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 450 | static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2, |
| 451 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 452 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 453 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 454 | IMPLEMENT_UNSIGNED_ICMP(==, Ty); |
| 455 | IMPLEMENT_POINTER_ICMP(==); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 456 | default: |
| 457 | cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n"; |
| 458 | abort(); |
| 459 | } |
| 460 | return Dest; |
| 461 | } |
| 462 | |
| 463 | static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2, |
| 464 | const Type *Ty) { |
| 465 | GenericValue Dest; |
| 466 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 467 | IMPLEMENT_UNSIGNED_ICMP(!=, Ty); |
| 468 | IMPLEMENT_POINTER_ICMP(!=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 469 | default: |
| 470 | cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n"; |
| 471 | abort(); |
| 472 | } |
| 473 | return Dest; |
| 474 | } |
| 475 | |
| 476 | static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2, |
| 477 | const Type *Ty) { |
| 478 | GenericValue Dest; |
| 479 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 480 | IMPLEMENT_UNSIGNED_ICMP(<, Ty); |
| 481 | IMPLEMENT_POINTER_ICMP(<); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 482 | default: |
| 483 | cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n"; |
| 484 | abort(); |
| 485 | } |
| 486 | return Dest; |
| 487 | } |
| 488 | |
| 489 | static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2, |
| 490 | const Type *Ty) { |
| 491 | GenericValue Dest; |
| 492 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 493 | IMPLEMENT_SIGNED_ICMP(<, Ty); |
| 494 | IMPLEMENT_POINTER_ICMP(<); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 495 | default: |
| 496 | cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n"; |
| 497 | abort(); |
| 498 | } |
| 499 | return Dest; |
| 500 | } |
| 501 | |
| 502 | static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2, |
| 503 | const Type *Ty) { |
| 504 | GenericValue Dest; |
| 505 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 506 | IMPLEMENT_UNSIGNED_ICMP(>, Ty); |
| 507 | IMPLEMENT_POINTER_ICMP(>); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 508 | default: |
| 509 | cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n"; |
| 510 | abort(); |
| 511 | } |
| 512 | return Dest; |
| 513 | } |
| 514 | |
| 515 | static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2, |
| 516 | const Type *Ty) { |
| 517 | GenericValue Dest; |
| 518 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 519 | IMPLEMENT_SIGNED_ICMP(>, Ty); |
| 520 | IMPLEMENT_POINTER_ICMP(>); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 521 | default: |
| 522 | cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n"; |
| 523 | abort(); |
| 524 | } |
| 525 | return Dest; |
| 526 | } |
| 527 | |
| 528 | static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2, |
| 529 | const Type *Ty) { |
| 530 | GenericValue Dest; |
| 531 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 532 | IMPLEMENT_UNSIGNED_ICMP(<=, Ty); |
| 533 | IMPLEMENT_POINTER_ICMP(<=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 534 | default: |
| 535 | cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n"; |
| 536 | abort(); |
| 537 | } |
| 538 | return Dest; |
| 539 | } |
| 540 | |
| 541 | static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2, |
| 542 | const Type *Ty) { |
| 543 | GenericValue Dest; |
| 544 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 545 | IMPLEMENT_SIGNED_ICMP(<=, Ty); |
| 546 | IMPLEMENT_POINTER_ICMP(<=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 547 | default: |
| 548 | cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n"; |
| 549 | abort(); |
| 550 | } |
| 551 | return Dest; |
| 552 | } |
| 553 | |
| 554 | static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2, |
| 555 | const Type *Ty) { |
| 556 | GenericValue Dest; |
| 557 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 558 | IMPLEMENT_UNSIGNED_ICMP(>=,Ty); |
| 559 | IMPLEMENT_POINTER_ICMP(>=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 560 | default: |
| 561 | cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n"; |
| 562 | abort(); |
| 563 | } |
| 564 | return Dest; |
| 565 | } |
| 566 | |
| 567 | static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2, |
| 568 | const Type *Ty) { |
| 569 | GenericValue Dest; |
| 570 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 571 | IMPLEMENT_SIGNED_ICMP(>=, Ty); |
| 572 | IMPLEMENT_POINTER_ICMP(>=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 573 | default: |
| 574 | cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n"; |
| 575 | abort(); |
| 576 | } |
| 577 | return Dest; |
| 578 | } |
| 579 | |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 580 | void Interpreter::visitICmpInst(ICmpInst &I) { |
| 581 | ExecutionContext &SF = ECStack.back(); |
| 582 | const Type *Ty = I.getOperand(0)->getType(); |
| 583 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 584 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 585 | GenericValue R; // Result |
| 586 | |
| 587 | switch (I.getPredicate()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 588 | case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break; |
| 589 | case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break; |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 590 | case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break; |
| 591 | case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break; |
| 592 | case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break; |
| 593 | case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break; |
| 594 | case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break; |
| 595 | case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break; |
| 596 | case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break; |
| 597 | case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break; |
| 598 | default: |
| 599 | cerr << "Don't know how to handle this ICmp predicate!\n-->" << I; |
| 600 | abort(); |
| 601 | } |
| 602 | |
| 603 | SetValue(&I, R, SF); |
| 604 | } |
| 605 | |
| 606 | #define IMPLEMENT_FCMP(OP, TY) \ |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 607 | case Type::TY##TyID: Dest.Int1Val = Src1.TY##Val OP Src2.TY##Val; break |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 608 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 609 | static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 610 | const Type *Ty) { |
| 611 | GenericValue Dest; |
| 612 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 613 | IMPLEMENT_FCMP(==, Float); |
| 614 | IMPLEMENT_FCMP(==, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 615 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 616 | cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 617 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 618 | } |
| 619 | return Dest; |
| 620 | } |
| 621 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 622 | static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 623 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 624 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 625 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 626 | IMPLEMENT_FCMP(!=, Float); |
| 627 | IMPLEMENT_FCMP(!=, Double); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 629 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 630 | cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 631 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 632 | } |
| 633 | return Dest; |
| 634 | } |
| 635 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 636 | static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 637 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 638 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 639 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 640 | IMPLEMENT_FCMP(<=, Float); |
| 641 | IMPLEMENT_FCMP(<=, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 642 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 643 | cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 644 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 645 | } |
| 646 | return Dest; |
| 647 | } |
| 648 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 649 | static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 650 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 651 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 652 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 653 | IMPLEMENT_FCMP(>=, Float); |
| 654 | IMPLEMENT_FCMP(>=, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 655 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 656 | cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 657 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 658 | } |
| 659 | return Dest; |
| 660 | } |
| 661 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 662 | static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2, |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 663 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 664 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 665 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 666 | IMPLEMENT_FCMP(<, Float); |
| 667 | IMPLEMENT_FCMP(<, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 668 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 669 | cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 670 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 671 | } |
| 672 | return Dest; |
| 673 | } |
| 674 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 675 | static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2, |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 676 | const Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 677 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 678 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 679 | IMPLEMENT_FCMP(>, Float); |
| 680 | IMPLEMENT_FCMP(>, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 681 | default: |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 682 | cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 683 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 684 | } |
| 685 | return Dest; |
| 686 | } |
| 687 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 688 | #define IMPLEMENT_UNORDERED(TY, X,Y) \ |
| 689 | if (TY == Type::FloatTy) \ |
| 690 | if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \ |
| 691 | Dest.Int1Val = true; \ |
| 692 | return Dest; \ |
| 693 | } \ |
| 694 | else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \ |
| 695 | Dest.Int1Val = true; \ |
| 696 | return Dest; \ |
| 697 | } |
| 698 | |
| 699 | |
| 700 | static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2, |
| 701 | const Type *Ty) { |
| 702 | GenericValue Dest; |
| 703 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
| 704 | return executeFCMP_OEQ(Src1, Src2, Ty); |
| 705 | } |
| 706 | |
| 707 | static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2, |
| 708 | const Type *Ty) { |
| 709 | GenericValue Dest; |
| 710 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
| 711 | return executeFCMP_ONE(Src1, Src2, Ty); |
| 712 | } |
| 713 | |
| 714 | static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2, |
| 715 | const Type *Ty) { |
| 716 | GenericValue Dest; |
| 717 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
| 718 | return executeFCMP_OLE(Src1, Src2, Ty); |
| 719 | } |
| 720 | |
| 721 | static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2, |
| 722 | const Type *Ty) { |
| 723 | GenericValue Dest; |
| 724 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
| 725 | return executeFCMP_OGE(Src1, Src2, Ty); |
| 726 | } |
| 727 | |
| 728 | static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2, |
| 729 | const Type *Ty) { |
| 730 | GenericValue Dest; |
| 731 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
| 732 | return executeFCMP_OLT(Src1, Src2, Ty); |
| 733 | } |
| 734 | |
| 735 | static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2, |
| 736 | const Type *Ty) { |
| 737 | GenericValue Dest; |
| 738 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
| 739 | return executeFCMP_OGT(Src1, Src2, Ty); |
| 740 | } |
| 741 | |
| 742 | static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2, |
| 743 | const Type *Ty) { |
| 744 | GenericValue Dest; |
| 745 | if (Ty == Type::FloatTy) |
| 746 | Dest.Int1Val = (Src1.FloatVal == Src1.FloatVal && |
| 747 | Src2.FloatVal == Src2.FloatVal); |
| 748 | else |
| 749 | Dest.Int1Val = (Src1.DoubleVal == Src1.DoubleVal && |
| 750 | Src2.DoubleVal == Src2.DoubleVal); |
| 751 | return Dest; |
| 752 | } |
| 753 | |
| 754 | static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2, |
| 755 | const Type *Ty) { |
| 756 | GenericValue Dest; |
| 757 | if (Ty == Type::FloatTy) |
| 758 | Dest.Int1Val = (Src1.FloatVal != Src1.FloatVal || |
| 759 | Src2.FloatVal != Src2.FloatVal); |
| 760 | else |
| 761 | Dest.Int1Val = (Src1.DoubleVal != Src1.DoubleVal || |
| 762 | Src2.DoubleVal != Src2.DoubleVal); |
| 763 | return Dest; |
| 764 | } |
| 765 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 766 | void Interpreter::visitFCmpInst(FCmpInst &I) { |
| 767 | ExecutionContext &SF = ECStack.back(); |
| 768 | const Type *Ty = I.getOperand(0)->getType(); |
| 769 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 770 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 771 | GenericValue R; // Result |
| 772 | |
| 773 | switch (I.getPredicate()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 774 | case FCmpInst::FCMP_FALSE: R.Int1Val = false; break; |
| 775 | case FCmpInst::FCMP_TRUE: R.Int1Val = true; break; |
| 776 | case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break; |
| 777 | case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break; |
| 778 | case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break; |
| 779 | case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break; |
| 780 | case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break; |
| 781 | case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break; |
| 782 | case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break; |
| 783 | case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break; |
| 784 | case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break; |
| 785 | case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break; |
| 786 | case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break; |
| 787 | case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break; |
| 788 | case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break; |
| 789 | case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 790 | default: |
| 791 | cerr << "Don't know how to handle this FCmp predicate!\n-->" << I; |
| 792 | abort(); |
| 793 | } |
| 794 | |
| 795 | SetValue(&I, R, SF); |
| 796 | } |
| 797 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 798 | static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, |
| 799 | GenericValue Src2, const Type *Ty) { |
| 800 | GenericValue Result; |
| 801 | switch (predicate) { |
| 802 | case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty); |
| 803 | case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty); |
| 804 | case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty); |
| 805 | case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty); |
| 806 | case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty); |
| 807 | case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty); |
| 808 | case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty); |
| 809 | case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty); |
| 810 | case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty); |
| 811 | case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 812 | case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty); |
| 813 | case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty); |
| 814 | case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty); |
| 815 | case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty); |
| 816 | case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty); |
| 817 | case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty); |
| 818 | case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty); |
| 819 | case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty); |
| 820 | case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty); |
| 821 | case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty); |
| 822 | case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty); |
| 823 | case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty); |
| 824 | case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty); |
| 825 | case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 826 | case FCmpInst::FCMP_FALSE: { |
| 827 | GenericValue Result; |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 828 | Result.Int1Val = false; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 829 | return Result; |
| 830 | } |
| 831 | case FCmpInst::FCMP_TRUE: { |
| 832 | GenericValue Result; |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 833 | Result.Int1Val = true; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 834 | return Result; |
| 835 | } |
| 836 | default: |
| 837 | cerr << "Unhandled Cmp predicate\n"; |
| 838 | abort(); |
| 839 | } |
| 840 | } |
| 841 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 842 | void Interpreter::visitBinaryOperator(BinaryOperator &I) { |
| 843 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 844 | const Type *Ty = I.getOperand(0)->getType(); |
| 845 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 846 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 847 | GenericValue R; // Result |
| 848 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 849 | switch (I.getOpcode()) { |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 850 | case Instruction::Add: R = executeAddInst (Src1, Src2, Ty); break; |
| 851 | case Instruction::Sub: R = executeSubInst (Src1, Src2, Ty); break; |
| 852 | case Instruction::Mul: R = executeMulInst (Src1, Src2, Ty); break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 853 | case Instruction::UDiv: R = executeUDivInst (Src1, Src2, Ty); break; |
| 854 | case Instruction::SDiv: R = executeSDivInst (Src1, Src2, Ty); break; |
| 855 | case Instruction::FDiv: R = executeFDivInst (Src1, Src2, Ty); break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 856 | case Instruction::URem: R = executeURemInst (Src1, Src2, Ty); break; |
| 857 | case Instruction::SRem: R = executeSRemInst (Src1, Src2, Ty); break; |
| 858 | case Instruction::FRem: R = executeFRemInst (Src1, Src2, Ty); break; |
Chris Lattner | b945e4d | 2003-04-22 20:37:39 +0000 | [diff] [blame] | 859 | case Instruction::And: R = executeAndInst (Src1, Src2, Ty); break; |
| 860 | case Instruction::Or: R = executeOrInst (Src1, Src2, Ty); break; |
| 861 | case Instruction::Xor: R = executeXorInst (Src1, Src2, Ty); break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 862 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 863 | cerr << "Don't know how to handle this binary operator!\n-->" << I; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 864 | abort(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 867 | SetValue(&I, R, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 870 | static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 871 | GenericValue Src3) { |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 872 | return Src1.Int1Val ? Src2 : Src3; |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | void Interpreter::visitSelectInst(SelectInst &I) { |
| 876 | ExecutionContext &SF = ECStack.back(); |
| 877 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 878 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 879 | GenericValue Src3 = getOperandValue(I.getOperand(2), SF); |
| 880 | GenericValue R = executeSelectInst(Src1, Src2, Src3); |
| 881 | SetValue(&I, R, SF); |
| 882 | } |
| 883 | |
| 884 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 885 | //===----------------------------------------------------------------------===// |
| 886 | // Terminator Instruction Implementations |
| 887 | //===----------------------------------------------------------------------===// |
| 888 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 889 | void Interpreter::exitCalled(GenericValue GV) { |
Brian Gaeke | d8400d8 | 2004-02-13 05:48:00 +0000 | [diff] [blame] | 890 | // runAtExitHandlers() assumes there are no stack frames, but |
| 891 | // if exit() was called, then it had a stack frame. Blow away |
| 892 | // the stack before interpreting atexit handlers. |
| 893 | ECStack.clear (); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 894 | runAtExitHandlers (); |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 895 | exit (GV.Int32Val); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 898 | /// Pop the last stack frame off of ECStack and then copy the result |
| 899 | /// back into the result variable if we are not returning void. The |
Jeff Cohen | 8c9191c | 2006-02-07 05:29:44 +0000 | [diff] [blame] | 900 | /// result variable may be the ExitValue, or the Value of the calling |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 901 | /// CallInst if there was a previous stack frame. This method may |
| 902 | /// invalidate any ECStack iterators you have. This method also takes |
| 903 | /// care of switching to the normal destination BB, if we are returning |
| 904 | /// from an invoke. |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 905 | /// |
| 906 | void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy, |
| 907 | GenericValue Result) { |
| 908 | // Pop the current stack frame. |
| 909 | ECStack.pop_back(); |
| 910 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 911 | if (ECStack.empty()) { // Finished main. Put result into exit code... |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 912 | if (RetTy && RetTy->isInteger()) { // Nonvoid return type? |
Jeff Cohen | 8c9191c | 2006-02-07 05:29:44 +0000 | [diff] [blame] | 913 | ExitValue = Result; // Capture the exit value of the program |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 914 | } else { |
Jeff Cohen | 8c9191c | 2006-02-07 05:29:44 +0000 | [diff] [blame] | 915 | memset(&ExitValue, 0, sizeof(ExitValue)); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 916 | } |
| 917 | } else { |
| 918 | // If we have a previous stack frame, and we have a previous call, |
| 919 | // fill in the return value... |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 920 | ExecutionContext &CallingSF = ECStack.back(); |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 921 | if (Instruction *I = CallingSF.Caller.getInstruction()) { |
Brian Gaeke | 2cb474c | 2003-11-07 19:26:23 +0000 | [diff] [blame] | 922 | if (CallingSF.Caller.getType() != Type::VoidTy) // Save result... |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 923 | SetValue(I, Result, CallingSF); |
| 924 | if (InvokeInst *II = dyn_cast<InvokeInst> (I)) |
| 925 | SwitchToNewBasicBlock (II->getNormalDest (), CallingSF); |
Brian Gaeke | 2cb474c | 2003-11-07 19:26:23 +0000 | [diff] [blame] | 926 | CallingSF.Caller = CallSite(); // We returned from the call... |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 931 | void Interpreter::visitReturnInst(ReturnInst &I) { |
| 932 | ExecutionContext &SF = ECStack.back(); |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 933 | const Type *RetTy = Type::VoidTy; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 934 | GenericValue Result; |
| 935 | |
| 936 | // Save away the return value... (if we are not 'ret void') |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 937 | if (I.getNumOperands()) { |
| 938 | RetTy = I.getReturnValue()->getType(); |
| 939 | Result = getOperandValue(I.getReturnValue(), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 942 | popStackAndReturnValueToCaller(RetTy, Result); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Brian Gaeke | 9bf06b1 | 2003-11-07 20:07:06 +0000 | [diff] [blame] | 945 | void Interpreter::visitUnwindInst(UnwindInst &I) { |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 946 | // Unwind stack |
| 947 | Instruction *Inst; |
| 948 | do { |
| 949 | ECStack.pop_back (); |
| 950 | if (ECStack.empty ()) |
| 951 | abort (); |
| 952 | Inst = ECStack.back ().Caller.getInstruction (); |
| 953 | } while (!(Inst && isa<InvokeInst> (Inst))); |
| 954 | |
| 955 | // Return from invoke |
| 956 | ExecutionContext &InvokingSF = ECStack.back (); |
| 957 | InvokingSF.Caller = CallSite (); |
| 958 | |
| 959 | // Go to exceptional destination BB of invoke instruction |
Chris Lattner | aeb2a1d | 2004-02-08 21:44:31 +0000 | [diff] [blame] | 960 | SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF); |
Brian Gaeke | 9bf06b1 | 2003-11-07 20:07:06 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Chris Lattner | ec7c1ab | 2004-10-16 18:21:33 +0000 | [diff] [blame] | 963 | void Interpreter::visitUnreachableInst(UnreachableInst &I) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 964 | cerr << "ERROR: Program executed an 'unreachable' instruction!\n"; |
Chris Lattner | ec7c1ab | 2004-10-16 18:21:33 +0000 | [diff] [blame] | 965 | abort(); |
| 966 | } |
| 967 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 968 | void Interpreter::visitBranchInst(BranchInst &I) { |
| 969 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 970 | BasicBlock *Dest; |
| 971 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 972 | Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... |
| 973 | if (!I.isUnconditional()) { |
| 974 | Value *Cond = I.getCondition(); |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 975 | if (getOperandValue(Cond, SF).Int1Val == 0) // If false cond... |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 976 | Dest = I.getSuccessor(1); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 977 | } |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 978 | SwitchToNewBasicBlock(Dest, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 981 | void Interpreter::visitSwitchInst(SwitchInst &I) { |
| 982 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 983 | GenericValue CondVal = getOperandValue(I.getOperand(0), SF); |
| 984 | const Type *ElTy = I.getOperand(0)->getType(); |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 985 | |
| 986 | // Check to see if any of the cases match... |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 987 | BasicBlock *Dest = 0; |
| 988 | for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 989 | if (executeICMP_EQ(CondVal, |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 990 | getOperandValue(I.getOperand(i), SF), ElTy).Int1Val) { |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 991 | Dest = cast<BasicBlock>(I.getOperand(i+1)); |
| 992 | break; |
| 993 | } |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 995 | if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 996 | SwitchToNewBasicBlock(Dest, SF); |
| 997 | } |
| 998 | |
| 999 | // SwitchToNewBasicBlock - This method is used to jump to a new basic block. |
| 1000 | // This function handles the actual updating of block and instruction iterators |
| 1001 | // as well as execution of all of the PHI nodes in the destination block. |
| 1002 | // |
| 1003 | // This method does this because all of the PHI nodes must be executed |
| 1004 | // atomically, reading their inputs before any of the results are updated. Not |
| 1005 | // doing this can cause problems if the PHI nodes depend on other PHI nodes for |
| 1006 | // their inputs. If the input PHI node is updated before it is read, incorrect |
| 1007 | // results can happen. Thus we use a two phase approach. |
| 1008 | // |
| 1009 | void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){ |
| 1010 | BasicBlock *PrevBB = SF.CurBB; // Remember where we came from... |
| 1011 | SF.CurBB = Dest; // Update CurBB to branch destination |
| 1012 | SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... |
| 1013 | |
| 1014 | if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do |
| 1015 | |
| 1016 | // Loop over all of the PHI nodes in the current block, reading their inputs. |
| 1017 | std::vector<GenericValue> ResultValues; |
| 1018 | |
| 1019 | for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) { |
| 1020 | // Search for the value corresponding to this previous bb... |
| 1021 | int i = PN->getBasicBlockIndex(PrevBB); |
| 1022 | assert(i != -1 && "PHINode doesn't contain entry for predecessor??"); |
| 1023 | Value *IncomingValue = PN->getIncomingValue(i); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 1025 | // Save the incoming value for this PHI node... |
| 1026 | ResultValues.push_back(getOperandValue(IncomingValue, SF)); |
| 1027 | } |
| 1028 | |
| 1029 | // Now loop over all of the PHI nodes setting their values... |
| 1030 | SF.CurInst = SF.CurBB->begin(); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 1031 | for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) { |
| 1032 | PHINode *PN = cast<PHINode>(SF.CurInst); |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 1033 | SetValue(PN, ResultValues[i], SF); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 1034 | } |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1037 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1038 | // Memory Instruction Implementations |
| 1039 | //===----------------------------------------------------------------------===// |
| 1040 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1041 | void Interpreter::visitAllocationInst(AllocationInst &I) { |
| 1042 | ExecutionContext &SF = ECStack.back(); |
| 1043 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1044 | const Type *Ty = I.getType()->getElementType(); // Type to be allocated |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1045 | |
Chris Lattner | cc82cc1 | 2002-04-28 21:57:33 +0000 | [diff] [blame] | 1046 | // Get the number of elements being allocated by the array... |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 1047 | unsigned NumElements = getOperandValue(I.getOperand(0), SF).Int32Val; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1048 | |
| 1049 | // Allocate enough memory to hold the type... |
Chris Lattner | d564496 | 2005-01-08 20:05:34 +0000 | [diff] [blame] | 1050 | void *Memory = malloc(NumElements * (size_t)TD.getTypeSize(Ty)); |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 1051 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1052 | GenericValue Result = PTOGV(Memory); |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1053 | assert(Result.PointerVal != 0 && "Null pointer returned by malloc!"); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1054 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1055 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1056 | if (I.getOpcode() == Instruction::Alloca) |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 1057 | ECStack.back().Allocas.add(Memory); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1060 | void Interpreter::visitFreeInst(FreeInst &I) { |
| 1061 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1062 | assert(isa<PointerType>(I.getOperand(0)->getType()) && "Freeing nonptr?"); |
| 1063 | GenericValue Value = getOperandValue(I.getOperand(0), SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1064 | // TODO: Check to make sure memory is allocated |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1065 | free(GVTOP(Value)); // Free memory |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1068 | // getElementOffset - The workhorse for getelementptr. |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1069 | // |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1070 | GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I, |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1071 | gep_type_iterator E, |
| 1072 | ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1073 | assert(isa<PointerType>(Ptr->getType()) && |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1074 | "Cannot getElementOffset of a nonpointer type!"); |
| 1075 | |
Chris Lattner | ea38c0e | 2001-11-07 19:46:27 +0000 | [diff] [blame] | 1076 | PointerTy Total = 0; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1077 | |
| 1078 | for (; I != E; ++I) { |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1079 | if (const StructType *STy = dyn_cast<StructType>(*I)) { |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 1080 | const StructLayout *SLO = TD.getStructLayout(STy); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1081 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1082 | const ConstantInt *CPU = cast<ConstantInt>(I.getOperand()); |
| 1083 | unsigned Index = unsigned(CPU->getZExtValue()); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1084 | |
Chris Lattner | d564496 | 2005-01-08 20:05:34 +0000 | [diff] [blame] | 1085 | Total += (PointerTy)SLO->MemberOffsets[Index]; |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1086 | } else { |
| 1087 | const SequentialType *ST = cast<SequentialType>(*I); |
Chris Lattner | 006a4a5 | 2003-02-25 21:14:59 +0000 | [diff] [blame] | 1088 | // Get the index number for the array... which must be long type... |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1089 | GenericValue IdxGV = getOperandValue(I.getOperand(), SF); |
| 1090 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1091 | int64_t Idx; |
| 1092 | unsigned BitWidth = |
| 1093 | cast<IntegerType>(I.getOperand()->getType())->getBitWidth(); |
Reid Spencer | 23e2883 | 2007-01-18 01:25:42 +0000 | [diff] [blame] | 1094 | if (BitWidth == 32) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1095 | Idx = (int64_t)(int32_t)IdxGV.Int32Val; |
Reid Spencer | 23e2883 | 2007-01-18 01:25:42 +0000 | [diff] [blame] | 1096 | else if (BitWidth == 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1097 | Idx = (int64_t)IdxGV.Int64Val; |
| 1098 | else |
Reid Spencer | 23e2883 | 2007-01-18 01:25:42 +0000 | [diff] [blame] | 1099 | assert(0 && "Invalid index type for getelementptr"); |
Chris Lattner | d564496 | 2005-01-08 20:05:34 +0000 | [diff] [blame] | 1100 | Total += PointerTy(TD.getTypeSize(ST->getElementType())*Idx); |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 1101 | } |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1104 | GenericValue Result; |
| 1105 | Result.PointerVal = getOperandValue(Ptr, SF).PointerVal + Total; |
| 1106 | return Result; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1109 | void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 1110 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1111 | SetValue(&I, TheEE->executeGEPOperation(I.getPointerOperand(), |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1112 | gep_type_begin(I), gep_type_end(I), SF), SF); |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1115 | void Interpreter::visitLoadInst(LoadInst &I) { |
| 1116 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1117 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1118 | GenericValue *Ptr = (GenericValue*)GVTOP(SRC); |
Chris Lattner | 374344c | 2003-05-08 16:52:43 +0000 | [diff] [blame] | 1119 | GenericValue Result = LoadValueFromMemory(Ptr, I.getType()); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1120 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1123 | void Interpreter::visitStoreInst(StoreInst &I) { |
| 1124 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | fddc755 | 2002-10-15 20:34:05 +0000 | [diff] [blame] | 1125 | GenericValue Val = getOperandValue(I.getOperand(0), SF); |
| 1126 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1127 | StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC), |
Chris Lattner | 683d5da9 | 2002-10-26 01:57:15 +0000 | [diff] [blame] | 1128 | I.getOperand(0)->getType()); |
Chris Lattner | fddc755 | 2002-10-15 20:34:05 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1131 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1132 | // Miscellaneous Instruction Implementations |
| 1133 | //===----------------------------------------------------------------------===// |
| 1134 | |
Brian Gaeke | fea483d | 2003-11-07 20:04:22 +0000 | [diff] [blame] | 1135 | void Interpreter::visitCallSite(CallSite CS) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1136 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1137 | |
| 1138 | // Check to see if this is an intrinsic function call... |
| 1139 | if (Function *F = CS.getCalledFunction()) |
Brian Gaeke | 34562ba | 2004-01-14 06:02:53 +0000 | [diff] [blame] | 1140 | if (F->isExternal ()) |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1141 | switch (F->getIntrinsicID()) { |
Brian Gaeke | 34562ba | 2004-01-14 06:02:53 +0000 | [diff] [blame] | 1142 | case Intrinsic::not_intrinsic: |
| 1143 | break; |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1144 | case Intrinsic::vastart: { // va_start |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1145 | GenericValue ArgIndex; |
| 1146 | ArgIndex.UIntPairVal.first = ECStack.size() - 1; |
| 1147 | ArgIndex.UIntPairVal.second = 0; |
| 1148 | SetValue(CS.getInstruction(), ArgIndex, SF); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1149 | return; |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1150 | } |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1151 | case Intrinsic::vaend: // va_end is a noop for the interpreter |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1152 | return; |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1153 | case Intrinsic::vacopy: // va_copy: dest = src |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1154 | SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF); |
| 1155 | return; |
| 1156 | default: |
Brian Gaeke | 34562ba | 2004-01-14 06:02:53 +0000 | [diff] [blame] | 1157 | // If it is an unknown intrinsic function, use the intrinsic lowering |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1158 | // class to transform it into hopefully tasty LLVM code. |
| 1159 | // |
| 1160 | Instruction *Prev = CS.getInstruction()->getPrev(); |
| 1161 | BasicBlock *Parent = CS.getInstruction()->getParent(); |
| 1162 | IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction())); |
| 1163 | |
| 1164 | // Restore the CurInst pointer to the first instruction newly inserted, if |
| 1165 | // any. |
| 1166 | if (!Prev) { |
| 1167 | SF.CurInst = Parent->begin(); |
| 1168 | } else { |
| 1169 | SF.CurInst = Prev; |
| 1170 | ++SF.CurInst; |
| 1171 | } |
Brian Gaeke | b440dea | 2004-04-23 18:05:28 +0000 | [diff] [blame] | 1172 | return; |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Brian Gaeke | fea483d | 2003-11-07 20:04:22 +0000 | [diff] [blame] | 1175 | SF.Caller = CS; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 1176 | std::vector<GenericValue> ArgVals; |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 1177 | const unsigned NumArgs = SF.Caller.arg_size(); |
| 1178 | ArgVals.reserve(NumArgs); |
| 1179 | for (CallSite::arg_iterator i = SF.Caller.arg_begin(), |
| 1180 | e = SF.Caller.arg_end(); i != e; ++i) { |
| 1181 | Value *V = *i; |
| 1182 | ArgVals.push_back(getOperandValue(V, SF)); |
Chris Lattner | 9378013 | 2003-01-13 00:58:52 +0000 | [diff] [blame] | 1183 | // Promote all integral types whose size is < sizeof(int) into ints. We do |
| 1184 | // this by zero or sign extending the value as appropriate according to the |
| 1185 | // source type. |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 1186 | const Type *Ty = V->getType(); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1187 | if (Ty->isInteger()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1188 | if (Ty->getPrimitiveSizeInBits() == 1) |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 1189 | ArgVals.back().Int32Val = ArgVals.back().Int1Val; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1190 | else if (Ty->getPrimitiveSizeInBits() <= 8) |
| 1191 | ArgVals.back().Int32Val = ArgVals.back().Int8Val; |
| 1192 | else if (Ty->getPrimitiveSizeInBits() <= 16) |
| 1193 | ArgVals.back().Int32Val = ArgVals.back().Int16Val; |
Chris Lattner | 9378013 | 2003-01-13 00:58:52 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1196 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1197 | // 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] | 1198 | // and treat it as a function pointer. |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1199 | GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 1200 | callFunction((Function*)GVTOP(SRC), ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1203 | static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2, |
| 1204 | const Type *Ty) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1205 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1206 | if (const IntegerType *ITy = cast<IntegerType>(Ty)) { |
| 1207 | unsigned BitWidth = ITy->getBitWidth(); |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1208 | if (BitWidth <= 8) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1209 | Dest.Int8Val = ((uint8_t)Src1.Int8Val) << ((uint32_t)Src2.Int8Val); |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1210 | else if (BitWidth <= 16) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1211 | Dest.Int16Val = ((uint16_t)Src1.Int16Val) << ((uint32_t)Src2.Int8Val); |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1212 | else if (BitWidth <= 32) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1213 | Dest.Int32Val = ((uint32_t)Src1.Int32Val) << ((uint32_t)Src2.Int8Val); |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1214 | else if (BitWidth <= 64) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1215 | Dest.Int64Val = ((uint64_t)Src1.Int64Val) << ((uint32_t)Src2.Int8Val); |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1216 | else { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1217 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; |
| 1218 | abort(); |
| 1219 | } |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1220 | maskToBitWidth(Dest, BitWidth); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1221 | } else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1222 | cerr << "Unhandled type for Shl instruction: " << *Ty << "\n"; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1223 | abort(); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1224 | } |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1225 | return Dest; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1228 | static GenericValue executeLShrInst(GenericValue Src1, GenericValue Src2, |
| 1229 | const Type *Ty) { |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1230 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1231 | if (const IntegerType *ITy = cast<IntegerType>(Ty)) { |
| 1232 | unsigned BitWidth = ITy->getBitWidth(); |
| 1233 | if (BitWidth <= 8) |
| 1234 | Dest.Int8Val = ((uint8_t)Src1.Int8Val) >> ((uint32_t)Src2.Int8Val); |
| 1235 | else if (BitWidth <= 16) |
| 1236 | Dest.Int16Val = ((uint16_t)Src1.Int16Val) >> ((uint32_t)Src2.Int8Val); |
| 1237 | else if (BitWidth <= 32) |
| 1238 | Dest.Int32Val = ((uint32_t)Src1.Int32Val) >> ((uint32_t)Src2.Int8Val); |
| 1239 | else if (BitWidth <= 64) |
| 1240 | Dest.Int64Val = ((uint64_t)Src1.Int64Val) >> ((uint32_t)Src2.Int8Val); |
| 1241 | else { |
| 1242 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; |
| 1243 | abort(); |
| 1244 | } |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1245 | maskToBitWidth(Dest, BitWidth); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1246 | } else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1247 | cerr << "Unhandled type for LShr instruction: " << *Ty << "\n"; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1248 | abort(); |
| 1249 | } |
| 1250 | return Dest; |
| 1251 | } |
| 1252 | |
| 1253 | static GenericValue executeAShrInst(GenericValue Src1, GenericValue Src2, |
| 1254 | const Type *Ty) { |
| 1255 | GenericValue Dest; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1256 | if (const IntegerType *ITy = cast<IntegerType>(Ty)) { |
| 1257 | unsigned BitWidth = ITy->getBitWidth(); |
| 1258 | if (BitWidth <= 8) |
| 1259 | Dest.Int8Val = ((int8_t)Src1.Int8Val) >> ((int32_t)Src2.Int8Val); |
| 1260 | else if (BitWidth <= 16) |
| 1261 | Dest.Int16Val = ((int16_t)Src1.Int16Val) >> ((int32_t)Src2.Int8Val); |
| 1262 | else if (BitWidth <= 32) |
| 1263 | Dest.Int32Val = ((int32_t)Src1.Int32Val) >> ((int32_t)Src2.Int8Val); |
| 1264 | else if (BitWidth <= 64) |
| 1265 | Dest.Int64Val = ((int64_t)Src1.Int64Val) >> ((int32_t)Src2.Int8Val); |
| 1266 | else { |
| 1267 | cerr << "Integer types > 64 bits not supported: " << *Ty << "\n"; \ |
| 1268 | abort(); |
| 1269 | } |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1270 | maskToBitWidth(Dest, BitWidth); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1271 | } else { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1272 | cerr << "Unhandled type for AShr instruction: " << *Ty << "\n"; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 1273 | abort(); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1274 | } |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1275 | return Dest; |
| 1276 | } |
| 1277 | |
| 1278 | void Interpreter::visitShl(ShiftInst &I) { |
| 1279 | ExecutionContext &SF = ECStack.back(); |
| 1280 | const Type *Ty = I.getOperand(0)->getType(); |
| 1281 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1282 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1283 | GenericValue Dest; |
| 1284 | Dest = executeShlInst (Src1, Src2, Ty); |
| 1285 | SetValue(&I, Dest, SF); |
| 1286 | } |
| 1287 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1288 | void Interpreter::visitLShr(ShiftInst &I) { |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1289 | ExecutionContext &SF = ECStack.back(); |
| 1290 | const Type *Ty = I.getOperand(0)->getType(); |
| 1291 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1292 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1293 | GenericValue Dest; |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1294 | Dest = executeLShrInst (Src1, Src2, Ty); |
| 1295 | SetValue(&I, Dest, SF); |
| 1296 | } |
| 1297 | |
| 1298 | void Interpreter::visitAShr(ShiftInst &I) { |
| 1299 | ExecutionContext &SF = ECStack.back(); |
| 1300 | const Type *Ty = I.getOperand(0)->getType(); |
| 1301 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1302 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1303 | GenericValue Dest; |
| 1304 | Dest = executeAShrInst (Src1, Src2, Ty); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1305 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Reid Spencer | 23e2883 | 2007-01-18 01:25:42 +0000 | [diff] [blame] | 1308 | #define INTEGER_ASSIGN(DEST, BITWIDTH, VAL) \ |
| 1309 | { \ |
| 1310 | uint64_t Mask = (1ull << BITWIDTH) - 1; \ |
| 1311 | if (BITWIDTH == 1) { \ |
| 1312 | Dest.Int1Val = (bool) (VAL & Mask); \ |
| 1313 | } else if (BITWIDTH <= 8) { \ |
| 1314 | Dest.Int8Val = (uint8_t) (VAL & Mask); \ |
| 1315 | } else if (BITWIDTH <= 16) { \ |
| 1316 | Dest.Int16Val = (uint16_t) (VAL & Mask); \ |
| 1317 | } else if (BITWIDTH <= 32) { \ |
| 1318 | Dest.Int32Val = (uint32_t) (VAL & Mask); \ |
| 1319 | } else \ |
| 1320 | Dest.Int64Val = (uint64_t) (VAL & Mask); \ |
| 1321 | } |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1322 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1323 | GenericValue Interpreter::executeTruncInst(Value *SrcVal, const Type *DstTy, |
| 1324 | ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1325 | const Type *SrcTy = SrcVal->getType(); |
| 1326 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1327 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1328 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1329 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1330 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1331 | assert(SBitWidth <= 64 && DBitWidth <= 64 && |
| 1332 | "Integer types > 64 bits not supported"); |
| 1333 | assert(SBitWidth > DBitWidth && "Invalid truncate"); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1334 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1335 | // Mask the source value to its actual bit width. This ensures that any |
| 1336 | // high order bits are cleared. |
| 1337 | uint64_t Mask = (1ULL << DBitWidth) - 1; |
| 1338 | uint64_t MaskedVal = 0; |
| 1339 | if (SBitWidth <= 8) |
| 1340 | MaskedVal = Src.Int8Val & Mask; |
| 1341 | else if (SBitWidth <= 16) |
| 1342 | MaskedVal = Src.Int16Val & Mask; |
| 1343 | else if (SBitWidth <= 32) |
| 1344 | MaskedVal = Src.Int32Val & Mask; |
| 1345 | else |
| 1346 | MaskedVal = Src.Int64Val & Mask; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1347 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1348 | INTEGER_ASSIGN(Dest, DBitWidth, MaskedVal); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1349 | return Dest; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1350 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1351 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1352 | GenericValue Interpreter::executeSExtInst(Value *SrcVal, const Type *DstTy, |
| 1353 | ExecutionContext &SF) { |
| 1354 | const Type *SrcTy = SrcVal->getType(); |
| 1355 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1356 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1357 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1358 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1359 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1360 | assert(SBitWidth <= 64 && DBitWidth <= 64 && |
| 1361 | "Integer types > 64 bits not supported"); |
| 1362 | assert(SBitWidth < DBitWidth && "Invalid sign extend"); |
Reid Spencer | c00a430 | 2007-01-20 08:32:52 +0000 | [diff] [blame] | 1363 | |
| 1364 | // Normalize to a 64-bit value. |
| 1365 | uint64_t Normalized = 0; |
| 1366 | if (SBitWidth <= 8) |
| 1367 | Normalized = Src.Int8Val; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1368 | else if (SBitWidth <= 16) |
Reid Spencer | c00a430 | 2007-01-20 08:32:52 +0000 | [diff] [blame] | 1369 | Normalized = Src.Int16Val; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1370 | else if (SBitWidth <= 32) |
Reid Spencer | c00a430 | 2007-01-20 08:32:52 +0000 | [diff] [blame] | 1371 | Normalized = Src.Int32Val; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1372 | else |
Reid Spencer | c00a430 | 2007-01-20 08:32:52 +0000 | [diff] [blame] | 1373 | Normalized = Src.Int64Val; |
| 1374 | |
Reid Spencer | a42c7fd | 2007-01-20 20:12:29 +0000 | [diff] [blame] | 1375 | Normalized = doSignExtension(Normalized, SITy); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1376 | |
| 1377 | // Now that we have a sign extended value, assign it to the destination |
Reid Spencer | c00a430 | 2007-01-20 08:32:52 +0000 | [diff] [blame] | 1378 | INTEGER_ASSIGN(Dest, DBitWidth, Normalized); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1379 | return Dest; |
| 1380 | } |
| 1381 | |
| 1382 | GenericValue Interpreter::executeZExtInst(Value *SrcVal, const Type *DstTy, |
| 1383 | ExecutionContext &SF) { |
| 1384 | const Type *SrcTy = SrcVal->getType(); |
| 1385 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1386 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1387 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1388 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1389 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1390 | assert(SBitWidth <= 64 && DBitWidth <= 64 && |
| 1391 | "Integer types > 64 bits not supported"); |
| 1392 | assert(SBitWidth < DBitWidth && "Invalid sign extend"); |
| 1393 | uint64_t Extended = 0; |
| 1394 | if (SBitWidth == 1) |
| 1395 | // For sign extension from bool, we must extend the source bits. |
| 1396 | Extended = (uint64_t) (Src.Int1Val & 1); |
| 1397 | else if (SBitWidth <= 8) |
| 1398 | Extended = (uint64_t) (uint8_t)Src.Int8Val; |
| 1399 | else if (SBitWidth <= 16) |
| 1400 | Extended = (uint64_t) (uint16_t)Src.Int16Val; |
| 1401 | else if (SBitWidth <= 32) |
| 1402 | Extended = (uint64_t) (uint32_t)Src.Int32Val; |
| 1403 | else |
| 1404 | Extended = (uint64_t) Src.Int64Val; |
| 1405 | |
| 1406 | // Now that we have a sign extended value, assign it to the destination |
| 1407 | INTEGER_ASSIGN(Dest, DBitWidth, Extended); |
| 1408 | return Dest; |
| 1409 | } |
| 1410 | |
| 1411 | GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, const Type *DstTy, |
| 1412 | ExecutionContext &SF) { |
| 1413 | const Type *SrcTy = SrcVal->getType(); |
| 1414 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1415 | assert(SrcTy == Type::DoubleTy && DstTy == Type::FloatTy && |
| 1416 | "Invalid FPTrunc instruction"); |
| 1417 | Dest.FloatVal = (float) Src.DoubleVal; |
| 1418 | return Dest; |
| 1419 | } |
| 1420 | |
| 1421 | GenericValue Interpreter::executeFPExtInst(Value *SrcVal, const Type *DstTy, |
| 1422 | ExecutionContext &SF) { |
| 1423 | const Type *SrcTy = SrcVal->getType(); |
| 1424 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1425 | assert(SrcTy == Type::FloatTy && DstTy == Type::DoubleTy && |
| 1426 | "Invalid FPTrunc instruction"); |
| 1427 | Dest.DoubleVal = (double) Src.FloatVal; |
| 1428 | return Dest; |
| 1429 | } |
| 1430 | |
| 1431 | GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, const Type *DstTy, |
| 1432 | ExecutionContext &SF) { |
| 1433 | const Type *SrcTy = SrcVal->getType(); |
| 1434 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1435 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1436 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1437 | assert(DBitWidth <= 64 && "Integer types > 64 bits not supported"); |
| 1438 | assert(SrcTy->isFloatingPoint() && "Invalid FPToUI instruction"); |
| 1439 | uint64_t Converted = 0; |
| 1440 | if (SrcTy->getTypeID() == Type::FloatTyID) |
| 1441 | Converted = (uint64_t) Src.FloatVal; |
| 1442 | else |
| 1443 | Converted = (uint64_t) Src.DoubleVal; |
| 1444 | |
| 1445 | INTEGER_ASSIGN(Dest, DBitWidth, Converted); |
| 1446 | return Dest; |
| 1447 | } |
| 1448 | |
| 1449 | GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, const Type *DstTy, |
| 1450 | ExecutionContext &SF) { |
| 1451 | const Type *SrcTy = SrcVal->getType(); |
| 1452 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1453 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1454 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1455 | assert(DBitWidth <= 64 && "Integer types > 64 bits not supported"); |
| 1456 | assert(SrcTy->isFloatingPoint() && "Invalid FPToSI instruction"); |
| 1457 | int64_t Converted = 0; |
| 1458 | if (SrcTy->getTypeID() == Type::FloatTyID) |
| 1459 | Converted = (int64_t) Src.FloatVal; |
| 1460 | else |
| 1461 | Converted = (int64_t) Src.DoubleVal; |
| 1462 | |
| 1463 | INTEGER_ASSIGN(Dest, DBitWidth, Converted); |
| 1464 | return Dest; |
| 1465 | } |
| 1466 | |
| 1467 | GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, const Type *DstTy, |
| 1468 | ExecutionContext &SF) { |
| 1469 | const Type *SrcTy = SrcVal->getType(); |
| 1470 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1471 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1472 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1473 | assert(SBitWidth <= 64 && "Integer types > 64 bits not supported"); |
| 1474 | assert(DstTy->isFloatingPoint() && "Invalid UIToFP instruction"); |
| 1475 | uint64_t Converted = 0; |
| 1476 | if (SBitWidth == 1) |
| 1477 | Converted = (uint64_t) Src.Int1Val; |
| 1478 | else if (SBitWidth <= 8) |
| 1479 | Converted = (uint64_t) Src.Int8Val; |
| 1480 | else if (SBitWidth <= 16) |
| 1481 | Converted = (uint64_t) Src.Int16Val; |
| 1482 | else if (SBitWidth <= 32) |
| 1483 | Converted = (uint64_t) Src.Int32Val; |
| 1484 | else |
| 1485 | Converted = (uint64_t) Src.Int64Val; |
| 1486 | |
| 1487 | if (DstTy->getTypeID() == Type::FloatTyID) |
| 1488 | Dest.FloatVal = (float) Converted; |
| 1489 | else |
| 1490 | Dest.DoubleVal = (double) Converted; |
| 1491 | return Dest; |
| 1492 | } |
| 1493 | |
| 1494 | GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, const Type *DstTy, |
| 1495 | ExecutionContext &SF) { |
| 1496 | const Type *SrcTy = SrcVal->getType(); |
| 1497 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1498 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1499 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1500 | assert(SBitWidth <= 64 && "Integer types > 64 bits not supported"); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame^] | 1501 | assert(DstTy->isFloatingPoint() && "Invalid SIToFP instruction"); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1502 | int64_t Converted = 0; |
| 1503 | if (SBitWidth == 1) |
| 1504 | Converted = 0LL - Src.Int1Val; |
| 1505 | else if (SBitWidth <= 8) |
| 1506 | Converted = (int64_t) (int8_t)Src.Int8Val; |
| 1507 | else if (SBitWidth <= 16) |
| 1508 | Converted = (int64_t) (int16_t)Src.Int16Val; |
| 1509 | else if (SBitWidth <= 32) |
| 1510 | Converted = (int64_t) (int32_t)Src.Int32Val; |
| 1511 | else |
| 1512 | Converted = (int64_t) Src.Int64Val; |
| 1513 | |
| 1514 | if (DstTy->getTypeID() == Type::FloatTyID) |
| 1515 | Dest.FloatVal = (float) Converted; |
| 1516 | else |
| 1517 | Dest.DoubleVal = (double) Converted; |
| 1518 | return Dest; |
| 1519 | } |
| 1520 | |
| 1521 | GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, const Type *DstTy, |
| 1522 | ExecutionContext &SF) { |
| 1523 | const Type *SrcTy = SrcVal->getType(); |
| 1524 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1525 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1526 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1527 | assert(DBitWidth <= 64 && "Integer types > 64 bits not supported"); |
| 1528 | assert(isa<PointerType>(SrcTy) && "Invalid PtrToInt instruction"); |
| 1529 | INTEGER_ASSIGN(Dest, DBitWidth, (intptr_t) Src.PointerVal); |
| 1530 | return Dest; |
| 1531 | } |
| 1532 | |
| 1533 | GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, const Type *DstTy, |
| 1534 | ExecutionContext &SF) { |
| 1535 | const Type *SrcTy = SrcVal->getType(); |
| 1536 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1537 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1538 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1539 | assert(SBitWidth <= 64 && "Integer types > 64 bits not supported"); |
| 1540 | assert(isa<PointerType>(DstTy) && "Invalid PtrToInt instruction"); |
| 1541 | uint64_t Converted = 0; |
| 1542 | if (SBitWidth == 1) |
| 1543 | Converted = (uint64_t) Src.Int1Val; |
| 1544 | else if (SBitWidth <= 8) |
| 1545 | Converted = (uint64_t) Src.Int8Val; |
| 1546 | else if (SBitWidth <= 16) |
| 1547 | Converted = (uint64_t) Src.Int16Val; |
| 1548 | else if (SBitWidth <= 32) |
| 1549 | Converted = (uint64_t) Src.Int32Val; |
| 1550 | else |
| 1551 | Converted = (uint64_t) Src.Int64Val; |
| 1552 | |
| 1553 | Dest.PointerVal = (PointerTy) Converted; |
| 1554 | return Dest; |
| 1555 | } |
| 1556 | |
| 1557 | GenericValue Interpreter::executeBitCastInst(Value *SrcVal, const Type *DstTy, |
| 1558 | ExecutionContext &SF) { |
| 1559 | |
| 1560 | const Type *SrcTy = SrcVal->getType(); |
| 1561 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
| 1562 | if (isa<PointerType>(DstTy)) { |
| 1563 | assert(isa<PointerType>(SrcTy) && "Invalid BitCast"); |
| 1564 | Dest.PointerVal = Src.PointerVal; |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1565 | } else if (DstTy->isInteger()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1566 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1567 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1568 | if (SrcTy == Type::FloatTy) { |
| 1569 | Dest.Int32Val = FloatToBits(Src.FloatVal); |
| 1570 | } else if (SrcTy == Type::DoubleTy) { |
| 1571 | Dest.Int64Val = DoubleToBits(Src.DoubleVal); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1572 | } else if (SrcTy->isInteger()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1573 | const IntegerType *SITy = cast<IntegerType>(SrcTy); |
| 1574 | unsigned SBitWidth = SITy->getBitWidth(); |
| 1575 | assert(SBitWidth <= 64 && "Integer types > 64 bits not supported"); |
| 1576 | assert(SBitWidth == DBitWidth && "Invalid BitCast"); |
| 1577 | if (SBitWidth == 1) |
| 1578 | Dest.Int1Val = Src.Int1Val; |
| 1579 | else if (SBitWidth <= 8) |
| 1580 | Dest.Int8Val = Src.Int8Val; |
| 1581 | else if (SBitWidth <= 16) |
| 1582 | Dest.Int16Val = Src.Int16Val; |
| 1583 | else if (SBitWidth <= 32) |
| 1584 | Dest.Int32Val = Src.Int32Val; |
| 1585 | else |
| 1586 | Dest.Int64Val = Src.Int64Val; |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1587 | maskToBitWidth(Dest, DBitWidth); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1588 | } else |
| 1589 | assert(0 && "Invalid BitCast"); |
| 1590 | } else if (DstTy == Type::FloatTy) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1591 | if (SrcTy->isInteger()) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1592 | Dest.FloatVal = BitsToFloat(Src.Int32Val); |
| 1593 | else |
| 1594 | Dest.FloatVal = Src.FloatVal; |
| 1595 | } else if (DstTy == Type::DoubleTy) { |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1596 | if (SrcTy->isInteger()) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1597 | Dest.DoubleVal = BitsToDouble(Src.Int64Val); |
| 1598 | else |
| 1599 | Dest.DoubleVal = Src.DoubleVal; |
| 1600 | } else |
| 1601 | assert(0 && "Invalid Bitcast"); |
| 1602 | |
| 1603 | return Dest; |
| 1604 | } |
| 1605 | |
| 1606 | void Interpreter::visitTruncInst(TruncInst &I) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1607 | ExecutionContext &SF = ECStack.back(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1608 | SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF); |
| 1609 | } |
| 1610 | |
| 1611 | void Interpreter::visitSExtInst(SExtInst &I) { |
| 1612 | ExecutionContext &SF = ECStack.back(); |
| 1613 | SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF); |
| 1614 | } |
| 1615 | |
| 1616 | void Interpreter::visitZExtInst(ZExtInst &I) { |
| 1617 | ExecutionContext &SF = ECStack.back(); |
| 1618 | SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF); |
| 1619 | } |
| 1620 | |
| 1621 | void Interpreter::visitFPTruncInst(FPTruncInst &I) { |
| 1622 | ExecutionContext &SF = ECStack.back(); |
| 1623 | SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF); |
| 1624 | } |
| 1625 | |
| 1626 | void Interpreter::visitFPExtInst(FPExtInst &I) { |
| 1627 | ExecutionContext &SF = ECStack.back(); |
| 1628 | SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF); |
| 1629 | } |
| 1630 | |
| 1631 | void Interpreter::visitUIToFPInst(UIToFPInst &I) { |
| 1632 | ExecutionContext &SF = ECStack.back(); |
| 1633 | SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF); |
| 1634 | } |
| 1635 | |
| 1636 | void Interpreter::visitSIToFPInst(SIToFPInst &I) { |
| 1637 | ExecutionContext &SF = ECStack.back(); |
| 1638 | SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF); |
| 1639 | } |
| 1640 | |
| 1641 | void Interpreter::visitFPToUIInst(FPToUIInst &I) { |
| 1642 | ExecutionContext &SF = ECStack.back(); |
| 1643 | SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF); |
| 1644 | } |
| 1645 | |
| 1646 | void Interpreter::visitFPToSIInst(FPToSIInst &I) { |
| 1647 | ExecutionContext &SF = ECStack.back(); |
| 1648 | SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF); |
| 1649 | } |
| 1650 | |
| 1651 | void Interpreter::visitPtrToIntInst(PtrToIntInst &I) { |
| 1652 | ExecutionContext &SF = ECStack.back(); |
| 1653 | SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF); |
| 1654 | } |
| 1655 | |
| 1656 | void Interpreter::visitIntToPtrInst(IntToPtrInst &I) { |
| 1657 | ExecutionContext &SF = ECStack.back(); |
| 1658 | SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF); |
| 1659 | } |
| 1660 | |
| 1661 | void Interpreter::visitBitCastInst(BitCastInst &I) { |
| 1662 | ExecutionContext &SF = ECStack.back(); |
| 1663 | SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1664 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1665 | |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1666 | #define IMPLEMENT_VAARG(TY) \ |
| 1667 | case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break |
| 1668 | |
| 1669 | void Interpreter::visitVAArgInst(VAArgInst &I) { |
| 1670 | ExecutionContext &SF = ECStack.back(); |
| 1671 | |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1672 | // Get the incoming valist parameter. LLI treats the valist as a |
| 1673 | // (ec-stack-depth var-arg-index) pair. |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1674 | GenericValue VAList = getOperandValue(I.getOperand(0), SF); |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1675 | GenericValue Dest; |
| 1676 | GenericValue Src = ECStack[VAList.UIntPairVal.first] |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1677 | .VarArgs[VAList.UIntPairVal.second]; |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1678 | const Type *Ty = I.getType(); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 1679 | switch (Ty->getTypeID()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1680 | case Type::IntegerTyID: { |
| 1681 | unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth(); |
| 1682 | if (BitWidth == 1) |
| 1683 | Dest.Int1Val = Src.Int1Val; |
| 1684 | else if (BitWidth <= 8) |
| 1685 | Dest.Int8Val = Src.Int8Val; |
| 1686 | else if (BitWidth <= 16) |
| 1687 | Dest.Int16Val = Src.Int16Val; |
| 1688 | else if (BitWidth <= 32) |
| 1689 | Dest.Int32Val = Src.Int32Val; |
| 1690 | else if (BitWidth <= 64) |
| 1691 | Dest.Int64Val = Src.Int64Val; |
| 1692 | else |
| 1693 | assert("Integer types > 64 bits not supported"); |
Reid Spencer | 65367b2 | 2007-01-18 02:12:51 +0000 | [diff] [blame] | 1694 | maskToBitWidth(Dest, BitWidth); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1695 | } |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1696 | IMPLEMENT_VAARG(Pointer); |
| 1697 | IMPLEMENT_VAARG(Float); |
| 1698 | IMPLEMENT_VAARG(Double); |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1699 | default: |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 1700 | cerr << "Unhandled dest type for vaarg instruction: " << *Ty << "\n"; |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1701 | abort(); |
| 1702 | } |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1703 | |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1704 | // Set the Value of this Instruction. |
| 1705 | SetValue(&I, Dest, SF); |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1706 | |
| 1707 | // Move the pointer to the next vararg. |
| 1708 | ++VAList.UIntPairVal.second; |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1711 | //===----------------------------------------------------------------------===// |
| 1712 | // Dispatch and Execution Code |
| 1713 | //===----------------------------------------------------------------------===// |
| 1714 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1715 | //===----------------------------------------------------------------------===// |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 1716 | // callFunction - Execute the specified function... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1717 | // |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 1718 | void Interpreter::callFunction(Function *F, |
| 1719 | const std::vector<GenericValue> &ArgVals) { |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1720 | assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 || |
| 1721 | ECStack.back().Caller.arg_size() == ArgVals.size()) && |
| 1722 | "Incorrect number of arguments passed into function call!"); |
Chris Lattner | 63bd613 | 2003-09-17 17:26:22 +0000 | [diff] [blame] | 1723 | // Make a new stack frame... and fill it in. |
| 1724 | ECStack.push_back(ExecutionContext()); |
| 1725 | ExecutionContext &StackFrame = ECStack.back(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 1726 | StackFrame.CurFunction = F; |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 1727 | |
| 1728 | // Special handling for external functions. |
| 1729 | if (F->isExternal()) { |
| 1730 | GenericValue Result = callExternalFunction (F, ArgVals); |
| 1731 | // Simulate a 'ret' instruction of the appropriate type. |
| 1732 | popStackAndReturnValueToCaller (F->getReturnType (), Result); |
| 1733 | return; |
| 1734 | } |
| 1735 | |
| 1736 | // Get pointers to first LLVM BB & Instruction in function. |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 1737 | StackFrame.CurBB = F->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1738 | StackFrame.CurInst = StackFrame.CurBB->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1739 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1740 | // Run through the function arguments and initialize their values... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1741 | assert((ArgVals.size() == F->arg_size() || |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1742 | (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&& |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1743 | "Invalid number of values passed to function invocation!"); |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 1744 | |
| 1745 | // Handle non-varargs arguments... |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1746 | unsigned i = 0; |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 1747 | 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] | 1748 | SetValue(AI, ArgVals[i], StackFrame); |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 1749 | |
| 1750 | // Handle varargs arguments... |
| 1751 | StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end()); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1754 | void Interpreter::run() { |
Brian Gaeke | 9ad671d | 2003-09-04 23:15:40 +0000 | [diff] [blame] | 1755 | while (!ECStack.empty()) { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 1756 | // Interpret a single instruction & increment the "PC". |
| 1757 | ExecutionContext &SF = ECStack.back(); // Current stack frame |
| 1758 | Instruction &I = *SF.CurInst++; // Increment before execute |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1759 | |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 1760 | // Track the number of dynamic instructions executed. |
| 1761 | ++NumDynamicInsts; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1762 | |
Bill Wendling | 480f093 | 2006-11-27 23:54:50 +0000 | [diff] [blame] | 1763 | DOUT << "About to interpret: " << I; |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 1764 | visit(I); // Dispatch to one of the visit* methods... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1765 | } |
| 1766 | } |