2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / exp_pakd.ads
blob9c3044bddff87e3a3da2b645a22fccce0254ba8c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ P A K D --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
10 -- --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 -- Expand routines for manipulation of packed arrays
29 with Types; use Types;
31 package Exp_Pakd is
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
39 -- store the entity for this type in the Packed_Array_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 right 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
132 -- C : Bits4;
133 -- D : Bits7;
134 -- E : Bits5;
135 -- end 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;
141 -- end record;
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
165 -- Big-endian case
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:
186 -- 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. The special flag
205 -- Is_Left_Justified_Modular is set in the modular type used as the
206 -- packed array type in the big-endian case to ensure that this required
207 -- left justification occurs.
209 -----------------
210 -- Subprograms --
211 -----------------
213 procedure Create_Packed_Array_Type (Typ : Entity_Id);
214 -- Typ is a array type or subtype to which pragma Pack applies. If the
215 -- Packed_Array_Type field of Typ is already set, then the call has no
216 -- effect, otherwise a suitable type or subtype is created and stored
217 -- in the Packed_Array_Type field of Typ. This created type is an Itype
218 -- so that Gigi will simply elaborate and freeze the type on first use
219 -- (which is typically the definition of the corresponding array type).
221 -- Note: although this routine is included in the expander package for
222 -- packed types, it is actually called unconditionally from Freeze,
223 -- whether or not expansion (and code generation) is enabled. We do this
224 -- since we want gigi to be able to properly compute type charactersitics
225 -- (for the Data Decomposition Annex of ASIS, and possible other future
226 -- uses) even if code generation is not active. Strictly this means that
227 -- this procedure is not part of the expander, but it seems appropriate
228 -- to keep it together with the other expansion routines that have to do
229 -- with packed array types.
231 procedure Expand_Packed_Boolean_Operator (N : Node_Id);
232 -- N is an N_Op_And, N_Op_Or or N_Op_Xor node whose operand type is a
233 -- packed boolean array. This routine expands the appropriate operations
234 -- to carry out the logical operation on the packed arrays. It handles
235 -- both the modular and array representation cases.
237 procedure Expand_Packed_Element_Reference (N : Node_Id);
238 -- N is an N_Indexed_Component node whose prefix is a packed array. In
239 -- the bit packed case, this routine can only be used for the expression
240 -- evaluation case not the assignment case, since the result is not a
241 -- variable. See Expand_Bit_Packed_Element_Set for how he assignment case
242 -- is handled in the bit packed case. For the enumeration case, the result
243 -- of this call is always a variable, so the call can be used for both the
244 -- expression evaluation and assignment cases.
246 procedure Expand_Bit_Packed_Element_Set (N : Node_Id);
247 -- N is an N_Assignment_Statement node whose name is an indexed
248 -- component of a bit-packed array. This procedure rewrites the entire
249 -- assignment statement with appropriate code to set the referenced
250 -- bits of the packed array type object. Note that this procedure is
251 -- used only for the bit-packed case, not for the enumeration case.
253 procedure Expand_Packed_Eq (N : Node_Id);
254 -- N is an N_Op_Eq node where the operands are packed arrays whose
255 -- representation is an array-of-bytes type (the case where a modular
256 -- type is used for the representation does not require any special
257 -- handling, because in the modular case, unused bits are zeroes.
259 procedure Expand_Packed_Not (N : Node_Id);
260 -- N is an N_Op_Not node where the operand is packed array of Boolean
261 -- in standard representation (i.e. component size is one bit). This
262 -- procedure expands the corresponding not operation. Note that the
263 -- non-standard representation case is handled by using a loop through
264 -- elements generated by the normal non-packed circuitry.
266 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean;
267 -- N is the node for a name. This function returns true if the name
268 -- involves a packed array reference. A node involves a packed array
269 -- reference if it is itself an indexed compoment referring to a bit-
270 -- packed array, or it is a selected component whose prefix involves
271 -- a packed array reference.
273 procedure Expand_Packed_Address_Reference (N : Node_Id);
274 -- The node N is an attribute reference for the 'Address reference, where
275 -- the prefix involves a packed array reference. This routine expands the
276 -- necessary code for performing the address reference in this case.
278 end Exp_Pakd;