comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: volatile vs aliased
Date: Wed, 05 Oct 2005 20:22:29 +0200
Date: 2005-10-05T20:22:29+02:00	[thread overview]
Message-ID: <87mzlnomca.fsf@ludovic-brenta.org> (raw)
In-Reply-To: 1128525722.605730.281980@g43g2000cwa.googlegroups.com

"REH" <spamjunk@stny.rr.com> writes:
> I'm converting some code written for Gnat to compile with Apex.  The
> software has bindings to C functions (OS calls).  Some of these take
> System.Address as parameters.  Apex complains about the variables used
> in these calls (via 'Address) because they not volatile or aliased (or
> imported, exported, etc.).  My question is: which should I use?  Is
> there any difference whether a variable is volatile or aliased?
>
> I am assuming the compiler is concerned about the variable possibly
> being changed in the call (which, of course, is true).  Is there a way
> to satisfy the compiler without stopping optimizations of the variable
> after the call, or do I need to use a second variable to do this?

I like this warning given by Apex; I think it would be nice if GNAT
would also warn in this situation.

pragma Volatile (Variable) says the compiler must not optimise away
any reads or writes to that variable, and that it may not add extra
reads or writes beyond those you explicitly request in your program
text.  You want to use that for hardware registers, where a "read"
operation may have a side effect such as changing the device's state.

Whenever you access a variable through an address value, the variable
is aliased, so you should say that explicitly.  Otherwise, the
compiler may be tempted to place the variable in a processor register,
which has no address.

In addition, pragma Import (Ada, Variable) says that the compiler may
not insert default initialisations for that variable, or assume
anything about the value of the variable, before you write to it.  But
the compiler may do optimisations, because it is allowed to assume
that no side effects occur when you read from or write to the variable
(that is, side effects not explicitly requested from the program
text).  In your situation, it seems that you want something like this
(which, incidentally, I use very often):

type T is ...;

procedure Read_Variable (At_Address : in  System.Address;
                         Into       : out T) is
   V : aliased T;
   -- suppress default initialisation, I know what I'm doing.
   pragma Import (Ada, V);
   for V'Address use At_Address;
begin
   -- perform my own explicit validation of the variable, perhaps
   -- using 'Valid; then, copy into Into:
   Into := V;
end Read_Variable;

-- 
Ludovic Brenta.



  reply	other threads:[~2005-10-05 18:22 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-10-05 15:22 volatile vs aliased REH
2005-10-05 18:22 ` Ludovic Brenta [this message]
2005-10-05 18:39   ` REH
2005-10-05 19:46     ` Ludovic Brenta
2005-10-05 20:02       ` REH
2005-10-05 20:11         ` Ludovic Brenta
2005-10-05 20:20           ` REH
2005-10-06  5:21             ` Ludovic Brenta
2005-10-05 20:55       ` Simon Wright
2005-10-06 18:32       ` Jeffrey R. Carter
2005-10-05 23:38   ` Randy Brukardt
2005-10-06  0:02     ` tmoran
2005-10-06 13:40     ` REH
2005-10-06 23:52       ` Randy Brukardt
2005-10-06 18:40     ` Jeffrey R. Carter
2005-10-06 19:37       ` Robert A Duff
2005-10-06 23:56         ` Randy Brukardt
2005-10-06 19:08     ` REH
2005-10-06 19:21       ` Ed Falis
2005-10-06 19:37         ` REH
2005-10-06 19:46       ` Robert A Duff
2005-10-06  8:05   ` Martin Krischik
2005-10-06  8:52     ` Dmitry A. Kazakov
2005-10-06 11:36     ` Rolf
2005-10-06 18:43       ` Björn Persson
2005-10-06 19:03         ` Niklas Holsti
2005-10-07  6:36         ` Martin Krischik
2005-10-07  6:33       ` Martin Krischik
2005-10-07 15:56         ` Adrian Knoth
2005-10-07 18:48           ` Martin Krischik
2005-10-07 22:44           ` REH
2005-10-08  6:10         ` Simon Wright
2005-10-17  2:16         ` Dave Thompson
replies disabled

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