1
0
mirror of https://github.com/cc65/cc65.git synced 2024-12-28 06:30:16 +00:00

Fixed a bug in sign extension of constant values

git-svn-id: svn://svn.cc65.org/cc65/trunk@1998 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz 2003-03-04 10:46:27 +00:00
parent 0fd653c416
commit c008e555b2
2 changed files with 13 additions and 10 deletions

View 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. */ /* Calculate an operation with left and right operand constant. */
{ {
switch (tok) { switch (tok) {

View File

@ -7,8 +7,8 @@
/* */ /* */
/* */ /* */
/* (C) 2002 Ullrich von Bassewitz */ /* (C) 2002 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Römerstrasse 52 */
/* D-70597 Stuttgart */ /* D-70794 Filderstadt */
/* EMail: uz@cc65.org */ /* EMail: uz@cc65.org */
/* */ /* */
/* */ /* */
@ -144,26 +144,29 @@ int TypeCast (ExprDesc* lval)
unsigned OldBits = OldSize * 8; unsigned OldBits = OldSize * 8;
unsigned NewBits = NewSize * 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 */ /* Check if the new datatype will have a smaller range */
if (NewBits <= OldBits) { if (NewBits <= OldBits) {
/* Cut the value to the new size */ /* Cut the value to the new size */
lval->ConstVal &= (0xFFFFFFFFUL >> (32 - NewBits)); lval->ConstVal &= (0xFFFFFFFFUL >> (32 - NewBits));
/* If the new type is signed, sign extend the value */ /* If the new type is signed and negative, sign extend the
if (!IsSignUnsigned (NewType)) { * value
*/
if (Negative && !IsSignUnsigned (NewType)) {
lval->ConstVal |= ((~0L) << NewBits); lval->ConstVal |= ((~0L) << NewBits);
} }
} else { } else {
/* Sign extend the value if needed */ /* Sign extend the value if needed */
if (!IsSignUnsigned (OldType) && !IsSignUnsigned (NewType)) { if (Negative && !IsSignUnsigned (OldType) && !IsSignUnsigned (NewType)) {
if (lval->ConstVal & (0x01UL << (OldBits-1))) {
lval->ConstVal |= ((~0L) << OldBits); lval->ConstVal |= ((~0L) << OldBits);
} }
} }
}
} else { } else {