]> git.sur5r.net Git - cc65/commitdiff
Fixed a bug in sign extension of constant values
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 4 Mar 2003 10:46:27 +0000 (10:46 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Tue, 4 Mar 2003 10:46:27 +0000 (10:46 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@1998 b7a2c559-68d2-44c3-8de9-860c34a00d81

src/cc65/expr.c
src/cc65/typecast.c

index e5e21e3ce796c9831b388aaf05413a54e6b864bc..df1ef8baa014446b534d6d7191f6e62f4d673d8e 100644 (file)
@@ -365,7 +365,7 @@ static void LoadConstant (unsigned Flags, ExprDesc* Expr)
 
 
 
-static int kcalc (int tok, long val1, long val2)
+static int kcalc (token_t tok, long val1, long val2)
 /* Calculate an operation with left and right operand constant. */
 {
     switch (tok) {
@@ -554,7 +554,7 @@ static unsigned FunctionParamList (FuncDesc* Func)
  * each parameter separately, or creating the parameter frame once and then
  * storing into this frame.
  * The function returns the size of the parameters pushed.
- */      
+ */
 {
     ExprDesc lval;
 
index 4984192f4f51f25a43d52a9242546209db68494d..ab410f66824ddcd84993a90713d5afb58b69e61a 100644 (file)
@@ -7,8 +7,8 @@
 /*                                                                           */
 /*                                                                           */
 /* (C) 2002      Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
+/*               Römerstrasse 52                                             */
+/*               D-70794 Filderstadt                                         */
 /* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
@@ -144,24 +144,27 @@ int TypeCast (ExprDesc* lval)
                 unsigned OldBits = OldSize * 8;
                 unsigned NewBits = NewSize * 8;
 
+                /* Remember if the old value was negative */
+                int Negative = (lval->ConstVal & (0x01UL << (OldBits-1))) != 0UL;
+
                 /* Check if the new datatype will have a smaller range */
                 if (NewBits <= OldBits) {
 
                     /* Cut the value to the new size */
                     lval->ConstVal &= (0xFFFFFFFFUL >> (32 - NewBits));
 
-                    /* If the new type is signed, sign extend the value */
-                    if (!IsSignUnsigned (NewType)) {
+                    /* If the new type is signed and negative, sign extend the 
+                     * value 
+                     */
+                    if (Negative && !IsSignUnsigned (NewType)) {
                         lval->ConstVal |= ((~0L) << NewBits);
                     }
 
                 } else {
 
                     /* Sign extend the value if needed */
-                    if (!IsSignUnsigned (OldType) && !IsSignUnsigned (NewType)) {
-                        if (lval->ConstVal & (0x01UL << (OldBits-1))) {
-                            lval->ConstVal |= ((~0L) << OldBits);
-                        }
+                    if (Negative && !IsSignUnsigned (OldType) && !IsSignUnsigned (NewType)) {
+                        lval->ConstVal |= ((~0L) << OldBits);
                     }
                 }