How can I get the abstract syntax tree of a c program in gcc?
I'm trying to automatically insert OpenMP pragmas to the input c program.
I need to analyze nested for loops for finding dependencies so that I can insert appropriate OpenMP pragmas.
So basically what I want to do is traverse and analyze the abstract syntax tree of the input c program.
How do I achieve this?
-
3They say Clang is better suited for this task than GCC. I have not tried either though, so no guarantees.n. m. could be an AI– n. m. could be an AI2013-02-28 11:05:54 +00:00Commented Feb 28, 2013 at 11:05
-
But I need to use gcc onlyVishal Vijay– Vishal Vijay2013-02-28 11:11:07 +00:00Commented Feb 28, 2013 at 11:11
-
3@VishalVijay: This sounds like an artificial constraint. Why must you use only GCC?Ira Baxter– Ira Baxter2013-02-28 11:36:58 +00:00Commented Feb 28, 2013 at 11:36
-
If you're committed to GCC here's another possibility which is out of your reach but which otherwise might be helpful ... rosecompiler.orgHigh Performance Mark– High Performance Mark2013-02-28 12:54:50 +00:00Commented Feb 28, 2013 at 12:54
2 Answers
You need full dataflow to find 'dependencies'. Then you will need to actually insert the OpenMP calls.
What you want is a program transformation system. GCC probably has the dependency information, but it is famously difficult to work with for custom projects. Others have mentioned Clang and Rose. Clang might be a decent choice, but custom analysis/transformation isn't its main purpose. Rose is designed to support custom tools, but IMHO is a rather complicated scheme to use in practice because of its use of the EDG front end, which isn't designed to support transformation.
[THE FOLLOWING TEXT WAS DELETED BY A MODERATOR. I HAVE PUT IT BACK, BECAUSE IT IS ONE THE VALID TRANSFORMATION SYSTEMS FOR THIS TASK. THE FACT THAT I AM RESPONSIBLE FOR IT IN NO WAY DIMINISHES ITS VALUE AS A USEFUL ANSWER TO THE OP.]
Our DMS Software Reengineering Toolkit with its C front end is explicitly designed to be a program transformation system. It has full data flow analysis (including points-to analysis, call graph construction and range analyses) tied to the AST in sensible ways. It provides source-to-source rewrite rules enabling changes to the ASTs expressed in surface syntax form; you can read the transformations rather than inspect a bunch of procedural code. With a modified AST, DMS can regenerate source code including the comments in a compilable form.
2 Comments
Not exactly an AST but GCCXML might help http://linux.die.net/man/1/gccxml
edit : as stated by Ira Baxter gccxml does not output information about function/methods bodies. Here's a fork that seems to fix that lack http://sourceforge.net/projects/gccxml-bodies/