如何在COMPILATION期间看到解析树,中间代码,优化代码和汇编代码? [英] How can I see parse tree, intermediate code, optimization code and assembly code during COMPILATION?

查看:273
本文介绍了如何在COMPILATION期间看到解析树,中间代码,优化代码和汇编代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Compilers课程,程序编译遵循以下步骤


  1. 词汇分析

  2. 语法分析

  3. 语义分析

  4. 中间代码生成

  5. 代码优化

  6. 目标代码生成。

如何查看每个步骤的输出,例如我想在语法分析后看到解析树。 / p>

我在Linux机器上使用GCC编译器编译程序。



我们可以看到程序的汇编代码 - 在gcc中的编译器选项,类似地有选项可以看到令牌,解析树和Inetmediate代码。

解决方案

虽然可以使用 -fdump-tree-all -fdump-rtl-all 选项在gcc,我不认为他们的输出对编译器学生非常有用。 FWIW,我开始工作gcc作为我的博士研究的一部分,已经完成两个本科课程,我发现 gcc 和它的调试文件是不透明的,很难跟踪。



此外,gcc并不真正遵循编译器的教科书设计。没有人真的,因为它不能很好地工作。我很确定gcc不产生解析树或抽象语法树。它会构建一个IR(称为gimple),在其上执行其高级优化。



我建议尝试LLVM,这是有良好设计的声誉并容易跟随。另一个选择是从教科书,特别是Appel书下载代码,假设它可用。



另一个建议,如果我可以推荐自己的一会儿,是请使用 phc
使用phc,您可以将解析树视为一个图像,并在编译器中的每一次传递之后查看AST和源代码。 这里是对AST和解析树。它们是使用phc简单生成的。在数据流分支上,您可以看到编译器IR, CFG,SSA形式,以及类型推断和别名分析的调试输出。您也可以开启和关闭优化和传递,以查看他们的效果。



我认为这对你很有用。


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.

解决方案

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. On the dataflow branch, 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.

这篇关于如何在COMPILATION期间看到解析树,中间代码,优化代码和汇编代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆