1 /* ------------------------------------------------------------------------
2 * $Id: symbol.c,v 1.1 2000/04/05 10:06:36 simonmar 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 * --------------------------------------------------------------------- */
22 /* -----------------------------------------------------------------------------
24 * -------------------------------------------------------------------------- */
25 int symbol_table_next=0;
26 int symbol_table_size=0;
27 name_object *symbol_table=NULL;
29 /* -----------------------------------------------------------------------------
30 * Create/grow symbol table
31 * -------------------------------------------------------------------------- */
33 void enlargeSymbolTable() {
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));
40 symbol_table_size += SYMBOL_TABLE_INIT_SIZE;
41 symbol_table = realloc(symbol_table,
42 symbol_table_size*sizeof(name_object));
44 if (symbol_table==NULL) {
45 fprintf(stderr,"{enlargeSymbolTable} unable to allocate %d elements",
51 /* -----------------------------------------------------------------------------
52 * Lookup/add name to symbol table
53 * -------------------------------------------------------------------------- */
55 name_id lookupSymbolTable(int type,int lineno,char* str) {
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))) {
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");
75 strcpy(symbol_table[symbol_table_next].filename,str);
76 return (symbol_table_next++);
79 /* -----------------------------------------------------------------------------
80 * Comparison function to be used by \texttt{qsort}
81 * -------------------------------------------------------------------------- */
83 int cmp_symbol_entry(const int *x, const int *y) {
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);
91 if (symbol_table[*x].type==CG_STACK) return 1;
97 /* -----------------------------------------------------------------------------
98 * Pretty print a symbol table entry
99 * -------------------------------------------------------------------------- */
101 void printSymbolTable_entry(int idx) {
103 if (symbol_table[idx].type==CG_SSTEP) {
104 fprintf(log,"(line %d of %s) ",symbol_table[idx].lineno,
105 symbol_table[idx].filename);
107 fprintf(log,"%s ",symbol_table[idx].filename);
111 void getNameFromSymbolTable(int idx, char* name) {
112 strcpy(name,symbol_table[idx].filename);