1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, 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 Namet
; use Namet
;
34 with Nlists
; use Nlists
;
35 with Nmake
; use Nmake
;
36 with Rtsfind
; use Rtsfind
;
38 with Sem_Ch3
; use Sem_Ch3
;
39 with Sem_Ch8
; use Sem_Ch8
;
40 with Sem_Ch13
; use Sem_Ch13
;
41 with Sem_Eval
; use Sem_Eval
;
42 with Sem_Res
; use Sem_Res
;
43 with Sem_Util
; use Sem_Util
;
44 with Sinfo
; use Sinfo
;
45 with Snames
; use Snames
;
46 with Stand
; use Stand
;
47 with Targparm
; use Targparm
;
48 with Tbuild
; use Tbuild
;
49 with Ttypes
; use Ttypes
;
50 with Uintp
; use Uintp
;
52 package body Exp_Pakd
is
54 ---------------------------
55 -- Endian Considerations --
56 ---------------------------
58 -- As described in the specification, bit numbering in a packed array
59 -- is consistent with bit numbering in a record representation clause,
60 -- and hence dependent on the endianness of the machine:
62 -- For little-endian machines, element zero is at the right hand end
63 -- (low order end) of a bit field.
65 -- For big-endian machines, element zero is at the left hand end
66 -- (high order end) of a bit field.
68 -- The shifts that are used to right justify a field therefore differ
69 -- in the two cases. For the little-endian case, we can simply use the
70 -- bit number (i.e. the element number * element size) as the count for
71 -- a right shift. For the big-endian case, we have to subtract the shift
72 -- count from an appropriate constant to use in the right shift. We use
73 -- rotates instead of shifts (which is necessary in the store case to
74 -- preserve other fields), and we expect that the backend will be able
75 -- to change the right rotate into a left rotate, avoiding the subtract,
76 -- if the architecture provides such an instruction.
78 ----------------------------------------------
79 -- Entity Tables for Packed Access Routines --
80 ----------------------------------------------
82 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
83 -- library routines. This table is used to obtain the entity for the
86 type E_Array
is array (Int
range 01 .. 63) of RE_Id
;
88 -- Array of Bits_nn entities. Note that we do not use library routines
89 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
90 -- entries from System.Unsigned, because we also use this table for
91 -- certain special unchecked conversions in the big-endian case.
93 Bits_Id
: constant E_Array
:=
109 16 => RE_Unsigned_16
,
125 32 => RE_Unsigned_32
,
158 -- Array of Get routine entities. These are used to obtain an element
159 -- from a packed array. The N'th entry is used to obtain elements from
160 -- a packed array whose component size is N. RE_Null is used as a null
161 -- entry, for the cases where a library routine is not used.
163 Get_Id
: constant E_Array
:=
228 -- Array of Get routine entities to be used in the case where the packed
229 -- array is itself a component of a packed structure, and therefore may
230 -- not be fully aligned. This only affects the even sizes, since for the
231 -- odd sizes, we do not get any fixed alignment in any case.
233 GetU_Id
: constant E_Array
:=
298 -- Array of Set routine entities. These are used to assign an element
299 -- of a packed array. The N'th entry is used to assign elements for
300 -- a packed array whose component size is N. RE_Null is used as a null
301 -- entry, for the cases where a library routine is not used.
303 Set_Id
: constant E_Array
:=
368 -- Array of Set routine entities to be used in the case where the packed
369 -- array is itself a component of a packed structure, and therefore may
370 -- not be fully aligned. This only affects the even sizes, since for the
371 -- odd sizes, we do not get any fixed alignment in any case.
373 SetU_Id
: constant E_Array
:=
438 -----------------------
439 -- Local Subprograms --
440 -----------------------
442 procedure Compute_Linear_Subscript
445 Subscr
: out Node_Id
);
446 -- Given a constrained array type Atyp, and an indexed component node
447 -- N referencing an array object of this type, build an expression of
448 -- type Standard.Integer representing the zero-based linear subscript
449 -- value. This expression includes any required range checks.
451 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
);
452 -- Given an expression of a packed array type, builds a corresponding
453 -- expression whose type is the implementation type used to represent
454 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
456 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean;
457 -- There are two versions of the Set routines, the ones used when the
458 -- object is known to be sufficiently well aligned given the number of
459 -- bits, and the ones used when the object is not known to be aligned.
460 -- This routine is used to determine which set to use. Obj is a reference
461 -- to the object, and Csiz is the component size of the packed array.
462 -- True is returned if the alignment of object is known to be sufficient,
463 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
466 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
467 -- Build a left shift node, checking for the case of a shift count of zero
469 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
470 -- Build a right shift node, checking for the case of a shift count of zero
472 function RJ_Unchecked_Convert_To
474 Expr
: Node_Id
) return Node_Id
;
475 -- The packed array code does unchecked conversions which in some cases
476 -- may involve non-discrete types with differing sizes. The semantics of
477 -- such conversions is potentially endian dependent, and the effect we
478 -- want here for such a conversion is to do the conversion in size as
479 -- though numeric items are involved, and we extend or truncate on the
480 -- left side. This happens naturally in the little-endian case, but in
481 -- the big endian case we can get left justification, when what we want
482 -- is right justification. This routine does the unchecked conversion in
483 -- a stepwise manner to ensure that it gives the expected result. Hence
484 -- the name (RJ = Right justified). The parameters Typ and Expr are as
485 -- for the case of a normal Unchecked_Convert_To call.
487 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
);
488 -- This routine is called in the Get and Set case for arrays that are
489 -- packed but not bit-packed, meaning that they have at least one
490 -- subscript that is of an enumeration type with a non-standard
491 -- representation. This routine modifies the given node to properly
492 -- reference the corresponding packed array type.
494 procedure Setup_Inline_Packed_Array_Reference
497 Obj
: in out Node_Id
;
499 Shift
: out Node_Id
);
500 -- This procedure performs common processing on the N_Indexed_Component
501 -- parameter given as N, whose prefix is a reference to a packed array.
502 -- This is used for the get and set when the component size is 1,2,4
503 -- or for other component sizes when the packed array type is a modular
504 -- type (i.e. the cases that are handled with inline code).
508 -- N is the N_Indexed_Component node for the packed array reference
510 -- Atyp is the constrained array type (the actual subtype has been
511 -- computed if necessary to obtain the constraints, but this is still
512 -- the original array type, not the Packed_Array_Type value).
514 -- Obj is the object which is to be indexed. It is always of type Atyp.
518 -- Obj is the object containing the desired bit field. It is of type
519 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
520 -- entire value, for the small static case, or the proper selected byte
521 -- from the array in the large or dynamic case. This node is analyzed
522 -- and resolved on return.
524 -- Shift is a node representing the shift count to be used in the
525 -- rotate right instruction that positions the field for access.
526 -- This node is analyzed and resolved on return.
528 -- Cmask is a mask corresponding to the width of the component field.
529 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
531 -- Note: in some cases the call to this routine may generate actions
532 -- (for handling multi-use references and the generation of the packed
533 -- array type on the fly). Such actions are inserted into the tree
534 -- directly using Insert_Action.
536 ------------------------------
537 -- Compute_Linear_Subcsript --
538 ------------------------------
540 procedure Compute_Linear_Subscript
543 Subscr
: out Node_Id
)
545 Loc
: constant Source_Ptr
:= Sloc
(N
);
554 -- Loop through dimensions
556 Indx
:= First_Index
(Atyp
);
557 Oldsub
:= First
(Expressions
(N
));
559 while Present
(Indx
) loop
560 Styp
:= Etype
(Indx
);
561 Newsub
:= Relocate_Node
(Oldsub
);
563 -- Get expression for the subscript value. First, if Do_Range_Check
564 -- is set on a subscript, then we must do a range check against the
565 -- original bounds (not the bounds of the packed array type). We do
566 -- this by introducing a subtype conversion.
568 if Do_Range_Check
(Newsub
)
569 and then Etype
(Newsub
) /= Styp
571 Newsub
:= Convert_To
(Styp
, Newsub
);
574 -- Now evolve the expression for the subscript. First convert
575 -- the subscript to be zero based and of an integer type.
577 -- Case of integer type, where we just subtract to get lower bound
579 if Is_Integer_Type
(Styp
) then
581 -- If length of integer type is smaller than standard integer,
582 -- then we convert to integer first, then do the subtract
584 -- Integer (subscript) - Integer (Styp'First)
586 if Esize
(Styp
) < Esize
(Standard_Integer
) then
588 Make_Op_Subtract
(Loc
,
589 Left_Opnd
=> Convert_To
(Standard_Integer
, Newsub
),
591 Convert_To
(Standard_Integer
,
592 Make_Attribute_Reference
(Loc
,
593 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
594 Attribute_Name
=> Name_First
)));
596 -- For larger integer types, subtract first, then convert to
597 -- integer, this deals with strange long long integer bounds.
599 -- Integer (subscript - Styp'First)
603 Convert_To
(Standard_Integer
,
604 Make_Op_Subtract
(Loc
,
607 Make_Attribute_Reference
(Loc
,
608 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
609 Attribute_Name
=> Name_First
)));
612 -- For the enumeration case, we have to use 'Pos to get the value
613 -- to work with before subtracting the lower bound.
615 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
617 -- This is not quite right for bizarre cases where the size of the
618 -- enumeration type is > Integer'Size bits due to rep clause ???
621 pragma Assert
(Is_Enumeration_Type
(Styp
));
624 Make_Op_Subtract
(Loc
,
625 Left_Opnd
=> Convert_To
(Standard_Integer
,
626 Make_Attribute_Reference
(Loc
,
627 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
628 Attribute_Name
=> Name_Pos
,
629 Expressions
=> New_List
(Newsub
))),
632 Convert_To
(Standard_Integer
,
633 Make_Attribute_Reference
(Loc
,
634 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
635 Attribute_Name
=> Name_Pos
,
636 Expressions
=> New_List
(
637 Make_Attribute_Reference
(Loc
,
638 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
639 Attribute_Name
=> Name_First
)))));
642 Set_Paren_Count
(Newsub
, 1);
644 -- For the first subscript, we just copy that subscript value
649 -- Otherwise, we must multiply what we already have by the current
650 -- stride and then add in the new value to the evolving subscript.
656 Make_Op_Multiply
(Loc
,
659 Make_Attribute_Reference
(Loc
,
660 Attribute_Name
=> Name_Range_Length
,
661 Prefix
=> New_Occurrence_Of
(Styp
, Loc
))),
662 Right_Opnd
=> Newsub
);
665 -- Move to next subscript
670 end Compute_Linear_Subscript
;
672 -------------------------
673 -- Convert_To_PAT_Type --
674 -------------------------
676 -- The PAT is always obtained from the actual subtype
678 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
) is
682 Convert_To_Actual_Subtype
(Aexp
);
683 Act_ST
:= Underlying_Type
(Etype
(Aexp
));
684 Create_Packed_Array_Type
(Act_ST
);
686 -- Just replace the etype with the packed array type. This works because
687 -- the expression will not be further analyzed, and Gigi considers the
688 -- two types equivalent in any case.
690 -- This is not strictly the case ??? If the reference is an actual in
691 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
692 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
693 -- array reference, reanalysis can produce spurious type errors when the
694 -- PAT type is replaced again with the original type of the array. Same
695 -- for the case of a dereference. The following is correct and minimal,
696 -- but the handling of more complex packed expressions in actuals is
697 -- confused. Probably the problem only remains for actuals in calls.
699 Set_Etype
(Aexp
, Packed_Array_Type
(Act_ST
));
701 if Is_Entity_Name
(Aexp
)
703 (Nkind
(Aexp
) = N_Indexed_Component
704 and then Is_Entity_Name
(Prefix
(Aexp
)))
705 or else Nkind
(Aexp
) = N_Explicit_Dereference
709 end Convert_To_PAT_Type
;
711 ------------------------------
712 -- Create_Packed_Array_Type --
713 ------------------------------
715 procedure Create_Packed_Array_Type
(Typ
: Entity_Id
) is
716 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
717 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
718 Csize
: constant Uint
:= Component_Size
(Typ
);
733 procedure Install_PAT
;
734 -- This procedure is called with Decl set to the declaration for the
735 -- packed array type. It creates the type and installs it as required.
737 procedure Set_PB_Type
;
738 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
739 -- requirements (see documentation in the spec of this package).
745 procedure Install_PAT
is
746 Pushed_Scope
: Boolean := False;
749 -- We do not want to put the declaration we have created in the tree
750 -- since it is often hard, and sometimes impossible to find a proper
751 -- place for it (the impossible case arises for a packed array type
752 -- with bounds depending on the discriminant, a declaration cannot
753 -- be put inside the record, and the reference to the discriminant
754 -- cannot be outside the record).
756 -- The solution is to analyze the declaration while temporarily
757 -- attached to the tree at an appropriate point, and then we install
758 -- the resulting type as an Itype in the packed array type field of
759 -- the original type, so that no explicit declaration is required.
761 -- Note: the packed type is created in the scope of its parent
762 -- type. There are at least some cases where the current scope
763 -- is deeper, and so when this is the case, we temporarily reset
764 -- the scope for the definition. This is clearly safe, since the
765 -- first use of the packed array type will be the implicit
766 -- reference from the corresponding unpacked type when it is
769 if Is_Itype
(Typ
) then
770 Set_Parent
(Decl
, Associated_Node_For_Itype
(Typ
));
772 Set_Parent
(Decl
, Declaration_Node
(Typ
));
775 if Scope
(Typ
) /= Current_Scope
then
776 Push_Scope
(Scope
(Typ
));
777 Pushed_Scope
:= True;
780 Set_Is_Itype
(PAT
, True);
781 Set_Packed_Array_Type
(Typ
, PAT
);
782 Analyze
(Decl
, Suppress
=> All_Checks
);
788 -- Set Esize and RM_Size to the actual size of the packed object
789 -- Do not reset RM_Size if already set, as happens in the case of
792 if Unknown_Esize
(PAT
) then
793 Set_Esize
(PAT
, PASize
);
796 if Unknown_RM_Size
(PAT
) then
797 Set_RM_Size
(PAT
, PASize
);
800 Adjust_Esize_Alignment
(PAT
);
802 -- Set remaining fields of packed array type
804 Init_Alignment
(PAT
);
805 Set_Parent
(PAT
, Empty
);
806 Set_Associated_Node_For_Itype
(PAT
, Typ
);
807 Set_Is_Packed_Array_Type
(PAT
, True);
808 Set_Original_Array_Type
(PAT
, Typ
);
810 -- We definitely do not want to delay freezing for packed array
811 -- types. This is of particular importance for the itypes that
812 -- are generated for record components depending on discriminants
813 -- where there is no place to put the freeze node.
815 Set_Has_Delayed_Freeze
(PAT
, False);
816 Set_Has_Delayed_Freeze
(Etype
(PAT
), False);
818 -- If we did allocate a freeze node, then clear out the reference
819 -- since it is obsolete (should we delete the freeze node???)
821 Set_Freeze_Node
(PAT
, Empty
);
822 Set_Freeze_Node
(Etype
(PAT
), Empty
);
829 procedure Set_PB_Type
is
831 -- If the user has specified an explicit alignment for the
832 -- type or component, take it into account.
834 if Csize
<= 2 or else Csize
= 4 or else Csize
mod 2 /= 0
835 or else Alignment
(Typ
) = 1
836 or else Component_Alignment
(Typ
) = Calign_Storage_Unit
838 PB_Type
:= RTE
(RE_Packed_Bytes1
);
840 elsif Csize
mod 4 /= 0
841 or else Alignment
(Typ
) = 2
843 PB_Type
:= RTE
(RE_Packed_Bytes2
);
846 PB_Type
:= RTE
(RE_Packed_Bytes4
);
850 -- Start of processing for Create_Packed_Array_Type
853 -- If we already have a packed array type, nothing to do
855 if Present
(Packed_Array_Type
(Typ
)) then
859 -- If our immediate ancestor subtype is constrained, and it already
860 -- has a packed array type, then just share the same type, since the
861 -- bounds must be the same. If the ancestor is not an array type but
862 -- a private type, as can happen with multiple instantiations, create
863 -- a new packed type, to avoid privacy issues.
865 if Ekind
(Typ
) = E_Array_Subtype
then
866 Ancest
:= Ancestor_Subtype
(Typ
);
869 and then Is_Array_Type
(Ancest
)
870 and then Is_Constrained
(Ancest
)
871 and then Present
(Packed_Array_Type
(Ancest
))
873 Set_Packed_Array_Type
(Typ
, Packed_Array_Type
(Ancest
));
878 -- We preset the result type size from the size of the original array
879 -- type, since this size clearly belongs to the packed array type. The
880 -- size of the conceptual unpacked type is always set to unknown.
882 PASize
:= RM_Size
(Typ
);
884 -- Case of an array where at least one index is of an enumeration
885 -- type with a non-standard representation, but the component size
886 -- is not appropriate for bit packing. This is the case where we
887 -- have Is_Packed set (we would never be in this unit otherwise),
888 -- but Is_Bit_Packed_Array is false.
890 -- Note that if the component size is appropriate for bit packing,
891 -- then the circuit for the computation of the subscript properly
892 -- deals with the non-standard enumeration type case by taking the
895 if not Is_Bit_Packed_Array
(Typ
) then
897 -- Here we build a declaration:
899 -- type tttP is array (index1, index2, ...) of component_type
901 -- where index1, index2, are the index types. These are the same
902 -- as the index types of the original array, except for the non-
903 -- standard representation enumeration type case, where we have
906 -- For the unconstrained array case, we use
910 -- For the constrained case, we use
912 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
913 -- Enum_Type'Pos (Enum_Type'Last);
916 Make_Defining_Identifier
(Loc
,
917 Chars
=> New_External_Name
(Chars
(Typ
), 'P'));
919 Set_Packed_Array_Type
(Typ
, PAT
);
922 Indexes
: constant List_Id
:= New_List
;
924 Indx_Typ
: Entity_Id
;
929 Indx
:= First_Index
(Typ
);
931 while Present
(Indx
) loop
932 Indx_Typ
:= Etype
(Indx
);
934 Enum_Case
:= Is_Enumeration_Type
(Indx_Typ
)
935 and then Has_Non_Standard_Rep
(Indx_Typ
);
937 -- Unconstrained case
939 if not Is_Constrained
(Typ
) then
941 Indx_Typ
:= Standard_Natural
;
944 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
949 if not Enum_Case
then
950 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
954 Make_Subtype_Indication
(Loc
,
956 New_Occurrence_Of
(Standard_Natural
, Loc
),
958 Make_Range_Constraint
(Loc
,
962 Make_Attribute_Reference
(Loc
,
964 New_Occurrence_Of
(Indx_Typ
, Loc
),
965 Attribute_Name
=> Name_Pos
,
966 Expressions
=> New_List
(
967 Make_Attribute_Reference
(Loc
,
969 New_Occurrence_Of
(Indx_Typ
, Loc
),
970 Attribute_Name
=> Name_First
))),
973 Make_Attribute_Reference
(Loc
,
975 New_Occurrence_Of
(Indx_Typ
, Loc
),
976 Attribute_Name
=> Name_Pos
,
977 Expressions
=> New_List
(
978 Make_Attribute_Reference
(Loc
,
980 New_Occurrence_Of
(Indx_Typ
, Loc
),
981 Attribute_Name
=> Name_Last
)))))));
989 if not Is_Constrained
(Typ
) then
991 Make_Unconstrained_Array_Definition
(Loc
,
992 Subtype_Marks
=> Indexes
,
993 Component_Definition
=>
994 Make_Component_Definition
(Loc
,
995 Aliased_Present
=> False,
996 Subtype_Indication
=>
997 New_Occurrence_Of
(Ctyp
, Loc
)));
1001 Make_Constrained_Array_Definition
(Loc
,
1002 Discrete_Subtype_Definitions
=> Indexes
,
1003 Component_Definition
=>
1004 Make_Component_Definition
(Loc
,
1005 Aliased_Present
=> False,
1006 Subtype_Indication
=>
1007 New_Occurrence_Of
(Ctyp
, Loc
)));
1011 Make_Full_Type_Declaration
(Loc
,
1012 Defining_Identifier
=> PAT
,
1013 Type_Definition
=> Typedef
);
1016 -- Set type as packed array type and install it
1018 Set_Is_Packed_Array_Type
(PAT
);
1022 -- Case of bit-packing required for unconstrained array. We create
1023 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1025 elsif not Is_Constrained
(Typ
) then
1027 Make_Defining_Identifier
(Loc
,
1028 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1030 Set_Packed_Array_Type
(Typ
, PAT
);
1034 Make_Subtype_Declaration
(Loc
,
1035 Defining_Identifier
=> PAT
,
1036 Subtype_Indication
=> New_Occurrence_Of
(PB_Type
, Loc
));
1040 -- Remaining code is for the case of bit-packing for constrained array
1042 -- The name of the packed array subtype is
1046 -- where sss is the component size in bits and ttt is the name of
1047 -- the parent packed type.
1051 Make_Defining_Identifier
(Loc
,
1052 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1054 Set_Packed_Array_Type
(Typ
, PAT
);
1056 -- Build an expression for the length of the array in bits.
1057 -- This is the product of the length of each of the dimensions
1063 Len_Expr
:= Empty
; -- suppress junk warning
1067 Make_Attribute_Reference
(Loc
,
1068 Attribute_Name
=> Name_Length
,
1069 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1070 Expressions
=> New_List
(
1071 Make_Integer_Literal
(Loc
, J
)));
1074 Len_Expr
:= Len_Dim
;
1078 Make_Op_Multiply
(Loc
,
1079 Left_Opnd
=> Len_Expr
,
1080 Right_Opnd
=> Len_Dim
);
1084 exit when J
> Number_Dimensions
(Typ
);
1088 -- Temporarily attach the length expression to the tree and analyze
1089 -- and resolve it, so that we can test its value. We assume that the
1090 -- total length fits in type Integer. This expression may involve
1091 -- discriminants, so we treat it as a default/per-object expression.
1093 Set_Parent
(Len_Expr
, Typ
);
1094 Analyze_Per_Use_Expression
(Len_Expr
, Standard_Long_Long_Integer
);
1096 -- Use a modular type if possible. We can do this if we have
1097 -- static bounds, and the length is small enough, and the length
1098 -- is not zero. We exclude the zero length case because the size
1099 -- of things is always at least one, and the zero length object
1100 -- would have an anomalous size.
1102 if Compile_Time_Known_Value
(Len_Expr
) then
1103 Len_Bits
:= Expr_Value
(Len_Expr
) * Csize
;
1105 -- Check for size known to be too large
1108 Uint_2
** (Standard_Integer_Size
- 1) * System_Storage_Unit
1110 if System_Storage_Unit
= 8 then
1112 ("packed array size cannot exceed " &
1113 "Integer''Last bytes", Typ
);
1116 ("packed array size cannot exceed " &
1117 "Integer''Last storage units", Typ
);
1120 -- Reset length to arbitrary not too high value to continue
1122 Len_Expr
:= Make_Integer_Literal
(Loc
, 65535);
1123 Analyze_And_Resolve
(Len_Expr
, Standard_Long_Long_Integer
);
1126 -- We normally consider small enough to mean no larger than the
1127 -- value of System_Max_Binary_Modulus_Power, checking that in the
1128 -- case of values longer than word size, we have long shifts.
1132 (Len_Bits
<= System_Word_Size
1133 or else (Len_Bits
<= System_Max_Binary_Modulus_Power
1134 and then Support_Long_Shifts_On_Target
))
1136 -- Also test for alignment given. If an alignment is given which
1137 -- is smaller than the natural modular alignment, force the array
1138 -- of bytes representation to accommodate the alignment.
1141 (No
(Alignment_Clause
(Typ
))
1143 Alignment
(Typ
) >= ((Len_Bits
+ System_Storage_Unit
)
1144 / System_Storage_Unit
))
1146 -- We can use the modular type, it has the form:
1148 -- subtype tttPn is btyp
1149 -- range 0 .. 2 ** ((Typ'Length (1)
1150 -- * ... * Typ'Length (n)) * Csize) - 1;
1152 -- The bounds are statically known, and btyp is one of the
1153 -- unsigned types, depending on the length.
1155 if Len_Bits
<= Standard_Short_Short_Integer_Size
then
1156 Btyp
:= RTE
(RE_Short_Short_Unsigned
);
1158 elsif Len_Bits
<= Standard_Short_Integer_Size
then
1159 Btyp
:= RTE
(RE_Short_Unsigned
);
1161 elsif Len_Bits
<= Standard_Integer_Size
then
1162 Btyp
:= RTE
(RE_Unsigned
);
1164 elsif Len_Bits
<= Standard_Long_Integer_Size
then
1165 Btyp
:= RTE
(RE_Long_Unsigned
);
1168 Btyp
:= RTE
(RE_Long_Long_Unsigned
);
1171 Lit
:= Make_Integer_Literal
(Loc
, 2 ** Len_Bits
- 1);
1172 Set_Print_In_Hex
(Lit
);
1175 Make_Subtype_Declaration
(Loc
,
1176 Defining_Identifier
=> PAT
,
1177 Subtype_Indication
=>
1178 Make_Subtype_Indication
(Loc
,
1179 Subtype_Mark
=> New_Occurrence_Of
(Btyp
, Loc
),
1182 Make_Range_Constraint
(Loc
,
1186 Make_Integer_Literal
(Loc
, 0),
1187 High_Bound
=> Lit
))));
1189 if PASize
= Uint_0
then
1198 -- Could not use a modular type, for all other cases, we build
1199 -- a packed array subtype:
1202 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1204 -- Bits is the length of the array in bits
1211 Make_Op_Multiply
(Loc
,
1213 Make_Integer_Literal
(Loc
, Csize
),
1214 Right_Opnd
=> Len_Expr
),
1217 Make_Integer_Literal
(Loc
, 7));
1219 Set_Paren_Count
(Bits_U1
, 1);
1222 Make_Op_Subtract
(Loc
,
1224 Make_Op_Divide
(Loc
,
1225 Left_Opnd
=> Bits_U1
,
1226 Right_Opnd
=> Make_Integer_Literal
(Loc
, 8)),
1227 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
1230 Make_Subtype_Declaration
(Loc
,
1231 Defining_Identifier
=> PAT
,
1232 Subtype_Indication
=>
1233 Make_Subtype_Indication
(Loc
,
1234 Subtype_Mark
=> New_Occurrence_Of
(PB_Type
, Loc
),
1236 Make_Index_Or_Discriminant_Constraint
(Loc
,
1237 Constraints
=> New_List
(
1240 Make_Integer_Literal
(Loc
, 0),
1242 Convert_To
(Standard_Integer
, PAT_High
))))));
1246 -- Currently the code in this unit requires that packed arrays
1247 -- represented by non-modular arrays of bytes be on a byte
1248 -- boundary for bit sizes handled by System.Pack_nn units.
1249 -- That's because these units assume the array being accessed
1250 -- starts on a byte boundary.
1252 if Get_Id
(UI_To_Int
(Csize
)) /= RE_Null
then
1253 Set_Must_Be_On_Byte_Boundary
(Typ
);
1256 end Create_Packed_Array_Type
;
1258 -----------------------------------
1259 -- Expand_Bit_Packed_Element_Set --
1260 -----------------------------------
1262 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
) is
1263 Loc
: constant Source_Ptr
:= Sloc
(N
);
1264 Lhs
: constant Node_Id
:= Name
(N
);
1266 Ass_OK
: constant Boolean := Assignment_OK
(Lhs
);
1267 -- Used to preserve assignment OK status when assignment is rewritten
1269 Rhs
: Node_Id
:= Expression
(N
);
1270 -- Initially Rhs is the right hand side value, it will be replaced
1271 -- later by an appropriate unchecked conversion for the assignment.
1281 -- The expression for the shift value that is required
1283 Shift_Used
: Boolean := False;
1284 -- Set True if Shift has been used in the generated code at least
1285 -- once, so that it must be duplicated if used again
1290 Rhs_Val_Known
: Boolean;
1292 -- If the value of the right hand side as an integer constant is
1293 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1294 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1295 -- the Rhs_Val is undefined.
1297 function Get_Shift
return Node_Id
;
1298 -- Function used to get the value of Shift, making sure that it
1299 -- gets duplicated if the function is called more than once.
1305 function Get_Shift
return Node_Id
is
1307 -- If we used the shift value already, then duplicate it. We
1308 -- set a temporary parent in case actions have to be inserted.
1311 Set_Parent
(Shift
, N
);
1312 return Duplicate_Subexpr_No_Checks
(Shift
);
1314 -- If first time, use Shift unchanged, and set flag for first use
1322 -- Start of processing for Expand_Bit_Packed_Element_Set
1325 pragma Assert
(Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
))));
1327 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1328 Convert_To_Actual_Subtype
(Obj
);
1329 Atyp
:= Etype
(Obj
);
1330 PAT
:= Packed_Array_Type
(Atyp
);
1331 Ctyp
:= Component_Type
(Atyp
);
1332 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1334 -- We convert the right hand side to the proper subtype to ensure
1335 -- that an appropriate range check is made (since the normal range
1336 -- check from assignment will be lost in the transformations). This
1337 -- conversion is analyzed immediately so that subsequent processing
1338 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1340 -- If the right-hand side is a string literal, create a temporary for
1341 -- it, constant-folding is not ready to wrap the bit representation
1342 -- of a string literal.
1344 if Nkind
(Rhs
) = N_String_Literal
then
1349 Make_Object_Declaration
(Loc
,
1350 Defining_Identifier
=>
1351 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T')),
1352 Object_Definition
=> New_Occurrence_Of
(Ctyp
, Loc
),
1353 Expression
=> New_Copy_Tree
(Rhs
));
1355 Insert_Actions
(N
, New_List
(Decl
));
1356 Rhs
:= New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
);
1360 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1361 Set_Parent
(Rhs
, N
);
1362 Analyze_And_Resolve
(Rhs
, Ctyp
);
1364 -- Case of component size 1,2,4 or any component size for the modular
1365 -- case. These are the cases for which we can inline the code.
1367 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1368 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1370 Setup_Inline_Packed_Array_Reference
(Lhs
, Atyp
, Obj
, Cmask
, Shift
);
1372 -- The statement to be generated is:
1374 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1376 -- where mask1 is obtained by shifting Cmask left Shift bits
1377 -- and then complementing the result.
1379 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1381 -- the "or ..." is omitted if rhs is constant and all 0 bits
1383 -- rhs is converted to the appropriate type
1385 -- The result is converted back to the array type, since
1386 -- otherwise we lose knowledge of the packed nature.
1388 -- Determine if right side is all 0 bits or all 1 bits
1390 if Compile_Time_Known_Value
(Rhs
) then
1391 Rhs_Val
:= Expr_Rep_Value
(Rhs
);
1392 Rhs_Val_Known
:= True;
1394 -- The following test catches the case of an unchecked conversion
1395 -- of an integer literal. This results from optimizing aggregates
1398 elsif Nkind
(Rhs
) = N_Unchecked_Type_Conversion
1399 and then Compile_Time_Known_Value
(Expression
(Rhs
))
1401 Rhs_Val
:= Expr_Rep_Value
(Expression
(Rhs
));
1402 Rhs_Val_Known
:= True;
1406 Rhs_Val_Known
:= False;
1409 -- Some special checks for the case where the right hand value
1410 -- is known at compile time. Basically we have to take care of
1411 -- the implicit conversion to the subtype of the component object.
1413 if Rhs_Val_Known
then
1415 -- If we have a biased component type then we must manually do
1416 -- the biasing, since we are taking responsibility in this case
1417 -- for constructing the exact bit pattern to be used.
1419 if Has_Biased_Representation
(Ctyp
) then
1420 Rhs_Val
:= Rhs_Val
- Expr_Rep_Value
(Type_Low_Bound
(Ctyp
));
1423 -- For a negative value, we manually convert the twos complement
1424 -- value to a corresponding unsigned value, so that the proper
1425 -- field width is maintained. If we did not do this, we would
1426 -- get too many leading sign bits later on.
1429 Rhs_Val
:= 2 ** UI_From_Int
(Csiz
) + Rhs_Val
;
1433 -- Now create copies removing side effects. Note that in some
1434 -- complex cases, this may cause the fact that we have already
1435 -- set a packed array type on Obj to get lost. So we save the
1436 -- type of Obj, and make sure it is reset properly.
1439 T
: constant Entity_Id
:= Etype
(Obj
);
1441 New_Lhs
:= Duplicate_Subexpr
(Obj
, True);
1442 New_Rhs
:= Duplicate_Subexpr_No_Checks
(Obj
);
1444 Set_Etype
(New_Lhs
, T
);
1445 Set_Etype
(New_Rhs
, T
);
1448 -- First we deal with the "and"
1450 if not Rhs_Val_Known
or else Rhs_Val
/= Cmask
then
1456 if Compile_Time_Known_Value
(Shift
) then
1458 Make_Integer_Literal
(Loc
,
1459 Modulus
(Etype
(Obj
)) - 1 -
1460 (Cmask
* (2 ** Expr_Value
(Get_Shift
))));
1461 Set_Print_In_Hex
(Mask1
);
1464 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1465 Set_Print_In_Hex
(Lit
);
1468 Right_Opnd
=> Make_Shift_Left
(Lit
, Get_Shift
));
1473 Left_Opnd
=> New_Rhs
,
1474 Right_Opnd
=> Mask1
);
1478 -- Then deal with the "or"
1480 if not Rhs_Val_Known
or else Rhs_Val
/= 0 then
1484 procedure Fixup_Rhs
;
1485 -- Adjust Rhs by bias if biased representation for components
1486 -- or remove extraneous high order sign bits if signed.
1488 procedure Fixup_Rhs
is
1489 Etyp
: constant Entity_Id
:= Etype
(Rhs
);
1492 -- For biased case, do the required biasing by simply
1493 -- converting to the biased subtype (the conversion
1494 -- will generate the required bias).
1496 if Has_Biased_Representation
(Ctyp
) then
1497 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1499 -- For a signed integer type that is not biased, generate
1500 -- a conversion to unsigned to strip high order sign bits.
1502 elsif Is_Signed_Integer_Type
(Ctyp
) then
1503 Rhs
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Csiz
)), Rhs
);
1506 -- Set Etype, since it can be referenced before the
1507 -- node is completely analyzed.
1509 Set_Etype
(Rhs
, Etyp
);
1511 -- We now need to do an unchecked conversion of the
1512 -- result to the target type, but it is important that
1513 -- this conversion be a right justified conversion and
1514 -- not a left justified conversion.
1516 Rhs
:= RJ_Unchecked_Convert_To
(Etype
(Obj
), Rhs
);
1522 and then Compile_Time_Known_Value
(Get_Shift
)
1525 Make_Integer_Literal
(Loc
,
1526 Rhs_Val
* (2 ** Expr_Value
(Get_Shift
)));
1527 Set_Print_In_Hex
(Or_Rhs
);
1530 -- We have to convert the right hand side to Etype (Obj).
1531 -- A special case case arises if what we have now is a Val
1532 -- attribute reference whose expression type is Etype (Obj).
1533 -- This happens for assignments of fields from the same
1534 -- array. In this case we get the required right hand side
1535 -- by simply removing the inner attribute reference.
1537 if Nkind
(Rhs
) = N_Attribute_Reference
1538 and then Attribute_Name
(Rhs
) = Name_Val
1539 and then Etype
(First
(Expressions
(Rhs
))) = Etype
(Obj
)
1541 Rhs
:= Relocate_Node
(First
(Expressions
(Rhs
)));
1544 -- If the value of the right hand side is a known integer
1545 -- value, then just replace it by an untyped constant,
1546 -- which will be properly retyped when we analyze and
1547 -- resolve the expression.
1549 elsif Rhs_Val_Known
then
1551 -- Note that Rhs_Val has already been normalized to
1552 -- be an unsigned value with the proper number of bits.
1555 Make_Integer_Literal
(Loc
, Rhs_Val
);
1557 -- Otherwise we need an unchecked conversion
1563 Or_Rhs
:= Make_Shift_Left
(Rhs
, Get_Shift
);
1566 if Nkind
(New_Rhs
) = N_Op_And
then
1567 Set_Paren_Count
(New_Rhs
, 1);
1572 Left_Opnd
=> New_Rhs
,
1573 Right_Opnd
=> Or_Rhs
);
1577 -- Now do the rewrite
1580 Make_Assignment_Statement
(Loc
,
1583 Unchecked_Convert_To
(Etype
(New_Lhs
), New_Rhs
)));
1584 Set_Assignment_OK
(Name
(N
), Ass_OK
);
1586 -- All other component sizes for non-modular case
1591 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1593 -- where Subscr is the computed linear subscript
1596 Bits_nn
: constant Entity_Id
:= RTE
(Bits_Id
(Csiz
));
1602 if No
(Bits_nn
) then
1604 -- Error, most likely High_Integrity_Mode restriction
1609 -- Acquire proper Set entity. We use the aligned or unaligned
1610 -- case as appropriate.
1612 if Known_Aligned_Enough
(Obj
, Csiz
) then
1613 Set_nn
:= RTE
(Set_Id
(Csiz
));
1615 Set_nn
:= RTE
(SetU_Id
(Csiz
));
1618 -- Now generate the set reference
1620 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1621 Convert_To_Actual_Subtype
(Obj
);
1622 Atyp
:= Etype
(Obj
);
1623 Compute_Linear_Subscript
(Atyp
, Lhs
, Subscr
);
1625 -- Below we must make the assumption that Obj is
1626 -- at least byte aligned, since otherwise its address
1627 -- cannot be taken. The assumption holds since the
1628 -- only arrays that can be misaligned are small packed
1629 -- arrays which are implemented as a modular type, and
1630 -- that is not the case here.
1633 Make_Procedure_Call_Statement
(Loc
,
1634 Name
=> New_Occurrence_Of
(Set_nn
, Loc
),
1635 Parameter_Associations
=> New_List
(
1636 Make_Attribute_Reference
(Loc
,
1638 Attribute_Name
=> Name_Address
),
1640 Unchecked_Convert_To
(Bits_nn
,
1641 Convert_To
(Ctyp
, Rhs
)))));
1646 Analyze
(N
, Suppress
=> All_Checks
);
1647 end Expand_Bit_Packed_Element_Set
;
1649 -------------------------------------
1650 -- Expand_Packed_Address_Reference --
1651 -------------------------------------
1653 procedure Expand_Packed_Address_Reference
(N
: Node_Id
) is
1654 Loc
: constant Source_Ptr
:= Sloc
(N
);
1666 -- We build up an expression serially that has the form
1668 -- outer_object'Address
1669 -- + (linear-subscript * component_size for each array reference
1670 -- + field'Bit_Position for each record field
1672 -- + ...) / Storage_Unit;
1674 -- Some additional conversions are required to deal with the addition
1675 -- operation, which is not normally visible to generated code.
1678 Ploc
:= Sloc
(Pref
);
1680 if Nkind
(Pref
) = N_Indexed_Component
then
1681 Convert_To_Actual_Subtype
(Prefix
(Pref
));
1682 Atyp
:= Etype
(Prefix
(Pref
));
1683 Compute_Linear_Subscript
(Atyp
, Pref
, Subscr
);
1686 Make_Op_Multiply
(Ploc
,
1687 Left_Opnd
=> Subscr
,
1689 Make_Attribute_Reference
(Ploc
,
1690 Prefix
=> New_Occurrence_Of
(Atyp
, Ploc
),
1691 Attribute_Name
=> Name_Component_Size
));
1693 elsif Nkind
(Pref
) = N_Selected_Component
then
1695 Make_Attribute_Reference
(Ploc
,
1696 Prefix
=> Selector_Name
(Pref
),
1697 Attribute_Name
=> Name_Bit_Position
);
1703 Term
:= Convert_To
(RTE
(RE_Integer_Address
), Term
);
1712 Right_Opnd
=> Term
);
1715 Pref
:= Prefix
(Pref
);
1719 Unchecked_Convert_To
(RTE
(RE_Address
),
1722 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1723 Make_Attribute_Reference
(Loc
,
1725 Attribute_Name
=> Name_Address
)),
1728 Make_Op_Divide
(Loc
,
1731 Make_Integer_Literal
(Loc
, System_Storage_Unit
)))));
1733 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1734 end Expand_Packed_Address_Reference
;
1736 ------------------------------------
1737 -- Expand_Packed_Boolean_Operator --
1738 ------------------------------------
1740 -- This routine expands "a op b" for the packed cases
1742 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
) is
1743 Loc
: constant Source_Ptr
:= Sloc
(N
);
1744 Typ
: constant Entity_Id
:= Etype
(N
);
1745 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1746 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1753 Convert_To_Actual_Subtype
(L
);
1754 Convert_To_Actual_Subtype
(R
);
1756 Ensure_Defined
(Etype
(L
), N
);
1757 Ensure_Defined
(Etype
(R
), N
);
1759 Apply_Length_Check
(R
, Etype
(L
));
1764 -- First an odd and silly test. We explicitly check for the XOR
1765 -- case where the component type is True .. True, since this will
1766 -- raise constraint error. A special check is required since CE
1767 -- will not be required other wise (cf Expand_Packed_Not).
1769 -- No such check is required for AND and OR, since for both these
1770 -- cases False op False = False, and True op True = True.
1772 if Nkind
(N
) = N_Op_Xor
then
1774 CT
: constant Entity_Id
:= Component_Type
(Rtyp
);
1775 BT
: constant Entity_Id
:= Base_Type
(CT
);
1779 Make_Raise_Constraint_Error
(Loc
,
1785 Make_Attribute_Reference
(Loc
,
1786 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
1787 Attribute_Name
=> Name_First
),
1791 New_Occurrence_Of
(Standard_True
, Loc
))),
1796 Make_Attribute_Reference
(Loc
,
1797 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
1798 Attribute_Name
=> Name_Last
),
1802 New_Occurrence_Of
(Standard_True
, Loc
)))),
1803 Reason
=> CE_Range_Check_Failed
));
1807 -- Now that that silliness is taken care of, get packed array type
1809 Convert_To_PAT_Type
(L
);
1810 Convert_To_PAT_Type
(R
);
1814 -- For the modular case, we expand a op b into
1816 -- rtyp!(pat!(a) op pat!(b))
1818 -- where rtyp is the Etype of the left operand. Note that we do not
1819 -- convert to the base type, since this would be unconstrained, and
1820 -- hence not have a corresponding packed array type set.
1822 -- Note that both operands must be modular for this code to be used
1824 if Is_Modular_Integer_Type
(PAT
)
1826 Is_Modular_Integer_Type
(Etype
(R
))
1832 if Nkind
(N
) = N_Op_And
then
1833 P
:= Make_Op_And
(Loc
, L
, R
);
1835 elsif Nkind
(N
) = N_Op_Or
then
1836 P
:= Make_Op_Or
(Loc
, L
, R
);
1838 else -- Nkind (N) = N_Op_Xor
1839 P
:= Make_Op_Xor
(Loc
, L
, R
);
1842 Rewrite
(N
, Unchecked_Convert_To
(Ltyp
, P
));
1845 -- For the array case, we insert the actions
1849 -- System.Bitops.Bit_And/Or/Xor
1851 -- Ltype'Length * Ltype'Component_Size;
1853 -- Rtype'Length * Rtype'Component_Size
1856 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1857 -- the second argument and fourth arguments are the lengths of the
1858 -- operands in bits. Then we replace the expression by a reference
1861 -- Note that if we are mixing a modular and array operand, everything
1862 -- works fine, since we ensure that the modular representation has the
1863 -- same physical layout as the array representation (that's what the
1864 -- left justified modular stuff in the big-endian case is about).
1868 Result_Ent
: constant Entity_Id
:=
1869 Make_Defining_Identifier
(Loc
,
1870 Chars
=> New_Internal_Name
('T'));
1875 if Nkind
(N
) = N_Op_And
then
1878 elsif Nkind
(N
) = N_Op_Or
then
1881 else -- Nkind (N) = N_Op_Xor
1885 Insert_Actions
(N
, New_List
(
1887 Make_Object_Declaration
(Loc
,
1888 Defining_Identifier
=> Result_Ent
,
1889 Object_Definition
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1891 Make_Procedure_Call_Statement
(Loc
,
1892 Name
=> New_Occurrence_Of
(RTE
(E_Id
), Loc
),
1893 Parameter_Associations
=> New_List
(
1895 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1897 Attribute_Name
=> Name_Address
),
1899 Make_Op_Multiply
(Loc
,
1901 Make_Attribute_Reference
(Loc
,
1904 (Etype
(First_Index
(Ltyp
)), Loc
),
1905 Attribute_Name
=> Name_Range_Length
),
1908 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
))),
1910 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1912 Attribute_Name
=> Name_Address
),
1914 Make_Op_Multiply
(Loc
,
1916 Make_Attribute_Reference
(Loc
,
1919 (Etype
(First_Index
(Rtyp
)), Loc
),
1920 Attribute_Name
=> Name_Range_Length
),
1923 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
1925 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1926 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
1927 Attribute_Name
=> Name_Address
)))));
1930 New_Occurrence_Of
(Result_Ent
, Loc
));
1934 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
1935 end Expand_Packed_Boolean_Operator
;
1937 -------------------------------------
1938 -- Expand_Packed_Element_Reference --
1939 -------------------------------------
1941 procedure Expand_Packed_Element_Reference
(N
: Node_Id
) is
1942 Loc
: constant Source_Ptr
:= Sloc
(N
);
1954 -- If not bit packed, we have the enumeration case, which is easily
1955 -- dealt with (just adjust the subscripts of the indexed component)
1957 -- Note: this leaves the result as an indexed component, which is
1958 -- still a variable, so can be used in the assignment case, as is
1959 -- required in the enumeration case.
1961 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
1962 Setup_Enumeration_Packed_Array_Reference
(N
);
1966 -- Remaining processing is for the bit-packed case
1968 Obj
:= Relocate_Node
(Prefix
(N
));
1969 Convert_To_Actual_Subtype
(Obj
);
1970 Atyp
:= Etype
(Obj
);
1971 PAT
:= Packed_Array_Type
(Atyp
);
1972 Ctyp
:= Component_Type
(Atyp
);
1973 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1975 -- Case of component size 1,2,4 or any component size for the modular
1976 -- case. These are the cases for which we can inline the code.
1978 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1979 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1981 Setup_Inline_Packed_Array_Reference
(N
, Atyp
, Obj
, Cmask
, Shift
);
1982 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1983 Set_Print_In_Hex
(Lit
);
1985 -- We generate a shift right to position the field, followed by a
1986 -- masking operation to extract the bit field, and we finally do an
1987 -- unchecked conversion to convert the result to the required target.
1989 -- Note that the unchecked conversion automatically deals with the
1990 -- bias if we are dealing with a biased representation. What will
1991 -- happen is that we temporarily generate the biased representation,
1992 -- but almost immediately that will be converted to the original
1993 -- unbiased component type, and the bias will disappear.
1997 Left_Opnd
=> Make_Shift_Right
(Obj
, Shift
),
2000 -- We neded to analyze this before we do the unchecked convert
2001 -- below, but we need it temporarily attached to the tree for
2002 -- this analysis (hence the temporary Set_Parent call).
2004 Set_Parent
(Arg
, Parent
(N
));
2005 Analyze_And_Resolve
(Arg
);
2008 RJ_Unchecked_Convert_To
(Ctyp
, Arg
));
2010 -- All other component sizes for non-modular case
2015 -- Component_Type!(Get_nn (Arr'address, Subscr))
2017 -- where Subscr is the computed linear subscript
2024 -- Acquire proper Get entity. We use the aligned or unaligned
2025 -- case as appropriate.
2027 if Known_Aligned_Enough
(Obj
, Csiz
) then
2028 Get_nn
:= RTE
(Get_Id
(Csiz
));
2030 Get_nn
:= RTE
(GetU_Id
(Csiz
));
2033 -- Now generate the get reference
2035 Compute_Linear_Subscript
(Atyp
, N
, Subscr
);
2037 -- Below we make the assumption that Obj is at least byte
2038 -- aligned, since otherwise its address cannot be taken.
2039 -- The assumption holds since the only arrays that can be
2040 -- misaligned are small packed arrays which are implemented
2041 -- as a modular type, and that is not the case here.
2044 Unchecked_Convert_To
(Ctyp
,
2045 Make_Function_Call
(Loc
,
2046 Name
=> New_Occurrence_Of
(Get_nn
, Loc
),
2047 Parameter_Associations
=> New_List
(
2048 Make_Attribute_Reference
(Loc
,
2050 Attribute_Name
=> Name_Address
),
2055 Analyze_And_Resolve
(N
, Ctyp
, Suppress
=> All_Checks
);
2057 end Expand_Packed_Element_Reference
;
2059 ----------------------
2060 -- Expand_Packed_Eq --
2061 ----------------------
2063 -- Handles expansion of "=" on packed array types
2065 procedure Expand_Packed_Eq
(N
: Node_Id
) is
2066 Loc
: constant Source_Ptr
:= Sloc
(N
);
2067 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2068 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2078 Convert_To_Actual_Subtype
(L
);
2079 Convert_To_Actual_Subtype
(R
);
2080 Ltyp
:= Underlying_Type
(Etype
(L
));
2081 Rtyp
:= Underlying_Type
(Etype
(R
));
2083 Convert_To_PAT_Type
(L
);
2084 Convert_To_PAT_Type
(R
);
2088 Make_Op_Multiply
(Loc
,
2090 Make_Attribute_Reference
(Loc
,
2091 Prefix
=> New_Occurrence_Of
(Ltyp
, Loc
),
2092 Attribute_Name
=> Name_Length
),
2094 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
)));
2097 Make_Op_Multiply
(Loc
,
2099 Make_Attribute_Reference
(Loc
,
2100 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
),
2101 Attribute_Name
=> Name_Length
),
2103 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
)));
2105 -- For the modular case, we transform the comparison to:
2107 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2109 -- where PAT is the packed array type. This works fine, since in the
2110 -- modular case we guarantee that the unused bits are always zeroes.
2111 -- We do have to compare the lengths because we could be comparing
2112 -- two different subtypes of the same base type.
2114 if Is_Modular_Integer_Type
(PAT
) then
2119 Left_Opnd
=> LLexpr
,
2120 Right_Opnd
=> RLexpr
),
2127 -- For the non-modular case, we call a runtime routine
2129 -- System.Bit_Ops.Bit_Eq
2130 -- (L'Address, L_Length, R'Address, R_Length)
2132 -- where PAT is the packed array type, and the lengths are the lengths
2133 -- in bits of the original packed arrays. This routine takes care of
2134 -- not comparing the unused bits in the last byte.
2138 Make_Function_Call
(Loc
,
2139 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Eq
), Loc
),
2140 Parameter_Associations
=> New_List
(
2141 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2143 Attribute_Name
=> Name_Address
),
2147 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2149 Attribute_Name
=> Name_Address
),
2154 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
2155 end Expand_Packed_Eq
;
2157 -----------------------
2158 -- Expand_Packed_Not --
2159 -----------------------
2161 -- Handles expansion of "not" on packed array types
2163 procedure Expand_Packed_Not
(N
: Node_Id
) is
2164 Loc
: constant Source_Ptr
:= Sloc
(N
);
2165 Typ
: constant Entity_Id
:= Etype
(N
);
2166 Opnd
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2173 Convert_To_Actual_Subtype
(Opnd
);
2174 Rtyp
:= Etype
(Opnd
);
2176 -- First an odd and silly test. We explicitly check for the case
2177 -- where the 'First of the component type is equal to the 'Last of
2178 -- this component type, and if this is the case, we make sure that
2179 -- constraint error is raised. The reason is that the NOT is bound
2180 -- to cause CE in this case, and we will not otherwise catch it.
2182 -- Believe it or not, this was reported as a bug. Note that nearly
2183 -- always, the test will evaluate statically to False, so the code
2184 -- will be statically removed, and no extra overhead caused.
2187 CT
: constant Entity_Id
:= Component_Type
(Rtyp
);
2191 Make_Raise_Constraint_Error
(Loc
,
2195 Make_Attribute_Reference
(Loc
,
2196 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
2197 Attribute_Name
=> Name_First
),
2200 Make_Attribute_Reference
(Loc
,
2201 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
2202 Attribute_Name
=> Name_Last
)),
2203 Reason
=> CE_Range_Check_Failed
));
2206 -- Now that that silliness is taken care of, get packed array type
2208 Convert_To_PAT_Type
(Opnd
);
2209 PAT
:= Etype
(Opnd
);
2211 -- For the case where the packed array type is a modular type,
2212 -- not A expands simply into:
2214 -- rtyp!(PAT!(A) xor mask)
2216 -- where PAT is the packed array type, and mask is a mask of all
2217 -- one bits of length equal to the size of this packed type and
2218 -- rtyp is the actual subtype of the operand
2220 Lit
:= Make_Integer_Literal
(Loc
, 2 ** RM_Size
(PAT
) - 1);
2221 Set_Print_In_Hex
(Lit
);
2223 if not Is_Array_Type
(PAT
) then
2225 Unchecked_Convert_To
(Rtyp
,
2228 Right_Opnd
=> Lit
)));
2230 -- For the array case, we insert the actions
2234 -- System.Bitops.Bit_Not
2236 -- Typ'Length * Typ'Component_Size;
2239 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2240 -- argument is the length of the operand in bits. Then we replace
2241 -- the expression by a reference to Result.
2245 Result_Ent
: constant Entity_Id
:=
2246 Make_Defining_Identifier
(Loc
,
2247 Chars
=> New_Internal_Name
('T'));
2250 Insert_Actions
(N
, New_List
(
2252 Make_Object_Declaration
(Loc
,
2253 Defining_Identifier
=> Result_Ent
,
2254 Object_Definition
=> New_Occurrence_Of
(Rtyp
, Loc
)),
2256 Make_Procedure_Call_Statement
(Loc
,
2257 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Not
), Loc
),
2258 Parameter_Associations
=> New_List
(
2260 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2262 Attribute_Name
=> Name_Address
),
2264 Make_Op_Multiply
(Loc
,
2266 Make_Attribute_Reference
(Loc
,
2269 (Etype
(First_Index
(Rtyp
)), Loc
),
2270 Attribute_Name
=> Name_Range_Length
),
2273 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2275 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2276 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
2277 Attribute_Name
=> Name_Address
)))));
2280 New_Occurrence_Of
(Result_Ent
, Loc
));
2284 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2286 end Expand_Packed_Not
;
2288 -------------------------------------
2289 -- Involves_Packed_Array_Reference --
2290 -------------------------------------
2292 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean is
2294 if Nkind
(N
) = N_Indexed_Component
2295 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
2299 elsif Nkind
(N
) = N_Selected_Component
then
2300 return Involves_Packed_Array_Reference
(Prefix
(N
));
2305 end Involves_Packed_Array_Reference
;
2307 --------------------------
2308 -- Known_Aligned_Enough --
2309 --------------------------
2311 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean is
2312 Typ
: constant Entity_Id
:= Etype
(Obj
);
2314 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean;
2315 -- If the component is in a record that contains previous packed
2316 -- components, consider it unaligned because the back-end might
2317 -- choose to pack the rest of the record. Lead to less efficient code,
2318 -- but safer vis-a-vis of back-end choices.
2320 --------------------------------
2321 -- In_Partially_Packed_Record --
2322 --------------------------------
2324 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean is
2325 Rec_Type
: constant Entity_Id
:= Scope
(Comp
);
2326 Prev_Comp
: Entity_Id
;
2329 Prev_Comp
:= First_Entity
(Rec_Type
);
2330 while Present
(Prev_Comp
) loop
2331 if Is_Packed
(Etype
(Prev_Comp
)) then
2334 elsif Prev_Comp
= Comp
then
2338 Next_Entity
(Prev_Comp
);
2342 end In_Partially_Packed_Record
;
2344 -- Start of processing for Known_Aligned_Enough
2347 -- Odd bit sizes don't need alignment anyway
2349 if Csiz
mod 2 = 1 then
2352 -- If we have a specified alignment, see if it is sufficient, if not
2353 -- then we can't possibly be aligned enough in any case.
2355 elsif Known_Alignment
(Etype
(Obj
)) then
2356 -- Alignment required is 4 if size is a multiple of 4, and
2357 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2359 if Alignment
(Etype
(Obj
)) < 4 - (Csiz
mod 4) then
2364 -- OK, alignment should be sufficient, if object is aligned
2366 -- If object is strictly aligned, then it is definitely aligned
2368 if Strict_Alignment
(Typ
) then
2371 -- Case of subscripted array reference
2373 elsif Nkind
(Obj
) = N_Indexed_Component
then
2375 -- If we have a pointer to an array, then this is definitely
2376 -- aligned, because pointers always point to aligned versions.
2378 if Is_Access_Type
(Etype
(Prefix
(Obj
))) then
2381 -- Otherwise, go look at the prefix
2384 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2387 -- Case of record field
2389 elsif Nkind
(Obj
) = N_Selected_Component
then
2391 -- What is significant here is whether the record type is packed
2393 if Is_Record_Type
(Etype
(Prefix
(Obj
)))
2394 and then Is_Packed
(Etype
(Prefix
(Obj
)))
2398 -- Or the component has a component clause which might cause
2399 -- the component to become unaligned (we can't tell if the
2400 -- backend is doing alignment computations).
2402 elsif Present
(Component_Clause
(Entity
(Selector_Name
(Obj
)))) then
2405 elsif In_Partially_Packed_Record
(Entity
(Selector_Name
(Obj
))) then
2408 -- In all other cases, go look at prefix
2411 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2414 elsif Nkind
(Obj
) = N_Type_Conversion
then
2415 return Known_Aligned_Enough
(Expression
(Obj
), Csiz
);
2417 -- For a formal parameter, it is safer to assume that it is not
2418 -- aligned, because the formal may be unconstrained while the actual
2419 -- is constrained. In this situation, a small constrained packed
2420 -- array, represented in modular form, may be unaligned.
2422 elsif Is_Entity_Name
(Obj
) then
2423 return not Is_Formal
(Entity
(Obj
));
2426 -- If none of the above, must be aligned
2429 end Known_Aligned_Enough
;
2431 ---------------------
2432 -- Make_Shift_Left --
2433 ---------------------
2435 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2439 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2443 Make_Op_Shift_Left
(Sloc
(N
),
2446 Set_Shift_Count_OK
(Nod
, True);
2449 end Make_Shift_Left
;
2451 ----------------------
2452 -- Make_Shift_Right --
2453 ----------------------
2455 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2459 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2463 Make_Op_Shift_Right
(Sloc
(N
),
2466 Set_Shift_Count_OK
(Nod
, True);
2469 end Make_Shift_Right
;
2471 -----------------------------
2472 -- RJ_Unchecked_Convert_To --
2473 -----------------------------
2475 function RJ_Unchecked_Convert_To
2477 Expr
: Node_Id
) return Node_Id
2479 Source_Typ
: constant Entity_Id
:= Etype
(Expr
);
2480 Target_Typ
: constant Entity_Id
:= Typ
;
2482 Src
: Node_Id
:= Expr
;
2488 Source_Siz
:= UI_To_Int
(RM_Size
(Source_Typ
));
2489 Target_Siz
:= UI_To_Int
(RM_Size
(Target_Typ
));
2491 -- First step, if the source type is not a discrete type, then we
2492 -- first convert to a modular type of the source length, since
2493 -- otherwise, on a big-endian machine, we get left-justification.
2494 -- We do it for little-endian machines as well, because there might
2495 -- be junk bits that are not cleared if the type is not numeric.
2497 if Source_Siz
/= Target_Siz
2498 and then not Is_Discrete_Type
(Source_Typ
)
2500 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Source_Siz
)), Src
);
2503 -- In the big endian case, if the lengths of the two types differ,
2504 -- then we must worry about possible left justification in the
2505 -- conversion, and avoiding that is what this is all about.
2507 if Bytes_Big_Endian
and then Source_Siz
/= Target_Siz
then
2509 -- Next step. If the target is not a discrete type, then we first
2510 -- convert to a modular type of the target length, since
2511 -- otherwise, on a big-endian machine, we get left-justification.
2513 if not Is_Discrete_Type
(Target_Typ
) then
2514 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Target_Siz
)), Src
);
2518 -- And now we can do the final conversion to the target type
2520 return Unchecked_Convert_To
(Target_Typ
, Src
);
2521 end RJ_Unchecked_Convert_To
;
2523 ----------------------------------------------
2524 -- Setup_Enumeration_Packed_Array_Reference --
2525 ----------------------------------------------
2527 -- All we have to do here is to find the subscripts that correspond
2528 -- to the index positions that have non-standard enumeration types
2529 -- and insert a Pos attribute to get the proper subscript value.
2531 -- Finally the prefix must be uncheck converted to the corresponding
2532 -- packed array type.
2534 -- Note that the component type is unchanged, so we do not need to
2535 -- fiddle with the types (Gigi always automatically takes the packed
2536 -- array type if it is set, as it will be in this case).
2538 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
) is
2539 Pfx
: constant Node_Id
:= Prefix
(N
);
2540 Typ
: constant Entity_Id
:= Etype
(N
);
2541 Exprs
: constant List_Id
:= Expressions
(N
);
2545 -- If the array is unconstrained, then we replace the array
2546 -- reference with its actual subtype. This actual subtype will
2547 -- have a packed array type with appropriate bounds.
2549 if not Is_Constrained
(Packed_Array_Type
(Etype
(Pfx
))) then
2550 Convert_To_Actual_Subtype
(Pfx
);
2553 Expr
:= First
(Exprs
);
2554 while Present
(Expr
) loop
2556 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2557 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
2560 if Is_Enumeration_Type
(Expr_Typ
)
2561 and then Has_Non_Standard_Rep
(Expr_Typ
)
2564 Make_Attribute_Reference
(Loc
,
2565 Prefix
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
2566 Attribute_Name
=> Name_Pos
,
2567 Expressions
=> New_List
(Relocate_Node
(Expr
))));
2568 Analyze_And_Resolve
(Expr
, Standard_Natural
);
2576 Make_Indexed_Component
(Sloc
(N
),
2578 Unchecked_Convert_To
(Packed_Array_Type
(Etype
(Pfx
)), Pfx
),
2579 Expressions
=> Exprs
));
2581 Analyze_And_Resolve
(N
, Typ
);
2583 end Setup_Enumeration_Packed_Array_Reference
;
2585 -----------------------------------------
2586 -- Setup_Inline_Packed_Array_Reference --
2587 -----------------------------------------
2589 procedure Setup_Inline_Packed_Array_Reference
2592 Obj
: in out Node_Id
;
2594 Shift
: out Node_Id
)
2596 Loc
: constant Source_Ptr
:= Sloc
(N
);
2603 Csiz
:= Component_Size
(Atyp
);
2605 Convert_To_PAT_Type
(Obj
);
2608 Cmask
:= 2 ** Csiz
- 1;
2610 if Is_Array_Type
(PAT
) then
2611 Otyp
:= Component_Type
(PAT
);
2612 Osiz
:= Component_Size
(PAT
);
2617 -- In the case where the PAT is a modular type, we want the actual
2618 -- size in bits of the modular value we use. This is neither the
2619 -- Object_Size nor the Value_Size, either of which may have been
2620 -- reset to strange values, but rather the minimum size. Note that
2621 -- since this is a modular type with full range, the issue of
2622 -- biased representation does not arise.
2624 Osiz
:= UI_From_Int
(Minimum_Size
(Otyp
));
2627 Compute_Linear_Subscript
(Atyp
, N
, Shift
);
2629 -- If the component size is not 1, then the subscript must be
2630 -- multiplied by the component size to get the shift count.
2634 Make_Op_Multiply
(Loc
,
2635 Left_Opnd
=> Make_Integer_Literal
(Loc
, Csiz
),
2636 Right_Opnd
=> Shift
);
2639 -- If we have the array case, then this shift count must be broken
2640 -- down into a byte subscript, and a shift within the byte.
2642 if Is_Array_Type
(PAT
) then
2645 New_Shift
: Node_Id
;
2648 -- We must analyze shift, since we will duplicate it
2650 Set_Parent
(Shift
, N
);
2652 (Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2654 -- The shift count within the word is
2659 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2660 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
));
2662 -- The subscript to be used on the PAT array is
2666 Make_Indexed_Component
(Loc
,
2668 Expressions
=> New_List
(
2669 Make_Op_Divide
(Loc
,
2670 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2671 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
))));
2676 -- For the modular integer case, the object to be manipulated is
2677 -- the entire array, so Obj is unchanged. Note that we will reset
2678 -- its type to PAT before returning to the caller.
2684 -- The one remaining step is to modify the shift count for the
2685 -- big-endian case. Consider the following example in a byte:
2687 -- xxxxxxxx bits of byte
2688 -- vvvvvvvv bits of value
2689 -- 33221100 little-endian numbering
2690 -- 00112233 big-endian numbering
2692 -- Here we have the case of 2-bit fields
2694 -- For the little-endian case, we already have the proper shift
2695 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2697 -- For the big endian case, we have to adjust the shift count,
2698 -- computing it as (N - F) - shift, where N is the number of bits
2699 -- in an element of the array used to implement the packed array,
2700 -- F is the number of bits in a source level array element, and
2701 -- shift is the count so far computed.
2703 if Bytes_Big_Endian
then
2705 Make_Op_Subtract
(Loc
,
2706 Left_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
- Csiz
),
2707 Right_Opnd
=> Shift
);
2710 Set_Parent
(Shift
, N
);
2711 Set_Parent
(Obj
, N
);
2712 Analyze_And_Resolve
(Obj
, Otyp
, Suppress
=> All_Checks
);
2713 Analyze_And_Resolve
(Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2715 -- Make sure final type of object is the appropriate packed type
2717 Set_Etype
(Obj
, Otyp
);
2719 end Setup_Inline_Packed_Array_Reference
;