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 30<br />
let instrOddEvens (il : instr list) (vml : varmap list) : varmap list list =<br />
let proc one hil i =<br />
match hil with<br />
| [ ] → (varmap list handle inst i vml) :: hil<br />
| vml' :: rst as l → (varmap list handle inst i vml') :: l<br />
in<br />
il | > L.fold left proc one [vml]<br />
|> L.tl<br />
|> L.rev<br />
Now that we can get the state on entry to statements and instructions, we can make a special visitor<br />
class such that when we inherit from it, the resulting visitor will have the current OddEven state<br />
available in every method.<br />
The vmlVisitorClass class inherits from nopCilVisitor. When visiting a statement, if the<br />
statement is a list <strong>of</strong> instructions, it uses instrOddEvens to store the entry states into a mutable<br />
instance eld state list. If the statement is not an instruction list, it uses getOddEvens to put<br />
the state on entry to the statement into another mutable instance eld current state. When<br />
an instruction is visited, it takes the head <strong>of</strong> state list, writes it to current state, and removes<br />
the rst state <strong>of</strong> state list. The visitor also adds a method get cur vml(), which returns<br />
current state.<br />
Inheriting from this visitor will cause the state found by the analysis on entry to a statement or an<br />
instruction to be available in the vstmt and vinst methods <strong>of</strong> the inheriting class. Classes inheriting<br />
from vmlVisitorClass must call super#vstmt and super#vinst if they override vstmt or vinst<br />
respectively. To get the current state, inheriting classes can call self#get cur vml. Additionally,<br />
when passing an inheritor <strong>of</strong> vmlVisitorClass to a function requiring a nopCilVisitor, we must<br />
do an up-cast because <strong>of</strong> the additional method get cur vml(). See the function evenOddAnalysis<br />
below.<br />
class vmlVisitorClass = object(self)<br />
inherit nopCilVisitor<br />
val mutable sid = − 1<br />
val mutable state list = [ ]<br />
val mutable current state = None<br />
method vstmt stm =<br />
sid ← stm.sid;<br />
begin match getOddEvens sid with<br />
| None → current state ← None<br />
| Some vml → begin<br />
match stm.skind with<br />
| Instr il →<br />
current state ← None;<br />
state list ← instrOddEvens il vml<br />
| → current state ← None<br />
end end;