Ticket #10: emx-cconv-on-var-crash.diff

File emx-cconv-on-var-crash.diff, 11.2 KB (added by dmik, 14 years ago)

Fixes the GCC 4.4.x crash when applying function attributes like system to variables.

  • i386/emx.c

    diff -ur config.orig-patch/i386/emx.c config/i386/emx.c
    old new  
    6767            case POINTER_TYPE:      return "POINTER_TYPE   ";
    6868            case VOID_TYPE:         return "VOID_TYPE      ";
    6969            case INTEGER_TYPE:      return "INTEGER_TYPE   ";
    70             case CHAR_TYPE:         return "CHAR_TYPE      ";
    7170            case BOOLEAN_TYPE:      return "BOOLEAN_TYPE   ";
    72             case SET_TYPE:          return "SET_TYPE       ";
    7371            case ARRAY_TYPE:        return "ARRAY_TYPE     ";
    7472            case RECORD_TYPE:       return "RECORD_TYPE    ";
    7573            case QUAL_UNION_TYPE:   return "QUAL_UNION_TYPE";
     
    137135
    138136    fprintf(stderr, "dbg: node=%d %s %p %s '%s'  type=%d %s %p %s '%s'  type_type=%d %s %p %s '%s'  context=%d %s %p %s '%s' (%s)\n",
    139137            TREE_CODE(node), code(node), (void*)node, birddump_callingconv(node), birddump_name(node),
    140            g type ? (int)TREE_CODE(type) : -1, code(type), (void*)type, birddump_callingconv(type), birddump_name (type),
     138            type ? (int)TREE_CODE(type) : -1, code(type), (void*)type, birddump_callingconv(type), birddump_name (type),
    141139            type2 ? (int)TREE_CODE(type2) : -1, code(type2), (void*)type2, birddump_callingconv(type2), birddump_name (type2),
    142140            context ? (int)TREE_CODE(context) : -1, code(context), (void*)context, birddump_callingconv(context), birddump_name (context),
    143141            pszFunction);
     
    299297  return rc;
    300298}
    301299
    302 #if 0 /* choose between the smaller non working version, and the
    303          large ugly one which is working. */
    304 
    305 tree emx_handle_vacpp_attribute (tree *node, tree name, tree args,
    306   int flags, _Bool *no_add_attrs)
    307 {
    308   tree *type;
    309   (void) args;
    310   (void) flags;
    311 
    312   dfprintf((stderr, "emx_handle_vacpp_attribute\n"));
    313   DUMP (*node);
    314 
    315   if (TREE_CODE (*node) != FUNCTION_TYPE
    316       && TREE_CODE (*node) != METHOD_TYPE
    317       && TREE_CODE (*node) != FIELD_DECL
    318       && TREE_CODE (*node) != TYPE_DECL)
    319     {
    320       warning ("`%s' attribute only applies to functions",
    321                IDENTIFIER_POINTER (name));
    322       *no_add_attrs = true;
    323     }
    324 
    325   if (TARGET_64BIT)
    326     {
    327       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
    328       *no_add_attrs = true;
    329     }
    330 
    331   /* Check if the attributes are compatible */
    332   if (ix86_check_append_attr (*node, name, no_add_attrs))
    333     return NULL_TREE;
    334 
    335   /* Now, a new type with our attribute please!
    336      If the FUNCTION_TYPE/METHOD_TYPE already have the attribute associated,
    337      there is no need to duplicate the type.
    338      @todo: I'm unsure wether or not we should do this from node and down... */
    339   for (type = node;
    340        TREE_CODE (*type) != FUNCTION_TYPE && TREE_CODE (*type) != METHOD_TYPE;
    341        type = &TREE_TYPE (*type))
    342       /* do nothing */;
    343 
    344   if (lookup_attribute (IDENTIFIER_POINTER (name), TYPE_ATTRIBUTES (*type)))
    345       return NULL_TREE;
    346 
    347   *type = build_type_copy (*type);
    348   *type = build_type_attribute_variant (*type, tree_cons (name, args, TYPE_ATTRIBUTES (*type)) );
    349   /**no_add_attrs = true; ?? */
    350 
    351   dfprintf((stderr, "emx_handle_vacpp_attribute: new type (%p)\n", (void*)type));
    352   DUMP (*node);
    353   return NULL_TREE;
    354 }
    355 
    356 #else
    357 
    358 /* This is the working version which we belive duplicate some which could be
    359    done by the generic attribute stuff in GCC... */
    360 tree emx_handle_vacpp_attribute (tree *node, tree name, tree args, int flags, _Bool *no_add_attrs)
    361 {
    362   tree type;
    363 
    364   dfprintf((stderr, "emx_handle_vacpp_attribute: node=%p\n", (void *)node));
    365   DUMP (*node);
    366 
    367   switch (TREE_CODE (*node))
    368     {
    369       /* Declarations!
    370          We need to attach the attribute to the type of the declaration.
    371          (Name mangling is done by emx_c_set_decl_assembler_name().)  */
    372       case FUNCTION_DECL:/* Function declaration. */
    373       case TYPE_DECL:    /* Function or function pointer type. */
    374       case FIELD_DECL:   /* Function pointer (or function?) as a struct, union or class member. */
    375       case PARM_DECL:    /* Function pointer as a parameter. */
    376       case VAR_DECL:     /* Function pointer variable. */
    377         /* If this is a type declaration with our attribute, we allow it
    378            only if it is a pointer-to-a-function type or a function type. */
    379         type = TREE_TYPE (*node);
    380         if (TREE_CODE (type) == POINTER_TYPE)
    381           type = TREE_TYPE(type);
    382         if (   TREE_CODE (type) != FUNCTION_TYPE
    383             && TREE_CODE (type) != METHOD_TYPE)
    384           {
    385             warning ("`%s' attribute only applies to functions and function types, not to '%T'.",
    386                      IDENTIFIER_POINTER (name), type);
    387             *no_add_attrs = true;
    388             break;
    389           }
    390 
    391         if (ix86_check_append_attr (type, name, no_add_attrs))
    392           break;
    393 
    394         /* If required we'll make a variant of the type for this attribute. */
    395         if (!lookup_attribute (IDENTIFIER_POINTER (name),
    396                                TYPE_ATTRIBUTES (type)))
    397           {
    398             tree *ptype = node;
    399             while (*ptype != type)
    400                 ptype = &TREE_TYPE (*ptype);
    401             *ptype = build_variant_type_copy (*ptype);
    402             *ptype = build_type_attribute_variant (
    403                 *ptype, tree_cons (name, args, TYPE_ATTRIBUTES (*ptype)) );
    404             dfprintf((stderr, "emx_handle_vacpp_attribute: new type\n"));
    405             DUMP(*node);
    406           }
    407         else
    408           dfprintf((stderr, "emx_handle_vacpp_attribute: use old type\n"));
    409         *no_add_attrs = true;
    410         break;
    411 
    412       /* Types!
    413          For types involving functions we need to convince decl_attributes()
    414          (and its callers) to supply a declaration so we safely can change
    415          the type. */
    416       case POINTER_TYPE:
    417         /* We allow:
    418            This being the return type of a function which is coming soon.
    419            This being a function pointer which declaration is coming next.
    420            Everything else is considered inappropriate use of the attribute. */
    421         if (   !(flags & ATTR_FLAG_FUNCTION_NEXT)
    422             && (    !(flags & ATTR_FLAG_DECL_NEXT)
    423                 ||  !(type = TREE_TYPE (*node))
    424                 || (   TREE_CODE (type) != FUNCTION_TYPE
    425                     && TREE_CODE (type) != METHOD_TYPE)))
    426         {
    427             warning ("`%s' attribute only applies to functions and function types",
    428                      IDENTIFIER_POINTER (name));
    429             *no_add_attrs = true;
    430             break;
    431         }
    432         /* fall thru */
    433       case FUNCTION_TYPE:
    434       case METHOD_TYPE:
    435 #if 0 /* bird: This fixes optlink/tst4.c and doesn't seem to break anything.
    436                If set to true we are in some cases imposing _Optlink onto following
    437                declarations. This is weird stuff!
    438             !! This problem is actually what we see with the other implementation!! */
    439         *no_add_attrs = true;
    440 #endif
    441         return tree_cons (name, args, NULL_TREE);
    442 
    443       default:
    444         warning ("`%s' attribute only applies to functions and function types (code=%d)",
    445                  IDENTIFIER_POINTER (name), TREE_CODE (*node));
    446         *no_add_attrs = true;
    447         break;
    448     }
    449 
    450   return NULL_TREE;
    451 }
    452 #endif
    453 
    454300void
    455301emx_eh_frame_section ()
    456302{
  • i386/emx.h

    diff -ur config.orig-patch/i386/emx.h config/i386/emx.h
    old new  
    119119
    120120/* We want the _System attribute */
    121121#define TARGET_SYSTEM_DECL_ATTRIBUTES
    122 #define ix86_handle_system_attribute    emx_handle_vacpp_attribute
    123122
    124123/* We want the _Optlink attribute */
    125124#define TARGET_OPTLINK_DECL_ATTRIBUTES
    126 #define ix86_handle_optlink_attribute   emx_handle_vacpp_attribute
    127125
    128126/* Do our own mangling on some kinds of decls */
    129127#define TARGET_C_SET_DECL_ASSEMBLER_NAME(decl) \
     
    310308extern void emx_eh_frame_section (void);
    311309extern void emx_output_function_begin_epilogue (FILE *);
    312310
    313 /* This function handles both _System and _Optlink attributes. */
    314 extern tree emx_handle_vacpp_attribute PARAMS ((tree *, tree, tree, int, _Bool *));
    315 
    316311/* These functions are for handling dllexport and dllimport. */
    317312extern void i386_emx_record_exported_symbol (const char *, int);
    318313extern void i386_emx_file_end (void);
  • i386/i386.c

    diff -ur config.orig-patch/i386/i386.c config/i386/i386.c
    old new  
    2968529685static const struct attribute_spec ix86_attribute_table[] =
    2968629686{
    2968729687  /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
    29688 #ifdef EMX /** @todo: ugly stuff, cleanup later. */
    29689   /* Stdcall attribute says callee is responsible for popping arguments
    29690      if they are not variable.  */
    29691   { "stdcall",   0, 0, false, false, false, ix86_handle_system_attribute },
    29692   /* Fastcall attribute says callee is responsible for popping arguments
    29693      if they are not variable.  */
    29694   { "fastcall",  0, 0, false, false,  false,  ix86_handle_system_attribute },
    29695   /* Cdecl attribute says the callee is a normal C declaration */
    29696   { "cdecl",     0, 0, false, false, false, ix86_handle_system_attribute },
    29697 #else
    2969829688  /* Stdcall attribute says callee is responsible for popping arguments
    2969929689     if they are not variable.  */
    2970029690  { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
     
    2970329693  { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
    2970429694  /* Cdecl attribute says the callee is a normal C declaration */
    2970529695  { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
    29706 #endif
    2970729696  /* Regparm attribute specifies how many integer arguments are to be
    2970829697     passed in registers.  */
    2970929698  { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
     
    2972829717  { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
    2972929718  { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
    2973029719  /* End element.  */
    29731 #ifdef EMX /** @todo: ugly stuff, cleanup later. */
    2973229720#ifdef TARGET_SYSTEM_DECL_ATTRIBUTES
    2973329721  /* System says the function is extern "C" and is not underscored. */
    29734   { "system",    0, 0, false, false, false, ix86_handle_system_attribute },
     29722  { "system",    0, 0, false, true,  true,  ix86_handle_cconv_attribute },
    2973529723#endif
    2973629724#ifdef TARGET_OPTLINK_DECL_ATTRIBUTES
    2973729725  /* Optlink is like regparm with a few differences */
    29738   { "optlink",   0, 0, false, false, false, ix86_handle_optlink_attribute },
    29739 #endif
    29740 #else
    29741 #ifdef TARGET_SYSTEM_DECL_ATTRIBUTES
    29742   /* System says the function is extern "C" and is not underscored. */
    29743   { "system",    0, 0, false, true,  true,  ix86_handle_system_attribute },
    29744 #endif
    29745 #ifdef TARGET_OPTLINK_DECL_ATTRIBUTES
    29746   /* Optlink is like regparm with a few differences */
    29747   { "optlink",   0, 0, false, true,  true,  ix86_handle_optlink_attribute },
    29748 #endif
     29726  { "optlink",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
    2974929727#endif
    2975029728  { NULL,        0, 0, false, false, false, NULL }
    2975129729};