This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] may_alias attribute


I tried to implement a may_alias attribute for the C/C++ front-ends.
The spesification is something like "types with this attribute behave
like char*, ie. can alias anything".

I think the patch succeeds in setting the attribute, but in
c_common_get_alias_set the TYPE_MAY_ALIAS_P test is never successful.
I clearly haven't understood fully how types are propagated across GCC
or something, so if someone with more knowledge could take a look at
this and give me some feedback, even if it's only "you idiot, that'll
never work, go away and stop bothering us", that would be nice.

Index: attribs.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/attribs.c,v
retrieving revision 1.11
diff -c -3 -p -r1.11 attribs.c
*** attribs.c	17 Jan 2002 20:34:40 -0000	1.11
--- attribs.c	25 Jan 2002 07:59:22 -0000
*************** static tree handle_no_limit_stack_attrib
*** 82,87 ****
--- 82,89 ----
  						     bool *));
  static tree handle_pure_attribute	PARAMS ((tree *, tree, tree, int,
  						 bool *));
+ static tree handle_may_alias_attribute	PARAMS ((tree *, tree, tree, int,
+ 						 bool *));
  static tree handle_deprecated_attribute	PARAMS ((tree *, tree, tree, int,
  						 bool *));
  static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
*************** static const struct attribute_spec c_com
*** 140,145 ****
--- 142,149 ----
  			      handle_no_limit_stack_attribute },
    { "pure",                   0, 0, true,  false, false,
  			      handle_pure_attribute },
+   { "may_alias",              0, 0, false, true,  false,
+ 			      handle_may_alias_attribute },
    { "deprecated",             0, 0, false, false, false,
  			      handle_deprecated_attribute },
    { "vector_size",	      1, 1, false, true, false,
*************** handle_nocommon_attribute (node, name, a
*** 479,484 ****
--- 483,513 ----
  {
    if (TREE_CODE (*node) == VAR_DECL)
      DECL_COMMON (*node) = 0;
+   else
+     {
+       warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
+       *no_add_attrs = true;
+     }
+ 
+   return NULL_TREE;
+ }
+ 
+ /* Handle a "may_alias" attribute; arguments as in
+    struct attribute_spec.handler.  */
+ 
+ static tree
+ handle_may_alias_attribute (node, name, args, flags, no_add_attrs)
+      tree *node;
+      tree name;
+      tree args ATTRIBUTE_UNUSED;
+      int flags ATTRIBUTE_UNUSED;
+      bool *no_add_attrs;
+ {
+   if (TYPE_P(*node)) {
+     TYPE_MAY_ALIAS (*node) = 1;
+     warning("setting may_alias");
+     debug_tree(*node);
+     }
    else
      {
        warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
Index: c-common.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/c-common.c,v
retrieving revision 1.287
diff -c -3 -p -r1.287 c-common.c
*** c-common.c	15 Jan 2002 22:27:07 -0000	1.287
--- c-common.c	25 Jan 2002 07:59:24 -0000
*************** c_common_get_alias_set (t)
*** 2239,2244 ****
--- 2239,2252 ----
        && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
      return 0;
  
+   
+   /* If it has the may_alias attribute, it can alias anything.  */
+   if (TYPE_P(t) && TYPE_MAY_ALIAS(t)) {
+       warning("returning alias set 0 because of may_alias");
+       debug_tree(t);
+       return 0;
+   }
+   
    /* That's all the expressions we handle specially.  */
    if (! TYPE_P (t))
      return -1;
Index: print-tree.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/print-tree.c,v
retrieving revision 1.53
diff -c -3 -p -r1.53 print-tree.c
*** print-tree.c	22 Jan 2002 14:33:32 -0000	1.53
--- print-tree.c	25 Jan 2002 07:59:24 -0000
*************** print_node (file, prefix, node, indent)
*** 514,519 ****
--- 514,521 ----
  
        if (TYPE_PACKED (node))
  	fputs (" packed", file);
+       if (TYPE_MAY_ALIAS (node))
+ 	fputs (" may-alias", file);
  
        if (TYPE_LANG_FLAG_0 (node))
  	fputs (" type_0", file);
Index: tree.h
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/tree.h,v
retrieving revision 1.299
diff -c -3 -p -r1.299 tree.h
*** tree.h	10 Jan 2002 18:51:15 -0000	1.299
--- tree.h	25 Jan 2002 07:59:25 -0000
*************** struct tree_block
*** 1123,1128 ****
--- 1123,1131 ----
  #define TYPE_NONALIASED_COMPONENT(NODE) \
    (ARRAY_TYPE_CHECK (NODE)->type.transparent_union_flag)
  
+ /* Indicates that objects of this type can alias anything. */
+ #define TYPE_MAY_ALIAS(NODE) (TYPE_CHECK (NODE)->type.may_alias)
+ 
  /* Indicated that objects of this type should be laid out in as
     compact a way as possible.  */
  #define TYPE_PACKED(NODE) (TYPE_CHECK (NODE)->type.packed_flag)
*************** struct tree_type
*** 1206,1211 ****
--- 1209,1216 ----
    unsigned restrict_flag : 1;
    unsigned pointer_depth : 2;
  
+   unsigned may_alias : 1;
+     
    unsigned lang_flag_0 : 1;
    unsigned lang_flag_1 : 1;
    unsigned lang_flag_2 : 1;

And here's a testcase I've been using to test it:

#include <stdio.h>

#define MAY_ALIAS __attribute__((may_alias))

typedef short* short_a MAY_ALIAS;

int main(void)
{
    int a = 0x12345678;
    short_a b = (short_a)&a;
 
    printf ("%x\n", a);
    b[1] = 0;
    printf ("%x\n", a);
 
    return 0;
}

--
Osku Salerma - osku@iki.fi - http://www.iki.fi/osku/
Information wants to be free.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]