OSDN Git Service

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