]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR fortran/46794 (ICE on valid code involving power of small integer kinds)
authorDaniel Kraft <d@domob.eu>
Sat, 4 Dec 2010 09:27:17 +0000 (10:27 +0100)
committerDaniel Kraft <domob@gcc.gnu.org>
Sat, 4 Dec 2010 09:27:17 +0000 (10:27 +0100)
2010-12-04  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-04  Daniel Kraft  <d@domob.eu>

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

From-SVN: r167453

gcc/fortran/ChangeLog
gcc/fortran/trans-expr.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/power2.f90 [new file with mode: 0644]

index 99c82d2c34b1609f4bd1240bc6746f00dc4204fe..a1d06c5872fed8979b78b9f93a29c465af3ee0ed 100644 (file)
@@ -1,3 +1,9 @@
+2010-12-04  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  Thomas Koenig  <tkoenig@gcc.gnu.org>
 
        PR fortran/44352
index 5857c0d3c08d4dc8e21e62c28daa29d0a12a5b11..46f80f7fb1733b3e7a0e4f2c2fb9afdfd1a11be2 100644 (file)
@@ -976,6 +976,7 @@ gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
   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_conv_power_op (gfc_se * se, gfc_expr * expr)
 
   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 @@ gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
        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 @@ gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
        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 @@ gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
 
   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);
+    }
 }
 
 
index f46c555621b1707313a22996b29226445ca9ce36..e48bf7818cf2c4802d67dc140613626cab2af28e 100644 (file)
@@ -1,3 +1,8 @@
+2010-12-04  Daniel Kraft  <d@domob.eu>
+
+       PR fortran/46794
+       * gfortran.dg/power2.f90: New test.
+
 2010-12-03  Jakub Jelinek  <jakub@redhat.com>
 
        PR debug/46123
diff --git a/gcc/testsuite/gfortran.dg/power2.f90 b/gcc/testsuite/gfortran.dg/power2.f90
new file mode 100644 (file)
index 0000000..01954cb
--- /dev/null
@@ -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