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: How to parse a code snippet at front-end plugin


On Sat, 11 Sep 2010 22:19:06 +0800 (CST)
"Thinker K.F. Li" <thinker@codemud.net> wrote:

> Hi,
> 
> I am try to make a plugin to analyze GIMPLE before genericize.  It
> will insert some code defined by user into the tree.  I want the user
> to specify the inserted code in C.  For example,
> 
> --------------------------------------------------
> {
> 	static int i=0;
> 
> 	printf("Hello %d\n", i++);
> }
> --------------------------------------------------
> 
> I try to find a way to make C language parser of GCC to compile it for
> me, and I will insert the code into the tree created from the main
> source.  But, it seems provide only functions to parse a complete C
> source file, and I try to push a buffer to the reader of libcpp.  But,
> it does not work.

I am not sure that there exist enough plugin hooks to do that (I am not sure even if there are plugin hooks to front-ends in GCC). However, I do see some way to perhaps implement what you suggest even in 4.5

First, don't think of your problem as adding C code. Think of it as adding Gimple stuff into some Gimple representation.
(I believe it is a case of aspect oriented programming, see http://en.wikipedia.org/wiki/Aspect-oriented_programming for more). But you want the user to specify the added code, using the already accepted C syntax of GCC (i.e. without hacking GCC parser).

So you could use attributes & pragmas or builtins for that purpose.

First, your user define its code snipped to insert as a C function with a special attribute, e.g.

  void say_hello(void) __attribute__((added_chunk));

  void say_hello(void) 
  { 
     static int i;
     printf("Hello %d\n", i++);
  }

Then, your user would mark with another attribute every function into which your chunk should be added, perhaps

  int some_complex_function(int a, void*b) __attribute((insert_chunk(say_hello)));

Or perhaps have a pragram asking this to insert the chunk in every function whose name start with some_

  #pragma GCCMELT insert_chunk_prefix(say_hello, some_)

The point here is that with the tricks above, you don't have to change GCC parser! and you could implement that using a plugin coded in C (this is not so simple, you have to filter Gimple). Better yet, you could implement that using GCC MELT http://gcc.gnu.org/wiki/MELT which provides pattern matching facilities on Gimple to make such tasks much simpler.

You still have to understand Gimple & Tree in detail and find a good position in the pass machinery to insert your new pass or passes.

If you succeed in making such a plugin please tell us.  I would be delighted if you coded it in MELT, as a MELT extension.

Good luck.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
8, rue de la Faiencerie, 92340 Bourg La Reine, France
*** opinions {are only mine, sont seulement les miennes} ***


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