1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2013, 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
;
37 with Rtsfind
; use Rtsfind
;
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 -- Entity Tables for Packed Access Routines --
82 ----------------------------------------------
84 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call library
85 -- routines. This table provides the entity for the proper routine.
87 type E_Array
is array (Int
range 01 .. 63) of RE_Id
;
89 -- Array of Bits_nn entities. Note that we do not use library routines
90 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
91 -- entries from System.Unsigned, because we also use this table for
92 -- certain special unchecked conversions in the big-endian case.
94 Bits_Id
: constant E_Array
:=
110 16 => RE_Unsigned_16
,
126 32 => RE_Unsigned_32
,
159 -- Array of Get routine entities. These are used to obtain an element from
160 -- a packed array. The N'th entry is used to obtain elements from a packed
161 -- array whose component size is N. RE_Null is used as a null entry, for
162 -- the cases where a library routine is not used.
164 Get_Id
: constant E_Array
:=
229 -- Array of Get routine entities to be used in the case where the packed
230 -- array is itself a component of a packed structure, and therefore may not
231 -- be fully aligned. This only affects the even sizes, since for the odd
232 -- sizes, we do not get any fixed alignment in any case.
234 GetU_Id
: constant E_Array
:=
299 -- Array of Set routine entities. These are used to assign an element of a
300 -- packed array. The N'th entry is used to assign elements for a packed
301 -- array whose component size is N. RE_Null is used as a null entry, for
302 -- the cases where a library routine is not used.
304 Set_Id
: constant E_Array
:=
369 -- Array of Set routine entities to be used in the case where the packed
370 -- array is itself a component of a packed structure, and therefore may not
371 -- be fully aligned. This only affects the even sizes, since for the odd
372 -- sizes, we do not get any fixed alignment in any case.
374 SetU_Id
: constant E_Array
:=
439 -----------------------
440 -- Local Subprograms --
441 -----------------------
443 procedure Compute_Linear_Subscript
446 Subscr
: out Node_Id
);
447 -- Given a constrained array type Atyp, and an indexed component node N
448 -- referencing an array object of this type, build an expression of type
449 -- Standard.Integer representing the zero-based linear subscript value.
450 -- This expression includes any required range checks.
452 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
);
453 -- Given an expression of a packed array type, builds a corresponding
454 -- expression whose type is the implementation type used to represent
455 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
457 procedure Get_Base_And_Bit_Offset
460 Offset
: out Node_Id
);
461 -- Given a node N for a name which involves a packed array reference,
462 -- return the base object of the reference and build an expression of
463 -- type Standard.Integer representing the zero-based offset in bits
464 -- from Base'Address to the first bit of the reference.
466 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean;
467 -- There are two versions of the Set routines, the ones used when the
468 -- object is known to be sufficiently well aligned given the number of
469 -- bits, and the ones used when the object is not known to be aligned.
470 -- This routine is used to determine which set to use. Obj is a reference
471 -- to the object, and Csiz is the component size of the packed array.
472 -- True is returned if the alignment of object is known to be sufficient,
473 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
476 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
477 -- Build a left shift node, checking for the case of a shift count of zero
479 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
480 -- Build a right shift node, checking for the case of a shift count of zero
482 function RJ_Unchecked_Convert_To
484 Expr
: Node_Id
) return Node_Id
;
485 -- The packed array code does unchecked conversions which in some cases
486 -- may involve non-discrete types with differing sizes. The semantics of
487 -- such conversions is potentially endian dependent, and the effect we
488 -- want here for such a conversion is to do the conversion in size as
489 -- though numeric items are involved, and we extend or truncate on the
490 -- left side. This happens naturally in the little-endian case, but in
491 -- the big endian case we can get left justification, when what we want
492 -- is right justification. This routine does the unchecked conversion in
493 -- a stepwise manner to ensure that it gives the expected result. Hence
494 -- the name (RJ = Right justified). The parameters Typ and Expr are as
495 -- for the case of a normal Unchecked_Convert_To call.
497 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
);
498 -- This routine is called in the Get and Set case for arrays that are
499 -- packed but not bit-packed, meaning that they have at least one
500 -- subscript that is of an enumeration type with a non-standard
501 -- representation. This routine modifies the given node to properly
502 -- reference the corresponding packed array type.
504 procedure Setup_Inline_Packed_Array_Reference
507 Obj
: in out Node_Id
;
509 Shift
: out Node_Id
);
510 -- This procedure performs common processing on the N_Indexed_Component
511 -- parameter given as N, whose prefix is a reference to a packed array.
512 -- This is used for the get and set when the component size is 1, 2, 4,
513 -- or for other component sizes when the packed array type is a modular
514 -- type (i.e. the cases that are handled with inline code).
518 -- N is the N_Indexed_Component node for the packed array reference
520 -- Atyp is the constrained array type (the actual subtype has been
521 -- computed if necessary to obtain the constraints, but this is still
522 -- the original array type, not the Packed_Array_Type value).
524 -- Obj is the object which is to be indexed. It is always of type Atyp.
528 -- Obj is the object containing the desired bit field. It is of type
529 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
530 -- entire value, for the small static case, or the proper selected byte
531 -- from the array in the large or dynamic case. This node is analyzed
532 -- and resolved on return.
534 -- Shift is a node representing the shift count to be used in the
535 -- rotate right instruction that positions the field for access.
536 -- This node is analyzed and resolved on return.
538 -- Cmask is a mask corresponding to the width of the component field.
539 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
541 -- Note: in some cases the call to this routine may generate actions
542 -- (for handling multi-use references and the generation of the packed
543 -- array type on the fly). Such actions are inserted into the tree
544 -- directly using Insert_Action.
548 Left_Justify
: Boolean := False;
549 Right_Justify
: Boolean := False) return Node_Id
;
550 -- Wrap N in a call to a byte swapping function, with appropriate type
551 -- conversions. If Left_Justify is set True, the value is left justified
552 -- before swapping. If Right_Justify is set True, the value is right
553 -- justified after swapping. The Etype of the returned node is an
554 -- integer type of an appropriate power-of-2 size.
562 Left_Justify
: Boolean := False;
563 Right_Justify
: Boolean := False) return Node_Id
565 Loc
: constant Source_Ptr
:= Sloc
(N
);
566 T
: constant Entity_Id
:= Etype
(N
);
567 T_Size
: constant Uint
:= RM_Size
(T
);
579 pragma Assert
(T_Size
> 8);
582 Swap_RE
:= RE_Bswap_16
;
584 elsif T_Size
<= 32 then
585 Swap_RE
:= RE_Bswap_32
;
587 else pragma Assert
(T_Size
<= 64);
588 Swap_RE
:= RE_Bswap_64
;
591 Swap_F
:= RTE
(Swap_RE
);
592 Swap_T
:= Etype
(Swap_F
);
593 Shift
:= Esize
(Swap_T
) - T_Size
;
595 Arg
:= RJ_Unchecked_Convert_To
(Swap_T
, N
);
597 if Left_Justify
and then Shift
> Uint_0
then
599 Make_Op_Shift_Left
(Loc
,
601 Right_Opnd
=> Make_Integer_Literal
(Loc
, Shift
));
605 Make_Function_Call
(Loc
,
606 Name
=> New_Occurrence_Of
(Swap_F
, Loc
),
607 Parameter_Associations
=> New_List
(Arg
));
609 if Right_Justify
and then Shift
> Uint_0
then
611 Make_Op_Shift_Right
(Loc
,
612 Left_Opnd
=> Swapped
,
613 Right_Opnd
=> Make_Integer_Literal
(Loc
, Shift
));
616 Set_Etype
(Swapped
, Swap_T
);
620 ------------------------------
621 -- Compute_Linear_Subscript --
622 ------------------------------
624 procedure Compute_Linear_Subscript
627 Subscr
: out Node_Id
)
629 Loc
: constant Source_Ptr
:= Sloc
(N
);
638 -- Loop through dimensions
640 Indx
:= First_Index
(Atyp
);
641 Oldsub
:= First
(Expressions
(N
));
643 while Present
(Indx
) loop
644 Styp
:= Etype
(Indx
);
645 Newsub
:= Relocate_Node
(Oldsub
);
647 -- Get expression for the subscript value. First, if Do_Range_Check
648 -- is set on a subscript, then we must do a range check against the
649 -- original bounds (not the bounds of the packed array type). We do
650 -- this by introducing a subtype conversion.
652 if Do_Range_Check
(Newsub
)
653 and then Etype
(Newsub
) /= Styp
655 Newsub
:= Convert_To
(Styp
, Newsub
);
658 -- Now evolve the expression for the subscript. First convert
659 -- the subscript to be zero based and of an integer type.
661 -- Case of integer type, where we just subtract to get lower bound
663 if Is_Integer_Type
(Styp
) then
665 -- If length of integer type is smaller than standard integer,
666 -- then we convert to integer first, then do the subtract
668 -- Integer (subscript) - Integer (Styp'First)
670 if Esize
(Styp
) < Esize
(Standard_Integer
) then
672 Make_Op_Subtract
(Loc
,
673 Left_Opnd
=> Convert_To
(Standard_Integer
, Newsub
),
675 Convert_To
(Standard_Integer
,
676 Make_Attribute_Reference
(Loc
,
677 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
678 Attribute_Name
=> Name_First
)));
680 -- For larger integer types, subtract first, then convert to
681 -- integer, this deals with strange long long integer bounds.
683 -- Integer (subscript - Styp'First)
687 Convert_To
(Standard_Integer
,
688 Make_Op_Subtract
(Loc
,
691 Make_Attribute_Reference
(Loc
,
692 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
693 Attribute_Name
=> Name_First
)));
696 -- For the enumeration case, we have to use 'Pos to get the value
697 -- to work with before subtracting the lower bound.
699 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
701 -- This is not quite right for bizarre cases where the size of the
702 -- enumeration type is > Integer'Size bits due to rep clause ???
705 pragma Assert
(Is_Enumeration_Type
(Styp
));
708 Make_Op_Subtract
(Loc
,
709 Left_Opnd
=> Convert_To
(Standard_Integer
,
710 Make_Attribute_Reference
(Loc
,
711 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
712 Attribute_Name
=> Name_Pos
,
713 Expressions
=> New_List
(Newsub
))),
716 Convert_To
(Standard_Integer
,
717 Make_Attribute_Reference
(Loc
,
718 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
719 Attribute_Name
=> Name_Pos
,
720 Expressions
=> New_List
(
721 Make_Attribute_Reference
(Loc
,
722 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
723 Attribute_Name
=> Name_First
)))));
726 Set_Paren_Count
(Newsub
, 1);
728 -- For the first subscript, we just copy that subscript value
733 -- Otherwise, we must multiply what we already have by the current
734 -- stride and then add in the new value to the evolving subscript.
740 Make_Op_Multiply
(Loc
,
743 Make_Attribute_Reference
(Loc
,
744 Attribute_Name
=> Name_Range_Length
,
745 Prefix
=> New_Occurrence_Of
(Styp
, Loc
))),
746 Right_Opnd
=> Newsub
);
749 -- Move to next subscript
754 end Compute_Linear_Subscript
;
756 -------------------------
757 -- Convert_To_PAT_Type --
758 -------------------------
760 -- The PAT is always obtained from the actual subtype
762 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
) is
766 Convert_To_Actual_Subtype
(Aexp
);
767 Act_ST
:= Underlying_Type
(Etype
(Aexp
));
768 Create_Packed_Array_Type
(Act_ST
);
770 -- Just replace the etype with the packed array type. This works because
771 -- the expression will not be further analyzed, and Gigi considers the
772 -- two types equivalent in any case.
774 -- This is not strictly the case ??? If the reference is an actual in
775 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
776 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
777 -- array reference, reanalysis can produce spurious type errors when the
778 -- PAT type is replaced again with the original type of the array. Same
779 -- for the case of a dereference. Ditto for function calls: expansion
780 -- may introduce additional actuals which will trigger errors if call is
781 -- reanalyzed. The following is correct and minimal, but the handling of
782 -- more complex packed expressions in actuals is confused. Probably the
783 -- problem only remains for actuals in calls.
785 Set_Etype
(Aexp
, Packed_Array_Type
(Act_ST
));
787 if Is_Entity_Name
(Aexp
)
789 (Nkind
(Aexp
) = N_Indexed_Component
790 and then Is_Entity_Name
(Prefix
(Aexp
)))
791 or else Nkind_In
(Aexp
, N_Explicit_Dereference
, N_Function_Call
)
795 end Convert_To_PAT_Type
;
797 ------------------------------
798 -- Create_Packed_Array_Type --
799 ------------------------------
801 procedure Create_Packed_Array_Type
(Typ
: Entity_Id
) is
802 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
803 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
804 Csize
: constant Uint
:= Component_Size
(Typ
);
819 procedure Install_PAT
;
820 -- This procedure is called with Decl set to the declaration for the
821 -- packed array type. It creates the type and installs it as required.
823 procedure Set_PB_Type
;
824 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
825 -- requirements (see documentation in the spec of this package).
831 procedure Install_PAT
is
832 Pushed_Scope
: Boolean := False;
835 -- We do not want to put the declaration we have created in the tree
836 -- since it is often hard, and sometimes impossible to find a proper
837 -- place for it (the impossible case arises for a packed array type
838 -- with bounds depending on the discriminant, a declaration cannot
839 -- be put inside the record, and the reference to the discriminant
840 -- cannot be outside the record).
842 -- The solution is to analyze the declaration while temporarily
843 -- attached to the tree at an appropriate point, and then we install
844 -- the resulting type as an Itype in the packed array type field of
845 -- the original type, so that no explicit declaration is required.
847 -- Note: the packed type is created in the scope of its parent
848 -- type. There are at least some cases where the current scope
849 -- is deeper, and so when this is the case, we temporarily reset
850 -- the scope for the definition. This is clearly safe, since the
851 -- first use of the packed array type will be the implicit
852 -- reference from the corresponding unpacked type when it is
855 if Is_Itype
(Typ
) then
856 Set_Parent
(Decl
, Associated_Node_For_Itype
(Typ
));
858 Set_Parent
(Decl
, Declaration_Node
(Typ
));
861 if Scope
(Typ
) /= Current_Scope
then
862 Push_Scope
(Scope
(Typ
));
863 Pushed_Scope
:= True;
866 Set_Is_Itype
(PAT
, True);
867 Set_Packed_Array_Type
(Typ
, PAT
);
868 Analyze
(Decl
, Suppress
=> All_Checks
);
874 -- Set Esize and RM_Size to the actual size of the packed object
875 -- Do not reset RM_Size if already set, as happens in the case of
878 if Unknown_Esize
(PAT
) then
879 Set_Esize
(PAT
, PASize
);
882 if Unknown_RM_Size
(PAT
) then
883 Set_RM_Size
(PAT
, PASize
);
886 Adjust_Esize_Alignment
(PAT
);
888 -- Set remaining fields of packed array type
890 Init_Alignment
(PAT
);
891 Set_Parent
(PAT
, Empty
);
892 Set_Associated_Node_For_Itype
(PAT
, Typ
);
893 Set_Is_Packed_Array_Type
(PAT
, True);
894 Set_Original_Array_Type
(PAT
, Typ
);
896 -- We definitely do not want to delay freezing for packed array
897 -- types. This is of particular importance for the itypes that
898 -- are generated for record components depending on discriminants
899 -- where there is no place to put the freeze node.
901 Set_Has_Delayed_Freeze
(PAT
, False);
902 Set_Has_Delayed_Freeze
(Etype
(PAT
), False);
904 -- If we did allocate a freeze node, then clear out the reference
905 -- since it is obsolete (should we delete the freeze node???)
907 Set_Freeze_Node
(PAT
, Empty
);
908 Set_Freeze_Node
(Etype
(PAT
), Empty
);
915 procedure Set_PB_Type
is
917 -- If the user has specified an explicit alignment for the
918 -- type or component, take it into account.
920 if Csize
<= 2 or else Csize
= 4 or else Csize
mod 2 /= 0
921 or else Alignment
(Typ
) = 1
922 or else Component_Alignment
(Typ
) = Calign_Storage_Unit
924 PB_Type
:= RTE
(RE_Packed_Bytes1
);
926 elsif Csize
mod 4 /= 0
927 or else Alignment
(Typ
) = 2
929 PB_Type
:= RTE
(RE_Packed_Bytes2
);
932 PB_Type
:= RTE
(RE_Packed_Bytes4
);
936 -- Start of processing for Create_Packed_Array_Type
939 -- If we already have a packed array type, nothing to do
941 if Present
(Packed_Array_Type
(Typ
)) then
945 -- If our immediate ancestor subtype is constrained, and it already
946 -- has a packed array type, then just share the same type, since the
947 -- bounds must be the same. If the ancestor is not an array type but
948 -- a private type, as can happen with multiple instantiations, create
949 -- a new packed type, to avoid privacy issues.
951 if Ekind
(Typ
) = E_Array_Subtype
then
952 Ancest
:= Ancestor_Subtype
(Typ
);
955 and then Is_Array_Type
(Ancest
)
956 and then Is_Constrained
(Ancest
)
957 and then Present
(Packed_Array_Type
(Ancest
))
959 Set_Packed_Array_Type
(Typ
, Packed_Array_Type
(Ancest
));
964 -- We preset the result type size from the size of the original array
965 -- type, since this size clearly belongs to the packed array type. The
966 -- size of the conceptual unpacked type is always set to unknown.
968 PASize
:= RM_Size
(Typ
);
970 -- Case of an array where at least one index is of an enumeration
971 -- type with a non-standard representation, but the component size
972 -- is not appropriate for bit packing. This is the case where we
973 -- have Is_Packed set (we would never be in this unit otherwise),
974 -- but Is_Bit_Packed_Array is false.
976 -- Note that if the component size is appropriate for bit packing,
977 -- then the circuit for the computation of the subscript properly
978 -- deals with the non-standard enumeration type case by taking the
981 if not Is_Bit_Packed_Array
(Typ
) then
983 -- Here we build a declaration:
985 -- type tttP is array (index1, index2, ...) of component_type
987 -- where index1, index2, are the index types. These are the same
988 -- as the index types of the original array, except for the non-
989 -- standard representation enumeration type case, where we have
992 -- For the unconstrained array case, we use
996 -- For the constrained case, we use
998 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
999 -- Enum_Type'Pos (Enum_Type'Last);
1002 Make_Defining_Identifier
(Loc
,
1003 Chars
=> New_External_Name
(Chars
(Typ
), 'P'));
1005 Set_Packed_Array_Type
(Typ
, PAT
);
1008 Indexes
: constant List_Id
:= New_List
;
1010 Indx_Typ
: Entity_Id
;
1011 Enum_Case
: Boolean;
1015 Indx
:= First_Index
(Typ
);
1017 while Present
(Indx
) loop
1018 Indx_Typ
:= Etype
(Indx
);
1020 Enum_Case
:= Is_Enumeration_Type
(Indx_Typ
)
1021 and then Has_Non_Standard_Rep
(Indx_Typ
);
1023 -- Unconstrained case
1025 if not Is_Constrained
(Typ
) then
1027 Indx_Typ
:= Standard_Natural
;
1030 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
1035 if not Enum_Case
then
1036 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
1040 Make_Subtype_Indication
(Loc
,
1042 New_Occurrence_Of
(Standard_Natural
, Loc
),
1044 Make_Range_Constraint
(Loc
,
1048 Make_Attribute_Reference
(Loc
,
1050 New_Occurrence_Of
(Indx_Typ
, Loc
),
1051 Attribute_Name
=> Name_Pos
,
1052 Expressions
=> New_List
(
1053 Make_Attribute_Reference
(Loc
,
1055 New_Occurrence_Of
(Indx_Typ
, Loc
),
1056 Attribute_Name
=> Name_First
))),
1059 Make_Attribute_Reference
(Loc
,
1061 New_Occurrence_Of
(Indx_Typ
, Loc
),
1062 Attribute_Name
=> Name_Pos
,
1063 Expressions
=> New_List
(
1064 Make_Attribute_Reference
(Loc
,
1066 New_Occurrence_Of
(Indx_Typ
, Loc
),
1067 Attribute_Name
=> Name_Last
)))))));
1075 if not Is_Constrained
(Typ
) then
1077 Make_Unconstrained_Array_Definition
(Loc
,
1078 Subtype_Marks
=> Indexes
,
1079 Component_Definition
=>
1080 Make_Component_Definition
(Loc
,
1081 Aliased_Present
=> False,
1082 Subtype_Indication
=>
1083 New_Occurrence_Of
(Ctyp
, Loc
)));
1087 Make_Constrained_Array_Definition
(Loc
,
1088 Discrete_Subtype_Definitions
=> Indexes
,
1089 Component_Definition
=>
1090 Make_Component_Definition
(Loc
,
1091 Aliased_Present
=> False,
1092 Subtype_Indication
=>
1093 New_Occurrence_Of
(Ctyp
, Loc
)));
1097 Make_Full_Type_Declaration
(Loc
,
1098 Defining_Identifier
=> PAT
,
1099 Type_Definition
=> Typedef
);
1102 -- Set type as packed array type and install it
1104 Set_Is_Packed_Array_Type
(PAT
);
1108 -- Case of bit-packing required for unconstrained array. We create
1109 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1111 elsif not Is_Constrained
(Typ
) then
1113 Make_Defining_Identifier
(Loc
,
1114 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1116 Set_Packed_Array_Type
(Typ
, PAT
);
1120 Make_Subtype_Declaration
(Loc
,
1121 Defining_Identifier
=> PAT
,
1122 Subtype_Indication
=> New_Occurrence_Of
(PB_Type
, Loc
));
1126 -- Remaining code is for the case of bit-packing for constrained array
1128 -- The name of the packed array subtype is
1132 -- where sss is the component size in bits and ttt is the name of
1133 -- the parent packed type.
1137 Make_Defining_Identifier
(Loc
,
1138 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1140 Set_Packed_Array_Type
(Typ
, PAT
);
1142 -- Build an expression for the length of the array in bits.
1143 -- This is the product of the length of each of the dimensions
1149 Len_Expr
:= Empty
; -- suppress junk warning
1153 Make_Attribute_Reference
(Loc
,
1154 Attribute_Name
=> Name_Length
,
1155 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1156 Expressions
=> New_List
(
1157 Make_Integer_Literal
(Loc
, J
)));
1160 Len_Expr
:= Len_Dim
;
1164 Make_Op_Multiply
(Loc
,
1165 Left_Opnd
=> Len_Expr
,
1166 Right_Opnd
=> Len_Dim
);
1170 exit when J
> Number_Dimensions
(Typ
);
1174 -- Temporarily attach the length expression to the tree and analyze
1175 -- and resolve it, so that we can test its value. We assume that the
1176 -- total length fits in type Integer. This expression may involve
1177 -- discriminants, so we treat it as a default/per-object expression.
1179 Set_Parent
(Len_Expr
, Typ
);
1180 Preanalyze_Spec_Expression
(Len_Expr
, Standard_Long_Long_Integer
);
1182 -- Use a modular type if possible. We can do this if we have
1183 -- static bounds, and the length is small enough, and the length
1184 -- is not zero. We exclude the zero length case because the size
1185 -- of things is always at least one, and the zero length object
1186 -- would have an anomalous size.
1188 if Compile_Time_Known_Value
(Len_Expr
) then
1189 Len_Bits
:= Expr_Value
(Len_Expr
) * Csize
;
1191 -- Check for size known to be too large
1194 Uint_2
** (Standard_Integer_Size
- 1) * System_Storage_Unit
1196 if System_Storage_Unit
= 8 then
1198 ("packed array size cannot exceed " &
1199 "Integer''Last bytes", Typ
);
1202 ("packed array size cannot exceed " &
1203 "Integer''Last storage units", Typ
);
1206 -- Reset length to arbitrary not too high value to continue
1208 Len_Expr
:= Make_Integer_Literal
(Loc
, 65535);
1209 Analyze_And_Resolve
(Len_Expr
, Standard_Long_Long_Integer
);
1212 -- We normally consider small enough to mean no larger than the
1213 -- value of System_Max_Binary_Modulus_Power, checking that in the
1214 -- case of values longer than word size, we have long shifts.
1218 (Len_Bits
<= System_Word_Size
1219 or else (Len_Bits
<= System_Max_Binary_Modulus_Power
1220 and then Support_Long_Shifts_On_Target
))
1222 -- We can use the modular type, it has the form:
1224 -- subtype tttPn is btyp
1225 -- range 0 .. 2 ** ((Typ'Length (1)
1226 -- * ... * Typ'Length (n)) * Csize) - 1;
1228 -- The bounds are statically known, and btyp is one of the
1229 -- unsigned types, depending on the length.
1231 if Len_Bits
<= Standard_Short_Short_Integer_Size
then
1232 Btyp
:= RTE
(RE_Short_Short_Unsigned
);
1234 elsif Len_Bits
<= Standard_Short_Integer_Size
then
1235 Btyp
:= RTE
(RE_Short_Unsigned
);
1237 elsif Len_Bits
<= Standard_Integer_Size
then
1238 Btyp
:= RTE
(RE_Unsigned
);
1240 elsif Len_Bits
<= Standard_Long_Integer_Size
then
1241 Btyp
:= RTE
(RE_Long_Unsigned
);
1244 Btyp
:= RTE
(RE_Long_Long_Unsigned
);
1247 Lit
:= Make_Integer_Literal
(Loc
, 2 ** Len_Bits
- 1);
1248 Set_Print_In_Hex
(Lit
);
1251 Make_Subtype_Declaration
(Loc
,
1252 Defining_Identifier
=> PAT
,
1253 Subtype_Indication
=>
1254 Make_Subtype_Indication
(Loc
,
1255 Subtype_Mark
=> New_Occurrence_Of
(Btyp
, Loc
),
1258 Make_Range_Constraint
(Loc
,
1262 Make_Integer_Literal
(Loc
, 0),
1263 High_Bound
=> Lit
))));
1265 if PASize
= Uint_0
then
1271 -- Propagate a given alignment to the modular type. This can
1272 -- cause it to be under-aligned, but that's OK.
1274 if Present
(Alignment_Clause
(Typ
)) then
1275 Set_Alignment
(PAT
, Alignment
(Typ
));
1282 -- Could not use a modular type, for all other cases, we build
1283 -- a packed array subtype:
1286 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1288 -- Bits is the length of the array in bits
1295 Make_Op_Multiply
(Loc
,
1297 Make_Integer_Literal
(Loc
, Csize
),
1298 Right_Opnd
=> Len_Expr
),
1301 Make_Integer_Literal
(Loc
, 7));
1303 Set_Paren_Count
(Bits_U1
, 1);
1306 Make_Op_Subtract
(Loc
,
1308 Make_Op_Divide
(Loc
,
1309 Left_Opnd
=> Bits_U1
,
1310 Right_Opnd
=> Make_Integer_Literal
(Loc
, 8)),
1311 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
1314 Make_Subtype_Declaration
(Loc
,
1315 Defining_Identifier
=> PAT
,
1316 Subtype_Indication
=>
1317 Make_Subtype_Indication
(Loc
,
1318 Subtype_Mark
=> New_Occurrence_Of
(PB_Type
, Loc
),
1320 Make_Index_Or_Discriminant_Constraint
(Loc
,
1321 Constraints
=> New_List
(
1324 Make_Integer_Literal
(Loc
, 0),
1326 Convert_To
(Standard_Integer
, PAT_High
))))));
1330 -- Currently the code in this unit requires that packed arrays
1331 -- represented by non-modular arrays of bytes be on a byte
1332 -- boundary for bit sizes handled by System.Pack_nn units.
1333 -- That's because these units assume the array being accessed
1334 -- starts on a byte boundary.
1336 if Get_Id
(UI_To_Int
(Csize
)) /= RE_Null
then
1337 Set_Must_Be_On_Byte_Boundary
(Typ
);
1340 end Create_Packed_Array_Type
;
1342 -----------------------------------
1343 -- Expand_Bit_Packed_Element_Set --
1344 -----------------------------------
1346 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
) is
1347 Loc
: constant Source_Ptr
:= Sloc
(N
);
1348 Lhs
: constant Node_Id
:= Name
(N
);
1350 Ass_OK
: constant Boolean := Assignment_OK
(Lhs
);
1351 -- Used to preserve assignment OK status when assignment is rewritten
1353 Rhs
: Node_Id
:= Expression
(N
);
1354 -- Initially Rhs is the right hand side value, it will be replaced
1355 -- later by an appropriate unchecked conversion for the assignment.
1365 -- The expression for the shift value that is required
1367 Shift_Used
: Boolean := False;
1368 -- Set True if Shift has been used in the generated code at least once,
1369 -- so that it must be duplicated if used again.
1374 Rhs_Val_Known
: Boolean;
1376 -- If the value of the right hand side as an integer constant is
1377 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1378 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1379 -- the Rhs_Val is undefined.
1381 Require_Byte_Swapping
: Boolean := False;
1382 -- True if byte swapping required, for the Reverse_Storage_Order case
1383 -- when the packed array is a free-standing object. (If it is part
1384 -- of a composite type, and therefore potentially not aligned on a byte
1385 -- boundary, the swapping is done by the back-end).
1387 function Get_Shift
return Node_Id
;
1388 -- Function used to get the value of Shift, making sure that it
1389 -- gets duplicated if the function is called more than once.
1395 function Get_Shift
return Node_Id
is
1397 -- If we used the shift value already, then duplicate it. We
1398 -- set a temporary parent in case actions have to be inserted.
1401 Set_Parent
(Shift
, N
);
1402 return Duplicate_Subexpr_No_Checks
(Shift
);
1404 -- If first time, use Shift unchanged, and set flag for first use
1412 -- Start of processing for Expand_Bit_Packed_Element_Set
1415 pragma Assert
(Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
))));
1417 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1418 Convert_To_Actual_Subtype
(Obj
);
1419 Atyp
:= Etype
(Obj
);
1420 PAT
:= Packed_Array_Type
(Atyp
);
1421 Ctyp
:= Component_Type
(Atyp
);
1422 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1424 -- We remove side effects, in case the rhs modifies the lhs, because we
1425 -- are about to transform the rhs into an expression that first READS
1426 -- the lhs, so we can do the necessary shifting and masking. Example:
1427 -- "X(2) := F(...);" where F modifies X(3). Otherwise, the side effect
1430 Remove_Side_Effects
(Rhs
);
1432 -- We convert the right hand side to the proper subtype to ensure
1433 -- that an appropriate range check is made (since the normal range
1434 -- check from assignment will be lost in the transformations). This
1435 -- conversion is analyzed immediately so that subsequent processing
1436 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1438 -- If the right-hand side is a string literal, create a temporary for
1439 -- it, constant-folding is not ready to wrap the bit representation
1440 -- of a string literal.
1442 if Nkind
(Rhs
) = N_String_Literal
then
1447 Make_Object_Declaration
(Loc
,
1448 Defining_Identifier
=> Make_Temporary
(Loc
, 'T', Rhs
),
1449 Object_Definition
=> New_Occurrence_Of
(Ctyp
, Loc
),
1450 Expression
=> New_Copy_Tree
(Rhs
));
1452 Insert_Actions
(N
, New_List
(Decl
));
1453 Rhs
:= New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
);
1457 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1458 Set_Parent
(Rhs
, N
);
1460 -- If we are building the initialization procedure for a packed array,
1461 -- and Initialize_Scalars is enabled, each component assignment is an
1462 -- out-of-range value by design. Compile this value without checks,
1463 -- because a call to the array init_proc must not raise an exception.
1466 and then Initialize_Scalars
1468 Analyze_And_Resolve
(Rhs
, Ctyp
, Suppress
=> All_Checks
);
1470 Analyze_And_Resolve
(Rhs
, Ctyp
);
1473 -- For the AAMP target, indexing of certain packed array is passed
1474 -- through to the back end without expansion, because the expansion
1475 -- results in very inefficient code on that target. This allows the
1476 -- GNAAMP back end to generate specialized macros that support more
1477 -- efficient indexing of packed arrays with components having sizes
1478 -- that are small powers of two.
1481 and then (Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4)
1486 -- Case of component size 1,2,4 or any component size for the modular
1487 -- case. These are the cases for which we can inline the code.
1489 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1490 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1492 Setup_Inline_Packed_Array_Reference
(Lhs
, Atyp
, Obj
, Cmask
, Shift
);
1494 -- The statement to be generated is:
1496 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, Shift)))
1498 -- or in the case of a freestanding Reverse_Storage_Order object,
1500 -- Obj := Swap (atyp!((Swap (Obj) and Mask1)
1501 -- or (shift_left (rhs, Shift))))
1503 -- where Mask1 is obtained by shifting Cmask left Shift bits
1504 -- and then complementing the result.
1506 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1508 -- the "or ..." is omitted if rhs is constant and all 0 bits
1510 -- rhs is converted to the appropriate type
1512 -- The result is converted back to the array type, since
1513 -- otherwise we lose knowledge of the packed nature.
1515 -- Determine if right side is all 0 bits or all 1 bits
1517 if Compile_Time_Known_Value
(Rhs
) then
1518 Rhs_Val
:= Expr_Rep_Value
(Rhs
);
1519 Rhs_Val_Known
:= True;
1521 -- The following test catches the case of an unchecked conversion of
1522 -- an integer literal. This results from optimizing aggregates of
1525 elsif Nkind
(Rhs
) = N_Unchecked_Type_Conversion
1526 and then Compile_Time_Known_Value
(Expression
(Rhs
))
1528 Rhs_Val
:= Expr_Rep_Value
(Expression
(Rhs
));
1529 Rhs_Val_Known
:= True;
1533 Rhs_Val_Known
:= False;
1536 -- Some special checks for the case where the right hand value is
1537 -- known at compile time. Basically we have to take care of the
1538 -- implicit conversion to the subtype of the component object.
1540 if Rhs_Val_Known
then
1542 -- If we have a biased component type then we must manually do the
1543 -- biasing, since we are taking responsibility in this case for
1544 -- constructing the exact bit pattern to be used.
1546 if Has_Biased_Representation
(Ctyp
) then
1547 Rhs_Val
:= Rhs_Val
- Expr_Rep_Value
(Type_Low_Bound
(Ctyp
));
1550 -- For a negative value, we manually convert the two's complement
1551 -- value to a corresponding unsigned value, so that the proper
1552 -- field width is maintained. If we did not do this, we would
1553 -- get too many leading sign bits later on.
1556 Rhs_Val
:= 2 ** UI_From_Int
(Csiz
) + Rhs_Val
;
1560 -- Now create copies removing side effects. Note that in some complex
1561 -- cases, this may cause the fact that we have already set a packed
1562 -- array type on Obj to get lost. So we save the type of Obj, and
1563 -- make sure it is reset properly.
1566 T
: constant Entity_Id
:= Etype
(Obj
);
1568 New_Lhs
:= Duplicate_Subexpr
(Obj
, True);
1569 New_Rhs
:= Duplicate_Subexpr_No_Checks
(Obj
);
1571 Set_Etype
(New_Lhs
, T
);
1572 Set_Etype
(New_Rhs
, T
);
1574 if Reverse_Storage_Order
(Base_Type
(Atyp
))
1575 and then Esize
(T
) > 8
1576 and then not In_Reverse_Storage_Order_Object
(Obj
)
1578 Require_Byte_Swapping
:= True;
1579 New_Rhs
:= Byte_Swap
(New_Rhs
,
1580 Left_Justify
=> Bytes_Big_Endian
,
1581 Right_Justify
=> not Bytes_Big_Endian
);
1585 -- First we deal with the "and"
1587 if not Rhs_Val_Known
or else Rhs_Val
/= Cmask
then
1593 if Compile_Time_Known_Value
(Shift
) then
1595 Make_Integer_Literal
(Loc
,
1596 Modulus
(Etype
(Obj
)) - 1 -
1597 (Cmask
* (2 ** Expr_Value
(Get_Shift
))));
1598 Set_Print_In_Hex
(Mask1
);
1601 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1602 Set_Print_In_Hex
(Lit
);
1605 Right_Opnd
=> Make_Shift_Left
(Lit
, Get_Shift
));
1610 Left_Opnd
=> New_Rhs
,
1611 Right_Opnd
=> Mask1
);
1615 -- Then deal with the "or"
1617 if not Rhs_Val_Known
or else Rhs_Val
/= 0 then
1621 procedure Fixup_Rhs
;
1622 -- Adjust Rhs by bias if biased representation for components
1623 -- or remove extraneous high order sign bits if signed.
1625 procedure Fixup_Rhs
is
1626 Etyp
: constant Entity_Id
:= Etype
(Rhs
);
1629 -- For biased case, do the required biasing by simply
1630 -- converting to the biased subtype (the conversion
1631 -- will generate the required bias).
1633 if Has_Biased_Representation
(Ctyp
) then
1634 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1636 -- For a signed integer type that is not biased, generate
1637 -- a conversion to unsigned to strip high order sign bits.
1639 elsif Is_Signed_Integer_Type
(Ctyp
) then
1640 Rhs
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Csiz
)), Rhs
);
1643 -- Set Etype, since it can be referenced before the node is
1644 -- completely analyzed.
1646 Set_Etype
(Rhs
, Etyp
);
1648 -- We now need to do an unchecked conversion of the
1649 -- result to the target type, but it is important that
1650 -- this conversion be a right justified conversion and
1651 -- not a left justified conversion.
1653 Rhs
:= RJ_Unchecked_Convert_To
(Etype
(Obj
), Rhs
);
1658 and then Compile_Time_Known_Value
(Get_Shift
)
1661 Make_Integer_Literal
(Loc
,
1662 Rhs_Val
* (2 ** Expr_Value
(Get_Shift
)));
1663 Set_Print_In_Hex
(Or_Rhs
);
1666 -- We have to convert the right hand side to Etype (Obj).
1667 -- A special case arises if what we have now is a Val
1668 -- attribute reference whose expression type is Etype (Obj).
1669 -- This happens for assignments of fields from the same
1670 -- array. In this case we get the required right hand side
1671 -- by simply removing the inner attribute reference.
1673 if Nkind
(Rhs
) = N_Attribute_Reference
1674 and then Attribute_Name
(Rhs
) = Name_Val
1675 and then Etype
(First
(Expressions
(Rhs
))) = Etype
(Obj
)
1677 Rhs
:= Relocate_Node
(First
(Expressions
(Rhs
)));
1680 -- If the value of the right hand side is a known integer
1681 -- value, then just replace it by an untyped constant,
1682 -- which will be properly retyped when we analyze and
1683 -- resolve the expression.
1685 elsif Rhs_Val_Known
then
1687 -- Note that Rhs_Val has already been normalized to
1688 -- be an unsigned value with the proper number of bits.
1690 Rhs
:= Make_Integer_Literal
(Loc
, Rhs_Val
);
1692 -- Otherwise we need an unchecked conversion
1698 Or_Rhs
:= Make_Shift_Left
(Rhs
, Get_Shift
);
1701 if Nkind
(New_Rhs
) = N_Op_And
then
1702 Set_Paren_Count
(New_Rhs
, 1);
1703 Set_Etype
(New_Rhs
, Etype
(Left_Opnd
(New_Rhs
)));
1706 -- If New_Rhs has been byte swapped, need to convert Or_Rhs
1707 -- to the return type of the byte swapping function now.
1709 if Require_Byte_Swapping
then
1710 Or_Rhs
:= Unchecked_Convert_To
(Etype
(New_Rhs
), Or_Rhs
);
1715 Left_Opnd
=> New_Rhs
,
1716 Right_Opnd
=> Or_Rhs
);
1720 if Require_Byte_Swapping
then
1721 Set_Etype
(New_Rhs
, Etype
(Obj
));
1723 Unchecked_Convert_To
(Etype
(Obj
),
1725 Left_Justify
=> not Bytes_Big_Endian
,
1726 Right_Justify
=> Bytes_Big_Endian
));
1729 -- Now do the rewrite
1732 Make_Assignment_Statement
(Loc
,
1735 Unchecked_Convert_To
(Etype
(New_Lhs
), New_Rhs
)));
1736 Set_Assignment_OK
(Name
(N
), Ass_OK
);
1738 -- All other component sizes for non-modular case
1743 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1745 -- where Subscr is the computed linear subscript
1748 Bits_nn
: constant Entity_Id
:= RTE
(Bits_Id
(Csiz
));
1754 if No
(Bits_nn
) then
1756 -- Error, most likely High_Integrity_Mode restriction
1761 -- Acquire proper Set entity. We use the aligned or unaligned
1762 -- case as appropriate.
1764 if Known_Aligned_Enough
(Obj
, Csiz
) then
1765 Set_nn
:= RTE
(Set_Id
(Csiz
));
1767 Set_nn
:= RTE
(SetU_Id
(Csiz
));
1770 -- Now generate the set reference
1772 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1773 Convert_To_Actual_Subtype
(Obj
);
1774 Atyp
:= Etype
(Obj
);
1775 Compute_Linear_Subscript
(Atyp
, Lhs
, Subscr
);
1777 -- Below we must make the assumption that Obj is
1778 -- at least byte aligned, since otherwise its address
1779 -- cannot be taken. The assumption holds since the
1780 -- only arrays that can be misaligned are small packed
1781 -- arrays which are implemented as a modular type, and
1782 -- that is not the case here.
1785 Make_Procedure_Call_Statement
(Loc
,
1786 Name
=> New_Occurrence_Of
(Set_nn
, Loc
),
1787 Parameter_Associations
=> New_List
(
1788 Make_Attribute_Reference
(Loc
,
1790 Attribute_Name
=> Name_Address
),
1792 Unchecked_Convert_To
(Bits_nn
,
1793 Convert_To
(Ctyp
, Rhs
)))));
1798 Analyze
(N
, Suppress
=> All_Checks
);
1799 end Expand_Bit_Packed_Element_Set
;
1801 -------------------------------------
1802 -- Expand_Packed_Address_Reference --
1803 -------------------------------------
1805 procedure Expand_Packed_Address_Reference
(N
: Node_Id
) is
1806 Loc
: constant Source_Ptr
:= Sloc
(N
);
1811 -- We build an expression that has the form
1813 -- outer_object'Address
1814 -- + (linear-subscript * component_size for each array reference
1815 -- + field'Bit_Position for each record field
1817 -- + ...) / Storage_Unit;
1819 Get_Base_And_Bit_Offset
(Prefix
(N
), Base
, Offset
);
1822 Unchecked_Convert_To
(RTE
(RE_Address
),
1825 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1826 Make_Attribute_Reference
(Loc
,
1828 Attribute_Name
=> Name_Address
)),
1831 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1832 Make_Op_Divide
(Loc
,
1833 Left_Opnd
=> Offset
,
1835 Make_Integer_Literal
(Loc
, System_Storage_Unit
))))));
1837 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1838 end Expand_Packed_Address_Reference
;
1840 ---------------------------------
1841 -- Expand_Packed_Bit_Reference --
1842 ---------------------------------
1844 procedure Expand_Packed_Bit_Reference
(N
: Node_Id
) is
1845 Loc
: constant Source_Ptr
:= Sloc
(N
);
1850 -- We build an expression that has the form
1852 -- (linear-subscript * component_size for each array reference
1853 -- + field'Bit_Position for each record field
1855 -- + ...) mod Storage_Unit;
1857 Get_Base_And_Bit_Offset
(Prefix
(N
), Base
, Offset
);
1860 Unchecked_Convert_To
(Universal_Integer
,
1862 Left_Opnd
=> Offset
,
1863 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
1865 Analyze_And_Resolve
(N
, Universal_Integer
);
1866 end Expand_Packed_Bit_Reference
;
1868 ------------------------------------
1869 -- Expand_Packed_Boolean_Operator --
1870 ------------------------------------
1872 -- This routine expands "a op b" for the packed cases
1874 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
) is
1875 Loc
: constant Source_Ptr
:= Sloc
(N
);
1876 Typ
: constant Entity_Id
:= Etype
(N
);
1877 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1878 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1885 Convert_To_Actual_Subtype
(L
);
1886 Convert_To_Actual_Subtype
(R
);
1888 Ensure_Defined
(Etype
(L
), N
);
1889 Ensure_Defined
(Etype
(R
), N
);
1891 Apply_Length_Check
(R
, Etype
(L
));
1896 -- Deal with silly case of XOR where the subcomponent has a range
1897 -- True .. True where an exception must be raised.
1899 if Nkind
(N
) = N_Op_Xor
then
1900 Silly_Boolean_Array_Xor_Test
(N
, Rtyp
);
1903 -- Now that that silliness is taken care of, get packed array type
1905 Convert_To_PAT_Type
(L
);
1906 Convert_To_PAT_Type
(R
);
1910 -- For the modular case, we expand a op b into
1912 -- rtyp!(pat!(a) op pat!(b))
1914 -- where rtyp is the Etype of the left operand. Note that we do not
1915 -- convert to the base type, since this would be unconstrained, and
1916 -- hence not have a corresponding packed array type set.
1918 -- Note that both operands must be modular for this code to be used
1920 if Is_Modular_Integer_Type
(PAT
)
1922 Is_Modular_Integer_Type
(Etype
(R
))
1928 if Nkind
(N
) = N_Op_And
then
1929 P
:= Make_Op_And
(Loc
, L
, R
);
1931 elsif Nkind
(N
) = N_Op_Or
then
1932 P
:= Make_Op_Or
(Loc
, L
, R
);
1934 else -- Nkind (N) = N_Op_Xor
1935 P
:= Make_Op_Xor
(Loc
, L
, R
);
1938 Rewrite
(N
, Unchecked_Convert_To
(Ltyp
, P
));
1941 -- For the array case, we insert the actions
1945 -- System.Bit_Ops.Bit_And/Or/Xor
1947 -- Ltype'Length * Ltype'Component_Size;
1949 -- Rtype'Length * Rtype'Component_Size
1952 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1953 -- the second argument and fourth arguments are the lengths of the
1954 -- operands in bits. Then we replace the expression by a reference
1957 -- Note that if we are mixing a modular and array operand, everything
1958 -- works fine, since we ensure that the modular representation has the
1959 -- same physical layout as the array representation (that's what the
1960 -- left justified modular stuff in the big-endian case is about).
1964 Result_Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
1968 if Nkind
(N
) = N_Op_And
then
1971 elsif Nkind
(N
) = N_Op_Or
then
1974 else -- Nkind (N) = N_Op_Xor
1978 Insert_Actions
(N
, New_List
(
1980 Make_Object_Declaration
(Loc
,
1981 Defining_Identifier
=> Result_Ent
,
1982 Object_Definition
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1984 Make_Procedure_Call_Statement
(Loc
,
1985 Name
=> New_Occurrence_Of
(RTE
(E_Id
), Loc
),
1986 Parameter_Associations
=> New_List
(
1988 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1990 Attribute_Name
=> Name_Address
),
1992 Make_Op_Multiply
(Loc
,
1994 Make_Attribute_Reference
(Loc
,
1997 (Etype
(First_Index
(Ltyp
)), Loc
),
1998 Attribute_Name
=> Name_Range_Length
),
2001 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
))),
2003 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2005 Attribute_Name
=> Name_Address
),
2007 Make_Op_Multiply
(Loc
,
2009 Make_Attribute_Reference
(Loc
,
2012 (Etype
(First_Index
(Rtyp
)), Loc
),
2013 Attribute_Name
=> Name_Range_Length
),
2016 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2018 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2019 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
2020 Attribute_Name
=> Name_Address
)))));
2023 New_Occurrence_Of
(Result_Ent
, Loc
));
2027 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2028 end Expand_Packed_Boolean_Operator
;
2030 -------------------------------------
2031 -- Expand_Packed_Element_Reference --
2032 -------------------------------------
2034 procedure Expand_Packed_Element_Reference
(N
: Node_Id
) is
2035 Loc
: constant Source_Ptr
:= Sloc
(N
);
2046 Byte_Swapped
: Boolean;
2047 -- Set true if bytes were swapped for the purpose of extracting the
2048 -- element, in which case we must swap back if the component type is
2049 -- a composite type with reverse scalar storage order.
2052 -- If the node is an actual in a call, the prefix has not been fully
2053 -- expanded, to account for the additional expansion for in-out actuals
2054 -- (see expand_actuals for details). If the prefix itself is a packed
2055 -- reference as well, we have to recurse to complete the transformation
2058 if Nkind
(Prefix
(N
)) = N_Indexed_Component
2059 and then not Analyzed
(Prefix
(N
))
2060 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Prefix
(N
))))
2062 Expand_Packed_Element_Reference
(Prefix
(N
));
2065 -- If not bit packed, we have the enumeration case, which is easily
2066 -- dealt with (just adjust the subscripts of the indexed component)
2068 -- Note: this leaves the result as an indexed component, which is
2069 -- still a variable, so can be used in the assignment case, as is
2070 -- required in the enumeration case.
2072 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
2073 Setup_Enumeration_Packed_Array_Reference
(N
);
2077 -- Remaining processing is for the bit-packed case
2079 Obj
:= Relocate_Node
(Prefix
(N
));
2080 Convert_To_Actual_Subtype
(Obj
);
2081 Atyp
:= Etype
(Obj
);
2082 PAT
:= Packed_Array_Type
(Atyp
);
2083 Ctyp
:= Component_Type
(Atyp
);
2084 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
2086 -- For the AAMP target, indexing of certain packed array is passed
2087 -- through to the back end without expansion, because the expansion
2088 -- results in very inefficient code on that target. This allows the
2089 -- GNAAMP back end to generate specialized macros that support more
2090 -- efficient indexing of packed arrays with components having sizes
2091 -- that are small powers of two.
2094 and then (Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4)
2099 -- Case of component size 1,2,4 or any component size for the modular
2100 -- case. These are the cases for which we can inline the code.
2102 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
2103 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
2105 Setup_Inline_Packed_Array_Reference
(N
, Atyp
, Obj
, Cmask
, Shift
);
2106 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
2107 Set_Print_In_Hex
(Lit
);
2109 -- Byte swapping required for the Reverse_Storage_Order case, but
2110 -- only for a free-standing object (see note on Require_Byte_Swapping
2111 -- in Expand_Bit_Packed_Element_Set).
2113 if Reverse_Storage_Order
(Atyp
)
2114 and then Esize
(Atyp
) > 8
2115 and then not In_Reverse_Storage_Order_Object
(Obj
)
2117 Obj
:= Byte_Swap
(Obj
,
2118 Left_Justify
=> Bytes_Big_Endian
,
2119 Right_Justify
=> not Bytes_Big_Endian
);
2120 Byte_Swapped
:= True;
2123 Byte_Swapped
:= False;
2126 -- We generate a shift right to position the field, followed by a
2127 -- masking operation to extract the bit field, and we finally do an
2128 -- unchecked conversion to convert the result to the required target.
2130 -- Note that the unchecked conversion automatically deals with the
2131 -- bias if we are dealing with a biased representation. What will
2132 -- happen is that we temporarily generate the biased representation,
2133 -- but almost immediately that will be converted to the original
2134 -- unbiased component type, and the bias will disappear.
2138 Left_Opnd
=> Make_Shift_Right
(Obj
, Shift
),
2141 -- Swap back if necessary
2143 Set_Etype
(Arg
, Ctyp
);
2146 and then (Is_Record_Type
(Ctyp
) or else Is_Array_Type
(Ctyp
))
2147 and then Reverse_Storage_Order
(Ctyp
)
2152 Left_Justify
=> not Bytes_Big_Endian
,
2153 Right_Justify
=> False);
2156 -- We needed to analyze this before we do the unchecked convert
2157 -- below, but we need it temporarily attached to the tree for
2158 -- this analysis (hence the temporary Set_Parent call).
2160 Set_Parent
(Arg
, Parent
(N
));
2161 Analyze_And_Resolve
(Arg
);
2163 Rewrite
(N
, RJ_Unchecked_Convert_To
(Ctyp
, Arg
));
2165 -- All other component sizes for non-modular case
2170 -- Component_Type!(Get_nn (Arr'address, Subscr))
2172 -- where Subscr is the computed linear subscript
2179 -- Acquire proper Get entity. We use the aligned or unaligned
2180 -- case as appropriate.
2182 if Known_Aligned_Enough
(Obj
, Csiz
) then
2183 Get_nn
:= RTE
(Get_Id
(Csiz
));
2185 Get_nn
:= RTE
(GetU_Id
(Csiz
));
2188 -- Now generate the get reference
2190 Compute_Linear_Subscript
(Atyp
, N
, Subscr
);
2192 -- Below we make the assumption that Obj is at least byte
2193 -- aligned, since otherwise its address cannot be taken.
2194 -- The assumption holds since the only arrays that can be
2195 -- misaligned are small packed arrays which are implemented
2196 -- as a modular type, and that is not the case here.
2199 Unchecked_Convert_To
(Ctyp
,
2200 Make_Function_Call
(Loc
,
2201 Name
=> New_Occurrence_Of
(Get_nn
, Loc
),
2202 Parameter_Associations
=> New_List
(
2203 Make_Attribute_Reference
(Loc
,
2205 Attribute_Name
=> Name_Address
),
2210 Analyze_And_Resolve
(N
, Ctyp
, Suppress
=> All_Checks
);
2212 end Expand_Packed_Element_Reference
;
2214 ----------------------
2215 -- Expand_Packed_Eq --
2216 ----------------------
2218 -- Handles expansion of "=" on packed array types
2220 procedure Expand_Packed_Eq
(N
: Node_Id
) is
2221 Loc
: constant Source_Ptr
:= Sloc
(N
);
2222 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2223 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2233 Convert_To_Actual_Subtype
(L
);
2234 Convert_To_Actual_Subtype
(R
);
2235 Ltyp
:= Underlying_Type
(Etype
(L
));
2236 Rtyp
:= Underlying_Type
(Etype
(R
));
2238 Convert_To_PAT_Type
(L
);
2239 Convert_To_PAT_Type
(R
);
2243 Make_Op_Multiply
(Loc
,
2245 Make_Attribute_Reference
(Loc
,
2246 Prefix
=> New_Occurrence_Of
(Ltyp
, Loc
),
2247 Attribute_Name
=> Name_Length
),
2249 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
)));
2252 Make_Op_Multiply
(Loc
,
2254 Make_Attribute_Reference
(Loc
,
2255 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
),
2256 Attribute_Name
=> Name_Length
),
2258 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
)));
2260 -- For the modular case, we transform the comparison to:
2262 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2264 -- where PAT is the packed array type. This works fine, since in the
2265 -- modular case we guarantee that the unused bits are always zeroes.
2266 -- We do have to compare the lengths because we could be comparing
2267 -- two different subtypes of the same base type.
2269 if Is_Modular_Integer_Type
(PAT
) then
2274 Left_Opnd
=> LLexpr
,
2275 Right_Opnd
=> RLexpr
),
2282 -- For the non-modular case, we call a runtime routine
2284 -- System.Bit_Ops.Bit_Eq
2285 -- (L'Address, L_Length, R'Address, R_Length)
2287 -- where PAT is the packed array type, and the lengths are the lengths
2288 -- in bits of the original packed arrays. This routine takes care of
2289 -- not comparing the unused bits in the last byte.
2293 Make_Function_Call
(Loc
,
2294 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Eq
), Loc
),
2295 Parameter_Associations
=> New_List
(
2296 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2298 Attribute_Name
=> Name_Address
),
2302 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2304 Attribute_Name
=> Name_Address
),
2309 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
2310 end Expand_Packed_Eq
;
2312 -----------------------
2313 -- Expand_Packed_Not --
2314 -----------------------
2316 -- Handles expansion of "not" on packed array types
2318 procedure Expand_Packed_Not
(N
: Node_Id
) is
2319 Loc
: constant Source_Ptr
:= Sloc
(N
);
2320 Typ
: constant Entity_Id
:= Etype
(N
);
2321 Opnd
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2328 Convert_To_Actual_Subtype
(Opnd
);
2329 Rtyp
:= Etype
(Opnd
);
2331 -- Deal with silly False..False and True..True subtype case
2333 Silly_Boolean_Array_Not_Test
(N
, Rtyp
);
2335 -- Now that the silliness is taken care of, get packed array type
2337 Convert_To_PAT_Type
(Opnd
);
2338 PAT
:= Etype
(Opnd
);
2340 -- For the case where the packed array type is a modular type, "not A"
2341 -- expands simply into:
2343 -- Rtyp!(PAT!(A) xor Mask)
2345 -- where PAT is the packed array type, Mask is a mask of all 1 bits of
2346 -- length equal to the size of this packed type, and Rtyp is the actual
2347 -- actual subtype of the operand.
2349 Lit
:= Make_Integer_Literal
(Loc
, 2 ** RM_Size
(PAT
) - 1);
2350 Set_Print_In_Hex
(Lit
);
2352 if not Is_Array_Type
(PAT
) then
2354 Unchecked_Convert_To
(Rtyp
,
2357 Right_Opnd
=> Lit
)));
2359 -- For the array case, we insert the actions
2363 -- System.Bit_Ops.Bit_Not
2365 -- Typ'Length * Typ'Component_Size,
2368 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second argument
2369 -- is the length of the operand in bits. We then replace the expression
2370 -- with a reference to Result.
2374 Result_Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2377 Insert_Actions
(N
, New_List
(
2378 Make_Object_Declaration
(Loc
,
2379 Defining_Identifier
=> Result_Ent
,
2380 Object_Definition
=> New_Occurrence_Of
(Rtyp
, Loc
)),
2382 Make_Procedure_Call_Statement
(Loc
,
2383 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Not
), Loc
),
2384 Parameter_Associations
=> New_List
(
2385 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2387 Attribute_Name
=> Name_Address
),
2389 Make_Op_Multiply
(Loc
,
2391 Make_Attribute_Reference
(Loc
,
2394 (Etype
(First_Index
(Rtyp
)), Loc
),
2395 Attribute_Name
=> Name_Range_Length
),
2398 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2400 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2401 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
2402 Attribute_Name
=> Name_Address
)))));
2404 Rewrite
(N
, New_Occurrence_Of
(Result_Ent
, Loc
));
2408 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2409 end Expand_Packed_Not
;
2411 -----------------------------
2412 -- Get_Base_And_Bit_Offset --
2413 -----------------------------
2415 procedure Get_Base_And_Bit_Offset
2418 Offset
: out Node_Id
)
2429 -- We build up an expression serially that has the form
2431 -- linear-subscript * component_size for each array reference
2432 -- + field'Bit_Position for each record field
2438 if Nkind
(Base
) = N_Indexed_Component
then
2439 Convert_To_Actual_Subtype
(Prefix
(Base
));
2440 Atyp
:= Etype
(Prefix
(Base
));
2441 Compute_Linear_Subscript
(Atyp
, Base
, Subscr
);
2444 Make_Op_Multiply
(Loc
,
2445 Left_Opnd
=> Subscr
,
2447 Make_Attribute_Reference
(Loc
,
2448 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2449 Attribute_Name
=> Name_Component_Size
));
2451 elsif Nkind
(Base
) = N_Selected_Component
then
2453 Make_Attribute_Reference
(Loc
,
2454 Prefix
=> Selector_Name
(Base
),
2455 Attribute_Name
=> Name_Bit_Position
);
2467 Left_Opnd
=> Offset
,
2468 Right_Opnd
=> Term
);
2471 Base
:= Prefix
(Base
);
2473 end Get_Base_And_Bit_Offset
;
2475 -------------------------------------
2476 -- Involves_Packed_Array_Reference --
2477 -------------------------------------
2479 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean is
2481 if Nkind
(N
) = N_Indexed_Component
2482 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
2486 elsif Nkind
(N
) = N_Selected_Component
then
2487 return Involves_Packed_Array_Reference
(Prefix
(N
));
2492 end Involves_Packed_Array_Reference
;
2494 --------------------------
2495 -- Known_Aligned_Enough --
2496 --------------------------
2498 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean is
2499 Typ
: constant Entity_Id
:= Etype
(Obj
);
2501 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean;
2502 -- If the component is in a record that contains previous packed
2503 -- components, consider it unaligned because the back-end might
2504 -- choose to pack the rest of the record. Lead to less efficient code,
2505 -- but safer vis-a-vis of back-end choices.
2507 --------------------------------
2508 -- In_Partially_Packed_Record --
2509 --------------------------------
2511 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean is
2512 Rec_Type
: constant Entity_Id
:= Scope
(Comp
);
2513 Prev_Comp
: Entity_Id
;
2516 Prev_Comp
:= First_Entity
(Rec_Type
);
2517 while Present
(Prev_Comp
) loop
2518 if Is_Packed
(Etype
(Prev_Comp
)) then
2521 elsif Prev_Comp
= Comp
then
2525 Next_Entity
(Prev_Comp
);
2529 end In_Partially_Packed_Record
;
2531 -- Start of processing for Known_Aligned_Enough
2534 -- Odd bit sizes don't need alignment anyway
2536 if Csiz
mod 2 = 1 then
2539 -- If we have a specified alignment, see if it is sufficient, if not
2540 -- then we can't possibly be aligned enough in any case.
2542 elsif Known_Alignment
(Etype
(Obj
)) then
2543 -- Alignment required is 4 if size is a multiple of 4, and
2544 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2546 if Alignment
(Etype
(Obj
)) < 4 - (Csiz
mod 4) then
2551 -- OK, alignment should be sufficient, if object is aligned
2553 -- If object is strictly aligned, then it is definitely aligned
2555 if Strict_Alignment
(Typ
) then
2558 -- Case of subscripted array reference
2560 elsif Nkind
(Obj
) = N_Indexed_Component
then
2562 -- If we have a pointer to an array, then this is definitely
2563 -- aligned, because pointers always point to aligned versions.
2565 if Is_Access_Type
(Etype
(Prefix
(Obj
))) then
2568 -- Otherwise, go look at the prefix
2571 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2574 -- Case of record field
2576 elsif Nkind
(Obj
) = N_Selected_Component
then
2578 -- What is significant here is whether the record type is packed
2580 if Is_Record_Type
(Etype
(Prefix
(Obj
)))
2581 and then Is_Packed
(Etype
(Prefix
(Obj
)))
2585 -- Or the component has a component clause which might cause
2586 -- the component to become unaligned (we can't tell if the
2587 -- backend is doing alignment computations).
2589 elsif Present
(Component_Clause
(Entity
(Selector_Name
(Obj
)))) then
2592 elsif In_Partially_Packed_Record
(Entity
(Selector_Name
(Obj
))) then
2595 -- In all other cases, go look at prefix
2598 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2601 elsif Nkind
(Obj
) = N_Type_Conversion
then
2602 return Known_Aligned_Enough
(Expression
(Obj
), Csiz
);
2604 -- For a formal parameter, it is safer to assume that it is not
2605 -- aligned, because the formal may be unconstrained while the actual
2606 -- is constrained. In this situation, a small constrained packed
2607 -- array, represented in modular form, may be unaligned.
2609 elsif Is_Entity_Name
(Obj
) then
2610 return not Is_Formal
(Entity
(Obj
));
2613 -- If none of the above, must be aligned
2616 end Known_Aligned_Enough
;
2618 ---------------------
2619 -- Make_Shift_Left --
2620 ---------------------
2622 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2626 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2630 Make_Op_Shift_Left
(Sloc
(N
),
2633 Set_Shift_Count_OK
(Nod
, True);
2636 end Make_Shift_Left
;
2638 ----------------------
2639 -- Make_Shift_Right --
2640 ----------------------
2642 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2646 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2650 Make_Op_Shift_Right
(Sloc
(N
),
2653 Set_Shift_Count_OK
(Nod
, True);
2656 end Make_Shift_Right
;
2658 -----------------------------
2659 -- RJ_Unchecked_Convert_To --
2660 -----------------------------
2662 function RJ_Unchecked_Convert_To
2664 Expr
: Node_Id
) return Node_Id
2666 Source_Typ
: constant Entity_Id
:= Etype
(Expr
);
2667 Target_Typ
: constant Entity_Id
:= Typ
;
2669 Src
: Node_Id
:= Expr
;
2675 Source_Siz
:= UI_To_Int
(RM_Size
(Source_Typ
));
2676 Target_Siz
:= UI_To_Int
(RM_Size
(Target_Typ
));
2678 -- For a little-endian target type stored byte-swapped on a
2679 -- big-endian machine, do not mask to Target_Siz bits.
2682 and then (Is_Record_Type
(Target_Typ
)
2684 Is_Array_Type
(Target_Typ
))
2685 and then Reverse_Storage_Order
(Target_Typ
)
2687 Source_Siz
:= Target_Siz
;
2690 -- First step, if the source type is not a discrete type, then we first
2691 -- convert to a modular type of the source length, since otherwise, on
2692 -- a big-endian machine, we get left-justification. We do it for little-
2693 -- endian machines as well, because there might be junk bits that are
2694 -- not cleared if the type is not numeric.
2696 if Source_Siz
/= Target_Siz
2697 and then not Is_Discrete_Type
(Source_Typ
)
2699 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Source_Siz
)), Src
);
2702 -- In the big endian case, if the lengths of the two types differ, then
2703 -- we must worry about possible left justification in the conversion,
2704 -- and avoiding that is what this is all about.
2706 if Bytes_Big_Endian
and then Source_Siz
/= Target_Siz
then
2708 -- Next step. If the target is not a discrete type, then we first
2709 -- convert to a modular type of the target length, since otherwise,
2710 -- on a big-endian machine, we get left-justification.
2712 if not Is_Discrete_Type
(Target_Typ
) then
2713 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Target_Siz
)), Src
);
2717 -- And now we can do the final conversion to the target type
2719 return Unchecked_Convert_To
(Target_Typ
, Src
);
2720 end RJ_Unchecked_Convert_To
;
2722 ----------------------------------------------
2723 -- Setup_Enumeration_Packed_Array_Reference --
2724 ----------------------------------------------
2726 -- All we have to do here is to find the subscripts that correspond to the
2727 -- index positions that have non-standard enumeration types and insert a
2728 -- Pos attribute to get the proper subscript value.
2730 -- Finally the prefix must be uncheck-converted to the corresponding packed
2733 -- Note that the component type is unchanged, so we do not need to fiddle
2734 -- with the types (Gigi always automatically takes the packed array type if
2735 -- it is set, as it will be in this case).
2737 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
) is
2738 Pfx
: constant Node_Id
:= Prefix
(N
);
2739 Typ
: constant Entity_Id
:= Etype
(N
);
2740 Exprs
: constant List_Id
:= Expressions
(N
);
2744 -- If the array is unconstrained, then we replace the array reference
2745 -- with its actual subtype. This actual subtype will have a packed array
2746 -- type with appropriate bounds.
2748 if not Is_Constrained
(Packed_Array_Type
(Etype
(Pfx
))) then
2749 Convert_To_Actual_Subtype
(Pfx
);
2752 Expr
:= First
(Exprs
);
2753 while Present
(Expr
) loop
2755 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2756 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
2759 if Is_Enumeration_Type
(Expr_Typ
)
2760 and then Has_Non_Standard_Rep
(Expr_Typ
)
2763 Make_Attribute_Reference
(Loc
,
2764 Prefix
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
2765 Attribute_Name
=> Name_Pos
,
2766 Expressions
=> New_List
(Relocate_Node
(Expr
))));
2767 Analyze_And_Resolve
(Expr
, Standard_Natural
);
2775 Make_Indexed_Component
(Sloc
(N
),
2777 Unchecked_Convert_To
(Packed_Array_Type
(Etype
(Pfx
)), Pfx
),
2778 Expressions
=> Exprs
));
2780 Analyze_And_Resolve
(N
, Typ
);
2781 end Setup_Enumeration_Packed_Array_Reference
;
2783 -----------------------------------------
2784 -- Setup_Inline_Packed_Array_Reference --
2785 -----------------------------------------
2787 procedure Setup_Inline_Packed_Array_Reference
2790 Obj
: in out Node_Id
;
2792 Shift
: out Node_Id
)
2794 Loc
: constant Source_Ptr
:= Sloc
(N
);
2801 Csiz
:= Component_Size
(Atyp
);
2803 Convert_To_PAT_Type
(Obj
);
2806 Cmask
:= 2 ** Csiz
- 1;
2808 if Is_Array_Type
(PAT
) then
2809 Otyp
:= Component_Type
(PAT
);
2810 Osiz
:= Component_Size
(PAT
);
2815 -- In the case where the PAT is a modular type, we want the actual
2816 -- size in bits of the modular value we use. This is neither the
2817 -- Object_Size nor the Value_Size, either of which may have been
2818 -- reset to strange values, but rather the minimum size. Note that
2819 -- since this is a modular type with full range, the issue of
2820 -- biased representation does not arise.
2822 Osiz
:= UI_From_Int
(Minimum_Size
(Otyp
));
2825 Compute_Linear_Subscript
(Atyp
, N
, Shift
);
2827 -- If the component size is not 1, then the subscript must be multiplied
2828 -- by the component size to get the shift count.
2832 Make_Op_Multiply
(Loc
,
2833 Left_Opnd
=> Make_Integer_Literal
(Loc
, Csiz
),
2834 Right_Opnd
=> Shift
);
2837 -- If we have the array case, then this shift count must be broken down
2838 -- into a byte subscript, and a shift within the byte.
2840 if Is_Array_Type
(PAT
) then
2843 New_Shift
: Node_Id
;
2846 -- We must analyze shift, since we will duplicate it
2848 Set_Parent
(Shift
, N
);
2850 (Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2852 -- The shift count within the word is
2857 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2858 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
));
2860 -- The subscript to be used on the PAT array is
2864 Make_Indexed_Component
(Loc
,
2866 Expressions
=> New_List
(
2867 Make_Op_Divide
(Loc
,
2868 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2869 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
))));
2874 -- For the modular integer case, the object to be manipulated is the
2875 -- entire array, so Obj is unchanged. Note that we will reset its type
2876 -- to PAT before returning to the caller.
2882 -- The one remaining step is to modify the shift count for the
2883 -- big-endian case. Consider the following example in a byte:
2885 -- xxxxxxxx bits of byte
2886 -- vvvvvvvv bits of value
2887 -- 33221100 little-endian numbering
2888 -- 00112233 big-endian numbering
2890 -- Here we have the case of 2-bit fields
2892 -- For the little-endian case, we already have the proper shift count
2893 -- set, e.g. for element 2, the shift count is 2*2 = 4.
2895 -- For the big endian case, we have to adjust the shift count, computing
2896 -- it as (N - F) - Shift, where N is the number of bits in an element of
2897 -- the array used to implement the packed array, F is the number of bits
2898 -- in a source array element, and Shift is the count so far computed.
2900 -- We also have to adjust if the storage order is reversed
2902 if Bytes_Big_Endian
xor Reverse_Storage_Order
(Base_Type
(Atyp
)) then
2904 Make_Op_Subtract
(Loc
,
2905 Left_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
- Csiz
),
2906 Right_Opnd
=> Shift
);
2909 Set_Parent
(Shift
, N
);
2910 Set_Parent
(Obj
, N
);
2911 Analyze_And_Resolve
(Obj
, Otyp
, Suppress
=> All_Checks
);
2912 Analyze_And_Resolve
(Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2914 -- Make sure final type of object is the appropriate packed type
2916 Set_Etype
(Obj
, Otyp
);
2918 end Setup_Inline_Packed_Array_Reference
;