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