Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

prof.c

Go to the documentation of this file.
00001 #include "c.h"
00002 
00003 
00004 struct callsite {
00005     char *file, *name;
00006     union coordinate {
00007         unsigned int coord;
00008         struct { unsigned int y:16,x:10,index:6; } le;
00009         struct { unsigned int index:6,x:10,y:16; } be;
00010     } u;
00011 };
00012 struct func {
00013     struct func *link;
00014     struct caller *callers;
00015     char *name;
00016     union coordinate src;
00017 };
00018 struct map {        /* source code map; 200 coordinates/map */
00019     int size;
00020     union coordinate u[200];
00021 };
00022 
00023 int npoints;        /* # of execution points if -b specified */
00024 int ncalled = -1;   /* #times prof.out says current function was called */
00025 static Symbol YYlink;   /* symbol for file's struct _bbdata */
00026 static Symbol YYcounts; /* symbol for _YYcounts if -b specified */
00027 static List maplist;    /* list of struct map *'s */
00028 static List filelist;   /* list of file names */
00029 static Symbol funclist; /* list of struct func *'s */
00030 static Symbol afunc;    /* current function's struct func */
00031 
00032 /* bbcall - build tree to set _callsite at call site *cp, emit call site data */
00033 static void bbcall(Symbol yycounts, Coordinate *cp, Tree *e) {
00034     static Symbol caller;
00035     Value v;
00036     union coordinate u;
00037     Symbol p = genident(STATIC, array(voidptype, 0, 0), GLOBAL);
00038     Tree t = *e;
00039 
00040     defglobal(p, LIT);
00041     defpointer(cp->file ? mkstr(cp->file)->u.c.loc : (Symbol)0);
00042     defpointer(mkstr(cfunc->name)->u.c.loc);
00043     if (IR->little_endian) {
00044         u.le.x = cp->x;
00045         u.le.y = cp->y;
00046     } else {
00047         u.be.x = cp->x;
00048         u.be.y = cp->y;
00049     }
00050     (*IR->defconst)(U, unsignedtype->size, (v.u = u.coord, v));
00051     if (caller == 0) {
00052         caller = mksymbol(EXTERN, "_caller", ptr(voidptype));
00053         caller->defined = 0;
00054     }
00055     if (generic((*e)->op) != CALL)
00056         t = (*e)->kids[0];
00057     assert(generic(t->op) == CALL);
00058     t = tree(t->op, t->type,
00059         tree(RIGHT, t->kids[0]->type,
00060             t->kids[0],
00061             tree(RIGHT, t->kids[0]->type, asgn(caller, idtree(p)), t->kids[0])),
00062         t->kids[1]);
00063     if (generic((*e)->op) != CALL)
00064         t = tree((*e)->op, (*e)->type, t, (*e)->kids[1]);
00065     *e = t;
00066 }
00067 
00068 /* bbentry - return tree for _prologue(&afunc, &YYlink)' */
00069 static void bbentry(Symbol yylink, Symbol f) {
00070     static Symbol prologue;
00071     
00072     afunc = genident(STATIC, array(voidptype, 4, 0), GLOBAL);
00073     if (prologue == 0) {
00074         prologue = mksymbol(EXTERN, "_prologue", ftype(inttype, voidptype));
00075         prologue->defined = 0;
00076     }
00077     walk(vcall(prologue, voidtype, pointer(idtree(afunc)), pointer(idtree(yylink)), NULL), 0, 0);
00078 
00079 }
00080 
00081 /* bbexit - return tree for _epilogue(&afunc)' */
00082 static void bbexit(Symbol yylink, Symbol f, Tree e) {
00083     static Symbol epilogue;
00084     
00085     if (epilogue == 0) {
00086         epilogue = mksymbol(EXTERN, "_epilogue", ftype(inttype, voidptype));
00087         epilogue->defined = 0;
00088     }
00089     walk(vcall(epilogue, voidtype, pointer(idtree(afunc)), NULL), 0, 0);
00090 }
00091 
00092 /* bbfile - add file to list of file names, return its index */
00093 static int bbfile(char *file) {
00094     if (file) {
00095         List lp;
00096         int i = 1;
00097         if ((lp = filelist) != NULL)
00098             do {
00099                 lp = lp->link;
00100                 if (((Symbol)lp->x)->u.c.v.p == file)
00101                     return i;
00102                 i++;
00103             } while (lp != filelist);
00104         filelist = append(mkstr(file), filelist);
00105         return i;
00106     }
00107     return 0;
00108 }
00109 
00110 /* bbfunc - emit function name and src coordinates */
00111 static void bbfunc(Symbol yylink, Symbol f) {
00112     Value v;
00113     union coordinate u;
00114 
00115     defglobal(afunc, DATA);
00116     defpointer(funclist);
00117     defpointer(NULL);
00118     defpointer(mkstr(f->name)->u.c.loc);
00119     if (IR->little_endian) {
00120         u.le.x = f->u.f.pt.x;
00121         u.le.y = f->u.f.pt.y;
00122         u.le.index = bbfile(f->u.f.pt.file);
00123     } else {
00124         u.be.x = f->u.f.pt.x;
00125         u.be.y = f->u.f.pt.y;
00126         u.be.index = bbfile(f->u.f.pt.file);
00127     }
00128     (*IR->defconst)(U, unsignedtype->size, (v.u = u.coord, v));
00129     funclist = afunc;
00130 }
00131 
00132 /* bbincr - build tree to increment execution point at *cp */
00133 static void bbincr(Symbol yycounts, Coordinate *cp, Tree *e) {
00134     struct map *mp = maplist->x;
00135     Tree t;
00136 
00137     /* append *cp to source map */
00138     if (mp->size >= NELEMS(mp->u)) {
00139         NEW(mp, PERM);
00140         mp->size = 0;
00141         maplist = append(mp, maplist);
00142     }
00143     if (IR->little_endian) {
00144         mp->u[mp->size].le.x = cp->x;
00145         mp->u[mp->size].le.y = cp->y;
00146         mp->u[mp->size++].le.index = bbfile(cp->file);
00147     } else {
00148         mp->u[mp->size].be.x = cp->x;
00149         mp->u[mp->size].be.y = cp->y;
00150         mp->u[mp->size++].be.index = bbfile(cp->file);
00151     }
00152     t = incr('+', rvalue((*optree['+'])(ADD, pointer(idtree(yycounts)),
00153         consttree(npoints++, inttype))), consttree(1, inttype));
00154     if (*e)
00155         *e = tree(RIGHT, (*e)->type, t, *e);
00156     else
00157         *e = t;
00158 }
00159 
00160 /* bbvars - emit definition for basic block counting data */
00161 static void bbvars(Symbol yylink) {
00162     int i, j, n = npoints;
00163     Value v;
00164     struct map **mp;
00165     Symbol coords, files, *p;
00166 
00167     if (!YYcounts && !yylink)
00168         return;
00169     if (YYcounts) {
00170         if (n <= 0)
00171             n = 1;
00172         YYcounts->type = array(unsignedtype, n, 0);
00173         defglobal(YYcounts, BSS);
00174     }
00175     files = genident(STATIC, array(charptype, 1, 0), GLOBAL);
00176     defglobal(files, LIT);
00177     for (p = ltov(&filelist, PERM); *p; p++)
00178         defpointer((*p)->u.c.loc);
00179     defpointer(NULL);
00180     coords = genident(STATIC, array(unsignedtype, n, 0), GLOBAL);
00181     defglobal(coords, LIT);
00182     for (i = n, mp = ltov(&maplist, PERM); *mp; i -= (*mp)->size, mp++)
00183         for (j = 0; j < (*mp)->size; j++)
00184             (*IR->defconst)(U, unsignedtype->size, (v.u = (*mp)->u[j].coord, v));
00185     if (i > 0)
00186         (*IR->space)(i*coords->type->type->size);
00187     defpointer(NULL);
00188     defglobal(yylink, DATA);
00189     defpointer(NULL);
00190     (*IR->defconst)(U, unsignedtype->size, (v.u = n, v));
00191     defpointer(YYcounts);
00192     defpointer(coords);
00193     defpointer(files);
00194     defpointer(funclist);
00195 }
00196 
00197 /* profInit - initialize basic block profiling options */
00198 void prof_init(int argc, char *argv[]) {
00199     int i;
00200     static int inited;
00201 
00202     if (inited)
00203         return;
00204     inited = 1;
00205     type_init(argc, argv);
00206     if (IR)
00207         for (i = 1; i < argc; i++)
00208             if (strncmp(argv[i], "-a", 2) == 0) {
00209                 if (ncalled == -1
00210                 && process(argv[i][2] ? &argv[i][2] : "prof.out") > 0)
00211                     ncalled = 0;
00212             } else if ((strcmp(argv[i], "-b") == 0
00213                      || strcmp(argv[i], "-C") == 0) && YYlink == 0) {
00214                 YYlink = genident(STATIC, array(unsignedtype, 0, 0), GLOBAL);
00215                 attach((Apply)bbentry, YYlink, &events.entry);
00216                 attach((Apply)bbexit,  YYlink, &events.returns);
00217                 attach((Apply)bbfunc,  YYlink, &events.exit);
00218                 attach((Apply)bbvars,  YYlink, &events.end);
00219                 if (strcmp(argv[i], "-b") == 0) {
00220                     YYcounts = genident(STATIC, array(unsignedtype, 0, 0), GLOBAL);
00221                     maplist = append(allocate(sizeof (struct map), PERM), maplist);
00222                     ((struct map *)maplist->x)->size = 0;
00223                     attach((Apply)bbcall, YYcounts, &events.calls);
00224                     attach((Apply)bbincr, YYcounts, &events.points);
00225                 }
00226             }
00227 }

Generated on Thu Aug 25 12:38:14 2005 for Quake III Arena by  doxygen 1.3.9.1