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

Optimization issue

Hello fellow coders,

I have a problem with the following lines of code. I believe it's an optimization issue.

// Is page1 full?
   RAM_A16 = PAGE1;
   if(lib.full == TRUE)
   {
   // Is page0 full?
      RAM_A16 = PAGE0;
      if((lib.full) == TRUE)
      {
      // Handle full library.
         handle_full_page(record);
      }
   }

lib and RAM_A16 are declared as:
   lib_stats xdata lib _at_ 0x8A3;

   RAM_A16 BIT P1.6
I have tried to declare lib as volatile and lowering the optimization level, both with the same result:

On page 1 of the xram (RAM_A16 = PAGE1), lib.full is TRUE, which is evaluated correctly, but when switching to page 0 of the xram (RAM_A16 = PAGE0), lib.full is FALSE, but is evaluated to TRUE.

Any help would be appreciated.

TIA,

Anders.

Parents Reply Children
  • Can you show us the assembly output from the compiler, as well as the declarations for the symbols here (lib.full, RAM_A16, etc)?

    Why the extra level of parentheses in the second if?

    Are you sure that lib.full is exactly equal to 1, and not just non-zero? "if (var == TRUE)" and "if (var)" are not the same test.

  • I removed the _at_ and added volatile. It solved it, not sure why a volatile variable can't be at an absolute location.

  • The volatile should have been in there right from the start. Actually, you're walking quite a bit outside the limits of what you can do in C code, here.

    Actually, what you're doing here amounts to lying to the compiler about your system's memory layout. You have paged xdata, but didn't inform the compiler about that. How is the compiled code supposed to guess that after writing to P1.6 every variable in xdata may mysteriously have lost its previous value? Your C code, as written, says that there is only one variable, when in reality, there are two independent ones. That cannot possibly work without "volatile" qualification, and even then, you're out on a very thin limb.

    Hiding this kind of information from the compiler is begging for trouble.