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 4. INSTRUMENTATION 38<br />
findOrCreateFunc : file → string → typ → varinfo.<br />
If the function is found, the varinfo for it is returned, otherwise a prototype for the function is<br />
added to the le, and the varinfo for it is returned.<br />
let initTutFunctions (f : file) : unit =<br />
let focf : string → typ → varinfo = findOrCreateFunc f in<br />
let bl type = mkFunTyp voidType ["f", charConstPtrType; "l", intType] in<br />
let el type = mkFunTyp voidType ["f", charConstPtrType; "l", intType; "c", intType; ] in<br />
tutfuns.begin loop ← focf begin loop str bl type;<br />
tutfuns.end loop ← focf end loop str el type<br />
4.1.2 Loop instrumentation<br />
The function makeInstrStmts creates four statements that we'll add to loops. In the returned tuple,<br />
the rst statement calls the loop begin instrumentation function. The second statement initializes<br />
the iteration counter to zero. The third statement increments the iteration counter, and the fourth<br />
statement calls the loop end instrumentation function.<br />
In makeInstrStmts we use some shorthand. mkString turns an OCaml string into a Cil.exp<br />
constant expression. The integer function does the same for OCaml ints. The v2e function creates<br />
an exp out <strong>of</strong> a varinfo. The var function creates an lval from a varinfo, and the i2s function<br />
creates a stmt from an instr. All these can be found in Tututil or Cil.<br />
let makeInstrStmts (counter : varinfo) (loc : location)<br />
: stmt × stmt × stmt × stmt =<br />
let f, l = mkString loc.file, integer loc.line in<br />
i2s (Call(None, v2e tutfuns.begin loop, [f; l], loc)),<br />
i2s (Set(var counter, zero, loc)),<br />
i2s (Set(var counter, BinOp(PlusA, v2e counter, one, counter.vtype), loc)),<br />
i2s (Call(None, v2e tutfuns.end loop, [f; l; v2e counter], loc))<br />
The class loopInstrumenterClass is a visitor that uses makeInstrStmts to instrument loops. In<br />
vstmt we update the statement s by writing to the mutable skind eld instead <strong>of</strong> rebuilding a<br />
whole new statement so that we don't have to worry about copying over all the other elds, which<br />
we'd like to remain the same. In particular, it is very easy to forget about statement labels.<br />
Further, we use ChangeDoChildrenPost because there might be nested loops. Remember:<br />
ChangeDoChildrenPost recurses into s's children before handling s. That way, we have no innite<br />
loops due to turning s into a statement that contains the original s.<br />
class loopInstrumenterClass (fd : fundec) = object(self)<br />
inherit nopCilVisitor