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
CHAPTER 3. DATAFLOW ANALYSIS 30 let instrOddEvens (il : instr list) (vml : varmap list) : varmap list list = let proc one hil i = match hil with | [ ] → (varmap list handle inst i vml) :: hil | vml' :: rst as l → (varmap list handle inst i vml') :: l in il | > L.fold left proc one [vml] |> L.tl |> L.rev Now that we can get the state on entry to statements and instructions, we can make a special visitor class such that when we inherit from it, the resulting visitor will have the current OddEven state available in every method. The vmlVisitorClass class inherits from nopCilVisitor. When visiting a statement, if the statement is a list of instructions, it uses instrOddEvens to store the entry states into a mutable instance eld state list. If the statement is not an instruction list, it uses getOddEvens to put the state on entry to the statement into another mutable instance eld current state. When an instruction is visited, it takes the head of state list, writes it to current state, and removes the rst state of state list. The visitor also adds a method get cur vml(), which returns current state. Inheriting from this visitor will cause the state found by the analysis on entry to a statement or an instruction to be available in the vstmt and vinst methods of the inheriting class. Classes inheriting from vmlVisitorClass must call super#vstmt and super#vinst if they override vstmt or vinst respectively. To get the current state, inheriting classes can call self#get cur vml. Additionally, when passing an inheritor of vmlVisitorClass to a function requiring a nopCilVisitor, we must do an up-cast because of the additional method get cur vml(). See the function evenOddAnalysis below. class vmlVisitorClass = object(self) inherit nopCilVisitor val mutable sid = − 1 val mutable state list = [ ] val mutable current state = None method vstmt stm = sid ← stm.sid; begin match getOddEvens sid with | None → current state ← None | Some vml → begin match stm.skind with | Instr il → current state ← None; state list ← instrOddEvens il vml | → current state ← None end end;
CHAPTER 3. DATAFLOW ANALYSIS 31 DoChildren method vinst i = try let data = L.hd state list in current state ← Some(data); state list ← L.tl state list; DoChildren with Failure "hd" → DoChildren method get cur vml () = match current state with | None → getOddEvens sid | Some vml → Some vml end The class varUseReporterClass inherits from vmlVisitorClass. Whenever it visits a variable use, it emits the oekind it nds for it in the current state as given by super#get cur vml(). class varUseReporterClass = object(self) inherit vmlVisitorClass as super method vvrbl (vi : varinfo) = match self#get cur vml () with | None → SkipChildren | Some vml → begin if L.mem assoc vi.vid vml then begin let vm = (vi.vid, L.assoc vi.vid vml) in E.log "%a: %a\n" d loc (!currentLoc) varmap list pretty [vm] end; SkipChildren end end The function evenOddAnalysis computes the OddEven analysis. It then invokes the varUseReporter visitor. Note that we have to coerce our visitor to a nopCilVisitor because we added the get cur vml() method. let evenOddAnalysis (fd : fundec) (loc : location) : unit = computeOddEven fd; let vis = ((new varUseReporterClass) :> nopCilVisitor) in ignore(visitCilFunction vis fd) The tut3 function is the entry point to this module. It applies evenOddAnalysis to all functions. let tut3 (f : file) : unit = iterGlobals f (onlyFunctions evenOddAnalysis)
- Page 1 and 2: A CIL Tutorial Using CIL for langua
- Page 3 and 4: Contents Preface 4 Introduction 5 0
- Page 5 and 6: CONTENTS 3 13 Whole-program Analysi
- Page 7 and 8: Introduction The C Intermediate Lan
- Page 9 and 10: References [1] clang: a C language
- Page 11 and 12: CHAPTER 0. OVERVIEW AND ORGANIZATIO
- Page 13 and 14: Chapter 1 The AST The Concrete Synt
- Page 15 and 16: CHAPTER 1. THE AST 13 1.2 Printing
- Page 17 and 18: References [1] Andrew W. Appel. Mod
- Page 19 and 20: CHAPTER 2. VISITING THE AST 17 open
- Page 21 and 22: CHAPTER 2. VISITING THE AST 19 $ ci
- Page 23 and 24: Chapter 3 Dataow Analysis Dataow An
- Page 25 and 26: CHAPTER 3. DATAFLOW ANALYSIS 23 Cod
- Page 27 and 28: CHAPTER 3. DATAFLOW ANALYSIS 25 let
- Page 29 and 30: CHAPTER 3. DATAFLOW ANALYSIS 27 and
- Page 31: CHAPTER 3. DATAFLOW ANALYSIS 29 let
- Page 35 and 36: CHAPTER 3. DATAFLOW ANALYSIS 33 tes
- Page 37 and 38: References [1] Aws Albarghouthi, Ra
- Page 39 and 40: CHAPTER 4. INSTRUMENTATION 37 type
- Page 41 and 42: CHAPTER 4. INSTRUMENTATION 39 metho
- Page 43 and 44: CHAPTER 4. INSTRUMENTATION 41 $ cil
- Page 45 and 46: CHAPTER 5. INTERPRETED CONSTRUCTORS
- Page 47 and 48: CHAPTER 5. INTERPRETED CONSTRUCTORS
- Page 49 and 50: Chapter 6 Overriding Functions When
- Page 51 and 52: CHAPTER 6. OVERRIDING FUNCTIONS 49
- Page 53 and 54: References [1] Kumar Avijit, Pratee
- Page 55 and 56: CHAPTER 7. TYPE QUALIFIERS 53 let c
- Page 57 and 58: CHAPTER 7. TYPE QUALIFIERS 55 let w
- Page 59 and 60: CHAPTER 7. TYPE QUALIFIERS 57 $ cil
- Page 61 and 62: Chapter 8 Dependant Type Qualiers O
- Page 63 and 64: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 65 and 66: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 67 and 68: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 69 and 70: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 71 and 72: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 73 and 74: Chapter 9 Type Qualier Inference In
- Page 75 and 76: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 77 and 78: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 79 and 80: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 81 and 82: CHAPTER 9. TYPE QUALIFIER INFERENCE
CHAPTER 3. DATAFLOW ANALYSIS 31<br />
DoChildren<br />
method vinst i =<br />
try let data = L.hd state list in<br />
current state ← Some(data);<br />
state list ← L.tl state list;<br />
DoChildren<br />
with Failure "hd" → DoChildren<br />
method get cur vml () =<br />
match current state with<br />
| None → getOddEvens sid<br />
| Some vml → Some vml<br />
end<br />
The class varUseReporterClass inherits from vmlVisitorClass. Whenever it visits a variable use,<br />
it emits the oekind it nds for it in the current state as given by super#get cur vml().<br />
class varUseReporterClass = object(self)<br />
inherit vmlVisitorClass as super<br />
method vvrbl (vi : varinfo) =<br />
match self#get cur vml () with<br />
| None → SkipChildren<br />
| Some vml → begin<br />
if L.mem assoc vi.vid vml then begin<br />
let vm = (vi.vid, L.assoc vi.vid vml) in<br />
E.log "%a: %a\n" d loc (!currentLoc) varmap list pretty [vm]<br />
end;<br />
SkipChildren<br />
end<br />
end<br />
The function evenOddAnalysis computes the OddEven analysis. It then invokes the varUseReporter<br />
visitor. Note that we have to coerce our visitor to a nopCilVisitor because we added the<br />
get cur vml() method.<br />
let evenOddAnalysis (fd : fundec) (loc : location) : unit =<br />
computeOddEven fd;<br />
let vis = ((new varUseReporterClass) :> nopCilVisitor) in<br />
ignore(visitCilFunction vis fd)<br />
The tut3 function is the entry point to this module. It applies evenOddAnalysis to all functions.<br />
let tut3 (f : file) : unit =<br />
iterGlobals f (onlyFunctions evenOddAnalysis)