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 4. INSTRUMENTATION 40 4.2 tut4.c In this C le, we dene the two functions added by src/tut4.ml. tut begin loop currently does nothing, but a more sophisticated analysis may wish to do some bookkeeping before each loop, so we include it as a placeholder. tut end loop simply outputs the number of iterations that a loop completed. We use c-1 because the counter is incremented at the top of the loop before the exit test. ../ciltut-lib/src/tut4.c # include void tut_begin_loop(const char *f, int l) {} void tut_end_loop(const char *f, int l, int c) { printf("loop: %s:%d - %d times\n", f, l, c - 1); fflush(stdout); return; } 4.3 test/tut4.c We can test the instrumentation on the small example below. At the end of each loop the instrumented code will print a message stating how many iterations there were. Thus, we should get 10 messages for the inner loop stating that there were 5 iterations, and one message for the outer loop stating that there were 10 iterations. # include int main() { int i, j; int c = 1; for (i = 0; i < 10; i++) { for (j = 0; j < 5; j++) { c *= i; } } ../test/tut4.c } return 0; Now, we can build the test program by doing the following:
CHAPTER 4. INSTRUMENTATION 41 $ ciltutcc --enable-tut4 -o tut4 test/tut4.c Then, when we run tut4, we get the following output: $ ./tut4 loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:18 - 5 times loop: test/tut4.c:17 - 10 times which is what we expected. 4.4 Exercises 1. Count the number of times: a variable is read/written, a particular instruction or statement is executed, how many times a branch is taken one way or the other, etc.
- 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 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: CHAPTER 4. INSTRUMENTATION 39 metho
- 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
- Page 83 and 84: Chapter 10 Adding a New Kind of Sta
- Page 85 and 86: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 87 and 88: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 89 and 90: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 91 and 92: CHAPTER 10. ADDING A NEW KIND OF ST
CHAPTER 4. INSTRUMENTATION 40<br />
4.2 tut4.c<br />
In this C le, we dene the two functions added by src/tut4.ml. tut begin loop currently does<br />
nothing, but a more sophisticated analysis may wish to do some bookkeeping before each loop, so<br />
we include it as a placeholder. tut end loop simply outputs the number <strong>of</strong> iterations that a loop<br />
completed. We use c-1 because the counter is incremented at the top <strong>of</strong> the loop before the exit<br />
test.<br />
../ciltut-lib/src/tut4.c<br />
# include <br />
void tut_begin_loop(const char *f, int l) {}<br />
void tut_end_loop(const char *f, int l, int c)<br />
{<br />
printf("loop: %s:%d - %d times\n", f, l, c - 1);<br />
fflush(stdout);<br />
return;<br />
}<br />
4.3 test/tut4.c<br />
We can test the instrumentation on the small example below. At the end <strong>of</strong> each loop the instrumented<br />
code will print a message stating how many iterations there were. Thus, we should get 10<br />
messages for the inner loop stating that there were 5 iterations, and one message for the outer loop<br />
stating that there were 10 iterations.<br />
# include <br />
int main()<br />
{<br />
int i, j;<br />
int c = 1;<br />
for (i = 0; i < 10; i++) {<br />
for (j = 0; j < 5; j++) {<br />
c *= i;<br />
}<br />
}<br />
../test/tut4.c<br />
}<br />
return 0;<br />
Now, we can build the test program by doing the following: