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