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

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

问题描述

我正在学习编译器课程,程序的编译遵循以下步骤

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

  1. 词法分析
  2. 语法分析
  3. 语义分析
  4. 中间代码生成
  5. 代码优化
  6. 目标代码生成.

如何查看每个步骤的输出,例如我想在语法分析后查看解析树.

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

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

I am compiling program on Linux machine with GCC compiler.

我们可以通过gcc中的-Wa编译器选项查看程序的汇编代码,类似的还有查看Tokens、Parse tree和Inetmediate代码的选项.

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.

推荐答案

虽然你可以使用 -fdump-tree-all-fdump-rtl-all 选项在 gcc 中,我不认为他们的输出对编译器学生很有用.FWIW,我开始研究 gcc 作为我博士研究的一部分,已经完成了两门本科课程,我发现 gcc 及其调试文件不透明且难以理解.

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.

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

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.

我建议改用 LLVM,它以设计精良且易于遵循而著称.另一种选择是从教科书下载代码,尤其是 Appel 书籍,假设它可用.

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.

另一个建议,如果我可以推荐一下我自己的,是使用 phc.使用 phc,您可以将解析树视为图像,并在每次编译器通过后查看 AST 和源代码.这里是部分AST和解析树的比较.它们是使用 phc 轻松生成的.您可以看到编译器 IR、CFG、SSA 形式以及类型推断和别名分析的调试输出.您还可以打开和关闭优化和传递以查看它们的效果.

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.

我认为这对您有用.

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

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