From: c164@po.berkeley.edu (Class Account)
Subject: Bug in "..." argument passing
Organization: GNUs Not Usenet
Distribution: gnu
Date: Tue, 8 Sep 1992 09:38:51 GMT

After compiling the attached program under gcc 2.2.2 on a DECstation
5000, execution should give the output

	(10)
	(20)

but instead yields 

	(2)
	(10)

In the file below, I have set the definition of SIZE to 1.  If SIZE is
set to 4, the output should be

	(10, 11, 12, 13)
	(20, 21, 22, 23)

but is instead

	(0, 0, 2, 2)
	(0, 2, 2, 10)

This is partially due to a bug in va-mips.h that I reported earlier.
Even when that bug is fixed (or I think fixed), however, the output is
wrong.  That is, when va-mips.h is altered so that the va_args
definition reads

    #define va_arg(AP, mode)						\
      ((mode *)(AP = (char *) ((__alignof(mode) > 4			\
				    ? ((int)AP + 8 - 1) & -8		\
				    : ((int)AP + 4 - 1) & -4)           \
			       + sizeof(mode))))[-1]

the output of the executed program is still

	(2, 10, 11, 13)
	(20, 21, 22, 23)

which is not quite correct.

Finally, when the function f is altered to return an int instead of a
T (and the return statement is altered accordingly), the output of the
executed program is correct.


P. N. Hilfinger


----------------------------------------------------------------------

Here is the compilation transcript:

po 66% gcc -v -o glorp -O glorp.c
Reading specs from /home/b/fa92/c164/lib/gcc-lib/mips-dec-ultrix/2.2.2/specs
gcc version 2.2.2
 /home/b/fa92/c164/lib/gcc-lib/mips-dec-ultrix/2.2.2/cpp -lang-c -v -undef -D__GNUC__=2 -D__ANSI_COMPAT -DMIPSEL -DR3000 -DSYSTYPE_BSD -D_SYSTYPE_BSD -Dbsd4_2 -Dhost_mips -Dmips -Dultrix -Dunix -D__ANSI_COMPAT -D__MIPSEL__ -D__R3000__ -D__SYSTYPE_BSD__ -D___SYSTYPE_BSD__ -D__bsd4_2__ -D__host_mips__ -D__mips__ -D__ultrix__ -D__unix__ -D__ANSI_COMPAT -D__MIPSEL -D__R3000 -D__SYSTYPE_BSD -D___SYSTYPE_BSD -D__bsd4_2 -D__host_mips -D__mips -D__ultrix -D__unix -D__OPTIMIZE__ -D__LANGUAGE_C__ -D__LANGUAGE_C -DLA
NGUAGE_C glorp.c /usr/tmp/cca22006.i
GNU CPP version 2.2.2 [AL 1.1, MM 19] DECstation running ultrix
 /home/b/fa92/c164/lib/gcc-lib/mips-dec-ultrix/2.2.2/cc1 /usr/tmp/cca22006.i -mgpopt -quiet -dumpbase glorp.c -O -version -o /usr/tmp/cca22006.s
GNU C version 2.2.2 [AL 1.1, MM 19] DECstation running ultrix compiled by GNU C version 2.2.2.
 as -nocpp -EL -O2 -v -o glorp.o /usr/tmp/cca22006.s
/usr/lib/cmplrs/cc/as0 -v -G 8 -EL -g0 -O2 /usr/tmp/cca22006.s -o /tmp/ctmaa22011 -t /tmp/ctmsta22011 
as0: f main 
0.0u 0.0s 0:00 23% 58+61k 0+6io 5pf+0w
/usr/lib/cmplrs/cc/as1 -v -G 8 -p0 -EL -g0 -O2 /tmp/ctmaa22011 -o glorp.o -t /tmp/ctmsta22011 
as1: f main 
0.0u 0.0s 0:00 26% 100+83k 0+4io 7pf+0w
 /home/b/fa92/c164/lib/gcc-lib/mips-dec-ultrix/2.2.2/mips-tfile -v -o glorp.o /usr/tmp/cca22006.s
mips-tfile version 2.2.2 [AL 1.1, MM 19] DECstation running ultrix
 /home/b/fa92/c164/lib/gcc-lib/mips-dec-ultrix/2.2.2/ld -EL -o glorp /usr/lib/cmplrs/cc/crt0.o -L/home/b/fa92/c164/lib/gcc-lib/mips-dec-ultrix/2.2.2 -L/usr/lib/cmplrs/cc -L/usr/lib/cmplrs/cc -L/home/b/fa92/c164/lib glorp.o -lgcc -lc -lgcc

----------------------------------------------------------------------


Here is the source file glorp.c:

------------------------------ cut here ------------------------------

#include <stdarg.h>


#define SIZE 1

typedef struct {
   int A[SIZE];
} T;

T f(int x, ...)
{
    va_list ap;
    T X;
    int i,j;

    va_start(ap, x);
    for (j = 0; j < x; j += 1) {
	X = va_arg(ap, T);
    
	printf("(");
	for (i = 0; i < SIZE; i += 1)
	    printf("%d%s", X.A[i], i == SIZE-1 ? ")\n" : ", ");
    }
    va_end(ap);
    return X;
}


main()
{
    T X, Y;
    int i;

    for (i = 0; i < SIZE; i += 1) {
	X.A[i] = i + 10;
	Y.A[i] = i + 20;
    }

    (void) f(2, X, Y);
}

Newsgroups: gnu.gcc.bug