Understanding -fdump-tree output gcc with GraphViz

泄露秘密 提交于 2021-02-07 03:11:01

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/54162940/understanding-fdump-tree-output-gcc-with-graphviz

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!