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

29.01.2014 Views

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.

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:

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

Saved successfully!

Ooh no, something went wrong!