1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 -- Expand routines for manipulation of packed arrays
28 with Rtsfind
; use Rtsfind
;
29 with Types
; use Types
;
33 -------------------------------------
34 -- Implementation of Packed Arrays --
35 -------------------------------------
37 -- When a packed array (sub)type is frozen, we create a corresponding
38 -- type that will be used to hold the bits of the packed value, and store
39 -- the entity for this type in the Packed_Array_Impl_Type field of the
40 -- E_Array_Type or E_Array_Subtype entity for the packed array.
42 -- This packed array type has the name xxxPn, where xxx is the name
43 -- of the packed type, and n is the component size. The expanded
44 -- declaration declares a type that is one of the following:
46 -- For an unconstrained array with component size 1,2,4 or any other
47 -- odd component size. These are the cases in which we do not need
48 -- to align the underlying array.
50 -- type xxxPn is new Packed_Bytes1;
52 -- For an unconstrained array with component size that is divisible
53 -- by 2, but not divisible by 4 (other than 2 itself). These are the
54 -- cases in which we can generate better code if the underlying array
55 -- is 2-byte aligned (see System.Pack_14 in file s-pack14 for example).
57 -- type xxxPn is new Packed_Bytes2;
59 -- For an unconstrained array with component size that is divisible
60 -- by 4, other than powers of 2 (which either come under the 1,2,4
61 -- exception above, or are not packed at all). These are cases where
62 -- we can generate better code if the underlying array is 4-byte
63 -- aligned (see System.Pack_20 in file s-pack20 for example).
65 -- type xxxPn is new Packed_Bytes4;
67 -- For a constrained array with a static index type where the number
68 -- of bits does not exceed the size of Unsigned:
70 -- type xxxPn is new Unsigned range 0 .. 2 ** nbits - 1;
72 -- For a constrained array with a static index type where the number
73 -- of bits is greater than the size of Unsigned, but does not exceed
74 -- the size of Long_Long_Unsigned:
76 -- type xxxPn is new Long_Long_Unsigned range 0 .. 2 ** nbits - 1;
78 -- For all other constrained arrays, we use one of
80 -- type xxxPn is new Packed_Bytes1 (0 .. m);
81 -- type xxxPn is new Packed_Bytes2 (0 .. m);
82 -- type xxxPn is new Packed_Bytes4 (0 .. m);
84 -- where m is calculated (from the length of the original packed array)
85 -- to hold the required number of bits, and the choice of the particular
86 -- Packed_Bytes{1,2,4} type is made on the basis of alignment needs as
87 -- described above for the unconstrained case.
89 -- When the packed array (sub)type is specified to have the reverse scalar
90 -- storage order, the Packed_Bytes{1,2,4} references above are replaced
91 -- with Rev_Packed_Bytes{1,2,4}. This is necessary because, although the
92 -- component type is Packed_Byte and therefore endian neutral, the scalar
93 -- storage order of the new type must be compatible with that of an outer
94 -- composite type, if this composite type contains a component whose type
95 -- is the packed array (sub)type and which does not start or does not end
96 -- on a storage unit boundary.
98 -- When a variable of packed array type is allocated, gigi will allocate
99 -- the amount of space indicated by the corresponding packed array type.
100 -- However, we do NOT attempt to rewrite the types of any references or
101 -- to retype the variable itself, since this would cause all kinds of
102 -- semantic problems in the front end (remember that expansion proceeds
103 -- at the same time as analysis).
105 -- For an indexed reference to a packed array, we simply convert the
106 -- reference to the appropriate equivalent reference to the object
107 -- of the packed array type (using unchecked conversion).
109 -- In some cases (for internally generated types, and for the subtypes
110 -- for record fields that depend on a discriminant), the corresponding
111 -- packed type cannot be easily generated in advance. In these cases,
112 -- we generate the required subtype on the fly at the reference point.
114 -- For the modular case, any unused bits are initialized to zero, and
115 -- all operations maintain these bits as zero (where necessary all
116 -- unchecked conversions from corresponding array values require
117 -- these bits to be clear, which is done automatically by gigi).
119 -- For the array cases, there can be unused bits in the last byte, and
120 -- these are neither initialized, nor treated specially in operations
121 -- (i.e. it is allowable for these bits to be clobbered, e.g. by not).
123 ---------------------------
124 -- Endian Considerations --
125 ---------------------------
127 -- The standard does not specify the way in which bits are numbered in
128 -- a packed array. There are two reasonable rules for deciding this:
130 -- Store the first bit at right end (low order) word. This means
131 -- that the scaled subscript can be used directly as a left shift
132 -- count (if we put bit 0 at the left end, then we need an extra
133 -- subtract to compute the shift count).
135 -- Layout the bits so that if the packed boolean array is overlaid on
136 -- a record, using unchecked conversion, then bit 0 of the array is
137 -- the same as the bit numbered bit 0 in a record representation
138 -- clause applying to the record. For example:
140 -- type Rec is record
146 -- for Rec use record
147 -- C at 0 range 0 .. 3;
148 -- D at 0 range 4 .. 10;
149 -- E at 0 range 11 .. 15;
152 -- type P16 is array (0 .. 15) of Boolean;
153 -- pragma Pack (P16);
155 -- Now if we use unchecked conversion to convert a value of the record
156 -- type to the packed array type, according to this second criterion,
157 -- we would expect field D to occupy bits 4..10 of the Boolean array.
159 -- Although not required, this correspondence seems a highly desirable
160 -- property, and is one that GNAT decides to guarantee. For a little
161 -- endian machine, we can also meet the first requirement, but for a
162 -- big endian machine, it will be necessary to store the first bit of
163 -- a Boolean array in the left end (most significant) bit of the word.
164 -- This may cost an extra instruction on some machines, but we consider
165 -- that a worthwhile price to pay for the consistency.
167 -- One more important point arises in the case where we have a constrained
168 -- subtype of an unconstrained array. Take the case of 20 bits. For the
169 -- unconstrained representation, we would use an array of bytes:
171 -- Little-endian case
172 -- 8-7-6-5-4-3-2-1 16-15-14-13-12-11-10-9 x-x-x-x-20-19-18-17
175 -- 1-2-3-4-5-6-7-8 9-10-11-12-13-14-15-16 17-18-19-20-x-x-x-x
177 -- For the constrained case, we use a 20-bit modular value, but in
178 -- general this value may well be stored in 32 bits. Let's look at
179 -- what it looks like:
181 -- Little-endian case
183 -- x-x-x-x-x-x-x-x-x-x-x-x-20-19-18-17-...-10-9-8-7-6-5-4-3-2-1
185 -- which stored in memory looks like
187 -- 8-7-...-2-1 16-15-...-10-9 x-x-x-x-20-19-18-17 x-x-x-x-x-x-x
189 -- An important rule is that the constrained and unconstrained cases
190 -- must have the same bit representation in memory, since we will often
191 -- convert from one to the other (e.g. when calling a procedure whose
192 -- formal is unconstrained). As we see, that criterion is met for the
193 -- little-endian case above. Now let's look at the big-endian case:
197 -- x-x-x-x-x-x-x-x-x-x-x-x-1-2-3-4-5-6-7-8-9-10-...-17-18-19-20
199 -- which stored in memory looks like
201 -- x-x-x-x-x-x-x-x x-x-x-x-1-2-3-4 5-6-...11-12 13-14-...-19-20
203 -- That won't do, the representation value in memory is NOT the same in
204 -- the constrained and unconstrained case. The solution is to store the
205 -- modular value left-justified:
207 -- 1-2-3-4-5-6-7-8-9-10-...-17-18-19-20-x-x-x-x-x-x-x-x-x-x-x
209 -- which stored in memory looks like
211 -- 1-2-...-7-8 9-10-...15-16 17-18-19-20-x-x-x-x x-x-x-x-x-x-x-x
213 -- and now, we do indeed have the same representation for the memory
214 -- version in the constrained and unconstrained cases.
216 ----------------------------------------------
217 -- Entity Tables for Packed Access Routines --
218 ----------------------------------------------
220 -- For the cases of component size = 3,5-7,9-15,17-31,33-63,65-127 we call
221 -- library routines. These tables provide the entity for the right routine.
222 -- They are exposed in the spec to allow checking for the presence of the
223 -- needed routine when an array is subject to pragma Pack.
225 type E_Array
is array (Int
range 1 .. 127) of RE_Id
;
227 -- Array of Bits_nn entities. Note that we do not use library routines
228 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
229 -- entries from System.Unsigned, because we also use this table for
230 -- certain special unchecked conversions in the big-endian case.
232 Bits_Id
: constant E_Array
:=
248 16 => RE_Unsigned_16
,
264 32 => RE_Unsigned_32
,
296 64 => RE_Unsigned_64
,
361 -- Array of Get routine entities. These are used to obtain an element from
362 -- a packed array. The N'th entry is used to obtain elements from a packed
363 -- array whose component size is N. RE_Null is used as a null entry, for
364 -- the cases where a library routine is not used.
366 Get_Id
: constant E_Array
:=
495 -- Array of Get routine entities to be used in the case where the packed
496 -- array is itself a component of a packed structure, and therefore may not
497 -- be fully aligned. This only affects the even sizes, since for the odd
498 -- sizes, we do not get any fixed alignment in any case.
500 GetU_Id
: constant E_Array
:=
629 -- Array of Set routine entities. These are used to assign an element of a
630 -- packed array. The N'th entry is used to assign elements for a packed
631 -- array whose component size is N. RE_Null is used as a null entry, for
632 -- the cases where a library routine is not used.
634 Set_Id
: constant E_Array
:=
763 -- Array of Set routine entities to be used in the case where the packed
764 -- array is itself a component of a packed structure, and therefore may not
765 -- be fully aligned. This only affects the even sizes, since for the odd
766 -- sizes, we do not get any fixed alignment in any case.
768 SetU_Id
: constant E_Array
:=
901 procedure Create_Packed_Array_Impl_Type
(Typ
: Entity_Id
);
902 -- Typ is a array type or subtype to which pragma Pack applies. If the
903 -- Packed_Array_Impl_Type field of Typ is already set, then the call has
904 -- no effect, otherwise a suitable type or subtype is created and stored in
905 -- the Packed_Array_Impl_Type field of Typ. This created type is an Itype
906 -- so that Gigi will simply elaborate and freeze the type on first use
907 -- (which is typically the definition of the corresponding array type).
909 -- Note: although this routine is included in the expander package for
910 -- packed types, it is actually called unconditionally from Freeze,
911 -- whether or not expansion (and code generation) is enabled. We do this
912 -- since we want gigi to be able to properly compute type characteristics
913 -- (for the Data Decomposition Annex of ASIS, and possible other future
914 -- uses) even if code generation is not active. Strictly this means that
915 -- this procedure is not part of the expander, but it seems appropriate
916 -- to keep it together with the other expansion routines that have to do
917 -- with packed array types.
919 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
);
920 -- N is an N_Op_And, N_Op_Or or N_Op_Xor node whose operand type is a
921 -- packed boolean array. This routine expands the appropriate operations
922 -- to carry out the logical operation on the packed arrays. It handles
923 -- both the modular and array representation cases.
925 procedure Expand_Packed_Element_Reference
(N
: Node_Id
);
926 -- N is an N_Indexed_Component node whose prefix is a packed array. In
927 -- the bit packed case, this routine can only be used for the expression
928 -- evaluation case, not the assignment case, since the result is not a
929 -- variable. See Expand_Bit_Packed_Element_Set for how the assignment case
930 -- is handled in the bit packed case. For the enumeration case, the result
931 -- of this call is always a variable, so the call can be used for both the
932 -- expression evaluation and assignment cases.
934 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
);
935 -- N is an N_Assignment_Statement node whose name is an indexed
936 -- component of a bit-packed array. This procedure rewrites the entire
937 -- assignment statement with appropriate code to set the referenced
938 -- bits of the packed array type object. Note that this procedure is
939 -- used only for the bit-packed case, not for the enumeration case.
941 procedure Expand_Packed_Eq
(N
: Node_Id
);
942 -- N is an N_Op_Eq node where the operands are packed arrays whose
943 -- representation is an array-of-bytes type (the case where a modular
944 -- type is used for the representation does not require any special
945 -- handling, because in the modular case, unused bits are zeroes.
947 procedure Expand_Packed_Not
(N
: Node_Id
);
948 -- N is an N_Op_Not node where the operand is packed array of Boolean
949 -- in standard representation (i.e. component size is one bit). This
950 -- procedure expands the corresponding not operation. Note that the
951 -- non-standard representation case is handled by using a loop through
952 -- elements generated by the normal non-packed circuitry.
954 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean;
955 -- N is the node for a name. This function returns true if the name
956 -- involves a packed array reference. A node involves a packed array
957 -- reference if it is itself an indexed component referring to a bit-
958 -- packed array, or it is a selected component whose prefix involves
959 -- a packed array reference.
961 procedure Expand_Packed_Address_Reference
(N
: Node_Id
);
962 -- The node N is an attribute reference for the 'Address reference, where
963 -- the prefix involves a packed array reference. This routine expands the
964 -- necessary code for performing the address reference in this case.
966 procedure Expand_Packed_Bit_Reference
(N
: Node_Id
);
967 -- The node N is an attribute reference for the 'Bit reference, where the
968 -- prefix involves a packed array reference. This routine expands the
969 -- necessary code for performing the bit reference in this case.