Bug 17708 - gfortran problem with goto inside loop
Summary: gfortran problem with goto inside loop
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: unknown
: P2 normal
Target Milestone: 4.0.0
Assignee: Not yet assigned to anyone
URL:
Keywords: patch, wrong-code
Depends on:
Blocks:
 
Reported: 2004-09-28 08:28 UTC by mimo
Modified: 2004-10-04 21:19 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2004-09-28 11:35:10


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description mimo 2004-09-28 08:28:28 UTC
Here is a simple example 
      program test
      do 10 i=1,3
      if(i == 2) goto 10
      write(*,*) ' loop with i =',i
 10   enddo
      end
  where the loop stops at the second pass (i=2). Replacing the
  "goto" by "cycle" cures the problem  (but this breaks compatibility
  with old g77 codes)
      program test
      do 10 i=1,3
      if(i == 2) cycle
      write(*,*) ' loop with i =',i
 10   enddo
      end
  I've used the gfortran version as of today (28 sep), on gcc 3.2.2
Comment 1 Andrew Pinski 2004-09-28 11:35:09 UTC
Confirmed.
Comment 2 Tobias Schlüter 2004-09-28 13:43:54 UTC
t02.original looks like this:
MAIN__ ()
{
  int4 i;

  {
    int4 count.0;

    count.0 = 3;
    i = 1;
    while (1)
      {
        if (count.0 <= 0)
          {
            goto L.2;
          }
        else
          {
            (void) 0;
          }
        _gfortran_filename = "pr17708.f90";
        _gfortran_line = 4;
        _gfortran_ioparm.unit = 6;
        _gfortran_ioparm.list_format = 1;
        _gfortran_st_write ();
        _gfortran_transfer_character (" loop with i =", 14);
        _gfortran_transfer_integer (&i, 4);
        _gfortran_st_write_done ();
        i = i + 1;
        count.0 = count.0 - 1;
      }
    L.2:;
  }
  __label_000010:; <<<<<<<<<<<<<<<<<<<< this is the wrong place
}
Comment 3 Tobias Schlüter 2004-09-28 16:07:39 UTC
The bug is actually in the frontend. From the -fdump-parse-tree output:
      DO i=1 3 1
        IF (= i 2)
          GOTO 10
        ENDIF
        WRITE UNIT=6 FMT=-1
        TRANSFER ' loop with i ='
        TRANSFER i
        DT_END
      END DO
10    NOP
The last two lines should clearly be interchanged.
Comment 4 Tobias Schlüter 2004-09-28 16:45:59 UTC
Patch here: http://gcc.gnu.org/ml/fortran/2004-09/msg00277.html
Comment 5 GCC Commits 2004-10-04 21:05:10 UTC
Subject: Bug 17708

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	tobi@gcc.gnu.org	2004-10-04 21:05:08

Modified files:
	gcc/fortran    : ChangeLog parse.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gfortran.dg: pr17708.f90 

Log message:
	fortran/
	PR fortran/17708
	* parse.c (accept_statement): Don't treat END DO like END IF and
	END SELECT.
	(parse_do_block): Generate possible END DO label inside END DO
	block.
	
	also, added ChangeLog entry for previous commit.
	
	testsuite/
	PR fortran/17708
	* gfortran.dg/pr17708.f90: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/ChangeLog.diff?cvsroot=gcc&r1=1.224&r2=1.225
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/fortran/parse.c.diff?cvsroot=gcc&r1=1.19&r2=1.20
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4384&r2=1.4385
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gfortran.dg/pr17708.f90.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 6 Tobias Schlüter 2004-10-04 21:05:40 UTC
Fixed.