1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
Python library for building dietcc passes
Example of using a pass:
The examples/zero_init pass zero-initializes all local variables. It can be
run by passing a --dietc-pass flag to dietcc:
$ which dietcc
[make sure it's on your path!]
$ cd examples/zero_init
$ make
$ ./default
Foo return value: 0
Foo return value: 32765
Foo return value: 32765
$ ./instrumented
Foo return value: 0
Foo return value: 0
Foo return value: 0
The examples/dynamic_typing pass adds a "dynamic_typeof" feature to C, and
uses it to implement pretty-printing in C:
$ which dietcc
[make sure it's on your path!]
$ cd examples/dynamic_typeof
$ tail -n13 test.c
int main() {
struct {
char x;
int y;
int z[3];
} foo = {1, 2, {3, 4, 5}};
pprint(&foo);
struct {
char a;
int b;
} bar = {100, 200};
pprint(&bar);
}
$ make
$ ./test
{ .x = (char)1, .y = (int)2, .z = { (int)3, (int)4, (int)5 } }
{ .a = (char)100, .b = (int)200 }
The examples/refcounting pass adds automated reference counting to C:
$ which dietcc
[make sure it's on your path!]
$ cd examples/refcounting
$ cat test.c
...
int *foo(void) {
int *ptr = 0;
for (int i = 0; i < 5; i++)
ptr = calloc(1, sizeof(*ptr));
ptr = calloc(1, sizeof(*ptr));
ptr = calloc(1, sizeof(*ptr));
*ptr = 5;
return ptr;
}
...
int *bar(void) {
struct bar bar;
bar.x = 5;
bar.y = calloc(1, sizeof(int));
*bar.y = bar.x;
return bar.y;
}
...
int main(void) {
int *x = foo();
printf("Result of foo(): %d\n", *x);
x = bar();
printf("Result of bar(): %d\n", *x);
return 0;
}
$ make
$ ./test
Allocated: 0x55bc2ca6e2a0 ; now tracking 1 regions
Allocated: 0x55bc2ca6e700 ; now tracking 2 regions
Freeing! Left: 1
Allocated: 0x55bc2ca6e750 ; now tracking 2 regions
Freeing! Left: 1
Allocated: 0x55bc2ca6e7a0 ; now tracking 2 regions
Freeing! Left: 1
Allocated: 0x55bc2ca6e7f0 ; now tracking 2 regions
Freeing! Left: 1
Allocated: 0x55bc2ca6e840 ; now tracking 2 regions
Allocated: 0x55bc2ca6e890 ; now tracking 3 regions
Freeing! Left: 2
Freeing! Left: 1
Result of foo(): 5
Allocated: 0x55bc2ca6e8e0 ; now tracking 2 regions
Result of bar(): 5
Freeing! Left: 1
Freeing! Left: 0
|