4

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?

4
  • 3
    They say Clang is better suited for this task than GCC. I have not tried either though, so no guarantees. Commented Feb 28, 2013 at 11:05
  • But I need to use gcc only Commented Feb 28, 2013 at 11:11
  • 3
    @VishalVijay: This sounds like an artificial constraint. Why must you use only GCC? Commented 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.org Commented Feb 28, 2013 at 12:54

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

The OP clearly states that he wants to use gcc only... That seems like a poor choice but that is his choice
@Ira Baxter thanks for the info. I will discuss about this with my team and get back to you later..
0

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/

5 Comments

gccxml doesn't produce ASTs or in fact anything for the bodies of functions. OP clearly wants ASTs of his loop code.
You are right, thanks for pointing that.I edited the answer to point to a gccxml fork that dumps function bodies too.
OK, so how does he get dependences between computations using those ASTs?
Clearly not an easy task.I guess the product you promote will do the job just fine.
DMS has a variety of dependence analyses built-in, yes, and a framework for building others. What OP wants to do isn't easy even with a good dependence analysis foundation. IMHO, its probably hopeless if he starts without one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.