bootstrap bugfix

Jeffrey A Law law@cygnus.com
Sat Dec 26 09:31:00 GMT 1998


Examine this hunk of code from genrtl.c...  Note the if/elseif clauses.
Naughty Naughty...



static rtx obstack_alloc_rtx (length)
     register int length;
{
  rtx rt = (rtx) obstack_alloc (rtl_obstack, length);

  if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(int))
    *(int *)rt = 0;
  else if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(HOST_WIDE_INT))
    *(HOST_WIDE_INT *)rt = 0;
  else
    bzero((char *) rt, sizeof(struct rtx_def) - sizeof(rtunion));

  return rt;
}



This can cause problems if this function is inlined.  Consider:

rtx
gen_rtx_fmt_i0 (code, mode, arg0)
     RTX_CODE code;
     enum machine_mode mode;
     int arg0;
{
  rtx rt = obstack_alloc_rtx (sizeof (struct rtx_def) + 1 * sizeof (rtunion));
  PUT_CODE (rt, code);
  PUT_MODE (rt, mode);
  XINT (rt, 0) = arg0;

  return rt;
}


We could end up moving the code which clears the first few bytes of "rt"
beyond the PUT_CODE/PUT_MODE stores.

gcc knows how to internally expand bzero and memset calls which clear memory
into efficient code, so it seems kind of silly to keep this losing code in
gengenrtl.c.


This patch fixes the problem:

        * gengenrtl.c (gencode): Always use bzero to clear memory instead
        of dangerous casts and stores.

Index: gengenrtl.c
===================================================================
RCS file: /egcs/carton/cvsfiles/egcs/gcc/gengenrtl.c,v
retrieving revision 1.17
diff -c -3 -p -r1.17 gengenrtl.c
*** gengenrtl.c	1998/12/16 20:56:21	1.17
--- gengenrtl.c	1998/12/26 17:29:08
*************** gencode (f)
*** 268,279 ****
    fputs ("static rtx obstack_alloc_rtx (length)\n", f);
    fputs ("     register int length;\n{\n", f);
    fputs ("  rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n", f);
!   fputs ("  if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(int))\n", f);
!   fputs ("    *(int *)rt = 0;\n", f);
!   fputs ("  else if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(HOST_WIDE_INT))\n", f);
!   fputs ("    *(HOST_WIDE_INT *)rt = 0;\n", f);
!   fputs ("  else\n", f);
!   fputs ("    bzero((char *) rt, sizeof(struct rtx_def) - sizeof(rtunion));\n\n", f);
    fputs ("  return rt;\n}\n\n", f);
  
    for (fmt = formats; *fmt; ++fmt)
--- 268,274 ----
    fputs ("static rtx obstack_alloc_rtx (length)\n", f);
    fputs ("     register int length;\n{\n", f);
    fputs ("  rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n", f);
!   fputs ("  bzero((char *) rt, sizeof(struct rtx_def) - sizeof(rtunion));\n\n", f);
    fputs ("  return rt;\n}\n\n", f);
  
    for (fmt = formats; *fmt; ++fmt)





More information about the Gcc-patches mailing list