C undefined behavior fix
<<* Given a pointer, I need a way to determine the address (as an int of
the appropriate size) that the CPU will present to the MMU when I dereference that pointer.
Quoted 1 line
Quoted 1 line
This is in general ill-defined, a compiler might generate code that sometimes does byte access to a particular byte, anmd sometimes gets the entire word in which the byte resides.
This is often a nasty issue, and is one of the few things in this area that Ada does not properly address.
If you have a memory mapped byte, you really want a way of saying
"when I read this byte, do a byte read, it will not work to do a word read"
pragma Atomic in Ada (volatile gets close in C, but is not close enough) will ensure a byte store in practice, but may not ensure byte reads.
Quoted 2 lines · [email protected] wrote:
pragma Atomic in Ada (volatile gets close in C, but is not close enough) will ensure a byte store in practice, but may not ensure byte reads.
I see no distinction between read and write in the text of the Ada standard.
Also I think if you declare a byte array with Atomic_Component and Volatile_Component and that the compiler accepts it for its target architecture, then the compiler is required to generate a byte read and store for each occurence in the text source. From C.6:
15 For an atomic object (including an atomic component) all reads and
updates of the object as a whole are indivisible.
16 For a volatile object all reads and updates of the object as a whole are
performed directly to memory.
20 {external effect (volatile/atomic objects) [partial]} The external
effect of a program (see 1.1.3) is defined to include each read and update of a volatile or atomic object. The implementation shall not generate any memory reads or updates of atomic or volatile objects other than those specified by the program.
In my exemple byte array, if I say X := T (I) I don't see how a conformant compiler accepting the declaration could generate anything other than one and exactly one byte read. Per 20 it has no right to read T (I+1) or T(I-1) since they are "other" objects (components to be pedantic).
Do you agree with interpretation? Now, I haven't checked what GNAT does there (what's accepted and what code is generated) which is of course the interesting part :).
Signature · 1 line
Laurent Guerby <[email protected]>