public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch, Fortran] PR fortran/46794: Fix ICE with powers of integers
@ 2010-12-03 22:38 Daniel Kraft
  2010-12-04  8:49 ` Tobias Burnus
  0 siblings, 1 reply; 9+ messages in thread
From: Daniel Kraft @ 2010-12-03 22:38 UTC (permalink / raw)
  To: Fortran List, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 797 bytes --]

Hi all,

I'm (a little) back to gfortran ;)  The attached patch fixes the problem
of PR 46794 -- namely that when we call _gfortran_pow_i4_i4 even for
integer operands with kinds 1 / 2, the result was not converted back
properly to the smaller kinds and this ICEd (in certain cases).  The fix
is simply to add an appropriate conversion.  I do not entirely like the
way this is done in the patch (with the two variables and "special
casing"), but don't see a better implementation -- what do you think?

Regression-tested on x86_64-unknown-linux-gnu without failures -- though 
the run somehow looked strange to me (on the compile-farm); I'll try 
again to be sure.  Ok for trunk?

Yours,
Daniel

-- 
http://www.pro-vegan.info/
--
Done:  Arc-Bar-Cav-Kni-Ran-Rog-Sam-Tou-Val-Wiz
To go: Hea-Mon-Pri


[-- Attachment #2: patch.changelog --]
[-- Type: text/plain, Size: 271 bytes --]

2010-12-03  Daniel Kraft  <d@domob.eu>

	PR fortran/46794
	* trans-expr.c (gfc_conv_power_op): Handle kind of result expression
	correctly for integer kind 1 and 2 operands.

2010-12-03  Daniel Kraft  <d@domob.eu>

	PR fortran/46794
	* gfortran.dg/power2.f90: New test.


[-- Attachment #3: patch.diff --]
[-- Type: text/plain, Size: 2476 bytes --]

Index: gcc/testsuite/gfortran.dg/power2.f90
===================================================================
--- gcc/testsuite/gfortran.dg/power2.f90	(revision 0)
+++ gcc/testsuite/gfortran.dg/power2.f90	(revision 0)
@@ -0,0 +1,22 @@
+! { dg-do compile }
+! PR fortran/46794
+
+! Check that results of powers of integers with kinds 1 and 2 are
+! correctly converted back; this used to ICE because a conversion
+! from kind 4 to the correct one was missing.
+
+! Contributed by Daniel Kraft, d@domob.eu.
+
+PROGRAM main
+  IMPLICIT NONE
+
+  INTEGER(KIND=1) :: k1
+  INTEGER(KIND=2) :: k2
+
+  k1 = 1_1 + 1_1**k1
+  k2 = 1_2 + 1_2**k2
+
+  k2 = 1_1 + 1_1**k2
+  k2 = 1_1 + 1_2**k1
+  k2 = 1_1 + 1_2**k2
+END PROGRAM main
Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 167440)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -976,6 +976,7 @@
   tree gfc_int4_type_node;
   int kind;
   int ikind;
+  int res_ikind_1, res_ikind_2;
   gfc_se lse;
   gfc_se rse;
   tree fndecl = NULL;
@@ -996,6 +997,13 @@
 
   gfc_int4_type_node = gfc_get_int_type (4);
 
+  /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
+     library routine.  But in the end, we have to convert the result back
+     if this case applies -- with res_ikind_K, we keep track whether operand K
+     falls into this case.  */
+  res_ikind_1 = -1;
+  res_ikind_2 = -1;
+
   kind = expr->value.op.op1->ts.kind;
   switch (expr->value.op.op2->ts.type)
     {
@@ -1006,6 +1014,7 @@
 	case 1:
 	case 2:
 	  rse.expr = convert (gfc_int4_type_node, rse.expr);
+	  res_ikind_2 = ikind;
 	  /* Fall through.  */
 
 	case 4:
@@ -1028,7 +1037,10 @@
 	case 1:
 	case 2:
 	  if (expr->value.op.op1->ts.type == BT_INTEGER)
-	    lse.expr = convert (gfc_int4_type_node, lse.expr);
+	    {
+	      lse.expr = convert (gfc_int4_type_node, lse.expr);
+	      res_ikind_1 = kind;
+	    }
 	  else
 	    gcc_unreachable ();
 	  /* Fall through.  */
@@ -1121,6 +1133,15 @@
 
   se->expr = build_call_expr_loc (input_location,
 			      fndecl, 2, lse.expr, rse.expr);
+
+  /* Convert the result back if it is of wrong integer kind.  */
+  if (res_ikind_1 != -1 && res_ikind_2 != -1)
+    {
+      /* We want the maximum of both operand kinds as result.  */
+      if (res_ikind_1 < res_ikind_2)
+	res_ikind_1 = res_ikind_2;
+      se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
+    }
 }
 
 


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-12-17 15:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-03 22:38 [Patch, Fortran] PR fortran/46794: Fix ICE with powers of integers Daniel Kraft
2010-12-04  8:49 ` Tobias Burnus
2010-12-04  9:28   ` Daniel Kraft
2010-12-04 18:19     ` Steve Kargl
2010-12-05 10:11       ` Daniel Kraft
2010-12-05 17:33         ` Steve Kargl
2010-12-04 12:14   ` Daniel Kraft
2010-12-09 16:16   ` Daniel Kraft
2010-12-17 16:22   ` Daniel Kraft

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).