Re: [cc65] optimizer or me?

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2004-07-15 09:03:19
On Wed, Jul 14, 2004 at 08:33:10PM -0700, Shawn Jefferson wrote:
> Well, a possible bug or my expectations are wrong?  In
> one of my projects I am testing for the existance of
> hardware at a particular address.  The listing is a
> little wierd.  I guess it's optimizing out the compare
> and first case, but should it do that?

The optimizer is removing the test because it knows you stored a one in this
place before. And yes, it is allowed to do that.

With another compiler you could declare the memory cell volatile, which would
cause the compiler to read and write it each time it is needed (that is, don't
remove reads or writes to this location). But - while cc65 understands the
keyword - cc65 won't generate different code, because it ignores the volatile
modifier.

The possibilities you have are:

    * Disable the optimizer for this module (affects all code in the module).

    * Place the stuff into a small assembler module.

    * Hide parts of the code from the optimizer:

        unsigned char ReadD104 (void)
        {
            return *(unsigned char*) 0xD104;
        }

        int main (void)
            char a;

            *(unsigned char*) 0xD104 = 1;
            if (ReadD104 () == 1) {
                a = 1;
            } else {
                a = 2;
            }
            return 1;
        }

The latter is not safe for all times, because in theory the compiler could
look at ReadD104, or even inline it, but hopefully it will also understand
volatile once it is smart enough to inline functions.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu Jul 15 09:03:23 2004

This archive was generated by hypermail 2.1.8 : 2004-07-15 09:03:31 CEST