* Eliminate another couple of calls to scm_wta.
[bpt/guile.git] / libguile / symbols.c
index 3943899..818fb59 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1995,1996,1997,1998 Free Software Foundation, Inc.
+/*     Copyright (C) 1995,1996,1997,1998, 2000 Free Software Foundation, Inc.
  * 
  * 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
 \f
 
 #include <stdio.h>
-#include "_scm.h"
-#include "chars.h"
-#include "eval.h"
-#include "variable.h"
-#include "alist.h"
-#include "vectors.h"
-#include "weaks.h"
-
-#include "validate.h"
-#include "symbols.h"
+#include "libguile/_scm.h"
+#include "libguile/chars.h"
+#include "libguile/eval.h"
+#include "libguile/hash.h"
+#include "libguile/smob.h"
+#include "libguile/variable.h"
+#include "libguile/alist.h"
+#include "libguile/fluids.h"
+#include "libguile/strings.h"
+#include "libguile/vectors.h"
+#include "libguile/hashtab.h"
+#include "libguile/weaks.h"
+#include "libguile/modules.h"
+
+#include "libguile/validate.h"
+#include "libguile/symbols.h"
 
 #ifdef HAVE_STRING_H
 #include <string.h>
 
 \f
 
-
-/* NUM_HASH_BUCKETS is the number of symbol scm_hash table buckets. 
- */
-#define NUM_HASH_BUCKETS 137
+static SCM symbols;
 
 \f
 
+static char *
+duplicate_string (const char * src, unsigned long length)
+{
+  char * dst = scm_must_malloc (length + 1, "duplicate_string");
+  memcpy (dst, src, length);
+  dst[length] = 0;
+  return dst;
+}
+
+\f
 
 /* {Symbols}
  */
 
 
-unsigned long 
-scm_strhash (unsigned char *str,scm_sizet len,unsigned long n)
+SCM
+scm_mem2symbol (const char *name, scm_sizet len)
 {
-  if (len > 5)
-    {
-      scm_sizet i = 5;
-      unsigned long h = 264 % n;
-      while (i--)
-       h = ((h << 8) + ((unsigned) (scm_downcase (str[h % len])))) % n;
-      return h;
-    }
-  else
-    {
-      scm_sizet i = len;
-      unsigned long h = 0;
-      while (i)
-       h = ((h << 8) + ((unsigned) (scm_downcase (str[--i])))) % n;
-      return h;
-    }
+  scm_sizet raw_hash = scm_string_hash ((const unsigned char *) name, len);
+  scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (symbols);
+
+  {
+    /* Try to find the symbol in the symbols table */
+
+    SCM l;
+
+    for (l = SCM_VELTS (symbols) [hash]; !SCM_NULLP (l); l = SCM_CDR (l))
+      {
+       SCM sym = SCM_CAAR (l);
+       if (SCM_SYMBOL_HASH (sym) == raw_hash && SCM_SYMBOL_LENGTH (sym) == len)
+         {
+           char *chrs = SCM_SYMBOL_CHARS (sym);
+           scm_sizet i = len;
+
+           while (i != 0)
+             {
+               --i;
+               if (name[i] != chrs[i])
+                 goto next_symbol;
+             }
+
+           return sym;
+         }
+      next_symbol:
+       ;
+      }
+  }
+
+  {
+    /* The symbol was not found - create it. */
+
+    SCM symbol;
+    SCM cell;
+    SCM slot;
+
+    SCM_NEWCELL2 (symbol);
+    SCM_SET_SYMBOL_CHARS (symbol, duplicate_string (name, len));
+    SCM_SET_SYMBOL_HASH (symbol, raw_hash);
+    SCM_SET_PROP_SLOTS (symbol, scm_cons (SCM_BOOL_F, SCM_EOL));
+    SCM_SET_SYMBOL_LENGTH (symbol, (long) len);
+
+    cell = scm_cons (symbol, SCM_UNDEFINED);
+    slot = SCM_VELTS (symbols) [hash];
+    SCM_VELTS (symbols) [hash] = scm_cons (cell, slot);
+
+    return symbol;
+  }
 }
 
-int scm_symhash_dim = NUM_HASH_BUCKETS;
+
+SCM
+scm_str2symbol (const char *str)
+{
+  return scm_mem2symbol (str, strlen (str));
+}
 
 
 /* scm_sym2vcell
@@ -103,62 +154,62 @@ int scm_symhash_dim = NUM_HASH_BUCKETS;
  */
 
 SCM 
-scm_sym2vcell (SCM sym,SCM thunk,SCM definep)
+scm_sym2vcell (SCM sym, SCM thunk, SCM definep)
+#define FUNC_NAME "scm_sym2vcell"
 {
-  if (SCM_NIMP(thunk))
+  if (SCM_NIMP (thunk))
     {
-      SCM var = scm_apply (thunk, sym, scm_cons(definep, scm_listofnull));
+      SCM var;
 
-      if (var == SCM_BOOL_F)
+      if (SCM_EVAL_CLOSURE_P (thunk))
+       /* Bypass evaluator in the standard case. */
+       var = scm_eval_closure_lookup (thunk, sym, definep);
+      else
+       var = scm_apply (thunk, sym, scm_cons (definep, scm_listofnull));
+
+      if (SCM_FALSEP (var))
        return SCM_BOOL_F;
+      else if (SCM_VARIABLEP (var))
+       return SCM_VARVCELL (var);
       else
-       {
-         if (SCM_IMP(var) || !SCM_VARIABLEP (var))
-           scm_wta (sym, "strangely interned symbol? ", "");
-         return SCM_VARVCELL (var);
-       }
+       SCM_MISC_ERROR ("strangely interned symbol: ~S", SCM_LIST1 (sym));
     }
   else
     {
       SCM lsym;
-      SCM * lsymp;
-      SCM z;
-      scm_sizet scm_hash = scm_strhash (SCM_UCHARS (sym), (scm_sizet) SCM_LENGTH (sym),
-                                   (unsigned long) scm_symhash_dim);
+      scm_sizet hash;
 
       SCM_DEFER_INTS;
-      for (lsym = SCM_VELTS (scm_symhash)[scm_hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
+      hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (scm_symhash);
+      for (lsym = SCM_VELTS (scm_symhash)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
        {
-         z = SCM_CAR (lsym);
-         if (SCM_CAR (z) == sym)
+         SCM z = SCM_CAR (lsym);
+         if (SCM_EQ_P (SCM_CAR (z), sym))
            {
              SCM_ALLOW_INTS;
              return z;
            }
        }
 
-      for (lsym = *(lsymp = &SCM_VELTS (scm_weak_symhash)[scm_hash]);
-          SCM_NIMP (lsym);
-          lsym = *(lsymp = SCM_CDRLOC (lsym)))
+      if (!SCM_FALSEP (definep))
        {
-         z = SCM_CAR (lsym);
-         if (SCM_CAR (z) == sym)
-           {
-             if (SCM_NFALSEP (definep))
-               {
-                 /* Move handle from scm_weak_symhash to scm_symhash. */
-                 *lsymp = SCM_CDR (lsym);
-                 SCM_SETCDR (lsym, SCM_VELTS(scm_symhash)[scm_hash]);
-                 SCM_VELTS(scm_symhash)[scm_hash] = lsym;
-               }
-             SCM_ALLOW_INTS;
-             return z;
-           }
+         SCM cell = scm_cons (sym, SCM_UNDEFINED);
+         SCM slot = SCM_VELTS (scm_symhash) [hash];
+
+         SCM_VELTS (scm_symhash) [hash] = scm_cons (cell, slot);
+
+         SCM_ALLOW_INTS;
+         return cell;
+       }
+      else
+       {
+         SCM_ALLOW_INTS;
+         return SCM_BOOL_F;
        }
-      SCM_ALLOW_INTS;
-      return scm_wta (sym, "uninterned symbol? ", "");
     }
 }
+#undef FUNC_NAME
+
 
 /* scm_sym2ovcell
  * looks up the symbol in an arbitrary obarray.
@@ -168,18 +219,14 @@ SCM
 scm_sym2ovcell_soft (SCM sym, SCM obarray)
 {
   SCM lsym, z;
-  scm_sizet scm_hash;
-
-  scm_hash = scm_strhash (SCM_UCHARS (sym),
-                         (scm_sizet) SCM_LENGTH (sym),
-                         SCM_LENGTH (obarray));
+  scm_sizet hash = SCM_SYMBOL_HASH (sym) % SCM_VECTOR_LENGTH (obarray);
   SCM_REDEFER_INTS;
-  for (lsym = SCM_VELTS (obarray)[scm_hash];
+  for (lsym = SCM_VELTS (obarray)[hash];
        SCM_NIMP (lsym);
        lsym = SCM_CDR (lsym))
     {
       z = SCM_CAR (lsym);
-      if (SCM_CAR (z) == sym)
+      if (SCM_EQ_P (SCM_CAR (z), sym))
        {
          SCM_REALLOW_INTS;
          return z;
@@ -192,14 +239,17 @@ scm_sym2ovcell_soft (SCM sym, SCM obarray)
 
 SCM 
 scm_sym2ovcell (SCM sym, SCM obarray)
+#define FUNC_NAME "scm_sym2ovcell"
 {
   SCM answer;
   answer = scm_sym2ovcell_soft (sym, obarray);
-  if (answer != SCM_BOOL_F)
+  if (!SCM_FALSEP (answer))
     return answer;
-  scm_wta (sym, "uninterned symbol? ", "");
+  SCM_MISC_ERROR ("uninterned symbol: ~S", SCM_LIST1 (sym));
   return SCM_UNSPECIFIED;              /* not reached */
 }
+#undef FUNC_NAME
+
 
 /* Intern a symbol whose name is the LEN characters at NAME in OBARRAY.
 
@@ -218,104 +268,47 @@ scm_sym2ovcell (SCM sym, SCM obarray)
    in OBARRAY; instead, just return #f.
 
    If OBARRAY is SCM_BOOL_F, create a symbol listed in no obarray and
-   return (SYMBOL . SCM_UNDEFINED).
-
-   If OBARRAY is scm_symhash, and that doesn't contain the symbol,
-   check scm_weak_symhash instead.  */
+   return (SYMBOL . SCM_UNDEFINED).  */
 
 
 SCM 
-scm_intern_obarray_soft (const char *name,scm_sizet len,SCM obarray,int softness)
+scm_intern_obarray_soft (const char *name,scm_sizet len,SCM obarray,unsigned int softness)
 {
+  SCM symbol = scm_mem2symbol (name, len);
+  scm_sizet raw_hash = SCM_SYMBOL_HASH (symbol);
+  scm_sizet hash;
   SCM lsym;
-  SCM z;
-  register scm_sizet i;
-  register unsigned char *tmp;
-  scm_sizet scm_hash;
-
-  SCM_REDEFER_INTS;
-
-  i = len;
-  tmp = (unsigned char *) name;
 
-  if (obarray == SCM_BOOL_F)
+  if (SCM_FALSEP (obarray))
     {
-      scm_hash = scm_strhash (tmp, i, 1019);
-      goto uninterned_symbol;
+      if (softness)
+       return SCM_BOOL_F;
+      else
+       return scm_cons (symbol, SCM_UNDEFINED);
     }
 
-  scm_hash = scm_strhash (tmp, i, SCM_LENGTH(obarray));
+  hash = raw_hash % SCM_VECTOR_LENGTH (obarray);
 
-  /* softness == -1 used to mean that it was known that the symbol
-     wasn't already in the obarray.  I don't think there are any
-     callers that use that case any more, but just in case...
-     -- JimB, Oct 1996  */
-  if (softness == -1)
-    abort ();
-
- retry_new_obarray:
-  for (lsym = SCM_VELTS (obarray)[scm_hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
+  for (lsym = SCM_VELTS (obarray)[hash]; SCM_NIMP (lsym); lsym = SCM_CDR (lsym))
     {
-      z = SCM_CAR (lsym);
-      z = SCM_CAR (z);
-      tmp = SCM_UCHARS (z);
-      if (SCM_LENGTH (z) != len)
-       goto trynext;
-      for (i = len; i--;)
-       if (((unsigned char *) name)[i] != tmp[i])
-         goto trynext;
-      {
-       SCM a;
-       a = SCM_CAR (lsym);
-       SCM_REALLOW_INTS;
+      SCM a = SCM_CAR (lsym);
+      SCM z = SCM_CAR (a);
+      if (SCM_EQ_P (z, symbol))
        return a;
-      }
-    trynext:;
-    }
-
-  if (obarray == scm_symhash)
-    {
-      obarray = scm_weak_symhash;
-      goto retry_new_obarray;
     }
   
- uninterned_symbol:
   if (softness)
     {
-      SCM_REALLOW_INTS;
       return SCM_BOOL_F;
     }
-
-  lsym = scm_makfromstr (name, len, SCM_SYMBOL_SLOTS);
-
-  SCM_SETLENGTH (lsym, (long) len, scm_tc7_msymbol);
-  SCM_SYMBOL_HASH (lsym) = scm_hash;
-  SCM_SYMBOL_PROPS (lsym) = SCM_EOL;
-  if (obarray == SCM_BOOL_F)
-    {
-      SCM answer;
-      SCM_REALLOW_INTS;
-      SCM_NEWCELL (answer);
-      SCM_DEFER_INTS;
-      SCM_SETCAR (answer, lsym);
-      SCM_SETCDR (answer, SCM_UNDEFINED);
-      SCM_REALLOW_INTS;
-      return answer;
-    }
   else
     {
-      SCM a;
-      SCM b;
-
-      SCM_NEWCELL (a);
-      SCM_NEWCELL (b);
-      SCM_SETCAR (a, lsym);
-      SCM_SETCDR (a, SCM_UNDEFINED);
-      SCM_SETCAR (b, a);
-      SCM_SETCDR (b, SCM_VELTS(obarray)[scm_hash]);
-      SCM_VELTS(obarray)[scm_hash] = b;
-      SCM_REALLOW_INTS;
-      return SCM_CAR (b);
+      SCM cell = scm_cons (symbol, SCM_UNDEFINED);
+      SCM slot = SCM_VELTS (obarray) [hash];
+
+      SCM_VELTS (obarray) [hash] = scm_cons (cell, slot);
+
+      return cell;
     }
 }
 
@@ -345,9 +338,10 @@ scm_intern0 (const char * name)
 SCM 
 scm_sysintern0_no_module_lookup (const char *name)
 {
+  scm_sizet len = strlen (name);
   SCM easy_answer;
   SCM_DEFER_INTS;
-  easy_answer = scm_intern_obarray_soft (name, strlen (name), scm_symhash, 1);
+  easy_answer = scm_intern_obarray_soft (name, len, scm_symhash, 1);
   if (SCM_NIMP (easy_answer))
     {
       SCM_ALLOW_INTS;
@@ -355,25 +349,18 @@ scm_sysintern0_no_module_lookup (const char *name)
     }
   else
     {
-      SCM lsym;
-      scm_sizet len = strlen (name);
-      register unsigned char *tmp = (unsigned char *) name;
-      scm_sizet scm_hash = scm_strhash (tmp, len, (unsigned long) scm_symhash_dim);
-      SCM_NEWCELL (lsym);
-      SCM_SETLENGTH (lsym, (long) len, scm_tc7_ssymbol);
-      SCM_SETCHARS (lsym, name);
-      lsym = scm_cons (lsym, SCM_UNDEFINED);
-      SCM_VELTS (scm_symhash)[scm_hash] = scm_cons (lsym, SCM_VELTS (scm_symhash)[scm_hash]);
+      SCM symbol = scm_mem2symbol (name, len);
+      scm_sizet raw_hash = SCM_SYMBOL_HASH (symbol);
+      scm_sizet hash = raw_hash % SCM_VECTOR_LENGTH (scm_symhash);
+      SCM cell = scm_cons (symbol, SCM_UNDEFINED);
+      SCM slot = SCM_VELTS (scm_symhash) [hash];
+
+      SCM_VELTS (scm_symhash) [hash] = scm_cons (cell, slot);
       SCM_ALLOW_INTS;
-      return lsym;
+      return cell;
     }
 }
 
-
-/* Is it safe to access SCM_TOP_LEVEL_LOOKUP_CLOSURE_VAR?
- */
-int scm_can_use_top_level_lookup_closure_var;
-
 /* Intern the symbol named NAME in scm_symhash, and give it the value
    VAL.  NAME is null-terminated.  Use the current top_level lookup
    closure to give NAME its value.
@@ -390,12 +377,12 @@ SCM
 scm_sysintern0 (const char *name)
 {
   SCM lookup_proc;
-  if (scm_can_use_top_level_lookup_closure_var && 
-      SCM_NIMP (lookup_proc = SCM_CDR (scm_top_level_lookup_closure_var)))
+  if (scm_module_system_booted_p
+      && SCM_NIMP (lookup_proc = SCM_TOP_LEVEL_LOOKUP_CLOSURE))
     {
-      SCM sym = SCM_CAR (scm_intern0 (name));
+      SCM sym = scm_str2symbol (name);
       SCM vcell = scm_sym2vcell (sym, lookup_proc, SCM_BOOL_T);
-      if (vcell == SCM_BOOL_F)
+      if (SCM_FALSEP (vcell))
          scm_misc_error ("sysintern0", "can't define variable", sym);
       return vcell;
     }
@@ -411,93 +398,78 @@ scm_symbol_value0 (const char *name)
   /* This looks silly - we look up the symbol twice.  But it is in
      fact necessary given the current module system because the module
      lookup closures are written in scheme which needs real symbols. */
-  SCM symbol = scm_intern_obarray_soft (name, strlen (name), scm_symhash, 0);
-  SCM vcell = scm_sym2vcell (SCM_CAR (symbol),
-                            SCM_CDR (scm_top_level_lookup_closure_var),
-                            SCM_BOOL_F);
+  SCM symbol = scm_str2symbol (name);
+  SCM vcell = scm_sym2vcell (symbol, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_F);
   if (SCM_FALSEP (vcell))
     return SCM_UNDEFINED;
   return SCM_CDR (vcell);
 }
 
+
 SCM_DEFINE (scm_symbol_p, "symbol?", 1, 0, 0, 
-           (SCM obj),
-           "Returns @t{#t} if @var{obj} is a symbol, otherwise returns @t{#f}. (r5rs)")
+           (SCM obj),
+           "Returns @code{#t} if @var{obj} is a symbol, otherwise returns\n"
+           "@code{#f}. (r5rs)")
 #define FUNC_NAME s_scm_symbol_p
 {
-  if SCM_IMP(obj) return SCM_BOOL_F;
-  return SCM_BOOL(SCM_SYMBOLP(obj));
+  return SCM_BOOL (SCM_SYMBOLP (obj));
 }
 #undef FUNC_NAME
 
 SCM_DEFINE (scm_symbol_to_string, "symbol->string", 1, 0, 0, 
            (SCM s),
-           "Returns the name of @var{symbol} as a string.  If the symbol was part of\n"
-           "an object returned as the value of a literal expression\n"
-           "(section @pxref{Literal expressions}) or by a call to the @samp{read} procedure,\n"
-           "and its name contains alphabetic characters, then the string returned\n"
-           "will contain characters in the implementation's preferred standard\n"
-           "case---some implementations will prefer upper case, others lower case.\n"
-           "If the symbol was returned by @samp{string->symbol}, the case of\n"
-           "characters in the string returned will be the same as the case in the\n"
-           "string that was passed to @samp{string->symbol}.  It is an error\n"
-           "to apply mutation procedures like @code{string-set!} to strings returned\n"
-           "by this procedure. (r5rs)\n\n"
-           "The following examples assume that the implementation's standard case is\n"
-           "lower case:\n\n"
-           "@format\n"
-           "@t{(symbol->string 'flying-fish)     \n"
-           "                                ==>  \"flying-fish\"\n"
-           "(symbol->string 'Martin)               ==>  \"martin\"\n"
+           "Returns the name of @var{symbol} as a string.  If the symbol\n"
+           "was part of an object returned as the value of a literal\n"
+           "expression (section @pxref{Literal expressions,,,r4rs, The\n"
+           "Revised^4 Report on Scheme}) or by a call to the @code{read}\n"
+           "procedure, and its name contains alphabetic characters, then\n"
+           "the string returned will contain characters in the\n"
+           "implementation's preferred standard case---some implementations\n"
+           "will prefer upper case, others lower case.  If the symbol was\n"
+           "returned by @code{string->symbol}, the case of characters in\n"
+           "the string returned will be the same as the case in the string\n"
+           "that was passed to @code{string->symbol}.  It is an error to\n"
+           "apply mutation procedures like @code{string-set!} to strings\n"
+           "returned by this procedure. (r5rs)\n\n"
+           "The following examples assume that the implementation's\n"
+           "standard case is lower case:\n\n"
+           "@lisp\n"
+           "(symbol->string 'flying-fish) @result{} \"flying-fish\"\n"
+           "(symbol->string 'Martin)      @result{}  \"martin\"\n"
            "(symbol->string\n"
-           "   (string->symbol "Malvina"))     \n"
-            "                           ==>  \"Malvina\"\n"
-           "}\n"
-           "@end format")
+           "   (string->symbol \"Malvina\")) @result{} \"Malvina\"\n"
+           "@end lisp")
 #define FUNC_NAME s_scm_symbol_to_string
 {
-  SCM_VALIDATE_SYMBOL (1,s);
-  return scm_makfromstr(SCM_CHARS(s), (scm_sizet)SCM_LENGTH(s), 0);
+  SCM_VALIDATE_SYMBOL (1, s);
+  return scm_makfromstr (SCM_SYMBOL_CHARS (s), SCM_SYMBOL_LENGTH (s), 0);
 }
 #undef FUNC_NAME
 
 
 SCM_DEFINE (scm_string_to_symbol, "string->symbol", 1, 0, 0, 
            (SCM s),
-           "Returns the symbol whose name is @var{string}.  This procedure can\n"
-           "create symbols with names containing special characters or letters in\n"
-           "the non-standard case, but it is usually a bad idea to create such\n"
-           "symbols because in some implementations of Scheme they cannot be read as\n"
-           "themselves.  See @samp{symbol->string}.\n\n"
-           "The following examples assume that the implementation's standard case is\n"
-           "lower case:\n\n"
-"@format\n"
-"@t{(eq? 'mISSISSIppi 'mississippi)  \n"
-"          ==>  #t\n"
-"(string->symbol \"mISSISSIppi\")  \n"
-"          ==>\n"
-"  @r{}the symbol with name \"mISSISSIppi\"\n"
-"(eq? 'bitBlt (string->symbol \"bitBlt\"))     \n"
-"          ==>  #f\n"
-"(eq? 'JollyWog\n"
-"     (string->symbol\n"
-"       (symbol->string 'JollyWog)))  \n"
-"          ==>  #t\n"
-"(string=? \"K. Harper, M.D.\"\n"
-"          (symbol->string\n"
-"            (string->symbol \"K. Harper, M.D.\")))  \n"
-"          ==>  #t\n"
-"}\n"
-           "@end format")
+           "Returns the symbol whose name is @var{string}. This procedure\n"
+           "can create symbols with names containing special characters or\n"
+           "letters in the non-standard case, but it is usually a bad idea\n"
+           "to create such because in some implementations of Scheme they\n"
+           "cannot be read as themselves.  See @code{symbol->string}.\n\n"
+           "The following examples assume that the implementation's\n"
+           "standard case is lower case:\n\n"
+           "@lisp\n"
+           "(eq? 'mISSISSIppi 'mississippi) @result{} #t\n"
+           "(string->symbol \"mISSISSIppi\") @result{} @r{the symbol with name \"mISSISSIppi\"}\n"
+           "(eq? 'bitBlt (string->symbol \"bitBlt\")) @result{} #f\n"
+           "(eq? 'JollyWog\n"
+           "  (string->symbol (symbol->string 'JollyWog))) @result{} #t\n"
+           "(string=? \"K. Harper, M.D.\"\n"
+           "  (symbol->string\n"
+           "    (string->symbol \"K. Harper, M.D.\"))) @result{}#t\n"
+           "@end lisp")
 #define FUNC_NAME s_scm_string_to_symbol
 {
-  SCM vcell;
-  SCM answer;
-
-  SCM_VALIDATE_ROSTRING (1,s);
-  vcell = scm_intern(SCM_ROCHARS(s), (scm_sizet)SCM_LENGTH(s));
-  answer = SCM_CAR (vcell);
-  return answer;
+  SCM_VALIDATE_STRING (1, s);
+  return scm_mem2symbol (SCM_STRING_CHARS (s), SCM_STRING_LENGTH (s));
 }
 #undef FUNC_NAME
 
@@ -521,24 +493,21 @@ SCM_DEFINE (scm_string_to_obarray_symbol, "string->obarray-symbol", 2, 1, 0,
   SCM answer;
   int softness;
 
-  SCM_VALIDATE_ROSTRING (2,s);
-  SCM_ASSERT((o == SCM_BOOL_F)
-            || (o == SCM_BOOL_T)
-            || (SCM_VECTORP(o)),
-            o, SCM_ARG1, FUNC_NAME);
+  SCM_VALIDATE_STRING (2, s);
+  SCM_ASSERT (SCM_BOOLP (o) || SCM_VECTORP (o), o, SCM_ARG1, FUNC_NAME);
 
-  softness = ((softp != SCM_UNDEFINED) && (softp != SCM_BOOL_F));
+  softness = (!SCM_UNBNDP (softp) && !SCM_FALSEP(softp));
   /* iron out some screwy calling conventions */
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
-  else if (o == SCM_BOOL_T)
+  else if (SCM_EQ_P (o, SCM_BOOL_T))
     o = SCM_BOOL_F;
     
-  vcell = scm_intern_obarray_soft (SCM_ROCHARS(s),
-                                  (scm_sizet)SCM_ROLENGTH(s),
+  vcell = scm_intern_obarray_soft (SCM_STRING_CHARS(s),
+                                  SCM_STRING_LENGTH (s),
                                   o,
                                   softness);
-  if (vcell == SCM_BOOL_F)
+  if (SCM_FALSEP (vcell))
     return vcell;
   answer = SCM_CAR (vcell);
   return answer;
@@ -554,10 +523,10 @@ SCM_DEFINE (scm_intern_symbol, "intern-symbol", 2, 0, 0,
 {
   scm_sizet hval;
   SCM_VALIDATE_SYMBOL (2,s);
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
   SCM_VALIDATE_VECTOR (1,o);
-  hval = scm_strhash (SCM_UCHARS (s), SCM_LENGTH (s), SCM_LENGTH(o));
+  hval = SCM_SYMBOL_HASH (s) % SCM_VECTOR_LENGTH (o);
   /* If the symbol is already interned, simply return. */
   SCM_REDEFER_INTS;
   {
@@ -568,7 +537,7 @@ SCM_DEFINE (scm_intern_symbol, "intern-symbol", 2, 0, 0,
         lsym = SCM_CDR (lsym))
       {
        sym = SCM_CAR (lsym);
-       if (SCM_CAR (sym) == s)
+       if (SCM_EQ_P (SCM_CAR (sym), s))
          {
            SCM_REALLOW_INTS;
            return SCM_UNSPECIFIED;
@@ -591,10 +560,10 @@ SCM_DEFINE (scm_unintern_symbol, "unintern-symbol", 2, 0, 0,
 {
   scm_sizet hval;
   SCM_VALIDATE_SYMBOL (2,s);
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
   SCM_VALIDATE_VECTOR (1,o);
-  hval = scm_strhash (SCM_UCHARS (s), SCM_LENGTH (s), SCM_LENGTH(o));
+  hval = SCM_SYMBOL_HASH (s) % SCM_VECTOR_LENGTH (o);
   SCM_DEFER_INTS;
   {
     SCM lsym_follow;
@@ -605,10 +574,10 @@ SCM_DEFINE (scm_unintern_symbol, "unintern-symbol", 2, 0, 0,
         lsym_follow = lsym, lsym = SCM_CDR (lsym))
       {
        sym = SCM_CAR (lsym);
-       if (SCM_CAR (sym) == s)
+       if (SCM_EQ_P (SCM_CAR (sym), s))
          {
            /* Found the symbol to unintern. */
-           if (lsym_follow == SCM_BOOL_F)
+           if (SCM_FALSEP (lsym_follow))
              SCM_VELTS(o)[hval] = lsym;
            else
              SCM_SETCDR (lsym_follow, SCM_CDR(lsym));
@@ -632,7 +601,7 @@ SCM_DEFINE (scm_symbol_binding, "symbol-binding", 2, 0, 0,
 {
   SCM vcell;
   SCM_VALIDATE_SYMBOL (2,s);
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
   SCM_VALIDATE_VECTOR (1,o);
   vcell = scm_sym2ovcell (s, o);
@@ -649,12 +618,10 @@ SCM_DEFINE (scm_symbol_interned_p, "symbol-interned?", 2, 0, 0,
 {
   SCM vcell;
   SCM_VALIDATE_SYMBOL (2,s);
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
   SCM_VALIDATE_VECTOR (1,o);
   vcell = scm_sym2ovcell_soft (s, o);
-  if (SCM_IMP(vcell) && (o == scm_symhash))
-    vcell = scm_sym2ovcell_soft (s, scm_weak_symhash);
   return (SCM_NIMP(vcell)
          ? SCM_BOOL_T
          : SCM_BOOL_F);
@@ -666,21 +633,18 @@ SCM_DEFINE (scm_symbol_bound_p, "symbol-bound?", 2, 0, 0,
            (SCM o, SCM s),
            "Return @var{#t} if @var{obarray} contains a symbol with name\n"
            "@var{string} bound to a defined value.  This differs from\n"
-           "@var{symbol-bound?} in that the mere mention of a symbol usually causes\n"
+           "@var{symbol-interned?} in that the mere mention of a symbol usually causes\n"
            "it to be interned; @code{symbol-bound?} determines whether a symbol has\n"
            "been given any meaningful value.")
 #define FUNC_NAME s_scm_symbol_bound_p
 {
   SCM vcell;
   SCM_VALIDATE_SYMBOL (2,s);
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
   SCM_VALIDATE_VECTOR (1,o);
   vcell = scm_sym2ovcell_soft (s, o);
-  return ((  SCM_NIMP(vcell)
-          && (SCM_CDR(vcell) != SCM_UNDEFINED))
-         ? SCM_BOOL_T
-         : SCM_BOOL_F);
+  return SCM_BOOL (SCM_NIMP (vcell) && !SCM_UNBNDP (SCM_CDR (vcell)));
 }
 #undef FUNC_NAME
 
@@ -694,7 +658,7 @@ SCM_DEFINE (scm_symbol_set_x, "symbol-set!", 3, 0, 0,
 {
   SCM vcell;
   SCM_VALIDATE_SYMBOL (2,s);
-  if (o == SCM_BOOL_F)
+  if (SCM_FALSEP (o))
     o = scm_symhash;
   SCM_VALIDATE_VECTOR (1,o);
   vcell = scm_sym2ovcell (s, o);
@@ -703,22 +667,6 @@ SCM_DEFINE (scm_symbol_set_x, "symbol-set!", 3, 0, 0,
 }
 #undef FUNC_NAME
 
-static void
-msymbolize (SCM s)
-{
-  SCM string;
-  string = scm_makfromstr (SCM_CHARS (s), SCM_LENGTH (s), SCM_SYMBOL_SLOTS);
-  SCM_SETCHARS (s, SCM_CHARS (string));
-  SCM_SETLENGTH (s, SCM_LENGTH (s), scm_tc7_msymbol);
-  SCM_SETCDR (string, SCM_EOL);
-  SCM_SETCAR (string, SCM_EOL);
-  SCM_SYMBOL_PROPS (s) = SCM_EOL;
-  /* If it's a tc7_ssymbol, it comes from scm_symhash */
-  SCM_SYMBOL_HASH (s) = scm_strhash (SCM_UCHARS (s),
-                                    (scm_sizet) SCM_LENGTH (s),
-                                    SCM_LENGTH (scm_symhash));
-}
-
 
 SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0, 
            (SCM s),
@@ -726,10 +674,6 @@ SCM_DEFINE (scm_symbol_fref, "symbol-fref", 1, 0, 0,
 #define FUNC_NAME s_scm_symbol_fref
 {
   SCM_VALIDATE_SYMBOL (1,s);
-  SCM_DEFER_INTS;
-  if (SCM_TYP7(s) == scm_tc7_ssymbol)
-    msymbolize (s);
-  SCM_ALLOW_INTS;
   return SCM_SYMBOL_FUNC (s);
 }
 #undef FUNC_NAME
@@ -741,10 +685,6 @@ SCM_DEFINE (scm_symbol_pref, "symbol-pref", 1, 0, 0,
 #define FUNC_NAME s_scm_symbol_pref
 {
   SCM_VALIDATE_SYMBOL (1,s);
-  SCM_DEFER_INTS;
-  if (SCM_TYP7(s) == scm_tc7_ssymbol)
-    msymbolize (s);
-  SCM_ALLOW_INTS;
   return SCM_SYMBOL_PROPS (s);
 }
 #undef FUNC_NAME
@@ -756,11 +696,7 @@ SCM_DEFINE (scm_symbol_fset_x, "symbol-fset!", 2, 0, 0,
 #define FUNC_NAME s_scm_symbol_fset_x
 {
   SCM_VALIDATE_SYMBOL (1,s);
-  SCM_DEFER_INTS;
-  if (SCM_TYP7(s) == scm_tc7_ssymbol)
-    msymbolize (s);
-  SCM_ALLOW_INTS;
-  SCM_SYMBOL_FUNC (s) = val;
+  SCM_SET_SYMBOL_FUNC (s, val);
   return SCM_UNSPECIFIED;
 }
 #undef FUNC_NAME
@@ -773,9 +709,7 @@ SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
 {
   SCM_VALIDATE_SYMBOL (1,s);
   SCM_DEFER_INTS;
-  if (SCM_TYP7(s) == scm_tc7_ssymbol)
-    msymbolize (s);
-  SCM_SYMBOL_PROPS (s) = val;
+  SCM_SET_SYMBOL_PROPS (s, val);
   SCM_ALLOW_INTS;
   return SCM_UNSPECIFIED;
 }
@@ -783,15 +717,12 @@ SCM_DEFINE (scm_symbol_pset_x, "symbol-pset!", 2, 0, 0,
 
 
 SCM_DEFINE (scm_symbol_hash, "symbol-hash", 1, 0, 0, 
-           (SCM s),
-           "Return the hash value derived from @var{symbol}'s name, i.e. the integer\n"
-           "index into @var{symbol}'s obarray at which it is stored.")
+           (SCM symbol),
+           "Return a hash value for @var{symbol}.")
 #define FUNC_NAME s_scm_symbol_hash
 {
-  SCM_VALIDATE_SYMBOL (1,s);
-  if (SCM_TYP7(s) == scm_tc7_ssymbol)
-    msymbolize (s);
-  return SCM_MAKINUM ((unsigned long)s ^ SCM_SYMBOL_HASH (s));
+  SCM_VALIDATE_SYMBOL (1, symbol);
+  return SCM_MAKINUM (SCM_SYMBOL_HASH (symbol));
 }
 #undef FUNC_NAME
 
@@ -800,7 +731,7 @@ static void
 copy_and_prune_obarray (SCM from, SCM to)
 {
   int i;
-  int length = SCM_LENGTH (from);
+  int length = SCM_VECTOR_LENGTH (from);
   for (i = 0; i < length; ++i)
     {
       SCM head = SCM_VELTS (from)[i]; /* GC protection */
@@ -827,70 +758,129 @@ SCM_DEFINE (scm_builtin_bindings, "builtin-bindings", 0, 0, 0,
            "unbound symbols.")
 #define FUNC_NAME s_scm_builtin_bindings
 {
-  int length = SCM_LENGTH (scm_symhash);
-  SCM obarray = scm_make_vector (SCM_MAKINUM (length), SCM_EOL);
+  int length = SCM_VECTOR_LENGTH (scm_symhash);
+  SCM obarray = scm_c_make_hash_table (length);
   copy_and_prune_obarray (scm_symhash, obarray);
   return obarray;
 }
 #undef FUNC_NAME
 
 
-SCM_DEFINE (scm_builtin_weak_bindings, "builtin-weak-bindings", 0, 0, 0, 
-            (),
-           "")
-#define FUNC_NAME s_scm_builtin_weak_bindings
-{
-  int length = SCM_LENGTH (scm_weak_symhash);
-  SCM obarray = scm_make_doubly_weak_hash_table (SCM_MAKINUM (length));
-  copy_and_prune_obarray (scm_weak_symhash, obarray);
-  return obarray;
-}
-#undef FUNC_NAME
+#define MAX_PREFIX_LENGTH 30
 
 static int gensym_counter;
-static SCM gensym_prefix;
-
-/* :FIXME:OPTIMIZE */
-SCM_DEFINE (scm_gensym, "gensym", 0, 2, 0,
-            (SCM name, SCM obarray),
-           "Create a new, unique symbol in @var{obarray}, using the global symbol\n"
-           "table by default.  If @var{name} is specified, it should be used as a\n"
-           "prefix for the new symbol's name.  The default prefix is @code{%%gensym}.")
+
+SCM_DEFINE (scm_gensym, "gensym", 0, 1, 0,
+            (SCM prefix),
+           "Create a new symbol with a name constructed from a prefix and\n"
+           "a counter value. The string @var{prefix} can be specified as\n"
+           "an optional argument. Default prefix is @code{g}.  The counter\n"
+           "is increased by 1 at each call. There is no provision for\n"
+           "resetting the counter.")
 #define FUNC_NAME s_scm_gensym
 {
-  SCM new;
-  if (SCM_UNBNDP (name))
-    name = gensym_prefix;
+  char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
+  char *name = buf;
+  int len;
+  if (SCM_UNBNDP (prefix))
+    {
+      name[0] = 'g';
+      len = 1;
+    }
   else
-    SCM_VALIDATE_ROSTRING (1,name);
+    {
+      SCM_VALIDATE_STRING (1, prefix);
+      len = SCM_STRING_LENGTH (prefix);
+      if (len > MAX_PREFIX_LENGTH)
+       name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
+      strncpy (name, SCM_STRING_CHARS (prefix), len);
+    }
+  {
+    int n_digits = scm_iint2str (gensym_counter++, 10, &name[len]);
+    SCM res = scm_mem2symbol (name, len + n_digits);
+    if (name != buf)
+      scm_must_free (name);
+    return res;
+  }
+}
+#undef FUNC_NAME
 
-  new = name;
-  if (SCM_UNBNDP (obarray))
+static int gentemp_counter;
+
+SCM_DEFINE (scm_gentemp, "gentemp", 0, 2, 0,
+            (SCM prefix, SCM obarray),
+           "Create a new symbol with a name unique in an obarray.\n"
+           "The name is constructed from an optional string @var{prefix}\n"
+           "and a counter value.  The default prefix is @code{t}.  The\n"
+           "@var{obarray} is specified as a second optional argument.\n"
+           "Default is the system obarray where all normal symbols are\n"
+           "interned.  The counter is increased by 1 at each\n"
+           "call.  There is no provision for resetting the counter.")
+#define FUNC_NAME s_scm_gentemp
+{
+  char buf[MAX_PREFIX_LENGTH + SCM_INTBUFLEN];
+  char *name = buf;
+  int len, n_digits;
+  if (SCM_UNBNDP (prefix))
+    {
+      name[0] = 't';
+      len = 1;
+    }
+  else
     {
-      obarray = SCM_BOOL_F;
-      goto skip_test;
+      SCM_VALIDATE_STRING (1, prefix);
+      len = SCM_STRING_LENGTH (prefix);
+      if (len > MAX_PREFIX_LENGTH)
+       name = SCM_MUST_MALLOC (MAX_PREFIX_LENGTH + SCM_INTBUFLEN);
+      strncpy (name, SCM_STRING_CHARS (prefix), len);
     }
+
+  if (SCM_UNBNDP (obarray))
+    obarray = scm_symhash;
   else
     SCM_ASSERT ((SCM_VECTORP (obarray) || SCM_WVECTP (obarray)),
                obarray,
                SCM_ARG2,
                FUNC_NAME);
-  while (scm_string_to_obarray_symbol (obarray, new, SCM_BOOL_T)
-        != SCM_BOOL_F)
-    skip_test:
-    new = scm_string_append
-      (scm_cons2 (name,
-                 scm_number_to_string (SCM_MAKINUM (gensym_counter++),
-                                       SCM_UNDEFINED),
-                 SCM_EOL));
-  return scm_string_to_obarray_symbol (obarray, new, SCM_BOOL_F);
+  do
+    n_digits = scm_iint2str (gentemp_counter++, 10, &name[len]);
+  while (!SCM_FALSEP (scm_intern_obarray_soft (name,
+                                              len + n_digits,
+                                              obarray,
+                                              1)));
+  {
+    SCM vcell = scm_intern_obarray_soft (name,
+                                        len + n_digits,
+                                        obarray,
+                                        0);
+    if (name != buf)
+      scm_must_free (name);
+    return SCM_CAR (vcell);
+  }
 }
 #undef FUNC_NAME
 
+
+void
+scm_symbols_prehistory ()
+{
+  symbols = scm_make_weak_key_hash_table (SCM_MAKINUM (277));
+  scm_permanent_object (symbols);
+}
+
+
 void
 scm_init_symbols ()
 {
   gensym_counter = 0;
-  gensym_prefix = scm_permanent_object (scm_makfrom0str ("%%gensym"));
-#include "symbols.x"
+  gentemp_counter = 0;
+#ifndef SCM_MAGIC_SNARFER
+#include "libguile/symbols.x"
+#endif
 }
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/