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
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 27<br />
and oekind <strong>of</strong> unop (vml : varmap list) (u : unop) (e : exp) : oekind =<br />
match u with<br />
| Neg → oekind <strong>of</strong> exp vml e<br />
| BNot → e | > oekind <strong>of</strong> exp vml | > oekind neg<br />
| LNot → Top<br />
The function oekind <strong>of</strong> binop determines whether the result <strong>of</strong> a binary operation is Odd or Even.<br />
In this example we are handling only the cases for addition, subtraction, and multiplication, but it<br />
is also possible to handle other cases.<br />
and oekind <strong>of</strong> binop (vml : varmap list) (b : binop) (e1 : exp) (e2 : exp) : oekind =<br />
let k1, k2 = oekind <strong>of</strong> exp vml e1, oekind <strong>of</strong> exp vml e2 in<br />
match b with<br />
| PlusA → begin<br />
match k1, k2 with<br />
| Even, Even → Even<br />
| Odd, Odd → Even<br />
| Even, Odd → Odd<br />
| Odd, Even → Odd<br />
| , → Top<br />
end<br />
(∗ ... ∗)<br />
| → Top<br />
If there is a write to memory or a function call, we must destroy anything we knew about a local<br />
variable whose address has been taken. In the case <strong>of</strong> a function call, if the analysis were to also<br />
handle global variables, it would also have to clear their state even if their addresses haven't been<br />
taken (but a global whose address hasn't been taken would still be safe from a memory write.) The<br />
function varmap list kill moves the state <strong>of</strong> variables whose address has been taken to Top in<br />
the resulting varmap.<br />
let varmap list kill (vml : varmap list) : varmap list =<br />
L.map (fun (vid, (vi, k)) →<br />
if vi.vaddr<strong>of</strong> then (vid, (vi, Top)) else (vid, (vi, k)))<br />
vml<br />
The function varmap list handle inst implements the transfer function for our dataow analysis.<br />
That is it looks at an instruction and a state, and calculates what the state should be after the<br />
instruction is run. On a simple assignment to a local variable, we replace its mapping in the input<br />
state with the oekind <strong>of</strong> the right-hand-side <strong>of</strong> the assignment. On memory writes, and function<br />
calls, we use varmap list kill to move the aected variables to Top.