This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Fix -gnatVa with pragma Atomic


This patch corrects a problem with use of pragma Atomic with
-gnatVa which resulted in an extra read of an atomic object.
There are two cases. First, Is_Known_Valid was not being
propagated to all relevant types, with the result that junk
extra declarations are generated even in cases where it is
impossible to have invalid values.

In the following example, a junk declaration for R1b is
generated, even though there is no need to do validity
checks:

with System;
package Atom1 is
   type Word is mod 2 ** 32;
   function Read (Here : System.Address) return Word;
end Atom1;

package body Atom1 is
  function Read (Here : System.Address) return Word is
    H : Word;
    pragma Atomic (H);
    for H'Address use Here;
  begin
    declare
      V : constant Word := H;
    begin
      return V;
    end;
  end Read;
end Atom1;

The -gnatG from the above example compiled with -gnatVa should not
contain the following declaration, which it previously did contain:

         R1b : constant atom1__word := h;

The second issue arises when there really is a validity check
needed, and in this case the atomic variable was read twice:

with System;
package Atom2 is
   type Word1 is mod 2 ** 32;
   subtype Word is Word1 range 1 .. 2 ** 32 - 1;
   function Read (Here : System.Address) return Word;
end Atom2;

package body Atom2 is
  function Read (Here : System.Address) return Word is
    H : Word;
    pragma Atomic (H);
    for H'Address use Here;
  begin
    declare
      V : constant Word := H;
    begin
      return V;
    end;
  end Read;
end Atom2;

Compiled with -gnatVa the -gnatG output used to contain the following
two lines:

         R1b : constant atom2__word := h;
         v : constant atom2__word := h;

and with the patch in place, we get instead:

        R1b : constant atom2__word := h;
        v : constant atom2__word := R1b;

which avoids the incorrect second read of h

Tested on x86_64-pc-linux-gnu, committed on trunk

2009-04-09  Robert Dewar  <dewar@adacore.com>

        * checks.adb:
        (Insert_Valid_Check): Avoid unnecessary generation of junk declaration
        when no invalid values exist, Avoid duplicate read of atomic variable.

        * cstand.adb (Build_Signed_Integer_Type): Set Is_Known_Valid
        (Standard_Unsigned): Set Is_Known_Valid

        * sem_ch3.adb (Analyze_Subtype_Declaration): Copy Is_Known_Valid on
	subtype declaration if no constraint.
        (Set_Modular_Size): Set Is_Known_Valid if appropriate
        (Build_Derived_Numeric_Type): Copy Is_Known_Valid if no constraint

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]