Make the wired-in packages code handle ndp mapping to ndp-seq or ndp-par
[ghc-hetmet.git] / utils / prof / cgprof / symbol.c
1 /* ------------------------------------------------------------------------
2  * $Id: symbol.c,v 1.3 2003/08/01 14:50:50 panne 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 <string.h>
21 #include "symbol.h"
22
23 /* -----------------------------------------------------------------------------
24  * Data structures
25  * -------------------------------------------------------------------------- */
26 int          symbol_table_next=0;
27 int          symbol_table_size=0;
28 name_object *symbol_table=NULL;
29
30 /* -----------------------------------------------------------------------------
31  * Create/grow symbol table
32  * -------------------------------------------------------------------------- */
33
34 void enlargeSymbolTable() {
35
36   if (symbol_table_size==0) {
37     symbol_table_next = 0;
38     symbol_table_size = SYMBOL_TABLE_INIT_SIZE;
39     symbol_table      = calloc(symbol_table_size,sizeof(name_object));
40   } else {
41     symbol_table_size += SYMBOL_TABLE_INIT_SIZE;
42     symbol_table       = realloc(symbol_table,
43                                  symbol_table_size*sizeof(name_object));
44   }
45   if (symbol_table==NULL) {
46     fprintf(stderr,"{enlargeSymbolTable} unable to allocate %d elements",
47             symbol_table_size);
48     exit(1);
49   }
50 }
51
52 /* -----------------------------------------------------------------------------
53  * Lookup/add name to symbol table
54  * -------------------------------------------------------------------------- */
55
56 name_id lookupSymbolTable(int type,int lineno,char* str) {
57   int i;
58   extern FILE *logFile;
59
60   for(i=0;i<symbol_table_next;i++) {
61     if ((type==symbol_table[i].type) && 
62         (strcmp(str,symbol_table[i].filename)==0) &&
63         (type==CG_STACK || (lineno==symbol_table[i].lineno))) {
64       return i;
65     }
66   }
67   fprintf(logFile,"{lookupSymbolTable} %d at %s line %d\n",type,str,lineno);
68   if (symbol_table_next==symbol_table_size) enlargeSymbolTable();
69   symbol_table[symbol_table_next].type    = type;
70   symbol_table[symbol_table_next].lineno  = lineno;
71   symbol_table[symbol_table_next].filename= malloc(1+strlen(str));
72   if (symbol_table[symbol_table_next].filename==NULL) {
73     fprintf(stderr,"{lookupSymbolTable} failed to allocate space");
74     exit(1);
75   }
76   strcpy(symbol_table[symbol_table_next].filename,str);
77   return (symbol_table_next++);
78 }
79
80 /* -----------------------------------------------------------------------------
81  * Comparison function to be used by \texttt{qsort}
82  * -------------------------------------------------------------------------- */
83
84 int cmp_symbol_entry(const int *x, const int *y) {
85   int i;
86
87   if (symbol_table[*x].type==symbol_table[*y].type) {
88     i = strcmp(symbol_table[*x].filename,symbol_table[*y].filename);
89     if  (i==0) return (symbol_table[*x].lineno - symbol_table[*y].lineno);
90     else return i;
91   } else {
92     if (symbol_table[*x].type==CG_STACK) return 1;
93     else return -1;
94   }
95 }
96
97
98 /* -----------------------------------------------------------------------------
99  * Pretty print a symbol table entry
100  * -------------------------------------------------------------------------- */
101
102 void printSymbolTable_entry(int idx) {
103   extern FILE *logFile;
104   if (symbol_table[idx].type==CG_SSTEP) {
105     fprintf(logFile,"(line %d of %s) ",symbol_table[idx].lineno,
106                                       symbol_table[idx].filename);
107   } else {
108     fprintf(logFile,"%s ",symbol_table[idx].filename);
109   }
110 }
111
112 void getNameFromSymbolTable(int idx, char* name) {
113   strcpy(name,symbol_table[idx].filename);
114 }
115