LLVM API Documentation

Execution.cpp
Go to the documentation of this file.
00001 //===-- Execution.cpp - Implement code to simulate the program ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file contains the actual instruction interpreter.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "interpreter"
00015 #include "Interpreter.h"
00016 #include "llvm/ADT/APInt.h"
00017 #include "llvm/ADT/Statistic.h"
00018 #include "llvm/CodeGen/IntrinsicLowering.h"
00019 #include "llvm/IR/Constants.h"
00020 #include "llvm/IR/DerivedTypes.h"
00021 #include "llvm/IR/GetElementPtrTypeIterator.h"
00022 #include "llvm/IR/Instructions.h"
00023 #include "llvm/Support/CommandLine.h"
00024 #include "llvm/Support/Debug.h"
00025 #include "llvm/Support/ErrorHandling.h"
00026 #include "llvm/Support/MathExtras.h"
00027 #include <algorithm>
00028 #include <cmath>
00029 using namespace llvm;
00030 
00031 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
00032 
00033 static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden,
00034           cl::desc("make the interpreter print every volatile load and store"));
00035 
00036 //===----------------------------------------------------------------------===//
00037 //                     Various Helper Functions
00038 //===----------------------------------------------------------------------===//
00039 
00040 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
00041   SF.Values[V] = Val;
00042 }
00043 
00044 //===----------------------------------------------------------------------===//
00045 //                    Binary Instruction Implementations
00046 //===----------------------------------------------------------------------===//
00047 
00048 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
00049    case Type::TY##TyID: \
00050      Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
00051      break
00052 
00053 static void executeFAddInst(GenericValue &Dest, GenericValue Src1,
00054                             GenericValue Src2, Type *Ty) {
00055   switch (Ty->getTypeID()) {
00056     IMPLEMENT_BINARY_OPERATOR(+, Float);
00057     IMPLEMENT_BINARY_OPERATOR(+, Double);
00058   default:
00059     dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n";
00060     llvm_unreachable(0);
00061   }
00062 }
00063 
00064 static void executeFSubInst(GenericValue &Dest, GenericValue Src1,
00065                             GenericValue Src2, Type *Ty) {
00066   switch (Ty->getTypeID()) {
00067     IMPLEMENT_BINARY_OPERATOR(-, Float);
00068     IMPLEMENT_BINARY_OPERATOR(-, Double);
00069   default:
00070     dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n";
00071     llvm_unreachable(0);
00072   }
00073 }
00074 
00075 static void executeFMulInst(GenericValue &Dest, GenericValue Src1,
00076                             GenericValue Src2, Type *Ty) {
00077   switch (Ty->getTypeID()) {
00078     IMPLEMENT_BINARY_OPERATOR(*, Float);
00079     IMPLEMENT_BINARY_OPERATOR(*, Double);
00080   default:
00081     dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n";
00082     llvm_unreachable(0);
00083   }
00084 }
00085 
00086 static void executeFDivInst(GenericValue &Dest, GenericValue Src1, 
00087                             GenericValue Src2, Type *Ty) {
00088   switch (Ty->getTypeID()) {
00089     IMPLEMENT_BINARY_OPERATOR(/, Float);
00090     IMPLEMENT_BINARY_OPERATOR(/, Double);
00091   default:
00092     dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n";
00093     llvm_unreachable(0);
00094   }
00095 }
00096 
00097 static void executeFRemInst(GenericValue &Dest, GenericValue Src1, 
00098                             GenericValue Src2, Type *Ty) {
00099   switch (Ty->getTypeID()) {
00100   case Type::FloatTyID:
00101     Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
00102     break;
00103   case Type::DoubleTyID:
00104     Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
00105     break;
00106   default:
00107     dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
00108     llvm_unreachable(0);
00109   }
00110 }
00111 
00112 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
00113    case Type::IntegerTyID:  \
00114       Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
00115       break;
00116 
00117 #define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY)                        \
00118   case Type::VectorTyID: {                                           \
00119     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());    \
00120     Dest.AggregateVal.resize( Src1.AggregateVal.size() );            \
00121     for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++)             \
00122       Dest.AggregateVal[_i].IntVal = APInt(1,                        \
00123       Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\
00124   } break;
00125 
00126 // Handle pointers specially because they must be compared with only as much
00127 // width as the host has.  We _do not_ want to be comparing 64 bit values when
00128 // running on a 32-bit target, otherwise the upper 32 bits might mess up
00129 // comparisons if they contain garbage.
00130 #define IMPLEMENT_POINTER_ICMP(OP) \
00131    case Type::PointerTyID: \
00132       Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
00133                             (void*)(intptr_t)Src2.PointerVal); \
00134       break;
00135 
00136 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
00137                                    Type *Ty) {
00138   GenericValue Dest;
00139   switch (Ty->getTypeID()) {
00140     IMPLEMENT_INTEGER_ICMP(eq,Ty);
00141     IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty);
00142     IMPLEMENT_POINTER_ICMP(==);
00143   default:
00144     dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
00145     llvm_unreachable(0);
00146   }
00147   return Dest;
00148 }
00149 
00150 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
00151                                    Type *Ty) {
00152   GenericValue Dest;
00153   switch (Ty->getTypeID()) {
00154     IMPLEMENT_INTEGER_ICMP(ne,Ty);
00155     IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty);
00156     IMPLEMENT_POINTER_ICMP(!=);
00157   default:
00158     dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
00159     llvm_unreachable(0);
00160   }
00161   return Dest;
00162 }
00163 
00164 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
00165                                     Type *Ty) {
00166   GenericValue Dest;
00167   switch (Ty->getTypeID()) {
00168     IMPLEMENT_INTEGER_ICMP(ult,Ty);
00169     IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty);
00170     IMPLEMENT_POINTER_ICMP(<);
00171   default:
00172     dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
00173     llvm_unreachable(0);
00174   }
00175   return Dest;
00176 }
00177 
00178 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
00179                                     Type *Ty) {
00180   GenericValue Dest;
00181   switch (Ty->getTypeID()) {
00182     IMPLEMENT_INTEGER_ICMP(slt,Ty);
00183     IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty);
00184     IMPLEMENT_POINTER_ICMP(<);
00185   default:
00186     dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
00187     llvm_unreachable(0);
00188   }
00189   return Dest;
00190 }
00191 
00192 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
00193                                     Type *Ty) {
00194   GenericValue Dest;
00195   switch (Ty->getTypeID()) {
00196     IMPLEMENT_INTEGER_ICMP(ugt,Ty);
00197     IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty);
00198     IMPLEMENT_POINTER_ICMP(>);
00199   default:
00200     dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
00201     llvm_unreachable(0);
00202   }
00203   return Dest;
00204 }
00205 
00206 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
00207                                     Type *Ty) {
00208   GenericValue Dest;
00209   switch (Ty->getTypeID()) {
00210     IMPLEMENT_INTEGER_ICMP(sgt,Ty);
00211     IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty);
00212     IMPLEMENT_POINTER_ICMP(>);
00213   default:
00214     dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
00215     llvm_unreachable(0);
00216   }
00217   return Dest;
00218 }
00219 
00220 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
00221                                     Type *Ty) {
00222   GenericValue Dest;
00223   switch (Ty->getTypeID()) {
00224     IMPLEMENT_INTEGER_ICMP(ule,Ty);
00225     IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty);
00226     IMPLEMENT_POINTER_ICMP(<=);
00227   default:
00228     dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
00229     llvm_unreachable(0);
00230   }
00231   return Dest;
00232 }
00233 
00234 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
00235                                     Type *Ty) {
00236   GenericValue Dest;
00237   switch (Ty->getTypeID()) {
00238     IMPLEMENT_INTEGER_ICMP(sle,Ty);
00239     IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty);
00240     IMPLEMENT_POINTER_ICMP(<=);
00241   default:
00242     dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
00243     llvm_unreachable(0);
00244   }
00245   return Dest;
00246 }
00247 
00248 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
00249                                     Type *Ty) {
00250   GenericValue Dest;
00251   switch (Ty->getTypeID()) {
00252     IMPLEMENT_INTEGER_ICMP(uge,Ty);
00253     IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty);
00254     IMPLEMENT_POINTER_ICMP(>=);
00255   default:
00256     dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
00257     llvm_unreachable(0);
00258   }
00259   return Dest;
00260 }
00261 
00262 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
00263                                     Type *Ty) {
00264   GenericValue Dest;
00265   switch (Ty->getTypeID()) {
00266     IMPLEMENT_INTEGER_ICMP(sge,Ty);
00267     IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty);
00268     IMPLEMENT_POINTER_ICMP(>=);
00269   default:
00270     dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
00271     llvm_unreachable(0);
00272   }
00273   return Dest;
00274 }
00275 
00276 void Interpreter::visitICmpInst(ICmpInst &I) {
00277   ExecutionContext &SF = ECStack.back();
00278   Type *Ty    = I.getOperand(0)->getType();
00279   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00280   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00281   GenericValue R;   // Result
00282   
00283   switch (I.getPredicate()) {
00284   case ICmpInst::ICMP_EQ:  R = executeICMP_EQ(Src1,  Src2, Ty); break;
00285   case ICmpInst::ICMP_NE:  R = executeICMP_NE(Src1,  Src2, Ty); break;
00286   case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
00287   case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
00288   case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
00289   case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
00290   case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
00291   case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
00292   case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
00293   case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
00294   default:
00295     dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I;
00296     llvm_unreachable(0);
00297   }
00298  
00299   SetValue(&I, R, SF);
00300 }
00301 
00302 #define IMPLEMENT_FCMP(OP, TY) \
00303    case Type::TY##TyID: \
00304      Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
00305      break
00306 
00307 #define IMPLEMENT_VECTOR_FCMP_T(OP, TY)                             \
00308   assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());     \
00309   Dest.AggregateVal.resize( Src1.AggregateVal.size() );             \
00310   for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++)              \
00311     Dest.AggregateVal[_i].IntVal = APInt(1,                         \
00312     Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\
00313   break;
00314 
00315 #define IMPLEMENT_VECTOR_FCMP(OP)                                   \
00316   case Type::VectorTyID:                                            \
00317     if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {   \
00318       IMPLEMENT_VECTOR_FCMP_T(OP, Float);                           \
00319     } else {                                                        \
00320         IMPLEMENT_VECTOR_FCMP_T(OP, Double);                        \
00321     }
00322 
00323 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
00324                                    Type *Ty) {
00325   GenericValue Dest;
00326   switch (Ty->getTypeID()) {
00327     IMPLEMENT_FCMP(==, Float);
00328     IMPLEMENT_FCMP(==, Double);
00329     IMPLEMENT_VECTOR_FCMP(==);
00330   default:
00331     dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
00332     llvm_unreachable(0);
00333   }
00334   return Dest;
00335 }
00336 
00337 #define IMPLEMENT_SCALAR_NANS(TY, X,Y)                                      \
00338   if (TY->isFloatTy()) {                                                    \
00339     if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) {             \
00340       Dest.IntVal = APInt(1,false);                                         \
00341       return Dest;                                                          \
00342     }                                                                       \
00343   } else {                                                                  \
00344     if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) {         \
00345       Dest.IntVal = APInt(1,false);                                         \
00346       return Dest;                                                          \
00347     }                                                                       \
00348   }
00349 
00350 #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG)                                   \
00351   assert(X.AggregateVal.size() == Y.AggregateVal.size());                   \
00352   Dest.AggregateVal.resize( X.AggregateVal.size() );                        \
00353   for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) {                       \
00354     if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val ||         \
00355         Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val)           \
00356       Dest.AggregateVal[_i].IntVal = APInt(1,FLAG);                         \
00357     else  {                                                                 \
00358       Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG);                        \
00359     }                                                                       \
00360   }
00361 
00362 #define MASK_VECTOR_NANS(TY, X,Y, FLAG)                                     \
00363   if (TY->isVectorTy()) {                                                   \
00364     if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) {          \
00365       MASK_VECTOR_NANS_T(X, Y, Float, FLAG)                                 \
00366     } else {                                                                \
00367       MASK_VECTOR_NANS_T(X, Y, Double, FLAG)                                \
00368     }                                                                       \
00369   }                                                                         \
00370 
00371 
00372 
00373 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
00374                                     Type *Ty)
00375 {
00376   GenericValue Dest;
00377   // if input is scalar value and Src1 or Src2 is NaN return false
00378   IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2)
00379   // if vector input detect NaNs and fill mask
00380   MASK_VECTOR_NANS(Ty, Src1, Src2, false)
00381   GenericValue DestMask = Dest;
00382   switch (Ty->getTypeID()) {
00383     IMPLEMENT_FCMP(!=, Float);
00384     IMPLEMENT_FCMP(!=, Double);
00385     IMPLEMENT_VECTOR_FCMP(!=);
00386     default:
00387       dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
00388       llvm_unreachable(0);
00389   }
00390   // in vector case mask out NaN elements
00391   if (Ty->isVectorTy())
00392     for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
00393       if (DestMask.AggregateVal[_i].IntVal == false)
00394         Dest.AggregateVal[_i].IntVal = APInt(1,false);
00395 
00396   return Dest;
00397 }
00398 
00399 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
00400                                    Type *Ty) {
00401   GenericValue Dest;
00402   switch (Ty->getTypeID()) {
00403     IMPLEMENT_FCMP(<=, Float);
00404     IMPLEMENT_FCMP(<=, Double);
00405     IMPLEMENT_VECTOR_FCMP(<=);
00406   default:
00407     dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
00408     llvm_unreachable(0);
00409   }
00410   return Dest;
00411 }
00412 
00413 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
00414                                    Type *Ty) {
00415   GenericValue Dest;
00416   switch (Ty->getTypeID()) {
00417     IMPLEMENT_FCMP(>=, Float);
00418     IMPLEMENT_FCMP(>=, Double);
00419     IMPLEMENT_VECTOR_FCMP(>=);
00420   default:
00421     dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
00422     llvm_unreachable(0);
00423   }
00424   return Dest;
00425 }
00426 
00427 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
00428                                    Type *Ty) {
00429   GenericValue Dest;
00430   switch (Ty->getTypeID()) {
00431     IMPLEMENT_FCMP(<, Float);
00432     IMPLEMENT_FCMP(<, Double);
00433     IMPLEMENT_VECTOR_FCMP(<);
00434   default:
00435     dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
00436     llvm_unreachable(0);
00437   }
00438   return Dest;
00439 }
00440 
00441 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
00442                                      Type *Ty) {
00443   GenericValue Dest;
00444   switch (Ty->getTypeID()) {
00445     IMPLEMENT_FCMP(>, Float);
00446     IMPLEMENT_FCMP(>, Double);
00447     IMPLEMENT_VECTOR_FCMP(>);
00448   default:
00449     dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
00450     llvm_unreachable(0);
00451   }
00452   return Dest;
00453 }
00454 
00455 #define IMPLEMENT_UNORDERED(TY, X,Y)                                     \
00456   if (TY->isFloatTy()) {                                                 \
00457     if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) {          \
00458       Dest.IntVal = APInt(1,true);                                       \
00459       return Dest;                                                       \
00460     }                                                                    \
00461   } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
00462     Dest.IntVal = APInt(1,true);                                         \
00463     return Dest;                                                         \
00464   }
00465 
00466 #define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC)                       \
00467   if (TY->isVectorTy()) {                                                \
00468     GenericValue DestMask = Dest;                                        \
00469     Dest = _FUNC(Src1, Src2, Ty);                                        \
00470       for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)               \
00471         if (DestMask.AggregateVal[_i].IntVal == true)                    \
00472           Dest.AggregateVal[_i].IntVal = APInt(1,true);                  \
00473       return Dest;                                                       \
00474   }
00475 
00476 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
00477                                    Type *Ty) {
00478   GenericValue Dest;
00479   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00480   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
00481   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ)
00482   return executeFCMP_OEQ(Src1, Src2, Ty);
00483 
00484 }
00485 
00486 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
00487                                    Type *Ty) {
00488   GenericValue Dest;
00489   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00490   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
00491   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE)
00492   return executeFCMP_ONE(Src1, Src2, Ty);
00493 }
00494 
00495 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
00496                                    Type *Ty) {
00497   GenericValue Dest;
00498   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00499   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
00500   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE)
00501   return executeFCMP_OLE(Src1, Src2, Ty);
00502 }
00503 
00504 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
00505                                    Type *Ty) {
00506   GenericValue Dest;
00507   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00508   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
00509   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE)
00510   return executeFCMP_OGE(Src1, Src2, Ty);
00511 }
00512 
00513 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
00514                                    Type *Ty) {
00515   GenericValue Dest;
00516   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00517   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
00518   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT)
00519   return executeFCMP_OLT(Src1, Src2, Ty);
00520 }
00521 
00522 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
00523                                      Type *Ty) {
00524   GenericValue Dest;
00525   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00526   MASK_VECTOR_NANS(Ty, Src1, Src2, true)
00527   IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT)
00528   return executeFCMP_OGT(Src1, Src2, Ty);
00529 }
00530 
00531 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
00532                                      Type *Ty) {
00533   GenericValue Dest;
00534   if(Ty->isVectorTy()) {
00535     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
00536     Dest.AggregateVal.resize( Src1.AggregateVal.size() );
00537     if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
00538       for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
00539         Dest.AggregateVal[_i].IntVal = APInt(1,
00540         ( (Src1.AggregateVal[_i].FloatVal ==
00541         Src1.AggregateVal[_i].FloatVal) &&
00542         (Src2.AggregateVal[_i].FloatVal ==
00543         Src2.AggregateVal[_i].FloatVal)));
00544     } else {
00545       for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
00546         Dest.AggregateVal[_i].IntVal = APInt(1,
00547         ( (Src1.AggregateVal[_i].DoubleVal ==
00548         Src1.AggregateVal[_i].DoubleVal) &&
00549         (Src2.AggregateVal[_i].DoubleVal ==
00550         Src2.AggregateVal[_i].DoubleVal)));
00551     }
00552   } else if (Ty->isFloatTy())
00553     Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal && 
00554                            Src2.FloatVal == Src2.FloatVal));
00555   else {
00556     Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal && 
00557                            Src2.DoubleVal == Src2.DoubleVal));
00558   }
00559   return Dest;
00560 }
00561 
00562 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
00563                                      Type *Ty) {
00564   GenericValue Dest;
00565   if(Ty->isVectorTy()) {
00566     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
00567     Dest.AggregateVal.resize( Src1.AggregateVal.size() );
00568     if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) {
00569       for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
00570         Dest.AggregateVal[_i].IntVal = APInt(1,
00571         ( (Src1.AggregateVal[_i].FloatVal !=
00572            Src1.AggregateVal[_i].FloatVal) ||
00573           (Src2.AggregateVal[_i].FloatVal !=
00574            Src2.AggregateVal[_i].FloatVal)));
00575       } else {
00576         for( size_t _i=0;_i<Src1.AggregateVal.size();_i++)
00577           Dest.AggregateVal[_i].IntVal = APInt(1,
00578           ( (Src1.AggregateVal[_i].DoubleVal !=
00579              Src1.AggregateVal[_i].DoubleVal) ||
00580             (Src2.AggregateVal[_i].DoubleVal !=
00581              Src2.AggregateVal[_i].DoubleVal)));
00582       }
00583   } else if (Ty->isFloatTy())
00584     Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal || 
00585                            Src2.FloatVal != Src2.FloatVal));
00586   else {
00587     Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal || 
00588                            Src2.DoubleVal != Src2.DoubleVal));
00589   }
00590   return Dest;
00591 }
00592 
00593 static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2,
00594                                     const Type *Ty, const bool val) {
00595   GenericValue Dest;
00596     if(Ty->isVectorTy()) {
00597       assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
00598       Dest.AggregateVal.resize( Src1.AggregateVal.size() );
00599       for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++)
00600         Dest.AggregateVal[_i].IntVal = APInt(1,val);
00601     } else {
00602       Dest.IntVal = APInt(1, val);
00603     }
00604 
00605     return Dest;
00606 }
00607 
00608 void Interpreter::visitFCmpInst(FCmpInst &I) {
00609   ExecutionContext &SF = ECStack.back();
00610   Type *Ty    = I.getOperand(0)->getType();
00611   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00612   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00613   GenericValue R;   // Result
00614   
00615   switch (I.getPredicate()) {
00616   default:
00617     dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I;
00618     llvm_unreachable(0);
00619   break;
00620   case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false); 
00621   break;
00622   case FCmpInst::FCMP_TRUE:  R = executeFCMP_BOOL(Src1, Src2, Ty, true); 
00623   break;
00624   case FCmpInst::FCMP_ORD:   R = executeFCMP_ORD(Src1, Src2, Ty); break;
00625   case FCmpInst::FCMP_UNO:   R = executeFCMP_UNO(Src1, Src2, Ty); break;
00626   case FCmpInst::FCMP_UEQ:   R = executeFCMP_UEQ(Src1, Src2, Ty); break;
00627   case FCmpInst::FCMP_OEQ:   R = executeFCMP_OEQ(Src1, Src2, Ty); break;
00628   case FCmpInst::FCMP_UNE:   R = executeFCMP_UNE(Src1, Src2, Ty); break;
00629   case FCmpInst::FCMP_ONE:   R = executeFCMP_ONE(Src1, Src2, Ty); break;
00630   case FCmpInst::FCMP_ULT:   R = executeFCMP_ULT(Src1, Src2, Ty); break;
00631   case FCmpInst::FCMP_OLT:   R = executeFCMP_OLT(Src1, Src2, Ty); break;
00632   case FCmpInst::FCMP_UGT:   R = executeFCMP_UGT(Src1, Src2, Ty); break;
00633   case FCmpInst::FCMP_OGT:   R = executeFCMP_OGT(Src1, Src2, Ty); break;
00634   case FCmpInst::FCMP_ULE:   R = executeFCMP_ULE(Src1, Src2, Ty); break;
00635   case FCmpInst::FCMP_OLE:   R = executeFCMP_OLE(Src1, Src2, Ty); break;
00636   case FCmpInst::FCMP_UGE:   R = executeFCMP_UGE(Src1, Src2, Ty); break;
00637   case FCmpInst::FCMP_OGE:   R = executeFCMP_OGE(Src1, Src2, Ty); break;
00638   }
00639  
00640   SetValue(&I, R, SF);
00641 }
00642 
00643 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, 
00644                                    GenericValue Src2, Type *Ty) {
00645   GenericValue Result;
00646   switch (predicate) {
00647   case ICmpInst::ICMP_EQ:    return executeICMP_EQ(Src1, Src2, Ty);
00648   case ICmpInst::ICMP_NE:    return executeICMP_NE(Src1, Src2, Ty);
00649   case ICmpInst::ICMP_UGT:   return executeICMP_UGT(Src1, Src2, Ty);
00650   case ICmpInst::ICMP_SGT:   return executeICMP_SGT(Src1, Src2, Ty);
00651   case ICmpInst::ICMP_ULT:   return executeICMP_ULT(Src1, Src2, Ty);
00652   case ICmpInst::ICMP_SLT:   return executeICMP_SLT(Src1, Src2, Ty);
00653   case ICmpInst::ICMP_UGE:   return executeICMP_UGE(Src1, Src2, Ty);
00654   case ICmpInst::ICMP_SGE:   return executeICMP_SGE(Src1, Src2, Ty);
00655   case ICmpInst::ICMP_ULE:   return executeICMP_ULE(Src1, Src2, Ty);
00656   case ICmpInst::ICMP_SLE:   return executeICMP_SLE(Src1, Src2, Ty);
00657   case FCmpInst::FCMP_ORD:   return executeFCMP_ORD(Src1, Src2, Ty);
00658   case FCmpInst::FCMP_UNO:   return executeFCMP_UNO(Src1, Src2, Ty);
00659   case FCmpInst::FCMP_OEQ:   return executeFCMP_OEQ(Src1, Src2, Ty);
00660   case FCmpInst::FCMP_UEQ:   return executeFCMP_UEQ(Src1, Src2, Ty);
00661   case FCmpInst::FCMP_ONE:   return executeFCMP_ONE(Src1, Src2, Ty);
00662   case FCmpInst::FCMP_UNE:   return executeFCMP_UNE(Src1, Src2, Ty);
00663   case FCmpInst::FCMP_OLT:   return executeFCMP_OLT(Src1, Src2, Ty);
00664   case FCmpInst::FCMP_ULT:   return executeFCMP_ULT(Src1, Src2, Ty);
00665   case FCmpInst::FCMP_OGT:   return executeFCMP_OGT(Src1, Src2, Ty);
00666   case FCmpInst::FCMP_UGT:   return executeFCMP_UGT(Src1, Src2, Ty);
00667   case FCmpInst::FCMP_OLE:   return executeFCMP_OLE(Src1, Src2, Ty);
00668   case FCmpInst::FCMP_ULE:   return executeFCMP_ULE(Src1, Src2, Ty);
00669   case FCmpInst::FCMP_OGE:   return executeFCMP_OGE(Src1, Src2, Ty);
00670   case FCmpInst::FCMP_UGE:   return executeFCMP_UGE(Src1, Src2, Ty);
00671   case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false);
00672   case FCmpInst::FCMP_TRUE:  return executeFCMP_BOOL(Src1, Src2, Ty, true);
00673   default:
00674     dbgs() << "Unhandled Cmp predicate\n";
00675     llvm_unreachable(0);
00676   }
00677 }
00678 
00679 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
00680   ExecutionContext &SF = ECStack.back();
00681   Type *Ty    = I.getOperand(0)->getType();
00682   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00683   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00684   GenericValue R;   // Result
00685 
00686   // First process vector operation
00687   if (Ty->isVectorTy()) {
00688     assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
00689     R.AggregateVal.resize(Src1.AggregateVal.size());
00690 
00691     // Macros to execute binary operation 'OP' over integer vectors
00692 #define INTEGER_VECTOR_OPERATION(OP)                               \
00693     for (unsigned i = 0; i < R.AggregateVal.size(); ++i)           \
00694       R.AggregateVal[i].IntVal =                                   \
00695       Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal;
00696 
00697     // Additional macros to execute binary operations udiv/sdiv/urem/srem since
00698     // they have different notation.
00699 #define INTEGER_VECTOR_FUNCTION(OP)                                \
00700     for (unsigned i = 0; i < R.AggregateVal.size(); ++i)           \
00701       R.AggregateVal[i].IntVal =                                   \
00702       Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal);
00703 
00704     // Macros to execute binary operation 'OP' over floating point type TY
00705     // (float or double) vectors
00706 #define FLOAT_VECTOR_FUNCTION(OP, TY)                               \
00707       for (unsigned i = 0; i < R.AggregateVal.size(); ++i)          \
00708         R.AggregateVal[i].TY =                                      \
00709         Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY;
00710 
00711     // Macros to choose appropriate TY: float or double and run operation
00712     // execution
00713 #define FLOAT_VECTOR_OP(OP) {                                         \
00714   if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())        \
00715     FLOAT_VECTOR_FUNCTION(OP, FloatVal)                               \
00716   else {                                                              \
00717     if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())     \
00718       FLOAT_VECTOR_FUNCTION(OP, DoubleVal)                            \
00719     else {                                                            \
00720       dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \
00721       llvm_unreachable(0);                                            \
00722     }                                                                 \
00723   }                                                                   \
00724 }
00725 
00726     switch(I.getOpcode()){
00727     default:
00728       dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
00729       llvm_unreachable(0);
00730       break;
00731     case Instruction::Add:   INTEGER_VECTOR_OPERATION(+) break;
00732     case Instruction::Sub:   INTEGER_VECTOR_OPERATION(-) break;
00733     case Instruction::Mul:   INTEGER_VECTOR_OPERATION(*) break;
00734     case Instruction::UDiv:  INTEGER_VECTOR_FUNCTION(udiv) break;
00735     case Instruction::SDiv:  INTEGER_VECTOR_FUNCTION(sdiv) break;
00736     case Instruction::URem:  INTEGER_VECTOR_FUNCTION(urem) break;
00737     case Instruction::SRem:  INTEGER_VECTOR_FUNCTION(srem) break;
00738     case Instruction::And:   INTEGER_VECTOR_OPERATION(&) break;
00739     case Instruction::Or:    INTEGER_VECTOR_OPERATION(|) break;
00740     case Instruction::Xor:   INTEGER_VECTOR_OPERATION(^) break;
00741     case Instruction::FAdd:  FLOAT_VECTOR_OP(+) break;
00742     case Instruction::FSub:  FLOAT_VECTOR_OP(-) break;
00743     case Instruction::FMul:  FLOAT_VECTOR_OP(*) break;
00744     case Instruction::FDiv:  FLOAT_VECTOR_OP(/) break;
00745     case Instruction::FRem:
00746       if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy())
00747         for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
00748           R.AggregateVal[i].FloatVal = 
00749           fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal);
00750       else {
00751         if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy())
00752           for (unsigned i = 0; i < R.AggregateVal.size(); ++i)
00753             R.AggregateVal[i].DoubleVal = 
00754             fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal);
00755         else {
00756           dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n";
00757           llvm_unreachable(0);
00758         }
00759       }
00760       break;
00761     }
00762   } else {
00763     switch (I.getOpcode()) {
00764     default:
00765       dbgs() << "Don't know how to handle this binary operator!\n-->" << I;
00766       llvm_unreachable(0);
00767       break;
00768     case Instruction::Add:   R.IntVal = Src1.IntVal + Src2.IntVal; break;
00769     case Instruction::Sub:   R.IntVal = Src1.IntVal - Src2.IntVal; break;
00770     case Instruction::Mul:   R.IntVal = Src1.IntVal * Src2.IntVal; break;
00771     case Instruction::FAdd:  executeFAddInst(R, Src1, Src2, Ty); break;
00772     case Instruction::FSub:  executeFSubInst(R, Src1, Src2, Ty); break;
00773     case Instruction::FMul:  executeFMulInst(R, Src1, Src2, Ty); break;
00774     case Instruction::FDiv:  executeFDivInst(R, Src1, Src2, Ty); break;
00775     case Instruction::FRem:  executeFRemInst(R, Src1, Src2, Ty); break;
00776     case Instruction::UDiv:  R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
00777     case Instruction::SDiv:  R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
00778     case Instruction::URem:  R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
00779     case Instruction::SRem:  R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
00780     case Instruction::And:   R.IntVal = Src1.IntVal & Src2.IntVal; break;
00781     case Instruction::Or:    R.IntVal = Src1.IntVal | Src2.IntVal; break;
00782     case Instruction::Xor:   R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
00783     }
00784   }
00785   SetValue(&I, R, SF);
00786 }
00787 
00788 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
00789                                       GenericValue Src3, const Type *Ty) {
00790     GenericValue Dest;
00791     if(Ty->isVectorTy()) {
00792       assert(Src1.AggregateVal.size() == Src2.AggregateVal.size());
00793       assert(Src2.AggregateVal.size() == Src3.AggregateVal.size());
00794       Dest.AggregateVal.resize( Src1.AggregateVal.size() );
00795       for (size_t i = 0; i < Src1.AggregateVal.size(); ++i)
00796         Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ?
00797           Src3.AggregateVal[i] : Src2.AggregateVal[i];
00798     } else {
00799       Dest = (Src1.IntVal == 0) ? Src3 : Src2;
00800     }
00801     return Dest;
00802 }
00803 
00804 void Interpreter::visitSelectInst(SelectInst &I) {
00805   ExecutionContext &SF = ECStack.back();
00806   const Type * Ty = I.getOperand(0)->getType();
00807   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00808   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00809   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
00810   GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty);
00811   SetValue(&I, R, SF);
00812 }
00813 
00814 //===----------------------------------------------------------------------===//
00815 //                     Terminator Instruction Implementations
00816 //===----------------------------------------------------------------------===//
00817 
00818 void Interpreter::exitCalled(GenericValue GV) {
00819   // runAtExitHandlers() assumes there are no stack frames, but
00820   // if exit() was called, then it had a stack frame. Blow away
00821   // the stack before interpreting atexit handlers.
00822   ECStack.clear();
00823   runAtExitHandlers();
00824   exit(GV.IntVal.zextOrTrunc(32).getZExtValue());
00825 }
00826 
00827 /// Pop the last stack frame off of ECStack and then copy the result
00828 /// back into the result variable if we are not returning void. The
00829 /// result variable may be the ExitValue, or the Value of the calling
00830 /// CallInst if there was a previous stack frame. This method may
00831 /// invalidate any ECStack iterators you have. This method also takes
00832 /// care of switching to the normal destination BB, if we are returning
00833 /// from an invoke.
00834 ///
00835 void Interpreter::popStackAndReturnValueToCaller(Type *RetTy,
00836                                                  GenericValue Result) {
00837   // Pop the current stack frame.
00838   ECStack.pop_back();
00839 
00840   if (ECStack.empty()) {  // Finished main.  Put result into exit code...
00841     if (RetTy && !RetTy->isVoidTy()) {          // Nonvoid return type?
00842       ExitValue = Result;   // Capture the exit value of the program
00843     } else {
00844       memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
00845     }
00846   } else {
00847     // If we have a previous stack frame, and we have a previous call,
00848     // fill in the return value...
00849     ExecutionContext &CallingSF = ECStack.back();
00850     if (Instruction *I = CallingSF.Caller.getInstruction()) {
00851       // Save result...
00852       if (!CallingSF.Caller.getType()->isVoidTy())
00853         SetValue(I, Result, CallingSF);
00854       if (InvokeInst *II = dyn_cast<InvokeInst> (I))
00855         SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
00856       CallingSF.Caller = CallSite();          // We returned from the call...
00857     }
00858   }
00859 }
00860 
00861 void Interpreter::visitReturnInst(ReturnInst &I) {
00862   ExecutionContext &SF = ECStack.back();
00863   Type *RetTy = Type::getVoidTy(I.getContext());
00864   GenericValue Result;
00865 
00866   // Save away the return value... (if we are not 'ret void')
00867   if (I.getNumOperands()) {
00868     RetTy  = I.getReturnValue()->getType();
00869     Result = getOperandValue(I.getReturnValue(), SF);
00870   }
00871 
00872   popStackAndReturnValueToCaller(RetTy, Result);
00873 }
00874 
00875 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
00876   report_fatal_error("Program executed an 'unreachable' instruction!");
00877 }
00878 
00879 void Interpreter::visitBranchInst(BranchInst &I) {
00880   ExecutionContext &SF = ECStack.back();
00881   BasicBlock *Dest;
00882 
00883   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
00884   if (!I.isUnconditional()) {
00885     Value *Cond = I.getCondition();
00886     if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
00887       Dest = I.getSuccessor(1);
00888   }
00889   SwitchToNewBasicBlock(Dest, SF);
00890 }
00891 
00892 void Interpreter::visitSwitchInst(SwitchInst &I) {
00893   ExecutionContext &SF = ECStack.back();
00894   Value* Cond = I.getCondition();
00895   Type *ElTy = Cond->getType();
00896   GenericValue CondVal = getOperandValue(Cond, SF);
00897 
00898   // Check to see if any of the cases match...
00899   BasicBlock *Dest = 0;
00900   for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) {
00901     GenericValue CaseVal = getOperandValue(i.getCaseValue(), SF);
00902     if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) {
00903       Dest = cast<BasicBlock>(i.getCaseSuccessor());
00904       break;
00905     }
00906   }
00907   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
00908   SwitchToNewBasicBlock(Dest, SF);
00909 }
00910 
00911 void Interpreter::visitIndirectBrInst(IndirectBrInst &I) {
00912   ExecutionContext &SF = ECStack.back();
00913   void *Dest = GVTOP(getOperandValue(I.getAddress(), SF));
00914   SwitchToNewBasicBlock((BasicBlock*)Dest, SF);
00915 }
00916 
00917 
00918 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
00919 // This function handles the actual updating of block and instruction iterators
00920 // as well as execution of all of the PHI nodes in the destination block.
00921 //
00922 // This method does this because all of the PHI nodes must be executed
00923 // atomically, reading their inputs before any of the results are updated.  Not
00924 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
00925 // their inputs.  If the input PHI node is updated before it is read, incorrect
00926 // results can happen.  Thus we use a two phase approach.
00927 //
00928 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
00929   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
00930   SF.CurBB   = Dest;                  // Update CurBB to branch destination
00931   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
00932 
00933   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
00934 
00935   // Loop over all of the PHI nodes in the current block, reading their inputs.
00936   std::vector<GenericValue> ResultValues;
00937 
00938   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
00939     // Search for the value corresponding to this previous bb...
00940     int i = PN->getBasicBlockIndex(PrevBB);
00941     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
00942     Value *IncomingValue = PN->getIncomingValue(i);
00943 
00944     // Save the incoming value for this PHI node...
00945     ResultValues.push_back(getOperandValue(IncomingValue, SF));
00946   }
00947 
00948   // Now loop over all of the PHI nodes setting their values...
00949   SF.CurInst = SF.CurBB->begin();
00950   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
00951     PHINode *PN = cast<PHINode>(SF.CurInst);
00952     SetValue(PN, ResultValues[i], SF);
00953   }
00954 }
00955 
00956 //===----------------------------------------------------------------------===//
00957 //                     Memory Instruction Implementations
00958 //===----------------------------------------------------------------------===//
00959 
00960 void Interpreter::visitAllocaInst(AllocaInst &I) {
00961   ExecutionContext &SF = ECStack.back();
00962 
00963   Type *Ty = I.getType()->getElementType();  // Type to be allocated
00964 
00965   // Get the number of elements being allocated by the array...
00966   unsigned NumElements = 
00967     getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
00968 
00969   unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty);
00970 
00971   // Avoid malloc-ing zero bytes, use max()...
00972   unsigned MemToAlloc = std::max(1U, NumElements * TypeSize);
00973 
00974   // Allocate enough memory to hold the type...
00975   void *Memory = malloc(MemToAlloc);
00976 
00977   DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x " 
00978                << NumElements << " (Total: " << MemToAlloc << ") at "
00979                << uintptr_t(Memory) << '\n');
00980 
00981   GenericValue Result = PTOGV(Memory);
00982   assert(Result.PointerVal != 0 && "Null pointer returned by malloc!");
00983   SetValue(&I, Result, SF);
00984 
00985   if (I.getOpcode() == Instruction::Alloca)
00986     ECStack.back().Allocas.add(Memory);
00987 }
00988 
00989 // getElementOffset - The workhorse for getelementptr.
00990 //
00991 GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
00992                                               gep_type_iterator E,
00993                                               ExecutionContext &SF) {
00994   assert(Ptr->getType()->isPointerTy() &&
00995          "Cannot getElementOffset of a nonpointer type!");
00996 
00997   uint64_t Total = 0;
00998 
00999   for (; I != E; ++I) {
01000     if (StructType *STy = dyn_cast<StructType>(*I)) {
01001       const StructLayout *SLO = TD.getStructLayout(STy);
01002 
01003       const ConstantInt *CPU = cast<ConstantInt>(I.getOperand());
01004       unsigned Index = unsigned(CPU->getZExtValue());
01005 
01006       Total += SLO->getElementOffset(Index);
01007     } else {
01008       SequentialType *ST = cast<SequentialType>(*I);
01009       // Get the index number for the array... which must be long type...
01010       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
01011 
01012       int64_t Idx;
01013       unsigned BitWidth = 
01014         cast<IntegerType>(I.getOperand()->getType())->getBitWidth();
01015       if (BitWidth == 32)
01016         Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue();
01017       else {
01018         assert(BitWidth == 64 && "Invalid index type for getelementptr");
01019         Idx = (int64_t)IdxGV.IntVal.getZExtValue();
01020       }
01021       Total += TD.getTypeAllocSize(ST->getElementType())*Idx;
01022     }
01023   }
01024 
01025   GenericValue Result;
01026   Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total;
01027   DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n");
01028   return Result;
01029 }
01030 
01031 void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) {
01032   ExecutionContext &SF = ECStack.back();
01033   SetValue(&I, executeGEPOperation(I.getPointerOperand(),
01034                                    gep_type_begin(I), gep_type_end(I), SF), SF);
01035 }
01036 
01037 void Interpreter::visitLoadInst(LoadInst &I) {
01038   ExecutionContext &SF = ECStack.back();
01039   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
01040   GenericValue *Ptr = (GenericValue*)GVTOP(SRC);
01041   GenericValue Result;
01042   LoadValueFromMemory(Result, Ptr, I.getType());
01043   SetValue(&I, Result, SF);
01044   if (I.isVolatile() && PrintVolatile)
01045     dbgs() << "Volatile load " << I;
01046 }
01047 
01048 void Interpreter::visitStoreInst(StoreInst &I) {
01049   ExecutionContext &SF = ECStack.back();
01050   GenericValue Val = getOperandValue(I.getOperand(0), SF);
01051   GenericValue SRC = getOperandValue(I.getPointerOperand(), SF);
01052   StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC),
01053                      I.getOperand(0)->getType());
01054   if (I.isVolatile() && PrintVolatile)
01055     dbgs() << "Volatile store: " << I;
01056 }
01057 
01058 //===----------------------------------------------------------------------===//
01059 //                 Miscellaneous Instruction Implementations
01060 //===----------------------------------------------------------------------===//
01061 
01062 void Interpreter::visitCallSite(CallSite CS) {
01063   ExecutionContext &SF = ECStack.back();
01064 
01065   // Check to see if this is an intrinsic function call...
01066   Function *F = CS.getCalledFunction();
01067   if (F && F->isDeclaration())
01068     switch (F->getIntrinsicID()) {
01069     case Intrinsic::not_intrinsic:
01070       break;
01071     case Intrinsic::vastart: { // va_start
01072       GenericValue ArgIndex;
01073       ArgIndex.UIntPairVal.first = ECStack.size() - 1;
01074       ArgIndex.UIntPairVal.second = 0;
01075       SetValue(CS.getInstruction(), ArgIndex, SF);
01076       return;
01077     }
01078     case Intrinsic::vaend:    // va_end is a noop for the interpreter
01079       return;
01080     case Intrinsic::vacopy:   // va_copy: dest = src
01081       SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF);
01082       return;
01083     default:
01084       // If it is an unknown intrinsic function, use the intrinsic lowering
01085       // class to transform it into hopefully tasty LLVM code.
01086       //
01087       BasicBlock::iterator me(CS.getInstruction());
01088       BasicBlock *Parent = CS.getInstruction()->getParent();
01089       bool atBegin(Parent->begin() == me);
01090       if (!atBegin)
01091         --me;
01092       IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction()));
01093 
01094       // Restore the CurInst pointer to the first instruction newly inserted, if
01095       // any.
01096       if (atBegin) {
01097         SF.CurInst = Parent->begin();
01098       } else {
01099         SF.CurInst = me;
01100         ++SF.CurInst;
01101       }
01102       return;
01103     }
01104 
01105 
01106   SF.Caller = CS;
01107   std::vector<GenericValue> ArgVals;
01108   const unsigned NumArgs = SF.Caller.arg_size();
01109   ArgVals.reserve(NumArgs);
01110   uint16_t pNum = 1;
01111   for (CallSite::arg_iterator i = SF.Caller.arg_begin(),
01112          e = SF.Caller.arg_end(); i != e; ++i, ++pNum) {
01113     Value *V = *i;
01114     ArgVals.push_back(getOperandValue(V, SF));
01115   }
01116 
01117   // To handle indirect calls, we must get the pointer value from the argument
01118   // and treat it as a function pointer.
01119   GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF);
01120   callFunction((Function*)GVTOP(SRC), ArgVals);
01121 }
01122 
01123 // auxiliary function for shift operations
01124 static unsigned getShiftAmount(uint64_t orgShiftAmount,
01125                                llvm::APInt valueToShift) {
01126   unsigned valueWidth = valueToShift.getBitWidth();
01127   if (orgShiftAmount < (uint64_t)valueWidth)
01128     return orgShiftAmount;
01129   // according to the llvm documentation, if orgShiftAmount > valueWidth,
01130   // the result is undfeined. but we do shift by this rule:
01131   return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount;
01132 }
01133 
01134 
01135 void Interpreter::visitShl(BinaryOperator &I) {
01136   ExecutionContext &SF = ECStack.back();
01137   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
01138   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01139   GenericValue Dest;
01140   const Type *Ty = I.getType();
01141 
01142   if (Ty->isVectorTy()) {
01143     uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
01144     assert(src1Size == Src2.AggregateVal.size());
01145     for (unsigned i = 0; i < src1Size; i++) {
01146       GenericValue Result;
01147       uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
01148       llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
01149       Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
01150       Dest.AggregateVal.push_back(Result);
01151     }
01152   } else {
01153     // scalar
01154     uint64_t shiftAmount = Src2.IntVal.getZExtValue();
01155     llvm::APInt valueToShift = Src1.IntVal;
01156     Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift));
01157   }
01158 
01159   SetValue(&I, Dest, SF);
01160 }
01161 
01162 void Interpreter::visitLShr(BinaryOperator &I) {
01163   ExecutionContext &SF = ECStack.back();
01164   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
01165   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01166   GenericValue Dest;
01167   const Type *Ty = I.getType();
01168 
01169   if (Ty->isVectorTy()) {
01170     uint32_t src1Size = uint32_t(Src1.AggregateVal.size());
01171     assert(src1Size == Src2.AggregateVal.size());
01172     for (unsigned i = 0; i < src1Size; i++) {
01173       GenericValue Result;
01174       uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
01175       llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
01176       Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
01177       Dest.AggregateVal.push_back(Result);
01178     }
01179   } else {
01180     // scalar
01181     uint64_t shiftAmount = Src2.IntVal.getZExtValue();
01182     llvm::APInt valueToShift = Src1.IntVal;
01183     Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift));
01184   }
01185 
01186   SetValue(&I, Dest, SF);
01187 }
01188 
01189 void Interpreter::visitAShr(BinaryOperator &I) {
01190   ExecutionContext &SF = ECStack.back();
01191   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
01192   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01193   GenericValue Dest;
01194   const Type *Ty = I.getType();
01195 
01196   if (Ty->isVectorTy()) {
01197     size_t src1Size = Src1.AggregateVal.size();
01198     assert(src1Size == Src2.AggregateVal.size());
01199     for (unsigned i = 0; i < src1Size; i++) {
01200       GenericValue Result;
01201       uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue();
01202       llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal;
01203       Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
01204       Dest.AggregateVal.push_back(Result);
01205     }
01206   } else {
01207     // scalar
01208     uint64_t shiftAmount = Src2.IntVal.getZExtValue();
01209     llvm::APInt valueToShift = Src1.IntVal;
01210     Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift));
01211   }
01212 
01213   SetValue(&I, Dest, SF);
01214 }
01215 
01216 GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy,
01217                                            ExecutionContext &SF) {
01218   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01219   Type *SrcTy = SrcVal->getType();
01220   if (SrcTy->isVectorTy()) {
01221     Type *DstVecTy = DstTy->getScalarType();
01222     unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
01223     unsigned NumElts = Src.AggregateVal.size();
01224     // the sizes of src and dst vectors must be equal
01225     Dest.AggregateVal.resize(NumElts);
01226     for (unsigned i = 0; i < NumElts; i++)
01227       Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth);
01228   } else {
01229     IntegerType *DITy = cast<IntegerType>(DstTy);
01230     unsigned DBitWidth = DITy->getBitWidth();
01231     Dest.IntVal = Src.IntVal.trunc(DBitWidth);
01232   }
01233   return Dest;
01234 }
01235 
01236 GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy,
01237                                           ExecutionContext &SF) {
01238   const Type *SrcTy = SrcVal->getType();
01239   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01240   if (SrcTy->isVectorTy()) {
01241     const Type *DstVecTy = DstTy->getScalarType();
01242     unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
01243     unsigned size = Src.AggregateVal.size();
01244     // the sizes of src and dst vectors must be equal.
01245     Dest.AggregateVal.resize(size);
01246     for (unsigned i = 0; i < size; i++)
01247       Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth);
01248   } else {
01249     const IntegerType *DITy = cast<IntegerType>(DstTy);
01250     unsigned DBitWidth = DITy->getBitWidth();
01251     Dest.IntVal = Src.IntVal.sext(DBitWidth);
01252   }
01253   return Dest;
01254 }
01255 
01256 GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy,
01257                                           ExecutionContext &SF) {
01258   const Type *SrcTy = SrcVal->getType();
01259   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01260   if (SrcTy->isVectorTy()) {
01261     const Type *DstVecTy = DstTy->getScalarType();
01262     unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
01263 
01264     unsigned size = Src.AggregateVal.size();
01265     // the sizes of src and dst vectors must be equal.
01266     Dest.AggregateVal.resize(size);
01267     for (unsigned i = 0; i < size; i++)
01268       Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth);
01269   } else {
01270     const IntegerType *DITy = cast<IntegerType>(DstTy);
01271     unsigned DBitWidth = DITy->getBitWidth();
01272     Dest.IntVal = Src.IntVal.zext(DBitWidth);
01273   }
01274   return Dest;
01275 }
01276 
01277 GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy,
01278                                              ExecutionContext &SF) {
01279   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01280 
01281   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
01282     assert(SrcVal->getType()->getScalarType()->isDoubleTy() &&
01283            DstTy->getScalarType()->isFloatTy() &&
01284            "Invalid FPTrunc instruction");
01285 
01286     unsigned size = Src.AggregateVal.size();
01287     // the sizes of src and dst vectors must be equal.
01288     Dest.AggregateVal.resize(size);
01289     for (unsigned i = 0; i < size; i++)
01290       Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal;
01291   } else {
01292     assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() &&
01293            "Invalid FPTrunc instruction");
01294     Dest.FloatVal = (float)Src.DoubleVal;
01295   }
01296 
01297   return Dest;
01298 }
01299 
01300 GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy,
01301                                            ExecutionContext &SF) {
01302   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01303 
01304   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
01305     assert(SrcVal->getType()->getScalarType()->isFloatTy() &&
01306            DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction");
01307 
01308     unsigned size = Src.AggregateVal.size();
01309     // the sizes of src and dst vectors must be equal.
01310     Dest.AggregateVal.resize(size);
01311     for (unsigned i = 0; i < size; i++)
01312       Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal;
01313   } else {
01314     assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() &&
01315            "Invalid FPExt instruction");
01316     Dest.DoubleVal = (double)Src.FloatVal;
01317   }
01318 
01319   return Dest;
01320 }
01321 
01322 GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy,
01323                                             ExecutionContext &SF) {
01324   Type *SrcTy = SrcVal->getType();
01325   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01326 
01327   if (SrcTy->getTypeID() == Type::VectorTyID) {
01328     const Type *DstVecTy = DstTy->getScalarType();
01329     const Type *SrcVecTy = SrcTy->getScalarType();
01330     uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
01331     unsigned size = Src.AggregateVal.size();
01332     // the sizes of src and dst vectors must be equal.
01333     Dest.AggregateVal.resize(size);
01334 
01335     if (SrcVecTy->getTypeID() == Type::FloatTyID) {
01336       assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction");
01337       for (unsigned i = 0; i < size; i++)
01338         Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
01339             Src.AggregateVal[i].FloatVal, DBitWidth);
01340     } else {
01341       for (unsigned i = 0; i < size; i++)
01342         Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
01343             Src.AggregateVal[i].DoubleVal, DBitWidth);
01344     }
01345   } else {
01346     // scalar
01347     uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
01348     assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction");
01349 
01350     if (SrcTy->getTypeID() == Type::FloatTyID)
01351       Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
01352     else {
01353       Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
01354     }
01355   }
01356 
01357   return Dest;
01358 }
01359 
01360 GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy,
01361                                             ExecutionContext &SF) {
01362   Type *SrcTy = SrcVal->getType();
01363   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01364 
01365   if (SrcTy->getTypeID() == Type::VectorTyID) {
01366     const Type *DstVecTy = DstTy->getScalarType();
01367     const Type *SrcVecTy = SrcTy->getScalarType();
01368     uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth();
01369     unsigned size = Src.AggregateVal.size();
01370     // the sizes of src and dst vectors must be equal
01371     Dest.AggregateVal.resize(size);
01372 
01373     if (SrcVecTy->getTypeID() == Type::FloatTyID) {
01374       assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction");
01375       for (unsigned i = 0; i < size; i++)
01376         Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt(
01377             Src.AggregateVal[i].FloatVal, DBitWidth);
01378     } else {
01379       for (unsigned i = 0; i < size; i++)
01380         Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt(
01381             Src.AggregateVal[i].DoubleVal, DBitWidth);
01382     }
01383   } else {
01384     // scalar
01385     unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
01386     assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction");
01387 
01388     if (SrcTy->getTypeID() == Type::FloatTyID)
01389       Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth);
01390     else {
01391       Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth);
01392     }
01393   }
01394   return Dest;
01395 }
01396 
01397 GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy,
01398                                             ExecutionContext &SF) {
01399   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01400 
01401   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
01402     const Type *DstVecTy = DstTy->getScalarType();
01403     unsigned size = Src.AggregateVal.size();
01404     // the sizes of src and dst vectors must be equal
01405     Dest.AggregateVal.resize(size);
01406 
01407     if (DstVecTy->getTypeID() == Type::FloatTyID) {
01408       assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction");
01409       for (unsigned i = 0; i < size; i++)
01410         Dest.AggregateVal[i].FloatVal =
01411             APIntOps::RoundAPIntToFloat(Src.AggregateVal[i].IntVal);
01412     } else {
01413       for (unsigned i = 0; i < size; i++)
01414         Dest.AggregateVal[i].DoubleVal =
01415             APIntOps::RoundAPIntToDouble(Src.AggregateVal[i].IntVal);
01416     }
01417   } else {
01418     // scalar
01419     assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction");
01420     if (DstTy->getTypeID() == Type::FloatTyID)
01421       Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal);
01422     else {
01423       Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal);
01424     }
01425   }
01426   return Dest;
01427 }
01428 
01429 GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy,
01430                                             ExecutionContext &SF) {
01431   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01432 
01433   if (SrcVal->getType()->getTypeID() == Type::VectorTyID) {
01434     const Type *DstVecTy = DstTy->getScalarType();
01435     unsigned size = Src.AggregateVal.size();
01436     // the sizes of src and dst vectors must be equal
01437     Dest.AggregateVal.resize(size);
01438 
01439     if (DstVecTy->getTypeID() == Type::FloatTyID) {
01440       assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction");
01441       for (unsigned i = 0; i < size; i++)
01442         Dest.AggregateVal[i].FloatVal =
01443             APIntOps::RoundSignedAPIntToFloat(Src.AggregateVal[i].IntVal);
01444     } else {
01445       for (unsigned i = 0; i < size; i++)
01446         Dest.AggregateVal[i].DoubleVal =
01447             APIntOps::RoundSignedAPIntToDouble(Src.AggregateVal[i].IntVal);
01448     }
01449   } else {
01450     // scalar
01451     assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction");
01452 
01453     if (DstTy->getTypeID() == Type::FloatTyID)
01454       Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal);
01455     else {
01456       Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal);
01457     }
01458   }
01459 
01460   return Dest;
01461 }
01462 
01463 GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy,
01464                                               ExecutionContext &SF) {
01465   uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth();
01466   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01467   assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction");
01468 
01469   Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal);
01470   return Dest;
01471 }
01472 
01473 GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy,
01474                                               ExecutionContext &SF) {
01475   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01476   assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction");
01477 
01478   uint32_t PtrSize = TD.getPointerSizeInBits();
01479   if (PtrSize != Src.IntVal.getBitWidth())
01480     Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize);
01481 
01482   Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue()));
01483   return Dest;
01484 }
01485 
01486 GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy,
01487                                              ExecutionContext &SF) {
01488 
01489   // This instruction supports bitwise conversion of vectors to integers and
01490   // to vectors of other types (as long as they have the same size)
01491   Type *SrcTy = SrcVal->getType();
01492   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
01493 
01494   if ((SrcTy->getTypeID() == Type::VectorTyID) ||
01495       (DstTy->getTypeID() == Type::VectorTyID)) {
01496     // vector src bitcast to vector dst or vector src bitcast to scalar dst or
01497     // scalar src bitcast to vector dst
01498     bool isLittleEndian = TD.isLittleEndian();
01499     GenericValue TempDst, TempSrc, SrcVec;
01500     const Type *SrcElemTy;
01501     const Type *DstElemTy;
01502     unsigned SrcBitSize;
01503     unsigned DstBitSize;
01504     unsigned SrcNum;
01505     unsigned DstNum;
01506 
01507     if (SrcTy->getTypeID() == Type::VectorTyID) {
01508       SrcElemTy = SrcTy->getScalarType();
01509       SrcBitSize = SrcTy->getScalarSizeInBits();
01510       SrcNum = Src.AggregateVal.size();
01511       SrcVec = Src;
01512     } else {
01513       // if src is scalar value, make it vector <1 x type>
01514       SrcElemTy = SrcTy;
01515       SrcBitSize = SrcTy->getPrimitiveSizeInBits();
01516       SrcNum = 1;
01517       SrcVec.AggregateVal.push_back(Src);
01518     }
01519 
01520     if (DstTy->getTypeID() == Type::VectorTyID) {
01521       DstElemTy = DstTy->getScalarType();
01522       DstBitSize = DstTy->getScalarSizeInBits();
01523       DstNum = (SrcNum * SrcBitSize) / DstBitSize;
01524     } else {
01525       DstElemTy = DstTy;
01526       DstBitSize = DstTy->getPrimitiveSizeInBits();
01527       DstNum = 1;
01528     }
01529 
01530     if (SrcNum * SrcBitSize != DstNum * DstBitSize)
01531       llvm_unreachable("Invalid BitCast");
01532 
01533     // If src is floating point, cast to integer first.
01534     TempSrc.AggregateVal.resize(SrcNum);
01535     if (SrcElemTy->isFloatTy()) {
01536       for (unsigned i = 0; i < SrcNum; i++)
01537         TempSrc.AggregateVal[i].IntVal =
01538             APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal);
01539 
01540     } else if (SrcElemTy->isDoubleTy()) {
01541       for (unsigned i = 0; i < SrcNum; i++)
01542         TempSrc.AggregateVal[i].IntVal =
01543             APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal);
01544     } else if (SrcElemTy->isIntegerTy()) {
01545       for (unsigned i = 0; i < SrcNum; i++)
01546         TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal;
01547     } else {
01548       // Pointers are not allowed as the element type of vector.
01549       llvm_unreachable("Invalid Bitcast");
01550     }
01551 
01552     // now TempSrc is integer type vector
01553     if (DstNum < SrcNum) {
01554       // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64>
01555       unsigned Ratio = SrcNum / DstNum;
01556       unsigned SrcElt = 0;
01557       for (unsigned i = 0; i < DstNum; i++) {
01558         GenericValue Elt;
01559         Elt.IntVal = 0;
01560         Elt.IntVal = Elt.IntVal.zext(DstBitSize);
01561         unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1);
01562         for (unsigned j = 0; j < Ratio; j++) {
01563           APInt Tmp;
01564           Tmp = Tmp.zext(SrcBitSize);
01565           Tmp = TempSrc.AggregateVal[SrcElt++].IntVal;
01566           Tmp = Tmp.zext(DstBitSize);
01567           Tmp = Tmp.shl(ShiftAmt);
01568           ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
01569           Elt.IntVal |= Tmp;
01570         }
01571         TempDst.AggregateVal.push_back(Elt);
01572       }
01573     } else {
01574       // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32>
01575       unsigned Ratio = DstNum / SrcNum;
01576       for (unsigned i = 0; i < SrcNum; i++) {
01577         unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1);
01578         for (unsigned j = 0; j < Ratio; j++) {
01579           GenericValue Elt;
01580           Elt.IntVal = Elt.IntVal.zext(SrcBitSize);
01581           Elt.IntVal = TempSrc.AggregateVal[i].IntVal;
01582           Elt.IntVal = Elt.IntVal.lshr(ShiftAmt);
01583           // it could be DstBitSize == SrcBitSize, so check it
01584           if (DstBitSize < SrcBitSize)
01585             Elt.IntVal = Elt.IntVal.trunc(DstBitSize);
01586           ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
01587           TempDst.AggregateVal.push_back(Elt);
01588         }
01589       }
01590     }
01591 
01592     // convert result from integer to specified type
01593     if (DstTy->getTypeID() == Type::VectorTyID) {
01594       if (DstElemTy->isDoubleTy()) {
01595         Dest.AggregateVal.resize(DstNum);
01596         for (unsigned i = 0; i < DstNum; i++)
01597           Dest.AggregateVal[i].DoubleVal =
01598               TempDst.AggregateVal[i].IntVal.bitsToDouble();
01599       } else if (DstElemTy->isFloatTy()) {
01600         Dest.AggregateVal.resize(DstNum);
01601         for (unsigned i = 0; i < DstNum; i++)
01602           Dest.AggregateVal[i].FloatVal =
01603               TempDst.AggregateVal[i].IntVal.bitsToFloat();
01604       } else {
01605         Dest = TempDst;
01606       }
01607     } else {
01608       if (DstElemTy->isDoubleTy())
01609         Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble();
01610       else if (DstElemTy->isFloatTy()) {
01611         Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat();
01612       } else {
01613         Dest.IntVal = TempDst.AggregateVal[0].IntVal;
01614       }
01615     }
01616   } else { //  if ((SrcTy->getTypeID() == Type::VectorTyID) ||
01617            //     (DstTy->getTypeID() == Type::VectorTyID))
01618 
01619     // scalar src bitcast to scalar dst
01620     if (DstTy->isPointerTy()) {
01621       assert(SrcTy->isPointerTy() && "Invalid BitCast");
01622       Dest.PointerVal = Src.PointerVal;
01623     } else if (DstTy->isIntegerTy()) {
01624       if (SrcTy->isFloatTy())
01625         Dest.IntVal = APInt::floatToBits(Src.FloatVal);
01626       else if (SrcTy->isDoubleTy()) {
01627         Dest.IntVal = APInt::doubleToBits(Src.DoubleVal);
01628       } else if (SrcTy->isIntegerTy()) {
01629         Dest.IntVal = Src.IntVal;
01630       } else {
01631         llvm_unreachable("Invalid BitCast");
01632       }
01633     } else if (DstTy->isFloatTy()) {
01634       if (SrcTy->isIntegerTy())
01635         Dest.FloatVal = Src.IntVal.bitsToFloat();
01636       else {
01637         Dest.FloatVal = Src.FloatVal;
01638       }
01639     } else if (DstTy->isDoubleTy()) {
01640       if (SrcTy->isIntegerTy())
01641         Dest.DoubleVal = Src.IntVal.bitsToDouble();
01642       else {
01643         Dest.DoubleVal = Src.DoubleVal;
01644       }
01645     } else {
01646       llvm_unreachable("Invalid Bitcast");
01647     }
01648   }
01649 
01650   return Dest;
01651 }
01652 
01653 void Interpreter::visitTruncInst(TruncInst &I) {
01654   ExecutionContext &SF = ECStack.back();
01655   SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF);
01656 }
01657 
01658 void Interpreter::visitSExtInst(SExtInst &I) {
01659   ExecutionContext &SF = ECStack.back();
01660   SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF);
01661 }
01662 
01663 void Interpreter::visitZExtInst(ZExtInst &I) {
01664   ExecutionContext &SF = ECStack.back();
01665   SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF);
01666 }
01667 
01668 void Interpreter::visitFPTruncInst(FPTruncInst &I) {
01669   ExecutionContext &SF = ECStack.back();
01670   SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF);
01671 }
01672 
01673 void Interpreter::visitFPExtInst(FPExtInst &I) {
01674   ExecutionContext &SF = ECStack.back();
01675   SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF);
01676 }
01677 
01678 void Interpreter::visitUIToFPInst(UIToFPInst &I) {
01679   ExecutionContext &SF = ECStack.back();
01680   SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF);
01681 }
01682 
01683 void Interpreter::visitSIToFPInst(SIToFPInst &I) {
01684   ExecutionContext &SF = ECStack.back();
01685   SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF);
01686 }
01687 
01688 void Interpreter::visitFPToUIInst(FPToUIInst &I) {
01689   ExecutionContext &SF = ECStack.back();
01690   SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF);
01691 }
01692 
01693 void Interpreter::visitFPToSIInst(FPToSIInst &I) {
01694   ExecutionContext &SF = ECStack.back();
01695   SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF);
01696 }
01697 
01698 void Interpreter::visitPtrToIntInst(PtrToIntInst &I) {
01699   ExecutionContext &SF = ECStack.back();
01700   SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF);
01701 }
01702 
01703 void Interpreter::visitIntToPtrInst(IntToPtrInst &I) {
01704   ExecutionContext &SF = ECStack.back();
01705   SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF);
01706 }
01707 
01708 void Interpreter::visitBitCastInst(BitCastInst &I) {
01709   ExecutionContext &SF = ECStack.back();
01710   SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF);
01711 }
01712 
01713 #define IMPLEMENT_VAARG(TY) \
01714    case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break
01715 
01716 void Interpreter::visitVAArgInst(VAArgInst &I) {
01717   ExecutionContext &SF = ECStack.back();
01718 
01719   // Get the incoming valist parameter.  LLI treats the valist as a
01720   // (ec-stack-depth var-arg-index) pair.
01721   GenericValue VAList = getOperandValue(I.getOperand(0), SF);
01722   GenericValue Dest;
01723   GenericValue Src = ECStack[VAList.UIntPairVal.first]
01724                       .VarArgs[VAList.UIntPairVal.second];
01725   Type *Ty = I.getType();
01726   switch (Ty->getTypeID()) {
01727   case Type::IntegerTyID:
01728     Dest.IntVal = Src.IntVal;
01729     break;
01730   IMPLEMENT_VAARG(Pointer);
01731   IMPLEMENT_VAARG(Float);
01732   IMPLEMENT_VAARG(Double);
01733   default:
01734     dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n";
01735     llvm_unreachable(0);
01736   }
01737 
01738   // Set the Value of this Instruction.
01739   SetValue(&I, Dest, SF);
01740 
01741   // Move the pointer to the next vararg.
01742   ++VAList.UIntPairVal.second;
01743 }
01744 
01745 void Interpreter::visitExtractElementInst(ExtractElementInst &I) {
01746   ExecutionContext &SF = ECStack.back();
01747   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
01748   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01749   GenericValue Dest;
01750 
01751   Type *Ty = I.getType();
01752   const unsigned indx = unsigned(Src2.IntVal.getZExtValue());
01753 
01754   if(Src1.AggregateVal.size() > indx) {
01755     switch (Ty->getTypeID()) {
01756     default:
01757       dbgs() << "Unhandled destination type for extractelement instruction: "
01758       << *Ty << "\n";
01759       llvm_unreachable(0);
01760       break;
01761     case Type::IntegerTyID:
01762       Dest.IntVal = Src1.AggregateVal[indx].IntVal;
01763       break;
01764     case Type::FloatTyID:
01765       Dest.FloatVal = Src1.AggregateVal[indx].FloatVal;
01766       break;
01767     case Type::DoubleTyID:
01768       Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal;
01769       break;
01770     }
01771   } else {
01772     dbgs() << "Invalid index in extractelement instruction\n";
01773   }
01774 
01775   SetValue(&I, Dest, SF);
01776 }
01777 
01778 void Interpreter::visitInsertElementInst(InsertElementInst &I) {
01779   ExecutionContext &SF = ECStack.back();
01780   Type *Ty = I.getType();
01781 
01782   if(!(Ty->isVectorTy()) )
01783     llvm_unreachable("Unhandled dest type for insertelement instruction");
01784 
01785   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
01786   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01787   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
01788   GenericValue Dest;
01789 
01790   Type *TyContained = Ty->getContainedType(0);
01791 
01792   const unsigned indx = unsigned(Src3.IntVal.getZExtValue());
01793   Dest.AggregateVal = Src1.AggregateVal;
01794 
01795   if(Src1.AggregateVal.size() <= indx)
01796       llvm_unreachable("Invalid index in insertelement instruction");
01797   switch (TyContained->getTypeID()) {
01798     default:
01799       llvm_unreachable("Unhandled dest type for insertelement instruction");
01800     case Type::IntegerTyID:
01801       Dest.AggregateVal[indx].IntVal = Src2.IntVal;
01802       break;
01803     case Type::FloatTyID:
01804       Dest.AggregateVal[indx].FloatVal = Src2.FloatVal;
01805       break;
01806     case Type::DoubleTyID:
01807       Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal;
01808       break;
01809   }
01810   SetValue(&I, Dest, SF);
01811 }
01812 
01813 void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){
01814   ExecutionContext &SF = ECStack.back();
01815 
01816   Type *Ty = I.getType();
01817   if(!(Ty->isVectorTy()))
01818     llvm_unreachable("Unhandled dest type for shufflevector instruction");
01819 
01820   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
01821   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01822   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
01823   GenericValue Dest;
01824 
01825   // There is no need to check types of src1 and src2, because the compiled
01826   // bytecode can't contain different types for src1 and src2 for a
01827   // shufflevector instruction.
01828 
01829   Type *TyContained = Ty->getContainedType(0);
01830   unsigned src1Size = (unsigned)Src1.AggregateVal.size();
01831   unsigned src2Size = (unsigned)Src2.AggregateVal.size();
01832   unsigned src3Size = (unsigned)Src3.AggregateVal.size();
01833 
01834   Dest.AggregateVal.resize(src3Size);
01835 
01836   switch (TyContained->getTypeID()) {
01837     default:
01838       llvm_unreachable("Unhandled dest type for insertelement instruction");
01839       break;
01840     case Type::IntegerTyID:
01841       for( unsigned i=0; i<src3Size; i++) {
01842         unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
01843         if(j < src1Size)
01844           Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal;
01845         else if(j < src1Size + src2Size)
01846           Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal;
01847         else
01848           // The selector may not be greater than sum of lengths of first and
01849           // second operands and llasm should not allow situation like
01850           // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef,
01851           //                      <2 x i32> < i32 0, i32 5 >,
01852           // where i32 5 is invalid, but let it be additional check here:
01853           llvm_unreachable("Invalid mask in shufflevector instruction");
01854       }
01855       break;
01856     case Type::FloatTyID:
01857       for( unsigned i=0; i<src3Size; i++) {
01858         unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
01859         if(j < src1Size)
01860           Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal;
01861         else if(j < src1Size + src2Size)
01862           Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal;
01863         else
01864           llvm_unreachable("Invalid mask in shufflevector instruction");
01865         }
01866       break;
01867     case Type::DoubleTyID:
01868       for( unsigned i=0; i<src3Size; i++) {
01869         unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue();
01870         if(j < src1Size)
01871           Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal;
01872         else if(j < src1Size + src2Size)
01873           Dest.AggregateVal[i].DoubleVal =
01874             Src2.AggregateVal[j-src1Size].DoubleVal;
01875         else
01876           llvm_unreachable("Invalid mask in shufflevector instruction");
01877       }
01878       break;
01879   }
01880   SetValue(&I, Dest, SF);
01881 }
01882 
01883 void Interpreter::visitExtractValueInst(ExtractValueInst &I) {
01884   ExecutionContext &SF = ECStack.back();
01885   Value *Agg = I.getAggregateOperand();
01886   GenericValue Dest;
01887   GenericValue Src = getOperandValue(Agg, SF);
01888 
01889   ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
01890   unsigned Num = I.getNumIndices();
01891   GenericValue *pSrc = &Src;
01892 
01893   for (unsigned i = 0 ; i < Num; ++i) {
01894     pSrc = &pSrc->AggregateVal[*IdxBegin];
01895     ++IdxBegin;
01896   }
01897 
01898   Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
01899   switch (IndexedType->getTypeID()) {
01900     default:
01901       llvm_unreachable("Unhandled dest type for extractelement instruction");
01902     break;
01903     case Type::IntegerTyID:
01904       Dest.IntVal = pSrc->IntVal;
01905     break;
01906     case Type::FloatTyID:
01907       Dest.FloatVal = pSrc->FloatVal;
01908     break;
01909     case Type::DoubleTyID:
01910       Dest.DoubleVal = pSrc->DoubleVal;
01911     break;
01912     case Type::ArrayTyID:
01913     case Type::StructTyID:
01914     case Type::VectorTyID:
01915       Dest.AggregateVal = pSrc->AggregateVal;
01916     break;
01917     case Type::PointerTyID:
01918       Dest.PointerVal = pSrc->PointerVal;
01919     break;
01920   }
01921 
01922   SetValue(&I, Dest, SF);
01923 }
01924 
01925 void Interpreter::visitInsertValueInst(InsertValueInst &I) {
01926 
01927   ExecutionContext &SF = ECStack.back();
01928   Value *Agg = I.getAggregateOperand();
01929 
01930   GenericValue Src1 = getOperandValue(Agg, SF);
01931   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
01932   GenericValue Dest = Src1; // Dest is a slightly changed Src1
01933 
01934   ExtractValueInst::idx_iterator IdxBegin = I.idx_begin();
01935   unsigned Num = I.getNumIndices();
01936 
01937   GenericValue *pDest = &Dest;
01938   for (unsigned i = 0 ; i < Num; ++i) {
01939     pDest = &pDest->AggregateVal[*IdxBegin];
01940     ++IdxBegin;
01941   }
01942   // pDest points to the target value in the Dest now
01943 
01944   Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices());
01945 
01946   switch (IndexedType->getTypeID()) {
01947     default:
01948       llvm_unreachable("Unhandled dest type for insertelement instruction");
01949     break;
01950     case Type::IntegerTyID:
01951       pDest->IntVal = Src2.IntVal;
01952     break;
01953     case Type::FloatTyID:
01954       pDest->FloatVal = Src2.FloatVal;
01955     break;
01956     case Type::DoubleTyID:
01957       pDest->DoubleVal = Src2.DoubleVal;
01958     break;
01959     case Type::ArrayTyID:
01960     case Type::StructTyID:
01961     case Type::VectorTyID:
01962       pDest->AggregateVal = Src2.AggregateVal;
01963     break;
01964     case Type::PointerTyID:
01965       pDest->PointerVal = Src2.PointerVal;
01966     break;
01967   }
01968 
01969   SetValue(&I, Dest, SF);
01970 }
01971 
01972 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
01973                                                 ExecutionContext &SF) {
01974   switch (CE->getOpcode()) {
01975   case Instruction::Trunc:
01976       return executeTruncInst(CE->getOperand(0), CE->getType(), SF);
01977   case Instruction::ZExt:
01978       return executeZExtInst(CE->getOperand(0), CE->getType(), SF);
01979   case Instruction::SExt:
01980       return executeSExtInst(CE->getOperand(0), CE->getType(), SF);
01981   case Instruction::FPTrunc:
01982       return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF);
01983   case Instruction::FPExt:
01984       return executeFPExtInst(CE->getOperand(0), CE->getType(), SF);
01985   case Instruction::UIToFP:
01986       return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF);
01987   case Instruction::SIToFP:
01988       return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF);
01989   case Instruction::FPToUI:
01990       return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF);
01991   case Instruction::FPToSI:
01992       return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF);
01993   case Instruction::PtrToInt:
01994       return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF);
01995   case Instruction::IntToPtr:
01996       return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF);
01997   case Instruction::BitCast:
01998       return executeBitCastInst(CE->getOperand(0), CE->getType(), SF);
01999   case Instruction::GetElementPtr:
02000     return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE),
02001                                gep_type_end(CE), SF);
02002   case Instruction::FCmp:
02003   case Instruction::ICmp:
02004     return executeCmpInst(CE->getPredicate(),
02005                           getOperandValue(CE->getOperand(0), SF),
02006                           getOperandValue(CE->getOperand(1), SF),
02007                           CE->getOperand(0)->getType());
02008   case Instruction::Select:
02009     return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
02010                              getOperandValue(CE->getOperand(1), SF),
02011                              getOperandValue(CE->getOperand(2), SF),
02012                              CE->getOperand(0)->getType());
02013   default :
02014     break;
02015   }
02016 
02017   // The cases below here require a GenericValue parameter for the result
02018   // so we initialize one, compute it and then return it.
02019   GenericValue Op0 = getOperandValue(CE->getOperand(0), SF);
02020   GenericValue Op1 = getOperandValue(CE->getOperand(1), SF);
02021   GenericValue Dest;
02022   Type * Ty = CE->getOperand(0)->getType();
02023   switch (CE->getOpcode()) {
02024   case Instruction::Add:  Dest.IntVal = Op0.IntVal + Op1.IntVal; break;
02025   case Instruction::Sub:  Dest.IntVal = Op0.IntVal - Op1.IntVal; break;
02026   case Instruction::Mul:  Dest.IntVal = Op0.IntVal * Op1.IntVal; break;
02027   case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break;
02028   case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break;
02029   case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break;
02030   case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break;
02031   case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break;
02032   case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break;
02033   case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break;
02034   case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break;
02035   case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break;
02036   case Instruction::And:  Dest.IntVal = Op0.IntVal & Op1.IntVal; break;
02037   case Instruction::Or:   Dest.IntVal = Op0.IntVal | Op1.IntVal; break;
02038   case Instruction::Xor:  Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break;
02039   case Instruction::Shl:  
02040     Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue());
02041     break;
02042   case Instruction::LShr: 
02043     Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue());
02044     break;
02045   case Instruction::AShr: 
02046     Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue());
02047     break;
02048   default:
02049     dbgs() << "Unhandled ConstantExpr: " << *CE << "\n";
02050     llvm_unreachable("Unhandled ConstantExpr");
02051   }
02052   return Dest;
02053 }
02054 
02055 GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) {
02056   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
02057     return getConstantExprValue(CE, SF);
02058   } else if (Constant *CPV = dyn_cast<Constant>(V)) {
02059     return getConstantValue(CPV);
02060   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
02061     return PTOGV(getPointerToGlobal(GV));
02062   } else {
02063     return SF.Values[V];
02064   }
02065 }
02066 
02067 //===----------------------------------------------------------------------===//
02068 //                        Dispatch and Execution Code
02069 //===----------------------------------------------------------------------===//
02070 
02071 //===----------------------------------------------------------------------===//
02072 // callFunction - Execute the specified function...
02073 //
02074 void Interpreter::callFunction(Function *F,
02075                                const std::vector<GenericValue> &ArgVals) {
02076   assert((ECStack.empty() || ECStack.back().Caller.getInstruction() == 0 ||
02077           ECStack.back().Caller.arg_size() == ArgVals.size()) &&
02078          "Incorrect number of arguments passed into function call!");
02079   // Make a new stack frame... and fill it in.
02080   ECStack.push_back(ExecutionContext());
02081   ExecutionContext &StackFrame = ECStack.back();
02082   StackFrame.CurFunction = F;
02083 
02084   // Special handling for external functions.
02085   if (F->isDeclaration()) {
02086     GenericValue Result = callExternalFunction (F, ArgVals);
02087     // Simulate a 'ret' instruction of the appropriate type.
02088     popStackAndReturnValueToCaller (F->getReturnType (), Result);
02089     return;
02090   }
02091 
02092   // Get pointers to first LLVM BB & Instruction in function.
02093   StackFrame.CurBB     = F->begin();
02094   StackFrame.CurInst   = StackFrame.CurBB->begin();
02095 
02096   // Run through the function arguments and initialize their values...
02097   assert((ArgVals.size() == F->arg_size() ||
02098          (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&&
02099          "Invalid number of values passed to function invocation!");
02100 
02101   // Handle non-varargs arguments...
02102   unsigned i = 0;
02103   for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); 
02104        AI != E; ++AI, ++i)
02105     SetValue(AI, ArgVals[i], StackFrame);
02106 
02107   // Handle varargs arguments...
02108   StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end());
02109 }
02110 
02111 
02112 void Interpreter::run() {
02113   while (!ECStack.empty()) {
02114     // Interpret a single instruction & increment the "PC".
02115     ExecutionContext &SF = ECStack.back();  // Current stack frame
02116     Instruction &I = *SF.CurInst++;         // Increment before execute
02117 
02118     // Track the number of dynamic instructions executed.
02119     ++NumDynamicInsts;
02120 
02121     DEBUG(dbgs() << "About to interpret: " << I);
02122     visit(I);   // Dispatch to one of the visit* methods...
02123 #if 0
02124     // This is not safe, as visiting the instruction could lower it and free I.
02125 DEBUG(
02126     if (!isa<CallInst>(I) && !isa<InvokeInst>(I) && 
02127         I.getType() != Type::VoidTy) {
02128       dbgs() << "  --> ";
02129       const GenericValue &Val = SF.Values[&I];
02130       switch (I.getType()->getTypeID()) {
02131       default: llvm_unreachable("Invalid GenericValue Type");
02132       case Type::VoidTyID:    dbgs() << "void"; break;
02133       case Type::FloatTyID:   dbgs() << "float " << Val.FloatVal; break;
02134       case Type::DoubleTyID:  dbgs() << "double " << Val.DoubleVal; break;
02135       case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal);
02136         break;
02137       case Type::IntegerTyID: 
02138         dbgs() << "i" << Val.IntVal.getBitWidth() << " "
02139                << Val.IntVal.toStringUnsigned(10)
02140                << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n";
02141         break;
02142       }
02143     });
02144 #endif
02145   }
02146 }