OSDN Git Service

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