From 5294a698ee4cea2593f685b81b3b623e72b66518 Mon Sep 17 00:00:00 2001 From: borutr Date: Mon, 23 Mar 2009 06:21:41 +0000 Subject: [PATCH] * src/SDCCval.c, src/SDCCmain.c, device/include/limits.h, device/include/pic/limits.h, device/include/pic16/limits.h, device/lib/_fs2schar.c, device/lib/pic/libsdcc/fs2schar.c, device/lib/pic16/libsdcc/float/fs2schar.c, doc/sdccman.lyx, support/regression/tests/funsigned-char.c: applied patch 2704189: character constant sign fix thanks to Robert Larice git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@5421 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 10 +++ device/include/limits.h | 25 +++++--- device/include/pic/limits.h | 29 +++++---- device/include/pic16/limits.h | 29 +++++---- device/lib/_fs2schar.c | 78 +++++++++++------------ device/lib/pic/libsdcc/fs2schar.c | 12 ++-- device/lib/pic16/libsdcc/float/fs2schar.c | 12 ++-- doc/sdccman.lyx | 43 +++++++++++++ src/SDCCmain.c | 3 + src/SDCCval.c | 14 +++- support/regression/tests/funsigned-char.c | 50 +++++++++++++++ 11 files changed, 217 insertions(+), 88 deletions(-) create mode 100644 support/regression/tests/funsigned-char.c diff --git a/ChangeLog b/ChangeLog index c5b7c7e3..1cbf384e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2009-03-23 Borut Razem + + * src/SDCCval.c, src/SDCCmain.c, device/include/limits.h, + device/include/pic/limits.h, device/include/pic16/limits.h, + device/lib/_fs2schar.c, device/lib/pic/libsdcc/fs2schar.c, + device/lib/pic16/libsdcc/float/fs2schar.c, doc/sdccman.lyx, + support/regression/tests/funsigned-char.c: + applied patch 2704189: character constant sign fix + thanks to Robert Larice + 2009-03-22 Borut Razem * Small Device C Compiler 2.9.0 released diff --git a/device/include/limits.h b/device/include/limits.h index 23cbc60c..ba67bb27 100644 --- a/device/include/limits.h +++ b/device/include/limits.h @@ -1,37 +1,42 @@ /*------------------------------------------------------------------------- - limits.h - ANSI defines constants for sizes of integral types - + limits.h - ANSI defines constants for sizes of integral types + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + This program 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 General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - + In other words, you are welcome to use, share and improve this program. You are forbidden to forbid anyone else to use, share and improve - what you give them. Help stamp out software-hoarding! + what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ #ifndef __SDC51_LIMITS_H #define __SDC51_LIMITS_H 1 #define CHAR_BIT 8 /* bits in a char */ -#define CHAR_MAX 127 -#define CHAR_MIN -128 -#define SCHAR_MAX CHAR_MAX -#define SCHAR_MIN CHAR_MIN +#define SCHAR_MAX 127 +#define SCHAR_MIN -128 #define UCHAR_MAX 0xff #define UCHAR_MIN 0 +#ifdef SDCC_CHAR_UNSIGNED +#define CHAR_MAX UCHAR_MAX +#define CHAR_MIN UCHAR_MIN +#else +#define CHAR_MAX SCHAR_MAX +#define CHAR_MIN SCHAR_MIN +#endif #define INT_MIN -32768 #define INT_MAX 32767 #define SHRT_MAX INT_MAX diff --git a/device/include/pic/limits.h b/device/include/pic/limits.h index c928ff45..8ab7d52e 100644 --- a/device/include/pic/limits.h +++ b/device/include/pic/limits.h @@ -1,8 +1,8 @@ /*------------------------------------------------------------------------- - limits.h - ANSI defines constants for sizes of integral types - - Adopted for the pic16 port by Vangelis Rokas - [ vrokas at otenet.gr ] 2004 + limits.h - ANSI defines constants for sizes of integral types + + Adopted for the pic16 port by Vangelis Rokas + [ vrokas at otenet.gr ] 2004 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) @@ -10,31 +10,36 @@ under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + This program 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 General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - + In other words, you are welcome to use, share and improve this program. You are forbidden to forbid anyone else to use, share and improve - what you give them. Help stamp out software-hoarding! + what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ #ifndef __LIMITS_H #define __LIMITS_H 1 #define CHAR_BIT 8 /* bits in a char */ -#define CHAR_MAX 127 -#define CHAR_MIN -128 -#define SCHAR_MAX CHAR_MAX -#define SCHAR_MIN CHAR_MIN +#define SCHAR_MAX 127 +#define SCHAR_MIN -128 #define UCHAR_MAX 0xff #define UCHAR_MIN 0 +#ifdef SDCC_CHAR_UNSIGNED +#define CHAR_MAX UCHAR_MAX +#define CHAR_MIN UCHAR_MIN +#else +#define CHAR_MAX SCHAR_MAX +#define CHAR_MIN SCHAR_MIN +#endif #define INT_MIN -32768 #define INT_MAX 32767 #define SHRT_MAX INT_MAX diff --git a/device/include/pic16/limits.h b/device/include/pic16/limits.h index c928ff45..8ab7d52e 100644 --- a/device/include/pic16/limits.h +++ b/device/include/pic16/limits.h @@ -1,8 +1,8 @@ /*------------------------------------------------------------------------- - limits.h - ANSI defines constants for sizes of integral types - - Adopted for the pic16 port by Vangelis Rokas - [ vrokas at otenet.gr ] 2004 + limits.h - ANSI defines constants for sizes of integral types + + Adopted for the pic16 port by Vangelis Rokas + [ vrokas at otenet.gr ] 2004 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) @@ -10,31 +10,36 @@ under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + This program 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 General Public License for more details. - + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - + In other words, you are welcome to use, share and improve this program. You are forbidden to forbid anyone else to use, share and improve - what you give them. Help stamp out software-hoarding! + what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ #ifndef __LIMITS_H #define __LIMITS_H 1 #define CHAR_BIT 8 /* bits in a char */ -#define CHAR_MAX 127 -#define CHAR_MIN -128 -#define SCHAR_MAX CHAR_MAX -#define SCHAR_MIN CHAR_MIN +#define SCHAR_MAX 127 +#define SCHAR_MIN -128 #define UCHAR_MAX 0xff #define UCHAR_MIN 0 +#ifdef SDCC_CHAR_UNSIGNED +#define CHAR_MAX UCHAR_MAX +#define CHAR_MIN UCHAR_MIN +#else +#define CHAR_MAX SCHAR_MAX +#define CHAR_MIN SCHAR_MIN +#endif #define INT_MIN -32768 #define INT_MAX 32767 #define SHRT_MAX INT_MAX diff --git a/device/lib/_fs2schar.c b/device/lib/_fs2schar.c index b9b6ac6d..70b53288 100644 --- a/device/lib/_fs2schar.c +++ b/device/lib/_fs2schar.c @@ -27,48 +27,48 @@ // char __fs2schar (float x) static void dummy(void) __naked { - __asm - .globl ___fs2schar + __asm + .globl ___fs2schar ___fs2schar: - lcall ___fs2slong - jnz fs2schar_not_zero - mov a, dpl - orl a, dph - orl a, b - jnz fs2schar_clr_a - ret + lcall ___fs2slong + jnz fs2schar_not_zero + mov a, dpl + orl a, dph + orl a, b + jnz fs2schar_clr_a + ret fs2schar_clr_a: - clr a + clr a fs2schar_not_zero: - jnb sign_a, fs2schar_pos + jnb sign_a, fs2schar_pos fs2schar_neg: - cpl a - jnz fs2schar_maxval_neg - mov a, b - cpl a - jnz fs2schar_maxval_neg - mov a, dph - cpl a - jnz fs2schar_maxval_neg - mov a, dpl - jnb acc.7, fs2schar_maxval_neg - ret + cpl a + jnz fs2schar_maxval_neg + mov a, b + cpl a + jnz fs2schar_maxval_neg + mov a, dph + cpl a + jnz fs2schar_maxval_neg + mov a, dpl + jnb acc.7, fs2schar_maxval_neg + ret fs2schar_maxval_neg: - mov dpl, #0x80 - ret + mov dpl, #0x80 + ret fs2schar_pos: - jnz fs2schar_maxval_pos - mov a, b - jnz fs2schar_maxval_pos - mov a, dph - jnz fs2schar_maxval_pos - mov a, dpl - jb acc.7, fs2schar_maxval_pos - ret + jnz fs2schar_maxval_pos + mov a, b + jnz fs2schar_maxval_pos + mov a, dph + jnz fs2schar_maxval_pos + mov a, dpl + jb acc.7, fs2schar_maxval_pos + ret fs2schar_maxval_pos: - mov dpl, #0x7F - ret - __endasm; + mov dpl, #0x7F + ret + __endasm; } #else @@ -77,10 +77,10 @@ fs2schar_maxval_pos: signed char __fs2schar (float f) { signed long sl=__fs2slong(f); - if (sl>=CHAR_MAX) - return CHAR_MAX; - if (sl<=CHAR_MIN) - return -CHAR_MIN; + if (sl>=SCHAR_MAX) + return SCHAR_MAX; + if (sl<=SCHAR_MIN) + return -SCHAR_MIN; return sl; } diff --git a/device/lib/pic/libsdcc/fs2schar.c b/device/lib/pic/libsdcc/fs2schar.c index a482b2d8..325d30b6 100644 --- a/device/lib/pic/libsdcc/fs2schar.c +++ b/device/lib/pic/libsdcc/fs2schar.c @@ -1,8 +1,8 @@ /*------------------------------------------------------------------------- - fs2schar.c :- + fs2schar.c :- Adopted for float and pic16 port by - - Vangelis Rokas, vrokas@otenet.gr (2004) + - Vangelis Rokas, vrokas@otenet.gr (2004) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the @@ -36,9 +36,9 @@ signed char __fs2schar (float f) _FS_REENTRANT { signed long sl=__fs2slong(f); - if (sl>=CHAR_MAX) - return CHAR_MAX; - if (sl<=CHAR_MIN) - return -CHAR_MIN; + if (sl>=SCHAR_MAX) + return SCHAR_MAX; + if (sl<=SCHAR_MIN) + return -SCHAR_MIN; return sl; } diff --git a/device/lib/pic16/libsdcc/float/fs2schar.c b/device/lib/pic16/libsdcc/float/fs2schar.c index a482b2d8..325d30b6 100644 --- a/device/lib/pic16/libsdcc/float/fs2schar.c +++ b/device/lib/pic16/libsdcc/float/fs2schar.c @@ -1,8 +1,8 @@ /*------------------------------------------------------------------------- - fs2schar.c :- + fs2schar.c :- Adopted for float and pic16 port by - - Vangelis Rokas, vrokas@otenet.gr (2004) + - Vangelis Rokas, vrokas@otenet.gr (2004) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the @@ -36,9 +36,9 @@ signed char __fs2schar (float f) _FS_REENTRANT { signed long sl=__fs2slong(f); - if (sl>=CHAR_MAX) - return CHAR_MAX; - if (sl<=CHAR_MIN) - return -CHAR_MIN; + if (sl>=SCHAR_MAX) + return SCHAR_MAX; + if (sl<=SCHAR_MIN) + return -SCHAR_MIN; return sl; } diff --git a/doc/sdccman.lyx b/doc/sdccman.lyx index e3a92c5c..4d528bf0 100644 --- a/doc/sdccman.lyx +++ b/doc/sdccman.lyx @@ -24296,6 +24296,49 @@ status collapsed \begin_inset Text +\begin_layout Standard +SDCC_CHAR_UNSIGNED +\begin_inset LatexCommand index +name "SDCC\\_CHAR\\_UNSIGNED" + +\end_inset + + +\end_layout + +\end_inset + + +\begin_inset Text + +\begin_layout Standard +when +\emph on +- +\begin_inset ERT +status collapsed + +\begin_layout Standard + + +\backslash +/ +\end_layout + +\end_inset + +-funsigned-char +\emph default + option is used +\end_layout + +\end_inset + + + + +\begin_inset Text + \begin_layout Standard SDCC_STACK_TENBIT \begin_inset LatexCommand index diff --git a/src/SDCCmain.c b/src/SDCCmain.c index 12909d3b..8b1194ba 100644 --- a/src/SDCCmain.c +++ b/src/SDCCmain.c @@ -1988,6 +1988,9 @@ preProcess (char **envp) if (options.noOverlay) addSet(&preArgvSet, Safe_strdup("-DSDCC_NOOVERLAY")); + if (options.unsigned_char) + addSet(&preArgvSet, Safe_strdup("-DSDCC_CHAR_UNSIGNED")); + /* set the macro for large model */ switch (options.model) { diff --git a/src/SDCCval.c b/src/SDCCval.c index de08d2a6..e520aff5 100644 --- a/src/SDCCval.c +++ b/src/SDCCval.c @@ -840,10 +840,18 @@ value *constCharVal (unsigned char v) val->type = val->etype = newLink (SPECIFIER); /* create the specifier */ SPEC_SCLS (val->type) = S_LITERAL; - /* let's start with a signed char */ + SPEC_NOUN (val->type) = V_CHAR; - SPEC_USIGN (val->type) = 1; - SPEC_CVAL (val->type).v_uint = v; + + if (options.unsigned_char) + { + SPEC_USIGN (val->type) = 1; + SPEC_CVAL (val->type).v_uint = (unsigned char) v; + } + else + { + SPEC_CVAL (val->type).v_int = (signed char) v; + } return val; } diff --git a/support/regression/tests/funsigned-char.c b/support/regression/tests/funsigned-char.c new file mode 100644 index 00000000..90d53828 --- /dev/null +++ b/support/regression/tests/funsigned-char.c @@ -0,0 +1,50 @@ +/* + * check for the correct signness of a char constant + * (signed versus unsigned) + * (indirect via integer promotion) + * + * Note, the check for the --funsigned-char must be invoked by hand + * see the following emacs sexp + * (compile "SDCCFLAGS=--funsigned-char make -C .. ALL_TESTS='./tests/funsigned-char.c'") + * (compile "make -C .. ALL_TESTS='./tests/funsigned-char.c'") + * + */ + +#include +#include +#include + +int glb_schar_to_int = ~ (signed char) '\200'; +int glb_uchar_to_int = ~ (unsigned char) '\200'; +int glb_char_to_int = ~ '\200'; + +int tst_schar_to_int() { return ~ (signed char) '\200'; } +int tst_uchar_to_int() { return ~ (unsigned char) '\200'; } +int tst_char_to_int() { return ~ '\200'; } + + +void +testBug(void) +{ +#ifdef SDCC_CHAR_UNSIGNED + ASSERT(CHAR_MAX == 255); + ASSERT(CHAR_MIN == 0); +#else + ASSERT(CHAR_MAX == 127); + ASSERT(CHAR_MIN == -128); +#endif + + ASSERT(tst_uchar_to_int() == -129); + ASSERT(glb_uchar_to_int == -129); + + ASSERT(tst_schar_to_int() == 127); + ASSERT(glb_schar_to_int == 127); + +#ifdef SDCC_CHAR_UNSIGNED + ASSERT(tst_char_to_int() == -129); + ASSERT(glb_char_to_int == -129); +#else + ASSERT(tst_char_to_int() == 127); + ASSERT(glb_char_to_int == 127); +#endif +} -- 2.30.2