Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a pass to gcc?

Has anybody added a pass to gcc ? or not really a pass but adding an option to do some nasty things... :-) ...

I still have the same problem about calling a function just before returning from another...so I would like to investigate it by implementing something in gcc...

Cheers.

EDIT: Adding a pass to a compiler means revisiting the tree to perform some optimizations or some analysis. I would like to emulate the behavior of __cyg_profile_func_exit but only for some functions and be able to access the original return value.

So I'm going to try to enhance my question. I would like to emulate really basic AOSD-like behavior. AOSD or Aspect oriented programming enables to add crosscutting concerns (debugging is a cross-cutting concern).

int main(int argc, char ** argv) {
  return foo(argc);
}

int foo(int arg_num) { 
   int result = arg_num > 3 ? arg_num : 42;
   return result;
}

int dbg(int returned) {
   printf("Return %d", returned);
}

I would like to be able to say, I'd like to trigger the dbg function after function foo has been executed. The problem is how to tell the compiler to modify the control flow and execute dbg. dbg should be executed between return and foo(argc) ...

That's really like __cyg_profile_function_exit but only in some cases (and the problem in __cyg_profile_function_exit is that you cannot easily see and modify the returned value).

like image 425
LB40 Avatar asked Apr 08 '09 20:04

LB40


2 Answers

If you still are interested in adding a GCC pass, you can start reading up GCC Wiki material just about that:

  • http://gcc.gnu.org/wiki/WritingANewPass and "Implementing Passes" from http://www.airs.com/dnovillo/200711-GCC-Internals/ on how to, well, add a pass.
  • The intermediate representation you are interested in is called GIMPLE. Some introduction is at http://www.airs.com/dnovillo/200711-GCC-Internals/200711-GCC-Internals-3-IR.pdf
  • Other information at http://gcc.gnu.org/wiki/GettingStarted
like image 165
Laurynas Biveinis Avatar answered Oct 19 '22 23:10

Laurynas Biveinis


Just for future reference: Upcoming versions of gcc (4.4.0+) will provide support for plugins specifically meant for use cases such as adding optimization passes to the compiler without having to bootstrap the whole compiler.

May 6, 2009:GCC can now be extended using a generic plugin framework on host platforms that support dynamically loadable objects. (see gcc.gnu.org)

like image 37
none Avatar answered Oct 20 '22 01:10

none