I think the question's title is self explanatory, I want to dump an abstract syntax tree generated by gcc into a .dot file (Those files generated by Graphviz) because then I want to view it in a .png file or similar. Is there any way I can do that?
Thanks in advance :)
In the first step of lexical analysis, the code will be broken down into smaller pieces called tokens. In the next step of syntax analysis, the tokens are converted into a tree called the abstract syntax tree. The structure of the tree is similar to the code structure.
AST is an acronym for Abstract Syntax Tree. It is a representation of tokens generated from statements and expressions in a programming language. With the AST, the interpreter or the compiler can generate machine code or evaluate an instruction.
There are two methods, both including two steps
Using GCC internal vcg support
Compile your code (say test.c) with vcg dumps
gcc -fdump-tree-vcg -g test.c
Use any third party tool to get dot output from vcg
graph-easy test.c.006t.vcg --as_dot
Compile with raw dumps and then preprocess them with some scripts to form dot files (like in this useful article)
Both methods have their own good and bad sides -- with first you can really get only one dump of AST before gimple translation, but it is easy. With second you may convert any raw dump to dot-format, but you must support scripts, that is overhead.
What to prefer -- is on your own choice.
UPD: times are changing. Brand new option for gcc 4.8.2 makes it possible to generate dot files immediately. Just supply:
gcc test.c -fdump-tree-all-graph
and you will get a plenty of already formatted for you dot files:
test.c.008t.lower.dot
test.c.012t.cfg.dot
test.c.016t.ssa.dot
... etc ...
Please be sure to use new versions of GCC with this option.
According to the man page, you can get this information via -fdump-
command.
Let's look at a dummy example:
// main.c
int sum(int a, int b) {
return a + b;
}
int main(void) {
if (sum(8, 10) < 20) {
return -1;
}
return 1;
}
For gcc 7.3.0:
gcc -fdump-tree-all-graph main.c -o main
There are a lot of options to get the necessary information. Check out the manual for this info.
After that, you'll get many files. Some of them with .dot respresentation(graph option is used):
main.c.003t.original
main.c.004t.gimple
main.c.006t.omplower
...
main.c.011t.cfg
main.c.011t.cfg.dot
...
With GraphViz we can retrieve a pretty-printed graph for each function:
dot -Tpng main.c.011t.cfg.dot -o main.png
You'll get something like this: main.png
There are a lot of developer options which can help you understand how compiler process your file at a low level: GCC Developer Options
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With