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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 9. TYPE QUALIFIER INFERENCE 75<br />

class graphBuilder (g : graph) (fd : fundec) = object(self)<br />

inherit nopCilVisitor<br />

method vinst (i : instr) =<br />

match i with<br />

| Set(lv, e, loc) →<br />

typesAddEdge g (typeOf e) (typeOfLval lv);<br />

DoChildren<br />

(∗ ... ∗)<br />

end<br />

The function functionBuildGraph invokes the visitor graphBuilder on function fd, building the<br />

graph in the mutable array g.<br />

let functionBuildGraph (g : graph) (fd : fundec) (loc : location) : unit =<br />

let vis = new graphBuilder g fd in<br />

ignore(visitCilFunction vis fd)<br />

The function fileBuildGraph simply calls the functions we dened above, rst for marking the<br />

AST with "Node" type attributes, and second building the graph. The function returns the resulting<br />

graph.<br />

let fileBuildGraph (f : file) : graph =<br />

let g = f | > addNodeMarks | > graphCreate in<br />

functionBuildGraph g<br />

|> onlyFunctions<br />

|> iterGlobals f;<br />

g<br />

9.1.3 Inferene Algorithm<br />

The inference algorithm must be seeded somehow. In this example, the nodeColorFinder visitor<br />

takes any type qualiers that the programmer added explicitly to the program, and puts them into<br />

our graph. However, in a dierent application, depending on the meanings <strong>of</strong> the type qualiers, it<br />

may be possible to deduce some <strong>of</strong> the missing type qualiers before the inference stage begins.<br />

class nodeColorFinder (g : graph) = object(self)<br />

inherit nopCilVisitor<br />

method vtype (t : typ) =<br />

let id = node <strong>of</strong> type t in<br />

let c = T.colors <strong>of</strong> type t in<br />

g.(id).ncolors ← c;<br />

DoChildren<br />

end

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

Saved successfully!

Ooh no, something went wrong!