OSDN Git Service

New Language: Ada
[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: 1.125 $
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 Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
457    --  Build a left shift node, checking for the case of a shift count of zero
458
459    function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
460    --  Build a right shift node, checking for the case of a shift count of zero
461
462    function RJ_Unchecked_Convert_To
463      (Typ  : Entity_Id;
464       Expr : Node_Id)
465       return Node_Id;
466    --  The packed array code does unchecked conversions which in some cases
467    --  may involve non-discrete types with differing sizes. The semantics of
468    --  such conversions is potentially endian dependent, and the effect we
469    --  want here for such a conversion is to do the conversion in size as
470    --  though numeric items are involved, and we extend or truncate on the
471    --  left side. This happens naturally in the little-endian case, but in
472    --  the big endian case we can get left justification, when what we want
473    --  is right justification. This routine does the unchecked conversion in
474    --  a stepwise manner to ensure that it gives the expected result. Hence
475    --  the name (RJ = Right justified). The parameters Typ and Expr are as
476    --  for the case of a normal Unchecked_Convert_To call.
477
478    procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
479    --  This routine is called in the Get and Set case for arrays that are
480    --  packed but not bit-packed, meaning that they have at least one
481    --  subscript that is of an enumeration type with a non-standard
482    --  representation. This routine modifies the given node to properly
483    --  reference the corresponding packed array type.
484
485    procedure Setup_Inline_Packed_Array_Reference
486      (N      : Node_Id;
487       Atyp   : Entity_Id;
488       Obj    : in out Node_Id;
489       Cmask  : out Uint;
490       Shift  : out Node_Id);
491    --  This procedure performs common processing on the N_Indexed_Component
492    --  parameter given as N, whose prefix is a reference to a packed array.
493    --  This is used for the get and set when the component size is 1,2,4
494    --  or for other component sizes when the packed array type is a modular
495    --  type (i.e. the cases that are handled with inline code).
496    --
497    --  On entry:
498    --
499    --    N is the N_Indexed_Component node for the packed array reference
500    --
501    --    Atyp is the constrained array type (the actual subtype has been
502    --    computed if necessary to obtain the constraints, but this is still
503    --    the original array type, not the Packed_Array_Type value).
504    --
505    --    Obj is the object which is to be indexed. It is always of type Atyp.
506    --
507    --  On return:
508    --
509    --    Obj is the object containing the desired bit field. It is of type
510    --    Unsigned or Long_Long_Unsigned, and is either the entire value,
511    --    for the small static case, or the proper selected byte from the
512    --    array in the large or dynamic case. This node is analyzed and
513    --    resolved on return.
514    --
515    --    Shift is a node representing the shift count to be used in the
516    --    rotate right instruction that positions the field for access.
517    --    This node is analyzed and resolved on return.
518    --
519    --    Cmask is a mask corresponding to the width of the component field.
520    --    Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
521    --
522    --  Note: in some cases the call to this routine may generate actions
523    --  (for handling multi-use references and the generation of the packed
524    --  array type on the fly). Such actions are inserted into the tree
525    --  directly using Insert_Action.
526
527    ------------------------------
528    -- Compute_Linear_Subcsript --
529    ------------------------------
530
531    procedure Compute_Linear_Subscript
532      (Atyp   : Entity_Id;
533       N      : Node_Id;
534       Subscr : out Node_Id)
535    is
536       Loc    : constant Source_Ptr := Sloc (N);
537       Oldsub : Node_Id;
538       Newsub : Node_Id;
539       Indx   : Node_Id;
540       Styp   : Entity_Id;
541
542    begin
543       Subscr := Empty;
544
545       --  Loop through dimensions
546
547       Indx   := First_Index (Atyp);
548       Oldsub := First (Expressions (N));
549
550       while Present (Indx) loop
551          Styp := Etype (Indx);
552          Newsub := Relocate_Node (Oldsub);
553
554          --  Get expression for the subscript value. First, if Do_Range_Check
555          --  is set on a subscript, then we must do a range check against the
556          --  original bounds (not the bounds of the packed array type). We do
557          --  this by introducing a subtype conversion.
558
559          if Do_Range_Check (Newsub)
560            and then Etype (Newsub) /= Styp
561          then
562             Newsub := Convert_To (Styp, Newsub);
563          end if;
564
565          --  Now evolve the expression for the subscript. First convert
566          --  the subscript to be zero based and of an integer type.
567
568          --  Case of integer type, where we just subtract to get lower bound
569
570          if Is_Integer_Type (Styp) then
571
572             --  If length of integer type is smaller than standard integer,
573             --  then we convert to integer first, then do the subtract
574
575             --  Integer (subscript) - Integer (Styp'First)
576
577             if Esize (Styp) < Esize (Standard_Integer) then
578                Newsub :=
579                  Make_Op_Subtract (Loc,
580                    Left_Opnd => Convert_To (Standard_Integer, Newsub),
581                  Right_Opnd =>
582                    Convert_To (Standard_Integer,
583                      Make_Attribute_Reference (Loc,
584                        Prefix => New_Occurrence_Of (Styp, Loc),
585                        Attribute_Name => Name_First)));
586
587             --  For larger integer types, subtract first, then convert to
588             --  integer, this deals with strange long long integer bounds.
589
590             --    Integer (subscript - Styp'First)
591
592             else
593                Newsub :=
594                  Convert_To (Standard_Integer,
595                    Make_Op_Subtract (Loc,
596                      Left_Opnd => Newsub,
597                    Right_Opnd =>
598                      Make_Attribute_Reference (Loc,
599                        Prefix => New_Occurrence_Of (Styp, Loc),
600                        Attribute_Name => Name_First)));
601             end if;
602
603          --  For the enumeration case, we have to use 'Pos to get the value
604          --  to work with before subtracting the lower bound.
605
606          --    Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
607
608          --  This is not quite right for bizarre cases where the size of the
609          --  enumeration type is > Integer'Size bits due to rep clause ???
610
611          else
612             pragma Assert (Is_Enumeration_Type (Styp));
613
614             Newsub :=
615               Make_Op_Subtract (Loc,
616                 Left_Opnd => Convert_To (Standard_Integer,
617                   Make_Attribute_Reference (Loc,
618                     Prefix => New_Occurrence_Of (Styp, Loc),
619                     Attribute_Name => Name_Pos,
620                     Expressions => New_List (Newsub))),
621
622                 Right_Opnd =>
623                   Convert_To (Standard_Integer,
624                     Make_Attribute_Reference (Loc,
625                       Prefix => New_Occurrence_Of (Styp, Loc),
626                       Attribute_Name => Name_Pos,
627                       Expressions => New_List (
628                         Make_Attribute_Reference (Loc,
629                         Prefix => New_Occurrence_Of (Styp, Loc),
630                         Attribute_Name => Name_First)))));
631          end if;
632
633          Set_Paren_Count (Newsub, 1);
634
635          --  For the first subscript, we just copy that subscript value
636
637          if No (Subscr) then
638             Subscr := Newsub;
639
640          --  Otherwise, we must multiply what we already have by the current
641          --  stride and then add in the new value to the evolving subscript.
642
643          else
644             Subscr :=
645               Make_Op_Add (Loc,
646                 Left_Opnd =>
647                   Make_Op_Multiply (Loc,
648                     Left_Opnd  => Subscr,
649                     Right_Opnd =>
650                       Make_Attribute_Reference (Loc,
651                         Attribute_Name => Name_Range_Length,
652                         Prefix         => New_Occurrence_Of (Styp, Loc))),
653                 Right_Opnd => Newsub);
654          end if;
655
656          --  Move to next subscript
657
658          Next_Index (Indx);
659          Next (Oldsub);
660       end loop;
661    end Compute_Linear_Subscript;
662
663    -------------------------
664    -- Convert_To_PAT_Type --
665    -------------------------
666
667    --  The PAT is always obtained from the actual subtype
668
669    procedure Convert_To_PAT_Type (Aexp : Entity_Id) is
670       Act_ST : Entity_Id;
671
672    begin
673       Convert_To_Actual_Subtype (Aexp);
674       Act_ST := Underlying_Type (Etype (Aexp));
675       Create_Packed_Array_Type (Act_ST);
676
677       --  Just replace the etype with the packed array type. This works
678       --  because the expression will not be further analyzed, and Gigi
679       --  considers the two types equivalent in any case.
680
681       Set_Etype (Aexp, Packed_Array_Type (Act_ST));
682    end Convert_To_PAT_Type;
683
684    ------------------------------
685    -- Create_Packed_Array_Type --
686    ------------------------------
687
688    procedure Create_Packed_Array_Type (Typ : Entity_Id) is
689       Loc      : constant Source_Ptr := Sloc (Typ);
690       Ctyp     : constant Entity_Id  := Component_Type (Typ);
691       Csize    : constant Uint       := Component_Size (Typ);
692
693       Ancest   : Entity_Id;
694       PB_Type  : Entity_Id;
695       Esiz     : Uint;
696       Decl     : Node_Id;
697       PAT      : Entity_Id;
698       Len_Dim  : Node_Id;
699       Len_Expr : Node_Id;
700       Len_Bits : Uint;
701       Bits_U1  : Node_Id;
702       PAT_High : Node_Id;
703       Btyp     : Entity_Id;
704       Lit      : Node_Id;
705
706       procedure Install_PAT;
707       --  This procedure is called with Decl set to the declaration for the
708       --  packed array type. It creates the type and installs it as required.
709
710       procedure Set_PB_Type;
711       --  Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
712       --  requirements (see documentation in the spec of this package).
713
714       -----------------
715       -- Install_PAT --
716       -----------------
717
718       procedure Install_PAT is
719          Pushed_Scope : Boolean := False;
720
721       begin
722          --  We do not want to put the declaration we have created in the tree
723          --  since it is often hard, and sometimes impossible to find a proper
724          --  place for it (the impossible case arises for a packed array type
725          --  with bounds depending on the discriminant, a declaration cannot
726          --  be put inside the record, and the reference to the discriminant
727          --  cannot be outside the record).
728
729          --  The solution is to analyze the declaration while temporarily
730          --  attached to the tree at an appropriate point, and then we install
731          --  the resulting type as an Itype in the packed array type field of
732          --  the original type, so that no explicit declaration is required.
733
734          --  Note: the packed type is created in the scope of its parent
735          --  type. There are at least some cases where the current scope
736          --  is deeper, and so when this is the case, we temporarily reset
737          --  the scope for the definition. This is clearly safe, since the
738          --  first use of the packed array type will be the implicit
739          --  reference from the corresponding unpacked type when it is
740          --  elaborated.
741
742          if Is_Itype (Typ) then
743             Set_Parent (Decl, Associated_Node_For_Itype (Typ));
744          else
745             Set_Parent (Decl, Declaration_Node (Typ));
746          end if;
747
748          if Scope (Typ) /= Current_Scope then
749             New_Scope (Scope (Typ));
750             Pushed_Scope := True;
751          end if;
752
753          Set_Is_Itype (PAT, True);
754          Set_Is_Packed_Array_Type (PAT, True);
755          Analyze (Decl, Suppress => All_Checks);
756
757          if Pushed_Scope then
758             Pop_Scope;
759          end if;
760
761          --  Set Esize and RM_Size to the actual size of the packed object
762          --  Do not reset RM_Size if already set, as happens in the case
763          --  of a modular type
764
765          Set_Esize (PAT, Esiz);
766
767          if Unknown_RM_Size (PAT) then
768             Set_RM_Size (PAT, Esiz);
769          end if;
770
771          --  Set remaining fields of packed array type
772
773          Init_Alignment (PAT);
774          Set_Parent     (PAT, Empty);
775          Set_Packed_Array_Type (Typ, PAT);
776          Set_Associated_Node_For_Itype (PAT, Typ);
777
778          --  We definitely do not want to delay freezing for packed array
779          --  types. This is of particular importance for the itypes that
780          --  are generated for record components depending on discriminants
781          --  where there is no place to put the freeze node.
782
783          Set_Has_Delayed_Freeze (PAT, False);
784          Set_Has_Delayed_Freeze (Etype (PAT), False);
785       end Install_PAT;
786
787       -----------------
788       -- Set_PB_Type --
789       -----------------
790
791       procedure Set_PB_Type is
792       begin
793          --  If the user has specified an explicit alignment for the
794          --  component, take it into account.
795
796          if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
797            or else Component_Alignment (Typ) = Calign_Storage_Unit
798          then
799             PB_Type := RTE (RE_Packed_Bytes1);
800
801          elsif Csize mod 4 /= 0 then
802             PB_Type := RTE (RE_Packed_Bytes2);
803
804          else
805             PB_Type := RTE (RE_Packed_Bytes4);
806          end if;
807       end Set_PB_Type;
808
809    --  Start of processing for Create_Packed_Array_Type
810
811    begin
812       --  If we already have a packed array type, nothing to do
813
814       if Present (Packed_Array_Type (Typ)) then
815          return;
816       end if;
817
818       --  If our immediate ancestor subtype is constrained, and it already
819       --  has a packed array type, then just share the same type, since the
820       --  bounds must be the same.
821
822       if Ekind (Typ) = E_Array_Subtype then
823          Ancest := Ancestor_Subtype (Typ);
824
825          if Present (Ancest)
826            and then Is_Constrained (Ancest)
827            and then Present (Packed_Array_Type (Ancest))
828          then
829             Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
830             return;
831          end if;
832       end if;
833
834       --  We preset the result type size from the size of the original array
835       --  type, since this size clearly belongs to the packed array type. The
836       --  size of the conceptual unpacked type is always set to unknown.
837
838       Esiz := Esize (Typ);
839
840       --  Case of an array where at least one index is of an enumeration
841       --  type with a non-standard representation, but the component size
842       --  is not appropriate for bit packing. This is the case where we
843       --  have Is_Packed set (we would never be in this unit otherwise),
844       --  but Is_Bit_Packed_Array is false.
845
846       --  Note that if the component size is appropriate for bit packing,
847       --  then the circuit for the computation of the subscript properly
848       --  deals with the non-standard enumeration type case by taking the
849       --  Pos anyway.
850
851       if not Is_Bit_Packed_Array (Typ) then
852
853          --  Here we build a declaration:
854
855          --    type tttP is array (index1, index2, ...) of component_type
856
857          --  where index1, index2, are the index types. These are the same
858          --  as the index types of the original array, except for the non-
859          --  standard representation enumeration type case, where we have
860          --  two subcases.
861
862          --  For the unconstrained array case, we use
863
864          --    Natural range <>
865
866          --  For the constrained case, we use
867
868          --    Natural range Enum_Type'Pos (Enum_Type'First) ..
869          --                  Enum_Type'Pos (Enum_Type'Last);
870
871          PAT :=
872            Make_Defining_Identifier (Loc,
873              Chars => New_External_Name (Chars (Typ), 'P'));
874
875          Set_Packed_Array_Type (Typ, PAT);
876
877          declare
878             Indexes   : List_Id := New_List;
879             Indx      : Node_Id;
880             Indx_Typ  : Entity_Id;
881             Enum_Case : Boolean;
882             Typedef   : Node_Id;
883
884          begin
885             Indx := First_Index (Typ);
886
887             while Present (Indx) loop
888                Indx_Typ := Etype (Indx);
889
890                Enum_Case := Is_Enumeration_Type (Indx_Typ)
891                               and then Has_Non_Standard_Rep (Indx_Typ);
892
893                --  Unconstrained case
894
895                if not Is_Constrained (Typ) then
896                   if Enum_Case then
897                      Indx_Typ := Standard_Natural;
898                   end if;
899
900                   Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
901
902                --  Constrained case
903
904                else
905                   if not Enum_Case then
906                      Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
907
908                   else
909                      Append_To (Indexes,
910                        Make_Subtype_Indication (Loc,
911                          Subtype_Mark =>
912                            New_Occurrence_Of (Standard_Natural, Loc),
913                          Constraint =>
914                            Make_Range_Constraint (Loc,
915                              Range_Expression =>
916                                Make_Range (Loc,
917                                  Low_Bound =>
918                                    Make_Attribute_Reference (Loc,
919                                      Prefix =>
920                                        New_Occurrence_Of (Indx_Typ, Loc),
921                                      Attribute_Name => Name_Pos,
922                                      Expressions => New_List (
923                                        Make_Attribute_Reference (Loc,
924                                          Prefix =>
925                                            New_Occurrence_Of (Indx_Typ, Loc),
926                                          Attribute_Name => Name_First))),
927
928                                  High_Bound =>
929                                    Make_Attribute_Reference (Loc,
930                                      Prefix =>
931                                        New_Occurrence_Of (Indx_Typ, Loc),
932                                      Attribute_Name => Name_Pos,
933                                      Expressions => New_List (
934                                        Make_Attribute_Reference (Loc,
935                                          Prefix =>
936                                            New_Occurrence_Of (Indx_Typ, Loc),
937                                          Attribute_Name => Name_Last)))))));
938
939                   end if;
940                end if;
941
942                Next_Index (Indx);
943             end loop;
944
945             if not Is_Constrained (Typ) then
946                Typedef :=
947                  Make_Unconstrained_Array_Definition (Loc,
948                    Subtype_Marks => Indexes,
949                    Subtype_Indication =>
950                       New_Occurrence_Of (Ctyp, Loc));
951
952             else
953                Typedef :=
954                   Make_Constrained_Array_Definition (Loc,
955                     Discrete_Subtype_Definitions => Indexes,
956                     Subtype_Indication =>
957                       New_Occurrence_Of (Ctyp, Loc));
958             end if;
959
960             Decl :=
961               Make_Full_Type_Declaration (Loc,
962                 Defining_Identifier => PAT,
963                 Type_Definition => Typedef);
964          end;
965
966          Install_PAT;
967          return;
968
969       --  Case of bit-packing required for unconstrained array. We simply
970       --  use Packed_Bytes{1,2,4} as appropriate, and we do not need to
971       --  construct a special packed array type.
972
973       elsif not Is_Constrained (Typ) then
974          Set_PB_Type;
975          Set_Packed_Array_Type (Typ, PB_Type);
976          Set_Is_Packed_Array_Type (Packed_Array_Type (Typ), True);
977          return;
978
979       --  Remaining code is for the case of bit-packing for constrained array
980
981       --  The name of the packed array subtype is
982
983       --    ttt___Xsss
984
985       --  where sss is the component size in bits and ttt is the name of
986       --  the parent packed type.
987
988       else
989          PAT :=
990            Make_Defining_Identifier (Loc,
991              Chars => Make_Packed_Array_Type_Name (Typ, Csize));
992
993          Set_Packed_Array_Type (Typ, PAT);
994
995          --  Build an expression for the length of the array in bits.
996          --  This is the product of the length of each of the dimensions
997
998          declare
999             J : Nat := 1;
1000
1001          begin
1002             Len_Expr := Empty; -- suppress junk warning
1003
1004             loop
1005                Len_Dim :=
1006                  Make_Attribute_Reference (Loc,
1007                    Attribute_Name => Name_Length,
1008                    Prefix         => New_Occurrence_Of (Typ, Loc),
1009                    Expressions    => New_List (
1010                      Make_Integer_Literal (Loc, J)));
1011
1012                if J = 1 then
1013                   Len_Expr := Len_Dim;
1014
1015                else
1016                   Len_Expr :=
1017                     Make_Op_Multiply (Loc,
1018                       Left_Opnd  => Len_Expr,
1019                       Right_Opnd => Len_Dim);
1020                end if;
1021
1022                J := J + 1;
1023                exit when J > Number_Dimensions (Typ);
1024             end loop;
1025          end;
1026
1027          --  Temporarily attach the length expression to the tree and analyze
1028          --  and resolve it, so that we can test its value. We assume that the
1029          --  total length fits in type Integer.
1030
1031          Set_Parent (Len_Expr, Typ);
1032          Analyze_And_Resolve (Len_Expr, Standard_Integer);
1033
1034          --  Use a modular type if possible. We can do this if we are we
1035          --  have static bounds, and the length is small enough, and the
1036          --  length is not zero. We exclude the zero length case because the
1037          --  size of things is always at least one, and the zero length object
1038          --  would have an anomous size
1039
1040          if Compile_Time_Known_Value (Len_Expr) then
1041             Len_Bits := Expr_Value (Len_Expr) * Csize;
1042
1043             --  We normally consider small enough to mean no larger than the
1044             --  value of System_Max_Binary_Modulus_Power, except that in
1045             --  No_Run_Time mode, we use the Word Size on machines for
1046             --  which double length shifts are not generated in line.
1047
1048             if Len_Bits > 0
1049               and then
1050                 (Len_Bits <= System_Word_Size
1051                    or else (Len_Bits <= System_Max_Binary_Modulus_Power
1052                               and then (not No_Run_Time
1053                                           or else
1054                                         Long_Shifts_Inlined_On_Target)))
1055             then
1056                --  We can use the modular type, it has the form:
1057
1058                --    subtype tttPn is btyp
1059                --      range 0 .. 2 ** (Esize (Typ) * Csize) - 1;
1060
1061                --  Here Siz is 1, 2 or 4, as computed above, and btyp is either
1062                --  Unsigned or Long_Long_Unsigned depending on the length.
1063
1064                if Len_Bits <= Standard_Integer_Size then
1065                   Btyp := RTE (RE_Unsigned);
1066                else
1067                   Btyp := RTE (RE_Long_Long_Unsigned);
1068                end if;
1069
1070                Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1071                Set_Print_In_Hex (Lit);
1072
1073                Decl :=
1074                  Make_Subtype_Declaration (Loc,
1075                    Defining_Identifier => PAT,
1076                      Subtype_Indication =>
1077                        Make_Subtype_Indication (Loc,
1078                          Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1079
1080                          Constraint =>
1081                            Make_Range_Constraint (Loc,
1082                              Range_Expression =>
1083                                Make_Range (Loc,
1084                                  Low_Bound =>
1085                                    Make_Integer_Literal (Loc, 0),
1086                                  High_Bound => Lit))));
1087
1088                if Esiz = Uint_0 then
1089                   Esiz := Len_Bits;
1090                end if;
1091
1092                Install_PAT;
1093                return;
1094             end if;
1095          end if;
1096
1097          --  Could not use a modular type, for all other cases, we build
1098          --  a packed array subtype:
1099
1100          --    subtype tttPn is
1101          --      System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1102
1103          --  Bits is the length of the array in bits.
1104
1105          Set_PB_Type;
1106
1107          Bits_U1 :=
1108            Make_Op_Add (Loc,
1109              Left_Opnd =>
1110                Make_Op_Multiply (Loc,
1111                  Left_Opnd  =>
1112                    Make_Integer_Literal (Loc, Csize),
1113                  Right_Opnd => Len_Expr),
1114
1115              Right_Opnd =>
1116                Make_Integer_Literal (Loc, 7));
1117
1118          Set_Paren_Count (Bits_U1, 1);
1119
1120          PAT_High :=
1121            Make_Op_Subtract (Loc,
1122              Left_Opnd =>
1123                Make_Op_Divide (Loc,
1124                  Left_Opnd => Bits_U1,
1125                  Right_Opnd => Make_Integer_Literal (Loc, 8)),
1126              Right_Opnd => Make_Integer_Literal (Loc, 1));
1127
1128          Decl :=
1129            Make_Subtype_Declaration (Loc,
1130              Defining_Identifier => PAT,
1131                Subtype_Indication =>
1132                  Make_Subtype_Indication (Loc,
1133                    Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1134                    Constraint =>
1135
1136                      Make_Index_Or_Discriminant_Constraint (Loc,
1137                        Constraints => New_List (
1138                          Make_Range (Loc,
1139                            Low_Bound =>
1140                              Make_Integer_Literal (Loc, 0),
1141                            High_Bound => PAT_High)))));
1142
1143          Install_PAT;
1144       end if;
1145    end Create_Packed_Array_Type;
1146
1147    -----------------------------------
1148    -- Expand_Bit_Packed_Element_Set --
1149    -----------------------------------
1150
1151    procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1152       Loc : constant Source_Ptr := Sloc (N);
1153       Lhs : constant Node_Id    := Name (N);
1154
1155       Ass_OK : constant Boolean := Assignment_OK (Lhs);
1156       --  Used to preserve assignment OK status when assignment is rewritten
1157
1158       Rhs : Node_Id := Expression (N);
1159       --  Initially Rhs is the right hand side value, it will be replaced
1160       --  later by an appropriate unchecked conversion for the assignment.
1161
1162       Obj    : Node_Id;
1163       Atyp   : Entity_Id;
1164       PAT    : Entity_Id;
1165       Ctyp   : Entity_Id;
1166       Csiz   : Int;
1167       Shift  : Node_Id;
1168       Cmask  : Uint;
1169
1170       New_Lhs : Node_Id;
1171       New_Rhs : Node_Id;
1172
1173       Rhs_Val_Known : Boolean;
1174       Rhs_Val       : Uint;
1175       --  If the value of the right hand side as an integer constant is
1176       --  known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1177       --  contains the value. Otherwise Rhs_Val_Known is set False, and
1178       --  the Rhs_Val is undefined.
1179
1180    begin
1181       pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1182
1183       Obj := Relocate_Node (Prefix (Lhs));
1184       Convert_To_Actual_Subtype (Obj);
1185       Atyp := Etype (Obj);
1186       PAT  := Packed_Array_Type (Atyp);
1187       Ctyp := Component_Type (Atyp);
1188       Csiz := UI_To_Int (Component_Size (Atyp));
1189
1190       --  We convert the right hand side to the proper subtype to ensure
1191       --  that an appropriate range check is made (since the normal range
1192       --  check from assignment will be lost in the transformations). This
1193       --  conversion is analyzed immediately so that subsequent processing
1194       --  can work with an analyzed Rhs (and e.g. look at its Etype)
1195
1196       Rhs := Convert_To (Ctyp, Rhs);
1197       Set_Parent (Rhs, N);
1198       Analyze_And_Resolve (Rhs, Ctyp);
1199
1200       --  Case of component size 1,2,4 or any component size for the modular
1201       --  case. These are the cases for which we can inline the code.
1202
1203       if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1204         or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1205       then
1206          Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1207
1208          --  The statement to be generated is:
1209
1210          --    Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1211
1212          --      where mask1 is obtained by shifting Cmask left Shift bits
1213          --      and then complementing the result.
1214
1215          --      the "and Mask1" is omitted if rhs is constant and all 1 bits
1216
1217          --      the "or ..." is omitted if rhs is constant and all 0 bits
1218
1219          --      rhs is converted to the appropriate type.
1220
1221          --      The result is converted back to the array type, since
1222          --      otherwise we lose knowledge of the packed nature.
1223
1224          --  Determine if right side is all 0 bits or all 1 bits
1225
1226          if Compile_Time_Known_Value (Rhs) then
1227             Rhs_Val       := Expr_Rep_Value (Rhs);
1228             Rhs_Val_Known := True;
1229
1230          --  The following test catches the case of an unchecked conversion
1231          --  of an integer literal. This results from optimizing aggregates
1232          --  of packed types.
1233
1234          elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1235            and then Compile_Time_Known_Value (Expression (Rhs))
1236          then
1237             Rhs_Val       := Expr_Rep_Value (Expression (Rhs));
1238             Rhs_Val_Known := True;
1239
1240          else
1241             Rhs_Val       := No_Uint;
1242             Rhs_Val_Known := False;
1243          end if;
1244
1245          --  Some special checks for the case where the right hand value
1246          --  is known at compile time. Basically we have to take care of
1247          --  the implicit conversion to the subtype of the component object.
1248
1249          if Rhs_Val_Known then
1250
1251             --  If we have a biased component type then we must manually do
1252             --  the biasing, since we are taking responsibility in this case
1253             --  for constructing the exact bit pattern to be used.
1254
1255             if Has_Biased_Representation (Ctyp) then
1256                Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1257             end if;
1258
1259             --  For a negative value, we manually convert the twos complement
1260             --  value to a corresponding unsigned value, so that the proper
1261             --  field width is maintained. If we did not do this, we would
1262             --  get too many leading sign bits later on.
1263
1264             if Rhs_Val < 0 then
1265                Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1266             end if;
1267          end if;
1268
1269          New_Lhs := Duplicate_Subexpr (Obj, True);
1270          New_Rhs := Duplicate_Subexpr (Obj);
1271
1272          --  First we deal with the "and"
1273
1274          if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1275             declare
1276                Mask1 : Node_Id;
1277                Lit   : Node_Id;
1278
1279             begin
1280                if Compile_Time_Known_Value (Shift) then
1281                   Mask1 :=
1282                     Make_Integer_Literal (Loc,
1283                       Modulus (Etype (Obj)) - 1 -
1284                                  (Cmask * (2 ** Expr_Value (Shift))));
1285                   Set_Print_In_Hex (Mask1);
1286
1287                else
1288                   Lit := Make_Integer_Literal (Loc, Cmask);
1289                   Set_Print_In_Hex (Lit);
1290                   Mask1 :=
1291                     Make_Op_Not (Loc,
1292                       Right_Opnd => Make_Shift_Left (Lit, Shift));
1293                end if;
1294
1295                New_Rhs :=
1296                  Make_Op_And (Loc,
1297                    Left_Opnd  => New_Rhs,
1298                    Right_Opnd => Mask1);
1299             end;
1300          end if;
1301
1302          --  Then deal with the "or"
1303
1304          if not Rhs_Val_Known or else Rhs_Val /= 0 then
1305             declare
1306                Or_Rhs : Node_Id;
1307
1308                procedure Fixup_Rhs;
1309                --  Adjust Rhs by bias if biased representation for components
1310                --  or remove extraneous high order sign bits if signed.
1311
1312                procedure Fixup_Rhs is
1313                   Etyp : constant Entity_Id := Etype (Rhs);
1314
1315                begin
1316                   --  For biased case, do the required biasing by simply
1317                   --  converting to the biased subtype (the conversion
1318                   --  will generate the required bias).
1319
1320                   if Has_Biased_Representation (Ctyp) then
1321                      Rhs := Convert_To (Ctyp, Rhs);
1322
1323                   --  For a signed integer type that is not biased, generate
1324                   --  a conversion to unsigned to strip high order sign bits.
1325
1326                   elsif Is_Signed_Integer_Type (Ctyp) then
1327                      Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1328                   end if;
1329
1330                   --  Set Etype, since it can be referenced before the
1331                   --  node is completely analyzed.
1332
1333                   Set_Etype (Rhs, Etyp);
1334
1335                   --  We now need to do an unchecked conversion of the
1336                   --  result to the target type, but it is important that
1337                   --  this conversion be a right justified conversion and
1338                   --  not a left justified conversion.
1339
1340                   Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1341
1342                end Fixup_Rhs;
1343
1344             begin
1345                if Rhs_Val_Known
1346                  and then Compile_Time_Known_Value (Shift)
1347                then
1348                   Or_Rhs :=
1349                     Make_Integer_Literal (Loc,
1350                       Rhs_Val * (2 ** Expr_Value (Shift)));
1351                   Set_Print_In_Hex (Or_Rhs);
1352
1353                else
1354                   --  We have to convert the right hand side to Etype (Obj).
1355                   --  A special case case arises if what we have now is a Val
1356                   --  attribute reference whose expression type is Etype (Obj).
1357                   --  This happens for assignments of fields from the same
1358                   --  array. In this case we get the required right hand side
1359                   --  by simply removing the inner attribute reference.
1360
1361                   if Nkind (Rhs) = N_Attribute_Reference
1362                     and then Attribute_Name (Rhs) = Name_Val
1363                     and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1364                   then
1365                      Rhs := Relocate_Node (First (Expressions (Rhs)));
1366                      Fixup_Rhs;
1367
1368                   --  If the value of the right hand side is a known integer
1369                   --  value, then just replace it by an untyped constant,
1370                   --  which will be properly retyped when we analyze and
1371                   --  resolve the expression.
1372
1373                   elsif Rhs_Val_Known then
1374
1375                      --  Note that Rhs_Val has already been normalized to
1376                      --  be an unsigned value with the proper number of bits.
1377
1378                      Rhs :=
1379                        Make_Integer_Literal (Loc, Rhs_Val);
1380
1381                   --  Otherwise we need an unchecked conversion
1382
1383                   else
1384                      Fixup_Rhs;
1385                   end if;
1386
1387                   Or_Rhs := Make_Shift_Left (Rhs, Shift);
1388                end if;
1389
1390                if Nkind (New_Rhs) = N_Op_And then
1391                   Set_Paren_Count (New_Rhs, 1);
1392                end if;
1393
1394                New_Rhs :=
1395                  Make_Op_Or (Loc,
1396                    Left_Opnd  => New_Rhs,
1397                    Right_Opnd => Or_Rhs);
1398             end;
1399          end if;
1400
1401          --  Now do the rewrite
1402
1403          Rewrite (N,
1404            Make_Assignment_Statement (Loc,
1405              Name       => New_Lhs,
1406              Expression =>
1407                Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1408          Set_Assignment_OK (Name (N), Ass_OK);
1409
1410       --  All other component sizes for non-modular case
1411
1412       else
1413          --  We generate
1414
1415          --    Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1416
1417          --  where Subscr is the computed linear subscript.
1418
1419          declare
1420             Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1421             Set_nn  : Entity_Id;
1422             Subscr  : Node_Id;
1423             Atyp    : Entity_Id;
1424
1425          begin
1426             --  Acquire proper Set entity. We use the aligned or unaligned
1427             --  case as appropriate.
1428
1429             if Must_Be_Aligned (Obj) then
1430                Set_nn := RTE (Set_Id (Csiz));
1431             else
1432                Set_nn := RTE (SetU_Id (Csiz));
1433             end if;
1434
1435             --  Now generate the set reference
1436
1437             Obj := Relocate_Node (Prefix (Lhs));
1438             Convert_To_Actual_Subtype (Obj);
1439             Atyp := Etype (Obj);
1440             Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1441
1442             Rewrite (N,
1443               Make_Procedure_Call_Statement (Loc,
1444                   Name => New_Occurrence_Of (Set_nn, Loc),
1445                   Parameter_Associations => New_List (
1446                     Make_Attribute_Reference (Loc,
1447                       Attribute_Name => Name_Address,
1448                       Prefix => Obj),
1449                     Subscr,
1450                     Unchecked_Convert_To (Bits_nn,
1451                       Convert_To (Ctyp, Rhs)))));
1452
1453          end;
1454       end if;
1455
1456       Analyze (N, Suppress => All_Checks);
1457    end Expand_Bit_Packed_Element_Set;
1458
1459    -------------------------------------
1460    -- Expand_Packed_Address_Reference --
1461    -------------------------------------
1462
1463    procedure Expand_Packed_Address_Reference (N : Node_Id) is
1464       Loc    : constant Source_Ptr := Sloc (N);
1465       Ploc   : Source_Ptr;
1466       Pref   : Node_Id;
1467       Expr   : Node_Id;
1468       Term   : Node_Id;
1469       Atyp   : Entity_Id;
1470       Subscr : Node_Id;
1471
1472    begin
1473       Pref := Prefix (N);
1474       Expr := Empty;
1475
1476       --  We build up an expression serially that has the form
1477
1478       --    outer_object'Address
1479       --      + (linear-subscript * component_size  for each array reference
1480       --      +  field'Bit_Position                 for each record field
1481       --      +  ...
1482       --      +  ...) / Storage_Unit;
1483
1484       --  Some additional conversions are required to deal with the addition
1485       --  operation, which is not normally visible to generated code.
1486
1487       loop
1488          Ploc := Sloc (Pref);
1489
1490          if Nkind (Pref) = N_Indexed_Component then
1491             Convert_To_Actual_Subtype (Prefix (Pref));
1492             Atyp := Etype (Prefix (Pref));
1493             Compute_Linear_Subscript (Atyp, Pref, Subscr);
1494
1495             Term :=
1496               Make_Op_Multiply (Ploc,
1497                 Left_Opnd => Subscr,
1498                 Right_Opnd =>
1499                  Make_Attribute_Reference (Ploc,
1500                    Prefix => New_Occurrence_Of (Atyp, Ploc),
1501                    Attribute_Name => Name_Component_Size));
1502
1503          elsif Nkind (Pref) = N_Selected_Component then
1504             Term :=
1505               Make_Attribute_Reference (Ploc,
1506                 Prefix => Selector_Name (Pref),
1507                 Attribute_Name => Name_Bit_Position);
1508
1509          else
1510             exit;
1511          end if;
1512
1513          Term := Convert_To (RTE (RE_Integer_Address), Term);
1514
1515          if No (Expr) then
1516             Expr := Term;
1517
1518          else
1519             Expr :=
1520               Make_Op_Add (Ploc,
1521                 Left_Opnd  => Expr,
1522                 Right_Opnd => Term);
1523          end if;
1524
1525          Pref := Prefix (Pref);
1526       end loop;
1527
1528       Rewrite (N,
1529         Unchecked_Convert_To (RTE (RE_Address),
1530           Make_Op_Add (Loc,
1531             Left_Opnd =>
1532               Unchecked_Convert_To (RTE (RE_Integer_Address),
1533                 Make_Attribute_Reference (Loc,
1534                   Prefix => Pref,
1535                   Attribute_Name => Name_Address)),
1536
1537             Right_Opnd =>
1538               Make_Op_Divide (Loc,
1539                 Left_Opnd => Expr,
1540                 Right_Opnd =>
1541                   Make_Integer_Literal (Loc, System_Storage_Unit)))));
1542
1543       Analyze_And_Resolve (N, RTE (RE_Address));
1544    end Expand_Packed_Address_Reference;
1545
1546    ------------------------------------
1547    -- Expand_Packed_Boolean_Operator --
1548    ------------------------------------
1549
1550    --  This routine expands "a op b" for the packed cases
1551
1552    procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1553       Loc : constant Source_Ptr := Sloc (N);
1554       Typ : constant Entity_Id  := Etype (N);
1555       L   : constant Node_Id    := Relocate_Node (Left_Opnd  (N));
1556       R   : constant Node_Id    := Relocate_Node (Right_Opnd (N));
1557
1558       Ltyp : Entity_Id;
1559       Rtyp : Entity_Id;
1560       PAT  : Entity_Id;
1561
1562    begin
1563       Convert_To_Actual_Subtype (L);
1564       Convert_To_Actual_Subtype (R);
1565
1566       Ensure_Defined (Etype (L), N);
1567       Ensure_Defined (Etype (R), N);
1568
1569       Apply_Length_Check (R, Etype (L));
1570
1571       Ltyp := Etype (L);
1572       Rtyp := Etype (R);
1573
1574       --  First an odd and silly test. We explicitly check for the XOR
1575       --  case where the component type is True .. True, since this will
1576       --  raise constraint error. A special check is required since CE
1577       --  will not be required other wise (cf Expand_Packed_Not).
1578
1579       --  No such check is required for AND and OR, since for both these
1580       --  cases False op False = False, and True op True = True.
1581
1582       if Nkind (N) = N_Op_Xor then
1583          declare
1584             CT : constant Entity_Id := Component_Type (Rtyp);
1585             BT : constant Entity_Id := Base_Type (CT);
1586
1587          begin
1588             Insert_Action (N,
1589               Make_Raise_Constraint_Error (Loc,
1590                 Condition =>
1591                   Make_Op_And (Loc,
1592                     Left_Opnd =>
1593                       Make_Op_Eq (Loc,
1594                         Left_Opnd =>
1595                           Make_Attribute_Reference (Loc,
1596                             Prefix         => New_Occurrence_Of (CT, Loc),
1597                             Attribute_Name => Name_First),
1598
1599                         Right_Opnd =>
1600                           Convert_To (BT,
1601                             New_Occurrence_Of (Standard_True, Loc))),
1602
1603                     Right_Opnd =>
1604                       Make_Op_Eq (Loc,
1605                         Left_Opnd =>
1606                           Make_Attribute_Reference (Loc,
1607                             Prefix         => New_Occurrence_Of (CT, Loc),
1608                             Attribute_Name => Name_Last),
1609
1610                         Right_Opnd =>
1611                           Convert_To (BT,
1612                             New_Occurrence_Of (Standard_True, Loc))))));
1613          end;
1614       end if;
1615
1616       --  Now that that silliness is taken care of, get packed array type
1617
1618       Convert_To_PAT_Type (L);
1619       Convert_To_PAT_Type (R);
1620
1621       PAT := Etype (L);
1622
1623       --  For the modular case, we expand a op b into
1624
1625       --    rtyp!(pat!(a) op pat!(b))
1626
1627       --  where rtyp is the Etype of the left operand. Note that we do not
1628       --  convert to the base type, since this would be unconstrained, and
1629       --  hence not have a corresponding packed array type set.
1630
1631       if Is_Modular_Integer_Type (PAT) then
1632          declare
1633             P : Node_Id;
1634
1635          begin
1636             if Nkind (N) = N_Op_And then
1637                P := Make_Op_And (Loc, L, R);
1638
1639             elsif Nkind (N) = N_Op_Or then
1640                P := Make_Op_Or  (Loc, L, R);
1641
1642             else -- Nkind (N) = N_Op_Xor
1643                P := Make_Op_Xor (Loc, L, R);
1644             end if;
1645
1646             Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1647          end;
1648
1649       --  For the array case, we insert the actions
1650
1651       --    Result : Ltype;
1652
1653       --    System.Bitops.Bit_And/Or/Xor
1654       --     (Left'Address,
1655       --      Ltype'Length * Ltype'Component_Size;
1656       --      Right'Address,
1657       --      Rtype'Length * Rtype'Component_Size
1658       --      Result'Address);
1659
1660       --  where Left and Right are the Packed_Bytes{1,2,4} operands and
1661       --  the second argument and fourth arguments are the lengths of the
1662       --  operands in bits. Then we replace the expression by a reference
1663       --  to Result.
1664
1665       else
1666          declare
1667             Result_Ent : constant Entity_Id :=
1668                            Make_Defining_Identifier (Loc,
1669                              Chars => New_Internal_Name ('T'));
1670
1671             E_Id : RE_Id;
1672
1673          begin
1674             if Nkind (N) = N_Op_And then
1675                E_Id := RE_Bit_And;
1676
1677             elsif Nkind (N) = N_Op_Or then
1678                E_Id := RE_Bit_Or;
1679
1680             else -- Nkind (N) = N_Op_Xor
1681                E_Id := RE_Bit_Xor;
1682             end if;
1683
1684             Insert_Actions (N, New_List (
1685
1686               Make_Object_Declaration (Loc,
1687                 Defining_Identifier => Result_Ent,
1688                 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1689
1690               Make_Procedure_Call_Statement (Loc,
1691                 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1692                   Parameter_Associations => New_List (
1693
1694                     Make_Attribute_Reference (Loc,
1695                       Attribute_Name => Name_Address,
1696                       Prefix => L),
1697
1698                     Make_Op_Multiply (Loc,
1699                       Left_Opnd =>
1700                         Make_Attribute_Reference (Loc,
1701                           Prefix =>
1702                             New_Occurrence_Of
1703                               (Etype (First_Index (Ltyp)), Loc),
1704                           Attribute_Name => Name_Range_Length),
1705                       Right_Opnd =>
1706                         Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1707
1708                     Make_Attribute_Reference (Loc,
1709                       Attribute_Name => Name_Address,
1710                       Prefix => R),
1711
1712                     Make_Op_Multiply (Loc,
1713                       Left_Opnd =>
1714                         Make_Attribute_Reference (Loc,
1715                           Prefix =>
1716                             New_Occurrence_Of
1717                               (Etype (First_Index (Rtyp)), Loc),
1718                           Attribute_Name => Name_Range_Length),
1719                       Right_Opnd =>
1720                         Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1721
1722                     Make_Attribute_Reference (Loc,
1723                       Attribute_Name => Name_Address,
1724                       Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1725
1726             Rewrite (N,
1727               New_Occurrence_Of (Result_Ent, Loc));
1728          end;
1729       end if;
1730
1731       Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1732    end Expand_Packed_Boolean_Operator;
1733
1734    -------------------------------------
1735    -- Expand_Packed_Element_Reference --
1736    -------------------------------------
1737
1738    procedure Expand_Packed_Element_Reference (N : Node_Id) is
1739       Loc   : constant Source_Ptr := Sloc (N);
1740       Obj   : Node_Id;
1741       Atyp  : Entity_Id;
1742       PAT   : Entity_Id;
1743       Ctyp  : Entity_Id;
1744       Csiz  : Int;
1745       Shift : Node_Id;
1746       Cmask : Uint;
1747       Lit   : Node_Id;
1748       Arg   : Node_Id;
1749
1750    begin
1751       --  If not bit packed, we have the enumeration case, which is easily
1752       --  dealt with (just adjust the subscripts of the indexed component)
1753
1754       --  Note: this leaves the result as an indexed component, which is
1755       --  still a variable, so can be used in the assignment case, as is
1756       --  required in the enumeration case.
1757
1758       if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1759          Setup_Enumeration_Packed_Array_Reference (N);
1760          return;
1761       end if;
1762
1763       --  Remaining processing is for the bit-packed case.
1764
1765       Obj := Relocate_Node (Prefix (N));
1766       Convert_To_Actual_Subtype (Obj);
1767       Atyp := Etype (Obj);
1768       PAT  := Packed_Array_Type (Atyp);
1769       Ctyp := Component_Type (Atyp);
1770       Csiz := UI_To_Int (Component_Size (Atyp));
1771
1772       --  Case of component size 1,2,4 or any component size for the modular
1773       --  case. These are the cases for which we can inline the code.
1774
1775       if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1776         or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1777       then
1778          Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1779          Lit := Make_Integer_Literal (Loc, Cmask);
1780          Set_Print_In_Hex (Lit);
1781
1782          --  We generate a shift right to position the field, followed by a
1783          --  masking operation to extract the bit field, and we finally do an
1784          --  unchecked conversion to convert the result to the required target.
1785
1786          --  Note that the unchecked conversion automatically deals with the
1787          --  bias if we are dealing with a biased representation. What will
1788          --  happen is that we temporarily generate the biased representation,
1789          --  but almost immediately that will be converted to the original
1790          --  unbiased component type, and the bias will disappear.
1791
1792          Arg :=
1793            Make_Op_And (Loc,
1794              Left_Opnd  => Make_Shift_Right (Obj, Shift),
1795              Right_Opnd => Lit);
1796
1797          Analyze_And_Resolve (Arg);
1798
1799          Rewrite (N,
1800            RJ_Unchecked_Convert_To (Ctyp, Arg));
1801
1802       --  All other component sizes for non-modular case
1803
1804       else
1805          --  We generate
1806
1807          --    Component_Type!(Get_nn (Arr'address, Subscr))
1808
1809          --  where Subscr is the computed linear subscript.
1810
1811          declare
1812             Get_nn : Entity_Id;
1813             Subscr : Node_Id;
1814
1815          begin
1816             --  Acquire proper Get entity. We use the aligned or unaligned
1817             --  case as appropriate.
1818
1819             if Must_Be_Aligned (Obj) then
1820                Get_nn := RTE (Get_Id (Csiz));
1821             else
1822                Get_nn := RTE (GetU_Id (Csiz));
1823             end if;
1824
1825             --  Now generate the get reference
1826
1827             Compute_Linear_Subscript (Atyp, N, Subscr);
1828
1829             Rewrite (N,
1830               Unchecked_Convert_To (Ctyp,
1831                 Make_Function_Call (Loc,
1832                   Name => New_Occurrence_Of (Get_nn, Loc),
1833                   Parameter_Associations => New_List (
1834                     Make_Attribute_Reference (Loc,
1835                       Attribute_Name => Name_Address,
1836                       Prefix => Obj),
1837                     Subscr))));
1838          end;
1839       end if;
1840
1841       Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1842
1843    end Expand_Packed_Element_Reference;
1844
1845    ----------------------
1846    -- Expand_Packed_Eq --
1847    ----------------------
1848
1849    --  Handles expansion of "=" on packed array types
1850
1851    procedure Expand_Packed_Eq (N : Node_Id) is
1852       Loc : constant Source_Ptr := Sloc (N);
1853       L   : constant Node_Id    := Relocate_Node (Left_Opnd  (N));
1854       R   : constant Node_Id    := Relocate_Node (Right_Opnd (N));
1855
1856       LLexpr : Node_Id;
1857       RLexpr : Node_Id;
1858
1859       Ltyp : Entity_Id;
1860       Rtyp : Entity_Id;
1861       PAT  : Entity_Id;
1862
1863    begin
1864       Convert_To_Actual_Subtype (L);
1865       Convert_To_Actual_Subtype (R);
1866       Ltyp := Underlying_Type (Etype (L));
1867       Rtyp := Underlying_Type (Etype (R));
1868
1869       Convert_To_PAT_Type (L);
1870       Convert_To_PAT_Type (R);
1871       PAT := Etype (L);
1872
1873       LLexpr :=
1874         Make_Op_Multiply (Loc,
1875           Left_Opnd =>
1876             Make_Attribute_Reference (Loc,
1877               Attribute_Name => Name_Length,
1878               Prefix => New_Occurrence_Of (Ltyp, Loc)),
1879           Right_Opnd =>
1880             Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1881
1882       RLexpr :=
1883         Make_Op_Multiply (Loc,
1884           Left_Opnd =>
1885             Make_Attribute_Reference (Loc,
1886               Attribute_Name => Name_Length,
1887               Prefix => New_Occurrence_Of (Rtyp, Loc)),
1888           Right_Opnd =>
1889             Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1890
1891       --  For the modular case, we transform the comparison to:
1892
1893       --    Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1894
1895       --  where PAT is the packed array type. This works fine, since in the
1896       --  modular case we guarantee that the unused bits are always zeroes.
1897       --  We do have to compare the lengths because we could be comparing
1898       --  two different subtypes of the same base type.
1899
1900       if Is_Modular_Integer_Type (PAT) then
1901          Rewrite (N,
1902            Make_And_Then (Loc,
1903              Left_Opnd =>
1904                Make_Op_Eq (Loc,
1905                  Left_Opnd  => LLexpr,
1906                  Right_Opnd => RLexpr),
1907
1908              Right_Opnd =>
1909                Make_Op_Eq (Loc,
1910                  Left_Opnd => L,
1911                  Right_Opnd => R)));
1912
1913       --  For the non-modular case, we call a runtime routine
1914
1915       --    System.Bit_Ops.Bit_Eq
1916       --      (L'Address, L_Length, R'Address, R_Length)
1917
1918       --  where PAT is the packed array type, and the lengths are the lengths
1919       --  in bits of the original packed arrays. This routine takes care of
1920       --  not comparing the unused bits in the last byte.
1921
1922       else
1923          Rewrite (N,
1924            Make_Function_Call (Loc,
1925              Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1926              Parameter_Associations => New_List (
1927                Make_Attribute_Reference (Loc,
1928                  Attribute_Name => Name_Address,
1929                  Prefix => L),
1930
1931                LLexpr,
1932
1933                Make_Attribute_Reference (Loc,
1934                  Attribute_Name => Name_Address,
1935                  Prefix => R),
1936
1937                RLexpr)));
1938       end if;
1939
1940       Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
1941    end Expand_Packed_Eq;
1942
1943    -----------------------
1944    -- Expand_Packed_Not --
1945    -----------------------
1946
1947    --  Handles expansion of "not" on packed array types
1948
1949    procedure Expand_Packed_Not (N : Node_Id) is
1950       Loc  : constant Source_Ptr := Sloc (N);
1951       Typ  : constant Entity_Id  := Etype (N);
1952       Opnd : constant Node_Id    := Relocate_Node (Right_Opnd (N));
1953
1954       Rtyp : Entity_Id;
1955       PAT  : Entity_Id;
1956       Lit  : Node_Id;
1957
1958    begin
1959       Convert_To_Actual_Subtype (Opnd);
1960       Rtyp := Etype (Opnd);
1961
1962       --  First an odd and silly test. We explicitly check for the case
1963       --  where the 'First of the component type is equal to the 'Last of
1964       --  this component type, and if this is the case, we make sure that
1965       --  constraint error is raised. The reason is that the NOT is bound
1966       --  to cause CE in this case, and we will not otherwise catch it.
1967
1968       --  Believe it or not, this was reported as a bug. Note that nearly
1969       --  always, the test will evaluate statically to False, so the code
1970       --  will be statically removed, and no extra overhead caused.
1971
1972       declare
1973          CT : constant Entity_Id := Component_Type (Rtyp);
1974
1975       begin
1976          Insert_Action (N,
1977            Make_Raise_Constraint_Error (Loc,
1978              Condition =>
1979                Make_Op_Eq (Loc,
1980                  Left_Opnd =>
1981                    Make_Attribute_Reference (Loc,
1982                      Prefix         => New_Occurrence_Of (CT, Loc),
1983                      Attribute_Name => Name_First),
1984
1985                  Right_Opnd =>
1986                    Make_Attribute_Reference (Loc,
1987                      Prefix         => New_Occurrence_Of (CT, Loc),
1988                      Attribute_Name => Name_Last))));
1989       end;
1990
1991       --  Now that that silliness is taken care of, get packed array type
1992
1993       Convert_To_PAT_Type (Opnd);
1994       PAT := Etype (Opnd);
1995
1996       --  For the case where the packed array type is a modular type,
1997       --  not A expands simply into:
1998
1999       --     rtyp!(PAT!(A) xor mask)
2000
2001       --  where PAT is the packed array type, and mask is a mask of all
2002       --  one bits of length equal to the size of this packed type and
2003       --  rtyp is the actual subtype of the operand
2004
2005       Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2006       Set_Print_In_Hex (Lit);
2007
2008       if not Is_Array_Type (PAT) then
2009          Rewrite (N,
2010            Unchecked_Convert_To (Rtyp,
2011              Make_Op_Xor (Loc,
2012                Left_Opnd  => Opnd,
2013                Right_Opnd => Lit)));
2014
2015       --  For the array case, we insert the actions
2016
2017       --    Result : Typ;
2018
2019       --    System.Bitops.Bit_Not
2020       --     (Opnd'Address,
2021       --      Typ'Length * Typ'Component_Size;
2022       --      Result'Address);
2023
2024       --  where Opnd is the Packed_Bytes{1,2,4} operand and the second
2025       --  argument is the length of the operand in bits. Then we replace
2026       --  the expression by a reference to Result.
2027
2028       else
2029          declare
2030             Result_Ent : constant Entity_Id :=
2031                            Make_Defining_Identifier (Loc,
2032                              Chars => New_Internal_Name ('T'));
2033
2034          begin
2035             Insert_Actions (N, New_List (
2036
2037               Make_Object_Declaration (Loc,
2038                 Defining_Identifier => Result_Ent,
2039                 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2040
2041               Make_Procedure_Call_Statement (Loc,
2042                 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2043                   Parameter_Associations => New_List (
2044
2045                     Make_Attribute_Reference (Loc,
2046                       Attribute_Name => Name_Address,
2047                       Prefix => Opnd),
2048
2049                     Make_Op_Multiply (Loc,
2050                       Left_Opnd =>
2051                         Make_Attribute_Reference (Loc,
2052                           Prefix =>
2053                             New_Occurrence_Of
2054                               (Etype (First_Index (Rtyp)), Loc),
2055                           Attribute_Name => Name_Range_Length),
2056                       Right_Opnd =>
2057                         Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2058
2059                     Make_Attribute_Reference (Loc,
2060                       Attribute_Name => Name_Address,
2061                       Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2062
2063             Rewrite (N,
2064               New_Occurrence_Of (Result_Ent, Loc));
2065          end;
2066       end if;
2067
2068       Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2069
2070    end Expand_Packed_Not;
2071
2072    -------------------------------------
2073    -- Involves_Packed_Array_Reference --
2074    -------------------------------------
2075
2076    function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2077    begin
2078       if Nkind (N) = N_Indexed_Component
2079         and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2080       then
2081          return True;
2082
2083       elsif Nkind (N) = N_Selected_Component then
2084          return Involves_Packed_Array_Reference (Prefix (N));
2085
2086       else
2087          return False;
2088       end if;
2089    end Involves_Packed_Array_Reference;
2090
2091    ---------------------
2092    -- Make_Shift_Left --
2093    ---------------------
2094
2095    function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2096       Nod : Node_Id;
2097
2098    begin
2099       if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2100          return N;
2101       else
2102          Nod :=
2103            Make_Op_Shift_Left (Sloc (N),
2104              Left_Opnd  => N,
2105              Right_Opnd => S);
2106          Set_Shift_Count_OK (Nod, True);
2107          return Nod;
2108       end if;
2109    end Make_Shift_Left;
2110
2111    ----------------------
2112    -- Make_Shift_Right --
2113    ----------------------
2114
2115    function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2116       Nod : Node_Id;
2117
2118    begin
2119       if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2120          return N;
2121       else
2122          Nod :=
2123            Make_Op_Shift_Right (Sloc (N),
2124              Left_Opnd  => N,
2125              Right_Opnd => S);
2126          Set_Shift_Count_OK (Nod, True);
2127          return Nod;
2128       end if;
2129    end Make_Shift_Right;
2130
2131    -----------------------------
2132    -- RJ_Unchecked_Convert_To --
2133    -----------------------------
2134
2135    function RJ_Unchecked_Convert_To
2136      (Typ  : Entity_Id;
2137       Expr : Node_Id)
2138       return Node_Id
2139    is
2140       Source_Typ : constant Entity_Id := Etype (Expr);
2141       Target_Typ : constant Entity_Id := Typ;
2142
2143       Src : Node_Id := Expr;
2144
2145       Source_Siz : Nat;
2146       Target_Siz : Nat;
2147
2148    begin
2149       Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2150       Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2151
2152       --  In the big endian case, if the lengths of the two types differ,
2153       --  then we must worry about possible left justification in the
2154       --  conversion, and avoiding that is what this is all about.
2155
2156       if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2157
2158          --  First step, if the source type is not a discrete type, then we
2159          --  first convert to a modular type of the source length, since
2160          --  otherwise, on a big-endian machine, we get left-justification.
2161
2162          if not Is_Discrete_Type (Source_Typ) then
2163             Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2164          end if;
2165
2166          --  Next step. If the target is not a discrete type, then we first
2167          --  convert to a modular type of the target length, since
2168          --  otherwise, on a big-endian machine, we get left-justification.
2169
2170          if not Is_Discrete_Type (Target_Typ) then
2171             Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2172          end if;
2173       end if;
2174
2175       --  And now we can do the final conversion to the target type
2176
2177       return Unchecked_Convert_To (Target_Typ, Src);
2178    end RJ_Unchecked_Convert_To;
2179
2180    ----------------------------------------------
2181    -- Setup_Enumeration_Packed_Array_Reference --
2182    ----------------------------------------------
2183
2184    --  All we have to do here is to find the subscripts that correspond
2185    --  to the index positions that have non-standard enumeration types
2186    --  and insert a Pos attribute to get the proper subscript value.
2187    --  Finally the prefix must be uncheck converted to the corresponding
2188    --  packed array type.
2189
2190    --  Note that the component type is unchanged, so we do not need to
2191    --  fiddle with the types (Gigi always automatically takes the packed
2192    --  array type if it is set, as it will be in this case).
2193
2194    procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2195       Pfx   : constant Node_Id   := Prefix (N);
2196       Typ   : constant Entity_Id := Etype (N);
2197       Exprs : constant List_Id   := Expressions (N);
2198       Expr  : Node_Id;
2199
2200    begin
2201       --  If the array is unconstrained, then we replace the array
2202       --  reference with its actual subtype. This actual subtype will
2203       --  have a packed array type with appropriate bounds.
2204
2205       if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2206          Convert_To_Actual_Subtype (Pfx);
2207       end if;
2208
2209       Expr := First (Exprs);
2210       while Present (Expr) loop
2211          declare
2212             Loc      : constant Source_Ptr := Sloc (Expr);
2213             Expr_Typ : constant Entity_Id := Etype (Expr);
2214
2215          begin
2216             if Is_Enumeration_Type (Expr_Typ)
2217               and then Has_Non_Standard_Rep (Expr_Typ)
2218             then
2219                Rewrite (Expr,
2220                  Make_Attribute_Reference (Loc,
2221                    Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2222                    Attribute_Name => Name_Pos,
2223                    Expressions    => New_List (Relocate_Node (Expr))));
2224                Analyze_And_Resolve (Expr, Standard_Natural);
2225             end if;
2226          end;
2227
2228          Next (Expr);
2229       end loop;
2230
2231       Rewrite (N,
2232         Make_Indexed_Component (Sloc (N),
2233           Prefix      =>
2234             Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2235           Expressions => Exprs));
2236
2237       Analyze_And_Resolve (N, Typ);
2238
2239    end Setup_Enumeration_Packed_Array_Reference;
2240
2241    -----------------------------------------
2242    -- Setup_Inline_Packed_Array_Reference --
2243    -----------------------------------------
2244
2245    procedure Setup_Inline_Packed_Array_Reference
2246      (N      : Node_Id;
2247       Atyp   : Entity_Id;
2248       Obj    : in out Node_Id;
2249       Cmask  : out Uint;
2250       Shift  : out Node_Id)
2251    is
2252       Loc    : constant Source_Ptr := Sloc (N);
2253       Ctyp   : Entity_Id;
2254       PAT    : Entity_Id;
2255       Otyp   : Entity_Id;
2256       Csiz   : Uint;
2257       Osiz   : Uint;
2258
2259    begin
2260       Ctyp := Component_Type (Atyp);
2261       Csiz := Component_Size (Atyp);
2262
2263       Convert_To_PAT_Type (Obj);
2264       PAT  := Etype (Obj);
2265
2266       Cmask := 2 ** Csiz - 1;
2267
2268       if Is_Array_Type (PAT) then
2269          Otyp := Component_Type (PAT);
2270          Osiz := Esize (Otyp);
2271
2272       else
2273          Otyp := PAT;
2274
2275          --  In the case where the PAT is a modular type, we want the actual
2276          --  size in bits of the modular value we use. This is neither the
2277          --  Object_Size nor the Value_Size, either of which may have been
2278          --  reset to strange values, but rather the minimum size. Note that
2279          --  since this is a modular type with full range, the issue of
2280          --  biased representation does not arise.
2281
2282          Osiz := UI_From_Int (Minimum_Size (Otyp));
2283       end if;
2284
2285       Compute_Linear_Subscript (Atyp, N, Shift);
2286
2287       --  If the component size is not 1, then the subscript must be
2288       --  multiplied by the component size to get the shift count.
2289
2290       if Csiz /= 1 then
2291          Shift :=
2292            Make_Op_Multiply (Loc,
2293              Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2294              Right_Opnd => Shift);
2295       end if;
2296
2297       --  If we have the array case, then this shift count must be broken
2298       --  down into a byte subscript, and a shift within the byte.
2299
2300       if Is_Array_Type (PAT) then
2301
2302          declare
2303             New_Shift : Node_Id;
2304
2305          begin
2306             --  We must analyze shift, since we will duplicate it
2307
2308             Set_Parent (Shift, N);
2309             Analyze_And_Resolve
2310               (Shift, Standard_Integer, Suppress => All_Checks);
2311
2312             --  The shift count within the word is
2313             --    shift mod Osiz
2314
2315             New_Shift :=
2316               Make_Op_Mod (Loc,
2317                 Left_Opnd  => Duplicate_Subexpr (Shift),
2318                 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2319
2320             --  The subscript to be used on the PAT array is
2321             --    shift / Osiz
2322
2323             Obj :=
2324               Make_Indexed_Component (Loc,
2325                 Prefix => Obj,
2326                 Expressions => New_List (
2327                   Make_Op_Divide (Loc,
2328                     Left_Opnd => Duplicate_Subexpr (Shift),
2329                     Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2330
2331             Shift := New_Shift;
2332          end;
2333
2334       --  For the modular integer case, the object to be manipulated is
2335       --  the entire array, so Obj is unchanged. Note that we will reset
2336       --  its type to PAT before returning to the caller.
2337
2338       else
2339          null;
2340       end if;
2341
2342       --  The one remaining step is to modify the shift count for the
2343       --  big-endian case. Consider the following example in a byte:
2344
2345       --     xxxxxxxx  bits of byte
2346       --     vvvvvvvv  bits of value
2347       --     33221100  little-endian numbering
2348       --     00112233  big-endian numbering
2349
2350       --  Here we have the case of 2-bit fields
2351
2352       --  For the little-endian case, we already have the proper shift
2353       --  count set, e.g. for element 2, the shift count is 2*2 = 4.
2354
2355       --  For the big endian case, we have to adjust the shift count,
2356       --  computing it as (N - F) - shift, where N is the number of bits
2357       --  in an element of the array used to implement the packed array,
2358       --  F is the number of bits in a source level array element, and
2359       --  shift is the count so far computed.
2360
2361       if Bytes_Big_Endian then
2362          Shift :=
2363            Make_Op_Subtract (Loc,
2364              Left_Opnd  => Make_Integer_Literal (Loc, Osiz - Csiz),
2365              Right_Opnd => Shift);
2366       end if;
2367
2368       Set_Parent (Shift, N);
2369       Set_Parent (Obj, N);
2370       Analyze_And_Resolve (Obj,   Otyp,             Suppress => All_Checks);
2371       Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2372
2373       --  Make sure final type of object is the appropriate packed type
2374
2375       Set_Etype (Obj, Otyp);
2376
2377    end Setup_Inline_Packed_Array_Reference;
2378
2379 end Exp_Pakd;