OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [src/] [profio.c] - Blame information for rev 130

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
/* C compiler: prof.out input
2
 
3
prof.out format:
4
#files
5
    name
6
    ... (#files-1 times)
7
#functions
8
    name file# x y count caller file x y
9
    ... (#functions-1 times)
10
#points
11
    file# x y count
12
    ... (#points-1 times)
13
*/
14
#include "c.h"
15
 
16
static char rcsid[] = "$Id: profio.c,v 1.1 2002/08/28 23:12:45 drh Exp $";
17
 
18
struct count {                  /* count data: */
19
        int x, y;                       /* source coordinate */
20
        int count;                      /* associated execution count */
21
};
22
 
23
#define MAXTOKEN 64
24
 
25
struct file {                   /* per-file prof.out data: */
26
        struct file *link;              /* link to next file */
27
        char *name;                     /* file name */
28
        int size;                       /* size of counts[] */
29
        int count;                      /* counts[0..count-1] hold valid data */
30
        struct count *counts;           /* count data */
31
        struct func {                   /* function data: */
32
                struct func *link;              /* link to next function */
33
                char *name;                     /* function name */
34
                struct count count;             /* total number of calls */
35
                struct caller {         /* caller data: */
36
                        struct caller *link;    /* link to next caller */
37
                        char *name;             /* caller's name */
38
                        char *file;             /* call site: file, x, y */
39
                        int x, y;
40
                        int count;              /* number of calls from this site */
41
                } *callers;
42
        } *funcs;                       /* list of functions */
43
} *filelist;
44
FILE *fp;
45
 
46
/* acaller - add caller and site (file,x,y) to callee's callers list */
47
static void acaller(char *caller, char *file, int x, int y, int count, struct func *callee) {
48
        struct caller *q;
49
 
50
        assert(callee);
51
        for (q = callee->callers; q && (caller != q->name
52
                || file != q->file || x != q->x || y != q->y); q = q->link)
53
                ;
54
        if (!q) {
55
                struct caller **r;
56
                NEW(q, PERM);
57
                q->name = caller;
58
                q->file = file;
59
                q->x = x;
60
                q->y = y;
61
                q->count = 0;
62
                for (r = &callee->callers; *r && (strcmp(q->name, (*r)->name) > 0
63
                        || strcmp(q->file, (*r)->file) > 0 || q->y > (*r)->y || q->y > (*r)->y); r = &(*r)->link)
64
                        ;
65
                q->link = *r;
66
                *r = q;
67
        }
68
        q->count += count;
69
}
70
 
71
/* compare - return <0, 0, >0 if a<b, a==b, a>b, resp. */
72
static int compare(const void *x, const void *y) {
73
        struct count *a = (struct count *)x, *b = (struct count *)y;
74
 
75
        if (a->y == b->y)
76
                return a->x - b->x;
77
        return a->y - b->y;
78
}
79
 
80
/* findfile - return file name's file list entry, or 0 */
81
static struct file *findfile(char *name) {
82
        struct file *p;
83
 
84
        for (p = filelist; p; p = p->link)
85
                if (p->name == name)
86
                        return p;
87
        return 0;
88
}
89
 
90
/* afunction - add function name and its data to file's function list */
91
static struct func *afunction(char *name, char *file, int x, int y, int count) {
92
        struct file *p = findfile(file);
93
        struct func *q;
94
 
95
        assert(p);
96
        for (q = p->funcs; q && name != q->name; q = q->link)
97
                ;
98
        if (!q) {
99
                struct func **r;
100
                NEW(q, PERM);
101
                q->name = name;
102
                q->count.x = x;
103
                q->count.y = y;
104
                q->count.count = 0;
105
                q->callers = 0;
106
                for (r = &p->funcs; *r && compare(&q->count, &(*r)->count) > 0; r = &(*r)->link)
107
                        ;
108
                q->link = *r;
109
                *r = q;
110
        }
111
        q->count.count += count;
112
        return q;
113
}
114
 
115
/* apoint - append execution point i to file's data */
116
static void apoint(int i, char *file, int x, int y, int count) {
117
        struct file *p = findfile(file);
118
 
119
        assert(p);
120
        if (i >= p->size) {
121
                int j;
122
                if (p->size == 0) {
123
                        p->size = i >= 200 ? 2*i : 200;
124
                        p->counts = newarray(p->size, sizeof *p->counts, PERM);
125
                } else {
126
                        struct count *new;
127
                        p->size = 2*i;
128
                        new = newarray(p->size, sizeof *new, PERM);
129
                        for (j = 0; j < p->count; j++)
130
                                new[j] = p->counts[j];
131
                        p->counts = new;
132
                }
133
                for (j = p->count; j < p->size; j++) {
134
                        static struct count z;
135
                        p->counts[j] = z;
136
                }
137
        }
138
        if (p->counts[i].x != x || p->counts[i].y != y)
139
                for (i = 0; i < p->count; i++)
140
                        if (p->counts[i].x == x && p->counts[i].y == y)
141
                                break;
142
        if (i >= p->count)
143
                if (i >= p->size)
144
                        apoint(i, file, x, y, count);
145
                else {
146
                        p->count = i + 1;
147
                        p->counts[i].x = x;
148
                        p->counts[i].y = y;
149
                        p->counts[i].count = count;
150
                }
151
        else
152
                p->counts[i].count += count;
153
}
154
 
155
/* findcount - return count associated with (file,x,y) or -1 */
156
int findcount(char *file, int x, int y) {
157
        static struct file *cursor;
158
 
159
        if (cursor == 0 || cursor->name != file)
160
                cursor = findfile(file);
161
        if (cursor) {
162
                int l, u;
163
                struct count *c = cursor->counts;
164
                for (l = 0, u = cursor->count - 1; l <= u; ) {
165
                        int k = (l + u)/2;
166
                        if (c[k].y > y || c[k].y == y && c[k].x > x)
167
                                u = k - 1;
168
                        else if (c[k].y < y || c[k].y == y && c[k].x < x)
169
                                l = k + 1;
170
                        else
171
                                return c[k].count;
172
                }
173
        }
174
        return -1;
175
}
176
 
177
/* findfunc - return count associated with function name in file or -1 */
178
int findfunc(char *name, char *file) {
179
        static struct file *cursor;
180
 
181
        if (cursor == 0 || cursor->name != file)
182
                cursor = findfile(file);
183
        if (cursor) {
184
                struct func *p;
185
                for (p = cursor->funcs; p; p = p->link)
186
                        if (p->name == name)
187
                                return p->count.count;
188
        }
189
        return -1;
190
}
191
 
192
/* getd - read a nonnegative number */
193
static int getd(void) {
194
        int c, n = 0;
195
 
196
        while ((c = getc(fp)) != EOF && (c == ' ' || c == '\n' || c == '\t'))
197
                ;
198
        if (c >= '0' && c <= '9') {
199
                do
200
                        n = 10*n + (c - '0');
201
                while ((c = getc(fp)) >= '0' && c <= '9');
202
                return n;
203
        }
204
        return -1;
205
}
206
 
207
/* getstr - read a string */
208
static char *getstr(void) {
209
        int c;
210
        char buf[MAXTOKEN], *s = buf;
211
 
212
        while ((c = getc(fp)) != EOF && c != ' ' && c != '\n' && c != '\t')
213
                if (s - buf < (int)sizeof buf - 2)
214
                        *s++ = c;
215
        *s = 0;
216
        return s == buf ? (char *)0 : string(buf);
217
}
218
 
219
/* gather - read prof.out data from fd */
220
static int gather(void) {
221
        int i, nfiles, nfuncs, npoints;
222
        char *files[64];
223
 
224
        if ((nfiles = getd()) < 0)
225
                return 0;
226
        assert(nfiles < NELEMS(files));
227
        for (i = 0; i < nfiles; i++) {
228
                if ((files[i] = getstr()) == 0)
229
                        return -1;
230
                if (!findfile(files[i])) {
231
                        struct file *new;
232
                        NEW(new, PERM);
233
                        new->name = files[i];
234
                        new->size = new->count = 0;
235
                        new->counts = 0;
236
                        new->funcs = 0;
237
                        new->link = filelist;
238
                        filelist = new;
239
                }
240
        }
241
        if ((nfuncs = getd()) < 0)
242
                return -1;
243
        for (i = 0; i < nfuncs; i++) {
244
                struct func *q;
245
                char *name, *file;
246
                int f, x, y, count;
247
                if ((name = getstr()) == 0 || (f = getd()) <= 0
248
                || (x = getd()) < 0 || (y = getd()) < 0 || (count = getd()) < 0)
249
                        return -1;
250
                q = afunction(name, files[f-1], x, y, count);
251
                if ((name = getstr()) == 0 || (file = getstr()) == 0
252
                || (x = getd()) < 0 || (y = getd()) < 0)
253
                        return -1;
254
                if (*name != '?')
255
                        acaller(name, file, x, y, count, q);
256
        }
257
        if ((npoints = getd()) < 0)
258
                return -1;
259
        for (i = 0; i < npoints; i++) {
260
                int f, x, y, count;
261
                if ((f = getd()) < 0 || (x = getd()) < 0 || (y = getd()) < 0
262
                || (count = getd()) < 0)
263
                        return -1;
264
                if (f)
265
                        apoint(i, files[f-1], x, y, count);
266
        }
267
        return 1;
268
}
269
 
270
/* process - read prof.out data from file */
271
int process(char *file) {
272
        int more;
273
 
274
        if ((fp = fopen(file, "r")) != NULL) {
275
                struct file *p;
276
                while ((more = gather()) > 0)
277
                        ;
278
                fclose(fp);
279
                if (more < 0)
280
                        return more;
281
                for (p = filelist; p; p = p->link)
282
                        qsort(p->counts, p->count, sizeof *p->counts, compare);
283
                return 1;
284
        }
285
        return 0;
286
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.