This is the mail archive of the gcc@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]

Re: c plugin guidance wanted


On Sun, 2018-07-29 at 13:41 -0700, Joe Perches wrote:
> I would like to implement a gcc plugin that can compress an
> __attribute__((format(printf, x, y))) const char array before
> storage so that the formats can be size reduced when stored and
> expanded appropriately before use.
> 
> As this is the first plugin I am trying to implement, I'm a bit
> lost in details and I am looking for guidance.
> 
> Basically:
> 
> Take the printf_format_type CONST_DECL format being verified by
> gcc/c-format-main.c and after the verification, compress the
> format and its length before storage into its .read_only section.
> 
> A custom vsnprintf routine would expand the compressed format to
> its original form before use.
> 
> All tips to necessary gcc plugin implementation details appreciated.

If I understand your idea correctly, you're wanting to write a plugin
that turns something like:

  printf ("some format string %s %i %s etc\n", arg1, arg2, arg3);

into something like:

  your_custom_printf ("cmprssd_frmt_strng", arg1, arg2, arg3);

I don't know how familiar with the internals of GCC you are.  The way
GCC stores functions internally has three phases:
* tree-like structures, coming out of the frontends, which become...
* "gimple" statements, in SSA form, in linked lists within basic blocks
in a CFG [1], which become...
* lower-level rtx instructions, initially in a CFG, which eventually
become assembler.
(I'm probably oversimplifying here).

So for any plugin pass you need to choose whether it's going to work on
the gimple form, or on the rtx form, and where to put your pass so that
it interacts well with gcc's other passes.

You can see the changing internal representation by passing in
  -fdump-tree-all -fdump-ipa-all -fdump-rtl-all
and then wading through the dumpfiles, or, for functions with non-
trivial control flow:
  -fdump-tree-all-graph -fdump-ipa-all-graph -fdump-rtl-all-graph
to get .dot files, if you have GraphViz handy.

It sounds like you can implement the above transformation by purely
manipulating one call statement, changing which function is to be
called, and manipulating the format string argument.  I believe this
could be done either on gimple, or on rtl.

Note that we already do some optimizations where e.g.
  printf ("%s", string);
get turned into:
  puts (string);

So you might want to decide how your optimization ought to interact
with those ones: would you want a "compressed_puts", or to do your
optimization first so that this one doesn't happen? etc

Hope this is helpful
Dave

[1] (strictly speaking, it takes a few passes before gimple statements
are in a CFG, and then a little later into SSA form)


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