Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...