cc64 is a small-C compiler written in Forth, hosted on the Commodore C64, Plus4 and C16 with 64k, and on the Commander X16. It is targeting the 6502 CPU.
Other
96
stars
6
forks
source link
Wrongly removed "read from memory" on inline optimization #22
When activating the "Optimize code, inline standard funtions" (-Os) or the "Optimize code, inline more code" (-Oi) option, if a C source code has (in sequence) a direct write statement and a direct read statement on the same RAM memory location, the reading step is omitted.
The problem arises if the memory is accessed using the address directly. It does not occur if the address to read and write from is stored in a (temporary) pointer variable, or if the optimization is disabled.
Currently, the only workaround to avoid allocating an additional variable is to assign a value of 0 to the (destination) memory location: this prevents the optimizer from exploiting the value just written.
However, I believe there should be a way to indicate that (specific?) memory locations can be "volatile" so that the optimizer cannot make any assumptions about the reusability of their value.
When activating the "Optimize code, inline standard funtions" (
-Os
) or the "Optimize code, inline more code" (-Oi
) option, if a C source code has (in sequence) a direct write statement and a direct read statement on the same RAM memory location, the reading step is omitted.The problem arises if the memory is accessed using the address directly. It does not occur if the address to read and write from is stored in a (temporary) pointer variable, or if the optimization is disabled.
Currently, the only workaround to avoid allocating an additional variable is to assign a value of 0 to the (destination) memory location: this prevents the optimizer from exploiting the value just written.
However, I believe there should be a way to indicate that (specific?) memory locations can be "volatile" so that the optimizer cannot make any assumptions about the reusability of their value.
Example:
This is the correct assembly output (without
-Osir
optimizations):This is the (wrong) assembly output:
This is the workaround:
Command line used to compile: