Update dependencies
[nasm/avx512.git] / rdoff / symtab.c
blob303796346723d9f3c79e5c5385e0fd1a7eb19506
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 *
34 symtabNew(void)
36 symtab mytab;
38 mytab = (symtabTab *) calloc(SYMTABSIZE ,sizeof(symtabNode *));
39 if (mytab == NULL) {
40 fprintf(stderr,"symtab: out of memory\n");
41 exit(3);
44 return mytab;
47 /* ------------------------------------- */
48 void
49 symtabDone(void *stab)
51 symtab mytab = (symtab)stab;
52 int i;
53 symtabNode *this, *next;
55 for (i=0; i < SYMTABSIZE; ++i) {
57 for (this = (*mytab)[i]; this; this=next)
58 { next = this->next; free (this); }
61 free (*mytab);
64 /* ------------------------------------- */
65 void
66 symtabInsert(void *stab, symtabEnt *ent)
68 symtab mytab = (symtab) stab;
69 symtabNode *node;
70 int slot;
72 node = malloc(sizeof(symtabNode));
73 if (node == NULL) {
74 fprintf(stderr,"symtab: out of memory\n");
75 exit(3);
78 slot = slotnum(ent->name);
80 node->ent = *ent;
81 node->next = (*mytab)[slot];
82 (*mytab)[slot] = node;
85 /* ------------------------------------- */
86 symtabEnt *
87 symtabFind(void *stab, const char *name)
89 symtab mytab = (symtab) stab;
90 int slot = slotnum(name);
91 symtabNode *node = (*mytab)[slot];
93 while (node) {
94 if (!strcmp(node->ent.name,name)) {
95 return &(node->ent);
97 node = node->next;
100 return NULL;
103 /* ------------------------------------- */
104 void
105 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:%08lx (%ld)\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");