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

Dependant Type Qualiers<br />

Of course, there are more colors than just red, green, and blue. Furthermore, the color <strong>of</strong> a type<br />

might depend on some other data in the program. In this example we introduce type qualiers<br />

parameterized by arbitrary C expressions. Since types may depend on runtime data, type-checking<br />

must happen partially at runtime, which we will accomplish by instrumenting the code with the<br />

appropriate checks. Furthermore, the information we know about the color <strong>of</strong> a type may be<br />

imprecise, or rather, for example, a function may accept a range <strong>of</strong> colors rather than only one<br />

exact color. We introduce type qualiers for expressing this.<br />

First, we'll dene OCaml types for representing the type qualiers. Then, we'll dene functions<br />

for extracting these qualiers from C type attributes. This will involve keeping a typing context<br />

that maps strings to expressions so that the identiers in type attributes may be translated into<br />

<strong>CIL</strong> variables. With the ability to extract colors from types, we will then be able to perform type<br />

checking as in the previous example. However, instead <strong>of</strong> generating a compile-time answer about<br />

correct typing, we must generate a list <strong>of</strong> instructions that test type compatibility at runtime.<br />

It will be possible to determine at compile time that some <strong>of</strong> these checks will always succeed.<br />

Writing an optimization pass to remove these checks is left as an exercise.<br />

OCaml Style Note: It would be better style to put each <strong>of</strong> these parts in<br />

separate OCaml modules: one for dening OCaml types; one for compiling the<br />

color qualiers in type attributes into <strong>CIL</strong> expressions; one for setting up the<br />

runtime checks; and one containing the visitors used for type checking.<br />

59

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

Saved successfully!

Ooh no, something went wrong!