diff options
author | Eric Botcazou <ebotcazou@adacore.com> | 2019-09-17 07:59:16 +0000 |
---|---|---|
committer | Pierre-Marie de Rodat <pmderodat@gcc.gnu.org> | 2019-09-17 07:59:16 +0000 |
commit | 37915d022449d21fe7bfa7db640a15e77cd3352e (patch) | |
tree | 63d68b13e71558c24f9d2b3a8d47774eb456eb8f /gcc/ada | |
parent | 38b06e7a19d4002697117965d0bfce7ee77e1d40 (diff) | |
download | gcc-37915d022449d21fe7bfa7db640a15e77cd3352e.zip gcc-37915d022449d21fe7bfa7db640a15e77cd3352e.tar.gz gcc-37915d022449d21fe7bfa7db640a15e77cd3352e.tar.bz2 |
[Ada] Fix wrong value of 'Size for slices of bit-packed arrays
This fixes a long-standing issue in the compiler which would return
a wrong value for the Size attribute applied to slices of bit-packed
arrays whose size is not a multiple of the storage unit.
The problem is that the computation was done in the code generator
after the bit-packed array had been internally rewritten into an
array of bytes, so the Size was always rounded up to the next byte.
The computation is now rewritten into the product of the Length and
Compnent_Size attribute of the slices before being sent to the code
generator.
2019-09-17 Eric Botcazou <ebotcazou@adacore.com>
gcc/ada/
* exp_attr.adb (Expand_Size_Attribute): Chain the special cases
on the back-end path and rewrite the attribute appled to slices
of bit-packed arrays into the product of the Length and the
Compoent_Size attributes of the slices.
* exp_ch5.adb (Expand_Assign_Array_Bitfield): Use Size attribute
directly to compute the bitfield's size.
gcc/testsuite/
* gnat.dg/pack25.adb: New testcase.
From-SVN: r275769
Diffstat (limited to 'gcc/ada')
-rw-r--r-- | gcc/ada/ChangeLog | 9 | ||||
-rw-r--r-- | gcc/ada/exp_attr.adb | 24 | ||||
-rw-r--r-- | gcc/ada/exp_ch5.adb | 18 |
3 files changed, 35 insertions, 16 deletions
diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index ecc67e5..ee7945c 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,12 @@ +2019-09-17 Eric Botcazou <ebotcazou@adacore.com> + + * exp_attr.adb (Expand_Size_Attribute): Chain the special cases + on the back-end path and rewrite the attribute appled to slices + of bit-packed arrays into the product of the Length and the + Compoent_Size attributes of the slices. + * exp_ch5.adb (Expand_Assign_Array_Bitfield): Use Size attribute + directly to compute the bitfield's size. + 2019-09-17 Bob Duff <duff@adacore.com> * exp_ch5.adb (Expand_Assign_Array_Loop_Or_Bitfield): Add tests diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb index c7d1647..c5ff9b5 100644 --- a/gcc/ada/exp_attr.adb +++ b/gcc/ada/exp_attr.adb @@ -7600,18 +7600,36 @@ package body Exp_Attr is New_Occurrence_Of (Get_Actual_Subtype (Pref), Loc), Attribute_Name => Name_Size)); Analyze_And_Resolve (N, Typ); - end if; - -- If Size applies to a dereference of an access to unconstrained + -- If Size is applied to a dereference of an access to unconstrained -- packed array, the back end needs to see its unconstrained nominal -- type, but also a hint to the actual constrained type. - if Nkind (Pref) = N_Explicit_Dereference + elsif Nkind (Pref) = N_Explicit_Dereference and then Is_Array_Type (Ptyp) and then not Is_Constrained (Ptyp) and then Is_Packed (Ptyp) then Set_Actual_Designated_Subtype (Pref, Get_Actual_Subtype (Pref)); + + -- If Size was applied to a slice of a bit-packed array, we rewrite + -- it into the product of Length and Component_Size. We need to do so + -- because bit-packed arrays are represented internally as arrays of + -- System.Unsigned_Types.Packed_Byte for code generation purposes so + -- the size is always rounded up in the back end. + + elsif Nkind (Original_Node (Pref)) = N_Slice + and then Is_Bit_Packed_Array (Ptyp) + then + Rewrite (N, + Make_Op_Multiply (Loc, + Make_Attribute_Reference (Loc, + Prefix => Duplicate_Subexpr (Pref, True), + Attribute_Name => Name_Length), + Make_Attribute_Reference (Loc, + Prefix => Duplicate_Subexpr (Pref, True), + Attribute_Name => Name_Component_Size))); + Analyze_And_Resolve (N, Typ); end if; return; diff --git a/gcc/ada/exp_ch5.adb b/gcc/ada/exp_ch5.adb index 6ef3fb2..7c2d632 100644 --- a/gcc/ada/exp_ch5.adb +++ b/gcc/ada/exp_ch5.adb @@ -1408,23 +1408,15 @@ package body Exp_Ch5 is Expressions => New_List (New_Copy_Tree (Right_Lo))), Attribute_Name => Name_Bit); - -- Compute the Size of the bitfield. ???We can't use Size here, because - -- it doesn't work properly for slices of packed arrays, so we compute - -- the L'Size as L'Length*L'Component_Size. - -- + -- Compute the Size of the bitfield + -- Note that the length check has already been done, so we can use the -- size of either L or R. Size : constant Node_Id := - Make_Op_Multiply (Loc, - Make_Attribute_Reference (Loc, - Prefix => - Duplicate_Subexpr (Name (N), True), - Attribute_Name => Name_Length), - Make_Attribute_Reference (Loc, - Prefix => - Duplicate_Subexpr (Name (N), True), - Attribute_Name => Name_Component_Size)); + Make_Attribute_Reference (Loc, + Prefix => Duplicate_Subexpr (Name (N), True), + Attribute_Name => Name_Size); begin return Make_Procedure_Call_Statement (Loc, |