xref: /aosp_15_r20/external/mesa3d/src/mesa/program/symbol_table.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2008 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 
25 #include "main/errors.h"
26 #include "symbol_table.h"
27 #include "util/hash_table.h"
28 #include "util/u_string.h"
29 
30 struct symbol {
31    /** Symbol name. */
32    char *name;
33 
34     /**
35      * Link to the next symbol in the table with the same name
36      *
37      * The linked list of symbols with the same name is ordered by scope
38      * from inner-most to outer-most.
39      */
40     struct symbol *next_with_same_name;
41 
42     /**
43      * Link to the next symbol in the table with the same scope
44      *
45      * The linked list of symbols with the same scope is unordered.  Symbols
46      * in this list my have unique names.
47      */
48     struct symbol *next_with_same_scope;
49 
50     /** Scope depth where this symbol was defined. */
51     unsigned depth;
52 
53     /**
54      * Arbitrary user supplied data.
55      */
56     void *data;
57 };
58 
59 
60 /**
61  * Element of the scope stack.
62  */
63 struct scope_level {
64     /** Link to next (inner) scope level. */
65     struct scope_level *next;
66 
67     /** Linked list of symbols with the same scope. */
68     struct symbol *symbols;
69 };
70 
71 
72 /**
73  *
74  */
75 struct _mesa_symbol_table {
76     /** Hash table containing all symbols in the symbol table. */
77     struct hash_table *ht;
78 
79     /** Top of scope stack. */
80     struct scope_level *current_scope;
81 
82     /** Current scope depth. */
83     unsigned depth;
84 };
85 
86 void
_mesa_symbol_table_pop_scope(struct _mesa_symbol_table * table)87 _mesa_symbol_table_pop_scope(struct _mesa_symbol_table *table)
88 {
89     struct scope_level *const scope = table->current_scope;
90     struct symbol *sym = scope->symbols;
91 
92     table->current_scope = scope->next;
93     table->depth--;
94 
95     free(scope);
96 
97     while (sym != NULL) {
98         struct symbol *const next = sym->next_with_same_scope;
99         struct hash_entry *hte = _mesa_hash_table_search(table->ht,
100                                                          sym->name);
101         if (sym->next_with_same_name) {
102            /* If there is a symbol with this name in an outer scope update
103             * the hash table to point to it.
104             */
105            hte->data = sym->next_with_same_name;
106         } else {
107            _mesa_hash_table_remove(table->ht, hte);
108         }
109 
110         free(sym);
111         sym = next;
112     }
113 }
114 
115 
116 void
_mesa_symbol_table_push_scope(struct _mesa_symbol_table * table)117 _mesa_symbol_table_push_scope(struct _mesa_symbol_table *table)
118 {
119     struct scope_level *const scope = calloc(1, sizeof(*scope));
120     if (scope == NULL) {
121        _mesa_error_no_memory(__func__);
122        return;
123     }
124 
125     scope->next = table->current_scope;
126     table->current_scope = scope;
127     table->depth++;
128 }
129 
130 
131 static struct symbol *
find_symbol(struct _mesa_symbol_table * table,const char * name)132 find_symbol(struct _mesa_symbol_table *table, const char *name)
133 {
134    struct hash_entry *entry = _mesa_hash_table_search(table->ht, name);
135    return entry ? (struct symbol *) entry->data : NULL;
136 }
137 
138 
139 /**
140  * Determine the scope "distance" of a symbol from the current scope
141  *
142  * \return
143  * A non-negative number for the number of scopes between the current scope
144  * and the scope where a symbol was defined.  A value of zero means the current
145  * scope.  A negative number if the symbol does not exist.
146  */
147 int
_mesa_symbol_table_symbol_scope(struct _mesa_symbol_table * table,const char * name)148 _mesa_symbol_table_symbol_scope(struct _mesa_symbol_table *table,
149                                 const char *name)
150 {
151    struct symbol *const sym = find_symbol(table, name);
152 
153    if (sym) {
154       assert(sym->depth <= table->depth);
155       return table->depth - sym->depth;
156    }
157 
158    return -1;
159 }
160 
161 
162 void *
_mesa_symbol_table_find_symbol(struct _mesa_symbol_table * table,const char * name)163 _mesa_symbol_table_find_symbol(struct _mesa_symbol_table *table,
164                                const char *name)
165 {
166    struct symbol *const sym = find_symbol(table, name);
167    if (sym)
168       return sym->data;
169 
170    return NULL;
171 }
172 
173 
174 int
_mesa_symbol_table_add_symbol(struct _mesa_symbol_table * table,const char * name,void * declaration)175 _mesa_symbol_table_add_symbol(struct _mesa_symbol_table *table,
176                               const char *name, void *declaration)
177 {
178    struct symbol *new_sym;
179    uint32_t hash = _mesa_hash_string(name);
180    struct hash_entry *entry = _mesa_hash_table_search_pre_hashed(table->ht, hash, name);
181    struct symbol *sym = entry ? entry->data : NULL;
182 
183    if (sym && sym->depth == table->depth)
184       return -1;
185 
186    new_sym = calloc(1, sizeof(*sym) + (sym ? 0 : (strlen(name) + 1)));
187    if (new_sym == NULL) {
188       _mesa_error_no_memory(__func__);
189       return -1;
190    }
191 
192    if (sym) {
193       /* Store link to symbol in outer scope with the same name */
194       new_sym->next_with_same_name = sym;
195       new_sym->name = sym->name;
196 
197       entry->data = new_sym;
198    } else {
199       new_sym->name = (char *)(new_sym + 1);
200       strcpy(new_sym->name, name);
201 
202       _mesa_hash_table_insert_pre_hashed(table->ht, hash, new_sym->name, new_sym);
203    }
204 
205    new_sym->next_with_same_scope = table->current_scope->symbols;
206    new_sym->data = declaration;
207    new_sym->depth = table->depth;
208 
209    table->current_scope->symbols = new_sym;
210 
211    return 0;
212 }
213 
214 int
_mesa_symbol_table_replace_symbol(struct _mesa_symbol_table * table,const char * name,void * declaration)215 _mesa_symbol_table_replace_symbol(struct _mesa_symbol_table *table,
216                                   const char *name,
217                                   void *declaration)
218 {
219     struct symbol *sym = find_symbol(table, name);
220 
221     /* If the symbol doesn't exist, it cannot be replaced. */
222     if (sym == NULL)
223        return -1;
224 
225     sym->data = declaration;
226     return 0;
227 }
228 
229 struct _mesa_symbol_table *
_mesa_symbol_table_ctor(void)230 _mesa_symbol_table_ctor(void)
231 {
232     struct _mesa_symbol_table *table = calloc(1, sizeof(*table));
233 
234     if (table != NULL) {
235        table->ht = _mesa_hash_table_create(NULL, _mesa_hash_string,
236                                            _mesa_key_string_equal);
237 
238        _mesa_symbol_table_push_scope(table);
239     }
240 
241     return table;
242 }
243 
244 
245 void
_mesa_symbol_table_dtor(struct _mesa_symbol_table * table)246 _mesa_symbol_table_dtor(struct _mesa_symbol_table *table)
247 {
248    /* Free all the scopes and symbols left in the table.  This is like repeated
249     * _mesa_symbol_table_pop_scope(), but not maintining the hash table as we
250     * blow it all away.
251     */
252    while (table->current_scope) {
253       struct scope_level *scope = table->current_scope;
254       table->current_scope = scope->next;
255 
256       while (scope->symbols) {
257          struct symbol *sym = scope->symbols;
258          scope->symbols = sym->next_with_same_scope;
259          free(sym);
260       }
261       free(scope);
262    }
263 
264    _mesa_hash_table_destroy(table->ht, NULL);
265    free(table);
266 }
267