tokens.dat: Data file containing alphanumeric tokens not in other .dats
[nasm.git] / rdoff / symtab.c
blob6026ccd8aae9f6fb2d030cfb35d0177fe66bcf6d
1 /* symtab.c Routines to maintain and manipulate a symbol table
3 * These routines donated to the NASM effort by Graeme Defty.
5 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
6 * Julian Hall. All rights reserved. The software is
7 * redistributable under the licence given in the file "Licence"
8 * distributed in the NASM archive.
9 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "symtab.h"
15 #include "hash.h"
17 #define SYMTABSIZE 64
18 #define slotnum(x) (hash((x)) % SYMTABSIZE)
20 /* ------------------------------------- */
21 /* Private data types */
23 typedef struct tagSymtabNode {
24 struct tagSymtabNode *next;
25 symtabEnt ent;
26 } symtabNode;
28 typedef symtabNode *(symtabTab[SYMTABSIZE]);
30 typedef symtabTab *symtab;
32 /* ------------------------------------- */
33 void *symtabNew(void)
35 symtab mytab;
37 mytab = (symtabTab *) calloc(SYMTABSIZE, sizeof(symtabNode *));
38 if (mytab == NULL) {
39 fprintf(stderr, "symtab: out of memory\n");
40 exit(3);
43 return mytab;
46 /* ------------------------------------- */
47 void symtabDone(void *stab)
49 symtab mytab = (symtab) stab;
50 int i;
51 symtabNode *this, *next;
53 for (i = 0; i < SYMTABSIZE; ++i) {
55 for (this = (*mytab)[i]; this; this = next) {
56 next = this->next;
57 free(this);
61 free(*mytab);
64 /* ------------------------------------- */
65 void symtabInsert(void *stab, symtabEnt * ent)
67 symtab mytab = (symtab) stab;
68 symtabNode *node;
69 int slot;
71 node = malloc(sizeof(symtabNode));
72 if (node == NULL) {
73 fprintf(stderr, "symtab: out of memory\n");
74 exit(3);
77 slot = slotnum(ent->name);
79 node->ent = *ent;
80 node->next = (*mytab)[slot];
81 (*mytab)[slot] = node;
84 /* ------------------------------------- */
85 symtabEnt *symtabFind(void *stab, const char *name)
87 symtab mytab = (symtab) stab;
88 int slot = slotnum(name);
89 symtabNode *node = (*mytab)[slot];
91 while (node) {
92 if (!strcmp(node->ent.name, name)) {
93 return &(node->ent);
95 node = node->next;
98 return NULL;
101 /* ------------------------------------- */
102 void symtabDump(void *stab, FILE * of)
104 symtab mytab = (symtab) stab;
105 int i;
106 char *SegNames[3] = { "code", "data", "bss" };
108 fprintf(of, "Symbol table is ...\n");
109 for (i = 0; i < SYMTABSIZE; ++i) {
110 symtabNode *l = (symtabNode *) (*mytab)[i];
112 if (l) {
113 fprintf(of, " ... slot %d ...\n", i);
115 while (l) {
116 if ((l->ent.segment) == -1) {
117 fprintf(of, "%-32s Unresolved reference\n", l->ent.name);
118 } else {
119 fprintf(of, "%-32s %s:%08"PRIx32" (%"PRId32")\n", l->ent.name,
120 SegNames[l->ent.segment],
121 l->ent.offset, l->ent.flags);
123 l = l->next;
126 fprintf(of, "........... end of Symbol table.\n");