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

Adding a New Kind <strong>of</strong> Statement<br />

In this section, we will take a look at two dierent ways <strong>of</strong> adding a new statement to C without any<br />

changes to the lexer or parser. In particular, we will add a statement to C that proles the cache<br />

miss rate and used memory bandwidth in a block <strong>of</strong> code. Adding the new syntax and replacing<br />

the new statement with calls into our runtime library will be the easy part. The more dicult task<br />

will be the implementation <strong>of</strong> the runtime library. In particular, we must arrange for each thread<br />

to maintain sucient state to handle arbitrary nesting <strong>of</strong> the new statement.<br />

10.1 tut10.ml<br />

The two dierent ways <strong>of</strong> adding syntax involve further use <strong>of</strong> the same attribute syntax that<br />

we used for type qualiers. In the rst method, we transform a specially constructed if statement.<br />

In the second method we transform Block statements that have been annotated with the<br />

blockattribute ((...)) syntax supported by <strong>CIL</strong>. When using the transformed if statement,<br />

we'll be able to use a C preprocessor macro to make it look more like a standard C statement.<br />

First, we dene a string for the name <strong>of</strong> the new statement. Then, in hasCacheReportAttrs and<br />

isCacheReportType, we dene functions for determining whether a type or set <strong>of</strong> attributes contains<br />

the the name <strong>of</strong> the new statement.<br />

let cacheReportStr = "cache report"<br />

let hasCacheReportAttrs : attributes → bool = hasAttribute cacheReportStr<br />

let isCacheReportType (t : typ) : bool = t | > typeAttrs | > hasCacheReportAttrs<br />

The function isCacheReportState looks for two kinds <strong>of</strong> statements: either an if statement testing<br />

the constant 0 cast to a type with the cache report attribute, or a Block with that attribute.<br />

81

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

Saved successfully!

Ooh no, something went wrong!