OSDN Git Service

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