[net.lang.f77] bug using loop variable as start value

sundman@ttds.UUCP (Bo Sundman) (01/15/85)

C The example below reproduces a bug in f77.
C
C The error seems to be caused by using L both as loop variable and as the
C start value of the loop. It is clear from the output that the first loop
C is never executed as L obtains its large value, 2147479256, before the first
C test. The problem should thus have nothing to do with the question
C whether the loop variable is undefined or not after exiting the loop.
C
	DIMENSION X(10)
	L=1
	DO 100 L=L,10
	   X(L)=L
100	CONTINUE
200	WRITE(*,*)L,X
	I=1
	DO 300 L=I,10
	   X(L)=L
300	CONTINUE
400	WRITE(*,*)L,X
	END

C Output from the example:
C
C  2147479256  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
C  11   1.00000   2.00000   3.00000   4.00000   5.00000   6.00000   7.00000
C   8.00000   9.00000   10.0000
-- 
	Bo Sundman                      (..mcvax!enea!ttds!sundman)
        Thermo-Calc group, Division of Physical Metallurgy
	The Royal Institute of Technology
	Stockholm, Sweden

woods@hao.UUCP (Greg Woods) (01/17/85)

> C The example below reproduces a bug in f77.
> C
> C The error seems to be caused by using L both as loop variable and as the
> C start value of the loop. It is clear from the output that the first loop
> C is never executed as L obtains its large value, 2147479256, before the first
> C test. The problem should thus have nothing to do with the question
> C whether the loop variable is undefined or not after exiting the loop.
> C
> 	DIMENSION X(10)
> 	L=1
> 	DO 100 L=L,10
> 	   X(L)=L
> 100	CONTINUE

  This is not a bug. You are violating the standard. The standard does not
allow this type of loop initialization. The order of evaluation in the 
DO 100 statement is not guaranteed to be what you think it will be.

--Greg
-- 
{ucbvax!hplabs | allegra!nbires | decvax!stcvax | harpo!seismo | ihnp4!stcvax}
       		        !hao!woods
   
     "...sometimes the light's all shining on me;
	 other times I can barely see..."

west@sdcsla.UUCP (Larry West) (01/19/85)

In article <787@ttds.UUCP> sundman@ttds.UUCP (Bo Sundman) writes:
>C
>	DIMENSION X(10)
>	L=1
>	DO 100 L=L,10
>	   X(L)=L
>100	CONTINUE
>200	WRITE(*,*)L,X
>	I=1
>	DO 300 L=I,10
>	   X(L)=L
>300	CONTINUE
>400	WRITE(*,*)L,X
>	END
>
>C Output from the example:
>C
>C  2147479256  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
>C  11   1.00000   2.00000   3.00000   4.00000   5.00000   6.00000   7.00000
>C   8.00000   9.00000   10.0000

Bo did not say what computer/OS this was done on, but I tried this out
on a Sun and Vax-11/750, running nearly identical 4.2bsd.   The bug only
appeared on the Vax, withOUT the "-O" flag.   According to "f77 -v", this
is "Berkeley F77, version 1.0", while on the Sun it's "SMI F77, version 1.1".

Looking at the assembly language output (VAX, w/o "-O"), it's easy to see
the error: at the start of the loop, register 10 is loaded from itself:

L13:
	movl	$1,v.2-v.1(r11)
	movl	r10,r10
	cmpl	r10,$10
	jgtr	L16

This is the first access to "r10" in the program.   The correct code is
emitted by the VAX f77 with "-O":

L13:	movl	$1,v.2-v.1(r11)
	movl	v.2-v.1(r11),r10
	movl	r10,r10
	cmpl	r10,$10
	jgtr	L16

Seems like optimization by comparison, of course...


-- 

--|  Larry West, UC San Diego, Institute for Cognitive Science
--|  UUCP:	{decvax!ucbvax,ihnp4}!sdcsvax!sdcsla!west
--|  ARPA:	west@NPRDC	{ NOT: <sdcsla!west@NPRDC> }

west@sdcsla.UUCP (Larry West) (01/19/85)

In article <787@ttds.UUCP> sundman@ttds.UUCP (Bo Sundman) writes:
>	DIMENSION X(10)
>	L=1
>	DO 100 L=L,10
>	   X(L)=L
>100	CONTINUE
>200	WRITE(*,*)L,X
>	I=1
>	DO 300 L=I,10
>	   X(L)=L
>300	CONTINUE
>400	WRITE(*,*)L,X
>	END
>
>C Output from the example:
>C
>C  2147479256  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
>C  11   1.00000   2.00000   3.00000   4.00000   5.00000   6.00000   7.00000
>C   8.00000   9.00000   10.0000
 
Bo did not say what computer/OS this was done on, but I tried this out
on a Sun and Vax-11/750, running nearly identical 4.2bsd.   The bug only
appeared on the Vax, withOUT the "-O" flag.   According to "f77 -v", this
is "Berkeley F77, version 1.0", while on the Sun it's "SMI F77, version 1.1".
 
Looking at the assembly language output (VAX, w/o "-O"), it's easy to see
the error: at the start of the loop, register 10 is loaded from itself:
 
L13: 
        movl    $1,v.2-v.1(r11)
        movl    r10,r10
        cmpl    r10,$10 
        jgtr    L16 
 
This is the first access to "r10" in the program.   The correct code is 
emitted by the VAX f77 with "-O": 
 
L13:    movl    $1,v.2-v.1(r11) 
        movl    v.2-v.1(r11),r10
        movl    r10,r10
        cmpl    r10,$10
        jgtr    L16
 
Seems like optimization by comparison, of course... 
-- 

--|  Larry West, UC San Diego, Institute for Cognitive Science
--|  UUCP:	{decvax!ucbvax,ihnp4}!sdcsvax!sdcsla!west
--|  ARPA:	west@NPRDC	{ NOT: <sdcsla!west@NPRDC> }