Ticket #504 (Fixed)Thu Feb 11 02:02:20 UTC 2021
cc 5.85: Compiler considers local variable not accessed by inline assembler BL
Reported by: | Timothy Baldwin (184) | Severity: | Normal |
Part: | RISC OS: C/C++ toolchain | Release: | |
Milestone: | Status | Fixed |
Details by Timothy Baldwin (184):
Consider this program which erroneously prints “FAIL”:
<code><pre>#include <stdio.h>
void set_x(int *x) {
*x = 1;
}
int test(void) {
int x = 0;
__asm {
MOV r0, &x
BL set_x, {r0}, {}, {r0-r3, ip, lr, psr}
}
return x;
}
int main(void) {
int x = test();
puts(x ? “OK” : “FAIL”);
return !x;
}</pre></code>
The function test compiles to:
<code><pre>test
STMDB sp!,{v1,lr}
SUB sp,sp,#4
MOV a1,sp
BL set_x
MOV a1,#0
ADD sp,sp,#4
LDMIA sp!,{v1,pc}</pre></code>
Which although it allocates storage for the local variable and passes it’s address to set_x neither initialises the memory nor reads it. In the practical problem I was trying to call a non-APCS routine.
GCC would require “memory” specifying in the clobber list, but for ROOL C no requirement nor method is documented. Arrays or structs appear to work.
Changelog:
Modified by Sprow (202) Thu, March 11 2021 - 14:37:29 GMT
- Summary changed from DDE30 C Compiler considers local variable not accessed by inline assembler BL to cc 5.85: Compiler considers local variable not accessed by inline assembler BL
If you wrap the test() function in a
#pragma no_optimise_cse
#pragma optimise_cse
it works. It should know because of address-of x there’s a chance whatever x is may be modified by the function call, but for some reason hasn’t understood that as it does if you make a function call in C.
Modified by Sprow (202) Mon, November 01 2021 - 08:38:31 GMT
- Status changed from Open to Fixed
Fixed in cc 5.88