【问题标题】:Understanding -fdump-tree output gcc with GraphViz使用 GraphViz 理解 -fdump-tree 输出 gcc
【发布时间】:2019-01-12 19:03:51
【问题描述】:

我已经为这个虚拟脚本创建了一个树转储:How can I dump an abstract syntax tree generated by gcc into a .dot file?

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;
}

以及我所拥有的图像:

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

尤其是这里的% 符号和FREQ:0 声明是什么意思?

【问题讨论】:

    标签: c gcc abstract-syntax-tree graphviz dot


    【解决方案1】:

    您链接的答案显示了如何从 GCC 调试转储中获取 Control Flow Graph。所以你的图片实际上并没有显示语法树。

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

      /* 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 语句转换为COND_EXPR 节点。您可以在 .original 转储中看到这一点(其中 COND_EXPR 节点像 C if 语句一样打印)。但是没有从该通道生成.dot 文件。一旦编译过程进入中间端,就是GIMPLE,而GIMPLE(作为SSA的变体)根本不代表使用forif语句等高级语言结构的控制流。

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

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 1970-01-01
      • 2018-09-27
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 2013-02-23
      相关资源
      最近更新 更多