8
struct { unsigned int y:16,x:10,index:6; } le;
9
struct { unsigned int index:6,x:10,y:16; } be;
14
struct caller *callers;
18
struct map { /* source code map; 200 coordinates/map */
20
union coordinate u[200];
23
int npoints; /* # of execution points if -b specified */
24
int ncalled = -1; /* #times prof.out says current function was called */
25
static Symbol YYlink; /* symbol for file's struct _bbdata */
26
static Symbol YYcounts; /* symbol for _YYcounts if -b specified */
27
static List maplist; /* list of struct map *'s */
28
static List filelist; /* list of file names */
29
static Symbol funclist; /* list of struct func *'s */
30
static Symbol afunc; /* current function's struct func */
32
/* bbcall - build tree to set _callsite at call site *cp, emit call site data */
33
static void bbcall(Symbol yycounts, Coordinate *cp, Tree *e) {
37
Symbol p = genident(STATIC, array(voidptype, 0, 0), GLOBAL);
41
defpointer(cp->file ? mkstr(cp->file)->u.c.loc : (Symbol)0);
42
defpointer(mkstr(cfunc->name)->u.c.loc);
43
if (IR->little_endian) {
50
(*IR->defconst)(U, unsignedtype->size, (v.u = u.coord, v));
52
caller = mksymbol(EXTERN, "_caller", ptr(voidptype));
55
if (generic((*e)->op) != CALL)
57
assert(generic(t->op) == CALL);
58
t = tree(t->op, t->type,
59
tree(RIGHT, t->kids[0]->type,
61
tree(RIGHT, t->kids[0]->type, asgn(caller, idtree(p)), t->kids[0])),
63
if (generic((*e)->op) != CALL)
64
t = tree((*e)->op, (*e)->type, t, (*e)->kids[1]);
68
/* bbentry - return tree for _prologue(&afunc, &YYlink)' */
69
static void bbentry(Symbol yylink, Symbol f) {
70
static Symbol prologue;
72
afunc = genident(STATIC, array(voidptype, 4, 0), GLOBAL);
74
prologue = mksymbol(EXTERN, "_prologue", ftype(inttype, voidptype));
75
prologue->defined = 0;
77
walk(vcall(prologue, voidtype, pointer(idtree(afunc)), pointer(idtree(yylink)), NULL), 0, 0);
81
/* bbexit - return tree for _epilogue(&afunc)' */
82
static void bbexit(Symbol yylink, Symbol f, Tree e) {
83
static Symbol epilogue;
86
epilogue = mksymbol(EXTERN, "_epilogue", ftype(inttype, voidptype));
87
epilogue->defined = 0;
89
walk(vcall(epilogue, voidtype, pointer(idtree(afunc)), NULL), 0, 0);
92
/* bbfile - add file to list of file names, return its index */
93
static int bbfile(char *file) {
97
if ((lp = filelist) != NULL)
100
if (((Symbol)lp->x)->u.c.v.p == file)
103
} while (lp != filelist);
104
filelist = append(mkstr(file), filelist);
110
/* bbfunc - emit function name and src coordinates */
111
static void bbfunc(Symbol yylink, Symbol f) {
115
defglobal(afunc, DATA);
116
defpointer(funclist);
118
defpointer(mkstr(f->name)->u.c.loc);
119
if (IR->little_endian) {
120
u.le.x = f->u.f.pt.x;
121
u.le.y = f->u.f.pt.y;
122
u.le.index = bbfile(f->u.f.pt.file);
124
u.be.x = f->u.f.pt.x;
125
u.be.y = f->u.f.pt.y;
126
u.be.index = bbfile(f->u.f.pt.file);
128
(*IR->defconst)(U, unsignedtype->size, (v.u = u.coord, v));
132
/* bbincr - build tree to increment execution point at *cp */
133
static void bbincr(Symbol yycounts, Coordinate *cp, Tree *e) {
134
struct map *mp = maplist->x;
137
/* append *cp to source map */
138
if (mp->size >= NELEMS(mp->u)) {
141
maplist = append(mp, maplist);
143
if (IR->little_endian) {
144
mp->u[mp->size].le.x = cp->x;
145
mp->u[mp->size].le.y = cp->y;
146
mp->u[mp->size++].le.index = bbfile(cp->file);
148
mp->u[mp->size].be.x = cp->x;
149
mp->u[mp->size].be.y = cp->y;
150
mp->u[mp->size++].be.index = bbfile(cp->file);
152
t = incr('+', rvalue((*optree['+'])(ADD, pointer(idtree(yycounts)),
153
consttree(npoints++, inttype))), consttree(1, inttype));
155
*e = tree(RIGHT, (*e)->type, t, *e);
160
/* bbvars - emit definition for basic block counting data */
161
static void bbvars(Symbol yylink) {
162
int i, j, n = npoints;
165
Symbol coords, files, *p;
167
if (!YYcounts && !yylink)
172
YYcounts->type = array(unsignedtype, n, 0);
173
defglobal(YYcounts, BSS);
175
files = genident(STATIC, array(charptype, 1, 0), GLOBAL);
176
defglobal(files, LIT);
177
for (p = ltov(&filelist, PERM); *p; p++)
178
defpointer((*p)->u.c.loc);
180
coords = genident(STATIC, array(unsignedtype, n, 0), GLOBAL);
181
defglobal(coords, LIT);
182
for (i = n, mp = ltov(&maplist, PERM); *mp; i -= (*mp)->size, mp++)
183
for (j = 0; j < (*mp)->size; j++)
184
(*IR->defconst)(U, unsignedtype->size, (v.u = (*mp)->u[j].coord, v));
186
(*IR->space)(i*coords->type->type->size);
188
defglobal(yylink, DATA);
190
(*IR->defconst)(U, unsignedtype->size, (v.u = n, v));
191
defpointer(YYcounts);
194
defpointer(funclist);
197
/* profInit - initialize basic block profiling options */
198
void prof_init(int argc, char *argv[]) {
205
type_init(argc, argv);
207
for (i = 1; i < argc; i++)
208
if (strncmp(argv[i], "-a", 2) == 0) {
210
&& process(argv[i][2] ? &argv[i][2] : "prof.out") > 0)
212
} else if ((strcmp(argv[i], "-b") == 0
213
|| strcmp(argv[i], "-C") == 0) && YYlink == 0) {
214
YYlink = genident(STATIC, array(unsignedtype, 0, 0), GLOBAL);
215
attach((Apply)bbentry, YYlink, &events.entry);
216
attach((Apply)bbexit, YYlink, &events.returns);
217
attach((Apply)bbfunc, YYlink, &events.exit);
218
attach((Apply)bbvars, YYlink, &events.end);
219
if (strcmp(argv[i], "-b") == 0) {
220
YYcounts = genident(STATIC, array(unsignedtype, 0, 0), GLOBAL);
221
maplist = append(allocate(sizeof (struct map), PERM), maplist);
222
((struct map *)maplist->x)->size = 0;
223
attach((Apply)bbcall, YYcounts, &events.calls);
224
attach((Apply)bbincr, YYcounts, &events.points);