OSDN Git Service

2007-08-14 Tristan Gingold <gingold@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_pakd.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P _ P A K D                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Einfo;    use Einfo;
30 with Errout;   use Errout;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Layout;   use Layout;
34 with Namet;    use Namet;
35 with Nlists;   use Nlists;
36 with Nmake;    use Nmake;
37 with Rtsfind;  use Rtsfind;
38 with Sem;      use Sem;
39 with Sem_Ch3;  use Sem_Ch3;
40 with Sem_Ch8;  use Sem_Ch8;
41 with Sem_Ch13; use Sem_Ch13;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Res;  use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sinfo;    use Sinfo;
46 with Snames;   use Snames;
47 with Stand;    use Stand;
48 with Targparm; use Targparm;
49 with Tbuild;   use Tbuild;
50 with Ttypes;   use Ttypes;
51 with Uintp;    use Uintp;
52
53 package body Exp_Pakd is
54
55    ---------------------------
56    -- Endian Considerations --
57    ---------------------------
58
59    --  As described in the specification, bit numbering in a packed array
60    --  is consistent with bit numbering in a record representation clause,
61    --  and hence dependent on the endianness of the machine:
62
63    --    For little-endian machines, element zero is at the right hand end
64    --    (low order end) of a bit field.
65
66    --    For big-endian machines, element zero is at the left hand end
67    --    (high order end) of a bit field.
68
69    --  The shifts that are used to right justify a field therefore differ
70    --  in the two cases. For the little-endian case, we can simply use the
71    --  bit number (i.e. the element number * element size) as the count for
72    --  a right shift. For the big-endian case, we have to subtract the shift
73    --  count from an appropriate constant to use in the right shift. We use
74    --  rotates instead of shifts (which is necessary in the store case to
75    --  preserve other fields), and we expect that the backend will be able
76    --  to change the right rotate into a left rotate, avoiding the subtract,
77    --  if the architecture provides such an instruction.
78
79    ----------------------------------------------
80    -- Entity Tables for Packed Access Routines --
81    ----------------------------------------------
82
83    --  For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
84    --  library routines. This table is used to obtain the entity for the
85    --  proper routine.
86
87    type E_Array is array (Int range 01 .. 63) of RE_Id;
88
89    --  Array of Bits_nn entities. Note that we do not use library routines
90    --  for the 8-bit and 16-bit cases, but we still fill in the table, using
91    --  entries from System.Unsigned, because we also use this table for
92    --  certain special unchecked conversions in the big-endian case.
93
94    Bits_Id : constant E_Array :=
95      (01 => RE_Bits_1,
96       02 => RE_Bits_2,
97       03 => RE_Bits_03,
98       04 => RE_Bits_4,
99       05 => RE_Bits_05,
100       06 => RE_Bits_06,
101       07 => RE_Bits_07,
102       08 => RE_Unsigned_8,
103       09 => RE_Bits_09,
104       10 => RE_Bits_10,
105       11 => RE_Bits_11,
106       12 => RE_Bits_12,
107       13 => RE_Bits_13,
108       14 => RE_Bits_14,
109       15 => RE_Bits_15,
110       16 => RE_Unsigned_16,
111       17 => RE_Bits_17,
112       18 => RE_Bits_18,
113       19 => RE_Bits_19,
114       20 => RE_Bits_20,
115       21 => RE_Bits_21,
116       22 => RE_Bits_22,
117       23 => RE_Bits_23,
118       24 => RE_Bits_24,
119       25 => RE_Bits_25,
120       26 => RE_Bits_26,
121       27 => RE_Bits_27,
122       28 => RE_Bits_28,
123       29 => RE_Bits_29,
124       30 => RE_Bits_30,
125       31 => RE_Bits_31,
126       32 => RE_Unsigned_32,
127       33 => RE_Bits_33,
128       34 => RE_Bits_34,
129       35 => RE_Bits_35,
130       36 => RE_Bits_36,
131       37 => RE_Bits_37,
132       38 => RE_Bits_38,
133       39 => RE_Bits_39,
134       40 => RE_Bits_40,
135       41 => RE_Bits_41,
136       42 => RE_Bits_42,
137       43 => RE_Bits_43,
138       44 => RE_Bits_44,
139       45 => RE_Bits_45,
140       46 => RE_Bits_46,
141       47 => RE_Bits_47,
142       48 => RE_Bits_48,
143       49 => RE_Bits_49,
144       50 => RE_Bits_50,
145       51 => RE_Bits_51,
146       52 => RE_Bits_52,
147       53 => RE_Bits_53,
148       54 => RE_Bits_54,
149       55 => RE_Bits_55,
150       56 => RE_Bits_56,
151       57 => RE_Bits_57,
152       58 => RE_Bits_58,
153       59 => RE_Bits_59,
154       60 => RE_Bits_60,
155       61 => RE_Bits_61,
156       62 => RE_Bits_62,
157       63 => RE_Bits_63);
158
159    --  Array of Get routine entities. These are used to obtain an element
160    --  from a packed array. The N'th entry is used to obtain elements from
161    --  a packed array whose component size is N. RE_Null is used as a null
162    --  entry, for the cases where a library routine is not used.
163
164    Get_Id : constant E_Array :=
165      (01 => RE_Null,
166       02 => RE_Null,
167       03 => RE_Get_03,
168       04 => RE_Null,
169       05 => RE_Get_05,
170       06 => RE_Get_06,
171       07 => RE_Get_07,
172       08 => RE_Null,
173       09 => RE_Get_09,
174       10 => RE_Get_10,
175       11 => RE_Get_11,
176       12 => RE_Get_12,
177       13 => RE_Get_13,
178       14 => RE_Get_14,
179       15 => RE_Get_15,
180       16 => RE_Null,
181       17 => RE_Get_17,
182       18 => RE_Get_18,
183       19 => RE_Get_19,
184       20 => RE_Get_20,
185       21 => RE_Get_21,
186       22 => RE_Get_22,
187       23 => RE_Get_23,
188       24 => RE_Get_24,
189       25 => RE_Get_25,
190       26 => RE_Get_26,
191       27 => RE_Get_27,
192       28 => RE_Get_28,
193       29 => RE_Get_29,
194       30 => RE_Get_30,
195       31 => RE_Get_31,
196       32 => RE_Null,
197       33 => RE_Get_33,
198       34 => RE_Get_34,
199       35 => RE_Get_35,
200       36 => RE_Get_36,
201       37 => RE_Get_37,
202       38 => RE_Get_38,
203       39 => RE_Get_39,
204       40 => RE_Get_40,
205       41 => RE_Get_41,
206       42 => RE_Get_42,
207       43 => RE_Get_43,
208       44 => RE_Get_44,
209       45 => RE_Get_45,
210       46 => RE_Get_46,
211       47 => RE_Get_47,
212       48 => RE_Get_48,
213       49 => RE_Get_49,
214       50 => RE_Get_50,
215       51 => RE_Get_51,
216       52 => RE_Get_52,
217       53 => RE_Get_53,
218       54 => RE_Get_54,
219       55 => RE_Get_55,
220       56 => RE_Get_56,
221       57 => RE_Get_57,
222       58 => RE_Get_58,
223       59 => RE_Get_59,
224       60 => RE_Get_60,
225       61 => RE_Get_61,
226       62 => RE_Get_62,
227       63 => RE_Get_63);
228
229    --  Array of Get routine entities to be used in the case where the packed
230    --  array is itself a component of a packed structure, and therefore may
231    --  not be fully aligned. This only affects the even sizes, since for the
232    --  odd sizes, we do not get any fixed alignment in any case.
233
234    GetU_Id : constant E_Array :=
235      (01 => RE_Null,
236       02 => RE_Null,
237       03 => RE_Get_03,
238       04 => RE_Null,
239       05 => RE_Get_05,
240       06 => RE_GetU_06,
241       07 => RE_Get_07,
242       08 => RE_Null,
243       09 => RE_Get_09,
244       10 => RE_GetU_10,
245       11 => RE_Get_11,
246       12 => RE_GetU_12,
247       13 => RE_Get_13,
248       14 => RE_GetU_14,
249       15 => RE_Get_15,
250       16 => RE_Null,
251       17 => RE_Get_17,
252       18 => RE_GetU_18,
253       19 => RE_Get_19,
254       20 => RE_GetU_20,
255       21 => RE_Get_21,
256       22 => RE_GetU_22,
257       23 => RE_Get_23,
258       24 => RE_GetU_24,
259       25 => RE_Get_25,
260       26 => RE_GetU_26,
261       27 => RE_Get_27,
262       28 => RE_GetU_28,
263       29 => RE_Get_29,
264       30 => RE_GetU_30,
265       31 => RE_Get_31,
266       32 => RE_Null,
267       33 => RE_Get_33,
268       34 => RE_GetU_34,
269       35 => RE_Get_35,
270       36 => RE_GetU_36,
271       37 => RE_Get_37,
272       38 => RE_GetU_38,
273       39 => RE_Get_39,
274       40 => RE_GetU_40,
275       41 => RE_Get_41,
276       42 => RE_GetU_42,
277       43 => RE_Get_43,
278       44 => RE_GetU_44,
279       45 => RE_Get_45,
280       46 => RE_GetU_46,
281       47 => RE_Get_47,
282       48 => RE_GetU_48,
283       49 => RE_Get_49,
284       50 => RE_GetU_50,
285       51 => RE_Get_51,
286       52 => RE_GetU_52,
287       53 => RE_Get_53,
288       54 => RE_GetU_54,
289       55 => RE_Get_55,
290       56 => RE_GetU_56,
291       57 => RE_Get_57,
292       58 => RE_GetU_58,
293       59 => RE_Get_59,
294       60 => RE_GetU_60,
295       61 => RE_Get_61,
296       62 => RE_GetU_62,
297       63 => RE_Get_63);
298
299    --  Array of Set routine entities. These are used to assign an element
300    --  of a packed array. The N'th entry is used to assign elements for
301    --  a packed array whose component size is N. RE_Null is used as a null
302    --  entry, for the cases where a library routine is not used.
303
304    Set_Id : constant E_Array :=
305      (01 => RE_Null,
306       02 => RE_Null,
307       03 => RE_Set_03,
308       04 => RE_Null,
309       05 => RE_Set_05,
310       06 => RE_Set_06,
311       07 => RE_Set_07,
312       08 => RE_Null,
313       09 => RE_Set_09,
314       10 => RE_Set_10,
315       11 => RE_Set_11,
316       12 => RE_Set_12,
317       13 => RE_Set_13,
318       14 => RE_Set_14,
319       15 => RE_Set_15,
320       16 => RE_Null,
321       17 => RE_Set_17,
322       18 => RE_Set_18,
323       19 => RE_Set_19,
324       20 => RE_Set_20,
325       21 => RE_Set_21,
326       22 => RE_Set_22,
327       23 => RE_Set_23,
328       24 => RE_Set_24,
329       25 => RE_Set_25,
330       26 => RE_Set_26,
331       27 => RE_Set_27,
332       28 => RE_Set_28,
333       29 => RE_Set_29,
334       30 => RE_Set_30,
335       31 => RE_Set_31,
336       32 => RE_Null,
337       33 => RE_Set_33,
338       34 => RE_Set_34,
339       35 => RE_Set_35,
340       36 => RE_Set_36,
341       37 => RE_Set_37,
342       38 => RE_Set_38,
343       39 => RE_Set_39,
344       40 => RE_Set_40,
345       41 => RE_Set_41,
346       42 => RE_Set_42,
347       43 => RE_Set_43,
348       44 => RE_Set_44,
349       45 => RE_Set_45,
350       46 => RE_Set_46,
351       47 => RE_Set_47,
352       48 => RE_Set_48,
353       49 => RE_Set_49,
354       50 => RE_Set_50,
355       51 => RE_Set_51,
356       52 => RE_Set_52,
357       53 => RE_Set_53,
358       54 => RE_Set_54,
359       55 => RE_Set_55,
360       56 => RE_Set_56,
361       57 => RE_Set_57,
362       58 => RE_Set_58,
363       59 => RE_Set_59,
364       60 => RE_Set_60,
365       61 => RE_Set_61,
366       62 => RE_Set_62,
367       63 => RE_Set_63);
368
369    --  Array of Set routine entities to be used in the case where the packed
370    --  array is itself a component of a packed structure, and therefore may
371    --  not be fully aligned. This only affects the even sizes, since for the
372    --  odd sizes, we do not get any fixed alignment in any case.
373
374    SetU_Id : constant E_Array :=
375      (01 => RE_Null,
376       02 => RE_Null,
377       03 => RE_Set_03,
378       04 => RE_Null,
379       05 => RE_Set_05,
380       06 => RE_SetU_06,
381       07 => RE_Set_07,
382       08 => RE_Null,
383       09 => RE_Set_09,
384       10 => RE_SetU_10,
385       11 => RE_Set_11,
386       12 => RE_SetU_12,
387       13 => RE_Set_13,
388       14 => RE_SetU_14,
389       15 => RE_Set_15,
390       16 => RE_Null,
391       17 => RE_Set_17,
392       18 => RE_SetU_18,
393       19 => RE_Set_19,
394       20 => RE_SetU_20,
395       21 => RE_Set_21,
396       22 => RE_SetU_22,
397       23 => RE_Set_23,
398       24 => RE_SetU_24,
399       25 => RE_Set_25,
400       26 => RE_SetU_26,
401       27 => RE_Set_27,
402       28 => RE_SetU_28,
403       29 => RE_Set_29,
404       30 => RE_SetU_30,
405       31 => RE_Set_31,
406       32 => RE_Null,
407       33 => RE_Set_33,
408       34 => RE_SetU_34,
409       35 => RE_Set_35,
410       36 => RE_SetU_36,
411       37 => RE_Set_37,
412       38 => RE_SetU_38,
413       39 => RE_Set_39,
414       40 => RE_SetU_40,
415       41 => RE_Set_41,
416       42 => RE_SetU_42,
417       43 => RE_Set_43,
418       44 => RE_SetU_44,
419       45 => RE_Set_45,
420       46 => RE_SetU_46,
421       47 => RE_Set_47,
422       48 => RE_SetU_48,
423       49 => RE_Set_49,
424       50 => RE_SetU_50,
425       51 => RE_Set_51,
426       52 => RE_SetU_52,
427       53 => RE_Set_53,
428       54 => RE_SetU_54,
429       55 => RE_Set_55,
430       56 => RE_SetU_56,
431       57 => RE_Set_57,
432       58 => RE_SetU_58,
433       59 => RE_Set_59,
434       60 => RE_SetU_60,
435       61 => RE_Set_61,
436       62 => RE_SetU_62,
437       63 => RE_Set_63);
438
439    -----------------------
440    -- Local Subprograms --
441    -----------------------
442
443    procedure Compute_Linear_Subscript
444      (Atyp   : Entity_Id;
445       N      : Node_Id;
446       Subscr : out Node_Id);
447    --  Given a constrained array type Atyp, and an indexed component node
448    --  N referencing an array object of this type, build an expression of
449    --  type Standard.Integer representing the zero-based linear subscript
450    --  value. This expression includes any required range checks.
451
452    procedure Convert_To_PAT_Type (Aexp : Node_Id);
453    --  Given an expression of a packed array type, builds a corresponding
454    --  expression whose type is the implementation type used to represent
455    --  the packed array. Aexp is analyzed and resolved on entry and on exit.
456
457    function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
458    --  There are two versions of the Set routines, the ones used when the
459    --  object is known to be sufficiently well aligned given the number of
460    --  bits, and the ones used when the object is not known to be aligned.
461    --  This routine is used to determine which set to use. Obj is a reference
462    --  to the object, and Csiz is the component size of the packed array.
463    --  True is returned if the alignment of object is known to be sufficient,
464    --  defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
465    --  2 otherwise.
466
467    function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
468    --  Build a left shift node, checking for the case of a shift count of zero
469
470    function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
471    --  Build a right shift node, checking for the case of a shift count of zero
472
473    function RJ_Unchecked_Convert_To
474      (Typ  : Entity_Id;
475       Expr : Node_Id) return Node_Id;
476    --  The packed array code does unchecked conversions which in some cases
477    --  may involve non-discrete types with differing sizes. The semantics of
478    --  such conversions is potentially endian dependent, and the effect we
479    --  want here for such a conversion is to do the conversion in size as
480    --  though numeric items are involved, and we extend or truncate on the
481    --  left side. This happens naturally in the little-endian case, but in
482    --  the big endian case we can get left justification, when what we want
483    --  is right justification. This routine does the unchecked conversion in
484    --  a stepwise manner to ensure that it gives the expected result. Hence
485    --  the name (RJ = Right justified). The parameters Typ and Expr are as
486    --  for the case of a normal Unchecked_Convert_To call.
487
488    procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
489    --  This routine is called in the Get and Set case for arrays that are
490    --  packed but not bit-packed, meaning that they have at least one
491    --  subscript that is of an enumeration type with a non-standard
492    --  representation. This routine modifies the given node to properly
493    --  reference the corresponding packed array type.
494
495    procedure Setup_Inline_Packed_Array_Reference
496      (N      : Node_Id;
497       Atyp   : Entity_Id;
498       Obj    : in out Node_Id;
499       Cmask  : out Uint;
500       Shift  : out Node_Id);
501    --  This procedure performs common processing on the N_Indexed_Component
502    --  parameter given as N, whose prefix is a reference to a packed array.
503    --  This is used for the get and set when the component size is 1,2,4
504    --  or for other component sizes when the packed array type is a modular
505    --  type (i.e. the cases that are handled with inline code).
506    --
507    --  On entry:
508    --
509    --    N is the N_Indexed_Component node for the packed array reference
510    --
511    --    Atyp is the constrained array type (the actual subtype has been
512    --    computed if necessary to obtain the constraints, but this is still
513    --    the original array type, not the Packed_Array_Type value).
514    --
515    --    Obj is the object which is to be indexed. It is always of type Atyp.
516    --
517    --  On return:
518    --
519    --    Obj is the object containing the desired bit field. It is of type
520    --    Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
521    --    entire value, for the small static case, or the proper selected byte
522    --    from the array in the large or dynamic case. This node is analyzed
523    --    and resolved on return.
524    --
525    --    Shift is a node representing the shift count to be used in the
526    --    rotate right instruction that positions the field for access.
527    --    This node is analyzed and resolved on return.
528    --
529    --    Cmask is a mask corresponding to the width of the component field.
530    --    Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
531    --
532    --  Note: in some cases the call to this routine may generate actions
533    --  (for handling multi-use references and the generation of the packed
534    --  array type on the fly). Such actions are inserted into the tree
535    --  directly using Insert_Action.
536
537    ------------------------------
538    -- Compute_Linear_Subcsript --
539    ------------------------------
540
541    procedure Compute_Linear_Subscript
542      (Atyp   : Entity_Id;
543       N      : Node_Id;
544       Subscr : out Node_Id)
545    is
546       Loc    : constant Source_Ptr := Sloc (N);
547       Oldsub : Node_Id;
548       Newsub : Node_Id;
549       Indx   : Node_Id;
550       Styp   : Entity_Id;
551
552    begin
553       Subscr := Empty;
554
555       --  Loop through dimensions
556
557       Indx   := First_Index (Atyp);
558       Oldsub := First (Expressions (N));
559
560       while Present (Indx) loop
561          Styp := Etype (Indx);
562          Newsub := Relocate_Node (Oldsub);
563
564          --  Get expression for the subscript value. First, if Do_Range_Check
565          --  is set on a subscript, then we must do a range check against the
566          --  original bounds (not the bounds of the packed array type). We do
567          --  this by introducing a subtype conversion.
568
569          if Do_Range_Check (Newsub)
570            and then Etype (Newsub) /= Styp
571          then
572             Newsub := Convert_To (Styp, Newsub);
573          end if;
574
575          --  Now evolve the expression for the subscript. First convert
576          --  the subscript to be zero based and of an integer type.
577
578          --  Case of integer type, where we just subtract to get lower bound
579
580          if Is_Integer_Type (Styp) then
581
582             --  If length of integer type is smaller than standard integer,
583             --  then we convert to integer first, then do the subtract
584
585             --  Integer (subscript) - Integer (Styp'First)
586
587             if Esize (Styp) < Esize (Standard_Integer) then
588                Newsub :=
589                  Make_Op_Subtract (Loc,
590                    Left_Opnd => Convert_To (Standard_Integer, Newsub),
591                  Right_Opnd =>
592                    Convert_To (Standard_Integer,
593                      Make_Attribute_Reference (Loc,
594                        Prefix         => New_Occurrence_Of (Styp, Loc),
595                        Attribute_Name => Name_First)));
596
597             --  For larger integer types, subtract first, then convert to
598             --  integer, this deals with strange long long integer bounds.
599
600             --    Integer (subscript - Styp'First)
601
602             else
603                Newsub :=
604                  Convert_To (Standard_Integer,
605                    Make_Op_Subtract (Loc,
606                      Left_Opnd => Newsub,
607                    Right_Opnd =>
608                      Make_Attribute_Reference (Loc,
609                        Prefix         => New_Occurrence_Of (Styp, Loc),
610                        Attribute_Name => Name_First)));
611             end if;
612
613          --  For the enumeration case, we have to use 'Pos to get the value
614          --  to work with before subtracting the lower bound.
615
616          --    Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
617
618          --  This is not quite right for bizarre cases where the size of the
619          --  enumeration type is > Integer'Size bits due to rep clause ???
620
621          else
622             pragma Assert (Is_Enumeration_Type (Styp));
623
624             Newsub :=
625               Make_Op_Subtract (Loc,
626                 Left_Opnd => Convert_To (Standard_Integer,
627                   Make_Attribute_Reference (Loc,
628                     Prefix         => New_Occurrence_Of (Styp, Loc),
629                     Attribute_Name => Name_Pos,
630                     Expressions    => New_List (Newsub))),
631
632                 Right_Opnd =>
633                   Convert_To (Standard_Integer,
634                     Make_Attribute_Reference (Loc,
635                       Prefix         => New_Occurrence_Of (Styp, Loc),
636                       Attribute_Name => Name_Pos,
637                       Expressions    => New_List (
638                         Make_Attribute_Reference (Loc,
639                         Prefix         => New_Occurrence_Of (Styp, Loc),
640                         Attribute_Name => Name_First)))));
641          end if;
642
643          Set_Paren_Count (Newsub, 1);
644
645          --  For the first subscript, we just copy that subscript value
646
647          if No (Subscr) then
648             Subscr := Newsub;
649
650          --  Otherwise, we must multiply what we already have by the current
651          --  stride and then add in the new value to the evolving subscript.
652
653          else
654             Subscr :=
655               Make_Op_Add (Loc,
656                 Left_Opnd =>
657                   Make_Op_Multiply (Loc,
658                     Left_Opnd  => Subscr,
659                     Right_Opnd =>
660                       Make_Attribute_Reference (Loc,
661                         Attribute_Name => Name_Range_Length,
662                         Prefix         => New_Occurrence_Of (Styp, Loc))),
663                 Right_Opnd => Newsub);
664          end if;
665
666          --  Move to next subscript
667
668          Next_Index (Indx);
669          Next (Oldsub);
670       end loop;
671    end Compute_Linear_Subscript;
672
673    -------------------------
674    -- Convert_To_PAT_Type --
675    -------------------------
676
677    --  The PAT is always obtained from the actual subtype
678
679    procedure Convert_To_PAT_Type (Aexp : Node_Id) is
680       Act_ST : Entity_Id;
681
682    begin
683       Convert_To_Actual_Subtype (Aexp);
684       Act_ST := Underlying_Type (Etype (Aexp));
685       Create_Packed_Array_Type (Act_ST);
686
687       --  Just replace the etype with the packed array type. This works because
688       --  the expression will not be further analyzed, and Gigi considers the
689       --  two types equivalent in any case.
690
691       --  This is not strictly the case ??? If the reference is an actual in
692       --  call, the expansion of the prefix is delayed, and must be reanalyzed,
693       --  see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
694       --  array reference, reanalysis can produce spurious type errors when the
695       --  PAT type is replaced again with the original type of the array. Same
696       --  for the case of a dereference. The following is correct and minimal,
697       --  but the handling of more complex packed expressions in actuals is
698       --  confused. Probably the problem only remains for actuals in calls.
699
700       Set_Etype (Aexp, Packed_Array_Type (Act_ST));
701
702       if Is_Entity_Name (Aexp)
703         or else
704            (Nkind (Aexp) = N_Indexed_Component
705              and then Is_Entity_Name (Prefix (Aexp)))
706         or else Nkind (Aexp) = N_Explicit_Dereference
707       then
708          Set_Analyzed (Aexp);
709       end if;
710    end Convert_To_PAT_Type;
711
712    ------------------------------
713    -- Create_Packed_Array_Type --
714    ------------------------------
715
716    procedure Create_Packed_Array_Type (Typ : Entity_Id) is
717       Loc      : constant Source_Ptr := Sloc (Typ);
718       Ctyp     : constant Entity_Id  := Component_Type (Typ);
719       Csize    : constant Uint       := Component_Size (Typ);
720
721       Ancest   : Entity_Id;
722       PB_Type  : Entity_Id;
723       PASize   : Uint;
724       Decl     : Node_Id;
725       PAT      : Entity_Id;
726       Len_Dim  : Node_Id;
727       Len_Expr : Node_Id;
728       Len_Bits : Uint;
729       Bits_U1  : Node_Id;
730       PAT_High : Node_Id;
731       Btyp     : Entity_Id;
732       Lit      : Node_Id;
733
734       procedure Install_PAT;
735       --  This procedure is called with Decl set to the declaration for the
736       --  packed array type. It creates the type and installs it as required.
737
738       procedure Set_PB_Type;
739       --  Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
740       --  requirements (see documentation in the spec of this package).
741
742       -----------------
743       -- Install_PAT --
744       -----------------
745
746       procedure Install_PAT is
747          Pushed_Scope : Boolean := False;
748
749       begin
750          --  We do not want to put the declaration we have created in the tree
751          --  since it is often hard, and sometimes impossible to find a proper
752          --  place for it (the impossible case arises for a packed array type
753          --  with bounds depending on the discriminant, a declaration cannot
754          --  be put inside the record, and the reference to the discriminant
755          --  cannot be outside the record).
756
757          --  The solution is to analyze the declaration while temporarily
758          --  attached to the tree at an appropriate point, and then we install
759          --  the resulting type as an Itype in the packed array type field of
760          --  the original type, so that no explicit declaration is required.
761
762          --  Note: the packed type is created in the scope of its parent
763          --  type. There are at least some cases where the current scope
764          --  is deeper, and so when this is the case, we temporarily reset
765          --  the scope for the definition. This is clearly safe, since the
766          --  first use of the packed array type will be the implicit
767          --  reference from the corresponding unpacked type when it is
768          --  elaborated.
769
770          if Is_Itype (Typ) then
771             Set_Parent (Decl, Associated_Node_For_Itype (Typ));
772          else
773             Set_Parent (Decl, Declaration_Node (Typ));
774          end if;
775
776          if Scope (Typ) /= Current_Scope then
777             Push_Scope (Scope (Typ));
778             Pushed_Scope := True;
779          end if;
780
781          Set_Is_Itype (PAT, True);
782          Set_Packed_Array_Type (Typ, PAT);
783          Analyze (Decl, Suppress => All_Checks);
784
785          if Pushed_Scope then
786             Pop_Scope;
787          end if;
788
789          --  Set Esize and RM_Size to the actual size of the packed object
790          --  Do not reset RM_Size if already set, as happens in the case of
791          --  a modular type.
792
793          if Unknown_Esize (PAT) then
794             Set_Esize (PAT, PASize);
795          end if;
796
797          if Unknown_RM_Size (PAT) then
798             Set_RM_Size (PAT, PASize);
799          end if;
800
801          Adjust_Esize_Alignment (PAT);
802
803          --  Set remaining fields of packed array type
804
805          Init_Alignment                (PAT);
806          Set_Parent                    (PAT, Empty);
807          Set_Associated_Node_For_Itype (PAT, Typ);
808          Set_Is_Packed_Array_Type      (PAT, True);
809          Set_Original_Array_Type       (PAT, Typ);
810
811          --  We definitely do not want to delay freezing for packed array
812          --  types. This is of particular importance for the itypes that
813          --  are generated for record components depending on discriminants
814          --  where there is no place to put the freeze node.
815
816          Set_Has_Delayed_Freeze (PAT, False);
817          Set_Has_Delayed_Freeze (Etype (PAT), False);
818
819          --  If we did allocate a freeze node, then clear out the reference
820          --  since it is obsolete (should we delete the freeze node???)
821
822          Set_Freeze_Node (PAT, Empty);
823          Set_Freeze_Node (Etype (PAT), Empty);
824       end Install_PAT;
825
826       -----------------
827       -- Set_PB_Type --
828       -----------------
829
830       procedure Set_PB_Type is
831       begin
832          --  If the user has specified an explicit alignment for the
833          --  type or component, take it into account.
834
835          if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
836            or else Alignment (Typ) = 1
837            or else Component_Alignment (Typ) = Calign_Storage_Unit
838          then
839             PB_Type := RTE (RE_Packed_Bytes1);
840
841          elsif Csize mod 4 /= 0
842            or else Alignment (Typ) = 2
843          then
844             PB_Type := RTE (RE_Packed_Bytes2);
845
846          else
847             PB_Type := RTE (RE_Packed_Bytes4);
848          end if;
849       end Set_PB_Type;
850
851    --  Start of processing for Create_Packed_Array_Type
852
853    begin
854       --  If we already have a packed array type, nothing to do
855
856       if Present (Packed_Array_Type (Typ)) then
857          return;
858       end if;
859
860       --  If our immediate ancestor subtype is constrained, and it already
861       --  has a packed array type, then just share the same type, since the
862       --  bounds must be the same. If the ancestor is not an array type but
863       --  a private type, as can happen with multiple instantiations, create
864       --  a new packed type, to avoid privacy issues.
865
866       if Ekind (Typ) = E_Array_Subtype then
867          Ancest := Ancestor_Subtype (Typ);
868
869          if Present (Ancest)
870            and then Is_Array_Type (Ancest)
871            and then Is_Constrained (Ancest)
872            and then Present (Packed_Array_Type (Ancest))
873          then
874             Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
875             return;
876          end if;
877       end if;
878
879       --  We preset the result type size from the size of the original array
880       --  type, since this size clearly belongs to the packed array type. The
881       --  size of the conceptual unpacked type is always set to unknown.
882
883       PASize := RM_Size (Typ);
884
885       --  Case of an array where at least one index is of an enumeration
886       --  type with a non-standard representation, but the component size
887       --  is not appropriate for bit packing. This is the case where we
888       --  have Is_Packed set (we would never be in this unit otherwise),
889       --  but Is_Bit_Packed_Array is false.
890
891       --  Note that if the component size is appropriate for bit packing,
892       --  then the circuit for the computation of the subscript properly
893       --  deals with the non-standard enumeration type case by taking the
894       --  Pos anyway.
895
896       if not Is_Bit_Packed_Array (Typ) then
897
898          --  Here we build a declaration:
899
900          --    type tttP is array (index1, index2, ...) of component_type
901
902          --  where index1, index2, are the index types. These are the same
903          --  as the index types of the original array, except for the non-
904          --  standard representation enumeration type case, where we have
905          --  two subcases.
906
907          --  For the unconstrained array case, we use
908
909          --    Natural range <>
910
911          --  For the constrained case, we use
912
913          --    Natural range Enum_Type'Pos (Enum_Type'First) ..
914          --                  Enum_Type'Pos (Enum_Type'Last);
915
916          PAT :=
917            Make_Defining_Identifier (Loc,
918              Chars => New_External_Name (Chars (Typ), 'P'));
919
920          Set_Packed_Array_Type (Typ, PAT);
921
922          declare
923             Indexes   : constant List_Id := New_List;
924             Indx      : Node_Id;
925             Indx_Typ  : Entity_Id;
926             Enum_Case : Boolean;
927             Typedef   : Node_Id;
928
929          begin
930             Indx := First_Index (Typ);
931
932             while Present (Indx) loop
933                Indx_Typ := Etype (Indx);
934
935                Enum_Case := Is_Enumeration_Type (Indx_Typ)
936                               and then Has_Non_Standard_Rep (Indx_Typ);
937
938                --  Unconstrained case
939
940                if not Is_Constrained (Typ) then
941                   if Enum_Case then
942                      Indx_Typ := Standard_Natural;
943                   end if;
944
945                   Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
946
947                --  Constrained case
948
949                else
950                   if not Enum_Case then
951                      Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
952
953                   else
954                      Append_To (Indexes,
955                        Make_Subtype_Indication (Loc,
956                          Subtype_Mark =>
957                            New_Occurrence_Of (Standard_Natural, Loc),
958                          Constraint =>
959                            Make_Range_Constraint (Loc,
960                              Range_Expression =>
961                                Make_Range (Loc,
962                                  Low_Bound =>
963                                    Make_Attribute_Reference (Loc,
964                                      Prefix =>
965                                        New_Occurrence_Of (Indx_Typ, Loc),
966                                      Attribute_Name => Name_Pos,
967                                      Expressions => New_List (
968                                        Make_Attribute_Reference (Loc,
969                                          Prefix =>
970                                            New_Occurrence_Of (Indx_Typ, Loc),
971                                          Attribute_Name => Name_First))),
972
973                                  High_Bound =>
974                                    Make_Attribute_Reference (Loc,
975                                      Prefix =>
976                                        New_Occurrence_Of (Indx_Typ, Loc),
977                                      Attribute_Name => Name_Pos,
978                                      Expressions => New_List (
979                                        Make_Attribute_Reference (Loc,
980                                          Prefix =>
981                                            New_Occurrence_Of (Indx_Typ, Loc),
982                                          Attribute_Name => Name_Last)))))));
983
984                   end if;
985                end if;
986
987                Next_Index (Indx);
988             end loop;
989
990             if not Is_Constrained (Typ) then
991                Typedef :=
992                  Make_Unconstrained_Array_Definition (Loc,
993                    Subtype_Marks => Indexes,
994                    Component_Definition =>
995                      Make_Component_Definition (Loc,
996                        Aliased_Present    => False,
997                        Subtype_Indication =>
998                           New_Occurrence_Of (Ctyp, Loc)));
999
1000             else
1001                Typedef :=
1002                   Make_Constrained_Array_Definition (Loc,
1003                     Discrete_Subtype_Definitions => Indexes,
1004                     Component_Definition =>
1005                       Make_Component_Definition (Loc,
1006                         Aliased_Present    => False,
1007                         Subtype_Indication =>
1008                           New_Occurrence_Of (Ctyp, Loc)));
1009             end if;
1010
1011             Decl :=
1012               Make_Full_Type_Declaration (Loc,
1013                 Defining_Identifier => PAT,
1014                 Type_Definition => Typedef);
1015          end;
1016
1017          --  Set type as packed array type and install it
1018
1019          Set_Is_Packed_Array_Type (PAT);
1020          Install_PAT;
1021          return;
1022
1023       --  Case of bit-packing required for unconstrained array. We create
1024       --  a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1025
1026       elsif not Is_Constrained (Typ) then
1027          PAT :=
1028            Make_Defining_Identifier (Loc,
1029              Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1030
1031          Set_Packed_Array_Type (Typ, PAT);
1032          Set_PB_Type;
1033
1034          Decl :=
1035            Make_Subtype_Declaration (Loc,
1036              Defining_Identifier => PAT,
1037                Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1038          Install_PAT;
1039          return;
1040
1041       --  Remaining code is for the case of bit-packing for constrained array
1042
1043       --  The name of the packed array subtype is
1044
1045       --    ttt___Xsss
1046
1047       --  where sss is the component size in bits and ttt is the name of
1048       --  the parent packed type.
1049
1050       else
1051          PAT :=
1052            Make_Defining_Identifier (Loc,
1053              Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1054
1055          Set_Packed_Array_Type (Typ, PAT);
1056
1057          --  Build an expression for the length of the array in bits.
1058          --  This is the product of the length of each of the dimensions
1059
1060          declare
1061             J : Nat := 1;
1062
1063          begin
1064             Len_Expr := Empty; -- suppress junk warning
1065
1066             loop
1067                Len_Dim :=
1068                  Make_Attribute_Reference (Loc,
1069                    Attribute_Name => Name_Length,
1070                    Prefix         => New_Occurrence_Of (Typ, Loc),
1071                    Expressions    => New_List (
1072                      Make_Integer_Literal (Loc, J)));
1073
1074                if J = 1 then
1075                   Len_Expr := Len_Dim;
1076
1077                else
1078                   Len_Expr :=
1079                     Make_Op_Multiply (Loc,
1080                       Left_Opnd  => Len_Expr,
1081                       Right_Opnd => Len_Dim);
1082                end if;
1083
1084                J := J + 1;
1085                exit when J > Number_Dimensions (Typ);
1086             end loop;
1087          end;
1088
1089          --  Temporarily attach the length expression to the tree and analyze
1090          --  and resolve it, so that we can test its value. We assume that the
1091          --  total length fits in type Integer. This expression may involve
1092          --  discriminants, so we treat it as a default/per-object expression.
1093
1094          Set_Parent (Len_Expr, Typ);
1095          Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1096
1097          --  Use a modular type if possible. We can do this if we have
1098          --  static bounds, and the length is small enough, and the length
1099          --  is not zero. We exclude the zero length case because the size
1100          --  of things is always at least one, and the zero length object
1101          --  would have an anomalous size.
1102
1103          if Compile_Time_Known_Value (Len_Expr) then
1104             Len_Bits := Expr_Value (Len_Expr) * Csize;
1105
1106             --  Check for size known to be too large
1107
1108             if Len_Bits >
1109               Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1110             then
1111                if System_Storage_Unit = 8 then
1112                   Error_Msg_N
1113                     ("packed array size cannot exceed " &
1114                      "Integer''Last bytes", Typ);
1115                else
1116                   Error_Msg_N
1117                     ("packed array size cannot exceed " &
1118                      "Integer''Last storage units", Typ);
1119                end if;
1120
1121                --  Reset length to arbitrary not too high value to continue
1122
1123                Len_Expr := Make_Integer_Literal (Loc, 65535);
1124                Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1125             end if;
1126
1127             --  We normally consider small enough to mean no larger than the
1128             --  value of System_Max_Binary_Modulus_Power, checking that in the
1129             --  case of values longer than word size, we have long shifts.
1130
1131             if Len_Bits > 0
1132               and then
1133                 (Len_Bits <= System_Word_Size
1134                    or else (Len_Bits <= System_Max_Binary_Modulus_Power
1135                               and then Support_Long_Shifts_On_Target))
1136
1137             --  Also test for alignment given. If an alignment is given which
1138             --  is smaller than the natural modular alignment, force the array
1139             --  of bytes representation to accommodate the alignment.
1140
1141               and then
1142                 (No (Alignment_Clause (Typ))
1143                    or else
1144                  Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1145                                              / System_Storage_Unit))
1146             then
1147                --  We can use the modular type, it has the form:
1148
1149                --    subtype tttPn is btyp
1150                --      range 0 .. 2 ** ((Typ'Length (1)
1151                --                * ... * Typ'Length (n)) * Csize) - 1;
1152
1153                --  The bounds are statically known, and btyp is one of the
1154                --  unsigned types, depending on the length.
1155
1156                if Len_Bits <= Standard_Short_Short_Integer_Size then
1157                   Btyp := RTE (RE_Short_Short_Unsigned);
1158
1159                elsif Len_Bits <= Standard_Short_Integer_Size then
1160                   Btyp := RTE (RE_Short_Unsigned);
1161
1162                elsif Len_Bits <= Standard_Integer_Size then
1163                   Btyp := RTE (RE_Unsigned);
1164
1165                elsif Len_Bits <= Standard_Long_Integer_Size then
1166                   Btyp := RTE (RE_Long_Unsigned);
1167
1168                else
1169                   Btyp := RTE (RE_Long_Long_Unsigned);
1170                end if;
1171
1172                Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1173                Set_Print_In_Hex (Lit);
1174
1175                Decl :=
1176                  Make_Subtype_Declaration (Loc,
1177                    Defining_Identifier => PAT,
1178                      Subtype_Indication =>
1179                        Make_Subtype_Indication (Loc,
1180                          Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1181
1182                          Constraint =>
1183                            Make_Range_Constraint (Loc,
1184                              Range_Expression =>
1185                                Make_Range (Loc,
1186                                  Low_Bound =>
1187                                    Make_Integer_Literal (Loc, 0),
1188                                  High_Bound => Lit))));
1189
1190                if PASize = Uint_0 then
1191                   PASize := Len_Bits;
1192                end if;
1193
1194                Install_PAT;
1195                return;
1196             end if;
1197          end if;
1198
1199          --  Could not use a modular type, for all other cases, we build
1200          --  a packed array subtype:
1201
1202          --    subtype tttPn is
1203          --      System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1204
1205          --  Bits is the length of the array in bits
1206
1207          Set_PB_Type;
1208
1209          Bits_U1 :=
1210            Make_Op_Add (Loc,
1211              Left_Opnd =>
1212                Make_Op_Multiply (Loc,
1213                  Left_Opnd  =>
1214                    Make_Integer_Literal (Loc, Csize),
1215                  Right_Opnd => Len_Expr),
1216
1217              Right_Opnd =>
1218                Make_Integer_Literal (Loc, 7));
1219
1220          Set_Paren_Count (Bits_U1, 1);
1221
1222          PAT_High :=
1223            Make_Op_Subtract (Loc,
1224              Left_Opnd =>
1225                Make_Op_Divide (Loc,
1226                  Left_Opnd => Bits_U1,
1227                  Right_Opnd => Make_Integer_Literal (Loc, 8)),
1228              Right_Opnd => Make_Integer_Literal (Loc, 1));
1229
1230          Decl :=
1231            Make_Subtype_Declaration (Loc,
1232              Defining_Identifier => PAT,
1233                Subtype_Indication =>
1234                  Make_Subtype_Indication (Loc,
1235                    Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1236                    Constraint =>
1237                      Make_Index_Or_Discriminant_Constraint (Loc,
1238                        Constraints => New_List (
1239                          Make_Range (Loc,
1240                            Low_Bound =>
1241                              Make_Integer_Literal (Loc, 0),
1242                            High_Bound =>
1243                              Convert_To (Standard_Integer, PAT_High))))));
1244
1245          Install_PAT;
1246
1247          --  Currently the code in this unit requires that packed arrays
1248          --  represented by non-modular arrays of bytes be on a byte
1249          --  boundary for bit sizes handled by System.Pack_nn units.
1250          --  That's because these units assume the array being accessed
1251          --  starts on a byte boundary.
1252
1253          if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1254             Set_Must_Be_On_Byte_Boundary (Typ);
1255          end if;
1256       end if;
1257    end Create_Packed_Array_Type;
1258
1259    -----------------------------------
1260    -- Expand_Bit_Packed_Element_Set --
1261    -----------------------------------
1262
1263    procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1264       Loc : constant Source_Ptr := Sloc (N);
1265       Lhs : constant Node_Id    := Name (N);
1266
1267       Ass_OK : constant Boolean := Assignment_OK (Lhs);
1268       --  Used to preserve assignment OK status when assignment is rewritten
1269
1270       Rhs : Node_Id := Expression (N);
1271       --  Initially Rhs is the right hand side value, it will be replaced
1272       --  later by an appropriate unchecked conversion for the assignment.
1273
1274       Obj    : Node_Id;
1275       Atyp   : Entity_Id;
1276       PAT    : Entity_Id;
1277       Ctyp   : Entity_Id;
1278       Csiz   : Int;
1279       Cmask  : Uint;
1280
1281       Shift : Node_Id;
1282       --  The expression for the shift value that is required
1283
1284       Shift_Used : Boolean := False;
1285       --  Set True if Shift has been used in the generated code at least
1286       --  once, so that it must be duplicated if used again
1287
1288       New_Lhs : Node_Id;
1289       New_Rhs : Node_Id;
1290
1291       Rhs_Val_Known : Boolean;
1292       Rhs_Val       : Uint;
1293       --  If the value of the right hand side as an integer constant is
1294       --  known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1295       --  contains the value. Otherwise Rhs_Val_Known is set False, and
1296       --  the Rhs_Val is undefined.
1297
1298       function Get_Shift return Node_Id;
1299       --  Function used to get the value of Shift, making sure that it
1300       --  gets duplicated if the function is called more than once.
1301
1302       ---------------
1303       -- Get_Shift --
1304       ---------------
1305
1306       function Get_Shift return Node_Id is
1307       begin
1308          --  If we used the shift value already, then duplicate it. We
1309          --  set a temporary parent in case actions have to be inserted.
1310
1311          if Shift_Used then
1312             Set_Parent (Shift, N);
1313             return Duplicate_Subexpr_No_Checks (Shift);
1314
1315          --  If first time, use Shift unchanged, and set flag for first use
1316
1317          else
1318             Shift_Used := True;
1319             return Shift;
1320          end if;
1321       end Get_Shift;
1322
1323    --  Start of processing for Expand_Bit_Packed_Element_Set
1324
1325    begin
1326       pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1327
1328       Obj := Relocate_Node (Prefix (Lhs));
1329       Convert_To_Actual_Subtype (Obj);
1330       Atyp := Etype (Obj);
1331       PAT  := Packed_Array_Type (Atyp);
1332       Ctyp := Component_Type (Atyp);
1333       Csiz := UI_To_Int (Component_Size (Atyp));
1334
1335       --  We convert the right hand side to the proper subtype to ensure
1336       --  that an appropriate range check is made (since the normal range
1337       --  check from assignment will be lost in the transformations). This
1338       --  conversion is analyzed immediately so that subsequent processing
1339       --  can work with an analyzed Rhs (and e.g. look at its Etype)
1340
1341       --  If the right-hand side is a string literal, create a temporary for
1342       --  it, constant-folding is not ready to wrap the bit representation
1343       --  of a string literal.
1344
1345       if Nkind (Rhs) = N_String_Literal then
1346          declare
1347             Decl : Node_Id;
1348          begin
1349             Decl :=
1350               Make_Object_Declaration (Loc,
1351                 Defining_Identifier =>
1352                   Make_Defining_Identifier (Loc,  New_Internal_Name ('T')),
1353                 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1354                 Expression => New_Copy_Tree (Rhs));
1355
1356             Insert_Actions (N, New_List (Decl));
1357             Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1358          end;
1359       end if;
1360
1361       Rhs := Convert_To (Ctyp, Rhs);
1362       Set_Parent (Rhs, N);
1363       Analyze_And_Resolve (Rhs, Ctyp);
1364
1365       --  Case of component size 1,2,4 or any component size for the modular
1366       --  case. These are the cases for which we can inline the code.
1367
1368       if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1369         or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1370       then
1371          Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1372
1373          --  The statement to be generated is:
1374
1375          --    Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1376
1377          --      where mask1 is obtained by shifting Cmask left Shift bits
1378          --      and then complementing the result.
1379
1380          --      the "and Mask1" is omitted if rhs is constant and all 1 bits
1381
1382          --      the "or ..." is omitted if rhs is constant and all 0 bits
1383
1384          --      rhs is converted to the appropriate type
1385
1386          --      The result is converted back to the array type, since
1387          --      otherwise we lose knowledge of the packed nature.
1388
1389          --  Determine if right side is all 0 bits or all 1 bits
1390
1391          if Compile_Time_Known_Value (Rhs) then
1392             Rhs_Val       := Expr_Rep_Value (Rhs);
1393             Rhs_Val_Known := True;
1394
1395          --  The following test catches the case of an unchecked conversion
1396          --  of an integer literal. This results from optimizing aggregates
1397          --  of packed types.
1398
1399          elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1400            and then Compile_Time_Known_Value (Expression (Rhs))
1401          then
1402             Rhs_Val       := Expr_Rep_Value (Expression (Rhs));
1403             Rhs_Val_Known := True;
1404
1405          else
1406             Rhs_Val       := No_Uint;
1407             Rhs_Val_Known := False;
1408          end if;
1409
1410          --  Some special checks for the case where the right hand value
1411          --  is known at compile time. Basically we have to take care of
1412          --  the implicit conversion to the subtype of the component object.
1413
1414          if Rhs_Val_Known then
1415
1416             --  If we have a biased component type then we must manually do
1417             --  the biasing, since we are taking responsibility in this case
1418             --  for constructing the exact bit pattern to be used.
1419
1420             if Has_Biased_Representation (Ctyp) then
1421                Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1422             end if;
1423
1424             --  For a negative value, we manually convert the twos complement
1425             --  value to a corresponding unsigned value, so that the proper
1426             --  field width is maintained. If we did not do this, we would
1427             --  get too many leading sign bits later on.
1428
1429             if Rhs_Val < 0 then
1430                Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1431             end if;
1432          end if;
1433
1434          New_Lhs := Duplicate_Subexpr (Obj, True);
1435          New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1436
1437          --  First we deal with the "and"
1438
1439          if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1440             declare
1441                Mask1 : Node_Id;
1442                Lit   : Node_Id;
1443
1444             begin
1445                if Compile_Time_Known_Value (Shift) then
1446                   Mask1 :=
1447                     Make_Integer_Literal (Loc,
1448                       Modulus (Etype (Obj)) - 1 -
1449                                  (Cmask * (2 ** Expr_Value (Get_Shift))));
1450                   Set_Print_In_Hex (Mask1);
1451
1452                else
1453                   Lit := Make_Integer_Literal (Loc, Cmask);
1454                   Set_Print_In_Hex (Lit);
1455                   Mask1 :=
1456                     Make_Op_Not (Loc,
1457                       Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1458                end if;
1459
1460                New_Rhs :=
1461                  Make_Op_And (Loc,
1462                    Left_Opnd  => New_Rhs,
1463                    Right_Opnd => Mask1);
1464             end;
1465          end if;
1466
1467          --  Then deal with the "or"
1468
1469          if not Rhs_Val_Known or else Rhs_Val /= 0 then
1470             declare
1471                Or_Rhs : Node_Id;
1472
1473                procedure Fixup_Rhs;
1474                --  Adjust Rhs by bias if biased representation for components
1475                --  or remove extraneous high order sign bits if signed.
1476
1477                procedure Fixup_Rhs is
1478                   Etyp : constant Entity_Id := Etype (Rhs);
1479
1480                begin
1481                   --  For biased case, do the required biasing by simply
1482                   --  converting to the biased subtype (the conversion
1483                   --  will generate the required bias).
1484
1485                   if Has_Biased_Representation (Ctyp) then
1486                      Rhs := Convert_To (Ctyp, Rhs);
1487
1488                   --  For a signed integer type that is not biased, generate
1489                   --  a conversion to unsigned to strip high order sign bits.
1490
1491                   elsif Is_Signed_Integer_Type (Ctyp) then
1492                      Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1493                   end if;
1494
1495                   --  Set Etype, since it can be referenced before the
1496                   --  node is completely analyzed.
1497
1498                   Set_Etype (Rhs, Etyp);
1499
1500                   --  We now need to do an unchecked conversion of the
1501                   --  result to the target type, but it is important that
1502                   --  this conversion be a right justified conversion and
1503                   --  not a left justified conversion.
1504
1505                   Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1506
1507                end Fixup_Rhs;
1508
1509             begin
1510                if Rhs_Val_Known
1511                  and then Compile_Time_Known_Value (Get_Shift)
1512                then
1513                   Or_Rhs :=
1514                     Make_Integer_Literal (Loc,
1515                       Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1516                   Set_Print_In_Hex (Or_Rhs);
1517
1518                else
1519                   --  We have to convert the right hand side to Etype (Obj).
1520                   --  A special case case arises if what we have now is a Val
1521                   --  attribute reference whose expression type is Etype (Obj).
1522                   --  This happens for assignments of fields from the same
1523                   --  array. In this case we get the required right hand side
1524                   --  by simply removing the inner attribute reference.
1525
1526                   if Nkind (Rhs) = N_Attribute_Reference
1527                     and then Attribute_Name (Rhs) = Name_Val
1528                     and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1529                   then
1530                      Rhs := Relocate_Node (First (Expressions (Rhs)));
1531                      Fixup_Rhs;
1532
1533                   --  If the value of the right hand side is a known integer
1534                   --  value, then just replace it by an untyped constant,
1535                   --  which will be properly retyped when we analyze and
1536                   --  resolve the expression.
1537
1538                   elsif Rhs_Val_Known then
1539
1540                      --  Note that Rhs_Val has already been normalized to
1541                      --  be an unsigned value with the proper number of bits.
1542
1543                      Rhs :=
1544                        Make_Integer_Literal (Loc, Rhs_Val);
1545
1546                   --  Otherwise we need an unchecked conversion
1547
1548                   else
1549                      Fixup_Rhs;
1550                   end if;
1551
1552                   Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1553                end if;
1554
1555                if Nkind (New_Rhs) = N_Op_And then
1556                   Set_Paren_Count (New_Rhs, 1);
1557                end if;
1558
1559                New_Rhs :=
1560                  Make_Op_Or (Loc,
1561                    Left_Opnd  => New_Rhs,
1562                    Right_Opnd => Or_Rhs);
1563             end;
1564          end if;
1565
1566          --  Now do the rewrite
1567
1568          Rewrite (N,
1569            Make_Assignment_Statement (Loc,
1570              Name       => New_Lhs,
1571              Expression =>
1572                Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1573          Set_Assignment_OK (Name (N), Ass_OK);
1574
1575       --  All other component sizes for non-modular case
1576
1577       else
1578          --  We generate
1579
1580          --    Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1581
1582          --  where Subscr is the computed linear subscript
1583
1584          declare
1585             Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1586             Set_nn  : Entity_Id;
1587             Subscr  : Node_Id;
1588             Atyp    : Entity_Id;
1589
1590          begin
1591             if No (Bits_nn) then
1592
1593                --  Error, most likely High_Integrity_Mode restriction
1594
1595                return;
1596             end if;
1597
1598             --  Acquire proper Set entity. We use the aligned or unaligned
1599             --  case as appropriate.
1600
1601             if Known_Aligned_Enough (Obj, Csiz) then
1602                Set_nn := RTE (Set_Id (Csiz));
1603             else
1604                Set_nn := RTE (SetU_Id (Csiz));
1605             end if;
1606
1607             --  Now generate the set reference
1608
1609             Obj := Relocate_Node (Prefix (Lhs));
1610             Convert_To_Actual_Subtype (Obj);
1611             Atyp := Etype (Obj);
1612             Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1613
1614             --  Below we must make the assumption that Obj is
1615             --  at least byte aligned, since otherwise its address
1616             --  cannot be taken. The assumption holds since the
1617             --  only arrays that can be misaligned are small packed
1618             --  arrays which are implemented as a modular type, and
1619             --  that is not the case here.
1620
1621             Rewrite (N,
1622               Make_Procedure_Call_Statement (Loc,
1623                   Name => New_Occurrence_Of (Set_nn, Loc),
1624                   Parameter_Associations => New_List (
1625                     Make_Attribute_Reference (Loc,
1626                       Attribute_Name => Name_Address,
1627                       Prefix         => Obj),
1628                     Subscr,
1629                     Unchecked_Convert_To (Bits_nn,
1630                       Convert_To (Ctyp, Rhs)))));
1631
1632          end;
1633       end if;
1634
1635       Analyze (N, Suppress => All_Checks);
1636    end Expand_Bit_Packed_Element_Set;
1637
1638    -------------------------------------
1639    -- Expand_Packed_Address_Reference --
1640    -------------------------------------
1641
1642    procedure Expand_Packed_Address_Reference (N : Node_Id) is
1643       Loc    : constant Source_Ptr := Sloc (N);
1644       Ploc   : Source_Ptr;
1645       Pref   : Node_Id;
1646       Expr   : Node_Id;
1647       Term   : Node_Id;
1648       Atyp   : Entity_Id;
1649       Subscr : Node_Id;
1650
1651    begin
1652       Pref := Prefix (N);
1653       Expr := Empty;
1654
1655       --  We build up an expression serially that has the form
1656
1657       --    outer_object'Address
1658       --      + (linear-subscript * component_size  for each array reference
1659       --      +  field'Bit_Position                 for each record field
1660       --      +  ...
1661       --      +  ...) / Storage_Unit;
1662
1663       --  Some additional conversions are required to deal with the addition
1664       --  operation, which is not normally visible to generated code.
1665
1666       loop
1667          Ploc := Sloc (Pref);
1668
1669          if Nkind (Pref) = N_Indexed_Component then
1670             Convert_To_Actual_Subtype (Prefix (Pref));
1671             Atyp := Etype (Prefix (Pref));
1672             Compute_Linear_Subscript (Atyp, Pref, Subscr);
1673
1674             Term :=
1675               Make_Op_Multiply (Ploc,
1676                 Left_Opnd => Subscr,
1677                 Right_Opnd =>
1678                  Make_Attribute_Reference (Ploc,
1679                    Prefix         => New_Occurrence_Of (Atyp, Ploc),
1680                    Attribute_Name => Name_Component_Size));
1681
1682          elsif Nkind (Pref) = N_Selected_Component then
1683             Term :=
1684               Make_Attribute_Reference (Ploc,
1685                 Prefix         => Selector_Name (Pref),
1686                 Attribute_Name => Name_Bit_Position);
1687
1688          else
1689             exit;
1690          end if;
1691
1692          Term := Convert_To (RTE (RE_Integer_Address), Term);
1693
1694          if No (Expr) then
1695             Expr := Term;
1696
1697          else
1698             Expr :=
1699               Make_Op_Add (Ploc,
1700                 Left_Opnd  => Expr,
1701                 Right_Opnd => Term);
1702          end if;
1703
1704          Pref := Prefix (Pref);
1705       end loop;
1706
1707       Rewrite (N,
1708         Unchecked_Convert_To (RTE (RE_Address),
1709           Make_Op_Add (Loc,
1710             Left_Opnd =>
1711               Unchecked_Convert_To (RTE (RE_Integer_Address),
1712                 Make_Attribute_Reference (Loc,
1713                   Prefix         => Pref,
1714                   Attribute_Name => Name_Address)),
1715
1716             Right_Opnd =>
1717               Make_Op_Divide (Loc,
1718                 Left_Opnd => Expr,
1719                 Right_Opnd =>
1720                   Make_Integer_Literal (Loc, System_Storage_Unit)))));
1721
1722       Analyze_And_Resolve (N, RTE (RE_Address));
1723    end Expand_Packed_Address_Reference;
1724
1725    ------------------------------------
1726    -- Expand_Packed_Boolean_Operator --
1727    ------------------------------------
1728
1729    --  This routine expands "a op b" for the packed cases
1730
1731    procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1732       Loc : constant Source_Ptr := Sloc (N);
1733       Typ : constant Entity_Id  := Etype (N);
1734       L   : constant Node_Id    := Relocate_Node (Left_Opnd  (N));
1735       R   : constant Node_Id    := Relocate_Node (Right_Opnd (N));
1736
1737       Ltyp : Entity_Id;
1738       Rtyp : Entity_Id;
1739       PAT  : Entity_Id;
1740
1741    begin
1742       Convert_To_Actual_Subtype (L);
1743       Convert_To_Actual_Subtype (R);
1744
1745       Ensure_Defined (Etype (L), N);
1746       Ensure_Defined (Etype (R), N);
1747
1748       Apply_Length_Check (R, Etype (L));
1749
1750       Ltyp := Etype (L);
1751       Rtyp := Etype (R);
1752
1753       --  First an odd and silly test. We explicitly check for the XOR
1754       --  case where the component type is True .. True, since this will
1755       --  raise constraint error. A special check is required since CE
1756       --  will not be required other wise (cf Expand_Packed_Not).
1757
1758       --  No such check is required for AND and OR, since for both these
1759       --  cases False op False = False, and True op True = True.
1760
1761       if Nkind (N) = N_Op_Xor then
1762          declare
1763             CT : constant Entity_Id := Component_Type (Rtyp);
1764             BT : constant Entity_Id := Base_Type (CT);
1765
1766          begin
1767             Insert_Action (N,
1768               Make_Raise_Constraint_Error (Loc,
1769                 Condition =>
1770                   Make_Op_And (Loc,
1771                     Left_Opnd =>
1772                       Make_Op_Eq (Loc,
1773                         Left_Opnd =>
1774                           Make_Attribute_Reference (Loc,
1775                             Prefix         => New_Occurrence_Of (CT, Loc),
1776                             Attribute_Name => Name_First),
1777
1778                         Right_Opnd =>
1779                           Convert_To (BT,
1780                             New_Occurrence_Of (Standard_True, Loc))),
1781
1782                     Right_Opnd =>
1783                       Make_Op_Eq (Loc,
1784                         Left_Opnd =>
1785                           Make_Attribute_Reference (Loc,
1786                             Prefix         => New_Occurrence_Of (CT, Loc),
1787                             Attribute_Name => Name_Last),
1788
1789                         Right_Opnd =>
1790                           Convert_To (BT,
1791                             New_Occurrence_Of (Standard_True, Loc)))),
1792                 Reason => CE_Range_Check_Failed));
1793          end;
1794       end if;
1795
1796       --  Now that that silliness is taken care of, get packed array type
1797
1798       Convert_To_PAT_Type (L);
1799       Convert_To_PAT_Type (R);
1800
1801       PAT := Etype (L);
1802
1803       --  For the modular case, we expand a op b into
1804
1805       --    rtyp!(pat!(a) op pat!(b))
1806
1807       --  where rtyp is the Etype of the left operand. Note that we do not
1808       --  convert to the base type, since this would be unconstrained, and
1809       --  hence not have a corresponding packed array type set.
1810
1811       --  Note that both operands must be modular for this code to be used
1812
1813       if Is_Modular_Integer_Type (PAT)
1814            and then
1815          Is_Modular_Integer_Type (Etype (R))
1816       then
1817          declare
1818             P : Node_Id;
1819
1820          begin
1821             if Nkind (N) = N_Op_And then
1822                P := Make_Op_And (Loc, L, R);
1823
1824             elsif Nkind (N) = N_Op_Or then
1825                P := Make_Op_Or  (Loc, L, R);
1826
1827             else -- Nkind (N) = N_Op_Xor
1828                P := Make_Op_Xor (Loc, L, R);
1829             end if;
1830
1831             Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1832          end;
1833
1834       --  For the array case, we insert the actions
1835
1836       --    Result : Ltype;
1837
1838       --    System.Bitops.Bit_And/Or/Xor
1839       --     (Left'Address,
1840       --      Ltype'Length * Ltype'Component_Size;
1841       --      Right'Address,
1842       --      Rtype'Length * Rtype'Component_Size
1843       --      Result'Address);
1844
1845       --  where Left and Right are the Packed_Bytes{1,2,4} operands and
1846       --  the second argument and fourth arguments are the lengths of the
1847       --  operands in bits. Then we replace the expression by a reference
1848       --  to Result.
1849
1850       --  Note that if we are mixing a modular and array operand, everything
1851       --  works fine, since we ensure that the modular representation has the
1852       --  same physical layout as the array representation (that's what the
1853       --  left justified modular stuff in the big-endian case is about).
1854
1855       else
1856          declare
1857             Result_Ent : constant Entity_Id :=
1858                            Make_Defining_Identifier (Loc,
1859                              Chars => New_Internal_Name ('T'));
1860
1861             E_Id : RE_Id;
1862
1863          begin
1864             if Nkind (N) = N_Op_And then
1865                E_Id := RE_Bit_And;
1866
1867             elsif Nkind (N) = N_Op_Or then
1868                E_Id := RE_Bit_Or;
1869
1870             else -- Nkind (N) = N_Op_Xor
1871                E_Id := RE_Bit_Xor;
1872             end if;
1873
1874             Insert_Actions (N, New_List (
1875
1876               Make_Object_Declaration (Loc,
1877                 Defining_Identifier => Result_Ent,
1878                 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1879
1880               Make_Procedure_Call_Statement (Loc,
1881                 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1882                   Parameter_Associations => New_List (
1883
1884                     Make_Byte_Aligned_Attribute_Reference (Loc,
1885                       Attribute_Name => Name_Address,
1886                       Prefix         => L),
1887
1888                     Make_Op_Multiply (Loc,
1889                       Left_Opnd =>
1890                         Make_Attribute_Reference (Loc,
1891                           Prefix =>
1892                             New_Occurrence_Of
1893                               (Etype (First_Index (Ltyp)), Loc),
1894                           Attribute_Name => Name_Range_Length),
1895                       Right_Opnd =>
1896                         Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1897
1898                     Make_Byte_Aligned_Attribute_Reference (Loc,
1899                       Attribute_Name => Name_Address,
1900                       Prefix         => R),
1901
1902                     Make_Op_Multiply (Loc,
1903                       Left_Opnd =>
1904                         Make_Attribute_Reference (Loc,
1905                           Prefix =>
1906                             New_Occurrence_Of
1907                               (Etype (First_Index (Rtyp)), Loc),
1908                           Attribute_Name => Name_Range_Length),
1909                       Right_Opnd =>
1910                         Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1911
1912                     Make_Byte_Aligned_Attribute_Reference (Loc,
1913                       Attribute_Name => Name_Address,
1914                       Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1915
1916             Rewrite (N,
1917               New_Occurrence_Of (Result_Ent, Loc));
1918          end;
1919       end if;
1920
1921       Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1922    end Expand_Packed_Boolean_Operator;
1923
1924    -------------------------------------
1925    -- Expand_Packed_Element_Reference --
1926    -------------------------------------
1927
1928    procedure Expand_Packed_Element_Reference (N : Node_Id) is
1929       Loc   : constant Source_Ptr := Sloc (N);
1930       Obj   : Node_Id;
1931       Atyp  : Entity_Id;
1932       PAT   : Entity_Id;
1933       Ctyp  : Entity_Id;
1934       Csiz  : Int;
1935       Shift : Node_Id;
1936       Cmask : Uint;
1937       Lit   : Node_Id;
1938       Arg   : Node_Id;
1939
1940    begin
1941       --  If not bit packed, we have the enumeration case, which is easily
1942       --  dealt with (just adjust the subscripts of the indexed component)
1943
1944       --  Note: this leaves the result as an indexed component, which is
1945       --  still a variable, so can be used in the assignment case, as is
1946       --  required in the enumeration case.
1947
1948       if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1949          Setup_Enumeration_Packed_Array_Reference (N);
1950          return;
1951       end if;
1952
1953       --  Remaining processing is for the bit-packed case
1954
1955       Obj := Relocate_Node (Prefix (N));
1956       Convert_To_Actual_Subtype (Obj);
1957       Atyp := Etype (Obj);
1958       PAT  := Packed_Array_Type (Atyp);
1959       Ctyp := Component_Type (Atyp);
1960       Csiz := UI_To_Int (Component_Size (Atyp));
1961
1962       --  Case of component size 1,2,4 or any component size for the modular
1963       --  case. These are the cases for which we can inline the code.
1964
1965       if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1966         or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1967       then
1968          Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1969          Lit := Make_Integer_Literal (Loc, Cmask);
1970          Set_Print_In_Hex (Lit);
1971
1972          --  We generate a shift right to position the field, followed by a
1973          --  masking operation to extract the bit field, and we finally do an
1974          --  unchecked conversion to convert the result to the required target.
1975
1976          --  Note that the unchecked conversion automatically deals with the
1977          --  bias if we are dealing with a biased representation. What will
1978          --  happen is that we temporarily generate the biased representation,
1979          --  but almost immediately that will be converted to the original
1980          --  unbiased component type, and the bias will disappear.
1981
1982          Arg :=
1983            Make_Op_And (Loc,
1984              Left_Opnd  => Make_Shift_Right (Obj, Shift),
1985              Right_Opnd => Lit);
1986
1987          --  We neded to analyze this before we do the unchecked convert
1988          --  below, but we need it temporarily attached to the tree for
1989          --  this analysis (hence the temporary Set_Parent call).
1990
1991          Set_Parent (Arg, Parent (N));
1992          Analyze_And_Resolve (Arg);
1993
1994          Rewrite (N,
1995            RJ_Unchecked_Convert_To (Ctyp, Arg));
1996
1997       --  All other component sizes for non-modular case
1998
1999       else
2000          --  We generate
2001
2002          --    Component_Type!(Get_nn (Arr'address, Subscr))
2003
2004          --  where Subscr is the computed linear subscript
2005
2006          declare
2007             Get_nn : Entity_Id;
2008             Subscr : Node_Id;
2009
2010          begin
2011             --  Acquire proper Get entity. We use the aligned or unaligned
2012             --  case as appropriate.
2013
2014             if Known_Aligned_Enough (Obj, Csiz) then
2015                Get_nn := RTE (Get_Id (Csiz));
2016             else
2017                Get_nn := RTE (GetU_Id (Csiz));
2018             end if;
2019
2020             --  Now generate the get reference
2021
2022             Compute_Linear_Subscript (Atyp, N, Subscr);
2023
2024             --  Below we make the assumption that Obj is at least byte
2025             --  aligned, since otherwise its address cannot be taken.
2026             --  The assumption holds since the only arrays that can be
2027             --  misaligned are small packed arrays which are implemented
2028             --  as a modular type, and that is not the case here.
2029
2030             Rewrite (N,
2031               Unchecked_Convert_To (Ctyp,
2032                 Make_Function_Call (Loc,
2033                   Name => New_Occurrence_Of (Get_nn, Loc),
2034                   Parameter_Associations => New_List (
2035                     Make_Attribute_Reference (Loc,
2036                       Attribute_Name => Name_Address,
2037                       Prefix         => Obj),
2038                     Subscr))));
2039          end;
2040       end if;
2041
2042       Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2043
2044    end Expand_Packed_Element_Reference;
2045
2046    ----------------------
2047    -- Expand_Packed_Eq --
2048    ----------------------
2049
2050    --  Handles expansion of "=" on packed array types
2051
2052    procedure Expand_Packed_Eq (N : Node_Id) is
2053       Loc : constant Source_Ptr := Sloc (N);
2054       L   : constant Node_Id    := Relocate_Node (Left_Opnd  (N));
2055       R   : constant Node_Id    := Relocate_Node (Right_Opnd (N));
2056
2057       LLexpr : Node_Id;
2058       RLexpr : Node_Id;
2059
2060       Ltyp : Entity_Id;
2061       Rtyp : Entity_Id;
2062       PAT  : Entity_Id;
2063
2064    begin
2065       Convert_To_Actual_Subtype (L);
2066       Convert_To_Actual_Subtype (R);
2067       Ltyp := Underlying_Type (Etype (L));
2068       Rtyp := Underlying_Type (Etype (R));
2069
2070       Convert_To_PAT_Type (L);
2071       Convert_To_PAT_Type (R);
2072       PAT := Etype (L);
2073
2074       LLexpr :=
2075         Make_Op_Multiply (Loc,
2076           Left_Opnd =>
2077             Make_Attribute_Reference (Loc,
2078               Attribute_Name => Name_Length,
2079               Prefix         => New_Occurrence_Of (Ltyp, Loc)),
2080           Right_Opnd =>
2081             Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2082
2083       RLexpr :=
2084         Make_Op_Multiply (Loc,
2085           Left_Opnd =>
2086             Make_Attribute_Reference (Loc,
2087               Attribute_Name => Name_Length,
2088               Prefix         => New_Occurrence_Of (Rtyp, Loc)),
2089           Right_Opnd =>
2090             Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2091
2092       --  For the modular case, we transform the comparison to:
2093
2094       --    Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2095
2096       --  where PAT is the packed array type. This works fine, since in the
2097       --  modular case we guarantee that the unused bits are always zeroes.
2098       --  We do have to compare the lengths because we could be comparing
2099       --  two different subtypes of the same base type.
2100
2101       if Is_Modular_Integer_Type (PAT) then
2102          Rewrite (N,
2103            Make_And_Then (Loc,
2104              Left_Opnd =>
2105                Make_Op_Eq (Loc,
2106                  Left_Opnd  => LLexpr,
2107                  Right_Opnd => RLexpr),
2108
2109              Right_Opnd =>
2110                Make_Op_Eq (Loc,
2111                  Left_Opnd => L,
2112                  Right_Opnd => R)));
2113
2114       --  For the non-modular case, we call a runtime routine
2115
2116       --    System.Bit_Ops.Bit_Eq
2117       --      (L'Address, L_Length, R'Address, R_Length)
2118
2119       --  where PAT is the packed array type, and the lengths are the lengths
2120       --  in bits of the original packed arrays. This routine takes care of
2121       --  not comparing the unused bits in the last byte.
2122
2123       else
2124          Rewrite (N,
2125            Make_Function_Call (Loc,
2126              Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2127              Parameter_Associations => New_List (
2128                Make_Byte_Aligned_Attribute_Reference (Loc,
2129                  Attribute_Name => Name_Address,
2130                  Prefix         => L),
2131
2132                LLexpr,
2133
2134                Make_Byte_Aligned_Attribute_Reference (Loc,
2135                  Attribute_Name => Name_Address,
2136                  Prefix         => R),
2137
2138                RLexpr)));
2139       end if;
2140
2141       Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2142    end Expand_Packed_Eq;
2143
2144    -----------------------
2145    -- Expand_Packed_Not --
2146    -----------------------
2147
2148    --  Handles expansion of "not" on packed array types
2149
2150    procedure Expand_Packed_Not (N : Node_Id) is
2151       Loc  : constant Source_Ptr := Sloc (N);
2152       Typ  : constant Entity_Id  := Etype (N);
2153       Opnd : constant Node_Id    := Relocate_Node (Right_Opnd (N));
2154
2155       Rtyp : Entity_Id;
2156       PAT  : Entity_Id;
2157       Lit  : Node_Id;
2158
2159    begin
2160       Convert_To_Actual_Subtype (Opnd);
2161       Rtyp := Etype (Opnd);
2162
2163       --  First an odd and silly test. We explicitly check for the case
2164       --  where the 'First of the component type is equal to the 'Last of
2165       --  this component type, and if this is the case, we make sure that
2166       --  constraint error is raised. The reason is that the NOT is bound
2167       --  to cause CE in this case, and we will not otherwise catch it.
2168
2169       --  Believe it or not, this was reported as a bug. Note that nearly
2170       --  always, the test will evaluate statically to False, so the code
2171       --  will be statically removed, and no extra overhead caused.
2172
2173       declare
2174          CT : constant Entity_Id := Component_Type (Rtyp);
2175
2176       begin
2177          Insert_Action (N,
2178            Make_Raise_Constraint_Error (Loc,
2179              Condition =>
2180                Make_Op_Eq (Loc,
2181                  Left_Opnd =>
2182                    Make_Attribute_Reference (Loc,
2183                      Prefix         => New_Occurrence_Of (CT, Loc),
2184                      Attribute_Name => Name_First),
2185
2186                  Right_Opnd =>
2187                    Make_Attribute_Reference (Loc,
2188                      Prefix         => New_Occurrence_Of (CT, Loc),
2189                      Attribute_Name => Name_Last)),
2190              Reason => CE_Range_Check_Failed));
2191       end;
2192
2193       --  Now that that silliness is taken care of, get packed array type
2194
2195       Convert_To_PAT_Type (Opnd);
2196       PAT := Etype (Opnd);
2197
2198       --  For the case where the packed array type is a modular type,
2199       --  not A expands simply into:
2200
2201       --     rtyp!(PAT!(A) xor mask)
2202
2203       --  where PAT is the packed array type, and mask is a mask of all
2204       --  one bits of length equal to the size of this packed type and
2205       --  rtyp is the actual subtype of the operand
2206
2207       Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2208       Set_Print_In_Hex (Lit);
2209
2210       if not Is_Array_Type (PAT) then
2211          Rewrite (N,
2212            Unchecked_Convert_To (Rtyp,
2213              Make_Op_Xor (Loc,
2214                Left_Opnd  => Opnd,
2215                Right_Opnd => Lit)));
2216
2217       --  For the array case, we insert the actions
2218
2219       --    Result : Typ;
2220
2221       --    System.Bitops.Bit_Not
2222       --     (Opnd'Address,
2223       --      Typ'Length * Typ'Component_Size;
2224       --      Result'Address);
2225
2226       --  where Opnd is the Packed_Bytes{1,2,4} operand and the second
2227       --  argument is the length of the operand in bits. Then we replace
2228       --  the expression by a reference to Result.
2229
2230       else
2231          declare
2232             Result_Ent : constant Entity_Id :=
2233                            Make_Defining_Identifier (Loc,
2234                              Chars => New_Internal_Name ('T'));
2235
2236          begin
2237             Insert_Actions (N, New_List (
2238
2239               Make_Object_Declaration (Loc,
2240                 Defining_Identifier => Result_Ent,
2241                 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2242
2243               Make_Procedure_Call_Statement (Loc,
2244                 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2245                   Parameter_Associations => New_List (
2246
2247                     Make_Byte_Aligned_Attribute_Reference (Loc,
2248                       Attribute_Name => Name_Address,
2249                       Prefix         => Opnd),
2250
2251                     Make_Op_Multiply (Loc,
2252                       Left_Opnd =>
2253                         Make_Attribute_Reference (Loc,
2254                           Prefix =>
2255                             New_Occurrence_Of
2256                               (Etype (First_Index (Rtyp)), Loc),
2257                           Attribute_Name => Name_Range_Length),
2258                       Right_Opnd =>
2259                         Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2260
2261                     Make_Byte_Aligned_Attribute_Reference (Loc,
2262                       Attribute_Name => Name_Address,
2263                       Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2264
2265             Rewrite (N,
2266               New_Occurrence_Of (Result_Ent, Loc));
2267          end;
2268       end if;
2269
2270       Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2271
2272    end Expand_Packed_Not;
2273
2274    -------------------------------------
2275    -- Involves_Packed_Array_Reference --
2276    -------------------------------------
2277
2278    function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2279    begin
2280       if Nkind (N) = N_Indexed_Component
2281         and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2282       then
2283          return True;
2284
2285       elsif Nkind (N) = N_Selected_Component then
2286          return Involves_Packed_Array_Reference (Prefix (N));
2287
2288       else
2289          return False;
2290       end if;
2291    end Involves_Packed_Array_Reference;
2292
2293    --------------------------
2294    -- Known_Aligned_Enough --
2295    --------------------------
2296
2297    function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2298       Typ : constant Entity_Id := Etype (Obj);
2299
2300       function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2301       --  If the component is in a record that contains previous packed
2302       --  components, consider it unaligned because the back-end might
2303       --  choose to pack the rest of the record. Lead to less efficient code,
2304       --  but safer vis-a-vis of back-end choices.
2305
2306       --------------------------------
2307       -- In_Partially_Packed_Record --
2308       --------------------------------
2309
2310       function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2311          Rec_Type  : constant Entity_Id := Scope (Comp);
2312          Prev_Comp : Entity_Id;
2313
2314       begin
2315          Prev_Comp := First_Entity (Rec_Type);
2316          while Present (Prev_Comp) loop
2317             if Is_Packed (Etype (Prev_Comp)) then
2318                return True;
2319
2320             elsif Prev_Comp = Comp then
2321                return False;
2322             end if;
2323
2324             Next_Entity (Prev_Comp);
2325          end loop;
2326
2327          return False;
2328       end  In_Partially_Packed_Record;
2329
2330    --  Start of processing for Known_Aligned_Enough
2331
2332    begin
2333       --  Odd bit sizes don't need alignment anyway
2334
2335       if Csiz mod 2 = 1 then
2336          return True;
2337
2338       --  If we have a specified alignment, see if it is sufficient, if not
2339       --  then we can't possibly be aligned enough in any case.
2340
2341       elsif Known_Alignment (Etype (Obj)) then
2342          --  Alignment required is 4 if size is a multiple of 4, and
2343          --  2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2344
2345          if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2346             return False;
2347          end if;
2348       end if;
2349
2350       --  OK, alignment should be sufficient, if object is aligned
2351
2352       --  If object is strictly aligned, then it is definitely aligned
2353
2354       if Strict_Alignment (Typ) then
2355          return True;
2356
2357       --  Case of subscripted array reference
2358
2359       elsif Nkind (Obj) = N_Indexed_Component then
2360
2361          --  If we have a pointer to an array, then this is definitely
2362          --  aligned, because pointers always point to aligned versions.
2363
2364          if Is_Access_Type (Etype (Prefix (Obj))) then
2365             return True;
2366
2367          --  Otherwise, go look at the prefix
2368
2369          else
2370             return Known_Aligned_Enough (Prefix (Obj), Csiz);
2371          end if;
2372
2373       --  Case of record field
2374
2375       elsif Nkind (Obj) = N_Selected_Component then
2376
2377          --  What is significant here is whether the record type is packed
2378
2379          if Is_Record_Type (Etype (Prefix (Obj)))
2380            and then Is_Packed (Etype (Prefix (Obj)))
2381          then
2382             return False;
2383
2384          --  Or the component has a component clause which might cause
2385          --  the component to become unaligned (we can't tell if the
2386          --  backend is doing alignment computations).
2387
2388          elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2389             return False;
2390
2391          elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2392             return False;
2393
2394          --  In all other cases, go look at prefix
2395
2396          else
2397             return Known_Aligned_Enough (Prefix (Obj), Csiz);
2398          end if;
2399
2400       elsif Nkind (Obj) = N_Type_Conversion then
2401          return Known_Aligned_Enough (Expression (Obj), Csiz);
2402
2403       --  For a formal parameter, it is safer to assume that it is not
2404       --  aligned, because the formal may be unconstrained while the actual
2405       --  is constrained. In this situation, a small constrained packed
2406       --  array, represented in modular form, may be unaligned.
2407
2408       elsif Is_Entity_Name (Obj) then
2409          return not Is_Formal (Entity (Obj));
2410       else
2411
2412       --  If none of the above, must be aligned
2413          return True;
2414       end if;
2415    end Known_Aligned_Enough;
2416
2417    ---------------------
2418    -- Make_Shift_Left --
2419    ---------------------
2420
2421    function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2422       Nod : Node_Id;
2423
2424    begin
2425       if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2426          return N;
2427       else
2428          Nod :=
2429            Make_Op_Shift_Left (Sloc (N),
2430              Left_Opnd  => N,
2431              Right_Opnd => S);
2432          Set_Shift_Count_OK (Nod, True);
2433          return Nod;
2434       end if;
2435    end Make_Shift_Left;
2436
2437    ----------------------
2438    -- Make_Shift_Right --
2439    ----------------------
2440
2441    function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2442       Nod : Node_Id;
2443
2444    begin
2445       if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2446          return N;
2447       else
2448          Nod :=
2449            Make_Op_Shift_Right (Sloc (N),
2450              Left_Opnd  => N,
2451              Right_Opnd => S);
2452          Set_Shift_Count_OK (Nod, True);
2453          return Nod;
2454       end if;
2455    end Make_Shift_Right;
2456
2457    -----------------------------
2458    -- RJ_Unchecked_Convert_To --
2459    -----------------------------
2460
2461    function RJ_Unchecked_Convert_To
2462      (Typ  : Entity_Id;
2463       Expr : Node_Id) return Node_Id
2464    is
2465       Source_Typ : constant Entity_Id := Etype (Expr);
2466       Target_Typ : constant Entity_Id := Typ;
2467
2468       Src : Node_Id := Expr;
2469
2470       Source_Siz : Nat;
2471       Target_Siz : Nat;
2472
2473    begin
2474       Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2475       Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2476
2477       --  First step, if the source type is not a discrete type, then we
2478       --  first convert to a modular type of the source length, since
2479       --  otherwise, on a big-endian machine, we get left-justification.
2480       --  We do it for little-endian machines as well, because there might
2481       --  be junk bits that are not cleared if the type is not numeric.
2482
2483       if Source_Siz /= Target_Siz
2484         and then  not Is_Discrete_Type (Source_Typ)
2485       then
2486          Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2487       end if;
2488
2489       --  In the big endian case, if the lengths of the two types differ,
2490       --  then we must worry about possible left justification in the
2491       --  conversion, and avoiding that is what this is all about.
2492
2493       if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2494
2495          --  Next step. If the target is not a discrete type, then we first
2496          --  convert to a modular type of the target length, since
2497          --  otherwise, on a big-endian machine, we get left-justification.
2498
2499          if not Is_Discrete_Type (Target_Typ) then
2500             Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2501          end if;
2502       end if;
2503
2504       --  And now we can do the final conversion to the target type
2505
2506       return Unchecked_Convert_To (Target_Typ, Src);
2507    end RJ_Unchecked_Convert_To;
2508
2509    ----------------------------------------------
2510    -- Setup_Enumeration_Packed_Array_Reference --
2511    ----------------------------------------------
2512
2513    --  All we have to do here is to find the subscripts that correspond
2514    --  to the index positions that have non-standard enumeration types
2515    --  and insert a Pos attribute to get the proper subscript value.
2516
2517    --  Finally the prefix must be uncheck converted to the corresponding
2518    --  packed array type.
2519
2520    --  Note that the component type is unchanged, so we do not need to
2521    --  fiddle with the types (Gigi always automatically takes the packed
2522    --  array type if it is set, as it will be in this case).
2523
2524    procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2525       Pfx   : constant Node_Id   := Prefix (N);
2526       Typ   : constant Entity_Id := Etype (N);
2527       Exprs : constant List_Id   := Expressions (N);
2528       Expr  : Node_Id;
2529
2530    begin
2531       --  If the array is unconstrained, then we replace the array
2532       --  reference with its actual subtype. This actual subtype will
2533       --  have a packed array type with appropriate bounds.
2534
2535       if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2536          Convert_To_Actual_Subtype (Pfx);
2537       end if;
2538
2539       Expr := First (Exprs);
2540       while Present (Expr) loop
2541          declare
2542             Loc      : constant Source_Ptr := Sloc (Expr);
2543             Expr_Typ : constant Entity_Id := Etype (Expr);
2544
2545          begin
2546             if Is_Enumeration_Type (Expr_Typ)
2547               and then Has_Non_Standard_Rep (Expr_Typ)
2548             then
2549                Rewrite (Expr,
2550                  Make_Attribute_Reference (Loc,
2551                    Prefix         => New_Occurrence_Of (Expr_Typ, Loc),
2552                    Attribute_Name => Name_Pos,
2553                    Expressions    => New_List (Relocate_Node (Expr))));
2554                Analyze_And_Resolve (Expr, Standard_Natural);
2555             end if;
2556          end;
2557
2558          Next (Expr);
2559       end loop;
2560
2561       Rewrite (N,
2562         Make_Indexed_Component (Sloc (N),
2563           Prefix      =>
2564             Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2565           Expressions => Exprs));
2566
2567       Analyze_And_Resolve (N, Typ);
2568
2569    end Setup_Enumeration_Packed_Array_Reference;
2570
2571    -----------------------------------------
2572    -- Setup_Inline_Packed_Array_Reference --
2573    -----------------------------------------
2574
2575    procedure Setup_Inline_Packed_Array_Reference
2576      (N      : Node_Id;
2577       Atyp   : Entity_Id;
2578       Obj    : in out Node_Id;
2579       Cmask  : out Uint;
2580       Shift  : out Node_Id)
2581    is
2582       Loc    : constant Source_Ptr := Sloc (N);
2583       PAT    : Entity_Id;
2584       Otyp   : Entity_Id;
2585       Csiz   : Uint;
2586       Osiz   : Uint;
2587
2588    begin
2589       Csiz := Component_Size (Atyp);
2590
2591       Convert_To_PAT_Type (Obj);
2592       PAT := Etype (Obj);
2593
2594       Cmask := 2 ** Csiz - 1;
2595
2596       if Is_Array_Type (PAT) then
2597          Otyp := Component_Type (PAT);
2598          Osiz := Component_Size (PAT);
2599
2600       else
2601          Otyp := PAT;
2602
2603          --  In the case where the PAT is a modular type, we want the actual
2604          --  size in bits of the modular value we use. This is neither the
2605          --  Object_Size nor the Value_Size, either of which may have been
2606          --  reset to strange values, but rather the minimum size. Note that
2607          --  since this is a modular type with full range, the issue of
2608          --  biased representation does not arise.
2609
2610          Osiz := UI_From_Int (Minimum_Size (Otyp));
2611       end if;
2612
2613       Compute_Linear_Subscript (Atyp, N, Shift);
2614
2615       --  If the component size is not 1, then the subscript must be
2616       --  multiplied by the component size to get the shift count.
2617
2618       if Csiz /= 1 then
2619          Shift :=
2620            Make_Op_Multiply (Loc,
2621              Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2622              Right_Opnd => Shift);
2623       end if;
2624
2625       --  If we have the array case, then this shift count must be broken
2626       --  down into a byte subscript, and a shift within the byte.
2627
2628       if Is_Array_Type (PAT) then
2629
2630          declare
2631             New_Shift : Node_Id;
2632
2633          begin
2634             --  We must analyze shift, since we will duplicate it
2635
2636             Set_Parent (Shift, N);
2637             Analyze_And_Resolve
2638               (Shift, Standard_Integer, Suppress => All_Checks);
2639
2640             --  The shift count within the word is
2641             --    shift mod Osiz
2642
2643             New_Shift :=
2644               Make_Op_Mod (Loc,
2645                 Left_Opnd  => Duplicate_Subexpr (Shift),
2646                 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2647
2648             --  The subscript to be used on the PAT array is
2649             --    shift / Osiz
2650
2651             Obj :=
2652               Make_Indexed_Component (Loc,
2653                 Prefix => Obj,
2654                 Expressions => New_List (
2655                   Make_Op_Divide (Loc,
2656                     Left_Opnd => Duplicate_Subexpr (Shift),
2657                     Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2658
2659             Shift := New_Shift;
2660          end;
2661
2662       --  For the modular integer case, the object to be manipulated is
2663       --  the entire array, so Obj is unchanged. Note that we will reset
2664       --  its type to PAT before returning to the caller.
2665
2666       else
2667          null;
2668       end if;
2669
2670       --  The one remaining step is to modify the shift count for the
2671       --  big-endian case. Consider the following example in a byte:
2672
2673       --     xxxxxxxx  bits of byte
2674       --     vvvvvvvv  bits of value
2675       --     33221100  little-endian numbering
2676       --     00112233  big-endian numbering
2677
2678       --  Here we have the case of 2-bit fields
2679
2680       --  For the little-endian case, we already have the proper shift
2681       --  count set, e.g. for element 2, the shift count is 2*2 = 4.
2682
2683       --  For the big endian case, we have to adjust the shift count,
2684       --  computing it as (N - F) - shift, where N is the number of bits
2685       --  in an element of the array used to implement the packed array,
2686       --  F is the number of bits in a source level array element, and
2687       --  shift is the count so far computed.
2688
2689       if Bytes_Big_Endian then
2690          Shift :=
2691            Make_Op_Subtract (Loc,
2692              Left_Opnd  => Make_Integer_Literal (Loc, Osiz - Csiz),
2693              Right_Opnd => Shift);
2694       end if;
2695
2696       Set_Parent (Shift, N);
2697       Set_Parent (Obj, N);
2698       Analyze_And_Resolve (Obj,   Otyp,             Suppress => All_Checks);
2699       Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2700
2701       --  Make sure final type of object is the appropriate packed type
2702
2703       Set_Etype (Obj, Otyp);
2704
2705    end Setup_Inline_Packed_Array_Reference;
2706
2707 end Exp_Pakd;