convertFromInteger, as originally written, expected sign-extended
[oota-llvm.git] / lib / ExecutionEngine / ExecutionEngine.cpp
index b1e8774a6b4965d60f852ca866e4bbb8c51989ce..96604f10bcccdefc675a39de000f4eff157f72e1 100644 (file)
@@ -231,7 +231,42 @@ int ExecutionEngine::runFunctionAsMain(Function *Fn,
   std::vector<GenericValue> GVArgs;
   GenericValue GVArgc;
   GVArgc.IntVal = APInt(32, argv.size());
+
+  // Check main() type
   unsigned NumArgs = Fn->getFunctionType()->getNumParams();
+  const FunctionType *FTy = Fn->getFunctionType();
+  const Type* PPInt8Ty = PointerType::get(PointerType::get(Type::Int8Ty));
+  switch (NumArgs) {
+  case 3:
+   if (FTy->getParamType(2) != PPInt8Ty) {
+     cerr << "Invalid type for third argument of main() supplied\n";
+     abort();
+   }
+   // FALLS THROUGH
+  case 2:
+   if (FTy->getParamType(1) != PPInt8Ty) {
+     cerr << "Invalid type for second argument of main() supplied\n";
+     abort();
+   }
+   // FALLS THROUGH
+  case 1:
+   if (FTy->getParamType(0) != Type::Int32Ty) {
+     cerr << "Invalid type for first argument of main() supplied\n";
+     abort();
+   }
+   // FALLS THROUGH
+  case 0:
+   if (FTy->getReturnType() != Type::Int32Ty &&
+       FTy->getReturnType() != Type::VoidTy) {
+     cerr << "Invalid return type of main() supplied\n";
+     abort();
+   }
+   break;
+  default:
+   cerr << "Invalid number of arguments of main() supplied\n";
+   abort();
+  }
+  
   if (NumArgs) {
     GVArgs.push_back(GVArgc); // Arg #0 = argc.
     if (NumArgs > 1) {
@@ -301,7 +336,7 @@ void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
 
 /// This function converts a Constant* into a GenericValue. The interesting 
 /// part is if C is a ConstantExpr.
-/// @brief Get a GenericValue for a Constnat*
+/// @brief Get a GenericValue for a Constant*
 GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
   // If its undefined, return the garbage.
   if (isa<UndefValue>(C)) 
@@ -341,11 +376,13 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
       return GV;
     }
     case Instruction::FPTrunc: {
+      // FIXME long double
       GenericValue GV = getConstantValue(Op0);
       GV.FloatVal = float(GV.DoubleVal);
       return GV;
     }
     case Instruction::FPExt:{
+      // FIXME long double
       GenericValue GV = getConstantValue(Op0);
       GV.DoubleVal = double(GV.FloatVal);
       return GV;
@@ -354,16 +391,32 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
       GenericValue GV = getConstantValue(Op0);
       if (CE->getType() == Type::FloatTy)
         GV.FloatVal = float(GV.IntVal.roundToDouble());
-      else
+      else if (CE->getType() == Type::DoubleTy)
         GV.DoubleVal = GV.IntVal.roundToDouble();
+      else if (CE->getType() == Type::X86_FP80Ty) {
+        const uint64_t zero[] = {0, 0};
+        APFloat apf = APFloat(APInt(80, 2, zero));
+        (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData(), 
+                               GV.IntVal.getBitWidth(), false,
+                               APFloat::rmNearestTiesToEven);
+        GV.IntVal = apf.convertToAPInt();
+      }
       return GV;
     }
     case Instruction::SIToFP: {
       GenericValue GV = getConstantValue(Op0);
       if (CE->getType() == Type::FloatTy)
         GV.FloatVal = float(GV.IntVal.signedRoundToDouble());
-      else
+      else if (CE->getType() == Type::DoubleTy)
         GV.DoubleVal = GV.IntVal.signedRoundToDouble();
+      else if (CE->getType() == Type::X86_FP80Ty) {
+        const uint64_t zero[] = { 0, 0};
+        APFloat apf = APFloat(APInt(80, 2, zero));
+        (void)apf.convertFromZeroExtendedInteger(GV.IntVal.getRawData(), 
+                               GV.IntVal.getBitWidth(), true,
+                               APFloat::rmNearestTiesToEven);
+        GV.IntVal = apf.convertToAPInt();
+      }
       return GV;
     }
     case Instruction::FPToUI: // double->APInt conversion handles sign
@@ -372,8 +425,16 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
       uint32_t BitWidth = cast<IntegerType>(CE->getType())->getBitWidth();
       if (Op0->getType() == Type::FloatTy)
         GV.IntVal = APIntOps::RoundFloatToAPInt(GV.FloatVal, BitWidth);
-      else
+      else if (Op0->getType() == Type::DoubleTy)
         GV.IntVal = APIntOps::RoundDoubleToAPInt(GV.DoubleVal, BitWidth);
+      else if (Op0->getType() == Type::X86_FP80Ty) {
+        APFloat apf = APFloat(GV.IntVal);
+        uint64_t v;
+        (void)apf.convertToInteger(&v, BitWidth,
+                                   CE->getOpcode()==Instruction::FPToSI, 
+                                   APFloat::rmTowardZero);
+        GV.IntVal = v; // endian?
+      }
       return GV;
     }
     case Instruction::PtrToInt: {
@@ -477,6 +538,35 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
             GV.DoubleVal = ::fmod(LHS.DoubleVal,RHS.DoubleVal); break;
         }
         break;
+      case Type::X86_FP80TyID:
+      case Type::PPC_FP128TyID:
+      case Type::FP128TyID: {
+        APFloat apfLHS = APFloat(LHS.IntVal);
+        switch (CE->getOpcode()) {
+          default: assert(0 && "Invalid long double opcode"); abort();
+          case Instruction::Add:  
+            apfLHS.add(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+            GV.IntVal = apfLHS.convertToAPInt();
+            break;
+          case Instruction::Sub:  
+            apfLHS.subtract(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+            GV.IntVal = apfLHS.convertToAPInt();
+            break;
+          case Instruction::Mul:  
+            apfLHS.multiply(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+            GV.IntVal = apfLHS.convertToAPInt();
+            break;
+          case Instruction::FDiv: 
+            apfLHS.divide(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+            GV.IntVal = apfLHS.convertToAPInt();
+            break;
+          case Instruction::FRem: 
+            apfLHS.mod(APFloat(RHS.IntVal), APFloat::rmNearestTiesToEven);
+            GV.IntVal = apfLHS.convertToAPInt();
+            break;
+          }
+        }
+        break;
       }
       return GV;
     }
@@ -490,10 +580,15 @@ GenericValue ExecutionEngine::getConstantValue(const Constant *C) {
   GenericValue Result;
   switch (C->getType()->getTypeID()) {
   case Type::FloatTyID: 
-    Result.FloatVal = (float)cast<ConstantFP>(C)->getValue(); 
+    Result.FloatVal = cast<ConstantFP>(C)->getValueAPF().convertToFloat(); 
     break;
   case Type::DoubleTyID:
-    Result.DoubleVal = (double)cast<ConstantFP>(C)->getValue(); 
+    Result.DoubleVal = cast<ConstantFP>(C)->getValueAPF().convertToDouble();
+    break;
+  case Type::X86_FP80TyID:
+  case Type::FP128TyID:
+  case Type::PPC_FP128TyID:
+    Result.IntVal = cast <ConstantFP>(C)->getValueAPF().convertToAPInt();
     break;
   case Type::IntegerTyID:
     Result.IntVal = cast<ConstantInt>(C)->getValue();
@@ -548,6 +643,17 @@ void ExecutionEngine::StoreValueToMemory(const GenericValue &Val, GenericValue *
   case Type::DoubleTyID:
     *((double*)Ptr) = Val.DoubleVal;
     break;
+  case Type::X86_FP80TyID: {
+      uint16_t *Dest = (uint16_t*)Ptr;
+      const uint16_t *Src = (uint16_t*)Val.IntVal.getRawData();
+      // This is endian dependent, but it will only work on x86 anyway.
+      Dest[0] = Src[4];
+      Dest[1] = Src[0];
+      Dest[2] = Src[1];
+      Dest[3] = Src[2];
+      Dest[4] = Src[3];
+      break;
+    }
   case Type::PointerTyID: 
     *((PointerTy*)Ptr) = Val.PointerVal;
     break;
@@ -573,7 +679,7 @@ void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
     } else if (BitWidth <= 64) {
       Result.IntVal = APInt(BitWidth, *((uint64_t*)Ptr));
     } else
-      Result.IntVal = APInt(BitWidth, BitWidth/64, (uint64_t*)Ptr);
+      Result.IntVal = APInt(BitWidth, (BitWidth+63)/64, (uint64_t*)Ptr);
     break;
   }
   case Type::FloatTyID:
@@ -585,6 +691,17 @@ void ExecutionEngine::LoadValueFromMemory(GenericValue &Result,
   case Type::PointerTyID: 
     Result.PointerVal = *((PointerTy*)Ptr);
     break;
+  case Type::X86_FP80TyID: {
+    // This is endian dependent, but it will only work on x86 anyway.
+    uint16_t x[8], *p = (uint16_t*)Ptr;
+    x[0] = p[1];
+    x[1] = p[2];
+    x[2] = p[3];
+    x[3] = p[4];
+    x[4] = p[0];
+    Result.IntVal = APInt(80, 2, x);
+    break;
+  }
   default:
     cerr << "Cannot load value of type " << *Ty << "!\n";
     abort();