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

8.1.2 Context for compiling type qualiers<br />

We would like to allow our color type qualiers to refer to C expressions. Therefore, in order<br />

to interpret them, and to compile them into our OCaml type (color), we require a context to<br />

translate names into <strong>CIL</strong> expressions. Instances <strong>of</strong> the ctxt type simply map strings to <strong>CIL</strong><br />

expressions. Here, we'll also write some functions for looking up strings in the context, adding<br />

variables, expressions, and elds to a context, and extending a context so that type qualiers may<br />

refer to global varialbes, local variables, and structure elds. We also include a function so that the<br />

types <strong>of</strong> formal parameters may refer to other formal parameters.<br />

type ctxt = exp SM.t<br />

let exp <strong>of</strong> string (c : ctxt) (s : string) : exp = SM.find s c<br />

let ctxt add var (c : ctxt) (vi : varinfo) : ctxt = SM.add vi.vname (v2e vi) c<br />

let ctxt add exp (c : ctxt) (s : string) (e : exp) : ctxt = SM.add s e c<br />

let ctxt add field (blv : lval) (c : ctxt) (fi : fieldinfo) : ctxt =<br />

SM.add fi.fname (Lval(addOffsetLval (Field(fi, NoOffset)) blv)) c<br />

The function context for globals generates a fresh context containing mappings for the global<br />

variables. It does this by folding over the list <strong>of</strong> globals in f.globals and using ctxt add var when<br />

encountering a global variable declaration or denition.<br />

let context for globals (f : file) : ctxt =<br />

L.fold left (fun c g →<br />

match g with<br />

| GVarDecl(vi, ) → ctxt add var c vi<br />

| GVar(vi, , ) → ctxt add var c vi<br />

| → c)<br />

SM.empty f.globals<br />

The function context for locals extends a context with mappings for local variables and function<br />

parameters.<br />

let context for locals (c : ctxt) (fd : fundec) : ctxt =<br />

L.fold left ctxt add var c (fd.slocals @ fd.sformals)<br />

Used at a call site, the function context for call extends a context with mappings from formal<br />

parameters to actual parameters. Since there may be more actuals than formals, we use<br />

list take dened in Tututil to limit the mapped actuals to the number <strong>of</strong> formals. Then, we<br />

map ctxt add exp over the lists. It is used for resolving function argument types at a call site,<br />

which may refer to the other function arguments.

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

Saved successfully!

Ooh no, something went wrong!