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 3. DATAFLOW ANALYSIS 29<br />

let collectVars (fd : fundec) : varmap list =<br />

(fd.sformals @ fd.slocals)<br />

|> L.filter (fun vi → isIntegralType vi.vtype)<br />

|> L.map (fun vi → (vi.vid, (vi, Bottom)))<br />

Here, the function computeOddEven calls the compute function <strong>of</strong> the OddEven module. First though,<br />

use <strong>CIL</strong>'s Cfg module to compute the control-ow graph for the function. Then, we grab the rst<br />

statement <strong>of</strong> the function and add it to the stmtStartData hash with a state indicating that on<br />

entry to the function the state for every variable is Bottom, which we get from the collectVars<br />

function. In practice, this code should be placed in a try...with block, to handle cases where, e.g.<br />

the function is empty.<br />

let computeOddEven (fd : fundec) : unit =<br />

Cfg.clearCFGinfo fd;<br />

ignore(Cfg.cfgFun fd);<br />

let first stmt = L.hd fd.sbody.bstmts in<br />

let vml = collectVars fd in<br />

IH.clear OddEvenDF.stmtStartData;<br />

IH.add OddEvenDF.stmtStartData first stmt.sid vml;<br />

OddEven.compute [first stmt]<br />

3.1.6 Using the results<br />

The function getOddEvens returns the state on entry to a statement by looking it up in the hash<br />

table OddEvenDF.stmtStartData, which is lled in by OddEven.compute.<br />

let getOddEvens (sid : int) : varmap list option =<br />

try Some(IH.find OddEvenDF.stmtStartData sid)<br />

with Not found → None<br />

The function instrOddEvens calculates the states that exist on entry to each <strong>of</strong> a list <strong>of</strong> instructions.<br />

In addition to the list <strong>of</strong> instructions, it takes as input the state on entry to the rst instruction in<br />

the list.

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

Saved successfully!

Ooh no, something went wrong!