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

let context for call (c : ctxt) (fe : exp) (args : exp list) : ctxt =<br />

match typeOf fe with<br />

| TFun( , Some stal, , ) →<br />

let formals = L.map fst3 stal in<br />

let actuals = list take (L.length stal) args in<br />

L.fold left2 ctxt add exp c formals actuals<br />

| → c<br />

When compiling a eld access, the function context for struct extends a context with mappings<br />

from eld names to expressions accessing those elds through the given lval. It is used for compiling<br />

the type <strong>of</strong> a structure eld, which may refer to other elds <strong>of</strong> the same structure.<br />

let context for struct (c : ctxt) (loc : location) (lv : lval) : ctxt =<br />

let blv, <strong>of</strong>f = removeOffsetLval lv in<br />

match <strong>of</strong>f with<br />

| NoOffset | Index → c<br />

| Field(fi, NoOffset) → L.fold left (ctxt add field blv) c fi.fcomp.cfields<br />

| → E.s(E.bug "%a: Expected field w/o <strong>of</strong>fset: %a" d loc loc d lval lv)<br />

8.1.3 Compiling type qualiers<br />

Now that we can build a context for compiling the type qualiers, we can go ahead with the actual<br />

compilation. In particular, we need to translate a <strong>CIL</strong> attribute parameter into an <strong>CIL</strong> expression.<br />

Once we have compiled the type qualiers, we'll be able to enforce rules about what sorts <strong>of</strong><br />

color combinations can be on the same type. In particular, a type can have either no color, an exact<br />

color, or at most one lower bound and one upper bound.<br />

Below, the function exp <strong>of</strong> ap translates attrparams into <strong>CIL</strong> exps in a given context. If the<br />

attrparam uses a name not mapped in the context, we print an error and stop compilation.<br />

let rec exp <strong>of</strong> ap (c : ctxt) (loc : location) (ap : attrparam) : exp =<br />

let eoap = exp <strong>of</strong> ap c loc in<br />

match ap with<br />

| AInt i → integer i<br />

| AStr s → mkString s<br />

| ACons(s, [ ]) → begin<br />

try exp <strong>of</strong> string c s<br />

with Not found →<br />

E.s (E.error "%a: %s not in context for %a"<br />

d loc loc s d attrparam ap)<br />

end<br />

| AUnOp(uop, ap) →<br />

let e = eoap ap in

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

Saved successfully!

Ooh no, something went wrong!