OSDN Git Service

* ug_words, gnat_ugn.texi: Added alpha-ivms transitioning section to
[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. If the ancestor is not an array type but
855       --  a private type, as can happen with multiple instantiations, create
856       --  a new packed type, to avoid privacy issues.
857
858       if Ekind (Typ) = E_Array_Subtype then
859          Ancest := Ancestor_Subtype (Typ);
860
861          if Present (Ancest)
862            and then Is_Array_Type (Ancest)
863            and then Is_Constrained (Ancest)
864            and then Present (Packed_Array_Type (Ancest))
865          then
866             Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
867             return;
868          end if;
869       end if;
870
871       --  We preset the result type size from the size of the original array
872       --  type, since this size clearly belongs to the packed array type. The
873       --  size of the conceptual unpacked type is always set to unknown.
874
875       PASize := Esize (Typ);
876
877       --  Case of an array where at least one index is of an enumeration
878       --  type with a non-standard representation, but the component size
879       --  is not appropriate for bit packing. This is the case where we
880       --  have Is_Packed set (we would never be in this unit otherwise),
881       --  but Is_Bit_Packed_Array is false.
882
883       --  Note that if the component size is appropriate for bit packing,
884       --  then the circuit for the computation of the subscript properly
885       --  deals with the non-standard enumeration type case by taking the
886       --  Pos anyway.
887
888       if not Is_Bit_Packed_Array (Typ) then
889
890          --  Here we build a declaration:
891
892          --    type tttP is array (index1, index2, ...) of component_type
893
894          --  where index1, index2, are the index types. These are the same
895          --  as the index types of the original array, except for the non-
896          --  standard representation enumeration type case, where we have
897          --  two subcases.
898
899          --  For the unconstrained array case, we use
900
901          --    Natural range <>
902
903          --  For the constrained case, we use
904
905          --    Natural range Enum_Type'Pos (Enum_Type'First) ..
906          --                  Enum_Type'Pos (Enum_Type'Last);
907
908          PAT :=
909            Make_Defining_Identifier (Loc,
910              Chars => New_External_Name (Chars (Typ), 'P'));
911
912          Set_Packed_Array_Type (Typ, PAT);
913
914          declare
915             Indexes   : constant List_Id := New_List;
916             Indx      : Node_Id;
917             Indx_Typ  : Entity_Id;
918             Enum_Case : Boolean;
919             Typedef   : Node_Id;
920
921          begin
922             Indx := First_Index (Typ);
923
924             while Present (Indx) loop
925                Indx_Typ := Etype (Indx);
926
927                Enum_Case := Is_Enumeration_Type (Indx_Typ)
928                               and then Has_Non_Standard_Rep (Indx_Typ);
929
930                --  Unconstrained case
931
932                if not Is_Constrained (Typ) then
933                   if Enum_Case then
934                      Indx_Typ := Standard_Natural;
935                   end if;
936
937                   Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
938
939                --  Constrained case
940
941                else
942                   if not Enum_Case then
943                      Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
944
945                   else
946                      Append_To (Indexes,
947                        Make_Subtype_Indication (Loc,
948                          Subtype_Mark =>
949                            New_Occurrence_Of (Standard_Natural, Loc),
950                          Constraint =>
951                            Make_Range_Constraint (Loc,
952                              Range_Expression =>
953                                Make_Range (Loc,
954                                  Low_Bound =>
955                                    Make_Attribute_Reference (Loc,
956                                      Prefix =>
957                                        New_Occurrence_Of (Indx_Typ, Loc),
958                                      Attribute_Name => Name_Pos,
959                                      Expressions => New_List (
960                                        Make_Attribute_Reference (Loc,
961                                          Prefix =>
962                                            New_Occurrence_Of (Indx_Typ, Loc),
963                                          Attribute_Name => Name_First))),
964
965                                  High_Bound =>
966                                    Make_Attribute_Reference (Loc,
967                                      Prefix =>
968                                        New_Occurrence_Of (Indx_Typ, Loc),
969                                      Attribute_Name => Name_Pos,
970                                      Expressions => New_List (
971                                        Make_Attribute_Reference (Loc,
972                                          Prefix =>
973                                            New_Occurrence_Of (Indx_Typ, Loc),
974                                          Attribute_Name => Name_Last)))))));
975
976                   end if;
977                end if;
978
979                Next_Index (Indx);
980             end loop;
981
982             if not Is_Constrained (Typ) then
983                Typedef :=
984                  Make_Unconstrained_Array_Definition (Loc,
985                    Subtype_Marks => Indexes,
986                    Component_Definition =>
987                      Make_Component_Definition (Loc,
988                        Aliased_Present    => False,
989                        Subtype_Indication =>
990                           New_Occurrence_Of (Ctyp, Loc)));
991
992             else
993                Typedef :=
994                   Make_Constrained_Array_Definition (Loc,
995                     Discrete_Subtype_Definitions => Indexes,
996                     Component_Definition =>
997                       Make_Component_Definition (Loc,
998                         Aliased_Present    => False,
999                         Subtype_Indication =>
1000                           New_Occurrence_Of (Ctyp, Loc)));
1001             end if;
1002
1003             Decl :=
1004               Make_Full_Type_Declaration (Loc,
1005                 Defining_Identifier => PAT,
1006                 Type_Definition => Typedef);
1007          end;
1008
1009          --  Set type as packed array type and install it
1010
1011          Set_Is_Packed_Array_Type (PAT);
1012          Install_PAT;
1013          return;
1014
1015       --  Case of bit-packing required for unconstrained array. We create
1016       --  a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1017
1018       elsif not Is_Constrained (Typ) then
1019          PAT :=
1020            Make_Defining_Identifier (Loc,
1021              Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1022
1023          Set_Packed_Array_Type (Typ, PAT);
1024          Set_PB_Type;
1025
1026          Decl :=
1027            Make_Subtype_Declaration (Loc,
1028              Defining_Identifier => PAT,
1029                Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1030          Install_PAT;
1031          return;
1032
1033       --  Remaining code is for the case of bit-packing for constrained array
1034
1035       --  The name of the packed array subtype is
1036
1037       --    ttt___Xsss
1038
1039       --  where sss is the component size in bits and ttt is the name of
1040       --  the parent packed type.
1041
1042       else
1043          PAT :=
1044            Make_Defining_Identifier (Loc,
1045              Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1046
1047          Set_Packed_Array_Type (Typ, PAT);
1048
1049          --  Build an expression for the length of the array in bits.
1050          --  This is the product of the length of each of the dimensions
1051
1052          declare
1053             J : Nat := 1;
1054
1055          begin
1056             Len_Expr := Empty; -- suppress junk warning
1057
1058             loop
1059                Len_Dim :=
1060                  Make_Attribute_Reference (Loc,
1061                    Attribute_Name => Name_Length,
1062                    Prefix         => New_Occurrence_Of (Typ, Loc),
1063                    Expressions    => New_List (
1064                      Make_Integer_Literal (Loc, J)));
1065
1066                if J = 1 then
1067                   Len_Expr := Len_Dim;
1068
1069                else
1070                   Len_Expr :=
1071                     Make_Op_Multiply (Loc,
1072                       Left_Opnd  => Len_Expr,
1073                       Right_Opnd => Len_Dim);
1074                end if;
1075
1076                J := J + 1;
1077                exit when J > Number_Dimensions (Typ);
1078             end loop;
1079          end;
1080
1081          --  Temporarily attach the length expression to the tree and analyze
1082          --  and resolve it, so that we can test its value. We assume that the
1083          --  total length fits in type Integer. This expression may involve
1084          --  discriminants, so we treat it as a default/per-object expression.
1085
1086          Set_Parent (Len_Expr, Typ);
1087          Analyze_Per_Use_Expression (Len_Expr, Standard_Integer);
1088
1089          --  Use a modular type if possible. We can do this if we have
1090          --  static bounds, and the length is small enough, and the length
1091          --  is not zero. We exclude the zero length case because the size
1092          --  of things is always at least one, and the zero length object
1093          --  would have an anomalous size.
1094
1095          if Compile_Time_Known_Value (Len_Expr) then
1096             Len_Bits := Expr_Value (Len_Expr) * Csize;
1097
1098             --  We normally consider small enough to mean no larger than the
1099             --  value of System_Max_Binary_Modulus_Power, checking that in the
1100             --  case of values longer than word size, we have long shifts.
1101
1102             if Len_Bits > 0
1103               and then
1104                 (Len_Bits <= System_Word_Size
1105                    or else (Len_Bits <= System_Max_Binary_Modulus_Power
1106                               and then Support_Long_Shifts_On_Target))
1107
1108             --  Also test for alignment given. If an alignment is given which
1109             --  is smaller than the natural modular alignment, force the array
1110             --  of bytes representation to accommodate the alignment.
1111
1112               and then
1113                 (No (Alignment_Clause (Typ))
1114                    or else
1115                  Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1116                                              / System_Storage_Unit))
1117             then
1118                --  We can use the modular type, it has the form:
1119
1120                --    subtype tttPn is btyp
1121                --      range 0 .. 2 ** ((Typ'Length (1)
1122                --                * ... * Typ'Length (n)) * Csize) - 1;
1123
1124                --  The bounds are statically known, and btyp is one
1125                --  of the unsigned types, depending on the length. If the
1126                --  type is its first subtype, i.e. it is a user-defined
1127                --  type, no object of the type will be larger, and it is
1128                --  worthwhile to use a small unsigned type.
1129
1130                if Len_Bits <= Standard_Short_Integer_Size
1131                  and then First_Subtype (Typ) = Typ
1132                then
1133                   Btyp := RTE (RE_Short_Unsigned);
1134
1135                elsif Len_Bits <= Standard_Integer_Size then
1136                   Btyp := RTE (RE_Unsigned);
1137
1138                elsif Len_Bits <= Standard_Long_Integer_Size then
1139                   Btyp := RTE (RE_Long_Unsigned);
1140
1141                else
1142                   Btyp := RTE (RE_Long_Long_Unsigned);
1143                end if;
1144
1145                Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1146                Set_Print_In_Hex (Lit);
1147
1148                Decl :=
1149                  Make_Subtype_Declaration (Loc,
1150                    Defining_Identifier => PAT,
1151                      Subtype_Indication =>
1152                        Make_Subtype_Indication (Loc,
1153                          Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1154
1155                          Constraint =>
1156                            Make_Range_Constraint (Loc,
1157                              Range_Expression =>
1158                                Make_Range (Loc,
1159                                  Low_Bound =>
1160                                    Make_Integer_Literal (Loc, 0),
1161                                  High_Bound => Lit))));
1162
1163                if PASize = Uint_0 then
1164                   PASize := Len_Bits;
1165                end if;
1166
1167                Install_PAT;
1168                return;
1169             end if;
1170          end if;
1171
1172          --  Could not use a modular type, for all other cases, we build
1173          --  a packed array subtype:
1174
1175          --    subtype tttPn is
1176          --      System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1177
1178          --  Bits is the length of the array in bits
1179
1180          Set_PB_Type;
1181
1182          Bits_U1 :=
1183            Make_Op_Add (Loc,
1184              Left_Opnd =>
1185                Make_Op_Multiply (Loc,
1186                  Left_Opnd  =>
1187                    Make_Integer_Literal (Loc, Csize),
1188                  Right_Opnd => Len_Expr),
1189
1190              Right_Opnd =>
1191                Make_Integer_Literal (Loc, 7));
1192
1193          Set_Paren_Count (Bits_U1, 1);
1194
1195          PAT_High :=
1196            Make_Op_Subtract (Loc,
1197              Left_Opnd =>
1198                Make_Op_Divide (Loc,
1199                  Left_Opnd => Bits_U1,
1200                  Right_Opnd => Make_Integer_Literal (Loc, 8)),
1201              Right_Opnd => Make_Integer_Literal (Loc, 1));
1202
1203          Decl :=
1204            Make_Subtype_Declaration (Loc,
1205              Defining_Identifier => PAT,
1206                Subtype_Indication =>
1207                  Make_Subtype_Indication (Loc,
1208                    Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1209                    Constraint =>
1210
1211                      Make_Index_Or_Discriminant_Constraint (Loc,
1212                        Constraints => New_List (
1213                          Make_Range (Loc,
1214                            Low_Bound =>
1215                              Make_Integer_Literal (Loc, 0),
1216                            High_Bound => PAT_High)))));
1217
1218          Install_PAT;
1219
1220          --  Currently the code in this unit requires that packed arrays
1221          --  represented by non-modular arrays of bytes be on a byte
1222          --  boundary.
1223
1224          Set_Must_Be_On_Byte_Boundary (Typ);
1225       end if;
1226    end Create_Packed_Array_Type;
1227
1228    -----------------------------------
1229    -- Expand_Bit_Packed_Element_Set --
1230    -----------------------------------
1231
1232    procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1233       Loc : constant Source_Ptr := Sloc (N);
1234       Lhs : constant Node_Id    := Name (N);
1235
1236       Ass_OK : constant Boolean := Assignment_OK (Lhs);
1237       --  Used to preserve assignment OK status when assignment is rewritten
1238
1239       Rhs : Node_Id := Expression (N);
1240       --  Initially Rhs is the right hand side value, it will be replaced
1241       --  later by an appropriate unchecked conversion for the assignment.
1242
1243       Obj    : Node_Id;
1244       Atyp   : Entity_Id;
1245       PAT    : Entity_Id;
1246       Ctyp   : Entity_Id;
1247       Csiz   : Int;
1248       Cmask  : Uint;
1249
1250       Shift : Node_Id;
1251       --  The expression for the shift value that is required
1252
1253       Shift_Used : Boolean := False;
1254       --  Set True if Shift has been used in the generated code at least
1255       --  once, so that it must be duplicated if used again
1256
1257       New_Lhs : Node_Id;
1258       New_Rhs : Node_Id;
1259
1260       Rhs_Val_Known : Boolean;
1261       Rhs_Val       : Uint;
1262       --  If the value of the right hand side as an integer constant is
1263       --  known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1264       --  contains the value. Otherwise Rhs_Val_Known is set False, and
1265       --  the Rhs_Val is undefined.
1266
1267       function Get_Shift return Node_Id;
1268       --  Function used to get the value of Shift, making sure that it
1269       --  gets duplicated if the function is called more than once.
1270
1271       ---------------
1272       -- Get_Shift --
1273       ---------------
1274
1275       function Get_Shift return Node_Id is
1276       begin
1277          --  If we used the shift value already, then duplicate it. We
1278          --  set a temporary parent in case actions have to be inserted.
1279
1280          if Shift_Used then
1281             Set_Parent (Shift, N);
1282             return Duplicate_Subexpr_No_Checks (Shift);
1283
1284          --  If first time, use Shift unchanged, and set flag for first use
1285
1286          else
1287             Shift_Used := True;
1288             return Shift;
1289          end if;
1290       end Get_Shift;
1291
1292    --  Start of processing for Expand_Bit_Packed_Element_Set
1293
1294    begin
1295       pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1296
1297       Obj := Relocate_Node (Prefix (Lhs));
1298       Convert_To_Actual_Subtype (Obj);
1299       Atyp := Etype (Obj);
1300       PAT  := Packed_Array_Type (Atyp);
1301       Ctyp := Component_Type (Atyp);
1302       Csiz := UI_To_Int (Component_Size (Atyp));
1303
1304       --  We convert the right hand side to the proper subtype to ensure
1305       --  that an appropriate range check is made (since the normal range
1306       --  check from assignment will be lost in the transformations). This
1307       --  conversion is analyzed immediately so that subsequent processing
1308       --  can work with an analyzed Rhs (and e.g. look at its Etype)
1309
1310       --  If the right-hand side is a string literal, create a temporary for
1311       --  it, constant-folding is not ready to wrap the bit representation
1312       --  of a string literal.
1313
1314       if Nkind (Rhs) = N_String_Literal then
1315          declare
1316             Decl : Node_Id;
1317          begin
1318             Decl :=
1319               Make_Object_Declaration (Loc,
1320                 Defining_Identifier =>
1321                   Make_Defining_Identifier (Loc,  New_Internal_Name ('T')),
1322                 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1323                 Expression => New_Copy_Tree (Rhs));
1324
1325             Insert_Actions (N, New_List (Decl));
1326             Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1327          end;
1328       end if;
1329
1330       Rhs := Convert_To (Ctyp, Rhs);
1331       Set_Parent (Rhs, N);
1332       Analyze_And_Resolve (Rhs, Ctyp);
1333
1334       --  Case of component size 1,2,4 or any component size for the modular
1335       --  case. These are the cases for which we can inline the code.
1336
1337       if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1338         or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1339       then
1340          Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1341
1342          --  The statement to be generated is:
1343
1344          --    Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1345
1346          --      where mask1 is obtained by shifting Cmask left Shift bits
1347          --      and then complementing the result.
1348
1349          --      the "and Mask1" is omitted if rhs is constant and all 1 bits
1350
1351          --      the "or ..." is omitted if rhs is constant and all 0 bits
1352
1353          --      rhs is converted to the appropriate type
1354
1355          --      The result is converted back to the array type, since
1356          --      otherwise we lose knowledge of the packed nature.
1357
1358          --  Determine if right side is all 0 bits or all 1 bits
1359
1360          if Compile_Time_Known_Value (Rhs) then
1361             Rhs_Val       := Expr_Rep_Value (Rhs);
1362             Rhs_Val_Known := True;
1363
1364          --  The following test catches the case of an unchecked conversion
1365          --  of an integer literal. This results from optimizing aggregates
1366          --  of packed types.
1367
1368          elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1369            and then Compile_Time_Known_Value (Expression (Rhs))
1370          then
1371             Rhs_Val       := Expr_Rep_Value (Expression (Rhs));
1372             Rhs_Val_Known := True;
1373
1374          else
1375             Rhs_Val       := No_Uint;
1376             Rhs_Val_Known := False;
1377          end if;
1378
1379          --  Some special checks for the case where the right hand value
1380          --  is known at compile time. Basically we have to take care of
1381          --  the implicit conversion to the subtype of the component object.
1382
1383          if Rhs_Val_Known then
1384
1385             --  If we have a biased component type then we must manually do
1386             --  the biasing, since we are taking responsibility in this case
1387             --  for constructing the exact bit pattern to be used.
1388
1389             if Has_Biased_Representation (Ctyp) then
1390                Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1391             end if;
1392
1393             --  For a negative value, we manually convert the twos complement
1394             --  value to a corresponding unsigned value, so that the proper
1395             --  field width is maintained. If we did not do this, we would
1396             --  get too many leading sign bits later on.
1397
1398             if Rhs_Val < 0 then
1399                Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1400             end if;
1401          end if;
1402
1403          New_Lhs := Duplicate_Subexpr (Obj, True);
1404          New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1405
1406          --  First we deal with the "and"
1407
1408          if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1409             declare
1410                Mask1 : Node_Id;
1411                Lit   : Node_Id;
1412
1413             begin
1414                if Compile_Time_Known_Value (Shift) then
1415                   Mask1 :=
1416                     Make_Integer_Literal (Loc,
1417                       Modulus (Etype (Obj)) - 1 -
1418                                  (Cmask * (2 ** Expr_Value (Get_Shift))));
1419                   Set_Print_In_Hex (Mask1);
1420
1421                else
1422                   Lit := Make_Integer_Literal (Loc, Cmask);
1423                   Set_Print_In_Hex (Lit);
1424                   Mask1 :=
1425                     Make_Op_Not (Loc,
1426                       Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1427                end if;
1428
1429                New_Rhs :=
1430                  Make_Op_And (Loc,
1431                    Left_Opnd  => New_Rhs,
1432                    Right_Opnd => Mask1);
1433             end;
1434          end if;
1435
1436          --  Then deal with the "or"
1437
1438          if not Rhs_Val_Known or else Rhs_Val /= 0 then
1439             declare
1440                Or_Rhs : Node_Id;
1441
1442                procedure Fixup_Rhs;
1443                --  Adjust Rhs by bias if biased representation for components
1444                --  or remove extraneous high order sign bits if signed.
1445
1446                procedure Fixup_Rhs is
1447                   Etyp : constant Entity_Id := Etype (Rhs);
1448
1449                begin
1450                   --  For biased case, do the required biasing by simply
1451                   --  converting to the biased subtype (the conversion
1452                   --  will generate the required bias).
1453
1454                   if Has_Biased_Representation (Ctyp) then
1455                      Rhs := Convert_To (Ctyp, Rhs);
1456
1457                   --  For a signed integer type that is not biased, generate
1458                   --  a conversion to unsigned to strip high order sign bits.
1459
1460                   elsif Is_Signed_Integer_Type (Ctyp) then
1461                      Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1462                   end if;
1463
1464                   --  Set Etype, since it can be referenced before the
1465                   --  node is completely analyzed.
1466
1467                   Set_Etype (Rhs, Etyp);
1468
1469                   --  We now need to do an unchecked conversion of the
1470                   --  result to the target type, but it is important that
1471                   --  this conversion be a right justified conversion and
1472                   --  not a left justified conversion.
1473
1474                   Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1475
1476                end Fixup_Rhs;
1477
1478             begin
1479                if Rhs_Val_Known
1480                  and then Compile_Time_Known_Value (Get_Shift)
1481                then
1482                   Or_Rhs :=
1483                     Make_Integer_Literal (Loc,
1484                       Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1485                   Set_Print_In_Hex (Or_Rhs);
1486
1487                else
1488                   --  We have to convert the right hand side to Etype (Obj).
1489                   --  A special case case arises if what we have now is a Val
1490                   --  attribute reference whose expression type is Etype (Obj).
1491                   --  This happens for assignments of fields from the same
1492                   --  array. In this case we get the required right hand side
1493                   --  by simply removing the inner attribute reference.
1494
1495                   if Nkind (Rhs) = N_Attribute_Reference
1496                     and then Attribute_Name (Rhs) = Name_Val
1497                     and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1498                   then
1499                      Rhs := Relocate_Node (First (Expressions (Rhs)));
1500                      Fixup_Rhs;
1501
1502                   --  If the value of the right hand side is a known integer
1503                   --  value, then just replace it by an untyped constant,
1504                   --  which will be properly retyped when we analyze and
1505                   --  resolve the expression.
1506
1507                   elsif Rhs_Val_Known then
1508
1509                      --  Note that Rhs_Val has already been normalized to
1510                      --  be an unsigned value with the proper number of bits.
1511
1512                      Rhs :=
1513                        Make_Integer_Literal (Loc, Rhs_Val);
1514
1515                   --  Otherwise we need an unchecked conversion
1516
1517                   else
1518                      Fixup_Rhs;
1519                   end if;
1520
1521                   Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1522                end if;
1523
1524                if Nkind (New_Rhs) = N_Op_And then
1525                   Set_Paren_Count (New_Rhs, 1);
1526                end if;
1527
1528                New_Rhs :=
1529                  Make_Op_Or (Loc,
1530                    Left_Opnd  => New_Rhs,
1531                    Right_Opnd => Or_Rhs);
1532             end;
1533          end if;
1534
1535          --  Now do the rewrite
1536
1537          Rewrite (N,
1538            Make_Assignment_Statement (Loc,
1539              Name       => New_Lhs,
1540              Expression =>
1541                Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1542          Set_Assignment_OK (Name (N), Ass_OK);
1543
1544       --  All other component sizes for non-modular case
1545
1546       else
1547          --  We generate
1548
1549          --    Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1550
1551          --  where Subscr is the computed linear subscript
1552
1553          declare
1554             Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1555             Set_nn  : Entity_Id;
1556             Subscr  : Node_Id;
1557             Atyp    : Entity_Id;
1558
1559          begin
1560             if No (Bits_nn) then
1561
1562                --  Error, most likely High_Integrity_Mode restriction
1563
1564                return;
1565             end if;
1566
1567             --  Acquire proper Set entity. We use the aligned or unaligned
1568             --  case as appropriate.
1569
1570             if Known_Aligned_Enough (Obj, Csiz) then
1571                Set_nn := RTE (Set_Id (Csiz));
1572             else
1573                Set_nn := RTE (SetU_Id (Csiz));
1574             end if;
1575
1576             --  Now generate the set reference
1577
1578             Obj := Relocate_Node (Prefix (Lhs));
1579             Convert_To_Actual_Subtype (Obj);
1580             Atyp := Etype (Obj);
1581             Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1582
1583             --  Below we must make the assumption that Obj is
1584             --  at least byte aligned, since otherwise its address
1585             --  cannot be taken. The assumption holds since the
1586             --  only arrays that can be misaligned are small packed
1587             --  arrays which are implemented as a modular type, and
1588             --  that is not the case here.
1589
1590             Rewrite (N,
1591               Make_Procedure_Call_Statement (Loc,
1592                   Name => New_Occurrence_Of (Set_nn, Loc),
1593                   Parameter_Associations => New_List (
1594                     Make_Attribute_Reference (Loc,
1595                       Attribute_Name => Name_Address,
1596                       Prefix         => Obj),
1597                     Subscr,
1598                     Unchecked_Convert_To (Bits_nn,
1599                       Convert_To (Ctyp, Rhs)))));
1600
1601          end;
1602       end if;
1603
1604       Analyze (N, Suppress => All_Checks);
1605    end Expand_Bit_Packed_Element_Set;
1606
1607    -------------------------------------
1608    -- Expand_Packed_Address_Reference --
1609    -------------------------------------
1610
1611    procedure Expand_Packed_Address_Reference (N : Node_Id) is
1612       Loc    : constant Source_Ptr := Sloc (N);
1613       Ploc   : Source_Ptr;
1614       Pref   : Node_Id;
1615       Expr   : Node_Id;
1616       Term   : Node_Id;
1617       Atyp   : Entity_Id;
1618       Subscr : Node_Id;
1619
1620    begin
1621       Pref := Prefix (N);
1622       Expr := Empty;
1623
1624       --  We build up an expression serially that has the form
1625
1626       --    outer_object'Address
1627       --      + (linear-subscript * component_size  for each array reference
1628       --      +  field'Bit_Position                 for each record field
1629       --      +  ...
1630       --      +  ...) / Storage_Unit;
1631
1632       --  Some additional conversions are required to deal with the addition
1633       --  operation, which is not normally visible to generated code.
1634
1635       loop
1636          Ploc := Sloc (Pref);
1637
1638          if Nkind (Pref) = N_Indexed_Component then
1639             Convert_To_Actual_Subtype (Prefix (Pref));
1640             Atyp := Etype (Prefix (Pref));
1641             Compute_Linear_Subscript (Atyp, Pref, Subscr);
1642
1643             Term :=
1644               Make_Op_Multiply (Ploc,
1645                 Left_Opnd => Subscr,
1646                 Right_Opnd =>
1647                  Make_Attribute_Reference (Ploc,
1648                    Prefix         => New_Occurrence_Of (Atyp, Ploc),
1649                    Attribute_Name => Name_Component_Size));
1650
1651          elsif Nkind (Pref) = N_Selected_Component then
1652             Term :=
1653               Make_Attribute_Reference (Ploc,
1654                 Prefix         => Selector_Name (Pref),
1655                 Attribute_Name => Name_Bit_Position);
1656
1657          else
1658             exit;
1659          end if;
1660
1661          Term := Convert_To (RTE (RE_Integer_Address), Term);
1662
1663          if No (Expr) then
1664             Expr := Term;
1665
1666          else
1667             Expr :=
1668               Make_Op_Add (Ploc,
1669                 Left_Opnd  => Expr,
1670                 Right_Opnd => Term);
1671          end if;
1672
1673          Pref := Prefix (Pref);
1674       end loop;
1675
1676       Rewrite (N,
1677         Unchecked_Convert_To (RTE (RE_Address),
1678           Make_Op_Add (Loc,
1679             Left_Opnd =>
1680               Unchecked_Convert_To (RTE (RE_Integer_Address),
1681                 Make_Attribute_Reference (Loc,
1682                   Prefix         => Pref,
1683                   Attribute_Name => Name_Address)),
1684
1685             Right_Opnd =>
1686               Make_Op_Divide (Loc,
1687                 Left_Opnd => Expr,
1688                 Right_Opnd =>
1689                   Make_Integer_Literal (Loc, System_Storage_Unit)))));
1690
1691       Analyze_And_Resolve (N, RTE (RE_Address));
1692    end Expand_Packed_Address_Reference;
1693
1694    ------------------------------------
1695    -- Expand_Packed_Boolean_Operator --
1696    ------------------------------------
1697
1698    --  This routine expands "a op b" for the packed cases
1699
1700    procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1701       Loc : constant Source_Ptr := Sloc (N);
1702       Typ : constant Entity_Id  := Etype (N);
1703       L   : constant Node_Id    := Relocate_Node (Left_Opnd  (N));
1704       R   : constant Node_Id    := Relocate_Node (Right_Opnd (N));
1705
1706       Ltyp : Entity_Id;
1707       Rtyp : Entity_Id;
1708       PAT  : Entity_Id;
1709
1710    begin
1711       Convert_To_Actual_Subtype (L);
1712       Convert_To_Actual_Subtype (R);
1713
1714       Ensure_Defined (Etype (L), N);
1715       Ensure_Defined (Etype (R), N);
1716
1717       Apply_Length_Check (R, Etype (L));
1718
1719       Ltyp := Etype (L);
1720       Rtyp := Etype (R);
1721
1722       --  First an odd and silly test. We explicitly check for the XOR
1723       --  case where the component type is True .. True, since this will
1724       --  raise constraint error. A special check is required since CE
1725       --  will not be required other wise (cf Expand_Packed_Not).
1726
1727       --  No such check is required for AND and OR, since for both these
1728       --  cases False op False = False, and True op True = True.
1729
1730       if Nkind (N) = N_Op_Xor then
1731          declare
1732             CT : constant Entity_Id := Component_Type (Rtyp);
1733             BT : constant Entity_Id := Base_Type (CT);
1734
1735          begin
1736             Insert_Action (N,
1737               Make_Raise_Constraint_Error (Loc,
1738                 Condition =>
1739                   Make_Op_And (Loc,
1740                     Left_Opnd =>
1741                       Make_Op_Eq (Loc,
1742                         Left_Opnd =>
1743                           Make_Attribute_Reference (Loc,
1744                             Prefix         => New_Occurrence_Of (CT, Loc),
1745                             Attribute_Name => Name_First),
1746
1747                         Right_Opnd =>
1748                           Convert_To (BT,
1749                             New_Occurrence_Of (Standard_True, Loc))),
1750
1751                     Right_Opnd =>
1752                       Make_Op_Eq (Loc,
1753                         Left_Opnd =>
1754                           Make_Attribute_Reference (Loc,
1755                             Prefix         => New_Occurrence_Of (CT, Loc),
1756                             Attribute_Name => Name_Last),
1757
1758                         Right_Opnd =>
1759                           Convert_To (BT,
1760                             New_Occurrence_Of (Standard_True, Loc)))),
1761                 Reason => CE_Range_Check_Failed));
1762          end;
1763       end if;
1764
1765       --  Now that that silliness is taken care of, get packed array type
1766
1767       Convert_To_PAT_Type (L);
1768       Convert_To_PAT_Type (R);
1769
1770       PAT := Etype (L);
1771
1772       --  For the modular case, we expand a op b into
1773
1774       --    rtyp!(pat!(a) op pat!(b))
1775
1776       --  where rtyp is the Etype of the left operand. Note that we do not
1777       --  convert to the base type, since this would be unconstrained, and
1778       --  hence not have a corresponding packed array type set.
1779
1780       --  Note that both operands must be modular for this code to be used
1781
1782       if Is_Modular_Integer_Type (PAT)
1783            and then
1784          Is_Modular_Integer_Type (Etype (R))
1785       then
1786          declare
1787             P : Node_Id;
1788
1789          begin
1790             if Nkind (N) = N_Op_And then
1791                P := Make_Op_And (Loc, L, R);
1792
1793             elsif Nkind (N) = N_Op_Or then
1794                P := Make_Op_Or  (Loc, L, R);
1795
1796             else -- Nkind (N) = N_Op_Xor
1797                P := Make_Op_Xor (Loc, L, R);
1798             end if;
1799
1800             Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1801          end;
1802
1803       --  For the array case, we insert the actions
1804
1805       --    Result : Ltype;
1806
1807       --    System.Bitops.Bit_And/Or/Xor
1808       --     (Left'Address,
1809       --      Ltype'Length * Ltype'Component_Size;
1810       --      Right'Address,
1811       --      Rtype'Length * Rtype'Component_Size
1812       --      Result'Address);
1813
1814       --  where Left and Right are the Packed_Bytes{1,2,4} operands and
1815       --  the second argument and fourth arguments are the lengths of the
1816       --  operands in bits. Then we replace the expression by a reference
1817       --  to Result.
1818
1819       --  Note that if we are mixing a modular and array operand, everything
1820       --  works fine, since we ensure that the modular representation has the
1821       --  same physical layout as the array representation (that's what the
1822       --  left justified modular stuff in the big-endian case is about).
1823
1824       else
1825          declare
1826             Result_Ent : constant Entity_Id :=
1827                            Make_Defining_Identifier (Loc,
1828                              Chars => New_Internal_Name ('T'));
1829
1830             E_Id : RE_Id;
1831
1832          begin
1833             if Nkind (N) = N_Op_And then
1834                E_Id := RE_Bit_And;
1835
1836             elsif Nkind (N) = N_Op_Or then
1837                E_Id := RE_Bit_Or;
1838
1839             else -- Nkind (N) = N_Op_Xor
1840                E_Id := RE_Bit_Xor;
1841             end if;
1842
1843             Insert_Actions (N, New_List (
1844
1845               Make_Object_Declaration (Loc,
1846                 Defining_Identifier => Result_Ent,
1847                 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1848
1849               Make_Procedure_Call_Statement (Loc,
1850                 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1851                   Parameter_Associations => New_List (
1852
1853                     Make_Byte_Aligned_Attribute_Reference (Loc,
1854                       Attribute_Name => Name_Address,
1855                       Prefix         => L),
1856
1857                     Make_Op_Multiply (Loc,
1858                       Left_Opnd =>
1859                         Make_Attribute_Reference (Loc,
1860                           Prefix =>
1861                             New_Occurrence_Of
1862                               (Etype (First_Index (Ltyp)), Loc),
1863                           Attribute_Name => Name_Range_Length),
1864                       Right_Opnd =>
1865                         Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1866
1867                     Make_Byte_Aligned_Attribute_Reference (Loc,
1868                       Attribute_Name => Name_Address,
1869                       Prefix         => R),
1870
1871                     Make_Op_Multiply (Loc,
1872                       Left_Opnd =>
1873                         Make_Attribute_Reference (Loc,
1874                           Prefix =>
1875                             New_Occurrence_Of
1876                               (Etype (First_Index (Rtyp)), Loc),
1877                           Attribute_Name => Name_Range_Length),
1878                       Right_Opnd =>
1879                         Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1880
1881                     Make_Byte_Aligned_Attribute_Reference (Loc,
1882                       Attribute_Name => Name_Address,
1883                       Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1884
1885             Rewrite (N,
1886               New_Occurrence_Of (Result_Ent, Loc));
1887          end;
1888       end if;
1889
1890       Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1891    end Expand_Packed_Boolean_Operator;
1892
1893    -------------------------------------
1894    -- Expand_Packed_Element_Reference --
1895    -------------------------------------
1896
1897    procedure Expand_Packed_Element_Reference (N : Node_Id) is
1898       Loc   : constant Source_Ptr := Sloc (N);
1899       Obj   : Node_Id;
1900       Atyp  : Entity_Id;
1901       PAT   : Entity_Id;
1902       Ctyp  : Entity_Id;
1903       Csiz  : Int;
1904       Shift : Node_Id;
1905       Cmask : Uint;
1906       Lit   : Node_Id;
1907       Arg   : Node_Id;
1908
1909    begin
1910       --  If not bit packed, we have the enumeration case, which is easily
1911       --  dealt with (just adjust the subscripts of the indexed component)
1912
1913       --  Note: this leaves the result as an indexed component, which is
1914       --  still a variable, so can be used in the assignment case, as is
1915       --  required in the enumeration case.
1916
1917       if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1918          Setup_Enumeration_Packed_Array_Reference (N);
1919          return;
1920       end if;
1921
1922       --  Remaining processing is for the bit-packed case
1923
1924       Obj := Relocate_Node (Prefix (N));
1925       Convert_To_Actual_Subtype (Obj);
1926       Atyp := Etype (Obj);
1927       PAT  := Packed_Array_Type (Atyp);
1928       Ctyp := Component_Type (Atyp);
1929       Csiz := UI_To_Int (Component_Size (Atyp));
1930
1931       --  Case of component size 1,2,4 or any component size for the modular
1932       --  case. These are the cases for which we can inline the code.
1933
1934       if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1935         or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1936       then
1937          Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1938          Lit := Make_Integer_Literal (Loc, Cmask);
1939          Set_Print_In_Hex (Lit);
1940
1941          --  We generate a shift right to position the field, followed by a
1942          --  masking operation to extract the bit field, and we finally do an
1943          --  unchecked conversion to convert the result to the required target.
1944
1945          --  Note that the unchecked conversion automatically deals with the
1946          --  bias if we are dealing with a biased representation. What will
1947          --  happen is that we temporarily generate the biased representation,
1948          --  but almost immediately that will be converted to the original
1949          --  unbiased component type, and the bias will disappear.
1950
1951          Arg :=
1952            Make_Op_And (Loc,
1953              Left_Opnd  => Make_Shift_Right (Obj, Shift),
1954              Right_Opnd => Lit);
1955
1956          --  We neded to analyze this before we do the unchecked convert
1957          --  below, but we need it temporarily attached to the tree for
1958          --  this analysis (hence the temporary Set_Parent call).
1959
1960          Set_Parent (Arg, Parent (N));
1961          Analyze_And_Resolve (Arg);
1962
1963          Rewrite (N,
1964            RJ_Unchecked_Convert_To (Ctyp, Arg));
1965
1966       --  All other component sizes for non-modular case
1967
1968       else
1969          --  We generate
1970
1971          --    Component_Type!(Get_nn (Arr'address, Subscr))
1972
1973          --  where Subscr is the computed linear subscript
1974
1975          declare
1976             Get_nn : Entity_Id;
1977             Subscr : Node_Id;
1978
1979          begin
1980             --  Acquire proper Get entity. We use the aligned or unaligned
1981             --  case as appropriate.
1982
1983             if Known_Aligned_Enough (Obj, Csiz) then
1984                Get_nn := RTE (Get_Id (Csiz));
1985             else
1986                Get_nn := RTE (GetU_Id (Csiz));
1987             end if;
1988
1989             --  Now generate the get reference
1990
1991             Compute_Linear_Subscript (Atyp, N, Subscr);
1992
1993             --  Below we make the assumption that Obj is at least byte
1994             --  aligned, since otherwise its address cannot be taken.
1995             --  The assumption holds since the only arrays that can be
1996             --  misaligned are small packed arrays which are implemented
1997             --  as a modular type, and that is not the case here.
1998
1999             Rewrite (N,
2000               Unchecked_Convert_To (Ctyp,
2001                 Make_Function_Call (Loc,
2002                   Name => New_Occurrence_Of (Get_nn, Loc),
2003                   Parameter_Associations => New_List (
2004                     Make_Attribute_Reference (Loc,
2005                       Attribute_Name => Name_Address,
2006                       Prefix         => Obj),
2007                     Subscr))));
2008          end;
2009       end if;
2010
2011       Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2012
2013    end Expand_Packed_Element_Reference;
2014
2015    ----------------------
2016    -- Expand_Packed_Eq --
2017    ----------------------
2018
2019    --  Handles expansion of "=" on packed array types
2020
2021    procedure Expand_Packed_Eq (N : Node_Id) is
2022       Loc : constant Source_Ptr := Sloc (N);
2023       L   : constant Node_Id    := Relocate_Node (Left_Opnd  (N));
2024       R   : constant Node_Id    := Relocate_Node (Right_Opnd (N));
2025
2026       LLexpr : Node_Id;
2027       RLexpr : Node_Id;
2028
2029       Ltyp : Entity_Id;
2030       Rtyp : Entity_Id;
2031       PAT  : Entity_Id;
2032
2033    begin
2034       Convert_To_Actual_Subtype (L);
2035       Convert_To_Actual_Subtype (R);
2036       Ltyp := Underlying_Type (Etype (L));
2037       Rtyp := Underlying_Type (Etype (R));
2038
2039       Convert_To_PAT_Type (L);
2040       Convert_To_PAT_Type (R);
2041       PAT := Etype (L);
2042
2043       LLexpr :=
2044         Make_Op_Multiply (Loc,
2045           Left_Opnd =>
2046             Make_Attribute_Reference (Loc,
2047               Attribute_Name => Name_Length,
2048               Prefix         => New_Occurrence_Of (Ltyp, Loc)),
2049           Right_Opnd =>
2050             Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2051
2052       RLexpr :=
2053         Make_Op_Multiply (Loc,
2054           Left_Opnd =>
2055             Make_Attribute_Reference (Loc,
2056               Attribute_Name => Name_Length,
2057               Prefix         => New_Occurrence_Of (Rtyp, Loc)),
2058           Right_Opnd =>
2059             Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2060
2061       --  For the modular case, we transform the comparison to:
2062
2063       --    Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2064
2065       --  where PAT is the packed array type. This works fine, since in the
2066       --  modular case we guarantee that the unused bits are always zeroes.
2067       --  We do have to compare the lengths because we could be comparing
2068       --  two different subtypes of the same base type.
2069
2070       if Is_Modular_Integer_Type (PAT) then
2071          Rewrite (N,
2072            Make_And_Then (Loc,
2073              Left_Opnd =>
2074                Make_Op_Eq (Loc,
2075                  Left_Opnd  => LLexpr,
2076                  Right_Opnd => RLexpr),
2077
2078              Right_Opnd =>
2079                Make_Op_Eq (Loc,
2080                  Left_Opnd => L,
2081                  Right_Opnd => R)));
2082
2083       --  For the non-modular case, we call a runtime routine
2084
2085       --    System.Bit_Ops.Bit_Eq
2086       --      (L'Address, L_Length, R'Address, R_Length)
2087
2088       --  where PAT is the packed array type, and the lengths are the lengths
2089       --  in bits of the original packed arrays. This routine takes care of
2090       --  not comparing the unused bits in the last byte.
2091
2092       else
2093          Rewrite (N,
2094            Make_Function_Call (Loc,
2095              Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2096              Parameter_Associations => New_List (
2097                Make_Byte_Aligned_Attribute_Reference (Loc,
2098                  Attribute_Name => Name_Address,
2099                  Prefix         => L),
2100
2101                LLexpr,
2102
2103                Make_Byte_Aligned_Attribute_Reference (Loc,
2104                  Attribute_Name => Name_Address,
2105                  Prefix         => R),
2106
2107                RLexpr)));
2108       end if;
2109
2110       Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2111    end Expand_Packed_Eq;
2112
2113    -----------------------
2114    -- Expand_Packed_Not --
2115    -----------------------
2116
2117    --  Handles expansion of "not" on packed array types
2118
2119    procedure Expand_Packed_Not (N : Node_Id) is
2120       Loc  : constant Source_Ptr := Sloc (N);
2121       Typ  : constant Entity_Id  := Etype (N);
2122       Opnd : constant Node_Id    := Relocate_Node (Right_Opnd (N));
2123
2124       Rtyp : Entity_Id;
2125       PAT  : Entity_Id;
2126       Lit  : Node_Id;
2127
2128    begin
2129       Convert_To_Actual_Subtype (Opnd);
2130       Rtyp := Etype (Opnd);
2131
2132       --  First an odd and silly test. We explicitly check for the case
2133       --  where the 'First of the component type is equal to the 'Last of
2134       --  this component type, and if this is the case, we make sure that
2135       --  constraint error is raised. The reason is that the NOT is bound
2136       --  to cause CE in this case, and we will not otherwise catch it.
2137
2138       --  Believe it or not, this was reported as a bug. Note that nearly
2139       --  always, the test will evaluate statically to False, so the code
2140       --  will be statically removed, and no extra overhead caused.
2141
2142       declare
2143          CT : constant Entity_Id := Component_Type (Rtyp);
2144
2145       begin
2146          Insert_Action (N,
2147            Make_Raise_Constraint_Error (Loc,
2148              Condition =>
2149                Make_Op_Eq (Loc,
2150                  Left_Opnd =>
2151                    Make_Attribute_Reference (Loc,
2152                      Prefix         => New_Occurrence_Of (CT, Loc),
2153                      Attribute_Name => Name_First),
2154
2155                  Right_Opnd =>
2156                    Make_Attribute_Reference (Loc,
2157                      Prefix         => New_Occurrence_Of (CT, Loc),
2158                      Attribute_Name => Name_Last)),
2159              Reason => CE_Range_Check_Failed));
2160       end;
2161
2162       --  Now that that silliness is taken care of, get packed array type
2163
2164       Convert_To_PAT_Type (Opnd);
2165       PAT := Etype (Opnd);
2166
2167       --  For the case where the packed array type is a modular type,
2168       --  not A expands simply into:
2169
2170       --     rtyp!(PAT!(A) xor mask)
2171
2172       --  where PAT is the packed array type, and mask is a mask of all
2173       --  one bits of length equal to the size of this packed type and
2174       --  rtyp is the actual subtype of the operand
2175
2176       Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2177       Set_Print_In_Hex (Lit);
2178
2179       if not Is_Array_Type (PAT) then
2180          Rewrite (N,
2181            Unchecked_Convert_To (Rtyp,
2182              Make_Op_Xor (Loc,
2183                Left_Opnd  => Opnd,
2184                Right_Opnd => Lit)));
2185
2186       --  For the array case, we insert the actions
2187
2188       --    Result : Typ;
2189
2190       --    System.Bitops.Bit_Not
2191       --     (Opnd'Address,
2192       --      Typ'Length * Typ'Component_Size;
2193       --      Result'Address);
2194
2195       --  where Opnd is the Packed_Bytes{1,2,4} operand and the second
2196       --  argument is the length of the operand in bits. Then we replace
2197       --  the expression by a reference to Result.
2198
2199       else
2200          declare
2201             Result_Ent : constant Entity_Id :=
2202                            Make_Defining_Identifier (Loc,
2203                              Chars => New_Internal_Name ('T'));
2204
2205          begin
2206             Insert_Actions (N, New_List (
2207
2208               Make_Object_Declaration (Loc,
2209                 Defining_Identifier => Result_Ent,
2210                 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2211
2212               Make_Procedure_Call_Statement (Loc,
2213                 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2214                   Parameter_Associations => New_List (
2215
2216                     Make_Byte_Aligned_Attribute_Reference (Loc,
2217                       Attribute_Name => Name_Address,
2218                       Prefix         => Opnd),
2219
2220                     Make_Op_Multiply (Loc,
2221                       Left_Opnd =>
2222                         Make_Attribute_Reference (Loc,
2223                           Prefix =>
2224                             New_Occurrence_Of
2225                               (Etype (First_Index (Rtyp)), Loc),
2226                           Attribute_Name => Name_Range_Length),
2227                       Right_Opnd =>
2228                         Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2229
2230                     Make_Byte_Aligned_Attribute_Reference (Loc,
2231                       Attribute_Name => Name_Address,
2232                       Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2233
2234             Rewrite (N,
2235               New_Occurrence_Of (Result_Ent, Loc));
2236          end;
2237       end if;
2238
2239       Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2240
2241    end Expand_Packed_Not;
2242
2243    -------------------------------------
2244    -- Involves_Packed_Array_Reference --
2245    -------------------------------------
2246
2247    function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2248    begin
2249       if Nkind (N) = N_Indexed_Component
2250         and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2251       then
2252          return True;
2253
2254       elsif Nkind (N) = N_Selected_Component then
2255          return Involves_Packed_Array_Reference (Prefix (N));
2256
2257       else
2258          return False;
2259       end if;
2260    end Involves_Packed_Array_Reference;
2261
2262    --------------------------
2263    -- Known_Aligned_Enough --
2264    --------------------------
2265
2266    function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2267       Typ : constant Entity_Id := Etype (Obj);
2268
2269       function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2270       --  If the component is in a record that contains previous packed
2271       --  components, consider it unaligned because the back-end might
2272       --  choose to pack the rest of the record. Lead to less efficient code,
2273       --  but safer vis-a-vis of back-end choices.
2274
2275       --------------------------------
2276       -- In_Partially_Packed_Record --
2277       --------------------------------
2278
2279       function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2280          Rec_Type  : constant Entity_Id := Scope (Comp);
2281          Prev_Comp : Entity_Id;
2282
2283       begin
2284          Prev_Comp := First_Entity (Rec_Type);
2285          while Present (Prev_Comp) loop
2286             if Is_Packed (Etype (Prev_Comp)) then
2287                return True;
2288
2289             elsif Prev_Comp = Comp then
2290                return False;
2291             end if;
2292
2293             Next_Entity (Prev_Comp);
2294          end loop;
2295
2296          return False;
2297       end  In_Partially_Packed_Record;
2298
2299    --  Start of processing for Known_Aligned_Enough
2300
2301    begin
2302       --  Odd bit sizes don't need alignment anyway
2303
2304       if Csiz mod 2 = 1 then
2305          return True;
2306
2307       --  If we have a specified alignment, see if it is sufficient, if not
2308       --  then we can't possibly be aligned enough in any case.
2309
2310       elsif Known_Alignment (Etype (Obj)) then
2311          --  Alignment required is 4 if size is a multiple of 4, and
2312          --  2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2313
2314          if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2315             return False;
2316          end if;
2317       end if;
2318
2319       --  OK, alignment should be sufficient, if object is aligned
2320
2321       --  If object is strictly aligned, then it is definitely aligned
2322
2323       if Strict_Alignment (Typ) then
2324          return True;
2325
2326       --  Case of subscripted array reference
2327
2328       elsif Nkind (Obj) = N_Indexed_Component then
2329
2330          --  If we have a pointer to an array, then this is definitely
2331          --  aligned, because pointers always point to aligned versions.
2332
2333          if Is_Access_Type (Etype (Prefix (Obj))) then
2334             return True;
2335
2336          --  Otherwise, go look at the prefix
2337
2338          else
2339             return Known_Aligned_Enough (Prefix (Obj), Csiz);
2340          end if;
2341
2342       --  Case of record field
2343
2344       elsif Nkind (Obj) = N_Selected_Component then
2345
2346          --  What is significant here is whether the record type is packed
2347
2348          if Is_Record_Type (Etype (Prefix (Obj)))
2349            and then Is_Packed (Etype (Prefix (Obj)))
2350          then
2351             return False;
2352
2353          --  Or the component has a component clause which might cause
2354          --  the component to become unaligned (we can't tell if the
2355          --  backend is doing alignment computations).
2356
2357          elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2358             return False;
2359
2360          elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2361             return False;
2362
2363          --  In all other cases, go look at prefix
2364
2365          else
2366             return Known_Aligned_Enough (Prefix (Obj), Csiz);
2367          end if;
2368
2369       elsif Nkind (Obj) = N_Type_Conversion then
2370          return Known_Aligned_Enough (Expression (Obj), Csiz);
2371
2372       --  For a formal parameter, it is safer to assume that it is not
2373       --  aligned, because the formal may be unconstrained while the actual
2374       --  is constrained. In this situation, a small constrained packed
2375       --  array, represented in modular form, may be unaligned.
2376
2377       elsif Is_Entity_Name (Obj) then
2378          return not Is_Formal (Entity (Obj));
2379       else
2380
2381       --  If none of the above, must be aligned
2382          return True;
2383       end if;
2384    end Known_Aligned_Enough;
2385
2386    ---------------------
2387    -- Make_Shift_Left --
2388    ---------------------
2389
2390    function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2391       Nod : Node_Id;
2392
2393    begin
2394       if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2395          return N;
2396       else
2397          Nod :=
2398            Make_Op_Shift_Left (Sloc (N),
2399              Left_Opnd  => N,
2400              Right_Opnd => S);
2401          Set_Shift_Count_OK (Nod, True);
2402          return Nod;
2403       end if;
2404    end Make_Shift_Left;
2405
2406    ----------------------
2407    -- Make_Shift_Right --
2408    ----------------------
2409
2410    function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2411       Nod : Node_Id;
2412
2413    begin
2414       if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2415          return N;
2416       else
2417          Nod :=
2418            Make_Op_Shift_Right (Sloc (N),
2419              Left_Opnd  => N,
2420              Right_Opnd => S);
2421          Set_Shift_Count_OK (Nod, True);
2422          return Nod;
2423       end if;
2424    end Make_Shift_Right;
2425
2426    -----------------------------
2427    -- RJ_Unchecked_Convert_To --
2428    -----------------------------
2429
2430    function RJ_Unchecked_Convert_To
2431      (Typ  : Entity_Id;
2432       Expr : Node_Id) return Node_Id
2433    is
2434       Source_Typ : constant Entity_Id := Etype (Expr);
2435       Target_Typ : constant Entity_Id := Typ;
2436
2437       Src : Node_Id := Expr;
2438
2439       Source_Siz : Nat;
2440       Target_Siz : Nat;
2441
2442    begin
2443       Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2444       Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2445
2446       --  First step, if the source type is not a discrete type, then we
2447       --  first convert to a modular type of the source length, since
2448       --  otherwise, on a big-endian machine, we get left-justification.
2449       --  We do it for little-endian machines as well, because there might
2450       --  be junk bits that are not cleared if the type is not numeric.
2451
2452       if Source_Siz /= Target_Siz
2453         and then  not Is_Discrete_Type (Source_Typ)
2454       then
2455          Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2456       end if;
2457
2458       --  In the big endian case, if the lengths of the two types differ,
2459       --  then we must worry about possible left justification in the
2460       --  conversion, and avoiding that is what this is all about.
2461
2462       if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2463
2464          --  Next step. If the target is not a discrete type, then we first
2465          --  convert to a modular type of the target length, since
2466          --  otherwise, on a big-endian machine, we get left-justification.
2467
2468          if not Is_Discrete_Type (Target_Typ) then
2469             Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2470          end if;
2471       end if;
2472
2473       --  And now we can do the final conversion to the target type
2474
2475       return Unchecked_Convert_To (Target_Typ, Src);
2476    end RJ_Unchecked_Convert_To;
2477
2478    ----------------------------------------------
2479    -- Setup_Enumeration_Packed_Array_Reference --
2480    ----------------------------------------------
2481
2482    --  All we have to do here is to find the subscripts that correspond
2483    --  to the index positions that have non-standard enumeration types
2484    --  and insert a Pos attribute to get the proper subscript value.
2485
2486    --  Finally the prefix must be uncheck converted to the corresponding
2487    --  packed array type.
2488
2489    --  Note that the component type is unchanged, so we do not need to
2490    --  fiddle with the types (Gigi always automatically takes the packed
2491    --  array type if it is set, as it will be in this case).
2492
2493    procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2494       Pfx   : constant Node_Id   := Prefix (N);
2495       Typ   : constant Entity_Id := Etype (N);
2496       Exprs : constant List_Id   := Expressions (N);
2497       Expr  : Node_Id;
2498
2499    begin
2500       --  If the array is unconstrained, then we replace the array
2501       --  reference with its actual subtype. This actual subtype will
2502       --  have a packed array type with appropriate bounds.
2503
2504       if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2505          Convert_To_Actual_Subtype (Pfx);
2506       end if;
2507
2508       Expr := First (Exprs);
2509       while Present (Expr) loop
2510          declare
2511             Loc      : constant Source_Ptr := Sloc (Expr);
2512             Expr_Typ : constant Entity_Id := Etype (Expr);
2513
2514          begin
2515             if Is_Enumeration_Type (Expr_Typ)
2516               and then Has_Non_Standard_Rep (Expr_Typ)
2517             then
2518                Rewrite (Expr,
2519                  Make_Attribute_Reference (Loc,
2520                    Prefix         => New_Occurrence_Of (Expr_Typ, Loc),
2521                    Attribute_Name => Name_Pos,
2522                    Expressions    => New_List (Relocate_Node (Expr))));
2523                Analyze_And_Resolve (Expr, Standard_Natural);
2524             end if;
2525          end;
2526
2527          Next (Expr);
2528       end loop;
2529
2530       Rewrite (N,
2531         Make_Indexed_Component (Sloc (N),
2532           Prefix      =>
2533             Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2534           Expressions => Exprs));
2535
2536       Analyze_And_Resolve (N, Typ);
2537
2538    end Setup_Enumeration_Packed_Array_Reference;
2539
2540    -----------------------------------------
2541    -- Setup_Inline_Packed_Array_Reference --
2542    -----------------------------------------
2543
2544    procedure Setup_Inline_Packed_Array_Reference
2545      (N      : Node_Id;
2546       Atyp   : Entity_Id;
2547       Obj    : in out Node_Id;
2548       Cmask  : out Uint;
2549       Shift  : out Node_Id)
2550    is
2551       Loc    : constant Source_Ptr := Sloc (N);
2552       PAT    : Entity_Id;
2553       Otyp   : Entity_Id;
2554       Csiz   : Uint;
2555       Osiz   : Uint;
2556
2557    begin
2558       Csiz := Component_Size (Atyp);
2559
2560       Convert_To_PAT_Type (Obj);
2561       PAT  := Etype (Obj);
2562
2563       Cmask := 2 ** Csiz - 1;
2564
2565       if Is_Array_Type (PAT) then
2566          Otyp := Component_Type (PAT);
2567          Osiz := Component_Size (PAT);
2568
2569       else
2570          Otyp := PAT;
2571
2572          --  In the case where the PAT is a modular type, we want the actual
2573          --  size in bits of the modular value we use. This is neither the
2574          --  Object_Size nor the Value_Size, either of which may have been
2575          --  reset to strange values, but rather the minimum size. Note that
2576          --  since this is a modular type with full range, the issue of
2577          --  biased representation does not arise.
2578
2579          Osiz := UI_From_Int (Minimum_Size (Otyp));
2580       end if;
2581
2582       Compute_Linear_Subscript (Atyp, N, Shift);
2583
2584       --  If the component size is not 1, then the subscript must be
2585       --  multiplied by the component size to get the shift count.
2586
2587       if Csiz /= 1 then
2588          Shift :=
2589            Make_Op_Multiply (Loc,
2590              Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2591              Right_Opnd => Shift);
2592       end if;
2593
2594       --  If we have the array case, then this shift count must be broken
2595       --  down into a byte subscript, and a shift within the byte.
2596
2597       if Is_Array_Type (PAT) then
2598
2599          declare
2600             New_Shift : Node_Id;
2601
2602          begin
2603             --  We must analyze shift, since we will duplicate it
2604
2605             Set_Parent (Shift, N);
2606             Analyze_And_Resolve
2607               (Shift, Standard_Integer, Suppress => All_Checks);
2608
2609             --  The shift count within the word is
2610             --    shift mod Osiz
2611
2612             New_Shift :=
2613               Make_Op_Mod (Loc,
2614                 Left_Opnd  => Duplicate_Subexpr (Shift),
2615                 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2616
2617             --  The subscript to be used on the PAT array is
2618             --    shift / Osiz
2619
2620             Obj :=
2621               Make_Indexed_Component (Loc,
2622                 Prefix => Obj,
2623                 Expressions => New_List (
2624                   Make_Op_Divide (Loc,
2625                     Left_Opnd => Duplicate_Subexpr (Shift),
2626                     Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2627
2628             Shift := New_Shift;
2629          end;
2630
2631       --  For the modular integer case, the object to be manipulated is
2632       --  the entire array, so Obj is unchanged. Note that we will reset
2633       --  its type to PAT before returning to the caller.
2634
2635       else
2636          null;
2637       end if;
2638
2639       --  The one remaining step is to modify the shift count for the
2640       --  big-endian case. Consider the following example in a byte:
2641
2642       --     xxxxxxxx  bits of byte
2643       --     vvvvvvvv  bits of value
2644       --     33221100  little-endian numbering
2645       --     00112233  big-endian numbering
2646
2647       --  Here we have the case of 2-bit fields
2648
2649       --  For the little-endian case, we already have the proper shift
2650       --  count set, e.g. for element 2, the shift count is 2*2 = 4.
2651
2652       --  For the big endian case, we have to adjust the shift count,
2653       --  computing it as (N - F) - shift, where N is the number of bits
2654       --  in an element of the array used to implement the packed array,
2655       --  F is the number of bits in a source level array element, and
2656       --  shift is the count so far computed.
2657
2658       if Bytes_Big_Endian then
2659          Shift :=
2660            Make_Op_Subtract (Loc,
2661              Left_Opnd  => Make_Integer_Literal (Loc, Osiz - Csiz),
2662              Right_Opnd => Shift);
2663       end if;
2664
2665       Set_Parent (Shift, N);
2666       Set_Parent (Obj, N);
2667       Analyze_And_Resolve (Obj,   Otyp,             Suppress => All_Checks);
2668       Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2669
2670       --  Make sure final type of object is the appropriate packed type
2671
2672       Set_Etype (Obj, Otyp);
2673
2674    end Setup_Inline_Packed_Array_Reference;
2675
2676 end Exp_Pakd;