OSDN Git Service

* tree.h (DECIMAL_FLOAT_TYPE_P): New.
[pf3gnuchains/gcc-fork.git] / gcc / convert.c
index ece3637..ab780d8 100644 (file)
@@ -1,6 +1,6 @@
 /* Utility routines for data type conversion for GCC.
    Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1997, 1998,
-   2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -33,27 +33,35 @@ Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
 #include "toplev.h"
 #include "langhooks.h"
 #include "real.h"
-/* Convert EXPR to some pointer or reference type TYPE.
 
+/* Convert EXPR to some pointer or reference type TYPE.
    EXPR must be pointer, reference, integer, enumeral, or literal zero;
    in other cases error is called.  */
 
 tree
 convert_to_pointer (tree type, tree expr)
 {
+  if (TREE_TYPE (expr) == type)
+    return expr;
+
   if (integer_zerop (expr))
-    return build_int_cst (type, 0);
+    {
+      tree t = build_int_cst (type, 0);
+      if (TREE_OVERFLOW (expr) || TREE_CONSTANT_OVERFLOW (expr))
+       t = force_fit_type (t, 0, TREE_OVERFLOW (expr),
+                           TREE_CONSTANT_OVERFLOW (expr));
+      return t;
+    }
 
   switch (TREE_CODE (TREE_TYPE (expr)))
     {
     case POINTER_TYPE:
     case REFERENCE_TYPE:
-      return build1 (NOP_EXPR, type, expr);
+      return fold_build1 (NOP_EXPR, type, expr);
 
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
     case BOOLEAN_TYPE:
-    case CHAR_TYPE:
       if (TYPE_PRECISION (TREE_TYPE (expr)) != POINTER_SIZE)
        expr = fold_build1 (NOP_EXPR,
                             lang_hooks.types.type_for_size (POINTER_SIZE, 0),
@@ -263,6 +271,28 @@ convert_to_real (tree type, tree expr)
                 && FLOAT_TYPE_P (TREE_TYPE (arg1)))
               {
                  tree newtype = type;
+
+                 if (TYPE_MODE (TREE_TYPE (arg0)) == SDmode
+                     || TYPE_MODE (TREE_TYPE (arg1)) == SDmode)
+                   newtype = dfloat32_type_node;
+                 if (TYPE_MODE (TREE_TYPE (arg0)) == DDmode
+                     || TYPE_MODE (TREE_TYPE (arg1)) == DDmode)
+                   newtype = dfloat64_type_node;
+                 if (TYPE_MODE (TREE_TYPE (arg0)) == TDmode
+                     || TYPE_MODE (TREE_TYPE (arg1)) == TDmode)
+                    newtype = dfloat128_type_node;
+                 if (newtype == dfloat32_type_node
+                     || newtype == dfloat64_type_node
+                     || newtype == dfloat128_type_node)
+                   {
+                     expr = build2 (TREE_CODE (expr), newtype,
+                                    fold (convert_to_real (newtype, arg0)),
+                                    fold (convert_to_real (newtype, arg1)));
+                     if (newtype == type)
+                       return expr;
+                     break;
+                   }
+
                  if (TYPE_PRECISION (TREE_TYPE (arg0)) > TYPE_PRECISION (newtype))
                    newtype = TREE_TYPE (arg0);
                  if (TYPE_PRECISION (TREE_TYPE (arg1)) > TYPE_PRECISION (newtype))
@@ -285,13 +315,16 @@ convert_to_real (tree type, tree expr)
   switch (TREE_CODE (TREE_TYPE (expr)))
     {
     case REAL_TYPE:
-      return build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
-                    type, expr);
+      /* Ignore the conversion if we don't need to store intermediate
+        results and neither type is a decimal float.  */
+      return build1 ((flag_float_store
+                    || DECIMAL_FLOAT_TYPE_P (type)
+                    || DECIMAL_FLOAT_TYPE_P (itype))
+                    ? CONVERT_EXPR : NOP_EXPR, type, expr);
 
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
     case BOOLEAN_TYPE:
-    case CHAR_TYPE:
       return build1 (FLOAT_EXPR, type, expr);
 
     case COMPLEX_TYPE:
@@ -425,7 +458,6 @@ convert_to_integer (tree type, tree expr)
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
     case BOOLEAN_TYPE:
-    case CHAR_TYPE:
       /* If this is a logical operation, which just returns 0 or 1, we can
         change the type of the expression.  */
 
@@ -500,7 +532,7 @@ convert_to_integer (tree type, tree expr)
          /* We can pass truncation down through right shifting
             when the shift count is a nonpositive constant.  */
          if (TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST
-             && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) < 0)
+             && tree_int_cst_sgn (TREE_OPERAND (expr, 1)) <= 0)
            goto trunc1;
          break;
 
@@ -606,7 +638,17 @@ convert_to_integer (tree type, tree expr)
                                || ex_form == RSHIFT_EXPR
                                || ex_form == LROTATE_EXPR
                                || ex_form == RROTATE_EXPR))
-                       || ex_form == LSHIFT_EXPR)
+                       || ex_form == LSHIFT_EXPR
+                       /* If we have !flag_wrapv, and either ARG0 or
+                          ARG1 is of a signed type, we have to do
+                          PLUS_EXPR or MINUS_EXPR in an unsigned
+                          type.  Otherwise, we would introduce
+                          signed-overflow undefinedness.  */
+                       || (!flag_wrapv
+                           && (ex_form == PLUS_EXPR
+                               || ex_form == MINUS_EXPR)
+                           && (!TYPE_UNSIGNED (TREE_TYPE (arg0))
+                               || !TYPE_UNSIGNED (TREE_TYPE (arg1)))))
                      typex = lang_hooks.types.unsigned_type (typex);
                    else
                      typex = lang_hooks.types.signed_type (typex);
@@ -697,7 +739,6 @@ convert_to_complex (tree type, tree expr)
     case INTEGER_TYPE:
     case ENUMERAL_TYPE:
     case BOOLEAN_TYPE:
-    case CHAR_TYPE:
       return build2 (COMPLEX_EXPR, type, convert (subtype, expr),
                     convert (subtype, integer_zero_node));