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: About changing the tree structure in pragmas


I managed to create function call within the gcc's abstract syntax
tree (AST) as a side effect of executing a pragma. I post a simple
example how it is done, in case someone else sticks on this simple
point like me :)

Example problem scenario:
Suppose that we have the following "C" source file:

int test_fn_call()
{
   printf("in test_fn_call\n");
   return 0;
}

int main(void)
{
   #pragma test_pragma
   printf("Hello World\n");
   return 0;
}

We want when "#pragma test_pragma" is met to build a call to the
"test_fn_call" function.

The expected output is:
in test_fn_call
Hello World

Problem solution:
Below is the code that handles the execution of the "#pragma test_pragma"

static void handle_pragma_test(cpp_reader *ARG_UNUSED(dummy))
{
   tree fn_decl;
   tree fn_call;
   tree fn_id;

   fn_id = get_identifier("test_fn_call");
   fn_decl = lookup_name(fn_id);

   fn_call = build_function_call_expr(fn_decl, NULL_TREE);
   add_stmt(fn_call);
} /* End of void handle_pragma_test */


-- Ferad Zyulkyarov Barcelona Supercomputing Center



On 11/30/06, Ferad Zyulkyarov <feradz@gmail.com> wrote:
Hi,

I am trying to implement a pragma that will change the tree structure
of the program. Although I looked at some basic tutorials about
implementing front-ends for gcc and looked at the implementation of
the currently available pragmas I am not well recognized with the
gcc's interface for manipulating the program's tree structure.

I will appreciate your advices on implementing the following scenario:

code example:

1: printf("Outside pragma 1");
2: #pragma test_pragma
3: {
4: /* the pragma is supposed to put 'printf("test_pragma");' just here
before all statements that follow */
5: printf("Inside pragma 1");
6: }
7: printf("Outside pragma 2");

Expected output:
Outside pragma 1
test_pragma
Inside pragma 1
Outside pragma 2

I suppose that probably there is a way to do this like:
tree* function_call = build(FUNCTION_CALL, "printf", param_list);

Also, I would be very thankful for any resources about the API for
manipulating the program's tree structure.

Thanks,
Ferad Zyulkyarov



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