Fix ICE in lto_symtab_merge_symbols_1 (PR lto/88004).
[official-gcc.git] / gcc / ada / exp_pakd.adb
blob632c3bd6350eead4b88ce390c93ad0a7304bfd9b
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-2018, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Layout; use Layout;
33 with Lib.Xref; use Lib.Xref;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
37 with Opt; use Opt;
38 with Sem; use Sem;
39 with Sem_Aux; use Sem_Aux;
40 with Sem_Ch3; use Sem_Ch3;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Ch13; use Sem_Ch13;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sinfo; use Sinfo;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Targparm; use Targparm;
50 with Tbuild; use Tbuild;
51 with Ttypes; use Ttypes;
52 with Uintp; use Uintp;
54 package body Exp_Pakd is
56 ---------------------------
57 -- Endian Considerations --
58 ---------------------------
60 -- As described in the specification, bit numbering in a packed array
61 -- is consistent with bit numbering in a record representation clause,
62 -- and hence dependent on the endianness of the machine:
64 -- For little-endian machines, element zero is at the right hand end
65 -- (low order end) of a bit field.
67 -- For big-endian machines, element zero is at the left hand end
68 -- (high order end) of a bit field.
70 -- The shifts that are used to right justify a field therefore differ in
71 -- the two cases. For the little-endian case, we can simply use the bit
72 -- number (i.e. the element number * element size) as the count for a right
73 -- shift. For the big-endian case, we have to subtract the shift count from
74 -- an appropriate constant to use in the right shift. We use rotates
75 -- instead of shifts (which is necessary in the store case to preserve
76 -- other fields), and we expect that the backend will be able to change the
77 -- right rotate into a left rotate, avoiding the subtract, if the machine
78 -- architecture provides such an instruction.
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Compute_Linear_Subscript
85 (Atyp : Entity_Id;
86 N : Node_Id;
87 Subscr : out Node_Id);
88 -- Given a constrained array type Atyp, and an indexed component node N
89 -- referencing an array object of this type, build an expression of type
90 -- Standard.Integer representing the zero-based linear subscript value.
91 -- This expression includes any required range checks.
93 function Compute_Number_Components
94 (N : Node_Id;
95 Typ : Entity_Id) return Node_Id;
96 -- Build an expression that multiplies the length of the dimensions of the
97 -- array, used to control array equality checks.
99 procedure Convert_To_PAT_Type (Aexp : Node_Id);
100 -- Given an expression of a packed array type, builds a corresponding
101 -- expression whose type is the implementation type used to represent
102 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
104 procedure Get_Base_And_Bit_Offset
105 (N : Node_Id;
106 Base : out Node_Id;
107 Offset : out Node_Id);
108 -- Given a node N for a name which involves a packed array reference,
109 -- return the base object of the reference and build an expression of
110 -- type Standard.Integer representing the zero-based offset in bits
111 -- from Base'Address to the first bit of the reference.
113 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
114 -- There are two versions of the Set routines, the ones used when the
115 -- object is known to be sufficiently well aligned given the number of
116 -- bits, and the ones used when the object is not known to be aligned.
117 -- This routine is used to determine which set to use. Obj is a reference
118 -- to the object, and Csiz is the component size of the packed array.
119 -- True is returned if the alignment of object is known to be sufficient,
120 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
121 -- 2 otherwise.
123 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
124 -- Build a left shift node, checking for the case of a shift count of zero
126 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
127 -- Build a right shift node, checking for the case of a shift count of zero
129 function RJ_Unchecked_Convert_To
130 (Typ : Entity_Id;
131 Expr : Node_Id) return Node_Id;
132 -- The packed array code does unchecked conversions which in some cases
133 -- may involve non-discrete types with differing sizes. The semantics of
134 -- such conversions is potentially endianness dependent, and the effect
135 -- we want here for such a conversion is to do the conversion in size as
136 -- though numeric items are involved, and we extend or truncate on the
137 -- left side. This happens naturally in the little-endian case, but in
138 -- the big endian case we can get left justification, when what we want
139 -- is right justification. This routine does the unchecked conversion in
140 -- a stepwise manner to ensure that it gives the expected result. Hence
141 -- the name (RJ = Right justified). The parameters Typ and Expr are as
142 -- for the case of a normal Unchecked_Convert_To call.
144 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
145 -- This routine is called in the Get and Set case for arrays that are
146 -- packed but not bit-packed, meaning that they have at least one
147 -- subscript that is of an enumeration type with a non-standard
148 -- representation. This routine modifies the given node to properly
149 -- reference the corresponding packed array type.
151 procedure Setup_Inline_Packed_Array_Reference
152 (N : Node_Id;
153 Atyp : Entity_Id;
154 Obj : in out Node_Id;
155 Cmask : out Uint;
156 Shift : out Node_Id);
157 -- This procedure performs common processing on the N_Indexed_Component
158 -- parameter given as N, whose prefix is a reference to a packed array.
159 -- This is used for the get and set when the component size is 1, 2, 4,
160 -- or for other component sizes when the packed array type is a modular
161 -- type (i.e. the cases that are handled with inline code).
163 -- On entry:
165 -- N is the N_Indexed_Component node for the packed array reference
167 -- Atyp is the constrained array type (the actual subtype has been
168 -- computed if necessary to obtain the constraints, but this is still
169 -- the original array type, not the Packed_Array_Impl_Type value).
171 -- Obj is the object which is to be indexed. It is always of type Atyp.
173 -- On return:
175 -- Obj is the object containing the desired bit field. It is of type
176 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
177 -- entire value, for the small static case, or the proper selected byte
178 -- from the array in the large or dynamic case. This node is analyzed
179 -- and resolved on return.
181 -- Shift is a node representing the shift count to be used in the
182 -- rotate right instruction that positions the field for access.
183 -- This node is analyzed and resolved on return.
185 -- Cmask is a mask corresponding to the width of the component field.
186 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
188 -- Note: in some cases the call to this routine may generate actions
189 -- (for handling multi-use references and the generation of the packed
190 -- array type on the fly). Such actions are inserted into the tree
191 -- directly using Insert_Action.
193 function Revert_Storage_Order (N : Node_Id) return Node_Id;
194 -- Perform appropriate justification and byte ordering adjustments for N,
195 -- an element of a packed array type, when both the component type and
196 -- the enclosing packed array type have reverse scalar storage order.
197 -- On little-endian targets, the value is left justified before byte
198 -- swapping. The Etype of the returned expression is an integer type of
199 -- an appropriate power-of-2 size.
201 --------------------------
202 -- Revert_Storage_Order --
203 --------------------------
205 function Revert_Storage_Order (N : Node_Id) return Node_Id is
206 Loc : constant Source_Ptr := Sloc (N);
207 T : constant Entity_Id := Etype (N);
208 T_Size : constant Uint := RM_Size (T);
210 Swap_RE : RE_Id;
211 Swap_F : Entity_Id;
212 Swap_T : Entity_Id;
213 -- Swapping function
215 Arg : Node_Id;
216 Adjusted : Node_Id;
217 Shift : Uint;
219 begin
220 if T_Size <= 8 then
222 -- Array component size is less than a byte: no swapping needed
224 Swap_F := Empty;
225 Swap_T := RTE (RE_Unsigned_8);
227 else
228 -- Select byte swapping function depending on array component size
230 if T_Size <= 16 then
231 Swap_RE := RE_Bswap_16;
233 elsif T_Size <= 32 then
234 Swap_RE := RE_Bswap_32;
236 else pragma Assert (T_Size <= 64);
237 Swap_RE := RE_Bswap_64;
238 end if;
240 Swap_F := RTE (Swap_RE);
241 Swap_T := Etype (Swap_F);
243 end if;
245 Shift := Esize (Swap_T) - T_Size;
247 Arg := RJ_Unchecked_Convert_To (Swap_T, N);
249 if not Bytes_Big_Endian and then Shift > Uint_0 then
250 Arg :=
251 Make_Op_Shift_Left (Loc,
252 Left_Opnd => Arg,
253 Right_Opnd => Make_Integer_Literal (Loc, Shift));
254 end if;
256 if Present (Swap_F) then
257 Adjusted :=
258 Make_Function_Call (Loc,
259 Name => New_Occurrence_Of (Swap_F, Loc),
260 Parameter_Associations => New_List (Arg));
261 else
262 Adjusted := Arg;
263 end if;
265 Set_Etype (Adjusted, Swap_T);
266 return Adjusted;
267 end Revert_Storage_Order;
269 ------------------------------
270 -- Compute_Linear_Subscript --
271 ------------------------------
273 procedure Compute_Linear_Subscript
274 (Atyp : Entity_Id;
275 N : Node_Id;
276 Subscr : out Node_Id)
278 Loc : constant Source_Ptr := Sloc (N);
279 Oldsub : Node_Id;
280 Newsub : Node_Id;
281 Indx : Node_Id;
282 Styp : Entity_Id;
284 begin
285 Subscr := Empty;
287 -- Loop through dimensions
289 Indx := First_Index (Atyp);
290 Oldsub := First (Expressions (N));
292 while Present (Indx) loop
293 Styp := Etype (Indx);
294 Newsub := Relocate_Node (Oldsub);
296 -- Get expression for the subscript value. First, if Do_Range_Check
297 -- is set on a subscript, then we must do a range check against the
298 -- original bounds (not the bounds of the packed array type). We do
299 -- this by introducing a subtype conversion.
301 if Do_Range_Check (Newsub)
302 and then Etype (Newsub) /= Styp
303 then
304 Newsub := Convert_To (Styp, Newsub);
305 end if;
307 -- Now evolve the expression for the subscript. First convert
308 -- the subscript to be zero based and of an integer type.
310 -- Case of integer type, where we just subtract to get lower bound
312 if Is_Integer_Type (Styp) then
314 -- If length of integer type is smaller than standard integer,
315 -- then we convert to integer first, then do the subtract
317 -- Integer (subscript) - Integer (Styp'First)
319 if Esize (Styp) < Esize (Standard_Integer) then
320 Newsub :=
321 Make_Op_Subtract (Loc,
322 Left_Opnd => Convert_To (Standard_Integer, Newsub),
323 Right_Opnd =>
324 Convert_To (Standard_Integer,
325 Make_Attribute_Reference (Loc,
326 Prefix => New_Occurrence_Of (Styp, Loc),
327 Attribute_Name => Name_First)));
329 -- For larger integer types, subtract first, then convert to
330 -- integer, this deals with strange long long integer bounds.
332 -- Integer (subscript - Styp'First)
334 else
335 Newsub :=
336 Convert_To (Standard_Integer,
337 Make_Op_Subtract (Loc,
338 Left_Opnd => Newsub,
339 Right_Opnd =>
340 Make_Attribute_Reference (Loc,
341 Prefix => New_Occurrence_Of (Styp, Loc),
342 Attribute_Name => Name_First)));
343 end if;
345 -- For the enumeration case, we have to use 'Pos to get the value
346 -- to work with before subtracting the lower bound.
348 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
350 -- This is not quite right for bizarre cases where the size of the
351 -- enumeration type is > Integer'Size bits due to rep clause ???
353 else
354 pragma Assert (Is_Enumeration_Type (Styp));
356 Newsub :=
357 Make_Op_Subtract (Loc,
358 Left_Opnd => Convert_To (Standard_Integer,
359 Make_Attribute_Reference (Loc,
360 Prefix => New_Occurrence_Of (Styp, Loc),
361 Attribute_Name => Name_Pos,
362 Expressions => New_List (Newsub))),
364 Right_Opnd =>
365 Convert_To (Standard_Integer,
366 Make_Attribute_Reference (Loc,
367 Prefix => New_Occurrence_Of (Styp, Loc),
368 Attribute_Name => Name_Pos,
369 Expressions => New_List (
370 Make_Attribute_Reference (Loc,
371 Prefix => New_Occurrence_Of (Styp, Loc),
372 Attribute_Name => Name_First)))));
373 end if;
375 Set_Paren_Count (Newsub, 1);
377 -- For the first subscript, we just copy that subscript value
379 if No (Subscr) then
380 Subscr := Newsub;
382 -- Otherwise, we must multiply what we already have by the current
383 -- stride and then add in the new value to the evolving subscript.
385 else
386 Subscr :=
387 Make_Op_Add (Loc,
388 Left_Opnd =>
389 Make_Op_Multiply (Loc,
390 Left_Opnd => Subscr,
391 Right_Opnd =>
392 Make_Attribute_Reference (Loc,
393 Attribute_Name => Name_Range_Length,
394 Prefix => New_Occurrence_Of (Styp, Loc))),
395 Right_Opnd => Newsub);
396 end if;
398 -- Move to next subscript
400 Next_Index (Indx);
401 Next (Oldsub);
402 end loop;
403 end Compute_Linear_Subscript;
405 -------------------------------
406 -- Compute_Number_Components --
407 -------------------------------
409 function Compute_Number_Components
410 (N : Node_Id;
411 Typ : Entity_Id) return Node_Id
413 Loc : constant Source_Ptr := Sloc (N);
414 Len_Expr : Node_Id;
416 begin
417 Len_Expr :=
418 Make_Attribute_Reference (Loc,
419 Attribute_Name => Name_Length,
420 Prefix => New_Occurrence_Of (Typ, Loc),
421 Expressions => New_List (Make_Integer_Literal (Loc, 1)));
423 for J in 2 .. Number_Dimensions (Typ) loop
424 Len_Expr :=
425 Make_Op_Multiply (Loc,
426 Left_Opnd => Len_Expr,
427 Right_Opnd =>
428 Make_Attribute_Reference (Loc,
429 Attribute_Name => Name_Length,
430 Prefix => New_Occurrence_Of (Typ, Loc),
431 Expressions => New_List (Make_Integer_Literal (Loc, J))));
432 end loop;
434 return Len_Expr;
435 end Compute_Number_Components;
437 -------------------------
438 -- Convert_To_PAT_Type --
439 -------------------------
441 -- The PAT is always obtained from the actual subtype
443 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
444 Act_ST : Entity_Id;
446 begin
447 Convert_To_Actual_Subtype (Aexp);
448 Act_ST := Underlying_Type (Etype (Aexp));
449 Create_Packed_Array_Impl_Type (Act_ST);
451 -- Just replace the etype with the packed array type. This works because
452 -- the expression will not be further analyzed, and Gigi considers the
453 -- two types equivalent in any case.
455 -- This is not strictly the case ??? If the reference is an actual in
456 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
457 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
458 -- array reference, reanalysis can produce spurious type errors when the
459 -- PAT type is replaced again with the original type of the array. Same
460 -- for the case of a dereference. Ditto for function calls: expansion
461 -- may introduce additional actuals which will trigger errors if call is
462 -- reanalyzed. The following is correct and minimal, but the handling of
463 -- more complex packed expressions in actuals is confused. Probably the
464 -- problem only remains for actuals in calls.
466 Set_Etype (Aexp, Packed_Array_Impl_Type (Act_ST));
468 if Is_Entity_Name (Aexp)
469 or else
470 (Nkind (Aexp) = N_Indexed_Component
471 and then Is_Entity_Name (Prefix (Aexp)))
472 or else Nkind_In (Aexp, N_Explicit_Dereference, N_Function_Call)
473 then
474 Set_Analyzed (Aexp);
475 end if;
476 end Convert_To_PAT_Type;
478 -----------------------------------
479 -- Create_Packed_Array_Impl_Type --
480 -----------------------------------
482 procedure Create_Packed_Array_Impl_Type (Typ : Entity_Id) is
483 Loc : constant Source_Ptr := Sloc (Typ);
484 Ctyp : constant Entity_Id := Component_Type (Typ);
485 Csize : constant Uint := Component_Size (Typ);
487 Ancest : Entity_Id;
488 PB_Type : Entity_Id;
489 PASize : Uint;
490 Decl : Node_Id;
491 PAT : Entity_Id;
492 Len_Expr : Node_Id;
493 Len_Bits : Uint;
494 Bits_U1 : Node_Id;
495 PAT_High : Node_Id;
496 Btyp : Entity_Id;
497 Lit : Node_Id;
499 procedure Install_PAT;
500 -- This procedure is called with Decl set to the declaration for the
501 -- packed array type. It creates the type and installs it as required.
503 procedure Set_PB_Type;
504 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
505 -- requirements (see documentation in the spec of this package).
507 -----------------
508 -- Install_PAT --
509 -----------------
511 procedure Install_PAT is
512 Pushed_Scope : Boolean := False;
514 begin
515 -- We do not want to put the declaration we have created in the tree
516 -- since it is often hard, and sometimes impossible to find a proper
517 -- place for it (the impossible case arises for a packed array type
518 -- with bounds depending on the discriminant, a declaration cannot
519 -- be put inside the record, and the reference to the discriminant
520 -- cannot be outside the record).
522 -- The solution is to analyze the declaration while temporarily
523 -- attached to the tree at an appropriate point, and then we install
524 -- the resulting type as an Itype in the packed array type field of
525 -- the original type, so that no explicit declaration is required.
527 -- Note: the packed type is created in the scope of its parent type.
528 -- There are at least some cases where the current scope is deeper,
529 -- and so when this is the case, we temporarily reset the scope
530 -- for the definition. This is clearly safe, since the first use
531 -- of the packed array type will be the implicit reference from
532 -- the corresponding unpacked type when it is elaborated.
534 if Is_Itype (Typ) then
535 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
536 else
537 Set_Parent (Decl, Declaration_Node (Typ));
538 end if;
540 if Scope (Typ) /= Current_Scope then
541 Push_Scope (Scope (Typ));
542 Pushed_Scope := True;
543 end if;
545 Set_Is_Itype (PAT, True);
546 Set_Is_Packed_Array_Impl_Type (PAT, True);
547 Set_Packed_Array_Impl_Type (Typ, PAT);
548 Analyze (Decl, Suppress => All_Checks);
550 if Pushed_Scope then
551 Pop_Scope;
552 end if;
554 -- Set Esize and RM_Size to the actual size of the packed object
555 -- Do not reset RM_Size if already set, as happens in the case of
556 -- a modular type.
558 if Unknown_Esize (PAT) then
559 Set_Esize (PAT, PASize);
560 end if;
562 if Unknown_RM_Size (PAT) then
563 Set_RM_Size (PAT, PASize);
564 end if;
566 Adjust_Esize_Alignment (PAT);
568 -- Set remaining fields of packed array type
570 Init_Alignment (PAT);
571 Set_Parent (PAT, Empty);
572 Set_Associated_Node_For_Itype (PAT, Typ);
573 Set_Original_Array_Type (PAT, Typ);
575 -- Propagate representation aspects
577 Set_Is_Atomic (PAT, Is_Atomic (Typ));
578 Set_Is_Independent (PAT, Is_Independent (Typ));
579 Set_Is_Volatile (PAT, Is_Volatile (Typ));
580 Set_Is_Volatile_Full_Access (PAT, Is_Volatile_Full_Access (Typ));
581 Set_Treat_As_Volatile (PAT, Treat_As_Volatile (Typ));
583 -- For a non-bit-packed array, propagate reverse storage order
584 -- flag from original base type to packed array base type.
586 if not Is_Bit_Packed_Array (Typ) then
587 Set_Reverse_Storage_Order
588 (Etype (PAT), Reverse_Storage_Order (Base_Type (Typ)));
589 end if;
591 -- We definitely do not want to delay freezing for packed array
592 -- types. This is of particular importance for the itypes that are
593 -- generated for record components depending on discriminants where
594 -- there is no place to put the freeze node.
596 Set_Has_Delayed_Freeze (PAT, False);
597 Set_Has_Delayed_Freeze (Etype (PAT), False);
599 -- If we did allocate a freeze node, then clear out the reference
600 -- since it is obsolete (should we delete the freeze node???)
602 Set_Freeze_Node (PAT, Empty);
603 Set_Freeze_Node (Etype (PAT), Empty);
604 end Install_PAT;
606 -----------------
607 -- Set_PB_Type --
608 -----------------
610 procedure Set_PB_Type is
611 begin
612 -- If the user has specified an explicit alignment for the
613 -- type or component, take it into account.
615 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
616 or else Alignment (Typ) = 1
617 or else Component_Alignment (Typ) = Calign_Storage_Unit
618 then
619 PB_Type := RTE (RE_Packed_Bytes1);
621 elsif Csize mod 4 /= 0
622 or else Alignment (Typ) = 2
623 then
624 PB_Type := RTE (RE_Packed_Bytes2);
626 else
627 PB_Type := RTE (RE_Packed_Bytes4);
628 end if;
629 end Set_PB_Type;
631 -- Start of processing for Create_Packed_Array_Impl_Type
633 begin
634 -- If we already have a packed array type, nothing to do
636 if Present (Packed_Array_Impl_Type (Typ)) then
637 return;
638 end if;
640 -- If our immediate ancestor subtype is constrained, and it already
641 -- has a packed array type, then just share the same type, since the
642 -- bounds must be the same. If the ancestor is not an array type but
643 -- a private type, as can happen with multiple instantiations, create
644 -- a new packed type, to avoid privacy issues.
646 if Ekind (Typ) = E_Array_Subtype then
647 Ancest := Ancestor_Subtype (Typ);
649 if Present (Ancest)
650 and then Is_Array_Type (Ancest)
651 and then Is_Constrained (Ancest)
652 and then Present (Packed_Array_Impl_Type (Ancest))
653 then
654 Set_Packed_Array_Impl_Type (Typ, Packed_Array_Impl_Type (Ancest));
655 return;
656 end if;
657 end if;
659 -- We preset the result type size from the size of the original array
660 -- type, since this size clearly belongs to the packed array type. The
661 -- size of the conceptual unpacked type is always set to unknown.
663 PASize := RM_Size (Typ);
665 -- Case of an array where at least one index is of an enumeration
666 -- type with a non-standard representation, but the component size
667 -- is not appropriate for bit packing. This is the case where we
668 -- have Is_Packed set (we would never be in this unit otherwise),
669 -- but Is_Bit_Packed_Array is false.
671 -- Note that if the component size is appropriate for bit packing,
672 -- then the circuit for the computation of the subscript properly
673 -- deals with the non-standard enumeration type case by taking the
674 -- Pos anyway.
676 if not Is_Bit_Packed_Array (Typ) then
678 -- Here we build a declaration:
680 -- type tttP is array (index1, index2, ...) of component_type
682 -- where index1, index2, are the index types. These are the same
683 -- as the index types of the original array, except for the non-
684 -- standard representation enumeration type case, where we have
685 -- two subcases.
687 -- For the unconstrained array case, we use
689 -- Natural range <>
691 -- For the constrained case, we use
693 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
694 -- Enum_Type'Pos (Enum_Type'Last);
696 -- Note that tttP is created even if no index subtype is a non
697 -- standard enumeration, because we still need to remove padding
698 -- normally inserted for component alignment.
700 PAT :=
701 Make_Defining_Identifier (Loc,
702 Chars => New_External_Name (Chars (Typ), 'P'));
704 declare
705 Indexes : constant List_Id := New_List;
706 Indx : Node_Id;
707 Indx_Typ : Entity_Id;
708 Enum_Case : Boolean;
709 Typedef : Node_Id;
711 begin
712 Indx := First_Index (Typ);
714 while Present (Indx) loop
715 Indx_Typ := Etype (Indx);
717 Enum_Case := Is_Enumeration_Type (Indx_Typ)
718 and then Has_Non_Standard_Rep (Indx_Typ);
720 -- Unconstrained case
722 if not Is_Constrained (Typ) then
723 if Enum_Case then
724 Indx_Typ := Standard_Natural;
725 end if;
727 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
729 -- Constrained case
731 else
732 if not Enum_Case then
733 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
735 else
736 Append_To (Indexes,
737 Make_Subtype_Indication (Loc,
738 Subtype_Mark =>
739 New_Occurrence_Of (Standard_Natural, Loc),
740 Constraint =>
741 Make_Range_Constraint (Loc,
742 Range_Expression =>
743 Make_Range (Loc,
744 Low_Bound =>
745 Make_Attribute_Reference (Loc,
746 Prefix =>
747 New_Occurrence_Of (Indx_Typ, Loc),
748 Attribute_Name => Name_Pos,
749 Expressions => New_List (
750 Make_Attribute_Reference (Loc,
751 Prefix =>
752 New_Occurrence_Of (Indx_Typ, Loc),
753 Attribute_Name => Name_First))),
755 High_Bound =>
756 Make_Attribute_Reference (Loc,
757 Prefix =>
758 New_Occurrence_Of (Indx_Typ, Loc),
759 Attribute_Name => Name_Pos,
760 Expressions => New_List (
761 Make_Attribute_Reference (Loc,
762 Prefix =>
763 New_Occurrence_Of (Indx_Typ, Loc),
764 Attribute_Name => Name_Last)))))));
766 end if;
767 end if;
769 Next_Index (Indx);
770 end loop;
772 if not Is_Constrained (Typ) then
773 Typedef :=
774 Make_Unconstrained_Array_Definition (Loc,
775 Subtype_Marks => Indexes,
776 Component_Definition =>
777 Make_Component_Definition (Loc,
778 Aliased_Present => False,
779 Subtype_Indication =>
780 New_Occurrence_Of (Ctyp, Loc)));
782 else
783 Typedef :=
784 Make_Constrained_Array_Definition (Loc,
785 Discrete_Subtype_Definitions => Indexes,
786 Component_Definition =>
787 Make_Component_Definition (Loc,
788 Aliased_Present => False,
789 Subtype_Indication =>
790 New_Occurrence_Of (Ctyp, Loc)));
791 end if;
793 Decl :=
794 Make_Full_Type_Declaration (Loc,
795 Defining_Identifier => PAT,
796 Type_Definition => Typedef);
797 end;
799 Install_PAT;
800 return;
802 -- Case of bit-packing required for unconstrained array. We create
803 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
805 elsif not Is_Constrained (Typ) then
807 -- When generating standard DWARF (i.e when GNAT_Encodings is
808 -- DWARF_GNAT_Encodings_Minimal), the ___XP suffix will be stripped
809 -- by the back-end but generate it anyway to ease compiler debugging.
810 -- This will help to distinguish implementation types from original
811 -- packed arrays.
813 PAT :=
814 Make_Defining_Identifier (Loc,
815 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
817 Set_PB_Type;
819 Decl :=
820 Make_Subtype_Declaration (Loc,
821 Defining_Identifier => PAT,
822 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
824 Install_PAT;
825 return;
827 -- Remaining code is for the case of bit-packing for constrained array
829 -- The name of the packed array subtype is
831 -- ttt___XPsss
833 -- where sss is the component size in bits and ttt is the name of
834 -- the parent packed type.
836 else
837 PAT :=
838 Make_Defining_Identifier (Loc,
839 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
841 -- Build an expression for the length of the array in bits.
842 -- This is the product of the length of each of the dimensions
844 Len_Expr := Compute_Number_Components (Typ, Typ);
846 -- Temporarily attach the length expression to the tree and analyze
847 -- and resolve it, so that we can test its value. We assume that the
848 -- total length fits in type Integer. This expression may involve
849 -- discriminants, so we treat it as a default/per-object expression.
851 Set_Parent (Len_Expr, Typ);
852 Preanalyze_Spec_Expression (Len_Expr, Standard_Long_Long_Integer);
854 -- Use a modular type if possible. We can do this if we have
855 -- static bounds, and the length is small enough, and the length
856 -- is not zero. We exclude the zero length case because the size
857 -- of things is always at least one, and the zero length object
858 -- would have an anomalous size.
860 if Compile_Time_Known_Value (Len_Expr) then
861 Len_Bits := Expr_Value (Len_Expr) * Csize;
863 -- Check for size known to be too large
865 if Len_Bits >
866 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
867 then
868 if System_Storage_Unit = 8 then
869 Error_Msg_N
870 ("packed array size cannot exceed " &
871 "Integer''Last bytes", Typ);
872 else
873 Error_Msg_N
874 ("packed array size cannot exceed " &
875 "Integer''Last storage units", Typ);
876 end if;
878 -- Reset length to arbitrary not too high value to continue
880 Len_Expr := Make_Integer_Literal (Loc, 65535);
881 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
882 end if;
884 -- We normally consider small enough to mean no larger than the
885 -- value of System_Max_Binary_Modulus_Power, checking that in the
886 -- case of values longer than word size, we have long shifts.
888 if Len_Bits > 0
889 and then
890 (Len_Bits <= System_Word_Size
891 or else (Len_Bits <= System_Max_Binary_Modulus_Power
892 and then Support_Long_Shifts_On_Target))
893 then
894 -- We can use the modular type, it has the form:
896 -- subtype tttPn is btyp
897 -- range 0 .. 2 ** ((Typ'Length (1)
898 -- * ... * Typ'Length (n)) * Csize) - 1;
900 -- The bounds are statically known, and btyp is one of the
901 -- unsigned types, depending on the length.
903 if Len_Bits <= Standard_Short_Short_Integer_Size then
904 Btyp := RTE (RE_Short_Short_Unsigned);
906 elsif Len_Bits <= Standard_Short_Integer_Size then
907 Btyp := RTE (RE_Short_Unsigned);
909 elsif Len_Bits <= Standard_Integer_Size then
910 Btyp := RTE (RE_Unsigned);
912 elsif Len_Bits <= Standard_Long_Integer_Size then
913 Btyp := RTE (RE_Long_Unsigned);
915 else
916 Btyp := RTE (RE_Long_Long_Unsigned);
917 end if;
919 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
920 Set_Print_In_Hex (Lit);
922 Decl :=
923 Make_Subtype_Declaration (Loc,
924 Defining_Identifier => PAT,
925 Subtype_Indication =>
926 Make_Subtype_Indication (Loc,
927 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
929 Constraint =>
930 Make_Range_Constraint (Loc,
931 Range_Expression =>
932 Make_Range (Loc,
933 Low_Bound =>
934 Make_Integer_Literal (Loc, 0),
935 High_Bound => Lit))));
937 if PASize = Uint_0 then
938 PASize := Len_Bits;
939 end if;
941 Install_PAT;
943 -- Propagate a given alignment to the modular type. This can
944 -- cause it to be under-aligned, but that's OK.
946 if Present (Alignment_Clause (Typ)) then
947 Set_Alignment (PAT, Alignment (Typ));
948 end if;
950 return;
951 end if;
952 end if;
954 -- Could not use a modular type, for all other cases, we build
955 -- a packed array subtype:
957 -- subtype tttPn is
958 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
960 -- Bits is the length of the array in bits
962 Set_PB_Type;
964 Bits_U1 :=
965 Make_Op_Add (Loc,
966 Left_Opnd =>
967 Make_Op_Multiply (Loc,
968 Left_Opnd =>
969 Make_Integer_Literal (Loc, Csize),
970 Right_Opnd => Len_Expr),
972 Right_Opnd =>
973 Make_Integer_Literal (Loc, 7));
975 Set_Paren_Count (Bits_U1, 1);
977 PAT_High :=
978 Make_Op_Subtract (Loc,
979 Left_Opnd =>
980 Make_Op_Divide (Loc,
981 Left_Opnd => Bits_U1,
982 Right_Opnd => Make_Integer_Literal (Loc, 8)),
983 Right_Opnd => Make_Integer_Literal (Loc, 1));
985 Decl :=
986 Make_Subtype_Declaration (Loc,
987 Defining_Identifier => PAT,
988 Subtype_Indication =>
989 Make_Subtype_Indication (Loc,
990 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
991 Constraint =>
992 Make_Index_Or_Discriminant_Constraint (Loc,
993 Constraints => New_List (
994 Make_Range (Loc,
995 Low_Bound =>
996 Make_Integer_Literal (Loc, 0),
997 High_Bound =>
998 Convert_To (Standard_Integer, PAT_High))))));
1000 Install_PAT;
1002 -- Currently the code in this unit requires that packed arrays
1003 -- represented by non-modular arrays of bytes be on a byte
1004 -- boundary for bit sizes handled by System.Pack_nn units.
1005 -- That's because these units assume the array being accessed
1006 -- starts on a byte boundary.
1008 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1009 Set_Must_Be_On_Byte_Boundary (Typ);
1010 end if;
1011 end if;
1012 end Create_Packed_Array_Impl_Type;
1014 -----------------------------------
1015 -- Expand_Bit_Packed_Element_Set --
1016 -----------------------------------
1018 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1019 Loc : constant Source_Ptr := Sloc (N);
1020 Lhs : constant Node_Id := Name (N);
1022 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1023 -- Used to preserve assignment OK status when assignment is rewritten
1025 Rhs : Node_Id := Expression (N);
1026 -- Initially Rhs is the right hand side value, it will be replaced
1027 -- later by an appropriate unchecked conversion for the assignment.
1029 Obj : Node_Id;
1030 Atyp : Entity_Id;
1031 PAT : Entity_Id;
1032 Ctyp : Entity_Id;
1033 Csiz : Int;
1034 Cmask : Uint;
1036 Shift : Node_Id;
1037 -- The expression for the shift value that is required
1039 Shift_Used : Boolean := False;
1040 -- Set True if Shift has been used in the generated code at least once,
1041 -- so that it must be duplicated if used again.
1043 New_Lhs : Node_Id;
1044 New_Rhs : Node_Id;
1046 Rhs_Val_Known : Boolean;
1047 Rhs_Val : Uint;
1048 -- If the value of the right hand side as an integer constant is
1049 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1050 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1051 -- the Rhs_Val is undefined.
1053 function Get_Shift return Node_Id;
1054 -- Function used to get the value of Shift, making sure that it
1055 -- gets duplicated if the function is called more than once.
1057 ---------------
1058 -- Get_Shift --
1059 ---------------
1061 function Get_Shift return Node_Id is
1062 begin
1063 -- If we used the shift value already, then duplicate it. We
1064 -- set a temporary parent in case actions have to be inserted.
1066 if Shift_Used then
1067 Set_Parent (Shift, N);
1068 return Duplicate_Subexpr_No_Checks (Shift);
1070 -- If first time, use Shift unchanged, and set flag for first use
1072 else
1073 Shift_Used := True;
1074 return Shift;
1075 end if;
1076 end Get_Shift;
1078 -- Start of processing for Expand_Bit_Packed_Element_Set
1080 begin
1081 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1083 Obj := Relocate_Node (Prefix (Lhs));
1084 Convert_To_Actual_Subtype (Obj);
1085 Atyp := Etype (Obj);
1086 PAT := Packed_Array_Impl_Type (Atyp);
1087 Ctyp := Component_Type (Atyp);
1088 Csiz := UI_To_Int (Component_Size (Atyp));
1090 -- We remove side effects, in case the rhs modifies the lhs, because we
1091 -- are about to transform the rhs into an expression that first READS
1092 -- the lhs, so we can do the necessary shifting and masking. Example:
1093 -- "X(2) := F(...);" where F modifies X(3). Otherwise, the side effect
1094 -- will be lost.
1096 Remove_Side_Effects (Rhs);
1098 -- We convert the right hand side to the proper subtype to ensure
1099 -- that an appropriate range check is made (since the normal range
1100 -- check from assignment will be lost in the transformations). This
1101 -- conversion is analyzed immediately so that subsequent processing
1102 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1104 -- If the right-hand side is a string literal, create a temporary for
1105 -- it, constant-folding is not ready to wrap the bit representation
1106 -- of a string literal.
1108 if Nkind (Rhs) = N_String_Literal then
1109 declare
1110 Decl : Node_Id;
1111 begin
1112 Decl :=
1113 Make_Object_Declaration (Loc,
1114 Defining_Identifier => Make_Temporary (Loc, 'T', Rhs),
1115 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1116 Expression => New_Copy_Tree (Rhs));
1118 Insert_Actions (N, New_List (Decl));
1119 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1120 end;
1121 end if;
1123 Rhs := Convert_To (Ctyp, Rhs);
1124 Set_Parent (Rhs, N);
1126 -- If we are building the initialization procedure for a packed array,
1127 -- and Initialize_Scalars is enabled, each component assignment is an
1128 -- out-of-range value by design. Compile this value without checks,
1129 -- because a call to the array init_proc must not raise an exception.
1131 -- Condition is not consistent with description above, Within_Init_Proc
1132 -- is True also when we are building the IP for a record or protected
1133 -- type that has a packed array component???
1135 if Within_Init_Proc
1136 and then Initialize_Scalars
1137 then
1138 Analyze_And_Resolve (Rhs, Ctyp, Suppress => All_Checks);
1139 else
1140 Analyze_And_Resolve (Rhs, Ctyp);
1141 end if;
1143 -- Case of component size 1,2,4 or any component size for the modular
1144 -- case. These are the cases for which we can inline the code.
1146 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1147 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1148 then
1149 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1151 -- The statement to be generated is:
1153 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, Shift)))
1155 -- or in the case of a freestanding Reverse_Storage_Order object,
1157 -- Obj := Swap (atyp!((Swap (Obj) and Mask1)
1158 -- or (shift_left (rhs, Shift))))
1160 -- where Mask1 is obtained by shifting Cmask left Shift bits
1161 -- and then complementing the result.
1163 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1165 -- the "or ..." is omitted if rhs is constant and all 0 bits
1167 -- rhs is converted to the appropriate type
1169 -- The result is converted back to the array type, since
1170 -- otherwise we lose knowledge of the packed nature.
1172 -- Determine if right side is all 0 bits or all 1 bits
1174 if Compile_Time_Known_Value (Rhs) then
1175 Rhs_Val := Expr_Rep_Value (Rhs);
1176 Rhs_Val_Known := True;
1178 -- The following test catches the case of an unchecked conversion of
1179 -- an integer literal. This results from optimizing aggregates of
1180 -- packed types.
1182 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1183 and then Compile_Time_Known_Value (Expression (Rhs))
1184 then
1185 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1186 Rhs_Val_Known := True;
1188 else
1189 Rhs_Val := No_Uint;
1190 Rhs_Val_Known := False;
1191 end if;
1193 -- Some special checks for the case where the right hand value is
1194 -- known at compile time. Basically we have to take care of the
1195 -- implicit conversion to the subtype of the component object.
1197 if Rhs_Val_Known then
1199 -- If we have a biased component type then we must manually do the
1200 -- biasing, since we are taking responsibility in this case for
1201 -- constructing the exact bit pattern to be used.
1203 if Has_Biased_Representation (Ctyp) then
1204 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1205 end if;
1207 -- For a negative value, we manually convert the two's complement
1208 -- value to a corresponding unsigned value, so that the proper
1209 -- field width is maintained. If we did not do this, we would
1210 -- get too many leading sign bits later on.
1212 if Rhs_Val < 0 then
1213 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1214 end if;
1215 end if;
1217 -- Now create copies removing side effects. Note that in some complex
1218 -- cases, this may cause the fact that we have already set a packed
1219 -- array type on Obj to get lost. So we save the type of Obj, and
1220 -- make sure it is reset properly.
1222 New_Lhs := Duplicate_Subexpr (Obj, Name_Req => True);
1223 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1225 -- First we deal with the "and"
1227 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1228 declare
1229 Mask1 : Node_Id;
1230 Lit : Node_Id;
1232 begin
1233 if Compile_Time_Known_Value (Shift) then
1234 Mask1 :=
1235 Make_Integer_Literal (Loc,
1236 Modulus (Etype (Obj)) - 1 -
1237 (Cmask * (2 ** Expr_Value (Get_Shift))));
1238 Set_Print_In_Hex (Mask1);
1240 else
1241 Lit := Make_Integer_Literal (Loc, Cmask);
1242 Set_Print_In_Hex (Lit);
1243 Mask1 :=
1244 Make_Op_Not (Loc,
1245 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1246 end if;
1248 New_Rhs :=
1249 Make_Op_And (Loc,
1250 Left_Opnd => New_Rhs,
1251 Right_Opnd => Mask1);
1252 end;
1253 end if;
1255 -- Then deal with the "or"
1257 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1258 declare
1259 Or_Rhs : Node_Id;
1261 procedure Fixup_Rhs;
1262 -- Adjust Rhs by bias if biased representation for components
1263 -- or remove extraneous high order sign bits if signed.
1265 procedure Fixup_Rhs is
1266 Etyp : constant Entity_Id := Etype (Rhs);
1268 begin
1269 -- For biased case, do the required biasing by simply
1270 -- converting to the biased subtype (the conversion
1271 -- will generate the required bias).
1273 if Has_Biased_Representation (Ctyp) then
1274 Rhs := Convert_To (Ctyp, Rhs);
1276 -- For a signed integer type that is not biased, generate
1277 -- a conversion to unsigned to strip high order sign bits.
1279 elsif Is_Signed_Integer_Type (Ctyp) then
1280 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1281 end if;
1283 -- Set Etype, since it can be referenced before the node is
1284 -- completely analyzed.
1286 Set_Etype (Rhs, Etyp);
1288 -- We now need to do an unchecked conversion of the
1289 -- result to the target type, but it is important that
1290 -- this conversion be a right justified conversion and
1291 -- not a left justified conversion.
1293 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1294 end Fixup_Rhs;
1296 begin
1297 if Rhs_Val_Known
1298 and then Compile_Time_Known_Value (Get_Shift)
1299 then
1300 Or_Rhs :=
1301 Make_Integer_Literal (Loc,
1302 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1303 Set_Print_In_Hex (Or_Rhs);
1305 else
1306 -- We have to convert the right hand side to Etype (Obj).
1307 -- A special case arises if what we have now is a Val
1308 -- attribute reference whose expression type is Etype (Obj).
1309 -- This happens for assignments of fields from the same
1310 -- array. In this case we get the required right hand side
1311 -- by simply removing the inner attribute reference.
1313 if Nkind (Rhs) = N_Attribute_Reference
1314 and then Attribute_Name (Rhs) = Name_Val
1315 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1316 then
1317 Rhs := Relocate_Node (First (Expressions (Rhs)));
1318 Fixup_Rhs;
1320 -- If the value of the right hand side is a known integer
1321 -- value, then just replace it by an untyped constant,
1322 -- which will be properly retyped when we analyze and
1323 -- resolve the expression.
1325 elsif Rhs_Val_Known then
1327 -- Note that Rhs_Val has already been normalized to
1328 -- be an unsigned value with the proper number of bits.
1330 Rhs := Make_Integer_Literal (Loc, Rhs_Val);
1332 -- Otherwise we need an unchecked conversion
1334 else
1335 Fixup_Rhs;
1336 end if;
1338 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1339 end if;
1341 if Nkind (New_Rhs) = N_Op_And then
1342 Set_Paren_Count (New_Rhs, 1);
1343 Set_Etype (New_Rhs, Etype (Left_Opnd (New_Rhs)));
1344 end if;
1346 New_Rhs :=
1347 Make_Op_Or (Loc,
1348 Left_Opnd => New_Rhs,
1349 Right_Opnd => Or_Rhs);
1350 end;
1351 end if;
1353 -- Now do the rewrite
1355 Rewrite (N,
1356 Make_Assignment_Statement (Loc,
1357 Name => New_Lhs,
1358 Expression =>
1359 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1360 Set_Assignment_OK (Name (N), Ass_OK);
1362 -- All other component sizes for non-modular case
1364 else
1365 -- We generate
1367 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1369 -- where Subscr is the computed linear subscript
1371 declare
1372 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1373 Set_nn : Entity_Id;
1374 Subscr : Node_Id;
1375 Atyp : Entity_Id;
1376 Rev_SSO : Node_Id;
1378 begin
1379 if No (Bits_nn) then
1381 -- Error, most likely High_Integrity_Mode restriction
1383 return;
1384 end if;
1386 -- Acquire proper Set entity. We use the aligned or unaligned
1387 -- case as appropriate.
1389 if Known_Aligned_Enough (Obj, Csiz) then
1390 Set_nn := RTE (Set_Id (Csiz));
1391 else
1392 Set_nn := RTE (SetU_Id (Csiz));
1393 end if;
1395 -- Now generate the set reference
1397 Obj := Relocate_Node (Prefix (Lhs));
1398 Convert_To_Actual_Subtype (Obj);
1399 Atyp := Etype (Obj);
1400 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1402 -- Set indication of whether the packed array has reverse SSO
1404 Rev_SSO :=
1405 New_Occurrence_Of
1406 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1408 -- Below we must make the assumption that Obj is
1409 -- at least byte aligned, since otherwise its address
1410 -- cannot be taken. The assumption holds since the
1411 -- only arrays that can be misaligned are small packed
1412 -- arrays which are implemented as a modular type, and
1413 -- that is not the case here.
1415 Rewrite (N,
1416 Make_Procedure_Call_Statement (Loc,
1417 Name => New_Occurrence_Of (Set_nn, Loc),
1418 Parameter_Associations => New_List (
1419 Make_Attribute_Reference (Loc,
1420 Prefix => Obj,
1421 Attribute_Name => Name_Address),
1422 Subscr,
1423 Unchecked_Convert_To (Bits_nn, Convert_To (Ctyp, Rhs)),
1424 Rev_SSO)));
1426 end;
1427 end if;
1429 Analyze (N, Suppress => All_Checks);
1430 end Expand_Bit_Packed_Element_Set;
1432 -------------------------------------
1433 -- Expand_Packed_Address_Reference --
1434 -------------------------------------
1436 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1437 Loc : constant Source_Ptr := Sloc (N);
1438 Base : Node_Id;
1439 Offset : Node_Id;
1441 begin
1442 -- We build an expression that has the form
1444 -- outer_object'Address
1445 -- + (linear-subscript * component_size for each array reference
1446 -- + field'Bit_Position for each record field
1447 -- + ...
1448 -- + ...) / Storage_Unit;
1450 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1452 Rewrite (N,
1453 Unchecked_Convert_To (RTE (RE_Address),
1454 Make_Op_Add (Loc,
1455 Left_Opnd =>
1456 Unchecked_Convert_To (RTE (RE_Integer_Address),
1457 Make_Attribute_Reference (Loc,
1458 Prefix => Base,
1459 Attribute_Name => Name_Address)),
1461 Right_Opnd =>
1462 Unchecked_Convert_To (RTE (RE_Integer_Address),
1463 Make_Op_Divide (Loc,
1464 Left_Opnd => Offset,
1465 Right_Opnd =>
1466 Make_Integer_Literal (Loc, System_Storage_Unit))))));
1468 Analyze_And_Resolve (N, RTE (RE_Address));
1469 end Expand_Packed_Address_Reference;
1471 ---------------------------------
1472 -- Expand_Packed_Bit_Reference --
1473 ---------------------------------
1475 procedure Expand_Packed_Bit_Reference (N : Node_Id) is
1476 Loc : constant Source_Ptr := Sloc (N);
1477 Base : Node_Id;
1478 Offset : Node_Id;
1480 begin
1481 -- We build an expression that has the form
1483 -- (linear-subscript * component_size for each array reference
1484 -- + field'Bit_Position for each record field
1485 -- + ...
1486 -- + ...) mod Storage_Unit;
1488 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1490 Rewrite (N,
1491 Unchecked_Convert_To (Universal_Integer,
1492 Make_Op_Mod (Loc,
1493 Left_Opnd => Offset,
1494 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
1496 Analyze_And_Resolve (N, Universal_Integer);
1497 end Expand_Packed_Bit_Reference;
1499 ------------------------------------
1500 -- Expand_Packed_Boolean_Operator --
1501 ------------------------------------
1503 -- This routine expands "a op b" for the packed cases
1505 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1506 Loc : constant Source_Ptr := Sloc (N);
1507 Typ : constant Entity_Id := Etype (N);
1508 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1509 R : Node_Id := Relocate_Node (Right_Opnd (N));
1511 Ltyp : Entity_Id;
1512 Rtyp : Entity_Id;
1513 PAT : Entity_Id;
1515 begin
1516 Convert_To_Actual_Subtype (L);
1517 Convert_To_Actual_Subtype (R);
1519 Ensure_Defined (Etype (L), N);
1520 Ensure_Defined (Etype (R), N);
1522 Apply_Length_Check (R, Etype (L));
1524 Ltyp := Etype (L);
1525 Rtyp := Etype (R);
1527 -- Deal with silly case of XOR where the subcomponent has a range
1528 -- True .. True where an exception must be raised.
1530 if Nkind (N) = N_Op_Xor then
1531 R := Duplicate_Subexpr (R);
1532 Silly_Boolean_Array_Xor_Test (N, R, Rtyp);
1533 end if;
1535 -- Now that that silliness is taken care of, get packed array type
1537 Convert_To_PAT_Type (L);
1538 Convert_To_PAT_Type (R);
1540 PAT := Etype (L);
1542 -- For the modular case, we expand a op b into
1544 -- rtyp!(pat!(a) op pat!(b))
1546 -- where rtyp is the Etype of the left operand. Note that we do not
1547 -- convert to the base type, since this would be unconstrained, and
1548 -- hence not have a corresponding packed array type set.
1550 -- Note that both operands must be modular for this code to be used
1552 if Is_Modular_Integer_Type (PAT)
1553 and then
1554 Is_Modular_Integer_Type (Etype (R))
1555 then
1556 declare
1557 P : Node_Id;
1559 begin
1560 if Nkind (N) = N_Op_And then
1561 P := Make_Op_And (Loc, L, R);
1563 elsif Nkind (N) = N_Op_Or then
1564 P := Make_Op_Or (Loc, L, R);
1566 else -- Nkind (N) = N_Op_Xor
1567 P := Make_Op_Xor (Loc, L, R);
1568 end if;
1570 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1571 end;
1573 -- For the array case, we insert the actions
1575 -- Result : Ltype;
1577 -- System.Bit_Ops.Bit_And/Or/Xor
1578 -- (Left'Address,
1579 -- Ltype'Length * Ltype'Component_Size;
1580 -- Right'Address,
1581 -- Rtype'Length * Rtype'Component_Size
1582 -- Result'Address);
1584 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1585 -- the second argument and fourth arguments are the lengths of the
1586 -- operands in bits. Then we replace the expression by a reference
1587 -- to Result.
1589 -- Note that if we are mixing a modular and array operand, everything
1590 -- works fine, since we ensure that the modular representation has the
1591 -- same physical layout as the array representation (that's what the
1592 -- left justified modular stuff in the big-endian case is about).
1594 else
1595 declare
1596 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
1597 E_Id : RE_Id;
1599 begin
1600 if Nkind (N) = N_Op_And then
1601 E_Id := RE_Bit_And;
1603 elsif Nkind (N) = N_Op_Or then
1604 E_Id := RE_Bit_Or;
1606 else -- Nkind (N) = N_Op_Xor
1607 E_Id := RE_Bit_Xor;
1608 end if;
1610 Insert_Actions (N, New_List (
1612 Make_Object_Declaration (Loc,
1613 Defining_Identifier => Result_Ent,
1614 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1616 Make_Procedure_Call_Statement (Loc,
1617 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1618 Parameter_Associations => New_List (
1620 Make_Byte_Aligned_Attribute_Reference (Loc,
1621 Prefix => L,
1622 Attribute_Name => Name_Address),
1624 Make_Op_Multiply (Loc,
1625 Left_Opnd =>
1626 Make_Attribute_Reference (Loc,
1627 Prefix =>
1628 New_Occurrence_Of
1629 (Etype (First_Index (Ltyp)), Loc),
1630 Attribute_Name => Name_Range_Length),
1632 Right_Opnd =>
1633 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1635 Make_Byte_Aligned_Attribute_Reference (Loc,
1636 Prefix => R,
1637 Attribute_Name => Name_Address),
1639 Make_Op_Multiply (Loc,
1640 Left_Opnd =>
1641 Make_Attribute_Reference (Loc,
1642 Prefix =>
1643 New_Occurrence_Of
1644 (Etype (First_Index (Rtyp)), Loc),
1645 Attribute_Name => Name_Range_Length),
1647 Right_Opnd =>
1648 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1650 Make_Byte_Aligned_Attribute_Reference (Loc,
1651 Prefix => New_Occurrence_Of (Result_Ent, Loc),
1652 Attribute_Name => Name_Address)))));
1654 Rewrite (N,
1655 New_Occurrence_Of (Result_Ent, Loc));
1656 end;
1657 end if;
1659 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1660 end Expand_Packed_Boolean_Operator;
1662 -------------------------------------
1663 -- Expand_Packed_Element_Reference --
1664 -------------------------------------
1666 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1667 Loc : constant Source_Ptr := Sloc (N);
1668 Obj : Node_Id;
1669 Atyp : Entity_Id;
1670 PAT : Entity_Id;
1671 Ctyp : Entity_Id;
1672 Csiz : Int;
1673 Shift : Node_Id;
1674 Cmask : Uint;
1675 Lit : Node_Id;
1676 Arg : Node_Id;
1678 begin
1679 -- If the node is an actual in a call, the prefix has not been fully
1680 -- expanded, to account for the additional expansion for in-out actuals
1681 -- (see expand_actuals for details). If the prefix itself is a packed
1682 -- reference as well, we have to recurse to complete the transformation
1683 -- of the prefix.
1685 if Nkind (Prefix (N)) = N_Indexed_Component
1686 and then not Analyzed (Prefix (N))
1687 and then Is_Bit_Packed_Array (Etype (Prefix (Prefix (N))))
1688 then
1689 Expand_Packed_Element_Reference (Prefix (N));
1690 end if;
1692 -- The prefix may be rewritten below as a conversion. If it is a source
1693 -- entity generate reference to it now, to prevent spurious warnings
1694 -- about unused entities.
1696 if Is_Entity_Name (Prefix (N))
1697 and then Comes_From_Source (Prefix (N))
1698 then
1699 Generate_Reference (Entity (Prefix (N)), Prefix (N), 'r');
1700 end if;
1702 -- If not bit packed, we have the enumeration case, which is easily
1703 -- dealt with (just adjust the subscripts of the indexed component)
1705 -- Note: this leaves the result as an indexed component, which is
1706 -- still a variable, so can be used in the assignment case, as is
1707 -- required in the enumeration case.
1709 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1710 Setup_Enumeration_Packed_Array_Reference (N);
1711 return;
1712 end if;
1714 -- Remaining processing is for the bit-packed case
1716 Obj := Relocate_Node (Prefix (N));
1717 Convert_To_Actual_Subtype (Obj);
1718 Atyp := Etype (Obj);
1719 PAT := Packed_Array_Impl_Type (Atyp);
1720 Ctyp := Component_Type (Atyp);
1721 Csiz := UI_To_Int (Component_Size (Atyp));
1723 -- Case of component size 1,2,4 or any component size for the modular
1724 -- case. These are the cases for which we can inline the code.
1726 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1727 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1728 then
1729 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1730 Lit := Make_Integer_Literal (Loc, Cmask);
1731 Set_Print_In_Hex (Lit);
1733 -- We generate a shift right to position the field, followed by a
1734 -- masking operation to extract the bit field, and we finally do an
1735 -- unchecked conversion to convert the result to the required target.
1737 -- Note that the unchecked conversion automatically deals with the
1738 -- bias if we are dealing with a biased representation. What will
1739 -- happen is that we temporarily generate the biased representation,
1740 -- but almost immediately that will be converted to the original
1741 -- unbiased component type, and the bias will disappear.
1743 Arg :=
1744 Make_Op_And (Loc,
1745 Left_Opnd => Make_Shift_Right (Obj, Shift),
1746 Right_Opnd => Lit);
1747 Set_Etype (Arg, Ctyp);
1749 -- Component extraction is performed on a native endianness scalar
1750 -- value: if Atyp has reverse storage order, then it has been byte
1751 -- swapped, and if the component being extracted is itself of a
1752 -- composite type with reverse storage order, then we need to swap
1753 -- it back to its expected endianness after extraction.
1755 if Reverse_Storage_Order (Atyp)
1756 and then (Is_Record_Type (Ctyp) or else Is_Array_Type (Ctyp))
1757 and then Reverse_Storage_Order (Ctyp)
1758 then
1759 Arg := Revert_Storage_Order (Arg);
1760 end if;
1762 -- We needed to analyze this before we do the unchecked convert
1763 -- below, but we need it temporarily attached to the tree for
1764 -- this analysis (hence the temporary Set_Parent call).
1766 Set_Parent (Arg, Parent (N));
1767 Analyze_And_Resolve (Arg);
1769 Rewrite (N, RJ_Unchecked_Convert_To (Ctyp, Arg));
1771 -- All other component sizes for non-modular case
1773 else
1774 -- We generate
1776 -- Component_Type!(Get_nn (Arr'address, Subscr))
1778 -- where Subscr is the computed linear subscript
1780 declare
1781 Get_nn : Entity_Id;
1782 Subscr : Node_Id;
1783 Rev_SSO : constant Node_Id :=
1784 New_Occurrence_Of
1785 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1787 begin
1788 -- Acquire proper Get entity. We use the aligned or unaligned
1789 -- case as appropriate.
1791 if Known_Aligned_Enough (Obj, Csiz) then
1792 Get_nn := RTE (Get_Id (Csiz));
1793 else
1794 Get_nn := RTE (GetU_Id (Csiz));
1795 end if;
1797 -- Now generate the get reference
1799 Compute_Linear_Subscript (Atyp, N, Subscr);
1801 -- Below we make the assumption that Obj is at least byte
1802 -- aligned, since otherwise its address cannot be taken.
1803 -- The assumption holds since the only arrays that can be
1804 -- misaligned are small packed arrays which are implemented
1805 -- as a modular type, and that is not the case here.
1807 Rewrite (N,
1808 Unchecked_Convert_To (Ctyp,
1809 Make_Function_Call (Loc,
1810 Name => New_Occurrence_Of (Get_nn, Loc),
1811 Parameter_Associations => New_List (
1812 Make_Attribute_Reference (Loc,
1813 Prefix => Obj,
1814 Attribute_Name => Name_Address),
1815 Subscr,
1816 Rev_SSO))));
1817 end;
1818 end if;
1820 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1821 end Expand_Packed_Element_Reference;
1823 ----------------------
1824 -- Expand_Packed_Eq --
1825 ----------------------
1827 -- Handles expansion of "=" on packed array types
1829 procedure Expand_Packed_Eq (N : Node_Id) is
1830 Loc : constant Source_Ptr := Sloc (N);
1831 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1832 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1834 LLexpr : Node_Id;
1835 RLexpr : Node_Id;
1837 Ltyp : Entity_Id;
1838 Rtyp : Entity_Id;
1839 PAT : Entity_Id;
1841 begin
1842 Convert_To_Actual_Subtype (L);
1843 Convert_To_Actual_Subtype (R);
1844 Ltyp := Underlying_Type (Etype (L));
1845 Rtyp := Underlying_Type (Etype (R));
1847 Convert_To_PAT_Type (L);
1848 Convert_To_PAT_Type (R);
1849 PAT := Etype (L);
1851 LLexpr :=
1852 Make_Op_Multiply (Loc,
1853 Left_Opnd => Compute_Number_Components (N, Ltyp),
1854 Right_Opnd => Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1856 RLexpr :=
1857 Make_Op_Multiply (Loc,
1858 Left_Opnd => Compute_Number_Components (N, Rtyp),
1859 Right_Opnd => Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1861 -- For the modular case, we transform the comparison to:
1863 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1865 -- where PAT is the packed array type. This works fine, since in the
1866 -- modular case we guarantee that the unused bits are always zeroes.
1867 -- We do have to compare the lengths because we could be comparing
1868 -- two different subtypes of the same base type.
1870 if Is_Modular_Integer_Type (PAT) then
1871 Rewrite (N,
1872 Make_And_Then (Loc,
1873 Left_Opnd =>
1874 Make_Op_Eq (Loc,
1875 Left_Opnd => LLexpr,
1876 Right_Opnd => RLexpr),
1878 Right_Opnd =>
1879 Make_Op_Eq (Loc,
1880 Left_Opnd => L,
1881 Right_Opnd => R)));
1883 -- For the non-modular case, we call a runtime routine
1885 -- System.Bit_Ops.Bit_Eq
1886 -- (L'Address, L_Length, R'Address, R_Length)
1888 -- where PAT is the packed array type, and the lengths are the lengths
1889 -- in bits of the original packed arrays. This routine takes care of
1890 -- not comparing the unused bits in the last byte.
1892 else
1893 Rewrite (N,
1894 Make_Function_Call (Loc,
1895 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1896 Parameter_Associations => New_List (
1897 Make_Byte_Aligned_Attribute_Reference (Loc,
1898 Prefix => L,
1899 Attribute_Name => Name_Address),
1901 LLexpr,
1903 Make_Byte_Aligned_Attribute_Reference (Loc,
1904 Prefix => R,
1905 Attribute_Name => Name_Address),
1907 RLexpr)));
1908 end if;
1910 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
1911 end Expand_Packed_Eq;
1913 -----------------------
1914 -- Expand_Packed_Not --
1915 -----------------------
1917 -- Handles expansion of "not" on packed array types
1919 procedure Expand_Packed_Not (N : Node_Id) is
1920 Loc : constant Source_Ptr := Sloc (N);
1921 Typ : constant Entity_Id := Etype (N);
1922 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
1924 Rtyp : Entity_Id;
1925 PAT : Entity_Id;
1926 Lit : Node_Id;
1928 begin
1929 Convert_To_Actual_Subtype (Opnd);
1930 Rtyp := Etype (Opnd);
1932 -- Deal with silly False..False and True..True subtype case
1934 Silly_Boolean_Array_Not_Test (N, Rtyp);
1936 -- Now that the silliness is taken care of, get packed array type
1938 Convert_To_PAT_Type (Opnd);
1939 PAT := Etype (Opnd);
1941 -- For the case where the packed array type is a modular type, "not A"
1942 -- expands simply into:
1944 -- Rtyp!(PAT!(A) xor Mask)
1946 -- where PAT is the packed array type, Mask is a mask of all 1 bits of
1947 -- length equal to the size of this packed type, and Rtyp is the actual
1948 -- actual subtype of the operand.
1950 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
1951 Set_Print_In_Hex (Lit);
1953 if not Is_Array_Type (PAT) then
1954 Rewrite (N,
1955 Unchecked_Convert_To (Rtyp,
1956 Make_Op_Xor (Loc,
1957 Left_Opnd => Opnd,
1958 Right_Opnd => Lit)));
1960 -- For the array case, we insert the actions
1962 -- Result : Typ;
1964 -- System.Bit_Ops.Bit_Not
1965 -- (Opnd'Address,
1966 -- Typ'Length * Typ'Component_Size,
1967 -- Result'Address);
1969 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second argument
1970 -- is the length of the operand in bits. We then replace the expression
1971 -- with a reference to Result.
1973 else
1974 declare
1975 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
1977 begin
1978 Insert_Actions (N, New_List (
1979 Make_Object_Declaration (Loc,
1980 Defining_Identifier => Result_Ent,
1981 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
1983 Make_Procedure_Call_Statement (Loc,
1984 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
1985 Parameter_Associations => New_List (
1986 Make_Byte_Aligned_Attribute_Reference (Loc,
1987 Prefix => Opnd,
1988 Attribute_Name => Name_Address),
1990 Make_Op_Multiply (Loc,
1991 Left_Opnd =>
1992 Make_Attribute_Reference (Loc,
1993 Prefix =>
1994 New_Occurrence_Of
1995 (Etype (First_Index (Rtyp)), Loc),
1996 Attribute_Name => Name_Range_Length),
1998 Right_Opnd =>
1999 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2001 Make_Byte_Aligned_Attribute_Reference (Loc,
2002 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2003 Attribute_Name => Name_Address)))));
2005 Rewrite (N, New_Occurrence_Of (Result_Ent, Loc));
2006 end;
2007 end if;
2009 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2010 end Expand_Packed_Not;
2012 -----------------------------
2013 -- Get_Base_And_Bit_Offset --
2014 -----------------------------
2016 procedure Get_Base_And_Bit_Offset
2017 (N : Node_Id;
2018 Base : out Node_Id;
2019 Offset : out Node_Id)
2021 Loc : Source_Ptr;
2022 Term : Node_Id;
2023 Atyp : Entity_Id;
2024 Subscr : Node_Id;
2026 begin
2027 Base := N;
2028 Offset := Empty;
2030 -- We build up an expression serially that has the form
2032 -- linear-subscript * component_size for each array reference
2033 -- + field'Bit_Position for each record field
2034 -- + ...
2036 loop
2037 Loc := Sloc (Base);
2039 if Nkind (Base) = N_Indexed_Component then
2040 Convert_To_Actual_Subtype (Prefix (Base));
2041 Atyp := Etype (Prefix (Base));
2042 Compute_Linear_Subscript (Atyp, Base, Subscr);
2044 Term :=
2045 Make_Op_Multiply (Loc,
2046 Left_Opnd => Subscr,
2047 Right_Opnd =>
2048 Make_Attribute_Reference (Loc,
2049 Prefix => New_Occurrence_Of (Atyp, Loc),
2050 Attribute_Name => Name_Component_Size));
2052 elsif Nkind (Base) = N_Selected_Component then
2053 Term :=
2054 Make_Attribute_Reference (Loc,
2055 Prefix => Selector_Name (Base),
2056 Attribute_Name => Name_Bit_Position);
2058 else
2059 return;
2060 end if;
2062 if No (Offset) then
2063 Offset := Term;
2065 else
2066 Offset :=
2067 Make_Op_Add (Loc,
2068 Left_Opnd => Offset,
2069 Right_Opnd => Term);
2070 end if;
2072 Base := Prefix (Base);
2073 end loop;
2074 end Get_Base_And_Bit_Offset;
2076 -------------------------------------
2077 -- Involves_Packed_Array_Reference --
2078 -------------------------------------
2080 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2081 begin
2082 if Nkind (N) = N_Indexed_Component
2083 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2084 then
2085 return True;
2087 elsif Nkind (N) = N_Selected_Component then
2088 return Involves_Packed_Array_Reference (Prefix (N));
2090 else
2091 return False;
2092 end if;
2093 end Involves_Packed_Array_Reference;
2095 --------------------------
2096 -- Known_Aligned_Enough --
2097 --------------------------
2099 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2100 Typ : constant Entity_Id := Etype (Obj);
2102 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2103 -- If the component is in a record that contains previous packed
2104 -- components, consider it unaligned because the back-end might
2105 -- choose to pack the rest of the record. Lead to less efficient code,
2106 -- but safer vis-a-vis of back-end choices.
2108 --------------------------------
2109 -- In_Partially_Packed_Record --
2110 --------------------------------
2112 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2113 Rec_Type : constant Entity_Id := Scope (Comp);
2114 Prev_Comp : Entity_Id;
2116 begin
2117 Prev_Comp := First_Entity (Rec_Type);
2118 while Present (Prev_Comp) loop
2119 if Is_Packed (Etype (Prev_Comp)) then
2120 return True;
2122 elsif Prev_Comp = Comp then
2123 return False;
2124 end if;
2126 Next_Entity (Prev_Comp);
2127 end loop;
2129 return False;
2130 end In_Partially_Packed_Record;
2132 -- Start of processing for Known_Aligned_Enough
2134 begin
2135 -- Odd bit sizes don't need alignment anyway
2137 if Csiz mod 2 = 1 then
2138 return True;
2140 -- If we have a specified alignment, see if it is sufficient, if not
2141 -- then we can't possibly be aligned enough in any case.
2143 elsif Known_Alignment (Etype (Obj)) then
2144 -- Alignment required is 4 if size is a multiple of 4, and
2145 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2147 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2148 return False;
2149 end if;
2150 end if;
2152 -- OK, alignment should be sufficient, if object is aligned
2154 -- If object is strictly aligned, then it is definitely aligned
2156 if Strict_Alignment (Typ) then
2157 return True;
2159 -- Case of subscripted array reference
2161 elsif Nkind (Obj) = N_Indexed_Component then
2163 -- If we have a pointer to an array, then this is definitely
2164 -- aligned, because pointers always point to aligned versions.
2166 if Is_Access_Type (Etype (Prefix (Obj))) then
2167 return True;
2169 -- Otherwise, go look at the prefix
2171 else
2172 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2173 end if;
2175 -- Case of record field
2177 elsif Nkind (Obj) = N_Selected_Component then
2179 -- What is significant here is whether the record type is packed
2181 if Is_Record_Type (Etype (Prefix (Obj)))
2182 and then Is_Packed (Etype (Prefix (Obj)))
2183 then
2184 return False;
2186 -- Or the component has a component clause which might cause
2187 -- the component to become unaligned (we can't tell if the
2188 -- backend is doing alignment computations).
2190 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2191 return False;
2193 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2194 return False;
2196 -- In all other cases, go look at prefix
2198 else
2199 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2200 end if;
2202 elsif Nkind (Obj) = N_Type_Conversion then
2203 return Known_Aligned_Enough (Expression (Obj), Csiz);
2205 -- For a formal parameter, it is safer to assume that it is not
2206 -- aligned, because the formal may be unconstrained while the actual
2207 -- is constrained. In this situation, a small constrained packed
2208 -- array, represented in modular form, may be unaligned.
2210 elsif Is_Entity_Name (Obj) then
2211 return not Is_Formal (Entity (Obj));
2212 else
2214 -- If none of the above, must be aligned
2215 return True;
2216 end if;
2217 end Known_Aligned_Enough;
2219 ---------------------
2220 -- Make_Shift_Left --
2221 ---------------------
2223 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2224 Nod : Node_Id;
2226 begin
2227 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2228 return N;
2229 else
2230 Nod :=
2231 Make_Op_Shift_Left (Sloc (N),
2232 Left_Opnd => N,
2233 Right_Opnd => S);
2234 Set_Shift_Count_OK (Nod, True);
2235 return Nod;
2236 end if;
2237 end Make_Shift_Left;
2239 ----------------------
2240 -- Make_Shift_Right --
2241 ----------------------
2243 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2244 Nod : Node_Id;
2246 begin
2247 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2248 return N;
2249 else
2250 Nod :=
2251 Make_Op_Shift_Right (Sloc (N),
2252 Left_Opnd => N,
2253 Right_Opnd => S);
2254 Set_Shift_Count_OK (Nod, True);
2255 return Nod;
2256 end if;
2257 end Make_Shift_Right;
2259 -----------------------------
2260 -- RJ_Unchecked_Convert_To --
2261 -----------------------------
2263 function RJ_Unchecked_Convert_To
2264 (Typ : Entity_Id;
2265 Expr : Node_Id) return Node_Id
2267 Source_Typ : constant Entity_Id := Etype (Expr);
2268 Target_Typ : constant Entity_Id := Typ;
2270 Src : Node_Id := Expr;
2272 Source_Siz : Nat;
2273 Target_Siz : Nat;
2275 begin
2276 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2277 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2279 -- For a little-endian target type stored byte-swapped on a
2280 -- big-endian machine, do not mask to Target_Siz bits.
2282 if Bytes_Big_Endian
2283 and then (Is_Record_Type (Target_Typ)
2284 or else
2285 Is_Array_Type (Target_Typ))
2286 and then Reverse_Storage_Order (Target_Typ)
2287 then
2288 Source_Siz := Target_Siz;
2289 end if;
2291 -- First step, if the source type is not a discrete type, then we first
2292 -- convert to a modular type of the source length, since otherwise, on
2293 -- a big-endian machine, we get left-justification. We do it for little-
2294 -- endian machines as well, because there might be junk bits that are
2295 -- not cleared if the type is not numeric. This can be done only if the
2296 -- source siz is different from 0 (i.e. known), otherwise we must trust
2297 -- the type declarations (case of non-discrete components).
2299 if Source_Siz /= 0
2300 and then Source_Siz /= Target_Siz
2301 and then not Is_Discrete_Type (Source_Typ)
2302 then
2303 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2304 end if;
2306 -- In the big endian case, if the lengths of the two types differ, then
2307 -- we must worry about possible left justification in the conversion,
2308 -- and avoiding that is what this is all about.
2310 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2312 -- Next step. If the target is not a discrete type, then we first
2313 -- convert to a modular type of the target length, since otherwise,
2314 -- on a big-endian machine, we get left-justification.
2316 if not Is_Discrete_Type (Target_Typ) then
2317 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2318 end if;
2319 end if;
2321 -- And now we can do the final conversion to the target type
2323 return Unchecked_Convert_To (Target_Typ, Src);
2324 end RJ_Unchecked_Convert_To;
2326 ----------------------------------------------
2327 -- Setup_Enumeration_Packed_Array_Reference --
2328 ----------------------------------------------
2330 -- All we have to do here is to find the subscripts that correspond to the
2331 -- index positions that have non-standard enumeration types and insert a
2332 -- Pos attribute to get the proper subscript value.
2334 -- Finally the prefix must be uncheck-converted to the corresponding packed
2335 -- array type.
2337 -- Note that the component type is unchanged, so we do not need to fiddle
2338 -- with the types (Gigi always automatically takes the packed array type if
2339 -- it is set, as it will be in this case).
2341 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2342 Pfx : constant Node_Id := Prefix (N);
2343 Typ : constant Entity_Id := Etype (N);
2344 Exprs : constant List_Id := Expressions (N);
2345 Expr : Node_Id;
2347 begin
2348 -- If the array is unconstrained, then we replace the array reference
2349 -- with its actual subtype. This actual subtype will have a packed array
2350 -- type with appropriate bounds.
2352 if not Is_Constrained (Packed_Array_Impl_Type (Etype (Pfx))) then
2353 Convert_To_Actual_Subtype (Pfx);
2354 end if;
2356 Expr := First (Exprs);
2357 while Present (Expr) loop
2358 declare
2359 Loc : constant Source_Ptr := Sloc (Expr);
2360 Expr_Typ : constant Entity_Id := Etype (Expr);
2362 begin
2363 if Is_Enumeration_Type (Expr_Typ)
2364 and then Has_Non_Standard_Rep (Expr_Typ)
2365 then
2366 Rewrite (Expr,
2367 Make_Attribute_Reference (Loc,
2368 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2369 Attribute_Name => Name_Pos,
2370 Expressions => New_List (Relocate_Node (Expr))));
2371 Analyze_And_Resolve (Expr, Standard_Natural);
2372 end if;
2373 end;
2375 Next (Expr);
2376 end loop;
2378 Rewrite (N,
2379 Make_Indexed_Component (Sloc (N),
2380 Prefix =>
2381 Unchecked_Convert_To (Packed_Array_Impl_Type (Etype (Pfx)), Pfx),
2382 Expressions => Exprs));
2384 Analyze_And_Resolve (N, Typ);
2385 end Setup_Enumeration_Packed_Array_Reference;
2387 -----------------------------------------
2388 -- Setup_Inline_Packed_Array_Reference --
2389 -----------------------------------------
2391 procedure Setup_Inline_Packed_Array_Reference
2392 (N : Node_Id;
2393 Atyp : Entity_Id;
2394 Obj : in out Node_Id;
2395 Cmask : out Uint;
2396 Shift : out Node_Id)
2398 Loc : constant Source_Ptr := Sloc (N);
2399 PAT : Entity_Id;
2400 Otyp : Entity_Id;
2401 Csiz : Uint;
2402 Osiz : Uint;
2404 begin
2405 Csiz := Component_Size (Atyp);
2407 Convert_To_PAT_Type (Obj);
2408 PAT := Etype (Obj);
2410 Cmask := 2 ** Csiz - 1;
2412 if Is_Array_Type (PAT) then
2413 Otyp := Component_Type (PAT);
2414 Osiz := Component_Size (PAT);
2416 else
2417 Otyp := PAT;
2419 -- In the case where the PAT is a modular type, we want the actual
2420 -- size in bits of the modular value we use. This is neither the
2421 -- Object_Size nor the Value_Size, either of which may have been
2422 -- reset to strange values, but rather the minimum size. Note that
2423 -- since this is a modular type with full range, the issue of
2424 -- biased representation does not arise.
2426 Osiz := UI_From_Int (Minimum_Size (Otyp));
2427 end if;
2429 Compute_Linear_Subscript (Atyp, N, Shift);
2431 -- If the component size is not 1, then the subscript must be multiplied
2432 -- by the component size to get the shift count.
2434 if Csiz /= 1 then
2435 Shift :=
2436 Make_Op_Multiply (Loc,
2437 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2438 Right_Opnd => Shift);
2439 end if;
2441 -- If we have the array case, then this shift count must be broken down
2442 -- into a byte subscript, and a shift within the byte.
2444 if Is_Array_Type (PAT) then
2446 declare
2447 New_Shift : Node_Id;
2449 begin
2450 -- We must analyze shift, since we will duplicate it
2452 Set_Parent (Shift, N);
2453 Analyze_And_Resolve
2454 (Shift, Standard_Integer, Suppress => All_Checks);
2456 -- The shift count within the word is
2457 -- shift mod Osiz
2459 New_Shift :=
2460 Make_Op_Mod (Loc,
2461 Left_Opnd => Duplicate_Subexpr (Shift),
2462 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2464 -- The subscript to be used on the PAT array is
2465 -- shift / Osiz
2467 Obj :=
2468 Make_Indexed_Component (Loc,
2469 Prefix => Obj,
2470 Expressions => New_List (
2471 Make_Op_Divide (Loc,
2472 Left_Opnd => Duplicate_Subexpr (Shift),
2473 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2475 Shift := New_Shift;
2476 end;
2478 -- For the modular integer case, the object to be manipulated is the
2479 -- entire array, so Obj is unchanged. Note that we will reset its type
2480 -- to PAT before returning to the caller.
2482 else
2483 null;
2484 end if;
2486 -- The one remaining step is to modify the shift count for the
2487 -- big-endian case. Consider the following example in a byte:
2489 -- xxxxxxxx bits of byte
2490 -- vvvvvvvv bits of value
2491 -- 33221100 little-endian numbering
2492 -- 00112233 big-endian numbering
2494 -- Here we have the case of 2-bit fields
2496 -- For the little-endian case, we already have the proper shift count
2497 -- set, e.g. for element 2, the shift count is 2*2 = 4.
2499 -- For the big endian case, we have to adjust the shift count, computing
2500 -- it as (N - F) - Shift, where N is the number of bits in an element of
2501 -- the array used to implement the packed array, F is the number of bits
2502 -- in a source array element, and Shift is the count so far computed.
2504 -- We also have to adjust if the storage order is reversed
2506 if Bytes_Big_Endian xor Reverse_Storage_Order (Base_Type (Atyp)) then
2507 Shift :=
2508 Make_Op_Subtract (Loc,
2509 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2510 Right_Opnd => Shift);
2511 end if;
2513 Set_Parent (Shift, N);
2514 Set_Parent (Obj, N);
2515 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2516 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2518 -- Make sure final type of object is the appropriate packed type
2520 Set_Etype (Obj, Otyp);
2522 end Setup_Inline_Packed_Array_Reference;
2524 end Exp_Pakd;