使用GraphViz了解-fdump-tree输出gcc [英] Understanding -fdump-tree output gcc with GraphViz

查看:138
本文介绍了使用GraphViz了解-fdump-tree输出gcc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个树转储,其描述方式如下:

据我所知,gcc并不是学习AST表示法的最佳方法.但是无论如何,了解图像内容的含义将是一件很不错的事情.

特别是这里的符号和 FREQ:0 语句是什么意思?

解决方案

您链接的答案显示了如何获取.因此,您的图像实际上没有显示语法树.

GCC C前端没有经典意义上的抽象语法树.在解析过程中,许多语法构造都降低了,通常是一堆 goto .例如, c_finish_loop 具有以下内容:

 /*如果存在退出条件,则可以使用gotos构建IF跳出循环或跳到顶部.如果没有退出条件,那么我们只需跳回到顶部即可.*/出口= build_and_jump(& LABEL_EXPR_LABEL(顶部)); 

if 语句被转换为 COND_EXPR 节点.您可以在 .original 转储(在其中像C if 语句一样打印 COND_EXPR 节点)中看到这一点.但是,该过程没有生成 .dot 文件.一旦编译过程进入中间阶段,它就是GIMPLE,而GIMPLE(作为SSA变体)不使用高级语言结构(例如 for if )表示控制流.声明.

Clang具有更传统的AST,由 clang -Xclang -ast-dump 打印.它仍然不是Graphviz的合适输入,但至少数据在那里.如果您的目标是了解GCC,请查看C ++前端,它在解析器中保留了更丰富的结构.

I've created a tree dump how described here: How can I dump an abstract syntax tree generated by gcc into a .dot file? for this dummy script:

int fact(int n) {
    if (n<=1) {
        return 1;
    }
    return n * fact(n-1);
}

int main(void) {
    int a = 4;
    int res = fact(a);
    return res;
}

And the image what I've got:

As I know gcc is not the best way to learn AST representation. But anyway it would be nice to understand what image's content means.

Especially what % sign here means and a FREQ:0 statement?

解决方案

The answer you link shows how to obtain the Control Flow Graph from GCC debugging dumps. So your image does not actually show a syntax tree.

The GCC C front end does not have an abstract syntax tree in the classical sense. A lot of syntactic constructs are lowered during parsing, often to a bunch of gotos. For example, c_finish_loop has this:

  /* If we have an exit condition, then we build an IF with gotos either
     out of the loop, or to the top of it.  If there's no exit condition,
     then we just build a jump back to the top.  */
  exit = build_and_jump (&LABEL_EXPR_LABEL (top));

if statements are turned into COND_EXPR nodes. You can see that in the .original dump (where the COND_EXPR node is printed like a C if statement). But there's no .dot file generated from that pass. Once the compilation process enters the middle end, it's GIMPLE, and GIMPLE (as an SSA variant) does not represent control flow using high-level language constructs such as for and if statements at all.

Clang has a more traditional AST, printed by clang -Xclang -ast-dump. It's still not suitable input for Graphviz, but at least the data is there. If your goal is to understand GCC, have a look at the C++ front end, which preserves a richer structure in the parser.

这篇关于使用GraphViz了解-fdump-tree输出gcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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