[llvm-commits] [llvm] r41875 - in /llvm/trunk: lib/AsmParser/Lexer.l lib/AsmParser/llvmAsmParser.y lib/Bitcode/Reader/BitcodeReader.cpp lib/Bitcode/Writer/BitcodeWriter.cpp lib/CodeGen/AsmPrinter.cpp lib/CodeGen/MachOWriter.cpp lib/CodeGen/SelectionDAG/DAGCombiner.cpp lib/CodeGen/SelectionDAG/LegalizeDAG.cpp lib/CodeGen/SelectionDAG/SelectionDAG.cpp lib/Target/CBackend/CBackend.cpp lib/Target/MSIL/MSILWriter.cpp lib/VMCore/AsmWriter.cpp lib/VMCore/ConstantFold.cpp lib/VMCore/Constants.cpp tools/llvm2cpp/CppWriter.cpp

Dale Johannesen dalej at apple.com
Tue Sep 11 20:30:34 PDT 2007


Author: johannes
Date: Tue Sep 11 22:30:33 2007
New Revision: 41875

URL: http://llvm.org/viewvc/llvm-project?rev=41875&view=rev
Log:
Revise previous patch per review comments.
Next round of x87 long double stuff.
Getting close now, basically works.


Modified:
    llvm/trunk/lib/AsmParser/Lexer.l
    llvm/trunk/lib/AsmParser/llvmAsmParser.y
    llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/CodeGen/AsmPrinter.cpp
    llvm/trunk/lib/CodeGen/MachOWriter.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
    llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
    llvm/trunk/lib/Target/CBackend/CBackend.cpp
    llvm/trunk/lib/Target/MSIL/MSILWriter.cpp
    llvm/trunk/lib/VMCore/AsmWriter.cpp
    llvm/trunk/lib/VMCore/ConstantFold.cpp
    llvm/trunk/lib/VMCore/Constants.cpp
    llvm/trunk/tools/llvm2cpp/CppWriter.cpp

Modified: llvm/trunk/lib/AsmParser/Lexer.l
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/Lexer.l?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/AsmParser/Lexer.l (original)
+++ llvm/trunk/lib/AsmParser/Lexer.l Tue Sep 11 22:30:33 2007
@@ -284,8 +284,8 @@
 float           { RET_TY(Type::FloatTy, FLOAT); }
 double          { RET_TY(Type::DoubleTy,DOUBLE);}
 x86_fp80        { RET_TY(Type::X86_FP80Ty, X86_FP80);}
-fp128            { RET_TY(Type::FP128Ty, FP128);}
-ppc_fp128         { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
+fp128           { RET_TY(Type::FP128Ty, FP128);}
+ppc_fp128       { RET_TY(Type::PPC_FP128Ty, PPC_FP128);}
 label           { RET_TY(Type::LabelTy, LABEL); }
 type            { return TYPE;   }
 opaque          { return OPAQUE; }
@@ -477,21 +477,21 @@
                 }
 
 {FPConstant}    { llvmAsmlval.FPVal = new APFloat(atof(yytext)); return FPVAL; }
-{HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext)); 
+{HexFPConstant} { llvmAsmlval.FPVal = new APFloat(HexToFP(yytext+2)); 
                   return FPVAL; 
                 }
 {HexFP80Constant} { uint64_t Pair[2];
-                    HexToIntPair(yytext, Pair);
+                    HexToIntPair(yytext+3, Pair);
                     llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
                     return FPVAL;
                 }
 {HexFP128Constant} { uint64_t Pair[2];
-                    HexToIntPair(yytext, Pair);
+                    HexToIntPair(yytext+3, Pair);
                     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
                     return FPVAL;
                 }
 {HexPPC128Constant} { uint64_t Pair[2];
-                    HexToIntPair(yytext, Pair);
+                    HexToIntPair(yytext+3, Pair);
                     llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
                     return FPVAL;
                 }

Modified: llvm/trunk/lib/AsmParser/llvmAsmParser.y
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/llvmAsmParser.y?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/AsmParser/llvmAsmParser.y (original)
+++ llvm/trunk/lib/AsmParser/llvmAsmParser.y Tue Sep 11 22:30:33 2007
@@ -1866,7 +1866,7 @@
     $$ = ConstantInt::getFalse();
     CHECK_FOR_ERROR
   }
-  | FPType FPVAL {                   // Float & Double constants
+  | FPType FPVAL {                   // Floating point constants
     if (!ConstantFP::isValueValidForType($1, *$2))
       GEN_ERROR("Floating point constant invalid for type");
     // Lexer has no type info, so builds all float and double FP constants 

Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Tue Sep 11 22:30:33 2007
@@ -629,7 +629,6 @@
         V = ConstantFP::get(CurTy, APFloat(APInt(32, (uint32_t)Record[0])));
       else if (CurTy == Type::DoubleTy)
         V = ConstantFP::get(CurTy, APFloat(APInt(64, Record[0])));
-      // FIXME: Make long double constants work.  BitsToDouble does not make it.
       else if (CurTy == Type::X86_FP80Ty)
         V = ConstantFP::get(CurTy, APFloat(APInt(80, 2, &Record[0])));
       else if (CurTy == Type::FP128Ty)

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Tue Sep 11 22:30:33 2007
@@ -526,11 +526,8 @@
     } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
       Code = bitc::CST_CODE_FLOAT;
       const Type *Ty = CFP->getType();
-      if (Ty == Type::FloatTy)
-        Record.push_back((uint32_t)*CFP->getValueAPF().convertToAPInt().
-                                      getRawData());
-      else if (Ty == Type::DoubleTy) {
-        Record.push_back(*CFP->getValueAPF().convertToAPInt().getRawData());
+      if (Ty == Type::FloatTy || Ty == Type::DoubleTy) {
+        Record.push_back(CFP->getValueAPF().convertToAPInt().getZExtValue());
       } else if (Ty == Type::X86_FP80Ty) {
         const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
         Record.push_back(p[0]);

Modified: llvm/trunk/lib/CodeGen/AsmPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/AsmPrinter.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/AsmPrinter.cpp (original)
+++ llvm/trunk/lib/CodeGen/AsmPrinter.cpp Tue Sep 11 22:30:33 2007
@@ -831,7 +831,7 @@
     // precision...
     if (CFP->getType() == Type::DoubleTy) {
       double Val = CFP->getValueAPF().convertToDouble();  // for comment only
-      uint64_t i = *CFP->getValueAPF().convertToAPInt().getRawData();
+      uint64_t i = CFP->getValueAPF().convertToAPInt().getZExtValue();
       if (TAI->getData64bitsDirective())
         O << TAI->getData64bitsDirective() << i << "\t"
           << TAI->getCommentString() << " double value: " << Val << "\n";
@@ -851,13 +851,50 @@
           << " double most significant word " << Val << "\n";
       }
       return;
-    } else {
+    } else if (CFP->getType() == Type::FloatTy) {
       float Val = CFP->getValueAPF().convertToFloat();  // for comment only
       O << TAI->getData32bitsDirective()
-        << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData()
+        << CFP->getValueAPF().convertToAPInt().getZExtValue()
         << "\t" << TAI->getCommentString() << " float " << Val << "\n";
       return;
-    }
+    } else if (CFP->getType() == Type::X86_FP80Ty) {
+      // all long double variants are printed as hex
+      const uint64_t *p = CFP->getValueAPF().convertToAPInt().getRawData();
+      if (TD->isBigEndian()) {
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
+          << "\t" << TAI->getCommentString()
+          << " long double most significant halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0])
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[1])
+          << "\t" << TAI->getCommentString()
+          << " long double least significant halfword\n";
+       } else {
+        O << TAI->getData16bitsDirective() << uint16_t(p[1])
+          << "\t" << TAI->getCommentString()
+          << " long double least significant halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0])
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 16)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 32)
+          << "\t" << TAI->getCommentString()
+          << " long double next halfword\n";
+        O << TAI->getData16bitsDirective() << uint16_t(p[0] >> 48)
+          << "\t" << TAI->getCommentString()
+          << " long double most significant halfword\n";
+      }
+      return;
+    } else assert(0 && "Floating point constant type not handled");
   } else if (CV->getType() == Type::Int64Ty) {
     if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
       uint64_t Val = CI->getZExtValue();

Modified: llvm/trunk/lib/CodeGen/MachOWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachOWriter.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/MachOWriter.cpp (original)
+++ llvm/trunk/lib/CodeGen/MachOWriter.cpp Tue Sep 11 22:30:33 2007
@@ -861,8 +861,8 @@
         break;
       }
       case Type::FloatTyID: {
-        uint32_t val = (uint32_t)*cast<ConstantFP>(PC)->
-                                  getValueAPF().convertToAPInt().getRawData();
+        uint32_t val = cast<ConstantFP>(PC)->getValueAPF().convertToAPInt().
+                        getZExtValue();
         if (TD->isBigEndian())
           val = ByteSwap_32(val);
         ptr[0] = val;
@@ -872,8 +872,8 @@
         break;
       }
       case Type::DoubleTyID: {
-        uint64_t val = *cast<ConstantFP>(PC)->getValueAPF().convertToAPInt().
-                         getRawData();
+        uint64_t val = cast<ConstantFP>(PC)->getValueAPF().convertToAPInt().
+                         getZExtValue();
         if (TD->isBigEndian())
           val = ByteSwap_64(val);
         ptr[0] = val;

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/DAGCombiner.cpp Tue Sep 11 22:30:33 2007
@@ -3800,8 +3800,8 @@
       default: assert(0 && "Unknown FP type");
       case MVT::f32:
         if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
-          Tmp = DAG.getConstant((uint32_t)*CFP->getValueAPF().
-                              convertToAPInt().getRawData(), MVT::i32);
+          Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
+                              convertToAPInt().getZExtValue(), MVT::i32);
           return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
                               ST->getSrcValueOffset(), ST->isVolatile(),
                               ST->getAlignment());
@@ -3809,8 +3809,8 @@
         break;
       case MVT::f64:
         if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
-          Tmp = DAG.getConstant(*CFP->getValueAPF().convertToAPInt().
-                                  getRawData(), MVT::i64);
+          Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
+                                  getZExtValue(), MVT::i64);
           return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
                               ST->getSrcValueOffset(), ST->isVolatile(),
                               ST->getAlignment());
@@ -3818,7 +3818,7 @@
           // Many FP stores are not make apparent until after legalize, e.g. for
           // argument passing.  Since this is so common, custom legalize the
           // 64-bit integer store into two 32-bit stores.
-          uint64_t Val = *CFP->getValueAPF().convertToAPInt().getRawData();
+          uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
           SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
           SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
           if (!TLI.isLittleEndian()) std::swap(Lo, Hi);

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp Tue Sep 11 22:30:33 2007
@@ -489,11 +489,8 @@
   ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
                                       Type::FloatTy, CFP->getValueAPF());
   if (!UseCP) {
-    const APFloat& Val = LLVMC->getValueAPF();
-    return isDouble
-      ? DAG.getConstant(*Val.convertToAPInt().getRawData(), MVT::i64)
-      : DAG.getConstant((uint32_t )*Val.convertToAPInt().getRawData(), 
-                        MVT::i32);
+    return DAG.getConstant(LLVMC->getValueAPF().convertToAPInt().getZExtValue(),
+                           isDouble ? MVT::i64 : MVT::i32);
   }
 
   if (isDouble && CFP->isValueValidForType(MVT::f32, CFP->getValueAPF()) &&
@@ -1981,13 +1978,13 @@
       // together.
       if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
         if (CFP->getValueType(0) == MVT::f32) {
-          Tmp3 = DAG.getConstant((uint32_t)*CFP->getValueAPF().
-                                          convertToAPInt().getRawData(),
+          Tmp3 = DAG.getConstant((uint32_t)CFP->getValueAPF().
+                                          convertToAPInt().getZExtValue(),
                                   MVT::i32);
         } else {
           assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
-          Tmp3 = DAG.getConstant(*CFP->getValueAPF().convertToAPInt().
-                                   getRawData(), MVT::i64);
+          Tmp3 = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
+                                   getZExtValue(), MVT::i64);
         }
         Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
                               SVOffset, isVolatile, Alignment);

Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Tue Sep 11 22:30:33 2007
@@ -109,12 +109,12 @@
   } else if (isa<ConstantFPSDNode>(NotZero)) {
     MVT::ValueType VT = NotZero.getValueType();
     if (VT== MVT::f64) {
-      if (*((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
-                  convertToAPInt().getRawData())) != (uint64_t)-1)
+      if (((cast<ConstantFPSDNode>(NotZero)->getValueAPF().
+                  convertToAPInt().getZExtValue())) != (uint64_t)-1)
         return false;
     } else {
-      if ((uint32_t)*cast<ConstantFPSDNode>(NotZero)->
-                      getValueAPF().convertToAPInt().getRawData() != 
+      if ((uint32_t)cast<ConstantFPSDNode>(NotZero)->
+                      getValueAPF().convertToAPInt().getZExtValue() != 
           (uint32_t)-1)
         return false;
     }
@@ -1697,9 +1697,9 @@
     }
     case ISD::BIT_CONVERT:
       if (VT == MVT::i32 && C->getValueType(0) == MVT::f32)
-        return getConstant((uint32_t)*V.convertToAPInt().getRawData(), VT);
+        return getConstant((uint32_t)V.convertToAPInt().getZExtValue(), VT);
       else if (VT == MVT::i64 && C->getValueType(0) == MVT::f64)
-        return getConstant(*V.convertToAPInt().getRawData(), VT);
+        return getConstant(V.convertToAPInt().getZExtValue(), VT);
       break;
     }
   }

Modified: llvm/trunk/lib/Target/CBackend/CBackend.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/CBackend/CBackend.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/Target/CBackend/CBackend.cpp (original)
+++ llvm/trunk/lib/Target/CBackend/CBackend.cpp Tue Sep 11 22:30:33 2007
@@ -1715,14 +1715,14 @@
 
         if (FPC->getType() == Type::DoubleTy) {
           double Val = FPC->getValueAPF().convertToDouble();
-          uint64_t i = *FPC->getValueAPF().convertToAPInt().getRawData();
+          uint64_t i = FPC->getValueAPF().convertToAPInt().getZExtValue();
           Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
               << " = 0x" << std::hex << i << std::dec
               << "ULL;    /* " << Val << " */\n";
         } else if (FPC->getType() == Type::FloatTy) {
           float Val = FPC->getValueAPF().convertToFloat();
-          uint32_t i = (uint32_t)*FPC->getValueAPF().convertToAPInt().
-                                    getRawData();
+          uint32_t i = (uint32_t)FPC->getValueAPF().convertToAPInt().
+                                    getZExtValue();
           Out << "static const ConstantFloatTy FPConstant" << FPCounter++
               << " = 0x" << std::hex << i << std::dec
               << "U;    /* " << Val << " */\n";

Modified: llvm/trunk/lib/Target/MSIL/MSILWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/MSIL/MSILWriter.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/Target/MSIL/MSILWriter.cpp (original)
+++ llvm/trunk/lib/Target/MSIL/MSILWriter.cpp Tue Sep 11 22:30:33 2007
@@ -428,10 +428,10 @@
     uint64_t X;
     unsigned Size;
     if (FP->getType()->getTypeID()==Type::FloatTyID) {
-      X = (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData();
+      X = (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue();
       Size = 4;  
     } else {
-      X = *FP->getValueAPF().convertToAPInt().getRawData();
+      X = FP->getValueAPF().convertToAPInt().getZExtValue();
       Size = 8;  
     }
     Out << "\tldc.r" << Size << "\t( " << utohexstr(X) << ')';
@@ -1473,10 +1473,10 @@
     const ConstantFP* FP = cast<ConstantFP>(C);
     if (Ty->getTypeID() == Type::FloatTyID)
       Out << "int32 (" << 
-        (uint32_t)*FP->getValueAPF().convertToAPInt().getRawData() << ')';
+        (uint32_t)FP->getValueAPF().convertToAPInt().getZExtValue() << ')';
     else
       Out << "int64 (" << 
-        *FP->getValueAPF().convertToAPInt().getRawData() << ')';
+        FP->getValueAPF().convertToAPInt().getZExtValue() << ')';
     break;
   }
   case Type::ArrayTyID:

Modified: llvm/trunk/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/AsmWriter.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/trunk/lib/VMCore/AsmWriter.cpp Tue Sep 11 22:30:33 2007
@@ -481,35 +481,64 @@
     else 
       Out << CI->getValue().toStringSigned(10);
   } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
-    // We would like to output the FP constant value in exponential notation,
-    // but we cannot do this if doing so will lose precision.  Check here to
-    // make sure that we only output it in exponential format if we can parse
-    // the value back and get the same value.
-    //
-    bool isDouble = &CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble;
-    double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() :
-                              CFP->getValueAPF().convertToFloat();
-    std::string StrVal = ftostr(CFP->getValueAPF());
-
-    // Check to make sure that the stringized number is not some string like
-    // "Inf" or NaN, that atof will accept, but the lexer will not.  Check that
-    // the string matches the "[-+]?[0-9]" regex.
-    //
-    if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
-        ((StrVal[0] == '-' || StrVal[0] == '+') &&
-         (StrVal[1] >= '0' && StrVal[1] <= '9')))
-      // Reparse stringized version!
-      if (atof(StrVal.c_str()) == Val) {
-        Out << StrVal;
-        return;
+    if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEdouble ||
+        &CFP->getValueAPF().getSemantics() == &APFloat::IEEEsingle) {
+      // We would like to output the FP constant value in exponential notation,
+      // but we cannot do this if doing so will lose precision.  Check here to
+      // make sure that we only output it in exponential format if we can parse
+      // the value back and get the same value.
+      //
+      bool isDouble = &CFP->getValueAPF().getSemantics()==&APFloat::IEEEdouble;
+      double Val = (isDouble) ? CFP->getValueAPF().convertToDouble() :
+                                CFP->getValueAPF().convertToFloat();
+      std::string StrVal = ftostr(CFP->getValueAPF());
+
+      // Check to make sure that the stringized number is not some string like
+      // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
+      // that the string matches the "[-+]?[0-9]" regex.
+      //
+      if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
+          ((StrVal[0] == '-' || StrVal[0] == '+') &&
+           (StrVal[1] >= '0' && StrVal[1] <= '9'))) {
+        // Reparse stringized version!
+        if (atof(StrVal.c_str()) == Val) {
+          Out << StrVal;
+          return;
+        }
       }
-
-    // Otherwise we could not reparse it to exactly the same value, so we must
-    // output the string in hexadecimal format!
-    assert(sizeof(double) == sizeof(uint64_t) &&
-           "assuming that double is 64 bits!");
-    Out << "0x" << utohexstr(DoubleToBits(Val));
-
+      // Otherwise we could not reparse it to exactly the same value, so we must
+      // output the string in hexadecimal format!
+      assert(sizeof(double) == sizeof(uint64_t) &&
+             "assuming that double is 64 bits!");
+      Out << "0x" << utohexstr(DoubleToBits(Val));
+    } else {
+      // Some form of long double.  These appear as a magic letter identifying
+      // the type, then a fixed number of hex digits.
+      Out << "0x";
+      if (&CFP->getValueAPF().getSemantics() == &APFloat::x87DoubleExtended)
+        Out << 'K';
+      else if (&CFP->getValueAPF().getSemantics() == &APFloat::IEEEquad)
+        Out << 'L';
+      else
+        assert(0 && "Unsupported floating point type");
+      const uint64_t* p = CFP->getValueAPF().convertToAPInt().getRawData();
+      uint64_t word = *p;
+      int shiftcount=60;
+      int width = CFP->getValueAPF().convertToAPInt().getBitWidth();
+      for (int j=0; j<width; j+=4, shiftcount-=4) {
+        unsigned int nibble = (word>>shiftcount) & 15;
+        if (nibble < 10)
+          Out << (unsigned char)(nibble + '0');
+        else
+          Out << (unsigned char)(nibble - 10 + 'A');
+        if (shiftcount == 0) {
+          word = *(++p);
+          shiftcount = 60;
+          if (width-j-4 < 64)
+            shiftcount = width-j-4;
+        }
+      }
+    }
   } else if (isa<ConstantAggregateZero>(CV)) {
     Out << "zeroinitializer";
   } else if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {

Modified: llvm/trunk/lib/VMCore/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/ConstantFold.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/ConstantFold.cpp (original)
+++ llvm/trunk/lib/VMCore/ConstantFold.cpp Tue Sep 11 22:30:33 2007
@@ -87,8 +87,8 @@
     
     if (SrcEltTy->getTypeID() == Type::DoubleTyID) {
       for (unsigned i = 0; i != SrcNumElts; ++i) {
-        uint64_t V = *cast<ConstantFP>(CV->getOperand(i))->
-                       getValueAPF().convertToAPInt().getRawData();
+        uint64_t V = cast<ConstantFP>(CV->getOperand(i))->
+                       getValueAPF().convertToAPInt().getZExtValue();
         Constant *C = ConstantInt::get(Type::Int64Ty, V);
         Result.push_back(ConstantExpr::getBitCast(C, DstEltTy ));
       }
@@ -97,8 +97,8 @@
 
     assert(SrcEltTy->getTypeID() == Type::FloatTyID);
     for (unsigned i = 0; i != SrcNumElts; ++i) {
-      uint32_t V = (uint32_t)*cast<ConstantFP>(CV->getOperand(i))->
-                               getValueAPF().convertToAPInt().getRawData();
+      uint32_t V = (uint32_t)cast<ConstantFP>(CV->getOperand(i))->
+                               getValueAPF().convertToAPInt().getZExtValue();
       Constant *C = ConstantInt::get(Type::Int32Ty, V);
       Result.push_back(ConstantExpr::getBitCast(C, DstEltTy));
     }
@@ -331,9 +331,8 @@
         return const_cast<Constant*>(V);
 
       if (DestTy->isFloatingPoint()) {
-        if (DestTy == Type::FloatTy)
-          return ConstantFP::get(DestTy, APFloat(CI->getValue()));
-        assert(DestTy == Type::DoubleTy && "Unknown FP type!");
+        assert((DestTy == Type::DoubleTy || DestTy == Type::FloatTy) && 
+               "Unknown FP type!");
         return ConstantFP::get(DestTy, APFloat(CI->getValue()));
       }
       // Otherwise, can't fold this (vector?)

Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Tue Sep 11 22:30:33 2007
@@ -245,8 +245,14 @@
   // temporary
   if (Ty==Type::FloatTy)
     assert(&V.getSemantics()==&APFloat::IEEEsingle);
-  else
+  else if (Ty==Type::DoubleTy)
     assert(&V.getSemantics()==&APFloat::IEEEdouble);
+  else if (Ty==Type::X86_FP80Ty)
+    assert(&V.getSemantics()==&APFloat::x87DoubleExtended);
+  else if (Ty==Type::FP128Ty)
+    assert(&V.getSemantics()==&APFloat::IEEEquad);
+  else
+    assert(0);
 }
 
 bool ConstantFP::isNullValue() const {
@@ -294,8 +300,14 @@
   // temporary
   if (Ty==Type::FloatTy)
     assert(&V.getSemantics()==&APFloat::IEEEsingle);
-  else
+  else if (Ty==Type::DoubleTy)
     assert(&V.getSemantics()==&APFloat::IEEEdouble);
+  else if (Ty==Type::X86_FP80Ty)
+    assert(&V.getSemantics()==&APFloat::x87DoubleExtended);
+  else if (Ty==Type::FP128Ty)
+    assert(&V.getSemantics()==&APFloat::IEEEquad);
+  else
+    assert(0);
   
   DenseMapAPFloatKeyInfo::KeyTy Key(V);
   ConstantFP *&Slot = (*FPConstants)[Key];
@@ -713,11 +725,14 @@
            &Val2.getSemantics() == &APFloat::IEEEdouble ||
            Val2.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven) == 
              APFloat::opOK;
-  // TODO: Figure out how to test if we can use a shorter type instead!
   case Type::X86_FP80TyID:
-  case Type::PPC_FP128TyID:
+    return &Val2.getSemantics() == &APFloat::IEEEsingle || 
+           &Val2.getSemantics() == &APFloat::IEEEdouble ||
+           &Val2.getSemantics() == &APFloat::x87DoubleExtended;
   case Type::FP128TyID:
-    return true;
+    return &Val2.getSemantics() == &APFloat::IEEEsingle || 
+           &Val2.getSemantics() == &APFloat::IEEEdouble ||
+           &Val2.getSemantics() == &APFloat::IEEEquad;
   }
 }
 

Modified: llvm/trunk/tools/llvm2cpp/CppWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm2cpp/CppWriter.cpp?rev=41875&r1=41874&r2=41875&view=diff

==============================================================================
--- llvm/trunk/tools/llvm2cpp/CppWriter.cpp (original)
+++ llvm/trunk/tools/llvm2cpp/CppWriter.cpp Tue Sep 11 22:30:33 2007
@@ -250,11 +250,11 @@
       }
     else if (CFP->getType() == Type::DoubleTy)
       Out << "BitsToDouble(0x" << std::hex 
-          << *CFP->getValueAPF().convertToAPInt().getRawData()
+          << CFP->getValueAPF().convertToAPInt().getZExtValue()
           << std::dec << "ULL) /* " << StrVal << " */";
     else 
       Out << "BitsToFloat(0x" << std::hex 
-          << (uint32_t)*CFP->getValueAPF().convertToAPInt().getRawData()
+          << (uint32_t)CFP->getValueAPF().convertToAPInt().getZExtValue()
           << std::dec << "U) /* " << StrVal << " */";
     Out << ")";
 #if HAVE_PRINTF_A





More information about the llvm-commits mailing list