2 * Symbol table handling
4 * Copyright (C) 2001-2007 Michael Urman, Peter Johnson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
32 #include "libyasm-stdint.h"
50 SYM_UNKNOWN, /* for unknown type (COMMON/EXTERN) */
51 SYM_EQU, /* for EQU defined symbols (expressions) */
52 SYM_LABEL, /* for labels */
53 SYM_CURPOS, /* for labels representing the current
55 SYM_SPECIAL /* for special symbols that need to be in
56 the symbol table but otherwise have no
63 yasm_sym_status status;
64 yasm_sym_vis visibility;
65 unsigned long def_line; /* line where symbol was first defined */
66 unsigned long decl_line; /* line where symbol was first declared */
67 unsigned long use_line; /* line where symbol was first used */
69 yasm_expr *expn; /* equ value */
71 /* bytecode immediately preceding a label */
72 /*@dependent@*/ yasm_bytecode *precbc;
74 unsigned int size; /* 0 if not user-defined */
75 const char *segment; /* for segmented systems like DOS */
77 /* associated data; NULL if none */
78 /*@null@*/ /*@only@*/ yasm__assoc_data *assoc_data;
81 /* Linked list of symbols not in the symbol table. */
82 typedef struct non_table_symrec_s {
83 /*@reldef@*/ SLIST_ENTRY(non_table_symrec_s) link;
84 /*@owned@*/ yasm_symrec *rec;
88 /* The symbol table: a hash array mapped trie (HAMT). */
89 /*@only@*/ HAMT *sym_table;
90 /* Symbols not in the table */
91 SLIST_HEAD(nontablesymhead_s, non_table_symrec_s) non_table_syms;
97 objext_valparams_destroy(void *data)
99 yasm_vps_destroy((yasm_valparamhead *)data);
103 objext_valparams_print(void *data, FILE *f, int indent_level)
105 yasm_vps_print((yasm_valparamhead *)data, f);
108 static yasm_assoc_data_callback objext_valparams_cb = {
109 objext_valparams_destroy,
110 objext_valparams_print
114 common_size_destroy(void *data)
116 yasm_expr **e = (yasm_expr **)data;
117 yasm_expr_destroy(*e);
122 common_size_print(void *data, FILE *f, int indent_level)
124 yasm_expr **e = (yasm_expr **)data;
125 yasm_expr_print(*e, f);
128 static yasm_assoc_data_callback common_size_cb = {
134 yasm_symtab_create(void)
136 yasm_symtab *symtab = yasm_xmalloc(sizeof(yasm_symtab));
137 symtab->sym_table = HAMT_create(0, yasm_internal_error_);
138 SLIST_INIT(&symtab->non_table_syms);
139 symtab->case_sensitive = 1;
144 yasm_symtab_set_case_sensitive(yasm_symtab *symtab, int sensitive)
146 symtab->case_sensitive = sensitive;
150 symrec_destroy_one(/*@only@*/ void *d)
152 yasm_symrec *sym = d;
153 yasm_xfree(sym->name);
154 if (sym->type == SYM_EQU && (sym->status & YASM_SYM_VALUED))
155 yasm_expr_destroy(sym->value.expn);
156 yasm__assoc_data_destroy(sym->assoc_data);
160 static /*@partial@*/ yasm_symrec *
161 symrec_new_common(/*@keep@*/ char *name, int case_sensitive)
163 yasm_symrec *rec = yasm_xmalloc(sizeof(yasm_symrec));
165 if (!case_sensitive) {
167 for (c=name; *c; c++)
172 rec->type = SYM_UNKNOWN;
176 rec->visibility = YASM_SYM_LOCAL;
179 rec->assoc_data = NULL;
183 static /*@partial@*/ /*@dependent@*/ yasm_symrec *
184 symtab_get_or_new_in_table(yasm_symtab *symtab, /*@only@*/ char *name)
186 yasm_symrec *rec = symrec_new_common(name, symtab->case_sensitive);
189 rec->status = YASM_SYM_NOSTATUS;
191 if (!symtab->case_sensitive) {
193 for (c=name; *c; c++)
197 return HAMT_insert(symtab->sym_table, name, rec, &replace,
201 static /*@partial@*/ /*@dependent@*/ yasm_symrec *
202 symtab_get_or_new_not_in_table(yasm_symtab *symtab, /*@only@*/ char *name)
204 non_table_symrec *sym = yasm_xmalloc(sizeof(non_table_symrec));
205 sym->rec = symrec_new_common(name, symtab->case_sensitive);
207 sym->rec->status = YASM_SYM_NOTINTABLE;
209 SLIST_INSERT_HEAD(&symtab->non_table_syms, sym, link);
214 /* create a new symrec */
215 /*@-freshtrans -mustfree@*/
216 static /*@partial@*/ /*@dependent@*/ yasm_symrec *
217 symtab_get_or_new(yasm_symtab *symtab, const char *name, int in_table)
219 char *symname = yasm__xstrdup(name);
222 return symtab_get_or_new_in_table(symtab, symname);
224 return symtab_get_or_new_not_in_table(symtab, symname);
226 /*@=freshtrans =mustfree@*/
229 yasm_symtab_traverse(yasm_symtab *symtab, void *d,
230 int (*func) (yasm_symrec *sym, void *d))
232 return HAMT_traverse(symtab->sym_table, d, (int (*) (void *, void *))func);
235 const yasm_symtab_iter *
236 yasm_symtab_first(const yasm_symtab *symtab)
238 return (const yasm_symtab_iter *)HAMT_first(symtab->sym_table);
241 /*@null@*/ const yasm_symtab_iter *
242 yasm_symtab_next(const yasm_symtab_iter *prev)
244 return (const yasm_symtab_iter *)HAMT_next((const HAMTEntry *)prev);
248 yasm_symtab_iter_value(const yasm_symtab_iter *cur)
250 return (yasm_symrec *)HAMTEntry_get_data((const HAMTEntry *)cur);
254 yasm_symtab_abs_sym(yasm_symtab *symtab)
256 yasm_symrec *rec = symtab_get_or_new(symtab, "", 1);
262 yasm_expr_create_ident(yasm_expr_int(yasm_intnum_create_uint(0)), 0);
263 rec->status |= YASM_SYM_DEFINED|YASM_SYM_VALUED|YASM_SYM_USED;
268 yasm_symtab_use(yasm_symtab *symtab, const char *name, unsigned long line)
270 yasm_symrec *rec = symtab_get_or_new(symtab, name, 1);
271 if (rec->use_line == 0)
272 rec->use_line = line; /* set line number of first use */
273 rec->status |= YASM_SYM_USED;
278 yasm_symtab_get(yasm_symtab *symtab, const char *name)
280 if (!symtab->case_sensitive) {
281 char *_name = yasm__xstrdup(name);
284 for (c=_name; *c; c++)
286 ret = HAMT_search(symtab->sym_table, _name);
290 return HAMT_search(symtab->sym_table, name);
293 static /*@dependent@*/ yasm_symrec *
294 symtab_define(yasm_symtab *symtab, const char *name, sym_type type,
295 int in_table, unsigned long line)
297 yasm_symrec *rec = symtab_get_or_new(symtab, name, in_table);
299 /* Has it been defined before (either by DEFINED or COMMON/EXTERN)? */
300 if (rec->status & YASM_SYM_DEFINED) {
301 yasm_error_set_xref(rec->def_line!=0 ? rec->def_line : rec->decl_line,
302 N_("`%s' previously defined here"), name);
303 yasm_error_set(YASM_ERROR_GENERAL, N_("redefinition of `%s'"),
306 if (rec->visibility & YASM_SYM_EXTERN)
307 yasm_warn_set(YASM_WARN_GENERAL,
308 N_("`%s' both defined and declared extern"), name);
309 rec->def_line = line; /* set line number of definition */
311 rec->status |= YASM_SYM_DEFINED;
319 yasm_symtab_define_equ(yasm_symtab *symtab, const char *name, yasm_expr *e,
322 yasm_symrec *rec = symtab_define(symtab, name, SYM_EQU, 1, line);
323 if (yasm_error_occurred())
326 rec->status |= YASM_SYM_VALUED;
331 yasm_symtab_define_label(yasm_symtab *symtab, const char *name,
332 yasm_bytecode *precbc, int in_table,
335 yasm_symrec *rec = symtab_define(symtab, name, SYM_LABEL, in_table, line);
336 if (yasm_error_occurred())
338 rec->value.precbc = precbc;
339 if (in_table && precbc)
340 yasm_bc__add_symrec(precbc, rec);
345 yasm_symtab_define_curpos(yasm_symtab *symtab, const char *name,
346 yasm_bytecode *precbc, unsigned long line)
348 yasm_symrec *rec = symtab_define(symtab, name, SYM_CURPOS, 0, line);
349 if (yasm_error_occurred())
351 rec->value.precbc = precbc;
356 yasm_symtab_define_special(yasm_symtab *symtab, const char *name,
359 yasm_symrec *rec = symtab_define(symtab, name, SYM_SPECIAL, 1, 0);
360 if (yasm_error_occurred())
362 rec->status |= YASM_SYM_VALUED;
363 rec->visibility = vis;
368 yasm_symtab_declare(yasm_symtab *symtab, const char *name, yasm_sym_vis vis,
371 yasm_symrec *rec = symtab_get_or_new(symtab, name, 1);
372 yasm_symrec_declare(rec, vis, line);
377 yasm_symrec_declare(yasm_symrec *rec, yasm_sym_vis vis, unsigned long line)
379 /* Allowable combinations:
380 * Existing State-------------- vis New State-------------------
381 * DEFINED GLOBAL COMMON EXTERN GCE DEFINED GLOBAL COMMON EXTERN
382 * 0 - 0 0 GCE 0 G C E
390 if ((vis == YASM_SYM_GLOBAL) ||
391 (!(rec->status & YASM_SYM_DEFINED) &&
392 (!(rec->visibility & (YASM_SYM_COMMON | YASM_SYM_EXTERN)) ||
393 ((rec->visibility & YASM_SYM_COMMON) && (vis == YASM_SYM_COMMON)) ||
394 ((rec->visibility & YASM_SYM_EXTERN) && (vis == YASM_SYM_EXTERN))))) {
395 rec->decl_line = line;
396 rec->visibility |= vis;
398 yasm_error_set(YASM_ERROR_GENERAL,
399 N_("duplicate definition of `%s'; first defined on line %lu"),
400 rec->name, rec->def_line!=0 ? rec->def_line : rec->decl_line);
403 typedef struct symtab_finalize_info {
404 unsigned long firstundef_line;
406 yasm_errwarns *errwarns;
407 } symtab_finalize_info;
410 symtab_parser_finalize_checksym(yasm_symrec *sym, /*@null@*/ void *d)
412 symtab_finalize_info *info = (symtab_finalize_info *)d;
414 /* error if a symbol is used but never defined or extern/common declared */
415 if ((sym->status & YASM_SYM_USED) && !(sym->status & YASM_SYM_DEFINED) &&
416 !(sym->visibility & (YASM_SYM_EXTERN | YASM_SYM_COMMON))) {
417 if (info->undef_extern)
418 sym->visibility |= YASM_SYM_EXTERN;
420 yasm_error_set(YASM_ERROR_GENERAL,
421 N_("undefined symbol `%s' (first use)"), sym->name);
422 yasm_errwarn_propagate(info->errwarns, sym->use_line);
423 if (sym->use_line < info->firstundef_line)
424 info->firstundef_line = sym->use_line;
432 yasm_symtab_parser_finalize(yasm_symtab *symtab, int undef_extern,
433 yasm_errwarns *errwarns)
435 symtab_finalize_info info;
436 info.firstundef_line = ULONG_MAX;
437 info.undef_extern = undef_extern;
438 info.errwarns = errwarns;
439 yasm_symtab_traverse(symtab, &info, symtab_parser_finalize_checksym);
440 if (info.firstundef_line < ULONG_MAX) {
441 yasm_error_set(YASM_ERROR_GENERAL,
442 N_(" (Each undefined symbol is reported only once.)"));
443 yasm_errwarn_propagate(errwarns, info.firstundef_line);
448 yasm_symtab_destroy(yasm_symtab *symtab)
450 HAMT_destroy(symtab->sym_table, symrec_destroy_one);
452 while (!SLIST_EMPTY(&symtab->non_table_syms)) {
453 non_table_symrec *sym = SLIST_FIRST(&symtab->non_table_syms);
454 SLIST_REMOVE_HEAD(&symtab->non_table_syms, link);
455 symrec_destroy_one(sym->rec);
462 typedef struct symrec_print_data {
469 symrec_print_wrapper(yasm_symrec *sym, /*@null@*/ void *d)
471 symrec_print_data *data = (symrec_print_data *)d;
472 assert(data != NULL);
473 fprintf(data->f, "%*sSymbol `%s'\n", data->indent_level, "", sym->name);
474 yasm_symrec_print(sym, data->f, data->indent_level+1);
479 yasm_symtab_print(yasm_symtab *symtab, FILE *f, int indent_level)
481 symrec_print_data data;
483 data.indent_level = indent_level;
484 yasm_symtab_traverse(symtab, &data, symrec_print_wrapper);
489 yasm_symrec_get_name(const yasm_symrec *sym)
495 yasm_symrec_get_global_name(const yasm_symrec *sym, const yasm_object *object)
497 if (sym->visibility & (YASM_SYM_GLOBAL|YASM_SYM_COMMON|YASM_SYM_EXTERN)) {
498 char *name = yasm_xmalloc(strlen(object->global_prefix) +
500 strlen(object->global_suffix) + 1);
501 strcpy(name, object->global_prefix);
502 strcat(name, sym->name);
503 strcat(name, object->global_suffix);
506 return yasm__xstrdup(sym->name);
510 yasm_symrec_get_visibility(const yasm_symrec *sym)
512 return sym->visibility;
516 yasm_symrec_get_status(const yasm_symrec *sym)
522 yasm_symrec_get_def_line(const yasm_symrec *sym)
524 return sym->def_line;
528 yasm_symrec_get_decl_line(const yasm_symrec *sym)
530 return sym->decl_line;
534 yasm_symrec_get_use_line(const yasm_symrec *sym)
536 return sym->use_line;
540 yasm_symrec_get_equ(const yasm_symrec *sym)
542 if (sym->type == SYM_EQU && (sym->status & YASM_SYM_VALUED))
543 return sym->value.expn;
544 return (const yasm_expr *)NULL;
548 yasm_symrec_get_label(const yasm_symrec *sym,
549 yasm_symrec_get_label_bytecodep *precbc)
551 if (!(sym->type == SYM_LABEL || sym->type == SYM_CURPOS)
552 || !sym->value.precbc) {
553 *precbc = (yasm_symrec_get_label_bytecodep)0xDEADBEEF;
556 *precbc = sym->value.precbc;
561 yasm_symrec_set_size(yasm_symrec *sym, int size)
567 yasm_symrec_get_size(const yasm_symrec *sym)
573 yasm_symrec_set_segment(yasm_symrec *sym, const char *segment)
575 sym->segment = segment;
579 yasm_symrec_get_segment(const yasm_symrec *sym)
585 yasm_symrec_is_abs(const yasm_symrec *sym)
587 return (sym->def_line == 0 && sym->type == SYM_EQU &&
588 sym->name[0] == '\0');
592 yasm_symrec_is_special(const yasm_symrec *sym)
594 return (sym->type == SYM_SPECIAL);
598 yasm_symrec_is_curpos(const yasm_symrec *sym)
600 return (sym->type == SYM_CURPOS);
604 yasm_symrec_set_objext_valparams(yasm_symrec *sym,
605 /*@only@*/ yasm_valparamhead *objext_valparams)
607 yasm_symrec_add_data(sym, &objext_valparams_cb, objext_valparams);
611 yasm_symrec_get_objext_valparams(yasm_symrec *sym)
613 return yasm_symrec_get_data(sym, &objext_valparams_cb);
617 yasm_symrec_set_common_size(yasm_symrec *sym,
618 /*@only@*/ yasm_expr *common_size)
620 yasm_expr **ep = yasm_xmalloc(sizeof(yasm_expr *));
622 yasm_symrec_add_data(sym, &common_size_cb, ep);
626 yasm_symrec_get_common_size(yasm_symrec *sym)
628 return (yasm_expr **)yasm_symrec_get_data(sym, &common_size_cb);
632 yasm_symrec_get_data(yasm_symrec *sym,
633 const yasm_assoc_data_callback *callback)
635 return yasm__assoc_data_get(sym->assoc_data, callback);
639 yasm_symrec_add_data(yasm_symrec *sym,
640 const yasm_assoc_data_callback *callback, void *data)
642 sym->assoc_data = yasm__assoc_data_add(sym->assoc_data, callback, data);
646 yasm_symrec_print(const yasm_symrec *sym, FILE *f, int indent_level)
650 fprintf(f, "%*s-Unknown (Common/Extern)-\n", indent_level, "");
653 fprintf(f, "%*s_EQU_\n", indent_level, "");
654 fprintf(f, "%*sExpn=", indent_level, "");
655 if (sym->status & YASM_SYM_VALUED)
656 yasm_expr_print(sym->value.expn, f);
658 fprintf(f, "***UNVALUED***");
663 fprintf(f, "%*s_%s_\n%*sSection:\n", indent_level, "",
664 sym->type == SYM_LABEL ? "Label" : "CurPos",
666 yasm_section_print(yasm_bc_get_section(sym->value.precbc), f,
668 fprintf(f, "%*sPreceding bytecode:\n", indent_level, "");
669 yasm_bc_print(sym->value.precbc, f, indent_level+1);
672 fprintf(f, "%*s-Special-\n", indent_level, "");
676 fprintf(f, "%*sStatus=", indent_level, "");
677 if (sym->status == YASM_SYM_NOSTATUS)
678 fprintf(f, "None\n");
680 if (sym->status & YASM_SYM_USED)
682 if (sym->status & YASM_SYM_DEFINED)
683 fprintf(f, "Defined,");
684 if (sym->status & YASM_SYM_VALUED)
685 fprintf(f, "Valued,");
686 if (sym->status & YASM_SYM_NOTINTABLE)
687 fprintf(f, "Not in Table,");
691 fprintf(f, "%*sVisibility=", indent_level, "");
692 if (sym->visibility == YASM_SYM_LOCAL)
693 fprintf(f, "Local\n");
695 if (sym->visibility & YASM_SYM_GLOBAL)
696 fprintf(f, "Global,");
697 if (sym->visibility & YASM_SYM_COMMON)
698 fprintf(f, "Common,");
699 if (sym->visibility & YASM_SYM_EXTERN)
700 fprintf(f, "Extern,");
704 if (sym->assoc_data) {
705 fprintf(f, "%*sAssociated data:\n", indent_level, "");
706 yasm__assoc_data_print(sym->assoc_data, f, indent_level+1);
709 fprintf(f, "%*sLine Index (Defined)=%lu\n", indent_level, "",
711 fprintf(f, "%*sLine Index (Declared)=%lu\n", indent_level, "",
713 fprintf(f, "%*sLine Index (Used)=%lu\n", indent_level, "", sym->use_line);