1 /* ------------------------------------------------------------------------
2 * $Id: symbol.c,v 1.3 2003/08/01 14:50:50 panne Exp $
4 * Copyright (C) 1995-2000 University of Oxford
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;
16 * (3) any redistribution of the software, in original or modified
17 * form, be without fee and subject to these same conditions.
18 * --------------------------------------------------------------------- */
23 /* -----------------------------------------------------------------------------
25 * -------------------------------------------------------------------------- */
26 int symbol_table_next=0;
27 int symbol_table_size=0;
28 name_object *symbol_table=NULL;
30 /* -----------------------------------------------------------------------------
31 * Create/grow symbol table
32 * -------------------------------------------------------------------------- */
34 void enlargeSymbolTable() {
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));
41 symbol_table_size += SYMBOL_TABLE_INIT_SIZE;
42 symbol_table = realloc(symbol_table,
43 symbol_table_size*sizeof(name_object));
45 if (symbol_table==NULL) {
46 fprintf(stderr,"{enlargeSymbolTable} unable to allocate %d elements",
52 /* -----------------------------------------------------------------------------
53 * Lookup/add name to symbol table
54 * -------------------------------------------------------------------------- */
56 name_id lookupSymbolTable(int type,int lineno,char* str) {
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))) {
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");
76 strcpy(symbol_table[symbol_table_next].filename,str);
77 return (symbol_table_next++);
80 /* -----------------------------------------------------------------------------
81 * Comparison function to be used by \texttt{qsort}
82 * -------------------------------------------------------------------------- */
84 int cmp_symbol_entry(const int *x, const int *y) {
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);
92 if (symbol_table[*x].type==CG_STACK) return 1;
98 /* -----------------------------------------------------------------------------
99 * Pretty print a symbol table entry
100 * -------------------------------------------------------------------------- */
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);
108 fprintf(logFile,"%s ",symbol_table[idx].filename);
112 void getNameFromSymbolTable(int idx, char* name) {
113 strcpy(name,symbol_table[idx].filename);