Reworked test files for better error reporting
[nasm/perl-rewrite.git] / rdoff / symtab.c
blobf6b1fe5d0a32445f50e945c66f3958c31a86ec81
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 license given in the file "LICENSE"
8 * distributed in the NASM archive.
9 */
11 #include "compiler.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include "symtab.h"
18 #include "hash.h"
20 #define SYMTABSIZE 64
21 #define slotnum(x) (hash((x)) % SYMTABSIZE)
23 /* ------------------------------------- */
24 /* Private data types */
26 typedef struct tagSymtabNode {
27 struct tagSymtabNode *next;
28 symtabEnt ent;
29 } symtabNode;
31 typedef symtabNode *(symtabTab[SYMTABSIZE]);
33 typedef symtabTab *symtab;
35 /* ------------------------------------- */
36 void *symtabNew(void)
38 symtab mytab;
40 mytab = (symtabTab *) calloc(SYMTABSIZE, sizeof(symtabNode *));
41 if (mytab == NULL) {
42 fprintf(stderr, "symtab: out of memory\n");
43 exit(3);
46 return mytab;
49 /* ------------------------------------- */
50 void symtabDone(void *stab)
52 symtab mytab = (symtab) stab;
53 int i;
54 symtabNode *this, *next;
56 for (i = 0; i < SYMTABSIZE; ++i) {
58 for (this = (*mytab)[i]; this; this = next) {
59 next = this->next;
60 free(this);
64 free(*mytab);
67 /* ------------------------------------- */
68 void symtabInsert(void *stab, symtabEnt * ent)
70 symtab mytab = (symtab) stab;
71 symtabNode *node;
72 int slot;
74 node = malloc(sizeof(symtabNode));
75 if (node == NULL) {
76 fprintf(stderr, "symtab: out of memory\n");
77 exit(3);
80 slot = slotnum(ent->name);
82 node->ent = *ent;
83 node->next = (*mytab)[slot];
84 (*mytab)[slot] = node;
87 /* ------------------------------------- */
88 symtabEnt *symtabFind(void *stab, const char *name)
90 symtab mytab = (symtab) stab;
91 int slot = slotnum(name);
92 symtabNode *node = (*mytab)[slot];
94 while (node) {
95 if (!strcmp(node->ent.name, name)) {
96 return &(node->ent);
98 node = node->next;
101 return NULL;
104 /* ------------------------------------- */
105 void symtabDump(void *stab, FILE * of)
107 symtab mytab = (symtab) stab;
108 int i;
109 char *SegNames[3] = { "code", "data", "bss" };
111 fprintf(of, "Symbol table is ...\n");
112 for (i = 0; i < SYMTABSIZE; ++i) {
113 symtabNode *l = (symtabNode *) (*mytab)[i];
115 if (l) {
116 fprintf(of, " ... slot %d ...\n", i);
118 while (l) {
119 if ((l->ent.segment) == -1) {
120 fprintf(of, "%-32s Unresolved reference\n", l->ent.name);
121 } else {
122 fprintf(of, "%-32s %s:%08"PRIx32" (%"PRId32")\n", l->ent.name,
123 SegNames[l->ent.segment],
124 l->ent.offset, l->ent.flags);
126 l = l->next;
129 fprintf(of, "........... end of Symbol table.\n");