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