Changes For Bug 352
[oota-llvm.git] / lib / ExecutionEngine / Interpreter / Execution.cpp
index 456e2a8f3cb86a9b79b029100831a2d8b996b233..5e1ae0629f8ee532379fb627ec6cf080b2740488 100644 (file)
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Instructions.h"
-#include "llvm/IntrinsicLowering.h"
+#include "llvm/CodeGen/IntrinsicLowering.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
-#include "Support/Statistic.h"
-#include "Support/Debug.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Support/Debug.h"
 #include <cmath>  // For fmod
 using namespace llvm;
 
@@ -66,7 +66,9 @@ static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
                                   const Type *Ty);
 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty);
-                                   
+static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, 
+                                      GenericValue Src3);
+
 GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
                                                 ExecutionContext &SF) {
   switch (CE->getOpcode()) {
@@ -139,9 +141,12 @@ GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE,
     return executeShrInst(getOperandValue(CE->getOperand(0), SF),
                           getOperandValue(CE->getOperand(1), SF),
                           CE->getOperand(0)->getType());
-  
+  case Instruction::Select:
+    return executeSelectInst(getOperandValue(CE->getOperand(0), SF),
+                             getOperandValue(CE->getOperand(1), SF),
+                             getOperandValue(CE->getOperand(2), SF));
   default:
-    std::cerr << "Unhandled ConstantExpr: " << CE << "\n";
+    std::cerr << "Unhandled ConstantExpr: " << *CE << "\n";
     abort();
     return GenericValue();
   }
@@ -177,7 +182,7 @@ void Interpreter::initializeExecutionEngine() {
 static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(+, UByte);
     IMPLEMENT_BINARY_OPERATOR(+, SByte);
     IMPLEMENT_BINARY_OPERATOR(+, UShort);
@@ -198,7 +203,7 @@ static GenericValue executeAddInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(-, UByte);
     IMPLEMENT_BINARY_OPERATOR(-, SByte);
     IMPLEMENT_BINARY_OPERATOR(-, UShort);
@@ -219,7 +224,7 @@ static GenericValue executeSubInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(*, UByte);
     IMPLEMENT_BINARY_OPERATOR(*, SByte);
     IMPLEMENT_BINARY_OPERATOR(*, UShort);
@@ -231,7 +236,7 @@ static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
     IMPLEMENT_BINARY_OPERATOR(*, Float);
     IMPLEMENT_BINARY_OPERATOR(*, Double);
   default:
-    std::cout << "Unhandled type for Mul instruction: " << Ty << "\n";
+    std::cout << "Unhandled type for Mul instruction: " << *Ty << "\n";
     abort();
   }
   return Dest;
@@ -240,7 +245,7 @@ static GenericValue executeMulInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(/, UByte);
     IMPLEMENT_BINARY_OPERATOR(/, SByte);
     IMPLEMENT_BINARY_OPERATOR(/, UShort);
@@ -261,7 +266,7 @@ static GenericValue executeDivInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(%, UByte);
     IMPLEMENT_BINARY_OPERATOR(%, SByte);
     IMPLEMENT_BINARY_OPERATOR(%, UShort);
@@ -286,7 +291,7 @@ static GenericValue executeRemInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(&, Bool);
     IMPLEMENT_BINARY_OPERATOR(&, UByte);
     IMPLEMENT_BINARY_OPERATOR(&, SByte);
@@ -306,7 +311,7 @@ static GenericValue executeAndInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2, 
                                   const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(|, Bool);
     IMPLEMENT_BINARY_OPERATOR(|, UByte);
     IMPLEMENT_BINARY_OPERATOR(|, SByte);
@@ -326,7 +331,7 @@ static GenericValue executeOrInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2, 
                                    const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_BINARY_OPERATOR(^, Bool);
     IMPLEMENT_BINARY_OPERATOR(^, UByte);
     IMPLEMENT_BINARY_OPERATOR(^, SByte);
@@ -358,7 +363,7 @@ static GenericValue executeXorInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2, 
                                     const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SETCC(==, UByte);
     IMPLEMENT_SETCC(==, SByte);
     IMPLEMENT_SETCC(==, UShort);
@@ -380,7 +385,7 @@ static GenericValue executeSetEQInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2, 
                                     const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SETCC(!=, UByte);
     IMPLEMENT_SETCC(!=, SByte);
     IMPLEMENT_SETCC(!=, UShort);
@@ -403,7 +408,7 @@ static GenericValue executeSetNEInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2, 
                                     const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SETCC(<=, UByte);
     IMPLEMENT_SETCC(<=, SByte);
     IMPLEMENT_SETCC(<=, UShort);
@@ -416,7 +421,7 @@ static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
     IMPLEMENT_SETCC(<=, Double);
     IMPLEMENT_POINTERSETCC(<=);
   default:
-    std::cout << "Unhandled type for SetLE instruction: " << Ty << "\n";
+    std::cout << "Unhandled type for SetLE instruction: " << *Ty << "\n";
     abort();
   }
   return Dest;
@@ -425,7 +430,7 @@ static GenericValue executeSetLEInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2, 
                                     const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SETCC(>=, UByte);
     IMPLEMENT_SETCC(>=, SByte);
     IMPLEMENT_SETCC(>=, UShort);
@@ -447,7 +452,7 @@ static GenericValue executeSetGEInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2, 
                                     const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SETCC(<, UByte);
     IMPLEMENT_SETCC(<, SByte);
     IMPLEMENT_SETCC(<, UShort);
@@ -469,7 +474,7 @@ static GenericValue executeSetLTInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeSetGTInst(GenericValue Src1, GenericValue Src2, 
                                     const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SETCC(>, UByte);
     IMPLEMENT_SETCC(>, SByte);
     IMPLEMENT_SETCC(>, UShort);
@@ -518,6 +523,21 @@ void Interpreter::visitBinaryOperator(BinaryOperator &I) {
   SetValue(&I, R, SF);
 }
 
+static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, 
+                                      GenericValue Src3) {
+  return Src1.BoolVal ? Src2 : Src3;
+}
+
+void Interpreter::visitSelectInst(SelectInst &I) {
+  ExecutionContext &SF = ECStack.back();
+  GenericValue Src1 = getOperandValue(I.getOperand(0), SF);
+  GenericValue Src2 = getOperandValue(I.getOperand(1), SF);
+  GenericValue Src3 = getOperandValue(I.getOperand(2), SF);
+  GenericValue R = executeSelectInst(Src1, Src2, Src3);
+  SetValue(&I, R, SF);
+}
+
+
 //===----------------------------------------------------------------------===//
 //                     Terminator Instruction Implementations
 //===----------------------------------------------------------------------===//
@@ -719,7 +739,7 @@ GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I,
       GenericValue IdxGV = getOperandValue(I.getOperand(), SF);
 
       uint64_t Idx;
-      switch (I.getOperand()->getType()->getPrimitiveID()) {
+      switch (I.getOperand()->getType()->getTypeID()) {
       default: assert(0 && "Illegal getelementptr index for sequential type!");
       case Type::SByteTyID:  Idx = IdxGV.SByteVal; break;
       case Type::ShortTyID:  Idx = IdxGV.ShortVal; break;
@@ -802,6 +822,7 @@ void Interpreter::visitCallSite(CallSite CS) {
         SF.CurInst = Prev;
         ++SF.CurInst;
       }
+      return;
     }
 
   SF.Caller = CS;
@@ -844,7 +865,7 @@ void Interpreter::visitCallSite(CallSite CS) {
 static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
                                    const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SHIFT(<<, UByte);
     IMPLEMENT_SHIFT(<<, SByte);
     IMPLEMENT_SHIFT(<<, UShort);
@@ -862,7 +883,7 @@ static GenericValue executeShlInst(GenericValue Src1, GenericValue Src2,
 static GenericValue executeShrInst(GenericValue Src1, GenericValue Src2,
                                    const Type *Ty) {
   GenericValue Dest;
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_SHIFT(>>, UByte);
     IMPLEMENT_SHIFT(>>, SByte);
     IMPLEMENT_SHIFT(>>, UShort);
@@ -903,7 +924,7 @@ void Interpreter::visitShr(ShiftInst &I) {
 
 #define IMPLEMENT_CAST_CASE_START(DESTTY, DESTCTY)    \
   case Type::DESTTY##TyID:                      \
-    switch (SrcTy->getPrimitiveID()) {          \
+    switch (SrcTy->getTypeID()) {          \
       IMPLEMENT_CAST(DESTTY, DESTCTY, Bool);    \
       IMPLEMENT_CAST(DESTTY, DESTCTY, UByte);   \
       IMPLEMENT_CAST(DESTTY, DESTCTY, SByte);   \
@@ -920,7 +941,7 @@ void Interpreter::visitShr(ShiftInst &I) {
       IMPLEMENT_CAST(DESTTY, DESTCTY, Double)
 
 #define IMPLEMENT_CAST_CASE_END()    \
-    default: std::cout << "Unhandled cast: " << SrcTy << " to " << Ty << "\n"; \
+    default: std::cout << "Unhandled cast: " << *SrcTy << " to " << *Ty << "\n"; \
       abort();                                  \
     }                                           \
     break
@@ -935,7 +956,7 @@ GenericValue Interpreter::executeCastOperation(Value *SrcVal, const Type *Ty,
   const Type *SrcTy = SrcVal->getType();
   GenericValue Dest, Src = getOperandValue(SrcVal, SF);
 
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_CAST_CASE(UByte  , (unsigned char));
     IMPLEMENT_CAST_CASE(SByte  , (  signed char));
     IMPLEMENT_CAST_CASE(UShort , (unsigned short));
@@ -986,7 +1007,7 @@ void Interpreter::visitVAArgInst(VAArgInst &I) {
   GenericValue Src = ECStack[VAList.UIntPairVal.first]
        .VarArgs[VAList.UIntPairVal.second];
   const Type *Ty = I.getType();
-  switch (Ty->getPrimitiveID()) {
+  switch (Ty->getTypeID()) {
     IMPLEMENT_VAARG(UByte);
     IMPLEMENT_VAARG(SByte);
     IMPLEMENT_VAARG(UShort);