Menu ▾ ▴

#4261 mcs51: Incorrect register allocation for _Bool with --stack-auto

open
nobody
None
MCS51
5
2026-11-19
2026-11-19
No

Description

When compiling with -mmcs51 --model-large --stack-auto, a local _Bool variable initialised from a cast of an unsigned char may be allocated to a general-purpose register instead of a bit register.

The register may also be assigned to a live range handled by block spilling (remainSpil). At the end of the block, the corresponding pop restores the spilled pointer value into that register, overwriting the _Bool variable before its next use. As a result, a subsequent function call receives a stale value.

Reproducer

The reproducer repro.c is attached. Compile it with:

sdcc -mmcs51 --model-large --stack-auto --opt-code-speed --std-c23 -c repro.c

Observed: The third argument passed to process() is 0 when a key is pressed.

Expected: The third argument should be 1.

Root Cause

In serialRegAssign(), the CAST case allows the result to reuse the register assigned to its operand without checking whether that register satisfies the result's register class requirements.

For a _Bool result, regType is REG_BIT. However, the current condition only checks whether the operand is allocated to a bit register; it does not check whether the result itself requires one.

The block-spill safety check is bypassed for REG_BIT values, allowing the _Bool result to be allocated to a GPR. That register is subsequently clobbered by the block-level push/pop used to preserve the pointer's live range.

Patch

The attached patch adds the missing register-class check in the CAST case, preventing the result from reusing an incompatible register.

The mcs51-large-stack-auto regression tests show no new failures with this patch.

2 Attachments

Discussion


Log in to post a comment.