29.01.2014 Views

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 13. WHOLE-PROGRAM ANALYSIS 111<br />

module D = Graph.Graphviz.Dot(struct<br />

(∗ ... ∗)<br />

end)<br />

The graph <strong>of</strong> callgraph functions converts a <strong>CIL</strong> call-graph into an Ocamlgraph graph that we<br />

can use to generate a .dot le.<br />

let graph <strong>of</strong> callgraph (cg : CG.callgraph) : SG.t =<br />

let g = SG.create () in<br />

H.iter (fun s n → SG.add vertex g n) cg;<br />

H.iter (fun s n →<br />

Inthash.iter (fun i n' →<br />

SG.add edge g n n'<br />

) n.CG.cnCallees<br />

) cg;<br />

g<br />

The function tut13 is the entry point for this module. It computes the call-graph, converts it to a<br />

graph for the Ocamlgraph graph library, and passses it to the graph library function that produces<br />

the .dot le.<br />

let tut13 (f : file) : unit =<br />

let o = open out !Ciltutoptions.tut13out in<br />

f | > CG.computeGraph | > graph <strong>of</strong> callgraph | > D.output graph o;<br />

close out o<br />

13.2 Example<br />

The dicult part <strong>of</strong> arranging for whole-program analysis is the more complicated compilation<br />

process. Here are two source les that we'll use to generate one call-graph:<br />

In the rst le, we'll declare an extern function bar and dene a function foo that calls it.<br />

../test/tut13a.c<br />

extern int bar(int x);<br />

int foo(int x)<br />

{<br />

return bar(x);<br />

}<br />

In the second le, we'll make an extern declaration for the function foo, and dene the function<br />

bar that in turn calls foo. The main function simply calls bar. (Obviously, this program is a<br />

nonsense example.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!