1 /* Copyright © International Business Machines Corp., 2006
4 * Author: Josh Triplett <josh@freedesktop.org>
5 * Dan Sheridan <djs@adelard.com>
7 * Licensed under the Open Software License version 1.1
22 #include "expression.h"
23 #include "linearize.h"
26 /* Draw the subgraph for a given entrypoint. Includes details of loads
27 * and stores for globals, and marks return bbs */
28 static void graph_ep(struct entrypoint
*ep
)
30 struct basic_block
*bb
;
31 struct instruction
*insn
;
33 const char *fname
, *sname
;
35 fname
= show_ident(ep
->name
->ident
);
36 sname
= stream_name(ep
->entry
->bb
->pos
.stream
);
38 printf("subgraph cluster%p {\n"
40 " label=<<TABLE BORDER=\"0\" CELLBORDER=\"0\">\n"
41 " <TR><TD>%s</TD></TR>\n"
42 " <TR><TD><FONT POINT-SIZE=\"21\">%s()</FONT></TD></TR>\n"
47 ep
, sname
, fname
, sname
, fname
, ep
->entry
->bb
);
49 FOR_EACH_PTR(ep
->bbs
, bb
) {
50 struct basic_block
*child
;
52 const char * s
= ", ls=\"[";
55 printf(" bb%p [shape=ellipse,label=%d,line=%d,col=%d",
56 bb
, bb
->pos
.line
, bb
->pos
.line
, bb
->pos
.pos
);
59 /* List loads and stores */
60 FOR_EACH_PTR(bb
->insns
, insn
) {
61 switch(insn
->opcode
) {
63 if (insn
->symbol
->type
== PSEUDO_SYM
) {
64 printf("%s store(%s)", s
, show_ident(insn
->symbol
->sym
->ident
));
70 if (insn
->symbol
->type
== PSEUDO_SYM
) {
71 printf("%s load(%s)", s
, show_ident(insn
->symbol
->sym
->ident
));
81 } END_FOR_EACH_PTR(insn
);
88 /* Edges between bbs; lower weight for upward edges */
89 FOR_EACH_PTR(bb
->children
, child
) {
90 printf(" bb%p -> bb%p [op=br, %s];\n", bb
, child
,
91 (bb
->pos
.line
> child
->pos
.line
) ? "weight=5" : "weight=10");
92 } END_FOR_EACH_PTR(child
);
93 } END_FOR_EACH_PTR(bb
);
99 /* Insert edges for intra- or inter-file calls, depending on the value
100 * of internal. Bold edges are used for calls with destinations;
101 * dashed for calls to external functions */
102 static void graph_calls(struct entrypoint
*ep
, int internal
)
104 struct basic_block
*bb
;
105 struct instruction
*insn
;
107 show_ident(ep
->name
->ident
);
108 stream_name(ep
->entry
->bb
->pos
.stream
);
110 FOR_EACH_PTR(ep
->bbs
, bb
) {
113 if (!bb
->parents
&& !bb
->children
&& !bb
->insns
&& verbose
< 2)
116 FOR_EACH_PTR(bb
->insns
, insn
) {
117 if (insn
->opcode
== OP_CALL
&&
118 internal
== !(insn
->func
->sym
->ctype
.modifiers
& MOD_EXTERN
)) {
120 /* Find the symbol for the callee's definition */
122 if (insn
->func
->type
== PSEUDO_SYM
) {
123 for (sym
= insn
->func
->sym
->ident
->symbols
;
124 sym
; sym
= sym
->next_id
) {
125 if (sym
->namespace & NS_SYMBOL
&& sym
->ep
)
130 printf("bb%p -> bb%p"
131 "[label=%d,line=%d,col=%d,op=call,style=bold,weight=30];\n",
132 bb
, sym
->ep
->entry
->bb
,
133 insn
->pos
.line
, insn
->pos
.line
, insn
->pos
.pos
);
135 printf("bb%p -> \"%s\" "
136 "[label=%d,line=%d,col=%d,op=extern,style=dashed];\n",
137 bb
, show_pseudo(insn
->func
),
138 insn
->pos
.line
, insn
->pos
.line
, insn
->pos
.pos
);
141 } END_FOR_EACH_PTR(insn
);
142 } END_FOR_EACH_PTR(bb
);
145 int main(int argc
, char **argv
)
147 struct string_list
*filelist
= NULL
;
151 struct symbol_list
*fsyms
, *all_syms
=NULL
;
153 printf("digraph call_graph {\n");
154 fsyms
= sparse_initialize(argc
, argv
, &filelist
);
155 concat_symbol_list(fsyms
, &all_syms
);
157 /* Linearize all symbols, graph internal basic block
158 * structures and intra-file calls */
159 FOR_EACH_PTR_NOTAG(filelist
, file
) {
161 fsyms
= sparse(file
);
162 concat_symbol_list(fsyms
, &all_syms
);
164 FOR_EACH_PTR(fsyms
, sym
) {
166 linearize_symbol(sym
);
167 } END_FOR_EACH_PTR(sym
);
169 FOR_EACH_PTR(fsyms
, sym
) {
172 graph_calls(sym
->ep
, 1);
174 } END_FOR_EACH_PTR_NOTAG(sym
);
176 } END_FOR_EACH_PTR_NOTAG(file
);
178 /* Graph inter-file calls */
179 FOR_EACH_PTR(all_syms
, sym
) {
181 graph_calls(sym
->ep
, 0);
182 } END_FOR_EACH_PTR_NOTAG(sym
);