diff --git a/ase/stx/bootstrp.c b/ase/stx/bootstrp.c index c3415314..e19be497 100644 --- a/ase/stx/bootstrp.c +++ b/ase/stx/bootstrp.c @@ -1,5 +1,5 @@ /* - * $Id: bootstrp.c,v 1.29 2005-07-19 16:09:34 bacon Exp $ + * $Id: bootstrp.c,v 1.30 2005-08-11 09:57:54 bacon Exp $ */ #include @@ -215,14 +215,6 @@ static class_info_t class_info[] = XP_NULL, XP_STX_SPEC_BYTE_INDEXABLE }, - { - XP_TEXT("SymbolTable"), - XP_TEXT("IndexedCollection"), - XP_NULL, - XP_NULL, - XP_NULL, - XP_STX_SPEC_WORD_INDEXABLE - }, { XP_TEXT("Dictionary"), XP_TEXT("IndexedCollection"), @@ -271,14 +263,6 @@ static class_info_t class_info[] = XP_NULL, XP_STX_SPEC_NOT_INDEXABLE }, - { - XP_TEXT("Symlink"), - XP_TEXT("Link"), - XP_TEXT("symbol"), - XP_NULL, - XP_NULL, - XP_STX_SPEC_NOT_INDEXABLE - }, { XP_NULL, XP_NULL, @@ -351,8 +335,6 @@ int xp_stx_bootstrap (xp_stx_t* stx) } /* more initialization */ - XP_STX_CLASS(stx,stx->symbol_table) = - xp_stx_lookup_class(stx, XP_TEXT("SymbolTable")); XP_STX_CLASS(stx,stx->smalltalk) = stx->class_system_dictionary; symbol_Smalltalk = xp_stx_new_symbol (stx, XP_TEXT("Smalltalk")); @@ -379,11 +361,9 @@ int xp_stx_bootstrap (xp_stx_t* stx) static void __create_bootstrapping_objects (xp_stx_t* stx) { - xp_word_t class_SymlinkMeta; xp_word_t class_SymbolMeta; xp_word_t class_MetaclassMeta; xp_word_t class_AssociationMeta; - xp_word_t symbol_Symlink; xp_word_t symbol_Symbol; xp_word_t symbol_Metaclass; xp_word_t symbol_Association; @@ -397,18 +377,13 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) xp_assert (stx->true == XP_STX_TRUE); xp_assert (stx->false == XP_STX_FALSE); - /* symbol table & system dictionary */ - /* TODO: symbol table and dictionary size */ - stx->symbol_table = xp_stx_alloc_word_object ( - stx, XP_NULL, 0, XP_NULL, 1000); + /* system dictionary */ + /* TODO: dictionary size */ stx->smalltalk = xp_stx_alloc_word_object ( stx, XP_NULL, 1, XP_NULL, 256); /* set tally */ XP_STX_WORD_AT(stx,stx->smalltalk,0) = XP_STX_TO_SMALLINT(0); - /* Symlink */ - stx->class_symlink = xp_stx_alloc_word_object( - stx, XP_NULL, XP_STX_CLASS_SIZE, XP_NULL, 0); /* Symbol */ stx->class_symbol = xp_stx_alloc_word_object( stx, XP_NULL, XP_STX_CLASS_SIZE, XP_NULL, 0); @@ -423,9 +398,6 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) * as a normal class. "Metaclass class" is an instance of * Metaclass. */ - /* Symlink class */ - class_SymlinkMeta = xp_stx_alloc_word_object( - stx, XP_NULL, XP_STX_METACLASS_SIZE, XP_NULL, 0); /* Symbol class */ class_SymbolMeta = xp_stx_alloc_word_object( stx, XP_NULL, XP_STX_METACLASS_SIZE, XP_NULL, 0); @@ -436,8 +408,6 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) class_AssociationMeta = xp_stx_alloc_word_object( stx, XP_NULL, XP_STX_METACLASS_SIZE, XP_NULL, 0); - /* (Symlink class) setClass: Metaclass */ - XP_STX_CLASS(stx,class_SymlinkMeta) = stx->class_metaclass; /* (Symbol class) setClass: Metaclass */ XP_STX_CLASS(stx,class_SymbolMeta) = stx->class_metaclass; /* (Metaclass class) setClass: Metaclass */ @@ -445,8 +415,6 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) /* (Association class) setClass: Metaclass */ XP_STX_CLASS(stx,class_AssociationMeta) = stx->class_metaclass; - /* Symlink setClass: (Symlink class) */ - XP_STX_CLASS(stx,stx->class_symlink) = class_SymlinkMeta; /* Symbol setClass: (Symbol class) */ XP_STX_CLASS(stx,stx->class_symbol) = class_SymbolMeta; /* Metaclass setClass: (Metaclass class) */ @@ -454,9 +422,6 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) /* Association setClass: (Association class) */ XP_STX_CLASS(stx,stx->class_association) = class_AssociationMeta; - /* (Symlink class) setSpec: XP_STX_CLASS_SIZE */ - XP_STX_WORD_AT(stx,class_SymlinkMeta,XP_STX_CLASS_SPEC) = - XP_STX_TO_SMALLINT((XP_STX_CLASS_SIZE << XP_STX_SPEC_INDEXABLE_BITS) | XP_STX_SPEC_NOT_INDEXABLE); /* (Symbol class) setSpec: CLASS_SIZE */ XP_STX_WORD_AT(stx,class_SymbolMeta,XP_STX_CLASS_SPEC) = XP_STX_TO_SMALLINT((XP_STX_CLASS_SIZE << XP_STX_SPEC_INDEXABLE_BITS) | XP_STX_SPEC_NOT_INDEXABLE); @@ -468,11 +433,8 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) XP_STX_TO_SMALLINT((XP_STX_CLASS_SIZE << XP_STX_SPEC_INDEXABLE_BITS) | XP_STX_SPEC_NOT_INDEXABLE); /* specs for class_metaclass, class_association, - * class_symbol, class_symlink are set later in - * __create_builtin_classes */ + * class_symbol are set later in __create_builtin_classes */ - /* #Symlink */ - symbol_Symlink = xp_stx_new_symbol (stx, XP_TEXT("Symlink")); /* #Symbol */ symbol_Symbol = xp_stx_new_symbol (stx, XP_TEXT("Symbol")); /* #Metaclass */ @@ -480,8 +442,6 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) /* #Association */ symbol_Association = xp_stx_new_symbol (stx, XP_TEXT("Association")); - /* Symlink setName: #Symlink */ - XP_STX_WORD_AT(stx,stx->class_symlink,XP_STX_CLASS_NAME) = symbol_Symlink; /* Symbol setName: #Symbol */ XP_STX_WORD_AT(stx,stx->class_symbol,XP_STX_CLASS_NAME) = symbol_Symbol; /* Metaclass setName: #Metaclass */ @@ -490,8 +450,6 @@ static void __create_bootstrapping_objects (xp_stx_t* stx) XP_STX_WORD_AT(stx,stx->class_association,XP_STX_CLASS_NAME) = symbol_Association; /* register class names into the system dictionary */ - xp_stx_dict_put (stx, - stx->smalltalk, symbol_Symlink, stx->class_symlink); xp_stx_dict_put (stx, stx->smalltalk, symbol_Symbol, stx->class_symbol); xp_stx_dict_put (stx, diff --git a/ase/stx/dict.c b/ase/stx/dict.c index ad09fcb2..e452a168 100644 --- a/ase/stx/dict.c +++ b/ase/stx/dict.c @@ -1,5 +1,5 @@ /* - * $Id: dict.c,v 1.7 2005-08-03 15:49:17 bacon Exp $ + * $Id: dict.c,v 1.8 2005-08-11 09:57:54 bacon Exp $ */ #include @@ -65,7 +65,7 @@ static xp_word_t __dict_find_slot ( return index; } -static void __dict_grow (xp_stx_t* stx, xp_word_t dict) +static void __grow_dict (xp_stx_t* stx, xp_word_t dict) { xp_word_t new, size, index, assoc; @@ -151,7 +151,7 @@ xp_word_t xp_stx_dict_put ( capa = XP_STX_SIZE(stx,dict) - 1; tally = XP_STX_FROM_SMALLINT(XP_STX_WORD_AT(stx,dict,0)); if (capa <= tally + 1) { - __dict_grow (stx, dict); + __grow_dict (stx, dict); /* refresh tally */ tally = XP_STX_FROM_SMALLINT(XP_STX_WORD_AT(stx,dict,0)); } diff --git a/ase/stx/stx.c b/ase/stx/stx.c index 118992dd..558b72f7 100644 --- a/ase/stx/stx.c +++ b/ase/stx/stx.c @@ -1,5 +1,5 @@ /* - * $Id: stx.c,v 1.35 2005-07-24 16:50:53 bacon Exp $ + * $Id: stx.c,v 1.36 2005-08-11 09:57:54 bacon Exp $ */ #include @@ -23,10 +23,10 @@ xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_word_t capacity) } stx->symtab.size = 0; - stx->symtab.capacity = 256; - stx->symtab.data = (xp_word_t*)xp_malloc ( + stx->symtab.capacity = 128; /* TODO: symbol table size */ + stx->symtab.datum = (xp_word_t*)xp_malloc ( xp_sizeof(xp_word_t) * stx->symtab.capacity); - if (stx->symtab.data == XP_NULL) { + if (stx->symtab.datum == XP_NULL) { xp_stx_memory_close (&stx->memory); if (stx->__malloced) xp_free (stx); return XP_NULL; @@ -36,10 +36,8 @@ xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_word_t capacity) stx->true = XP_STX_TRUE; stx->false = XP_STX_FALSE; - stx->symbol_table = XP_STX_NIL; stx->smalltalk = XP_STX_NIL; - stx->class_symlink = XP_STX_NIL; stx->class_symbol = XP_STX_NIL; stx->class_metaclass = XP_STX_NIL; stx->class_association = XP_STX_NIL; @@ -54,7 +52,7 @@ xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_word_t capacity) stx->class_smallinteger = XP_STX_NIL; for (i = 0; i < stx->symtab.capacity; i++) { - stx->symtab.data[i] = stx->nil; + stx->symtab.datum[i] = stx->nil; } stx->__wantabort = xp_false; @@ -63,7 +61,7 @@ xp_stx_t* xp_stx_open (xp_stx_t* stx, xp_word_t capacity) void xp_stx_close (xp_stx_t* stx) { - xp_free (stx->symtab.data); + xp_free (stx->symtab.datum); xp_stx_memory_close (&stx->memory); if (stx->__malloced) xp_free (stx); } diff --git a/ase/stx/stx.h b/ase/stx/stx.h index b7244b77..a5fb0797 100644 --- a/ase/stx/stx.h +++ b/ase/stx/stx.h @@ -1,5 +1,5 @@ /* - * $Id: stx.h,v 1.39 2005-07-24 16:50:53 bacon Exp $ + * $Id: stx.h,v 1.40 2005-08-11 09:57:54 bacon Exp $ */ #ifndef _XP_STX_STX_H_ @@ -57,25 +57,26 @@ struct xp_stx_memory_t xp_bool_t __malloced; }; +struct xp_stx_symtab_t +{ + xp_word_t* datum; + xp_word_t size; + xp_word_t capacity; +}; + +typedef struct xp_stx_symtab_t xp_stx_symtab_t; + struct xp_stx_t { xp_stx_memory_t memory; - - struct - { - xp_word_t* data; - xp_word_t size; - xp_word_t capacity; - } symtab; + xp_stx_symtab_t symtab; xp_word_t nil; xp_word_t true; xp_word_t false; - xp_word_t symbol_table; xp_word_t smalltalk; - xp_word_t class_symlink; xp_word_t class_symbol; xp_word_t class_metaclass; xp_word_t class_association; diff --git a/ase/stx/symbol.c b/ase/stx/symbol.c index a4a47fb9..fc243132 100644 --- a/ase/stx/symbol.c +++ b/ase/stx/symbol.c @@ -1,22 +1,43 @@ /* - * $Id: symbol.c,v 1.20 2005-08-06 04:10:11 bacon Exp $ + * $Id: symbol.c,v 1.21 2005-08-11 09:57:54 bacon Exp $ */ #include #include #include -xp_word_t xp_stx_new_symlink (xp_stx_t* stx, xp_word_t symbol) +static void __grow_symtab (xp_stx_t* stx) { - xp_word_t x; + xp_word_t capa, ncapa, i, j; + xp_word_t* nspace; - x = xp_stx_alloc_word_object( - stx, XP_NULL, XP_STX_SYMLINK_SIZE, XP_NULL, 0); - XP_STX_CLASS(stx,x) = stx->class_symlink; - XP_STX_WORD_AT(stx,x,XP_STX_SYMLINK_LINK) = stx->nil; - XP_STX_WORD_AT(stx,x,XP_STX_SYMLINK_SYMBOL) = symbol; + capa = stx->symtab.capacity; + ncapa = capa << 1; - return x; + nspace = (xp_word_t*)xp_malloc(xp_sizeof(xp_word_t) * ncapa); + if (nspace == XP_NULL) { + /* TODO: handle memory error */ + } + + for (i = 0; i < capa; i++) { + xp_word_t x = stx->symtab.datum[i]; + if (x == stx->nil) continue; + + j = xp_stx_strxhash ( + XP_STX_DATA(stx,x), XP_STX_SIZE(stx,x)) % ncapa; + + while (1) { + if (nspace[j] == stx->nil) { + nspace[j] = x; + break; + } + j = (j % ncapa) + 1; + } + } + + stx->symtab.capacity = ncapa; + xp_free (stx->symtab.datum); + stx->symtab.datum = nspace; } xp_word_t xp_stx_new_symbol (xp_stx_t* stx, const xp_char_t* name) @@ -27,86 +48,47 @@ xp_word_t xp_stx_new_symbol (xp_stx_t* stx, const xp_char_t* name) xp_word_t xp_stx_new_symbolx ( xp_stx_t* stx, const xp_char_t* name, xp_word_t len) { - xp_word_t x, hash, table, link, next; - - table = stx->symbol_table; - hash = xp_stx_strxhash(name,len) % XP_STX_SIZE(stx,table); - link = XP_STX_WORD_AT(stx,table,hash); - - if (link == stx->nil) { - x = xp_stx_alloc_char_objectx (stx, name, len); - XP_STX_CLASS(stx,x) = stx->class_symbol; - XP_STX_WORD_AT(stx,table,hash) = xp_stx_new_symlink(stx,x); - } - else { - do { - x = XP_STX_WORD_AT(stx,link,XP_STX_SYMLINK_SYMBOL); - xp_assert (xp_stx_classof(stx,x) == stx->class_symbol); - - if (xp_strxcmp ( - XP_STX_DATA(stx,x), - XP_STX_SIZE(stx,x), name) == 0) return x; - - next = XP_STX_WORD_AT(stx,link,XP_STX_SYMLINK_LINK); - if (next == stx->nil) { - x = xp_stx_alloc_char_objectx (stx, name, len); - XP_STX_CLASS(stx,x) = stx->class_symbol; - XP_STX_WORD_AT(stx,link,XP_STX_SYMLINK_LINK) = - xp_stx_new_symlink(stx,x); - break; - } - - link = next; - } while (1); - } - - return x; -} - -void xp_stx_traverse_symbol_table ( - xp_stx_t* stx, void (*func) (xp_stx_t*,xp_word_t,void*), void* data) -{ - xp_word_t link; - xp_word_t size; - xp_word_t table; - - table = stx->symbol_table; - size = XP_STX_SIZE(stx,table); - - while (size-- > 0) { - link = XP_STX_WORD_AT(stx,table,size); - - while (link != stx->nil) { - func (stx, XP_STX_WORD_AT(stx,link,XP_STX_SYMLINK_SYMBOL), data); - link = XP_STX_WORD_AT(stx,link,XP_STX_SYMLINK_LINK); - } - } -} - -#if 0 -xp_word_t xp_stx_new_symbolx ( - xp_stx_t* stx, const xp_char_t* name, xp_word_t len) -{ - xp_word_t capa, hash, index; + xp_word_t capa, hash, index, size, x; capa = stx->symtab.capacity; size = stx->symtab.size; if (capa <= size + 1) { __grow_symtab (stx); + capa = stx->symtab.capacity; } hash = xp_stx_strxhash(name,len); index = hash % stx->symtab.capacity; while (1) { - symbol = stx->symtab.datum[index]; - if (symbol != stx->nil) break; + x = stx->symtab.datum[index]; + if (x == stx->nil) { + /* insert a new item into an empty slot */ + x = xp_stx_alloc_char_objectx (stx, name, len); + XP_STX_CLASS(stx,x) = stx->class_symbol; + stx->symtab.datum[index] = x; + stx->symtab.size++; + break; + } if (xp_strxncmp(name, len, - XP_STX_DATA(stx,symbol), XP_STX_SIZE(stx,symbol)) == 0) break; + XP_STX_DATA(stx,x), XP_STX_SIZE(stx,x)) == 0) break; - index = index % stx->symtabl.capacity + 1; + index = (index % stx->symtab.capacity) + 1; + } + + return x; +} + +void xp_stx_traverse_symbol_table ( + xp_stx_t* stx, void (*func) (xp_stx_t*,xp_word_t,void*), void* data) +{ + xp_word_t index, x; + + for (index = 0; index < stx->symtab.capacity; index++) { + x = stx->symtab.datum[index]; + if (x != stx->nil) func (stx, x, data); } } -#endif + diff --git a/ase/test/stx/stx.c b/ase/test/stx/stx.c index 42d29011..276d1c28 100644 --- a/ase/test/stx/stx.c +++ b/ase/test/stx/stx.c @@ -167,6 +167,9 @@ int xp_main (int argc, xp_char_t* argv[]) xp_printf (XP_TEXT("stx.false %lu\n"), (unsigned long)stx.false); xp_printf (XP_TEXT("-------------\n")); + + xp_printf (XP_TEXT(">> SYMBOL_TABLE (%u/%u symbols/slots) <<\n"), + (unsigned int)stx.symtab.size, (unsigned int)stx.symtab.capacity); xp_stx_traverse_symbol_table (&stx, print_symbol_names, XP_NULL); xp_printf (XP_TEXT("-------------\n"));