From 61218d193a20df95d34fa975b296448d382416a8 Mon Sep 17 00:00:00 2001 From: "Kaveh R. Ghazi" Date: Wed, 14 Apr 2004 14:32:55 +0000 Subject: [PATCH] builtins.c (fold_builtin_isdigit): New. * builtins.c (fold_builtin_isdigit): New. (fold_builtin): Handle BUILT_IN_ISDIGIT. * defaults.h: Add TARGET_DIGIT0 and sort. * doc/tm.texi: Add TARGET_BS and TARGET_DIGIT0. testsuite: * gcc.dg/torture/builtin-ctype-2.c: Test builtin isdigit. From-SVN: r80681 --- gcc/ChangeLog | 7 ++++++ gcc/builtins.c | 25 +++++++++++++++++++ gcc/defaults.h | 9 ++++--- gcc/doc/tm.texi | 13 +++++++--- gcc/testsuite/ChangeLog | 4 +++ .../gcc.dg/torture/builtin-ctype-2.c | 23 +++++++++++++++++ 6 files changed, 73 insertions(+), 8 deletions(-) diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 00c65114eb2..0312f280199 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,10 @@ +2004-04-14 Kaveh R. Ghazi + + * builtins.c (fold_builtin_isdigit): New. + (fold_builtin): Handle BUILT_IN_ISDIGIT. + * defaults.h: Add TARGET_DIGIT0 and sort. + * doc/tm.texi: Add TARGET_BS and TARGET_DIGIT0. + 2004-04-14 Kaveh R. Ghazi * builtins.c (fold_builtin_cabs, fold_builtin): Use diff --git a/gcc/builtins.c b/gcc/builtins.c index 184711bbc4b..e1ba8586a1c 100644 --- a/gcc/builtins.c +++ b/gcc/builtins.c @@ -6767,6 +6767,28 @@ fold_builtin_toascii (tree arglist) } } +/* Fold a call to builtin isdigit. */ + +static tree +fold_builtin_isdigit (tree arglist) +{ + if (! validate_arglist (arglist, INTEGER_TYPE, VOID_TYPE)) + return 0; + else + { + /* Transform isdigit(c) -> (unsigned)(c) - '0' <= 9. */ + /* According to the C standard, isdigit is unaffected by locale. */ + tree arg = TREE_VALUE (arglist); + arg = build1 (NOP_EXPR, unsigned_type_node, arg); + arg = build (MINUS_EXPR, unsigned_type_node, arg, + fold (build1 (NOP_EXPR, unsigned_type_node, + build_int_2 (TARGET_DIGIT0, 0)))); + arg = build (LE_EXPR, integer_type_node, arg, + fold (build1 (NOP_EXPR, unsigned_type_node, + build_int_2 (9, 0)))); + return fold (arg); + } +} /* Used by constant folding to eliminate some builtin calls early. EXP is the CALL_EXPR of a call to a builtin function. */ @@ -7257,6 +7279,9 @@ fold_builtin (tree exp) case BUILT_IN_TOASCII: return fold_builtin_toascii (arglist); + case BUILT_IN_ISDIGIT: + return fold_builtin_isdigit (arglist); + default: break; } diff --git a/gcc/defaults.h b/gcc/defaults.h index 5867291460b..b2384090849 100644 --- a/gcc/defaults.h +++ b/gcc/defaults.h @@ -39,12 +39,13 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #ifndef TARGET_BELL # define TARGET_BELL 007 # define TARGET_BS 010 -# define TARGET_TAB 011 -# define TARGET_NEWLINE 012 -# define TARGET_VT 013 -# define TARGET_FF 014 # define TARGET_CR 015 +# define TARGET_DIGIT0 060 # define TARGET_ESC 033 +# define TARGET_FF 014 +# define TARGET_NEWLINE 012 +# define TARGET_TAB 011 +# define TARGET_VT 013 #endif /* Store in OUTPUT a string (made with alloca) containing an diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 39ddcc39130..85515bf0b3c 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -1787,13 +1787,16 @@ of words in each data entry. @section Target Character Escape Sequences @cindex escape sequences -By default, GCC assumes that the C character escape sequences take on -their ASCII values for the target. If this is not correct, you must -explicitly define all of the macros below. All of them must evaluate -to constants; they are used in @code{case} statements. +By default, GCC assumes that the C character escape sequences and other +characters take on their ASCII values for the target. If this is not +correct, you must explicitly define all of the macros below. All of +them must evaluate to constants; they are used in @code{case} +statements. @findex TARGET_BELL +@findex TARGET_BS @findex TARGET_CR +@findex TARGET_DIGIT0 @findex TARGET_ESC @findex TARGET_FF @findex TARGET_NEWLINE @@ -1802,7 +1805,9 @@ to constants; they are used in @code{case} statements. @multitable {@code{TARGET_NEWLINE}} {Escape} {ASCII character} @item Macro @tab Escape @tab ASCII character @item @code{TARGET_BELL} @tab @kbd{\a} @tab @code{07}, @code{BEL} +@item @code{TARGET_BS} @tab @kbd{\b} @tab @code{08}, @code{BS} @item @code{TARGET_CR} @tab @kbd{\r} @tab @code{0D}, @code{CR} +@item @code{TARGET_DIGIT0} @tab @kbd{0} @tab @code{30}, @code{ZERO} @item @code{TARGET_ESC} @tab @kbd{\e}, @kbd{\E} @tab @code{1B}, @code{ESC} @item @code{TARGET_FF} @tab @kbd{\f} @tab @code{0C}, @code{FF} @item @code{TARGET_NEWLINE} @tab @kbd{\n} @tab @code{0A}, @code{LF} diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 178d8157c91..47f20116f35 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2004-04-14 Kaveh R. Ghazi + + * gcc.dg/torture/builtin-ctype-2.c: Test builtin isdigit. + 2004-04-13 Uros Bizjak : * gcc.dg/i386-387-1.c: Add new test for __builtin_tan. diff --git a/gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c b/gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c index a306bcc9dae..7046aad6562 100644 --- a/gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c +++ b/gcc/testsuite/gcc.dg/torture/builtin-ctype-2.c @@ -75,6 +75,29 @@ void test(int i) if (toascii(i) != (i & 0x7f)) link_failure_var(); + TEST_CTYPE_CST_TRUE (isdigit, '0'); + TEST_CTYPE_CST_TRUE (isdigit, '1'); + TEST_CTYPE_CST_TRUE (isdigit, '2'); + TEST_CTYPE_CST_TRUE (isdigit, '3'); + TEST_CTYPE_CST_TRUE (isdigit, '4'); + TEST_CTYPE_CST_TRUE (isdigit, '5'); + TEST_CTYPE_CST_TRUE (isdigit, '6'); + TEST_CTYPE_CST_TRUE (isdigit, '7'); + TEST_CTYPE_CST_TRUE (isdigit, '8'); + TEST_CTYPE_CST_TRUE (isdigit, '9'); + + TEST_CTYPE_CST_FALSE (isdigit, '0'-1); + TEST_CTYPE_CST_FALSE (isdigit, '9'+1); + TEST_CTYPE_CST_FALSE (isdigit, -1); + TEST_CTYPE_CST_FALSE (isdigit, 0); + TEST_CTYPE_CST_FALSE (isdigit, 255); + TEST_CTYPE_CST_FALSE (isdigit, 256); + TEST_CTYPE_CST_FALSE (isdigit, 10000); + TEST_CTYPE_CST_FALSE (isdigit, __INT_MAX__); + + /* This ctype call should transform into another expression. */ + if (isdigit(i) != ((unsigned)i - '0' <= 9)) + link_failure_var(); #endif /* __OPTIMIZE__ */ } -- 2.30.2