diff --git a/libguile/tags.h b/libguile/tags.h index 3294533..25aea09 100644 --- a/libguile/tags.h +++ b/libguile/tags.h @@ -516,12 +516,47 @@ 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_lisp_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_lisp_nil and + * scm_is_true_and_not_lisp_nil) + * + * - SCM_ELISP_NIL and SCM_EOL must differ in exactly one bit position. + * (used to implement scm_is_null_or_lisp_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_lisp_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) +#define SCM_XXX_ANOTHER_LISP_FALSE_DONT_USE SCM_MAKIFLAG (2) +#define SCM_EOL SCM_MAKIFLAG (3) +#define SCM_BOOL_T SCM_MAKIFLAG (4) +#define SCM_XXX_ANOTHER_BOOLEAN_DONT_USE SCM_MAKIFLAG (5) + +#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 @@ -531,14 +566,50 @@ 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_lisp_nil, + * scm_is_false_or_lisp_nil, + * scm_is_true_and_not_lisp_nil, + * scm_is_lisp_false, + * scm_is_lisp_true, + * scm_is_bool_and_not_lisp_nil (aka scm_is_bool) + * scm_is_bool_or_lisp_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. + */ +#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)))) /* Evaluator byte codes ('immediate symbols'). These constants are used only diff --git a/libguile/print.c b/libguile/print.c index 6c44d59..fd65bf9 100644 --- a/libguile/print.c +++ b/libguile/print.c @@ -61,18 +61,17 @@ static const char *iflagnames[] = { "#f", + "#nil", /* Elisp nil value. Should print from elisp as symbol `nil'. */ + "#", + "()", "#t", + "#", + "#", "#", "#", - "()", - "#", /* Unbound slot marker for GOOPS. For internal use in GOOPS only. */ "#", - - /* 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"); diff --git a/libguile/boolean.h b/libguile/boolean.h index 5a83797..2c480c0 100644 --- a/libguile/boolean.h +++ b/libguile/boolean.h @@ -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_lisp_nil(x) (scm_is_eq ((x), SCM_BOOL_F)) +#define scm_is_true_or_lisp_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_lisp_nil(x) (scm_is_eq ((x), SCM_BOOL_F)) +#define scm_is_true_assume_not_lisp_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_lisp_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_BOOL_F)) +#else +# define scm_is_false_or_lisp_nil(x) (scm_is_false_assume_not_lisp_nil (x)) +#endif +#define scm_is_true_and_not_lisp_nil(x) (!scm_is_false_or_lisp_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_lisp_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_lisp_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_lisp_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_BOOL_T, SCM_ELISP_NIL)) +#else +# define scm_is_bool_or_lisp_nil(x) (scm_is_bool_and_not_lisp_nil (x)) +#endif + +#define scm_is_bool_and_not_lisp_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_lisp_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/boolean.c b/libguile/boolean.c index d79bf79..9a4f896 100644 --- a/libguile/boolean.c +++ b/libguile/boolean.c @@ -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_lisp_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_lisp_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_lisp_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/pairs.h b/libguile/pairs.h index a6d44d2..9c75709 100644 --- a/libguile/pairs.h +++ b/libguile/pairs.h @@ -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_lisp_nil if it's important (for correctness) + * that %nil must NOT be considered null. + */ +#define scm_is_null_and_not_lisp_nil(x) (scm_is_eq ((x), SCM_EOL)) + +/* + * Use scm_is_null_assume_not_lisp_nil if %nil will never be tested, + * for increased efficiency. + */ +#define scm_is_null_assume_not_lisp_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_lisp_nil(x) \ + (SCM_MATCHES_BITS_IN_COMMON ((x), SCM_ELISP_NIL, SCM_EOL)) +#else +# define scm_is_null_or_lisp_nil(x) (scm_is_null_assume_not_lisp_nil (x)) +#endif + +/* XXX Should scm_is_null treat %nil as null by default? */ +#define scm_is_null(x) (scm_is_null_and_not_lisp_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/pairs.c b/libguile/pairs.c index aaaeb11..49eef82 100644 --- a/libguile/pairs.c +++ b/libguile/pairs.c @@ -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_lisp_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/lang.h b/libguile/lang.h index 47128de..985f5ef 100644 --- a/libguile/lang.h +++ b/libguile/lang.h @@ -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_lisp_nil (x)) #endif /* SCM_LANG_H */ diff --git a/libguile/eval.i.c b/libguile/eval.i.c index 99aa265..9da1664 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_lisp_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_lisp_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_lisp_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_lisp_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_lisp_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_lisp_nil (val)) RETURN (val); else x = SCM_CDR (x);