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

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

问题描述

我创建了一个树转储,这里描述的是:

据我所知,gcc 不是学习 AST 表示的最佳方式.但无论如何,了解图像内容的含义会很好.

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

解决方案

您链接的答案显示了如何获取 控制流图来自 GCC 调试转储.因此,您的图像实际上并未显示语法树.

GCC C 前端没有经典意义上的抽象语法树.许多语法结构在解析过程中被降低,通常是一堆 goto .例如,c_finish_loop 有这个:

/* 如果我们有一个退出条件,那么我们使用 goto 构建一个 IF出循环,或到它的顶部.如果没有退出条件,然后我们只是建立一个跳回顶部.*/exit = build_and_jump (&LABEL_EXPR_LABEL (top));

if 语句变成了 COND_EXPR 节点.您可以在 .original 转储中看到它(其中 COND_EXPR 节点像 C if 语句一样打印).但是没有从该传递生成的 .dot 文件.编译过程一旦进入中端,就是GIMPLE,而GIMPLE(作为SSA变种)不代表使用forif等高级语言结构的控制流完全没有声明.

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天全站免登陆