public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* plugin: modify attribute arguments inside plugin
@ 2014-08-02  8:25 Klemen Jan Enova
  2014-08-03  7:31 ` Klemen Jan Enova
  0 siblings, 1 reply; 2+ messages in thread
From: Klemen Jan Enova @ 2014-08-02  8:25 UTC (permalink / raw)
  To: gcc-help

I would like to code a plugin, that would count the number of accesses 
to a field of some C struct.
That struct would be marked with __attribute__((access_count)). When 
handling that attribute, I
would like to assign an index to it (as an attribute argument), so that 
I can update the proper
element of my counter array, when there is a COMPONENT_REF with a marked 
struct as the target.

But attribute arguments are normally passed by the user, can the plugin 
change them, and how?
Juts checking that argument would be simpler and faster than looking for 
the target's name
(in a hashtable, or just an ordered array if there aren't going to be so 
many tracked structs)
and getting an index from that.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: plugin: modify attribute arguments inside plugin
  2014-08-02  8:25 plugin: modify attribute arguments inside plugin Klemen Jan Enova
@ 2014-08-03  7:31 ` Klemen Jan Enova
  0 siblings, 0 replies; 2+ messages in thread
From: Klemen Jan Enova @ 2014-08-03  7:31 UTC (permalink / raw)
  To: gcc-help

I should have coded the plugin before asking. Sorry for that. I hope
this will provide context.

Here is the function, that sets the attribute argument:

static tree handle_field_access_count_attribute(tree *node, tree name,
tree args, int flags,
bool *no_add_attrs)
{
  tree field;

  *no_add_attrs = true;
  if(TREE_CODE(*node) == RECORD_TYPE || TREE_CODE(*node) == UNION_TYPE) {
    if(args != NULL_TREE)
      error("attribute does not take any arguments");

/* TODO: Let TREE_VALUE(args) equal curr_index */

    for(field = TYPE_FIELDS(*node), field, field = TREE_CHAIN(field))
      curr_index++;

      any_tracked_structs = true;
}

  error("attribute applies to struct and union types only");

  return NULL_TREE;
}

Here is the function, that uses the argument:

static size_t get_offset_from_component_ref(tree cref)
{
/*
* If foo is an instance of struct bar with the field x
* [cref: foo.x] -> [target: foo], [field: x]
*/
  tree base = GET_OPERAND(cref, 0);
  tree field = GET_OPERAND(cref, 1);
  tree seek_field = TYPE_FIELDS(base);
  size_t offset;

/*
* TODO: The offset of the first field of the struct was stored
* in the attribute arguments, get that.
*/

  while(strcmp(IDENTIFIER_POINTER(seek_field), IDENTIFIER_POINTER(field))
  {
   offset++;
   TREE_CHAIN(seek_field);
  }

  return offset;
}

The whole source is here: https://github.com/jkenova/field-count

2014-08-02 10:25 GMT+02:00 Klemen Jan Enova <klemen.jan.enova@gmail.com>:
> I would like to code a plugin, that would count the number of accesses to a
> field of some C struct.
> That struct would be marked with __attribute__((access_count)). When
> handling that attribute, I
> would like to assign an index to it (as an attribute argument), so that I
> can update the proper
> element of my counter array, when there is a COMPONENT_REF with a marked
> struct as the target.
>
> But attribute arguments are normally passed by the user, can the plugin
> change them, and how?
> Juts checking that argument would be simpler and faster than looking for the
> target's name
> (in a hashtable, or just an ordered array if there aren't going to be so
> many tracked structs)
> and getting an index from that.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-08-03  7:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-02  8:25 plugin: modify attribute arguments inside plugin Klemen Jan Enova
2014-08-03  7:31 ` Klemen Jan Enova

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).