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

Viewed 15452

I am studying Compilers course, compilation of program follows below steps

  1. Lexical analysis
  2. Syntax analysis
  3. Semantic analysis
  4. Intermediate code generation
  5. Code optimization
  6. Target code generation.

How can I see output of each step e.g I want to see parse tree after syntax analysis.

I am compiling program on Linux machine with GCC compiler.

We can see assembly code of the program by using -Wa compiler option in gcc, similarly is there options to see Tokens, Parse tree and Inetmediate code.

3 Answers

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

Lexical analysis

The tokens can be emitted through:

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

Intermediate code generation

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

Semantic analysis

The semantic analysis is simultaneously performed while the AST is being generated. The AST 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 can query code optimizations through printing the optimization pipeline as it is applied to the c-code:

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

Target code generation

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

clang test.c -S

Bonus

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

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

The output generated 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
Related