nasm/rdoff/symtab.c

132 lines
2.7 KiB
C
Raw Normal View History

2002-05-01 04:58:18 +08:00
/* symtab.c Routines to maintain and manipulate a symbol table
*
* These routines donated to the NASM effort by Graeme Defty.
2002-05-01 04:51:32 +08:00
*
* The Netwide Assembler is copyright (C) 1996 Simon Tatham and
* Julian Hall. All rights reserved. The software is
* redistributable under the licence given in the file "Licence"
* distributed in the NASM archive.
*/
#include <stdio.h>
#include <stdlib.h>
2002-05-01 05:00:33 +08:00
#include <string.h>
2002-05-01 04:58:18 +08:00
#include <malloc.h>
2002-05-01 04:51:32 +08:00
#include "symtab.h"
2002-05-01 04:58:18 +08:00
#include "hash.h"
2002-05-01 04:51:32 +08:00
2002-05-01 04:58:18 +08:00
#define SYMTABSIZE 64
#define slotnum(x) (hash((x)) % SYMTABSIZE)
2002-05-01 04:51:32 +08:00
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
2002-05-01 04:51:32 +08:00
/* Private data types */
2002-05-01 04:58:18 +08:00
typedef struct tagSymtabNode {
struct tagSymtabNode * next;
symtabEnt ent;
} symtabNode;
typedef symtabNode *(symtabTab[SYMTABSIZE]);
2002-05-01 04:51:32 +08:00
2002-05-01 04:58:18 +08:00
typedef symtabTab *symtab;
2002-05-01 04:51:32 +08:00
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
void *
symtabNew(void)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:58:18 +08:00
symtab mytab;
mytab = (symtabTab *) calloc(SYMTABSIZE ,sizeof(symtabNode *));
if (mytab == NULL) {
2002-05-01 04:51:32 +08:00
fprintf(stderr,"symtab: out of memory\n");
exit(3);
}
2002-05-01 04:58:18 +08:00
return mytab;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
void
symtabDone(void *stab)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:58:18 +08:00
symtab mytab = (symtab)stab;
int i;
symtabNode *this, *next;
for (i=0; i < SYMTABSIZE; ++i) {
for (this = (*mytab)[i]; this; this=next)
{ next = this->next; free (this); }
}
free (*mytab);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
void
symtabInsert(void *stab, symtabEnt *ent)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:58:18 +08:00
symtab mytab = (symtab) stab;
symtabNode *node;
int slot;
2002-05-01 04:51:32 +08:00
2002-05-01 04:58:18 +08:00
node = malloc(sizeof(symtabNode));
if (node == NULL) {
2002-05-01 04:51:32 +08:00
fprintf(stderr,"symtab: out of memory\n");
exit(3);
}
2002-05-01 04:58:18 +08:00
slot = slotnum(ent->name);
node->ent = *ent;
node->next = (*mytab)[slot];
(*mytab)[slot] = node;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
symtabEnt *
symtabFind(void *stab, const char *name)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:58:18 +08:00
symtab mytab = (symtab) stab;
int slot = slotnum(name);
symtabNode *node = (*mytab)[slot];
while (node) {
if (!strcmp(node->ent.name,name)) {
return &(node->ent);
}
node = node->next;
}
return NULL;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
void
symtabDump(void *stab, FILE* of)
2002-05-01 04:51:32 +08:00
{
2002-05-01 04:58:18 +08:00
symtab mytab = (symtab)stab;
int i;
2002-05-01 05:00:33 +08:00
char *SegNames[3]={"code","data","bss"};
2002-05-01 04:58:18 +08:00
fprintf(of, "Symbol table is ...\n");
for (i=0; i < SYMTABSIZE; ++i) {
symtabNode *l = (symtabNode *)(*mytab)[i];
if (l) {
fprintf(of, " ... slot %d ...\n", i);
}
while(l) {
if ((l->ent.segment) == -1) {
fprintf(of,"%-32s Unresolved reference\n",l->ent.name);
} else {
fprintf(of, "%-32s %s:%08lx (%ld)\n",l->ent.name,
SegNames[l->ent.segment],
l->ent.offset, l->ent.flags);
}
l = l->next;
2002-05-01 04:58:18 +08:00
}
}
fprintf(of, "........... end of Symbol table.\n");
2002-05-01 04:51:32 +08:00
}