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
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
CHAPTER 3. DATAFLOW ANALYSIS 22<br />
static analysis. Now is a good time to take a look in dataflow.mli at the signature <strong>of</strong> the module<br />
that we'll be implementing, and what the functions <strong>of</strong> the module mean. Additionally, the Dataflow<br />
module is well documented in the main <strong>CIL</strong> documentation.<br />
3.1 tut3.ml<br />
The dataow analysis here is a common textbook example for abstract interpretation, an even/odd<br />
analysis. First, we'll dene types and operations over the abstract state <strong>of</strong> the program. Then, we'll<br />
apply the functor. Following this, we'll write some boilerplate code for accessing the results <strong>of</strong> the<br />
analysis in an AST visitor. It should be straightforward to repurpose the code in this tutorial for<br />
many other kinds <strong>of</strong> dataow analysis, so feel free to use it as a starting point. For dataow analysis<br />
in the backwards direction, there is also a BackwardsDataFlow functor in the Dataflow module.<br />
module IH = Inthash (∗ An int → α hashtable library ∗)<br />
module DF = Dataflow (∗ <strong>CIL</strong>'s dataow analysis library ∗)<br />
When debug is true, the dataow library emints out lots <strong>of</strong> debugging information.<br />
let debug = ref false<br />
3.1.1 Type Denitions<br />
The abstract state for the analysis is a mapping from local variables <strong>of</strong> integral type to one <strong>of</strong> the<br />
oekind constructors. When a variable is mapped to one <strong>of</strong> these kinds, it has the following meaning:<br />
• Top The variable could be either odd or even.<br />
• Even The variable is an even integer.<br />
• Odd The variable is an odd integer.<br />
• Bottom The variable is uninitialized.<br />
type oekind = Top | Odd | Even | Bottom<br />
We'll use association lists to represent the mapping. An element <strong>of</strong> the mapping for a variable<br />
vi : varinfo is like: (vi.vid, (vi, kind)). We'll also need some utility functions for examining<br />
and manipulating the mappings.