This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[gfortran] Empty if statements


With the recent expression folding changes we fold if statements before they 
are complete. Attached patch fixes this by completing an if statement before 
adding it to the containing block.

Tested on i686-linux.
Applied to tree-ssa branch.

Paul

2004-01-11  Paul Brook  <paul@codesourcery.com>

	* trans-stmt.c (gfc_trans_if_1): New function.
	(gfc_trans_if): Use it.

	* gfortran.fortran-torture/execute/emptyif.f90: New test.
Index: trans-stmt.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/Attic/trans-stmt.c,v
retrieving revision 1.1.2.14
diff -c -p -r1.1.2.14 trans-stmt.c
*** trans-stmt.c	1 Jan 2004 12:09:12 -0000	1.1.2.14
--- trans-stmt.c	11 Jan 2004 23:02:24 -0000
*************** gfc_trans_stop (gfc_code * code)
*** 377,442 ****
  
     where COND_S is the simplified version of the predicate. PRE_COND_S
     are the pre side-effects produced by the translation of the
!    conditional.  */
  
! tree
! gfc_trans_if (gfc_code * code)
  {
    gfc_se if_se;
!   tree top, stmt, tail, ifstmt;
  
!   top = tail = NULL_TREE;
  
    /* Ignore the top EXEC_IF, it only announces an IF construct. The
       actual code we must translate is in code->block.  */
  
!   code = code->block;
! 
!   /* If code->expr != NULL, then we need to build a condition
!      expression. This is true for IF(cond) and ELSEIF(cond). The
!      final, unconditional ELSE is chained at the bottom of this
!      function.  */
! 
!   while (code && code->expr)
!     {
! 
!       /* Initialize a statement builder for each block. Puts in NULL_TREEs.  */
!       gfc_init_se (&if_se, NULL);
!       gfc_start_block (&if_se.pre);
! 
!       /* Calculate the IF condition expression.  */
!       gfc_conv_expr_val (&if_se, code->expr);
! 
!       /* Translate the THEN clause.  */
!       stmt = gfc_trans_code (code->next);
! 
!       /* Build the condition expression and add it to the condition block.  */
!       ifstmt = build_v (COND_EXPR, if_se.expr, stmt, build_empty_stmt ());
!       gfc_add_expr_to_block (&if_se.pre, ifstmt);
! 
!       /* Finish off this statement.  */
!       stmt = gfc_finish_block (&if_se.pre);
! 
!       /* If this is an ELSE IF, insert it into the else of the previous
!          condition.  */
!       if (tail)
! 	TREE_OPERAND (tail, 2) = stmt;
!       tail = ifstmt;
! 
!       /* Store in TOP if this is the first translated IF block of this
!          construct.  */
!       if (!top)
! 	top = stmt;
! 
!       /* Advance to the next block, if there is one.  */
!       code = code->block;
!     }
! 
!   /* See about the unconditional ELSE.  */
!   if (code)
!     TREE_OPERAND (tail, 2) = gfc_trans_code (code->next);
! 
!   return top;
  }
  
  
--- 377,428 ----
  
     where COND_S is the simplified version of the predicate. PRE_COND_S
     are the pre side-effects produced by the translation of the
!    conditional.
!    We need to build the chain recursively otherwise we run into
!    problems with folding incomplete statements.  */
  
! static tree
! gfc_trans_if_1 (gfc_code * code)
  {
    gfc_se if_se;
!   tree stmt, elsestmt;
  
!   /* Check for an unconditional ELSE clause.  */
!   if (!code->expr)
!     return gfc_trans_code (code->next);
! 
!   /* Initialize a statement builder for each block. Puts in NULL_TREEs.  */
!   gfc_init_se (&if_se, NULL);
!   gfc_start_block (&if_se.pre);
! 
!   /* Calculate the IF condition expression.  */
!   gfc_conv_expr_val (&if_se, code->expr);
! 
!   /* Translate the THEN clause.  */
!   stmt = gfc_trans_code (code->next);
! 
!   /* Translate the ELSE clause.  */
!   if (code->block)
!     elsestmt = gfc_trans_if_1 (code->block);
!   else
!     elsestmt = build_empty_stmt ();
! 
!   /* Build the condition expression and add it to the condition block.  */
!   stmt = build_v (COND_EXPR, if_se.expr, stmt, elsestmt);
!   
!   gfc_add_expr_to_block (&if_se.pre, stmt);
! 
!   /* Finish off this statement.  */
!   return gfc_finish_block (&if_se.pre);
! }
  
+ tree
+ gfc_trans_if (gfc_code * code)
+ {
    /* Ignore the top EXEC_IF, it only announces an IF construct. The
       actual code we must translate is in code->block.  */
  
!   return gfc_trans_if_1 (code->block);
  }
  
  

Attachment: emptyif.f90
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]