1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 a variable of packed array type is allocated, gigi will allocate
90 -- the amount of space indicated by the corresponding packed array type.
91 -- However, we do NOT attempt to rewrite the types of any references or
92 -- to retype the variable itself, since this would cause all kinds of
93 -- semantic problems in the front end (remember that expansion proceeds
94 -- at the same time as analysis).
96 -- For an indexed reference to a packed array, we simply convert the
97 -- reference to the appropriate equivalent reference to the object
98 -- of the packed array type (using unchecked conversion).
100 -- In some cases (for internally generated types, and for the subtypes
101 -- for record fields that depend on a discriminant), the corresponding
102 -- packed type cannot be easily generated in advance. In these cases,
103 -- we generate the required subtype on the fly at the reference point.
105 -- For the modular case, any unused bits are initialized to zero, and
106 -- all operations maintain these bits as zero (where necessary all
107 -- unchecked conversions from corresponding array values require
108 -- these bits to be clear, which is done automatically by gigi).
110 -- For the array cases, there can be unused bits in the last byte, and
111 -- these are neither initialized, nor treated specially in operations
112 -- (i.e. it is allowable for these bits to be clobbered, e.g. by not).
114 ---------------------------
115 -- Endian Considerations --
116 ---------------------------
118 -- The standard does not specify the way in which bits are numbered in
119 -- a packed array. There are two reasonable rules for deciding this:
121 -- Store the first bit at right end (low order) word. This means
122 -- that the scaled subscript can be used directly as a left shift
123 -- count (if we put bit 0 at the left end, then we need an extra
124 -- subtract to compute the shift count).
126 -- Layout the bits so that if the packed boolean array is overlaid on
127 -- a record, using unchecked conversion, then bit 0 of the array is
128 -- the same as the bit numbered bit 0 in a record representation
129 -- clause applying to the record. For example:
131 -- type Rec is record
137 -- for Rec use record
138 -- C at 0 range 0 .. 3;
139 -- D at 0 range 4 .. 10;
140 -- E at 0 range 11 .. 15;
143 -- type P16 is array (0 .. 15) of Boolean;
144 -- pragma Pack (P16);
146 -- Now if we use unchecked conversion to convert a value of the record
147 -- type to the packed array type, according to this second criterion,
148 -- we would expect field D to occupy bits 4..10 of the Boolean array.
150 -- Although not required, this correspondence seems a highly desirable
151 -- property, and is one that GNAT decides to guarantee. For a little
152 -- endian machine, we can also meet the first requirement, but for a
153 -- big endian machine, it will be necessary to store the first bit of
154 -- a Boolean array in the left end (most significant) bit of the word.
155 -- This may cost an extra instruction on some machines, but we consider
156 -- that a worthwhile price to pay for the consistency.
158 -- One more important point arises in the case where we have a constrained
159 -- subtype of an unconstrained array. Take the case of 20 bits. For the
160 -- unconstrained representation, we would use an array of bytes:
162 -- Little-endian case
163 -- 8-7-6-5-4-3-2-1 16-15-14-13-12-11-10-9 x-x-x-x-20-19-18-17
166 -- 1-2-3-4-5-6-7-8 9-10-11-12-13-14-15-16 17-18-19-20-x-x-x-x
168 -- For the constrained case, we use a 20-bit modular value, but in
169 -- general this value may well be stored in 32 bits. Let's look at
170 -- what it looks like:
172 -- Little-endian case
174 -- 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
176 -- which stored in memory looks like
178 -- 8-7-...-2-1 16-15-...-10-9 x-x-x-x-20-19-18-17 x-x-x-x-x-x-x
180 -- An important rule is that the constrained and unconstrained cases
181 -- must have the same bit representation in memory, since we will often
182 -- convert from one to the other (e.g. when calling a procedure whose
183 -- formal is unconstrained). As we see, that criterion is met for the
184 -- little-endian case above. Now let's look at the big-endian case:
188 -- 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
190 -- which stored in memory looks like
192 -- x-x-x-x-x-x-x-x x-x-x-x-1-2-3-4 5-6-...11-12 13-14-...-19-20
194 -- That won't do, the representation value in memory is NOT the same in
195 -- the constrained and unconstrained case. The solution is to store the
196 -- modular value left-justified:
198 -- 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
200 -- which stored in memory looks like
202 -- 1-2-...-7-8 9-10-...15-16 17-18-19-20-x-x-x-x x-x-x-x-x-x-x-x
204 -- and now, we do indeed have the same representation for the memory
205 -- version in the constrained and unconstrained cases.
207 ----------------------------------------------
208 -- Entity Tables for Packed Access Routines --
209 ----------------------------------------------
211 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call library
212 -- routines. These tables provide the entity for the proper routine. They
213 -- are exposed in the spec to allow checking for the presence of the needed
214 -- routine when an array is subject to pragma Pack.
216 type E_Array
is array (Int
range 01 .. 63) of RE_Id
;
218 -- Array of Bits_nn entities. Note that we do not use library routines
219 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
220 -- entries from System.Unsigned, because we also use this table for
221 -- certain special unchecked conversions in the big-endian case.
223 Bits_Id
: constant E_Array
:=
239 16 => RE_Unsigned_16
,
255 32 => RE_Unsigned_32
,
288 -- Array of Get routine entities. These are used to obtain an element from
289 -- a packed array. The N'th entry is used to obtain elements from a packed
290 -- array whose component size is N. RE_Null is used as a null entry, for
291 -- the cases where a library routine is not used.
293 Get_Id
: constant E_Array
:=
358 -- Array of Get routine entities to be used in the case where the packed
359 -- array is itself a component of a packed structure, and therefore may not
360 -- be fully aligned. This only affects the even sizes, since for the odd
361 -- sizes, we do not get any fixed alignment in any case.
363 GetU_Id
: constant E_Array
:=
428 -- Array of Set routine entities. These are used to assign an element of a
429 -- packed array. The N'th entry is used to assign elements for a packed
430 -- array whose component size is N. RE_Null is used as a null entry, for
431 -- the cases where a library routine is not used.
433 Set_Id
: constant E_Array
:=
498 -- Array of Set routine entities to be used in the case where the packed
499 -- array is itself a component of a packed structure, and therefore may not
500 -- be fully aligned. This only affects the even sizes, since for the odd
501 -- sizes, we do not get any fixed alignment in any case.
503 SetU_Id
: constant E_Array
:=
572 procedure Create_Packed_Array_Impl_Type
(Typ
: Entity_Id
);
573 -- Typ is a array type or subtype to which pragma Pack applies. If the
574 -- Packed_Array_Impl_Type field of Typ is already set, then the call has
575 -- no effect, otherwise a suitable type or subtype is created and stored in
576 -- the Packed_Array_Impl_Type field of Typ. This created type is an Itype
577 -- so that Gigi will simply elaborate and freeze the type on first use
578 -- (which is typically the definition of the corresponding array type).
580 -- Note: although this routine is included in the expander package for
581 -- packed types, it is actually called unconditionally from Freeze,
582 -- whether or not expansion (and code generation) is enabled. We do this
583 -- since we want gigi to be able to properly compute type characteristics
584 -- (for the Data Decomposition Annex of ASIS, and possible other future
585 -- uses) even if code generation is not active. Strictly this means that
586 -- this procedure is not part of the expander, but it seems appropriate
587 -- to keep it together with the other expansion routines that have to do
588 -- with packed array types.
590 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
);
591 -- N is an N_Op_And, N_Op_Or or N_Op_Xor node whose operand type is a
592 -- packed boolean array. This routine expands the appropriate operations
593 -- to carry out the logical operation on the packed arrays. It handles
594 -- both the modular and array representation cases.
596 procedure Expand_Packed_Element_Reference
(N
: Node_Id
);
597 -- N is an N_Indexed_Component node whose prefix is a packed array. In
598 -- the bit packed case, this routine can only be used for the expression
599 -- evaluation case, not the assignment case, since the result is not a
600 -- variable. See Expand_Bit_Packed_Element_Set for how the assignment case
601 -- is handled in the bit packed case. For the enumeration case, the result
602 -- of this call is always a variable, so the call can be used for both the
603 -- expression evaluation and assignment cases.
605 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
);
606 -- N is an N_Assignment_Statement node whose name is an indexed
607 -- component of a bit-packed array. This procedure rewrites the entire
608 -- assignment statement with appropriate code to set the referenced
609 -- bits of the packed array type object. Note that this procedure is
610 -- used only for the bit-packed case, not for the enumeration case.
612 procedure Expand_Packed_Eq
(N
: Node_Id
);
613 -- N is an N_Op_Eq node where the operands are packed arrays whose
614 -- representation is an array-of-bytes type (the case where a modular
615 -- type is used for the representation does not require any special
616 -- handling, because in the modular case, unused bits are zeroes.
618 procedure Expand_Packed_Not
(N
: Node_Id
);
619 -- N is an N_Op_Not node where the operand is packed array of Boolean
620 -- in standard representation (i.e. component size is one bit). This
621 -- procedure expands the corresponding not operation. Note that the
622 -- non-standard representation case is handled by using a loop through
623 -- elements generated by the normal non-packed circuitry.
625 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean;
626 -- N is the node for a name. This function returns true if the name
627 -- involves a packed array reference. A node involves a packed array
628 -- reference if it is itself an indexed component referring to a bit-
629 -- packed array, or it is a selected component whose prefix involves
630 -- a packed array reference.
632 procedure Expand_Packed_Address_Reference
(N
: Node_Id
);
633 -- The node N is an attribute reference for the 'Address reference, where
634 -- the prefix involves a packed array reference. This routine expands the
635 -- necessary code for performing the address reference in this case.
637 procedure Expand_Packed_Bit_Reference
(N
: Node_Id
);
638 -- The node N is an attribute reference for the 'Bit reference, where the
639 -- prefix involves a packed array reference. This routine expands the
640 -- necessary code for performing the bit reference in this case.