From: bernhardheld Date: Tue, 29 Nov 2005 21:57:12 +0000 (+0000) Subject: * src/SDCCast.c (decorateType): try to optimize literals before promotion X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=7b476fe7d47fe2b6d3d23dfbd0a5dd0f75fc2a2f;p=fw%2Fsdcc * src/SDCCast.c (decorateType): try to optimize literals before promotion git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4000 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/src/SDCCast.c b/src/SDCCast.c index 2a0451b4..2c98465f 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -3957,9 +3957,30 @@ decorateType (ast * tree, RESULT_TYPE resultType) if (IS_CHAR (RTYPE (tree)) && IS_CHAR (LTYPE (tree)) && (IS_UNSIGNED (RTYPE (tree)) != IS_UNSIGNED (LTYPE (tree)))) { - werror (W_CMP_SU_CHAR); - tree->left = addCast (tree->left , RESULT_TYPE_INT, TRUE); - tree->right = addCast (tree->right, RESULT_TYPE_INT, TRUE); + /* Literals are 'optimized' to 'unsigned char'. Try to figure out, + if it's possible to use a 'signed char' */ + + /* is left a 'unsigned char'? */ + if (IS_LITERAL (RTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)) && + /* the value range of a 'unsigned char' is 0...255; + if the actual value is < 128 it can be changed to signed */ + (int) floatFromVal (valFromType (RETYPE (tree))) < 128) + { + /* now we've got 2 'signed char'! */ + SPEC_USIGN (RETYPE (tree)) = 0; + } + /* same test for the left operand: */ + else if (IS_LITERAL (LTYPE (tree)) && IS_UNSIGNED (LTYPE (tree)) && + (int) floatFromVal (valFromType (LETYPE (tree))) < 128) + { + SPEC_USIGN (LETYPE (tree)) = 0; + } + else + { + werror (W_CMP_SU_CHAR); + tree->left = addCast (tree->left , RESULT_TYPE_INT, TRUE); + tree->right = addCast (tree->right, RESULT_TYPE_INT, TRUE); + } } LRVAL (tree) = RRVAL (tree) = 1;