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 2. VISITING THE AST 18 Coding Hint: The inx function |> has type (α → (α → β) → β). The code in tut2 is another way of writing iterGlobals f (onlyFunctions (processFunction funvar)) but without all the extra parenthesis. |> is included in the F# standard library, but since it isn't in OCaml's we dene it in Tututil. You can read it as expression on the left is passed to function on right, and we can chain them up like I have in tut2 without writing lots of extra parenthesis. let tut2 (funvar : string × string) (f : file) : unit = funvar | > processFunction | > onlyFunctions | > iterGlobals f 2.2 test/tut2.c In main.ml tut2 in tut2.ml is called with argument ("foo","bar") meaning that assignments to global variables called bar should be removed from functions called foo. Thus, when the code below is compiled with tut2 enabled, the program should print 37 and exit. # include int bar = 37; int foo() { int l; bar = 0; l = bar; return l; } int main() { int r; r = foo(); printf("r = %d\n", r); return 0; } ../test/tut2.c As with test/tut1.c, we can build this le with the following command:
CHAPTER 2. VISITING THE AST 19 $ ciltutcc --enable-tut2 -o tut2 test/tut2.c test/tut2.c:16: Deleted assignment: #line 16 "test/tut2.c" bar = 0; Again, using the E.log function above, ciltutcc informs us that it has removed an assignment to bar on line 16. Now, we can run the resulting program: $ ./tut2 r = 37 As expected, the assignment of 0 to bar has been removed from the function foo, and the program prints the original value. 2.3 Exercises 1. Take a look at the methods available for use in a visitor class. Rewrite this example without using iterGlobals. That is, use only the visitor class to do all the work.
- 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: CHAPTER 2. VISITING THE AST 17 open
- 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 and 32: CHAPTER 3. DATAFLOW ANALYSIS 29 let
- Page 33 and 34: CHAPTER 3. DATAFLOW ANALYSIS 31 DoC
- 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
CHAPTER 2. VISITING THE AST 19<br />
$ ciltutcc --enable-tut2 -o tut2 test/tut2.c<br />
test/tut2.c:16: Deleted assignment: #line 16 "test/tut2.c"<br />
bar = 0;<br />
Again, using the E.log function above, ciltutcc informs us that it has removed an assignment<br />
to bar on line 16. Now, we can run the resulting program:<br />
$ ./tut2<br />
r = 37<br />
As expected, the assignment <strong>of</strong> 0 to bar has been removed from the function foo, and the<br />
program prints the original value.<br />
2.3 Exercises<br />
1. Take a look at the methods available for use in a visitor class. Rewrite this example without<br />
using iterGlobals. That is, use only the visitor class to do all the work.