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<br />

Type Qualier Inference<br />

In the previous two sections, we saw how to add type-qualiers to the language, and how to do<br />

any extra type-checking that they might require. In this section, we'll see how one can begin to do<br />

type-qualier inference. This might be useful, for example, if we wish to make our changes to C<br />

more unobtrusive. That is, we would like to add expressiveness to the language and type-system<br />

without thrusting a large annotation burden onto the programmer. One way to accomplish this is<br />

by inferring as many type qualiers as possible. Additionally, analyses like this one, which push type<br />

qualiers around, can be used for purposes aside from type-inference. For example, type qualier<br />

inference can be used to perform an imprecise (though still very useful) thread-escape analysis.<br />

9.1 tut9.ml<br />

We'll use the following method to infer type qualiers. First, we'll place a numeric IDs on each<br />

type node in the AST. Then we'll build a graph with these numeric IDs on each node and directed<br />

edges given by the relationships between types that we nd in the program. Next, we iterate over<br />

the nodes until we reach a x-point that satises the relationships encoded by the graph edges.<br />

Following that, we add the inferred type qualiers to the program. If an inferred type qualier<br />

doesn't match the one that the programmer has given explicitly, we emit a compile-time warning.<br />

This inference pass is intended to be followed by a type-checking phase as in one <strong>of</strong> the previous<br />

sections. Therefore, it is important that we don't infer anything that won't type-check!<br />

We'll need the standard library Queue module, and a few things from Chapter 7, in particular the<br />

denition <strong>of</strong> the color type.<br />

module Q = Queue<br />

module T = Tut7<br />

type colors = T.color list<br />

Even though we are borrowing some OCaml types and functions from Tut7, the C type system for<br />

this analysis is slightly dierent. In Tut7 for two types to be compatible, their colors had to match<br />

exactly. In this tutorial, we use the same colors, with a dierent restriction: type A is {included<br />

71

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

Saved successfully!

Ooh no, something went wrong!