From: jones@bsm.biochemistry.ucl.ac.UK (David Jones)
Subject: gcc 2.3.1 caller-saves bug
Organization: GNUs Not Usenet
Distribution: gnu
Date: Mon, 9 Nov 1992 01:39:41 GMT

The following piece of code causes a segmentation fault when run after
compilation with gcc 2.3.1. The same code works fine with gcc 2.2.2 and
several other compilers I have tried.

System: Solbourne 5/602 (33 MHz SPARC)
OS: 4.1A.1 (corresponds to SunOS 4.1.1)
gcc config: --target=sparc-sun-sunos4.1
Compilation command line: gcc -O2 gccbug.c

The bug appears to be linked to the -fcaller-saves optimization
flag as the bug does not manifest itself with the following compile
command: gcc -O2 -fno-caller-saves gccbug.c

Also the following command produces a fatal compiler error:
gcc -O -fcaller-saves gccbug.c

Reading specs from /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.1/specs
gcc version 2.3.1
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.1/cpp -lang-c -v -undef -D__GNUC__=2 -Dsparc -Dsun -Dunix -D__sparc__ -D__sun__ -D__unix__ -D__sparc -D__sun -D__unix -D__OPTIMIZE__ gccbug.c /usr/tmp/cca29543.i
GNU CPP version 2.3.1 (sparc)
 /usr/local/lib/gcc-lib/sparc-sun-sunos4.1/2.3.1/cc1 /usr/tmp/cca29543.i -quiet -dumpbase gccbug.c -O -version -fcaller-saves -o /usr/tmp/cca29543.s
GNU C version 2.3.1 (sparc) compiled by GNU C version 2.3.1.
gcc: Internal compiler error: program cc1 got fatal signal 6

------ SOURCE CODE: gccbug.c ------
extern char *malloc();
extern int rand();

main()
{
    char w[1000];
    int i, j, nres, t[1000];
    float dv, (**dmat)[4][4];
    short at1, at2;

    nres = 200;			/* runs fine with small numbers! */

    for (i = 0; i < nres; i++)
    {
	w[i] = rand() & 15;
	t[i] = 0;
    }

    dmat = (float (**)[4][4]) malloc(nres * sizeof(*dmat));
    if (!dmat)
	return 1;
    for (i = 0; i < nres; i++)
    {
	dmat[i] = (float (*)[4][4]) malloc(nres * sizeof(**dmat));
	if (!dmat[i])
	    return 1;
    }

    for (i = 0; i < nres; i++)
	for (j = i; j < nres; j++)
	    for (at1 = 0; at1 <= 3; at1++)
		for (at2 = 0; at2 <= 3; at2++)
		    if (i != j || at1 != at2)
			if ((w[i] & (1 << at1)) && (w[j] & (1 << at2)))
			{
			    dv = 20.0 * (rand() & 32767) / 32768.0;
			    dmat[i][j][at1][at2] = dmat[j][i][at2][at1] = dv;
			    if (at1 == 0 && at2 == 0 && abs(i - j) > 1 && dv <= 14.0)
			    {
				t[i]++;
				t[j]++;
			    }
			}
			else
			    dmat[i][j][at1][at2] = dmat[j][i][at2][at1] = 999.0;
		    else
			dmat[i][j][at1][at2] = 0.0;

    return 0;
}

------ GDB 4.5 output ------
Program received signal 11, Segmentation fault
0x2614 in main () at gccbug.c:30
(gdb) list 30
25		if (!dmat[i])
26		    return 1;
27	    }
28	
29	    for (i = 0; i < nres; i++)
30		for (j = i; j < nres; j++)
31		    for (at1 = 0; at1 <= 3; at1++)
32			for (at2 = 0; at2 <= 3; at2++)
33			    if (i != j || at1 != at2)
34				if ((w[i] & (1 << at1)) && (w[j] & (1 << at2)))
(gdb) info frame
Stack level 0, frame at 0xf7fffdb8:
 pc = 0x2614 in main (gccbug.c:30); saved pc 0x206c
 source language c.
 Arglist at 0xf7fffdb8, args: 
 Locals at 0xf7fffdb8, Previous frame's sp is 0xf7fffdb8
 Saved registers:
  l0 at 0xf7ffe988, l1 at 0xf7ffe98c, l2 at 0xf7ffe990, l3 at 0xf7ffe994,
  l4 at 0xf7ffe998, l5 at 0xf7ffe99c, l6 at 0xf7ffe9a0, l7 at 0xf7ffe9a4,
  i0 at 0xf7ffe9a8, i1 at 0xf7ffe9ac, i2 at 0xf7ffe9b0, i3 at 0xf7ffe9b4,
  i4 at 0xf7ffe9b8, i5 at 0xf7ffe9bc, fp at 0xf7ffe9c0, i7 at 0xf7ffe9c4
(gdb) info registers
g0             0x0	0
g1             0xf8000000	-134217728
g2             0xf7fffda8	-134218328
g3             0xf7fffffc	-134217732
g4             0xf7fffda8	-134218328
g5             0x0	0
g6             0x0	0
g7             0x0	0
o0             0x4	4
o1             0x1d8320	1934112
o2             0x89b8	35256
o3             0xc	12
o4             0x4479c000	1148829696
o5             0x0	0
sp             0xf7ffe988	-134223480
o7             0x2454	9300
l0             0x96	150
l1             0x0	0
l2             0x4	4
l3             0x3	3
l4             0xc	12
l5             0x30	48
l6             0x6120	24864
l7             0x2540	9536
i0             0x3	3
i1             0x0	0
i2             0xffffec78	-5000
i3             0xf7fffffc	-134217732
i4             0x254	596
i5             0x4	4
fp             0xf7fffdb8	-134218312
i7             0x2064	8292
y              0x7fd41bb2	2144607154
psr            0x11001086	285216902
wim            0x0	0
tbr            0x0	0
pc             0x2614	9748
npc            0x2618	9752
fpsr           0x0	0
cpsr           0x0	0
(gdb) info locals
w = {"\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\


005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t\016\017\f\005\n\013\b\001\006\a\004\r\002\003\000\t"...}
i = 0
j = 150
t = {0 <repeats 1000 times>}
dv = 11.8621826
dmat = (float (**)[4][4]) 0x6120
at1 = 4
at2 = 4
(gdb) 

------ ASSEMBLER SOURCE ------
gcc2_compiled.:
.stabs "/home/bsm/jones/",100,0,0,Ltext0
.stabs "gccbug.c",100,0,0,Ltext0
.text
Ltext0:
.stabs "int:t1=r1;-2147483648;2147483647;",128,0,0,0
.stabs "char:t2=r2;0;127;",128,0,0,0
.stabs "long int:t3=r1;-2147483648;2147483647;",128,0,0,0
.stabs "unsigned int:t4=r1;0;-1;",128,0,0,0
.stabs "long unsigned int:t5=r1;0;-1;",128,0,0,0
.stabs "short int:t6=r1;-32768;32767;",128,0,0,0
.stabs "long long int:t7=r1;0;-1;",128,0,0,0
.stabs "short unsigned int:t8=r1;0;65535;",128,0,0,0
.stabs "long long unsigned int:t9=r1;0;-1;",128,0,0,0
.stabs "signed char:t10=r1;-128;127;",128,0,0,0
.stabs "unsigned char:t11=r1;0;255;",128,0,0,0
.stabs "float:t12=r1;4;0;",128,0,0,0
.stabs "double:t13=r1;8;0;",128,0,0,0
.stabs "long double:t14=r1;8;0;",128,0,0,0
.stabs "void:t15=15",128,0,0,0
.data
	.align 8
LC0:
	.double 0r20
	.align 8
LC1:
	.double 0r32768
	.align 8
LC2:
	.double 0r14
.text
	.align 4
LC3:
	.single 0r999
	.align 4
LC4:
	.single 0r0
	.align 4
	.global _main
	.proc	04
_main:
.stabn 68,0,5,LM1
LM1:
	!#PROLOGUE# 0
	save %sp,-4096,%sp
	add %sp,-1072,%sp
	!#PROLOGUE# 1
.stabn 68,0,6,LM2
LM2:
LBB2:
.stabn 68,0,5,LM3
LM3:
	call ___main,0
	mov 0,%l1
.stabn 68,0,13,LM4
LM4:
	sethi %hi(-5000),%o0
	or %o0,%lo(-5000),%l3
	add %fp,-16,%l2
	mov %l2,%l0
L5:
.stabn 68,0,15,LM5
LM5:
	call _rand,0
	nop
	add %l2,%l1,%o1
	and %o0,15,%o0
	stb %o0,[%o1-1000]
.stabn 68,0,16,LM6
LM6:
	st %g0,[%l0+%l3]
.stabn 68,0,13,LM7
LM7:
	add %l1,1,%l1
	cmp %l1,200
	bl L5
	add %l0,4,%l0
.stabn 68,0,19,LM8
LM8:
	mov 200,%g3
	sll %g3,2,%l0
	call _malloc,0
	mov %l0,%o0
.stabn 68,0,20,LM9
LM9:
	orcc %o0,%g0,%l6
	bne L6
	mov 200,%g1
.stabn 68,0,21,LM10
LM10:
L37:
	b L35
	mov 1,%i0
L6:
.stabn 68,0,22,LM11
LM11:
	cmp %g1,0
	be L38
	mov 0,%l1
	mov 0,%i0
.stabn 68,0,24,LM12
LM12:
	mov 200,%g3
L39:
	call _malloc,0
	sll %g3,6,%o0
.stabn 68,0,25,LM13
LM13:
	cmp %o0,0
	be L37
	st %o0,[%i0+%l6]
.stabn 68,0,22,LM14
LM14:
	add %i0,4,%i0
	cmp %i0,%l0
	bl L39
	mov 200,%g3
.stabn 68,0,29,LM15
LM15:
	mov 0,%l1
L38:
	cmp %l1,200
	bge,a L35
	mov 0,%i0
	mov 0,%o5
	add %fp,-16,%g2
	st %g2,[%fp-4]
	ld [%fp-4],%f4
.stabn 68,0,30,LM16
LM16:
	cmp %l1,200
L44:
	bge L14
	mov %l1,%l0
	st %f4,[%fp-4]
	ld [%fp-4],%g4
	sll %l1,2,%i1
	add %i1,%g2,%g1
	sethi %hi(-5020),%g3
	or %g3,%lo(-5020),%g3
	add %g3,%fp,%g3
	st %g1,[%g3]
	ld [%g3],%f6
L33:
	mov 0,%i5
.stabn 68,0,31,LM17
LM17:
	sll %l0,6,%l7
	mov 0,%l2
.stabn 68,0,32,LM18
LM18:
L43:
	sll %i5,16,%o0
	sra %o0,16,%l3
	sll %l3,4,%l5
	sll %l0,2,%i4
	sll %l3,2,%l4
	sethi %hi(-5000),%o0
	or %o0,%lo(-5000),%i2
	st %f6,[%fp-4]
	ld [%fp-4],%i3
	cmp %l1,%l0
L42:
	bne L40
	add %g2,%l1,%o0
	sll %l2,16,%o0
	sra %o0,16,%o0
	cmp %l3,%o0
	be L24
	add %g2,%l1,%o0
.stabn 68,0,34,LM19
LM19:
L40:
	ldsb [%o0-1000],%o0
	sra %o0,%l3,%o0
	andcc %o0,1,%g0
	be L26
	add %g2,%l0,%o0
	ldsb [%o0-1000],%o1
	sll %l2,16,%o0
	sra %o0,16,%i0
	sra %o1,%i0,%o1
	andcc %o1,1,%g0
	be L26
	sethi %hi(-5032),%g1
.stabn 68,0,36,LM20
LM20:
	or %g1,%lo(-5032),%g1
	add %g1,%fp,%g1
	st %g2,[%g1]
	sethi %hi(-5036),%g3
	or %g3,%lo(-5036),%g3
	add %g3,%fp,%g3
	st %g4,[%g3]
	sethi %hi(-5040),%g1
	or %g1,%lo(-5040),%g1
	add %g1,%fp,%g1
	st %o5,[%g1]
	sethi %hi(-5044),%g3
	or %g3,%lo(-5044),%g3
	add %g3,%fp,%g3
	st %f4,[%g3]
	sethi %hi(-5048),%g1
	or %g1,%lo(-5048),%g1
	add %g1,%fp,%g1
	call _rand,0
	st %f6,[%g1]
	sethi %hi(32767),%g3
	or %g3,%lo(32767),%g3
	and %o0,%g3,%o0
	sethi %hi(-5020),%g1
	or %g1,%lo(-5020),%g1
	add %g1,%fp,%g1
	st %o0,[%g1]
	ld [%g1],%f5
	fitod %f5,%f2
	sethi %hi(LC0),%g1
	ldd [%g1+%lo(LC0)],%f0
	fmuld %f2,%f0,%f2
	sethi %hi(LC1),%g3
	ldd [%g3+%lo(LC1)],%f0
	fdivd %f2,%f0,%f2
.stabn 68,0,37,LM21
LM21:
	sethi %hi(-5040),%g1
	or %g1,%lo(-5040),%g1
	add %g1,%fp,%g1
	ld [%g1],%o5
	sll %i0,4,%o1
.stabn 68,0,38,LM22
LM22:
	sethi %hi(-5036),%g1
.stabn 68,0,37,LM23
LM23:
	ld [%l6+%i4],%o0
.stabn 68,0,38,LM24
LM24:
	or %g1,%lo(-5036),%g1
.stabn 68,0,37,LM25
LM25:
	ld [%l6+%i1],%o2
	add %o5,%o0,%o0
	add %o1,%o0,%o1
	sll %i0,2,%o0
	add %l7,%o2,%o2
.stabn 68,0,36,LM26
LM26:
	fdtos %f2,%f2
.stabn 68,0,37,LM27
LM27:
	add %l5,%o2,%o2
.stabn 68,0,38,LM28
LM28:
	sethi %hi(-5032),%g3
	or %g3,%lo(-5032),%g3
	add %g3,%fp,%g3
.stabn 68,0,37,LM29
LM29:
	st %f2,[%o1+%l4]
	st %f2,[%o2+%o0]
.stabn 68,0,38,LM30
LM30:
	ld [%g3],%g2
	add %g1,%fp,%g1
	sethi %hi(-5044),%g3
	or %g3,%lo(-5044),%g3
	ld [%g1],%g4
	add %g3,%fp,%g3
	sethi %hi(-5048),%g1
	or %g1,%lo(-5048),%g1
	ld [%g3],%f4
	add %g1,%fp,%g1
	cmp %l3,0
	bne L23
	ld [%g1],%f6
	cmp %i0,0
	bne L41
	add %l2,1,%o0
	subcc %l1,%l0,%o0
	bneg,a L28
	sub %g0,%o0,%o0
L28:
	cmp %o0,1
	ble L23
	sethi %hi(LC2),%g3
	fstod %f2,%f2
	ldd [%g3+%lo(LC2)],%f0
	fcmped %f2,%f0
	nop
	fbug L41
	add %l2,1,%o0
.stabn 68,0,40,LM31
LM31:
	ld [%g4+%i2],%o0
	add %o0,1,%o0
	st %o0,[%g4+%i2]
.stabn 68,0,41,LM32
LM32:
	ld [%i3+%i2],%o0
	add %o0,1,%o0
.stabn 68,0,43,LM33
LM33:
	b L23
	st %o0,[%i3+%i2]
L26:
.stabn 68,0,45,LM34
LM34:
	sll %l2,16,%o3
	sra %o3,16,%o3
	ld [%l6+%i4],%o0
	sll %o3,4,%o1
	ld [%l6+%i1],%o2
	sll %o3,2,%o3
	sethi %hi(1148829696),%o4
	add %o5,%o0,%o0
	add %o1,%o0,%o1
	st %o4,[%o1+%l4]
	add %l7,%o2,%o2
	add %l5,%o2,%o2
	b L23
	st %o4,[%o2+%o3]
L24:
.stabn 68,0,47,LM35
LM35:
	ld [%l6+%i1],%o0
	add %l7,%o0,%o0
	add %l5,%o0,%o0
	st %g0,[%o0+%l4]
.stabn 68,0,32,LM36
LM36:
L23:
	add %l2,1,%o0
L41:
	mov %o0,%l2
	sll %o0,16,%o0
	sra %o0,16,%o0
	cmp %o0,3
	ble L42
	cmp %l1,%l0
.stabn 68,0,31,LM37
LM37:
	add %i5,1,%o0
	mov %o0,%i5
	sll %o0,16,%o0
	sra %o0,16,%o0
	cmp %o0,3
	ble,a L43
	mov 0,%l2
.stabn 68,0,30,LM38
LM38:
	sethi %hi(-5020),%g1
	or %g1,%lo(-5020),%g1
	add %g1,%fp,%g1
	st %f6,[%g1]
	ld [%g1],%g3
	add %l0,1,%l0
	add %g3,4,%g1
	st %g1,[%g1]
	cmp %l0,200
	bl L33
	ld [%g1],%f6
.stabn 68,0,29,LM39
LM39:
L14:
	add %o5,64,%o5
	sethi %hi(-5020),%g1
	or %g1,%lo(-5020),%g1
	add %g1,%fp,%g1
	st %f4,[%g1]
	ld [%g1],%g3
	add %l1,1,%l1
	add %g3,4,%g1
	st %g1,[%g1] <----- **** CRASHES HERE ****
	cmp %l1,200
	bl L44
	ld [%g1],%f4
.stabn 68,0,49,LM40
LM40:
	mov 0,%i0
L35:
.stabn 68,0,50,LM41
LM41:
LBE2:
	ret
	restore
.stabs "main:F1",36,0,0,_main
.stabs "w:16=ar1;0;999;2",128,0,0,-1016
.stabs "i:r1",64,0,0,17
.stabs "j:r1",64,0,0,16
.stabs "t:17=ar1;0;999;1",128,0,0,-5016
.stabs "dv:r12",64,0,0,34
.stabs "dmat:r18=*19=*20=ar1;0;3;21=ar1;0;3;12",64,0,0,22
.stabs "at1:r6",64,0,0,29
.stabs "at2:r6",64,0,0,18
.stabn 192,0,0,LBB2
.stabn 224,0,0,LBE2

Newsgroups: gnu.gcc.bug