Handle bit-field test after shift/mask

Previously, bit-field tests were incorrectly combined with load in
`if (x.bitfield)`.  Delay the test until after the shift/mask
is done.  Still combine tests with load if no shift/mask is required.

Fixes #1139
This commit is contained in:
Jesse Rosenstock
2020-07-31 08:41:05 +02:00
committed by Oliver Schmidt
parent d0c7108dcf
commit 847982c6bf
3 changed files with 409 additions and 73 deletions

View File

@@ -33,6 +33,8 @@
#include <limits.h>
/* cc65 */
#include "codegen.h"
#include "error.h"
@@ -113,13 +115,23 @@ void LoadExpr (unsigned Flags, struct ExprDesc* Expr)
** field is completely contained in the lower byte, we will throw away
** the high byte anyway and may therefore load just the low byte.
*/
unsigned EndBit = 0; /* End bit for bit-fields, or zero if non-bit-field. */
int AdjustBitField = 0;
if (ED_IsBitField (Expr)) {
Flags |= (Expr->BitOffs + Expr->BitWidth <= CHAR_BITS) ? CF_CHAR : CF_INT;
EndBit = Expr->BitOffs + Expr->BitWidth;
AdjustBitField = Expr->BitOffs != 0 || (EndBit != CHAR_BITS && EndBit != INT_BITS);
Flags |= (EndBit <= CHAR_BITS) ? CF_CHAR : CF_INT;
Flags |= CF_UNSIGNED;
} else if ((Flags & CF_TYPEMASK) == 0) {
Flags |= TypeOf (Expr->Type);
}
if (ED_NeedsTest (Expr)) {
/* Setting CF_TEST will cause the load to perform optimizations and not actually load all
** bits of the bit-field, instead just computing the condition codes. Therefore, if
** adjustment is required, we do not set CF_TEST here, but handle it below.
*/
if (ED_NeedsTest (Expr) && !AdjustBitField) {
Flags |= CF_TEST;
}
@@ -188,34 +200,57 @@ void LoadExpr (unsigned Flags, struct ExprDesc* Expr)
Internal ("Invalid location in LoadExpr: 0x%04X", ED_GetLoc (Expr));
}
/* Handle bit fields. The actual type may have been casted or
** converted, so be sure to always use unsigned ints for the
** operations.
/* Handle bit fields if necessary. The actual type may have been casted or converted,
** so be sure to always use unsigned ints for the operations.
*/
if (ED_IsBitField (Expr)) {
if (AdjustBitField) {
/* If the field was loaded as a char, force the shift/mask ops to be char ops.
** If it is a char, the load has already put 0 in the upper byte, so that can remain.
** CF_FORCECHAR does nothing if the type is not CF_CHAR.
*/
unsigned F = Flags | CF_FORCECHAR | CF_CONST;
/* Shift right by the bit offset */
g_asr (F, Expr->BitOffs);
/* Since we have now shifted down, we can do char ops as long as the width fits in
** a char.
/* We always need to do something with the low byte, so there is no opportunity
** for optimization by skipping it.
*/
if (Expr->BitWidth <= CHAR_BITS) {
F |= CF_CHAR;
}
CHECK (Expr->BitOffs < CHAR_BITS);
/* And by the width if the field doesn't end on a char or int boundary. If it does
** end on a boundary, then zeros have already been shifted in. g_and emits no code
** if the mask is all ones.
*/
if (Expr->BitOffs + Expr->BitWidth != CHAR_BITS &&
Expr->BitOffs + Expr->BitWidth != INT_BITS) {
g_and (F, (0x0001U << Expr->BitWidth) - 1U);
if (ED_NeedsTest (Expr)) {
/* If we need to do a test, then we avoid shifting (ASR only shifts one bit
** at a time, so is slow) and just AND with the appropriate mask, then test
** the result of that.
*/
/* Avoid overly large shift. */
if (EndBit == sizeof (unsigned long) * CHAR_BIT) {
g_and (F, (~0UL << Expr->BitOffs));
} else {
g_and (F, ((1UL << EndBit) - 1) & (~0UL << Expr->BitOffs));
}
/* TODO: When long bit-fields are supported, an optimization to test only 3 bytes
** when EndBit <= 24 is possible.
*/
g_test (F);
ED_TestDone (Expr);
} else {
/* Shift right by the bit offset; no code is emitted if BitOffs is zero */
g_asr (F, Expr->BitOffs);
/* Since we have now shifted down, we can do char ops as long as the width fits in
** a char.
*/
if (Expr->BitWidth <= CHAR_BITS) {
F |= CF_CHAR;
}
/* And by the width if the field doesn't end on a char or int boundary.
** If it does end on a boundary, then zeros have already been shifted in.
** g_and emits no code if the mask is all ones.
*/
if (EndBit != CHAR_BITS && EndBit != INT_BITS) {
g_and (F, (0x0001U << Expr->BitWidth) - 1U);
}
}
}