From 9a9d35ffaa1445248ca6449bfedae640659def22 Mon Sep 17 00:00:00 2001 From: Arnaud Charlet Date: Mon, 4 Aug 2014 15:19:06 +0200 Subject: [PATCH] [multiple changes] 2014-08-04 Robert Dewar * gnat_rm.texi: Add section on use of address clause for memory mapped I/O. 2014-08-04 Ed Schonberg * sem_ch3.adb (Analyze_Subtype_Declaration): A subtype, in particular the subtype created for a generic actual, inherits invariant information from the base type. From-SVN: r213589 --- gcc/ada/ChangeLog | 11 +++++++++ gcc/ada/gnat_rm.texi | 59 ++++++++++++++++++++++++++++++++++++++++++++ gcc/ada/sem_ch3.adb | 8 ++++++ 3 files changed, 78 insertions(+) diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 2423d29a62e..ca0d4e8429f 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,14 @@ +2014-08-04 Robert Dewar + + * gnat_rm.texi: Add section on use of address clause for memory + mapped I/O. + +2014-08-04 Ed Schonberg + + * sem_ch3.adb (Analyze_Subtype_Declaration): A subtype, in + particular the subtype created for a generic actual, inherits + invariant information from the base type. + 2014-08-04 Robert Dewar * aspects.ads, aspects.adb: Add entries for aspect Obsolescent. diff --git a/gcc/ada/gnat_rm.texi b/gcc/ada/gnat_rm.texi index c782ea3b65c..f1f0ccf13e8 100644 --- a/gcc/ada/gnat_rm.texi +++ b/gcc/ada/gnat_rm.texi @@ -14813,6 +14813,7 @@ source file location. * Handling of Records with Holes:: * Enumeration Clauses:: * Address Clauses:: +* Use of Address Clauses for Memory-Mapped I/O:: * Effect of Convention on Representation:: * Conventions and Anonymous Access Types:: * Determining the Representations chosen by GNAT:: @@ -16606,6 +16607,64 @@ end Overwrite_Array; then the program compiles without the warning and when run will generate the output @code{X was not clobbered}. +@node Use of Address Clauses for Memory-Mapped I/O +@section Use of Address Clauses for Memory-Mapped I/O +@cindex Memory-mapped I/O + +A common pattern is to use an address clause to map an atomic variable to +a location in memory that corresponds to a memory-mapped I/O operation or +operations, for example: + +@smallexample @c ada + type Mem_Word is record + A,B,C,D : Byte; + end record; + pragma Atomic (Mem_Word); + for Mem_Word_Size use 32; + + Mem : Mem_Word; + for Mem'Address use some-address; + ... + Temp := Mem; + Temp.A := 32; + Mem := Temp; +@end smallexample + +@noindent +For a full access (reference or modification) of the variable (Mem) in +this case, as in the above examples, GNAT guarantees that the entire atomic +word will be accessed. It is not clear whether the RM requires this. For +example in the above, can the compiler reference only the Mem.A field as +an optimization? Whatever the answer to this question is, GNAT makes the +guarantee that for such a reference, the entire word is read or written. + +A problem arises with a component access such as: + +@smallexample @c ada + Mem.A := 32; +@end smallexample + +@noindent +Note that the component A is not declared as atomic. This means that it is +not clear what this assignment means. It could correspond to full word read +and write as given in the first example, or on architectures that supported +such an operation it might be a single byte store instruction. The RM does +not have anything to say in this situation, and GNAT does not make any +guarantee. The code generated may vary from target to target. GNAT will issue +a warning in such a case: + +@smallexample @c ada + Mem.A := 32; + | + >>> warning: access to non-atomic component of atomic array, + may cause unexpected accesses to atomic object +@end smallexample + +@noindent +It is best to be explicit in this situation, by either declaring the +components to be atomic if you want the byte store, or explicitly writing +the full word access sequence if that is what the hardware requires. + @node Effect of Convention on Representation @section Effect of Convention on Representation @cindex Convention, effect on representation diff --git a/gcc/ada/sem_ch3.adb b/gcc/ada/sem_ch3.adb index 695b27ef169..dd71672d39b 100644 --- a/gcc/ada/sem_ch3.adb +++ b/gcc/ada/sem_ch3.adb @@ -4944,6 +4944,14 @@ package body Sem_Ch3 is end if; end if; + -- A type invariant applies to any subtype in its scope, in particular + -- to a generic actual. + + if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then + Set_Has_Invariants (Id); + Set_Invariant_Procedure (Id, Invariant_Procedure (T)); + end if; + -- Make sure that generic actual types are properly frozen. The subtype -- is marked as a generic actual type when the enclosing instance is -- analyzed, so here we identify the subtype from the tree structure.