bbexit.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct callsite {
  4. char *file, *name;
  5. union coordinate {
  6. struct { unsigned int index:6,x:10,y:16; } be;
  7. struct { unsigned int y:16,x:10,index:6; } le;
  8. unsigned int coord;
  9. } u;
  10. } *_caller;
  11. static struct _bbdata {
  12. struct _bbdata *link;
  13. unsigned npoints, *counts;
  14. union coordinate *coords;
  15. char **files;
  16. struct func {
  17. struct func *link;
  18. struct caller {
  19. struct caller *link;
  20. struct callsite *caller;
  21. unsigned count;
  22. } *callers;
  23. char *name;
  24. union coordinate src;
  25. } *funcs;
  26. } tail, *_bblist = &tail;
  27. static void unpack(unsigned int coord, int *index, int *x, int *y) {
  28. static union { int x; char endian; } little = { 1 };
  29. union coordinate u;
  30. u.coord = coord;
  31. if (little.endian) {
  32. *index = u.le.index;
  33. *x = u.le.x;
  34. *y = u.le.y;
  35. } else {
  36. *index = u.be.index;
  37. *x = u.be.x;
  38. *y = u.be.y;
  39. }
  40. }
  41. static void profout(struct _bbdata *p, FILE *fp) {
  42. int i, index, x, y;
  43. struct func *f;
  44. struct caller *q;
  45. for (i = 0; p->files[i]; i++)
  46. ;
  47. fprintf(fp, "%d\n", i);
  48. for (i = 0; p->files[i]; i++)
  49. fprintf(fp, "%s\n", p->files[i]);
  50. for (i = 0, f = p->funcs; f; i++, f = f->link)
  51. if (q = f->callers)
  52. for (i--; q; q = q->link)
  53. i++;
  54. fprintf(fp, "%d\n", i);
  55. for (f = p->funcs; f; f = f->link) {
  56. int n = 0;
  57. for (q = f->callers; q; n += q->count, q = q->link) {
  58. unpack(f->src.coord, &index, &x, &y);
  59. fprintf(fp, "%s %d %d %d %d", f->name, index, x, y, q->count);
  60. if (q->caller) {
  61. unpack(q->caller->u.coord, &index, &x, &y);
  62. fprintf(fp, " %s %s %d %d\n", q->caller->name, q->caller->file, x, y);
  63. } else
  64. fprintf(fp, " ? ? 0 0\n");
  65. }
  66. if (n == 0) {
  67. unpack(f->src.coord, &index, &x, &y);
  68. fprintf(fp, "%s %d %d %d 0 ? ? 0 0\n", f->name, index, x, y);
  69. }
  70. }
  71. fprintf(fp, "%d\n", p->npoints);
  72. for (i = 0; i < p->npoints; i++) {
  73. unpack(p->coords[i].coord, &index, &x, &y);
  74. fprintf(fp, "%d %d %d %d\n", index, x, y, p->counts[i]);
  75. }
  76. }
  77. static void bbexit(void) {
  78. FILE *fp;
  79. if (_bblist != &tail && (fp = fopen("prof.out", "a"))) {
  80. for ( ; _bblist != &tail; _bblist = _bblist->link)
  81. profout(_bblist, fp);
  82. fclose(fp);
  83. }
  84. }
  85. void _epilogue(struct func *callee) {
  86. _caller = 0;
  87. }
  88. void _prologue(struct func *callee, struct _bbdata *yylink) {
  89. static struct caller callers[4096];
  90. static int next;
  91. struct caller *p;
  92. if (!yylink->link) {
  93. yylink->link = _bblist;
  94. _bblist = yylink;
  95. if (next == 0)
  96. atexit(bbexit);
  97. }
  98. for (p = callee->callers; p; p = p->link)
  99. if (p->caller == _caller) {
  100. p->count++;
  101. break;
  102. }
  103. if (!p && next < sizeof callers/sizeof callers[0]) {
  104. p = &callers[next++];
  105. p->caller = _caller;
  106. p->count = 1;
  107. p->link = callee->callers;
  108. callee->callers = p;
  109. }
  110. _caller = 0;
  111. }