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 8. DEPENDANT TYPE QUALIFIERS 69<br />

$ ciltutcc --enable-tut8 -o tut8 test/tut8.c<br />

which completes successfully, but when we run it:<br />

$ ./tut8<br />

test/tut8.c:40 Bad color coercion: (50,50,50) > (10,10,10)<br />

a runtime type-check assertion fails at the call to bar, as expected.<br />

8.3 Exercises<br />

1. The visitor does not check casts, though this is another obvious place to check color compatibility.<br />

Add code to the visitor for checking casts, but change the static and runtime<br />

type-checks so that only a warning is emitted if colors for a cast are not compatible. (If the<br />

programmer is adding an explicit cast, maybe they know what they're doing.)<br />

2. In Chapter 7 we recursively descended into types, checking color compatibility at all levels.<br />

Extend this code to do similar checking.<br />

3. When a variable is the target <strong>of</strong> an assignment, then all types that reference it are changed.<br />

The new types must be included in the corresponding old types. In order to capture all cases<br />

in which an assignment may change a type, we must place restrictions on the expressions<br />

that may appear in types. What are these restrictions? Modify this code to enforce those<br />

restrictions, and to place appropriate runtime checks when an assignment may change a type<br />

(i.e. inclusion checks for the types pre- and post-assignment).<br />

4. Create an optimization visitor or dataow analysis that removes checks that we can be sure<br />

will always succeed.<br />

8.4 Further Reading<br />

Deputy [2] is an extension to C's type-system that adds dependent type-qualiers that track the<br />

length <strong>of</strong> memory buers. In performing type-checking, the compiler adds runtime checks to the<br />

program whenever pointer arithmetic is performed (or any variable mentioned in a type-qualier<br />

may be modied), since doing so may alter a type.<br />

Shoal [1] is also an extension to C's type-system that adds dependent type-qualiers that allow<br />

the compiler to track membership <strong>of</strong> an object in a pointer-based data-structure. In particular, the<br />

type <strong>of</strong> an external pointer into a data-structure may be qualied by a pointer to a distinguished<br />

member <strong>of</strong> the data-structure (i.e. the root <strong>of</strong> a tree). This has the result that the number <strong>of</strong><br />

external pointers into a data-structure can be tracked simply by counting the number <strong>of</strong> pointers<br />

qualied by pointers to the distinguished member. In Shoal this is used to check the safety <strong>of</strong><br />

converting pointer data-structures from thread-private to thread-shared and back again.

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

Saved successfully!

Ooh no, something went wrong!