NASM 0.98.12
[nasm/avx512.git] / rdoff / symtab.c
blob8570f9812a0860c5d059ca5d4903c5383667d8f0
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>
13 #include <malloc.h>
15 #include "symtab.h"
16 #include "hash.h"
18 #define SYMTABSIZE 64
19 #define slotnum(x) (hash((x)) % SYMTABSIZE)
21 /* ------------------------------------- */
22 /* Private data types */
24 typedef struct tagSymtabNode {
25 struct tagSymtabNode * next;
26 symtabEnt ent;
27 } symtabNode;
29 typedef symtabNode *(symtabTab[SYMTABSIZE]);
31 typedef symtabTab *symtab;
33 /* ------------------------------------- */
34 void *
35 symtabNew(void)
37 symtab mytab;
39 mytab = (symtabTab *) calloc(SYMTABSIZE ,sizeof(symtabNode *));
40 if (mytab == NULL) {
41 fprintf(stderr,"symtab: out of memory\n");
42 exit(3);
45 return mytab;
48 /* ------------------------------------- */
49 void
50 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; free (this); }
62 free (*mytab);
65 /* ------------------------------------- */
66 void
67 symtabInsert(void *stab, symtabEnt *ent)
69 symtab mytab = (symtab) stab;
70 symtabNode *node;
71 int slot;
73 node = malloc(sizeof(symtabNode));
74 if (node == NULL) {
75 fprintf(stderr,"symtab: out of memory\n");
76 exit(3);
79 slot = slotnum(ent->name);
81 node->ent = *ent;
82 node->next = (*mytab)[slot];
83 (*mytab)[slot] = node;
86 /* ------------------------------------- */
87 symtabEnt *
88 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
106 symtabDump(void *stab, FILE* of)
108 symtab mytab = (symtab)stab;
109 int i;
110 char *SegNames[3]={"code","data","bss"};
112 fprintf(of, "Symbol table is ...\n");
113 for (i=0; i < SYMTABSIZE; ++i) {
114 symtabNode *l = (symtabNode *)(*mytab)[i];
116 if (l) {
117 fprintf(of, " ... slot %d ...\n", i);
119 while(l) {
120 fprintf(of, "%-32s %s:%08lx (%ld)\n",l->ent.name,
121 SegNames[l->ent.segment],
122 l->ent.offset, l->ent.flags);
123 l = l->next;
126 fprintf(of, "........... end of Symbol table.\n");