diff --git a/src/cc65/assignment.c b/src/cc65/assignment.c index ab501523d..2adb1c20e 100644 --- a/src/cc65/assignment.c +++ b/src/cc65/assignment.c @@ -345,7 +345,12 @@ static void OpAssignBitField (const GenDesc* Gen, ExprDesc* Expr, const char* Op /* Here we simply "wrap" the shift count around the width */ Expr2.IVal &= ExprBits - 1; - } + + /* Additional check for bit-fields */ + if (Expr2.IVal >= (long)Expr->Type->A.B.Width) { + Warning ("Shift count %ld >= width of bit-field", Expr2.IVal); + } + } } /* Adjust the types of the operands if needed */ @@ -549,6 +554,12 @@ static void OpAssignArithmetic (const GenDesc* Gen, ExprDesc* Expr, const char* /* Here we simply "wrap" the shift count around the width */ Expr2.IVal &= ExprBits - 1; + + /* Additional check for bit width */ + if (Expr2.IVal >= (long)BitSizeOf (Expr->Type)) { + Warning ("Shift count %ld >= width of %s", + Expr2.IVal, GetBasicTypeName (Expr->Type)); + } } } Gen->Func (Flags | CF_CONST, Expr2.IVal); diff --git a/src/cc65/shiftexpr.c b/src/cc65/shiftexpr.c index 48426f1f2..b8fb70434 100644 --- a/src/cc65/shiftexpr.c +++ b/src/cc65/shiftexpr.c @@ -160,6 +160,15 @@ void ShiftExpr (struct ExprDesc* Expr) /* Here we simply "wrap" the shift count around the width */ Expr2.IVal &= ExprBits - 1; + /* Additional check for bit-fields */ + if (IsTypeBitField (Expr->Type) && + Tok == TOK_SHR && + Expr2.IVal >= (long) Expr->Type->A.B.Width) { + if (!ED_IsUneval (Expr)) { + Warning ("Right-shift count %ld >= width of bit-field", Expr2.IVal); + } + } + /* If the shift count is zero, nothing happens. If the left hand ** side is a constant, the result is constant. */