Problem with optimisation
Hi,
We have a problem with a simple code. This code is reduced until the bug still exist.
here the problem code.
uint16_t methodShouldRet
{
uint8_t Index = 0;
uint16_t number = 0xFFFF;
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
while(
{
if(690 < numberPool[Index])
{
found = 1;
}
Index++;
}
if(!found)
{
number = 1050;
}
return number;
}
This code look strange but this code must return the value 1050. BUT starting using version 4.8 of GCC, this code doesn't return 1050. It return garbage. some time if returning 65535, and other time 65311 etc...
Here is my main
int main(void)
{
printf(
printf("test bug\n");
printf("value = %d\n", methodShouldRet
while(1);
}
we have modified ou syscall to send the value on USART3.
If we don't use optimisation -O0, the return value is 1050.
Here my compiling line
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall -g3 -DSTM32F4XX -DUSE_STDPERIPH
here my link line
arm-none-eabi-g++ -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wall -g3 -T "stm32.ld" -Xlinker --gc-sections -Wl,-Map,
Here the result from the asm listing file
8011076: b003 add sp, #12
8011078: bdf0 pop {r4, r5, r6, r7, pc}
801107a: bf00 nop
801107c: 40020c00 .word 0x40020c00
8011080: 40004800 .word 0x40004800
08011084 <methodShouldRe
_Z22methodShoul
C:\Projects/
uint16_t methodShouldRet
{
8011084: b410 push {r4}
C:\Projects/
uint8_t Index = 0;
uint16_t number = 0xFFFF;
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
8011086: 4a18 ldr r2, [pc, #96] ; (80110e8 <methodShouldRe
C:\Projects/
/* Enable USART */
USART_Cmd(USART3, ENABLE);
}
uint16_t methodShouldRet
{
8011088: b083 sub sp, #12
C:\Projects/
uint8_t Index = 0;
uint16_t number = 0xFFFF;
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
801108a: 6810 ldr r0, [r2, #0]
801108c: 9000 str r0, [sp, #0]
C:\Projects/
while(
801108e: f8bd 3000 ldrh.w r3, [sp]
C:\Projects/
{
uint8_t Index = 0;
uint16_t number = 0xFFFF;
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
8011092: 8892 ldrh r2, [r2, #4]
8011094: f8ad 2004 strh.w r2, [sp, #4]
C:\Projects/
while(
8011098: f64f 70ff movw r0, #65535 ; 0xffff
801109c: 4283 cmp r3, r0
801109e: d01f beq.n 80110e0 <methodShouldRe
C:\Projects/
{
if(690 < numberPool[Index])
80110a0: f240 22b2 movw r2, #690 ; 0x2b2
C:\Projects/
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
while(
80110a4: f8bd 1002 ldrh.w r1, [sp, #2]
C:\Projects/
{
if(690 < numberPool[Index])
80110a8: 4293 cmp r3, r2
80110aa: bf94 ite ls
80110ac: 2300 movls r3, #0
80110ae: 2301 movhi r3, #1
C:\Projects/
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
while(
80110b0: 4281 cmp r1, r0
C:\Projects/
{
if(690 < numberPool[Index])
{
found = 1;
80110b2: 461c mov r4, r3
C:\Projects/
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
while(
80110b4: d00c beq.n 80110d0 <methodShouldRe
80110b6: b93b cbnz r3, 80110c8 <methodShouldRe
80110b8: f8bd 3004 ldrh.w r3, [sp, #4]
C:\Projects/
{
if(690 < numberPool[Index])
{
found = 1;
80110bc: 4291 cmp r1, r2
80110be: bf94 ite ls
80110c0: 2400 movls r4, #0
80110c2: 2401 movhi r4, #1
C:\Projects/
uint8_t found = 0;
uint16_t numberPool[3] = {300, 315, 330};
while(
80110c4: 4283 cmp r3, r0
80110c6: d003 beq.n 80110d0 <methodShouldRe
C:\Projects/
{
number = 1050;
}
return number;
}
80110c8: b003 add sp, #12
80110ca: f85d 4b04 ldr.w r4, [sp], #4
80110ce: 4770 bx lr
C:\Projects/
Index++;
}
if(!found)
{
number = 1050;
80110d0: f64f 73ff movw r3, #65535 ; 0xffff
80110d4: f240 401a movw r0, #1050 ; 0x41a
80110d8: 2c00 cmp r4, #0
80110da: bf18 it ne
80110dc: 4618 movne r0, r3
80110de: e7f3 b.n 80110c8 <methodShouldRe
80110e0: f240 401a movw r0, #1050 ; 0x41a
80110e4: e7f0 b.n 80110c8 <methodShouldRe
80110e6: bf00 nop
80110e8: 08015b98 .word 0x08015b98
Is there really a problem with the optimiser or I missing something here ?
Regards
Jonathan
Question information
- Language:
- English Edit question
- Status:
- Solved
- Assignee:
- No assignee Edit question
- Solved by:
- Jonathan Dumaresq
- Solved:
- Last query:
- Last reply: