MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / ada / exp_pakd.adb
blobc3908a54538dbe6108d62a20b276052d3e003eca
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-2023, 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 Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Errout; use Errout;
32 with Exp_Dbug; use Exp_Dbug;
33 with Exp_Util; use Exp_Util;
34 with Layout; use Layout;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Sem; use Sem;
41 with Sem_Aux; use Sem_Aux;
42 with Sem_Ch3; use Sem_Ch3;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sinfo; use Sinfo;
49 with Sinfo.Nodes; use Sinfo.Nodes;
50 with Sinfo.Utils; use Sinfo.Utils;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Ttypes; use Ttypes;
56 with Uintp; use Uintp;
58 package body Exp_Pakd is
60 ---------------------------
61 -- Endian Considerations --
62 ---------------------------
64 -- As described in the specification, bit numbering in a packed array
65 -- is consistent with bit numbering in a record representation clause,
66 -- and hence dependent on the endianness of the machine:
68 -- For little-endian machines, element zero is at the right hand end
69 -- (low order end) of a bit field.
71 -- For big-endian machines, element zero is at the left hand end
72 -- (high order end) of a bit field.
74 -- The shifts that are used to right justify a field therefore differ in
75 -- the two cases. For the little-endian case, we can simply use the bit
76 -- number (i.e. the element number * element size) as the count for a right
77 -- shift. For the big-endian case, we have to subtract the shift count from
78 -- an appropriate constant to use in the right shift. We use rotates
79 -- instead of shifts (which is necessary in the store case to preserve
80 -- other fields), and we expect that the backend will be able to change the
81 -- right rotate into a left rotate, avoiding the subtract, if the machine
82 -- architecture provides such an instruction.
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
88 procedure Compute_Linear_Subscript
89 (Atyp : Entity_Id;
90 N : Node_Id;
91 Subscr : out Node_Id);
92 -- Given a constrained array type Atyp, and an indexed component node N
93 -- referencing an array object of this type, build an expression of type
94 -- Standard.Integer representing the zero-based linear subscript value.
95 -- This expression includes any required range checks.
97 function Compute_Number_Components
98 (N : Node_Id;
99 Typ : Entity_Id) return Node_Id;
100 -- Build an expression that multiplies the length of the dimensions of the
101 -- array, used to control array equality checks.
103 procedure Convert_To_PAT_Type (Aexp : Node_Id);
104 -- Given an expression of a packed array type, builds a corresponding
105 -- expression whose type is the implementation type used to represent
106 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
108 procedure Get_Base_And_Bit_Offset
109 (N : Node_Id;
110 Base : out Node_Id;
111 Offset : out Node_Id);
112 -- Given a node N for a name which involves a packed array reference,
113 -- return the base object of the reference and build an expression of
114 -- type Standard.Integer representing the zero-based offset in bits
115 -- from Base'Address to the first bit of the reference.
117 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
118 -- There are two versions of the Set routines, the ones used when the
119 -- object is known to be sufficiently well aligned given the number of
120 -- bits, and the ones used when the object is not known to be aligned.
121 -- This routine is used to determine which set to use. Obj is a reference
122 -- to the object, and Csiz is the component size of the packed array.
123 -- True is returned if the alignment of object is known to be sufficient,
124 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
125 -- 2 otherwise.
127 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
128 -- Build a left shift node, checking for the case of a shift count of zero
130 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
131 -- Build a right shift node, checking for the case of a shift count of zero
133 function RJ_Unchecked_Convert_To
134 (Typ : Entity_Id;
135 Expr : Node_Id) return Node_Id;
136 -- The packed array code does unchecked conversions which in some cases
137 -- may involve non-discrete types with differing sizes. The semantics of
138 -- such conversions is potentially endianness dependent, and the effect
139 -- we want here for such a conversion is to do the conversion in size as
140 -- though numeric items are involved, and we extend or truncate on the
141 -- left side. This happens naturally in the little-endian case, but in
142 -- the big endian case we can get left justification, when what we want
143 -- is right justification. This routine does the unchecked conversion in
144 -- a stepwise manner to ensure that it gives the expected result. Hence
145 -- the name (RJ = Right justified). The parameters Typ and Expr are as
146 -- for the case of a normal Unchecked_Convert_To call.
148 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
149 -- This routine is called in the Get and Set case for arrays that are
150 -- packed but not bit-packed, meaning that they have at least one
151 -- subscript that is of an enumeration type with a non-standard
152 -- representation. This routine modifies the given node to properly
153 -- reference the corresponding packed array type.
155 procedure Setup_Inline_Packed_Array_Reference
156 (N : Node_Id;
157 Atyp : Entity_Id;
158 Obj : in out Node_Id;
159 Cmask : out Uint;
160 Shift : out Node_Id);
161 -- This procedure performs common processing on the N_Indexed_Component
162 -- parameter given as N, whose prefix is a reference to a packed array.
163 -- This is used for the get and set when the component size is 1, 2, 4,
164 -- or for other component sizes when the packed array type is a modular
165 -- type (i.e. the cases that are handled with inline code).
167 -- On entry:
169 -- N is the N_Indexed_Component node for the packed array reference
171 -- Atyp is the constrained array type (the actual subtype has been
172 -- computed if necessary to obtain the constraints, but this is still
173 -- the original array type, not the Packed_Array_Impl_Type value).
175 -- Obj is the object which is to be indexed. It is always of type Atyp.
177 -- On return:
179 -- Obj is the object containing the desired bit field. It is of type
180 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
181 -- entire value, for the small static case, or the proper selected byte
182 -- from the array in the large or dynamic case. This node is analyzed
183 -- and resolved on return.
185 -- Shift is a node representing the shift count to be used in the
186 -- rotate right instruction that positions the field for access.
187 -- This node is analyzed and resolved on return.
189 -- Cmask is a mask corresponding to the width of the component field.
190 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
192 -- Note: in some cases the call to this routine may generate actions
193 -- (for handling multi-use references and the generation of the packed
194 -- array type on the fly). Such actions are inserted into the tree
195 -- directly using Insert_Action.
197 function Revert_Storage_Order (N : Node_Id) return Node_Id;
198 -- Perform appropriate justification and byte ordering adjustments for N,
199 -- an element of a packed array type, when both the component type and
200 -- the enclosing packed array type have reverse scalar storage order.
201 -- On little-endian targets, the value is left justified before byte
202 -- swapping. The Etype of the returned expression is an integer type of
203 -- an appropriate power-of-2 size.
205 --------------------------
206 -- Revert_Storage_Order --
207 --------------------------
209 function Revert_Storage_Order (N : Node_Id) return Node_Id is
210 Loc : constant Source_Ptr := Sloc (N);
211 T : constant Entity_Id := Etype (N);
212 T_Size : constant Uint := RM_Size (T);
214 Swap_RE : RE_Id;
215 Swap_F : Entity_Id;
216 Swap_T : Entity_Id;
217 -- Swapping function
219 Arg : Node_Id;
220 Adjusted : Node_Id;
221 Shift : Uint;
223 begin
224 if T_Size <= 8 then
226 -- Array component size is less than a byte: no swapping needed
228 Swap_F := Empty;
229 Swap_T := RTE (RE_Unsigned_8);
231 else
232 -- Select byte swapping function depending on array component size
234 if T_Size <= 16 then
235 Swap_RE := RE_Bswap_16;
237 elsif T_Size <= 32 then
238 Swap_RE := RE_Bswap_32;
240 elsif T_Size <= 64 then
241 Swap_RE := RE_Bswap_64;
243 else pragma Assert (T_Size <= 128);
244 Swap_RE := RE_Bswap_128;
245 end if;
247 Swap_F := RTE (Swap_RE);
248 Swap_T := Etype (Swap_F);
250 end if;
252 Shift := Esize (Swap_T) - T_Size;
254 Arg := RJ_Unchecked_Convert_To (Swap_T, N);
256 if not Bytes_Big_Endian and then Shift > Uint_0 then
257 Arg :=
258 Make_Op_Shift_Left (Loc,
259 Left_Opnd => Arg,
260 Right_Opnd => Make_Integer_Literal (Loc, Shift));
261 end if;
263 if Present (Swap_F) then
264 Adjusted :=
265 Make_Function_Call (Loc,
266 Name => New_Occurrence_Of (Swap_F, Loc),
267 Parameter_Associations => New_List (Arg));
268 else
269 Adjusted := Arg;
270 end if;
272 Set_Etype (Adjusted, Swap_T);
273 return Adjusted;
274 end Revert_Storage_Order;
276 ------------------------------
277 -- Compute_Linear_Subscript --
278 ------------------------------
280 procedure Compute_Linear_Subscript
281 (Atyp : Entity_Id;
282 N : Node_Id;
283 Subscr : out Node_Id)
285 Loc : constant Source_Ptr := Sloc (N);
286 Oldsub : Node_Id;
287 Newsub : Node_Id;
288 Indx : Node_Id;
289 Styp : Entity_Id;
291 begin
292 Subscr := Empty;
294 -- Loop through dimensions
296 Indx := First_Index (Atyp);
297 Oldsub := First (Expressions (N));
299 while Present (Indx) loop
300 Styp := Etype (Indx);
301 Newsub := Relocate_Node (Oldsub);
303 -- Get expression for the subscript value. First, if Do_Range_Check
304 -- is set on a subscript, then we must do a range check against the
305 -- original bounds (not the bounds of the packed array type). We do
306 -- this by introducing a subtype conversion.
308 if Do_Range_Check (Newsub)
309 and then Etype (Newsub) /= Styp
310 then
311 Newsub := Convert_To (Styp, Newsub);
312 end if;
314 -- Now evolve the expression for the subscript. First convert
315 -- the subscript to be zero based and of an integer type.
317 -- Case of integer type, where we just subtract to get lower bound
319 if Is_Integer_Type (Styp) then
321 -- If length of integer type is smaller than standard integer,
322 -- then we convert to integer first, then do the subtract
324 -- Integer (subscript) - Integer (Styp'First)
326 if Esize (Styp) < Standard_Integer_Size then
327 Newsub :=
328 Make_Op_Subtract (Loc,
329 Left_Opnd => Convert_To (Standard_Integer, Newsub),
330 Right_Opnd =>
331 Convert_To (Standard_Integer,
332 Make_Attribute_Reference (Loc,
333 Prefix => New_Occurrence_Of (Styp, Loc),
334 Attribute_Name => Name_First)));
336 -- For larger integer types, subtract first, then convert to
337 -- integer, this deals with strange long long integer bounds.
339 -- Integer (subscript - Styp'First)
341 else
342 Newsub :=
343 Convert_To (Standard_Integer,
344 Make_Op_Subtract (Loc,
345 Left_Opnd => Newsub,
346 Right_Opnd =>
347 Make_Attribute_Reference (Loc,
348 Prefix => New_Occurrence_Of (Styp, Loc),
349 Attribute_Name => Name_First)));
350 end if;
352 -- For the enumeration case, we have to use 'Pos to get the value
353 -- to work with before subtracting the lower bound.
355 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
357 -- This is not quite right for bizarre cases where the size of the
358 -- enumeration type is > Integer'Size bits due to rep clause ???
360 else
361 pragma Assert (Is_Enumeration_Type (Styp));
363 Newsub :=
364 Make_Op_Subtract (Loc,
365 Left_Opnd => 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 (Newsub))),
371 Right_Opnd =>
372 Convert_To (Standard_Integer,
373 Make_Attribute_Reference (Loc,
374 Prefix => New_Occurrence_Of (Styp, Loc),
375 Attribute_Name => Name_Pos,
376 Expressions => New_List (
377 Make_Attribute_Reference (Loc,
378 Prefix => New_Occurrence_Of (Styp, Loc),
379 Attribute_Name => Name_First)))));
380 end if;
382 Set_Paren_Count (Newsub, 1);
384 -- For the first subscript, we just copy that subscript value
386 if No (Subscr) then
387 Subscr := Newsub;
389 -- Otherwise, we must multiply what we already have by the current
390 -- stride and then add in the new value to the evolving subscript.
392 else
393 Subscr :=
394 Make_Op_Add (Loc,
395 Left_Opnd =>
396 Make_Op_Multiply (Loc,
397 Left_Opnd => Subscr,
398 Right_Opnd =>
399 Make_Attribute_Reference (Loc,
400 Attribute_Name => Name_Range_Length,
401 Prefix => New_Occurrence_Of (Styp, Loc))),
402 Right_Opnd => Newsub);
403 end if;
405 -- Move to next subscript
407 Next_Index (Indx);
408 Next (Oldsub);
409 end loop;
410 end Compute_Linear_Subscript;
412 -------------------------------
413 -- Compute_Number_Components --
414 -------------------------------
416 function Compute_Number_Components
417 (N : Node_Id;
418 Typ : Entity_Id) return Node_Id
420 Loc : constant Source_Ptr := Sloc (N);
421 Len_Expr : Node_Id;
423 begin
424 Len_Expr :=
425 Make_Attribute_Reference (Loc,
426 Attribute_Name => Name_Length,
427 Prefix => New_Occurrence_Of (Typ, Loc),
428 Expressions => New_List (Make_Integer_Literal (Loc, 1)));
430 for J in 2 .. Number_Dimensions (Typ) loop
431 Len_Expr :=
432 Make_Op_Multiply (Loc,
433 Left_Opnd => Len_Expr,
434 Right_Opnd =>
435 Make_Attribute_Reference (Loc,
436 Attribute_Name => Name_Length,
437 Prefix => New_Occurrence_Of (Typ, Loc),
438 Expressions => New_List (Make_Integer_Literal (Loc, J))));
439 end loop;
441 return Len_Expr;
442 end Compute_Number_Components;
444 -------------------------
445 -- Convert_To_PAT_Type --
446 -------------------------
448 -- The PAT is always obtained from the actual subtype
450 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
451 Act_ST : Entity_Id;
453 begin
454 Convert_To_Actual_Subtype (Aexp);
455 Act_ST := Underlying_Type (Etype (Aexp));
456 Create_Packed_Array_Impl_Type (Act_ST);
458 -- Just replace the etype with the packed array type. This works because
459 -- the expression will not be further analyzed, and Gigi considers the
460 -- two types equivalent in any case.
462 -- This is not strictly the case ??? If the reference is an actual in
463 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
464 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
465 -- array reference, reanalysis can produce spurious type errors when the
466 -- PAT type is replaced again with the original type of the array. Same
467 -- for the case of a dereference. Ditto for function calls: expansion
468 -- may introduce additional actuals which will trigger errors if call is
469 -- reanalyzed. The following is correct and minimal, but the handling of
470 -- more complex packed expressions in actuals is confused. Probably the
471 -- problem only remains for actuals in calls.
473 Set_Etype (Aexp, Packed_Array_Impl_Type (Act_ST));
475 if Is_Entity_Name (Aexp)
476 or else
477 (Nkind (Aexp) = N_Indexed_Component
478 and then Is_Entity_Name (Prefix (Aexp)))
479 or else Nkind (Aexp) in N_Explicit_Dereference | N_Function_Call
480 then
481 Set_Analyzed (Aexp);
482 end if;
483 end Convert_To_PAT_Type;
485 -----------------------------------
486 -- Create_Packed_Array_Impl_Type --
487 -----------------------------------
489 procedure Create_Packed_Array_Impl_Type (Typ : Entity_Id) is
490 Loc : constant Source_Ptr := Sloc (Typ);
491 Ctyp : constant Entity_Id := Component_Type (Typ);
492 Csize : constant Uint := Component_Size (Typ);
494 Ancest : Entity_Id;
495 PB_Type : Entity_Id;
496 PASize : Uint := No_Uint;
497 Decl : Node_Id;
498 PAT : Entity_Id;
499 Len_Expr : Node_Id;
500 Len_Bits : Uint;
501 Bits_U1 : Node_Id;
502 PAT_High : Node_Id;
503 Btyp : Entity_Id;
504 Lit : Node_Id;
506 procedure Install_PAT;
507 -- This procedure is called with Decl set to the declaration for the
508 -- packed array type. It creates the type and installs it as required.
510 procedure Set_PB_Type;
511 -- Set PB_Type to [Rev_]Packed_Bytes{1,2,4} as required by the alignment
512 -- and the scalar storage order requirements (see documentation in the
513 -- spec of this package).
515 -----------------
516 -- Install_PAT --
517 -----------------
519 procedure Install_PAT is
520 Pushed_Scope : Boolean := False;
522 begin
523 -- We do not want to put the declaration we have created in the tree
524 -- since it is often hard, and sometimes impossible to find a proper
525 -- place for it (the impossible case arises for a packed array type
526 -- with bounds depending on the discriminant, a declaration cannot
527 -- be put inside the record, and the reference to the discriminant
528 -- cannot be outside the record).
530 -- The solution is to analyze the declaration while temporarily
531 -- attached to the tree at an appropriate point, and then we install
532 -- the resulting type as an Itype in the packed array type field of
533 -- the original type, so that no explicit declaration is required.
535 -- Note: the packed type is created in the scope of its parent type.
536 -- There are at least some cases where the current scope is deeper,
537 -- and so when this is the case, we temporarily reset the scope
538 -- for the definition. This is clearly safe, since the first use
539 -- of the packed array type will be the implicit reference from
540 -- the corresponding unpacked type when it is elaborated.
542 if Is_Itype (Typ) then
543 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
544 else
545 Set_Parent (Decl, Declaration_Node (Typ));
546 end if;
548 if Scope (Typ) /= Current_Scope then
549 Push_Scope (Scope (Typ));
550 Pushed_Scope := True;
551 end if;
553 Set_Is_Itype (PAT, True);
554 Set_Is_Packed_Array_Impl_Type (PAT, True);
555 Set_Packed_Array_Impl_Type (Typ, PAT);
556 Analyze (Decl, Suppress => All_Checks);
558 if Pushed_Scope then
559 Pop_Scope;
560 end if;
562 -- Set Esize and RM_Size to the actual size of the packed object
563 -- Do not reset RM_Size if already set, as happens in the case of
564 -- a modular type.
566 if Present (PASize) then
567 if not Known_Esize (PAT) then
568 Set_Esize (PAT, PASize);
569 end if;
571 if not Known_RM_Size (PAT) then
572 Set_RM_Size (PAT, PASize);
573 end if;
574 end if;
576 -- In the case of a modular type, make sure the alignment is
577 -- consistent with the Esize.
579 if Is_Scalar_Type (PAT) then
580 while Alignment (PAT) * System_Storage_Unit < Esize (PAT)
581 and then Alignment (PAT) < Maximum_Alignment
582 loop
583 Set_Alignment (PAT, 2 * Alignment (PAT));
584 end loop;
585 end if;
587 -- Then, in all cases, make sure the opposite is also true
589 Adjust_Esize_Alignment (PAT);
591 -- Set remaining fields of packed array type
593 Set_Parent (PAT, Empty);
594 Set_Associated_Node_For_Itype (PAT, Typ);
595 Set_Original_Array_Type (PAT, Typ);
597 -- Propagate representation aspects
599 Set_Is_Atomic (PAT, Is_Atomic (Typ));
600 Set_Is_Independent (PAT, Is_Independent (Typ));
601 Set_Is_Volatile (PAT, Is_Volatile (Typ));
602 Set_Is_Volatile_Full_Access (PAT, Is_Volatile_Full_Access (Typ));
603 Set_Treat_As_Volatile (PAT, Treat_As_Volatile (Typ));
605 -- We definitely do not want to delay freezing for packed array
606 -- types. This is of particular importance for the itypes that are
607 -- generated for record components depending on discriminants where
608 -- there is no place to put the freeze node.
610 Set_Has_Delayed_Freeze (PAT, False);
611 Set_Has_Delayed_Freeze (Etype (PAT), False);
613 -- If we did allocate a freeze node, then clear out the reference
614 -- since it is obsolete (should we delete the freeze node???)
616 Set_Freeze_Node (PAT, Empty);
617 Set_Freeze_Node (Etype (PAT), Empty);
618 end Install_PAT;
620 -----------------
621 -- Set_PB_Type --
622 -----------------
624 procedure Set_PB_Type is
625 begin
626 -- If the user has specified an explicit alignment for the
627 -- type or component, take it into account.
629 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
630 or else (Known_Alignment (Typ) and then Alignment (Typ) = 1)
631 or else Component_Alignment (Typ) = Calign_Storage_Unit
632 then
633 if Reverse_Storage_Order (Typ) then
634 PB_Type := RTE (RE_Rev_Packed_Bytes1);
635 else
636 PB_Type := RTE (RE_Packed_Bytes1);
637 end if;
639 elsif Csize mod 4 /= 0
640 or else (Known_Alignment (Typ) and then Alignment (Typ) = 2)
641 then
642 if Reverse_Storage_Order (Typ) then
643 PB_Type := RTE (RE_Rev_Packed_Bytes2);
644 else
645 PB_Type := RTE (RE_Packed_Bytes2);
646 end if;
648 else
649 if Reverse_Storage_Order (Typ) then
650 PB_Type := RTE (RE_Rev_Packed_Bytes4);
651 else
652 PB_Type := RTE (RE_Packed_Bytes4);
653 end if;
654 end if;
656 -- The Rev_Packed_Bytes{1,2,4} types cannot be directly declared with
657 -- the reverse scalar storage order in System.Unsigned_Types because
658 -- their component type is aliased and the combination would then be
659 -- flagged as illegal by the compiler. Moreover changing the compiler
660 -- would not address the bootstrap path issue with earlier versions.
662 Set_Reverse_Storage_Order (PB_Type, Reverse_Storage_Order (Typ));
663 end Set_PB_Type;
665 -- Start of processing for Create_Packed_Array_Impl_Type
667 begin
668 -- If we already have a packed array type, nothing to do
670 if Present (Packed_Array_Impl_Type (Typ)) then
671 return;
672 end if;
674 -- If our immediate ancestor subtype is constrained, and it already has
675 -- a packed array type, and it has the same size, then just share the
676 -- same type, since the bounds must be the same. If the ancestor is not
677 -- an array type but a private type, as can happen with multiple
678 -- instantiations, create a new packed type, to avoid privacy issues.
680 if Ekind (Typ) = E_Array_Subtype then
681 Ancest := Ancestor_Subtype (Typ);
683 if Present (Ancest)
684 and then Is_Array_Type (Ancest)
685 and then Is_Constrained (Ancest)
686 and then Present (Packed_Array_Impl_Type (Ancest))
687 and then Known_Esize (Typ)
688 and then Known_Esize (Ancest)
689 and then Esize (Typ) = Esize (Ancest)
690 then
691 Set_Packed_Array_Impl_Type (Typ, Packed_Array_Impl_Type (Ancest));
692 return;
693 end if;
694 end if;
696 -- We preset the result type size from the size of the original array
697 -- type, since this size clearly belongs to the packed array type. The
698 -- size of the conceptual unpacked type is always set to unknown.
700 if Known_RM_Size (Typ) then
701 PASize := RM_Size (Typ);
702 end if;
704 -- Case of an array where at least one index is of an enumeration
705 -- type with a non-standard representation, but the component size
706 -- is not appropriate for bit packing. This is the case where we
707 -- have Is_Packed set (we would never be in this unit otherwise),
708 -- but Is_Bit_Packed_Array is false.
710 -- Note that if the component size is appropriate for bit packing,
711 -- then the circuit for the computation of the subscript properly
712 -- deals with the non-standard enumeration type case by taking the
713 -- Pos anyway.
715 if not Is_Bit_Packed_Array (Typ) then
717 -- Here we build a declaration:
719 -- type tttP is array (index1, index2, ...) of component_type
721 -- where index1, index2, are the index types. These are the same
722 -- as the index types of the original array, except for the non-
723 -- standard representation enumeration type case, where we have
724 -- two subcases.
726 -- For the unconstrained array case, we use
728 -- Natural range <>
730 -- For the constrained case, we use
732 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
733 -- Enum_Type'Pos (Enum_Type'Last);
735 -- Note that tttP is created even if no index subtype is a non
736 -- standard enumeration, because we still need to remove padding
737 -- normally inserted for component alignment.
739 PAT :=
740 Make_Defining_Identifier (Loc,
741 Chars => New_External_Name (Chars (Typ), 'P'));
743 declare
744 Indexes : constant List_Id := New_List;
745 Indx : Node_Id;
746 Indx_Typ : Entity_Id;
747 Enum_Case : Boolean;
748 Typedef : Node_Id;
750 begin
751 Indx := First_Index (Typ);
753 while Present (Indx) loop
754 Indx_Typ := Etype (Indx);
756 Enum_Case := Is_Enumeration_Type (Indx_Typ)
757 and then Has_Non_Standard_Rep (Indx_Typ);
759 -- Unconstrained case
761 if not Is_Constrained (Typ) then
762 if Enum_Case then
763 Indx_Typ := Standard_Natural;
764 end if;
766 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
768 -- Constrained case
770 else
771 if not Enum_Case then
772 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
774 else
775 Append_To (Indexes,
776 Make_Subtype_Indication (Loc,
777 Subtype_Mark =>
778 New_Occurrence_Of (Standard_Natural, Loc),
779 Constraint =>
780 Make_Range_Constraint (Loc,
781 Range_Expression =>
782 Make_Range (Loc,
783 Low_Bound =>
784 Make_Attribute_Reference (Loc,
785 Prefix =>
786 New_Occurrence_Of (Indx_Typ, Loc),
787 Attribute_Name => Name_Pos,
788 Expressions => New_List (
789 Make_Attribute_Reference (Loc,
790 Prefix =>
791 New_Occurrence_Of (Indx_Typ, Loc),
792 Attribute_Name => Name_First))),
794 High_Bound =>
795 Make_Attribute_Reference (Loc,
796 Prefix =>
797 New_Occurrence_Of (Indx_Typ, Loc),
798 Attribute_Name => Name_Pos,
799 Expressions => New_List (
800 Make_Attribute_Reference (Loc,
801 Prefix =>
802 New_Occurrence_Of (Indx_Typ, Loc),
803 Attribute_Name => Name_Last)))))));
805 end if;
806 end if;
808 Next_Index (Indx);
809 end loop;
811 if not Is_Constrained (Typ) then
812 Typedef :=
813 Make_Unconstrained_Array_Definition (Loc,
814 Subtype_Marks => Indexes,
815 Component_Definition =>
816 Make_Component_Definition (Loc,
817 Aliased_Present => False,
818 Subtype_Indication =>
819 New_Occurrence_Of (Ctyp, Loc)));
821 else
822 Typedef :=
823 Make_Constrained_Array_Definition (Loc,
824 Discrete_Subtype_Definitions => Indexes,
825 Component_Definition =>
826 Make_Component_Definition (Loc,
827 Aliased_Present => False,
828 Subtype_Indication =>
829 New_Occurrence_Of (Ctyp, Loc)));
830 end if;
832 Decl :=
833 Make_Full_Type_Declaration (Loc,
834 Defining_Identifier => PAT,
835 Type_Definition => Typedef);
836 end;
838 Install_PAT;
840 -- Propagate the reverse storage order flag to the base type
842 Set_Reverse_Storage_Order (Etype (PAT), Reverse_Storage_Order (Typ));
843 return;
845 -- Case of bit-packing required for unconstrained array. We create
846 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
848 elsif not Is_Constrained (Typ) then
850 -- When generating standard DWARF (i.e when GNAT_Encodings is not
851 -- DWARF_GNAT_Encodings_All), the ___XP suffix will be stripped
852 -- by the back-end but generate it anyway to ease compiler debugging.
853 -- This will help to distinguish implementation types from original
854 -- packed arrays.
856 PAT :=
857 Make_Defining_Identifier (Loc,
858 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
860 Set_PB_Type;
862 Decl :=
863 Make_Subtype_Declaration (Loc,
864 Defining_Identifier => PAT,
865 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
867 Install_PAT;
868 return;
870 -- Remaining code is for the case of bit-packing for constrained array
872 -- The name of the packed array subtype is
874 -- ttt___XPsss
876 -- where sss is the component size in bits and ttt is the name of
877 -- the parent packed type.
879 else
880 PAT :=
881 Make_Defining_Identifier (Loc,
882 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
884 -- Build an expression for the length of the array in bits.
885 -- This is the product of the length of each of the dimensions
887 Len_Expr := Compute_Number_Components (Typ, Typ);
889 -- Temporarily attach the length expression to the tree and analyze
890 -- and resolve it, so that we can test its value. We assume that the
891 -- total length fits in type Integer. This expression may involve
892 -- discriminants, so we treat it as a default/per-object expression.
894 Set_Parent (Len_Expr, Typ);
895 Preanalyze_Spec_Expression (Len_Expr, Standard_Long_Long_Integer);
897 -- Use a modular type if possible. We can do this if we have
898 -- static bounds, and the length is small enough, and the length
899 -- is not zero. We exclude the zero length case because the size
900 -- of things is always at least one, and the zero length object
901 -- would have an anomalous size.
903 if Compile_Time_Known_Value (Len_Expr) then
904 Len_Bits := Expr_Value (Len_Expr) * Csize;
906 -- Check for size known to be too large
908 if Len_Bits >
909 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
910 then
911 if System_Storage_Unit = 8 then
912 Error_Msg_N
913 ("packed array size cannot exceed " &
914 "Integer''Last bytes", Typ);
915 else
916 Error_Msg_N
917 ("packed array size cannot exceed " &
918 "Integer''Last storage units", Typ);
919 end if;
921 -- Reset length to arbitrary not too high value to continue
923 Len_Expr := Make_Integer_Literal (Loc, 65535);
924 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
925 end if;
927 -- We normally consider small enough to mean no larger than the
928 -- value of System_Max_Binary_Modulus_Power, checking that in the
929 -- case of values longer than word size, we have long shifts.
931 if Len_Bits > 0
932 and then
933 (Len_Bits <= System_Word_Size
934 or else (Len_Bits <= System_Max_Binary_Modulus_Power
935 and then Support_Long_Shifts_On_Target))
936 then
937 -- We can use the modular type, it has the form:
939 -- subtype tttPn is btyp
940 -- range 0 .. 2 ** ((Typ'Length (1)
941 -- * ... * Typ'Length (n)) * Csize) - 1;
943 -- The bounds are statically known, and btyp is one of the
944 -- unsigned types, depending on the length.
946 Btyp := Small_Integer_Type_For (Len_Bits, Uns => True);
947 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
948 Set_Print_In_Hex (Lit);
950 Decl :=
951 Make_Subtype_Declaration (Loc,
952 Defining_Identifier => PAT,
953 Subtype_Indication =>
954 Make_Subtype_Indication (Loc,
955 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
957 Constraint =>
958 Make_Range_Constraint (Loc,
959 Range_Expression =>
960 Make_Range (Loc,
961 Low_Bound =>
962 Make_Integer_Literal (Loc, 0),
963 High_Bound => Lit))));
965 if Present (PASize) then
966 PASize := Len_Bits;
967 end if;
969 Install_PAT;
971 -- Propagate a given alignment to the modular type. This can
972 -- cause it to be under-aligned, but that's OK.
974 if Present (Alignment_Clause (Typ)) then
975 Set_Alignment (PAT, Alignment (Typ));
976 end if;
978 return;
979 end if;
980 end if;
982 -- Could not use a modular type, for all other cases, we build
983 -- a packed array subtype:
985 -- subtype tttPn is
986 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
988 -- Bits is the length of the array in bits
990 Set_PB_Type;
992 Bits_U1 :=
993 Make_Op_Add (Loc,
994 Left_Opnd =>
995 Make_Op_Multiply (Loc,
996 Left_Opnd =>
997 Make_Integer_Literal (Loc, Csize),
998 Right_Opnd => Len_Expr),
1000 Right_Opnd =>
1001 Make_Integer_Literal (Loc, 7));
1003 Set_Paren_Count (Bits_U1, 1);
1005 PAT_High :=
1006 Make_Op_Subtract (Loc,
1007 Left_Opnd =>
1008 Make_Op_Divide (Loc,
1009 Left_Opnd => Bits_U1,
1010 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1011 Right_Opnd => Make_Integer_Literal (Loc, 1));
1013 Decl :=
1014 Make_Subtype_Declaration (Loc,
1015 Defining_Identifier => PAT,
1016 Subtype_Indication =>
1017 Make_Subtype_Indication (Loc,
1018 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1019 Constraint =>
1020 Make_Index_Or_Discriminant_Constraint (Loc,
1021 Constraints => New_List (
1022 Make_Range (Loc,
1023 Low_Bound =>
1024 Make_Integer_Literal (Loc, 0),
1025 High_Bound =>
1026 Convert_To (Standard_Integer, PAT_High))))));
1028 Install_PAT;
1030 -- Currently the code in this unit requires that packed arrays
1031 -- represented by non-modular arrays of bytes be on a byte
1032 -- boundary for bit sizes handled by System.Pack_nn units.
1033 -- That's because these units assume the array being accessed
1034 -- starts on a byte boundary.
1036 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1037 Set_Must_Be_On_Byte_Boundary (Typ);
1038 end if;
1039 end if;
1040 end Create_Packed_Array_Impl_Type;
1042 -----------------------------------
1043 -- Expand_Bit_Packed_Element_Set --
1044 -----------------------------------
1046 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1047 Loc : constant Source_Ptr := Sloc (N);
1048 Lhs : constant Node_Id := Name (N);
1050 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1051 -- Used to preserve assignment OK status when assignment is rewritten
1053 Expr : Node_Id;
1055 Rhs : Node_Id := Expression (N);
1056 -- Initially Rhs is the right hand side value, it will be replaced
1057 -- later by an appropriate unchecked conversion for the assignment.
1059 Obj : Node_Id;
1060 Atyp : Entity_Id;
1061 PAT : Entity_Id;
1062 Ctyp : Entity_Id;
1063 Csiz : Int;
1064 Cmask : Uint;
1066 Shift : Node_Id;
1067 -- The expression for the shift value that is required
1069 Shift_Used : Boolean := False;
1070 -- Set True if Shift has been used in the generated code at least once,
1071 -- so that it must be duplicated if used again.
1073 New_Lhs : Node_Id;
1074 New_Rhs : Node_Id;
1076 Rhs_Val_Known : Boolean;
1077 Rhs_Val : Uint;
1078 -- If the value of the right hand side as an integer constant is
1079 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1080 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1081 -- the Rhs_Val is undefined.
1083 function Get_Shift return Node_Id;
1084 -- Function used to get the value of Shift, making sure that it
1085 -- gets duplicated if the function is called more than once.
1087 ---------------
1088 -- Get_Shift --
1089 ---------------
1091 function Get_Shift return Node_Id is
1092 begin
1093 -- If we used the shift value already, then duplicate it. We
1094 -- set a temporary parent in case actions have to be inserted.
1096 if Shift_Used then
1097 Set_Parent (Shift, N);
1098 return Duplicate_Subexpr_No_Checks (Shift);
1100 -- If first time, use Shift unchanged, and set flag for first use
1102 else
1103 Shift_Used := True;
1104 return Shift;
1105 end if;
1106 end Get_Shift;
1108 -- Start of processing for Expand_Bit_Packed_Element_Set
1110 begin
1111 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1113 Obj := Relocate_Node (Prefix (Lhs));
1114 Convert_To_Actual_Subtype (Obj);
1115 Atyp := Etype (Obj);
1116 PAT := Packed_Array_Impl_Type (Atyp);
1117 Ctyp := Component_Type (Atyp);
1118 Csiz := UI_To_Int (Component_Size (Atyp));
1120 -- We remove side effects, in case the rhs modifies the lhs, because we
1121 -- are about to transform the rhs into an expression that first READS
1122 -- the lhs, so we can do the necessary shifting and masking. Example:
1123 -- "X(2) := F(...);" where F modifies X(3). Otherwise, the side effect
1124 -- will be lost.
1126 Remove_Side_Effects (Rhs);
1128 -- We convert the right hand side to the proper subtype to ensure
1129 -- that an appropriate range check is made (since the normal range
1130 -- check from assignment will be lost in the transformations). This
1131 -- conversion is analyzed immediately so that subsequent processing
1132 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1134 -- If the right-hand side is a string literal, create a temporary for
1135 -- it, constant-folding is not ready to wrap the bit representation
1136 -- of a string literal.
1138 if Nkind (Rhs) = N_String_Literal then
1139 declare
1140 Decl : Node_Id;
1141 begin
1142 Decl :=
1143 Make_Object_Declaration (Loc,
1144 Defining_Identifier => Make_Temporary (Loc, 'T', Rhs),
1145 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1146 Expression => New_Copy_Tree (Rhs));
1148 Insert_Actions (N, New_List (Decl));
1149 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1150 end;
1151 end if;
1153 Rhs := Convert_To (Ctyp, Rhs);
1154 Set_Parent (Rhs, N);
1156 -- If we are building the initialization procedure for a packed array,
1157 -- and Initialize_Scalars is enabled, each component assignment is an
1158 -- out-of-range value by design. Compile this value without checks,
1159 -- because a call to the array init_proc must not raise an exception.
1161 -- Condition is not consistent with description above, Within_Init_Proc
1162 -- is True also when we are building the IP for a record or protected
1163 -- type that has a packed array component???
1165 if Within_Init_Proc
1166 and then Initialize_Scalars
1167 then
1168 Analyze_And_Resolve (Rhs, Ctyp, Suppress => All_Checks);
1169 else
1170 Analyze_And_Resolve (Rhs, Ctyp);
1171 end if;
1173 -- If any of the indices has a nonstandard representation, introduce
1174 -- the proper Rep_To_Pos conversion, which in turn will generate index
1175 -- checks when needed. We do this on a copy of the index expression,
1176 -- rather that rewriting the LHS altogether.
1178 Expr := First (Expressions (Lhs));
1179 while Present (Expr) loop
1180 declare
1181 Expr_Typ : constant Entity_Id := Etype (Expr);
1182 Loc : constant Source_Ptr := Sloc (Expr);
1184 Expr_Copy : Node_Id;
1186 begin
1187 if Is_Enumeration_Type (Expr_Typ)
1188 and then Has_Non_Standard_Rep (Expr_Typ)
1189 then
1190 Expr_Copy :=
1191 Make_Attribute_Reference (Loc,
1192 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
1193 Attribute_Name => Name_Pos,
1194 Expressions => New_List (Relocate_Node (Expr)));
1195 Set_Parent (Expr_Copy, N);
1196 Analyze_And_Resolve (Expr_Copy, Standard_Natural);
1197 end if;
1198 end;
1200 Next (Expr);
1201 end loop;
1203 -- Case of component size 1,2,4 or any component size for the modular
1204 -- case. These are the cases for which we can inline the code.
1206 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1207 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1208 then
1209 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1211 -- The statement to be generated is:
1213 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, Shift)))
1215 -- or in the case of a freestanding Reverse_Storage_Order object,
1217 -- Obj := Swap (atyp!((Swap (Obj) and Mask1)
1218 -- or (shift_left (rhs, Shift))))
1220 -- where Mask1 is obtained by shifting Cmask left Shift bits
1221 -- and then complementing the result.
1223 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1225 -- the "or ..." is omitted if rhs is constant and all 0 bits
1227 -- rhs is converted to the appropriate type
1229 -- The result is converted back to the array type, since
1230 -- otherwise we lose knowledge of the packed nature.
1232 -- Determine if right side is all 0 bits or all 1 bits
1234 if Compile_Time_Known_Value (Rhs) then
1235 Rhs_Val := Expr_Rep_Value (Rhs);
1236 Rhs_Val_Known := True;
1238 -- The following test catches the case of an unchecked conversion of
1239 -- an integer literal. This results from optimizing aggregates of
1240 -- packed types.
1242 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1243 and then Compile_Time_Known_Value (Expression (Rhs))
1244 then
1245 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1246 Rhs_Val_Known := True;
1248 else
1249 Rhs_Val := No_Uint;
1250 Rhs_Val_Known := False;
1251 end if;
1253 -- Some special checks for the case where the right hand value is
1254 -- known at compile time. Basically we have to take care of the
1255 -- implicit conversion to the subtype of the component object.
1257 if Rhs_Val_Known then
1259 -- If we have a biased component type then we must manually do the
1260 -- biasing, since we are taking responsibility in this case for
1261 -- constructing the exact bit pattern to be used.
1263 if Has_Biased_Representation (Ctyp) then
1264 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1265 end if;
1267 -- For a negative value, we manually convert the two's complement
1268 -- value to a corresponding unsigned value, so that the proper
1269 -- field width is maintained. If we did not do this, we would
1270 -- get too many leading sign bits later on.
1272 if Rhs_Val < 0 then
1273 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1274 end if;
1275 end if;
1277 -- Now create copies removing side effects. Note that in some complex
1278 -- cases, this may cause the fact that we have already set a packed
1279 -- array type on Obj to get lost. So we save the type of Obj, and
1280 -- make sure it is reset properly.
1282 declare
1283 T : constant Entity_Id := Etype (Obj);
1284 begin
1285 New_Lhs := Duplicate_Subexpr (Obj, Name_Req => True);
1286 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1287 Set_Etype (Obj, T);
1288 Set_Etype (New_Lhs, T);
1289 Set_Etype (New_Rhs, T);
1290 end;
1292 -- First we deal with the "and"
1294 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1295 declare
1296 Mask1 : Node_Id;
1297 Lit : Node_Id;
1299 begin
1300 if Compile_Time_Known_Value (Shift) then
1301 Mask1 :=
1302 Make_Integer_Literal (Loc,
1303 Modulus (Etype (Obj)) - 1 -
1304 (Cmask * (2 ** Expr_Value (Get_Shift))));
1305 Set_Print_In_Hex (Mask1);
1307 else
1308 Lit := Make_Integer_Literal (Loc, Cmask);
1309 Set_Print_In_Hex (Lit);
1310 Mask1 :=
1311 Make_Op_Not (Loc,
1312 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1313 end if;
1315 New_Rhs :=
1316 Make_Op_And (Loc,
1317 Left_Opnd => New_Rhs,
1318 Right_Opnd => Mask1);
1319 end;
1320 end if;
1322 -- Then deal with the "or"
1324 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1325 declare
1326 Or_Rhs : Node_Id;
1328 procedure Fixup_Rhs;
1329 -- Adjust Rhs by bias if biased representation for components
1330 -- or remove extraneous high order sign bits if signed.
1332 procedure Fixup_Rhs is
1333 Etyp : constant Entity_Id := Etype (Rhs);
1335 begin
1336 -- For biased case, do the required biasing by simply
1337 -- converting to the biased subtype (the conversion
1338 -- will generate the required bias).
1340 if Has_Biased_Representation (Ctyp) then
1341 Rhs := Convert_To (Ctyp, Rhs);
1343 -- For a signed integer type that is not biased, generate
1344 -- a conversion to unsigned to strip high order sign bits.
1346 elsif Is_Signed_Integer_Type (Ctyp) then
1347 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1348 end if;
1350 -- Set Etype, since it can be referenced before the node is
1351 -- completely analyzed.
1353 Set_Etype (Rhs, Etyp);
1355 -- We now need to do an unchecked conversion of the
1356 -- result to the target type, but it is important that
1357 -- this conversion be a right justified conversion and
1358 -- not a left justified conversion.
1360 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1361 end Fixup_Rhs;
1363 begin
1364 if Rhs_Val_Known
1365 and then Compile_Time_Known_Value (Get_Shift)
1366 then
1367 Or_Rhs :=
1368 Make_Integer_Literal (Loc,
1369 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1370 Set_Print_In_Hex (Or_Rhs);
1372 else
1373 -- We have to convert the right hand side to Etype (Obj).
1374 -- A special case arises if what we have now is a Val
1375 -- attribute reference whose expression type is Etype (Obj).
1376 -- This happens for assignments of fields from the same
1377 -- array. In this case we get the required right hand side
1378 -- by simply removing the inner attribute reference.
1380 if Nkind (Rhs) = N_Attribute_Reference
1381 and then Attribute_Name (Rhs) = Name_Val
1382 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1383 then
1384 Rhs := Relocate_Node (First (Expressions (Rhs)));
1385 Fixup_Rhs;
1387 -- If the value of the right hand side is a known integer
1388 -- value, then just replace it by an untyped constant,
1389 -- which will be properly retyped when we analyze and
1390 -- resolve the expression.
1392 elsif Rhs_Val_Known then
1394 -- Note that Rhs_Val has already been normalized to
1395 -- be an unsigned value with the proper number of bits.
1397 Rhs := Make_Integer_Literal (Loc, Rhs_Val);
1399 -- Otherwise we need an unchecked conversion
1401 else
1402 Fixup_Rhs;
1403 end if;
1405 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1406 end if;
1408 if Nkind (New_Rhs) = N_Op_And then
1409 Set_Paren_Count (New_Rhs, 1);
1410 Set_Etype (New_Rhs, Etype (Left_Opnd (New_Rhs)));
1411 end if;
1413 New_Rhs :=
1414 Make_Op_Or (Loc,
1415 Left_Opnd => New_Rhs,
1416 Right_Opnd => Or_Rhs);
1417 end;
1418 end if;
1420 -- Now do the rewrite
1422 Rewrite (N,
1423 Make_Assignment_Statement (Loc,
1424 Name => New_Lhs,
1425 Expression =>
1426 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1427 Set_Assignment_OK (Name (N), Ass_OK);
1429 -- All other component sizes for non-modular case
1431 else
1432 -- We generate
1434 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1436 -- where Subscr is the computed linear subscript
1438 declare
1439 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1440 Set_nn : Entity_Id;
1441 Subscr : Node_Id;
1442 Atyp : Entity_Id;
1443 Rev_SSO : Node_Id;
1445 begin
1446 if No (Bits_nn) then
1448 -- Error, most likely High_Integrity_Mode restriction
1450 return;
1451 end if;
1453 -- Acquire proper Set entity. We use the aligned or unaligned
1454 -- case as appropriate.
1456 if Known_Aligned_Enough (Obj, Csiz) then
1457 Set_nn := RTE (Set_Id (Csiz));
1458 else
1459 Set_nn := RTE (SetU_Id (Csiz));
1460 end if;
1462 -- Now generate the set reference
1464 Obj := Relocate_Node (Prefix (Lhs));
1465 Convert_To_Actual_Subtype (Obj);
1466 Atyp := Etype (Obj);
1467 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1469 -- Set indication of whether the packed array has reverse SSO
1471 Rev_SSO :=
1472 New_Occurrence_Of
1473 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1475 -- Below we must make the assumption that Obj is
1476 -- at least byte aligned, since otherwise its address
1477 -- cannot be taken. The assumption holds since the
1478 -- only arrays that can be misaligned are small packed
1479 -- arrays which are implemented as a modular type, and
1480 -- that is not the case here.
1482 Rewrite (N,
1483 Make_Procedure_Call_Statement (Loc,
1484 Name => New_Occurrence_Of (Set_nn, Loc),
1485 Parameter_Associations => New_List (
1486 Make_Attribute_Reference (Loc,
1487 Prefix => Obj,
1488 Attribute_Name => Name_Address),
1489 Subscr,
1490 Unchecked_Convert_To (Bits_nn, Convert_To (Ctyp, Rhs)),
1491 Rev_SSO)));
1493 end;
1494 end if;
1496 Analyze (N, Suppress => All_Checks);
1497 end Expand_Bit_Packed_Element_Set;
1499 -------------------------------------
1500 -- Expand_Packed_Address_Reference --
1501 -------------------------------------
1503 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1504 Loc : constant Source_Ptr := Sloc (N);
1505 Base : Node_Id;
1506 Offset : Node_Id;
1508 begin
1509 -- We build an expression that has the form
1511 -- outer_object'Address
1512 -- + (linear-subscript * component_size for each array reference
1513 -- + field'Bit_Position for each record field
1514 -- + ...
1515 -- + ...) / Storage_Unit;
1517 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1519 Rewrite (N,
1520 Unchecked_Convert_To (RTE (RE_Address),
1521 Make_Op_Add (Loc,
1522 Left_Opnd =>
1523 Unchecked_Convert_To (RTE (RE_Integer_Address),
1524 Make_Attribute_Reference (Loc,
1525 Prefix => Base,
1526 Attribute_Name => Name_Address)),
1528 Right_Opnd =>
1529 Unchecked_Convert_To (RTE (RE_Integer_Address),
1530 Make_Op_Divide (Loc,
1531 Left_Opnd => Offset,
1532 Right_Opnd =>
1533 Make_Integer_Literal (Loc, System_Storage_Unit))))));
1535 Analyze_And_Resolve (N, RTE (RE_Address));
1536 end Expand_Packed_Address_Reference;
1538 ---------------------------------
1539 -- Expand_Packed_Bit_Reference --
1540 ---------------------------------
1542 procedure Expand_Packed_Bit_Reference (N : Node_Id) is
1543 Loc : constant Source_Ptr := Sloc (N);
1544 Base : Node_Id;
1545 Offset : Node_Id;
1547 begin
1548 -- We build an expression that has the form
1550 -- (linear-subscript * component_size for each array reference
1551 -- + field'Bit_Position for each record field
1552 -- + ...
1553 -- + ...) mod Storage_Unit;
1555 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1557 Rewrite (N,
1558 Unchecked_Convert_To (Standard_Natural,
1559 Make_Op_Mod (Loc,
1560 Left_Opnd => Offset,
1561 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
1563 Analyze_And_Resolve (N, Standard_Natural);
1564 end Expand_Packed_Bit_Reference;
1566 ------------------------------------
1567 -- Expand_Packed_Boolean_Operator --
1568 ------------------------------------
1570 -- This routine expands "a op b" for the packed cases
1572 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1573 Loc : constant Source_Ptr := Sloc (N);
1574 Typ : constant Entity_Id := Etype (N);
1575 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1576 R : Node_Id := Relocate_Node (Right_Opnd (N));
1578 Ltyp : Entity_Id;
1579 Rtyp : Entity_Id;
1580 PAT : Entity_Id;
1582 begin
1583 Convert_To_Actual_Subtype (L);
1584 Convert_To_Actual_Subtype (R);
1586 Ensure_Defined (Etype (L), N);
1587 Ensure_Defined (Etype (R), N);
1589 Apply_Length_Check (R, Etype (L));
1591 Ltyp := Etype (L);
1592 Rtyp := Etype (R);
1594 -- Deal with silly case of XOR where the subcomponent has a range
1595 -- True .. True where an exception must be raised.
1597 if Nkind (N) = N_Op_Xor then
1598 R := Duplicate_Subexpr (R);
1599 Silly_Boolean_Array_Xor_Test (N, R, Rtyp);
1600 end if;
1602 -- Now that silliness is taken care of, get packed array type
1604 Convert_To_PAT_Type (L);
1605 Convert_To_PAT_Type (R);
1607 PAT := Etype (L);
1609 -- For the modular case, we expand a op b into
1611 -- rtyp!(pat!(a) op pat!(b))
1613 -- where rtyp is the Etype of the left operand. Note that we do not
1614 -- convert to the base type, since this would be unconstrained, and
1615 -- hence not have a corresponding packed array type set.
1617 -- Note that both operands must be modular for this code to be used
1619 if Is_Modular_Integer_Type (PAT)
1620 and then
1621 Is_Modular_Integer_Type (Etype (R))
1622 then
1623 declare
1624 P : Node_Id;
1626 begin
1627 if Nkind (N) = N_Op_And then
1628 P := Make_Op_And (Loc, L, R);
1630 elsif Nkind (N) = N_Op_Or then
1631 P := Make_Op_Or (Loc, L, R);
1633 else -- Nkind (N) = N_Op_Xor
1634 P := Make_Op_Xor (Loc, L, R);
1635 end if;
1637 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1638 end;
1640 -- For the array case, we insert the actions
1642 -- Result : Ltype;
1644 -- System.Bit_Ops.Bit_And/Or/Xor
1645 -- (Left'Address,
1646 -- Ltype'Length * Ltype'Component_Size;
1647 -- Right'Address,
1648 -- Rtype'Length * Rtype'Component_Size
1649 -- Result'Address);
1651 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1652 -- the second argument and fourth arguments are the lengths of the
1653 -- operands in bits. Then we replace the expression by a reference
1654 -- to Result.
1656 -- Note that if we are mixing a modular and array operand, everything
1657 -- works fine, since we ensure that the modular representation has the
1658 -- same physical layout as the array representation (that's what the
1659 -- left justified modular stuff in the big-endian case is about).
1661 else
1662 declare
1663 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
1664 E_Id : RE_Id;
1666 begin
1667 if Nkind (N) = N_Op_And then
1668 E_Id := RE_Bit_And;
1670 elsif Nkind (N) = N_Op_Or then
1671 E_Id := RE_Bit_Or;
1673 else -- Nkind (N) = N_Op_Xor
1674 E_Id := RE_Bit_Xor;
1675 end if;
1677 Insert_Actions (N, New_List (
1679 Make_Object_Declaration (Loc,
1680 Defining_Identifier => Result_Ent,
1681 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1683 Make_Procedure_Call_Statement (Loc,
1684 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1685 Parameter_Associations => New_List (
1687 Make_Byte_Aligned_Attribute_Reference (Loc,
1688 Prefix => L,
1689 Attribute_Name => Name_Address),
1691 Make_Op_Multiply (Loc,
1692 Left_Opnd =>
1693 Make_Attribute_Reference (Loc,
1694 Prefix =>
1695 New_Occurrence_Of
1696 (Etype (First_Index (Ltyp)), Loc),
1697 Attribute_Name => Name_Range_Length),
1699 Right_Opnd =>
1700 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1702 Make_Byte_Aligned_Attribute_Reference (Loc,
1703 Prefix => R,
1704 Attribute_Name => Name_Address),
1706 Make_Op_Multiply (Loc,
1707 Left_Opnd =>
1708 Make_Attribute_Reference (Loc,
1709 Prefix =>
1710 New_Occurrence_Of
1711 (Etype (First_Index (Rtyp)), Loc),
1712 Attribute_Name => Name_Range_Length),
1714 Right_Opnd =>
1715 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1717 Make_Byte_Aligned_Attribute_Reference (Loc,
1718 Prefix => New_Occurrence_Of (Result_Ent, Loc),
1719 Attribute_Name => Name_Address)))));
1721 Rewrite (N,
1722 New_Occurrence_Of (Result_Ent, Loc));
1723 end;
1724 end if;
1726 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1727 end Expand_Packed_Boolean_Operator;
1729 -------------------------------------
1730 -- Expand_Packed_Element_Reference --
1731 -------------------------------------
1733 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1734 Loc : constant Source_Ptr := Sloc (N);
1735 Obj : Node_Id;
1736 Atyp : Entity_Id;
1737 PAT : Entity_Id;
1738 Ctyp : Entity_Id;
1739 Csiz : Int;
1740 Shift : Node_Id;
1741 Cmask : Uint;
1742 Lit : Node_Id;
1743 Arg : Node_Id;
1745 begin
1746 -- If the node is an actual in a call, the prefix has not been fully
1747 -- expanded, to account for the additional expansion for in-out actuals
1748 -- (see expand_actuals for details). If the prefix itself is a packed
1749 -- reference as well, we have to recurse to complete the transformation
1750 -- of the prefix.
1752 if Nkind (Prefix (N)) = N_Indexed_Component
1753 and then not Analyzed (Prefix (N))
1754 and then Is_Bit_Packed_Array (Etype (Prefix (Prefix (N))))
1755 then
1756 Expand_Packed_Element_Reference (Prefix (N));
1757 end if;
1759 -- The prefix may be rewritten below as a conversion. If it is a source
1760 -- entity generate reference to it now, to prevent spurious warnings
1761 -- about unused entities.
1763 if Is_Entity_Name (Prefix (N))
1764 and then Comes_From_Source (Prefix (N))
1765 then
1766 Generate_Reference (Entity (Prefix (N)), Prefix (N), 'r');
1767 end if;
1769 -- If not bit packed, we have the enumeration case, which is easily
1770 -- dealt with (just adjust the subscripts of the indexed component)
1772 -- Note: this leaves the result as an indexed component, which is
1773 -- still a variable, so can be used in the assignment case, as is
1774 -- required in the enumeration case.
1776 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1777 Setup_Enumeration_Packed_Array_Reference (N);
1778 return;
1779 end if;
1781 -- Remaining processing is for the bit-packed case
1783 Obj := Relocate_Node (Prefix (N));
1784 Convert_To_Actual_Subtype (Obj);
1785 Atyp := Etype (Obj);
1786 PAT := Packed_Array_Impl_Type (Atyp);
1787 Ctyp := Component_Type (Atyp);
1788 Csiz := UI_To_Int (Component_Size (Atyp));
1790 -- Case of component size 1,2,4 or any component size for the modular
1791 -- case. These are the cases for which we can inline the code.
1793 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1794 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1795 then
1796 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1797 Lit := Make_Integer_Literal (Loc, Cmask);
1798 Set_Print_In_Hex (Lit);
1800 -- We generate a shift right to position the field, followed by a
1801 -- masking operation to extract the bit field, and we finally do an
1802 -- unchecked conversion to convert the result to the required target.
1804 -- Note that the unchecked conversion automatically deals with the
1805 -- bias if we are dealing with a biased representation. What will
1806 -- happen is that we temporarily generate the biased representation,
1807 -- but almost immediately that will be converted to the original
1808 -- unbiased component type, and the bias will disappear.
1810 Arg :=
1811 Make_Op_And (Loc,
1812 Left_Opnd => Make_Shift_Right (Obj, Shift),
1813 Right_Opnd => Lit);
1814 Set_Etype (Arg, Ctyp);
1816 -- Component extraction is performed on a native endianness scalar
1817 -- value: if Atyp has reverse storage order, then it has been byte
1818 -- swapped, and if the component being extracted is itself of a
1819 -- composite type with reverse storage order, then we need to swap
1820 -- it back to its expected endianness after extraction.
1822 if Reverse_Storage_Order (Atyp)
1823 and then (Is_Record_Type (Ctyp) or else Is_Array_Type (Ctyp))
1824 and then Reverse_Storage_Order (Ctyp)
1825 then
1826 Arg := Revert_Storage_Order (Arg);
1827 end if;
1829 -- We needed to analyze this before we do the unchecked convert
1830 -- below, but we need it temporarily attached to the tree for
1831 -- this analysis (hence the temporary Set_Parent call).
1833 Set_Parent (Arg, Parent (N));
1834 Analyze_And_Resolve (Arg);
1836 Rewrite (N, RJ_Unchecked_Convert_To (Ctyp, Arg));
1838 -- All other component sizes for non-modular case
1840 else
1841 -- We generate
1843 -- Component_Type!(Get_nn (Arr'address, Subscr))
1845 -- where Subscr is the computed linear subscript
1847 declare
1848 Get_nn : Entity_Id;
1849 Subscr : Node_Id;
1850 Rev_SSO : constant Node_Id :=
1851 New_Occurrence_Of
1852 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1854 begin
1855 -- Acquire proper Get entity. We use the aligned or unaligned
1856 -- case as appropriate.
1858 if Known_Aligned_Enough (Obj, Csiz) then
1859 Get_nn := RTE (Get_Id (Csiz));
1860 else
1861 Get_nn := RTE (GetU_Id (Csiz));
1862 end if;
1864 -- Now generate the get reference
1866 Compute_Linear_Subscript (Atyp, N, Subscr);
1868 -- Below we make the assumption that Obj is at least byte
1869 -- aligned, since otherwise its address cannot be taken.
1870 -- The assumption holds since the only arrays that can be
1871 -- misaligned are small packed arrays which are implemented
1872 -- as a modular type, and that is not the case here.
1874 Rewrite (N,
1875 Unchecked_Convert_To (Ctyp,
1876 Make_Function_Call (Loc,
1877 Name => New_Occurrence_Of (Get_nn, Loc),
1878 Parameter_Associations => New_List (
1879 Make_Attribute_Reference (Loc,
1880 Prefix => Obj,
1881 Attribute_Name => Name_Address),
1882 Subscr,
1883 Rev_SSO))));
1884 end;
1885 end if;
1887 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1888 end Expand_Packed_Element_Reference;
1890 ----------------------
1891 -- Expand_Packed_Eq --
1892 ----------------------
1894 -- Handles expansion of "=" on packed array types
1896 procedure Expand_Packed_Eq (N : Node_Id) is
1897 Loc : constant Source_Ptr := Sloc (N);
1898 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1899 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1901 LLexpr : Node_Id;
1902 RLexpr : Node_Id;
1904 Ltyp : Entity_Id;
1905 Rtyp : Entity_Id;
1906 PAT : Entity_Id;
1908 begin
1909 Convert_To_Actual_Subtype (L);
1910 Convert_To_Actual_Subtype (R);
1911 Ltyp := Underlying_Type (Etype (L));
1912 Rtyp := Underlying_Type (Etype (R));
1914 Convert_To_PAT_Type (L);
1915 Convert_To_PAT_Type (R);
1916 PAT := Etype (L);
1918 LLexpr :=
1919 Make_Op_Multiply (Loc,
1920 Left_Opnd => Compute_Number_Components (N, Ltyp),
1921 Right_Opnd => Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1923 RLexpr :=
1924 Make_Op_Multiply (Loc,
1925 Left_Opnd => Compute_Number_Components (N, Rtyp),
1926 Right_Opnd => Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1928 -- For the modular case, we transform the comparison to:
1930 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1932 -- where PAT is the packed array type. This works fine, since in the
1933 -- modular case we guarantee that the unused bits are always zeroes.
1934 -- We do have to compare the lengths because we could be comparing
1935 -- two different subtypes of the same base type. We can only do this
1936 -- if the PATs on both sides are modular (in which case they are
1937 -- necessarily structurally the same -- same Modulus and so on);
1938 -- otherwise, we have a case where the right operand is not of
1939 -- compile time known size.
1941 if Is_Modular_Integer_Type (PAT)
1942 and then Is_Modular_Integer_Type (Etype (R))
1943 then
1944 pragma Assert (RM_Size (Etype (R)) = RM_Size (PAT));
1945 pragma Assert (Modulus (Etype (R)) = Modulus (PAT));
1947 Rewrite (N,
1948 Make_And_Then (Loc,
1949 Left_Opnd =>
1950 Make_Op_Eq (Loc,
1951 Left_Opnd => LLexpr,
1952 Right_Opnd => RLexpr),
1954 Right_Opnd =>
1955 Make_Op_Eq (Loc,
1956 Left_Opnd => L,
1957 Right_Opnd => R)));
1959 -- For the non-modular case, we call a runtime routine
1961 -- System.Bit_Ops.Bit_Eq
1962 -- (L'Address, L_Length, R'Address, R_Length)
1964 -- where PAT is the packed array type, and the lengths are the lengths
1965 -- in bits of the original packed arrays. This routine takes care of
1966 -- not comparing the unused bits in the last byte.
1968 else
1969 Rewrite (N,
1970 Make_Function_Call (Loc,
1971 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1972 Parameter_Associations => New_List (
1973 Make_Byte_Aligned_Attribute_Reference (Loc,
1974 Prefix => L,
1975 Attribute_Name => Name_Address),
1977 LLexpr,
1979 Make_Byte_Aligned_Attribute_Reference (Loc,
1980 Prefix => R,
1981 Attribute_Name => Name_Address),
1983 RLexpr)));
1984 end if;
1986 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
1987 end Expand_Packed_Eq;
1989 -----------------------
1990 -- Expand_Packed_Not --
1991 -----------------------
1993 -- Handles expansion of "not" on packed array types
1995 procedure Expand_Packed_Not (N : Node_Id) is
1996 Loc : constant Source_Ptr := Sloc (N);
1997 Typ : constant Entity_Id := Etype (N);
1998 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2000 Rtyp : Entity_Id;
2001 PAT : Entity_Id;
2002 Lit : Node_Id;
2003 Size : Unat;
2005 begin
2006 Convert_To_Actual_Subtype (Opnd);
2007 Rtyp := Etype (Opnd);
2009 -- Deal with silly False..False and True..True subtype case
2011 Silly_Boolean_Array_Not_Test (N, Rtyp);
2013 -- Now that the silliness is taken care of, get packed array type
2015 Convert_To_PAT_Type (Opnd);
2016 PAT := Etype (Opnd);
2018 -- For the case where the packed array type is a modular type, "not A"
2019 -- expands simply into:
2021 -- Rtyp!(PAT!(A) xor Mask)
2023 -- where PAT is the packed array type, Mask is a mask of all 1 bits of
2024 -- length equal to the size of this packed type, and Rtyp is the actual
2025 -- actual subtype of the operand. Preserve old behavior in case size is
2026 -- not set.
2028 if Known_RM_Size (PAT) then
2029 Size := RM_Size (PAT);
2030 else
2031 Size := Uint_0;
2032 end if;
2033 Lit := Make_Integer_Literal (Loc, 2 ** Size - 1);
2034 Set_Print_In_Hex (Lit);
2036 if not Is_Array_Type (PAT) then
2037 Rewrite (N,
2038 Unchecked_Convert_To (Rtyp,
2039 Make_Op_Xor (Loc,
2040 Left_Opnd => Opnd,
2041 Right_Opnd => Lit)));
2043 -- For the array case, we insert the actions
2045 -- Result : Typ;
2047 -- System.Bit_Ops.Bit_Not
2048 -- (Opnd'Address,
2049 -- Typ'Length * Typ'Component_Size,
2050 -- Result'Address);
2052 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second argument
2053 -- is the length of the operand in bits. We then replace the expression
2054 -- with a reference to Result.
2056 else
2057 declare
2058 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
2060 begin
2061 Insert_Actions (N, New_List (
2062 Make_Object_Declaration (Loc,
2063 Defining_Identifier => Result_Ent,
2064 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2066 Make_Procedure_Call_Statement (Loc,
2067 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2068 Parameter_Associations => New_List (
2069 Make_Byte_Aligned_Attribute_Reference (Loc,
2070 Prefix => Opnd,
2071 Attribute_Name => Name_Address),
2073 Make_Op_Multiply (Loc,
2074 Left_Opnd =>
2075 Make_Attribute_Reference (Loc,
2076 Prefix =>
2077 New_Occurrence_Of
2078 (Etype (First_Index (Rtyp)), Loc),
2079 Attribute_Name => Name_Range_Length),
2081 Right_Opnd =>
2082 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2084 Make_Byte_Aligned_Attribute_Reference (Loc,
2085 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2086 Attribute_Name => Name_Address)))));
2088 Rewrite (N, New_Occurrence_Of (Result_Ent, Loc));
2089 end;
2090 end if;
2092 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2093 end Expand_Packed_Not;
2095 -----------------------------
2096 -- Get_Base_And_Bit_Offset --
2097 -----------------------------
2099 procedure Get_Base_And_Bit_Offset
2100 (N : Node_Id;
2101 Base : out Node_Id;
2102 Offset : out Node_Id)
2104 Loc : Source_Ptr;
2105 Term : Node_Id;
2106 Atyp : Entity_Id;
2107 Subscr : Node_Id;
2109 begin
2110 Base := N;
2111 Offset := Empty;
2113 -- We build up an expression serially that has the form
2115 -- linear-subscript * component_size for each array reference
2116 -- + field'Bit_Position for each record field
2117 -- + ...
2119 loop
2120 Loc := Sloc (Base);
2122 if Nkind (Base) = N_Indexed_Component then
2123 Convert_To_Actual_Subtype (Prefix (Base));
2124 Atyp := Etype (Prefix (Base));
2125 Compute_Linear_Subscript (Atyp, Base, Subscr);
2127 Term :=
2128 Make_Op_Multiply (Loc,
2129 Left_Opnd => Subscr,
2130 Right_Opnd =>
2131 Make_Attribute_Reference (Loc,
2132 Prefix => New_Occurrence_Of (Atyp, Loc),
2133 Attribute_Name => Name_Component_Size));
2135 elsif Nkind (Base) = N_Selected_Component then
2136 Term :=
2137 Make_Attribute_Reference (Loc,
2138 Prefix => Selector_Name (Base),
2139 Attribute_Name => Name_Bit_Position);
2141 else
2142 return;
2143 end if;
2145 if No (Offset) then
2146 Offset := Term;
2148 else
2149 Offset :=
2150 Make_Op_Add (Loc,
2151 Left_Opnd => Offset,
2152 Right_Opnd => Term);
2153 end if;
2155 Base := Prefix (Base);
2156 end loop;
2157 end Get_Base_And_Bit_Offset;
2159 -------------------------------------
2160 -- Involves_Packed_Array_Reference --
2161 -------------------------------------
2163 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2164 begin
2165 if Nkind (N) = N_Indexed_Component
2166 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2167 then
2168 return True;
2170 elsif Nkind (N) = N_Selected_Component then
2171 return Involves_Packed_Array_Reference (Prefix (N));
2173 else
2174 return False;
2175 end if;
2176 end Involves_Packed_Array_Reference;
2178 --------------------------
2179 -- Known_Aligned_Enough --
2180 --------------------------
2182 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2183 Typ : constant Entity_Id := Etype (Obj);
2185 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2186 -- If the component is in a record that contains previous packed
2187 -- components, consider it unaligned because the back-end might
2188 -- choose to pack the rest of the record. Lead to less efficient code,
2189 -- but safer vis-a-vis of back-end choices.
2191 --------------------------------
2192 -- In_Partially_Packed_Record --
2193 --------------------------------
2195 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2196 Rec_Type : constant Entity_Id := Scope (Comp);
2197 Prev_Comp : Entity_Id;
2199 begin
2200 Prev_Comp := First_Entity (Rec_Type);
2201 while Present (Prev_Comp) loop
2202 if Is_Packed (Etype (Prev_Comp)) then
2203 return True;
2205 elsif Prev_Comp = Comp then
2206 return False;
2207 end if;
2209 Next_Entity (Prev_Comp);
2210 end loop;
2212 return False;
2213 end In_Partially_Packed_Record;
2215 -- Start of processing for Known_Aligned_Enough
2217 begin
2218 -- Odd bit sizes don't need alignment anyway
2220 if Csiz mod 2 = 1 then
2221 return True;
2223 -- If we have a specified alignment, see if it is sufficient, if not
2224 -- then we can't possibly be aligned enough in any case.
2226 elsif Known_Alignment (Etype (Obj)) then
2227 -- Alignment required is 4 if size is a multiple of 4, and
2228 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2230 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2231 return False;
2232 end if;
2233 end if;
2235 -- OK, alignment should be sufficient, if object is aligned
2237 -- If object is strictly aligned, then it is definitely aligned
2239 if Strict_Alignment (Typ) then
2240 return True;
2242 -- Case of subscripted array reference
2244 elsif Nkind (Obj) = N_Indexed_Component then
2246 -- If we have a pointer to an array, then this is definitely
2247 -- aligned, because pointers always point to aligned versions.
2249 if Is_Access_Type (Etype (Prefix (Obj))) then
2250 return True;
2252 -- Otherwise, go look at the prefix
2254 else
2255 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2256 end if;
2258 -- Case of record field
2260 elsif Nkind (Obj) = N_Selected_Component then
2262 -- What is significant here is whether the record type is packed
2264 if Is_Record_Type (Etype (Prefix (Obj)))
2265 and then Is_Packed (Etype (Prefix (Obj)))
2266 then
2267 return False;
2269 -- Or the component has a component clause which might cause
2270 -- the component to become unaligned (we can't tell if the
2271 -- backend is doing alignment computations).
2273 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2274 return False;
2276 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2277 return False;
2279 -- In all other cases, go look at prefix
2281 else
2282 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2283 end if;
2285 elsif Nkind (Obj) = N_Type_Conversion then
2286 return Known_Aligned_Enough (Expression (Obj), Csiz);
2288 -- For a formal parameter, it is safer to assume that it is not
2289 -- aligned, because the formal may be unconstrained while the actual
2290 -- is constrained. In this situation, a small constrained packed
2291 -- array, represented in modular form, may be unaligned.
2293 elsif Is_Entity_Name (Obj) then
2294 return not Is_Formal (Entity (Obj));
2295 else
2297 -- If none of the above, must be aligned
2298 return True;
2299 end if;
2300 end Known_Aligned_Enough;
2302 ---------------------
2303 -- Make_Shift_Left --
2304 ---------------------
2306 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2307 Nod : Node_Id;
2309 begin
2310 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2311 return N;
2312 else
2313 Nod :=
2314 Make_Op_Shift_Left (Sloc (N),
2315 Left_Opnd => N,
2316 Right_Opnd => S);
2317 Set_Shift_Count_OK (Nod, True);
2318 return Nod;
2319 end if;
2320 end Make_Shift_Left;
2322 ----------------------
2323 -- Make_Shift_Right --
2324 ----------------------
2326 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2327 Nod : Node_Id;
2329 begin
2330 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2331 return N;
2332 else
2333 Nod :=
2334 Make_Op_Shift_Right (Sloc (N),
2335 Left_Opnd => N,
2336 Right_Opnd => S);
2337 Set_Shift_Count_OK (Nod, True);
2338 return Nod;
2339 end if;
2340 end Make_Shift_Right;
2342 -----------------------------
2343 -- RJ_Unchecked_Convert_To --
2344 -----------------------------
2346 function RJ_Unchecked_Convert_To
2347 (Typ : Entity_Id;
2348 Expr : Node_Id) return Node_Id
2350 Source_Typ : constant Entity_Id := Etype (Expr);
2351 Target_Typ : constant Entity_Id := Typ;
2353 Src : Node_Id := Expr;
2355 Source_Siz : Nat;
2356 Target_Siz : Nat;
2358 begin
2359 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2360 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2362 -- For a little-endian target type stored byte-swapped on a
2363 -- big-endian machine, do not mask to Target_Siz bits.
2365 if Bytes_Big_Endian
2366 and then (Is_Record_Type (Target_Typ)
2367 or else
2368 Is_Array_Type (Target_Typ))
2369 and then Reverse_Storage_Order (Target_Typ)
2370 then
2371 Source_Siz := Target_Siz;
2372 end if;
2374 -- First step, if the source type is not a discrete type, then we first
2375 -- convert to a modular type of the source length, since otherwise, on
2376 -- a big-endian machine, we get left-justification. We do it for little-
2377 -- endian machines as well, because there might be junk bits that are
2378 -- not cleared if the type is not numeric. This can be done only if the
2379 -- source siz is different from 0 (i.e. known), otherwise we must trust
2380 -- the type declarations (case of non-discrete components).
2382 if Source_Siz /= 0
2383 and then Source_Siz /= Target_Siz
2384 and then not Is_Discrete_Type (Source_Typ)
2385 then
2386 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2387 end if;
2389 -- In the big endian case, if the lengths of the two types differ, then
2390 -- we must worry about possible left justification in the conversion,
2391 -- and avoiding that is what this is all about.
2393 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2395 -- Next step. If the target is not a discrete type, then we first
2396 -- convert to a modular type of the target length, since otherwise,
2397 -- on a big-endian machine, we get left-justification.
2399 if not Is_Discrete_Type (Target_Typ) then
2400 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2401 end if;
2402 end if;
2404 -- And now we can do the final conversion to the target type
2406 return Unchecked_Convert_To (Target_Typ, Src);
2407 end RJ_Unchecked_Convert_To;
2409 ----------------------------------------------
2410 -- Setup_Enumeration_Packed_Array_Reference --
2411 ----------------------------------------------
2413 -- All we have to do here is to find the subscripts that correspond to the
2414 -- index positions that have non-standard enumeration types and insert a
2415 -- Pos attribute to get the proper subscript value.
2417 -- Finally the prefix must be uncheck-converted to the corresponding packed
2418 -- array type.
2420 -- Note that the component type is unchanged, so we do not need to fiddle
2421 -- with the types (Gigi always automatically takes the packed array type if
2422 -- it is set, as it will be in this case).
2424 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2425 Pfx : constant Node_Id := Prefix (N);
2426 Typ : constant Entity_Id := Etype (N);
2427 Exprs : constant List_Id := Expressions (N);
2428 Expr : Node_Id;
2430 begin
2431 -- If the array is unconstrained, then we replace the array reference
2432 -- with its actual subtype. This actual subtype will have a packed array
2433 -- type with appropriate bounds.
2435 if not Is_Constrained (Packed_Array_Impl_Type (Etype (Pfx))) then
2436 Convert_To_Actual_Subtype (Pfx);
2437 end if;
2439 Expr := First (Exprs);
2440 while Present (Expr) loop
2441 declare
2442 Loc : constant Source_Ptr := Sloc (Expr);
2443 Expr_Typ : constant Entity_Id := Etype (Expr);
2445 begin
2446 if Is_Enumeration_Type (Expr_Typ)
2447 and then Has_Non_Standard_Rep (Expr_Typ)
2448 then
2449 Rewrite (Expr,
2450 Make_Attribute_Reference (Loc,
2451 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2452 Attribute_Name => Name_Pos,
2453 Expressions => New_List (Relocate_Node (Expr))));
2454 Analyze_And_Resolve (Expr, Standard_Natural);
2455 end if;
2456 end;
2458 Next (Expr);
2459 end loop;
2461 Rewrite (N,
2462 Make_Indexed_Component (Sloc (N),
2463 Prefix =>
2464 Unchecked_Convert_To (Packed_Array_Impl_Type (Etype (Pfx)), Pfx),
2465 Expressions => Exprs));
2467 Analyze_And_Resolve (N, Typ);
2468 end Setup_Enumeration_Packed_Array_Reference;
2470 -----------------------------------------
2471 -- Setup_Inline_Packed_Array_Reference --
2472 -----------------------------------------
2474 procedure Setup_Inline_Packed_Array_Reference
2475 (N : Node_Id;
2476 Atyp : Entity_Id;
2477 Obj : in out Node_Id;
2478 Cmask : out Uint;
2479 Shift : out Node_Id)
2481 Loc : constant Source_Ptr := Sloc (N);
2482 PAT : Entity_Id;
2483 Otyp : Entity_Id;
2484 Csiz : Uint;
2485 Osiz : Uint;
2487 begin
2488 Csiz := Component_Size (Atyp);
2490 Convert_To_PAT_Type (Obj);
2491 PAT := Etype (Obj);
2493 Cmask := 2 ** Csiz - 1;
2495 if Is_Array_Type (PAT) then
2496 Otyp := Component_Type (PAT);
2497 Osiz := Component_Size (PAT);
2499 else
2500 Otyp := PAT;
2502 -- In the case where the PAT is a modular type, we want the actual
2503 -- size in bits of the modular value we use. This is neither the
2504 -- Object_Size nor the Value_Size, either of which may have been
2505 -- reset to strange values, but rather the minimum size. Note that
2506 -- since this is a modular type with full range, the issue of
2507 -- biased representation does not arise.
2509 Osiz := UI_From_Int (Minimum_Size (Otyp));
2510 end if;
2512 Compute_Linear_Subscript (Atyp, N, Shift);
2514 -- If the component size is not 1, then the subscript must be multiplied
2515 -- by the component size to get the shift count.
2517 if Csiz /= 1 then
2518 Shift :=
2519 Make_Op_Multiply (Loc,
2520 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2521 Right_Opnd => Shift);
2522 end if;
2524 -- If we have the array case, then this shift count must be broken down
2525 -- into a byte subscript, and a shift within the byte.
2527 if Is_Array_Type (PAT) then
2529 declare
2530 New_Shift : Node_Id;
2532 begin
2533 -- We must analyze shift, since we will duplicate it
2535 Set_Parent (Shift, N);
2536 Analyze_And_Resolve
2537 (Shift, Standard_Integer, Suppress => All_Checks);
2539 -- The shift count within the word is
2540 -- shift mod Osiz
2542 New_Shift :=
2543 Make_Op_Mod (Loc,
2544 Left_Opnd => Duplicate_Subexpr (Shift),
2545 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2547 -- The subscript to be used on the PAT array is
2548 -- shift / Osiz
2550 Obj :=
2551 Make_Indexed_Component (Loc,
2552 Prefix => Obj,
2553 Expressions => New_List (
2554 Make_Op_Divide (Loc,
2555 Left_Opnd => Duplicate_Subexpr (Shift),
2556 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2558 Shift := New_Shift;
2559 end;
2561 -- For the modular integer case, the object to be manipulated is the
2562 -- entire array, so Obj is unchanged. Note that we will reset its type
2563 -- to PAT before returning to the caller.
2565 else
2566 null;
2567 end if;
2569 -- The one remaining step is to modify the shift count for the
2570 -- big-endian case. Consider the following example in a byte:
2572 -- xxxxxxxx bits of byte
2573 -- vvvvvvvv bits of value
2574 -- 33221100 little-endian numbering
2575 -- 00112233 big-endian numbering
2577 -- Here we have the case of 2-bit fields
2579 -- For the little-endian case, we already have the proper shift count
2580 -- set, e.g. for element 2, the shift count is 2*2 = 4.
2582 -- For the big endian case, we have to adjust the shift count, computing
2583 -- it as (N - F) - Shift, where N is the number of bits in an element of
2584 -- the array used to implement the packed array, F is the number of bits
2585 -- in a source array element, and Shift is the count so far computed.
2587 -- We also have to adjust if the storage order is reversed
2589 if Bytes_Big_Endian xor Reverse_Storage_Order (Base_Type (Atyp)) then
2590 Shift :=
2591 Make_Op_Subtract (Loc,
2592 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2593 Right_Opnd => Shift);
2594 end if;
2596 Set_Parent (Shift, N);
2597 Set_Parent (Obj, N);
2598 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2599 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2601 -- Make sure final type of object is the appropriate packed type
2603 Set_Etype (Obj, Otyp);
2605 end Setup_Inline_Packed_Array_Reference;
2607 end Exp_Pakd;