1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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
;
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
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 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
);
94 -- Given an expression of a packed array type, builds a corresponding
95 -- expression whose type is the implementation type used to represent
96 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
98 procedure Get_Base_And_Bit_Offset
101 Offset
: out Node_Id
);
102 -- Given a node N for a name which involves a packed array reference,
103 -- return the base object of the reference and build an expression of
104 -- type Standard.Integer representing the zero-based offset in bits
105 -- from Base'Address to the first bit of the reference.
107 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean;
108 -- There are two versions of the Set routines, the ones used when the
109 -- object is known to be sufficiently well aligned given the number of
110 -- bits, and the ones used when the object is not known to be aligned.
111 -- This routine is used to determine which set to use. Obj is a reference
112 -- to the object, and Csiz is the component size of the packed array.
113 -- True is returned if the alignment of object is known to be sufficient,
114 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
117 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
118 -- Build a left shift node, checking for the case of a shift count of zero
120 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
121 -- Build a right shift node, checking for the case of a shift count of zero
123 function RJ_Unchecked_Convert_To
125 Expr
: Node_Id
) return Node_Id
;
126 -- The packed array code does unchecked conversions which in some cases
127 -- may involve non-discrete types with differing sizes. The semantics of
128 -- such conversions is potentially endianness dependent, and the effect
129 -- we want here for such a conversion is to do the conversion in size as
130 -- though numeric items are involved, and we extend or truncate on the
131 -- left side. This happens naturally in the little-endian case, but in
132 -- the big endian case we can get left justification, when what we want
133 -- is right justification. This routine does the unchecked conversion in
134 -- a stepwise manner to ensure that it gives the expected result. Hence
135 -- the name (RJ = Right justified). The parameters Typ and Expr are as
136 -- for the case of a normal Unchecked_Convert_To call.
138 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
);
139 -- This routine is called in the Get and Set case for arrays that are
140 -- packed but not bit-packed, meaning that they have at least one
141 -- subscript that is of an enumeration type with a non-standard
142 -- representation. This routine modifies the given node to properly
143 -- reference the corresponding packed array type.
145 procedure Setup_Inline_Packed_Array_Reference
148 Obj
: in out Node_Id
;
150 Shift
: out Node_Id
);
151 -- This procedure performs common processing on the N_Indexed_Component
152 -- parameter given as N, whose prefix is a reference to a packed array.
153 -- This is used for the get and set when the component size is 1, 2, 4,
154 -- or for other component sizes when the packed array type is a modular
155 -- type (i.e. the cases that are handled with inline code).
159 -- N is the N_Indexed_Component node for the packed array reference
161 -- Atyp is the constrained array type (the actual subtype has been
162 -- computed if necessary to obtain the constraints, but this is still
163 -- the original array type, not the Packed_Array_Impl_Type value).
165 -- Obj is the object which is to be indexed. It is always of type Atyp.
169 -- Obj is the object containing the desired bit field. It is of type
170 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
171 -- entire value, for the small static case, or the proper selected byte
172 -- from the array in the large or dynamic case. This node is analyzed
173 -- and resolved on return.
175 -- Shift is a node representing the shift count to be used in the
176 -- rotate right instruction that positions the field for access.
177 -- This node is analyzed and resolved on return.
179 -- Cmask is a mask corresponding to the width of the component field.
180 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
182 -- Note: in some cases the call to this routine may generate actions
183 -- (for handling multi-use references and the generation of the packed
184 -- array type on the fly). Such actions are inserted into the tree
185 -- directly using Insert_Action.
187 function Revert_Storage_Order
(N
: Node_Id
) return Node_Id
;
188 -- Perform appropriate justification and byte ordering adjustments for N,
189 -- an element of a packed array type, when both the component type and
190 -- the enclosing packed array type have reverse scalar storage order.
191 -- On little-endian targets, the value is left justified before byte
192 -- swapping. The Etype of the returned expression is an integer type of
193 -- an appropriate power-of-2 size.
195 --------------------------
196 -- Revert_Storage_Order --
197 --------------------------
199 function Revert_Storage_Order
(N
: Node_Id
) return Node_Id
is
200 Loc
: constant Source_Ptr
:= Sloc
(N
);
201 T
: constant Entity_Id
:= Etype
(N
);
202 T_Size
: constant Uint
:= RM_Size
(T
);
216 -- Array component size is less than a byte: no swapping needed
219 Swap_T
:= RTE
(RE_Unsigned_8
);
222 -- Select byte swapping function depending on array component size
225 Swap_RE
:= RE_Bswap_16
;
227 elsif T_Size
<= 32 then
228 Swap_RE
:= RE_Bswap_32
;
230 else pragma Assert
(T_Size
<= 64);
231 Swap_RE
:= RE_Bswap_64
;
234 Swap_F
:= RTE
(Swap_RE
);
235 Swap_T
:= Etype
(Swap_F
);
239 Shift
:= Esize
(Swap_T
) - T_Size
;
241 Arg
:= RJ_Unchecked_Convert_To
(Swap_T
, N
);
243 if not Bytes_Big_Endian
and then Shift
> Uint_0
then
245 Make_Op_Shift_Left
(Loc
,
247 Right_Opnd
=> Make_Integer_Literal
(Loc
, Shift
));
250 if Present
(Swap_F
) then
252 Make_Function_Call
(Loc
,
253 Name
=> New_Occurrence_Of
(Swap_F
, Loc
),
254 Parameter_Associations
=> New_List
(Arg
));
259 Set_Etype
(Adjusted
, Swap_T
);
261 end Revert_Storage_Order
;
263 ------------------------------
264 -- Compute_Linear_Subscript --
265 ------------------------------
267 procedure Compute_Linear_Subscript
270 Subscr
: out Node_Id
)
272 Loc
: constant Source_Ptr
:= Sloc
(N
);
281 -- Loop through dimensions
283 Indx
:= First_Index
(Atyp
);
284 Oldsub
:= First
(Expressions
(N
));
286 while Present
(Indx
) loop
287 Styp
:= Etype
(Indx
);
288 Newsub
:= Relocate_Node
(Oldsub
);
290 -- Get expression for the subscript value. First, if Do_Range_Check
291 -- is set on a subscript, then we must do a range check against the
292 -- original bounds (not the bounds of the packed array type). We do
293 -- this by introducing a subtype conversion.
295 if Do_Range_Check
(Newsub
)
296 and then Etype
(Newsub
) /= Styp
298 Newsub
:= Convert_To
(Styp
, Newsub
);
301 -- Now evolve the expression for the subscript. First convert
302 -- the subscript to be zero based and of an integer type.
304 -- Case of integer type, where we just subtract to get lower bound
306 if Is_Integer_Type
(Styp
) then
308 -- If length of integer type is smaller than standard integer,
309 -- then we convert to integer first, then do the subtract
311 -- Integer (subscript) - Integer (Styp'First)
313 if Esize
(Styp
) < Esize
(Standard_Integer
) then
315 Make_Op_Subtract
(Loc
,
316 Left_Opnd
=> Convert_To
(Standard_Integer
, Newsub
),
318 Convert_To
(Standard_Integer
,
319 Make_Attribute_Reference
(Loc
,
320 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
321 Attribute_Name
=> Name_First
)));
323 -- For larger integer types, subtract first, then convert to
324 -- integer, this deals with strange long long integer bounds.
326 -- Integer (subscript - Styp'First)
330 Convert_To
(Standard_Integer
,
331 Make_Op_Subtract
(Loc
,
334 Make_Attribute_Reference
(Loc
,
335 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
336 Attribute_Name
=> Name_First
)));
339 -- For the enumeration case, we have to use 'Pos to get the value
340 -- to work with before subtracting the lower bound.
342 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
344 -- This is not quite right for bizarre cases where the size of the
345 -- enumeration type is > Integer'Size bits due to rep clause ???
348 pragma Assert
(Is_Enumeration_Type
(Styp
));
351 Make_Op_Subtract
(Loc
,
352 Left_Opnd
=> Convert_To
(Standard_Integer
,
353 Make_Attribute_Reference
(Loc
,
354 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
355 Attribute_Name
=> Name_Pos
,
356 Expressions
=> New_List
(Newsub
))),
359 Convert_To
(Standard_Integer
,
360 Make_Attribute_Reference
(Loc
,
361 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
362 Attribute_Name
=> Name_Pos
,
363 Expressions
=> New_List
(
364 Make_Attribute_Reference
(Loc
,
365 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
366 Attribute_Name
=> Name_First
)))));
369 Set_Paren_Count
(Newsub
, 1);
371 -- For the first subscript, we just copy that subscript value
376 -- Otherwise, we must multiply what we already have by the current
377 -- stride and then add in the new value to the evolving subscript.
383 Make_Op_Multiply
(Loc
,
386 Make_Attribute_Reference
(Loc
,
387 Attribute_Name
=> Name_Range_Length
,
388 Prefix
=> New_Occurrence_Of
(Styp
, Loc
))),
389 Right_Opnd
=> Newsub
);
392 -- Move to next subscript
397 end Compute_Linear_Subscript
;
399 -------------------------
400 -- Convert_To_PAT_Type --
401 -------------------------
403 -- The PAT is always obtained from the actual subtype
405 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
) is
409 Convert_To_Actual_Subtype
(Aexp
);
410 Act_ST
:= Underlying_Type
(Etype
(Aexp
));
411 Create_Packed_Array_Impl_Type
(Act_ST
);
413 -- Just replace the etype with the packed array type. This works because
414 -- the expression will not be further analyzed, and Gigi considers the
415 -- two types equivalent in any case.
417 -- This is not strictly the case ??? If the reference is an actual in
418 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
419 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
420 -- array reference, reanalysis can produce spurious type errors when the
421 -- PAT type is replaced again with the original type of the array. Same
422 -- for the case of a dereference. Ditto for function calls: expansion
423 -- may introduce additional actuals which will trigger errors if call is
424 -- reanalyzed. The following is correct and minimal, but the handling of
425 -- more complex packed expressions in actuals is confused. Probably the
426 -- problem only remains for actuals in calls.
428 Set_Etype
(Aexp
, Packed_Array_Impl_Type
(Act_ST
));
430 if Is_Entity_Name
(Aexp
)
432 (Nkind
(Aexp
) = N_Indexed_Component
433 and then Is_Entity_Name
(Prefix
(Aexp
)))
434 or else Nkind_In
(Aexp
, N_Explicit_Dereference
, N_Function_Call
)
438 end Convert_To_PAT_Type
;
440 -----------------------------------
441 -- Create_Packed_Array_Impl_Type --
442 -----------------------------------
444 procedure Create_Packed_Array_Impl_Type
(Typ
: Entity_Id
) is
445 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
446 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
447 Csize
: constant Uint
:= Component_Size
(Typ
);
462 procedure Install_PAT
;
463 -- This procedure is called with Decl set to the declaration for the
464 -- packed array type. It creates the type and installs it as required.
466 procedure Set_PB_Type
;
467 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
468 -- requirements (see documentation in the spec of this package).
474 procedure Install_PAT
is
475 Pushed_Scope
: Boolean := False;
478 -- We do not want to put the declaration we have created in the tree
479 -- since it is often hard, and sometimes impossible to find a proper
480 -- place for it (the impossible case arises for a packed array type
481 -- with bounds depending on the discriminant, a declaration cannot
482 -- be put inside the record, and the reference to the discriminant
483 -- cannot be outside the record).
485 -- The solution is to analyze the declaration while temporarily
486 -- attached to the tree at an appropriate point, and then we install
487 -- the resulting type as an Itype in the packed array type field of
488 -- the original type, so that no explicit declaration is required.
490 -- Note: the packed type is created in the scope of its parent type.
491 -- There are at least some cases where the current scope is deeper,
492 -- and so when this is the case, we temporarily reset the scope
493 -- for the definition. This is clearly safe, since the first use
494 -- of the packed array type will be the implicit reference from
495 -- the corresponding unpacked type when it is elaborated.
497 if Is_Itype
(Typ
) then
498 Set_Parent
(Decl
, Associated_Node_For_Itype
(Typ
));
500 Set_Parent
(Decl
, Declaration_Node
(Typ
));
503 if Scope
(Typ
) /= Current_Scope
then
504 Push_Scope
(Scope
(Typ
));
505 Pushed_Scope
:= True;
508 Set_Is_Itype
(PAT
, True);
509 Set_Packed_Array_Impl_Type
(Typ
, PAT
);
510 Analyze
(Decl
, Suppress
=> All_Checks
);
516 -- Set Esize and RM_Size to the actual size of the packed object
517 -- Do not reset RM_Size if already set, as happens in the case of
520 if Unknown_Esize
(PAT
) then
521 Set_Esize
(PAT
, PASize
);
524 if Unknown_RM_Size
(PAT
) then
525 Set_RM_Size
(PAT
, PASize
);
528 Adjust_Esize_Alignment
(PAT
);
530 -- Set remaining fields of packed array type
532 Init_Alignment
(PAT
);
533 Set_Parent
(PAT
, Empty
);
534 Set_Associated_Node_For_Itype
(PAT
, Typ
);
535 Set_Is_Packed_Array_Impl_Type
(PAT
, True);
536 Set_Original_Array_Type
(PAT
, Typ
);
538 -- For a non-bit-packed array, propagate reverse storage order
539 -- flag from original base type to packed array base type.
541 if not Is_Bit_Packed_Array
(Typ
) then
542 Set_Reverse_Storage_Order
543 (Etype
(PAT
), Reverse_Storage_Order
(Base_Type
(Typ
)));
546 -- We definitely do not want to delay freezing for packed array
547 -- types. This is of particular importance for the itypes that are
548 -- generated for record components depending on discriminants where
549 -- there is no place to put the freeze node.
551 Set_Has_Delayed_Freeze
(PAT
, False);
552 Set_Has_Delayed_Freeze
(Etype
(PAT
), False);
554 -- If we did allocate a freeze node, then clear out the reference
555 -- since it is obsolete (should we delete the freeze node???)
557 Set_Freeze_Node
(PAT
, Empty
);
558 Set_Freeze_Node
(Etype
(PAT
), Empty
);
565 procedure Set_PB_Type
is
567 -- If the user has specified an explicit alignment for the
568 -- type or component, take it into account.
570 if Csize
<= 2 or else Csize
= 4 or else Csize
mod 2 /= 0
571 or else Alignment
(Typ
) = 1
572 or else Component_Alignment
(Typ
) = Calign_Storage_Unit
574 PB_Type
:= RTE
(RE_Packed_Bytes1
);
576 elsif Csize
mod 4 /= 0
577 or else Alignment
(Typ
) = 2
579 PB_Type
:= RTE
(RE_Packed_Bytes2
);
582 PB_Type
:= RTE
(RE_Packed_Bytes4
);
586 -- Start of processing for Create_Packed_Array_Impl_Type
589 -- If we already have a packed array type, nothing to do
591 if Present
(Packed_Array_Impl_Type
(Typ
)) then
595 -- If our immediate ancestor subtype is constrained, and it already
596 -- has a packed array type, then just share the same type, since the
597 -- bounds must be the same. If the ancestor is not an array type but
598 -- a private type, as can happen with multiple instantiations, create
599 -- a new packed type, to avoid privacy issues.
601 if Ekind
(Typ
) = E_Array_Subtype
then
602 Ancest
:= Ancestor_Subtype
(Typ
);
605 and then Is_Array_Type
(Ancest
)
606 and then Is_Constrained
(Ancest
)
607 and then Present
(Packed_Array_Impl_Type
(Ancest
))
609 Set_Packed_Array_Impl_Type
(Typ
, Packed_Array_Impl_Type
(Ancest
));
614 -- We preset the result type size from the size of the original array
615 -- type, since this size clearly belongs to the packed array type. The
616 -- size of the conceptual unpacked type is always set to unknown.
618 PASize
:= RM_Size
(Typ
);
620 -- Case of an array where at least one index is of an enumeration
621 -- type with a non-standard representation, but the component size
622 -- is not appropriate for bit packing. This is the case where we
623 -- have Is_Packed set (we would never be in this unit otherwise),
624 -- but Is_Bit_Packed_Array is false.
626 -- Note that if the component size is appropriate for bit packing,
627 -- then the circuit for the computation of the subscript properly
628 -- deals with the non-standard enumeration type case by taking the
631 if not Is_Bit_Packed_Array
(Typ
) then
633 -- Here we build a declaration:
635 -- type tttP is array (index1, index2, ...) of component_type
637 -- where index1, index2, are the index types. These are the same
638 -- as the index types of the original array, except for the non-
639 -- standard representation enumeration type case, where we have
642 -- For the unconstrained array case, we use
646 -- For the constrained case, we use
648 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
649 -- Enum_Type'Pos (Enum_Type'Last);
651 -- Note that tttP is created even if no index subtype is a non
652 -- standard enumeration, because we still need to remove padding
653 -- normally inserted for component alignment.
656 Make_Defining_Identifier
(Loc
,
657 Chars
=> New_External_Name
(Chars
(Typ
), 'P'));
659 Set_Packed_Array_Impl_Type
(Typ
, PAT
);
662 Indexes
: constant List_Id
:= New_List
;
664 Indx_Typ
: Entity_Id
;
669 Indx
:= First_Index
(Typ
);
671 while Present
(Indx
) loop
672 Indx_Typ
:= Etype
(Indx
);
674 Enum_Case
:= Is_Enumeration_Type
(Indx_Typ
)
675 and then Has_Non_Standard_Rep
(Indx_Typ
);
677 -- Unconstrained case
679 if not Is_Constrained
(Typ
) then
681 Indx_Typ
:= Standard_Natural
;
684 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
689 if not Enum_Case
then
690 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
694 Make_Subtype_Indication
(Loc
,
696 New_Occurrence_Of
(Standard_Natural
, Loc
),
698 Make_Range_Constraint
(Loc
,
702 Make_Attribute_Reference
(Loc
,
704 New_Occurrence_Of
(Indx_Typ
, Loc
),
705 Attribute_Name
=> Name_Pos
,
706 Expressions
=> New_List
(
707 Make_Attribute_Reference
(Loc
,
709 New_Occurrence_Of
(Indx_Typ
, Loc
),
710 Attribute_Name
=> Name_First
))),
713 Make_Attribute_Reference
(Loc
,
715 New_Occurrence_Of
(Indx_Typ
, Loc
),
716 Attribute_Name
=> Name_Pos
,
717 Expressions
=> New_List
(
718 Make_Attribute_Reference
(Loc
,
720 New_Occurrence_Of
(Indx_Typ
, Loc
),
721 Attribute_Name
=> Name_Last
)))))));
729 if not Is_Constrained
(Typ
) then
731 Make_Unconstrained_Array_Definition
(Loc
,
732 Subtype_Marks
=> Indexes
,
733 Component_Definition
=>
734 Make_Component_Definition
(Loc
,
735 Aliased_Present
=> False,
736 Subtype_Indication
=>
737 New_Occurrence_Of
(Ctyp
, Loc
)));
741 Make_Constrained_Array_Definition
(Loc
,
742 Discrete_Subtype_Definitions
=> Indexes
,
743 Component_Definition
=>
744 Make_Component_Definition
(Loc
,
745 Aliased_Present
=> False,
746 Subtype_Indication
=>
747 New_Occurrence_Of
(Ctyp
, Loc
)));
751 Make_Full_Type_Declaration
(Loc
,
752 Defining_Identifier
=> PAT
,
753 Type_Definition
=> Typedef
);
756 -- Set type as packed array type and install it
758 Set_Is_Packed_Array_Impl_Type
(PAT
);
762 -- Case of bit-packing required for unconstrained array. We create
763 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
765 elsif not Is_Constrained
(Typ
) then
767 Make_Defining_Identifier
(Loc
,
768 Chars
=> Make_Packed_Array_Impl_Type_Name
(Typ
, Csize
));
770 Set_Packed_Array_Impl_Type
(Typ
, PAT
);
774 Make_Subtype_Declaration
(Loc
,
775 Defining_Identifier
=> PAT
,
776 Subtype_Indication
=> New_Occurrence_Of
(PB_Type
, Loc
));
780 -- Remaining code is for the case of bit-packing for constrained array
782 -- The name of the packed array subtype is
786 -- where sss is the component size in bits and ttt is the name of
787 -- the parent packed type.
791 Make_Defining_Identifier
(Loc
,
792 Chars
=> Make_Packed_Array_Impl_Type_Name
(Typ
, Csize
));
794 Set_Packed_Array_Impl_Type
(Typ
, PAT
);
796 -- Build an expression for the length of the array in bits.
797 -- This is the product of the length of each of the dimensions
803 Len_Expr
:= Empty
; -- suppress junk warning
807 Make_Attribute_Reference
(Loc
,
808 Attribute_Name
=> Name_Length
,
809 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
810 Expressions
=> New_List
(
811 Make_Integer_Literal
(Loc
, J
)));
818 Make_Op_Multiply
(Loc
,
819 Left_Opnd
=> Len_Expr
,
820 Right_Opnd
=> Len_Dim
);
824 exit when J
> Number_Dimensions
(Typ
);
828 -- Temporarily attach the length expression to the tree and analyze
829 -- and resolve it, so that we can test its value. We assume that the
830 -- total length fits in type Integer. This expression may involve
831 -- discriminants, so we treat it as a default/per-object expression.
833 Set_Parent
(Len_Expr
, Typ
);
834 Preanalyze_Spec_Expression
(Len_Expr
, Standard_Long_Long_Integer
);
836 -- Use a modular type if possible. We can do this if we have
837 -- static bounds, and the length is small enough, and the length
838 -- is not zero. We exclude the zero length case because the size
839 -- of things is always at least one, and the zero length object
840 -- would have an anomalous size.
842 if Compile_Time_Known_Value
(Len_Expr
) then
843 Len_Bits
:= Expr_Value
(Len_Expr
) * Csize
;
845 -- Check for size known to be too large
848 Uint_2
** (Standard_Integer_Size
- 1) * System_Storage_Unit
850 if System_Storage_Unit
= 8 then
852 ("packed array size cannot exceed " &
853 "Integer''Last bytes", Typ
);
856 ("packed array size cannot exceed " &
857 "Integer''Last storage units", Typ
);
860 -- Reset length to arbitrary not too high value to continue
862 Len_Expr
:= Make_Integer_Literal
(Loc
, 65535);
863 Analyze_And_Resolve
(Len_Expr
, Standard_Long_Long_Integer
);
866 -- We normally consider small enough to mean no larger than the
867 -- value of System_Max_Binary_Modulus_Power, checking that in the
868 -- case of values longer than word size, we have long shifts.
872 (Len_Bits
<= System_Word_Size
873 or else (Len_Bits
<= System_Max_Binary_Modulus_Power
874 and then Support_Long_Shifts_On_Target
))
876 -- We can use the modular type, it has the form:
878 -- subtype tttPn is btyp
879 -- range 0 .. 2 ** ((Typ'Length (1)
880 -- * ... * Typ'Length (n)) * Csize) - 1;
882 -- The bounds are statically known, and btyp is one of the
883 -- unsigned types, depending on the length.
885 if Len_Bits
<= Standard_Short_Short_Integer_Size
then
886 Btyp
:= RTE
(RE_Short_Short_Unsigned
);
888 elsif Len_Bits
<= Standard_Short_Integer_Size
then
889 Btyp
:= RTE
(RE_Short_Unsigned
);
891 elsif Len_Bits
<= Standard_Integer_Size
then
892 Btyp
:= RTE
(RE_Unsigned
);
894 elsif Len_Bits
<= Standard_Long_Integer_Size
then
895 Btyp
:= RTE
(RE_Long_Unsigned
);
898 Btyp
:= RTE
(RE_Long_Long_Unsigned
);
901 Lit
:= Make_Integer_Literal
(Loc
, 2 ** Len_Bits
- 1);
902 Set_Print_In_Hex
(Lit
);
905 Make_Subtype_Declaration
(Loc
,
906 Defining_Identifier
=> PAT
,
907 Subtype_Indication
=>
908 Make_Subtype_Indication
(Loc
,
909 Subtype_Mark
=> New_Occurrence_Of
(Btyp
, Loc
),
912 Make_Range_Constraint
(Loc
,
916 Make_Integer_Literal
(Loc
, 0),
917 High_Bound
=> Lit
))));
919 if PASize
= Uint_0
then
925 -- Propagate a given alignment to the modular type. This can
926 -- cause it to be under-aligned, but that's OK.
928 if Present
(Alignment_Clause
(Typ
)) then
929 Set_Alignment
(PAT
, Alignment
(Typ
));
936 -- Could not use a modular type, for all other cases, we build
937 -- a packed array subtype:
940 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
942 -- Bits is the length of the array in bits
949 Make_Op_Multiply
(Loc
,
951 Make_Integer_Literal
(Loc
, Csize
),
952 Right_Opnd
=> Len_Expr
),
955 Make_Integer_Literal
(Loc
, 7));
957 Set_Paren_Count
(Bits_U1
, 1);
960 Make_Op_Subtract
(Loc
,
963 Left_Opnd
=> Bits_U1
,
964 Right_Opnd
=> Make_Integer_Literal
(Loc
, 8)),
965 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
968 Make_Subtype_Declaration
(Loc
,
969 Defining_Identifier
=> PAT
,
970 Subtype_Indication
=>
971 Make_Subtype_Indication
(Loc
,
972 Subtype_Mark
=> New_Occurrence_Of
(PB_Type
, Loc
),
974 Make_Index_Or_Discriminant_Constraint
(Loc
,
975 Constraints
=> New_List
(
978 Make_Integer_Literal
(Loc
, 0),
980 Convert_To
(Standard_Integer
, PAT_High
))))));
984 -- Currently the code in this unit requires that packed arrays
985 -- represented by non-modular arrays of bytes be on a byte
986 -- boundary for bit sizes handled by System.Pack_nn units.
987 -- That's because these units assume the array being accessed
988 -- starts on a byte boundary.
990 if Get_Id
(UI_To_Int
(Csize
)) /= RE_Null
then
991 Set_Must_Be_On_Byte_Boundary
(Typ
);
994 end Create_Packed_Array_Impl_Type
;
996 -----------------------------------
997 -- Expand_Bit_Packed_Element_Set --
998 -----------------------------------
1000 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
) is
1001 Loc
: constant Source_Ptr
:= Sloc
(N
);
1002 Lhs
: constant Node_Id
:= Name
(N
);
1004 Ass_OK
: constant Boolean := Assignment_OK
(Lhs
);
1005 -- Used to preserve assignment OK status when assignment is rewritten
1007 Rhs
: Node_Id
:= Expression
(N
);
1008 -- Initially Rhs is the right hand side value, it will be replaced
1009 -- later by an appropriate unchecked conversion for the assignment.
1019 -- The expression for the shift value that is required
1021 Shift_Used
: Boolean := False;
1022 -- Set True if Shift has been used in the generated code at least once,
1023 -- so that it must be duplicated if used again.
1028 Rhs_Val_Known
: Boolean;
1030 -- If the value of the right hand side as an integer constant is
1031 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1032 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1033 -- the Rhs_Val is undefined.
1035 function Get_Shift
return Node_Id
;
1036 -- Function used to get the value of Shift, making sure that it
1037 -- gets duplicated if the function is called more than once.
1043 function Get_Shift
return Node_Id
is
1045 -- If we used the shift value already, then duplicate it. We
1046 -- set a temporary parent in case actions have to be inserted.
1049 Set_Parent
(Shift
, N
);
1050 return Duplicate_Subexpr_No_Checks
(Shift
);
1052 -- If first time, use Shift unchanged, and set flag for first use
1060 -- Start of processing for Expand_Bit_Packed_Element_Set
1063 pragma Assert
(Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
))));
1065 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1066 Convert_To_Actual_Subtype
(Obj
);
1067 Atyp
:= Etype
(Obj
);
1068 PAT
:= Packed_Array_Impl_Type
(Atyp
);
1069 Ctyp
:= Component_Type
(Atyp
);
1070 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1072 -- We remove side effects, in case the rhs modifies the lhs, because we
1073 -- are about to transform the rhs into an expression that first READS
1074 -- the lhs, so we can do the necessary shifting and masking. Example:
1075 -- "X(2) := F(...);" where F modifies X(3). Otherwise, the side effect
1078 Remove_Side_Effects
(Rhs
);
1080 -- We convert the right hand side to the proper subtype to ensure
1081 -- that an appropriate range check is made (since the normal range
1082 -- check from assignment will be lost in the transformations). This
1083 -- conversion is analyzed immediately so that subsequent processing
1084 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1086 -- If the right-hand side is a string literal, create a temporary for
1087 -- it, constant-folding is not ready to wrap the bit representation
1088 -- of a string literal.
1090 if Nkind
(Rhs
) = N_String_Literal
then
1095 Make_Object_Declaration
(Loc
,
1096 Defining_Identifier
=> Make_Temporary
(Loc
, 'T', Rhs
),
1097 Object_Definition
=> New_Occurrence_Of
(Ctyp
, Loc
),
1098 Expression
=> New_Copy_Tree
(Rhs
));
1100 Insert_Actions
(N
, New_List
(Decl
));
1101 Rhs
:= New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
);
1105 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1106 Set_Parent
(Rhs
, N
);
1108 -- If we are building the initialization procedure for a packed array,
1109 -- and Initialize_Scalars is enabled, each component assignment is an
1110 -- out-of-range value by design. Compile this value without checks,
1111 -- because a call to the array init_proc must not raise an exception.
1113 -- Condition is not consistent with description above, Within_Init_Proc
1114 -- is True also when we are building the IP for a record or protected
1115 -- type that has a packed array component???
1118 and then Initialize_Scalars
1120 Analyze_And_Resolve
(Rhs
, Ctyp
, Suppress
=> All_Checks
);
1122 Analyze_And_Resolve
(Rhs
, Ctyp
);
1125 -- For the AAMP target, indexing of certain packed array is passed
1126 -- through to the back end without expansion, because the expansion
1127 -- results in very inefficient code on that target. This allows the
1128 -- GNAAMP back end to generate specialized macros that support more
1129 -- efficient indexing of packed arrays with components having sizes
1130 -- that are small powers of two.
1133 and then (Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4)
1138 -- Case of component size 1,2,4 or any component size for the modular
1139 -- case. These are the cases for which we can inline the code.
1141 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1142 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1144 Setup_Inline_Packed_Array_Reference
(Lhs
, Atyp
, Obj
, Cmask
, Shift
);
1146 -- The statement to be generated is:
1148 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, Shift)))
1150 -- or in the case of a freestanding Reverse_Storage_Order object,
1152 -- Obj := Swap (atyp!((Swap (Obj) and Mask1)
1153 -- or (shift_left (rhs, Shift))))
1155 -- where Mask1 is obtained by shifting Cmask left Shift bits
1156 -- and then complementing the result.
1158 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1160 -- the "or ..." is omitted if rhs is constant and all 0 bits
1162 -- rhs is converted to the appropriate type
1164 -- The result is converted back to the array type, since
1165 -- otherwise we lose knowledge of the packed nature.
1167 -- Determine if right side is all 0 bits or all 1 bits
1169 if Compile_Time_Known_Value
(Rhs
) then
1170 Rhs_Val
:= Expr_Rep_Value
(Rhs
);
1171 Rhs_Val_Known
:= True;
1173 -- The following test catches the case of an unchecked conversion of
1174 -- an integer literal. This results from optimizing aggregates of
1177 elsif Nkind
(Rhs
) = N_Unchecked_Type_Conversion
1178 and then Compile_Time_Known_Value
(Expression
(Rhs
))
1180 Rhs_Val
:= Expr_Rep_Value
(Expression
(Rhs
));
1181 Rhs_Val_Known
:= True;
1185 Rhs_Val_Known
:= False;
1188 -- Some special checks for the case where the right hand value is
1189 -- known at compile time. Basically we have to take care of the
1190 -- implicit conversion to the subtype of the component object.
1192 if Rhs_Val_Known
then
1194 -- If we have a biased component type then we must manually do the
1195 -- biasing, since we are taking responsibility in this case for
1196 -- constructing the exact bit pattern to be used.
1198 if Has_Biased_Representation
(Ctyp
) then
1199 Rhs_Val
:= Rhs_Val
- Expr_Rep_Value
(Type_Low_Bound
(Ctyp
));
1202 -- For a negative value, we manually convert the two's complement
1203 -- value to a corresponding unsigned value, so that the proper
1204 -- field width is maintained. If we did not do this, we would
1205 -- get too many leading sign bits later on.
1208 Rhs_Val
:= 2 ** UI_From_Int
(Csiz
) + Rhs_Val
;
1212 -- Now create copies removing side effects. Note that in some complex
1213 -- cases, this may cause the fact that we have already set a packed
1214 -- array type on Obj to get lost. So we save the type of Obj, and
1215 -- make sure it is reset properly.
1217 New_Lhs
:= Duplicate_Subexpr
(Obj
, Name_Req
=> True);
1218 New_Rhs
:= Duplicate_Subexpr_No_Checks
(Obj
);
1220 -- First we deal with the "and"
1222 if not Rhs_Val_Known
or else Rhs_Val
/= Cmask
then
1228 if Compile_Time_Known_Value
(Shift
) then
1230 Make_Integer_Literal
(Loc
,
1231 Modulus
(Etype
(Obj
)) - 1 -
1232 (Cmask
* (2 ** Expr_Value
(Get_Shift
))));
1233 Set_Print_In_Hex
(Mask1
);
1236 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1237 Set_Print_In_Hex
(Lit
);
1240 Right_Opnd
=> Make_Shift_Left
(Lit
, Get_Shift
));
1245 Left_Opnd
=> New_Rhs
,
1246 Right_Opnd
=> Mask1
);
1250 -- Then deal with the "or"
1252 if not Rhs_Val_Known
or else Rhs_Val
/= 0 then
1256 procedure Fixup_Rhs
;
1257 -- Adjust Rhs by bias if biased representation for components
1258 -- or remove extraneous high order sign bits if signed.
1260 procedure Fixup_Rhs
is
1261 Etyp
: constant Entity_Id
:= Etype
(Rhs
);
1264 -- For biased case, do the required biasing by simply
1265 -- converting to the biased subtype (the conversion
1266 -- will generate the required bias).
1268 if Has_Biased_Representation
(Ctyp
) then
1269 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1271 -- For a signed integer type that is not biased, generate
1272 -- a conversion to unsigned to strip high order sign bits.
1274 elsif Is_Signed_Integer_Type
(Ctyp
) then
1275 Rhs
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Csiz
)), Rhs
);
1278 -- Set Etype, since it can be referenced before the node is
1279 -- completely analyzed.
1281 Set_Etype
(Rhs
, Etyp
);
1283 -- We now need to do an unchecked conversion of the
1284 -- result to the target type, but it is important that
1285 -- this conversion be a right justified conversion and
1286 -- not a left justified conversion.
1288 Rhs
:= RJ_Unchecked_Convert_To
(Etype
(Obj
), Rhs
);
1293 and then Compile_Time_Known_Value
(Get_Shift
)
1296 Make_Integer_Literal
(Loc
,
1297 Rhs_Val
* (2 ** Expr_Value
(Get_Shift
)));
1298 Set_Print_In_Hex
(Or_Rhs
);
1301 -- We have to convert the right hand side to Etype (Obj).
1302 -- A special case arises if what we have now is a Val
1303 -- attribute reference whose expression type is Etype (Obj).
1304 -- This happens for assignments of fields from the same
1305 -- array. In this case we get the required right hand side
1306 -- by simply removing the inner attribute reference.
1308 if Nkind
(Rhs
) = N_Attribute_Reference
1309 and then Attribute_Name
(Rhs
) = Name_Val
1310 and then Etype
(First
(Expressions
(Rhs
))) = Etype
(Obj
)
1312 Rhs
:= Relocate_Node
(First
(Expressions
(Rhs
)));
1315 -- If the value of the right hand side is a known integer
1316 -- value, then just replace it by an untyped constant,
1317 -- which will be properly retyped when we analyze and
1318 -- resolve the expression.
1320 elsif Rhs_Val_Known
then
1322 -- Note that Rhs_Val has already been normalized to
1323 -- be an unsigned value with the proper number of bits.
1325 Rhs
:= Make_Integer_Literal
(Loc
, Rhs_Val
);
1327 -- Otherwise we need an unchecked conversion
1333 Or_Rhs
:= Make_Shift_Left
(Rhs
, Get_Shift
);
1336 if Nkind
(New_Rhs
) = N_Op_And
then
1337 Set_Paren_Count
(New_Rhs
, 1);
1338 Set_Etype
(New_Rhs
, Etype
(Left_Opnd
(New_Rhs
)));
1343 Left_Opnd
=> New_Rhs
,
1344 Right_Opnd
=> Or_Rhs
);
1348 -- Now do the rewrite
1351 Make_Assignment_Statement
(Loc
,
1354 Unchecked_Convert_To
(Etype
(New_Lhs
), New_Rhs
)));
1355 Set_Assignment_OK
(Name
(N
), Ass_OK
);
1357 -- All other component sizes for non-modular case
1362 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1364 -- where Subscr is the computed linear subscript
1367 Bits_nn
: constant Entity_Id
:= RTE
(Bits_Id
(Csiz
));
1374 if No
(Bits_nn
) then
1376 -- Error, most likely High_Integrity_Mode restriction
1381 -- Acquire proper Set entity. We use the aligned or unaligned
1382 -- case as appropriate.
1384 if Known_Aligned_Enough
(Obj
, Csiz
) then
1385 Set_nn
:= RTE
(Set_Id
(Csiz
));
1387 Set_nn
:= RTE
(SetU_Id
(Csiz
));
1390 -- Now generate the set reference
1392 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1393 Convert_To_Actual_Subtype
(Obj
);
1394 Atyp
:= Etype
(Obj
);
1395 Compute_Linear_Subscript
(Atyp
, Lhs
, Subscr
);
1397 -- Set indication of whether the packed array has reverse SSO
1401 (Boolean_Literals
(Reverse_Storage_Order
(Atyp
)), Loc
);
1403 -- Below we must make the assumption that Obj is
1404 -- at least byte aligned, since otherwise its address
1405 -- cannot be taken. The assumption holds since the
1406 -- only arrays that can be misaligned are small packed
1407 -- arrays which are implemented as a modular type, and
1408 -- that is not the case here.
1411 Make_Procedure_Call_Statement
(Loc
,
1412 Name
=> New_Occurrence_Of
(Set_nn
, Loc
),
1413 Parameter_Associations
=> New_List
(
1414 Make_Attribute_Reference
(Loc
,
1416 Attribute_Name
=> Name_Address
),
1418 Unchecked_Convert_To
(Bits_nn
, Convert_To
(Ctyp
, Rhs
)),
1424 Analyze
(N
, Suppress
=> All_Checks
);
1425 end Expand_Bit_Packed_Element_Set
;
1427 -------------------------------------
1428 -- Expand_Packed_Address_Reference --
1429 -------------------------------------
1431 procedure Expand_Packed_Address_Reference
(N
: Node_Id
) is
1432 Loc
: constant Source_Ptr
:= Sloc
(N
);
1437 -- We build an expression that has the form
1439 -- outer_object'Address
1440 -- + (linear-subscript * component_size for each array reference
1441 -- + field'Bit_Position for each record field
1443 -- + ...) / Storage_Unit;
1445 Get_Base_And_Bit_Offset
(Prefix
(N
), Base
, Offset
);
1448 Unchecked_Convert_To
(RTE
(RE_Address
),
1451 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1452 Make_Attribute_Reference
(Loc
,
1454 Attribute_Name
=> Name_Address
)),
1457 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1458 Make_Op_Divide
(Loc
,
1459 Left_Opnd
=> Offset
,
1461 Make_Integer_Literal
(Loc
, System_Storage_Unit
))))));
1463 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1464 end Expand_Packed_Address_Reference
;
1466 ---------------------------------
1467 -- Expand_Packed_Bit_Reference --
1468 ---------------------------------
1470 procedure Expand_Packed_Bit_Reference
(N
: Node_Id
) is
1471 Loc
: constant Source_Ptr
:= Sloc
(N
);
1476 -- We build an expression that has the form
1478 -- (linear-subscript * component_size for each array reference
1479 -- + field'Bit_Position for each record field
1481 -- + ...) mod Storage_Unit;
1483 Get_Base_And_Bit_Offset
(Prefix
(N
), Base
, Offset
);
1486 Unchecked_Convert_To
(Universal_Integer
,
1488 Left_Opnd
=> Offset
,
1489 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
1491 Analyze_And_Resolve
(N
, Universal_Integer
);
1492 end Expand_Packed_Bit_Reference
;
1494 ------------------------------------
1495 -- Expand_Packed_Boolean_Operator --
1496 ------------------------------------
1498 -- This routine expands "a op b" for the packed cases
1500 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
) is
1501 Loc
: constant Source_Ptr
:= Sloc
(N
);
1502 Typ
: constant Entity_Id
:= Etype
(N
);
1503 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1504 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1511 Convert_To_Actual_Subtype
(L
);
1512 Convert_To_Actual_Subtype
(R
);
1514 Ensure_Defined
(Etype
(L
), N
);
1515 Ensure_Defined
(Etype
(R
), N
);
1517 Apply_Length_Check
(R
, Etype
(L
));
1522 -- Deal with silly case of XOR where the subcomponent has a range
1523 -- True .. True where an exception must be raised.
1525 if Nkind
(N
) = N_Op_Xor
then
1526 Silly_Boolean_Array_Xor_Test
(N
, Rtyp
);
1529 -- Now that that silliness is taken care of, get packed array type
1531 Convert_To_PAT_Type
(L
);
1532 Convert_To_PAT_Type
(R
);
1536 -- For the modular case, we expand a op b into
1538 -- rtyp!(pat!(a) op pat!(b))
1540 -- where rtyp is the Etype of the left operand. Note that we do not
1541 -- convert to the base type, since this would be unconstrained, and
1542 -- hence not have a corresponding packed array type set.
1544 -- Note that both operands must be modular for this code to be used
1546 if Is_Modular_Integer_Type
(PAT
)
1548 Is_Modular_Integer_Type
(Etype
(R
))
1554 if Nkind
(N
) = N_Op_And
then
1555 P
:= Make_Op_And
(Loc
, L
, R
);
1557 elsif Nkind
(N
) = N_Op_Or
then
1558 P
:= Make_Op_Or
(Loc
, L
, R
);
1560 else -- Nkind (N) = N_Op_Xor
1561 P
:= Make_Op_Xor
(Loc
, L
, R
);
1564 Rewrite
(N
, Unchecked_Convert_To
(Ltyp
, P
));
1567 -- For the array case, we insert the actions
1571 -- System.Bit_Ops.Bit_And/Or/Xor
1573 -- Ltype'Length * Ltype'Component_Size;
1575 -- Rtype'Length * Rtype'Component_Size
1578 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1579 -- the second argument and fourth arguments are the lengths of the
1580 -- operands in bits. Then we replace the expression by a reference
1583 -- Note that if we are mixing a modular and array operand, everything
1584 -- works fine, since we ensure that the modular representation has the
1585 -- same physical layout as the array representation (that's what the
1586 -- left justified modular stuff in the big-endian case is about).
1590 Result_Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1594 if Nkind
(N
) = N_Op_And
then
1597 elsif Nkind
(N
) = N_Op_Or
then
1600 else -- Nkind (N) = N_Op_Xor
1604 Insert_Actions
(N
, New_List
(
1606 Make_Object_Declaration
(Loc
,
1607 Defining_Identifier
=> Result_Ent
,
1608 Object_Definition
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1610 Make_Procedure_Call_Statement
(Loc
,
1611 Name
=> New_Occurrence_Of
(RTE
(E_Id
), Loc
),
1612 Parameter_Associations
=> New_List
(
1614 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1616 Attribute_Name
=> Name_Address
),
1618 Make_Op_Multiply
(Loc
,
1620 Make_Attribute_Reference
(Loc
,
1623 (Etype
(First_Index
(Ltyp
)), Loc
),
1624 Attribute_Name
=> Name_Range_Length
),
1627 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
))),
1629 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1631 Attribute_Name
=> Name_Address
),
1633 Make_Op_Multiply
(Loc
,
1635 Make_Attribute_Reference
(Loc
,
1638 (Etype
(First_Index
(Rtyp
)), Loc
),
1639 Attribute_Name
=> Name_Range_Length
),
1642 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
1644 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1645 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
1646 Attribute_Name
=> Name_Address
)))));
1649 New_Occurrence_Of
(Result_Ent
, Loc
));
1653 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
1654 end Expand_Packed_Boolean_Operator
;
1656 -------------------------------------
1657 -- Expand_Packed_Element_Reference --
1658 -------------------------------------
1660 procedure Expand_Packed_Element_Reference
(N
: Node_Id
) is
1661 Loc
: constant Source_Ptr
:= Sloc
(N
);
1673 -- If the node is an actual in a call, the prefix has not been fully
1674 -- expanded, to account for the additional expansion for in-out actuals
1675 -- (see expand_actuals for details). If the prefix itself is a packed
1676 -- reference as well, we have to recurse to complete the transformation
1679 if Nkind
(Prefix
(N
)) = N_Indexed_Component
1680 and then not Analyzed
(Prefix
(N
))
1681 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Prefix
(N
))))
1683 Expand_Packed_Element_Reference
(Prefix
(N
));
1686 -- The prefix may be rewritten below as a conversion. If it is a source
1687 -- entity generate reference to it now, to prevent spurious warnings
1688 -- about unused entities.
1690 if Is_Entity_Name
(Prefix
(N
))
1691 and then Comes_From_Source
(Prefix
(N
))
1693 Generate_Reference
(Entity
(Prefix
(N
)), Prefix
(N
), 'r');
1696 -- If not bit packed, we have the enumeration case, which is easily
1697 -- dealt with (just adjust the subscripts of the indexed component)
1699 -- Note: this leaves the result as an indexed component, which is
1700 -- still a variable, so can be used in the assignment case, as is
1701 -- required in the enumeration case.
1703 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
1704 Setup_Enumeration_Packed_Array_Reference
(N
);
1708 -- Remaining processing is for the bit-packed case
1710 Obj
:= Relocate_Node
(Prefix
(N
));
1711 Convert_To_Actual_Subtype
(Obj
);
1712 Atyp
:= Etype
(Obj
);
1713 PAT
:= Packed_Array_Impl_Type
(Atyp
);
1714 Ctyp
:= Component_Type
(Atyp
);
1715 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1717 -- For the AAMP target, indexing of certain packed array is passed
1718 -- through to the back end without expansion, because the expansion
1719 -- results in very inefficient code on that target. This allows the
1720 -- GNAAMP back end to generate specialized macros that support more
1721 -- efficient indexing of packed arrays with components having sizes
1722 -- that are small powers of two.
1725 and then (Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4)
1730 -- Case of component size 1,2,4 or any component size for the modular
1731 -- case. These are the cases for which we can inline the code.
1733 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1734 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1736 Setup_Inline_Packed_Array_Reference
(N
, Atyp
, Obj
, Cmask
, Shift
);
1737 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1738 Set_Print_In_Hex
(Lit
);
1740 -- We generate a shift right to position the field, followed by a
1741 -- masking operation to extract the bit field, and we finally do an
1742 -- unchecked conversion to convert the result to the required target.
1744 -- Note that the unchecked conversion automatically deals with the
1745 -- bias if we are dealing with a biased representation. What will
1746 -- happen is that we temporarily generate the biased representation,
1747 -- but almost immediately that will be converted to the original
1748 -- unbiased component type, and the bias will disappear.
1752 Left_Opnd
=> Make_Shift_Right
(Obj
, Shift
),
1754 Set_Etype
(Arg
, Ctyp
);
1756 -- Component extraction is performed on a native endianness scalar
1757 -- value: if Atyp has reverse storage order, then it has been byte
1758 -- swapped, and if the component being extracted is itself of a
1759 -- composite type with reverse storage order, then we need to swap
1760 -- it back to its expected endianness after extraction.
1762 if Reverse_Storage_Order
(Atyp
)
1763 and then (Is_Record_Type
(Ctyp
) or else Is_Array_Type
(Ctyp
))
1764 and then Reverse_Storage_Order
(Ctyp
)
1766 Arg
:= Revert_Storage_Order
(Arg
);
1769 -- We needed to analyze this before we do the unchecked convert
1770 -- below, but we need it temporarily attached to the tree for
1771 -- this analysis (hence the temporary Set_Parent call).
1773 Set_Parent
(Arg
, Parent
(N
));
1774 Analyze_And_Resolve
(Arg
);
1776 Rewrite
(N
, RJ_Unchecked_Convert_To
(Ctyp
, Arg
));
1778 -- All other component sizes for non-modular case
1783 -- Component_Type!(Get_nn (Arr'address, Subscr))
1785 -- where Subscr is the computed linear subscript
1790 Rev_SSO
: constant Node_Id
:=
1792 (Boolean_Literals
(Reverse_Storage_Order
(Atyp
)), Loc
);
1795 -- Acquire proper Get entity. We use the aligned or unaligned
1796 -- case as appropriate.
1798 if Known_Aligned_Enough
(Obj
, Csiz
) then
1799 Get_nn
:= RTE
(Get_Id
(Csiz
));
1801 Get_nn
:= RTE
(GetU_Id
(Csiz
));
1804 -- Now generate the get reference
1806 Compute_Linear_Subscript
(Atyp
, N
, Subscr
);
1808 -- Below we make the assumption that Obj is at least byte
1809 -- aligned, since otherwise its address cannot be taken.
1810 -- The assumption holds since the only arrays that can be
1811 -- misaligned are small packed arrays which are implemented
1812 -- as a modular type, and that is not the case here.
1815 Unchecked_Convert_To
(Ctyp
,
1816 Make_Function_Call
(Loc
,
1817 Name
=> New_Occurrence_Of
(Get_nn
, Loc
),
1818 Parameter_Associations
=> New_List
(
1819 Make_Attribute_Reference
(Loc
,
1821 Attribute_Name
=> Name_Address
),
1827 Analyze_And_Resolve
(N
, Ctyp
, Suppress
=> All_Checks
);
1828 end Expand_Packed_Element_Reference
;
1830 ----------------------
1831 -- Expand_Packed_Eq --
1832 ----------------------
1834 -- Handles expansion of "=" on packed array types
1836 procedure Expand_Packed_Eq
(N
: Node_Id
) is
1837 Loc
: constant Source_Ptr
:= Sloc
(N
);
1838 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1839 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1849 Convert_To_Actual_Subtype
(L
);
1850 Convert_To_Actual_Subtype
(R
);
1851 Ltyp
:= Underlying_Type
(Etype
(L
));
1852 Rtyp
:= Underlying_Type
(Etype
(R
));
1854 Convert_To_PAT_Type
(L
);
1855 Convert_To_PAT_Type
(R
);
1859 Make_Op_Multiply
(Loc
,
1861 Make_Attribute_Reference
(Loc
,
1862 Prefix
=> New_Occurrence_Of
(Ltyp
, Loc
),
1863 Attribute_Name
=> Name_Length
),
1865 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
)));
1868 Make_Op_Multiply
(Loc
,
1870 Make_Attribute_Reference
(Loc
,
1871 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
),
1872 Attribute_Name
=> Name_Length
),
1874 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
)));
1876 -- For the modular case, we transform the comparison to:
1878 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1880 -- where PAT is the packed array type. This works fine, since in the
1881 -- modular case we guarantee that the unused bits are always zeroes.
1882 -- We do have to compare the lengths because we could be comparing
1883 -- two different subtypes of the same base type.
1885 if Is_Modular_Integer_Type
(PAT
) then
1890 Left_Opnd
=> LLexpr
,
1891 Right_Opnd
=> RLexpr
),
1898 -- For the non-modular case, we call a runtime routine
1900 -- System.Bit_Ops.Bit_Eq
1901 -- (L'Address, L_Length, R'Address, R_Length)
1903 -- where PAT is the packed array type, and the lengths are the lengths
1904 -- in bits of the original packed arrays. This routine takes care of
1905 -- not comparing the unused bits in the last byte.
1909 Make_Function_Call
(Loc
,
1910 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Eq
), Loc
),
1911 Parameter_Associations
=> New_List
(
1912 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1914 Attribute_Name
=> Name_Address
),
1918 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1920 Attribute_Name
=> Name_Address
),
1925 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
1926 end Expand_Packed_Eq
;
1928 -----------------------
1929 -- Expand_Packed_Not --
1930 -----------------------
1932 -- Handles expansion of "not" on packed array types
1934 procedure Expand_Packed_Not
(N
: Node_Id
) is
1935 Loc
: constant Source_Ptr
:= Sloc
(N
);
1936 Typ
: constant Entity_Id
:= Etype
(N
);
1937 Opnd
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1944 Convert_To_Actual_Subtype
(Opnd
);
1945 Rtyp
:= Etype
(Opnd
);
1947 -- Deal with silly False..False and True..True subtype case
1949 Silly_Boolean_Array_Not_Test
(N
, Rtyp
);
1951 -- Now that the silliness is taken care of, get packed array type
1953 Convert_To_PAT_Type
(Opnd
);
1954 PAT
:= Etype
(Opnd
);
1956 -- For the case where the packed array type is a modular type, "not A"
1957 -- expands simply into:
1959 -- Rtyp!(PAT!(A) xor Mask)
1961 -- where PAT is the packed array type, Mask is a mask of all 1 bits of
1962 -- length equal to the size of this packed type, and Rtyp is the actual
1963 -- actual subtype of the operand.
1965 Lit
:= Make_Integer_Literal
(Loc
, 2 ** RM_Size
(PAT
) - 1);
1966 Set_Print_In_Hex
(Lit
);
1968 if not Is_Array_Type
(PAT
) then
1970 Unchecked_Convert_To
(Rtyp
,
1973 Right_Opnd
=> Lit
)));
1975 -- For the array case, we insert the actions
1979 -- System.Bit_Ops.Bit_Not
1981 -- Typ'Length * Typ'Component_Size,
1984 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second argument
1985 -- is the length of the operand in bits. We then replace the expression
1986 -- with a reference to Result.
1990 Result_Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1993 Insert_Actions
(N
, New_List
(
1994 Make_Object_Declaration
(Loc
,
1995 Defining_Identifier
=> Result_Ent
,
1996 Object_Definition
=> New_Occurrence_Of
(Rtyp
, Loc
)),
1998 Make_Procedure_Call_Statement
(Loc
,
1999 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Not
), Loc
),
2000 Parameter_Associations
=> New_List
(
2001 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2003 Attribute_Name
=> Name_Address
),
2005 Make_Op_Multiply
(Loc
,
2007 Make_Attribute_Reference
(Loc
,
2010 (Etype
(First_Index
(Rtyp
)), Loc
),
2011 Attribute_Name
=> Name_Range_Length
),
2014 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2016 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2017 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
2018 Attribute_Name
=> Name_Address
)))));
2020 Rewrite
(N
, New_Occurrence_Of
(Result_Ent
, Loc
));
2024 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2025 end Expand_Packed_Not
;
2027 -----------------------------
2028 -- Get_Base_And_Bit_Offset --
2029 -----------------------------
2031 procedure Get_Base_And_Bit_Offset
2034 Offset
: out Node_Id
)
2045 -- We build up an expression serially that has the form
2047 -- linear-subscript * component_size for each array reference
2048 -- + field'Bit_Position for each record field
2054 if Nkind
(Base
) = N_Indexed_Component
then
2055 Convert_To_Actual_Subtype
(Prefix
(Base
));
2056 Atyp
:= Etype
(Prefix
(Base
));
2057 Compute_Linear_Subscript
(Atyp
, Base
, Subscr
);
2060 Make_Op_Multiply
(Loc
,
2061 Left_Opnd
=> Subscr
,
2063 Make_Attribute_Reference
(Loc
,
2064 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2065 Attribute_Name
=> Name_Component_Size
));
2067 elsif Nkind
(Base
) = N_Selected_Component
then
2069 Make_Attribute_Reference
(Loc
,
2070 Prefix
=> Selector_Name
(Base
),
2071 Attribute_Name
=> Name_Bit_Position
);
2083 Left_Opnd
=> Offset
,
2084 Right_Opnd
=> Term
);
2087 Base
:= Prefix
(Base
);
2089 end Get_Base_And_Bit_Offset
;
2091 -------------------------------------
2092 -- Involves_Packed_Array_Reference --
2093 -------------------------------------
2095 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean is
2097 if Nkind
(N
) = N_Indexed_Component
2098 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
2102 elsif Nkind
(N
) = N_Selected_Component
then
2103 return Involves_Packed_Array_Reference
(Prefix
(N
));
2108 end Involves_Packed_Array_Reference
;
2110 --------------------------
2111 -- Known_Aligned_Enough --
2112 --------------------------
2114 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean is
2115 Typ
: constant Entity_Id
:= Etype
(Obj
);
2117 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean;
2118 -- If the component is in a record that contains previous packed
2119 -- components, consider it unaligned because the back-end might
2120 -- choose to pack the rest of the record. Lead to less efficient code,
2121 -- but safer vis-a-vis of back-end choices.
2123 --------------------------------
2124 -- In_Partially_Packed_Record --
2125 --------------------------------
2127 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean is
2128 Rec_Type
: constant Entity_Id
:= Scope
(Comp
);
2129 Prev_Comp
: Entity_Id
;
2132 Prev_Comp
:= First_Entity
(Rec_Type
);
2133 while Present
(Prev_Comp
) loop
2134 if Is_Packed
(Etype
(Prev_Comp
)) then
2137 elsif Prev_Comp
= Comp
then
2141 Next_Entity
(Prev_Comp
);
2145 end In_Partially_Packed_Record
;
2147 -- Start of processing for Known_Aligned_Enough
2150 -- Odd bit sizes don't need alignment anyway
2152 if Csiz
mod 2 = 1 then
2155 -- If we have a specified alignment, see if it is sufficient, if not
2156 -- then we can't possibly be aligned enough in any case.
2158 elsif Known_Alignment
(Etype
(Obj
)) then
2159 -- Alignment required is 4 if size is a multiple of 4, and
2160 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2162 if Alignment
(Etype
(Obj
)) < 4 - (Csiz
mod 4) then
2167 -- OK, alignment should be sufficient, if object is aligned
2169 -- If object is strictly aligned, then it is definitely aligned
2171 if Strict_Alignment
(Typ
) then
2174 -- Case of subscripted array reference
2176 elsif Nkind
(Obj
) = N_Indexed_Component
then
2178 -- If we have a pointer to an array, then this is definitely
2179 -- aligned, because pointers always point to aligned versions.
2181 if Is_Access_Type
(Etype
(Prefix
(Obj
))) then
2184 -- Otherwise, go look at the prefix
2187 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2190 -- Case of record field
2192 elsif Nkind
(Obj
) = N_Selected_Component
then
2194 -- What is significant here is whether the record type is packed
2196 if Is_Record_Type
(Etype
(Prefix
(Obj
)))
2197 and then Is_Packed
(Etype
(Prefix
(Obj
)))
2201 -- Or the component has a component clause which might cause
2202 -- the component to become unaligned (we can't tell if the
2203 -- backend is doing alignment computations).
2205 elsif Present
(Component_Clause
(Entity
(Selector_Name
(Obj
)))) then
2208 elsif In_Partially_Packed_Record
(Entity
(Selector_Name
(Obj
))) then
2211 -- In all other cases, go look at prefix
2214 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2217 elsif Nkind
(Obj
) = N_Type_Conversion
then
2218 return Known_Aligned_Enough
(Expression
(Obj
), Csiz
);
2220 -- For a formal parameter, it is safer to assume that it is not
2221 -- aligned, because the formal may be unconstrained while the actual
2222 -- is constrained. In this situation, a small constrained packed
2223 -- array, represented in modular form, may be unaligned.
2225 elsif Is_Entity_Name
(Obj
) then
2226 return not Is_Formal
(Entity
(Obj
));
2229 -- If none of the above, must be aligned
2232 end Known_Aligned_Enough
;
2234 ---------------------
2235 -- Make_Shift_Left --
2236 ---------------------
2238 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2242 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2246 Make_Op_Shift_Left
(Sloc
(N
),
2249 Set_Shift_Count_OK
(Nod
, True);
2252 end Make_Shift_Left
;
2254 ----------------------
2255 -- Make_Shift_Right --
2256 ----------------------
2258 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2262 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2266 Make_Op_Shift_Right
(Sloc
(N
),
2269 Set_Shift_Count_OK
(Nod
, True);
2272 end Make_Shift_Right
;
2274 -----------------------------
2275 -- RJ_Unchecked_Convert_To --
2276 -----------------------------
2278 function RJ_Unchecked_Convert_To
2280 Expr
: Node_Id
) return Node_Id
2282 Source_Typ
: constant Entity_Id
:= Etype
(Expr
);
2283 Target_Typ
: constant Entity_Id
:= Typ
;
2285 Src
: Node_Id
:= Expr
;
2291 Source_Siz
:= UI_To_Int
(RM_Size
(Source_Typ
));
2292 Target_Siz
:= UI_To_Int
(RM_Size
(Target_Typ
));
2294 -- For a little-endian target type stored byte-swapped on a
2295 -- big-endian machine, do not mask to Target_Siz bits.
2298 and then (Is_Record_Type
(Target_Typ
)
2300 Is_Array_Type
(Target_Typ
))
2301 and then Reverse_Storage_Order
(Target_Typ
)
2303 Source_Siz
:= Target_Siz
;
2306 -- First step, if the source type is not a discrete type, then we first
2307 -- convert to a modular type of the source length, since otherwise, on
2308 -- a big-endian machine, we get left-justification. We do it for little-
2309 -- endian machines as well, because there might be junk bits that are
2310 -- not cleared if the type is not numeric.
2312 if Source_Siz
/= Target_Siz
2313 and then not Is_Discrete_Type
(Source_Typ
)
2315 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Source_Siz
)), Src
);
2318 -- In the big endian case, if the lengths of the two types differ, then
2319 -- we must worry about possible left justification in the conversion,
2320 -- and avoiding that is what this is all about.
2322 if Bytes_Big_Endian
and then Source_Siz
/= Target_Siz
then
2324 -- Next step. If the target is not a discrete type, then we first
2325 -- convert to a modular type of the target length, since otherwise,
2326 -- on a big-endian machine, we get left-justification.
2328 if not Is_Discrete_Type
(Target_Typ
) then
2329 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Target_Siz
)), Src
);
2333 -- And now we can do the final conversion to the target type
2335 return Unchecked_Convert_To
(Target_Typ
, Src
);
2336 end RJ_Unchecked_Convert_To
;
2338 ----------------------------------------------
2339 -- Setup_Enumeration_Packed_Array_Reference --
2340 ----------------------------------------------
2342 -- All we have to do here is to find the subscripts that correspond to the
2343 -- index positions that have non-standard enumeration types and insert a
2344 -- Pos attribute to get the proper subscript value.
2346 -- Finally the prefix must be uncheck-converted to the corresponding packed
2349 -- Note that the component type is unchanged, so we do not need to fiddle
2350 -- with the types (Gigi always automatically takes the packed array type if
2351 -- it is set, as it will be in this case).
2353 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
) is
2354 Pfx
: constant Node_Id
:= Prefix
(N
);
2355 Typ
: constant Entity_Id
:= Etype
(N
);
2356 Exprs
: constant List_Id
:= Expressions
(N
);
2360 -- If the array is unconstrained, then we replace the array reference
2361 -- with its actual subtype. This actual subtype will have a packed array
2362 -- type with appropriate bounds.
2364 if not Is_Constrained
(Packed_Array_Impl_Type
(Etype
(Pfx
))) then
2365 Convert_To_Actual_Subtype
(Pfx
);
2368 Expr
:= First
(Exprs
);
2369 while Present
(Expr
) loop
2371 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2372 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
2375 if Is_Enumeration_Type
(Expr_Typ
)
2376 and then Has_Non_Standard_Rep
(Expr_Typ
)
2379 Make_Attribute_Reference
(Loc
,
2380 Prefix
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
2381 Attribute_Name
=> Name_Pos
,
2382 Expressions
=> New_List
(Relocate_Node
(Expr
))));
2383 Analyze_And_Resolve
(Expr
, Standard_Natural
);
2391 Make_Indexed_Component
(Sloc
(N
),
2393 Unchecked_Convert_To
(Packed_Array_Impl_Type
(Etype
(Pfx
)), Pfx
),
2394 Expressions
=> Exprs
));
2396 Analyze_And_Resolve
(N
, Typ
);
2397 end Setup_Enumeration_Packed_Array_Reference
;
2399 -----------------------------------------
2400 -- Setup_Inline_Packed_Array_Reference --
2401 -----------------------------------------
2403 procedure Setup_Inline_Packed_Array_Reference
2406 Obj
: in out Node_Id
;
2408 Shift
: out Node_Id
)
2410 Loc
: constant Source_Ptr
:= Sloc
(N
);
2417 Csiz
:= Component_Size
(Atyp
);
2419 Convert_To_PAT_Type
(Obj
);
2422 Cmask
:= 2 ** Csiz
- 1;
2424 if Is_Array_Type
(PAT
) then
2425 Otyp
:= Component_Type
(PAT
);
2426 Osiz
:= Component_Size
(PAT
);
2431 -- In the case where the PAT is a modular type, we want the actual
2432 -- size in bits of the modular value we use. This is neither the
2433 -- Object_Size nor the Value_Size, either of which may have been
2434 -- reset to strange values, but rather the minimum size. Note that
2435 -- since this is a modular type with full range, the issue of
2436 -- biased representation does not arise.
2438 Osiz
:= UI_From_Int
(Minimum_Size
(Otyp
));
2441 Compute_Linear_Subscript
(Atyp
, N
, Shift
);
2443 -- If the component size is not 1, then the subscript must be multiplied
2444 -- by the component size to get the shift count.
2448 Make_Op_Multiply
(Loc
,
2449 Left_Opnd
=> Make_Integer_Literal
(Loc
, Csiz
),
2450 Right_Opnd
=> Shift
);
2453 -- If we have the array case, then this shift count must be broken down
2454 -- into a byte subscript, and a shift within the byte.
2456 if Is_Array_Type
(PAT
) then
2459 New_Shift
: Node_Id
;
2462 -- We must analyze shift, since we will duplicate it
2464 Set_Parent
(Shift
, N
);
2466 (Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2468 -- The shift count within the word is
2473 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2474 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
));
2476 -- The subscript to be used on the PAT array is
2480 Make_Indexed_Component
(Loc
,
2482 Expressions
=> New_List
(
2483 Make_Op_Divide
(Loc
,
2484 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2485 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
))));
2490 -- For the modular integer case, the object to be manipulated is the
2491 -- entire array, so Obj is unchanged. Note that we will reset its type
2492 -- to PAT before returning to the caller.
2498 -- The one remaining step is to modify the shift count for the
2499 -- big-endian case. Consider the following example in a byte:
2501 -- xxxxxxxx bits of byte
2502 -- vvvvvvvv bits of value
2503 -- 33221100 little-endian numbering
2504 -- 00112233 big-endian numbering
2506 -- Here we have the case of 2-bit fields
2508 -- For the little-endian case, we already have the proper shift count
2509 -- set, e.g. for element 2, the shift count is 2*2 = 4.
2511 -- For the big endian case, we have to adjust the shift count, computing
2512 -- it as (N - F) - Shift, where N is the number of bits in an element of
2513 -- the array used to implement the packed array, F is the number of bits
2514 -- in a source array element, and Shift is the count so far computed.
2516 -- We also have to adjust if the storage order is reversed
2518 if Bytes_Big_Endian
xor Reverse_Storage_Order
(Base_Type
(Atyp
)) then
2520 Make_Op_Subtract
(Loc
,
2521 Left_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
- Csiz
),
2522 Right_Opnd
=> Shift
);
2525 Set_Parent
(Shift
, N
);
2526 Set_Parent
(Obj
, N
);
2527 Analyze_And_Resolve
(Obj
, Otyp
, Suppress
=> All_Checks
);
2528 Analyze_And_Resolve
(Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2530 -- Make sure final type of object is the appropriate packed type
2532 Set_Etype
(Obj
, Otyp
);
2534 end Setup_Inline_Packed_Array_Reference
;