OSDN Git Service

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