/*
 *  smoke16/libObj.c - Functions for SymTabs.
 *  $Revision: 1.9 $
 *  Copyright 1997, Benjamin Sittler
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* #define DEBUG_OBJ */
/* #define DEBUG_SYMS */
/* #define DEBUG_RELOCS */
/* #define DEBUG_SEARCH */

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <smoke16/libObj.h>

SymTab *newSymTab(void) {
  SymTab *tab = (SymTab *)malloc(sizeof(SymTab));
  int bucket;

  if (tab) {
#ifdef DEBUG_SYMS
    fprintf(stderr, "created symbol table %p\n", tab);
#endif
    tab -> syms = 0;
    for (bucket = 0; bucket < HASHSIZE; bucket++)
      tab -> tab[bucket] = (Sym *)0;
  }
  else {
#ifdef DEBUG_SYMS
    fprintf(stderr, "failed to create symbol table\n");
#endif
    perror("malloc");
  }
  return tab;
}

void freeSymTab(SymTab *tab)
{
  int bucket;
  Sym *sym;

#ifdef DEBUG_SYMS
  fprintf(stderr, "deleting symbol table %p\n", tab);
#endif
  for (bucket = 0; bucket < HASHSIZE; bucket++)
    for (sym = tab -> tab[bucket]; sym; sym = tab -> tab[bucket]) {
      tab -> tab[bucket] = sym -> next;
      freeSym(sym);
    }
  free((void *)tab);
}

Sym *lookinSymTab(SymTab *tab, char *string)
{
  Sym *sym;

#ifdef DEBUG_SYMS
  fprintf(stderr, "searching for '%s' in symbol table %p\n", string, tab);
#endif
  for (sym = tab -> tab[hashpjw(string)]; sym; sym = sym -> next)
    if (strcmp(string, sym -> string) == 0) {
#ifdef DEBUG_SYMS
      fprintf(stderr, "found '%s' in %p\n", string, tab);
#endif
      return sym;
    }
#ifdef DEBUG_SYMS
  fprintf(stderr, "did not find '%s' in %p\n", string, tab);
#endif
  return 0;
}

void addtoSymTab(SymTab *tab, Sym *sym) {
  Sym **s = tab -> tab + hashpjw(sym -> string);

#ifdef DEBUG_SYMS
  fprintf(stderr, "adding '%s' to symbol table %p\n", sym -> string, tab);
#endif
  sym -> next = (Sym *)0;
  while (*s)
    s = &((*s) -> next);
  *s = sym;
  tab -> syms++;
}

Sym *enterinSymTab(SymTab *tab, Sym *sym) {
  Sym *found;
  
  if ((found = lookinSymTab(tab, sym -> string))) {
    if (defSym(found, sym -> type, sym -> other, sym -> desc, sym -> value, sym -> refcnt))
      found = 0;
    else
      freeSym(sym);
  } else
    addtoSymTab(tab, (found = sym));
  return found;
}

int removefromSymTab(SymTab *tab, Sym *sym) {
  Sym * found, ** prev;

  prev = &(tab -> tab[hashpjw(sym -> string)]);
  for (found = *prev;
      found;
      prev = &(found -> next), found = *prev)
    if (found == sym) {
#ifdef DEBUG_SYMS
      fprintf(stderr, "deleting '%s' from %p\n", sym -> string, tab);
#endif
      *prev = found -> next;
      tab -> syms--;
      freeSym(sym);
      return 0;
    }
#ifdef DEBUG_SYMS
  fprintf(stderr, "did not find '%s' in %p\n", sym -> string, tab);
#endif
  return 1;
}
