[gnu.gcc.bug] serious bug in inline functions.

glenne%hplsla@HP-SDE.SDE.HP.COM (Glenn Engel) (05/02/89)

gcc-1.35 / hp9k320g.h

Here's a nasty little bug dealing with inline functions. 
In the function main below, 4 bytes of local space are allocated but when
the second call to printit is made, the compiler makes use of a temporary int 
at -6 from fp, thus munching part of the stack.  The program appears to run
fine but has very serious side affects when the contents of the stack are
used in other functions.

gcc version 1.35
 /usr/local/lib/gcc-cpp -v -undef -D__GNUC__ -Dhp9000s200 -Dhp9000s300 -DPWB -Dmc68k -Dhpux -Dunix -D__hp9000s200__ -D__hp9000s300__ -D__PWB__ -D__mc68k__ -D__hpux__ -D__unix__ -D__HAVE_68881__ -Dmc68020 t.c /tmp/cca17275.cpp
GNU CPP version 1.35
 /usr/local/lib/gcc-cc1 /tmp/cca17275.cpp -quiet -dumpbase t.c -version -o /tmp/cca17275.s
GNU C version 1.35 (68k, MIT syntax) compiled by GNU C version 1.35.
 /usr/local/lib/gcc-as -mc68020 -o t.o /tmp/cca17275.s
 /usr/local/lib/gcc-ld /usr/local/lib/gnu/crt0.o t.o /usr/local/lib/gcc-gnulib -lc


typedef unsigned char usc;

printit(usc * cptr)
{
  printf("%c=\n",*cptr);
  }

static inline void doit(usc *cptr)
{
  printit(cptr);
  }

static inline void doitadr(usc ch)
{
  printit(&ch);
  }

main()
{
  usc ch;

  ch = 'a';

  doit(&ch);
  doitadr(ch);
  }


#NO_APP
gcc_compiled.:
.text
LC0:
	.ascii "%c=\12\0"
	.even
.globl _printit
_printit:
	link a6,#0
	movel a6@(8),a0
	clrl d0
	moveb a0@,d0
	movel d0,sp@-
	pea LC0
	jbsr _printf
L1:
	unlk a6
	rts
	.even
.globl _main
_main:
	link a6,#-4                  <************   only 4 bytes
	movel d2,sp@-
	moveb #97,a6@(-1)
	movel a6,d2
	subql #1,d2
	movel a6,d0
	subql #2,d0
	movel d2,sp@-
	jbsr _printit
	addqw #4,sp
L7:
	clrl d0
	moveb a6@(-1),d0
	movel d0,a6@(-6)            <**************  offset -6 used
	movel a6,d0
	subql #4,d0
	moveb a6@(-3),d0
	movel a6,d0
	subql #3,d0
	movel d0,sp@-
	jbsr _printit
	addqw #4,sp
L8:
L6:
	movel a6@(-8),d2
	unlk a6
	rts

--
 |  Glenn R. Engel
 |  Hewlett-Packard 
 |  (206) 335-2066
 |  glenne%hplsla@hplabs.hp.com