1 #ifndef IVL_symbols_H
2 #define IVL_symbols_H
3 /*
4  * Copyright (c) 2001-2014 Stephen Williams (steve@icarus.com)
5  *
6  *    This source code is free software; you can redistribute it
7  *    and/or modify it in source code form under the terms of the GNU
8  *    General Public License as published by the Free Software
9  *    Foundation; either version 2 of the License, or (at your option)
10  *    any later version.
11  *
12  *    This program is distributed in the hope that it will be useful,
13  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *    GNU General Public License for more details.
16  *
17  *    You should have received a copy of the GNU General Public License
18  *    along with this program; if not, write to the Free Software
19  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 /*
23  * The symbol_table_t is intended as a means to hold and quickly index
24  * large symbol tables with small symbol values. That is, the value
25  * should fit in a 32bit unsigned integer (not even necessarily a
26  * pointer.)
27  *
28  * The key is an unstructured ASCII string, terminated by a
29  * null. Items added to the table are not removed, unless the entire
30  * table is deleted.
31  *
32  * The compiler uses symbol tables to help match up operands to
33  * referenced objects in the source. The compiler knows by the context
34  * that the symbol appears what kind of thing is referenced, and so
35  * what symbol table to look in.
36  */
37 
38 # include  "config.h"
39 # include  "vvp_net.h"
40 
41 /*
42  * This is the basic type of a symbol table. It is opaque. Don't even
43  * try to look inside. The actual implementation is given in the
44  * symbols.cc source file.
45  */
46 typedef class symbol_table_s *symbol_table_t;
47 
48 typedef struct symbol_value_s {
49       union {
50 	    vvp_net_t*net;
51 	    void*ptr;
52       };
53 } symbol_value_t;
54 
55 
56 class symbol_table_s {
57     public:
58       explicit symbol_table_s();
59       virtual ~symbol_table_s();
60 
61 	// This method locates the value in the symbol table and sets its
62 	// value. If the key doesn't yet exist, create it.
63       void sym_set_value(const char*key, symbol_value_t val);
64 
65 	// This method locates the value in the symbol table and returns
66 	// it. If the value does not exist, create it, initialize it with
67 	// zero and return the zero value.
68       symbol_value_t sym_get_value(const char*key);
69 
70     private:
symbol_table_s(const symbol_table_s &)71       symbol_table_s(const symbol_table_s&) { assert(0); };
72       struct tree_node_*root;
73       struct key_strings*str_chunk;
74       unsigned str_used;
75 
76       symbol_value_t find_value_(struct tree_node_*cur,
77 				 const char*key, symbol_value_t val,
78 				 bool force_flag);
79       char*key_strdup_(const char*str);
80 };
81 
82 /*
83  * Create a new symbol table or release an existing one. A new symbol
84  * table has no keys and no values. As a symbol table is built up, it
85  * consumes more and more memory. When the table is no longer needed,
86  * the delete_symbol_table method will delete the table, including all
87  * the space for the keys.
88  */
new_symbol_table(void)89 inline symbol_table_t new_symbol_table(void) { return new symbol_table_s; }
delete_symbol_table(symbol_table_t tbl)90 inline void delete_symbol_table(symbol_table_t tbl) { delete tbl; }
91 
92 // These are obsolete, and here only to support older code.
sym_set_value(symbol_table_t tbl,const char * key,symbol_value_t val)93 inline void sym_set_value(symbol_table_t tbl, const char*key, symbol_value_t val)
94 { tbl->sym_set_value(key, val); }
95 
sym_get_value(symbol_table_t tbl,const char * key)96 inline symbol_value_t sym_get_value(symbol_table_t tbl, const char*key)
97 { return tbl->sym_get_value(key); }
98 
99 /*
100  * This template is a type-safe interface to the symbol table.
101  */
102 template <class T> class symbol_map_s : private symbol_table_s {
103 
104     public:
sym_set_value(const char * key,T * val)105       void sym_set_value(const char*key, T*val)
106       { symbol_value_t tmp;
107 	tmp.ptr = val;
108 	symbol_table_s::sym_set_value(key, tmp);
109       }
110 
sym_get_value(const char * key)111       T* sym_get_value(const char*key)
112       { symbol_value_t val = symbol_table_s::sym_get_value(key);
113 	return reinterpret_cast<T*>(val.ptr);
114       }
115 };
116 
117 #endif /* IVL_symbols_H */
118