comp.lang.ada
 help / color / mirror / Atom feed
From: dewar@gnat.com (Robert Dewar)
Subject: Re: Pragma Volatile
Date: 30 Sep 2001 06:10:03 -0700
Date: 2001-09-30T13:10:03+00:00	[thread overview]
Message-ID: <5ee5b646.0109300510.71255128@posting.google.com> (raw)
In-Reply-To: 3BB64B13.81AFFB4D@acm.org

Jeffrey Carter <jrcarter@acm.org> wrote in message news:<3BB64B13.81AFFB4D@acm.org>...
> If a register is an Integer, I would generally do
> 
> B : Integer;
> for B'Address use Register_Location;
> pragma Volatile (B);
> ...
> B := 0;
> B := 1;
> 
> But then, I don't have the C pointer fixation.


Be very careful about assumptions here. Suppose that
B is a 32-bit word, then there is nothing in the language
that says that

  B := 0;

has to generate a 32-bit store, it would be perfectly 
valid if the compiler generated a call to bcopy that
moved four bytes independently (inefficient, but not
incorrect).

I often find people making this kind of assumption, and
in the case of reads I saw one awful bug caused by this:

    
    type R is array (natural range <>) of Boolean;
    pragma Pack (R);
 
    Register : R (1 .. 32);
    for Register'Address use ...

    ...

    B := Register (3);

one compiler generated a word load, and extracted bit 3.
another compiler generated a byte load, and extracted bit 3 from the
byte.

Both code sequences are perfectly valid, and just as
efficient as one another. But the hardware had been
build to require word loads, and byte loads were don't
care and put the hardware into some obscure state.

The "fix" was to use a constrained base type (this was
the only use of the type anyway), and then both compilers
generated the 32-bit word load.

But of course the real bug is in the implicit assumption
in the code.

Yes, it is very neat to use the address clause for memory
mapped I/O devices, but it is definitely cheating in that
it is making unwarranted assumptions about the exact code
sequences generated. 

Technically such code is quite wrong, and should be replaced by
machine code inserts. But that is heavy
in practice. At the very least, document assumptions
like this ferociously, and isolate them in special
packages known to need looking at when porting. 

Robert Dewar
Ada Core Technologies



  reply	other threads:[~2001-09-30 13:10 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-09-25 14:07 Pragma Volatile Jon R. Harshaw
2001-09-25 14:23 ` David C. Hoos
2001-09-25 14:38 ` Marin David Condic
2001-09-25 23:03 ` Mark Johnson
2001-09-29 17:38   ` Tucker Taft
2001-09-29 18:22     ` minyard
2001-09-29 22:28       ` Jeffrey Carter
2001-09-30 13:10         ` Robert Dewar [this message]
2001-09-30 21:19           ` Jeffrey Carter
2001-10-01  2:58             ` minyard
2001-10-02  9:38           ` AG
2001-10-02 10:59             ` Jeff Creem
2001-09-30  2:03       ` DuckE
2001-09-30 13:01       ` Robert Dewar
2001-09-30 20:12         ` minyard
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox