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

let colors <strong>of</strong> exp (c : ctxt) (loc : location) (e : exp) : colors =<br />

match e with<br />

| Lval lv → colors <strong>of</strong> lval c loc lv<br />

| → colors <strong>of</strong> type c loc (typeOf e)<br />

colorCheckVisitor visits a function with a context c that contains mappings for the local variables<br />

and formal parameters. At assignments and function calls, it inserts function calls that the type<br />

<strong>of</strong> the destination lvalue or formal parameter includes the type <strong>of</strong> the right-hand-side or actual<br />

parameter.<br />

These instructions are inserted using self#queueInstr. This is yet another mechanism with<br />

which one can modify the AST. self#queueInstr can be called from any visitor method. Instructions<br />

queued up with self#queueInstr are dequeued (i.e. inserted) at the latest possible point<br />

before the AST node currently being visited. So, if we queue up an instruction while visiting an<br />

instruction, the new instruction will be inserted immediately before the one currently being visited.<br />

The case for checking function calls is ommitted because <strong>of</strong> its length, but it is a very useful<br />

pattern, and might help with one <strong>of</strong> the exercises from a previous chapter.<br />

class colorCheckVisitor (c : ctxt) = object(self)<br />

inherit nopCilVisitor<br />

method vinst (i : instr) =<br />

match i with<br />

| Set(lv, e, loc) →<br />

let lvc = colors <strong>of</strong> lval c loc lv in<br />

let ec = colors <strong>of</strong> exp c loc e in<br />

self#queueInstr (colors includes loc ec lvc);<br />

DoChildren<br />

| → DoChildren<br />

end<br />

The function checkColorTypes invokes the visitor colorCHeckVisitor on function fd using context<br />

c.<br />

let checkColorTypes (c : ctxt) (fd : fundec) (loc : location) : unit =<br />

let c = context for locals c fd in<br />

let vis = new colorCheckVisitor c in<br />

ignore(visitCilFunction vis fd)<br />

The function tut8 is the entry point to the code in this tutorial. It rst initializes our dynamic<br />

type-checking functions (tut8 init), then builds a global context for compiling type attributes<br />

(context for globals), performs the type-check, and nally erases the color attributes.

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

Saved successfully!

Ooh no, something went wrong!