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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9. TYPE QUALIFIER INFERENCE 74<br />

OCaml Pitfall: In graphCreate you might be tempted to say:<br />

A.make n {ncolors = [ ]; ...}. However this would be problematic. The array<br />

would be initialized, not with copies <strong>of</strong> the given node record, but rather<br />

with references to a single node because the record literal would be evaluated<br />

before being passed to A.make. On the other hand, the newNode function is<br />

called for each array element, allocating a fresh node for each one.<br />

The function typesAddEdge places an edge in the graph from the node for type from type to the<br />

node for type to type.<br />

let rec typesAddEdge (g : graph) (from type : typ) (to type : typ) : unit =<br />

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

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

graphAddEdge g from id to id<br />

Before dening the visitor that will build the graph, we dene a couple <strong>of</strong> functions that will make<br />

handling function calls a bit easier. The function addEdgesForCallArgs adds edges arising from<br />

the assignment <strong>of</strong> actuals to formals at function call sites.<br />

let addEdgesForCallArgs (g : graph) (fe : exp) (aes : exp list) : unit =<br />

let fts = fe | > function elements | > snd | > L.map snd3 in<br />

let ats = aes | > list take (L.length fts) | > L.map typeOf in<br />

L.iter2 (typesAddEdge g) ats fts<br />

The function addEdgesForCallRet adds edges for the assignment <strong>of</strong> return values at call sites.<br />

let addEdgesForCallRet (g : graph) (fe : exp) (rlvo : lval option) : unit =<br />

match rlvo with<br />

| None → ()<br />

| Some rlv →<br />

let rt, = function elements fe in<br />

typesAddEdge g rt (typeOfLval rlv)<br />

The visitor graphBuilder adds edges to the graph. Edges encode constraints on type qualiers.<br />

We place an edge from node A to node B when the type <strong>of</strong> A must be included in the type <strong>of</strong><br />

B. Thus, if we have an assignment lv = e the type <strong>of</strong> lv has to be big enough to handle the<br />

type <strong>of</strong> e. That is, the type <strong>of</strong> e must be included in the type <strong>of</strong> lv. Then, we can just think <strong>of</strong><br />

casts, parameter passing, and the return statements as dierent ways <strong>of</strong> doing assignments, and<br />

add edges for them the same way.

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

Saved successfully!

Ooh no, something went wrong!