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

后端 未结 3 660
春和景丽
春和景丽 2020-11-30 05:53

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

  1. Lexical analysis
  2. Syntax analysis
  3. Semantic analysis
  4. Interm
3条回答
  •  执念已碎
    2020-11-30 06:11

    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
    

提交回复
热议问题