cancel
Showing results for 
Search instead for 
Did you mean: 

Compiler optimization does not produce correct code

Fero
Associate II

Hi there,

I have encouraged a strange behavior inside newlib_lock_glue.c.

On few devices the code does note execute correctly when the optimization is turned on.

To me it looks like the optimization throw away the return and just continue evaluating. the function was called (eventually) from sprintf.

I am running FreeRTOS and thread-safe strategy #4 (allow lock usage from interrupts).

The return is missing from the disassembly and I do not see any instruction that will implement it inside of the if code.

Here is the code with disassembly and callstack

0693W00000UokFgQAJ.pnglock is not null

0693W00000UokG5QAJ.pngIs the generated assembly correct?

Can you point me to the solution please? I am out of ideas.

This is not my code, newlib_lock_glue.c is generated by CubeIDE

here are mine gcc flags

-mcpu=cortex-m7
-std=gnu11
-g3
-DUSE_HAL_DRIVER
-DSTM32H7A3xx
-DSTM32_THREAD_SAFE_STRATEGY=4
-c
-Os
-ffunction-sections
-fdata-sections
-Wall
-Wextra
-Wshadow
-Wundef
-fstack-usage
--specs=nano.specs
-mfpu=fpv5-d16
-mfloat-abi=hard
-mthumb

Thank you

3 REPLIES 3
Piranha
Chief II

The POP instruction at the address 0x080931E6 restores the PC register to the LR register value, which was stored with a PUSH instruction on entry. Therefore the POP instruction acts as a return.

Semer CHERNI
ST Employee

Hello @František Horínek​ 

First let me thank you for posting.

When activating the optimization option, the compiler will attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program.

Therefore, when debugging the execution will not be in line with the written code.

For debugging it is recommended to use a (-O0) or (-Og) optimization level.

I recommended that you check this link: Optimize Options (Using the GNU Compiler Collection (GCC))

I hope my reply answered your question.

Feel free to reply in case if you need further info.

Kind regards,

Semer.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

> I hope my reply answered your question.

The question basically was: "Where is the return instruction?" Your post doesn't answer the question in any way, but you are still pretending it does. What's the point of such useless posts?

> First let me thank you for posting.

Thanking is expressing a gratitude for someone doing some favor to someone. Thanking for reporting a bug is OK, but not for just asking a question. Asking a question is asking for a favor, not doing a favor - the exact opposite.

> Feel free to reply in case if you need further info.

Another nonsense from some management SJWs. People don't need your permission to reply!