[project @ 2000-04-05 10:06:36 by simonmar]
[ghc-hetmet.git] / ghc / utils / prof / cgprof / symbol.c
1 /* ------------------------------------------------------------------------
2  * $Id: symbol.c,v 1.1 2000/04/05 10:06:36 simonmar Exp $
3  *                                                                      
4  *      Copyright (C) 1995-2000 University of Oxford
5  *                                                                      
6  * Permission to use, copy, modify, and distribute this software,
7  * and to incorporate it, in whole or in part, into other software,
8  * is hereby granted without fee, provided that
9  *   (1) the above copyright notice and this permission notice appear in
10  *       all copies of the source code, and the above copyright notice
11  *       appear in clearly visible form on all supporting documentation
12  *       and distribution media;
13  *   (2) modified versions of this software be accompanied by a complete
14  *       change history describing author, date, and modifications made;
15  *       and
16  *   (3) any redistribution of the software, in original or modified
17  *       form, be without fee and subject to these same conditions.
18  * --------------------------------------------------------------------- */
19
20 #include "symbol.h"
21
22 /* -----------------------------------------------------------------------------
23  * Data structures
24  * -------------------------------------------------------------------------- */
25 int          symbol_table_next=0;
26 int          symbol_table_size=0;
27 name_object *symbol_table=NULL;
28
29 /* -----------------------------------------------------------------------------
30  * Create/grow symbol table
31  * -------------------------------------------------------------------------- */
32
33 void enlargeSymbolTable() {
34
35   if (symbol_table_size==0) {
36     symbol_table_next = 0;
37     symbol_table_size = SYMBOL_TABLE_INIT_SIZE;
38     symbol_table      = calloc(symbol_table_size,sizeof(name_object));
39   } else {
40     symbol_table_size += SYMBOL_TABLE_INIT_SIZE;
41     symbol_table       = realloc(symbol_table,
42                                  symbol_table_size*sizeof(name_object));
43   }
44   if (symbol_table==NULL) {
45     fprintf(stderr,"{enlargeSymbolTable} unable to allocate %d elements",
46             symbol_table_size);
47     exit(1);
48   }
49 }
50
51 /* -----------------------------------------------------------------------------
52  * Lookup/add name to symbol table
53  * -------------------------------------------------------------------------- */
54
55 name_id lookupSymbolTable(int type,int lineno,char* str) {
56   int i;
57   extern FILE *log;
58
59   for(i=0;i<symbol_table_next;i++) {
60     if ((type==symbol_table[i].type) && 
61         (strcmp(str,symbol_table[i].filename)==0) &&
62         (type==CG_STACK || (lineno==symbol_table[i].lineno))) {
63       return i;
64     }
65   }
66   fprintf(log,"{lookupSymbolTable} %d at %s line %d\n",type,str,lineno);
67   if (symbol_table_next==symbol_table_size) enlargeSymbolTable();
68   symbol_table[symbol_table_next].type    = type;
69   symbol_table[symbol_table_next].lineno  = lineno;
70   symbol_table[symbol_table_next].filename= malloc(1+strlen(str));
71   if (symbol_table[symbol_table_next].filename==NULL) {
72     fprintf(stderr,"{lookupSymbolTable} failed to allocate space");
73     exit(1);
74   }
75   strcpy(symbol_table[symbol_table_next].filename,str);
76   return (symbol_table_next++);
77 }
78
79 /* -----------------------------------------------------------------------------
80  * Comparison function to be used by \texttt{qsort}
81  * -------------------------------------------------------------------------- */
82
83 int cmp_symbol_entry(const int *x, const int *y) {
84   int i;
85
86   if (symbol_table[*x].type==symbol_table[*y].type) {
87     i = strcmp(symbol_table[*x].filename,symbol_table[*y].filename);
88     if  (i==0) return (symbol_table[*x].lineno - symbol_table[*y].lineno);
89     else return i;
90   } else {
91     if (symbol_table[*x].type==CG_STACK) return 1;
92     else return -1;
93   }
94 }
95
96
97 /* -----------------------------------------------------------------------------
98  * Pretty print a symbol table entry
99  * -------------------------------------------------------------------------- */
100
101 void printSymbolTable_entry(int idx) {
102   extern FILE *log;
103   if (symbol_table[idx].type==CG_SSTEP) {
104     fprintf(log,"(line %d of %s) ",symbol_table[idx].lineno,
105                                       symbol_table[idx].filename);
106   } else {
107     fprintf(log,"%s ",symbol_table[idx].filename);
108   }
109 }
110
111 void getNameFromSymbolTable(int idx, char* name) {
112   strcpy(name,symbol_table[idx].filename);
113 }
114