nasm/rdoff/symtab.c

128 lines
2.9 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: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 {
2005-01-16 06:15:51 +08:00
struct tagSymtabNode *next;
symtabEnt ent;
2002-05-01 04:58:18 +08:00
} symtabNode;
2005-01-16 06:15:51 +08:00
typedef symtabNode *(symtabTab[SYMTABSIZE]);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
typedef symtabTab *symtab;
2002-05-01 04:51:32 +08:00
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
2005-01-16 06:15:51 +08:00
void *symtabNew(void)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
symtab mytab;
2002-05-01 04:58:18 +08:00
2005-01-16 06:15:51 +08:00
mytab = (symtabTab *) calloc(SYMTABSIZE, sizeof(symtabNode *));
if (mytab == NULL) {
fprintf(stderr, "symtab: out of memory\n");
exit(3);
}
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
return mytab;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
2005-01-16 06:15:51 +08:00
void symtabDone(void *stab)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
symtab mytab = (symtab) stab;
int i;
symtabNode *this, *next;
2002-05-01 04:58:18 +08:00
2005-01-16 06:15:51 +08:00
for (i = 0; i < SYMTABSIZE; ++i) {
2002-05-01 04:58:18 +08:00
2005-01-16 06:15:51 +08:00
for (this = (*mytab)[i]; this; this = next) {
next = this->next;
free(this);
}
2002-05-01 04:58:18 +08:00
2005-01-16 06:15:51 +08:00
}
free(*mytab);
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:58:18 +08:00
/* ------------------------------------- */
2005-01-16 06:15:51 +08:00
void symtabInsert(void *stab, symtabEnt * ent)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
symtab mytab = (symtab) stab;
symtabNode *node;
int slot;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
node = malloc(sizeof(symtabNode));
if (node == NULL) {
fprintf(stderr, "symtab: out of memory\n");
exit(3);
}
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
slot = slotnum(ent->name);
2002-05-01 04:58:18 +08:00
2005-01-16 06:15:51 +08:00
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
{
2005-01-16 06:15:51 +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
/* ------------------------------------- */
2005-01-16 06:15:51 +08:00
void symtabDump(void *stab, FILE * of)
2002-05-01 04:51:32 +08:00
{
2005-01-16 06:15:51 +08:00
symtab mytab = (symtab) stab;
int i;
char *SegNames[3] = { "code", "data", "bss" };
2005-01-16 06:15:51 +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 {
2007-04-14 09:24:14 +08:00
fprintf(of, "%-32s %s:%08"PRIx32" (%"PRId32")\n", l->ent.name,
2005-01-16 06:15:51 +08:00
SegNames[l->ent.segment],
l->ent.offset, l->ent.flags);
}
l = l->next;
}
}
fprintf(of, "........... end of Symbol table.\n");
2002-05-01 04:51:32 +08:00
}