We ran into three interesting cases of labelled DO loops migrating code from Gould-SEL MPX and from VMS. The code is shown below:
PROGRAM t_labels
IMPLICIT NONE
INTEGER :: i,j
! DO loop to a labelled executable statement
DO 100 i=1,3
100 WRITE(*,'("Executable labelled end of DO, i:",I3)')i
! 2 DO loops ending on the same label
DO 200 i=1,2
DO 200 j= 1,2
200 WRITE(*,
1 '("2 DO loops to the same labelled WRITE, i,j:",2I3)')i,j
! Jump to the end of a DO loop from within the loop
DO 300 i=1,3
IF (i .EQ. 2) GOTO 300
WRITE(*,'("Within the DO loop")')
300 WRITE(*,'("End of the DO loop with jump within",I3)')i
! Jump from outside to the terminating label of a DO loop
IF (i .GT. 0) GOTO 400
DO 400 i = 1,3
WRITE(*,'("Within DO loop with jump from outside",I3)')i
400 CONTINUE
END PROGRAM t_labels
The fpt commands to deal with these are:
% change DO CONTINUE to DO ENDDO
% remove labels from executable statements
% separate DO and GOTO destination labels
The code then becomes:
PROGRAM t_labels
!
IMPLICIT NONE
!
INTEGER :: i,j
!
! DO loop to a labelled executable statement
DO 1000 i = 1,3
WRITE (*,'("Executable labelled end of DO, i:",I3)')i
1000 ENDDO
!
! 2 DO loops ending on the same label
DO 1020 i = 1,2
DO 1010 j = 1,2
WRITE (*, &
'("2 DO loops to the same labelled WRITE, i,j:",2I3)')i,j
1010 ENDDO
1020 ENDDO
!------------^-----------------------------------------------------------------
!!! FPT - 2311 GOTO references from inside DO loop directed to different label
!------------------------------------------------------------------------------
!
! Jump to the end of a DO loop from within the loop
DO 1040 i = 1,3
IF (i .EQ. 2) GOTO 1030
WRITE (*,'("Within the DO loop")')
1030 CONTINUE
WRITE (*,'("End of the DO loop with jump within",I3)')i
1040 ENDDO
!------------^-----------------------------------------------------------------
!!! FPT - 2311 GOTO references from inside DO loop directed to different label
!------------------------------------------------------------------------------
!
! Jump from outside to the terminating label of a DO loop
IF (i .GT. 0) GOTO 1060
DO 1050 i = 1,3
WRITE (*,'("Within DO loop with jump from outside",I3)')i
1050 ENDDO
1060 CONTINUE
!---------------^--------------------------------------------------------------
!!! FPT - 2309 GOTO references from outside DO loop directed to different label
!------------------------------------------------------------------------------
!
END PROGRAM t_labels
The repositioning of the labels is important. If the labelled statement terminates a DO loop the repositioned label goes after the executable statement. If it is the target of a GOTO (or equivalent) it goes before the statement. If both, fpt splits the label into 2 labels, one before and one after.
The last case, jumping from outside a loop to the terminating label, is mercifully thrown out by modern compilers (e.g. gfortran, ifort, ifx). The Gould-SEL MPX compiler jumps over the loop and keeps going. The VMS compilers jumped into the loop without initialising the loop control variable. Good luck with that!
fpt will do this systematically through a large code. You are welcome to try it - see http://simconglobal.com.