[comp.sys.amiga] Small bug in Lattice 3.10

andrew@alberta.UUCP (Andrew Folkins) (10/29/87)

Here's a slightly obscure bug in Lattice 3.10 :

#include <stdio.h>

long t;

main()
{
   int i;

   t = 0;

   t |= 1 << 31;

   for (i = 0; i < 32; i++)
      if (t & (1 << i)) printf("bit %d set\n", i);
}

produces :

bit -2147483617 set
bit -2147483553 set
bit -2147483489 set
*break*

Lattice's Object Module Disassembler gives :

0016 7000                       MOVEQ     #00,D0              t = 0
0018 23C0 00000000-02           MOVE.L    D0,02.00000000
001E 08C0001F                   BSET      #1F,D0              t |= ...
0022 23C0 00000000-02           MOVE.L    D0,02.00000000
0028 2B40FFFC                   MOVE.L    D0,FFFC(A5)         i = 0
002C 202DFFFC                   MOVE.L    FFFC(A5),D0
0030 0C8000000020               CMPI.L    #00000020,D0

It seems the compiler doesn't notice that the value in D0 has been
changed, and tries to do some optimization by initializing i from
what it thinks is a register containing 0 rather than CLRing i.
It looks like this won't show up very often, you have to assign the
same value to different variables, separated by only BSET's.
(This was fun to find, stick a printf before the loop and it works.)

--
Andrew Folkins        ...ihnp4!alberta!andrew    
The University of Alberta, Edmonton, Alberta, Canada      
 
"Gee, Mr. Wizard, are we going to discover the meaning of life?"
"We will if we're not careful, Timmy."