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