Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

symtable Class Reference

This class implements a symbol table for the VHDL compiler. More...

#include <symtable.h>

Inheritance diagram for symtable:

Inheritance graph
[legend]
Collaboration diagram for symtable:

Collaboration graph
[legend]
List of all members.

Public Member Functions

void init (uint size=SQRT_SYMTAB)
 symtable (unsigned int size=SQRT_SYMTAB, pool *p=NULL)
void dealloc ()
 ~symtable ()
void set_pool (pool *p)
void set_owner (sym *parent)
symget_owner (void)
symenter_sym (STRING str, uint kind)
 Enter a symbol in the symbol table.

symenter_sym (const char *pChar, uint kind)
symfind_sym (STRING name)
 Search for a symbol in the symbol table whose name is in the STRING object name.

symfind_sym (const char *pChar)
unsigned int get_percent_alloced (void)
unsigned int get_max_list (void)
void pr (FILE *fp=stdout)
 Print the contents of the string table.

void dealloc_sub_tables ()
 Traverse the symbol table and find sub-symbols that have scope (e.g., symbol tables).

symfirst (void)
 The functions first and get_item are used to iterate through the string table.

symget_sym (void)
 get_str is used to iterate through the string table.


Private Member Functions

symnew_sym (STRING str, uint kind)
 Allocate a new symbol.

 symtable (const symtable &st)

Private Attributes

unsigned int table_size
unsigned int hash_slot
LIST< sym * >::handle list_handle
sparse_array< chain_elem > * hash
poolalloc_pool
symowner

Detailed Description

This class implements a symbol table for the VHDL compiler.

The symbol table consists of symbols with unique names (contained in the STRING class, entered in the string table).

This class was originally a generic template (HASH) for building hash tables. However, this template uses other templates (e.g., the sparse_array and LIST templates). C++ compilers (especially the HP C++ compiler) tend to have trouble with nested templates. So this class has been changed to be a specific class, instead of a template.

Definition at line 59 of file symtable.h.


Constructor & Destructor Documentation

symtable::symtable const symtable st  )  [inline, private]
 

Definition at line 104 of file symtable.h.

References FALSE.

00104 { assert( FALSE ); }

symtable::symtable unsigned int  size = SQRT_SYMTAB,
pool p = NULL
[inline]
 

Definition at line 113 of file symtable.h.

References alloc_pool, init(), NULL, and owner.

00114     {
00115 
00116         owner = NULL;
00117         alloc_pool = p;
00118         init( size );
00119     }

symtable::~symtable  )  [inline]
 

Definition at line 126 of file symtable.h.

References dealloc(), and dealloc_sub_tables().

00127     {
00128         dealloc_sub_tables();  // Deallocate all the "child" symbol tables.
00129         dealloc();             // Deallocate the sparse array and list memory for
00130                                // this symbol table.
00131     }


Member Function Documentation

void symtable::dealloc void   )  [inline]
 

Definition at line 120 of file symtable.h.

References sparse_array< chain_elem >::dealloc(), and hash.

Referenced by sym_scope::dealloc(), sym_component::proc_list_dealloc(), and ~symtable().

00121     {
00122         hash->dealloc();
00123 
00124         delete hash;
00125     }

void symtable::dealloc_sub_tables  ) 
 

Traverse the symbol table and find sub-symbols that have scope (e.g., symbol tables).

For each of these, call dealloc_sub_tables, and then dealloc.

Definition at line 227 of file symtable.C.

References sym::dealloc(), sym::get_symtab(), sym::has_scope(), hash, LIST< T >::list, NULL, sparse_array< chain_elem >::probe(), table_size, and uint.

Referenced by sym_component::proc_list_dealloc(), and ~symtable().

00228 {
00229     uint i;
00230 
00231     for (i = 0; i < table_size; i++) {
00232         if (hash->probe( i )) {
00233             LIST<sym *>::handle h;
00234             sym * pListSym;
00235             
00236             for (h = (*hash)[i].list.first(); h != NULL; h = (*hash)[i].list.next(h)) {
00237                 pListSym = (*hash)[i].list.get_item( h );
00238                 if (pListSym->has_scope()) {
00239                     pListSym->get_symtab()->dealloc_sub_tables();
00240                     pListSym->dealloc();
00241                 }
00242             } // for
00243         } // if
00244     } // for
00245 } // dealloc_sub_tables

sym* symtable::enter_sym const char *  pChar,
uint  kind
[inline]
 

Definition at line 158 of file symtable.h.

References enter_sym(), pChar, and STRING::SetText().

00159     {
00160         STRING str;
00161 
00162         str.SetText( pChar );
00163         return enter_sym( str, kind );
00164     }  // enter_sym

sym * symtable::enter_sym STRING  name,
uint  kind
 

Enter a symbol in the symbol table.

If "name" is not entered in the string table, enter it as well.

Definition at line 174 of file symtable.C.

References strtable::find_string(), STRING::GetText(), hash, hash_services::hash_value(), sparse_array< chain_elem >::insert(), new_sym(), NULL, sparse_array< chain_elem >::probe(), strtab, sy_process, table_size, and TRUE.

Referenced by enter_sym().

00175 {
00176     unsigned int ix, val;
00177     sym *retsym;
00178     STRING str;
00179 
00180     retsym = NULL;
00181 
00182     if (kind == sy_process) {
00183         // Processes are special cases.  They are allocated by the
00184         // symbol table class, since they are derived from the
00185         // symbol type.  But they are not entered into the symbol
00186         // table, since in VHDL a process name can't be referenced.
00187         // Allocation of processes by the symbol table allows uniform
00188         // allocation of all symbol derived objects.
00189         retsym = new_sym( name, kind);
00190     }
00191     else {
00192 
00193         assert( name.GetText() != NULL );
00194 
00195         val = hash_value( name.GetText() );
00196         str = strtab.find_string( name, TRUE );  // insert into table if its not there.
00197         // table size should always be a power of 2, so
00198         // table_size - 1 is likely to be a prime.
00199         ix = val % (table_size - 1);
00200         if (! hash->probe( ix ) ) {
00201 
00202             hash->insert( chain_elem(), ix );
00203             
00204             retsym = new_sym( str, kind );
00205             (*hash)[ix].list.add( retsym );
00206         }
00207         else {
00208             retsym = (*hash)[ix].search_list( str );
00209             if ( retsym == NULL) {
00210                 retsym = new_sym( str, kind );
00211                 (*hash)[ix].list.add( retsym );
00212             }
00213         }
00214     }
00215 
00216     return retsym;
00217 } // enter_sym

sym* symtable::find_sym const char *  pChar  )  [inline]
 

Definition at line 170 of file symtable.h.

References find_sym(), pChar, and STRING::SetText().

00171     {
00172         STRING str;
00173 
00174         str.SetText( pChar );
00175         return find_sym( str );
00176 
00177     } // find_sym with a char * argument

sym * symtable::find_sym STRING  name  ) 
 

Search for a symbol in the symbol table whose name is in the STRING object name.

Definition at line 139 of file symtable.C.

References FALSE, strtable::find_string(), STRING::GetText(), hash, hash_services::hash_value(), NULL, sparse_array< chain_elem >::probe(), strtab, and table_size.

Referenced by find_sym().

00140 {
00141     STRING str;
00142     sym *retsym;
00143     
00144     retsym = NULL;
00145     if (name.GetText() != NULL) {
00146         // If the name is not entered into the string table then
00147         // its not in the symbol table.  Of course the name could
00148         // be in the string table and not in the symbol table, since
00149         // the string table is global and symbol could be out of scope.
00150         str = strtab.find_string( name, FALSE );
00151         if (str.GetText() != NULL) {
00152             unsigned int ix, val;
00153 
00154             val = hash_value( str.GetText() );
00155             // table size should always be a power of 2, so
00156             // table_size - 1 is likely to be a prime.
00157             ix = val % (table_size - 1);
00158             if (hash->probe( ix )) { 
00159                 retsym =  (*hash)[ ix ].search_list( str );
00160             }   
00161         }
00162     }
00163 
00164     return retsym;
00165 } // find_sym

sym* symtable::first void   )  [inline]
 

The functions first and get_item are used to iterate through the string table.

Note that the strings will be returned in hash order, which is pseudorandom. An example is shown below:

sym *pSym;

for (pSym = symtab.first(); pSym != NULL; pSym = symtab.get_str()) { printf("%s\n", sym->get_name().GetText() ); }

Definition at line 220 of file symtable.h.

References get_sym(), hash_slot, list_handle, and NULL.

00221     {
00222         hash_slot = 0;
00223         list_handle = NULL;
00224         return get_sym();
00225     }

unsigned int symtable::get_max_list void   )  [inline]
 

Definition at line 182 of file symtable.h.

References hash, sparse_array< chain_elem >::probe(), table_size, and uint.

00183     {
00184         uint i;
00185         uint max, len;
00186 
00187         max = 0;
00188         for (i = 0; i < table_size; i++) {
00189             if (hash->probe( i )) {
00190                 len = (*hash)[i].list_len();
00191                 if (len > max) {
00192                     max = len;
00193                 }
00194             }
00195         }
00196         return max;
00197     } // get_max_list

sym* symtable::get_owner void   )  [inline]
 

Definition at line 144 of file symtable.h.

References NULL, and owner.

Referenced by new_sym().

00145     { 
00146         assert( owner != NULL );
00147         return owner; 
00148     }

unsigned int symtable::get_percent_alloced void   )  [inline]
 

Definition at line 180 of file symtable.h.

References sparse_array< chain_elem >::get_percent_alloced(), and hash.

00180 { return hash->get_percent_alloced(); }

sym * symtable::get_sym void   ) 
 

get_str is used to iterate through the string table.

See symtable.h.

Definition at line 275 of file symtable.C.

References hash, hash_slot, list_handle, NULL, sparse_array< chain_elem >::probe(), and table_size.

Referenced by first().

00276 {
00277     sym * pHashSym;
00278 
00279     pHashSym = NULL;
00280 
00281     while (list_handle == NULL && hash_slot < table_size) {
00282         if (hash->probe( hash_slot )) {
00283             list_handle = (*hash)[hash_slot].list.first();
00284         }
00285         if (list_handle == NULL) {
00286             hash_slot++;
00287         }
00288     } // while
00289 
00290     if (list_handle != NULL && hash_slot < table_size) {
00291 
00292         pHashSym = (*hash)[hash_slot].list.get_item( list_handle );
00293 
00294         list_handle = (*hash)[hash_slot].list.next(list_handle);
00295 
00296         // if we've hit the end of the list, increment the
00297         // slot so we're not stuck in the same place
00298         if (list_handle == NULL)
00299             hash_slot++;        
00300     }
00301 
00302     return pHashSym;
00303 } // get_str

void symtable::init uint  size = SQRT_SYMTAB  )  [inline]
 

Definition at line 107 of file symtable.h.

References sparse_array< chain_elem >::get_total_size(), hash, and table_size.

Referenced by symtable().

00108     {
00109         hash = new sparse_array<chain_elem>( (const unsigned int)size );
00110         table_size = hash->get_total_size();
00111     }

sym * symtable::new_sym STRING  str,
uint  kind
[private]
 

Allocate a new symbol.

The STRING should have already been entered in the string table. Note that as different derived symbol types are added to dsym.h this function must be changed as well.

Definition at line 59 of file symtable.C.

References alloc_pool, bad_symbol, FALSE, get_owner(), sym::get_symtab(), sym::get_typtab(), NULL, set_owner(), typetable::set_pool(), set_pool(), sym::set_scope(), sy_block_label, sy_component, sy_const, sy_ident, sy_package, sy_process, sy_subprog, and sy_type.

Referenced by enter_sym().

00060 {
00061     sym *pNewSym;
00062 
00063     assert( alloc_pool != NULL );
00064 
00065     switch ( kind ) {
00066     // IDENT: has scope
00067     case sy_ident:
00068         pNewSym = new( alloc_pool ) sym_ident( str );
00069         pNewSym->set_scope( get_owner() );
00070         break;
00071     // TYPE : no scope
00072     case sy_type:
00073         pNewSym = new( alloc_pool ) sym_type( str );
00074         break;
00075     // CONST: no scope
00076     case sy_const:
00077         pNewSym = new( alloc_pool ) sym_const( str );
00078         break;
00079 
00080     // SUBPROG: local symbol table
00081     case sy_subprog:
00082         pNewSym = new( alloc_pool ) sym_subprog( str );
00083 
00084         // the subprogram is in the scope of the owner of this symbol table
00085         pNewSym->set_scope( get_owner() );
00086         // A function or procedure will allocate memory from
00087         // from the same pool as its parent scope (which will be
00088         // either a component or the global scope, in the case of
00089         // a function in a package).
00090         //
00091         pNewSym->get_symtab()->set_pool( alloc_pool );
00092         // Set the allocation pool for the type table
00093         pNewSym->get_typtab()->set_pool( alloc_pool );  
00094         // The symbol table is owned by the subprogram
00095         pNewSym->get_symtab()->set_owner( pNewSym );
00096         break;
00097 
00098     // COMPONENT: local memory pool and local symbol table
00099     case sy_component:
00100         pNewSym = new( alloc_pool ) sym_component( str );
00101 
00102         // symbols in the component scope are owned by the component
00103         pNewSym->get_symtab()->set_owner( pNewSym  );
00104         break;
00105 
00106     // PROCESS: local symbol table
00107     case sy_process:
00108         pNewSym = new( alloc_pool ) sym_process( str );
00109 
00110         // the process is in the scope of its component
00111         pNewSym->set_scope( get_owner() );
00112         // set the memory pool to the pool of the parent
00113         pNewSym->get_symtab()->set_pool( alloc_pool );
00114         // Set the allocation pool for the type table
00115         pNewSym->get_typtab()->set_pool( alloc_pool );  
00116         // The symbol table is owned by the process
00117         pNewSym->get_symtab()->set_owner( pNewSym );
00118         break;
00119     case sy_package:   // its not currently clear what to do about packages
00120     case sy_block_label:
00121     case bad_symbol:
00122     default:
00123         // bad symbol kind
00124         assert( FALSE );
00125         break;
00126     } // switch
00127 
00128     assert( pNewSym != NULL );
00129 
00130     return pNewSym;
00131 } // new_sym

void symtable::pr FILE *  fp = stdout  ) 
 

Print the contents of the string table.

Definition at line 250 of file symtable.C.

References sym::get_name(), hash, LIST< T >::list, NULL, STRING::pr(), sparse_array< chain_elem >::probe(), table_size, and uint.

00251 {
00252     uint i;
00253 
00254     for (i = 0; i < table_size; i++) {
00255         if (hash->probe( i )) {
00256             LIST<sym *>::handle h;
00257             sym * pListSym;
00258             
00259             for (h = (*hash)[i].list.first(); h != NULL; h = (*hash)[i].list.next(h)) {
00260                 // fprintf(fp, "hash_label::pr: i = %d, ", i );
00261                 pListSym = (*hash)[i].list.get_item( h );
00262                 pListSym->get_name().pr(fp);
00263                 fprintf(fp, "\n");
00264             } // for
00265         } // if
00266     } // for
00267 } // pr

void symtable::set_owner sym parent  )  [inline]
 

Definition at line 138 of file symtable.h.

References owner.

Referenced by new_sym().

00138 { owner = parent; }

void symtable::set_pool pool p  )  [inline]
 

Definition at line 133 of file symtable.h.

References alloc_pool.

Referenced by sym_component::init(), and new_sym().

00134     {
00135         alloc_pool = p;
00136     }


Member Data Documentation

pool* symtable::alloc_pool [private]
 

Definition at line 93 of file symtable.h.

Referenced by new_sym(), set_pool(), and symtable().

sparse_array<chain_elem>* symtable::hash [private]
 

Definition at line 92 of file symtable.h.

Referenced by dealloc(), dealloc_sub_tables(), enter_sym(), find_sym(), get_max_list(), get_percent_alloced(), get_sym(), init(), and pr().

unsigned int symtable::hash_slot [private]
 

Definition at line 89 of file symtable.h.

Referenced by first(), and get_sym().

LIST<sym *>::handle symtable::list_handle [private]
 

Definition at line 90 of file symtable.h.

Referenced by first(), and get_sym().

sym* symtable::owner [private]
 

Definition at line 95 of file symtable.h.

Referenced by get_owner(), set_owner(), and symtable().

unsigned int symtable::table_size [private]
 

Definition at line 88 of file symtable.h.

Referenced by dealloc_sub_tables(), enter_sym(), find_sym(), get_max_list(), get_sym(), init(), and pr().


The documentation for this class was generated from the following files:
Generated on Wed Mar 31 21:16:13 2004 for Data Structures for a VHDL Compiler by doxygen 1.3.3