comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Forcing GNAT to use 32-bit load/store instructions on ARM?
Date: Mon, 7 Jul 2014 17:38:54 -0500
Date: 2014-07-07T17:38:54-05:00	[thread overview]
Message-ID: <lpf7hv$am9$1@loke.gir.dk> (raw)
In-Reply-To: lpcck1$vtv$1@dont-email.me

"MatthiasR" <MatthiasR@invalid.invalid> wrote in message 
news:lpcck1$vtv$1@dont-email.me...
> Randy Brukardt wrote:
>
>> Right. If the OP really did exactly as he said, then GNAT has a bug (no
>> matter what his declaration was). [But I've seen plenty of cases where
>> someone (including me) thought they'd tried something when they really 
>> had
>> not for one reason or another.]
>
> According to the RM, the compiler must not divide a read or write from/to 
> a
> variable with pragma/aspect 'atomic' into multiple reads/writes. There is
> nothing said, that the variable always has to be read/written as a whole. 
> So
> it seems to be perfectly OK to write only one byte of a 32 bit variable, 
> if
> a value has been assigned to only a part of a record which is contained in
> this byte.

No. C.6(22/2) surely applies. (All atomic objects are also volatile 
objects.) Anyone claiming that C.6(22/2) does not apply to a load/store of 
part of an object is seeing what they want - that would be inconsistent with 
the way such wording is intepreted in the rest of the standard.

But any attempt to write of a record component of an atomic object 
necessarily must violate either C.6(20) or C.6(22/2). (Either you don't 
access all of the bits, or you have to have a read not in the source code to 
read the bits that you aren't going to write.)  From that I conclude that 
Ada doesn't (really) support atomic composite types (and any attempt to 
declare such a thing ought to be rejected, since the language provides no 
other possibilities).

I doubt any compiler will do that for practical reasons. Since C.6(22/2) is 
Implementation Advice, a compiler is allowed to ignore it if it documents 
that it does so. That's probably what GNAT is doing here, but clearly that's 
harmful (as noted by this discussion). I'd rather that the implementation 
reject any assignments for which it cannot make this guarantee, but I 
suppose the language gives no justification for doing so. (That's a common 
problem in Chapter 13/Annex C features.) Does GNAT at least give a warning?

Clearly, we need a partial aggregate syntax (it's the only way for an atomic 
record write to make sense), and that needs to be clear that it includes 
both a read and a write of the object for the purposes of volatile 
variables. Probably someone should submit this problem to Ada-Comment for 
study in the next version of Ada (whenever that might be).

> There is something said about this topic in the latest GNAT docs:
> https://docs.adacore.com/gnat-unw-docs/html/gnat_ugn_8.html#SEC97
>
> They recommend to use a temporary variable to ensure an access to the 
> record
> variable as a whole.

Right. Partial writes of atomic objects shouldn't be allowed at all, and 
thus the programmer should never write one.

> That is not really elegant, because records with representation clauses,
> directly mapped onto hardware registers, look like the most natural way to
> access these registers.

That *is* the most natural way to access those registers. But you can never 
access a *part* of a register, you always have to read or write the entire 
register at a time. Ada makes you make that explicit in your code, thus a 
temporary is required (pending new syntax).

> Reading Ada books, one even could get the impression
> that record represenation clauses are made primarily for this purpose.

It is.

> Unfortunately there seem to be no way to force a specific access width, so
> this feature is not really useable (without the mentioned workaround with
> the temporary variable) on architectures where the allowed access width is
> not uniform over the whole address range.

No, you're looking at this the wrong way. If you write this in C, you read 
the entire register and then do some sort of bit-mask, and then write the 
thing back. You have to do that same thing in Ada, just using the record 
type instead of the bit mask. Ada replaces the bit masking operations by 
more readable record component accesses; it doesn't suddenly allow you to 
write something that you couldn't possibly write in C [a direct write of 
part of a register]. (Whether Ada *should* have allowed you do to that is a 
totally different question and relatively irrelevant at the moment because 
the language isn't likely to support anything new in the near future.)

                                                   Randy.




  parent reply	other threads:[~2014-07-07 22:38 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-30 22:11 Forcing GNAT to use 32-bit load/store instructions on ARM? daniel.dmk
2014-06-30 23:41 ` Jeffrey Carter
2014-07-01 12:06   ` Simon Clubley
2014-07-01 15:44     ` Niklas Holsti
2014-07-01 17:26       ` Simon Clubley
2014-07-01 17:18     ` Simon Wright
2014-07-01 19:43       ` Simon Wright
2014-07-01 17:28     ` Jeffrey Carter
2014-07-01  0:55 ` anon
2014-07-01  4:30 ` Niklas Holsti
2014-07-01  8:11 ` Dmitry A. Kazakov
2014-07-01 12:09   ` Simon Clubley
2014-07-01 12:20     ` Dmitry A. Kazakov
2014-07-01 17:00       ` Simon Clubley
2014-07-01 19:36         ` Dmitry A. Kazakov
2014-07-01 20:08           ` Simon Clubley
2014-07-02 22:24             ` Randy Brukardt
2014-07-06 20:40               ` MatthiasR
2014-07-07  0:25                 ` Simon Clubley
2014-07-07 22:38                 ` Randy Brukardt [this message]
2014-07-08  6:51                   ` Simon Wright
2014-07-10 11:47                     ` Simon Wright
2014-07-10 13:06                       ` Simon Clubley
2014-07-11 18:05                         ` Simon Wright
2014-07-11 20:22                           ` Simon Clubley
2014-07-08  8:50                   ` Brian Drummond
2014-07-08 12:12                   ` Simon Clubley
2014-07-08 13:26                     ` G.B.
2014-07-08 17:13                       ` Simon Clubley
2014-07-08 15:36                     ` Adam Beneschan
2014-07-08 15:40                       ` Adam Beneschan
2014-07-08 20:34                     ` Randy Brukardt
2014-07-09  7:31                       ` Dmitry A. Kazakov
2014-07-10  0:11                         ` Simon Clubley
2014-07-20 11:35                   ` MatthiasR
2014-07-20 15:49                     ` Simon Clubley
2014-07-26 11:05                       ` MatthiasR
2014-08-10 11:20                         ` MatthiasR
2014-07-01 12:03 ` Simon Clubley
2014-07-01 19:52   ` daniel.dmk
2014-07-01 20:40     ` Simon Clubley
2014-07-01 20:55       ` Simon Clubley
2014-07-01 21:01       ` Niklas Holsti
2014-07-01 21:20         ` Simon Clubley
2014-07-01 22:38           ` Niklas Holsti
2014-07-02 16:49             ` Simon Clubley
2014-07-01 21:55         ` daniel.dmk
2014-07-02  7:30     ` Simon Wright
2014-07-02 18:52       ` daniel.dmk
2014-07-04 23:51       ` Niklas Holsti
2014-07-05  0:18         ` Niklas Holsti
replies disabled

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