OSDN Git Service

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