Patchwork Go patch committed: Don't remove float conversion of typed constant

login
register
mail settings
Submitter Ian Taylor
Date Nov. 16, 2012, 4:31 p.m.
Message ID <mcrboex8q7o.fsf@google.com>
Download mbox | patch
Permalink /patch/199683/
State New
Headers show

Comments

Ian Taylor - Nov. 16, 2012, 4:31 p.m.
This patch to the Go frontend fixes the case of float64(float32(1.0)).
Here float32(1.0) is a typed constant.  The compiler was translating
that to float64(1.0), which is not correct.  Bootstrapped and ran Go
testsuite on x86_64-unknown-linux-gnu.  Committed to mainline and 4.7
branch.

Ian

Patch

Index: gcc/go/gofrontend/expressions.cc
===================================================================
--- gcc/go/gofrontend/expressions.cc	(revision 193484)
+++ gcc/go/gofrontend/expressions.cc	(working copy)
@@ -2962,6 +2962,46 @@  Type_conversion_expression::do_lower(Gog
 	{
 	  if (!nc.set_type(type, true, location))
 	    return Expression::make_error(location);
+
+	  // Don't simply convert to or from a float or complex type
+	  // with a different size.  That may change the value.
+	  Type* vtype = val->type();
+	  if (vtype->is_abstract())
+	    ;
+	  else if (type->float_type() != NULL)
+	    {
+	      if (vtype->float_type() != NULL)
+		{
+		  if (type->float_type()->bits() != vtype->float_type()->bits())
+		    return this;
+		}
+	      else if (vtype->complex_type() != NULL)
+		{
+		  if (type->float_type()->bits() * 2
+		      != vtype->complex_type()->bits())
+		    return this;
+		}
+	    }
+	  else if (type->complex_type() != NULL)
+	    {
+	      if (vtype->complex_type() != NULL)
+		{
+		  if (type->complex_type()->bits()
+		      != vtype->complex_type()->bits())
+		    return this;
+		}
+	      else if (vtype->float_type() != NULL)
+		{
+		  if (type->complex_type()->bits()
+		      != vtype->float_type()->bits() * 2)
+		    return this;
+		}
+	    }
+	  else if (vtype->float_type() != NULL)
+	    return this;
+	  else if (vtype->complex_type() != NULL)
+	    return this;
+
 	  return nc.expression(location);
 	}
     }