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] Consistent setting of Pure flag on function with address parameters.


A subprogram that has an Address parameter and is declared in a Pure package
is not considered Pure, because the parameter may be used as a pointer and the
referenced data may change even if the address value itself does not.

This check was previously performed only on the subprogram body, leading to
improper optimizations when invoking such functions declared in a pure package.
It is now performed as well at the freeze point of the subprogram.

The following must execute quietly:

   gnatmake -q main -O

---
with Pure_Pkg; use Pure_Pkg;

procedure Main is
  I : Integer;
begin

  I := Add_Three (0);
  if I /= 3 then
    raise Program_Error;
  end if;

  I := 0;
  I := Bump_Int (I'Address);
  I := Bump_Int (I'Address);
  I := Bump_Int (I'Address);
  if I /= 3 then
    raise Program_Error;
  end if;

end;
---
package body Pure_Pkg is

  function Add_Three (I : Integer) return Integer is
    J : Integer := I;
  begin
    J := Bump_Int (J'Address);
    J := Bump_Int (J'Address);
    J := Bump_Int (J'Address);
    return J;
  end;

  function Bump_Int (X : System.Address) return Integer is
    I : Integer;
    for I'Address use X;
  begin
    return I + 1;
  end;

end Pure_Pkg;
---
with System;
package Pure_Pkg is
  pragma Pure;

  function Add_Three (I : Integer) return Integer;

  function Bump_Int (X : System.Address) return Integer;
end Pure_Pkg;

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

2015-10-23  Ed Schonberg  <schonberg@adacore.com>

	* sem_util.ads, sem_util.adb (Check_Function_With_Address_Parameter):
	A subprogram that has an Address parameter and is declared in a Pure
	package is not considered Pure, because the parameter may be used as a
	pointer and the referenced data may change even if the address value
	itself does not.
	* freeze.adb (Freeze_Subprogram): use it.
	* exp_ch6.adb (Expand_N_Subprogram_Body): Use it.

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]