This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Cast converted volatile access not generating buggy code

I have a code snippet as given below

static unsigned int ticks;

void IRQ_SomeTimer(void) {
   ticks ++;
}

int main()
{
  int sav_ticks;
  sav_ticks = ticks;
  while (sav_ticks + TIME_TO_WAIT > *(volatile unsigned int *)&ticks);
  /* Do somethine here */
  return 0;
}

This compiles and runs on ARM-Mx targets up to optimization level O2, at optimization level O3 the code generated for

while (sav_ticks + TIME_TO_WAIT > *(volatile unsigned int *)&ticks);

is the same as the code generated for

while (sav_ticks + TIME_TO_WAIT > ticks);

Hence the code generated by O3 never comes out of the while loop!! Is this a BUG in Keil armcc(?) I am using Keil U-Vision (MDK ARM 4.73).

Parents
  • By cast I could selectively make the identifier to behave as volatile and hence obtaining more optimized code than the one where it is just declared volatile

    Who the heck cast things to volatile pointers, and back-and-forth, project's got to be a nightmare to manage, for nominal to non-existent benefit.

    Code it as ((volatile uint32_t)ticks)

    Does the example I presented compile cleanly on all the compilers, and optimization settings? It's simpler, cleaner, and doesn't break when the count wraps.

    Submit your bug report to your support contact at Keil

Reply
  • By cast I could selectively make the identifier to behave as volatile and hence obtaining more optimized code than the one where it is just declared volatile

    Who the heck cast things to volatile pointers, and back-and-forth, project's got to be a nightmare to manage, for nominal to non-existent benefit.

    Code it as ((volatile uint32_t)ticks)

    Does the example I presented compile cleanly on all the compilers, and optimization settings? It's simpler, cleaner, and doesn't break when the count wraps.

    Submit your bug report to your support contact at Keil

Children
No data