public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Klemen Jan Enova <klemen.jan.enova@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Re: plugin: modify attribute arguments inside plugin
Date: Sun, 03 Aug 2014 07:31:00 -0000	[thread overview]
Message-ID: <CACbjx2X0OC14A+uR0gV8hVG+b7XObF3c3eizdq3x7bW00R6nRQ@mail.gmail.com> (raw)
In-Reply-To: <53DCA062.1000707@gmail.com>

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.

      reply	other threads:[~2014-08-03  7:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-02  8:25 Klemen Jan Enova
2014-08-03  7:31 ` Klemen Jan Enova [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CACbjx2X0OC14A+uR0gV8hVG+b7XObF3c3eizdq3x7bW00R6nRQ@mail.gmail.com \
    --to=klemen.jan.enova@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).