IR print visitor: print ir_dereference instructions
[mesa.git] / symbol_table.c
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 #include "main/imports.h"
25 #include "symbol_table.h"
26 #include "hash_table.h"
27
28 struct symbol {
29 /**
30 * Link to the next symbol in the table with the same name
31 *
32 * The linked list of symbols with the same name is ordered by scope
33 * from inner-most to outer-most.
34 */
35 struct symbol *next_with_same_name;
36
37
38 /**
39 * Link to the next symbol in the table with the same scope
40 *
41 * The linked list of symbols with the same scope is unordered. Symbols
42 * in this list my have unique names.
43 */
44 struct symbol *next_with_same_scope;
45
46
47 /**
48 * Header information for the list of symbols with the same name.
49 */
50 struct symbol_header *hdr;
51
52
53 /**
54 * Name space of the symbol
55 *
56 * Name space are arbitrary user assigned integers. No two symbols can
57 * exist in the same name space at the same scope level.
58 */
59 int name_space;
60
61
62 /**
63 * Arbitrary user supplied data.
64 */
65 void *data;
66
67 /** Scope depth where this symbol was defined. */
68 unsigned depth;
69 };
70
71
72 /**
73 */
74 struct symbol_header {
75 /** Linkage in list of all headers in a given symbol table. */
76 struct symbol_header *next;
77
78 /** Symbol name. */
79 const char *name;
80
81 /** Linked list of symbols with the same name. */
82 struct symbol *symbols;
83 };
84
85
86 /**
87 * Element of the scope stack.
88 */
89 struct scope_level {
90 /** Link to next (inner) scope level. */
91 struct scope_level *next;
92
93 /** Linked list of symbols with the same scope. */
94 struct symbol *symbols;
95 };
96
97
98 /**
99 *
100 */
101 struct _mesa_symbol_table {
102 /** Hash table containing all symbols in the symbol table. */
103 struct hash_table *ht;
104
105 /** Top of scope stack. */
106 struct scope_level *current_scope;
107
108 /** List of all symbol headers in the table. */
109 struct symbol_header *hdr;
110
111 /** Current scope depth. */
112 unsigned depth;
113 };
114
115
116 struct _mesa_symbol_table_iterator {
117 /**
118 * Name space of symbols returned by this iterator.
119 */
120 int name_space;
121
122
123 /**
124 * Currently iterated symbol
125 *
126 * The next call to \c _mesa_symbol_table_iterator_get will return this
127 * value. It will also update this value to the value that should be
128 * returned by the next call.
129 */
130 struct symbol *curr;
131 };
132
133
134 static void
135 check_symbol_table(struct _mesa_symbol_table *table)
136 {
137 #if 1
138 struct scope_level *scope;
139
140 for (scope = table->current_scope; scope != NULL; scope = scope->next) {
141 struct symbol *sym;
142
143 for (sym = scope->symbols
144 ; sym != NULL
145 ; sym = sym->next_with_same_name) {
146 const struct symbol_header *const hdr = sym->hdr;
147 struct symbol *sym2;
148
149 for (sym2 = hdr->symbols
150 ; sym2 != NULL
151 ; sym2 = sym2->next_with_same_name) {
152 assert(sym2->hdr == hdr);
153 }
154 }
155 }
156 #endif
157 }
158
159 void
160 _mesa_symbol_table_pop_scope(struct _mesa_symbol_table *table)
161 {
162 struct scope_level *const scope = table->current_scope;
163 struct symbol *sym = scope->symbols;
164
165 table->current_scope = scope->next;
166 table->depth--;
167
168 free(scope);
169
170 while (sym != NULL) {
171 struct symbol *const next = sym->next_with_same_scope;
172 struct symbol_header *const hdr = sym->hdr;
173
174 assert(hdr->symbols == sym);
175
176 hdr->symbols = sym->next_with_same_name;
177
178 free(sym);
179
180 sym = next;
181 }
182
183 check_symbol_table(table);
184 }
185
186
187 void
188 _mesa_symbol_table_push_scope(struct _mesa_symbol_table *table)
189 {
190 struct scope_level *const scope = calloc(1, sizeof(*scope));
191
192 scope->next = table->current_scope;
193 table->current_scope = scope;
194 table->depth++;
195 }
196
197
198 static struct symbol_header *
199 find_symbol(struct _mesa_symbol_table *table, const char *name)
200 {
201 return (struct symbol_header *) hash_table_find(table->ht, name);
202 }
203
204
205 struct _mesa_symbol_table_iterator *
206 _mesa_symbol_table_iterator_ctor(struct _mesa_symbol_table *table,
207 int name_space, const char *name)
208 {
209 struct _mesa_symbol_table_iterator *iter = calloc(1, sizeof(*iter));
210 struct symbol_header *const hdr = find_symbol(table, name);
211
212 iter->name_space = name_space;
213
214 if (hdr != NULL) {
215 struct symbol *sym;
216
217 for (sym = hdr->symbols; sym != NULL; sym = sym->next_with_same_name) {
218 assert(sym->hdr == hdr);
219
220 if ((name_space == -1) || (sym->name_space == name_space)) {
221 iter->curr = sym;
222 break;
223 }
224 }
225 }
226
227 return iter;
228 }
229
230
231 void
232 _mesa_symbol_table_iterator_dtor(struct _mesa_symbol_table_iterator *iter)
233 {
234 free(iter);
235 }
236
237
238 void *
239 _mesa_symbol_table_iterator_get(struct _mesa_symbol_table_iterator *iter)
240 {
241 return (iter->curr == NULL) ? NULL : iter->curr->data;
242 }
243
244
245 int
246 _mesa_symbol_table_iterator_next(struct _mesa_symbol_table_iterator *iter)
247 {
248 struct symbol_header *hdr;
249
250 if (iter->curr == NULL) {
251 return 0;
252 }
253
254 hdr = iter->curr->hdr;
255 iter->curr = iter->curr->next_with_same_name;
256
257 while (iter->curr != NULL) {
258 assert(iter->curr->hdr == hdr);
259
260 if ((iter->name_space == -1)
261 || (iter->curr->name_space == iter->name_space)) {
262 return 1;
263 }
264
265 iter->curr = iter->curr->next_with_same_name;
266 }
267
268 return 0;
269 }
270
271
272 /**
273 * Determine the scope "distance" of a symbol from the current scope
274 *
275 * \return
276 * A non-negative number for the number of scopes between the current scope
277 * and the scope where a symbol was defined. A value of zero means the current
278 * scope. A negative number if the symbol does not exist.
279 */
280 int
281 _mesa_symbol_table_symbol_scope(struct _mesa_symbol_table *table,
282 int name_space, const char *name)
283 {
284 struct symbol_header *const hdr = find_symbol(table, name);
285 struct symbol *sym;
286
287 if (hdr != NULL) {
288 for (sym = hdr->symbols; sym != NULL; sym = sym->next_with_same_name) {
289 assert(sym->hdr == hdr);
290
291 if ((name_space == -1) || (sym->name_space == name_space)) {
292 assert(sym->depth <= table->depth);
293 return sym->depth - table->depth;
294 }
295 }
296 }
297
298 return -1;
299 }
300
301
302 void *
303 _mesa_symbol_table_find_symbol(struct _mesa_symbol_table *table,
304 int name_space, const char *name)
305 {
306 struct symbol_header *const hdr = find_symbol(table, name);
307
308 if (hdr != NULL) {
309 struct symbol *sym;
310
311
312 for (sym = hdr->symbols; sym != NULL; sym = sym->next_with_same_name) {
313 assert(sym->hdr == hdr);
314
315 if ((name_space == -1) || (sym->name_space == name_space)) {
316 return sym->data;
317 }
318 }
319 }
320
321 return NULL;
322 }
323
324
325 int
326 _mesa_symbol_table_add_symbol(struct _mesa_symbol_table *table,
327 int name_space, const char *name,
328 void *declaration)
329 {
330 struct symbol_header *hdr;
331 struct symbol *sym;
332
333 check_symbol_table(table);
334
335 hdr = find_symbol(table, name);
336
337 check_symbol_table(table);
338
339 if (hdr == NULL) {
340 hdr = calloc(1, sizeof(*hdr));
341 hdr->name = name;
342
343 hash_table_insert(table->ht, hdr, name);
344 hdr->next = table->hdr;
345 table->hdr = hdr;
346 }
347
348 check_symbol_table(table);
349
350 /* If the symbol already exists in this namespace at this scope, it cannot
351 * be added to the table.
352 */
353 for (sym = hdr->symbols
354 ; (sym != NULL) && (sym->name_space != name_space)
355 ; sym = sym->next_with_same_name) {
356 /* empty */
357 }
358
359 if (sym && (sym->depth == table->depth))
360 return -1;
361
362 sym = calloc(1, sizeof(*sym));
363 sym->next_with_same_name = hdr->symbols;
364 sym->next_with_same_scope = table->current_scope->symbols;
365 sym->hdr = hdr;
366 sym->name_space = name_space;
367 sym->data = declaration;
368 sym->depth = table->depth;
369
370 assert(sym->hdr == hdr);
371
372 hdr->symbols = sym;
373 table->current_scope->symbols = sym;
374
375 check_symbol_table(table);
376 return 0;
377 }
378
379
380 struct _mesa_symbol_table *
381 _mesa_symbol_table_ctor(void)
382 {
383 struct _mesa_symbol_table *table = calloc(1, sizeof(*table));
384
385 if (table != NULL) {
386 table->ht = hash_table_ctor(32, hash_table_string_hash,
387 hash_table_string_compare);
388
389 _mesa_symbol_table_push_scope(table);
390 }
391
392 return table;
393 }
394
395
396 void
397 _mesa_symbol_table_dtor(struct _mesa_symbol_table *table)
398 {
399 struct symbol_header *hdr;
400 struct symbol_header *next;
401
402 while (table->current_scope != NULL) {
403 _mesa_symbol_table_pop_scope(table);
404 }
405
406 for (hdr = table->hdr; hdr != NULL; hdr = next) {
407 next = hdr->next;
408 _mesa_free(hdr);
409 }
410
411 hash_table_dtor(table->ht);
412 free(table);
413 }