How can I see parse tree, intermediate code, optimization code and assembly code during COMPILATION?

While you can use the -fdump-tree-all and -fdump-rtl-all options in gcc, I don’t think that their output is very useful to a compiler student. FWIW, I started working on gcc as part of my PhD studies, having already completed two undergraduate courses, and I found gcc and its debug files to be opaque and hard to follow.

In addition, gcc doesn’t really follow the textbook design of compilers. No-one does, really, because it doesn’t work well that way. I’m pretty sure gcc doesn’t produce a parse tree, or an abstract-syntax-tree. It does build an IR (called gimple) on which to perform its high-level optimizations.

I would suggest to try LLVM instead, which has a reputation for being well designed and easy to follow. Another alternative is to download the code from a textbook, especially the Appel book, assuming its available.

Another suggestion, if I may recommend my own for a moment, is to use phc.
With phc, you can see the parse tree as an image, and view the AST and the source code after every single pass in the compiler. Here is a comparison of parts of the AST and the parse tree. They are generated trivially using phc. You can see the compiler IRs, the CFG, SSA form, and debug output of type inference and alias analysis. You can also turn optimizations and passes on and off to see the effect that they have.

I think this could be useful for you.

Leave a Comment