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 3. DATAFLOW ANALYSIS 26<br />

let varmap list combine (vml1 : varmap list) (vml2 : varmap list) : varmap list =<br />

L.fold left varmap list combine one vml1 vml2<br />

The function varmap list replace replaces the entry for a variable in the state, e.g.<br />

assignment.<br />

for an<br />

let varmap list replace (vml : varmap list) (vm : varmap) : varmap list =<br />

vm :: (L.remove assoc (id <strong>of</strong> vm vm) vml)<br />

3.1.4 The oekind <strong>of</strong> an expression<br />

These functions determine the oekind <strong>of</strong> <strong>CIL</strong> expressions by recursing over their sub-expressions.<br />

We also use <strong>CIL</strong>'s built-in constant folding to obtain constants for some expressions, e.g. SizeOf.<br />

The function kind <strong>of</strong> int64 determine whether concrete integers are Odd or Even<br />

let kind <strong>of</strong> int64 (i : Int64.t) : oekind =<br />

let firstbit = Int64.logand i Int64.one in<br />

if firstbit = Int64.one then Odd else Even<br />

Depending on the abstract state, determine whether an expression e is for an Odd or Even integer.<br />

The function oekind <strong>of</strong> exp uses <strong>CIL</strong>'s constant folding function to deal with things like<br />

size<strong>of</strong>(type). Mutually recursive functions descend into unary and binary operations. Non-integer<br />

constants, addresses, and complex lvalues are translated as Top.<br />

let rec oekind <strong>of</strong> exp (vml : varmap list) (e : exp) : oekind =<br />

match e with<br />

| Const(CInt64(i, , )) → kind <strong>of</strong> int64 i<br />

| Lval(Var vi, NoOffset) → vml | > L.assoc vi.vid | > snd<br />

| SizeOf | SizeOfE | SizeOfStr | AlignOf | AlignOfE →<br />

e | > constFold true |> oekind <strong>of</strong> exp vml<br />

| UnOp(uo, e, t) → oekind <strong>of</strong> unop vml uo e<br />

| BinOp(bo, e1, e2, t) → oekind <strong>of</strong> binop vml bo e1 e2<br />

| CastE(t, e) → oekind <strong>of</strong> exp vml e<br />

| → Top<br />

The function oekind <strong>of</strong> unop determines whether the result <strong>of</strong> unary operation is Odd or Even. If<br />

the analysis were more clever, we could have said something about the LNot case, but for now we'll<br />

just say Top.

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

Saved successfully!

Ooh no, something went wrong!