LLVM API Documentation

Execution.cpp

Go to the documentation of this file.
00001 //===-- Execution.cpp - Implement code to simulate the program ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file contains the actual instruction interpreter.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "interpreter"
00015 #include "Interpreter.h"
00016 #include "llvm/Constants.h"
00017 #include "llvm/DerivedTypes.h"
00018 #include "llvm/Instructions.h"
00019 #include "llvm/CodeGen/IntrinsicLowering.h"
00020 #include "llvm/Support/GetElementPtrTypeIterator.h"
00021 #include "llvm/ADT/APInt.h"
00022 #include "llvm/ADT/Statistic.h"
00023 #include "llvm/Support/CommandLine.h"
00024 #include "llvm/Support/Debug.h"
00025 #include "llvm/Support/MathExtras.h"
00026 #include <algorithm>
00027 #include <cmath>
00028 #include <cstring>
00029 using namespace llvm;
00030 
00031 STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed");
00032 static Interpreter *TheEE = 0;
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 inline uint64_t doSignExtension(uint64_t Val, const IntegerType* ITy) {
00042   // Determine if the value is signed or not
00043   bool isSigned = (Val & (1 << (ITy->getBitWidth()-1))) != 0;
00044   // If its signed, extend the sign bits
00045   if (isSigned)
00046     Val |= ~ITy->getBitMask();
00047   return Val;
00048 }
00049 
00050 static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) {
00051   SF.Values[V] = Val;
00052 }
00053 
00054 void Interpreter::initializeExecutionEngine() {
00055   TheEE = this;
00056 }
00057 
00058 //===----------------------------------------------------------------------===//
00059 //                    Binary Instruction Implementations
00060 //===----------------------------------------------------------------------===//
00061 
00062 #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \
00063    case Type::TY##TyID: \
00064      Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \
00065      break
00066 
00067 #define IMPLEMENT_INTEGER_BINOP1(OP, TY) \
00068    case Type::IntegerTyID: { \
00069      Dest.IntVal = Src1.IntVal OP Src2.IntVal; \
00070      break; \
00071    }
00072 
00073 
00074 static void executeAddInst(GenericValue &Dest, GenericValue Src1, 
00075                            GenericValue Src2, const Type *Ty) {
00076   switch (Ty->getTypeID()) {
00077     IMPLEMENT_INTEGER_BINOP1(+, Ty);
00078     IMPLEMENT_BINARY_OPERATOR(+, Float);
00079     IMPLEMENT_BINARY_OPERATOR(+, Double);
00080   default:
00081     cerr << "Unhandled type for Add instruction: " << *Ty << "\n";
00082     abort();
00083   }
00084 }
00085 
00086 static void executeSubInst(GenericValue &Dest, GenericValue Src1, 
00087                            GenericValue Src2, const Type *Ty) {
00088   switch (Ty->getTypeID()) {
00089     IMPLEMENT_INTEGER_BINOP1(-, Ty);
00090     IMPLEMENT_BINARY_OPERATOR(-, Float);
00091     IMPLEMENT_BINARY_OPERATOR(-, Double);
00092   default:
00093     cerr << "Unhandled type for Sub instruction: " << *Ty << "\n";
00094     abort();
00095   }
00096 }
00097 
00098 static void executeMulInst(GenericValue &Dest, GenericValue Src1, 
00099                            GenericValue Src2, const Type *Ty) {
00100   switch (Ty->getTypeID()) {
00101     IMPLEMENT_INTEGER_BINOP1(*, Ty);
00102     IMPLEMENT_BINARY_OPERATOR(*, Float);
00103     IMPLEMENT_BINARY_OPERATOR(*, Double);
00104   default:
00105     cerr << "Unhandled type for Mul instruction: " << *Ty << "\n";
00106     abort();
00107   }
00108 }
00109 
00110 static void executeFDivInst(GenericValue &Dest, GenericValue Src1, 
00111                             GenericValue Src2, const Type *Ty) {
00112   switch (Ty->getTypeID()) {
00113     IMPLEMENT_BINARY_OPERATOR(/, Float);
00114     IMPLEMENT_BINARY_OPERATOR(/, Double);
00115   default:
00116     cerr << "Unhandled type for FDiv instruction: " << *Ty << "\n";
00117     abort();
00118   }
00119 }
00120 
00121 static void executeFRemInst(GenericValue &Dest, GenericValue Src1, 
00122                             GenericValue Src2, const Type *Ty) {
00123   switch (Ty->getTypeID()) {
00124   case Type::FloatTyID:
00125     Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal);
00126     break;
00127   case Type::DoubleTyID:
00128     Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal);
00129     break;
00130   default:
00131     cerr << "Unhandled type for Rem instruction: " << *Ty << "\n";
00132     abort();
00133   }
00134 }
00135 
00136 #define IMPLEMENT_INTEGER_ICMP(OP, TY) \
00137    case Type::IntegerTyID:  \
00138       Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \
00139       break;
00140 
00141 // Handle pointers specially because they must be compared with only as much
00142 // width as the host has.  We _do not_ want to be comparing 64 bit values when
00143 // running on a 32-bit target, otherwise the upper 32 bits might mess up
00144 // comparisons if they contain garbage.
00145 #define IMPLEMENT_POINTER_ICMP(OP) \
00146    case Type::PointerTyID: \
00147       Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \
00148                             (void*)(intptr_t)Src2.PointerVal); \
00149       break;
00150 
00151 static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2,
00152                                    const Type *Ty) {
00153   GenericValue Dest;
00154   switch (Ty->getTypeID()) {
00155     IMPLEMENT_INTEGER_ICMP(eq,Ty);
00156     IMPLEMENT_POINTER_ICMP(==);
00157   default:
00158     cerr << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n";
00159     abort();
00160   }
00161   return Dest;
00162 }
00163 
00164 static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2,
00165                                    const Type *Ty) {
00166   GenericValue Dest;
00167   switch (Ty->getTypeID()) {
00168     IMPLEMENT_INTEGER_ICMP(ne,Ty);
00169     IMPLEMENT_POINTER_ICMP(!=);
00170   default:
00171     cerr << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n";
00172     abort();
00173   }
00174   return Dest;
00175 }
00176 
00177 static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2,
00178                                     const Type *Ty) {
00179   GenericValue Dest;
00180   switch (Ty->getTypeID()) {
00181     IMPLEMENT_INTEGER_ICMP(ult,Ty);
00182     IMPLEMENT_POINTER_ICMP(<);
00183   default:
00184     cerr << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n";
00185     abort();
00186   }
00187   return Dest;
00188 }
00189 
00190 static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2,
00191                                     const Type *Ty) {
00192   GenericValue Dest;
00193   switch (Ty->getTypeID()) {
00194     IMPLEMENT_INTEGER_ICMP(slt,Ty);
00195     IMPLEMENT_POINTER_ICMP(<);
00196   default:
00197     cerr << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n";
00198     abort();
00199   }
00200   return Dest;
00201 }
00202 
00203 static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2,
00204                                     const Type *Ty) {
00205   GenericValue Dest;
00206   switch (Ty->getTypeID()) {
00207     IMPLEMENT_INTEGER_ICMP(ugt,Ty);
00208     IMPLEMENT_POINTER_ICMP(>);
00209   default:
00210     cerr << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n";
00211     abort();
00212   }
00213   return Dest;
00214 }
00215 
00216 static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2,
00217                                     const Type *Ty) {
00218   GenericValue Dest;
00219   switch (Ty->getTypeID()) {
00220     IMPLEMENT_INTEGER_ICMP(sgt,Ty);
00221     IMPLEMENT_POINTER_ICMP(>);
00222   default:
00223     cerr << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n";
00224     abort();
00225   }
00226   return Dest;
00227 }
00228 
00229 static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2,
00230                                     const Type *Ty) {
00231   GenericValue Dest;
00232   switch (Ty->getTypeID()) {
00233     IMPLEMENT_INTEGER_ICMP(ule,Ty);
00234     IMPLEMENT_POINTER_ICMP(<=);
00235   default:
00236     cerr << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n";
00237     abort();
00238   }
00239   return Dest;
00240 }
00241 
00242 static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2,
00243                                     const Type *Ty) {
00244   GenericValue Dest;
00245   switch (Ty->getTypeID()) {
00246     IMPLEMENT_INTEGER_ICMP(sle,Ty);
00247     IMPLEMENT_POINTER_ICMP(<=);
00248   default:
00249     cerr << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n";
00250     abort();
00251   }
00252   return Dest;
00253 }
00254 
00255 static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2,
00256                                     const Type *Ty) {
00257   GenericValue Dest;
00258   switch (Ty->getTypeID()) {
00259     IMPLEMENT_INTEGER_ICMP(uge,Ty);
00260     IMPLEMENT_POINTER_ICMP(>=);
00261   default:
00262     cerr << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n";
00263     abort();
00264   }
00265   return Dest;
00266 }
00267 
00268 static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2,
00269                                     const Type *Ty) {
00270   GenericValue Dest;
00271   switch (Ty->getTypeID()) {
00272     IMPLEMENT_INTEGER_ICMP(sge,Ty);
00273     IMPLEMENT_POINTER_ICMP(>=);
00274   default:
00275     cerr << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n";
00276     abort();
00277   }
00278   return Dest;
00279 }
00280 
00281 void Interpreter::visitICmpInst(ICmpInst &I) {
00282   ExecutionContext &SF = ECStack.back();
00283   const Type *Ty    = I.getOperand(0)->getType();
00284   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00285   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00286   GenericValue R;   // Result
00287   
00288   switch (I.getPredicate()) {
00289   case ICmpInst::ICMP_EQ:  R = executeICMP_EQ(Src1,  Src2, Ty); break;
00290   case ICmpInst::ICMP_NE:  R = executeICMP_NE(Src1,  Src2, Ty); break;
00291   case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break;
00292   case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break;
00293   case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break;
00294   case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break;
00295   case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break;
00296   case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break;
00297   case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break;
00298   case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break;
00299   default:
00300     cerr << "Don't know how to handle this ICmp predicate!\n-->" << I;
00301     abort();
00302   }
00303  
00304   SetValue(&I, R, SF);
00305 }
00306 
00307 #define IMPLEMENT_FCMP(OP, TY) \
00308    case Type::TY##TyID: \
00309      Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \
00310      break
00311 
00312 static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2,
00313                                    const Type *Ty) {
00314   GenericValue Dest;
00315   switch (Ty->getTypeID()) {
00316     IMPLEMENT_FCMP(==, Float);
00317     IMPLEMENT_FCMP(==, Double);
00318   default:
00319     cerr << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n";
00320     abort();
00321   }
00322   return Dest;
00323 }
00324 
00325 static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2,
00326                                    const Type *Ty) {
00327   GenericValue Dest;
00328   switch (Ty->getTypeID()) {
00329     IMPLEMENT_FCMP(!=, Float);
00330     IMPLEMENT_FCMP(!=, Double);
00331 
00332   default:
00333     cerr << "Unhandled type for FCmp NE instruction: " << *Ty << "\n";
00334     abort();
00335   }
00336   return Dest;
00337 }
00338 
00339 static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2,
00340                                    const Type *Ty) {
00341   GenericValue Dest;
00342   switch (Ty->getTypeID()) {
00343     IMPLEMENT_FCMP(<=, Float);
00344     IMPLEMENT_FCMP(<=, Double);
00345   default:
00346     cerr << "Unhandled type for FCmp LE instruction: " << *Ty << "\n";
00347     abort();
00348   }
00349   return Dest;
00350 }
00351 
00352 static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2,
00353                                    const Type *Ty) {
00354   GenericValue Dest;
00355   switch (Ty->getTypeID()) {
00356     IMPLEMENT_FCMP(>=, Float);
00357     IMPLEMENT_FCMP(>=, Double);
00358   default:
00359     cerr << "Unhandled type for FCmp GE instruction: " << *Ty << "\n";
00360     abort();
00361   }
00362   return Dest;
00363 }
00364 
00365 static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2,
00366                                    const Type *Ty) {
00367   GenericValue Dest;
00368   switch (Ty->getTypeID()) {
00369     IMPLEMENT_FCMP(<, Float);
00370     IMPLEMENT_FCMP(<, Double);
00371   default:
00372     cerr << "Unhandled type for FCmp LT instruction: " << *Ty << "\n";
00373     abort();
00374   }
00375   return Dest;
00376 }
00377 
00378 static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2,
00379                                      const Type *Ty) {
00380   GenericValue Dest;
00381   switch (Ty->getTypeID()) {
00382     IMPLEMENT_FCMP(>, Float);
00383     IMPLEMENT_FCMP(>, Double);
00384   default:
00385     cerr << "Unhandled type for FCmp GT instruction: " << *Ty << "\n";
00386     abort();
00387   }
00388   return Dest;
00389 }
00390 
00391 #define IMPLEMENT_UNORDERED(TY, X,Y)                                     \
00392   if (TY == Type::FloatTy) {                                             \
00393     if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) {          \
00394       Dest.IntVal = APInt(1,true);                                       \
00395       return Dest;                                                       \
00396     }                                                                    \
00397   } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \
00398     Dest.IntVal = APInt(1,true);                                         \
00399     return Dest;                                                         \
00400   }
00401 
00402 
00403 static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2,
00404                                    const Type *Ty) {
00405   GenericValue Dest;
00406   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00407   return executeFCMP_OEQ(Src1, Src2, Ty);
00408 }
00409 
00410 static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2,
00411                                    const Type *Ty) {
00412   GenericValue Dest;
00413   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00414   return executeFCMP_ONE(Src1, Src2, Ty);
00415 }
00416 
00417 static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2,
00418                                    const Type *Ty) {
00419   GenericValue Dest;
00420   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00421   return executeFCMP_OLE(Src1, Src2, Ty);
00422 }
00423 
00424 static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2,
00425                                    const Type *Ty) {
00426   GenericValue Dest;
00427   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00428   return executeFCMP_OGE(Src1, Src2, Ty);
00429 }
00430 
00431 static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2,
00432                                    const Type *Ty) {
00433   GenericValue Dest;
00434   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00435   return executeFCMP_OLT(Src1, Src2, Ty);
00436 }
00437 
00438 static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2,
00439                                      const Type *Ty) {
00440   GenericValue Dest;
00441   IMPLEMENT_UNORDERED(Ty, Src1, Src2)
00442   return executeFCMP_OGT(Src1, Src2, Ty);
00443 }
00444 
00445 static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2,
00446                                      const Type *Ty) {
00447   GenericValue Dest;
00448   if (Ty == Type::FloatTy)
00449     Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal && 
00450                            Src2.FloatVal == Src2.FloatVal));
00451   else
00452     Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal && 
00453                            Src2.DoubleVal == Src2.DoubleVal));
00454   return Dest;
00455 }
00456 
00457 static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2,
00458                                      const Type *Ty) {
00459   GenericValue Dest;
00460   if (Ty == Type::FloatTy)
00461     Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal || 
00462                            Src2.FloatVal != Src2.FloatVal));
00463   else
00464     Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal || 
00465                            Src2.DoubleVal != Src2.DoubleVal));
00466   return Dest;
00467 }
00468 
00469 void Interpreter::visitFCmpInst(FCmpInst &I) {
00470   ExecutionContext &SF = ECStack.back();
00471   const Type *Ty    = I.getOperand(0)->getType();
00472   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00473   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00474   GenericValue R;   // Result
00475   
00476   switch (I.getPredicate()) {
00477   case FCmpInst::FCMP_FALSE: R.IntVal = APInt(1,false); break;
00478   case FCmpInst::FCMP_TRUE:  R.IntVal = APInt(1,true); break;
00479   case FCmpInst::FCMP_ORD:   R = executeFCMP_ORD(Src1, Src2, Ty); break;
00480   case FCmpInst::FCMP_UNO:   R = executeFCMP_UNO(Src1, Src2, Ty); break;
00481   case FCmpInst::FCMP_UEQ:   R = executeFCMP_UEQ(Src1, Src2, Ty); break;
00482   case FCmpInst::FCMP_OEQ:   R = executeFCMP_OEQ(Src1, Src2, Ty); break;
00483   case FCmpInst::FCMP_UNE:   R = executeFCMP_UNE(Src1, Src2, Ty); break;
00484   case FCmpInst::FCMP_ONE:   R = executeFCMP_ONE(Src1, Src2, Ty); break;
00485   case FCmpInst::FCMP_ULT:   R = executeFCMP_ULT(Src1, Src2, Ty); break;
00486   case FCmpInst::FCMP_OLT:   R = executeFCMP_OLT(Src1, Src2, Ty); break;
00487   case FCmpInst::FCMP_UGT:   R = executeFCMP_UGT(Src1, Src2, Ty); break;
00488   case FCmpInst::FCMP_OGT:   R = executeFCMP_OGT(Src1, Src2, Ty); break;
00489   case FCmpInst::FCMP_ULE:   R = executeFCMP_ULE(Src1, Src2, Ty); break;
00490   case FCmpInst::FCMP_OLE:   R = executeFCMP_OLE(Src1, Src2, Ty); break;
00491   case FCmpInst::FCMP_UGE:   R = executeFCMP_UGE(Src1, Src2, Ty); break;
00492   case FCmpInst::FCMP_OGE:   R = executeFCMP_OGE(Src1, Src2, Ty); break;
00493   default:
00494     cerr << "Don't know how to handle this FCmp predicate!\n-->" << I;
00495     abort();
00496   }
00497  
00498   SetValue(&I, R, SF);
00499 }
00500 
00501 static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, 
00502                                    GenericValue Src2, const Type *Ty) {
00503   GenericValue Result;
00504   switch (predicate) {
00505   case ICmpInst::ICMP_EQ:    return executeICMP_EQ(Src1, Src2, Ty);
00506   case ICmpInst::ICMP_NE:    return executeICMP_NE(Src1, Src2, Ty);
00507   case ICmpInst::ICMP_UGT:   return executeICMP_UGT(Src1, Src2, Ty);
00508   case ICmpInst::ICMP_SGT:   return executeICMP_SGT(Src1, Src2, Ty);
00509   case ICmpInst::ICMP_ULT:   return executeICMP_ULT(Src1, Src2, Ty);
00510   case ICmpInst::ICMP_SLT:   return executeICMP_SLT(Src1, Src2, Ty);
00511   case ICmpInst::ICMP_UGE:   return executeICMP_UGE(Src1, Src2, Ty);
00512   case ICmpInst::ICMP_SGE:   return executeICMP_SGE(Src1, Src2, Ty);
00513   case ICmpInst::ICMP_ULE:   return executeICMP_ULE(Src1, Src2, Ty);
00514   case ICmpInst::ICMP_SLE:   return executeICMP_SLE(Src1, Src2, Ty);
00515   case FCmpInst::FCMP_ORD:   return executeFCMP_ORD(Src1, Src2, Ty);
00516   case FCmpInst::FCMP_UNO:   return executeFCMP_UNO(Src1, Src2, Ty);
00517   case FCmpInst::FCMP_OEQ:   return executeFCMP_OEQ(Src1, Src2, Ty);
00518   case FCmpInst::FCMP_UEQ:   return executeFCMP_UEQ(Src1, Src2, Ty);
00519   case FCmpInst::FCMP_ONE:   return executeFCMP_ONE(Src1, Src2, Ty);
00520   case FCmpInst::FCMP_UNE:   return executeFCMP_UNE(Src1, Src2, Ty);
00521   case FCmpInst::FCMP_OLT:   return executeFCMP_OLT(Src1, Src2, Ty);
00522   case FCmpInst::FCMP_ULT:   return executeFCMP_ULT(Src1, Src2, Ty);
00523   case FCmpInst::FCMP_OGT:   return executeFCMP_OGT(Src1, Src2, Ty);
00524   case FCmpInst::FCMP_UGT:   return executeFCMP_UGT(Src1, Src2, Ty);
00525   case FCmpInst::FCMP_OLE:   return executeFCMP_OLE(Src1, Src2, Ty);
00526   case FCmpInst::FCMP_ULE:   return executeFCMP_ULE(Src1, Src2, Ty);
00527   case FCmpInst::FCMP_OGE:   return executeFCMP_OGE(Src1, Src2, Ty);
00528   case FCmpInst::FCMP_UGE:   return executeFCMP_UGE(Src1, Src2, Ty);
00529   case FCmpInst::FCMP_FALSE: { 
00530     GenericValue Result;
00531     Result.IntVal = APInt(1, false);
00532     return Result;
00533   }
00534   case FCmpInst::FCMP_TRUE: {
00535     GenericValue Result;
00536     Result.IntVal = APInt(1, true);
00537     return Result;
00538   }
00539   default:
00540     cerr << "Unhandled Cmp predicate\n";
00541     abort();
00542   }
00543 }
00544 
00545 void Interpreter::visitBinaryOperator(BinaryOperator &I) {
00546   ExecutionContext &SF = ECStack.back();
00547   const Type *Ty    = I.getOperand(0)->getType();
00548   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00549   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00550   GenericValue R;   // Result
00551 
00552   switch (I.getOpcode()) {
00553   case Instruction::Add:   executeAddInst  (R, Src1, Src2, Ty); break;
00554   case Instruction::Sub:   executeSubInst  (R, Src1, Src2, Ty); break;
00555   case Instruction::Mul:   executeMulInst  (R, Src1, Src2, Ty); break;
00556   case Instruction::FDiv:  executeFDivInst (R, Src1, Src2, Ty); break;
00557   case Instruction::FRem:  executeFRemInst (R, Src1, Src2, Ty); break;
00558   case Instruction::UDiv:  R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break;
00559   case Instruction::SDiv:  R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break;
00560   case Instruction::URem:  R.IntVal = Src1.IntVal.urem(Src2.IntVal); break;
00561   case Instruction::SRem:  R.IntVal = Src1.IntVal.srem(Src2.IntVal); break;
00562   case Instruction::And:   R.IntVal = Src1.IntVal & Src2.IntVal; break;
00563   case Instruction::Or:    R.IntVal = Src1.IntVal | Src2.IntVal; break;
00564   case Instruction::Xor:   R.IntVal = Src1.IntVal ^ Src2.IntVal; break;
00565   default:
00566     cerr << "Don't know how to handle this binary operator!\n-->" << I;
00567     abort();
00568   }
00569 
00570   SetValue(&I, R, SF);
00571 }
00572 
00573 static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2,
00574                                       GenericValue Src3) {
00575   return Src1.IntVal == 0 ? Src3 : Src2;
00576 }
00577 
00578 void Interpreter::visitSelectInst(SelectInst &I) {
00579   ExecutionContext &SF = ECStack.back();
00580   GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
00581   GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
00582   GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
00583   GenericValue R = executeSelectInst(Src1, Src2, Src3);
00584   SetValue(&I, R, SF);
00585 }
00586 
00587 
00588 //===----------------------------------------------------------------------===//
00589 //                     Terminator Instruction Implementations
00590 //===----------------------------------------------------------------------===//
00591 
00592 void Interpreter::exitCalled(GenericValue GV) {
00593   // runAtExitHandlers() assumes there are no stack frames, but
00594   // if exit() was called, then it had a stack frame. Blow away
00595   // the stack before interpreting atexit handlers.
00596   ECStack.clear ();
00597   runAtExitHandlers ();
00598   exit (GV.IntVal.zextOrTrunc(32).getZExtValue());
00599 }
00600 
00601 /// Pop the last stack frame off of ECStack and then copy the result
00602 /// back into the result variable if we are not returning void. The
00603 /// result variable may be the ExitValue, or the Value of the calling
00604 /// CallInst if there was a previous stack frame. This method may
00605 /// invalidate any ECStack iterators you have. This method also takes
00606 /// care of switching to the normal destination BB, if we are returning
00607 /// from an invoke.
00608 ///
00609 void Interpreter::popStackAndReturnValueToCaller (const Type *RetTy,
00610                                                   GenericValue Result) {
00611   // Pop the current stack frame.
00612   ECStack.pop_back();
00613 
00614   if (ECStack.empty()) {  // Finished main.  Put result into exit code...
00615     if (RetTy && RetTy->isInteger()) {          // Nonvoid return type?
00616       ExitValue = Result;   // Capture the exit value of the program
00617     } else {
00618       memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped));
00619     }
00620   } else {
00621     // If we have a previous stack frame, and we have a previous call,
00622     // fill in the return value...
00623     ExecutionContext &CallingSF = ECStack.back();
00624     if (Instruction *I = CallingSF.Caller.getInstruction()) {
00625       if (CallingSF.Caller.getType() != Type::VoidTy)      // Save result...
00626         SetValue(I, Result, CallingSF);
00627       if (InvokeInst *II = dyn_cast<InvokeInst> (I))
00628         SwitchToNewBasicBlock (II->getNormalDest (), CallingSF);
00629       CallingSF.Caller = CallSite();          // We returned from the call...
00630     }
00631   }
00632 }
00633 
00634 void Interpreter::visitReturnInst(ReturnInst &I) {
00635   ExecutionContext &SF = ECStack.back();
00636   const Type *RetTy = Type::VoidTy;
00637   GenericValue Result;
00638 
00639   // Save away the return value... (if we are not 'ret void')
00640   if (I.getNumOperands()) {
00641     RetTy  = I.getReturnValue()->getType();
00642     Result = getOperandValue(I.getReturnValue(), SF);
00643   }
00644 
00645   popStackAndReturnValueToCaller(RetTy, Result);
00646 }
00647 
00648 void Interpreter::visitUnwindInst(UnwindInst &I) {
00649   // Unwind stack
00650   Instruction *Inst;
00651   do {
00652     ECStack.pop_back ();
00653     if (ECStack.empty ())
00654       abort ();
00655     Inst = ECStack.back ().Caller.getInstruction ();
00656   } while (!(Inst && isa<InvokeInst> (Inst)));
00657 
00658   // Return from invoke
00659   ExecutionContext &InvokingSF = ECStack.back ();
00660   InvokingSF.Caller = CallSite ();
00661 
00662   // Go to exceptional destination BB of invoke instruction
00663   SwitchToNewBasicBlock(cast<InvokeInst>(Inst)->getUnwindDest(), InvokingSF);
00664 }
00665 
00666 void Interpreter::visitUnreachableInst(UnreachableInst &I) {
00667   cerr << "ERROR: Program executed an 'unreachable' instruction!\n";
00668   abort();
00669 }
00670 
00671 void Interpreter::visitBranchInst(BranchInst &I) {
00672   ExecutionContext &SF = ECStack.back();
00673   BasicBlock *Dest;
00674 
00675   Dest = I.getSuccessor(0);          // Uncond branches have a fixed dest...
00676   if (!I.isUnconditional()) {
00677     Value *Cond = I.getCondition();
00678     if (getOperandValue(Cond, SF).IntVal == 0) // If false cond...
00679       Dest = I.getSuccessor(1);
00680   }
00681   SwitchToNewBasicBlock(Dest, SF);
00682 }
00683 
00684 void Interpreter::visitSwitchInst(SwitchInst &I) {
00685   ExecutionContext &SF = ECStack.back();
00686   GenericValue CondVal = getOperandValue(I.getOperand(0), SF);
00687   const Type *ElTy = I.getOperand(0)->getType();
00688 
00689   // Check to see if any of the cases match...
00690   BasicBlock *Dest = 0;
00691   for (unsigned i = 2, e = I.getNumOperands(); i != e; i += 2)
00692     if (executeICMP_EQ(CondVal, getOperandValue(I.getOperand(i), SF), ElTy)
00693         .IntVal != 0) {
00694       Dest = cast<BasicBlock>(I.getOperand(i+1));
00695       break;
00696     }
00697 
00698   if (!Dest) Dest = I.getDefaultDest();   // No cases matched: use default
00699   SwitchToNewBasicBlock(Dest, SF);
00700 }
00701 
00702 // SwitchToNewBasicBlock - This method is used to jump to a new basic block.
00703 // This function handles the actual updating of block and instruction iterators
00704 // as well as execution of all of the PHI nodes in the destination block.
00705 //
00706 // This method does this because all of the PHI nodes must be executed
00707 // atomically, reading their inputs before any of the results are updated.  Not
00708 // doing this can cause problems if the PHI nodes depend on other PHI nodes for
00709 // their inputs.  If the input PHI node is updated before it is read, incorrect
00710 // results can happen.  Thus we use a two phase approach.
00711 //
00712 void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){
00713   BasicBlock *PrevBB = SF.CurBB;      // Remember where we came from...
00714   SF.CurBB   = Dest;                  // Update CurBB to branch destination
00715   SF.CurInst = SF.CurBB->begin();     // Update new instruction ptr...
00716 
00717   if (!isa<PHINode>(SF.CurInst)) return;  // Nothing fancy to do
00718 
00719   // Loop over all of the PHI nodes in the current block, reading their inputs.
00720   std::vector<GenericValue> ResultValues;
00721 
00722   for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) {
00723     // Search for the value corresponding to this previous bb...
00724     int i = PN->getBasicBlockIndex(PrevBB);
00725     assert(i != -1 && "PHINode doesn't contain entry for predecessor??");
00726     Value *IncomingValue = PN->getIncomingValue(i);
00727 
00728     // Save the incoming value for this PHI node...
00729     ResultValues.push_back(getOperandValue(IncomingValue, SF));
00730   }
00731 
00732   // Now loop over all of the PHI nodes setting their values...
00733   SF.CurInst = SF.CurBB->begin();
00734   for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) {
00735     PHINode *PN = cast<PHINode>(SF.CurInst);
00736     SetValue(PN, ResultValues[i], SF);
00737   }
00738 }
00739 
00740 //===----------------------------------------------------------------------===//
00741 //                     Memory Instruction Implementations
00742 //===----------------------------------------------------------------------===//
00743 
00744 void Interpreter::visitAllocationInst(AllocationInst &I) {
00745   ExecutionContext &SF = ECStack.back();
00746 
00747   const Type *Ty = I.getType()->getElementType();  // Type to be allocated
00748 
00749   // Get the number of elements being allocated by the array...
00750   unsigned NumElements = 
00751     getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue();
00752 
00753   unsigned TypeSize = (size_t)TD.getABITypeSize(Ty);
00754 
00755   // Avoid malloc-ing zero bytes, use max()...
00756   unsigned MemToAlloc = std::max(1