This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: volatile pointers


Robert J. Brown wrote:
>
> >>>>> "Neville" == Neville Mair <nev@snet.net> writes:
>
>     Neville> I tried this #define SIMCR (( volatile unsigned short
>     Neville> *)0Xfffa00)
>
>     Neville> This creates a 'volatile pointer' to short.  Everytime
>     Neville> the register (SIMCR) is accessed, the pointer is
>     Neville> reloaded.
>
>     Neville> What is the syntax to create a pointer to a 'volatile
>     Neville> short'.
>
>     Neville> Sorry for the simple question, but I couldn't find an
>     Neville> answer anywhere else.
>
> Typically, what you want is a pointer that is a constant, but that
> points to some hardware memory mapped I/O device register.  You say it
> in C++ like this:
>
>   volatile unsigned short int* const foo = 0xfffa00;
>
> I do not think C has such flexibility in declaring such thins.  I
> always prefer to declare a const to a #define.  A good compiler treats
> it the same way when optimizing.
>
    But C should allow that as well.  I see no syntax specific to C++ in
what you've written, nor use of any language features I haven't observed
gcc providing when compiling C (as opposed to C++).

    ANSI C says that any attributes given before the star refer to the
object pointed to, and any attributes given after the star refer to the
pointer itself.  Thus your declaration.  The pointer itself cannot be
changed (const after star), and the object pointed to is volatile (before
star).

    Neville, your declaration seemed like it should be correct to me.
It's been my experience that a 'volatile pointer to short' would have to
be declared   (unsigned short* volatile)0xFFFA00 .

    I've just consulted my ANSI C reference manual, and it confirms that
your declaration should produce a 'pointer to volatile short'.  So it
appears that, in this instance, the compiler is broken, or at least in
violation of the ANSI C specification.

                           -----Carl N. Miller
                                <chaz@devastator.extern.ucsd.edu>