将 gcc -fdump-tree-all-raw 选项生成的 .orignal 文件转换成能被 graphviz 用来生成图片的 dot 文件。
1)代码
int main(int argc, char *argv[]){
int i=0;
int sum=0;
for(i=0; i<10; i++){
sum = sum + i;
}
return sum;
}
2)运行命令
gcc -fdump-tree-original-raw ./test.c
3)
a.out test.c test.c.004t.original
生成了a.out 和 test.c.004t.original
4)分析
gcc test.c -fdump-tree-all-graph
会生成更多文件,目前不知道这玩意 有什么用。
5)
test.c.004t.original的组成:
;; Function main (null)
;; enabled by -tree-original@1 statement_list 0 : @2 1 : @3
@2 bind_expr type: @4 vars: @5 body: @6
@3 return_expr type: @4 expr: @7
@4 void_type name: @8 algn: 8
@5 var_decl name: @9 type: @10 scpe: @11
srcp: test.c:3 init: @12
size: @13 algn: 32 used: 1
@6 statement_list 0 : @14 1 : @15 2 : @16
3 : @17 4 : @18 5 : @19
6 : @20 7 : @21 8 : @22
9 : @23 10 : @24
@7 modify_expr type: @10 op 0: @25 op 1: @12
@8 type_decl name: @26 type: @4
@9 identifier_node strg: i lngt: 1
@10 integer_type name: @27 size: @13 algn: 32
prec: 32 sign: signed min : @28
max : @29
@11 function_decl name: @30 type: @31 srcp: test.c:2
args: @32 link: extern
@12 integer_cst type: @10 int: 0
@13 integer_cst type: @33 int: 32
@14 decl_expr type: @4
@15 decl_expr type: @4
@16 modify_expr type: @10 op 0: @5 op 1: @12
@17 goto_expr type: @4 labl: @34
@18 label_expr type: @4 name: @35
@19 modify_expr type: @10 op 0: @36 op 1: @37
@20 postincrement_expr type: @10 op 0: @5 op 1: @38
@21 label_expr type: @4 name: @34
@22 cond_expr type: @4 op 0: @39 op 1: @40
op 2: @41
@23 label_expr type: @4 name: @42
@24 return_expr type: @4 expr: @43
@25 result_decl type: @10 scpe: @11 srcp: test.c:2
note: artificial size: @13
algn: 32
@26 identifier_node strg: void lngt: 4
@27 type_decl name: @44 type: @10
@28 integer_cst type: @10 int: -2147483648
@29 integer_cst type: @10 int: 2147483647
@30 identifier_node strg: main lngt: 4
@31 function_type size: @13 algn: 32 retn: @10
prms: @45
@32 parm_decl name: @46 type: @10 scpe: @11
srcp: test.c:2 argt: @10
size: @13 algn: 32 used: 0
@33 integer_type name: @47 size: @48 algn: 128
prec: 128 sign: unsigned min : @49
max : @50
@34 label_decl type: @4 scpe: @11 note: artificial
@35 label_decl type: @4 scpe: @11 note: artificial
@36 var_decl name: @51 type: @10 scpe: @11
srcp: test.c:4 init: @12
size: @13 algn: 32 used: 1
@37 plus_expr type: @10 op 0: @36 op 1: @5
@38 integer_cst type: @10 int: 1
@39 le_expr type: @10 op 0: @5 op 1: @52
@40 goto_expr type: @4 labl: @35
@41 goto_expr type: @4 labl: @42
@42 label_decl type: @4 scpe: @11 note: artificial
@43 modify_expr type: @10 op 0: @25 op 1: @36
@44 identifier_node strg: int lngt: 3
@45 tree_list valu: @10 chan: @53
@46 identifier_node strg: argc lngt: 4
@47 identifier_node strg: bitsizetype lngt: 11
@48 integer_cst type: @33 int: 128
@49 integer_cst type: @33 int: 0
@50 integer_cst type: @33 int: -1
@51 identifier_node strg: sum lngt: 3
@52 integer_cst type: @10 int: 9
@53 tree_list valu: @54 chan: @55
@54 pointer_type size: @56 algn: 64 ptd : @57
@55 tree_list valu: @4
@56 integer_cst type: @33 int: 64
@57 pointer_type size: @56 algn: 64 ptd : @58
@58 integer_type name: @59 size: @60 algn: 8
prec: 8 sign: unsigned min : @61
max : @62
@59 type_decl name: @63 type: @58
@60 integer_cst type: @33 int: 8
@61 integer_cst type: @58 int: 0
@62 integer_cst type: @58 int: -1
@63 identifier_node strg: char lngt: 4
这玩意可能就是AST树。
但是目前不会分析。
6)
pre.awk如下:
#! /usr/bin/gawk -f
/^[^;]/{
gsub(/^@/, "~@", $0);
gsub(/( *):( *)/, ":", $0);
print;
}
7)
treeviz.awk如下:
#! /usr/bin/gawk -f
#https://blog.youkuaiyun.com/l919898756
BEGIN {RS = "~@"; printf "digraph G {\n node [shape = record];";}
/^[0-9]/{
s = sprintf("%s [label = \"{%s | {", $1, $1);
for(i = 2; i < NF - 1; i++)
s = s sprintf("%s | ", $i);
s = s sprintf("%s}}\"];\n", $i);
$0 = s;
while (/([a-zA-Z]+):@([0-9]+)/){
format = sprintf("\\1 \\3\n %s:\\1 -> \\2;", $1);
$0 = gensub(/([a-zA-Z]+):@([0-9]+)(.*)$/, format, "g");
};
printf " %s\n", $0;
}
END {print "}"}
8)
chmod +x pre.awk treeviz.awk
9)
./pre.awk test.c.* | ./treeviz.awk > tree.dot
生成了dot文件
10)
dot -Tpng tree.dot -o tree.png
报了很多警告
Warning: node 16, port type unrecognized
Warning: node 17, port type unrecognized
Warning: node 18, port type unrecognized
Warning: node 19, port type unrecognized
Warning: node 20, port type unrecognized
Warning: node 21, port type unrecognized
Warning: node 22, port type unrecognized
Warning: node 23, port type unrecognized
Warning: node 24, port type unrecognized
Warning: node 25, port type unrecognized
Warning: node 25, port scpe unrecognized
Warning: node 25, port size unrecognized
Warning: node 29, port type unrecognized
Warning: node 48, port type unrecognized
Warning: node 49, port type unrecognized
Warning: node 34, port type unrecognized
Warning: node 34, port scpe unrecognized
Warning: node 35, port type unrecognized
11)生成的图如下

这个函数调用也是要命了。
所以得学习AST语法分析。
1638

被折叠的 条评论
为什么被折叠?



