diff options
Diffstat (limited to 'libguile')
98 files changed, 2613 insertions, 4578 deletions
diff --git a/libguile/Makefile.am b/libguile/Makefile.am index 046ce21cc..96c66a79e 100644 --- a/libguile/Makefile.am +++ b/libguile/Makefile.am @@ -43,8 +43,7 @@ gnulib_library = $(top_builddir)/lib/libgnu.la ETAGS_ARGS = --regex='/SCM_\(GLOBAL_\)?\(G?PROC\|G?PROC1\|SYMBOL\|VCELL\|CONST_LONG\).*\"\([^\"]\)*\"/\3/' \ --regex='/[ \t]*SCM_[G]?DEFINE1?[ \t]*(\([^,]*\),[^,]*/\1/' -lib_LTLIBRARIES = libguile.la \ - libguile-i18n-v-@LIBGUILE_I18N_MAJOR@.la +lib_LTLIBRARIES = libguile.la bin_PROGRAMS = guile noinst_PROGRAMS = guile_filter_doc_snarfage gen-scmconfig @@ -132,7 +131,6 @@ libguile_la_SOURCES = \ fluids.c \ fports.c \ frames.c \ - futures.c \ gc-malloc.c \ gc.c \ gdbint.c \ @@ -145,6 +143,7 @@ libguile_la_SOURCES = \ hash.c \ hashtab.c \ hooks.c \ + i18n.c \ init.c \ inline.c \ instructions.c \ @@ -203,15 +202,6 @@ libguile_la_SOURCES = \ vports.c \ weaks.c -libguile_i18n_v_@LIBGUILE_I18N_MAJOR@_la_SOURCES = i18n.c -libguile_i18n_v_@LIBGUILE_I18N_MAJOR@_la_CFLAGS = \ - $(libguile_la_CFLAGS) -libguile_i18n_v_@LIBGUILE_I18N_MAJOR@_la_LIBADD = \ - libguile.la $(gnulib_library) -libguile_i18n_v_@LIBGUILE_I18N_MAJOR@_la_LDFLAGS = \ - -module -L$(builddir) -lguile \ - -version-info @LIBGUILE_I18N_INTERFACE@ - DOT_X_FILES = \ alist.x \ arbiters.x \ @@ -239,7 +229,6 @@ DOT_X_FILES = \ feature.x \ fluids.x \ fports.x \ - futures.x \ gc-malloc.x \ gc.x \ gettext.x \ @@ -336,7 +325,6 @@ DOT_DOC_FILES = \ feature.doc \ fluids.doc \ fports.doc \ - futures.doc \ gc-malloc.doc \ gc.doc \ gettext.doc \ @@ -442,7 +430,11 @@ noinst_HEADERS += vm-engine.c vm-i-system.c vm-i-scheme.c vm-i-loader.c libguile_la_DEPENDENCIES = @LIBLOBJS@ libguile_la_LIBADD = @LIBLOBJS@ $(gnulib_library) $(LTLIBGMP) $(LTLIBUNISTRING) -libguile_la_LDFLAGS = @LTLIBINTL@ -version-info @LIBGUILE_INTERFACE_CURRENT@:@LIBGUILE_INTERFACE_REVISION@:@LIBGUILE_INTERFACE_AGE@ -export-dynamic -no-undefined +libguile_la_LDFLAGS = \ + @LTLIBINTL@ \ + -version-info @LIBGUILE_INTERFACE_CURRENT@:@LIBGUILE_INTERFACE_REVISION@:@LIBGUILE_INTERFACE_AGE@ \ + -export-dynamic -no-undefined \ + $(GNU_LD_FLAGS) if HAVE_LD_VERSION_SCRIPT @@ -465,6 +457,7 @@ modinclude_HEADERS = \ arrays.h \ async.h \ backtrace.h \ + bdw-gc.h \ boolean.h \ bitvectors.h \ bytevectors.h \ @@ -487,7 +480,6 @@ modinclude_HEADERS = \ fluids.h \ fports.h \ frames.h \ - futures.h \ gc.h \ gdb_interface.h \ gdbint.h \ @@ -605,6 +597,8 @@ libpath.h: $(srcdir)/Makefile.in $(top_builddir)/config.status @echo '#define SCM_PKGDATA_DIR "$(pkgdatadir)"' >> libpath.tmp @echo '#define SCM_LIBRARY_DIR "$(pkgdatadir)/$(GUILE_EFFECTIVE_VERSION)"'>>libpath.tmp @echo '#define SCM_SITE_DIR "$(pkgdatadir)/site"' >> libpath.tmp + @echo '#define SCM_LIB_DIR "$(libdir)"' >> libpath.tmp + @echo '#define SCM_EXTENSIONS_DIR "$(pkglibdir)/$(GUILE_EFFECTIVE_VERSION)/extensions"' >> libpath.tmp @echo '#define SCM_CCACHE_DIR "$(pkglibdir)/$(GUILE_EFFECTIVE_VERSION)/ccache"' >> libpath.tmp @echo '#define SCM_EFFECTIVE_VERSION "$(GUILE_EFFECTIVE_VERSION)"' >> libpath.tmp @echo '#define SCM_BUILD_INFO { \' >> libpath.tmp @@ -624,10 +618,11 @@ libpath.h: $(srcdir)/Makefile.in $(top_builddir)/config.status @echo ' { "infodir", "@infodir@" }, \' >> libpath.tmp @echo ' { "mandir", "@mandir@" }, \' >> libpath.tmp @echo ' { "includedir", "@includedir@" }, \' >> libpath.tmp - @echo ' { "pkgdatadir", "@pkgdatadir@" }, \' >> libpath.tmp - @echo ' { "pkglibdir", "@pkglibdir@" }, \' >> libpath.tmp - @echo ' { "pkgincludedir", "@pkgincludedir@" }, \' \ + @echo ' { "pkgdatadir", "$(pkgdatadir)" }, \' >> libpath.tmp + @echo ' { "pkglibdir", "$(pkglibdir)" }, \' >> libpath.tmp + @echo ' { "pkgincludedir", "$(pkgincludedir)" }, \' \ >> libpath.tmp + @echo ' { "extensionsdir", "$(pkglibdir)/$(GUILE_EFFECTIVE_VERSION)/extensions" }, \' >> libpath.tmp @echo ' { "guileversion", "@GUILE_VERSION@" }, \' >> libpath.tmp @echo ' { "libguileinterface", "@LIBGUILE_INTERFACE@" }, \' \ >> libpath.tmp diff --git a/libguile/__scm.h b/libguile/__scm.h index 32b52df51..8bb1b1a0e 100644 --- a/libguile/__scm.h +++ b/libguile/__scm.h @@ -104,11 +104,22 @@ * and then SCM_API marks them for export. */ #define SCM_INTERNAL extern +/* The SCM_DEPRECATED macro is used in declarations of deprecated functions + * or variables. Defining `SCM_BUILDING_DEPRECATED_CODE' allows deprecated + * functions to be implemented in terms of deprecated functions, and allows + * deprecated functions to be referred to by `scm_c_define_gsubr ()'. */ +#if !defined (SCM_BUILDING_DEPRECATED_CODE) \ + && defined (__GNUC__) && (__GNUC__ >= 3) +# define SCM_DEPRECATED SCM_API __attribute__ ((__deprecated__)) +#else +# define SCM_DEPRECATED SCM_API +#endif + /* The SCM_ALIGNED macro, when defined, can be used to instruct the compiler * to honor the given alignment constraint. */ -#if (defined __GNUC__) +#if defined __GNUC__ # define SCM_ALIGNED(x) __attribute__ ((aligned (x))) -#elif (defined __INTEL_COMPILER) +#elif defined __INTEL_COMPILER # define SCM_ALIGNED(x) __declspec (align (x)) #else /* Don't know how to align things. */ @@ -491,11 +502,24 @@ typedef long SCM_STACKITEM; #define SCM_STACK_PTR(ptr) ((SCM_STACKITEM *) (void *) (ptr)) -#define SCM_ASYNC_TICK /*fixme* should change names */ \ -do { \ - if (SCM_I_CURRENT_THREAD->pending_asyncs) \ - scm_async_click (); \ -} while (0) +SCM_API void scm_async_tick (void); + +#ifdef BUILDING_LIBGUILE + +/* FIXME: should change names */ +# define SCM_ASYNC_TICK \ + do \ + { \ + if (SCM_I_CURRENT_THREAD->pending_asyncs) \ + scm_async_click (); \ + } \ + while (0) + +#else /* !BUILDING_LIBGUILE */ + +# define SCM_ASYNC_TICK (scm_async_tick ()) + +#endif /* !BUILDING_LIBGUILE */ /* Anthony Green writes: @@ -665,6 +689,14 @@ SCM_API SCM scm_apply_generic (SCM gf, SCM args); #define SCM_C_INLINE_KEYWORD #endif +/* Handling thread-local storage (TLS). */ + +#ifdef SCM_HAVE_THREAD_STORAGE_CLASS +# define SCM_THREAD_LOCAL __thread +#else +# define SCM_THREAD_LOCAL +#endif + #endif /* SCM___SCM_H */ /* diff --git a/libguile/_scm.h b/libguile/_scm.h index 9907adf24..6cb7ce8a4 100644 --- a/libguile/_scm.h +++ b/libguile/_scm.h @@ -173,7 +173,7 @@ /* Major and minor versions must be single characters. */ #define SCM_OBJCODE_MAJOR_VERSION 0 -#define SCM_OBJCODE_MINOR_VERSION D +#define SCM_OBJCODE_MINOR_VERSION L #define SCM_OBJCODE_MAJOR_VERSION_STRING \ SCM_CPP_STRINGIFY(SCM_OBJCODE_MAJOR_VERSION) #define SCM_OBJCODE_MINOR_VERSION_STRING \ diff --git a/libguile/array-map.c b/libguile/array-map.c index fb9ceea37..eaac54a1f 100644 --- a/libguile/array-map.c +++ b/libguile/array-map.c @@ -37,7 +37,6 @@ #include "libguile/vectors.h" #include "libguile/bitvectors.h" #include "libguile/srfi-4.h" -#include "libguile/dynwind.h" #include "libguile/generalized-arrays.h" #include "libguile/generalized-vectors.h" @@ -78,6 +77,9 @@ static ra_iproc ra_asubrs[] = {0, 0, 0} }; +/* The WHAT argument for `scm_gc_malloc ()' et al. */ +static const char indices_gc_hint[] = "array-indices"; + #define GVREF scm_c_generalized_vector_ref #define GVSET scm_c_generalized_vector_set_x @@ -311,10 +313,8 @@ scm_ramapc (int (*cproc)(), SCM data, SCM ra0, SCM lra, const char *what) plvra = SCM_CDRLOC (*plvra); } - scm_dynwind_begin (0); - - vinds = scm_malloc (sizeof(long) * SCM_I_ARRAY_NDIM (ra0)); - scm_dynwind_free (vinds); + vinds = scm_gc_malloc_pointerless (sizeof(long) * SCM_I_ARRAY_NDIM (ra0), + indices_gc_hint); for (k = 0; k <= kmax; k++) vinds[k] = SCM_I_ARRAY_DIMS (ra0)[k].lbnd; @@ -343,7 +343,6 @@ scm_ramapc (int (*cproc)(), SCM data, SCM ra0, SCM lra, const char *what) } while (k >= 0); - scm_dynwind_end (); return 1; } } @@ -1015,10 +1014,8 @@ SCM_DEFINE (scm_array_index_map_x, "array-index-map!", 2, 0, 0, if (kmax < 0) return scm_array_set_x (ra, scm_call_0 (proc), SCM_EOL); - scm_dynwind_begin (0); - - vinds = scm_malloc (sizeof(long) * SCM_I_ARRAY_NDIM (ra)); - scm_dynwind_free (vinds); + vinds = scm_gc_malloc_pointerless (sizeof(long) * SCM_I_ARRAY_NDIM (ra), + indices_gc_hint); for (k = 0; k <= kmax; k++) vinds[k] = SCM_I_ARRAY_DIMS (ra)[k].lbnd; @@ -1050,7 +1047,6 @@ SCM_DEFINE (scm_array_index_map_x, "array-index-map!", 2, 0, 0, } while (k >= 0); - scm_dynwind_end (); return SCM_UNSPECIFIED; } else if (scm_is_generalized_vector (ra)) diff --git a/libguile/arrays.c b/libguile/arrays.c index 2be9ec3f0..8dc1d7888 100644 --- a/libguile/arrays.c +++ b/libguile/arrays.c @@ -211,7 +211,7 @@ scm_from_contiguous_typed_array (SCM type, SCM bounds, const void *bytes, scm_t_array_dim *s; SCM ra; scm_t_array_handle h; - void *base; + void *elts; size_t sz; ra = scm_i_shap2ra (bounds); @@ -230,16 +230,28 @@ scm_from_contiguous_typed_array (SCM type, SCM bounds, const void *bytes, scm_array_get_handle (ra, &h); - base = scm_array_handle_uniform_writable_elements (&h); - sz = scm_array_handle_uniform_element_size (&h); + elts = h.writable_elements; + sz = scm_array_handle_uniform_element_bit_size (&h); scm_array_handle_release (&h); - if (byte_len % sz) - SCM_MISC_ERROR ("byte length not a multiple of the unit size", SCM_EOL); - if (byte_len / sz != rlen) - SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL); + if (sz >= 8 && ((sz % 8) == 0)) + { + if (byte_len % (sz / 8)) + SCM_MISC_ERROR ("byte length not a multiple of the unit size", SCM_EOL); + if (byte_len / (sz / 8) != rlen) + SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL); + } + else if (sz < 8) + { + /* byte_len ?= ceil (rlen * sz / 8) */ + if (byte_len != (rlen * sz + 7) / 8) + SCM_MISC_ERROR ("byte length and dimensions do not match", SCM_EOL); + } + else + /* an internal guile error, really */ + SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL); - memcpy (base, bytes, byte_len); + memcpy (elts, bytes, byte_len); if (1 == SCM_I_ARRAY_NDIM (ra) && 0 == SCM_I_ARRAY_BASE (ra)) if (s->ubnd < s->lbnd || (0 == s->lbnd && 1 == s->inc)) @@ -1087,22 +1099,6 @@ scm_i_read_array (SCM port, int c) static SCM -array_mark (SCM ptr) -{ - return SCM_I_ARRAY_V (ptr); -} - -static size_t -array_free (SCM ptr) -{ - scm_gc_free (SCM_I_ARRAY_MEM (ptr), - (sizeof (scm_i_t_array) - + SCM_I_ARRAY_NDIM (ptr) * sizeof (scm_t_array_dim)), - "array"); - return 0; -} - -static SCM array_handle_ref (scm_t_array_handle *h, size_t pos) { return scm_c_generalized_vector_ref (SCM_I_ARRAY_V (h->array), pos); @@ -1138,8 +1134,6 @@ void scm_init_arrays () { scm_i_tc16_array = scm_make_smob_type ("array", 0); - scm_set_smob_mark (scm_i_tc16_array, array_mark); - scm_set_smob_free (scm_i_tc16_array, array_free); scm_set_smob_print (scm_i_tc16_array, scm_i_print_array); scm_set_smob_equalp (scm_i_tc16_array, scm_array_equal_p); diff --git a/libguile/async.c b/libguile/async.c index 3e5a581c6..664264f3c 100644 --- a/libguile/async.c +++ b/libguile/async.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -22,6 +22,8 @@ # include <config.h> #endif +#define SCM_BUILDING_DEPRECATED_CODE + #include <signal.h> #include "libguile/_scm.h" #include "libguile/eval.h" @@ -470,6 +472,29 @@ scm_dynwind_unblock_asyncs () scm_dynwind_unwind_handler (increase_block, t, SCM_F_WIND_EXPLICITLY); } + +/* These are function variants of the same-named macros (uppercase) for use + outside of libguile. This is so that `SCM_I_CURRENT_THREAD', which may + reside in TLS, is not accessed from outside of libguile. It thus allows + libguile to be built with the "local-dynamic" TLS model. */ + +void +scm_critical_section_start (void) +{ + SCM_CRITICAL_SECTION_START; +} + +void +scm_critical_section_end (void) +{ + SCM_CRITICAL_SECTION_END; +} + +void +scm_async_tick (void) +{ + SCM_ASYNC_TICK; +} diff --git a/libguile/async.h b/libguile/async.h index 427d9b4c8..ceb2b960b 100644 --- a/libguile/async.h +++ b/libguile/async.h @@ -3,7 +3,7 @@ #ifndef SCM_ASYNC_H #define SCM_ASYNC_H -/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004, 2005, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2004, 2005, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -29,10 +29,6 @@ -#define scm_mask_ints (SCM_I_CURRENT_THREAD->block_asyncs != 0) - - - SCM_API void scm_async_click (void); SCM_API void scm_switch (void); SCM_API SCM scm_async (SCM thunk); @@ -60,32 +56,42 @@ void scm_dynwind_unblock_asyncs (void); the manual. */ -/* Defined in threads.c. scm_i_critical_section_level is only used - for error checking and will go away eventually. */ -extern scm_i_pthread_mutex_t scm_i_critical_section_mutex; -extern int scm_i_critical_section_level; +/* Defined in threads.c. */ +SCM_INTERNAL scm_i_pthread_mutex_t scm_i_critical_section_mutex; + +SCM_API void scm_critical_section_start (void); +SCM_API void scm_critical_section_end (void); -#define SCM_CRITICAL_SECTION_START \ - do { \ - scm_i_pthread_mutex_lock (&scm_i_critical_section_mutex);\ - SCM_I_CURRENT_THREAD->block_asyncs++; \ - scm_i_critical_section_level++; \ +#ifdef BUILDING_LIBGUILE + +# define SCM_CRITICAL_SECTION_START \ + do { \ + scm_i_pthread_mutex_lock (&scm_i_critical_section_mutex); \ + SCM_I_CURRENT_THREAD->block_asyncs++; \ + SCM_I_CURRENT_THREAD->critical_section_level++; \ } while (0) -#define SCM_CRITICAL_SECTION_END \ - do { \ - scm_i_critical_section_level--; \ - SCM_I_CURRENT_THREAD->block_asyncs--; \ +# define SCM_CRITICAL_SECTION_END \ + do { \ + SCM_I_CURRENT_THREAD->critical_section_level--; \ + SCM_I_CURRENT_THREAD->block_asyncs--; \ scm_i_pthread_mutex_unlock (&scm_i_critical_section_mutex); \ - scm_async_click (); \ + scm_async_click (); \ } while (0) +#else /* !BUILDING_LIBGUILE */ + +# define SCM_CRITICAL_SECTION_START scm_critical_section_start () +# define SCM_CRITICAL_SECTION_END scm_critical_section_end () + +#endif /* !BUILDING_LIBGUILE */ + SCM_INTERNAL void scm_init_async (void); #if (SCM_ENABLE_DEPRECATED == 1) -SCM_API SCM scm_system_async (SCM thunk); -SCM_API SCM scm_unmask_signals (void); -SCM_API SCM scm_mask_signals (void); +SCM_DEPRECATED SCM scm_system_async (SCM thunk); +SCM_DEPRECATED SCM scm_unmask_signals (void); +SCM_DEPRECATED SCM scm_mask_signals (void); #endif diff --git a/libguile/boehm-gc.h b/libguile/bdw-gc.h index 9a92d08b2..3adf99e66 100644 --- a/libguile/boehm-gc.h +++ b/libguile/bdw-gc.h @@ -1,21 +1,22 @@ -#ifndef SCM_BOEHM_GC_H -#define SCM_BOEHM_GC_H +#ifndef SCM_BDW_GC_H +#define SCM_BDW_GC_H /* Copyright (C) 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 3 of + * the License, or (at your option) any later version. * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of + * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA */ /* Correct header inclusion. */ @@ -61,4 +62,4 @@ typedef void *GC_PTR; : 0) -#endif /* SCM_BOEHM_GC_H */ +#endif /* SCM_BDW_GC_H */ diff --git a/libguile/bitvectors.c b/libguile/bitvectors.c index f1d8473d9..c3b6f3e4e 100644 --- a/libguile/bitvectors.c +++ b/libguile/bitvectors.c @@ -45,15 +45,6 @@ static scm_t_bits scm_tc16_bitvector; #define BITVECTOR_BITS(obj) ((scm_t_uint32 *)SCM_SMOB_DATA(obj)) #define BITVECTOR_LENGTH(obj) ((size_t)SCM_SMOB_DATA_2(obj)) -static size_t -bitvector_free (SCM vec) -{ - scm_gc_free (BITVECTOR_BITS (vec), - sizeof (scm_t_uint32) * ((BITVECTOR_LENGTH (vec)+31)/32), - "bitvector"); - return 0; -} - static int bitvector_print (SCM vec, SCM port, scm_print_state *pstate) { @@ -120,8 +111,8 @@ scm_c_make_bitvector (size_t len, SCM fill) scm_t_uint32 *bits; SCM res; - bits = scm_gc_malloc (sizeof (scm_t_uint32) * word_len, - "bitvector"); + bits = scm_gc_malloc_pointerless (sizeof (scm_t_uint32) * word_len, + "bitvector"); SCM_NEWSMOB2 (res, scm_tc16_bitvector, bits, len); if (!SCM_UNBNDP (fill)) @@ -896,7 +887,6 @@ void scm_init_bitvectors () { scm_tc16_bitvector = scm_make_smob_type ("bitvector", 0); - scm_set_smob_free (scm_tc16_bitvector, bitvector_free); scm_set_smob_print (scm_tc16_bitvector, bitvector_print); scm_set_smob_equalp (scm_tc16_bitvector, bitvector_equalp); diff --git a/libguile/boolean.c b/libguile/boolean.c index d79bf7979..d7091bbef 100644 --- a/libguile/boolean.c +++ b/libguile/boolean.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995, 1996, 2000, 2001, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995, 1996, 2000, 2001, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -29,15 +29,37 @@ #include "libguile/lang.h" #include "libguile/tags.h" +#include "verify.h" + +/* + * These compile-time tests verify the properties needed for the + * efficient test macros defined in boolean.h, which are defined in + * terms of the SCM_MATCHES_BITS_IN_COMMON macro. + * + * See the comments preceeding the definitions of SCM_BOOL_F and + * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information. + */ +verify (SCM_VALUES_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \ + (SCM_BOOL_F, SCM_BOOL_T)); +verify (SCM_VALUES_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \ + (SCM_ELISP_NIL, SCM_BOOL_F)); +verify (SCM_VALUES_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \ + (SCM_ELISP_NIL, SCM_EOL)); +verify (SCM_VALUES_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS \ + (SCM_ELISP_NIL, SCM_BOOL_F, SCM_BOOL_T, \ + SCM_XXX_ANOTHER_BOOLEAN_DONT_USE)); +verify (SCM_VALUES_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS \ + (SCM_ELISP_NIL, SCM_BOOL_F, SCM_EOL, \ + SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE)); SCM_DEFINE (scm_not, "not", 1, 0, 0, (SCM x), "Return @code{#t} iff @var{x} is @code{#f}, else return @code{#f}.") #define FUNC_NAME s_scm_not { - return scm_from_bool (scm_is_false (x) || SCM_NILP (x)); + return scm_from_bool (scm_is_false_or_nil (x)); } #undef FUNC_NAME @@ -47,19 +69,14 @@ SCM_DEFINE (scm_boolean_p, "boolean?", 1, 0, 0, "Return @code{#t} iff @var{obj} is either @code{#t} or @code{#f}.") #define FUNC_NAME s_scm_boolean_p { - return scm_from_bool (scm_is_bool (obj) || SCM_NILP (obj)); + return scm_from_bool (scm_is_bool_or_nil (obj)); } #undef FUNC_NAME int -scm_is_bool (SCM x) -{ - return scm_is_eq (x, SCM_BOOL_F) || scm_is_eq (x, SCM_BOOL_T); -} - -int scm_to_bool (SCM x) { + /* XXX Should this first test use scm_is_false_or_nil instead? */ if (scm_is_eq (x, SCM_BOOL_F)) return 0; else if (scm_is_eq (x, SCM_BOOL_T)) diff --git a/libguile/boolean.h b/libguile/boolean.h index 5a8379713..4c97a4933 100644 --- a/libguile/boolean.h +++ b/libguile/boolean.h @@ -3,7 +3,7 @@ #ifndef SCM_BOOLEAN_H #define SCM_BOOLEAN_H -/* Copyright (C) 1995,1996,2000, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,2000, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -31,16 +31,97 @@ * */ +/* + * Use these macros if it's important (for correctness) + * that %nil MUST be considered true + */ +#define scm_is_false_and_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F)) +#define scm_is_true_or_nil(x) (!scm_is_eq ((x), SCM_BOOL_F)) + +/* + * Use these macros if %nil will never be tested, + * for increased efficiency. + */ +#define scm_is_false_assume_not_nil(x) (scm_is_eq ((x), SCM_BOOL_F)) +#define scm_is_true_assume_not_nil(x) (!scm_is_eq ((x), SCM_BOOL_F)) + +/* + * See the comments preceeding the definitions of SCM_BOOL_F and + * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information on + * how the following macro works. + */ +#if SCM_ENABLE_ELISP +# define scm_is_false_or_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_BOOL_F)) +#else +# define scm_is_false_or_nil(x) (scm_is_false_assume_not_nil (x)) +#endif +#define scm_is_true_and_not_nil(x) (!scm_is_false_or_nil (x)) -#define scm_is_false(x) scm_is_eq ((x), SCM_BOOL_F) -#define scm_is_true(x) !scm_is_false (x) +/* XXX Should these macros treat %nil as false by default? */ +#define scm_is_false(x) (scm_is_false_and_not_nil (x)) +#define scm_is_true(x) (!scm_is_false (x)) + +/* + * Since we know SCM_BOOL_F and SCM_BOOL_T differ by exactly one bit, + * and that SCM_BOOL_F and SCM_ELISP_NIL differ by exactly one bit, + * and that they of course can't be the same bit (or else SCM_BOOL_T + * and SCM_ELISP_NIL be would equal), it follows that SCM_BOOL_T and + * SCM_ELISP_NIL differ by exactly two bits, and these are the bits + * which will be ignored by SCM_MATCHES_BITS_IN_COMMON below. + * + * See the comments preceeding the definitions of SCM_BOOL_F and + * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information. + * + * If SCM_ENABLE_ELISP is true, then scm_is_bool_or_nil(x) + * returns 1 if and only if x is one of the following: SCM_BOOL_F, + * SCM_BOOL_T, SCM_ELISP_NIL, or SCM_XXX_ANOTHER_BOOLEAN_DONT_USE. + * Otherwise, it returns 0. + */ +#if SCM_ENABLE_ELISP +# define scm_is_bool_or_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_T, SCM_ELISP_NIL)) +#else +# define scm_is_bool_or_nil(x) (scm_is_bool_and_not_nil (x)) +#endif + +#define scm_is_bool_and_not_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_BOOL_T)) + +/* XXX Should scm_is_bool treat %nil as a boolean? */ +#define scm_is_bool(x) (scm_is_bool_and_not_nil (x)) -SCM_API int scm_is_bool (SCM x); #define scm_from_bool(x) ((x) ? SCM_BOOL_T : SCM_BOOL_F) SCM_API int scm_to_bool (SCM x); +/* + * The following macros efficiently implement boolean truth testing as + * expected by most lisps, which treat '() aka SCM_EOL as false. + * + * Since we know SCM_ELISP_NIL and SCM_BOOL_F differ by exactly one + * bit, and that SCM_ELISP_NIL and SCM_EOL differ by exactly one bit, + * and that they of course can't be the same bit (or else SCM_BOOL_F + * and SCM_EOL be would equal), it follows that SCM_BOOL_F and SCM_EOL + * differ by exactly two bits, and these are the bits which will be + * ignored by SCM_MATCHES_BITS_IN_COMMON below. + * + * See the comments preceeding the definitions of SCM_BOOL_F and + * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information. + * + * scm_is_lisp_false(x) returns 1 if and only if x is one of the + * following: SCM_BOOL_F, SCM_ELISP_NIL, SCM_EOL or + * SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE. Otherwise, it returns 0. + */ +#if SCM_ENABLE_ELISP +# define scm_is_lisp_false(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_F, SCM_EOL)) +# define scm_is_lisp_true(x) (!scm_is_lisp_false(x)) +#endif + + + SCM_API SCM scm_not (SCM x); SCM_API SCM scm_boolean_p (SCM obj); diff --git a/libguile/bytevectors.c b/libguile/bytevectors.c index 4246f0111..ce5fa962f 100644 --- a/libguile/bytevectors.c +++ b/libguile/bytevectors.c @@ -22,6 +22,7 @@ #endif #include <alloca.h> +#include <assert.h> #include <gmp.h> @@ -39,6 +40,7 @@ #include <byteswap.h> #include <striconveh.h> #include <uniconv.h> +#include <unistr.h> #ifdef HAVE_LIMITS_H # include <limits.h> @@ -587,23 +589,31 @@ SCM_DEFINE (scm_uniform_array_to_bytevector, "uniform-array->bytevector", #define FUNC_NAME s_scm_uniform_array_to_bytevector { SCM contents, ret; - size_t len; + size_t len, sz, byte_len; scm_t_array_handle h; - const void *base; - size_t sz; + const void *elts; contents = scm_array_contents (array, SCM_BOOL_T); if (scm_is_false (contents)) scm_wrong_type_arg_msg (FUNC_NAME, 0, array, "uniform contiguous array"); scm_array_get_handle (contents, &h); + assert (h.base == 0); - base = scm_array_handle_uniform_elements (&h); + elts = h.elements; len = h.dims->inc * (h.dims->ubnd - h.dims->lbnd + 1); - sz = scm_array_handle_uniform_element_size (&h); + sz = scm_array_handle_uniform_element_bit_size (&h); + if (sz >= 8 && ((sz % 8) == 0)) + byte_len = len * (sz / 8); + else if (sz < 8) + /* byte_len = ceil (len * sz / 8) */ + byte_len = (len * sz + 7) / 8; + else + /* an internal guile error, really */ + SCM_MISC_ERROR ("uniform elements larger than 8 bits must fill whole bytes", SCM_EOL); - ret = make_bytevector (len * sz, SCM_ARRAY_ELEMENT_TYPE_VU8); - memcpy (SCM_BYTEVECTOR_CONTENTS (ret), base, len * sz); + ret = make_bytevector (byte_len, SCM_ARRAY_ELEMENT_TYPE_VU8); + memcpy (SCM_BYTEVECTOR_CONTENTS (ret), elts, byte_len); scm_array_handle_release (&h); @@ -1862,58 +1872,50 @@ utf_encoding_name (char *name, size_t utf_width, SCM endianness) #define MAX_UTF_ENCODING_NAME_LEN 16 /* Produce the body of a `string->utf' function. */ -#define STRING_TO_UTF(_utf_width) \ - SCM utf; \ - int err; \ - char *c_str; \ - char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \ - char *c_utf = NULL, *c_locale; \ - size_t c_strlen, c_raw_strlen, c_utf_len = 0; \ - \ - SCM_VALIDATE_STRING (1, str); \ - if (endianness == SCM_UNDEFINED) \ - endianness = scm_sym_big; \ - else \ - SCM_VALIDATE_SYMBOL (2, endianness); \ - \ - c_strlen = scm_c_string_length (str); \ - c_raw_strlen = c_strlen * ((_utf_width) / 8); \ - do \ - { \ - c_str = (char *) alloca (c_raw_strlen + 1); \ - c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); \ - } \ - while (c_raw_strlen > c_strlen); \ - c_str[c_raw_strlen] = '\0'; \ - \ - utf_encoding_name (c_utf_name, (_utf_width), endianness); \ - \ - c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \ - strcpy (c_locale, locale_charset ()); \ - \ - err = mem_iconveh (c_str, c_raw_strlen, \ - c_locale, c_utf_name, \ - iconveh_question_mark, NULL, \ - &c_utf, &c_utf_len); \ - if (SCM_UNLIKELY (err)) \ - scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \ - scm_list_1 (str), err); \ - else \ - { \ - /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot \ - use `scm_c_take_bytevector ()'. */ \ - scm_dynwind_begin (0); \ - scm_dynwind_free (c_utf); \ - \ - utf = make_bytevector (c_utf_len, \ - SCM_ARRAY_ELEMENT_TYPE_VU8); \ - memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, \ - c_utf_len); \ - \ - scm_dynwind_end (); \ - } \ - \ - return (utf); +#define STRING_TO_UTF(_utf_width) \ + SCM utf; \ + int err; \ + char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \ + char *c_utf = NULL; \ + size_t c_strlen, c_utf_len = 0; \ + \ + SCM_VALIDATE_STRING (1, str); \ + if (endianness == SCM_UNDEFINED) \ + endianness = scm_sym_big; \ + else \ + SCM_VALIDATE_SYMBOL (2, endianness); \ + \ + utf_encoding_name (c_utf_name, (_utf_width), endianness); \ + \ + c_strlen = scm_i_string_length (str); \ + if (scm_i_is_narrow_string (str)) \ + { \ + err = mem_iconveh (scm_i_string_chars (str), c_strlen, \ + "ISO-8859-1", c_utf_name, \ + iconveh_question_mark, NULL, \ + &c_utf, &c_utf_len); \ + if (SCM_UNLIKELY (err)) \ + scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \ + scm_list_1 (str), err); \ + } \ + else \ + { \ + const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); \ + c_utf = u32_conv_to_encoding (c_utf_name, \ + iconveh_question_mark, \ + (scm_t_uint32 *) wbuf, \ + c_strlen, NULL, NULL, &c_utf_len); \ + if (SCM_UNLIKELY (c_utf == NULL)) \ + scm_syserror_msg (FUNC_NAME, "failed to convert string: ~A", \ + scm_list_1 (str), errno); \ + } \ + scm_dynwind_begin (0); \ + scm_dynwind_free (c_utf); \ + utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); \ + memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); \ + scm_dynwind_end (); \ + \ + return (utf); @@ -1925,36 +1927,30 @@ SCM_DEFINE (scm_string_to_utf8, "string->utf8", #define FUNC_NAME s_scm_string_to_utf8 { SCM utf; - char *c_str; uint8_t *c_utf; - size_t c_strlen, c_raw_strlen; + size_t c_strlen, c_utf_len = 0; SCM_VALIDATE_STRING (1, str); - c_strlen = scm_c_string_length (str); - c_raw_strlen = c_strlen; - do + c_strlen = scm_i_string_length (str); + if (scm_i_is_narrow_string (str)) + c_utf = u8_conv_from_encoding ("ISO-8859-1", iconveh_question_mark, + scm_i_string_chars (str), c_strlen, + NULL, NULL, &c_utf_len); + else { - c_str = (char *) alloca (c_raw_strlen + 1); - c_raw_strlen = scm_to_locale_stringbuf (str, c_str, c_strlen); + const scm_t_wchar *wbuf = scm_i_string_wide_chars (str); + c_utf = u32_to_u8 ((const uint32_t *) wbuf, c_strlen, NULL, &c_utf_len); } - while (c_raw_strlen > c_strlen); - c_str[c_raw_strlen] = '\0'; - - c_utf = u8_strconv_from_locale (c_str); if (SCM_UNLIKELY (c_utf == NULL)) scm_syserror (FUNC_NAME); else { - /* C_UTF is null-terminated. It is malloc(3)-allocated, so we cannot - use `scm_c_take_bytevector ()'. */ scm_dynwind_begin (0); scm_dynwind_free (c_utf); - utf = make_bytevector (UTF_STRLEN (8, c_utf), - SCM_ARRAY_ELEMENT_TYPE_VU8); - memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, - UTF_STRLEN (8, c_utf)); + utf = make_bytevector (c_utf_len, SCM_ARRAY_ELEMENT_TYPE_VU8); + memcpy (SCM_BYTEVECTOR_CONTENTS (utf), c_utf, c_utf_len); scm_dynwind_end (); } @@ -1991,10 +1987,10 @@ SCM_DEFINE (scm_string_to_utf32, "string->utf32", #define UTF_TO_STRING(_utf_width) \ SCM str = SCM_BOOL_F; \ int err; \ - char *c_str = NULL, *c_locale; \ + char *c_str = NULL; \ char c_utf_name[MAX_UTF_ENCODING_NAME_LEN]; \ - const char *c_utf; \ - size_t c_strlen = 0, c_utf_len; \ + char *c_utf; \ + size_t c_strlen = 0, c_utf_len = 0; \ \ SCM_VALIDATE_BYTEVECTOR (1, utf); \ if (endianness == SCM_UNDEFINED) \ @@ -2006,20 +2002,19 @@ SCM_DEFINE (scm_string_to_utf32, "string->utf32", c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); \ utf_encoding_name (c_utf_name, (_utf_width), endianness); \ \ - c_locale = (char *) alloca (strlen (locale_charset ()) + 1); \ - strcpy (c_locale, locale_charset ()); \ - \ err = mem_iconveh (c_utf, c_utf_len, \ - c_utf_name, c_locale, \ + c_utf_name, "UTF-8", \ iconveh_question_mark, NULL, \ &c_str, &c_strlen); \ if (SCM_UNLIKELY (err)) \ scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", \ scm_list_1 (utf), err); \ else \ - /* C_STR is null-terminated. */ \ - str = scm_take_locale_stringn (c_str, c_strlen); \ - \ + { \ + str = scm_from_stringn (c_str, c_strlen, "UTF-8", \ + SCM_FAILED_CONVERSION_ERROR); \ + free (c_str); \ + } \ return (str); @@ -2031,29 +2026,15 @@ SCM_DEFINE (scm_utf8_to_string, "utf8->string", #define FUNC_NAME s_scm_utf8_to_string { SCM str; - int err; - char *c_str = NULL, *c_locale; const char *c_utf; - size_t c_utf_len, c_strlen = 0; + size_t c_utf_len = 0; SCM_VALIDATE_BYTEVECTOR (1, utf); c_utf_len = SCM_BYTEVECTOR_LENGTH (utf); - - c_locale = (char *) alloca (strlen (locale_charset ()) + 1); - strcpy (c_locale, locale_charset ()); - c_utf = (char *) SCM_BYTEVECTOR_CONTENTS (utf); - err = mem_iconveh (c_utf, c_utf_len, - "UTF-8", c_locale, - iconveh_question_mark, NULL, - &c_str, &c_strlen); - if (SCM_UNLIKELY (err)) - scm_syserror_msg (FUNC_NAME, "failed to convert to string: ~A", - scm_list_1 (utf), err); - else - /* C_STR is null-terminated. */ - str = scm_take_locale_stringn (c_str, c_strlen); + str = scm_from_stringn (c_utf, c_utf_len, "UTF-8", + SCM_FAILED_CONVERSION_ERROR); return (str); } @@ -2081,7 +2062,6 @@ SCM_DEFINE (scm_utf32_to_string, "utf32->string", } #undef FUNC_NAME - /* Bytevectors as generalized vectors & arrays. */ diff --git a/libguile/chars.c b/libguile/chars.c index c7cb09c47..59ac6f412 100644 --- a/libguile/chars.c +++ b/libguile/chars.c @@ -45,7 +45,8 @@ SCM_DEFINE (scm_char_p, "char?", 1, 0, 0, SCM_DEFINE1 (scm_char_eq_p, "char=?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}.") + "Return @code{#t} if the Unicode code point of @var{x} is equal to the\n" + "code point of @var{y}, else @code{#f}.\n") #define FUNC_NAME s_scm_char_eq_p { SCM_VALIDATE_CHAR (1, x); @@ -57,8 +58,8 @@ SCM_DEFINE1 (scm_char_eq_p, "char=?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff @var{x} is less than @var{y} in the Unicode sequence,\n" - "else @code{#f}.") + "Return @code{#t} iff the code point of @var{x} is less than the code\n" + "point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_less_p { SCM_VALIDATE_CHAR (1, x); @@ -69,8 +70,8 @@ SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n" - "Unicode sequence, else @code{#f}.") + "Return @code{#t} if the Unicode code point of @var{x} is less than or\n" + "equal to the code point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_leq_p { SCM_VALIDATE_CHAR (1, x); @@ -81,8 +82,8 @@ SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff @var{x} is greater than @var{y} in the Unicode\n" - "sequence, else @code{#f}.") + "Return @code{#t} if the Unicode code point of @var{x} is greater than\n" + "the code point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_gr_p { SCM_VALIDATE_CHAR (1, x); @@ -93,8 +94,8 @@ SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n" - "Unicode sequence, else @code{#f}.") + "Return @code{#t} if the Unicode code point of @var{x} is greater than\n" + "or equal to the code point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_geq_p { SCM_VALIDATE_CHAR (1, x); @@ -103,10 +104,17 @@ SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr, } #undef FUNC_NAME +/* FIXME?: R6RS specifies that these comparisons are case-folded. + This is the same thing as comparing the uppercase characters in + practice, but, not in theory. Unicode has table containing their + definition of case-folded character mappings. A more correct + implementation would be to use that table and make a char-foldcase + function. */ + SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff @var{x} is the same character as @var{y} ignoring\n" - "case, else @code{#f}. Case is locale free and not context sensitive.") + "Return @code{#t} if the case-folded Unicode code point of @var{x} is\n" + "the same as the case-folded code point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_ci_eq_p { SCM_VALIDATE_CHAR (1, x); @@ -117,9 +125,8 @@ SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff the Unicode uppercase form of @var{x} is less\n" - "than the Unicode uppercase form @var{y} in the Unicode sequence,\n" - "else @code{#f}.") + "Return @code{#t} if the case-folded Unicode code point of @var{x} is\n" + "less than the case-folded code point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_ci_less_p { SCM_VALIDATE_CHAR (1, x); @@ -130,9 +137,9 @@ SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff the Unicode uppercase form of @var{x} is less\n" - "than or equal to the Unicode uppercase form of @var{y} in the\n" - "Unicode sequence, else @code{#f}.") + "Return @code{#t} iff the case-folded Unicodd code point of @var{x} is\n" + "less than or equal to the case-folded code point of @var{y}, else\n" + "@code{#f}") #define FUNC_NAME s_scm_char_ci_leq_p { SCM_VALIDATE_CHAR (1, x); @@ -143,9 +150,8 @@ SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff the Unicode uppercase form of @var{x} is greater\n" - "than the Unicode uppercase form of @var{y} in the Unicode\n" - "sequence, else @code{#f}.") + "Return @code{#t} iff the case-folded code point of @var{x} is greater\n" + "than the case-folded code point of @var{y}, else @code{#f}.") #define FUNC_NAME s_scm_char_ci_gr_p { SCM_VALIDATE_CHAR (1, x); @@ -156,9 +162,9 @@ SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr, SCM_DEFINE1 (scm_char_ci_geq_p, "char-ci>=?", scm_tc7_rpsubr, (SCM x, SCM y), - "Return @code{#t} iff the Unicode uppercase form of @var{x} is greater\n" - "than or equal to the Unicode uppercase form of @var{y} in the\n" - "Unicode sequence, else @code{#f}.") + "Return @code{#t} iff the case-folded Unicode code point of @var{x} is\n" + "greater than or equal to the case-folded code point of @var{y}, else\n" + "@code{#f}.") #define FUNC_NAME s_scm_char_ci_geq_p { SCM_VALIDATE_CHAR (1, x); @@ -196,7 +202,6 @@ SCM_DEFINE (scm_char_whitespace_p, "char-whitespace?", 1, 0, 0, #undef FUNC_NAME - SCM_DEFINE (scm_char_upper_case_p, "char-upper-case?", 1, 0, 0, (SCM chr), "Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.\n") @@ -217,10 +222,10 @@ SCM_DEFINE (scm_char_lower_case_p, "char-lower-case?", 1, 0, 0, #undef FUNC_NAME - SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0, (SCM chr), - "Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}.\n") + "Return @code{#t} iff @var{chr} is either uppercase or lowercase, else\n" + "@code{#f}.\n") #define FUNC_NAME s_scm_char_is_both_p { if (scm_is_true (scm_char_set_contains_p (scm_char_set_lower_case, chr))) @@ -230,12 +235,9 @@ SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0, #undef FUNC_NAME - - SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0, (SCM chr), - "Return the number corresponding to ordinal position of @var{chr} in the\n" - "ASCII sequence.") + "Return the Unicode code point of @var{chr}.") #define FUNC_NAME s_scm_char_to_integer { SCM_VALIDATE_CHAR (1, chr); @@ -244,10 +246,12 @@ SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0, #undef FUNC_NAME - SCM_DEFINE (scm_integer_to_char, "integer->char", 1, 0, 0, (SCM n), - "Return the character at position @var{n} in the ASCII sequence.") + "Return the character that has Unicode code point @var{n}. The integer\n" + "@var{n} must be a valid code point. Valid code points are in the\n" + "ranges 0 to @code{#xD7FF} inclusive or @code{#xE000} to\n" + "@code{#x10FFFF} inclusive.") #define FUNC_NAME s_scm_integer_to_char { scm_t_wchar cn; diff --git a/libguile/chars.h b/libguile/chars.h index 85b16739a..04eb9f09f 100644 --- a/libguile/chars.h +++ b/libguile/chars.h @@ -47,10 +47,15 @@ typedef scm_t_int32 scm_t_wchar; ? SCM_MAKE_ITAG8 ((scm_t_bits) (unsigned char) (x), scm_tc8_char) \ : SCM_MAKE_ITAG8 ((scm_t_bits) (x), scm_tc8_char)) +#define SCM_CODEPOINT_DOTTED_CIRCLE (0x25cc) +#define SCM_CODEPOINT_SURROGATE_START (0xd800) +#define SCM_CODEPOINT_SURROGATE_END (0xdfff) #define SCM_CODEPOINT_MAX (0x10ffff) #define SCM_IS_UNICODE_CHAR(c) \ - ((scm_t_wchar) (c) <= 0xd7ff \ - || ((scm_t_wchar) (c) >= 0xe000 && (scm_t_wchar) (c) <= SCM_CODEPOINT_MAX)) + (((scm_t_wchar) (c) >= 0 \ + && (scm_t_wchar) (c) < SCM_CODEPOINT_SURROGATE_START) \ + || ((scm_t_wchar) (c) > SCM_CODEPOINT_SURROGATE_END \ + && (scm_t_wchar) (c) <= SCM_CODEPOINT_MAX)) diff --git a/libguile/continuations.c b/libguile/continuations.c index aa1fb334e..a0e2f6d4f 100644 --- a/libguile/continuations.c +++ b/libguile/continuations.c @@ -225,7 +225,7 @@ scm_dynthrow (SCM cont, SCM val) SCM_STACKITEM *dst = thread->continuation_base; SCM_STACKITEM stack_top_element; - if (scm_i_critical_section_level) + if (thread->critical_section_level) { fprintf (stderr, "continuation invoked from within critical section.\n"); abort (); diff --git a/libguile/deprecated.c b/libguile/deprecated.c index d0669969c..df7ba4dc6 100644 --- a/libguile/deprecated.c +++ b/libguile/deprecated.c @@ -24,6 +24,8 @@ # include <config.h> #endif +#define SCM_BUILDING_DEPRECATED_CODE + #include "libguile/_scm.h" #include "libguile/async.h" #include "libguile/deprecated.h" @@ -633,6 +635,21 @@ scm_set_smob_mfpe (long tc, if (equalp) scm_set_smob_equalp (tc, equalp); } +size_t +scm_smob_free (SCM obj) +{ + long n = SCM_SMOBNUM (obj); + + scm_c_issue_deprecation_warning + ("`scm_smob_free' is deprecated. " + "It is no longer needed."); + + if (scm_smobs[n].size > 0) + scm_gc_free ((void *) SCM_CELL_WORD_1 (obj), + scm_smobs[n].size, SCM_SMOBNAME (n)); + return 0; +} + SCM scm_read_0str (char *expr) { @@ -1450,6 +1467,14 @@ scm_i_defer_ints_etc () "Use a mutex instead if appropriate."); } +int +scm_i_mask_ints (void) +{ + scm_c_issue_deprecation_warning ("`scm_mask_ints' is deprecated."); + return (SCM_I_CURRENT_THREAD->block_asyncs != 0); +} + + SCM scm_guard (SCM guardian, SCM obj, int throw_p) { diff --git a/libguile/deprecated.h b/libguile/deprecated.h index f428f7de8..5680d09e5 100644 --- a/libguile/deprecated.h +++ b/libguile/deprecated.h @@ -67,13 +67,13 @@ SCM_API char *scm_isymnames[]; /* From eval.h: Error messages of the evaluator. These were deprecated in * guile 1.7.0 on 2003-06-02. */ -SCM_API const char scm_s_expression[]; -SCM_API const char scm_s_test[]; -SCM_API const char scm_s_body[]; -SCM_API const char scm_s_bindings[]; -SCM_API const char scm_s_variable[]; -SCM_API const char scm_s_clauses[]; -SCM_API const char scm_s_formals[]; +SCM_DEPRECATED const char scm_s_expression[]; +SCM_DEPRECATED const char scm_s_test[]; +SCM_DEPRECATED const char scm_s_body[]; +SCM_DEPRECATED const char scm_s_bindings[]; +SCM_DEPRECATED const char scm_s_variable[]; +SCM_DEPRECATED const char scm_s_clauses[]; +SCM_DEPRECATED const char scm_s_formals[]; /* From eval.h: Helper macros for evaluation and application. These were @@ -97,18 +97,18 @@ SCM_API const char scm_s_formals[]; #define scm_sizet size_t -SCM_API SCM scm_wta (SCM arg, const char *pos, const char *s_subr); +SCM_DEPRECATED SCM scm_wta (SCM arg, const char *pos, const char *s_subr); #define SCM_WNA 8 #define SCM_OUTOFRANGE 10 #define SCM_NALLOC 11 -SCM_API void scm_register_module_xxx (char *module_name, void *init_func); -SCM_API SCM scm_registered_modules (void); -SCM_API SCM scm_clear_registered_modules (void); +SCM_DEPRECATED void scm_register_module_xxx (char *module_name, void *init_func); +SCM_DEPRECATED SCM scm_registered_modules (void); +SCM_DEPRECATED SCM scm_clear_registered_modules (void); -SCM_API SCM scm_protect_object (SCM obj); -SCM_API SCM scm_unprotect_object (SCM obj); +SCM_DEPRECATED SCM scm_protect_object (SCM obj); +SCM_DEPRECATED SCM scm_unprotect_object (SCM obj); #define SCM_SETAND_CAR(x, y) \ (SCM_SETCAR ((x), SCM_PACK (SCM_UNPACK (SCM_CAR (x)) & (y)))) @@ -120,23 +120,20 @@ SCM_API SCM scm_unprotect_object (SCM obj); (SCM_SETCDR ((x), SCM_PACK (SCM_UNPACK (SCM_CDR (x)) | (y)))) #define SCM_FREEP(x) (0) #define SCM_NFREEP(x) (1) -#define SCM_GC8MARKP(x) SCM_GC_MARK_P (x) -#define SCM_SETGC8MARK(x) SCM_SET_GC_MARK (x) -#define SCM_CLRGC8MARK(x) SCM_CLEAR_GC_MARK (x) #define SCM_GCTYP16(x) SCM_TYP16 (x) #define SCM_GCCDR(x) SCM_CDR (x) -SCM_API void scm_remember (SCM * ptr); +SCM_DEPRECATED void scm_remember (SCM * ptr); -SCM_API SCM scm_the_root_module (void); -SCM_API SCM scm_make_module (SCM name); -SCM_API SCM scm_ensure_user_module (SCM name); -SCM_API SCM scm_load_scheme_module (SCM name); +SCM_DEPRECATED SCM scm_the_root_module (void); +SCM_DEPRECATED SCM scm_make_module (SCM name); +SCM_DEPRECATED SCM scm_ensure_user_module (SCM name); +SCM_DEPRECATED SCM scm_load_scheme_module (SCM name); #define scm_port scm_t_port #define scm_ptob_descriptor scm_t_ptob_descriptor #define scm_port_rw_active scm_t_port_rw_active -SCM_API SCM scm_close_all_ports_except (SCM ports); +SCM_DEPRECATED SCM scm_close_all_ports_except (SCM ports); #define scm_rstate scm_t_rstate #define scm_rng scm_t_rng @@ -148,25 +145,27 @@ SCM_API SCM scm_close_all_ports_except (SCM ports); #define scm_tc7_msymbol scm_tc7_symbol #define scm_tcs_symbols scm_tc7_symbol -SCM_API SCM scm_makstr (size_t len, int); -SCM_API SCM scm_makfromstr (const char *src, size_t len, int); +SCM_DEPRECATED SCM scm_makstr (size_t len, int); +SCM_DEPRECATED SCM scm_makfromstr (const char *src, size_t len, int); -SCM_API SCM scm_variable_set_name_hint (SCM var, SCM hint); -SCM_API SCM scm_builtin_variable (SCM name); +SCM_DEPRECATED SCM scm_variable_set_name_hint (SCM var, SCM hint); +SCM_DEPRECATED SCM scm_builtin_variable (SCM name); -SCM_API SCM scm_internal_with_fluids (SCM fluids, SCM vals, - SCM (*cproc)(void *), void *cdata); +SCM_DEPRECATED SCM scm_internal_with_fluids (SCM fluids, SCM vals, + SCM (*cproc)(void *), + void *cdata); -SCM_API SCM scm_make_gsubr (const char *name, int req, int opt, int rst, - SCM (*fcn)()); -SCM_API SCM scm_make_gsubr_with_generic (const char *name, - int req, - int opt, - int rst, - SCM (*fcn)(), - SCM *gf); +SCM_DEPRECATED SCM scm_make_gsubr (const char *name, + int req, int opt, int rst, + SCM (*fcn)()); +SCM_DEPRECATED SCM scm_make_gsubr_with_generic (const char *name, + int req, + int opt, + int rst, + SCM (*fcn)(), + SCM *gf); -SCM_API SCM scm_create_hook (const char* name, int n_args); +SCM_DEPRECATED SCM scm_create_hook (const char* name, int n_args); #define SCM_LIST0 SCM_EOL #define SCM_LIST1(e0) scm_cons ((e0), SCM_EOL) @@ -188,76 +187,78 @@ SCM_API SCM scm_create_hook (const char* name, int n_args); #define scm_listify scm_list_n -SCM_API SCM scm_sloppy_memq (SCM x, SCM lst); -SCM_API SCM scm_sloppy_memv (SCM x, SCM lst); -SCM_API SCM scm_sloppy_member (SCM x, SCM lst); +SCM_DEPRECATED SCM scm_sloppy_memq (SCM x, SCM lst); +SCM_DEPRECATED SCM scm_sloppy_memv (SCM x, SCM lst); +SCM_DEPRECATED SCM scm_sloppy_member (SCM x, SCM lst); -SCM_API SCM scm_read_and_eval_x (SCM port); +SCM_DEPRECATED SCM scm_read_and_eval_x (SCM port); #define scm_subr_entry scm_t_subr_entry #define SCM_SUBR_DOC(x) SCM_BOOL_F -SCM_API SCM scm_make_subr (const char *name, int type, SCM (*fcn) ()); -SCM_API SCM scm_make_subr_with_generic (const char *name, - int type, - SCM (*fcn) (), - SCM *gf); -SCM_API SCM scm_make_subr_opt (const char *name, - int type, - SCM (*fcn) (), - int set); - -SCM_API SCM scm_call_catching_errors (SCM (*thunk)(), SCM (*err_filter)(), - void * closure); - -SCM_API long scm_make_smob_type_mfpe (char *name, size_t size, - SCM (*mark) (SCM), - size_t (*free) (SCM), - int (*print) (SCM, SCM, - scm_print_state*), - SCM (*equalp) (SCM, SCM)); - -SCM_API void scm_set_smob_mfpe (long tc, - SCM (*mark) (SCM), - size_t (*free) (SCM), - int (*print) (SCM, SCM, scm_print_state*), - SCM (*equalp) (SCM, SCM)); - -SCM_API SCM scm_strprint_obj (SCM obj); -SCM_API SCM scm_read_0str (char *expr); -SCM_API SCM scm_eval_0str (const char *expr); - -SCM_API char *scm_i_object_chars (SCM); +SCM_DEPRECATED SCM scm_make_subr (const char *name, int type, SCM (*fcn) ()); +SCM_DEPRECATED SCM scm_make_subr_with_generic (const char *name, + int type, + SCM (*fcn) (), + SCM *gf); +SCM_DEPRECATED SCM scm_make_subr_opt (const char *name, + int type, + SCM (*fcn) (), + int set); + +SCM_DEPRECATED SCM scm_call_catching_errors (SCM (*thunk)(), SCM (*err_filter)(), + void * closure); + +SCM_DEPRECATED long scm_make_smob_type_mfpe (char *name, size_t size, + SCM (*mark) (SCM), + size_t (*free) (SCM), + int (*print) (SCM, SCM, + scm_print_state*), + SCM (*equalp) (SCM, SCM)); + +SCM_DEPRECATED void scm_set_smob_mfpe (long tc, + SCM (*mark) (SCM), + size_t (*free) (SCM), + int (*print) (SCM, SCM, scm_print_state*), + SCM (*equalp) (SCM, SCM)); + +SCM_DEPRECATED size_t scm_smob_free (SCM obj); + +SCM_DEPRECATED SCM scm_strprint_obj (SCM obj); +SCM_DEPRECATED SCM scm_read_0str (char *expr); +SCM_DEPRECATED SCM scm_eval_0str (const char *expr); + +SCM_DEPRECATED char *scm_i_object_chars (SCM); #define SCM_CHARS(x) scm_i_object_chars(x) #define SCM_UCHARS(x) ((unsigned char *)SCM_CHARS(x)) -SCM_API long scm_i_object_length (SCM); +SCM_DEPRECATED long scm_i_object_length (SCM); #define SCM_LENGTH(x) scm_i_object_length(x) #define scm_strhash(str, len, n) (scm_string_hash ((str), (len)) % (n)) -SCM_API SCM scm_sym2ovcell_soft (SCM sym, SCM obarray); -SCM_API SCM scm_sym2ovcell (SCM sym, SCM obarray); -SCM_API SCM scm_intern_obarray_soft (const char *name, size_t len, +SCM_DEPRECATED SCM scm_sym2ovcell_soft (SCM sym, SCM obarray); +SCM_DEPRECATED SCM scm_sym2ovcell (SCM sym, SCM obarray); +SCM_DEPRECATED SCM scm_intern_obarray_soft (const char *name, size_t len, SCM obarray, unsigned int softness); -SCM_API SCM scm_intern_obarray (const char *name, size_t len, SCM obarray); -SCM_API SCM scm_symbol_value0 (const char *name); +SCM_DEPRECATED SCM scm_intern_obarray (const char *name, size_t len, SCM obarray); +SCM_DEPRECATED SCM scm_symbol_value0 (const char *name); -SCM_API SCM scm_string_to_obarray_symbol (SCM o, SCM s, SCM softp); -SCM_API SCM scm_intern_symbol (SCM o, SCM s); -SCM_API SCM scm_unintern_symbol (SCM o, SCM s); -SCM_API SCM scm_symbol_binding (SCM o, SCM s); +SCM_DEPRECATED SCM scm_string_to_obarray_symbol (SCM o, SCM s, SCM softp); +SCM_DEPRECATED SCM scm_intern_symbol (SCM o, SCM s); +SCM_DEPRECATED SCM scm_unintern_symbol (SCM o, SCM s); +SCM_DEPRECATED SCM scm_symbol_binding (SCM o, SCM s); #if 0 /* This name has been reused for real uninterned symbols. */ -SCM_API SCM scm_symbol_interned_p (SCM o, SCM s); +SCM_DEPRECATED SCM scm_symbol_interned_p (SCM o, SCM s); #endif -SCM_API SCM scm_symbol_bound_p (SCM o, SCM s); -SCM_API SCM scm_symbol_set_x (SCM o, SCM s, SCM v); +SCM_DEPRECATED SCM scm_symbol_bound_p (SCM o, SCM s); +SCM_DEPRECATED SCM scm_symbol_set_x (SCM o, SCM s, SCM v); -SCM_API SCM scm_gentemp (SCM prefix, SCM obarray); +SCM_DEPRECATED SCM scm_gentemp (SCM prefix, SCM obarray); #define SCM_OPDIRP(x) (SCM_DIRP (x) && (SCM_DIR_OPEN_P (x))) #define scm_fport scm_t_fport @@ -310,9 +311,9 @@ SCM_API SCM scm_gentemp (SCM prefix, SCM obarray); /* Users shouldn't know about INUMs. */ -SCM_API SCM scm_i_makinum (scm_t_signed_bits val); -SCM_API int scm_i_inump (SCM obj); -SCM_API scm_t_signed_bits scm_i_inum (SCM obj); +SCM_DEPRECATED SCM scm_i_makinum (scm_t_signed_bits val); +SCM_DEPRECATED int scm_i_inump (SCM obj); +SCM_DEPRECATED scm_t_signed_bits scm_i_inum (SCM obj); #define SCM_MAKINUM(x) scm_i_makinum(x) #define SCM_INUM(x) scm_i_inum(x) @@ -405,7 +406,7 @@ SCM_API scm_t_signed_bits scm_i_inum (SCM obj); copies the complete contents of OBJ, and sets *LENP to the length of the scheme string (if LENP is non-null). */ -SCM_API char *scm_c_string2str (SCM obj, char *str, size_t *lenp); +SCM_DEPRECATED char *scm_c_string2str (SCM obj, char *str, size_t *lenp); /* XXX - buggy interface, you don't know how many bytes have been copied. @@ -417,15 +418,15 @@ SCM_API char *scm_c_string2str (SCM obj, char *str, size_t *lenp); region to fit the string. If truncation occurs, the corresponding area of STR is left unchanged. */ -SCM_API char *scm_c_substring2str (SCM obj, char *str, size_t start, size_t len); +SCM_DEPRECATED char *scm_c_substring2str (SCM obj, char *str, size_t start, size_t len); -SCM_API char *scm_c_symbol2str (SCM obj, char *str, size_t *lenp); +SCM_DEPRECATED char *scm_c_symbol2str (SCM obj, char *str, size_t *lenp); /* Deprecated because the names belong to what is now scm_truncate_number and scm_round_number. */ -SCM_API double scm_truncate (double x); -SCM_API double scm_round (double x); +SCM_DEPRECATED double scm_truncate (double x); +SCM_DEPRECATED double scm_round (double x); /* Deprecated because we don't want people to access the internal representation of strings directly. @@ -459,8 +460,8 @@ SCM_API double scm_round (double x); symbols directly. */ -SCM_API char *scm_i_deprecated_symbol_chars (SCM sym); -SCM_API size_t scm_i_deprecated_symbol_length (SCM sym); +SCM_DEPRECATED char *scm_i_deprecated_symbol_chars (SCM sym); +SCM_DEPRECATED size_t scm_i_deprecated_symbol_length (SCM sym); #define SCM_SYMBOL_CHARS(x) scm_i_deprecated_symbol_chars(x) #define SCM_SYMBOL_LENGTH(x) scm_i_deprecated_symbol_length(x) @@ -469,8 +470,8 @@ SCM_API size_t scm_i_deprecated_symbol_length (SCM sym); than once and because the symbol of a keyword now has no dash. */ -SCM_API int scm_i_keywordp (SCM obj); -SCM_API SCM scm_i_keywordsym (SCM keyword); +SCM_DEPRECATED int scm_i_keywordp (SCM obj); +SCM_DEPRECATED SCM scm_i_keywordsym (SCM keyword); #define SCM_KEYWORDP(x) scm_i_keywordp(x) #define SCM_KEYWORDSYM(x) scm_i_keywordsym(x) @@ -480,13 +481,13 @@ SCM_API SCM scm_i_keywordsym (SCM keyword); #define SCM_VECTOR_MAX_LENGTH ((1L << 24) - 1) -SCM_API int scm_i_vectorp (SCM x); -SCM_API unsigned long scm_i_vector_length (SCM x); -SCM_API const SCM *scm_i_velts (SCM x); -SCM_API SCM *scm_i_writable_velts (SCM x); -SCM_API SCM scm_i_vector_ref (SCM x, size_t idx); -SCM_API void scm_i_vector_set (SCM x, size_t idx, SCM val); -SCM_API SCM scm_vector_equal_p (SCM x, SCM y); +SCM_DEPRECATED int scm_i_vectorp (SCM x); +SCM_DEPRECATED unsigned long scm_i_vector_length (SCM x); +SCM_DEPRECATED const SCM *scm_i_velts (SCM x); +SCM_DEPRECATED SCM *scm_i_writable_velts (SCM x); +SCM_DEPRECATED SCM scm_i_vector_ref (SCM x, size_t idx); +SCM_DEPRECATED void scm_i_vector_set (SCM x, size_t idx, SCM val); +SCM_DEPRECATED SCM scm_vector_equal_p (SCM x, SCM y); #define SCM_VECTORP(x) scm_i_vectorp(x) #define SCM_VECTOR_LENGTH(x) scm_i_vector_length(x) @@ -497,13 +498,13 @@ SCM_API SCM scm_vector_equal_p (SCM x, SCM y); typedef scm_i_t_array scm_t_array; -SCM_API int scm_i_arrayp (SCM a); -SCM_API size_t scm_i_array_ndim (SCM a); -SCM_API int scm_i_array_contp (SCM a); -SCM_API scm_t_array *scm_i_array_mem (SCM a); -SCM_API SCM scm_i_array_v (SCM a); -SCM_API size_t scm_i_array_base (SCM a); -SCM_API scm_t_array_dim *scm_i_array_dims (SCM a); +SCM_DEPRECATED int scm_i_arrayp (SCM a); +SCM_DEPRECATED size_t scm_i_array_ndim (SCM a); +SCM_DEPRECATED int scm_i_array_contp (SCM a); +SCM_DEPRECATED scm_t_array *scm_i_array_mem (SCM a); +SCM_DEPRECATED SCM scm_i_array_v (SCM a); +SCM_DEPRECATED size_t scm_i_array_base (SCM a); +SCM_DEPRECATED scm_t_array_dim *scm_i_array_dims (SCM a); #define SCM_ARRAYP(a) scm_i_arrayp(a) #define SCM_ARRAY_NDIM(a) scm_i_array_ndim(a) @@ -526,19 +527,19 @@ SCM_API scm_t_array_dim *scm_i_array_dims (SCM a); #define scm_last_debug_frame scm_i_deprecated_last_debug_frame () #define scm_stack_base scm_i_stack_base () -SCM_API SCM scm_i_cur_inp (void); -SCM_API SCM scm_i_cur_outp (void); -SCM_API SCM scm_i_cur_errp (void); -SCM_API SCM scm_i_cur_loadp (void); -SCM_API SCM scm_i_progargs (void); -SCM_API SCM scm_i_deprecated_dynwinds (void); -SCM_API scm_t_debug_frame *scm_i_deprecated_last_debug_frame (void); -SCM_API SCM_STACKITEM *scm_i_stack_base (void); +SCM_DEPRECATED SCM scm_i_cur_inp (void); +SCM_DEPRECATED SCM scm_i_cur_outp (void); +SCM_DEPRECATED SCM scm_i_cur_errp (void); +SCM_DEPRECATED SCM scm_i_cur_loadp (void); +SCM_DEPRECATED SCM scm_i_progargs (void); +SCM_DEPRECATED SCM scm_i_deprecated_dynwinds (void); +SCM_DEPRECATED scm_t_debug_frame *scm_i_deprecated_last_debug_frame (void); +SCM_DEPRECATED SCM_STACKITEM *scm_i_stack_base (void); /* Deprecated because it evaluates its argument twice. */ #define SCM_FLUIDP(x) scm_i_fluidp (x) -SCM_API int scm_i_fluidp (SCM x); +SCM_DEPRECATED int scm_i_fluidp (SCM x); /* In the old days, SCM_CRITICAL_SECTION_START stopped signal handlers from running, since in those days the handler directly ran scheme @@ -566,38 +567,44 @@ SCM_API int scm_i_fluidp (SCM x); similar DEFER/ALLOW region. */ -SCM_API void scm_i_defer_ints_etc (void); +SCM_DEPRECATED void scm_i_defer_ints_etc (void); #define SCM_DEFER_INTS scm_i_defer_ints_etc () #define SCM_ALLOW_INTS scm_i_defer_ints_etc () #define SCM_REDEFER_INTS scm_i_defer_ints_etc () #define SCM_REALLOW_INTS scm_i_defer_ints_etc () +/* In the old days (pre-1.8), this macro was sometimes used as an lvalue as + in "scm_mask_ints = 1" to block async execution. It no longer works. */ +#define scm_mask_ints (scm_i_mask_ints ()) + +SCM_DEPRECATED int scm_i_mask_ints (void); + /* Deprecated since they are unnecessary and had not been documented. */ -SCM_API SCM scm_guard (SCM guardian, SCM obj, int throw_p); -SCM_API SCM scm_get_one_zombie (SCM guardian); +SCM_DEPRECATED SCM scm_guard (SCM guardian, SCM obj, int throw_p); +SCM_DEPRECATED SCM scm_get_one_zombie (SCM guardian); /* Deprecated since guardians no longer have these special features. */ -SCM_API SCM scm_destroy_guardian_x (SCM guardian); -SCM_API SCM scm_guardian_greedy_p (SCM guardian); -SCM_API SCM scm_guardian_destroyed_p (SCM guardian); +SCM_DEPRECATED SCM scm_destroy_guardian_x (SCM guardian); +SCM_DEPRECATED SCM scm_guardian_greedy_p (SCM guardian); +SCM_DEPRECATED SCM scm_guardian_destroyed_p (SCM guardian); /* GC-related things deprecated with the move to BDW-GC starting from 1.9.3 (2009-09-15). */ -SCM_API unsigned long scm_mallocated; -SCM_API unsigned long scm_mtrigger; +SCM_DEPRECATED unsigned long scm_mallocated; +SCM_DEPRECATED unsigned long scm_mtrigger; -SCM_API size_t scm_max_segment_size; +SCM_DEPRECATED size_t scm_max_segment_size; #if defined (GUILE_DEBUG) || defined (GUILE_DEBUG_FREELIST) -SCM_API SCM scm_map_free_list (void); +SCM_DEPRECATED SCM scm_map_free_list (void); #endif #if defined (GUILE_DEBUG_FREELIST) -SCM_API SCM scm_gc_set_debug_check_freelist_x (SCM flag); +SCM_DEPRECATED SCM scm_gc_set_debug_check_freelist_x (SCM flag); #endif diff --git a/libguile/dynl.c b/libguile/dynl.c index dc98e7d17..52c43e542 100644 --- a/libguile/dynl.c +++ b/libguile/dynl.c @@ -48,6 +48,7 @@ maybe_drag_in_eprintf () #include <string.h> #include "libguile/_scm.h" +#include "libguile/libpath.h" #include "libguile/dynl.h" #include "libguile/smob.h" #include "libguile/keywords.h" @@ -113,7 +114,24 @@ sysdep_dynl_func (const char *symb, void *handle, const char *subr) static void sysdep_dynl_init () { + char *env; + lt_dlinit (); + + env = getenv ("GUILE_SYSTEM_EXTENSIONS_PATH"); + if (env && strcmp (env, "") == 0) + /* special-case interpret system-ltdl-path=="" as meaning no system path, + which is the case during the build */ + ; + else if (env) + /* FIXME: should this be a colon-separated path? Or is the only point to + allow the build system to turn off the installed extensions path? */ + lt_dladdsearchdir (env); + else + { + lt_dladdsearchdir (SCM_LIB_DIR); + lt_dladdsearchdir (SCM_EXTENSIONS_DIR); + } } scm_t_bits scm_tc16_dynamic_obj; diff --git a/libguile/environments.c b/libguile/environments.c deleted file mode 100644 index fd4b88300..000000000 --- a/libguile/environments.c +++ /dev/null @@ -1,2226 +0,0 @@ -/* Copyright (C) 1999,2000,2001, 2003, 2006, 2008 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - - -#ifdef HAVE_CONFIG_H -# include <config.h> -#endif - -#include "libguile/_scm.h" -#include "libguile/alist.h" -#include "libguile/eval.h" -#include "libguile/hash.h" -#include "libguile/list.h" -#include "libguile/ports.h" -#include "libguile/smob.h" -#include "libguile/symbols.h" -#include "libguile/vectors.h" -#include "libguile/weaks.h" - -#include "libguile/environments.h" - - - -scm_t_bits scm_tc16_environment; -scm_t_bits scm_tc16_observer; -#define DEFAULT_OBARRAY_SIZE 31 - -SCM scm_system_environment; - - - -/* error conditions */ - -/* - * Throw an error if symbol is not bound in environment func - */ -void -scm_error_environment_unbound (const char *func, SCM env, SCM symbol) -{ - /* Dirk:FIXME:: Should throw an environment:unbound type error */ - char error[] = "Symbol `~A' not bound in environment `~A'."; - SCM arguments = scm_cons2 (symbol, env, SCM_EOL); - scm_misc_error (func, error, arguments); -} - - -/* - * Throw an error if func tried to create (define) or remove - * (undefine) a new binding for symbol in env - */ -void -scm_error_environment_immutable_binding (const char *func, SCM env, SCM symbol) -{ - /* Dirk:FIXME:: Should throw an environment:immutable-binding type error */ - char error[] = "Immutable binding in environment ~A (symbol: `~A')."; - SCM arguments = scm_cons2 (env, symbol, SCM_EOL); - scm_misc_error (func, error, arguments); -} - - -/* - * Throw an error if func tried to change an immutable location. - */ -void -scm_error_environment_immutable_location (const char *func, SCM env, SCM symbol) -{ - /* Dirk:FIXME:: Should throw an environment:immutable-location type error */ - char error[] = "Immutable location in environment `~A' (symbol: `~A')."; - SCM arguments = scm_cons2 (env, symbol, SCM_EOL); - scm_misc_error (func, error, arguments); -} - - - -/* generic environments */ - - -/* Create an environment for the given type. Dereferencing type twice must - * deliver the initialized set of environment functions. Thus, type will - * also determine the signature of the underlying environment implementation. - * Dereferencing type once will typically deliver the data fields used by the - * underlying environment implementation. - */ -SCM -scm_make_environment (void *type) -{ - SCM_RETURN_NEWSMOB (scm_tc16_environment, type); -} - - -SCM_DEFINE (scm_environment_p, "environment?", 1, 0, 0, - (SCM obj), - "Return @code{#t} if @var{obj} is an environment, or @code{#f}\n" - "otherwise.") -#define FUNC_NAME s_scm_environment_p -{ - return scm_from_bool (SCM_ENVIRONMENT_P (obj)); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_bound_p, "environment-bound?", 2, 0, 0, - (SCM env, SCM sym), - "Return @code{#t} if @var{sym} is bound in @var{env}, or\n" - "@code{#f} otherwise.") -#define FUNC_NAME s_scm_environment_bound_p -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); - - return scm_from_bool (SCM_ENVIRONMENT_BOUND_P (env, sym)); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_ref, "environment-ref", 2, 0, 0, - (SCM env, SCM sym), - "Return the value of the location bound to @var{sym} in\n" - "@var{env}. If @var{sym} is unbound in @var{env}, signal an\n" - "@code{environment:unbound} error.") -#define FUNC_NAME s_scm_environment_ref -{ - SCM val; - - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); - - val = SCM_ENVIRONMENT_REF (env, sym); - - if (!SCM_UNBNDP (val)) - return val; - else - scm_error_environment_unbound (FUNC_NAME, env, sym); -} -#undef FUNC_NAME - - -/* This C function is identical to environment-ref, except that if symbol is - * unbound in env, it returns the value SCM_UNDEFINED, instead of signalling - * an error. - */ -SCM -scm_c_environment_ref (SCM env, SCM sym) -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_ref"); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, "scm_c_environment_ref"); - return SCM_ENVIRONMENT_REF (env, sym); -} - - -static SCM -environment_default_folder (SCM proc, SCM symbol, SCM value, SCM tail) -{ - return scm_call_3 (proc, symbol, value, tail); -} - - -SCM_DEFINE (scm_environment_fold, "environment-fold", 3, 0, 0, - (SCM env, SCM proc, SCM init), - "Iterate over all the bindings in @var{env}, accumulating some\n" - "value.\n" - "For each binding in @var{env}, apply @var{proc} to the symbol\n" - "bound, its value, and the result from the previous application\n" - "of @var{proc}.\n" - "Use @var{init} as @var{proc}'s third argument the first time\n" - "@var{proc} is applied.\n" - "If @var{env} contains no bindings, this function simply returns\n" - "@var{init}.\n" - "If @var{env} binds the symbol sym1 to the value val1, sym2 to\n" - "val2, and so on, then this procedure computes:\n" - "@lisp\n" - " (proc sym1 val1\n" - " (proc sym2 val2\n" - " ...\n" - " (proc symn valn\n" - " init)))\n" - "@end lisp\n" - "Each binding in @var{env} will be processed exactly once.\n" - "@code{environment-fold} makes no guarantees about the order in\n" - "which the bindings are processed.\n" - "Here is a function which, given an environment, constructs an\n" - "association list representing that environment's bindings,\n" - "using environment-fold:\n" - "@lisp\n" - " (define (environment->alist env)\n" - " (environment-fold env\n" - " (lambda (sym val tail)\n" - " (cons (cons sym val) tail))\n" - " '()))\n" - "@end lisp") -#define FUNC_NAME s_scm_environment_fold -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (scm_is_true (scm_procedure_p (proc)), - proc, SCM_ARG2, FUNC_NAME); - - return SCM_ENVIRONMENT_FOLD (env, environment_default_folder, proc, init); -} -#undef FUNC_NAME - - -/* This is the C-level analog of environment-fold. For each binding in ENV, - * make the call: - * (*proc) (data, symbol, value, previous) - * where previous is the value returned from the last call to *PROC, or INIT - * for the first call. If ENV contains no bindings, return INIT. - */ -SCM -scm_c_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_fold"); - - return SCM_ENVIRONMENT_FOLD (env, proc, data, init); -} - - -SCM_DEFINE (scm_environment_define, "environment-define", 3, 0, 0, - (SCM env, SCM sym, SCM val), - "Bind @var{sym} to a new location containing @var{val} in\n" - "@var{env}. If @var{sym} is already bound to another location\n" - "in @var{env} and the binding is mutable, that binding is\n" - "replaced. The new binding and location are both mutable. The\n" - "return value is unspecified.\n" - "If @var{sym} is already bound in @var{env}, and the binding is\n" - "immutable, signal an @code{environment:immutable-binding} error.") -#define FUNC_NAME s_scm_environment_define -{ - SCM status; - - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); - - status = SCM_ENVIRONMENT_DEFINE (env, sym, val); - - if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS)) - return SCM_UNSPECIFIED; - else if (scm_is_eq (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE)) - scm_error_environment_immutable_binding (FUNC_NAME, env, sym); - else - abort(); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_undefine, "environment-undefine", 2, 0, 0, - (SCM env, SCM sym), - "Remove any binding for @var{sym} from @var{env}. If @var{sym}\n" - "is unbound in @var{env}, do nothing. The return value is\n" - "unspecified.\n" - "If @var{sym} is already bound in @var{env}, and the binding is\n" - "immutable, signal an @code{environment:immutable-binding} error.") -#define FUNC_NAME s_scm_environment_undefine -{ - SCM status; - - SCM_ASSERT(SCM_ENVIRONMENT_P(env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT(scm_is_symbol(sym), sym, SCM_ARG2, FUNC_NAME); - - status = SCM_ENVIRONMENT_UNDEFINE (env, sym); - - if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS)) - return SCM_UNSPECIFIED; - else if (scm_is_eq (status, SCM_ENVIRONMENT_BINDING_IMMUTABLE)) - scm_error_environment_immutable_binding (FUNC_NAME, env, sym); - else - abort(); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_set_x, "environment-set!", 3, 0, 0, - (SCM env, SCM sym, SCM val), - "If @var{env} binds @var{sym} to some location, change that\n" - "location's value to @var{val}. The return value is\n" - "unspecified.\n" - "If @var{sym} is not bound in @var{env}, signal an\n" - "@code{environment:unbound} error. If @var{env} binds @var{sym}\n" - "to an immutable location, signal an\n" - "@code{environment:immutable-location} error.") -#define FUNC_NAME s_scm_environment_set_x -{ - SCM status; - - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); - - status = SCM_ENVIRONMENT_SET (env, sym, val); - - if (scm_is_eq (status, SCM_ENVIRONMENT_SUCCESS)) - return SCM_UNSPECIFIED; - else if (SCM_UNBNDP (status)) - scm_error_environment_unbound (FUNC_NAME, env, sym); - else if (scm_is_eq (status, SCM_ENVIRONMENT_LOCATION_IMMUTABLE)) - scm_error_environment_immutable_binding (FUNC_NAME, env, sym); - else - abort(); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_cell, "environment-cell", 3, 0, 0, - (SCM env, SCM sym, SCM for_write), - "Return the value cell which @var{env} binds to @var{sym}, or\n" - "@code{#f} if the binding does not live in a value cell.\n" - "The argument @var{for-write} indicates whether the caller\n" - "intends to modify the variable's value by mutating the value\n" - "cell. If the variable is immutable, then\n" - "@code{environment-cell} signals an\n" - "@code{environment:immutable-location} error.\n" - "If @var{sym} is unbound in @var{env}, signal an\n" - "@code{environment:unbound} error.\n" - "If you use this function, you should consider using\n" - "@code{environment-observe}, to be notified when @var{sym} gets\n" - "re-bound to a new value cell, or becomes undefined.") -#define FUNC_NAME s_scm_environment_cell -{ - SCM location; - - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, FUNC_NAME); - SCM_ASSERT (scm_is_bool (for_write), for_write, SCM_ARG3, FUNC_NAME); - - location = SCM_ENVIRONMENT_CELL (env, sym, scm_is_true (for_write)); - if (!SCM_IMP (location)) - return location; - else if (SCM_UNBNDP (location)) - scm_error_environment_unbound (FUNC_NAME, env, sym); - else if (scm_is_eq (location, SCM_ENVIRONMENT_LOCATION_IMMUTABLE)) - scm_error_environment_immutable_location (FUNC_NAME, env, sym); - else /* no cell */ - return location; -} -#undef FUNC_NAME - - -/* This C function is identical to environment-cell, with the following - * exceptions: If symbol is unbound in env, it returns the value - * SCM_UNDEFINED, instead of signalling an error. If symbol is bound to an - * immutable location but the cell is requested for write, the value - * SCM_ENVIRONMENT_LOCATION_IMMUTABLE is returned. - */ -SCM -scm_c_environment_cell(SCM env, SCM sym, int for_write) -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, "scm_c_environment_cell"); - SCM_ASSERT (scm_is_symbol (sym), sym, SCM_ARG2, "scm_c_environment_cell"); - - return SCM_ENVIRONMENT_CELL (env, sym, for_write); -} - - -static void -environment_default_observer (SCM env, SCM proc) -{ - scm_call_1 (proc, env); -} - - -SCM_DEFINE (scm_environment_observe, "environment-observe", 2, 0, 0, - (SCM env, SCM proc), - "Whenever @var{env}'s bindings change, apply @var{proc} to\n" - "@var{env}.\n" - "This function returns an object, token, which you can pass to\n" - "@code{environment-unobserve} to remove @var{proc} from the set\n" - "of procedures observing @var{env}. The type and value of\n" - "token is unspecified.") -#define FUNC_NAME s_scm_environment_observe -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return SCM_ENVIRONMENT_OBSERVE (env, environment_default_observer, proc, 0); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_observe_weak, "environment-observe-weak", 2, 0, 0, - (SCM env, SCM proc), - "This function is the same as environment-observe, except that\n" - "the reference @var{env} retains to @var{proc} is a weak\n" - "reference. This means that, if there are no other live,\n" - "non-weak references to @var{proc}, it will be\n" - "garbage-collected, and dropped from @var{env}'s\n" - "list of observing procedures.") -#define FUNC_NAME s_scm_environment_observe_weak -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return SCM_ENVIRONMENT_OBSERVE (env, environment_default_observer, proc, 1); -} -#undef FUNC_NAME - - -/* This is the C-level analog of the Scheme functions environment-observe and - * environment-observe-weak. Whenever env's bindings change, call the - * function proc, passing it env and data. If weak_p is non-zero, env will - * retain only a weak reference to data, and if data is garbage collected, the - * entire observation will be dropped. This function returns a token, with - * the same meaning as those returned by environment-observe and - * environment-observe-weak. - */ -SCM -scm_c_environment_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p) -#define FUNC_NAME "scm_c_environment_observe" -{ - SCM_ASSERT (SCM_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return SCM_ENVIRONMENT_OBSERVE (env, proc, data, weak_p); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_environment_unobserve, "environment-unobserve", 1, 0, 0, - (SCM token), - "Cancel the observation request which returned the value\n" - "@var{token}. The return value is unspecified.\n" - "If a call @code{(environment-observe env proc)} returns\n" - "@var{token}, then the call @code{(environment-unobserve token)}\n" - "will cause @var{proc} to no longer be called when @var{env}'s\n" - "bindings change.") -#define FUNC_NAME s_scm_environment_unobserve -{ - SCM env; - - SCM_ASSERT (SCM_OBSERVER_P (token), token, SCM_ARG1, FUNC_NAME); - - env = SCM_OBSERVER_ENVIRONMENT (token); - SCM_ENVIRONMENT_UNOBSERVE (env, token); - - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - - - -static int -environment_print (SCM env, SCM port, scm_print_state *pstate) -{ - return (*(SCM_ENVIRONMENT_FUNCS (env)->print)) (env, port, pstate); -} - - - -/* observers */ - - -static int -observer_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED) -{ - SCM address = scm_from_size_t (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, scm_from_int (16)); - - scm_puts ("#<observer ", port); - scm_display (base16, port); - scm_puts (">", port); - - return 1; -} - - - -/* obarrays - * - * Obarrays form the basic lookup tables used to implement most of guile's - * built-in environment types. An obarray is implemented as a hash table with - * symbols as keys. The content of the data depends on the environment type. - */ - - -/* - * Enter symbol into obarray. The symbol must not already exist in obarray. - * The freshly generated (symbol . data) cell is returned. - */ -static SCM -obarray_enter (SCM obarray, SCM symbol, SCM data) -{ - size_t hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (obarray); - SCM entry = scm_cons (symbol, data); - SCM slot = scm_cons (entry, SCM_HASHTABLE_BUCKET (obarray, hash)); - SCM_SET_HASHTABLE_BUCKET (obarray, hash, slot); - SCM_HASHTABLE_INCREMENT (obarray); - if (SCM_HASHTABLE_N_ITEMS (obarray) > SCM_HASHTABLE_UPPER (obarray)) - scm_i_rehash (obarray, scm_i_hash_symbol, 0, "obarray_enter"); - - return entry; -} - - -/* - * Enter symbol into obarray. An existing entry for symbol is replaced. If - * an entry existed, the old (symbol . data) cell is returned, #f otherwise. - */ -static SCM -obarray_replace (SCM obarray, SCM symbol, SCM data) -{ - size_t hash = scm_i_symbol_hash (symbol) % SCM_HASHTABLE_N_BUCKETS (obarray); - SCM new_entry = scm_cons (symbol, data); - SCM lsym; - SCM slot; - - for (lsym = SCM_HASHTABLE_BUCKET (obarray, hash); - !scm_is_null (lsym); - lsym = SCM_CDR (lsym)) - { - SCM old_entry = SCM_CAR (lsym); - if (scm_is_eq (SCM_CAR (old_entry), symbol)) - { - SCM_SETCAR (lsym, new_entry); - return old_entry; - } - } - - slot = scm_cons (new_entry, SCM_HASHTABLE_BUCKET (obarray, hash)); - SCM_SET_HASHTABLE_BUCKET (obarray, hash, slot); - SCM_HASHTABLE_INCREMENT (obarray); - if (SCM_HASHTABLE_N_ITEMS (obarray) > SCM_HASHTABLE_UPPER (obarray)) - scm_i_rehash (obarray, scm_i_hash_symbol, 0, "obarray_replace"); - - return SCM_BOOL_F; -} - - -/* - * Look up symbol in obarray - */ -static SCM -obarray_retrieve (SCM obarray, SCM sym) -{ - size_t hash = scm_i_symbol_hash (sym) % SCM_HASHTABLE_N_BUCKETS (obarray); - SCM lsym; - - for (lsym = SCM_HASHTABLE_BUCKET (obarray, hash); - !scm_is_null (lsym); - lsym = SCM_CDR (lsym)) - { - SCM entry = SCM_CAR (lsym); - if (scm_is_eq (SCM_CAR (entry), sym)) - return entry; - } - - return SCM_UNDEFINED; -} - - -/* - * Remove entry from obarray. If the symbol was found and removed, the old - * (symbol . data) cell is returned, #f otherwise. - */ -static SCM -obarray_remove (SCM obarray, SCM sym) -{ - size_t hash = scm_i_symbol_hash (sym) % SCM_HASHTABLE_N_BUCKETS (obarray); - SCM table_entry = SCM_HASHTABLE_BUCKET (obarray, hash); - SCM handle = scm_sloppy_assq (sym, table_entry); - - if (scm_is_pair (handle)) - { - SCM new_table_entry = scm_delq1_x (handle, table_entry); - SCM_SET_HASHTABLE_BUCKET (obarray, hash, new_table_entry); - SCM_HASHTABLE_DECREMENT (obarray); - } - - return handle; -} - - -static void -obarray_remove_all (SCM obarray) -{ - size_t size = SCM_HASHTABLE_N_BUCKETS (obarray); - size_t i; - - for (i = 0; i < size; i++) - { - SCM_SET_HASHTABLE_BUCKET (obarray, i, SCM_EOL); - } - SCM_SET_HASHTABLE_N_ITEMS (obarray, 0); -} - - - -/* core environments base - * - * This struct and the corresponding functions form a base class for guile's - * built-in environment types. - */ - - -struct core_environments_base { - struct scm_environment_funcs *funcs; - - SCM observers; - SCM weak_observers; -}; - - -#define CORE_ENVIRONMENTS_BASE(env) \ - ((struct core_environments_base *) SCM_CELL_WORD_1 (env)) -#define CORE_ENVIRONMENT_OBSERVERS(env) \ - (CORE_ENVIRONMENTS_BASE (env)->observers) -#define SCM_SET_CORE_ENVIRONMENT_OBSERVERS(env, v) \ - (CORE_ENVIRONMENT_OBSERVERS (env) = (v)) -#define CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR(env) \ - (CORE_ENVIRONMENTS_BASE (env)->weak_observers) -#define CORE_ENVIRONMENT_WEAK_OBSERVERS(env) \ - (scm_c_vector_ref (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env), 0)) -#define SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS(env, v) \ - (scm_c_vector_set_x (CORE_ENVIRONMENT_WEAK_OBSERVER_VECTOR (env), 0, (v))) - - - -static SCM -core_environments_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p) -{ - SCM observer; - - SCM_NEWSMOB3 (observer, scm_tc16_observer, - SCM_UNPACK (env), - SCM_UNPACK (data), - (scm_t_bits) proc); - - if (!weak_p) - { - SCM observers = CORE_ENVIRONMENT_OBSERVERS (env); - SCM new_observers = scm_cons (observer, observers); - SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env, new_observers); - } - else - { - SCM observers = CORE_ENVIRONMENT_WEAK_OBSERVERS (env); - SCM new_observers = scm_acons (SCM_BOOL_F, observer, observers); - SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env, new_observers); - } - - return observer; -} - - -static void -core_environments_unobserve (SCM env, SCM observer) -{ - unsigned int handling_weaks; - for (handling_weaks = 0; handling_weaks <= 1; ++handling_weaks) - { - SCM l = handling_weaks - ? CORE_ENVIRONMENT_WEAK_OBSERVERS (env) - : CORE_ENVIRONMENT_OBSERVERS (env); - - if (!scm_is_null (l)) - { - SCM rest = SCM_CDR (l); - SCM first = handling_weaks - ? SCM_CDAR (l) - : SCM_CAR (l); - - if (scm_is_eq (first, observer)) - { - /* Remove the first observer */ - if (handling_weaks) - SCM_SET_CORE_ENVIRONMENT_WEAK_OBSERVERS (env, rest); - else - SCM_SET_CORE_ENVIRONMENT_OBSERVERS (env, rest); - return; - } - - do { - SCM rest = SCM_CDR (l); - - if (!scm_is_null (rest)) - { - SCM next = handling_weaks - ? SCM_CDAR (l) - : SCM_CAR (l); - - if (scm_is_eq (next, observer)) - { - SCM_SETCDR (l, SCM_CDR (rest)); - return; - } - } - - l = rest; - } while (!scm_is_null (l)); - } - } - - /* Dirk:FIXME:: What to do now, since the observer is not found? */ -} - - -static void -core_environments_preinit (struct core_environments_base *body) -{ - body->funcs = NULL; - body->observers = SCM_BOOL_F; - body->weak_observers = SCM_BOOL_F; -} - - -static void -core_environments_init (struct core_environments_base *body, - struct scm_environment_funcs *funcs) -{ - body->funcs = funcs; - body->observers = SCM_EOL; - body->weak_observers = scm_make_weak_value_alist_vector (scm_from_int (1)); -} - - -/* Tell all observers to clear their caches. - * - * Environments have to be informed about changes in the following cases: - * - The observed env has a new binding. This must be always reported. - * - The observed env has dropped a binding. This must be always reported. - * - A binding in the observed environment has changed. This must only be - * reported, if there is a chance that the binding is being cached outside. - * However, this potential optimization is not performed currently. - * - * Errors that occur while the observers are called are accumulated and - * signalled as one single error message to the caller. - */ - -struct update_data -{ - SCM observer; - SCM environment; -}; - - -static SCM -update_catch_body (void *ptr) -{ - struct update_data *data = (struct update_data *) ptr; - SCM observer = data->observer; - - (*SCM_OBSERVER_PROC (observer)) - (data->environment, SCM_OBSERVER_DATA (observer)); - - return SCM_UNDEFINED; -} - - -static SCM -update_catch_handler (void *ptr, SCM tag, SCM args) -{ - struct update_data *data = (struct update_data *) ptr; - SCM observer = data->observer; - SCM message = - scm_from_locale_string ("Observer `~A' signals `~A' error: ~S"); - - return scm_cons (message, scm_list_3 (observer, tag, args)); -} - - -static void -core_environments_broadcast (SCM env) -#define FUNC_NAME "core_environments_broadcast" -{ - unsigned int handling_weaks; - SCM errors = SCM_EOL; - - for (handling_weaks = 0; handling_weaks <= 1; ++handling_weaks) - { - SCM observers = handling_weaks - ? CORE_ENVIRONMENT_WEAK_OBSERVERS (env) - : CORE_ENVIRONMENT_OBSERVERS (env); - - for (; !scm_is_null (observers); observers = SCM_CDR (observers)) - { - struct update_data data; - SCM observer = handling_weaks - ? SCM_CDAR (observers) - : SCM_CAR (observers); - SCM error; - - data.observer = observer; - data.environment = env; - - error = scm_internal_catch (SCM_BOOL_T, - update_catch_body, &data, - update_catch_handler, &data); - - if (!SCM_UNBNDP (error)) - errors = scm_cons (error, errors); - } - } - - if (!scm_is_null (errors)) - { - /* Dirk:FIXME:: As soon as scm_misc_error is fixed to handle the name - * parameter correctly it should not be necessary any more to also pass - * namestr in order to get the desired information from the error - * message. - */ - SCM ordered_errors = scm_reverse (errors); - scm_misc_error - (FUNC_NAME, - "Observers of `~A' have signalled the following errors: ~S", - scm_cons2 (env, ordered_errors, SCM_EOL)); - } -} -#undef FUNC_NAME - - - -/* leaf environments - * - * A leaf environment is simply a mutable set of definitions. A leaf - * environment supports no operations beyond the common set. - * - * Implementation: The obarray of the leaf environment holds (symbol . value) - * pairs. No further information is necessary, since all bindings and - * locations in a leaf environment are mutable. - */ - - -struct leaf_environment { - struct core_environments_base base; - - SCM obarray; -}; - - -#define LEAF_ENVIRONMENT(env) \ - ((struct leaf_environment *) SCM_CELL_WORD_1 (env)) - - - -static SCM -leaf_environment_ref (SCM env, SCM sym) -{ - SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - SCM binding = obarray_retrieve (obarray, sym); - return SCM_UNBNDP (binding) ? binding : SCM_CDR (binding); -} - - -static SCM -leaf_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) -{ - size_t i; - SCM result = init; - SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - - for (i = 0; i < SCM_HASHTABLE_N_BUCKETS (obarray); i++) - { - SCM l; - for (l = SCM_HASHTABLE_BUCKET (obarray, i); - !scm_is_null (l); - l = SCM_CDR (l)) - { - SCM binding = SCM_CAR (l); - SCM symbol = SCM_CAR (binding); - SCM value = SCM_CDR (binding); - result = (*proc) (data, symbol, value, result); - } - } - return result; -} - - -static SCM -leaf_environment_define (SCM env, SCM sym, SCM val) -#define FUNC_NAME "leaf_environment_define" -{ - SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - - obarray_replace (obarray, sym, val); - core_environments_broadcast (env); - - return SCM_ENVIRONMENT_SUCCESS; -} -#undef FUNC_NAME - - -static SCM -leaf_environment_undefine (SCM env, SCM sym) -#define FUNC_NAME "leaf_environment_undefine" -{ - SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - SCM removed = obarray_remove (obarray, sym); - - if (scm_is_true (removed)) - core_environments_broadcast (env); - - return SCM_ENVIRONMENT_SUCCESS; -} -#undef FUNC_NAME - - -static SCM -leaf_environment_set_x (SCM env, SCM sym, SCM val) -#define FUNC_NAME "leaf_environment_set_x" -{ - SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - SCM binding = obarray_retrieve (obarray, sym); - - if (!SCM_UNBNDP (binding)) - { - SCM_SETCDR (binding, val); - return SCM_ENVIRONMENT_SUCCESS; - } - else - { - return SCM_UNDEFINED; - } -} -#undef FUNC_NAME - - -static SCM -leaf_environment_cell (SCM env, SCM sym, int for_write SCM_UNUSED) -{ - SCM obarray = LEAF_ENVIRONMENT (env)->obarray; - SCM binding = obarray_retrieve (obarray, sym); - return binding; -} - - - -static int -leaf_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED) -{ - SCM address = scm_from_size_t (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, scm_from_int (16)); - - scm_puts ("#<leaf environment ", port); - scm_display (base16, port); - scm_puts (">", port); - - return 1; -} - - -static struct scm_environment_funcs leaf_environment_funcs = { - leaf_environment_ref, - leaf_environment_fold, - leaf_environment_define, - leaf_environment_undefine, - leaf_environment_set_x, - leaf_environment_cell, - core_environments_observe, - core_environments_unobserve, - leaf_environment_print -}; - - -void *scm_type_leaf_environment = &leaf_environment_funcs; - - -SCM_DEFINE (scm_make_leaf_environment, "make-leaf-environment", 0, 0, 0, - (), - "Create a new leaf environment, containing no bindings.\n" - "All bindings and locations created in the new environment\n" - "will be mutable.") -#define FUNC_NAME s_scm_make_leaf_environment -{ - size_t size = sizeof (struct leaf_environment); - struct leaf_environment *body = scm_gc_malloc (size, "leaf environment"); - SCM env; - - core_environments_preinit (&body->base); - body->obarray = SCM_BOOL_F; - - env = scm_make_environment (body); - - core_environments_init (&body->base, &leaf_environment_funcs); - body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE); - - return env; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_leaf_environment_p, "leaf-environment?", 1, 0, 0, - (SCM object), - "Return @code{#t} if object is a leaf environment, or @code{#f}\n" - "otherwise.") -#define FUNC_NAME s_scm_leaf_environment_p -{ - return scm_from_bool (SCM_LEAF_ENVIRONMENT_P (object)); -} -#undef FUNC_NAME - - - -/* eval environments - * - * A module's source code refers to definitions imported from other modules, - * and definitions made within itself. An eval environment combines two - * environments -- a local environment and an imported environment -- to - * produce a new environment in which both sorts of references can be - * resolved. - * - * Implementation: The obarray of the eval environment is used to cache - * entries from the local and imported environments such that in most of the - * cases only a single lookup is necessary. Since for neither the local nor - * the imported environment it is known, what kind of environment they form, - * the most general case is assumed. Therefore, entries in the obarray take - * one of the following forms: - * - * 1) (<symbol> location mutability . source-env), where mutability indicates - * one of the following states: IMMUTABLE if the location is known to be - * immutable, MUTABLE if the location is known to be mutable, UNKNOWN if - * the location has only been requested for non modifying accesses. - * - * 2) (symbol . source-env) if the symbol has a binding in the source-env, but - * if the source-env can't provide a cell for the binding. Thus, for every - * access, the source-env has to be contacted directly. - */ - - -struct eval_environment { - struct core_environments_base base; - - SCM obarray; - - SCM imported; - SCM imported_observer; - SCM local; - SCM local_observer; -}; - - -#define EVAL_ENVIRONMENT(env) \ - ((struct eval_environment *) SCM_CELL_WORD_1 (env)) - -#define IMMUTABLE SCM_I_MAKINUM (0) -#define MUTABLE SCM_I_MAKINUM (1) -#define UNKNOWN SCM_I_MAKINUM (2) - -#define CACHED_LOCATION(x) SCM_CAR (x) -#define CACHED_MUTABILITY(x) SCM_CADR (x) -#define SET_CACHED_MUTABILITY(x, v) SCM_SETCAR (SCM_CDR (x), (v)) -#define CACHED_SOURCE_ENVIRONMENT(x) SCM_CDDR (x) - - - -/* eval_environment_lookup will report one of the following distinct results: - * a) (<object> . value) if a cell could be obtained. - * b) <environment> if the environment has to be contacted directly. - * c) IMMUTABLE if an immutable cell was requested for write. - * d) SCM_UNDEFINED if there is no binding for the symbol. - */ -static SCM -eval_environment_lookup (SCM env, SCM sym, int for_write) -{ - SCM obarray = EVAL_ENVIRONMENT (env)->obarray; - SCM binding = obarray_retrieve (obarray, sym); - - if (!SCM_UNBNDP (binding)) - { - /* The obarray holds an entry for the symbol. */ - - SCM entry = SCM_CDR (binding); - - if (scm_is_pair (entry)) - { - /* The entry in the obarray is a cached location. */ - - SCM location = CACHED_LOCATION (entry); - SCM mutability; - - if (!for_write) - return location; - - mutability = CACHED_MUTABILITY (entry); - if (scm_is_eq (mutability, MUTABLE)) - return location; - - if (scm_is_eq (mutability, UNKNOWN)) - { - SCM source_env = CACHED_SOURCE_ENVIRONMENT (entry); - SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, 1); - - if (scm_is_pair (location)) - { - SET_CACHED_MUTABILITY (entry, MUTABLE); - return location; - } - else /* IMMUTABLE */ - { - SET_CACHED_MUTABILITY (entry, IMMUTABLE); - return IMMUTABLE; - } - } - - return IMMUTABLE; - } - else - { - /* The obarray entry is an environment */ - - return entry; - } - } - else - { - /* There is no entry for the symbol in the obarray. This can either - * mean that there has not been a request for the symbol yet, or that - * the symbol is really undefined. We are looking for the symbol in - * both the local and the imported environment. If we find a binding, a - * cached entry is created. - */ - - struct eval_environment *body = EVAL_ENVIRONMENT (env); - unsigned int handling_import; - - for (handling_import = 0; handling_import <= 1; ++handling_import) - { - SCM source_env = handling_import ? body->imported : body->local; - SCM location = SCM_ENVIRONMENT_CELL (source_env, sym, for_write); - - if (!SCM_UNBNDP (location)) - { - if (scm_is_pair (location)) - { - SCM mutability = for_write ? MUTABLE : UNKNOWN; - SCM entry = scm_cons2 (location, mutability, source_env); - obarray_enter (obarray, sym, entry); - return location; - } - else if (scm_is_eq (location, SCM_ENVIRONMENT_LOCATION_NO_CELL)) - { - obarray_enter (obarray, sym, source_env); - return source_env; - } - else - { - return IMMUTABLE; - } - } - } - - return SCM_UNDEFINED; - } -} - - -static SCM -eval_environment_ref (SCM env, SCM sym) -#define FUNC_NAME "eval_environment_ref" -{ - SCM location = eval_environment_lookup (env, sym, 0); - - if (scm_is_pair (location)) - return SCM_CDR (location); - else if (!SCM_UNBNDP (location)) - return SCM_ENVIRONMENT_REF (location, sym); - else - return SCM_UNDEFINED; -} -#undef FUNC_NAME - - -static SCM -eval_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail) -{ - SCM local = SCM_CAR (extended_data); - - if (!SCM_ENVIRONMENT_BOUND_P (local, symbol)) - { - SCM proc_as_nr = SCM_CADR (extended_data); - unsigned long int proc_as_ul = scm_to_ulong (proc_as_nr); - scm_environment_folder proc = (scm_environment_folder) proc_as_ul; - SCM data = SCM_CDDR (extended_data); - - return (*proc) (data, symbol, value, tail); - } - else - { - return tail; - } -} - - -static SCM -eval_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) -{ - SCM local = EVAL_ENVIRONMENT (env)->local; - SCM imported = EVAL_ENVIRONMENT (env)->imported; - SCM proc_as_nr = scm_from_ulong ((unsigned long) proc); - SCM extended_data = scm_cons2 (local, proc_as_nr, data); - SCM tmp_result = scm_c_environment_fold (imported, eval_environment_folder, extended_data, init); - - return scm_c_environment_fold (local, proc, data, tmp_result); -} - - -static SCM -eval_environment_define (SCM env, SCM sym, SCM val) -#define FUNC_NAME "eval_environment_define" -{ - SCM local = EVAL_ENVIRONMENT (env)->local; - return SCM_ENVIRONMENT_DEFINE (local, sym, val); -} -#undef FUNC_NAME - - -static SCM -eval_environment_undefine (SCM env, SCM sym) -#define FUNC_NAME "eval_environment_undefine" -{ - SCM local = EVAL_ENVIRONMENT (env)->local; - return SCM_ENVIRONMENT_UNDEFINE (local, sym); -} -#undef FUNC_NAME - - -static SCM -eval_environment_set_x (SCM env, SCM sym, SCM val) -#define FUNC_NAME "eval_environment_set_x" -{ - SCM location = eval_environment_lookup (env, sym, 1); - - if (scm_is_pair (location)) - { - SCM_SETCDR (location, val); - return SCM_ENVIRONMENT_SUCCESS; - } - else if (SCM_ENVIRONMENT_P (location)) - { - return SCM_ENVIRONMENT_SET (location, sym, val); - } - else if (scm_is_eq (location, IMMUTABLE)) - { - return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; - } - else - { - return SCM_UNDEFINED; - } -} -#undef FUNC_NAME - - -static SCM -eval_environment_cell (SCM env, SCM sym, int for_write) -#define FUNC_NAME "eval_environment_cell" -{ - SCM location = eval_environment_lookup (env, sym, for_write); - - if (scm_is_pair (location)) - return location; - else if (SCM_ENVIRONMENT_P (location)) - return SCM_ENVIRONMENT_LOCATION_NO_CELL; - else if (scm_is_eq (location, IMMUTABLE)) - return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; - else - return SCM_UNDEFINED; -} -#undef FUNC_NAME - - - -static int -eval_environment_print (SCM type, SCM port, scm_print_state *pstate SCM_UNUSED) -{ - SCM address = scm_from_size_t (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, scm_from_int (16)); - - scm_puts ("#<eval environment ", port); - scm_display (base16, port); - scm_puts (">", port); - - return 1; -} - - -static struct scm_environment_funcs eval_environment_funcs = { - eval_environment_ref, - eval_environment_fold, - eval_environment_define, - eval_environment_undefine, - eval_environment_set_x, - eval_environment_cell, - core_environments_observe, - core_environments_unobserve, - eval_environment_print -}; - - -void *scm_type_eval_environment = &eval_environment_funcs; - - -static void -eval_environment_observer (SCM caller SCM_UNUSED, SCM eval_env) -{ - SCM obarray = EVAL_ENVIRONMENT (eval_env)->obarray; - - obarray_remove_all (obarray); - core_environments_broadcast (eval_env); -} - - -SCM_DEFINE (scm_make_eval_environment, "make-eval-environment", 2, 0, 0, - (SCM local, SCM imported), - "Return a new environment object eval whose bindings are the\n" - "union of the bindings in the environments @var{local} and\n" - "@var{imported}, with bindings from @var{local} taking\n" - "precedence. Definitions made in eval are placed in @var{local}.\n" - "Applying @code{environment-define} or\n" - "@code{environment-undefine} to eval has the same effect as\n" - "applying the procedure to @var{local}.\n" - "Note that eval incorporates @var{local} and @var{imported} by\n" - "reference:\n" - "If, after creating eval, the program changes the bindings of\n" - "@var{local} or @var{imported}, those changes will be visible\n" - "in eval.\n" - "Since most Scheme evaluation takes place in eval environments,\n" - "they transparently cache the bindings received from @var{local}\n" - "and @var{imported}. Thus, the first time the program looks up\n" - "a symbol in eval, eval may make calls to @var{local} or\n" - "@var{imported} to find their bindings, but subsequent\n" - "references to that symbol will be as fast as references to\n" - "bindings in finite environments.\n" - "In typical use, @var{local} will be a finite environment, and\n" - "@var{imported} will be an import environment") -#define FUNC_NAME s_scm_make_eval_environment -{ - SCM env; - struct eval_environment *body; - - SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME); - - body = scm_gc_malloc (sizeof (struct eval_environment), "eval environment"); - - core_environments_preinit (&body->base); - body->obarray = SCM_BOOL_F; - body->imported = SCM_BOOL_F; - body->imported_observer = SCM_BOOL_F; - body->local = SCM_BOOL_F; - body->local_observer = SCM_BOOL_F; - - env = scm_make_environment (body); - - core_environments_init (&body->base, &eval_environment_funcs); - body->obarray = scm_c_make_hash_table (DEFAULT_OBARRAY_SIZE); - body->imported = imported; - body->imported_observer - = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1); - body->local = local; - body->local_observer - = SCM_ENVIRONMENT_OBSERVE (local, eval_environment_observer, env, 1); - - return env; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_eval_environment_p, "eval-environment?", 1, 0, 0, - (SCM object), - "Return @code{#t} if object is an eval environment, or @code{#f}\n" - "otherwise.") -#define FUNC_NAME s_scm_eval_environment_p -{ - return scm_from_bool (SCM_EVAL_ENVIRONMENT_P (object)); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_eval_environment_local, "eval-environment-local", 1, 0, 0, - (SCM env), - "Return the local environment of eval environment @var{env}.") -#define FUNC_NAME s_scm_eval_environment_local -{ - SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return EVAL_ENVIRONMENT (env)->local; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_eval_environment_set_local_x, "eval-environment-set-local!", 2, 0, 0, - (SCM env, SCM local), - "Change @var{env}'s local environment to @var{local}.") -#define FUNC_NAME s_scm_eval_environment_set_local_x -{ - struct eval_environment *body; - - SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_ENVIRONMENT_P (local), local, SCM_ARG2, FUNC_NAME); - - body = EVAL_ENVIRONMENT (env); - - obarray_remove_all (body->obarray); - SCM_ENVIRONMENT_UNOBSERVE (body->local, body->local_observer); - - body->local = local; - body->local_observer - = SCM_ENVIRONMENT_OBSERVE (local, eval_environment_observer, env, 1); - - core_environments_broadcast (env); - - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_eval_environment_imported, "eval-environment-imported", 1, 0, 0, - (SCM env), - "Return the imported environment of eval environment @var{env}.") -#define FUNC_NAME s_scm_eval_environment_imported -{ - SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return EVAL_ENVIRONMENT (env)->imported; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_eval_environment_set_imported_x, "eval-environment-set-imported!", 2, 0, 0, - (SCM env, SCM imported), - "Change @var{env}'s imported environment to @var{imported}.") -#define FUNC_NAME s_scm_eval_environment_set_imported_x -{ - struct eval_environment *body; - - SCM_ASSERT (SCM_EVAL_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_ENVIRONMENT_P (imported), imported, SCM_ARG2, FUNC_NAME); - - body = EVAL_ENVIRONMENT (env); - - obarray_remove_all (body->obarray); - SCM_ENVIRONMENT_UNOBSERVE (body->imported, body->imported_observer); - - body->imported = imported; - body->imported_observer - = SCM_ENVIRONMENT_OBSERVE (imported, eval_environment_observer, env, 1); - - core_environments_broadcast (env); - - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - - - -/* import environments - * - * An import environment combines the bindings of a set of argument - * environments, and checks for naming clashes. - * - * Implementation: The import environment does no caching at all. For every - * access, the list of imported environments is scanned. - */ - - -struct import_environment { - struct core_environments_base base; - - SCM imports; - SCM import_observers; - - SCM conflict_proc; -}; - - -#define IMPORT_ENVIRONMENT(env) \ - ((struct import_environment *) SCM_CELL_WORD_1 (env)) - - - -/* Lookup will report one of the following distinct results: - * a) <environment> if only environment binds the symbol. - * b) (env-1 env-2 ...) for conflicting bindings in env-1, ... - * c) SCM_UNDEFINED if there is no binding for the symbol. - */ -static SCM -import_environment_lookup (SCM env, SCM sym) -{ - SCM imports = IMPORT_ENVIRONMENT (env)->imports; - SCM result = SCM_UNDEFINED; - SCM l; - - for (l = imports; !scm_is_null (l); l = SCM_CDR (l)) - { - SCM imported = SCM_CAR (l); - - if (SCM_ENVIRONMENT_BOUND_P (imported, sym)) - { - if (SCM_UNBNDP (result)) - result = imported; - else if (scm_is_pair (result)) - result = scm_cons (imported, result); - else - result = scm_cons2 (imported, result, SCM_EOL); - } - } - - if (scm_is_pair (result)) - return scm_reverse (result); - else - return result; -} - - -static SCM -import_environment_conflict (SCM env, SCM sym, SCM imports) -{ - SCM conflict_proc = IMPORT_ENVIRONMENT (env)->conflict_proc; - SCM args = scm_cons2 (env, sym, scm_cons (imports, SCM_EOL)); - - return scm_apply_0 (conflict_proc, args); -} - - -static SCM -import_environment_ref (SCM env, SCM sym) -#define FUNC_NAME "import_environment_ref" -{ - SCM owner = import_environment_lookup (env, sym); - - if (SCM_UNBNDP (owner)) - { - return SCM_UNDEFINED; - } - else if (scm_is_pair (owner)) - { - SCM resolve = import_environment_conflict (env, sym, owner); - - if (SCM_ENVIRONMENT_P (resolve)) - return SCM_ENVIRONMENT_REF (resolve, sym); - else - return SCM_UNSPECIFIED; - } - else - { - return SCM_ENVIRONMENT_REF (owner, sym); - } -} -#undef FUNC_NAME - - -static SCM -import_environment_folder (SCM extended_data, SCM symbol, SCM value, SCM tail) -#define FUNC_NAME "import_environment_fold" -{ - SCM import_env = SCM_CAR (extended_data); - SCM imported_env = SCM_CADR (extended_data); - SCM owner = import_environment_lookup (import_env, symbol); - SCM proc_as_nr = SCM_CADDR (extended_data); - unsigned long int proc_as_ul = scm_to_ulong (proc_as_nr); - scm_environment_folder proc = (scm_environment_folder) proc_as_ul; - SCM data = SCM_CDDDR (extended_data); - - if (scm_is_pair (owner) && scm_is_eq (SCM_CAR (owner), imported_env)) - owner = import_environment_conflict (import_env, symbol, owner); - - if (SCM_ENVIRONMENT_P (owner)) - return (*proc) (data, symbol, value, tail); - else /* unresolved conflict */ - return (*proc) (data, symbol, SCM_UNSPECIFIED, tail); -} -#undef FUNC_NAME - - -static SCM -import_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) -{ - SCM proc_as_nr = scm_from_ulong ((unsigned long) proc); - SCM result = init; - SCM l; - - for (l = IMPORT_ENVIRONMENT (env)->imports; !scm_is_null (l); l = SCM_CDR (l)) - { - SCM imported_env = SCM_CAR (l); - SCM extended_data = scm_cons (env, scm_cons2 (imported_env, proc_as_nr, data)); - - result = scm_c_environment_fold (imported_env, import_environment_folder, extended_data, result); - } - - return result; -} - - -static SCM -import_environment_define (SCM env SCM_UNUSED, - SCM sym SCM_UNUSED, - SCM val SCM_UNUSED) -#define FUNC_NAME "import_environment_define" -{ - return SCM_ENVIRONMENT_BINDING_IMMUTABLE; -} -#undef FUNC_NAME - - -static SCM -import_environment_undefine (SCM env SCM_UNUSED, - SCM sym SCM_UNUSED) -#define FUNC_NAME "import_environment_undefine" -{ - return SCM_ENVIRONMENT_BINDING_IMMUTABLE; -} -#undef FUNC_NAME - - -static SCM -import_environment_set_x (SCM env, SCM sym, SCM val) -#define FUNC_NAME "import_environment_set_x" -{ - SCM owner = import_environment_lookup (env, sym); - - if (SCM_UNBNDP (owner)) - { - return SCM_UNDEFINED; - } - else if (scm_is_pair (owner)) - { - SCM resolve = import_environment_conflict (env, sym, owner); - - if (SCM_ENVIRONMENT_P (resolve)) - return SCM_ENVIRONMENT_SET (resolve, sym, val); - else - return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; - } - else - { - return SCM_ENVIRONMENT_SET (owner, sym, val); - } -} -#undef FUNC_NAME - - -static SCM -import_environment_cell (SCM env, SCM sym, int for_write) -#define FUNC_NAME "import_environment_cell" -{ - SCM owner = import_environment_lookup (env, sym); - - if (SCM_UNBNDP (owner)) - { - return SCM_UNDEFINED; - } - else if (scm_is_pair (owner)) - { - SCM resolve = import_environment_conflict (env, sym, owner); - - if (SCM_ENVIRONMENT_P (resolve)) - return SCM_ENVIRONMENT_CELL (resolve, sym, for_write); - else - return SCM_ENVIRONMENT_LOCATION_NO_CELL; - } - else - { - return SCM_ENVIRONMENT_CELL (owner, sym, for_write); - } -} -#undef FUNC_NAME - - - -static int -import_environment_print (SCM type, SCM port, - scm_print_state *pstate SCM_UNUSED) -{ - SCM address = scm_from_size_t (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, scm_from_int (16)); - - scm_puts ("#<import environment ", port); - scm_display (base16, port); - scm_puts (">", port); - - return 1; -} - - -static struct scm_environment_funcs import_environment_funcs = { - import_environment_ref, - import_environment_fold, - import_environment_define, - import_environment_undefine, - import_environment_set_x, - import_environment_cell, - core_environments_observe, - core_environments_unobserve, - import_environment_print -}; - - -void *scm_type_import_environment = &import_environment_funcs; - - -static void -import_environment_observer (SCM caller SCM_UNUSED, SCM import_env) -{ - core_environments_broadcast (import_env); -} - - -SCM_DEFINE (scm_make_import_environment, "make-import-environment", 2, 0, 0, - (SCM imports, SCM conflict_proc), - "Return a new environment @var{imp} whose bindings are the union\n" - "of the bindings from the environments in @var{imports};\n" - "@var{imports} must be a list of environments. That is,\n" - "@var{imp} binds a symbol to a location when some element of\n" - "@var{imports} does.\n" - "If two different elements of @var{imports} have a binding for\n" - "the same symbol, the @var{conflict-proc} is called with the\n" - "following parameters: the import environment, the symbol and\n" - "the list of the imported environments that bind the symbol.\n" - "If the @var{conflict-proc} returns an environment @var{env},\n" - "the conflict is considered as resolved and the binding from\n" - "@var{env} is used. If the @var{conflict-proc} returns some\n" - "non-environment object, the conflict is considered unresolved\n" - "and the symbol is treated as unspecified in the import\n" - "environment.\n" - "The checking for conflicts may be performed lazily, i. e. at\n" - "the moment when a value or binding for a certain symbol is\n" - "requested instead of the moment when the environment is\n" - "created or the bindings of the imports change.\n" - "All bindings in @var{imp} are immutable. If you apply\n" - "@code{environment-define} or @code{environment-undefine} to\n" - "@var{imp}, Guile will signal an\n" - " @code{environment:immutable-binding} error. However,\n" - "notice that the set of bindings in @var{imp} may still change,\n" - "if one of its imported environments changes.") -#define FUNC_NAME s_scm_make_import_environment -{ - size_t size = sizeof (struct import_environment); - struct import_environment *body = scm_gc_malloc (size, "import environment"); - SCM env; - - core_environments_preinit (&body->base); - body->imports = SCM_BOOL_F; - body->import_observers = SCM_BOOL_F; - body->conflict_proc = SCM_BOOL_F; - - env = scm_make_environment (body); - - core_environments_init (&body->base, &import_environment_funcs); - body->imports = SCM_EOL; - body->import_observers = SCM_EOL; - body->conflict_proc = conflict_proc; - - scm_import_environment_set_imports_x (env, imports); - - return env; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_import_environment_p, "import-environment?", 1, 0, 0, - (SCM object), - "Return @code{#t} if object is an import environment, or\n" - "@code{#f} otherwise.") -#define FUNC_NAME s_scm_import_environment_p -{ - return scm_from_bool (SCM_IMPORT_ENVIRONMENT_P (object)); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_import_environment_imports, "import-environment-imports", 1, 0, 0, - (SCM env), - "Return the list of environments imported by the import\n" - "environment @var{env}.") -#define FUNC_NAME s_scm_import_environment_imports -{ - SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return IMPORT_ENVIRONMENT (env)->imports; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_import_environment_set_imports_x, "import-environment-set-imports!", 2, 0, 0, - (SCM env, SCM imports), - "Change @var{env}'s list of imported environments to\n" - "@var{imports}, and check for conflicts.") -#define FUNC_NAME s_scm_import_environment_set_imports_x -{ - struct import_environment *body = IMPORT_ENVIRONMENT (env); - SCM import_observers = SCM_EOL; - SCM l; - - SCM_ASSERT (SCM_IMPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - for (l = imports; scm_is_pair (l); l = SCM_CDR (l)) - { - SCM obj = SCM_CAR (l); - SCM_ASSERT (SCM_ENVIRONMENT_P (obj), imports, SCM_ARG2, FUNC_NAME); - } - SCM_ASSERT (scm_is_null (l), imports, SCM_ARG2, FUNC_NAME); - - for (l = body->import_observers; !scm_is_null (l); l = SCM_CDR (l)) - { - SCM obs = SCM_CAR (l); - SCM_ENVIRONMENT_UNOBSERVE (env, obs); - } - - for (l = imports; !scm_is_null (l); l = SCM_CDR (l)) - { - SCM imp = SCM_CAR (l); - SCM obs = SCM_ENVIRONMENT_OBSERVE (imp, import_environment_observer, env, 1); - import_observers = scm_cons (obs, import_observers); - } - - body->imports = imports; - body->import_observers = import_observers; - - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - - - -/* export environments - * - * An export environment restricts an environment to a specified set of - * bindings. - * - * Implementation: The export environment does no caching at all. For every - * access, the signature is scanned. The signature that is stored internally - * is an alist of pairs (symbol . (mutability)). - */ - - -struct export_environment { - struct core_environments_base base; - - SCM private; - SCM private_observer; - - SCM signature; -}; - - -#define EXPORT_ENVIRONMENT(env) \ - ((struct export_environment *) SCM_CELL_WORD_1 (env)) - - -SCM_SYMBOL (symbol_immutable_location, "immutable-location"); -SCM_SYMBOL (symbol_mutable_location, "mutable-location"); - - - -static SCM -export_environment_ref (SCM env, SCM sym) -#define FUNC_NAME "export_environment_ref" -{ - struct export_environment *body = EXPORT_ENVIRONMENT (env); - SCM entry = scm_assq (sym, body->signature); - - if (scm_is_false (entry)) - return SCM_UNDEFINED; - else - return SCM_ENVIRONMENT_REF (body->private, sym); -} -#undef FUNC_NAME - - -static SCM -export_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init) -{ - struct export_environment *body = EXPORT_ENVIRONMENT (env); - SCM result = init; - SCM l; - - for (l = body->signature; !scm_is_null (l); l = SCM_CDR (l)) - { - SCM symbol = SCM_CAR (l); - SCM value = SCM_ENVIRONMENT_REF (body->private, symbol); - if (!SCM_UNBNDP (value)) - result = (*proc) (data, symbol, value, result); - } - return result; -} - - -static SCM -export_environment_define (SCM env SCM_UNUSED, - SCM sym SCM_UNUSED, - SCM val SCM_UNUSED) -#define FUNC_NAME "export_environment_define" -{ - return SCM_ENVIRONMENT_BINDING_IMMUTABLE; -} -#undef FUNC_NAME - - -static SCM -export_environment_undefine (SCM env SCM_UNUSED, SCM sym SCM_UNUSED) -#define FUNC_NAME "export_environment_undefine" -{ - return SCM_ENVIRONMENT_BINDING_IMMUTABLE; -} -#undef FUNC_NAME - - -static SCM -export_environment_set_x (SCM env, SCM sym, SCM val) -#define FUNC_NAME "export_environment_set_x" -{ - struct export_environment *body = EXPORT_ENVIRONMENT (env); - SCM entry = scm_assq (sym, body->signature); - - if (scm_is_false (entry)) - { - return SCM_UNDEFINED; - } - else - { - if (scm_is_eq (SCM_CADR (entry), symbol_mutable_location)) - return SCM_ENVIRONMENT_SET (body->private, sym, val); - else - return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; - } -} -#undef FUNC_NAME - - -static SCM -export_environment_cell (SCM env, SCM sym, int for_write) -#define FUNC_NAME "export_environment_cell" -{ - struct export_environment *body = EXPORT_ENVIRONMENT (env); - SCM entry = scm_assq (sym, body->signature); - - if (scm_is_false (entry)) - { - return SCM_UNDEFINED; - } - else - { - if (!for_write || scm_is_eq (SCM_CADR (entry), symbol_mutable_location)) - return SCM_ENVIRONMENT_CELL (body->private, sym, for_write); - else - return SCM_ENVIRONMENT_LOCATION_IMMUTABLE; - } -} -#undef FUNC_NAME - - - -static int -export_environment_print (SCM type, SCM port, - scm_print_state *pstate SCM_UNUSED) -{ - SCM address = scm_from_size_t (SCM_UNPACK (type)); - SCM base16 = scm_number_to_string (address, scm_from_int (16)); - - scm_puts ("#<export environment ", port); - scm_display (base16, port); - scm_puts (">", port); - - return 1; -} - - -static struct scm_environment_funcs export_environment_funcs = { - export_environment_ref, - export_environment_fold, - export_environment_define, - export_environment_undefine, - export_environment_set_x, - export_environment_cell, - core_environments_observe, - core_environments_unobserve, - export_environment_print -}; - - -void *scm_type_export_environment = &export_environment_funcs; - - -static void -export_environment_observer (SCM caller SCM_UNUSED, SCM export_env) -{ - core_environments_broadcast (export_env); -} - - -SCM_DEFINE (scm_make_export_environment, "make-export-environment", 2, 0, 0, - (SCM private, SCM signature), - "Return a new environment @var{exp} containing only those\n" - "bindings in private whose symbols are present in\n" - "@var{signature}. The @var{private} argument must be an\n" - "environment.\n\n" - "The environment @var{exp} binds symbol to location when\n" - "@var{env} does, and symbol is exported by @var{signature}.\n\n" - "@var{signature} is a list specifying which of the bindings in\n" - "@var{private} should be visible in @var{exp}. Each element of\n" - "@var{signature} should be a list of the form:\n" - " (symbol attribute ...)\n" - "where each attribute is one of the following:\n" - "@table @asis\n" - "@item the symbol @code{mutable-location}\n" - " @var{exp} should treat the\n" - " location bound to symbol as mutable. That is, @var{exp}\n" - " will pass calls to @code{environment-set!} or\n" - " @code{environment-cell} directly through to private.\n" - "@item the symbol @code{immutable-location}\n" - " @var{exp} should treat\n" - " the location bound to symbol as immutable. If the program\n" - " applies @code{environment-set!} to @var{exp} and symbol, or\n" - " calls @code{environment-cell} to obtain a writable value\n" - " cell, @code{environment-set!} will signal an\n" - " @code{environment:immutable-location} error. Note that, even\n" - " if an export environment treats a location as immutable, the\n" - " underlying environment may treat it as mutable, so its\n" - " value may change.\n" - "@end table\n" - "It is an error for an element of signature to specify both\n" - "@code{mutable-location} and @code{immutable-location}. If\n" - "neither is specified, @code{immutable-location} is assumed.\n\n" - "As a special case, if an element of signature is a lone\n" - "symbol @var{sym}, it is equivalent to an element of the form\n" - "@code{(sym)}.\n\n" - "All bindings in @var{exp} are immutable. If you apply\n" - "@code{environment-define} or @code{environment-undefine} to\n" - "@var{exp}, Guile will signal an\n" - "@code{environment:immutable-binding} error. However,\n" - "notice that the set of bindings in @var{exp} may still change,\n" - "if the bindings in private change.") -#define FUNC_NAME s_scm_make_export_environment -{ - size_t size; - struct export_environment *body; - SCM env; - - SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG1, FUNC_NAME); - - size = sizeof (struct export_environment); - body = scm_gc_malloc (size, "export environment"); - - core_environments_preinit (&body->base); - body->private = SCM_BOOL_F; - body->private_observer = SCM_BOOL_F; - body->signature = SCM_BOOL_F; - - env = scm_make_environment (body); - - core_environments_init (&body->base, &export_environment_funcs); - body->private = private; - body->private_observer - = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer, env, 1); - body->signature = SCM_EOL; - - scm_export_environment_set_signature_x (env, signature); - - return env; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_export_environment_p, "export-environment?", 1, 0, 0, - (SCM object), - "Return @code{#t} if object is an export environment, or\n" - "@code{#f} otherwise.") -#define FUNC_NAME s_scm_export_environment_p -{ - return scm_from_bool (SCM_EXPORT_ENVIRONMENT_P (object)); -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_export_environment_private, "export-environment-private", 1, 0, 0, - (SCM env), - "Return the private environment of export environment @var{env}.") -#define FUNC_NAME s_scm_export_environment_private -{ - SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return EXPORT_ENVIRONMENT (env)->private; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_export_environment_set_private_x, "export-environment-set-private!", 2, 0, 0, - (SCM env, SCM private), - "Change the private environment of export environment @var{env}.") -#define FUNC_NAME s_scm_export_environment_set_private_x -{ - struct export_environment *body; - - SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - SCM_ASSERT (SCM_ENVIRONMENT_P (private), private, SCM_ARG2, FUNC_NAME); - - body = EXPORT_ENVIRONMENT (env); - SCM_ENVIRONMENT_UNOBSERVE (private, body->private_observer); - - body->private = private; - body->private_observer - = SCM_ENVIRONMENT_OBSERVE (private, export_environment_observer, env, 1); - - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - - -SCM_DEFINE (scm_export_environment_signature, "export-environment-signature", 1, 0, 0, - (SCM env), - "Return the signature of export environment @var{env}.") -#define FUNC_NAME s_scm_export_environment_signature -{ - SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - - return EXPORT_ENVIRONMENT (env)->signature; -} -#undef FUNC_NAME - - -static SCM -export_environment_parse_signature (SCM signature, const char* caller) -{ - SCM result = SCM_EOL; - SCM l; - - for (l = signature; scm_is_pair (l); l = SCM_CDR (l)) - { - SCM entry = SCM_CAR (l); - - if (scm_is_symbol (entry)) - { - SCM new_entry = scm_cons2 (entry, symbol_immutable_location, SCM_EOL); - result = scm_cons (new_entry, result); - } - else - { - SCM sym; - SCM new_entry; - int immutable = 0; - int mutable = 0; - SCM mutability; - SCM l2; - - SCM_ASSERT (scm_is_pair (entry), entry, SCM_ARGn, caller); - SCM_ASSERT (scm_is_symbol (SCM_CAR (entry)), entry, SCM_ARGn, caller); - - sym = SCM_CAR (entry); - - for (l2 = SCM_CDR (entry); scm_is_pair (l2); l2 = SCM_CDR (l2)) - { - SCM attribute = SCM_CAR (l2); - if (scm_is_eq (attribute, symbol_immutable_location)) - immutable = 1; - else if (scm_is_eq (attribute, symbol_mutable_location)) - mutable = 1; - else - SCM_ASSERT (0, entry, SCM_ARGn, caller); - } - SCM_ASSERT (scm_is_null (l2), entry, SCM_ARGn, caller); - SCM_ASSERT (!mutable || !immutable, entry, SCM_ARGn, caller); - - if (!mutable && !immutable) - immutable = 1; - - mutability = mutable ? symbol_mutable_location : symbol_immutable_location; - new_entry = scm_cons2 (sym, mutability, SCM_EOL); - result = scm_cons (new_entry, result); - } - } - SCM_ASSERT (scm_is_null (l), signature, SCM_ARGn, caller); - - /* Dirk:FIXME:: Now we know that signature is syntactically correct. There - * are, however, no checks for symbols entered twice with contradicting - * mutabilities. It would be nice, to implement this test, to be able to - * call the sort functions conveniently from C. - */ - - return scm_reverse (result); -} - - -SCM_DEFINE (scm_export_environment_set_signature_x, "export-environment-set-signature!", 2, 0, 0, - (SCM env, SCM signature), - "Change the signature of export environment @var{env}.") -#define FUNC_NAME s_scm_export_environment_set_signature_x -{ - SCM parsed_sig; - - SCM_ASSERT (SCM_EXPORT_ENVIRONMENT_P (env), env, SCM_ARG1, FUNC_NAME); - parsed_sig = export_environment_parse_signature (signature, FUNC_NAME); - - EXPORT_ENVIRONMENT (env)->signature = parsed_sig; - - return SCM_UNSPECIFIED; -} -#undef FUNC_NAME - - - -void -scm_environments_prehistory () -{ - /* create environment smob */ - scm_tc16_environment = scm_make_smob_type ("environment", 0); - scm_set_smob_print (scm_tc16_environment, environment_print); - - /* create observer smob */ - scm_tc16_observer = scm_make_smob_type ("observer", 0); - scm_set_smob_print (scm_tc16_observer, observer_print); - - /* create system environment */ - scm_system_environment = scm_make_leaf_environment (); - scm_permanent_object (scm_system_environment); -} - - -void -scm_init_environments () -{ -#include "libguile/environments.x" -} - - -/* - Local Variables: - c-file-style: "gnu" - End: -*/ diff --git a/libguile/environments.h b/libguile/environments.h deleted file mode 100644 index 143963254..000000000 --- a/libguile/environments.h +++ /dev/null @@ -1,187 +0,0 @@ -/* classes: h_files */ - -#ifndef SCM_ENVIRONMENTS_H -#define SCM_ENVIRONMENTS_H - -/* Copyright (C) 1999,2000, 2006, 2008 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - - - -#include "libguile/__scm.h" - - - -/* The type for folding functions written in C. A function meant to be passed - * to scm_c_environment_fold should have the type scm_environment_folder. - */ -typedef SCM (*scm_environment_folder) (SCM data, SCM sym, SCM val, SCM tail); - - -/* The type for observer functions written in C. A function meant to be - * passed to scm_c_environment_observe should have the type - * scm_environment_observer. - */ -typedef void (*scm_environment_observer) (SCM env, SCM data); - - -struct scm_environment_funcs { - SCM (*ref) (SCM self, SCM symbol); - SCM (*fold) (SCM self, scm_environment_folder proc, SCM data, SCM init); - - SCM (*define) (SCM self, SCM symbol, SCM value); - SCM (*undefine) (SCM self, SCM symbol); - SCM (*set) (SCM self, SCM symbol, SCM value); - - SCM (*cell) (SCM self, SCM symbol, int for_write); - SCM (*observe) (SCM self, scm_environment_observer proc, SCM data, int weak_p); - void (*unobserve) (SCM self, SCM token); - - int (*print) (SCM self, SCM port, scm_print_state *pstate); -}; - - - -#define SCM_ENVIRONMENT_SUCCESS SCM_BOOL_T -#define SCM_ENVIRONMENT_BINDING_IMMUTABLE scm_from_int (0) -#define SCM_ENVIRONMENT_LOCATION_IMMUTABLE scm_from_int (1) -#define SCM_ENVIRONMENT_LOCATION_NO_CELL SCM_BOOL_F - -SCM_API scm_t_bits scm_tc16_environment; - -#define SCM_ENVIRONMENT_P(x) \ - (!SCM_IMP (x) && SCM_CELL_TYPE (x) == scm_tc16_environment) -#define SCM_ENVIRONMENT_FUNCS(env) \ - (*((struct scm_environment_funcs **) SCM_CELL_WORD_1 (env))) -#define SCM_ENVIRONMENT_BOUND_P(env, symbol) \ - (!SCM_UNBNDP (SCM_ENVIRONMENT_REF (env, symbol))) -#define SCM_ENVIRONMENT_REF(env, symbol) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->ref)) (env, symbol)) -#define SCM_ENVIRONMENT_FOLD(env, proc, data, init) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->fold)) (env, proc, data, init)) -#define SCM_ENVIRONMENT_DEFINE(env, symbol, value) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->define)) (env, symbol, value)) -#define SCM_ENVIRONMENT_UNDEFINE(env, symbol) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->undefine)) (env, symbol)) -#define SCM_ENVIRONMENT_SET(env, symbol, value) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->set)) (env, symbol, value)) -#define SCM_ENVIRONMENT_CELL(env, symbol, for_write) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->cell)) (env, symbol, for_write)) -#define SCM_ENVIRONMENT_OBSERVE(env, proc, data, weak_p) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->observe)) (env, proc, data, weak_p)) -#define SCM_ENVIRONMENT_UNOBSERVE(env, token) \ - ((*(SCM_ENVIRONMENT_FUNCS (env)->unobserve)) (env, token)) - -SCM_API scm_t_bits scm_tc16_observer; - -#define SCM_OBSERVER_P(x) \ - (!SCM_IMP (x) && (SCM_CELL_TYPE (x) == scm_tc16_observer)) -#define SCM_OBSERVER_ENVIRONMENT(x) \ - (SCM_CELL_OBJECT_1 (x)) -#define SCM_OBSERVER_DATA(x) \ - (SCM_CELL_OBJECT_2 (x)) -#define SCM_OBSERVER_PROC(x) \ - ((scm_environment_observer) SCM_CELL_WORD_3 (x)) - -SCM_API SCM scm_system_environment; - -SCM_API void scm_error_environment_unbound (const char *, SCM, SCM) SCM_NORETURN; -SCM_API void scm_error_environment_immutable_binding (const char *, SCM, SCM) SCM_NORETURN; -SCM_API void scm_error_environment_immutable_location (const char *, SCM, SCM) SCM_NORETURN; - -SCM_API SCM scm_make_environment (void *type); -SCM_API SCM scm_environment_p (SCM env); -SCM_API SCM scm_environment_bound_p (SCM env, SCM sym); -SCM_API SCM scm_environment_ref (SCM env, SCM sym); -SCM_API SCM scm_c_environment_ref (SCM env, SCM sym); -SCM_API SCM scm_environment_fold (SCM env, SCM proc, SCM init); -SCM_API SCM scm_c_environment_fold (SCM env, scm_environment_folder proc, SCM data, SCM init); -SCM_API SCM scm_environment_define (SCM env, SCM sym, SCM val); -SCM_API SCM scm_environment_undefine (SCM env, SCM sym); -SCM_API SCM scm_environment_set_x (SCM env, SCM sym, SCM val); -SCM_API SCM scm_environment_cell (SCM env, SCM sym, SCM for_write); -SCM_API SCM scm_c_environment_cell (SCM env, SCM sym, int for_write); -SCM_API SCM scm_environment_observe (SCM env, SCM proc); -SCM_API SCM scm_environment_observe_weak (SCM env, SCM proc); -SCM_API SCM scm_c_environment_observe (SCM env, scm_environment_observer proc, SCM data, int weak_p); -SCM_API SCM scm_environment_unobserve (SCM token); - -SCM_INTERNAL void scm_environments_prehistory (void); -SCM_INTERNAL void scm_init_environments (void); - - - -SCM_API void *scm_type_leaf_environment; - -#define SCM_LEAF_ENVIRONMENT_P(env) \ - (SCM_ENVIRONMENT_P (env) \ - && SCM_ENVIRONMENT_FUNCS (env) == scm_type_leaf_environment) - -SCM_API SCM scm_make_leaf_environment (void); -SCM_API SCM scm_leaf_environment_p (SCM env); - - - -SCM_API void *scm_type_eval_environment; - -#define SCM_EVAL_ENVIRONMENT_P(env) \ - (SCM_ENVIRONMENT_P (env) \ - && SCM_ENVIRONMENT_FUNCS (env) == scm_type_eval_environment) - -SCM_API SCM scm_make_eval_environment (SCM local, SCM imported); -SCM_API SCM scm_eval_environment_p (SCM env); -SCM_API SCM scm_eval_environment_local (SCM env); -SCM_API SCM scm_eval_environment_set_local_x (SCM env, SCM local); -SCM_API SCM scm_eval_environment_imported (SCM env); -SCM_API SCM scm_eval_environment_set_imported_x (SCM env, SCM imported); - - - -SCM_API void *scm_type_import_environment; - -#define SCM_IMPORT_ENVIRONMENT_P(env) \ - (SCM_ENVIRONMENT_P (env) \ - && SCM_ENVIRONMENT_FUNCS (env) == scm_type_import_environment) - -SCM_API SCM scm_make_import_environment (SCM imports, SCM conflict_proc); -SCM_API SCM scm_import_environment_p (SCM env); -SCM_API SCM scm_import_environment_imports (SCM env); -SCM_API SCM scm_import_environment_set_imports_x (SCM env, SCM imports); - - - -SCM_API void *scm_type_export_environment; - -#define SCM_EXPORT_ENVIRONMENT_P(env) \ - (SCM_ENVIRONMENT_P (env) \ - && SCM_ENVIRONMENT_FUNCS (env) == scm_type_export_environment) - -SCM_API SCM scm_make_export_environment (SCM private, SCM signature); -SCM_API SCM scm_export_environment_p (SCM env); -SCM_API SCM scm_export_environment_private (SCM env); -SCM_API SCM scm_export_environment_set_private_x (SCM env, SCM private); -SCM_API SCM scm_export_environment_signature (SCM env); -SCM_API SCM scm_export_environment_set_signature_x (SCM env, SCM signature); - -#endif /* SCM_ENVIRONMENTS_H */ - -/* - Local Variables: - c-file-style: "gnu" - End: -*/ diff --git a/libguile/eval.c b/libguile/eval.c index e58c05410..cdb90423a 100644 --- a/libguile/eval.c +++ b/libguile/eval.c @@ -41,7 +41,6 @@ #include "libguile/eq.h" #include "libguile/feature.h" #include "libguile/fluids.h" -#include "libguile/futures.h" #include "libguile/goops.h" #include "libguile/hash.h" #include "libguile/hashtab.h" @@ -409,7 +408,6 @@ static const char *const isymnames[] = "#@slot-ref", "#@slot-set!", "#@delay", - "#@future", "#@call-with-values", "#@else", "#@arrow", @@ -784,9 +782,6 @@ static SCM scm_m_do (SCM xorig, SCM env); static SCM scm_m_quasiquote (SCM xorig, SCM env); static SCM scm_m_delay (SCM xorig, SCM env); static SCM scm_m_generalized_set_x (SCM xorig, SCM env); -#if 0 /* Futures are disabled, see "futures.h". */ -static SCM scm_m_future (SCM xorig, SCM env); -#endif static SCM scm_m_define (SCM x, SCM env); static SCM scm_m_letrec (SCM xorig, SCM env); static SCM scm_m_let (SCM xorig, SCM env); @@ -2195,36 +2190,6 @@ scm_m_eval_when (SCM expr, SCM env SCM_UNUSED) return scm_list_1 (SCM_IM_BEGIN); } -#if 0 - -/* See futures.h for a comment why futures are not enabled. - */ - -SCM_SYNTAX (s_future, "future", scm_i_makbimacro, scm_m_future); -SCM_GLOBAL_SYMBOL (scm_sym_future, "future"); - -/* Like promises, futures are implemented as closures with an empty - * parameter list. Thus, (future <expression>) is transformed into - * (#@future '() <expression>), where the empty list represents the - * empty parameter list. This representation allows for easy creation - * of the closure during evaluation. */ -static SCM -scm_m_future (SCM expr, SCM env) -{ - const SCM new_expr = memoize_as_thunk_prototype (expr, env); - SCM_SETCAR (new_expr, SCM_IM_FUTURE); - return new_expr; -} - -static SCM -unmemoize_future (const SCM expr, const SCM env) -{ - const SCM thunk_expr = SCM_CADDR (expr); - return scm_list_2 (scm_sym_future, unmemoize_expression (thunk_expr, env)); -} - -#endif /* futures disabled. */ - SCM_SYNTAX (s_gset_x, "set!", scm_i_makbimacro, scm_m_generalized_set_x); SCM_SYMBOL (scm_sym_setter, "setter"); @@ -2493,13 +2458,6 @@ unmemoize_builtin_macro (const SCM expr, const SCM env) case (ISYMNUM (SCM_IM_CALL_WITH_VALUES)): return unmemoize_at_call_with_values (expr, env); -#if 0 - /* See futures.h for a comment why futures are not enabled. - */ - case (ISYMNUM (SCM_IM_FUTURE)): - return unmemoize_future (expr, env); -#endif - case (ISYMNUM (SCM_IM_SLOT_REF)): return unmemoize_atslot_ref (expr, env); diff --git a/libguile/eval.h b/libguile/eval.h index 4467358f5..522f639d8 100644 --- a/libguile/eval.h +++ b/libguile/eval.h @@ -157,9 +157,9 @@ SCM_INTERNAL void scm_init_eval (void); #if (SCM_ENABLE_DEPRECATED == 1) /* Deprecated in guile 1.7.0 on 2004-03-29. */ -SCM_API SCM scm_ceval (SCM x, SCM env); -SCM_API SCM scm_deval (SCM x, SCM env); -SCM_API SCM (*scm_ceval_ptr) (SCM x, SCM env); +SCM_DEPRECATED SCM scm_ceval (SCM x, SCM env); +SCM_DEPRECATED SCM scm_deval (SCM x, SCM env); +SCM_DEPRECATED SCM (*scm_ceval_ptr) (SCM x, SCM env); #endif diff --git a/libguile/eval.i.c b/libguile/eval.i.c index 25abf6cb9..5b4604a7a 100644 --- a/libguile/eval.i.c +++ b/libguile/eval.i.c @@ -304,7 +304,7 @@ dispatch: while (!scm_is_null (SCM_CDR (x))) { SCM test_result = EVALCAR (x, env); - if (scm_is_false (test_result) || SCM_NILP (test_result)) + if (scm_is_false_or_nil (test_result)) RETURN (SCM_BOOL_F); else x = SCM_CDR (x); @@ -442,8 +442,7 @@ dispatch: xx = SCM_CDR (clause); proc = EVALCAR (xx, env); guard_result = SCM_APPLY (proc, arg1, SCM_EOL); - if (scm_is_true (guard_result) - && !SCM_NILP (guard_result)) + if (scm_is_true_and_not_nil (guard_result)) { proc = SCM_CDDR (xx); proc = EVALCAR (proc, env); @@ -451,7 +450,7 @@ dispatch: goto apply_proc; } } - else if (scm_is_true (arg1) && !SCM_NILP (arg1)) + else if (scm_is_true_and_not_nil (arg1)) { x = SCM_CDR (clause); if (scm_is_null (x)) @@ -498,7 +497,7 @@ dispatch: SCM test_result = EVALCAR (test_form, env); - while (scm_is_false (test_result) || SCM_NILP (test_result)) + while (scm_is_false_or_nil (test_result)) { { /* Evaluate body forms. */ @@ -552,7 +551,7 @@ dispatch: { SCM test_result = EVALCAR (x, env); x = SCM_CDR (x); /* then expression */ - if (scm_is_false (test_result) || SCM_NILP (test_result)) + if (scm_is_false_or_nil (test_result)) { x = SCM_CDR (x); /* else expression */ if (scm_is_null (x)) @@ -623,7 +622,7 @@ dispatch: while (!scm_is_null (SCM_CDR (x))) { SCM val = EVALCAR (x, env); - if (scm_is_true (val) && !SCM_NILP (val)) + if (scm_is_true_and_not_nil (val)) RETURN (val); else x = SCM_CDR (x); @@ -735,13 +734,6 @@ dispatch: case (ISYMNUM (SCM_IM_DELAY)): RETURN (scm_make_promise (scm_closure (SCM_CDR (x), env))); -#if 0 - /* See futures.h for a comment why futures are not enabled. - */ - case (ISYMNUM (SCM_IM_FUTURE)): - RETURN (scm_i_make_future (scm_closure (SCM_CDR (x), env))); -#endif - /* PLACEHOLDER for case (ISYMNUM (SCM_IM_DISPATCH)): The following code (type_dispatch) is intended to be the tail of the case clause for the internal macro SCM_IM_DISPATCH. Please don't @@ -1259,7 +1251,7 @@ dispatch: RETURN (SCM_SMOB_APPLY_1 (proc, arg1)); case scm_tc7_gsubr: #ifdef DEVAL - debug.info->a.args = scm_cons (arg1, debug.info->a.args); + debug.info->a.args = debug.info->a.args; debug.info->a.proc = proc; #endif RETURN (scm_i_gsubr_apply (proc, arg1, SCM_UNDEFINED)); @@ -1896,7 +1888,7 @@ tail: #ifdef DEVAL args = (SCM_UNBNDP(arg1) ? SCM_EOL : debug.vect[0].a.args); debug.vect[0].a.proc = proc; - debug.vect[0].a.args = scm_cons (arg1, args); + debug.vect[0].a.args = args; #else args = (SCM_UNBNDP(arg1) ? SCM_EOL : scm_cons (arg1, args)); #endif diff --git a/libguile/frames.c b/libguile/frames.c index a6835fbb4..39f78e09a 100644 --- a/libguile/frames.c +++ b/libguile/frames.c @@ -80,32 +80,19 @@ SCM_DEFINE (scm_vm_frame_program, "vm-frame-program", 1, 0, 0, } #undef FUNC_NAME -SCM_DEFINE (scm_vm_frame_arguments, "vm-frame-arguments", 1, 0, 0, - (SCM frame), - "") -#define FUNC_NAME s_scm_vm_frame_arguments +SCM +scm_vm_frame_arguments (SCM frame) +#define FUNC_NAME "vm-frame-arguments" { - SCM *fp; - int i; - struct scm_objcode *bp; - SCM ret; + static SCM var = SCM_BOOL_F; SCM_VALIDATE_VM_FRAME (1, frame); - fp = SCM_VM_FRAME_FP (frame); - bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp)); + if (scm_is_false (var)) + var = scm_c_module_lookup (scm_c_resolve_module ("system vm frame"), + "vm-frame-arguments"); - if (!bp->nargs) - return SCM_EOL; - else if (bp->nrest) - ret = SCM_FRAME_VARIABLE (fp, bp->nargs - 1); - else - ret = scm_cons (SCM_FRAME_VARIABLE (fp, bp->nargs - 1), SCM_EOL); - - for (i = bp->nargs - 2; i >= 0; i--) - ret = scm_cons (SCM_FRAME_VARIABLE (fp, i), ret); - - return ret; + return scm_call_1 (SCM_VARIABLE_REF (var), frame); } #undef FUNC_NAME @@ -127,47 +114,114 @@ SCM_DEFINE (scm_vm_frame_source, "vm-frame-source", 1, 0, 0, } #undef FUNC_NAME +/* The number of locals would be a simple thing to compute, if it weren't for + the presence of not-yet-active frames on the stack. So we have a cheap + heuristic to detect not-yet-active frames, and skip over them. Perhaps we + should represent them more usefully. + */ +SCM_DEFINE (scm_vm_frame_num_locals, "vm-frame-num-locals", 1, 0, 0, + (SCM frame), + "") +#define FUNC_NAME s_scm_vm_frame_num_locals +{ + SCM *sp, *p; + unsigned int n = 0; + + SCM_VALIDATE_VM_FRAME (1, frame); + + sp = SCM_VM_FRAME_SP (frame); + p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); + while (p <= sp) + { + if (p + 1 < sp && p[1] == (SCM)0) + /* skip over not-yet-active frame */ + p += 3; + else + { + p++; + n++; + } + } + return scm_from_uint (n); +} +#undef FUNC_NAME + +/* Need same not-yet-active frame logic here as in vm-frame-num-locals */ SCM_DEFINE (scm_vm_frame_local_ref, "vm-frame-local-ref", 2, 0, 0, (SCM frame, SCM index), "") #define FUNC_NAME s_scm_vm_frame_local_ref { - SCM *fp; + SCM *sp, *p; + unsigned int n = 0; unsigned int i; - struct scm_objcode *bp; - - SCM_VALIDATE_VM_FRAME (1, frame); - - fp = SCM_VM_FRAME_FP (frame); - bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp)); + SCM_VALIDATE_VM_FRAME (1, frame); SCM_VALIDATE_UINT_COPY (2, index, i); - SCM_ASSERT_RANGE (2, index, i < bp->nargs + bp->nlocs); - return SCM_FRAME_VARIABLE (fp, i); + sp = SCM_VM_FRAME_SP (frame); + p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); + while (p <= sp) + { + if (p + 1 < sp && p[1] == (SCM)0) + /* skip over not-yet-active frame */ + p += 3; + else if (n == i) + return *p; + else + { + p++; + n++; + } + } + SCM_OUT_OF_RANGE (SCM_ARG2, index); } #undef FUNC_NAME +/* Need same not-yet-active frame logic here as in vm-frame-num-locals */ SCM_DEFINE (scm_vm_frame_local_set_x, "vm-frame-local-set!", 3, 0, 0, (SCM frame, SCM index, SCM val), "") #define FUNC_NAME s_scm_vm_frame_local_set_x { - SCM *fp; + SCM *sp, *p; + unsigned int n = 0; unsigned int i; - struct scm_objcode *bp; - - SCM_VALIDATE_VM_FRAME (1, frame); - - fp = SCM_VM_FRAME_FP (frame); - bp = SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp)); + SCM_VALIDATE_VM_FRAME (1, frame); SCM_VALIDATE_UINT_COPY (2, index, i); - SCM_ASSERT_RANGE (2, index, i < bp->nargs + bp->nlocs); - SCM_FRAME_VARIABLE (fp, i) = val; + sp = SCM_VM_FRAME_SP (frame); + p = SCM_FRAME_STACK_ADDRESS (SCM_VM_FRAME_FP (frame)); + while (p <= sp) + { + if (p + 1 < sp && p[1] == (SCM)0) + /* skip over not-yet-active frame */ + p += 3; + else if (n == i) + { + *p = val; + return SCM_UNSPECIFIED; + } + else + { + p++; + n++; + } + } + SCM_OUT_OF_RANGE (SCM_ARG2, index); +} +#undef FUNC_NAME - return SCM_UNSPECIFIED; +SCM_DEFINE (scm_vm_frame_instruction_pointer, "vm-frame-instruction-pointer", 1, 0, 0, + (SCM frame), + "") +#define FUNC_NAME s_scm_vm_frame_instruction_pointer +{ + SCM_VALIDATE_VM_FRAME (1, frame); + return scm_from_ulong ((unsigned long) + (SCM_VM_FRAME_IP (frame) + - SCM_PROGRAM_DATA (scm_vm_frame_program (frame))->base)); } #undef FUNC_NAME @@ -209,24 +263,6 @@ SCM_DEFINE (scm_vm_frame_dynamic_link, "vm-frame-dynamic-link", 1, 0, 0, } #undef FUNC_NAME -SCM_DEFINE (scm_vm_frame_stack, "vm-frame-stack", 1, 0, 0, - (SCM frame), - "") -#define FUNC_NAME s_scm_vm_frame_stack -{ - SCM *top, *bottom, ret = SCM_EOL; - - SCM_VALIDATE_VM_FRAME (1, frame); - - top = SCM_VM_FRAME_SP (frame); - bottom = SCM_FRAME_UPPER_ADDRESS (SCM_VM_FRAME_FP (frame)); - while (bottom <= top) - ret = scm_cons (*bottom++, ret); - - return ret; -} -#undef FUNC_NAME - extern SCM scm_c_vm_frame_prev (SCM frame) { diff --git a/libguile/frames.h b/libguile/frames.h index 0165924a7..f744c2b24 100644 --- a/libguile/frames.h +++ b/libguile/frames.h @@ -31,15 +31,15 @@ --------------- | ... | - | Intermed. val. 0 | <- fp + bp->nargs + bp->nlocs = SCM_FRAME_UPPER_ADDRESS (fp) - +==================+ + | Intermed. val. 0 | <- fp + nargs + nlocs + +------------------+ | Local variable 1 | - | Local variable 0 | <- fp + bp->nargs + | Local variable 0 | <- fp + nargs | Argument 1 | - | Argument 0 | <- fp + | Argument 0 | <- fp = SCM_FRAME_STACK_ADDRESS (fp) | Program | <- fp - 1 - +------------------+ - | Return address | + +==================+ + | Return address | <- SCM_FRAME_UPPER_ADDRESS (fp) | MV return address| | Dynamic link | <- fp - 4 = SCM_FRAME_DATA_ADDRESS (fp) = SCM_FRAME_LOWER_ADDRESS (fp) +==================+ @@ -50,10 +50,8 @@ assumed to be as long as SCM objects. */ #define SCM_FRAME_DATA_ADDRESS(fp) (fp - 4) -#define SCM_FRAME_UPPER_ADDRESS(fp) \ - (fp \ - + SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp))->nargs \ - + SCM_PROGRAM_DATA (SCM_FRAME_PROGRAM (fp))->nlocs) +#define SCM_FRAME_STACK_ADDRESS(fp) (fp) +#define SCM_FRAME_UPPER_ADDRESS(fp) (fp - 2) #define SCM_FRAME_LOWER_ADDRESS(fp) (fp - 4) #define SCM_FRAME_BYTE_CAST(x) ((scm_t_uint8 *) SCM_UNPACK (x)) @@ -71,8 +69,8 @@ (SCM_FRAME_STACK_CAST (SCM_FRAME_DATA_ADDRESS (fp)[0])) #define SCM_FRAME_SET_DYNAMIC_LINK(fp, dl) \ ((SCM_FRAME_DATA_ADDRESS (fp)[0])) = (SCM)(dl); -#define SCM_FRAME_VARIABLE(fp,i) fp[i] -#define SCM_FRAME_PROGRAM(fp) fp[-1] +#define SCM_FRAME_VARIABLE(fp,i) SCM_FRAME_STACK_ADDRESS (fp)[i] +#define SCM_FRAME_PROGRAM(fp) SCM_FRAME_STACK_ADDRESS (fp)[-1] /* @@ -105,12 +103,13 @@ SCM_API SCM scm_vm_frame_p (SCM obj); SCM_API SCM scm_vm_frame_program (SCM frame); SCM_API SCM scm_vm_frame_arguments (SCM frame); SCM_API SCM scm_vm_frame_source (SCM frame); +SCM_API SCM scm_vm_frame_num_locals (SCM frame); SCM_API SCM scm_vm_frame_local_ref (SCM frame, SCM index); SCM_API SCM scm_vm_frame_local_set_x (SCM frame, SCM index, SCM val); +SCM_API SCM scm_vm_frame_instruction_pointer (SCM frame); SCM_API SCM scm_vm_frame_return_address (SCM frame); SCM_API SCM scm_vm_frame_mv_return_address (SCM frame); SCM_API SCM scm_vm_frame_dynamic_link (SCM frame); -SCM_API SCM scm_vm_frame_stack (SCM frame); SCM_API SCM scm_c_vm_frame_prev (SCM frame); diff --git a/libguile/futures.c b/libguile/futures.c deleted file mode 100644 index b330f4ded..000000000 --- a/libguile/futures.c +++ /dev/null @@ -1,375 +0,0 @@ -/* Copyright (C) 1995,1996,1997,1998,2000,2001, 2002, 2003, 2006, 2008 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - - - - -#if 0 - -/* This whole file is not being compiled. See futures.h for the - reason. -*/ - -#ifdef HAVE_CONFIG_H -# include <config.h> -#endif - -#include "libguile/_scm.h" -#include "libguile/eval.h" -#include "libguile/ports.h" -#include "libguile/validate.h" -#include "libguile/stime.h" -#include "libguile/threads.h" - -#include "libguile/futures.h" - -#define LINK(list, obj) \ -do { \ - SCM_SET_FUTURE_NEXT (obj, list); \ - list = obj; \ -} while (0) - -#define UNLINK(list, obj) \ -do { \ - obj = list; \ - list = SCM_FUTURE_NEXT (list); \ -} while (0) - -scm_i_pthread_mutex_t future_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; - -static SCM futures = SCM_EOL; -static SCM young = SCM_EOL; -static SCM old = SCM_EOL; -static SCM undead = SCM_EOL; - -static long last_switch; - -#ifdef SCM_FUTURES_DEBUG -static int n_dead = 0; - -static SCM -count (SCM ls) -{ - int n = 0; - while (!scm_is_null (ls)) - { - ++n; - ls = SCM_FUTURE_NEXT (ls); - } - return scm_from_int (n); -} - -extern SCM scm_future_cache_status (void); - -SCM_DEFINE (scm_future_cache_status, "future-cache-status", 0, 0, 0, - (), - "Return a list containing number of futures, youngs, olds, undeads and deads.") -#define FUNC_NAME s_scm_future_cache_status -{ - int nd = n_dead; - n_dead = 0; - return scm_list_5 (count (futures), - count (young), - count (old), - count (undead), - scm_from_int (nd)); -} -#undef FUNC_NAME - -#endif - -SCM *scm_loc_sys_thread_handler; - -SCM_DEFINE (scm_make_future, "make-future", 1, 0, 0, - (SCM thunk), - "Make a future evaluating THUNK.") -#define FUNC_NAME s_scm_make_future -{ - SCM_VALIDATE_THUNK (1, thunk); - return scm_i_make_future (thunk); -} -#undef FUNC_NAME - -static char *s_future = "future"; - -static void -cleanup (scm_t_future *future) -{ - scm_i_pthread_mutex_destroy (&future->mutex); - scm_i_pthread_cond_destroy (&future->cond); - scm_gc_free (future, sizeof (*future), s_future); -#ifdef SCM_FUTURES_DEBUG - ++n_dead; -#endif -} - -static SCM -future_loop (scm_t_future *future) -{ - scm_i_scm_pthread_mutex_lock (&future->mutex); - do { - if (future->status == SCM_FUTURE_SIGNAL_ME) - scm_i_pthread_cond_broadcast (&future->cond); - future->status = SCM_FUTURE_COMPUTING; - future->data = (SCM_CLOSUREP (future->data) - ? scm_i_call_closure_0 (future->data) - : scm_call_0 (future->data)); - scm_i_scm_pthread_cond_wait (&future->cond, &future->mutex); - } while (!future->die_p); - future->status = SCM_FUTURE_DEAD; - scm_i_pthread_mutex_unlock (&future->mutex); - return SCM_UNSPECIFIED; -} - -static SCM -future_handler (scm_t_future *future, SCM key, SCM args) -{ - future->status = SCM_FUTURE_DEAD; - scm_i_pthread_mutex_unlock (&future->mutex); - return scm_apply_1 (*scm_loc_sys_thread_handler, key, args); -} - -static SCM -alloc_future (SCM thunk) -{ - scm_t_future *f = scm_gc_malloc (sizeof (*f), s_future); - SCM future; - f->data = SCM_BOOL_F; - scm_i_pthread_mutex_init (&f->mutex, NULL); - scm_i_pthread_cond_init (&f->cond, NULL); - f->die_p = 0; - f->status = SCM_FUTURE_TASK_ASSIGNED; - scm_i_scm_pthread_mutex_lock (&future_admin_mutex); - SCM_NEWSMOB2 (future, scm_tc16_future, futures, f); - SCM_SET_FUTURE_DATA (future, thunk); - futures = future; - scm_i_pthread_mutex_unlock (&future_admin_mutex); - scm_spawn_thread ((scm_t_catch_body) future_loop, - SCM_FUTURE (future), - (scm_t_catch_handler) future_handler, - SCM_FUTURE (future)); - return future; -} - -static void -kill_future (SCM future) -{ - SCM_FUTURE (future)->die_p = 1; - LINK (undead, future); -} - -SCM -scm_i_make_future (SCM thunk) -{ - SCM future; - scm_i_scm_pthread_mutex_lock (&future_admin_mutex); - while (1) - { - if (!scm_is_null (old)) - UNLINK (old, future); - else if (!scm_is_null (young)) - UNLINK (young, future); - else - { - scm_i_pthread_mutex_unlock (&future_admin_mutex); - return alloc_future (thunk); - } - if (scm_i_pthread_mutex_trylock (SCM_FUTURE_MUTEX (future))) - kill_future (future); - else if (!SCM_FUTURE_ALIVE_P (future)) - { - scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future)); - cleanup (SCM_FUTURE (future)); - } - else - break; - } - LINK (futures, future); - scm_i_pthread_mutex_unlock (&future_admin_mutex); - SCM_SET_FUTURE_DATA (future, thunk); - SCM_SET_FUTURE_STATUS (future, SCM_FUTURE_TASK_ASSIGNED); - scm_i_pthread_cond_signal (SCM_FUTURE_COND (future)); - scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future)); - return future; -} - - -static int -future_print (SCM exp, SCM port, scm_print_state *pstate) -{ - int writingp = SCM_WRITINGP (pstate); - scm_puts ("#<future ", port); - SCM_SET_WRITINGP (pstate, 1); - scm_iprin1 (SCM_FUTURE_DATA (exp), port, pstate); - SCM_SET_WRITINGP (pstate, writingp); - scm_putc ('>', port); - return !0; -} - -SCM_DEFINE (scm_future_ref, "future-ref", 1, 0, 0, - (SCM future), - "If the future @var{x} has not been computed yet, compute and\n" - "return @var{x}, otherwise just return the previously computed\n" - "value.") -#define FUNC_NAME s_scm_future_ref -{ - SCM res; - SCM_VALIDATE_FUTURE (1, future); - scm_i_scm_pthread_mutex_lock (SCM_FUTURE_MUTEX (future)); - if (SCM_FUTURE_STATUS (future) != SCM_FUTURE_COMPUTING) - { - SCM_SET_FUTURE_STATUS (future, SCM_FUTURE_SIGNAL_ME); - scm_i_scm_pthread_cond_wait (SCM_FUTURE_COND (future), - SCM_FUTURE_MUTEX (future)); - } - if (!SCM_FUTURE_ALIVE_P (future)) - { - scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future)); - SCM_MISC_ERROR ("requesting result from failed future ~A", - scm_list_1 (future)); - } - res = SCM_FUTURE_DATA (future); - scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future)); - return res; -} -#undef FUNC_NAME - -static void -kill_futures (SCM victims) -{ - while (!scm_is_null (victims)) - { - SCM future; - UNLINK (victims, future); - kill_future (future); - scm_i_pthread_cond_signal (SCM_FUTURE_COND (future)); - } -} - -static void -cleanup_undead () -{ - SCM next = undead, *nextloc = &undead; - while (!scm_is_null (next)) - { - if (scm_i_pthread_mutex_trylock (SCM_FUTURE_MUTEX (next))) - goto next; - else if (SCM_FUTURE_ALIVE_P (next)) - { - scm_i_pthread_cond_signal (SCM_FUTURE_COND (next)); - scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (next)); - next: - SCM_SET_GC_MARK (next); - nextloc = SCM_FUTURE_NEXTLOC (next); - next = *nextloc; - } - else - { - SCM future; - UNLINK (next, future); - scm_i_pthread_mutex_unlock (SCM_FUTURE_MUTEX (future)); - cleanup (SCM_FUTURE (future)); - *nextloc = next; - } - } -} - -static void -mark_futures (SCM futures) -{ - while (!scm_is_null (futures)) - { - SCM_SET_GC_MARK (futures); - futures = SCM_FUTURE_NEXT (futures); - } -} - -static void * -scan_futures (void *dummy1, void *dummy2, void *dummy3) -{ - SCM next, *nextloc; - - long now = scm_c_get_internal_run_time (); - if (now - last_switch > SCM_TIME_UNITS_PER_SECOND) - { - /* switch out old (> 1 sec), unused futures */ - kill_futures (old); - old = young; - young = SCM_EOL; - last_switch = now; - } - else - mark_futures (young); - - next = futures; - nextloc = &futures; - while (!scm_is_null (next)) - { - if (!SCM_GC_MARK_P (next)) - goto free; - keep: - nextloc = SCM_FUTURE_NEXTLOC (next); - next = *nextloc; - } - goto exit; - while (!scm_is_null (next)) - { - if (SCM_GC_MARK_P (next)) - { - *nextloc = next; - goto keep; - } - free: - { - SCM future; - UNLINK (next, future); - SCM_SET_GC_MARK (future); - LINK (young, future); - } - } - *nextloc = SCM_EOL; - exit: - cleanup_undead (); - mark_futures (old); - return 0; -} - -scm_t_bits scm_tc16_future; - -void -scm_init_futures () -{ - last_switch = scm_c_get_internal_run_time (); - - scm_loc_sys_thread_handler - = SCM_VARIABLE_LOC (scm_c_define ("%thread-handler", SCM_BOOL_F)); - - scm_tc16_future = scm_make_smob_type ("future", 0); - scm_set_smob_print (scm_tc16_future, future_print); - - scm_c_hook_add (&scm_before_sweep_c_hook, scan_futures, 0, 0); -#include "libguile/futures.x" -} - -#endif - -/* - Local Variables: - c-file-style: "gnu" - End: -*/ diff --git a/libguile/futures.h b/libguile/futures.h deleted file mode 100644 index 5d7712e1a..000000000 --- a/libguile/futures.h +++ /dev/null @@ -1,91 +0,0 @@ -/* classes: h_files */ - -#ifndef SCM_FUTURES_H -#define SCM_FUTURES_H - -/* Copyright (C) 2002, 2003, 2006, 2008 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA - */ - - - -#if 0 - -/* Futures have the following known bugs, which should be fixed before - including them in Guile: - - - The implementation of the thread cache needs to be better so that - it behaves reasonable under heavy use. - - - The dynamic state of a thread needs to be properly initialized - when it is retrieved from the cache. -*/ - -#include "libguile/__scm.h" -#include "libguile/threads.h" - - - -typedef struct scm_t_future { - SCM data; - scm_i_pthread_mutex_t mutex; - scm_i_pthread_cond_t cond; - int status; - int die_p; -} scm_t_future; - -#define SCM_FUTURE_DEAD 0 -#define SCM_FUTURE_SIGNAL_ME -1 -#define SCM_FUTURE_COMPUTING 1 -#define SCM_FUTURE_TASK_ASSIGNED 2 - -#define SCM_VALIDATE_FUTURE(pos, obj) \ - SCM_ASSERT_TYPE (SCM_TYP16_PREDICATE (scm_tc16_future, obj), \ - obj, pos, FUNC_NAME, "future"); -#define SCM_FUTURE(future) ((scm_t_future *) SCM_SMOB_DATA_2 (future)) -#define SCM_FUTURE_MUTEX(future) (&SCM_FUTURE (future)->mutex) -#define SCM_FUTURE_COND(future) (&SCM_FUTURE (future)->cond) -#define SCM_FUTURE_STATUS(future) (SCM_FUTURE (future)->status) -#define SCM_SET_FUTURE_STATUS(future, x) \ - do { SCM_FUTURE (future)->status = (x); } while (0) -#define SCM_FUTURE_ALIVE_P(future) (SCM_FUTURE_STATUS (future)) -#define SCM_FUTURE_DATA(future) (SCM_FUTURE (future)->data) -#define SCM_SET_FUTURE_DATA(future, x) \ - do { SCM_FUTURE (future)->data = (x); } while (0) -#define SCM_FUTURE_NEXT SCM_SMOB_OBJECT -#define SCM_FUTURE_NEXTLOC SCM_SMOB_OBJECT_LOC -#define SCM_SET_FUTURE_NEXT SCM_SET_SMOB_OBJECT - -SCM_API scm_t_bits scm_tc16_future; - -extern SCM *scm_loc_sys_thread_handler; - -SCM_INTERNAL SCM scm_i_make_future (SCM thunk); -SCM_API SCM scm_make_future (SCM thunk); -SCM_API SCM scm_future_ref (SCM future); - -void scm_init_futures (void); - -#endif /* Futures are disabled for now. */ - -#endif /* SCM_FUTURES_H */ - -/* - Local Variables: - c-file-style: "gnu" - End: -*/ diff --git a/libguile/gc-malloc.c b/libguile/gc-malloc.c index e48d2cfd1..669f7894b 100644 --- a/libguile/gc-malloc.c +++ b/libguile/gc-malloc.c @@ -205,9 +205,8 @@ scm_gc_malloc (size_t size, const char *what) void * scm_gc_calloc (size_t size, const char *what) { - void *ptr = scm_gc_malloc (size, what); - memset (ptr, 0x0, size); - return ptr; + /* `GC_MALLOC ()' always returns a zeroed buffer. */ + return scm_gc_malloc (size, what); } @@ -236,7 +235,7 @@ scm_gc_free (void *mem, size_t size, const char *what) char * scm_gc_strndup (const char *str, size_t n, const char *what) { - char *dst = GC_MALLOC (n+1); + char *dst = GC_MALLOC_ATOMIC (n + 1); memcpy (dst, str, n); dst[n] = 0; return dst; diff --git a/libguile/gc.c b/libguile/gc.c index d3c53c748..96e3c306f 100644 --- a/libguile/gc.c +++ b/libguile/gc.c @@ -56,7 +56,7 @@ extern unsigned long * __libc_ia64_register_backing_store_base; #include "libguile/gc.h" #include "libguile/dynwind.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" #ifdef GUILE_DEBUG_MALLOC #include "libguile/debug-malloc.h" @@ -146,18 +146,7 @@ scm_assert_cell_valid (SCM cell) */ if (scm_expensive_debug_cell_accesses_p) scm_i_expensive_validation_check (cell); -#if (SCM_DEBUG_MARKING_API == 0) - if (!SCM_GC_MARK_P (cell)) - { - fprintf (stderr, - "scm_assert_cell_valid: this object is unmarked. \n" - "It has been garbage-collected in the last GC run: " - "%lux\n", - (unsigned long) SCM_UNPACK (cell)); - abort (); - } -#endif /* SCM_DEBUG_MARKING_API */ - + scm_i_cell_validation_already_running = 0; /* re-enable */ } } @@ -680,7 +669,6 @@ scm_init_storage () #endif - scm_stand_in_procs = scm_make_weak_key_hash_table (scm_from_int (257)); scm_protects = scm_c_make_hash_table (31); return 0; diff --git a/libguile/gc.h b/libguile/gc.h index 40dab2ff5..34c9b8474 100644 --- a/libguile/gc.h +++ b/libguile/gc.h @@ -123,7 +123,7 @@ typedef struct scm_t_cell SCM_API int scm_debug_cell_accesses_p; SCM_API int scm_expensive_debug_cell_accesses_p; SCM_API int scm_debug_cells_gc_interval ; -void scm_i_expensive_validation_check (SCM cell); +SCM_API void scm_i_expensive_validation_check (SCM cell); #endif SCM_INTERNAL scm_i_pthread_mutex_t scm_i_gc_admin_mutex; @@ -139,11 +139,11 @@ void *scm_ia64_ar_bsp (const void *); #if (SCM_ENABLE_DEPRECATED == 1) -SCM_API size_t scm_default_init_heap_size_1; -SCM_API int scm_default_min_yield_1; -SCM_API size_t scm_default_init_heap_size_2; -SCM_API int scm_default_min_yield_2; -SCM_API size_t scm_default_max_segment_size; +SCM_DEPRECATED size_t scm_default_init_heap_size_1; +SCM_DEPRECATED int scm_default_min_yield_1; +SCM_DEPRECATED size_t scm_default_init_heap_size_2; +SCM_DEPRECATED int scm_default_min_yield_2; +SCM_DEPRECATED size_t scm_default_max_segment_size; #else #define scm_default_init_heap_size_1 deprecated #define scm_default_min_yield_1 deprecated @@ -243,23 +243,23 @@ SCM_API void scm_init_gc (void); #if SCM_ENABLE_DEPRECATED == 1 -SCM_API SCM scm_deprecated_newcell (void); -SCM_API SCM scm_deprecated_newcell2 (void); +SCM_DEPRECATED SCM scm_deprecated_newcell (void); +SCM_DEPRECATED SCM scm_deprecated_newcell2 (void); #define SCM_NEWCELL(_into) \ do { _into = scm_deprecated_newcell (); } while (0) #define SCM_NEWCELL2(_into) \ do { _into = scm_deprecated_newcell2 (); } while (0) -SCM_API void * scm_must_malloc (size_t len, const char *what); -SCM_API void * scm_must_realloc (void *where, - size_t olen, size_t len, - const char *what); -SCM_API char *scm_must_strdup (const char *str); -SCM_API char *scm_must_strndup (const char *str, size_t n); -SCM_API void scm_done_malloc (long size); -SCM_API void scm_done_free (long size); -SCM_API void scm_must_free (void *obj); +SCM_DEPRECATED void * scm_must_malloc (size_t len, const char *what); +SCM_DEPRECATED void * scm_must_realloc (void *where, + size_t olen, size_t len, + const char *what); +SCM_DEPRECATED char *scm_must_strdup (const char *str); +SCM_DEPRECATED char *scm_must_strndup (const char *str, size_t n); +SCM_DEPRECATED void scm_done_malloc (long size); +SCM_DEPRECATED void scm_done_free (long size); +SCM_DEPRECATED void scm_must_free (void *obj); #endif diff --git a/libguile/gen-scmconfig.c b/libguile/gen-scmconfig.c index 0e897ca8c..bc0c2df20 100644 --- a/libguile/gen-scmconfig.c +++ b/libguile/gen-scmconfig.c @@ -404,6 +404,13 @@ main (int argc, char *argv[]) pf ("typedef long int scm_t_off;\n"); #endif + pf ("/* Define to 1 if the compiler supports the " + "`__thread' storage class. */\n"); + if (SCM_I_GSC_HAVE_THREAD_STORAGE_CLASS) + pf ("#define SCM_HAVE_THREAD_STORAGE_CLASS\n"); + else + pf ("/* #undef SCM_HAVE_THREAD_STORAGE_CLASS */\n"); + #if USE_DLL_IMPORT pf ("\n"); pf ("/* Define some additional CPP macros on Win32 platforms. */\n"); diff --git a/libguile/gen-scmconfig.h.in b/libguile/gen-scmconfig.h.in index 1be95af94..770e08196 100644 --- a/libguile/gen-scmconfig.h.in +++ b/libguile/gen-scmconfig.h.in @@ -30,6 +30,7 @@ #define SCM_I_GSC_USE_NULL_THREADS @SCM_I_GSC_USE_NULL_THREADS@ #define SCM_I_GSC_NEED_BRACES_ON_PTHREAD_ONCE_INIT @SCM_I_GSC_NEED_BRACES_ON_PTHREAD_ONCE_INIT@ #define SCM_I_GSC_NEED_BRACES_ON_PTHREAD_MUTEX_INITIALIZER @SCM_I_GSC_NEED_BRACES_ON_PTHREAD_MUTEX_INITIALIZER@ +#define SCM_I_GSC_HAVE_THREAD_STORAGE_CLASS @SCM_I_GSC_HAVE_THREAD_STORAGE_CLASS@ #define SCM_I_GSC_HAVE_STRUCT_DIRENT64 @SCM_I_GSC_HAVE_STRUCT_DIRENT64@ /* diff --git a/libguile/generalized-arrays.c b/libguile/generalized-arrays.c index 6394405dd..8bbbed4d1 100644 --- a/libguile/generalized-arrays.c +++ b/libguile/generalized-arrays.c @@ -39,16 +39,26 @@ scm_is_array (SCM obj) return scm_i_array_implementation_for_obj (obj) ? 1 : 0; } -SCM_DEFINE (scm_array_p, "array?", 1, 0, 0, - (SCM obj), +SCM_DEFINE (scm_array_p_2, "array?", 1, 0, 0, + (SCM obj), "Return @code{#t} if the @var{obj} is an array, and @code{#f} if\n" "not.") -#define FUNC_NAME s_scm_array_p +#define FUNC_NAME s_scm_array_p_2 { return scm_from_bool (scm_is_array (obj)); } #undef FUNC_NAME +/* The array type predicate, with an extra argument kept for backward + compatibility. Note that we can't use `SCM_DEFINE' directly because there + would be an argument count mismatch that would be caught by + `snarf-check-and-output-texi.scm'. */ +SCM +scm_array_p (SCM obj, SCM unused) +{ + return scm_array_p_2 (obj); +} + int scm_is_typed_array (SCM obj, SCM type) { diff --git a/libguile/generalized-arrays.h b/libguile/generalized-arrays.h index cc7214e8b..1f9b6ad3d 100644 --- a/libguile/generalized-arrays.h +++ b/libguile/generalized-arrays.h @@ -35,7 +35,8 @@ /** Arrays */ SCM_API int scm_is_array (SCM obj); -SCM_API SCM scm_array_p (SCM v); +SCM_API SCM scm_array_p (SCM v, SCM unused); +SCM_INTERNAL SCM scm_array_p_2 (SCM); SCM_API int scm_is_typed_array (SCM obj, SCM type); SCM_API SCM scm_typed_array_p (SCM v, SCM type); diff --git a/libguile/goops.c b/libguile/goops.c index 4616fa240..24a823f28 100644 --- a/libguile/goops.c +++ b/libguile/goops.c @@ -282,11 +282,15 @@ SCM_DEFINE (scm_class_of, "class-of", 1, 0, 0, return SCM_STRUCT_TABLE_CLASS (SCM_CDR (handle)); else { - SCM name = SCM_STRUCT_TABLE_NAME (SCM_CDR (handle)); - SCM class = scm_make_extended_class_from_symbol (scm_is_true (name) - ? name - : scm_nullstr, - SCM_I_OPERATORP (x)); + SCM class, name; + + name = SCM_STRUCT_TABLE_NAME (SCM_CDR (handle)); + if (!scm_is_symbol (name)) + name = scm_string_to_symbol (scm_nullstr); + + class = + scm_make_extended_class_from_symbol (name, + SCM_I_OPERATORP (x)); SCM_SET_STRUCT_TABLE_CLASS (SCM_CDR (handle), class); return class; } @@ -1650,12 +1654,13 @@ SCM_DEFINE (scm_sys_modify_instance, "%modify-instance", 2, 0, 0, */ SCM_CRITICAL_SECTION_START; { - SCM car = SCM_CAR (old); - SCM cdr = SCM_CDR (old); - SCM_SETCAR (old, SCM_CAR (new)); - SCM_SETCDR (old, SCM_CDR (new)); - SCM_SETCAR (new, car); - SCM_SETCDR (new, cdr); + scm_t_bits word0, word1; + word0 = SCM_CELL_WORD_0 (old); + word1 = SCM_CELL_WORD_1 (old); + SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new)); + SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new)); + SCM_SET_CELL_WORD_0 (new, word0); + SCM_SET_CELL_WORD_1 (new, word1); } SCM_CRITICAL_SECTION_END; return SCM_UNSPECIFIED; @@ -1672,13 +1677,14 @@ SCM_DEFINE (scm_sys_modify_class, "%modify-class", 2, 0, 0, SCM_CRITICAL_SECTION_START; { - SCM car = SCM_CAR (old); - SCM cdr = SCM_CDR (old); - SCM_SETCAR (old, SCM_CAR (new)); - SCM_SETCDR (old, SCM_CDR (new)); + scm_t_bits word0, word1; + word0 = SCM_CELL_WORD_0 (old); + word1 = SCM_CELL_WORD_1 (old); + SCM_SET_CELL_WORD_0 (old, SCM_CELL_WORD_0 (new)); + SCM_SET_CELL_WORD_1 (old, SCM_CELL_WORD_1 (new)); SCM_STRUCT_DATA (old)[scm_vtable_index_vtable] = SCM_UNPACK (old); - SCM_SETCAR (new, car); - SCM_SETCDR (new, cdr); + SCM_SET_CELL_WORD_0 (new, word0); + SCM_SET_CELL_WORD_1 (new, word1); SCM_STRUCT_DATA (new)[scm_vtable_index_vtable] = SCM_UNPACK (new); } SCM_CRITICAL_SECTION_END; @@ -3000,7 +3006,7 @@ scm_wrap_component (SCM class, SCM container, void *data) obj, SCM_BOOL_F, scm_struct_ihashq, - scm_sloppy_assq, + (scm_t_assoc_fn) scm_sloppy_assq, 0); SCM_SETCDR (handle, container); return obj; diff --git a/libguile/guardians.c b/libguile/guardians.c index 580e212d0..51aa43310 100644 --- a/libguile/guardians.c +++ b/libguile/guardians.c @@ -62,7 +62,7 @@ #include "libguile/eval.h" #include "libguile/guardians.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" diff --git a/libguile/hashtab.c b/libguile/hashtab.c index 5c03d281f..4ba2ef9ab 100644 --- a/libguile/hashtab.c +++ b/libguile/hashtab.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -207,7 +207,7 @@ make_hash_table (int flags, unsigned long k, const char *func_name) void scm_i_rehash (SCM table, - unsigned long (*hash_fn)(), + scm_t_hash_fn hash_fn, void *closure, const char* func_name) { @@ -429,9 +429,13 @@ SCM_DEFINE (scm_doubly_weak_hash_table_p, "doubly-weak-hash-table?", 1, 0, 0, } #undef FUNC_NAME + +/* Accessing hash table entries. */ SCM -scm_hash_fn_get_handle (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*assoc_fn)(), void * closure) +scm_hash_fn_get_handle (SCM table, SCM obj, + scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn, + void * closure) #define FUNC_NAME "scm_hash_fn_get_handle" { int weak = 0; @@ -468,8 +472,9 @@ scm_hash_fn_get_handle (SCM table, SCM obj, unsigned long (*hash_fn)(), SCM (*as SCM -scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_fn)(), - SCM (*assoc_fn)(), void * closure) +scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, + scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn, + void * closure) #define FUNC_NAME "scm_hash_fn_create_handle_x" { int weak = 0; @@ -559,9 +564,10 @@ scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_ #undef FUNC_NAME -SCM -scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, unsigned long (*hash_fn)(), - SCM (*assoc_fn)(), void * closure) +SCM +scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, + scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn, + void *closure) { SCM it = scm_hash_fn_get_handle (table, obj, hash_fn, assoc_fn, closure); if (scm_is_pair (it)) @@ -573,9 +579,10 @@ scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, unsigned long (*hash_fn)(), -SCM -scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn)(), - SCM (*assoc_fn)(), void * closure) +SCM +scm_hash_fn_set_x (SCM table, SCM obj, SCM val, + scm_t_hash_fn hash_fn, scm_t_assoc_fn assoc_fn, + void *closure) { SCM it; @@ -587,8 +594,8 @@ scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn)(), SCM scm_hash_fn_remove_x (SCM table, SCM obj, - unsigned long (*hash_fn)(), - SCM (*assoc_fn)(), + scm_t_hash_fn hash_fn, + scm_t_assoc_fn assoc_fn, void *closure) { int weak = 0; @@ -659,7 +666,10 @@ SCM_DEFINE (scm_hashq_get_handle, "hashq-get-handle", 2, 0, 0, "Uses @code{eq?} for equality testing.") #define FUNC_NAME s_scm_hashq_get_handle { - return scm_hash_fn_get_handle (table, key, scm_ihashq, scm_sloppy_assq, 0); + return scm_hash_fn_get_handle (table, key, + (scm_t_hash_fn) scm_ihashq, + (scm_t_assoc_fn) scm_sloppy_assq, + 0); } #undef FUNC_NAME @@ -671,7 +681,10 @@ SCM_DEFINE (scm_hashq_create_handle_x, "hashq-create-handle!", 3, 0, 0, "associates @var{key} with @var{init}.") #define FUNC_NAME s_scm_hashq_create_handle_x { - return scm_hash_fn_create_handle_x (table, key, init, scm_ihashq, scm_sloppy_assq, 0); + return scm_hash_fn_create_handle_x (table, key, init, + (scm_t_hash_fn) scm_ihashq, + (scm_t_assoc_fn) scm_sloppy_assq, + 0); } #undef FUNC_NAME @@ -686,7 +699,10 @@ SCM_DEFINE (scm_hashq_ref, "hashq-ref", 2, 1, 0, { if (SCM_UNBNDP (dflt)) dflt = SCM_BOOL_F; - return scm_hash_fn_ref (table, key, dflt, scm_ihashq, scm_sloppy_assq, 0); + return scm_hash_fn_ref (table, key, dflt, + (scm_t_hash_fn) scm_ihashq, + (scm_t_assoc_fn) scm_sloppy_assq, + 0); } #undef FUNC_NAME @@ -698,7 +714,10 @@ SCM_DEFINE (scm_hashq_set_x, "hashq-set!", 3, 0, 0, "store @var{value} there. Uses @code{eq?} for equality testing.") #define FUNC_NAME s_scm_hashq_set_x { - return scm_hash_fn_set_x (table, key, val, scm_ihashq, scm_sloppy_assq, 0); + return scm_hash_fn_set_x (table, key, val, + (scm_t_hash_fn) scm_ihashq, + (scm_t_assoc_fn) scm_sloppy_assq, + 0); } #undef FUNC_NAME @@ -710,7 +729,10 @@ SCM_DEFINE (scm_hashq_remove_x, "hashq-remove!", 2, 0, 0, "@var{table}. Uses @code{eq?} for equality tests.") #define FUNC_NAME s_scm_hashq_remove_x { - return scm_hash_fn_remove_x (table, key, scm_ihashq, scm_sloppy_assq, 0); + return scm_hash_fn_remove_x (table, key, + (scm_t_hash_fn) scm_ihashq, + (scm_t_assoc_fn) scm_sloppy_assq, + 0); } #undef FUNC_NAME @@ -725,7 +747,10 @@ SCM_DEFINE (scm_hashv_get_handle, "hashv-get-handle", 2, 0, 0, "Uses @code{eqv?} for equality testing.") #define FUNC_NAME s_scm_hashv_get_handle { - return scm_hash_fn_get_handle (table, key, scm_ihashv, scm_sloppy_assv, 0); + return scm_hash_fn_get_handle (table, key, + (scm_t_hash_fn) scm_ihashv, + (scm_t_assoc_fn) scm_sloppy_assv, + 0); } #undef FUNC_NAME @@ -737,8 +762,10 @@ SCM_DEFINE (scm_hashv_create_handle_x, "hashv-create-handle!", 3, 0, 0, "associates @var{key} with @var{init}.") #define FUNC_NAME s_scm_hashv_create_handle_x { - return scm_hash_fn_create_handle_x (table, key, init, scm_ihashv, - scm_sloppy_assv, 0); + return scm_hash_fn_create_handle_x (table, key, init, + (scm_t_hash_fn) scm_ihashv, + (scm_t_assoc_fn) scm_sloppy_assv, + 0); } #undef FUNC_NAME @@ -753,7 +780,10 @@ SCM_DEFINE (scm_hashv_ref, "hashv-ref", 2, 1, 0, { if (SCM_UNBNDP (dflt)) dflt = SCM_BOOL_F; - return scm_hash_fn_ref (table, key, dflt, scm_ihashv, scm_sloppy_assv, 0); + return scm_hash_fn_ref (table, key, dflt, + (scm_t_hash_fn) scm_ihashv, + (scm_t_assoc_fn) scm_sloppy_assv, + 0); } #undef FUNC_NAME @@ -765,7 +795,10 @@ SCM_DEFINE (scm_hashv_set_x, "hashv-set!", 3, 0, 0, "store @var{value} there. Uses @code{eqv?} for equality testing.") #define FUNC_NAME s_scm_hashv_set_x { - return scm_hash_fn_set_x (table, key, val, scm_ihashv, scm_sloppy_assv, 0); + return scm_hash_fn_set_x (table, key, val, + (scm_t_hash_fn) scm_ihashv, + (scm_t_assoc_fn) scm_sloppy_assv, + 0); } #undef FUNC_NAME @@ -776,7 +809,10 @@ SCM_DEFINE (scm_hashv_remove_x, "hashv-remove!", 2, 0, 0, "@var{table}. Uses @code{eqv?} for equality tests.") #define FUNC_NAME s_scm_hashv_remove_x { - return scm_hash_fn_remove_x (table, key, scm_ihashv, scm_sloppy_assv, 0); + return scm_hash_fn_remove_x (table, key, + (scm_t_hash_fn) scm_ihashv, + (scm_t_assoc_fn) scm_sloppy_assv, + 0); } #undef FUNC_NAME @@ -790,7 +826,10 @@ SCM_DEFINE (scm_hash_get_handle, "hash-get-handle", 2, 0, 0, "Uses @code{equal?} for equality testing.") #define FUNC_NAME s_scm_hash_get_handle { - return scm_hash_fn_get_handle (table, key, scm_ihash, scm_sloppy_assoc, 0); + return scm_hash_fn_get_handle (table, key, + (scm_t_hash_fn) scm_ihash, + (scm_t_assoc_fn) scm_sloppy_assoc, + 0); } #undef FUNC_NAME @@ -802,7 +841,10 @@ SCM_DEFINE (scm_hash_create_handle_x, "hash-create-handle!", 3, 0, 0, "associates @var{key} with @var{init}.") #define FUNC_NAME s_scm_hash_create_handle_x { - return scm_hash_fn_create_handle_x (table, key, init, scm_ihash, scm_sloppy_assoc, 0); + return scm_hash_fn_create_handle_x (table, key, init, + (scm_t_hash_fn) scm_ihash, + (scm_t_assoc_fn) scm_sloppy_assoc, + 0); } #undef FUNC_NAME @@ -817,7 +859,10 @@ SCM_DEFINE (scm_hash_ref, "hash-ref", 2, 1, 0, { if (SCM_UNBNDP (dflt)) dflt = SCM_BOOL_F; - return scm_hash_fn_ref (table, key, dflt, scm_ihash, scm_sloppy_assoc, 0); + return scm_hash_fn_ref (table, key, dflt, + (scm_t_hash_fn) scm_ihash, + (scm_t_assoc_fn) scm_sloppy_assoc, + 0); } #undef FUNC_NAME @@ -830,7 +875,10 @@ SCM_DEFINE (scm_hash_set_x, "hash-set!", 3, 0, 0, "testing.") #define FUNC_NAME s_scm_hash_set_x { - return scm_hash_fn_set_x (table, key, val, scm_ihash, scm_sloppy_assoc, 0); + return scm_hash_fn_set_x (table, key, val, + (scm_t_hash_fn) scm_ihash, + (scm_t_assoc_fn) scm_sloppy_assoc, + 0); } #undef FUNC_NAME @@ -842,7 +890,10 @@ SCM_DEFINE (scm_hash_remove_x, "hash-remove!", 2, 0, 0, "@var{table}. Uses @code{equal?} for equality tests.") #define FUNC_NAME s_scm_hash_remove_x { - return scm_hash_fn_remove_x (table, key, scm_ihash, scm_sloppy_assoc, 0); + return scm_hash_fn_remove_x (table, key, + (scm_t_hash_fn) scm_ihash, + (scm_t_assoc_fn) scm_sloppy_assoc, + 0); } #undef FUNC_NAME @@ -858,17 +909,20 @@ typedef struct scm_t_ihashx_closure static unsigned long -scm_ihashx (SCM obj, unsigned long n, scm_t_ihashx_closure *closure) +scm_ihashx (SCM obj, unsigned long n, void *arg) { - SCM answer = scm_call_2 (closure->hash, obj, scm_from_ulong (n)); + SCM answer; + scm_t_ihashx_closure *closure = (scm_t_ihashx_closure *) arg; + answer = scm_call_2 (closure->hash, obj, scm_from_ulong (n)); return scm_to_ulong (answer); } static SCM -scm_sloppy_assx (SCM obj, SCM alist, scm_t_ihashx_closure *closure) +scm_sloppy_assx (SCM obj, SCM alist, void *arg) { + scm_t_ihashx_closure *closure = (scm_t_ihashx_closure *) arg; return scm_call_2 (closure->assoc, obj, alist); } @@ -985,7 +1039,8 @@ SCM_DEFINE (scm_hashx_remove_x, "hashx-remove!", 4, 0, 0, static const char s_scm_hash_fold[]; SCM -scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table) +scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure, + SCM init, SCM table) { long i, n; SCM buckets, result = init; @@ -1049,7 +1104,8 @@ scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table) static const char s_scm_hash_for_each[]; void -scm_internal_hash_for_each_handle (SCM (*fn) (), void *closure, SCM table) +scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, void *closure, + SCM table) { long i, n; SCM buckets; @@ -1091,7 +1147,8 @@ SCM_DEFINE (scm_hash_fold, "hash-fold", 3, 0, 0, SCM_VALIDATE_PROC (1, proc); if (!SCM_HASHTABLE_P (table)) SCM_VALIDATE_VECTOR (3, table); - return scm_internal_hash_fold (scm_call_3, (void *) SCM_UNPACK (proc), init, table); + return scm_internal_hash_fold ((scm_t_hash_fold_fn) scm_call_3, + (void *) SCM_UNPACK (proc), init, table); } #undef FUNC_NAME @@ -1131,7 +1188,7 @@ SCM_DEFINE (scm_hash_for_each_handle, "hash-for-each-handle", 2, 0, 0, if (!SCM_HASHTABLE_P (table)) SCM_VALIDATE_VECTOR (2, table); - scm_internal_hash_for_each_handle (call, + scm_internal_hash_for_each_handle ((scm_t_hash_handle_fn) call, (void *) SCM_UNPACK (proc), table); return SCM_UNSPECIFIED; diff --git a/libguile/hashtab.h b/libguile/hashtab.h index 8f8ebf9ce..b60cd4349 100644 --- a/libguile/hashtab.h +++ b/libguile/hashtab.h @@ -3,7 +3,7 @@ #ifndef SCM_HASHTAB_H #define SCM_HASHTAB_H -/* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1999,2000,2001, 2003, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -64,6 +64,22 @@ SCM_API scm_t_bits scm_tc16_hashtable; #define SCM_SET_HASHTABLE_BUCKET(h, i, x) \ SCM_SIMPLE_VECTOR_SET (SCM_HASHTABLE_VECTOR (h), i, x) +/* Function that computes a hash of OBJ modulo MAX. */ +typedef unsigned long (*scm_t_hash_fn) (SCM obj, unsigned long max, + void *closure); + +/* Function that returns the value associated with OBJ in ALIST according to + some equality predicate. */ +typedef SCM (*scm_t_assoc_fn) (SCM obj, SCM alist, void *closure); + +/* Function to fold over the entries of a hash table. */ +typedef SCM (*scm_t_hash_fold_fn) (void *closure, SCM key, SCM value, + SCM result); + +/* Function to iterate over the handles (key-value pairs) of a hash + table. */ +typedef SCM (*scm_t_hash_handle_fn) (void *closure, SCM handle); + typedef struct scm_t_hashtable { int flags; /* properties of table */ unsigned long n_items; /* number of items in table */ @@ -71,17 +87,11 @@ typedef struct scm_t_hashtable { unsigned long upper; /* when to grow */ int size_index; /* index into hashtable_size */ int min_size_index; /* minimum size_index */ - unsigned long (*hash_fn) (); /* for rehashing after a GC. */ + scm_t_hash_fn hash_fn; /* for rehashing after a GC. */ } scm_t_hashtable; -#if 0 -typedef unsigned int scm_t_hash_fn (SCM obj, unsigned int d, void *closure); -typedef SCM scm_t_assoc_fn (SCM key, SCM alist, void *closure); -typedef SCM scm_t_delete_fn (SCM elt, SCM list); -#endif - SCM_API SCM scm_vector_to_hash_table (SCM vector); SCM_API SCM scm_c_make_hash_table (unsigned long k); SCM_API SCM scm_make_hash_table (SCM n); @@ -94,16 +104,34 @@ SCM_API SCM scm_weak_key_hash_table_p (SCM h); SCM_API SCM scm_weak_value_hash_table_p (SCM h); SCM_API SCM scm_doubly_weak_hash_table_p (SCM h); -SCM_INTERNAL void scm_i_rehash (SCM table, unsigned long (*hash_fn)(), +SCM_INTERNAL void scm_i_rehash (SCM table, scm_t_hash_fn hash_fn, void *closure, const char *func_name); -SCM_API SCM scm_hash_fn_get_handle (SCM table, SCM obj, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure); -SCM_API SCM scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure); -SCM_API SCM scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure); -SCM_API SCM scm_hash_fn_set_x (SCM table, SCM obj, SCM val, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure); -SCM_API SCM scm_hash_fn_remove_x (SCM table, SCM obj, unsigned long (*hash_fn) (), SCM (*assoc_fn) (), void * closure); -SCM_API SCM scm_internal_hash_fold (SCM (*fn) (), void *closure, SCM init, SCM table); -SCM_API void scm_internal_hash_for_each_handle (SCM (*fn) (), void *closure, SCM table); + +SCM_API SCM scm_hash_fn_get_handle (SCM table, SCM obj, + scm_t_hash_fn hash_fn, + scm_t_assoc_fn assoc_fn, + void *closure); +SCM_API SCM scm_hash_fn_create_handle_x (SCM table, SCM obj, SCM init, + scm_t_hash_fn hash_fn, + scm_t_assoc_fn assoc_fn, + void *closure); +SCM_API SCM scm_hash_fn_ref (SCM table, SCM obj, SCM dflt, + scm_t_hash_fn hash_fn, + scm_t_assoc_fn assoc_fn, + void *closure); +SCM_API SCM scm_hash_fn_set_x (SCM table, SCM obj, SCM val, + scm_t_hash_fn hash_fn, + scm_t_assoc_fn assoc_fn, + void *closure); +SCM_API SCM scm_hash_fn_remove_x (SCM table, SCM obj, + scm_t_hash_fn hash_fn, + scm_t_assoc_fn assoc_fn, + void *closure); +SCM_API SCM scm_internal_hash_fold (scm_t_hash_fold_fn fn, void *closure, + SCM init, SCM table); +SCM_API void scm_internal_hash_for_each_handle (scm_t_hash_handle_fn fn, + void *closure, SCM table); SCM_API SCM scm_hash_clear_x (SCM table); SCM_API SCM scm_hashq_get_handle (SCM table, SCM obj); diff --git a/libguile/i18n.c b/libguile/i18n.c index fd15227b5..7deb39536 100644 --- a/libguile/i18n.c +++ b/libguile/i18n.c @@ -23,6 +23,7 @@ #include <alloca.h> #include "libguile/_scm.h" +#include "libguile/extensions.h" #include "libguile/feature.h" #include "libguile/i18n.h" #include "libguile/strings.h" @@ -36,6 +37,8 @@ #include <string.h> /* `strcoll ()' */ #include <ctype.h> /* `toupper ()' et al. */ #include <errno.h> +#include <unicase.h> +#include <unistr.h> #if (defined HAVE_NEWLOCALE) && (defined HAVE_STRCOLL_L) /* The GNU thread-aware locale API is documented in ``Thread-Aware Locale @@ -503,8 +506,22 @@ get_current_locale (SCM *result) return err; } +#else /* USE_GNU_LOCALE_API */ + +/* Convenient macro to run STATEMENT in the locale context of C_LOCALE. */ +#define RUN_IN_LOCALE_SECTION(_c_locale, _statement) \ + do \ + { \ + scm_t_locale old_loc; \ + \ + old_loc = uselocale (_c_locale); \ + _statement ; \ + uselocale (old_loc); \ + } \ + while (0) + -#endif /* !USE_GNU_LOCALE_API */ +#endif /* USE_GNU_LOCALE_API */ @@ -709,39 +726,123 @@ SCM_DEFINE (scm_locale_p, "locale?", 1, 0, 0, A similar API can be found in MzScheme starting from version 200: http://download.plt-scheme.org/chronology/mzmr200alpha14.html . */ +#define SCM_STRING_TO_U32_BUF(s1, c_s1) \ + do \ + { \ + if (scm_i_is_narrow_string (s1)) \ + { \ + size_t i, len; \ + const char *buf = scm_i_string_chars (s1); \ + \ + len = scm_i_string_length (s1); \ + c_s1 = (scm_t_wchar *) alloca (sizeof (scm_t_wchar) * (len + 1)); \ + \ + for (i = 0; i < len; i ++) \ + c_s1[i] = (unsigned char ) buf[i]; \ + c_s1[len] = 0; \ + } \ + else \ + c_s1 = (scm_t_wchar *) scm_i_string_wide_chars (s1); \ + } while (0) + -/* Compare null-terminated strings C_S1 and C_S2 according to LOCALE. Return - an integer whose sign is the same as the difference between C_S1 and - C_S2. */ +/* Compare UTF-32 strings according to LOCALE. Returns a negative value if + S1 compares smaller than S2, a positive value if S1 compares larger than + S2, or 0 if they compare equal. */ static inline int -compare_strings (const char *c_s1, const char *c_s2, SCM locale, - const char *func_name) +compare_u32_strings (SCM s1, SCM s2, SCM locale, const char *func_name) #define FUNC_NAME func_name { int result; scm_t_locale c_locale; + scm_t_wchar *c_s1, *c_s2; + SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, locale, c_locale); + + SCM_STRING_TO_U32_BUF (s1, c_s1); + SCM_STRING_TO_U32_BUF (s2, c_s2); + + if (c_locale) + RUN_IN_LOCALE_SECTION (c_locale, + result = u32_strcoll ((const scm_t_uint32 *) c_s1, + (const scm_t_uint32 *) c_s2)); + else + result = u32_strcoll ((const scm_t_uint32 *) c_s1, + (const scm_t_uint32 *) c_s2); + + scm_remember_upto_here_2 (s1, s2); + scm_remember_upto_here (locale); + return result; +} +#undef FUNC_NAME + +/* Return the current language of the locale. */ +static const char * +locale_language () +{ + /* FIXME: If the locale has been set with 'uselocale', + libunistring's uc_locale_language will return the incorrect + language: it will return the language appropriate for the global + (non-thread-specific) locale. + + There appears to be no portable way to extract the language from + the thread-specific locale_t. There is no LANGUAGE capability in + nl_langinfo or nl_langinfo_l. + + Thus, uc_locale_language needs to be fixed upstream. */ + return uc_locale_language (); +} + +static inline int +u32_locale_casecoll (const char *func_name, const scm_t_uint32 *c_s1, + const scm_t_uint32 *c_s2, + int *result) +{ + /* Note: Since this is called from `RUN_IN_LOCALE_SECTION', it must note + make any non-local exit. */ + + int ret; + const char *loc = locale_language (); + ret = u32_casecoll (c_s1, u32_strlen (c_s1), + c_s2, u32_strlen (c_s2), + loc, UNINORM_NFC, result); + + return ret == 0 ? ret : errno; +} + +static inline int +compare_u32_strings_ci (SCM s1, SCM s2, SCM locale, const char *func_name) +#define FUNC_NAME func_name +{ + int result, ret = 0; + scm_t_locale c_locale; + scm_t_wchar *c_s1, *c_s2; SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, locale, c_locale); + SCM_STRING_TO_U32_BUF (s1, c_s1); + SCM_STRING_TO_U32_BUF (s2, c_s2); + if (c_locale) + RUN_IN_LOCALE_SECTION + (c_locale, + ret = u32_locale_casecoll (func_name, + (const scm_t_uint32 *) c_s1, + (const scm_t_uint32 *) c_s2, + &result)); + else + ret = u32_locale_casecoll (func_name, + (const scm_t_uint32 *) c_s1, + (const scm_t_uint32 *) c_s2, + &result); + + if (SCM_UNLIKELY (ret != 0)) { -#ifdef USE_GNU_LOCALE_API - result = strcoll_l (c_s1, c_s2, c_locale); -#else -#ifdef HAVE_STRCOLL - RUN_IN_LOCALE_SECTION (c_locale, result = strcoll (c_s1, c_s2)); -#else - result = strcmp (c_s1, c_s2); -#endif -#endif /* !USE_GNU_LOCALE_API */ + errno = ret; + scm_syserror (FUNC_NAME); } - else -#ifdef HAVE_STRCOLL - result = strcoll (c_s1, c_s2); -#else - result = strcmp (c_s1, c_s2); -#endif + scm_remember_upto_here_2 (s1, s2); + scm_remember_upto_here (locale); return result; } @@ -785,71 +886,6 @@ str_downcase_l (register char *dst, register const char *src, #endif -/* Compare null-terminated strings C_S1 and C_S2 in a case-independent way - according to LOCALE. Return an integer whose sign is the same as the - difference between C_S1 and C_S2. */ -static inline int -compare_strings_ci (const char *c_s1, const char *c_s2, SCM locale, - const char *func_name) -#define FUNC_NAME func_name -{ - int result; - scm_t_locale c_locale; - char *c_us1, *c_us2; - - SCM_VALIDATE_OPTIONAL_LOCALE_COPY (3, locale, c_locale); - - c_us1 = (char *) alloca (strlen (c_s1) + 1); - c_us2 = (char *) alloca (strlen (c_s2) + 1); - - if (c_locale) - { -#ifdef USE_GNU_LOCALE_API - str_upcase_l (c_us1, c_s1, c_locale); - str_upcase_l (c_us2, c_s2, c_locale); - - result = strcoll_l (c_us1, c_us2, c_locale); -#else - int err; - scm_t_locale_settings prev_locale; - - err = enter_locale_section (c_locale, &prev_locale); - if (err) - { - scm_locale_error (func_name, err); - return 0; - } - - str_upcase (c_us1, c_s1); - str_upcase (c_us2, c_s2); - -#ifdef HAVE_STRCOLL - result = strcoll (c_us1, c_us2); -#else - result = strcmp (c_us1, c_us2); -#endif /* !HAVE_STRCOLL */ - - leave_locale_section (&prev_locale); - free_locale_settings (&prev_locale); -#endif /* !USE_GNU_LOCALE_API */ - } - else - { - str_upcase (c_us1, c_s1); - str_upcase (c_us2, c_s2); - -#ifdef HAVE_STRCOLL - result = strcoll (c_us1, c_us2); -#else - result = strcmp (c_us1, c_us2); -#endif - } - - return result; -} -#undef FUNC_NAME - - SCM_DEFINE (scm_string_locale_lt, "string-locale<?", 2, 1, 0, (SCM s1, SCM s2, SCM locale), "Compare strings @var{s1} and @var{s2} in a locale-dependent way." @@ -859,17 +895,11 @@ SCM_DEFINE (scm_string_locale_lt, "string-locale<?", 2, 1, 0, #define FUNC_NAME s_scm_string_locale_lt { int result; - const char *c_s1, *c_s2; SCM_VALIDATE_STRING (1, s1); SCM_VALIDATE_STRING (2, s2); - c_s1 = scm_i_string_chars (s1); - c_s2 = scm_i_string_chars (s2); - - result = compare_strings (c_s1, c_s2, locale, FUNC_NAME); - - scm_remember_upto_here_2 (s1, s2); + result = compare_u32_strings (s1, s2, locale, FUNC_NAME); return scm_from_bool (result < 0); } @@ -884,17 +914,11 @@ SCM_DEFINE (scm_string_locale_gt, "string-locale>?", 2, 1, 0, #define FUNC_NAME s_scm_string_locale_gt { int result; - const char *c_s1, *c_s2; SCM_VALIDATE_STRING (1, s1); SCM_VALIDATE_STRING (2, s2); - c_s1 = scm_i_string_chars (s1); - c_s2 = scm_i_string_chars (s2); - - result = compare_strings (c_s1, c_s2, locale, FUNC_NAME); - - scm_remember_upto_here_2 (s1, s2); + result = compare_u32_strings (s1, s2, locale, FUNC_NAME); return scm_from_bool (result > 0); } @@ -910,17 +934,11 @@ SCM_DEFINE (scm_string_locale_ci_lt, "string-locale-ci<?", 2, 1, 0, #define FUNC_NAME s_scm_string_locale_ci_lt { int result; - const char *c_s1, *c_s2; SCM_VALIDATE_STRING (1, s1); SCM_VALIDATE_STRING (2, s2); - c_s1 = scm_i_string_chars (s1); - c_s2 = scm_i_string_chars (s2); - - result = compare_strings_ci (c_s1, c_s2, locale, FUNC_NAME); - - scm_remember_upto_here_2 (s1, s2); + result = compare_u32_strings_ci (s1, s2, locale, FUNC_NAME); return scm_from_bool (result < 0); } @@ -936,17 +954,11 @@ SCM_DEFINE (scm_string_locale_ci_gt, "string-locale-ci>?", 2, 1, 0, #define FUNC_NAME s_scm_string_locale_ci_gt { int result; - const char *c_s1, *c_s2; SCM_VALIDATE_STRING (1, s1); SCM_VALIDATE_STRING (2, s2); - c_s1 = scm_i_string_chars (s1); - c_s2 = scm_i_string_chars (s2); - - result = compare_strings_ci (c_s1, c_s2, locale, FUNC_NAME); - - scm_remember_upto_here_2 (s1, s2); + result = compare_u32_strings_ci (s1, s2, locale, FUNC_NAME); return scm_from_bool (result > 0); } @@ -962,17 +974,11 @@ SCM_DEFINE (scm_string_locale_ci_eq, "string-locale-ci=?", 2, 1, 0, #define FUNC_NAME s_scm_string_locale_ci_eq { int result; - const char *c_s1, *c_s2; SCM_VALIDATE_STRING (1, s1); SCM_VALIDATE_STRING (2, s2); - c_s1 = scm_i_string_chars (s1); - c_s2 = scm_i_string_chars (s2); - - result = compare_strings_ci (c_s1, c_s2, locale, FUNC_NAME); - - scm_remember_upto_here_2 (s1, s2); + result = compare_u32_strings_ci (s1, s2, locale, FUNC_NAME); return scm_from_bool (result == 0); } @@ -985,15 +991,16 @@ SCM_DEFINE (scm_char_locale_lt, "char-locale<?", 2, 1, 0, "according to @var{locale} or to the current locale.") #define FUNC_NAME s_scm_char_locale_lt { - char c_c1[2], c_c2[2]; + int result; SCM_VALIDATE_CHAR (1, c1); SCM_VALIDATE_CHAR (2, c2); - c_c1[0] = (char)SCM_CHAR (c1); c_c1[1] = '\0'; - c_c2[0] = (char)SCM_CHAR (c2); c_c2[1] = '\0'; + result = compare_u32_strings (scm_string (scm_list_1 (c1)), + scm_string (scm_list_1 (c2)), + locale, FUNC_NAME); - return scm_from_bool (compare_strings (c_c1, c_c2, locale, FUNC_NAME) < 0); + return scm_from_bool (result < 0); } #undef FUNC_NAME @@ -1003,15 +1010,16 @@ SCM_DEFINE (scm_char_locale_gt, "char-locale>?", 2, 1, 0, "according to @var{locale} or to the current locale.") #define FUNC_NAME s_scm_char_locale_gt { - char c_c1[2], c_c2[2]; + int result; SCM_VALIDATE_CHAR (1, c1); SCM_VALIDATE_CHAR (2, c2); - c_c1[0] = (char)SCM_CHAR (c1); c_c1[1] = '\0'; - c_c2[0] = (char)SCM_CHAR (c2); c_c2[1] = '\0'; + result = compare_u32_strings (scm_string (scm_list_1 (c1)), + scm_string (scm_list_1 (c2)), + locale, FUNC_NAME); - return scm_from_bool (compare_strings (c_c1, c_c2, locale, FUNC_NAME) > 0); + return scm_from_bool (result > 0); } #undef FUNC_NAME @@ -1023,15 +1031,13 @@ SCM_DEFINE (scm_char_locale_ci_lt, "char-locale-ci<?", 2, 1, 0, #define FUNC_NAME s_scm_char_locale_ci_lt { int result; - char c_c1[2], c_c2[2]; SCM_VALIDATE_CHAR (1, c1); SCM_VALIDATE_CHAR (2, c2); - c_c1[0] = (char)SCM_CHAR (c1); c_c1[1] = '\0'; - c_c2[0] = (char)SCM_CHAR (c2); c_c2[1] = '\0'; - - result = compare_strings_ci (c_c1, c_c2, locale, FUNC_NAME); + result = compare_u32_strings_ci (scm_string (scm_list_1 (c1)), + scm_string (scm_list_1 (c2)), + locale, FUNC_NAME); return scm_from_bool (result < 0); } @@ -1045,15 +1051,13 @@ SCM_DEFINE (scm_char_locale_ci_gt, "char-locale-ci>?", 2, 1, 0, #define FUNC_NAME s_scm_char_locale_ci_gt { int result; - char c_c1[2], c_c2[2]; SCM_VALIDATE_CHAR (1, c1); SCM_VALIDATE_CHAR (2, c2); - c_c1[0] = (char)SCM_CHAR (c1); c_c1[1] = '\0'; - c_c2[0] = (char)SCM_CHAR (c2); c_c2[1] = '\0'; - - result = compare_strings_ci (c_c1, c_c2, locale, FUNC_NAME); + result = compare_u32_strings_ci (scm_string (scm_list_1 (c1)), + scm_string (scm_list_1 (c2)), + locale, FUNC_NAME); return scm_from_bool (result > 0); } @@ -1067,15 +1071,13 @@ SCM_DEFINE (scm_char_locale_ci_eq, "char-locale-ci=?", 2, 1, 0, #define FUNC_NAME s_scm_char_locale_ci_eq { int result; - char c_c1[2], c_c2[2]; SCM_VALIDATE_CHAR (1, c1); SCM_VALIDATE_CHAR (2, c2); - c_c1[0] = (char)SCM_CHAR (c1); c_c1[1] = '\0'; - c_c2[0] = (char)SCM_CHAR (c2); c_c2[1] = '\0'; - - result = compare_strings_ci (c_c1, c_c2, locale, FUNC_NAME); + result = compare_u32_strings_ci (scm_string (scm_list_1 (c1)), + scm_string (scm_list_1 (c2)), + locale, FUNC_NAME); return scm_from_bool (result == 0); } @@ -1085,33 +1087,79 @@ SCM_DEFINE (scm_char_locale_ci_eq, "char-locale-ci=?", 2, 1, 0, /* Locale-dependent alphabetic character mapping. */ +static inline int +u32_locale_tocase (const scm_t_uint32 *c_s1, size_t len, + scm_t_uint32 **p_c_s2, size_t * p_len2, + scm_t_uint32 *(*func) (const scm_t_uint32 *, size_t, + const char *, uninorm_t, + scm_t_uint32 *, size_t *)) +{ + /* Note: Since this is called from `RUN_IN_LOCALE_SECTION', it must not + make any non-local exit. */ + + scm_t_uint32 *ret; + const char *loc = locale_language (); + + /* The first NULL here indicates that no NFC or NFKC normalization + is done. The second NULL means the return buffer is + malloc'ed here. */ + ret = func (c_s1, len, loc, NULL, NULL, p_len2); + + if (ret == NULL) + { + *p_c_s2 = (scm_t_uint32 *) NULL; + *p_len2 = 0; + return errno; + } + *p_c_s2 = ret; + + return 0; +} + + + SCM_DEFINE (scm_char_locale_downcase, "char-locale-downcase", 1, 1, 0, (SCM chr, SCM locale), "Return the lowercase character that corresponds to @var{chr} " "according to either @var{locale} or the current locale.") #define FUNC_NAME s_scm_char_locale_downcase { - char c_chr; - int c_result; + int ret; scm_t_locale c_locale; + scm_t_wchar *buf; + scm_t_uint32 *downbuf; + size_t downlen; + SCM str, downchar; SCM_VALIDATE_CHAR (1, chr); - c_chr = SCM_CHAR (chr); - SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale); + str = scm_i_make_wide_string (1, &buf); + buf[0] = SCM_CHAR (chr); + if (c_locale != NULL) + RUN_IN_LOCALE_SECTION (c_locale, ret = + u32_locale_tocase ((scm_t_uint32 *) buf, 1, + &downbuf, + &downlen, u32_tolower)); + else + ret = + u32_locale_tocase ((scm_t_uint32 *) buf, 1, &downbuf, + &downlen, u32_tolower); + + if (SCM_UNLIKELY (ret != 0)) { -#ifdef USE_GNU_LOCALE_API - c_result = tolower_l ((int) c_chr, c_locale); -#else - RUN_IN_LOCALE_SECTION (c_locale, c_result = tolower ((int) c_chr)); -#endif + errno = ret; + scm_syserror (FUNC_NAME); } + + if (downlen == 1) + downchar = SCM_MAKE_CHAR ((scm_t_wchar) downbuf[0]); else - c_result = tolower ((int) c_chr); + downchar = chr; + free (downbuf); - return (SCM_MAKE_CHAR (c_result)); + return downchar; } #undef FUNC_NAME @@ -1121,27 +1169,40 @@ SCM_DEFINE (scm_char_locale_upcase, "char-locale-upcase", 1, 1, 0, "according to either @var{locale} or the current locale.") #define FUNC_NAME s_scm_char_locale_upcase { - char c_chr; - int c_result; + int ret; scm_t_locale c_locale; + scm_t_wchar *buf; + scm_t_uint32 *upbuf; + size_t uplen; + SCM str, upchar; SCM_VALIDATE_CHAR (1, chr); - c_chr = SCM_CHAR (chr); - SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale); + str = scm_i_make_wide_string (1, &buf); + buf[0] = SCM_CHAR (chr); + if (c_locale != NULL) + RUN_IN_LOCALE_SECTION (c_locale, ret = + u32_locale_tocase ((scm_t_uint32 *) buf, 1, + &upbuf, + &uplen, u32_toupper)); + else + ret = + u32_locale_tocase ((scm_t_uint32 *) buf, 1, &upbuf, + &uplen, u32_toupper); + + if (SCM_UNLIKELY (ret != 0)) { -#ifdef USE_GNU_LOCALE_API - c_result = toupper_l ((int) c_chr, c_locale); -#else - RUN_IN_LOCALE_SECTION (c_locale, c_result = toupper ((int) c_chr)); -#endif + errno = ret; + scm_syserror (FUNC_NAME); } + if (uplen == 1) + upchar = SCM_MAKE_CHAR ((scm_t_wchar) upbuf[0]); else - c_result = toupper ((int) c_chr); - - return (SCM_MAKE_CHAR (c_result)); + upchar = chr; + free (upbuf); + return upchar; } #undef FUNC_NAME @@ -1152,30 +1213,45 @@ SCM_DEFINE (scm_string_locale_upcase, "string-locale-upcase", 1, 1, 0, "locale.") #define FUNC_NAME s_scm_string_locale_upcase { - const char *c_str; - char *c_ustr; + scm_t_wchar *c_str, *c_buf; + scm_t_uint32 *c_upstr; + size_t len, uplen; + int ret; scm_t_locale c_locale; + SCM upstr; SCM_VALIDATE_STRING (1, str); SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale); - - c_str = scm_i_string_chars (str); - c_ustr = (char *) alloca (strlen (c_str) + 1); + len = scm_i_string_length (str); + if (len == 0) + return scm_nullstr; + SCM_STRING_TO_U32_BUF (str, c_str); if (c_locale) - { -#ifdef USE_GNU_LOCALE_API - str_upcase_l (c_ustr, c_str, c_locale); -#else - RUN_IN_LOCALE_SECTION (c_locale, str_upcase (c_ustr, c_str)); -#endif - } + RUN_IN_LOCALE_SECTION (c_locale, ret = + u32_locale_tocase ((scm_t_uint32 *) c_str, len, + &c_upstr, + &uplen, u32_toupper)); else - str_upcase (c_ustr, c_str); + ret = + u32_locale_tocase ((scm_t_uint32 *) c_str, len, + &c_upstr, &uplen, u32_toupper); scm_remember_upto_here (str); - return (scm_from_locale_string (c_ustr)); + if (SCM_UNLIKELY (ret != 0)) + { + errno = ret; + scm_syserror (FUNC_NAME); + } + + upstr = scm_i_make_wide_string (uplen, &c_buf); + memcpy (c_buf, c_upstr, uplen * sizeof (scm_t_wchar)); + free (c_upstr); + + scm_i_try_narrow_string (upstr); + + return upstr; } #undef FUNC_NAME @@ -1186,30 +1262,45 @@ SCM_DEFINE (scm_string_locale_downcase, "string-locale-downcase", 1, 1, 0, "locale.") #define FUNC_NAME s_scm_string_locale_downcase { - const char *c_str; - char *c_lstr; + scm_t_wchar *c_str, *c_buf; + scm_t_uint32 *c_downstr; + size_t len, downlen; + int ret; scm_t_locale c_locale; + SCM downstr; SCM_VALIDATE_STRING (1, str); SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale); - - c_str = scm_i_string_chars (str); - c_lstr = (char *) alloca (strlen (c_str) + 1); + len = scm_i_string_length (str); + if (len == 0) + return scm_nullstr; + SCM_STRING_TO_U32_BUF (str, c_str); if (c_locale) - { -#ifdef USE_GNU_LOCALE_API - str_downcase_l (c_lstr, c_str, c_locale); -#else - RUN_IN_LOCALE_SECTION (c_locale, str_downcase (c_lstr, c_str)); -#endif - } + RUN_IN_LOCALE_SECTION (c_locale, ret = + u32_locale_tocase ((scm_t_uint32 *) c_str, len, + &c_downstr, + &downlen, u32_tolower)); else - str_downcase (c_lstr, c_str); + ret = + u32_locale_tocase ((scm_t_uint32 *) c_str, len, + &c_downstr, &downlen, u32_tolower); scm_remember_upto_here (str); - return (scm_from_locale_string (c_lstr)); + if (SCM_UNLIKELY (ret != 0)) + { + errno = ret; + scm_syserror (FUNC_NAME); + } + + downstr = scm_i_make_wide_string (downlen, &c_buf); + memcpy (c_buf, c_downstr, downlen * sizeof (scm_t_wchar)); + free (c_downstr); + + scm_i_try_narrow_string (downstr); + + return downstr; } #undef FUNC_NAME @@ -1328,15 +1419,16 @@ SCM_DEFINE (scm_locale_string_to_inexact, "locale-string->inexact", 2. `nl_langinfo ()' is not available on Windows. 3. `nl_langinfo ()' may return strings encoded in a locale different from - the current one, thereby defeating `scm_from_locale_string ()'. - Example: support the current locale is "Latin-1" and one asks: + the current one. + For example: (nl-langinfo DAY_1 (make-locale LC_ALL "eo_EO.UTF-8")) - The result will be a UTF-8 string. However, `scm_from_locale_string', - which expects a Latin-1 string, won't be able to make much sense of the - returned string. Thus, we'd need an `scm_from_string ()' variant where - the locale (or charset) is explicitly passed. */ + returns a result that is a UTF-8 string, regardless of the + setting of the current locale. If nl_langinfo supports CODESET, + we can convert the string properly using scm_from_stringn. If + CODESET is not supported, we won't be able to make much sense of + the returned string. */ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0, @@ -1354,6 +1446,9 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0, nl_item c_item; char *c_result; scm_t_locale c_locale; +#ifdef HAVE_LANGINFO_CODESET + char *codeset; +#endif SCM_VALIDATE_INT_COPY (2, item, c_item); SCM_VALIDATE_OPTIONAL_LOCALE_COPY (2, locale, c_locale); @@ -1370,7 +1465,10 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0, { #ifdef USE_GNU_LOCALE_API c_result = nl_langinfo_l (c_item, c_locale); -#else +#ifdef HAVE_LANGINFO_CODESET + codeset = nl_langinfo_l (CODESET, c_locale); +#endif /* HAVE_LANGINFO_CODESET */ +#else /* !USE_GNU_LOCALE_API */ /* We can't use `RUN_IN_LOCALE_SECTION ()' here because the locale mutex is already taken. */ int lsec_err; @@ -1394,6 +1492,9 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0, else { c_result = nl_langinfo (c_item); +#ifdef HAVE_LANGINFO_CODESET + codeset = nl_langinfo (CODESET); +#endif /* HAVE_LANGINFO_CODESET */ restore_locale_settings (&lsec_prev_locale); free_locale_settings (&lsec_prev_locale); @@ -1401,7 +1502,12 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0, #endif } else - c_result = nl_langinfo (c_item); + { + c_result = nl_langinfo (c_item); +#ifdef HAVE_LANGINFO_CODESET + codeset = nl_langinfo (CODESET); +#endif /* HAVE_LANGINFO_CODESET */ + } c_result = strdup (c_result); scm_i_pthread_mutex_unlock (&scm_i_locale_mutex); @@ -1509,9 +1615,16 @@ SCM_DEFINE (scm_nl_langinfo, "nl-langinfo", 1, 1, 0, #endif default: - /* FIXME: `locale_string ()' is not appropriate here because of - encoding issues (see comment above). */ - result = scm_take_locale_string (c_result); +#ifdef HAVE_LANGINFO_CODESET + result = scm_from_stringn (c_result, strlen (c_result), + codeset, + SCM_FAILED_CONVERSION_QUESTION_MARK); +#else /* !HAVE_LANGINFO_CODESET */ + /* This may be incorrectly encoded if the locale differs + from the c_locale. */ + result = scm_from_locale_string (c_result); +#endif /* !HAVE_LANGINFO_CODESET */ + free (c_result); } } @@ -1707,6 +1820,15 @@ scm_init_i18n () SCM_VARIABLE_SET (scm_global_locale, global_locale_smob); } +void +scm_bootstrap_i18n () +{ + scm_c_register_extension ("libguile", "scm_init_i18n", + (scm_t_extension_init_func) scm_init_i18n, + NULL); + +} + /* Local Variables: diff --git a/libguile/i18n.h b/libguile/i18n.h index df2970b4e..16045eb0c 100644 --- a/libguile/i18n.h +++ b/libguile/i18n.h @@ -3,7 +3,7 @@ #ifndef SCM_I18N_H #define SCM_I18N_H -/* Copyright (C) 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -42,9 +42,12 @@ SCM_API SCM scm_string_locale_upcase (SCM chr, SCM locale); SCM_API SCM scm_string_locale_downcase (SCM chr, SCM locale); SCM_API SCM scm_locale_string_to_integer (SCM str, SCM base, SCM locale); SCM_API SCM scm_locale_string_to_inexact (SCM str, SCM locale); -SCM_API SCM scm_nl_langinfo (SCM item, SCM locale); -SCM_API void scm_init_i18n (void); +SCM_INTERNAL SCM scm_nl_langinfo (SCM item, SCM locale); + +SCM_INTERNAL void scm_init_i18n (void); +SCM_INTERNAL void scm_bootstrap_i18n (void); + #endif /* SCM_I18N_H */ diff --git a/libguile/inet_aton.c b/libguile/inet_aton.c index ebef71f55..c89378bb1 100644 --- a/libguile/inet_aton.c +++ b/libguile/inet_aton.c @@ -103,14 +103,14 @@ inet_aton(const char *cp_arg, struct in_addr *addr) base = 8; } while ((c = *cp) != '\0') { - if (isascii(c) && isdigit(c)) { + if (isascii ((int) c) && isdigit ((int) c)) { val = (val * base) + (c - '0'); cp++; continue; } - if (base == 16 && isascii(c) && isxdigit(c)) { + if (base == 16 && isascii ((int) c) && isxdigit ((int) c)) { val = (val << 4) + - (c + 10 - (islower(c) ? 'a' : 'A')); + (c + 10 - (islower((int) c) ? 'a' : 'A')); cp++; continue; } @@ -132,7 +132,7 @@ inet_aton(const char *cp_arg, struct in_addr *addr) /* * Check for trailing characters. */ - if (*cp && (!isascii(*cp) || !isspace(*cp))) + if (*cp && (!isascii ((int) (*cp)) || !isspace ((int) (*cp)))) return (0); /* * Concoct the address according to diff --git a/libguile/init.c b/libguile/init.c index 940d515f6..68156ef28 100644 --- a/libguile/init.c +++ b/libguile/init.c @@ -60,7 +60,6 @@ #include "libguile/filesys.h" #include "libguile/fluids.h" #include "libguile/fports.h" -#include "libguile/futures.h" #include "libguile/gc.h" #include "libguile/gdbint.h" #include "libguile/generalized-arrays.h" @@ -71,6 +70,7 @@ #include "libguile/hashtab.h" #include "libguile/hooks.h" #include "libguile/gettext.h" +#include "libguile/i18n.h" #include "libguile/iselect.h" #include "libguile/ioext.h" #include "libguile/keywords.h" @@ -478,11 +478,6 @@ scm_i_init_guile (SCM_STACKITEM *base) scm_init_dynwind (); scm_init_eq (); scm_init_error (); -#if 0 - /* See futures.h for a comment why futures are not enabled. - */ - scm_init_futures (); -#endif scm_init_fluids (); scm_init_feature (); /* Requires fluids */ scm_init_backtrace (); /* Requires fluids */ @@ -561,6 +556,7 @@ scm_i_init_guile (SCM_STACKITEM *base) scm_init_load_path (); scm_init_standard_ports (); /* Requires fports */ scm_init_dynamic_linking (); + scm_bootstrap_i18n (); #if SCM_ENABLE_ELISP scm_init_lang (); #endif /* SCM_ENABLE_ELISP */ diff --git a/libguile/inline.h b/libguile/inline.h index 49431697f..09a1b5a35 100644 --- a/libguile/inline.h +++ b/libguile/inline.h @@ -78,7 +78,7 @@ "inline.c", when `inline' is not supported at all or when "extern inline" is used. */ -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" SCM_API SCM scm_cell (scm_t_bits car, scm_t_bits cdr); @@ -243,7 +243,7 @@ SCM_C_EXTERN_INLINE SCM scm_array_handle_ref (scm_t_array_handle *h, ssize_t p) { - if (SCM_UNLIKELY (p < 0 && -p > h->base)) + if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base)) /* catch overflow */ scm_out_of_range (NULL, scm_from_ssize_t (p)); /* perhaps should catch overflow here too */ @@ -256,7 +256,7 @@ SCM_C_EXTERN_INLINE void scm_array_handle_set (scm_t_array_handle *h, ssize_t p, SCM v) { - if (SCM_UNLIKELY (p < 0 && -p > h->base)) + if (SCM_UNLIKELY (p < 0 && ((size_t)-p) > h->base)) /* catch overflow */ scm_out_of_range (NULL, scm_from_ssize_t (p)); /* perhaps should catch overflow here too */ diff --git a/libguile/lang.h b/libguile/lang.h index 47128de57..b86fb2e0a 100644 --- a/libguile/lang.h +++ b/libguile/lang.h @@ -3,7 +3,7 @@ #ifndef SCM_LANG_H #define SCM_LANG_H -/* Copyright (C) 1998, 2004, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1998, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -39,7 +39,7 @@ SCM_INTERNAL void scm_init_lang (void); #endif /* ! SCM_ENABLE_ELISP */ -#define SCM_NULL_OR_NIL_P(x) (scm_is_null (x) || SCM_NILP (x)) +#define SCM_NULL_OR_NIL_P(x) (scm_is_null_or_nil (x)) #endif /* SCM_LANG_H */ diff --git a/libguile/load.c b/libguile/load.c index fa25b0f84..50af25643 100644 --- a/libguile/load.c +++ b/libguile/load.c @@ -601,6 +601,8 @@ compiled_is_fresh (SCM full_filename, SCM compiled_filename) return res; } +SCM_KEYWORD (kw_env, "env"); + static SCM do_try_autocompile (void *data) { @@ -617,7 +619,9 @@ do_try_autocompile (void *data) if (scm_is_true (compile_file)) { - SCM res = scm_call_1 (scm_variable_ref (compile_file), source); + /* Auto-compile in the context of the current module. */ + SCM res = scm_call_3 (scm_variable_ref (compile_file), source, + kw_env, scm_current_module ()); scm_puts (";;; compiled ", scm_current_error_port ()); scm_display (res, scm_current_error_port ()); scm_newline (scm_current_error_port ()); @@ -681,8 +685,8 @@ scm_try_autocompile (SCM source) NULL, NULL); } -SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 1, 0, - (SCM filename, SCM exception_on_not_found), +SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 0, 0, 1, + (SCM args), "Search @var{%load-path} for the file named @var{filename} and\n" "load it into the top-level environment. If @var{filename} is a\n" "relative pathname and is not found in the list of search paths,\n" @@ -691,9 +695,33 @@ SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 1, 0, "@code{#f} is returned instead.") #define FUNC_NAME s_scm_primitive_load_path { + SCM filename, exception_on_not_found; SCM full_filename, compiled_filename; int compiled_is_fallback = 0; + if (scm_is_string (args)) + { + /* C code written for 1.8 and earlier expects this function to take a + single argument (the file name). */ + filename = args; + exception_on_not_found = SCM_UNDEFINED; + } + else + { + /* Starting from 1.9, this function takes 1 required and 1 optional + argument. */ + long len; + + SCM_VALIDATE_LIST_COPYLEN (SCM_ARG1, args, len); + if (len < 1 || len > 2) + scm_error_num_args_subr (FUNC_NAME); + + filename = SCM_CAR (args); + SCM_VALIDATE_STRING (SCM_ARG1, filename); + + exception_on_not_found = len > 1 ? SCM_CADR (args) : SCM_UNDEFINED; + } + if (SCM_UNBNDP (exception_on_not_found)) exception_on_not_found = SCM_BOOL_T; @@ -752,7 +780,7 @@ SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 1, 0, scm_puts (";;; found fresh local cache at ", scm_current_error_port ()); scm_display (fallback, scm_current_error_port ()); scm_newline (scm_current_error_port ()); - return scm_load_compiled_with_vm (compiled_filename); + return scm_load_compiled_with_vm (fallback); } } @@ -771,13 +799,16 @@ SCM_DEFINE (scm_primitive_load_path, "primitive-load-path", 1, 1, 0, SCM scm_c_primitive_load_path (const char *filename) { - return scm_primitive_load_path (scm_from_locale_string (filename), - SCM_BOOL_T); + return scm_primitive_load_path (scm_from_locale_string (filename)); } /* Information about the build environment. */ +SCM_VARIABLE_INIT (sys_host_type, "%host-type", + scm_from_locale_string (HOST_TYPE)); + + /* Initialize the scheme variable %guile-build-info, based on data provided by the Makefile, via libpath.h. */ static void diff --git a/libguile/load.h b/libguile/load.h index 1a1a86528..cf825fc0f 100644 --- a/libguile/load.h +++ b/libguile/load.h @@ -3,7 +3,7 @@ #ifndef SCM_LOAD_H #define SCM_LOAD_H -/* Copyright (C) 1995,1996,1998,2000,2001, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1998,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -34,7 +34,7 @@ SCM_API SCM scm_sys_library_dir (void); SCM_API SCM scm_sys_site_dir (void); SCM_API SCM scm_search_path (SCM path, SCM filename, SCM exts, SCM require_exts); SCM_API SCM scm_sys_search_load_path (SCM filename); -SCM_API SCM scm_primitive_load_path (SCM filename, SCM exception_on_not_found); +SCM_API SCM scm_primitive_load_path (SCM filename_and_exception_on_not_found); SCM_API SCM scm_c_primitive_load_path (const char *filename); SCM_INTERNAL SCM scm_sys_warn_autocompilation_enabled (void); SCM_INTERNAL void scm_init_load_path (void); diff --git a/libguile/macros.c b/libguile/macros.c index 3e0942c43..7d60a8ea1 100644 --- a/libguile/macros.c +++ b/libguile/macros.c @@ -22,6 +22,8 @@ # include <config.h> #endif +#define SCM_BUILDING_DEPRECATED_CODE + #include "libguile/_scm.h" #include "libguile/alist.h" /* for SCM_EXTEND_ENV (well...) */ #include "libguile/eval.h" diff --git a/libguile/macros.h b/libguile/macros.h index 8ff41c4a4..f05d2f161 100644 --- a/libguile/macros.h +++ b/libguile/macros.h @@ -3,7 +3,7 @@ #ifndef SCM_MACROS_H #define SCM_MACROS_H -/* Copyright (C) 1998,2000,2001,2002,2003, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1998,2000,2001,2002,2003, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -61,7 +61,7 @@ SCM_API SCM scm_make_synt (const char *name, SCM_INTERNAL void scm_init_macros (void); #if SCM_ENABLE_DEPRECATED == 1 -SCM_API SCM scm_makmacro (SCM code); +SCM_DEPRECATED SCM scm_makmacro (SCM code); #endif #endif /* SCM_MACROS_H */ diff --git a/libguile/modules.c b/libguile/modules.c index deae23a59..c7f0a46b0 100644 --- a/libguile/modules.c +++ b/libguile/modules.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008 Free Software Foundation, Inc. +/* Copyright (C) 1998,2000,2001,2002,2003,2004,2006,2007,2008,2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -46,6 +46,12 @@ static SCM the_module; static SCM the_root_module_var; +static SCM unbound_variable (const char *func, SCM sym) +{ + scm_error (scm_from_locale_symbol ("unbound-variable"), func, + "Unbound variable: ~S", scm_list_1 (sym), SCM_BOOL_F); +} + static SCM the_root_module () { @@ -740,7 +746,7 @@ scm_module_lookup (SCM module, SCM sym) var = scm_sym2var (sym, scm_module_lookup_closure (module), SCM_BOOL_F); if (scm_is_false (var)) - SCM_MISC_ERROR ("unbound variable: ~S", scm_list_1 (sym)); + unbound_variable (FUNC_NAME, sym); return var; } #undef FUNC_NAME @@ -757,7 +763,7 @@ scm_lookup (SCM sym) SCM var = scm_sym2var (sym, scm_current_module_lookup_closure (), SCM_BOOL_F); if (scm_is_false (var)) - scm_misc_error ("scm_lookup", "unbound variable: ~S", scm_list_1 (sym)); + unbound_variable (NULL, sym); return var; } diff --git a/libguile/objcodes.h b/libguile/objcodes.h index 2bb4e6040..ab4db3dab 100644 --- a/libguile/objcodes.h +++ b/libguile/objcodes.h @@ -23,13 +23,9 @@ /* objcode data should be directly mappable to this C structure. */ struct scm_objcode { - scm_t_uint8 nargs; - scm_t_uint8 nrest; - scm_t_uint16 nlocs; scm_t_uint32 len; /* the maximum index of base[] */ scm_t_uint32 metalen; /* well, i lie. this many bytes at the end of base[] for metadata */ - scm_t_uint32 unused; /* pad so that `base' is 8-byte aligned */ scm_t_uint8 base[0]; }; @@ -46,9 +42,6 @@ SCM_API scm_t_bits scm_tc16_objcode; #define SCM_OBJCODE_LEN(x) (SCM_OBJCODE_DATA (x)->len) #define SCM_OBJCODE_META_LEN(x) (SCM_OBJCODE_DATA (x)->metalen) #define SCM_OBJCODE_TOTAL_LEN(x) (SCM_OBJCODE_LEN (x) + SCM_OBJCODE_META_LEN (x)) -#define SCM_OBJCODE_NARGS(x) (SCM_OBJCODE_DATA (x)->nargs) -#define SCM_OBJCODE_NREST(x) (SCM_OBJCODE_DATA (x)->nrest) -#define SCM_OBJCODE_NLOCS(x) (SCM_OBJCODE_DATA (x)->nlocs) #define SCM_OBJCODE_BASE(x) (SCM_OBJCODE_DATA (x)->base) #define SCM_OBJCODE_IS_MMAP(x) (SCM_SMOB_FLAGS (x) & SCM_F_OBJCODE_IS_MMAP) diff --git a/libguile/pairs.c b/libguile/pairs.c index aaaeb110f..fb8b21f11 100644 --- a/libguile/pairs.c +++ b/libguile/pairs.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,2000,2001, 2004, 2005, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,2000,2001, 2004, 2005, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -27,11 +27,25 @@ #include "libguile/pairs.h" +#include "verify.h" + /* {Pairs} */ +/* + * This compile-time test verifies the properties needed for the + * efficient test macro scm_is_null_or_nil defined in pairs.h, + * which is defined in terms of the SCM_MATCHES_BITS_IN_COMMON macro. + * + * See the comments preceeding the definitions of SCM_BOOL_F and + * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information. + */ +verify (SCM_VALUES_DIFFER_IN_EXACTLY_ONE_BIT_POSITION \ + (SCM_ELISP_NIL, SCM_EOL)); + + #if (SCM_DEBUG_PAIR_ACCESSES == 1) #include "libguile/ports.h" diff --git a/libguile/pairs.h b/libguile/pairs.h index a6d44d289..47bb187ff 100644 --- a/libguile/pairs.h +++ b/libguile/pairs.h @@ -3,7 +3,7 @@ #ifndef SCM_PAIRS_H #define SCM_PAIRS_H -/* Copyright (C) 1995,1996,2000,2001, 2004, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,2000,2001, 2004, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -34,7 +34,32 @@ # define SCM_VALIDATE_PAIR(cell, expr) (expr) #endif -#define scm_is_null(x) (scm_is_eq ((x), SCM_EOL)) +/* + * Use scm_is_null_and_not_nil if it's important (for correctness) + * that %nil must NOT be considered null. + */ +#define scm_is_null_and_not_nil(x) (scm_is_eq ((x), SCM_EOL)) + +/* + * Use scm_is_null_assume_not_nil if %nil will never be tested, + * for increased efficiency. + */ +#define scm_is_null_assume_not_nil(x) (scm_is_eq ((x), SCM_EOL)) + +/* + * See the comments preceeding the definitions of SCM_BOOL_F and + * SCM_MATCHES_BITS_IN_COMMON in tags.h for more information on + * how the following macro works. + */ +#if SCM_ENABLE_ELISP +# define scm_is_null_or_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_EOL)) +#else +# define scm_is_null_or_nil(x) (scm_is_null_assume_not_nil (x)) +#endif + +/* XXX Should scm_is_null treat %nil as null by default? */ +#define scm_is_null(x) (scm_is_null_and_not_nil(x)) #define SCM_CAR(x) (SCM_VALIDATE_PAIR (x, SCM_CELL_OBJECT_0 (x))) #define SCM_CDR(x) (SCM_VALIDATE_PAIR (x, SCM_CELL_OBJECT_1 (x))) diff --git a/libguile/ports.c b/libguile/ports.c index e3d2b0da6..c3415c770 100644 --- a/libguile/ports.c +++ b/libguile/ports.c @@ -127,12 +127,6 @@ end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED) { } -static size_t -scm_port_free0 (SCM port) -{ - return 0; -} - scm_t_bits scm_make_port_type (char *name, int (*fill_input) (SCM port), @@ -153,7 +147,7 @@ scm_make_port_type (char *name, scm_ptobs[scm_numptob].name = name; scm_ptobs[scm_numptob].mark = 0; - scm_ptobs[scm_numptob].free = scm_port_free0; + scm_ptobs[scm_numptob].free = NULL; scm_ptobs[scm_numptob].print = scm_port_print; scm_ptobs[scm_numptob].equalp = 0; scm_ptobs[scm_numptob].close = 0; @@ -593,7 +587,7 @@ scm_new_port_table_entry (scm_t_bits tag) if ((enc = scm_i_get_port_encoding (SCM_BOOL_F)) == NULL) entry->encoding = NULL; else - entry->encoding = strdup (enc); + entry->encoding = scm_gc_strdup (enc, "port"); entry->ilseq_handler = scm_i_get_conversion_strategy (SCM_BOOL_F); SCM_SET_CELL_TYPE (z, tag); @@ -633,14 +627,11 @@ scm_i_remove_port (SCM port) #define FUNC_NAME "scm_remove_port" { scm_t_port *p = SCM_PTAB_ENTRY (port); - if (p->putback_buf) - scm_gc_free (p->putback_buf, p->putback_buf_size, "putback buffer"); - if (p->encoding) - { - free (p->encoding); - p->encoding = NULL; - } - scm_gc_free (p, sizeof (scm_t_port), "port"); + + scm_port_non_buffer (p); + + p->putback_buf = NULL; + p->putback_buf_size = 0; SCM_SETPTAB_ENTRY (port, 0); scm_hashq_remove_x (scm_i_port_weak_hash, port); @@ -1358,7 +1349,7 @@ scm_c_read (SCM port, void *buffer, size_t size) requested number of bytes. (Note that a single scm_fill_input call does not guarantee to fill the whole of the port's read buffer.) */ - if (pt->read_buf_size <= 1) + if (pt->read_buf_size <= 1 && pt->encoding == NULL) { /* The port that we are reading from is unbuffered - i.e. does not have its own persistent buffer - but we have a buffer, @@ -1370,7 +1361,14 @@ scm_c_read (SCM port, void *buffer, size_t size) We need to make sure that the port's normal (1 byte) buffer is reinstated in case one of the scm_fill_input () calls throws an exception; we use the scm_dynwind_* API to achieve - that. */ + that. + + A consequence of this optimization is that the fill_input + functions can't unget characters. That'll push data to the + pushback buffer instead of this psb buffer. */ +#if SCM_DEBUG == 1 + unsigned char *pback = pt->putback_buf; +#endif psb.pt = pt; psb.buffer = buffer; psb.size = size; @@ -1385,8 +1383,15 @@ scm_c_read (SCM port, void *buffer, size_t size) pt->read_buf_size -= (pt->read_end - pt->read_pos); pt->read_pos = pt->read_buf = pt->read_end; } +#if SCM_DEBUG == 1 + if (pback != pt->putback_buf + || pt->read_buf - (unsigned char *) buffer < 0) + scm_misc_error (FUNC_NAME, + "scm_c_read must not call a fill function that pushes " + "back characters onto an unbuffered port", SCM_EOL); +#endif n_read += pt->read_buf - (unsigned char *) buffer; - + /* Reinstate the port's normal buffer. */ scm_dynwind_end (); } @@ -1490,8 +1495,6 @@ scm_unget_byte (int c, SCM port) { size_t new_size = pt->read_buf_size * 2; unsigned char *tmp = (unsigned char *) - /* XXX: Can we use `GC_REALLOC' with `GC_MALLOC_ATOMIC'-allocated - data? (Ludo) */ scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size, "putback buffer"); @@ -1952,7 +1955,7 @@ scm_i_get_port_encoding (SCM port) } } -/* Returns ENC is if is a recognized encoding. If it isn't, it tries +/* Returns ENC if it is a recognized encoding. If it isn't, it tries to find an alias of ENC that is valid. Otherwise, it returns NULL. */ static const char * @@ -2016,12 +2019,10 @@ scm_i_set_port_encoding_x (SCM port, const char *enc) { /* Set the character encoding for this port. */ pt = SCM_PTAB_ENTRY (port); - if (pt->encoding) - free (pt->encoding); if (valid_enc == NULL) pt->encoding = NULL; else - pt->encoding = strdup (valid_enc); + pt->encoding = scm_gc_strdup (valid_enc, "port"); } } diff --git a/libguile/ports.h b/libguile/ports.h index 0f46e7f51..2ac736e44 100644 --- a/libguile/ports.h +++ b/libguile/ports.h @@ -310,7 +310,7 @@ SCM_API SCM scm_sys_make_void_port (SCM mode); SCM_INTERNAL void scm_init_ports (void); #if SCM_ENABLE_DEPRECATED==1 -SCM_API scm_t_port * scm_add_to_port_table (SCM port); +SCM_DEPRECATED scm_t_port * scm_add_to_port_table (SCM port); #endif #ifdef GUILE_DEBUG diff --git a/libguile/print.c b/libguile/print.c index 3bb6cb167..b07e2067f 100644 --- a/libguile/print.c +++ b/libguile/print.c @@ -63,18 +63,17 @@ static const char *iflagnames[] = { "#f", + "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */ + "#<XXX UNUSED LISP FALSE -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>", + "()", "#t", + "#<XXX UNUSED BOOLEAN -- DO NOT USE -- SHOULD NEVER BE SEEN XXX>", + "#<unspecified>", "#<undefined>", "#<eof>", - "()", - "#<unspecified>", /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */ "#<unbound>", - - /* Elisp nil value. This is its Scheme name; whenever it's printed in - * Elisp, it should appear as the symbol `nil'. */ - "#nil" }; SCM_SYMBOL (sym_reader, "reader"); @@ -463,13 +462,26 @@ iprin1 (SCM exp, SCM port, scm_print_state *pstate) /* Print the character if is graphic character. */ { scm_t_wchar *wbuf; - SCM wstr = scm_i_make_wide_string (1, &wbuf); + SCM wstr; char *buf; size_t len; const char *enc; enc = scm_i_get_port_encoding (port); - wbuf[0] = i; + if (uc_combining_class (i) == UC_CCC_NR) + { + wstr = scm_i_make_wide_string (1, &wbuf); + wbuf[0] = i; + } + else + { + /* Character is a combining character: print it connected + to a dotted circle instead of connecting it to the + backslash in '#\' */ + wstr = scm_i_make_wide_string (2, &wbuf); + wbuf[0] = SCM_CODEPOINT_DOTTED_CIRCLE; + wbuf[1] = i; + } if (enc == NULL) { if (i <= 0xFF) @@ -1220,8 +1232,8 @@ SCM_DEFINE (scm_write_char, "write-char", 1, 1, 0, SCM_VALIDATE_CHAR (1, chr); SCM_VALIDATE_OPORT_VALUE (2, port); - - scm_putc ((int) SCM_CHAR (chr), SCM_COERCE_OUTPORT (port)); + + scm_i_charprint (SCM_CHAR (chr), SCM_COERCE_OUTPORT (port)); #if 0 #ifdef HAVE_PIPE # ifdef EPIPE diff --git a/libguile/procprop.c b/libguile/procprop.c index 5054291b1..dcbfba794 100644 --- a/libguile/procprop.c +++ b/libguile/procprop.c @@ -42,6 +42,9 @@ SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure"); SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity"); +static SCM non_closure_props; +static scm_i_pthread_mutex_t non_closure_props_lock = SCM_I_PTHREAD_MUTEX_INITIALIZER; + SCM scm_i_procedure_arity (SCM proc) { @@ -74,10 +77,10 @@ scm_i_procedure_arity (SCM proc) r = 1; break; case scm_tc7_program: - a += SCM_PROGRAM_DATA (proc)->nargs; - r = SCM_PROGRAM_DATA (proc)->nrest; - a -= r; - break; + if (scm_i_program_arity (proc, &a, &o, &r)) + break; + else + return SCM_BOOL_F; case scm_tc7_lsubr_2: a += 2; r = 1; @@ -137,92 +140,99 @@ scm_i_procedure_arity (SCM proc) return scm_list_3 (scm_from_int (a), scm_from_int (o), scm_from_bool(r)); } -/* XXX - instead of using a stand-in value for everything except - closures, we should find other ways to store the procedure - properties for those other kinds of procedures. For example, subrs - have their own property slot, which is unused at present. -*/ - -static SCM -scm_stand_in_scm_proc(SCM proc) -{ - SCM handle, answer; - handle = scm_hashq_get_handle (scm_stand_in_procs, proc); - if (scm_is_false (handle)) - { - answer = scm_closure (scm_list_2 (SCM_EOL, SCM_BOOL_F), SCM_EOL); - scm_hashq_set_x (scm_stand_in_procs, proc, answer); - } - else - answer = SCM_CDR (handle); - return answer; -} +/* FIXME: instead of the weak hash, perhaps for some kinds of procedures, use + other means; for example subrs have their own property slot, which is unused + at present. */ SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0, (SCM proc), "Return @var{obj}'s property list.") #define FUNC_NAME s_scm_procedure_properties { + SCM props; + SCM_VALIDATE_PROC (1, proc); - return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc), - SCM_PROCPROPS (SCM_CLOSUREP (proc) - ? proc - : scm_stand_in_scm_proc (proc))); + if (SCM_CLOSUREP (proc)) + props = SCM_PROCPROPS (proc); + else + { + scm_i_pthread_mutex_lock (&non_closure_props_lock); + props = scm_hashq_ref (non_closure_props, proc, SCM_EOL); + scm_i_pthread_mutex_unlock (&non_closure_props_lock); + } + return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc), props); } #undef FUNC_NAME SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0, - (SCM proc, SCM new_val), - "Set @var{obj}'s property list to @var{alist}.") + (SCM proc, SCM alist), + "Set @var{proc}'s property list to @var{alist}.") #define FUNC_NAME s_scm_set_procedure_properties_x { - if (!SCM_CLOSUREP (proc)) - proc = scm_stand_in_scm_proc(proc); - SCM_VALIDATE_CLOSURE (1, proc); - SCM_SETPROCPROPS (proc, new_val); + SCM_VALIDATE_PROC (1, proc); + + if (SCM_CLOSUREP (proc)) + SCM_SETPROCPROPS (proc, alist); + else + { + scm_i_pthread_mutex_lock (&non_closure_props_lock); + scm_hashq_set_x (non_closure_props, proc, alist); + scm_i_pthread_mutex_unlock (&non_closure_props_lock); + } return SCM_UNSPECIFIED; } #undef FUNC_NAME SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0, - (SCM p, SCM k), - "Return the property of @var{obj} with name @var{key}.") + (SCM proc, SCM key), + "Return the property of @var{proc} with name @var{key}.") #define FUNC_NAME s_scm_procedure_property { - SCM assoc; - if (scm_is_eq (k, scm_sym_arity)) + SCM_VALIDATE_PROC (1, proc); + + if (scm_is_eq (key, scm_sym_arity)) + /* avoid a cons in this case */ + return scm_i_procedure_arity (proc); + else { - SCM arity; - SCM_ASSERT (scm_is_true (arity = scm_i_procedure_arity (p)), - p, SCM_ARG1, FUNC_NAME); - return arity; + SCM props; + if (SCM_CLOSUREP (proc)) + props = SCM_PROCPROPS (proc); + else + { + scm_i_pthread_mutex_lock (&non_closure_props_lock); + props = scm_hashq_ref (non_closure_props, proc, SCM_EOL); + scm_i_pthread_mutex_unlock (&non_closure_props_lock); + } + return scm_assq_ref (props, key); } - SCM_VALIDATE_PROC (1, p); - assoc = scm_sloppy_assq (k, - SCM_PROCPROPS (SCM_CLOSUREP (p) - ? p - : scm_stand_in_scm_proc (p))); - return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F); } #undef FUNC_NAME SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0, - (SCM p, SCM k, SCM v), - "In @var{obj}'s property list, set the property named @var{key} to\n" - "@var{value}.") + (SCM proc, SCM key, SCM val), + "In @var{proc}'s property list, set the property named @var{key} to\n" + "@var{val}.") #define FUNC_NAME s_scm_set_procedure_property_x { - SCM assoc; - if (!SCM_CLOSUREP (p)) - p = scm_stand_in_scm_proc(p); - SCM_VALIDATE_CLOSURE (1, p); - if (scm_is_eq (k, scm_sym_arity)) + SCM_VALIDATE_PROC (1, proc); + + if (scm_is_eq (key, scm_sym_arity)) SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL); - assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p)); - if (SCM_NIMP (assoc)) - SCM_SETCDR (assoc, v); + + if (SCM_CLOSUREP (proc)) + SCM_SETPROCPROPS (proc, + scm_assq_set_x (SCM_PROCPROPS (proc), key, val)); else - SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p))); + { + scm_i_pthread_mutex_lock (&non_closure_props_lock); + scm_hashq_set_x (non_closure_props, proc, + scm_assq_set_x (scm_hashq_ref (non_closure_props, proc, + SCM_EOL), + key, val)); + scm_i_pthread_mutex_unlock (&non_closure_props_lock); + } + return SCM_UNSPECIFIED; } #undef FUNC_NAME @@ -233,6 +243,7 @@ SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0, void scm_init_procprop () { + non_closure_props = scm_make_weak_key_hash_table (SCM_UNDEFINED); #include "libguile/procprop.x" } diff --git a/libguile/procprop.h b/libguile/procprop.h index 04cd38442..7a1131489 100644 --- a/libguile/procprop.h +++ b/libguile/procprop.h @@ -3,7 +3,7 @@ #ifndef SCM_PROCPROP_H #define SCM_PROCPROP_H -/* Copyright (C) 1995,1996,1998,2000, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1998,2000, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -35,9 +35,9 @@ SCM_API SCM scm_sym_system_procedure; SCM_INTERNAL SCM scm_i_procedure_arity (SCM proc); SCM_API SCM scm_procedure_properties (SCM proc); -SCM_API SCM scm_set_procedure_properties_x (SCM proc, SCM new_val); -SCM_API SCM scm_procedure_property (SCM p, SCM k); -SCM_API SCM scm_set_procedure_property_x (SCM p, SCM k, SCM v); +SCM_API SCM scm_set_procedure_properties_x (SCM proc, SCM alist); +SCM_API SCM scm_procedure_property (SCM proc, SCM key); +SCM_API SCM scm_set_procedure_property_x (SCM proc, SCM key, SCM val); SCM_INTERNAL void scm_init_procprop (void); #endif /* SCM_PROCPROP_H */ diff --git a/libguile/procs.c b/libguile/procs.c index 40d6231bb..5de2f33f1 100644 --- a/libguile/procs.c +++ b/libguile/procs.c @@ -144,16 +144,18 @@ SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0, case scm_tc7_gsubr: return scm_from_bool (SCM_GSUBR_REQ (SCM_GSUBR_TYPE (obj)) == 0); case scm_tc7_program: - return scm_from_bool (SCM_PROGRAM_DATA (obj)->nargs == 0 - || (SCM_PROGRAM_DATA (obj)->nargs == 1 - && SCM_PROGRAM_DATA (obj)->nrest)); + { + int a, o, r; + if (scm_i_program_arity (obj, &a, &o, &r)) + return scm_from_bool (a == 0); + else + return SCM_BOOL_F; + } case scm_tc7_pws: obj = SCM_PROCEDURE (obj); goto again; default: - if (SCM_PROGRAM_P (obj) && SCM_PROGRAM_DATA (obj)->nargs == 0) - return SCM_BOOL_T; - /* otherwise fall through */ + return SCM_BOOL_F; } } return SCM_BOOL_F; diff --git a/libguile/programs.c b/libguile/programs.c index b2bf80674..7399fa05f 100644 --- a/libguile/programs.c +++ b/libguile/programs.c @@ -102,22 +102,6 @@ SCM_DEFINE (scm_program_base, "program-base", 1, 0, 0, } #undef FUNC_NAME -SCM_DEFINE (scm_program_arity, "program-arity", 1, 0, 0, - (SCM program), - "") -#define FUNC_NAME s_scm_program_arity -{ - struct scm_objcode *p; - - SCM_VALIDATE_PROGRAM (1, program); - - p = SCM_PROGRAM_DATA (program); - return scm_list_3 (SCM_I_MAKINUM (p->nargs), - SCM_I_MAKINUM (p->nrest), - SCM_I_MAKINUM (p->nlocs)); -} -#undef FUNC_NAME - SCM_DEFINE (scm_program_objects, "program-objects", 1, 0, 0, (SCM program), "") @@ -209,6 +193,23 @@ SCM_DEFINE (scm_program_sources, "program-sources", 1, 0, 0, } #undef FUNC_NAME +SCM_DEFINE (scm_program_arities, "program-arities", 1, 0, 0, + (SCM program), + "") +#define FUNC_NAME s_scm_program_arities +{ + SCM meta; + + SCM_VALIDATE_PROGRAM (1, program); + + meta = scm_program_meta (program); + if (scm_is_false (meta)) + return SCM_BOOL_F; + + return scm_caddr (scm_call_0 (meta)); +} +#undef FUNC_NAME + SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0, (SCM program), "") @@ -222,7 +223,7 @@ SCM_DEFINE (scm_program_properties, "program-properties", 1, 0, 0, if (scm_is_false (meta)) return SCM_EOL; - return scm_cddr (scm_call_0 (meta)); + return scm_cdddr (scm_call_0 (meta)); } #undef FUNC_NAME @@ -281,11 +282,51 @@ SCM_DEFINE (scm_program_objcode, "program-objcode", 1, 0, 0, } #undef FUNC_NAME +/* This one is a shim to pre-case-lambda internal interfaces. Avoid it if you + can -- use program-arguments or the like. */ +static SCM sym_arglist; +int +scm_i_program_arity (SCM program, int *req, int *opt, int *rest) +{ + SCM arities, x; + + arities = scm_program_arities (program); + if (!scm_is_pair (arities)) + return 0; + /* take the last arglist, it will be least specific */ + while (scm_is_pair (scm_cdr (arities))) + arities = scm_cdr (arities); + x = scm_cddar (arities); + if (scm_is_pair (x)) + { + *req = scm_to_int (scm_car (x)); + x = scm_cdr (x); + if (scm_is_pair (x)) + { + *opt = scm_to_int (scm_car (x)); + x = scm_cdr (x); + if (scm_is_pair (x)) + *rest = scm_is_true (scm_car (x)); + else + *rest = 0; + } + else + *opt = *rest = 0; + } + else + *req = *opt = *rest = 0; + + return 1; +} + void scm_bootstrap_programs (void) { + /* arglist can't be snarfed, because snarfage is only loaded when (system vm + program) is loaded. perhaps static-alloc will fix this. */ + sym_arglist = scm_from_locale_symbol ("arglist"); scm_c_register_extension ("libguile", "scm_init_programs", (scm_t_extension_init_func)scm_init_programs, NULL); } diff --git a/libguile/programs.h b/libguile/programs.h index d52631fbb..836f1ff0d 100644 --- a/libguile/programs.h +++ b/libguile/programs.h @@ -26,7 +26,7 @@ * Programs */ -#define SCM_F_PROGRAM_IS_BOOT (1<<16) +#define SCM_F_PROGRAM_IS_BOOT 0x100 #define SCM_PROGRAM_P(x) (!SCM_IMP (x) && SCM_TYP7(x) == scm_tc7_program) #define SCM_PROGRAM_OBJCODE(x) (SCM_CELL_OBJECT_1 (x)) @@ -40,11 +40,11 @@ SCM_API SCM scm_make_program (SCM objcode, SCM objtable, SCM free_variables); SCM_API SCM scm_program_p (SCM obj); SCM_API SCM scm_program_base (SCM program); -SCM_API SCM scm_program_arity (SCM program); SCM_API SCM scm_program_meta (SCM program); SCM_API SCM scm_program_bindings (SCM program); SCM_API SCM scm_program_sources (SCM program); SCM_API SCM scm_program_source (SCM program, SCM ip); +SCM_API SCM scm_program_arities (SCM program); SCM_API SCM scm_program_properties (SCM program); SCM_API SCM scm_program_name (SCM program); SCM_API SCM scm_program_objects (SCM program); @@ -54,6 +54,7 @@ SCM_API SCM scm_program_objcode (SCM program); SCM_API SCM scm_c_program_source (SCM program, size_t ip); +SCM_INTERNAL int scm_i_program_arity (SCM program, int *req, int *opt, int *rest); SCM_INTERNAL void scm_i_program_print (SCM program, SCM port, scm_print_state *pstate); SCM_INTERNAL void scm_bootstrap_programs (void); diff --git a/libguile/pthread-threads.h b/libguile/pthread-threads.h index 4f72a4293..ca72f1674 100644 --- a/libguile/pthread-threads.h +++ b/libguile/pthread-threads.h @@ -30,7 +30,7 @@ #include <sched.h> /* `libgc' intercepts pthread calls by defining wrapping macros. */ -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" /* Threads */ diff --git a/libguile/read.c b/libguile/read.c index d91c868e1..fc56418df 100644 --- a/libguile/read.c +++ b/libguile/read.c @@ -181,8 +181,10 @@ static SCM *scm_read_hash_procedures; || ((_chr) == 'd') || ((_chr) == 'l')) /* Read an SCSH block comment. */ -static inline SCM scm_read_scsh_block_comment (int chr, SCM port); -static SCM scm_read_commented_expression (int chr, SCM port); +static inline SCM scm_read_scsh_block_comment (scm_t_wchar, SCM); +static SCM scm_read_r6rs_block_comment (scm_t_wchar, SCM); +static SCM scm_read_commented_expression (scm_t_wchar, SCM); +static SCM scm_get_hash_procedure (int); /* Read from PORT until a delimiter (e.g., a whitespace) is read. Return zero if the whole token fits in BUF, non-zero otherwise. */ @@ -289,6 +291,13 @@ flush_ws (SCM port, const char *eoferr) case ';': scm_read_commented_expression (c, port); break; + case '|': + if (scm_is_false (scm_get_hash_procedure (c))) + { + scm_read_r6rs_block_comment (c, port); + break; + } + /* fall through */ default: scm_ungetc (c, port); return '#'; @@ -313,7 +322,6 @@ flush_ws (SCM port, const char *eoferr) static SCM scm_read_expression (SCM port); static SCM scm_read_sharp (int chr, SCM port); -static SCM scm_get_hash_procedure (int c); static SCM recsexpr (SCM obj, long line, int column, SCM filename); @@ -844,16 +852,25 @@ scm_read_character (scm_t_wchar chr, SCM port) return SCM_MAKE_CHAR (scm_i_string_ref (charname, 0)); cp = scm_i_string_ref (charname, 0); + if (cp == SCM_CODEPOINT_DOTTED_CIRCLE && charname_len == 2) + return SCM_MAKE_CHAR (scm_i_string_ref (charname, 1)); + if (cp >= '0' && cp < '8') { /* Dirk:FIXME:: This type of character syntax is not R5RS * compliant. Further, it should be verified that the constant - * does only consist of octal digits. Finally, it should be - * checked whether the resulting fixnum is in the range of - * characters. */ + * does only consist of octal digits. */ SCM p = scm_string_to_number (charname, scm_from_uint (8)); if (SCM_I_INUMP (p)) - return SCM_MAKE_CHAR (SCM_I_INUM (p)); + { + scm_t_wchar c = SCM_I_INUM (p); + if (SCM_IS_UNICODE_CHAR (c)) + return SCM_MAKE_CHAR (c); + else + scm_i_input_error (FUNC_NAME, port, + "out-of-range octal character escape: ~a", + scm_list_1 (charname)); + } } /* The names of characters should never have non-Latin1 @@ -982,6 +999,45 @@ scm_read_scsh_block_comment (scm_t_wchar chr, SCM port) } static SCM +scm_read_r6rs_block_comment (scm_t_wchar chr, SCM port) +{ + /* Unlike SCSH-style block comments, SRFI-30/R6RS block comments may be + nested. So care must be taken. */ + int nesting_level = 1; + int opening_seen = 0, closing_seen = 0; + + while (nesting_level > 0) + { + int c = scm_getc (port); + + if (c == EOF) + scm_i_input_error (__FUNCTION__, port, + "unterminated `#| ... |#' comment", SCM_EOL); + + if (opening_seen) + { + if (c == '|') + nesting_level++; + opening_seen = 0; + } + else if (closing_seen) + { + if (c == '#') + nesting_level--; + closing_seen = 0; + } + else if (c == '|') + closing_seen = 1; + else if (c == '#') + opening_seen = 1; + else + opening_seen = closing_seen = 0; + } + + return SCM_UNSPECIFIED; +} + +static SCM scm_read_commented_expression (scm_t_wchar chr, SCM port) { scm_t_wchar c; @@ -1164,8 +1220,19 @@ scm_read_sharp (scm_t_wchar chr, SCM port) default: result = scm_read_sharp_extension (chr, port); if (scm_is_eq (result, SCM_UNSPECIFIED)) - scm_i_input_error (FUNC_NAME, port, "Unknown # object: ~S", - scm_list_1 (SCM_MAKE_CHAR (chr))); + { + /* To remain compatible with 1.8 and earlier, the following + characters have lower precedence than `read-hash-extend' + characters. */ + switch (chr) + { + case '|': + return scm_read_r6rs_block_comment (chr, port); + default: + scm_i_input_error (FUNC_NAME, port, "Unknown # object: ~S", + scm_list_1 (SCM_MAKE_CHAR (chr))); + } + } else return result; } @@ -1437,7 +1504,9 @@ scm_scan_for_encoding (SCM port) /* grab the next token */ i = 0; while (pos + i - header <= SCM_ENCODING_SEARCH_SIZE - && (isalnum(pos[i]) || pos[i] == '_' || pos[i] == '-' || pos[i] == '.')) + && pos + i - header < bytes_read + && (isalnum((int) pos[i]) || pos[i] == '_' || pos[i] == '-' + || pos[i] == '.')) i++; if (i == 0) diff --git a/libguile/root.h b/libguile/root.h index 676a7b44c..46b9be013 100644 --- a/libguile/root.h +++ b/libguile/root.h @@ -3,7 +3,7 @@ #ifndef SCM_ROOT_H #define SCM_ROOT_H -/* Copyright (C) 1996,1998,2000,2001, 2002, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1996,1998,2000,2001, 2002, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -34,13 +34,12 @@ #define scm_nullvect scm_sys_protects[2] #define scm_nullstr scm_sys_protects[3] #define scm_keyword_obarray scm_sys_protects[4] -#define scm_stand_in_procs scm_sys_protects[5] -#define scm_object_whash scm_sys_protects[6] -#define scm_asyncs scm_sys_protects[7] -#define scm_protects scm_sys_protects[8] -#define scm_properties_whash scm_sys_protects[9] -#define scm_source_whash scm_sys_protects[10] -#define SCM_NUM_PROTECTS 11 +#define scm_object_whash scm_sys_protects[5] +#define scm_asyncs scm_sys_protects[6] +#define scm_protects scm_sys_protects[7] +#define scm_properties_whash scm_sys_protects[8] +#define scm_source_whash scm_sys_protects[9] +#define SCM_NUM_PROTECTS 10 SCM_API SCM scm_sys_protects[]; diff --git a/libguile/scmsigs.c b/libguile/scmsigs.c index d9b36c574..f38d15c75 100644 --- a/libguile/scmsigs.c +++ b/libguile/scmsigs.c @@ -340,7 +340,10 @@ SCM_DEFINE (scm_sigaction_for_thread, "sigaction", 1, 3, 0, install_handler (csig, SCM_BOOL_F, SCM_BOOL_F); } else - SCM_OUT_OF_RANGE (2, handler); + { + SCM_CRITICAL_SECTION_END; + SCM_OUT_OF_RANGE (2, handler); + } } else if (scm_is_false (handler)) { diff --git a/libguile/smob.c b/libguile/smob.c index 86bb22f47..42a51fdff 100644 --- a/libguile/smob.c +++ b/libguile/smob.c @@ -38,7 +38,7 @@ #include "libguile/smob.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" #include <gc/gc_mark.h> @@ -97,6 +97,7 @@ scm_markcdr (SCM ptr) return SCM_CELL_OBJECT_1 (ptr); } + /* {Free} */ @@ -106,16 +107,7 @@ scm_free0 (SCM ptr SCM_UNUSED) return 0; } -size_t -scm_smob_free (SCM obj) -{ - long n = SCM_SMOBNUM (obj); - if (scm_smobs[n].size > 0) - scm_gc_free ((void *) SCM_CELL_WORD_1 (obj), - scm_smobs[n].size, SCM_SMOBNAME (n)); - return 0; -} - + /* {Print} */ @@ -307,11 +299,7 @@ scm_make_smob_type (char const *name, size_t size) scm_misc_error (FUNC_NAME, "maximum number of smobs exceeded", SCM_EOL); scm_smobs[new_smob].name = name; - if (size != 0) - { - scm_smobs[new_smob].size = size; - scm_smobs[new_smob].free = scm_smob_free; - } + scm_smobs[new_smob].size = size; /* Make a class object if Goops is present. */ if (SCM_UNPACK (scm_smob_class[0]) != 0) diff --git a/libguile/smob.h b/libguile/smob.h index d435bacb8..ee0e53e64 100644 --- a/libguile/smob.h +++ b/libguile/smob.h @@ -26,7 +26,7 @@ #include "libguile/__scm.h" #include "libguile/print.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" @@ -167,7 +167,6 @@ SCM_API void scm_i_finalize_smob (GC_PTR obj, GC_PTR data); SCM_API SCM scm_mark0 (SCM ptr); SCM_API SCM scm_markcdr (SCM ptr); SCM_API size_t scm_free0 (SCM ptr); -SCM_API size_t scm_smob_free (SCM obj); SCM_API int scm_smob_print (SCM exp, SCM port, scm_print_state *pstate); /* The following set of functions is the standard way to create new diff --git a/libguile/socket.c b/libguile/socket.c index 3a81ed9d0..ea2ba5ccb 100644 --- a/libguile/socket.c +++ b/libguile/socket.c @@ -349,7 +349,7 @@ scm_to_ipv6 (scm_t_uint8 dst[16], SCM src) scm_remember_upto_here_1 (src); } else - scm_wrong_type_arg (NULL, 0, src); + scm_wrong_type_arg_msg ("scm_to_ipv6", 0, src, "integer"); } #ifdef HAVE_INET_PTON @@ -399,8 +399,8 @@ SCM_DEFINE (scm_inet_ntop, "inet-ntop", 2, 0, 0, "@var{family} can be @code{AF_INET} or @code{AF_INET6}. E.g.,\n\n" "@lisp\n" "(inet-ntop AF_INET 2130706433) @result{} \"127.0.0.1\"\n" - "(inet-ntop AF_INET6 (- (expt 2 128) 1)) @result{}\n" - "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\n" + "(inet-ntop AF_INET6 (- (expt 2 128) 1))\n" + " @result{} \"ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff\"\n" "@end lisp") #define FUNC_NAME s_scm_inet_ntop { @@ -1169,7 +1169,8 @@ scm_to_sockaddr (SCM address, size_t *address_size) { struct sockaddr_in6 c_inet6; - scm_to_ipv6 (c_inet6.sin6_addr.s6_addr, address); + scm_to_ipv6 (c_inet6.sin6_addr.s6_addr, + SCM_SIMPLE_VECTOR_REF (address, 1)); c_inet6.sin6_port = htons (scm_to_ushort (SCM_SIMPLE_VECTOR_REF (address, 2))); c_inet6.sin6_flowinfo = diff --git a/libguile/srcprop.h b/libguile/srcprop.h index 89063bed4..ca8818ac4 100644 --- a/libguile/srcprop.h +++ b/libguile/srcprop.h @@ -3,7 +3,7 @@ #ifndef SCM_SRCPROP_H #define SCM_SRCPROP_H -/* Copyright (C) 1995,1996,2000,2001, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,2000,2001, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -35,12 +35,15 @@ #define scm_whash_handle SCM -#define scm_whash_get_handle(whash, key) scm_hash_fn_get_handle (whash, key, scm_ihashq, scm_sloppy_assq, 0) +#define scm_whash_get_handle(whash, key) \ + scm_hashq_get_handle ((whash), (key)) #define SCM_WHASHFOUNDP(h) (scm_is_true (h)) #define SCM_WHASHREF(whash, handle) SCM_CDR (handle) #define SCM_WHASHSET(whash, handle, obj) SCM_SETCDR (handle, obj) -#define scm_whash_create_handle(whash, key) scm_hash_fn_create_handle_x (whash, key, SCM_UNSPECIFIED, scm_ihashq, scm_sloppy_assq, 0) -#define scm_whash_lookup(whash, obj) scm_hash_fn_ref (whash, obj, SCM_BOOL_F, scm_ihashq, scm_sloppy_assq, 0) +#define scm_whash_create_handle(whash, key) \ + scm_hashq_create_handle_x ((whash), (key), SCM_UNSPECIFIED) +#define scm_whash_lookup(whash, obj) \ + scm_hashq_ref ((whash), (obj), SCM_BOOL_F) #define scm_whash_insert(whash, key, obj) \ do { \ register SCM w = (whash); \ diff --git a/libguile/srfi-14.c b/libguile/srfi-14.c index 7c0013193..38ef32049 100644 --- a/libguile/srfi-14.c +++ b/libguile/srfi-14.c @@ -1,6 +1,6 @@ /* srfi-14.c --- SRFI-14 procedures for Guile * - * Copyright (C) 2001, 2004, 2006, 2007 Free Software Foundation, Inc. + * Copyright (C) 2001, 2004, 2006, 2007, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -29,10 +29,23 @@ #include "libguile.h" #include "libguile/srfi-14.h" #include "libguile/strings.h" +#include "libguile/chars.h" /* Include the pre-computed standard charset data. */ #include "libguile/srfi-14.i.c" +scm_t_char_range cs_full_ranges[] = { + {0x0000, SCM_CODEPOINT_SURROGATE_START - 1} + , + {SCM_CODEPOINT_SURROGATE_END + 1, SCM_CODEPOINT_MAX} +}; + +scm_t_char_set cs_full = { + 2, + cs_full_ranges +}; + + #define SCM_CHARSET_DATA(charset) ((scm_t_char_set *) SCM_SMOB_DATA (charset)) #define SCM_CHARSET_SET(cs, idx) \ @@ -85,18 +98,11 @@ scm_i_charset_set (scm_t_char_set *cs, scm_t_wchar n) /* This char is one below the current range. */ if (i > 0 && cs->ranges[i - 1].hi + 1 == n) { - /* It is also one above the previous range, so combine them. */ - cs->ranges[i - 1].hi = cs->ranges[i].hi; - if (i < len - 1) - memmove (cs->ranges + i, cs->ranges + (i + 1), - sizeof (scm_t_char_range) * (len - i - 1)); - cs->ranges = scm_gc_realloc (cs->ranges, - sizeof (scm_t_char_range) * len, - sizeof (scm_t_char_range) * (len - - 1), - "character-set"); - cs->len = len - 1; - return; + /* It is also one above the previous range. */ + /* This is an impossible condition: in the previous + iteration, the test for 'one above the current range' + should already have inserted the character here. */ + abort (); } else { @@ -167,6 +173,103 @@ scm_i_charset_set (scm_t_char_set *cs, scm_t_wchar n) return; } +/* Put LO to HI inclusive into charset CS. */ +static void +scm_i_charset_set_range (scm_t_char_set *cs, scm_t_wchar lo, scm_t_wchar hi) +{ + size_t i; + + i = 0; + while (i < cs->len) + { + /* Already in this range */ + if (cs->ranges[i].lo <= lo && cs->ranges[i].hi >= hi) + return; + + /* cur: +---+ + new: +---+ + */ + if (cs->ranges[i].lo - 1 > hi) + { + /* Add a new range below the current one. */ + cs->ranges = scm_gc_realloc (cs->ranges, + sizeof (scm_t_char_range) * cs->len, + sizeof (scm_t_char_range) * (cs->len + 1), + "character-set"); + memmove (cs->ranges + (i + 1), cs->ranges + i, + sizeof (scm_t_char_range) * (cs->len - i)); + cs->ranges[i].lo = lo; + cs->ranges[i].hi = hi; + cs->len += 1; + return; + } + + /* cur: +---+ or +---+ or +---+ + new: +---+ +---+ +---+ + */ + if (cs->ranges[i].lo > lo + && (cs->ranges[i].lo - 1 <= hi && cs->ranges[i].hi >= hi)) + { + cs->ranges[i].lo = lo; + return; + } + + /* cur: +---+ or +---+ or +---+ + new: +---+ +---+ +---+ + */ + else if (cs->ranges[i].hi + 1 >= lo && cs->ranges[i].hi < hi) + { + if (cs->ranges[i].lo > lo) + cs->ranges[i].lo = lo; + if (cs->ranges[i].hi < hi) + cs->ranges[i].hi = hi; + while (i < cs->len - 1) + { + /* cur: --+ +---+ + new: -----+ + */ + if (cs->ranges[i + 1].lo - 1 > hi) + break; + + /* cur: --+ +---+ or --+ +---+ or --+ +--+ + new: -----+ ------+ ---------+ + */ + /* Combine this range with the previous one. */ + if (cs->ranges[i + 1].hi > hi) + cs->ranges[i].hi = cs->ranges[i + 1].hi; + if (i + 1 < cs->len) + memmove (cs->ranges + i + 1, cs->ranges + i + 2, + sizeof (scm_t_char_range) * (cs->len - i - 2)); + cs->ranges = scm_gc_realloc (cs->ranges, + sizeof (scm_t_char_range) * cs->len, + sizeof (scm_t_char_range) * (cs->len - 1), + "character-set"); + cs->len -= 1; + } + return; + } + i ++; + } + + /* This is a new range above all previous ranges. */ + if (cs->len == 0) + { + cs->ranges = scm_gc_malloc (sizeof (scm_t_char_range), "character-set"); + } + else + { + cs->ranges = scm_gc_realloc (cs->ranges, + sizeof (scm_t_char_range) * cs->len, + sizeof (scm_t_char_range) * (cs->len + 1), + "character-set"); + } + cs->len += 1; + cs->ranges[cs->len - 1].lo = lo; + cs->ranges[cs->len - 1].hi = hi; + + return; +} + /* If N is in charset CS, remove it. */ void scm_i_charset_unset (scm_t_char_set *cs, scm_t_wchar n) @@ -301,7 +404,7 @@ static void charsets_union (scm_t_char_set *a, scm_t_char_set *b) { size_t i = 0; - scm_t_wchar blo, bhi, n; + scm_t_wchar blo, bhi; if (b->len == 0) return; @@ -315,13 +418,11 @@ charsets_union (scm_t_char_set *a, scm_t_char_set *b) return; } - /* This needs optimization. */ while (i < b->len) { blo = b->ranges[i].lo; bhi = b->ranges[i].hi; - for (n = blo; n <= bhi; n++) - scm_i_charset_set (a, n); + scm_i_charset_set_range (a, blo, bhi); i++; } @@ -373,22 +474,35 @@ charsets_intersection (scm_t_char_set *a, scm_t_char_set *b) return; } +#define SCM_ADD_RANGE(low, high) \ + do { \ + p->ranges[idx].lo = (low); \ + p->ranges[idx++].hi = (high); \ + } while (0) +#define SCM_ADD_RANGE_SKIP_SURROGATES(low, high) \ + do { \ + p->ranges[idx].lo = (low); \ + p->ranges[idx++].hi = SCM_CODEPOINT_SURROGATE_START - 1; \ + p->ranges[idx].lo = SCM_CODEPOINT_SURROGATE_END + 1; \ + p->ranges[idx++].hi = (high); \ + } while (0) + + + /* Make P the compelement of Q. */ static void charsets_complement (scm_t_char_set *p, scm_t_char_set *q) { int k, idx; + idx = 0; if (q->len == 0) { /* Fill with all valid codepoints. */ p->len = 2; p->ranges = scm_gc_malloc (sizeof (scm_t_char_range) * 2, "character-set"); - p->ranges[0].lo = 0; - p->ranges[0].hi = 0xd7ff; - p->ranges[1].lo = 0xe000; - p->ranges[1].hi = SCM_CODEPOINT_MAX; + SCM_ADD_RANGE_SKIP_SURROGATES (0, SCM_CODEPOINT_MAX); return; } @@ -396,33 +510,42 @@ charsets_complement (scm_t_char_set *p, scm_t_char_set *q) scm_gc_free (p->ranges, sizeof (scm_t_char_set) * p->len, "character-set"); + /* Count the number of ranges needed for the output. */ p->len = 0; if (q->ranges[0].lo > 0) p->len++; if (q->ranges[q->len - 1].hi < SCM_CODEPOINT_MAX) p->len++; - p->len += q->len - 1; + p->len += q->len; p->ranges = (scm_t_char_range *) scm_gc_malloc (sizeof (scm_t_char_range) * p->len, "character-set"); - idx = 0; if (q->ranges[0].lo > 0) { - p->ranges[idx].lo = 0; - p->ranges[idx++].hi = q->ranges[0].lo - 1; + if (q->ranges[0].lo > SCM_CODEPOINT_SURROGATE_END) + SCM_ADD_RANGE_SKIP_SURROGATES (0, q->ranges[0].lo - 1); + else + SCM_ADD_RANGE (0, q->ranges[0].lo - 1); } for (k = 1; k < q->len; k++) { - p->ranges[idx].lo = q->ranges[k - 1].hi + 1; - p->ranges[idx++].hi = q->ranges[k].lo - 1; + if (q->ranges[k - 1].hi < SCM_CODEPOINT_SURROGATE_START + && q->ranges[k].lo - 1 > SCM_CODEPOINT_SURROGATE_END) + SCM_ADD_RANGE_SKIP_SURROGATES (q->ranges[k - 1].hi + 1, q->ranges[k].lo - 1); + else + SCM_ADD_RANGE (q->ranges[k - 1].hi + 1, q->ranges[k].lo - 1); } if (q->ranges[q->len - 1].hi < SCM_CODEPOINT_MAX) { - p->ranges[idx].lo = q->ranges[q->len - 1].hi + 1; - p->ranges[idx].hi = SCM_CODEPOINT_MAX; + if (q->ranges[q->len - 1].hi < SCM_CODEPOINT_SURROGATE_START) + SCM_ADD_RANGE_SKIP_SURROGATES (q->ranges[q->len - 1].hi + 1, SCM_CODEPOINT_MAX); + else + SCM_ADD_RANGE (q->ranges[q->len - 1].hi + 1, SCM_CODEPOINT_MAX); } return; } +#undef SCM_ADD_RANGE +#undef SCM_ADD_RANGE_SKIP_SURROGATES /* Replace A with elements only found in one of A or B. */ static void @@ -1161,7 +1284,7 @@ SCM_DEFINE (scm_char_set_filter_x, "char-set-filter!", 3, 0, 0, for (k = 0; k < p->len; k++) for (n = p->ranges[k].lo; n <= p->ranges[k].hi; n++) { - SCM res = scm_call_1 (pred, SCM_MAKE_CHAR (k)); + SCM res = scm_call_1 (pred, SCM_MAKE_CHAR (n)); if (scm_is_true (res)) SCM_CHARSET_SET (base_cs, n); @@ -1171,27 +1294,18 @@ SCM_DEFINE (scm_char_set_filter_x, "char-set-filter!", 3, 0, 0, #undef FUNC_NAME -SCM_DEFINE (scm_ucs_range_to_char_set, "ucs-range->char-set", 2, 2, 0, - (SCM lower, SCM upper, SCM error, SCM base_cs), - "Return a character set containing all characters whose\n" - "character codes lie in the half-open range\n" - "[@var{lower},@var{upper}).\n" - "\n" - "If @var{error} is a true value, an error is signalled if the\n" - "specified range contains characters which are not contained in\n" - "the implemented character range. If @var{error} is @code{#f},\n" - "these characters are silently left out of the resultung\n" - "character set.\n" - "\n" - "The characters in @var{base_cs} are added to the result, if\n" - "given.") -#define FUNC_NAME s_scm_ucs_range_to_char_set +/* Return a character set containing all the characters from [LOWER,UPPER), + giving range errors if ERROR, adding chars from BASE_CS, and recycling + BASE_CS if REUSE is true. */ +static SCM +scm_i_ucs_range_to_char_set (const char *FUNC_NAME, SCM lower, SCM upper, + SCM error, SCM base_cs, int reuse) { SCM cs; size_t clower, cupper; clower = scm_to_size_t (lower); - cupper = scm_to_size_t (upper); + cupper = scm_to_size_t (upper) - 1; SCM_ASSERT_RANGE (2, upper, cupper >= clower); if (!SCM_UNBNDP (error)) { @@ -1199,28 +1313,66 @@ SCM_DEFINE (scm_ucs_range_to_char_set, "ucs-range->char-set", 2, 2, 0, { SCM_ASSERT_RANGE (1, lower, SCM_IS_UNICODE_CHAR (clower)); SCM_ASSERT_RANGE (2, upper, SCM_IS_UNICODE_CHAR (cupper)); + if (clower < SCM_CODEPOINT_SURROGATE_START + && cupper > SCM_CODEPOINT_SURROGATE_END) + scm_error(scm_out_of_range_key, + FUNC_NAME, "invalid range - contains surrogate characters: ~S to ~S", + scm_list_2 (lower, upper), scm_list_1 (upper)); } } - if (clower > 0x10FFFF) - clower = 0x10FFFF; - if (cupper > 0x10FFFF) - cupper = 0x10FFFF; + if (SCM_UNBNDP (base_cs)) cs = make_char_set (FUNC_NAME); else { - SCM_VALIDATE_SMOB (4, base_cs, charset); - cs = scm_char_set_copy (base_cs); + SCM_VALIDATE_SMOB (3, base_cs, charset); + if (reuse) + cs = base_cs; + else + cs = scm_char_set_copy (base_cs); } - /* It not be difficult to write a more optimized version of the - following. */ - while (clower < cupper) + + if ((clower >= SCM_CODEPOINT_SURROGATE_START && clower <= SCM_CODEPOINT_SURROGATE_END) + && (cupper >= SCM_CODEPOINT_SURROGATE_START && cupper <= SCM_CODEPOINT_SURROGATE_END)) + return cs; + + if (clower > SCM_CODEPOINT_MAX) + clower = SCM_CODEPOINT_MAX; + if (clower >= SCM_CODEPOINT_SURROGATE_START && clower <= SCM_CODEPOINT_SURROGATE_END) + clower = SCM_CODEPOINT_SURROGATE_END + 1; + if (cupper > SCM_CODEPOINT_MAX) + cupper = SCM_CODEPOINT_MAX; + if (cupper >= SCM_CODEPOINT_SURROGATE_START && cupper <= SCM_CODEPOINT_SURROGATE_END) + cupper = SCM_CODEPOINT_SURROGATE_START - 1; + if (clower < SCM_CODEPOINT_SURROGATE_START && cupper > SCM_CODEPOINT_SURROGATE_END) { - SCM_CHARSET_SET (cs, clower); - clower++; + scm_i_charset_set_range (SCM_CHARSET_DATA (cs), clower, SCM_CODEPOINT_SURROGATE_START - 1); + scm_i_charset_set_range (SCM_CHARSET_DATA (cs), SCM_CODEPOINT_SURROGATE_END + 1, cupper); } + else + scm_i_charset_set_range (SCM_CHARSET_DATA (cs), clower, cupper); return cs; } + +SCM_DEFINE (scm_ucs_range_to_char_set, "ucs-range->char-set", 2, 2, 0, + (SCM lower, SCM upper, SCM error, SCM base_cs), + "Return a character set containing all characters whose\n" + "character codes lie in the half-open range\n" + "[@var{lower},@var{upper}).\n" + "\n" + "If @var{error} is a true value, an error is signalled if the\n" + "specified range contains characters which are not valid\n" + "Unicode code points. If @var{error} is @code{#f},\n" + "these characters are silently left out of the resultung\n" + "character set.\n" + "\n" + "The characters in @var{base_cs} are added to the result, if\n" + "given.") +#define FUNC_NAME s_scm_ucs_range_to_char_set +{ + return scm_i_ucs_range_to_char_set (FUNC_NAME, lower, upper, + error, base_cs, 0); +} #undef FUNC_NAME @@ -1240,28 +1392,9 @@ SCM_DEFINE (scm_ucs_range_to_char_set_x, "ucs-range->char-set!", 4, 0, 0, "returned.") #define FUNC_NAME s_scm_ucs_range_to_char_set_x { - size_t clower, cupper; - - clower = scm_to_size_t (lower); - cupper = scm_to_size_t (upper); - SCM_ASSERT_RANGE (2, upper, cupper >= clower); - if (scm_is_true (error)) - { - SCM_ASSERT_RANGE (1, lower, SCM_IS_UNICODE_CHAR (clower)); - SCM_ASSERT_RANGE (2, upper, SCM_IS_UNICODE_CHAR (cupper)); - } - if (clower > SCM_CODEPOINT_MAX) - clower = SCM_CODEPOINT_MAX; - if (cupper > SCM_CODEPOINT_MAX) - cupper = SCM_CODEPOINT_MAX; - - while (clower < cupper) - { - if (SCM_IS_UNICODE_CHAR (clower)) - SCM_CHARSET_SET (base_cs, clower); - clower++; - } - return base_cs; + SCM_VALIDATE_SMOB (4, base_cs, charset); + return scm_i_ucs_range_to_char_set (FUNC_NAME, lower, upper, + error, base_cs, 1); } #undef FUNC_NAME @@ -1455,7 +1588,9 @@ SCM_DEFINE (scm_char_set_any, "char-set-any", 2, 0, 0, SCM_VALIDATE_PROC (1, pred); SCM_VALIDATE_SMOB (2, cs, charset); - cs_data = (scm_t_char_set *) cs; + cs_data = SCM_CHARSET_DATA (cs); + if (cs_data->len == 0) + return SCM_BOOL_T; for (k = 0; k < cs_data->len; k++) for (n = cs_data->ranges[k].lo; n <= cs_data->ranges[k].hi; n++) @@ -1819,7 +1954,8 @@ SCM_DEFINE (scm_char_set_xor_x, "char-set-xor!", 1, 0, 1, (char-set-xor a a a) -> char set #\a (char-set-xor! a a a) -> char set #\a */ - return scm_char_set_xor (scm_cons (cs1, rest)); + cs1 = scm_char_set_xor (scm_cons (cs1, rest)); + return cs1; } #undef FUNC_NAME @@ -1862,6 +1998,7 @@ SCM scm_char_set_hex_digit; SCM scm_char_set_blank; SCM scm_char_set_ascii; SCM scm_char_set_empty; +SCM scm_char_set_designated; SCM scm_char_set_full; @@ -1876,31 +2013,59 @@ define_charset (const char *name, const scm_t_char_set *p) return scm_permanent_object (cs); } -#ifdef SCM_CHARSET_DEBUG -SCM_DEFINE (scm_debug_char_set, "debug-char-set", 1, 0, 0, - (SCM charset), - "Print out the internal C structure of @var{charset}.\n") -#define FUNC_NAME s_scm_debug_char_set +SCM_DEFINE (scm_sys_char_set_dump, "%char-set-dump", 1, 0, 0, (SCM charset), + "Returns an association list containing debugging information\n" + "for @var{charset}. The association list has the following entries." + "@table @code\n" + "@item char-set\n" + "The char-set itself.\n" + "@item len\n" + "The number of character ranges the char-set contains\n" + "@item ranges\n" + "A list of lists where each sublist a range of code points\n" + "and their associated characters" + "@end table") +#define FUNC_NAME s_scm_sys_char_set_dump { - int i; - scm_t_char_set *cs = SCM_CHARSET_DATA (charset); - fprintf (stderr, "cs %p\n", cs); - fprintf (stderr, "len %d\n", cs->len); - fprintf (stderr, "arr %p\n", cs->ranges); + SCM e1, e2, e3; + SCM ranges = SCM_EOL, elt; + size_t i; + scm_t_char_set *cs; + char codepoint_string_lo[9], codepoint_string_hi[9]; + + SCM_VALIDATE_SMOB (1, charset, charset); + cs = SCM_CHARSET_DATA (charset); + + e1 = scm_cons (scm_from_locale_symbol ("char-set"), + charset); + e2 = scm_cons (scm_from_locale_symbol ("n"), + scm_from_size_t (cs->len)); + for (i = 0; i < cs->len; i++) { - if (cs->ranges[i].lo == cs->ranges[i].hi) - fprintf (stderr, "%04x\n", cs->ranges[i].lo); + if (cs->ranges[i].lo > 0xFFFF) + sprintf (codepoint_string_lo, "U+%06x", cs->ranges[i].lo); else - fprintf (stderr, "%04x..%04x\t[%d]\n", - cs->ranges[i].lo, - cs->ranges[i].hi, cs->ranges[i].hi - cs->ranges[i].lo + 1); + sprintf (codepoint_string_lo, "U+%04x", cs->ranges[i].lo); + if (cs->ranges[i].hi > 0xFFFF) + sprintf (codepoint_string_hi, "U+%06x", cs->ranges[i].hi); + else + sprintf (codepoint_string_hi, "U+%04x", cs->ranges[i].hi); + + elt = scm_list_4 (SCM_MAKE_CHAR (cs->ranges[i].lo), + SCM_MAKE_CHAR (cs->ranges[i].hi), + scm_from_locale_string (codepoint_string_lo), + scm_from_locale_string (codepoint_string_hi)); + ranges = scm_append (scm_list_2 (ranges, + scm_list_1 (elt))); } - printf ("\n"); - return SCM_UNSPECIFIED; + e3 = scm_cons (scm_from_locale_symbol ("ranges"), + ranges); + + return scm_list_3 (e1, e2, e3); } #undef FUNC_NAME -#endif /* SCM_CHARSET_DEBUG */ + @@ -1937,6 +2102,7 @@ scm_init_srfi_14 (void) scm_char_set_blank = define_charset ("char-set:blank", &cs_blank); scm_char_set_ascii = define_charset ("char-set:ascii", &cs_ascii); scm_char_set_empty = define_charset ("char-set:empty", &cs_empty); + scm_char_set_designated = define_charset ("char-set:designated", &cs_designated); scm_char_set_full = define_charset ("char-set:full", &cs_full); #include "libguile/srfi-14.x" diff --git a/libguile/srfi-14.h b/libguile/srfi-14.h index 1b9c29518..4b1a4b298 100644 --- a/libguile/srfi-14.h +++ b/libguile/srfi-14.h @@ -100,9 +100,7 @@ SCM_API SCM scm_char_set_intersection_x (SCM cs1, SCM rest); SCM_API SCM scm_char_set_difference_x (SCM cs1, SCM rest); SCM_API SCM scm_char_set_xor_x (SCM cs1, SCM rest); SCM_API SCM scm_char_set_diff_plus_intersection_x (SCM cs1, SCM cs2, SCM rest); -#if SCM_CHARSET_DEBUG -SCM_API SCM scm_debug_char_set (SCM cs); -#endif /* SCM_CHARSET_DEBUG */ +SCM_API SCM scm_sys_char_set_dump (SCM charset); SCM_API SCM scm_char_set_lower_case; SCM_API SCM scm_char_set_upper_case; diff --git a/libguile/srfi-14.i.c b/libguile/srfi-14.i.c index 5ef21f333..fd537daf3 100644 --- a/libguile/srfi-14.i.c +++ b/libguile/srfi-14.i.c @@ -2,7 +2,8 @@ /* This file is #include'd by srfi-14.c. */ -/* This file was generated from http://unicode.org/Public/UNIDATA/UnicodeData.txt +/* This file was generated from + http://unicode.org/Public/UNIDATA/UnicodeData.txt with the unidata_to_charset.pl script. */ scm_t_char_range cs_lower_case_ranges[] = { @@ -6252,7 +6253,7 @@ scm_t_char_set cs_empty = { cs_empty_ranges }; -scm_t_char_range cs_full_ranges[] = { +scm_t_char_range cs_designated_ranges[] = { {0x0000, 0x0377} , {0x037a, 0x037e} @@ -6925,7 +6926,7 @@ scm_t_char_range cs_full_ranges[] = { , {0xac00, 0xd7a3} , - {0xd800, 0xfa2d} + {0xe000, 0xfa2d} , {0xfa30, 0xfa6a} , @@ -7144,7 +7145,7 @@ scm_t_char_range cs_full_ranges[] = { {0x100000, 0x10fffd} }; -scm_t_char_set cs_full = { +scm_t_char_set cs_designated = { 445, - cs_full_ranges + cs_designated_ranges }; diff --git a/libguile/srfi-4.c b/libguile/srfi-4.c index 302414364..af8eaa3c3 100644 --- a/libguile/srfi-4.c +++ b/libguile/srfi-4.c @@ -28,7 +28,7 @@ #include "libguile/_scm.h" #include "libguile/__scm.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" #include "libguile/srfi-4.h" #include "libguile/bitvectors.h" #include "libguile/bytevectors.h" diff --git a/libguile/srfi-4.h b/libguile/srfi-4.h index 3a45fd9e0..48001abe3 100644 --- a/libguile/srfi-4.h +++ b/libguile/srfi-4.h @@ -286,7 +286,7 @@ SCM_INTERNAL scm_i_t_array_set scm_i_uniform_vector_set_proc (SCM uvec); interface. */ -SCM_API size_t scm_uniform_element_size (SCM obj); +SCM_DEPRECATED size_t scm_uniform_element_size (SCM obj); #endif diff --git a/libguile/stackchk.h b/libguile/stackchk.h index 6aa0fec18..ed149067b 100644 --- a/libguile/stackchk.h +++ b/libguile/stackchk.h @@ -3,7 +3,7 @@ #ifndef SCM_STACKCHK_H #define SCM_STACKCHK_H -/* Copyright (C) 1995,1996,1998,2000, 2003, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1998,2000, 2003, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -34,7 +34,7 @@ */ #define SCM_STACK_CHECKING_P SCM_STACK_LIMIT -#ifdef STACK_CHECKING +#if defined BUILDING_LIBGUILE && defined STACK_CHECKING # if SCM_STACK_GROWS_UP # define SCM_STACK_OVERFLOW_P(s)\ (SCM_STACK_PTR (s) \ @@ -52,7 +52,7 @@ } #else # define SCM_CHECK_STACK /**/ -#endif /* STACK_CHECKING */ +#endif SCM_API int scm_stack_checking_enabled_p; diff --git a/libguile/stacks.c b/libguile/stacks.c index 45566cafa..a70186371 100644 --- a/libguile/stacks.c +++ b/libguile/stacks.c @@ -143,6 +143,11 @@ stack_depth (scm_t_debug_frame *dframe, scm_t_ptrdiff offset, SCM vmframe, { scm_t_debug_info *info = RELOC_INFO (dframe->info, offset); scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset); + /* If current frame is a macro during expansion, we should + skip the previously recorded macro transformer + application frame. */ + if (SCM_MACROEXPP (*dframe) && n > 0) + --n; n += (info - vect) / 2 + 1; /* Data in the apply part of an eval info frame comes from previous stack frame if the scm_t_debug_info vector is overflowed. */ @@ -151,7 +156,7 @@ stack_depth (scm_t_debug_frame *dframe, scm_t_ptrdiff offset, SCM vmframe, && !SCM_UNBNDP (info[1].a.proc)) ++n; } - else if (SCM_APPLYFRAMEP (*dframe)) + else { scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset); if (SCM_PROGRAM_P (vect[0].a.proc)) @@ -177,8 +182,6 @@ stack_depth (scm_t_debug_frame *dframe, scm_t_ptrdiff offset, SCM vmframe, else ++n; /* increment for non-program apply frame */ } - else - ++n; } if (dframe && SCM_VOIDFRAMEP (*dframe)) *id = RELOC_INFO(dframe->vect, offset)[0].id; @@ -282,6 +285,7 @@ read_frames (scm_t_debug_frame *dframe, scm_t_ptrdiff offset, { *(iframe - 1) = *iframe; --iframe; + ++n; } info = RELOC_INFO (dframe->info, offset); vect = RELOC_INFO (dframe->vect, offset); diff --git a/libguile/strings.c b/libguile/strings.c index 03ead8138..21295addf 100644 --- a/libguile/strings.c +++ b/libguile/strings.c @@ -1379,7 +1379,7 @@ scm_is_string (SCM obj) return IS_STRING (obj); } -static SCM +SCM scm_from_stringn (const char *str, size_t len, const char *encoding, scm_t_string_failed_conversion_handler handler) { @@ -1388,6 +1388,9 @@ scm_from_stringn (const char *str, size_t len, const char *encoding, int wide = 0; SCM res; + if (len == 0) + return scm_nullstr; + if (encoding == NULL) { /* If encoding is null, use Latin-1. */ @@ -1575,7 +1578,7 @@ unistring_escapes_to_guile_escapes (char **bufp, size_t *lenp) } char * -scm_to_locale_stringn (SCM str, size_t * lenp) +scm_to_locale_stringn (SCM str, size_t *lenp) { SCM outport; scm_t_port *pt; @@ -1682,6 +1685,8 @@ scm_to_stringn (SCM str, size_t *lenp, const char *encoding, scm_list_2 (scm_from_locale_string (enc), str)); } + if (handler == SCM_FAILED_CONVERSION_ESCAPE_SEQUENCE) + unistring_escapes_to_guile_escapes (&buf, &len); } if (lenp) *lenp = len; diff --git a/libguile/strings.h b/libguile/strings.h index 910d51ee6..4e5f70098 100644 --- a/libguile/strings.h +++ b/libguile/strings.h @@ -111,6 +111,10 @@ SCM_API SCM scm_substring_shared (SCM str, SCM start, SCM end); SCM_API SCM scm_substring_copy (SCM str, SCM start, SCM end); SCM_API SCM scm_string_append (SCM args); +SCM_API SCM scm_from_stringn (const char *str, size_t len, + const char *encoding, + scm_t_string_failed_conversion_handler + handler); SCM_API SCM scm_c_make_string (size_t len, SCM chr); SCM_API size_t scm_c_string_length (SCM str); SCM_API size_t scm_c_symbol_length (SCM sym); @@ -202,9 +206,9 @@ SCM_API SCM scm_sys_stringbuf_hist (void); #if SCM_ENABLE_DEPRECATED -SCM_API int scm_i_deprecated_stringp (SCM obj); -SCM_API char *scm_i_deprecated_string_chars (SCM str); -SCM_API size_t scm_i_deprecated_string_length (SCM str); +SCM_DEPRECATED int scm_i_deprecated_stringp (SCM obj); +SCM_DEPRECATED char *scm_i_deprecated_string_chars (SCM str); +SCM_DEPRECATED size_t scm_i_deprecated_string_length (SCM str); #define SCM_STRINGP(x) scm_i_deprecated_stringp(x) #define SCM_STRING_CHARS(x) scm_i_deprecated_string_chars(x) diff --git a/libguile/strports.c b/libguile/strports.c index 490a15f8b..95e93c9ef 100644 --- a/libguile/strports.c +++ b/libguile/strports.c @@ -290,7 +290,7 @@ st_truncate (SCM port, scm_t_off length) } SCM -scm_i_mkstrport (SCM pos, const char *locale_str, size_t str_len, long modes, const char *caller) +scm_i_mkstrport (SCM pos, const char *utf8_str, size_t str_len, long modes, const char *caller) { SCM z, str; scm_t_port *pt; @@ -301,11 +301,11 @@ scm_i_mkstrport (SCM pos, const char *locale_str, size_t str_len, long modes, co to a locale representation for storage. But, since string ports rely on string functionality for their memory management, we need to create a new string that has the 8-bit locale representation - of the underlying string. This violates the guideline that the - internal encoding of characters in strings is in unicode - codepoints. */ + of the underlying string. + + locale_str is already in the locale of the port. */ str = scm_i_make_string (str_len, &buf); - memcpy (buf, locale_str, str_len); + memcpy (buf, utf8_str, str_len); c_pos = scm_to_unsigned_integer (pos, 0, str_len); @@ -323,12 +323,14 @@ scm_i_mkstrport (SCM pos, const char *locale_str, size_t str_len, long modes, co pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size; pt->rw_random = 1; - scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex); /* ensure write_pos is writable. */ if ((modes & SCM_WRTNG) && pt->write_pos == pt->write_end) st_flush (z); + + scm_i_set_port_encoding_x (z, "UTF-8"); + scm_i_set_conversion_strategy_x (z, SCM_FAILED_CONVERSION_ERROR); return z; } @@ -348,13 +350,18 @@ scm_mkstrport (SCM pos, SCM str, long modes, const char *caller) of the underlying string. This violates the guideline that the internal encoding of characters in strings is in unicode codepoints. */ - buf = scm_to_locale_stringn (str, &str_len); + + /* String ports are are always initialized with "UTF-8" as their + encoding. */ + buf = scm_to_stringn (str, &str_len, "UTF-8", SCM_FAILED_CONVERSION_ERROR); z = scm_i_mkstrport (pos, buf, str_len, modes, caller); free (buf); return z; } -/* create a new string from a string port's buffer. */ +/* Create a new string from a string port's buffer, converting from + the port's 8-bit locale-specific representation to the standard + string representation. */ SCM scm_strport_to_string (SCM port) { scm_t_port *pt = SCM_PTAB_ENTRY (port); @@ -363,27 +370,20 @@ SCM scm_strport_to_string (SCM port) if (pt->rw_active == SCM_PORT_WRITE) st_flush (port); - str = scm_from_locale_stringn ((char *)pt->read_buf, pt->read_buf_size); - scm_remember_upto_here_1 (port); - return str; -} - -/* Create a vector containing the locale representation of the string in the - port's buffer. */ -SCM scm_strport_to_locale_u8vector (SCM port) -{ - scm_t_port *pt = SCM_PTAB_ENTRY (port); - SCM vec; - char *buf; - - if (pt->rw_active == SCM_PORT_WRITE) - st_flush (port); + if (pt->read_buf_size == 0) + return scm_nullstr; - buf = scm_malloc (pt->read_buf_size); - memcpy (buf, pt->read_buf, pt->read_buf_size); - vec = scm_take_u8vector ((unsigned char *) buf, pt->read_buf_size); + if (pt->encoding == NULL) + { + char *buf; + str = scm_i_make_string (pt->read_buf_size, &buf); + memcpy (buf, pt->read_buf, pt->read_buf_size); + } + else + str = scm_from_stringn ((char *)pt->read_buf, pt->read_buf_size, + pt->encoding, pt->ilseq_handler); scm_remember_upto_here_1 (port); - return vec; + return str; } SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0, @@ -410,25 +410,6 @@ SCM_DEFINE (scm_object_to_string, "object->string", 1, 1, 0, } #undef FUNC_NAME -SCM_DEFINE (scm_call_with_output_locale_u8vector, "call-with-output-locale-u8vector", 1, 0, 0, - (SCM proc), - "Calls the one-argument procedure @var{proc} with a newly created output\n" - "port. When the function returns, a vector containing the bytes of a\n" - "locale representation of the characters written into the port is returned\n") -#define FUNC_NAME s_scm_call_with_output_locale_u8vector -{ - SCM p; - - p = scm_mkstrport (SCM_INUM0, - scm_make_string (SCM_INUM0, SCM_UNDEFINED), - SCM_OPN | SCM_WRTNG, - FUNC_NAME); - scm_call_1 (proc, p); - - return scm_get_output_locale_u8vector (p); -} -#undef FUNC_NAME - SCM_DEFINE (scm_call_with_output_string, "call-with-output-string", 1, 0, 0, (SCM proc), "Calls the one-argument procedure @var{proc} with a newly created output\n" @@ -473,27 +454,6 @@ SCM_DEFINE (scm_open_input_string, "open-input-string", 1, 0, 0, } #undef FUNC_NAME -SCM_DEFINE (scm_open_input_locale_u8vector, "open-input-locale-u8vector", 1, 0, 0, - (SCM vec), - "Take a u8vector containing the bytes of a string encoded in the\n" - "current locale and return an input port that delivers characters\n" - "from the string. The port can be closed by\n" - "@code{close-input-port}, though its storage will be reclaimed\n" - "by the garbage collector if it becomes inaccessible.") -#define FUNC_NAME s_scm_open_input_locale_u8vector -{ - scm_t_array_handle hnd; - ssize_t inc; - size_t len; - const scm_t_uint8 *buf; - - buf = scm_u8vector_elements (vec, &hnd, &len, &inc); - SCM p = scm_i_mkstrport(SCM_INUM0, (const char *) buf, len, SCM_OPN | SCM_RDNG, FUNC_NAME); - scm_array_handle_release (&hnd); - return p; -} -#undef FUNC_NAME - SCM_DEFINE (scm_open_output_string, "open-output-string", 0, 0, 0, (void), "Return an output port that will accumulate characters for\n" @@ -526,19 +486,6 @@ SCM_DEFINE (scm_get_output_string, "get-output-string", 1, 0, 0, #undef FUNC_NAME -SCM_DEFINE (scm_get_output_locale_u8vector, "get-output-locale-u8vector", 1, 0, 0, - (SCM port), - "Given an output port created by @code{open-output-string},\n" - "return a u8 vector containing the characters of the string\n" - "encoded in the current locale.") -#define FUNC_NAME s_scm_get_output_locale_u8vector -{ - SCM_VALIDATE_OPOUTSTRPORT (1, port); - return scm_strport_to_locale_u8vector (port); -} -#undef FUNC_NAME - - /* Given a null-terminated string EXPR containing a Scheme expression read it, and return it as an SCM value. */ SCM @@ -626,7 +573,6 @@ scm_make_stptob () { scm_t_bits tc = scm_make_port_type ("string", stfill_buffer, st_write); - scm_set_port_mark (tc, scm_markstream); scm_set_port_end_input (tc, st_end_input); scm_set_port_flush (tc, st_flush); scm_set_port_seek (tc, st_seek); diff --git a/libguile/strports.h b/libguile/strports.h index b2ded01f1..d93266a12 100644 --- a/libguile/strports.h +++ b/libguile/strports.h @@ -47,16 +47,12 @@ SCM_API SCM scm_mkstrport (SCM pos, SCM str, long modes, const char * caller); SCM_INTERNAL SCM scm_i_mkstrport (SCM pos, const char *locale_str, size_t str_len, long modes, const char *caller); SCM_API SCM scm_strport_to_string (SCM port); -SCM_API SCM scm_strport_to_locale_u8vector (SCM port); SCM_API SCM scm_object_to_string (SCM obj, SCM printer); SCM_API SCM scm_call_with_output_string (SCM proc); -SCM_API SCM scm_call_with_output_locale_u8vector (SCM proc); SCM_API SCM scm_call_with_input_string (SCM str, SCM proc); SCM_API SCM scm_open_input_string (SCM str); -SCM_API SCM scm_open_input_locale_u8vector (SCM str); SCM_API SCM scm_open_output_string (void); SCM_API SCM scm_get_output_string (SCM port); -SCM_API SCM scm_get_output_locale_u8vector (SCM port); SCM_API SCM scm_c_read_string (const char *expr); SCM_API SCM scm_c_eval_string (const char *expr); SCM_API SCM scm_c_eval_string_in_module (const char *expr, SCM module); diff --git a/libguile/struct.c b/libguile/struct.c index b7e72a719..9dd900e37 100644 --- a/libguile/struct.c +++ b/libguile/struct.c @@ -41,7 +41,7 @@ #include <string.h> #endif -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" @@ -795,7 +795,7 @@ SCM_DEFINE (scm_struct_vtable_tag, "struct-vtable-tag", 1, 0, 0, */ unsigned long -scm_struct_ihashq (SCM obj, unsigned long n) +scm_struct_ihashq (SCM obj, unsigned long n, void *closure) { /* The length of the hash table should be a relative prime it's not necessary to shift down the address. */ @@ -809,7 +809,7 @@ scm_struct_create_handle (SCM obj) obj, SCM_BOOL_F, scm_struct_ihashq, - scm_sloppy_assq, + (scm_t_assoc_fn) scm_sloppy_assq, 0); if (scm_is_false (SCM_CDR (handle))) SCM_SETCDR (handle, scm_cons (SCM_BOOL_F, SCM_BOOL_F)); diff --git a/libguile/struct.h b/libguile/struct.h index 12069b487..a4626f3dd 100644 --- a/libguile/struct.h +++ b/libguile/struct.h @@ -3,7 +3,7 @@ #ifndef SCM_STRUCT_H #define SCM_STRUCT_H -/* Copyright (C) 1995,1997,1999,2000,2001, 2006, 2007, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1997,1999,2000,2001, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -96,7 +96,7 @@ SCM_API SCM scm_struct_ref (SCM handle, SCM pos); SCM_API SCM scm_struct_set_x (SCM handle, SCM pos, SCM val); SCM_API SCM scm_struct_vtable (SCM handle); SCM_API SCM scm_struct_vtable_tag (SCM handle); -SCM_API unsigned long scm_struct_ihashq (SCM obj, unsigned long n); +SCM_INTERNAL unsigned long scm_struct_ihashq (SCM, unsigned long, void *); SCM_API SCM scm_struct_create_handle (SCM obj); SCM_API SCM scm_struct_vtable_name (SCM vtable); SCM_API SCM scm_set_struct_vtable_name_x (SCM vtable, SCM name); diff --git a/libguile/tags.h b/libguile/tags.h index 146f2a59c..4a68f9c78 100644 --- a/libguile/tags.h +++ b/libguile/tags.h @@ -506,12 +506,54 @@ enum scm_tc8_tags #define SCM_MAKIFLAG(n) SCM_MAKE_ITAG8 ((n), scm_tc8_flag) #define SCM_IFLAGNUM(n) (SCM_ITAG8_DATA (n)) +/* + * IMPORTANT NOTE regarding IFLAG numbering!!! + * + * Several macros depend upon careful IFLAG numbering of SCM_BOOL_F, + * SCM_BOOL_T, SCM_ELISP_NIL, SCM_EOL, and the two SCM_XXX_*_DONT_USE + * constants. In particular: + * + * - SCM_BOOL_F and SCM_BOOL_T must differ in exactly one bit position. + * (used to implement scm_is_bool_and_not_nil, aka scm_is_bool) + * + * - SCM_ELISP_NIL and SCM_BOOL_F must differ in exactly one bit position. + * (used to implement scm_is_false_or_nil and + * scm_is_true_and_not_nil) + * + * - SCM_ELISP_NIL and SCM_EOL must differ in exactly one bit position. + * (used to implement scm_is_null_or_nil) + * + * - SCM_ELISP_NIL, SCM_BOOL_F, SCM_EOL, SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE + * must all be equal except for two bit positions. + * (used to implement scm_is_lisp_false) + * + * - SCM_ELISP_NIL, SCM_BOOL_F, SCM_BOOL_T, SCM_XXX_ANOTHER_BOOLEAN_DONT_USE + * must all be equal except for two bit positions. + * (used to implement scm_is_bool_or_nil) + * + * These properties allow the aforementioned macros to be implemented + * by bitwise ANDing with a mask and then comparing with a constant, + * using as a common basis the macro SCM_MATCHES_BITS_IN_COMMON, + * defined below. The properties are checked at compile-time using + * `verify' macros near the top of boolean.c and pairs.c. + */ #define SCM_BOOL_F SCM_MAKIFLAG (0) -#define SCM_BOOL_T SCM_MAKIFLAG (1) -#define SCM_UNDEFINED SCM_MAKIFLAG (2) -#define SCM_EOF_VAL SCM_MAKIFLAG (3) -#define SCM_EOL SCM_MAKIFLAG (4) -#define SCM_UNSPECIFIED SCM_MAKIFLAG (5) +#define SCM_ELISP_NIL SCM_MAKIFLAG (1) + +#ifdef BUILDING_LIBGUILE +#define SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE SCM_MAKIFLAG (2) +#endif + +#define SCM_EOL SCM_MAKIFLAG (3) +#define SCM_BOOL_T SCM_MAKIFLAG (4) + +#ifdef BUILDING_LIBGUILE +#define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE SCM_MAKIFLAG (5) +#endif + +#define SCM_UNSPECIFIED SCM_MAKIFLAG (6) +#define SCM_UNDEFINED SCM_MAKIFLAG (7) +#define SCM_EOF_VAL SCM_MAKIFLAG (8) /* When a variable is unbound this is marked by the SCM_UNDEFINED * value. The following is an unbound value which can be handled on @@ -521,19 +563,57 @@ enum scm_tc8_tags * the code which handles this value in C so that SCM_UNDEFINED can be * used instead. It is not ideal to let this kind of unique and * strange values loose on the Scheme level. */ -#define SCM_UNBOUND SCM_MAKIFLAG (6) - -/* The Elisp nil value. */ -#define SCM_ELISP_NIL SCM_MAKIFLAG (7) - +#define SCM_UNBOUND SCM_MAKIFLAG (9) #define SCM_UNBNDP(x) (scm_is_eq ((x), SCM_UNDEFINED)) +/* + * SCM_MATCHES_BITS_IN_COMMON(x,a,b) returns 1 if and only if x + * matches both a and b in every bit position where a and b are equal; + * otherwise it returns 0. Bit positions where a and b differ are + * ignored. + * + * This is used to efficiently compare against two values which differ + * in exactly one bit position, or against four values which differ in + * exactly two bit positions. It is the basis for the following + * macros: + * + * scm_is_null_or_nil, + * scm_is_false_or_nil, + * scm_is_true_and_not_nil, + * scm_is_lisp_false, + * scm_is_lisp_true, + * scm_is_bool_and_not_nil (aka scm_is_bool) + * scm_is_bool_or_nil. + */ +#define SCM_MATCHES_BITS_IN_COMMON(x,a,b) \ + ((SCM_UNPACK(x) & ~(SCM_UNPACK(a) ^ SCM_UNPACK(b))) == \ + (SCM_UNPACK(a) & SCM_UNPACK(b))) + +/* + * These macros are used for compile-time verification that the + * constants have the properties needed for the above macro to work + * properly. + */ +#ifdef BUILDING_LIBGUILE +#define SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED(x) ((x) & ((x)-1)) +#define SCM_HAS_EXACTLY_ONE_BIT_SET(x) \ + ((x) != 0 && SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED (x) == 0) +#define SCM_HAS_EXACTLY_TWO_BITS_SET(x) \ + (SCM_HAS_EXACTLY_ONE_BIT_SET (SCM_WITH_LEAST_SIGNIFICANT_1_BIT_CLEARED (x))) + +#define SCM_VALUES_DIFFER_IN_EXACTLY_ONE_BIT_POSITION(a,b) \ + (SCM_HAS_EXACTLY_ONE_BIT_SET (SCM_UNPACK(a) ^ SCM_UNPACK(b))) +#define SCM_VALUES_DIFFER_IN_EXACTLY_TWO_BIT_POSITIONS(a,b,c,d) \ + (SCM_HAS_EXACTLY_TWO_BITS_SET ((SCM_UNPACK(a) ^ SCM_UNPACK(b)) | \ + (SCM_UNPACK(b) ^ SCM_UNPACK(c)) | \ + (SCM_UNPACK(c) ^ SCM_UNPACK(d)))) +#endif /* BUILDING_LIBGUILE */ /* Evaluator byte codes ('immediate symbols'). These constants are used only * in eval but their values have to be allocated here. The indices of the - * SCM_IM_ symbols must agree with the declarations in print.c: + * SCM_IM_ symbols must agree with the declarations in eval.c: * scm_isymnames. */ #define SCM_ISYMP(n) (SCM_ITAG8 (n) == scm_tc8_isym) @@ -559,12 +639,11 @@ enum scm_tc8_tags #define SCM_IM_SLOT_REF SCM_MAKISYM (17) #define SCM_IM_SLOT_SET_X SCM_MAKISYM (18) #define SCM_IM_DELAY SCM_MAKISYM (19) -#define SCM_IM_FUTURE SCM_MAKISYM (20) -#define SCM_IM_CALL_WITH_VALUES SCM_MAKISYM (21) -#define SCM_IM_ELSE SCM_MAKISYM (22) -#define SCM_IM_ARROW SCM_MAKISYM (23) -#define SCM_IM_NIL_COND SCM_MAKISYM (24) /* Multi-language support */ -#define SCM_IM_BIND SCM_MAKISYM (25) /* Multi-language support */ +#define SCM_IM_CALL_WITH_VALUES SCM_MAKISYM (20) +#define SCM_IM_ELSE SCM_MAKISYM (21) +#define SCM_IM_ARROW SCM_MAKISYM (22) +#define SCM_IM_NIL_COND SCM_MAKISYM (23) /* Multi-language support */ +#define SCM_IM_BIND SCM_MAKISYM (24) /* Multi-language support */ diff --git a/libguile/threads.c b/libguile/threads.c index f440bf59d..2ca4f15e9 100644 --- a/libguile/threads.c +++ b/libguile/threads.c @@ -22,7 +22,7 @@ # include <config.h> #endif -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" #include "libguile/_scm.h" #if HAVE_UNISTD_H @@ -53,6 +53,7 @@ #include "libguile/init.h" #include "libguile/scmsigs.h" #include "libguile/strings.h" +#include "libguile/weaks.h" #ifdef __MINGW32__ #ifndef ETIMEDOUT @@ -84,8 +85,14 @@ to_timespec (SCM t, scm_t_timespec *waittime) } } + /*** Queues */ +/* Note: We annotate with "GC-robust" assignments whose purpose is to avoid + the risk of false references leading to unbounded retained space as + described in "Bounding Space Usage of Conservative Garbage Collectors", + H.J. Boehm, 2001. */ + /* Make an empty queue data structure. */ static SCM @@ -128,6 +135,10 @@ remqueue (SCM q, SCM c) if (scm_is_eq (c, SCM_CAR (q))) SCM_SETCAR (q, SCM_CDR (c)); SCM_SETCDR (prev, SCM_CDR (c)); + + /* GC-robust */ + SCM_SETCDR (c, SCM_EOL); + SCM_CRITICAL_SECTION_END; return 1; } @@ -157,6 +168,10 @@ dequeue (SCM q) if (scm_is_null (SCM_CDR (q))) SCM_SETCAR (q, SCM_EOL); SCM_CRITICAL_SECTION_END; + + /* GC-robust */ + SCM_SETCDR (c, SCM_EOL); + return SCM_CAR (c); } } @@ -199,15 +214,7 @@ thread_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) return 1; } -static size_t -thread_free (SCM obj) -{ - scm_i_thread *t = SCM_I_THREAD_DATA (obj); - assert (t->exited); - scm_gc_free (t, sizeof (*t), "thread"); - return 0; -} - + /*** Blocking on queues. */ /* See also scm_i_queue_async_cell for how such a block is @@ -279,141 +286,31 @@ unblock_from_queue (SCM queue) return thread; } + /* Getting into and out of guile mode. */ -/* Ken Raeburn observes that the implementation of suspend and resume - (and the things that build on top of them) are very likely not - correct (see below). We will need fix this eventually, and that's - why scm_leave_guile/scm_enter_guile are not exported in the API. - - Ken writes: - - Consider this sequence: - - Function foo, called in Guile mode, calls suspend (maybe indirectly - through scm_leave_guile), which does this: - - // record top of stack for the GC - t->top = SCM_STACK_PTR (&t); // just takes address of automatic - var 't' - // save registers. - SCM_FLUSH_REGISTER_WINDOWS; // sparc only - SCM_I_SETJMP (t->regs); // here's most of the magic - - ... and returns. - - Function foo has a SCM value X, a handle on a non-immediate object, in - a caller-saved register R, and it's the only reference to the object - currently. - - The compiler wants to use R in suspend, so it pushes the current - value, X, into a stack slot which will be reloaded on exit from - suspend; then it loads stuff into R and goes about its business. The - setjmp call saves (some of) the current registers, including R, which - no longer contains X. (This isn't a problem for a normal - setjmp/longjmp situation, where longjmp would be called before - setjmp's caller returns; the old value for X would be loaded back from - the stack after the longjmp, before the function returned.) - - So, suspend returns, loading X back into R (and invalidating the jump - buffer) in the process. The caller foo then goes off and calls a - bunch of other functions out of Guile mode, occasionally storing X on - the stack again, but, say, much deeper on the stack than suspend's - stack frame went, and the stack slot where suspend had written X has - long since been overwritten with other values. - - Okay, nothing actively broken so far. Now, let garbage collection - run, triggered by another thread. - - The thread calling foo is out of Guile mode at the time, so the - garbage collector just scans a range of stack addresses. Too bad that - X isn't stored there. So the pointed-to storage goes onto the free - list, and I think you can see where things go from there. - - Is there anything I'm missing that'll prevent this scenario from - happening? I mean, aside from, "well, suspend and scm_leave_guile - don't have many local variables, so they probably won't need to save - any registers on most systems, so we hope everything will wind up in - the jump buffer and we'll just get away with it"? - - (And, going the other direction, if scm_leave_guile and suspend push - the stack pointer over onto a new page, and foo doesn't make further - function calls and thus the stack pointer no longer includes that - page, are we guaranteed that the kernel cannot release the now-unused - stack page that contains the top-of-stack pointer we just saved? I - don't know if any OS actually does that. If it does, we could get - faults in garbage collection.) - - I don't think scm_without_guile has to have this problem, as it gets - more control over the stack handling -- but it should call setjmp - itself. I'd probably try something like: - - // record top of stack for the GC - t->top = SCM_STACK_PTR (&t); - // save registers. - SCM_FLUSH_REGISTER_WINDOWS; - SCM_I_SETJMP (t->regs); - res = func(data); - scm_enter_guile (t); - - ... though even that's making some assumptions about the stack - ordering of local variables versus caller-saved registers. - - For something like scm_leave_guile to work, I don't think it can just - rely on invalidated jump buffers. A valid jump buffer, and a handle - on the stack state at the point when the jump buffer was initialized, - together, would work fine, but I think then we're talking about macros - invoking setjmp in the caller's stack frame, and requiring that the - caller of scm_leave_guile also call scm_enter_guile before returning, - kind of like pthread_cleanup_push/pop calls that have to be paired up - in a function. (In fact, the pthread ones have to be paired up - syntactically, as if they might expand to a compound statement - incorporating the user's code, and invoking a compiler's - exception-handling primitives. Which might be something to think - about for cases where Guile is used with C++ exceptions or - pthread_cancel.) -*/ +#ifdef SCM_HAVE_THREAD_STORAGE_CLASS -scm_i_pthread_key_t scm_i_thread_key; +/* When thread-local storage (TLS) is available, a pointer to the + current-thread object is kept in TLS. Note that storing the thread-object + itself in TLS (rather than a pointer to some malloc'd memory) is not + possible since thread objects may live longer than the actual thread they + represent. */ +SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread = NULL; -static void -resume (scm_i_thread *t) -{ - t->top = NULL; -} +# define SET_CURRENT_THREAD(_t) scm_i_current_thread = (_t) -typedef void* scm_t_guile_ticket; +#else /* !SCM_HAVE_THREAD_STORAGE_CLASS */ -static void -scm_enter_guile (scm_t_guile_ticket ticket) -{ - scm_i_thread *t = (scm_i_thread *)ticket; - if (t) - { - resume (t); - } -} +/* Key used to retrieve the current thread with `pthread_getspecific ()'. */ +scm_i_pthread_key_t scm_i_thread_key; -static scm_i_thread * -suspend (void) -{ - scm_i_thread *t = SCM_I_CURRENT_THREAD; +# define SET_CURRENT_THREAD(_t) \ + scm_i_pthread_setspecific (scm_i_thread_key, (_t)) - /* record top of stack for the GC */ - t->top = SCM_STACK_PTR (&t); - /* save registers. */ - SCM_FLUSH_REGISTER_WINDOWS; - SCM_I_SETJMP (t->regs); - return t; -} +#endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */ -static scm_t_guile_ticket -scm_leave_guile () -{ - scm_i_thread *t = suspend (); - return (scm_t_guile_ticket) t; -} static scm_i_pthread_mutex_t thread_admin_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; static scm_i_thread *all_threads = NULL; @@ -440,6 +337,7 @@ guilify_self_1 (SCM_STACKITEM *base) t->active_asyncs = SCM_EOL; t->block_asyncs = 1; t->pending_asyncs = 1; + t->critical_section_level = 0; t->last_debug_frame = NULL; t->base = base; #ifdef __ia64__ @@ -478,7 +376,7 @@ guilify_self_1 (SCM_STACKITEM *base) t->exited = 0; t->guile_mode = 0; - scm_i_pthread_setspecific (scm_i_thread_key, t); + SET_CURRENT_THREAD (t); scm_i_pthread_mutex_lock (&thread_admin_mutex); t->next_thread = all_threads; @@ -563,14 +461,18 @@ do_thread_exit (void *v) while (!scm_is_null (t->mutexes)) { - SCM mutex = SCM_CAR (t->mutexes); - fat_mutex *m = SCM_MUTEX_DATA (mutex); - scm_i_pthread_mutex_lock (&m->lock); + SCM mutex = SCM_WEAK_PAIR_CAR (t->mutexes); - unblock_from_queue (m->waiting); + if (!SCM_UNBNDP (mutex)) + { + fat_mutex *m = SCM_MUTEX_DATA (mutex); - scm_i_pthread_mutex_unlock (&m->lock); - t->mutexes = SCM_CDR (t->mutexes); + scm_i_pthread_mutex_lock (&m->lock); + unblock_from_queue (m->waiting); + scm_i_pthread_mutex_unlock (&m->lock); + } + + t->mutexes = SCM_WEAK_PAIR_CDR (t->mutexes); } scm_i_pthread_mutex_unlock (&t->admin_mutex); @@ -592,7 +494,7 @@ on_thread_exit (void *v) t->held_mutex = NULL; } - scm_i_pthread_setspecific (scm_i_thread_key, v); + SET_CURRENT_THREAD (v); /* Ensure the signal handling thread has been launched, because we might be shutting it down. */ @@ -614,6 +516,10 @@ on_thread_exit (void *v) if (*tp == t) { *tp = t->next_thread; + + /* GC-robust */ + t->next_thread = NULL; + break; } thread_count--; @@ -627,9 +533,11 @@ on_thread_exit (void *v) scm_i_pthread_mutex_unlock (&thread_admin_mutex); - scm_i_pthread_setspecific (scm_i_thread_key, NULL); + SET_CURRENT_THREAD (NULL); } +#ifndef SCM_HAVE_THREAD_STORAGE_CLASS + static scm_i_pthread_once_t init_thread_key_once = SCM_I_PTHREAD_ONCE_INIT; static void @@ -638,6 +546,8 @@ init_thread_key (void) scm_i_pthread_key_create (&scm_i_thread_key, NULL); } +#endif + /* Perform any initializations necessary to bring the current thread into guile mode, initializing Guile itself, if necessary. @@ -655,9 +565,12 @@ scm_i_init_thread_for_guile (SCM_STACKITEM *base, SCM parent) { scm_i_thread *t; +#ifndef SCM_HAVE_THREAD_STORAGE_CLASS scm_i_pthread_once (&init_thread_key_once, init_thread_key); +#endif - if ((t = SCM_I_CURRENT_THREAD) == NULL) + t = SCM_I_CURRENT_THREAD; + if (t == NULL) { /* This thread has not been guilified yet. */ @@ -699,7 +612,7 @@ scm_i_init_thread_for_guile (SCM_STACKITEM *base, SCM parent) t->base = base; #endif - scm_enter_guile ((scm_t_guile_ticket) t); + t->top = NULL; return 1; } else @@ -806,7 +719,6 @@ scm_with_guile (void *(*func)(void *), void *data) SCM_UNUSED static void scm_leave_guile_cleanup (void *x) { - scm_leave_guile (); on_thread_exit (SCM_I_CURRENT_THREAD); } @@ -823,7 +735,6 @@ scm_i_with_guile_and_parent (void *(*func)(void *), void *data, SCM parent) scm_i_pthread_cleanup_push (scm_leave_guile_cleanup, NULL); res = scm_c_with_continuation_barrier (func, data); scm_i_pthread_cleanup_pop (0); - scm_leave_guile (); } else res = scm_c_with_continuation_barrier (func, data); @@ -834,14 +745,18 @@ scm_i_with_guile_and_parent (void *(*func)(void *), void *data, SCM parent) /*** Non-guile mode. */ -#if (defined HAVE_GC_DO_BLOCKING) && (!defined HAVE_DECL_GC_DO_BLOCKING) +#ifdef HAVE_GC_DO_BLOCKING -/* This declaration is missing from the public headers of GC 7.1. */ -extern void GC_do_blocking (void (*) (void *), void *); +# ifndef HAVE_GC_FN_TYPE +/* This typedef is missing from the public headers of GC 7.1 and earlier. */ +typedef void * (* GC_fn_type) (void *); +# endif /* HAVE_GC_FN_TYPE */ -#endif +# ifndef HAVE_DECL_GC_DO_BLOCKING +/* This declaration is missing from the public headers of GC 7.1. */ +extern void GC_do_blocking (GC_fn_type, void *); +# endif /* HAVE_DECL_GC_DO_BLOCKING */ -#ifdef HAVE_GC_DO_BLOCKING struct without_guile_arg { void * (*function) (void *); @@ -861,7 +776,9 @@ without_guile_trampoline (void *closure) SCM_I_CURRENT_THREAD->guile_mode = 1; } -#endif + +#endif /* HAVE_GC_DO_BLOCKING */ + void * scm_without_guile (void *(*func)(void *), void *data) @@ -875,7 +792,7 @@ scm_without_guile (void *(*func)(void *), void *data) arg.function = func; arg.data = data; - GC_do_blocking (without_guile_trampoline, &arg); + GC_do_blocking ((GC_fn_type) without_guile_trampoline, &arg); result = arg.result; } else @@ -1215,7 +1132,6 @@ fat_mutex_free (SCM mx) { fat_mutex *m = SCM_MUTEX_DATA (mx); scm_i_pthread_mutex_destroy (&m->lock); - scm_gc_free (m, sizeof (fat_mutex), "mutex"); return 0; } @@ -1318,7 +1234,14 @@ fat_mutex_lock (SCM mutex, scm_t_timespec *timeout, SCM owner, int *ret) { scm_i_thread *t = SCM_I_THREAD_DATA (new_owner); scm_i_pthread_mutex_lock (&t->admin_mutex); - t->mutexes = scm_cons (mutex, t->mutexes); + + /* Only keep a weak reference to MUTEX so that it's not + retained when not referenced elsewhere (bug #27450). Note + that the weak pair itself it still retained, but it's better + than retaining MUTEX and the threads referred to by its + associated queue. */ + t->mutexes = scm_weak_car_pair (mutex, t->mutexes); + scm_i_pthread_mutex_unlock (&t->admin_mutex); } *ret = 1; @@ -1621,14 +1544,6 @@ SCM_DEFINE (scm_mutex_locked_p, "mutex-locked?", 1, 0, 0, } #undef FUNC_NAME -static size_t -fat_cond_free (SCM mx) -{ - fat_cond *c = SCM_CONDVAR_DATA (mx); - scm_gc_free (c, sizeof (fat_cond), "condition-variable"); - return 0; -} - static int fat_cond_print (SCM cv, SCM port, scm_print_state *pstate SCM_UNUSED) { @@ -1727,26 +1642,6 @@ SCM_DEFINE (scm_condition_variable_p, "condition-variable?", 1, 0, 0, } #undef FUNC_NAME -/*** Marking stacks */ - -/* XXX - what to do with this? Do we need to handle this for blocked - threads as well? -*/ -#ifdef __ia64__ -# define SCM_MARK_BACKING_STORE() do { \ - ucontext_t ctx; \ - SCM_STACKITEM * top, * bot; \ - getcontext (&ctx); \ - scm_mark_locations ((SCM_STACKITEM *) &ctx.uc_mcontext, \ - ((size_t) (sizeof (SCM_STACKITEM) - 1 + sizeof ctx.uc_mcontext) \ - / sizeof (SCM_STACKITEM))); \ - bot = (SCM_STACKITEM *) SCM_I_CURRENT_THREAD->register_backing_store_base; \ - top = (SCM_STACKITEM *) scm_ia64_ar_bsp (&ctx); \ - scm_mark_locations (bot, top - bot); } while (0) -#else -# define SCM_MARK_BACKING_STORE() -#endif - /*** Select */ @@ -1870,11 +1765,13 @@ scm_dynwind_pthread_mutex_lock (scm_i_pthread_mutex_t *mutex) int scm_pthread_cond_wait (scm_i_pthread_cond_t *cond, scm_i_pthread_mutex_t *mutex) { - scm_t_guile_ticket t = scm_leave_guile (); - ((scm_i_thread *)t)->held_mutex = mutex; - int res = scm_i_pthread_cond_wait (cond, mutex); - ((scm_i_thread *)t)->held_mutex = NULL; - scm_enter_guile (t); + int res; + scm_i_thread *t = SCM_I_CURRENT_THREAD; + + t->held_mutex = mutex; + res = scm_i_pthread_cond_wait (cond, mutex); + t->held_mutex = NULL; + return res; } @@ -1883,11 +1780,13 @@ scm_pthread_cond_timedwait (scm_i_pthread_cond_t *cond, scm_i_pthread_mutex_t *mutex, const scm_t_timespec *wt) { - scm_t_guile_ticket t = scm_leave_guile (); - ((scm_i_thread *)t)->held_mutex = mutex; - int res = scm_i_pthread_cond_timedwait (cond, mutex, wt); - ((scm_i_thread *)t)->held_mutex = NULL; - scm_enter_guile (t); + int res; + scm_i_thread *t = SCM_I_CURRENT_THREAD; + + t->held_mutex = mutex; + res = scm_i_pthread_cond_timedwait (cond, mutex, wt); + t->held_mutex = NULL; + return res; } @@ -1989,7 +1888,6 @@ static int threads_initialized_p = 0; /* This mutex is used by SCM_CRITICAL_SECTION_START/END. */ scm_i_pthread_mutex_t scm_i_critical_section_mutex; -int scm_i_critical_section_level = 0; static SCM dynwind_critical_section_mutex; @@ -2036,7 +1934,6 @@ scm_init_threads () { scm_tc16_thread = scm_make_smob_type ("thread", sizeof (scm_i_thread)); scm_set_smob_print (scm_tc16_thread, thread_print); - scm_set_smob_free (scm_tc16_thread, thread_free); /* XXX: Could be removed */ scm_tc16_mutex = scm_make_smob_type ("mutex", sizeof (fat_mutex)); scm_set_smob_print (scm_tc16_mutex, fat_mutex_print); @@ -2045,7 +1942,6 @@ scm_init_threads () scm_tc16_condvar = scm_make_smob_type ("condition-variable", sizeof (fat_cond)); scm_set_smob_print (scm_tc16_condvar, fat_cond_print); - scm_set_smob_free (scm_tc16_condvar, fat_cond_free); scm_i_default_dynamic_state = SCM_BOOL_F; guilify_self_2 (SCM_BOOL_F); diff --git a/libguile/threads.h b/libguile/threads.h index 55102df17..5afe45faa 100644 --- a/libguile/threads.h +++ b/libguile/threads.h @@ -3,7 +3,7 @@ #ifndef SCM_THREADS_H #define SCM_THREADS_H -/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1996,1997,1998,2000,2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -117,6 +117,9 @@ typedef struct scm_i_thread { scm_t_contregs *pending_rbs_continuation; #endif + /* Whether this thread is in a critical section. */ + int critical_section_level; + } scm_i_thread; #define SCM_I_IS_THREAD(x) SCM_SMOB_PREDICATE (scm_tc16_thread, x) @@ -189,15 +192,29 @@ SCM_API SCM scm_thread_exited_p (SCM thread); SCM_API void scm_dynwind_critical_section (SCM mutex); -#define SCM_I_CURRENT_THREAD \ - ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key)) -SCM_API scm_i_pthread_key_t scm_i_thread_key; +#ifdef BUILDING_LIBGUILE + +# ifdef SCM_HAVE_THREAD_STORAGE_CLASS + +SCM_INTERNAL SCM_THREAD_LOCAL scm_i_thread *scm_i_current_thread; +# define SCM_I_CURRENT_THREAD (scm_i_current_thread) + +# else /* !SCM_HAVE_THREAD_STORAGE_CLASS */ + +SCM_INTERNAL scm_i_pthread_key_t scm_i_thread_key; +# define SCM_I_CURRENT_THREAD \ + ((scm_i_thread *) scm_i_pthread_getspecific (scm_i_thread_key)) + +# endif /* !SCM_HAVE_THREAD_STORAGE_CLASS */ + +# define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds) +# define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w)) +# define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame) +# define scm_i_set_last_debug_frame(f) \ + (SCM_I_CURRENT_THREAD->last_debug_frame = (f)) + +#endif /* BUILDING_LIBGUILE */ -#define scm_i_dynwinds() (SCM_I_CURRENT_THREAD->dynwinds) -#define scm_i_set_dynwinds(w) (SCM_I_CURRENT_THREAD->dynwinds = (w)) -#define scm_i_last_debug_frame() (SCM_I_CURRENT_THREAD->last_debug_frame) -#define scm_i_set_last_debug_frame(f) \ - (SCM_I_CURRENT_THREAD->last_debug_frame = (f)) SCM_INTERNAL scm_i_pthread_mutex_t scm_i_misc_mutex; diff --git a/libguile/throw.c b/libguile/throw.c index cf6ea4a49..07d215f41 100644 --- a/libguile/throw.c +++ b/libguile/throw.c @@ -732,7 +732,7 @@ scm_ithrow (SCM key, SCM args, int noreturn SCM_UNUSED) SCM dynpair = SCM_UNDEFINED; SCM winds; - if (scm_i_critical_section_level) + if (SCM_I_CURRENT_THREAD->critical_section_level) { SCM s = args; int i = 0; diff --git a/libguile/unidata_to_charset.pl b/libguile/unidata_to_charset.pl index 6871e67ee..d086c8ec4 100755 --- a/libguile/unidata_to_charset.pl +++ b/libguile/unidata_to_charset.pl @@ -254,10 +254,14 @@ sub empty { return 0; } -# Full -- All characters. -sub full { +# Designated -- All characters except for the surrogates +sub designated { my($codepoint, $name, $category, $uppercase, $lowercase)= @_; - return 1; + if ($category =~ (/Cs/)) { + return 0; + } else { + return 1; + } } @@ -362,7 +366,7 @@ sub compute { # Write a bit of a header print $out "/* srfi-14.i.c -- standard SRFI-14 character set data */\n\n"; print $out "/* This file is #include'd by srfi-14.c. */\n\n"; -print $out "/* This file was generated from\n" +print $out "/* This file was generated from\n"; print $out " http://unicode.org/Public/UNIDATA/UnicodeData.txt\n"; print $out " with the unidata_to_charset.pl script. */\n\n"; @@ -383,7 +387,7 @@ compute "symbol"; compute "blank"; compute "ascii"; compute "empty"; -compute "full"; +compute "designated"; close $in; close $out; diff --git a/libguile/uniform.c b/libguile/uniform.c index 28125da8b..6bab85675 100644 --- a/libguile/uniform.c +++ b/libguile/uniform.c @@ -44,13 +44,24 @@ const size_t scm_i_array_element_type_sizes[SCM_ARRAY_ELEMENT_TYPE_LAST + 1] = { 64, 128 }; -/* FIXME: return bit size instead of byte size? */ size_t scm_array_handle_uniform_element_size (scm_t_array_handle *h) { size_t ret = scm_i_array_element_type_sizes[h->element_type]; if (ret && ret % 8 == 0) return ret / 8; + else if (ret) + scm_wrong_type_arg_msg (NULL, 0, h->array, "byte-aligned uniform array"); + else + scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array"); +} + +size_t +scm_array_handle_uniform_element_bit_size (scm_t_array_handle *h) +{ + size_t ret = scm_i_array_element_type_sizes[h->element_type]; + if (ret) + return ret; else scm_wrong_type_arg_msg (NULL, 0, h->array, "uniform array"); } @@ -110,14 +121,15 @@ SCM_DEFINE (scm_uniform_vector_p, "uniform-vector?", 1, 0, 0, SCM_DEFINE (scm_uniform_vector_element_type, "uniform-vector-element-type", 1, 0, 0, (SCM v), - "Return the number of elements in the uniform vector, @var{v}.") + "Return the type of the elements in the uniform vector, @var{v}.") #define FUNC_NAME s_scm_uniform_vector_element_type { scm_t_array_handle h; - size_t len; - ssize_t inc; SCM ret; - scm_uniform_vector_elements (v, &h, &len, &inc); + + if (!scm_is_uniform_vector (v)) + scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, v, "uniform vector"); + scm_array_get_handle (v, &h); ret = scm_array_handle_element_type (&h); scm_array_handle_release (&h); return ret; @@ -144,15 +156,9 @@ SCM_DEFINE (scm_uniform_vector_element_size, "uniform-vector-element-size", 1, 0 SCM scm_c_uniform_vector_ref (SCM v, size_t idx) { - SCM ret; - scm_t_array_handle h; - size_t len; - ssize_t inc; - - scm_uniform_vector_elements (v, &h, &len, &inc); - ret = scm_array_handle_ref (&h, idx*inc); - scm_array_handle_release (&h); - return ret; + if (!scm_is_uniform_vector (v)) + scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); + return scm_c_generalized_vector_ref (v, idx); } SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0, @@ -168,13 +174,9 @@ SCM_DEFINE (scm_uniform_vector_ref, "uniform-vector-ref", 2, 0, 0, void scm_c_uniform_vector_set_x (SCM v, size_t idx, SCM val) { - scm_t_array_handle h; - size_t len; - ssize_t inc; - - scm_uniform_vector_elements (v, &h, &len, &inc); - scm_array_handle_set (&h, idx*inc, val); - scm_array_handle_release (&h); + if (!scm_is_uniform_vector (v)) + scm_wrong_type_arg_msg (NULL, 0, v, "uniform vector"); + scm_c_generalized_vector_set_x (v, idx, val); } SCM_DEFINE (scm_uniform_vector_set_x, "uniform-vector-set!", 3, 0, 0, @@ -193,15 +195,9 @@ SCM_DEFINE (scm_uniform_vector_to_list, "uniform-vector->list", 1, 0, 0, "Convert the uniform numeric vector @var{uvec} to a list.") #define FUNC_NAME s_scm_uniform_vector_to_list { - SCM ret; - scm_t_array_handle h; - size_t len; - ssize_t inc; - - scm_uniform_vector_elements (uvec, &h, &len, &inc); - ret = scm_generalized_vector_to_list (uvec); - scm_array_handle_release (&h); - return ret; + if (!scm_is_uniform_vector (uvec)) + scm_wrong_type_arg_msg (FUNC_NAME, SCM_ARG1, uvec, "uniform vector"); + return scm_generalized_vector_to_list (uvec); } #undef FUNC_NAME diff --git a/libguile/uniform.h b/libguile/uniform.h index b1f396594..f0d5915f6 100644 --- a/libguile/uniform.h +++ b/libguile/uniform.h @@ -36,8 +36,10 @@ SCM_INTERNAL const size_t scm_i_array_element_type_sizes[]; #define SCM_ARRAY_ELEMENT_TYPE_IS_UNBOXED(t) \ (scm_i_array_element_type_sizes[(t)] != 0) -/* returns type size in bits */ +/* type size in bytes */ SCM_API size_t scm_array_handle_uniform_element_size (scm_t_array_handle *h); +/* type size in bits */ +SCM_API size_t scm_array_handle_uniform_element_bit_size (scm_t_array_handle *h); SCM_API const void *scm_array_handle_uniform_elements (scm_t_array_handle *h); SCM_API void *scm_array_handle_uniform_writable_elements (scm_t_array_handle *h); diff --git a/libguile/vectors.c b/libguile/vectors.c index 5e710505a..b1b5890f4 100644 --- a/libguile/vectors.c +++ b/libguile/vectors.c @@ -41,7 +41,7 @@ #include "libguile/dynwind.h" #include "libguile/deprecation.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" diff --git a/libguile/vm-engine.c b/libguile/vm-engine.c index b373cd017..5d8f65587 100644 --- a/libguile/vm-engine.c +++ b/libguile/vm-engine.c @@ -51,7 +51,6 @@ VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs) size_t free_vars_count = 0; /* length of FREE_VARS */ SCM *objects = NULL; /* constant objects */ size_t object_count = 0; /* length of OBJECTS */ - SCM *stack_base = vp->stack_base; /* stack base address */ SCM *stack_limit = vp->stack_limit; /* stack limit address */ /* Internal variables */ @@ -108,16 +107,16 @@ VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs) /* Initial frame */ CACHE_REGISTER (); PUSH ((SCM)fp); /* dynamic link */ - PUSH (0); /* ra */ PUSH (0); /* mvra */ + PUSH ((SCM)ip); /* ra */ CACHE_PROGRAM (); PUSH (program); fp = sp + 1; - INIT_FRAME (); + ip = bp->base; /* MV-call frame, function & arguments */ PUSH ((SCM)fp); /* dynamic link */ - PUSH (0); /* ra */ PUSH (0); /* mvra */ + PUSH (0); /* ra */ PUSH (prog); if (SCM_UNLIKELY (sp + nargs >= stack_limit)) goto vm_error_too_many_args; @@ -171,6 +170,21 @@ VM_NAME (struct scm_vm *vp, SCM program, SCM *argv, int nargs) finish_args = SCM_EOL; goto vm_error; + vm_error_kwargs_length_not_even: + err_msg = scm_from_locale_string ("Bad keyword argument list: odd length"); + finish_args = SCM_EOL; + goto vm_error; + + vm_error_kwargs_invalid_keyword: + err_msg = scm_from_locale_string ("Bad keyword argument list: expected keyword"); + finish_args = SCM_EOL; + goto vm_error; + + vm_error_kwargs_unrecognized_keyword: + err_msg = scm_from_locale_string ("Bad keyword argument list: unrecognized keyword"); + finish_args = SCM_EOL; + goto vm_error; + vm_error_too_many_args: err_msg = scm_from_locale_string ("VM: Too many arguments"); finish_args = scm_list_1 (scm_from_int (nargs)); diff --git a/libguile/vm-engine.h b/libguile/vm-engine.h index 3c1bbf681..d69328944 100644 --- a/libguile/vm-engine.h +++ b/libguile/vm-engine.h @@ -107,7 +107,6 @@ ip = vp->ip; \ sp = vp->sp; \ fp = vp->fp; \ - stack_base = fp ? SCM_FRAME_UPPER_ADDRESS (fp) - 1 : vp->stack_base; \ } #define SYNC_REGISTER() \ @@ -256,7 +255,7 @@ goto vm_error_stack_overflow #define CHECK_UNDERFLOW() \ - if (sp < stack_base) \ + if (sp < SCM_FRAME_UPPER_ADDRESS (fp)) \ goto vm_error_stack_underflow; #define PUSH(x) do { sp++; CHECK_OVERFLOW (); *sp = x; } while (0) @@ -336,7 +335,6 @@ do { \ #define FETCH() (*ip++) #define FETCH_LENGTH(len) do { len=*ip++; len<<=8; len+=*ip++; len<<=8; len+=*ip++; } while (0) -#define FETCH_WIDTH(width) do { width=*ip++; } while (0) #undef CLOCK #if VM_USE_CLOCK @@ -361,46 +359,9 @@ do { \ } -/* - * Stack frame - */ - -#define INIT_ARGS() \ -{ \ - if (SCM_UNLIKELY (bp->nrest)) \ - { \ - int n = nargs - (bp->nargs - 1); \ - if (n < 0) \ - goto vm_error_wrong_num_args; \ - /* NB, can cause GC while setting up the \ - stack frame */ \ - POP_LIST (n); \ - } \ - else \ - { \ - if (SCM_UNLIKELY (nargs != bp->nargs)) \ - goto vm_error_wrong_num_args; \ - } \ -} - /* See frames.h for the layout of stack frames */ /* When this is called, bp points to the new program data, and the arguments are already on the stack */ -#define INIT_FRAME() \ -{ \ - int i; \ - \ - /* New registers */ \ - sp += bp->nlocs; \ - CHECK_OVERFLOW (); \ - stack_base = sp; \ - ip = bp->base; \ - \ - /* Init local variables */ \ - for (i=bp->nlocs; i;) \ - sp[-(--i)] = SCM_UNDEFINED; \ -} - #define DROP_FRAME() \ { \ sp -= 3; \ diff --git a/libguile/vm-i-scheme.c b/libguile/vm-i-scheme.c index 0cace147d..ff963d64c 100644 --- a/libguile/vm-i-scheme.c +++ b/libguile/vm-i-scheme.c @@ -32,13 +32,13 @@ VM_DEFINE_FUNCTION (100, not, "not", 1) { ARGS1 (x); - RETURN (SCM_BOOL (SCM_FALSEP (x))); + RETURN (SCM_BOOL (scm_is_false_or_nil (x))); } VM_DEFINE_FUNCTION (101, not_not, "not-not", 1) { ARGS1 (x); - RETURN (SCM_BOOL (!SCM_FALSEP (x))); + RETURN (SCM_BOOL (!scm_is_false_or_nil (x))); } VM_DEFINE_FUNCTION (102, eq, "eq?", 2) @@ -56,13 +56,13 @@ VM_DEFINE_FUNCTION (103, not_eq, "not-eq?", 2) VM_DEFINE_FUNCTION (104, nullp, "null?", 1) { ARGS1 (x); - RETURN (SCM_BOOL (SCM_NULLP (x))); + RETURN (SCM_BOOL (scm_is_null_or_nil (x))); } VM_DEFINE_FUNCTION (105, not_nullp, "not-null?", 1) { ARGS1 (x); - RETURN (SCM_BOOL (!SCM_NULLP (x))); + RETURN (SCM_BOOL (!scm_is_null_or_nil (x))); } VM_DEFINE_FUNCTION (106, eqv, "eqv?", 2) @@ -433,10 +433,17 @@ BV_FIXABLE_INT_REF (u16, u16_native, uint16, 2) VM_DEFINE_FUNCTION (142, bv_s16_native_ref, "bv-s16-native-ref", 2) BV_FIXABLE_INT_REF (s16, s16_native, int16, 2) VM_DEFINE_FUNCTION (143, bv_u32_native_ref, "bv-u32-native-ref", 2) -/* FIXME: u32 is always a fixnum on 64-bit builds */ +#if SIZEOF_VOID_P > 4 +BV_FIXABLE_INT_REF (u32, u32_native, uint32, 4) +#else BV_INT_REF (u32, uint32, 4) +#endif VM_DEFINE_FUNCTION (144, bv_s32_native_ref, "bv-s32-native-ref", 2) +#if SIZEOF_VOID_P > 4 +BV_FIXABLE_INT_REF (s32, s32_native, int32, 4) +#else BV_INT_REF (s32, int32, 4) +#endif VM_DEFINE_FUNCTION (145, bv_u64_native_ref, "bv-u64-native-ref", 2) BV_INT_REF (u64, uint64, 8) VM_DEFINE_FUNCTION (146, bv_s64_native_ref, "bv-s64-native-ref", 2) @@ -541,10 +548,17 @@ BV_FIXABLE_INT_SET (u16, u16_native, uint16, 0, SCM_T_UINT16_MAX, 2) VM_DEFINE_INSTRUCTION (160, bv_s16_native_set, "bv-s16-native-set", 0, 3, 0) BV_FIXABLE_INT_SET (s16, s16_native, int16, SCM_T_INT16_MIN, SCM_T_INT16_MAX, 2) VM_DEFINE_INSTRUCTION (161, bv_u32_native_set, "bv-u32-native-set", 0, 3, 0) -/* FIXME: u32 is always a fixnum on 64-bit builds */ +#if SIZEOF_VOID_P > 4 +BV_FIXABLE_INT_SET (u32, u32_native, uint32, 0, SCM_T_UINT32_MAX, 4) +#else BV_INT_SET (u32, uint32, 4) +#endif VM_DEFINE_INSTRUCTION (162, bv_s32_native_set, "bv-s32-native-set", 0, 3, 0) +#if SIZEOF_VOID_P > 4 +BV_FIXABLE_INT_SET (s32, s32_native, int32, SCM_T_INT32_MIN, SCM_T_INT32_MAX, 4) +#else BV_INT_SET (s32, int32, 4) +#endif VM_DEFINE_INSTRUCTION (163, bv_u64_native_set, "bv-u64-native-set", 0, 3, 0) BV_INT_SET (u64, uint64, 8) VM_DEFINE_INSTRUCTION (164, bv_s64_native_set, "bv-s64-native-set", 0, 3, 0) diff --git a/libguile/vm-i-system.c b/libguile/vm-i-system.c index 0662f8188..f58ffce58 100644 --- a/libguile/vm-i-system.c +++ b/libguile/vm-i-system.c @@ -46,14 +46,18 @@ VM_DEFINE_INSTRUCTION (1, halt, "halt", 0, 0, 0) } { - ASSERT (sp == stack_base); - ASSERT (stack_base == SCM_FRAME_UPPER_ADDRESS (fp) - 1); +#ifdef VM_ENABLE_STACK_NULLING + SCM *old_sp = sp; +#endif /* Restore registers */ sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1; - ip = NULL; + /* Setting the ip here doesn't actually affect control flow, as the calling + code will restore its own registers, but it does help when walking the + stack */ + ip = SCM_FRAME_RETURN_ADDRESS (fp); fp = SCM_FRAME_DYNAMIC_LINK (fp); - NULLSTACK (stack_base - sp); + NULLSTACK (old_sp - sp); } goto vm_done; @@ -273,7 +277,28 @@ VM_DEFINE_INSTRUCTION (22, long_local_ref, "long-local-ref", 2, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (23, variable_ref, "variable-ref", 0, 0, 1) +VM_DEFINE_INSTRUCTION (23, local_bound, "local-bound?", 1, 0, 1) +{ + if (LOCAL_REF (FETCH ()) == SCM_UNDEFINED) + PUSH (SCM_BOOL_F); + else + PUSH (SCM_BOOL_T); + NEXT; +} + +VM_DEFINE_INSTRUCTION (24, long_local_bound, "long-local-bound?", 2, 0, 1) +{ + unsigned int i = FETCH (); + i <<= 8; + i += FETCH (); + if (LOCAL_REF (i) == SCM_UNDEFINED) + PUSH (SCM_BOOL_F); + else + PUSH (SCM_BOOL_T); + NEXT; +} + +VM_DEFINE_INSTRUCTION (25, variable_ref, "variable-ref", 0, 0, 1) { SCM x = *sp; @@ -292,7 +317,16 @@ VM_DEFINE_INSTRUCTION (23, variable_ref, "variable-ref", 0, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (24, toplevel_ref, "toplevel-ref", 1, 0, 1) +VM_DEFINE_INSTRUCTION (26, variable_bound, "variable-bound?", 0, 0, 1) +{ + if (VARIABLE_BOUNDP (*sp)) + *sp = SCM_BOOL_T; + else + *sp = SCM_BOOL_F; + NEXT; +} + +VM_DEFINE_INSTRUCTION (27, toplevel_ref, "toplevel-ref", 1, 0, 1) { unsigned objnum = FETCH (); SCM what; @@ -315,7 +349,7 @@ VM_DEFINE_INSTRUCTION (24, toplevel_ref, "toplevel-ref", 1, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (25, long_toplevel_ref, "long-toplevel-ref", 2, 0, 1) +VM_DEFINE_INSTRUCTION (28, long_toplevel_ref, "long-toplevel-ref", 2, 0, 1) { SCM what; unsigned int objnum = FETCH (); @@ -342,14 +376,14 @@ VM_DEFINE_INSTRUCTION (25, long_toplevel_ref, "long-toplevel-ref", 2, 0, 1) /* set */ -VM_DEFINE_INSTRUCTION (26, local_set, "local-set", 1, 1, 0) +VM_DEFINE_INSTRUCTION (29, local_set, "local-set", 1, 1, 0) { LOCAL_SET (FETCH (), *sp); DROP (); NEXT; } -VM_DEFINE_INSTRUCTION (27, long_local_set, "long-local-set", 2, 1, 0) +VM_DEFINE_INSTRUCTION (30, long_local_set, "long-local-set", 2, 1, 0) { unsigned int i = FETCH (); i <<= 8; @@ -359,14 +393,14 @@ VM_DEFINE_INSTRUCTION (27, long_local_set, "long-local-set", 2, 1, 0) NEXT; } -VM_DEFINE_INSTRUCTION (28, variable_set, "variable-set", 0, 1, 0) +VM_DEFINE_INSTRUCTION (31, variable_set, "variable-set", 0, 1, 0) { VARIABLE_SET (sp[0], sp[-1]); DROPN (2); NEXT; } -VM_DEFINE_INSTRUCTION (29, toplevel_set, "toplevel-set", 1, 1, 0) +VM_DEFINE_INSTRUCTION (32, toplevel_set, "toplevel-set", 1, 1, 0) { unsigned objnum = FETCH (); SCM what; @@ -385,7 +419,7 @@ VM_DEFINE_INSTRUCTION (29, toplevel_set, "toplevel-set", 1, 1, 0) NEXT; } -VM_DEFINE_INSTRUCTION (30, long_toplevel_set, "long-toplevel-set", 2, 1, 0) +VM_DEFINE_INSTRUCTION (33, long_toplevel_set, "long-toplevel-set", 2, 1, 0) { SCM what; unsigned int objnum = FETCH (); @@ -411,63 +445,64 @@ VM_DEFINE_INSTRUCTION (30, long_toplevel_set, "long-toplevel-set", 2, 1, 0) * branch and jump */ -/* offset must be a signed 16 bit int!!! */ +/* offset must be at least 24 bits wide, and signed */ #define FETCH_OFFSET(offset) \ { \ - int h = FETCH (); \ - int l = FETCH (); \ - offset = (h << 8) + l; \ + offset = FETCH () << 16; \ + offset += FETCH () << 8; \ + offset += FETCH (); \ + offset -= (offset & (1<<23)) << 1; \ } #define BR(p) \ { \ - scm_t_int16 offset; \ + scm_t_int32 offset; \ FETCH_OFFSET (offset); \ if (p) \ - ip += ((scm_t_ptrdiff)offset) * 8 - (((unsigned long)ip) % 8); \ + ip += offset; \ NULLSTACK (1); \ DROP (); \ NEXT; \ } -VM_DEFINE_INSTRUCTION (31, br, "br", 2, 0, 0) +VM_DEFINE_INSTRUCTION (34, br, "br", 3, 0, 0) { - scm_t_int16 offset; + scm_t_int32 offset; FETCH_OFFSET (offset); - ip += ((scm_t_ptrdiff)offset) * 8 - (((unsigned long)ip) % 8); + ip += offset; NEXT; } -VM_DEFINE_INSTRUCTION (32, br_if, "br-if", 2, 0, 0) +VM_DEFINE_INSTRUCTION (35, br_if, "br-if", 3, 0, 0) { - BR (!SCM_FALSEP (*sp)); + BR (scm_is_true_and_not_nil (*sp)); } -VM_DEFINE_INSTRUCTION (33, br_if_not, "br-if-not", 2, 0, 0) +VM_DEFINE_INSTRUCTION (36, br_if_not, "br-if-not", 3, 0, 0) { - BR (SCM_FALSEP (*sp)); + BR (scm_is_false_or_nil (*sp)); } -VM_DEFINE_INSTRUCTION (34, br_if_eq, "br-if-eq", 2, 0, 0) +VM_DEFINE_INSTRUCTION (37, br_if_eq, "br-if-eq", 3, 0, 0) { sp--; /* underflow? */ BR (SCM_EQ_P (sp[0], sp[1])); } -VM_DEFINE_INSTRUCTION (35, br_if_not_eq, "br-if-not-eq", 2, 0, 0) +VM_DEFINE_INSTRUCTION (38, br_if_not_eq, "br-if-not-eq", 3, 0, 0) { sp--; /* underflow? */ BR (!SCM_EQ_P (sp[0], sp[1])); } -VM_DEFINE_INSTRUCTION (36, br_if_null, "br-if-null", 2, 0, 0) +VM_DEFINE_INSTRUCTION (39, br_if_null, "br-if-null", 3, 0, 0) { - BR (SCM_NULLP (*sp)); + BR (scm_is_null_or_nil (*sp)); } -VM_DEFINE_INSTRUCTION (37, br_if_not_null, "br-if-not-null", 2, 0, 0) +VM_DEFINE_INSTRUCTION (40, br_if_not_null, "br-if-not-null", 3, 0, 0) { - BR (!SCM_NULLP (*sp)); + BR (!scm_is_null_or_nil (*sp)); } @@ -475,15 +510,218 @@ VM_DEFINE_INSTRUCTION (37, br_if_not_null, "br-if-not-null", 2, 0, 0) * Subprogram call */ -VM_DEFINE_INSTRUCTION (38, new_frame, "new-frame", 0, 0, 3) +VM_DEFINE_INSTRUCTION (41, br_if_nargs_ne, "br-if-nargs-ne", 5, 0, 0) { + scm_t_ptrdiff n; + n = FETCH () << 8; + n += FETCH (); + scm_t_int32 offset; + FETCH_OFFSET (offset); + if (sp - (fp - 1) != n) + ip += offset; + NEXT; +} + +VM_DEFINE_INSTRUCTION (42, br_if_nargs_lt, "br-if-nargs-lt", 5, 0, 0) +{ + scm_t_ptrdiff n; + n = FETCH () << 8; + n += FETCH (); + scm_t_int32 offset; + FETCH_OFFSET (offset); + if (sp - (fp - 1) < n) + ip += offset; + NEXT; +} + +VM_DEFINE_INSTRUCTION (43, br_if_nargs_gt, "br-if-nargs-gt", 5, 0, 0) +{ + scm_t_ptrdiff n; + n = FETCH () << 8; + n += FETCH (); + scm_t_int32 offset; + FETCH_OFFSET (offset); + if (sp - (fp - 1) > n) + ip += offset; + NEXT; +} + +VM_DEFINE_INSTRUCTION (44, assert_nargs_ee, "assert-nargs-ee", 2, 0, 0) +{ + scm_t_ptrdiff n; + n = FETCH () << 8; + n += FETCH (); + if (sp - (fp - 1) != n) + goto vm_error_wrong_num_args; + NEXT; +} + +VM_DEFINE_INSTRUCTION (45, assert_nargs_ge, "assert-nargs-ge", 2, 0, 0) +{ + scm_t_ptrdiff n; + n = FETCH () << 8; + n += FETCH (); + if (sp - (fp - 1) < n) + goto vm_error_wrong_num_args; + NEXT; +} + +VM_DEFINE_INSTRUCTION (46, bind_optionals, "bind-optionals", 2, -1, -1) +{ + scm_t_ptrdiff n; + n = FETCH () << 8; + n += FETCH (); + while (sp - (fp - 1) < n) + PUSH (SCM_UNDEFINED); + NEXT; +} + +VM_DEFINE_INSTRUCTION (47, bind_optionals_shuffle, "bind-optionals/shuffle", 6, -1, -1) +{ + SCM *walk; + scm_t_ptrdiff nreq, nreq_and_opt, ntotal; + nreq = FETCH () << 8; + nreq += FETCH (); + nreq_and_opt = FETCH () << 8; + nreq_and_opt += FETCH (); + ntotal = FETCH () << 8; + ntotal += FETCH (); + + /* look in optionals for first keyword or last positional */ + /* starting after the last required positional arg */ + walk = fp + nreq; + while (/* while we have args */ + walk <= sp + /* and we still have positionals to fill */ + && walk - fp < nreq_and_opt + /* and we haven't reached a keyword yet */ + && !scm_is_keyword (*walk)) + /* bind this optional arg (by leaving it in place) */ + walk++; + /* now shuffle up, from walk to ntotal */ + { + scm_t_ptrdiff nshuf = sp - walk + 1, i; + sp = (fp - 1) + ntotal + nshuf; + CHECK_OVERFLOW (); + for (i = 0; i < nshuf; i++) + sp[-i] = walk[nshuf-i-1]; + } + /* and fill optionals & keyword args with SCM_UNDEFINED */ + while (walk <= (fp - 1) + ntotal) + *walk++ = SCM_UNDEFINED; + + NEXT; +} + +VM_DEFINE_INSTRUCTION (48, bind_kwargs, "bind-kwargs", 5, 0, 0) +{ + scm_t_uint16 idx; + scm_t_ptrdiff nkw; + int allow_other_keys_and_rest; + SCM kw; + idx = FETCH () << 8; + idx += FETCH (); + nkw = FETCH () << 8; + nkw += FETCH (); + allow_other_keys_and_rest = FETCH (); + + if (!(allow_other_keys_and_rest & 2) + &&(sp - (fp - 1) - nkw) % 2) + goto vm_error_kwargs_length_not_even; + + CHECK_OBJECT (idx); + kw = OBJECT_REF (idx); + /* switch nkw to be a negative index below sp */ + for (nkw = -(sp - (fp - 1) - nkw) + 1; nkw < 0; nkw += 2) + { + SCM walk; + if (!scm_is_keyword (sp[nkw])) + { + if (allow_other_keys_and_rest & 2) + /* reached the end of keywords, but we have a rest arg; just cut + out */ + break; + else + goto vm_error_kwargs_invalid_keyword; + } + for (walk = kw; scm_is_pair (walk); walk = SCM_CDR (walk)) + { + if (scm_is_eq (SCM_CAAR (walk), sp[nkw])) + { + SCM si = SCM_CDAR (walk); + LOCAL_SET (SCM_I_INUMP (si) ? SCM_I_INUM (si) : scm_to_long (si), + sp[nkw + 1]); + break; + } + } + if (!(allow_other_keys_and_rest & 1) && !scm_is_pair (walk)) + goto vm_error_kwargs_unrecognized_keyword; + } + + NEXT; +} + +VM_DEFINE_INSTRUCTION (49, push_rest, "push-rest", 2, -1, -1) +{ + scm_t_ptrdiff n; + SCM rest = SCM_EOL; + n = FETCH () << 8; + n += FETCH (); + while (sp - (fp - 1) > n) + /* No need to check for underflow. */ + CONS (rest, *sp--, rest); + PUSH (rest); + NEXT; +} + +VM_DEFINE_INSTRUCTION (50, bind_rest, "bind-rest", 4, -1, -1) +{ + scm_t_ptrdiff n; + scm_t_uint32 i; + SCM rest = SCM_EOL; + n = FETCH () << 8; + n += FETCH (); + i = FETCH () << 8; + i += FETCH (); + while (sp - (fp - 1) > n) + /* No need to check for underflow. */ + CONS (rest, *sp--, rest); + LOCAL_SET (i, rest); + NEXT; +} + +VM_DEFINE_INSTRUCTION (51, reserve_locals, "reserve-locals", 2, -1, -1) +{ + SCM *old_sp; + scm_t_int32 n; + n = FETCH () << 8; + n += FETCH (); + old_sp = sp; + sp = (fp - 1) + n; + + if (old_sp < sp) + { + CHECK_OVERFLOW (); + while (old_sp < sp) + *++old_sp = SCM_UNDEFINED; + } + else + NULLSTACK (old_sp - sp); + + NEXT; +} + +VM_DEFINE_INSTRUCTION (52, new_frame, "new-frame", 0, 0, 3) +{ + /* NB: if you change this, see frames.c:vm-frame-num-locals */ + /* and frames.h, vm-engine.c, etc of course */ PUSH ((SCM)fp); /* dynamic link */ PUSH (0); /* mvra */ PUSH (0); /* ra */ NEXT; } -VM_DEFINE_INSTRUCTION (39, call, "call", 1, -1, 1) +VM_DEFINE_INSTRUCTION (53, call, "call", 1, -1, 1) { SCM x; nargs = FETCH (); @@ -501,13 +739,12 @@ VM_DEFINE_INSTRUCTION (39, call, "call", 1, -1, 1) { program = x; CACHE_PROGRAM (); - INIT_ARGS (); - fp = sp - bp->nargs + 1; + fp = sp - nargs + 1; ASSERT (SCM_FRAME_RETURN_ADDRESS (fp) == 0); ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp) == 0); SCM_FRAME_SET_RETURN_ADDRESS (fp, ip); SCM_FRAME_SET_MV_RETURN_ADDRESS (fp, 0); - INIT_FRAME (); + ip = bp->base; ENTER_HOOK (); APPLY_HOOK (); NEXT; @@ -545,7 +782,7 @@ VM_DEFINE_INSTRUCTION (39, call, "call", 1, -1, 1) goto vm_error_wrong_type_apply; } -VM_DEFINE_INSTRUCTION (40, goto_args, "goto/args", 1, -1, 1) +VM_DEFINE_INSTRUCTION (54, goto_args, "goto/args", 1, -1, 1) { register SCM x; nargs = FETCH (); @@ -562,7 +799,8 @@ VM_DEFINE_INSTRUCTION (40, goto_args, "goto/args", 1, -1, 1) { int i; #ifdef VM_ENABLE_STACK_NULLING - SCM *old_sp; + SCM *old_sp = sp; + CHECK_STACK_LEAK (); #endif EXIT_HOOK (); @@ -570,22 +808,15 @@ VM_DEFINE_INSTRUCTION (40, goto_args, "goto/args", 1, -1, 1) /* switch programs */ program = x; CACHE_PROGRAM (); - INIT_ARGS (); - -#ifdef VM_ENABLE_STACK_NULLING - old_sp = sp; - CHECK_STACK_LEAK (); -#endif + /* shuffle down the program and the arguments */ + for (i = -1, sp = sp - nargs + 1; i < nargs; i++) + SCM_FRAME_STACK_ADDRESS (fp)[i] = sp[i]; - /* delay shuffling the new program+args down so that if INIT_ARGS had to - cons up a rest arg, going into GC, the stack still made sense */ - for (i = -1, sp = sp - bp->nargs + 1; i < bp->nargs; i++) - fp[i] = sp[i]; sp = fp + i - 1; NULLSTACK (old_sp - sp); - INIT_FRAME (); + ip = bp->base; ENTER_HOOK (); APPLY_HOOK (); @@ -624,7 +855,7 @@ VM_DEFINE_INSTRUCTION (40, goto_args, "goto/args", 1, -1, 1) goto vm_error_wrong_type_apply; } -VM_DEFINE_INSTRUCTION (41, goto_nargs, "goto/nargs", 0, 0, 1) +VM_DEFINE_INSTRUCTION (55, goto_nargs, "goto/nargs", 0, 0, 1) { SCM x; POP (x); @@ -633,7 +864,7 @@ VM_DEFINE_INSTRUCTION (41, goto_nargs, "goto/nargs", 0, 0, 1) goto vm_goto_args; } -VM_DEFINE_INSTRUCTION (42, call_nargs, "call/nargs", 0, 0, 1) +VM_DEFINE_INSTRUCTION (56, call_nargs, "call/nargs", 0, 0, 1) { SCM x; POP (x); @@ -642,15 +873,15 @@ VM_DEFINE_INSTRUCTION (42, call_nargs, "call/nargs", 0, 0, 1) goto vm_call; } -VM_DEFINE_INSTRUCTION (43, mv_call, "mv-call", 3, -1, 1) +VM_DEFINE_INSTRUCTION (57, mv_call, "mv-call", 4, -1, 1) { SCM x; - scm_t_int16 offset; + scm_t_int32 offset; scm_t_uint8 *mvra; nargs = FETCH (); FETCH_OFFSET (offset); - mvra = ip + ((scm_t_ptrdiff)offset) * 8 - ((unsigned long)ip) % 8; + mvra = ip + offset; x = sp[-nargs]; @@ -661,13 +892,12 @@ VM_DEFINE_INSTRUCTION (43, mv_call, "mv-call", 3, -1, 1) { program = x; CACHE_PROGRAM (); - INIT_ARGS (); - fp = sp - bp->nargs + 1; + fp = sp - nargs + 1; ASSERT (SCM_FRAME_RETURN_ADDRESS (fp) == 0); ASSERT (SCM_FRAME_MV_RETURN_ADDRESS (fp) == 0); SCM_FRAME_SET_RETURN_ADDRESS (fp, ip); SCM_FRAME_SET_MV_RETURN_ADDRESS (fp, mvra); - INIT_FRAME (); + ip = bp->base; ENTER_HOOK (); APPLY_HOOK (); NEXT; @@ -705,7 +935,7 @@ VM_DEFINE_INSTRUCTION (43, mv_call, "mv-call", 3, -1, 1) goto vm_error_wrong_type_apply; } -VM_DEFINE_INSTRUCTION (44, apply, "apply", 1, -1, 1) +VM_DEFINE_INSTRUCTION (58, apply, "apply", 1, -1, 1) { int len; SCM ls; @@ -724,7 +954,7 @@ VM_DEFINE_INSTRUCTION (44, apply, "apply", 1, -1, 1) goto vm_call; } -VM_DEFINE_INSTRUCTION (45, goto_apply, "goto/apply", 1, -1, 1) +VM_DEFINE_INSTRUCTION (59, goto_apply, "goto/apply", 1, -1, 1) { int len; SCM ls; @@ -743,7 +973,7 @@ VM_DEFINE_INSTRUCTION (45, goto_apply, "goto/apply", 1, -1, 1) goto vm_goto_args; } -VM_DEFINE_INSTRUCTION (46, call_cc, "call/cc", 0, 1, 1) +VM_DEFINE_INSTRUCTION (60, call_cc, "call/cc", 0, 1, 1) { int first; SCM proc, cont; @@ -780,7 +1010,7 @@ VM_DEFINE_INSTRUCTION (46, call_cc, "call/cc", 0, 1, 1) } } -VM_DEFINE_INSTRUCTION (47, goto_cc, "goto/cc", 0, 1, 1) +VM_DEFINE_INSTRUCTION (61, goto_cc, "goto/cc", 0, 1, 1) { int first; SCM proc, cont; @@ -812,7 +1042,7 @@ VM_DEFINE_INSTRUCTION (47, goto_cc, "goto/cc", 0, 1, 1) } } -VM_DEFINE_INSTRUCTION (48, return, "return", 0, 1, 1) +VM_DEFINE_INSTRUCTION (62, return, "return", 0, 1, 1) { vm_return: EXIT_HOOK (); @@ -823,20 +1053,19 @@ VM_DEFINE_INSTRUCTION (48, return, "return", 0, 1, 1) SCM ret; POP (ret); - ASSERT (sp == stack_base); - ASSERT (stack_base == SCM_FRAME_UPPER_ADDRESS (fp) - 1); + +#ifdef VM_ENABLE_STACK_NULLING + SCM *old_sp = sp; +#endif /* Restore registers */ sp = SCM_FRAME_LOWER_ADDRESS (fp); ip = SCM_FRAME_RETURN_ADDRESS (fp); fp = SCM_FRAME_DYNAMIC_LINK (fp); - { + #ifdef VM_ENABLE_STACK_NULLING - int nullcount = stack_base - sp; + NULLSTACK (old_sp - sp); #endif - stack_base = SCM_FRAME_UPPER_ADDRESS (fp) - 1; - NULLSTACK (nullcount); - } /* Set return value (sp is already pushed) */ *sp = ret; @@ -849,7 +1078,7 @@ VM_DEFINE_INSTRUCTION (48, return, "return", 0, 1, 1) NEXT; } -VM_DEFINE_INSTRUCTION (49, return_values, "return/values", 1, -1, -1) +VM_DEFINE_INSTRUCTION (63, return_values, "return/values", 1, -1, -1) { /* nvalues declared at top level, because for some reason gcc seems to think that perhaps it might be used without declaration. Fooey to that, I say. */ @@ -858,11 +1087,10 @@ VM_DEFINE_INSTRUCTION (49, return_values, "return/values", 1, -1, -1) EXIT_HOOK (); RETURN_HOOK (); - ASSERT (stack_base == SCM_FRAME_UPPER_ADDRESS (fp) - 1); - - /* data[1] is the mv return address */ if (nvalues != 1 && SCM_FRAME_MV_RETURN_ADDRESS (fp)) { + /* A multiply-valued continuation */ + SCM *vals = sp - nvalues; int i; /* Restore registers */ sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1; @@ -871,12 +1099,11 @@ VM_DEFINE_INSTRUCTION (49, return_values, "return/values", 1, -1, -1) /* Push return values, and the number of values */ for (i = 0; i < nvalues; i++) - *++sp = stack_base[1+i]; + *++sp = vals[i+1]; *++sp = SCM_I_MAKINUM (nvalues); - /* Finally set new stack_base */ - NULLSTACK (stack_base - sp + nvalues + 1); - stack_base = SCM_FRAME_UPPER_ADDRESS (fp) - 1; + /* Finally null the end of the stack */ + NULLSTACK (vals + nvalues - sp); } else if (nvalues >= 1) { @@ -884,17 +1111,17 @@ VM_DEFINE_INSTRUCTION (49, return_values, "return/values", 1, -1, -1) break with guile tradition and try and do something sensible. (Also, this block handles the single-valued return to an mv continuation.) */ + SCM *vals = sp - nvalues; /* Restore registers */ sp = SCM_FRAME_LOWER_ADDRESS (fp) - 1; ip = SCM_FRAME_RETURN_ADDRESS (fp); fp = SCM_FRAME_DYNAMIC_LINK (fp); /* Push first value */ - *++sp = stack_base[1]; + *++sp = vals[1]; - /* Finally set new stack_base */ - NULLSTACK (stack_base - sp + nvalues + 1); - stack_base = SCM_FRAME_UPPER_ADDRESS (fp) - 1; + /* Finally null the end of the stack */ + NULLSTACK (vals + nvalues - sp); } else goto vm_error_no_values; @@ -906,7 +1133,7 @@ VM_DEFINE_INSTRUCTION (49, return_values, "return/values", 1, -1, -1) NEXT; } -VM_DEFINE_INSTRUCTION (50, return_values_star, "return/values*", 1, -1, -1) +VM_DEFINE_INSTRUCTION (64, return_values_star, "return/values*", 1, -1, -1) { SCM l; @@ -929,7 +1156,7 @@ VM_DEFINE_INSTRUCTION (50, return_values_star, "return/values*", 1, -1, -1) goto vm_return_values; } -VM_DEFINE_INSTRUCTION (51, truncate_values, "truncate-values", 2, -1, -1) +VM_DEFINE_INSTRUCTION (65, truncate_values, "truncate-values", 2, -1, -1) { SCM x; int nbinds, rest; @@ -952,7 +1179,7 @@ VM_DEFINE_INSTRUCTION (51, truncate_values, "truncate-values", 2, -1, -1) NEXT; } -VM_DEFINE_INSTRUCTION (52, box, "box", 1, 1, 0) +VM_DEFINE_INSTRUCTION (66, box, "box", 1, 1, 0) { SCM val; POP (val); @@ -966,7 +1193,7 @@ VM_DEFINE_INSTRUCTION (52, box, "box", 1, 1, 0) (set! a (lambda () (b ...))) ...) */ -VM_DEFINE_INSTRUCTION (53, empty_box, "empty-box", 1, 0, 0) +VM_DEFINE_INSTRUCTION (67, empty_box, "empty-box", 1, 0, 0) { SYNC_BEFORE_GC (); LOCAL_SET (FETCH (), @@ -974,7 +1201,7 @@ VM_DEFINE_INSTRUCTION (53, empty_box, "empty-box", 1, 0, 0) NEXT; } -VM_DEFINE_INSTRUCTION (54, local_boxed_ref, "local-boxed-ref", 1, 0, 1) +VM_DEFINE_INSTRUCTION (68, local_boxed_ref, "local-boxed-ref", 1, 0, 1) { SCM v = LOCAL_REF (FETCH ()); ASSERT_BOUND_VARIABLE (v); @@ -982,7 +1209,7 @@ VM_DEFINE_INSTRUCTION (54, local_boxed_ref, "local-boxed-ref", 1, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (55, local_boxed_set, "local-boxed-set", 1, 1, 0) +VM_DEFINE_INSTRUCTION (69, local_boxed_set, "local-boxed-set", 1, 1, 0) { SCM v, val; v = LOCAL_REF (FETCH ()); @@ -992,7 +1219,7 @@ VM_DEFINE_INSTRUCTION (55, local_boxed_set, "local-boxed-set", 1, 1, 0) NEXT; } -VM_DEFINE_INSTRUCTION (56, free_ref, "free-ref", 1, 0, 1) +VM_DEFINE_INSTRUCTION (70, free_ref, "free-ref", 1, 0, 1) { scm_t_uint8 idx = FETCH (); @@ -1003,7 +1230,7 @@ VM_DEFINE_INSTRUCTION (56, free_ref, "free-ref", 1, 0, 1) /* no free-set -- if a var is assigned, it should be in a box */ -VM_DEFINE_INSTRUCTION (57, free_boxed_ref, "free-boxed-ref", 1, 0, 1) +VM_DEFINE_INSTRUCTION (71, free_boxed_ref, "free-boxed-ref", 1, 0, 1) { SCM v; scm_t_uint8 idx = FETCH (); @@ -1014,7 +1241,7 @@ VM_DEFINE_INSTRUCTION (57, free_boxed_ref, "free-boxed-ref", 1, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (58, free_boxed_set, "free-boxed-set", 1, 1, 0) +VM_DEFINE_INSTRUCTION (72, free_boxed_set, "free-boxed-set", 1, 1, 0) { SCM v, val; scm_t_uint8 idx = FETCH (); @@ -1026,7 +1253,7 @@ VM_DEFINE_INSTRUCTION (58, free_boxed_set, "free-boxed-set", 1, 1, 0) NEXT; } -VM_DEFINE_INSTRUCTION (59, make_closure, "make-closure", 0, 2, 1) +VM_DEFINE_INSTRUCTION (73, make_closure, "make-closure", 0, 2, 1) { SCM vect; POP (vect); @@ -1037,7 +1264,7 @@ VM_DEFINE_INSTRUCTION (59, make_closure, "make-closure", 0, 2, 1) NEXT; } -VM_DEFINE_INSTRUCTION (60, make_variable, "make-variable", 0, 0, 1) +VM_DEFINE_INSTRUCTION (74, make_variable, "make-variable", 0, 0, 1) { SYNC_BEFORE_GC (); /* fixme underflow */ @@ -1045,7 +1272,7 @@ VM_DEFINE_INSTRUCTION (60, make_variable, "make-variable", 0, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (61, fix_closure, "fix-closure", 2, 0, 1) +VM_DEFINE_INSTRUCTION (75, fix_closure, "fix-closure", 2, 0, 1) { SCM x, vect; unsigned int i = FETCH (); @@ -1059,7 +1286,7 @@ VM_DEFINE_INSTRUCTION (61, fix_closure, "fix-closure", 2, 0, 1) NEXT; } -VM_DEFINE_INSTRUCTION (62, define, "define", 0, 0, 2) +VM_DEFINE_INSTRUCTION (76, define, "define", 0, 0, 2) { SCM sym, val; POP (sym); @@ -1071,7 +1298,7 @@ VM_DEFINE_INSTRUCTION (62, define, "define", 0, 0, 2) NEXT; } -VM_DEFINE_INSTRUCTION (63, make_keyword, "make-keyword", 0, 1, 1) +VM_DEFINE_INSTRUCTION (77, make_keyword, "make-keyword", 0, 1, 1) { CHECK_UNDERFLOW (); SYNC_REGISTER (); @@ -1079,7 +1306,7 @@ VM_DEFINE_INSTRUCTION (63, make_keyword, "make-keyword", 0, 1, 1) NEXT; } -VM_DEFINE_INSTRUCTION (64, make_symbol, "make-symbol", 0, 1, 1) +VM_DEFINE_INSTRUCTION (78, make_symbol, "make-symbol", 0, 1, 1) { CHECK_UNDERFLOW (); SYNC_REGISTER (); diff --git a/libguile/vm.c b/libguile/vm.c index 95aaa4fe4..df02f0595 100644 --- a/libguile/vm.c +++ b/libguile/vm.c @@ -25,7 +25,7 @@ #include <string.h> #include <assert.h> -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" #include <gc/gc_mark.h> #include "_scm.h" @@ -180,9 +180,8 @@ static SCM really_make_boot_program (long nargs) { SCM u8vec; - scm_t_uint8 text[] = { scm_op_mv_call, 0, 0, 1, - scm_op_make_int8_1, scm_op_nop, scm_op_nop, scm_op_nop, - scm_op_halt }; + scm_t_uint8 text[] = { scm_op_mv_call, 0, 0, 0, 1, + scm_op_make_int8_1, scm_op_halt }; struct scm_objcode *bp; SCM ret; @@ -192,18 +191,14 @@ really_make_boot_program (long nargs) bp = scm_malloc (sizeof (struct scm_objcode) + sizeof (text)); memcpy (bp->base, text, sizeof (text)); - bp->nargs = 0; - bp->nrest = 0; - bp->nlocs = 0; bp->len = sizeof(text); bp->metalen = 0; - bp->unused = 0; u8vec = scm_take_u8vector ((scm_t_uint8*)bp, sizeof (struct scm_objcode) + sizeof (text)); ret = scm_make_program (scm_bytecode_to_objcode (u8vec), SCM_BOOL_F, SCM_BOOL_F); - SCM_SET_SMOB_FLAGS (ret, SCM_F_PROGRAM_IS_BOOT); + SCM_SET_CELL_WORD_0 (ret, SCM_CELL_WORD_0 (ret) | SCM_F_PROGRAM_IS_BOOT); return ret; } @@ -357,7 +352,8 @@ vm_stack_mark (GC_word *addr, struct GC_ms_entry *mark_stack_ptr, corresponding VM. */ vm = * ((struct scm_vm **) addr); - if ((SCM *) addr != vm->stack_base - 1 + if (vm == NULL + || (SCM *) addr != vm->stack_base - 1 || vm->stack_limit - vm->stack_base != vm->stack_size) /* ADDR must be a pointer to a free-list element, which we must ignore (see warning in <gc/gc_mark.h>). */ diff --git a/libguile/vports.c b/libguile/vports.c index cea11c61d..4fab2df18 100644 --- a/libguile/vports.c +++ b/libguile/vports.c @@ -1,4 +1,4 @@ -/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2006 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2006, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -92,19 +92,26 @@ sf_fill_input (SCM port) { SCM p = SCM_PACK (SCM_STREAM (port)); SCM ans; + scm_t_port *pt; ans = scm_call_0 (SCM_SIMPLE_VECTOR_REF (p, 3)); /* get char. */ if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans)) return EOF; SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input"); - { - scm_t_port *pt = SCM_PTAB_ENTRY (port); - - *pt->read_buf = SCM_CHAR (ans); - pt->read_pos = pt->read_buf; - pt->read_end = pt->read_buf + 1; - return *pt->read_buf; - } + pt = SCM_PTAB_ENTRY (port); + + if (pt->encoding == NULL) + { + scm_t_port *pt = SCM_PTAB_ENTRY (port); + + *pt->read_buf = SCM_CHAR (ans); + pt->read_pos = pt->read_buf; + pt->read_end = pt->read_buf + 1; + return *pt->read_buf; + } + else + scm_ungetc (SCM_CHAR (ans), port); + return SCM_CHAR (ans); } @@ -213,7 +220,6 @@ scm_make_sfptob () { scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write); - scm_set_port_mark (tc, scm_markstream); scm_set_port_flush (tc, sf_flush); scm_set_port_close (tc, sf_close); scm_set_port_input_waiting (tc, sf_input_waiting); diff --git a/libguile/weaks.c b/libguile/weaks.c index 4719980a7..913166ffd 100644 --- a/libguile/weaks.c +++ b/libguile/weaks.c @@ -32,7 +32,7 @@ #include "libguile/validate.h" #include "libguile/weaks.h" -#include "libguile/boehm-gc.h" +#include "libguile/bdw-gc.h" #include <gc/gc_typed.h> diff --git a/libguile/weaks.h b/libguile/weaks.h index 908e27628..eecc6184e 100644 --- a/libguile/weaks.h +++ b/libguile/weaks.h @@ -3,7 +3,7 @@ #ifndef SCM_WEAKS_H #define SCM_WEAKS_H -/* Copyright (C) 1995,1996,2000,2001, 2003, 2006, 2008 Free Software Foundation, Inc. +/* Copyright (C) 1995,1996,2000,2001, 2003, 2006, 2008, 2009 Free Software Foundation, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -55,9 +55,9 @@ /* Weak pairs. */ -SCM_API SCM scm_weak_car_pair (SCM car, SCM cdr); -SCM_API SCM scm_weak_cdr_pair (SCM car, SCM cdr); -SCM_API SCM scm_doubly_weak_pair (SCM car, SCM cdr); +SCM_INTERNAL SCM scm_weak_car_pair (SCM car, SCM cdr); +SCM_INTERNAL SCM scm_weak_cdr_pair (SCM car, SCM cdr); +SCM_INTERNAL SCM scm_doubly_weak_pair (SCM car, SCM cdr); /* Testing the weak component(s) of a cell for reachability. */ #define SCM_WEAK_PAIR_WORD_DELETED_P(_cell, _word) \ @@ -71,10 +71,12 @@ SCM_API SCM scm_doubly_weak_pair (SCM car, SCM cdr); ((SCM_WEAK_PAIR_CAR_DELETED_P (_cell)) \ || (SCM_WEAK_PAIR_CDR_DELETED_P (_cell))) -/* Accessing the components of a weak cell. */ +/* Accessing the components of a weak cell. These return `SCM_UNDEFINED' if + the car/cdr has been collected. */ #define SCM_WEAK_PAIR_WORD(_cell, _word) \ - ((SCM_WEAK_PAIR_WORD_DELETED_P ((_cell), (_word))) \ - ? SCM_BOOL_F : SCM_CAR (pair)) + (SCM_WEAK_PAIR_WORD_DELETED_P ((_cell), (_word)) \ + ? SCM_UNDEFINED \ + : SCM_CELL_OBJECT ((_cell), (_word))) #define SCM_WEAK_PAIR_CAR(_cell) (SCM_WEAK_PAIR_WORD ((_cell), 0)) #define SCM_WEAK_PAIR_CDR(_cell) (SCM_WEAK_PAIR_WORD ((_cell), 1)) diff --git a/libguile/win32-socket.c b/libguile/win32-socket.c index e845d886a..825b4c499 100644 --- a/libguile/win32-socket.c +++ b/libguile/win32-socket.c @@ -435,7 +435,7 @@ scm_i_socket_uncomment (char *line) while (end > line && (*end == '\r' || *end == '\n')) *end-- = '\0'; } - while (end > line && isspace (*end)) + while (end > line && isspace ((int) (*end))) *end-- = '\0'; return end; |