Boris Yost

Hey! It's optomizing my volatiles away!

Discussion created by Boris Yost on Oct 3, 2006
Latest reply on Oct 6, 2006 by Petr Cach
Dear Group:
I have code with the form of:
volatile unsigned int FPGA_mem_map[0x4000];
extern volatile unsigned int FPGA_mem_map[] @ (0x018000);
  do {
     FPGA_mem_map[ADDR]= reg;
      reg = dummy(reg); // magic line to prevent Metrowerks from optimizing volatiles
     status = (volatile) FPGA_mem_map[ADDR];
     done = DONE_MACRO(status);
     // done goes to zero when cleared
 } while((time_out<CLR_TIME_OUT_EXP) && done);
dummy() is a function that just returns it's argument.
We have PPAGE set to RUNTIME.
With the magic line included, it works.  With the magic line not included, it times out every time.
With this code, what MW does is that it sees that the value of reg was put in a processor register, and it is still preserved if the 'magic line' is not included, so it simply assigns the processor register to the variable status instead of fetching a fresh copy out of FPGA_mem_map[ADDR].  If the magic line is included, I guess that MW can't guarantee that the register wasn't used in the dummy() context, so it does fetch a fresh copy (by calling _LOAD_FAR_16).
We have IDE 5.5.1272, HC12 3.1 build 4047 and SP PE 29.4.01 .
We have been chasing this and a variety of other bugs involving volatile variables that aren't getting fetched fresh.  We've tried turning off all sorts of optomizations.  Any idea how to stop this behavior?