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] Adjust default alignment of elementary types


The algorithm used in the front-end to compute the alignment of elementary
types doesn't take into account the target ABI.  For example if:

  type Float64 is new Long_Float;

  type R is record
    I : Integer;
    F : Float64;
  end record;

is compiled on PowerPC/AIX or x86/Linux, the -gnatR2 output is:

for Float64'Size use 64;
for Float64'Alignment use 8;

for R'Size use 96;
for R'Alignment use 4;
for R use record
  I at 0 range  0 .. 31;
  F at 4 range  0 .. 63;
end record;

i.e. field F is at offset 4 in a non-packed record although the alignment of 
its type is reported as being 8.  This offset of 4 is mandated by the ABI.

As a matter of fact, the above representation is also the exact internal 
representation of the back-end so things are consistent in this respect;
the ABI dictates the offset but, in the C family of languages, the type can
be overaligned since there is no real way to expose the type's alignment.

Things are different in Ada since you can do

  procedure Q (A : System.Address) is
    F : Float64;
    for F'Address use A;
  begin
    F := 0.0;
  end;

and the alignment check will essentially expect Float64'Alignment for the 
address; if the address of the above F component of the R record is passed,
things can go awry, although passing the address of a Float64 in a non-packed
record to a Float64 overlay should just work.

This patch teaches the front-end about the target ABI; this is only needed in
limited cases: for double floating-point types on PowerPC and for double or
larger scalar types on x86.  Some glue code is needed in Gigi because the
internal representation of the back-end will stay the same.

Tested on i586-suse-linux, applied on the mainline.


2009-04-24  Eric Botcazou  <ebotcazou@adacore.com>

	* ttypes.ads (Target_Double_Float_Alignment): New variable.
	(Target_Double_Scalar_Alignment): Likewise.
	* get_targ.ads (Get_Strict_Alignment): Adjust external name.
	(Get_Double_Float_Alignment): New imported function.
	(Get_Double_Scalar_Alignment): Likewise.
	* layout.adb (Set_Elem_Alignment): Take into account specific caps for
	the alignment of "double" floating-point types and "double" or larger
	scalar types, as parameterized by Target_Double_Float_Alignment and
	Target_Double_Scalar_Alignment respectively.
	* gcc-interface/gigi.h (double_float_alignment): Declare.
	(double_scalar_alignment): Likewise.
	(is_double_float_or_array): Likewise.
	(is_double_scalar_or_array): Likewise.
	(get_target_double_float_alignment): Likewise.
	(get_target_double_scalar_alignment): Likewise.
	* gcc-interface/targtyps.c (get_strict_alignment): Rename into...
	(get_target_strict_alignment): ...this.
	(get_target_double_float_alignment): New function.
	(get_target_double_scalar_alignment): Likewise.
	* gcc-interface/decl.c (gnat_to_gnu_entity) <E_Signed_Integer_Subtype>:
	Test the presence of an alignment clause for under-aligned integer
	types.  Take into account specific caps for the alignment of "double"
	floating-point types and "double" or larger scalar types, as
	parameterized by Target_Double_Float_Alignment and
	Target_Double_Scalar_Alignment respectively.
	(validate_alignment): Likewise.
	* gcc-interface/trans.c (Attribute_to_gnu) <Attr_Alignment>: Likewise.
	(gigi): Initialize double_float_alignment and double_scalar_alignment.
	* gcc-interface/utils.c (double_float_alignment): New global variable.
	(double_scalar_alignment): Likewise.
	(is_double_float_or_array): New predicate.
	(is_double_scalar_or_array): Likewise.


2009-04-24  Eric Botcazou  <ebotcazou@adacore.com>

	* gnat.dg/alignment7.adb: New test.
	* gnat.dg/alignment8.adb: Likewise.


-- 
Eric Botcazou


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