1: Sample code that reproduces the problem.
int object;
int test(void)
{
object = 42;
return *(volatile int *)&object;
}
Save as volatile-read-stm8.c.
2: Exact command used to run SDCC on this sample code.
sdcc -mstm8 --c1mode -o volatile-read-stm8.asm < volatile-read-stm8.c
The input contains no headers or macros. Add --no-peep to compare the output
without peephole optimisation.
3: SDCC versions tested.
SDCC 4.6.4 r17064, locally built from unmodified compiler sources; the official
SDCC 4.6.4 #17038 snapshot; and SDCC 4.2.0 #13081, all on Linux.
4: Copy of the incorrect output, or a clear description of the observed versus expected behaviour.
The generated function stores 42 and returns without reading object:
ldw x, #0x002a
ldw _object+0, x
ret
I expected the return expression to read object through its volatile-qualified
lvalue. With --no-peep, the read is present:
ldw x, #0x002a
ldw _object+0, x
ldw x, _object+0
ret
The same omission occurs in SDCC 4.2.0 #13081. A corresponding example storing
and then reading an int * through int *volatile * also loses its read in
r17064.
The problem is reproduced with unmodified r17064 sources, before the
Z80-family symbol-lookup candidate. It is not introduced by that candidate.
Possibly related to [bugs:#4156]. In this example, --no-peep retains the
required read; the loss can therefore be isolated to peephole optimisation.
Attaching a candidate bugfix patch generated in discussion with Codex. It preserves volatile accesses through peephole replacements. Both
lineElem_tandlineNodegain aisVolatileAccessflag. Before peephole rewriting,peepHole()examines each line’s associatediCodethroughlineNode::ic. A new helper function,hasVolatileAccess(), checks the existing operands’isvolatilefields and, for pointer reads or writes, the pointed-to type’svolatilequalifier. If it finds avolatileaccess,peepHole()setslineNode::isVolatileAccess.replaceRule()then preserves that information when replacing assembly lines, independently of their subsequenticassociations.This is expected to fix [bugs:#4202], [bugs:#4203] and [bugs:#4204].
Related
Bugs:
#4202Bugs:
#4203Bugs:
#4204Last edit: Christopher Bazley 2026-10-22
Here's the same candidate
volatile-preservation fix, rebased onto [r17071].Related
Commit: [r17071]
The idea looks good. Some details:
Thanks for the review, @spth. Please take a look at this revised patch when you have time.
Looks good to me.
Fixed by [r17075]
Related
Commit: [r17075]