[ACCEPTED]-How can I see parse tree, intermediate code, optimization code and assembly code during COMPILATION?-disassembly

Accepted answer
Score: 21

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

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

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

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

I think this could be useful 1 for you.

Score: 12

You can see the preprocessor output with 5 -E. -fdump-tree-* dumps the tree internal represenation, e.g. -fdump-tree-all. Various 4 -d options exist to dump the RTL intermediate 3 representations, e.g. -fdump-rtl-all (see the manual for 2 the invidual passes that you get dumps of); in 1 addition, -dD dumps all macro definitions.

Score: 3

From the point of view of the clang compiler, you 19 can not see each and every output that is 18 generated by the compiler. This is because 17 clang works in a different way compared 16 to other compilers.

Lexical analysis

The tokens 15 can be emitted through:

clang test.c -Xclang -dump-tokens
clang test.c -Xclang -dump-raw-tokens

Intermediate code 14 generation

The byte code can be emitted through: clang 13 test.c -S -emit-llvm

Semantic analysis

The 12 semantic analysis is simultaneously performed 11 while the AST is being generated. The AST 10 can be emitted through:

clang test.c -Xclang -ast-dump
clang test.c -Xclang -ast-view (this generates a graph for the textual AST)

Code optimization

You 9 can query code optimizations through printing 8 the optimization pipeline as it is applied 7 to the c-code:

clang test.c -S -mllvm -print-after-all

Target code generation

The 6 generated code (i.e. the assembly output) can 5 be viewed through:

clang test.c -S


You can also see the complete 4 pipeline that clang invokes for a program. For 3 example, the pipeline for emitting an object 2 file can be viewd through:

clang -ccc-print-phases test.c -c

The output generated 1 on the terminal is:

0: input, "test.c", c
1: preprocessor, {0}, cpp-output
2: compiler, {1}, ir
3: backend, {2}, assembler
4: assembler, {3}, object

More Related questions