1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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_Ch3
; use Sem_Ch3
;
40 with Sem_Ch8
; use Sem_Ch8
;
41 with Sem_Ch13
; use Sem_Ch13
;
42 with Sem_Eval
; use Sem_Eval
;
43 with Sem_Res
; use Sem_Res
;
44 with Sem_Util
; use Sem_Util
;
45 with Sinfo
; use Sinfo
;
46 with Snames
; use Snames
;
47 with Stand
; use Stand
;
48 with Targparm
; use Targparm
;
49 with Tbuild
; use Tbuild
;
50 with Ttypes
; use Ttypes
;
51 with Uintp
; use Uintp
;
53 package body Exp_Pakd
is
55 ---------------------------
56 -- Endian Considerations --
57 ---------------------------
59 -- As described in the specification, bit numbering in a packed array
60 -- is consistent with bit numbering in a record representation clause,
61 -- and hence dependent on the endianness of the machine:
63 -- For little-endian machines, element zero is at the right hand end
64 -- (low order end) of a bit field.
66 -- For big-endian machines, element zero is at the left hand end
67 -- (high order end) of a bit field.
69 -- The shifts that are used to right justify a field therefore differ
70 -- in the two cases. For the little-endian case, we can simply use the
71 -- bit number (i.e. the element number * element size) as the count for
72 -- a right shift. For the big-endian case, we have to subtract the shift
73 -- count from an appropriate constant to use in the right shift. We use
74 -- rotates instead of shifts (which is necessary in the store case to
75 -- preserve other fields), and we expect that the backend will be able
76 -- to change the right rotate into a left rotate, avoiding the subtract,
77 -- if the architecture provides such an instruction.
79 ----------------------------------------------
80 -- Entity Tables for Packed Access Routines --
81 ----------------------------------------------
83 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
84 -- library routines. This table is used to obtain the entity for the
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
160 -- from a packed array. The N'th entry is used to obtain elements from
161 -- a packed array whose component size is N. RE_Null is used as a null
162 -- entry, for 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
231 -- not be fully aligned. This only affects the even sizes, since for the
232 -- odd 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
300 -- of a packed array. The N'th entry is used to assign elements for
301 -- a packed array whose component size is N. RE_Null is used as a null
302 -- entry, for 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
371 -- not be fully aligned. This only affects the even sizes, since for the
372 -- odd 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
448 -- N referencing an array object of this type, build an expression of
449 -- type Standard.Integer representing the zero-based linear subscript
450 -- value. 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 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean;
458 -- There are two versions of the Set routines, the ones used when the
459 -- object is known to be sufficiently well aligned given the number of
460 -- bits, and the ones used when the object is not known to be aligned.
461 -- This routine is used to determine which set to use. Obj is a reference
462 -- to the object, and Csiz is the component size of the packed array.
463 -- True is returned if the alignment of object is known to be sufficient,
464 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
467 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
468 -- Build a left shift node, checking for the case of a shift count of zero
470 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
471 -- Build a right shift node, checking for the case of a shift count of zero
473 function RJ_Unchecked_Convert_To
475 Expr
: Node_Id
) return Node_Id
;
476 -- The packed array code does unchecked conversions which in some cases
477 -- may involve non-discrete types with differing sizes. The semantics of
478 -- such conversions is potentially endian dependent, and the effect we
479 -- want here for such a conversion is to do the conversion in size as
480 -- though numeric items are involved, and we extend or truncate on the
481 -- left side. This happens naturally in the little-endian case, but in
482 -- the big endian case we can get left justification, when what we want
483 -- is right justification. This routine does the unchecked conversion in
484 -- a stepwise manner to ensure that it gives the expected result. Hence
485 -- the name (RJ = Right justified). The parameters Typ and Expr are as
486 -- for the case of a normal Unchecked_Convert_To call.
488 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
);
489 -- This routine is called in the Get and Set case for arrays that are
490 -- packed but not bit-packed, meaning that they have at least one
491 -- subscript that is of an enumeration type with a non-standard
492 -- representation. This routine modifies the given node to properly
493 -- reference the corresponding packed array type.
495 procedure Setup_Inline_Packed_Array_Reference
498 Obj
: in out Node_Id
;
500 Shift
: out Node_Id
);
501 -- This procedure performs common processing on the N_Indexed_Component
502 -- parameter given as N, whose prefix is a reference to a packed array.
503 -- This is used for the get and set when the component size is 1,2,4
504 -- or for other component sizes when the packed array type is a modular
505 -- type (i.e. the cases that are handled with inline code).
509 -- N is the N_Indexed_Component node for the packed array reference
511 -- Atyp is the constrained array type (the actual subtype has been
512 -- computed if necessary to obtain the constraints, but this is still
513 -- the original array type, not the Packed_Array_Type value).
515 -- Obj is the object which is to be indexed. It is always of type Atyp.
519 -- Obj is the object containing the desired bit field. It is of type
520 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
521 -- entire value, for the small static case, or the proper selected byte
522 -- from the array in the large or dynamic case. This node is analyzed
523 -- and resolved on return.
525 -- Shift is a node representing the shift count to be used in the
526 -- rotate right instruction that positions the field for access.
527 -- This node is analyzed and resolved on return.
529 -- Cmask is a mask corresponding to the width of the component field.
530 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
532 -- Note: in some cases the call to this routine may generate actions
533 -- (for handling multi-use references and the generation of the packed
534 -- array type on the fly). Such actions are inserted into the tree
535 -- directly using Insert_Action.
537 ------------------------------
538 -- Compute_Linear_Subscript --
539 ------------------------------
541 procedure Compute_Linear_Subscript
544 Subscr
: out Node_Id
)
546 Loc
: constant Source_Ptr
:= Sloc
(N
);
555 -- Loop through dimensions
557 Indx
:= First_Index
(Atyp
);
558 Oldsub
:= First
(Expressions
(N
));
560 while Present
(Indx
) loop
561 Styp
:= Etype
(Indx
);
562 Newsub
:= Relocate_Node
(Oldsub
);
564 -- Get expression for the subscript value. First, if Do_Range_Check
565 -- is set on a subscript, then we must do a range check against the
566 -- original bounds (not the bounds of the packed array type). We do
567 -- this by introducing a subtype conversion.
569 if Do_Range_Check
(Newsub
)
570 and then Etype
(Newsub
) /= Styp
572 Newsub
:= Convert_To
(Styp
, Newsub
);
575 -- Now evolve the expression for the subscript. First convert
576 -- the subscript to be zero based and of an integer type.
578 -- Case of integer type, where we just subtract to get lower bound
580 if Is_Integer_Type
(Styp
) then
582 -- If length of integer type is smaller than standard integer,
583 -- then we convert to integer first, then do the subtract
585 -- Integer (subscript) - Integer (Styp'First)
587 if Esize
(Styp
) < Esize
(Standard_Integer
) then
589 Make_Op_Subtract
(Loc
,
590 Left_Opnd
=> Convert_To
(Standard_Integer
, Newsub
),
592 Convert_To
(Standard_Integer
,
593 Make_Attribute_Reference
(Loc
,
594 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
595 Attribute_Name
=> Name_First
)));
597 -- For larger integer types, subtract first, then convert to
598 -- integer, this deals with strange long long integer bounds.
600 -- Integer (subscript - Styp'First)
604 Convert_To
(Standard_Integer
,
605 Make_Op_Subtract
(Loc
,
608 Make_Attribute_Reference
(Loc
,
609 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
610 Attribute_Name
=> Name_First
)));
613 -- For the enumeration case, we have to use 'Pos to get the value
614 -- to work with before subtracting the lower bound.
616 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
618 -- This is not quite right for bizarre cases where the size of the
619 -- enumeration type is > Integer'Size bits due to rep clause ???
622 pragma Assert
(Is_Enumeration_Type
(Styp
));
625 Make_Op_Subtract
(Loc
,
626 Left_Opnd
=> Convert_To
(Standard_Integer
,
627 Make_Attribute_Reference
(Loc
,
628 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
629 Attribute_Name
=> Name_Pos
,
630 Expressions
=> New_List
(Newsub
))),
633 Convert_To
(Standard_Integer
,
634 Make_Attribute_Reference
(Loc
,
635 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
636 Attribute_Name
=> Name_Pos
,
637 Expressions
=> New_List
(
638 Make_Attribute_Reference
(Loc
,
639 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
640 Attribute_Name
=> Name_First
)))));
643 Set_Paren_Count
(Newsub
, 1);
645 -- For the first subscript, we just copy that subscript value
650 -- Otherwise, we must multiply what we already have by the current
651 -- stride and then add in the new value to the evolving subscript.
657 Make_Op_Multiply
(Loc
,
660 Make_Attribute_Reference
(Loc
,
661 Attribute_Name
=> Name_Range_Length
,
662 Prefix
=> New_Occurrence_Of
(Styp
, Loc
))),
663 Right_Opnd
=> Newsub
);
666 -- Move to next subscript
671 end Compute_Linear_Subscript
;
673 -------------------------
674 -- Convert_To_PAT_Type --
675 -------------------------
677 -- The PAT is always obtained from the actual subtype
679 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
) is
683 Convert_To_Actual_Subtype
(Aexp
);
684 Act_ST
:= Underlying_Type
(Etype
(Aexp
));
685 Create_Packed_Array_Type
(Act_ST
);
687 -- Just replace the etype with the packed array type. This works because
688 -- the expression will not be further analyzed, and Gigi considers the
689 -- two types equivalent in any case.
691 -- This is not strictly the case ??? If the reference is an actual in
692 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
693 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
694 -- array reference, reanalysis can produce spurious type errors when the
695 -- PAT type is replaced again with the original type of the array. Same
696 -- for the case of a dereference. The following is correct and minimal,
697 -- but the handling of more complex packed expressions in actuals is
698 -- confused. Probably the problem only remains for actuals in calls.
700 Set_Etype
(Aexp
, Packed_Array_Type
(Act_ST
));
702 if Is_Entity_Name
(Aexp
)
704 (Nkind
(Aexp
) = N_Indexed_Component
705 and then Is_Entity_Name
(Prefix
(Aexp
)))
706 or else Nkind
(Aexp
) = N_Explicit_Dereference
710 end Convert_To_PAT_Type
;
712 ------------------------------
713 -- Create_Packed_Array_Type --
714 ------------------------------
716 procedure Create_Packed_Array_Type
(Typ
: Entity_Id
) is
717 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
718 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
719 Csize
: constant Uint
:= Component_Size
(Typ
);
734 procedure Install_PAT
;
735 -- This procedure is called with Decl set to the declaration for the
736 -- packed array type. It creates the type and installs it as required.
738 procedure Set_PB_Type
;
739 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
740 -- requirements (see documentation in the spec of this package).
746 procedure Install_PAT
is
747 Pushed_Scope
: Boolean := False;
750 -- We do not want to put the declaration we have created in the tree
751 -- since it is often hard, and sometimes impossible to find a proper
752 -- place for it (the impossible case arises for a packed array type
753 -- with bounds depending on the discriminant, a declaration cannot
754 -- be put inside the record, and the reference to the discriminant
755 -- cannot be outside the record).
757 -- The solution is to analyze the declaration while temporarily
758 -- attached to the tree at an appropriate point, and then we install
759 -- the resulting type as an Itype in the packed array type field of
760 -- the original type, so that no explicit declaration is required.
762 -- Note: the packed type is created in the scope of its parent
763 -- type. There are at least some cases where the current scope
764 -- is deeper, and so when this is the case, we temporarily reset
765 -- the scope for the definition. This is clearly safe, since the
766 -- first use of the packed array type will be the implicit
767 -- reference from the corresponding unpacked type when it is
770 if Is_Itype
(Typ
) then
771 Set_Parent
(Decl
, Associated_Node_For_Itype
(Typ
));
773 Set_Parent
(Decl
, Declaration_Node
(Typ
));
776 if Scope
(Typ
) /= Current_Scope
then
777 Push_Scope
(Scope
(Typ
));
778 Pushed_Scope
:= True;
781 Set_Is_Itype
(PAT
, True);
782 Set_Packed_Array_Type
(Typ
, PAT
);
783 Analyze
(Decl
, Suppress
=> All_Checks
);
789 -- Set Esize and RM_Size to the actual size of the packed object
790 -- Do not reset RM_Size if already set, as happens in the case of
793 if Unknown_Esize
(PAT
) then
794 Set_Esize
(PAT
, PASize
);
797 if Unknown_RM_Size
(PAT
) then
798 Set_RM_Size
(PAT
, PASize
);
801 Adjust_Esize_Alignment
(PAT
);
803 -- Set remaining fields of packed array type
805 Init_Alignment
(PAT
);
806 Set_Parent
(PAT
, Empty
);
807 Set_Associated_Node_For_Itype
(PAT
, Typ
);
808 Set_Is_Packed_Array_Type
(PAT
, True);
809 Set_Original_Array_Type
(PAT
, Typ
);
811 -- We definitely do not want to delay freezing for packed array
812 -- types. This is of particular importance for the itypes that
813 -- are generated for record components depending on discriminants
814 -- where there is no place to put the freeze node.
816 Set_Has_Delayed_Freeze
(PAT
, False);
817 Set_Has_Delayed_Freeze
(Etype
(PAT
), False);
819 -- If we did allocate a freeze node, then clear out the reference
820 -- since it is obsolete (should we delete the freeze node???)
822 Set_Freeze_Node
(PAT
, Empty
);
823 Set_Freeze_Node
(Etype
(PAT
), Empty
);
830 procedure Set_PB_Type
is
832 -- If the user has specified an explicit alignment for the
833 -- type or component, take it into account.
835 if Csize
<= 2 or else Csize
= 4 or else Csize
mod 2 /= 0
836 or else Alignment
(Typ
) = 1
837 or else Component_Alignment
(Typ
) = Calign_Storage_Unit
839 PB_Type
:= RTE
(RE_Packed_Bytes1
);
841 elsif Csize
mod 4 /= 0
842 or else Alignment
(Typ
) = 2
844 PB_Type
:= RTE
(RE_Packed_Bytes2
);
847 PB_Type
:= RTE
(RE_Packed_Bytes4
);
851 -- Start of processing for Create_Packed_Array_Type
854 -- If we already have a packed array type, nothing to do
856 if Present
(Packed_Array_Type
(Typ
)) then
860 -- If our immediate ancestor subtype is constrained, and it already
861 -- has a packed array type, then just share the same type, since the
862 -- bounds must be the same. If the ancestor is not an array type but
863 -- a private type, as can happen with multiple instantiations, create
864 -- a new packed type, to avoid privacy issues.
866 if Ekind
(Typ
) = E_Array_Subtype
then
867 Ancest
:= Ancestor_Subtype
(Typ
);
870 and then Is_Array_Type
(Ancest
)
871 and then Is_Constrained
(Ancest
)
872 and then Present
(Packed_Array_Type
(Ancest
))
874 Set_Packed_Array_Type
(Typ
, Packed_Array_Type
(Ancest
));
879 -- We preset the result type size from the size of the original array
880 -- type, since this size clearly belongs to the packed array type. The
881 -- size of the conceptual unpacked type is always set to unknown.
883 PASize
:= RM_Size
(Typ
);
885 -- Case of an array where at least one index is of an enumeration
886 -- type with a non-standard representation, but the component size
887 -- is not appropriate for bit packing. This is the case where we
888 -- have Is_Packed set (we would never be in this unit otherwise),
889 -- but Is_Bit_Packed_Array is false.
891 -- Note that if the component size is appropriate for bit packing,
892 -- then the circuit for the computation of the subscript properly
893 -- deals with the non-standard enumeration type case by taking the
896 if not Is_Bit_Packed_Array
(Typ
) then
898 -- Here we build a declaration:
900 -- type tttP is array (index1, index2, ...) of component_type
902 -- where index1, index2, are the index types. These are the same
903 -- as the index types of the original array, except for the non-
904 -- standard representation enumeration type case, where we have
907 -- For the unconstrained array case, we use
911 -- For the constrained case, we use
913 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
914 -- Enum_Type'Pos (Enum_Type'Last);
917 Make_Defining_Identifier
(Loc
,
918 Chars
=> New_External_Name
(Chars
(Typ
), 'P'));
920 Set_Packed_Array_Type
(Typ
, PAT
);
923 Indexes
: constant List_Id
:= New_List
;
925 Indx_Typ
: Entity_Id
;
930 Indx
:= First_Index
(Typ
);
932 while Present
(Indx
) loop
933 Indx_Typ
:= Etype
(Indx
);
935 Enum_Case
:= Is_Enumeration_Type
(Indx_Typ
)
936 and then Has_Non_Standard_Rep
(Indx_Typ
);
938 -- Unconstrained case
940 if not Is_Constrained
(Typ
) then
942 Indx_Typ
:= Standard_Natural
;
945 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
950 if not Enum_Case
then
951 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
955 Make_Subtype_Indication
(Loc
,
957 New_Occurrence_Of
(Standard_Natural
, Loc
),
959 Make_Range_Constraint
(Loc
,
963 Make_Attribute_Reference
(Loc
,
965 New_Occurrence_Of
(Indx_Typ
, Loc
),
966 Attribute_Name
=> Name_Pos
,
967 Expressions
=> New_List
(
968 Make_Attribute_Reference
(Loc
,
970 New_Occurrence_Of
(Indx_Typ
, Loc
),
971 Attribute_Name
=> Name_First
))),
974 Make_Attribute_Reference
(Loc
,
976 New_Occurrence_Of
(Indx_Typ
, Loc
),
977 Attribute_Name
=> Name_Pos
,
978 Expressions
=> New_List
(
979 Make_Attribute_Reference
(Loc
,
981 New_Occurrence_Of
(Indx_Typ
, Loc
),
982 Attribute_Name
=> Name_Last
)))))));
990 if not Is_Constrained
(Typ
) then
992 Make_Unconstrained_Array_Definition
(Loc
,
993 Subtype_Marks
=> Indexes
,
994 Component_Definition
=>
995 Make_Component_Definition
(Loc
,
996 Aliased_Present
=> False,
997 Subtype_Indication
=>
998 New_Occurrence_Of
(Ctyp
, Loc
)));
1002 Make_Constrained_Array_Definition
(Loc
,
1003 Discrete_Subtype_Definitions
=> Indexes
,
1004 Component_Definition
=>
1005 Make_Component_Definition
(Loc
,
1006 Aliased_Present
=> False,
1007 Subtype_Indication
=>
1008 New_Occurrence_Of
(Ctyp
, Loc
)));
1012 Make_Full_Type_Declaration
(Loc
,
1013 Defining_Identifier
=> PAT
,
1014 Type_Definition
=> Typedef
);
1017 -- Set type as packed array type and install it
1019 Set_Is_Packed_Array_Type
(PAT
);
1023 -- Case of bit-packing required for unconstrained array. We create
1024 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1026 elsif not Is_Constrained
(Typ
) then
1028 Make_Defining_Identifier
(Loc
,
1029 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1031 Set_Packed_Array_Type
(Typ
, PAT
);
1035 Make_Subtype_Declaration
(Loc
,
1036 Defining_Identifier
=> PAT
,
1037 Subtype_Indication
=> New_Occurrence_Of
(PB_Type
, Loc
));
1041 -- Remaining code is for the case of bit-packing for constrained array
1043 -- The name of the packed array subtype is
1047 -- where sss is the component size in bits and ttt is the name of
1048 -- the parent packed type.
1052 Make_Defining_Identifier
(Loc
,
1053 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1055 Set_Packed_Array_Type
(Typ
, PAT
);
1057 -- Build an expression for the length of the array in bits.
1058 -- This is the product of the length of each of the dimensions
1064 Len_Expr
:= Empty
; -- suppress junk warning
1068 Make_Attribute_Reference
(Loc
,
1069 Attribute_Name
=> Name_Length
,
1070 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1071 Expressions
=> New_List
(
1072 Make_Integer_Literal
(Loc
, J
)));
1075 Len_Expr
:= Len_Dim
;
1079 Make_Op_Multiply
(Loc
,
1080 Left_Opnd
=> Len_Expr
,
1081 Right_Opnd
=> Len_Dim
);
1085 exit when J
> Number_Dimensions
(Typ
);
1089 -- Temporarily attach the length expression to the tree and analyze
1090 -- and resolve it, so that we can test its value. We assume that the
1091 -- total length fits in type Integer. This expression may involve
1092 -- discriminants, so we treat it as a default/per-object expression.
1094 Set_Parent
(Len_Expr
, Typ
);
1095 Preanalyze_Spec_Expression
(Len_Expr
, Standard_Long_Long_Integer
);
1097 -- Use a modular type if possible. We can do this if we have
1098 -- static bounds, and the length is small enough, and the length
1099 -- is not zero. We exclude the zero length case because the size
1100 -- of things is always at least one, and the zero length object
1101 -- would have an anomalous size.
1103 if Compile_Time_Known_Value
(Len_Expr
) then
1104 Len_Bits
:= Expr_Value
(Len_Expr
) * Csize
;
1106 -- Check for size known to be too large
1109 Uint_2
** (Standard_Integer_Size
- 1) * System_Storage_Unit
1111 if System_Storage_Unit
= 8 then
1113 ("packed array size cannot exceed " &
1114 "Integer''Last bytes", Typ
);
1117 ("packed array size cannot exceed " &
1118 "Integer''Last storage units", Typ
);
1121 -- Reset length to arbitrary not too high value to continue
1123 Len_Expr
:= Make_Integer_Literal
(Loc
, 65535);
1124 Analyze_And_Resolve
(Len_Expr
, Standard_Long_Long_Integer
);
1127 -- We normally consider small enough to mean no larger than the
1128 -- value of System_Max_Binary_Modulus_Power, checking that in the
1129 -- case of values longer than word size, we have long shifts.
1133 (Len_Bits
<= System_Word_Size
1134 or else (Len_Bits
<= System_Max_Binary_Modulus_Power
1135 and then Support_Long_Shifts_On_Target
))
1137 -- Also test for alignment given. If an alignment is given which
1138 -- is smaller than the natural modular alignment, force the array
1139 -- of bytes representation to accommodate the alignment.
1142 (No
(Alignment_Clause
(Typ
))
1144 Alignment
(Typ
) >= ((Len_Bits
+ System_Storage_Unit
)
1145 / System_Storage_Unit
))
1147 -- We can use the modular type, it has the form:
1149 -- subtype tttPn is btyp
1150 -- range 0 .. 2 ** ((Typ'Length (1)
1151 -- * ... * Typ'Length (n)) * Csize) - 1;
1153 -- The bounds are statically known, and btyp is one of the
1154 -- unsigned types, depending on the length.
1156 if Len_Bits
<= Standard_Short_Short_Integer_Size
then
1157 Btyp
:= RTE
(RE_Short_Short_Unsigned
);
1159 elsif Len_Bits
<= Standard_Short_Integer_Size
then
1160 Btyp
:= RTE
(RE_Short_Unsigned
);
1162 elsif Len_Bits
<= Standard_Integer_Size
then
1163 Btyp
:= RTE
(RE_Unsigned
);
1165 elsif Len_Bits
<= Standard_Long_Integer_Size
then
1166 Btyp
:= RTE
(RE_Long_Unsigned
);
1169 Btyp
:= RTE
(RE_Long_Long_Unsigned
);
1172 Lit
:= Make_Integer_Literal
(Loc
, 2 ** Len_Bits
- 1);
1173 Set_Print_In_Hex
(Lit
);
1176 Make_Subtype_Declaration
(Loc
,
1177 Defining_Identifier
=> PAT
,
1178 Subtype_Indication
=>
1179 Make_Subtype_Indication
(Loc
,
1180 Subtype_Mark
=> New_Occurrence_Of
(Btyp
, Loc
),
1183 Make_Range_Constraint
(Loc
,
1187 Make_Integer_Literal
(Loc
, 0),
1188 High_Bound
=> Lit
))));
1190 if PASize
= Uint_0
then
1199 -- Could not use a modular type, for all other cases, we build
1200 -- a packed array subtype:
1203 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1205 -- Bits is the length of the array in bits
1212 Make_Op_Multiply
(Loc
,
1214 Make_Integer_Literal
(Loc
, Csize
),
1215 Right_Opnd
=> Len_Expr
),
1218 Make_Integer_Literal
(Loc
, 7));
1220 Set_Paren_Count
(Bits_U1
, 1);
1223 Make_Op_Subtract
(Loc
,
1225 Make_Op_Divide
(Loc
,
1226 Left_Opnd
=> Bits_U1
,
1227 Right_Opnd
=> Make_Integer_Literal
(Loc
, 8)),
1228 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
1231 Make_Subtype_Declaration
(Loc
,
1232 Defining_Identifier
=> PAT
,
1233 Subtype_Indication
=>
1234 Make_Subtype_Indication
(Loc
,
1235 Subtype_Mark
=> New_Occurrence_Of
(PB_Type
, Loc
),
1237 Make_Index_Or_Discriminant_Constraint
(Loc
,
1238 Constraints
=> New_List
(
1241 Make_Integer_Literal
(Loc
, 0),
1243 Convert_To
(Standard_Integer
, PAT_High
))))));
1247 -- Currently the code in this unit requires that packed arrays
1248 -- represented by non-modular arrays of bytes be on a byte
1249 -- boundary for bit sizes handled by System.Pack_nn units.
1250 -- That's because these units assume the array being accessed
1251 -- starts on a byte boundary.
1253 if Get_Id
(UI_To_Int
(Csize
)) /= RE_Null
then
1254 Set_Must_Be_On_Byte_Boundary
(Typ
);
1257 end Create_Packed_Array_Type
;
1259 -----------------------------------
1260 -- Expand_Bit_Packed_Element_Set --
1261 -----------------------------------
1263 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
) is
1264 Loc
: constant Source_Ptr
:= Sloc
(N
);
1265 Lhs
: constant Node_Id
:= Name
(N
);
1267 Ass_OK
: constant Boolean := Assignment_OK
(Lhs
);
1268 -- Used to preserve assignment OK status when assignment is rewritten
1270 Rhs
: Node_Id
:= Expression
(N
);
1271 -- Initially Rhs is the right hand side value, it will be replaced
1272 -- later by an appropriate unchecked conversion for the assignment.
1282 -- The expression for the shift value that is required
1284 Shift_Used
: Boolean := False;
1285 -- Set True if Shift has been used in the generated code at least
1286 -- once, so that it must be duplicated if used again
1291 Rhs_Val_Known
: Boolean;
1293 -- If the value of the right hand side as an integer constant is
1294 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1295 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1296 -- the Rhs_Val is undefined.
1298 function Get_Shift
return Node_Id
;
1299 -- Function used to get the value of Shift, making sure that it
1300 -- gets duplicated if the function is called more than once.
1306 function Get_Shift
return Node_Id
is
1308 -- If we used the shift value already, then duplicate it. We
1309 -- set a temporary parent in case actions have to be inserted.
1312 Set_Parent
(Shift
, N
);
1313 return Duplicate_Subexpr_No_Checks
(Shift
);
1315 -- If first time, use Shift unchanged, and set flag for first use
1323 -- Start of processing for Expand_Bit_Packed_Element_Set
1326 pragma Assert
(Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
))));
1328 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1329 Convert_To_Actual_Subtype
(Obj
);
1330 Atyp
:= Etype
(Obj
);
1331 PAT
:= Packed_Array_Type
(Atyp
);
1332 Ctyp
:= Component_Type
(Atyp
);
1333 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1335 -- We convert the right hand side to the proper subtype to ensure
1336 -- that an appropriate range check is made (since the normal range
1337 -- check from assignment will be lost in the transformations). This
1338 -- conversion is analyzed immediately so that subsequent processing
1339 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1341 -- If the right-hand side is a string literal, create a temporary for
1342 -- it, constant-folding is not ready to wrap the bit representation
1343 -- of a string literal.
1345 if Nkind
(Rhs
) = N_String_Literal
then
1350 Make_Object_Declaration
(Loc
,
1351 Defining_Identifier
=>
1352 Make_Defining_Identifier
(Loc
, New_Internal_Name
('T')),
1353 Object_Definition
=> New_Occurrence_Of
(Ctyp
, Loc
),
1354 Expression
=> New_Copy_Tree
(Rhs
));
1356 Insert_Actions
(N
, New_List
(Decl
));
1357 Rhs
:= New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
);
1361 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1362 Set_Parent
(Rhs
, N
);
1364 -- If we are building the initialization procedure for a packed array,
1365 -- and Initialize_Scalars is enabled, each component assignment is an
1366 -- out-of-range value by design. Compile this value without checks,
1367 -- because a call to the array init_proc must not raise an exception.
1370 and then Initialize_Scalars
1372 Analyze_And_Resolve
(Rhs
, Ctyp
, Suppress
=> All_Checks
);
1374 Analyze_And_Resolve
(Rhs
, Ctyp
);
1377 -- Case of component size 1,2,4 or any component size for the modular
1378 -- case. These are the cases for which we can inline the code.
1380 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1381 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1383 Setup_Inline_Packed_Array_Reference
(Lhs
, Atyp
, Obj
, Cmask
, Shift
);
1385 -- The statement to be generated is:
1387 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1389 -- where mask1 is obtained by shifting Cmask left Shift bits
1390 -- and then complementing the result.
1392 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1394 -- the "or ..." is omitted if rhs is constant and all 0 bits
1396 -- rhs is converted to the appropriate type
1398 -- The result is converted back to the array type, since
1399 -- otherwise we lose knowledge of the packed nature.
1401 -- Determine if right side is all 0 bits or all 1 bits
1403 if Compile_Time_Known_Value
(Rhs
) then
1404 Rhs_Val
:= Expr_Rep_Value
(Rhs
);
1405 Rhs_Val_Known
:= True;
1407 -- The following test catches the case of an unchecked conversion
1408 -- of an integer literal. This results from optimizing aggregates
1411 elsif Nkind
(Rhs
) = N_Unchecked_Type_Conversion
1412 and then Compile_Time_Known_Value
(Expression
(Rhs
))
1414 Rhs_Val
:= Expr_Rep_Value
(Expression
(Rhs
));
1415 Rhs_Val_Known
:= True;
1419 Rhs_Val_Known
:= False;
1422 -- Some special checks for the case where the right hand value
1423 -- is known at compile time. Basically we have to take care of
1424 -- the implicit conversion to the subtype of the component object.
1426 if Rhs_Val_Known
then
1428 -- If we have a biased component type then we must manually do
1429 -- the biasing, since we are taking responsibility in this case
1430 -- for constructing the exact bit pattern to be used.
1432 if Has_Biased_Representation
(Ctyp
) then
1433 Rhs_Val
:= Rhs_Val
- Expr_Rep_Value
(Type_Low_Bound
(Ctyp
));
1436 -- For a negative value, we manually convert the twos complement
1437 -- value to a corresponding unsigned value, so that the proper
1438 -- field width is maintained. If we did not do this, we would
1439 -- get too many leading sign bits later on.
1442 Rhs_Val
:= 2 ** UI_From_Int
(Csiz
) + Rhs_Val
;
1446 -- Now create copies removing side effects. Note that in some
1447 -- complex cases, this may cause the fact that we have already
1448 -- set a packed array type on Obj to get lost. So we save the
1449 -- type of Obj, and make sure it is reset properly.
1452 T
: constant Entity_Id
:= Etype
(Obj
);
1454 New_Lhs
:= Duplicate_Subexpr
(Obj
, True);
1455 New_Rhs
:= Duplicate_Subexpr_No_Checks
(Obj
);
1457 Set_Etype
(New_Lhs
, T
);
1458 Set_Etype
(New_Rhs
, T
);
1461 -- First we deal with the "and"
1463 if not Rhs_Val_Known
or else Rhs_Val
/= Cmask
then
1469 if Compile_Time_Known_Value
(Shift
) then
1471 Make_Integer_Literal
(Loc
,
1472 Modulus
(Etype
(Obj
)) - 1 -
1473 (Cmask
* (2 ** Expr_Value
(Get_Shift
))));
1474 Set_Print_In_Hex
(Mask1
);
1477 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1478 Set_Print_In_Hex
(Lit
);
1481 Right_Opnd
=> Make_Shift_Left
(Lit
, Get_Shift
));
1486 Left_Opnd
=> New_Rhs
,
1487 Right_Opnd
=> Mask1
);
1491 -- Then deal with the "or"
1493 if not Rhs_Val_Known
or else Rhs_Val
/= 0 then
1497 procedure Fixup_Rhs
;
1498 -- Adjust Rhs by bias if biased representation for components
1499 -- or remove extraneous high order sign bits if signed.
1501 procedure Fixup_Rhs
is
1502 Etyp
: constant Entity_Id
:= Etype
(Rhs
);
1505 -- For biased case, do the required biasing by simply
1506 -- converting to the biased subtype (the conversion
1507 -- will generate the required bias).
1509 if Has_Biased_Representation
(Ctyp
) then
1510 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1512 -- For a signed integer type that is not biased, generate
1513 -- a conversion to unsigned to strip high order sign bits.
1515 elsif Is_Signed_Integer_Type
(Ctyp
) then
1516 Rhs
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Csiz
)), Rhs
);
1519 -- Set Etype, since it can be referenced before the
1520 -- node is completely analyzed.
1522 Set_Etype
(Rhs
, Etyp
);
1524 -- We now need to do an unchecked conversion of the
1525 -- result to the target type, but it is important that
1526 -- this conversion be a right justified conversion and
1527 -- not a left justified conversion.
1529 Rhs
:= RJ_Unchecked_Convert_To
(Etype
(Obj
), Rhs
);
1535 and then Compile_Time_Known_Value
(Get_Shift
)
1538 Make_Integer_Literal
(Loc
,
1539 Rhs_Val
* (2 ** Expr_Value
(Get_Shift
)));
1540 Set_Print_In_Hex
(Or_Rhs
);
1543 -- We have to convert the right hand side to Etype (Obj).
1544 -- A special case case arises if what we have now is a Val
1545 -- attribute reference whose expression type is Etype (Obj).
1546 -- This happens for assignments of fields from the same
1547 -- array. In this case we get the required right hand side
1548 -- by simply removing the inner attribute reference.
1550 if Nkind
(Rhs
) = N_Attribute_Reference
1551 and then Attribute_Name
(Rhs
) = Name_Val
1552 and then Etype
(First
(Expressions
(Rhs
))) = Etype
(Obj
)
1554 Rhs
:= Relocate_Node
(First
(Expressions
(Rhs
)));
1557 -- If the value of the right hand side is a known integer
1558 -- value, then just replace it by an untyped constant,
1559 -- which will be properly retyped when we analyze and
1560 -- resolve the expression.
1562 elsif Rhs_Val_Known
then
1564 -- Note that Rhs_Val has already been normalized to
1565 -- be an unsigned value with the proper number of bits.
1568 Make_Integer_Literal
(Loc
, Rhs_Val
);
1570 -- Otherwise we need an unchecked conversion
1576 Or_Rhs
:= Make_Shift_Left
(Rhs
, Get_Shift
);
1579 if Nkind
(New_Rhs
) = N_Op_And
then
1580 Set_Paren_Count
(New_Rhs
, 1);
1585 Left_Opnd
=> New_Rhs
,
1586 Right_Opnd
=> Or_Rhs
);
1590 -- Now do the rewrite
1593 Make_Assignment_Statement
(Loc
,
1596 Unchecked_Convert_To
(Etype
(New_Lhs
), New_Rhs
)));
1597 Set_Assignment_OK
(Name
(N
), Ass_OK
);
1599 -- All other component sizes for non-modular case
1604 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1606 -- where Subscr is the computed linear subscript
1609 Bits_nn
: constant Entity_Id
:= RTE
(Bits_Id
(Csiz
));
1615 if No
(Bits_nn
) then
1617 -- Error, most likely High_Integrity_Mode restriction
1622 -- Acquire proper Set entity. We use the aligned or unaligned
1623 -- case as appropriate.
1625 if Known_Aligned_Enough
(Obj
, Csiz
) then
1626 Set_nn
:= RTE
(Set_Id
(Csiz
));
1628 Set_nn
:= RTE
(SetU_Id
(Csiz
));
1631 -- Now generate the set reference
1633 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1634 Convert_To_Actual_Subtype
(Obj
);
1635 Atyp
:= Etype
(Obj
);
1636 Compute_Linear_Subscript
(Atyp
, Lhs
, Subscr
);
1638 -- Below we must make the assumption that Obj is
1639 -- at least byte aligned, since otherwise its address
1640 -- cannot be taken. The assumption holds since the
1641 -- only arrays that can be misaligned are small packed
1642 -- arrays which are implemented as a modular type, and
1643 -- that is not the case here.
1646 Make_Procedure_Call_Statement
(Loc
,
1647 Name
=> New_Occurrence_Of
(Set_nn
, Loc
),
1648 Parameter_Associations
=> New_List
(
1649 Make_Attribute_Reference
(Loc
,
1651 Attribute_Name
=> Name_Address
),
1653 Unchecked_Convert_To
(Bits_nn
,
1654 Convert_To
(Ctyp
, Rhs
)))));
1659 Analyze
(N
, Suppress
=> All_Checks
);
1660 end Expand_Bit_Packed_Element_Set
;
1662 -------------------------------------
1663 -- Expand_Packed_Address_Reference --
1664 -------------------------------------
1666 procedure Expand_Packed_Address_Reference
(N
: Node_Id
) is
1667 Loc
: constant Source_Ptr
:= Sloc
(N
);
1679 -- We build up an expression serially that has the form
1681 -- outer_object'Address
1682 -- + (linear-subscript * component_size for each array reference
1683 -- + field'Bit_Position for each record field
1685 -- + ...) / Storage_Unit;
1687 -- Some additional conversions are required to deal with the addition
1688 -- operation, which is not normally visible to generated code.
1691 Ploc
:= Sloc
(Pref
);
1693 if Nkind
(Pref
) = N_Indexed_Component
then
1694 Convert_To_Actual_Subtype
(Prefix
(Pref
));
1695 Atyp
:= Etype
(Prefix
(Pref
));
1696 Compute_Linear_Subscript
(Atyp
, Pref
, Subscr
);
1699 Make_Op_Multiply
(Ploc
,
1700 Left_Opnd
=> Subscr
,
1702 Make_Attribute_Reference
(Ploc
,
1703 Prefix
=> New_Occurrence_Of
(Atyp
, Ploc
),
1704 Attribute_Name
=> Name_Component_Size
));
1706 elsif Nkind
(Pref
) = N_Selected_Component
then
1708 Make_Attribute_Reference
(Ploc
,
1709 Prefix
=> Selector_Name
(Pref
),
1710 Attribute_Name
=> Name_Bit_Position
);
1716 Term
:= Convert_To
(RTE
(RE_Integer_Address
), Term
);
1725 Right_Opnd
=> Term
);
1728 Pref
:= Prefix
(Pref
);
1732 Unchecked_Convert_To
(RTE
(RE_Address
),
1735 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1736 Make_Attribute_Reference
(Loc
,
1738 Attribute_Name
=> Name_Address
)),
1741 Make_Op_Divide
(Loc
,
1744 Make_Integer_Literal
(Loc
, System_Storage_Unit
)))));
1746 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1747 end Expand_Packed_Address_Reference
;
1749 ------------------------------------
1750 -- Expand_Packed_Boolean_Operator --
1751 ------------------------------------
1753 -- This routine expands "a op b" for the packed cases
1755 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
) is
1756 Loc
: constant Source_Ptr
:= Sloc
(N
);
1757 Typ
: constant Entity_Id
:= Etype
(N
);
1758 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1759 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1766 Convert_To_Actual_Subtype
(L
);
1767 Convert_To_Actual_Subtype
(R
);
1769 Ensure_Defined
(Etype
(L
), N
);
1770 Ensure_Defined
(Etype
(R
), N
);
1772 Apply_Length_Check
(R
, Etype
(L
));
1777 -- Deal with silly case of XOR where the subcomponent has a range
1778 -- True .. True where an exception must be raised.
1780 if Nkind
(N
) = N_Op_Xor
then
1781 Silly_Boolean_Array_Xor_Test
(N
, Rtyp
);
1784 -- Now that that silliness is taken care of, get packed array type
1786 Convert_To_PAT_Type
(L
);
1787 Convert_To_PAT_Type
(R
);
1791 -- For the modular case, we expand a op b into
1793 -- rtyp!(pat!(a) op pat!(b))
1795 -- where rtyp is the Etype of the left operand. Note that we do not
1796 -- convert to the base type, since this would be unconstrained, and
1797 -- hence not have a corresponding packed array type set.
1799 -- Note that both operands must be modular for this code to be used
1801 if Is_Modular_Integer_Type
(PAT
)
1803 Is_Modular_Integer_Type
(Etype
(R
))
1809 if Nkind
(N
) = N_Op_And
then
1810 P
:= Make_Op_And
(Loc
, L
, R
);
1812 elsif Nkind
(N
) = N_Op_Or
then
1813 P
:= Make_Op_Or
(Loc
, L
, R
);
1815 else -- Nkind (N) = N_Op_Xor
1816 P
:= Make_Op_Xor
(Loc
, L
, R
);
1819 Rewrite
(N
, Unchecked_Convert_To
(Ltyp
, P
));
1822 -- For the array case, we insert the actions
1826 -- System.Bitops.Bit_And/Or/Xor
1828 -- Ltype'Length * Ltype'Component_Size;
1830 -- Rtype'Length * Rtype'Component_Size
1833 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1834 -- the second argument and fourth arguments are the lengths of the
1835 -- operands in bits. Then we replace the expression by a reference
1838 -- Note that if we are mixing a modular and array operand, everything
1839 -- works fine, since we ensure that the modular representation has the
1840 -- same physical layout as the array representation (that's what the
1841 -- left justified modular stuff in the big-endian case is about).
1845 Result_Ent
: constant Entity_Id
:=
1846 Make_Defining_Identifier
(Loc
,
1847 Chars
=> New_Internal_Name
('T'));
1852 if Nkind
(N
) = N_Op_And
then
1855 elsif Nkind
(N
) = N_Op_Or
then
1858 else -- Nkind (N) = N_Op_Xor
1862 Insert_Actions
(N
, New_List
(
1864 Make_Object_Declaration
(Loc
,
1865 Defining_Identifier
=> Result_Ent
,
1866 Object_Definition
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1868 Make_Procedure_Call_Statement
(Loc
,
1869 Name
=> New_Occurrence_Of
(RTE
(E_Id
), Loc
),
1870 Parameter_Associations
=> New_List
(
1872 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1874 Attribute_Name
=> Name_Address
),
1876 Make_Op_Multiply
(Loc
,
1878 Make_Attribute_Reference
(Loc
,
1881 (Etype
(First_Index
(Ltyp
)), Loc
),
1882 Attribute_Name
=> Name_Range_Length
),
1885 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
))),
1887 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1889 Attribute_Name
=> Name_Address
),
1891 Make_Op_Multiply
(Loc
,
1893 Make_Attribute_Reference
(Loc
,
1896 (Etype
(First_Index
(Rtyp
)), Loc
),
1897 Attribute_Name
=> Name_Range_Length
),
1900 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
1902 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1903 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
1904 Attribute_Name
=> Name_Address
)))));
1907 New_Occurrence_Of
(Result_Ent
, Loc
));
1911 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
1912 end Expand_Packed_Boolean_Operator
;
1914 -------------------------------------
1915 -- Expand_Packed_Element_Reference --
1916 -------------------------------------
1918 procedure Expand_Packed_Element_Reference
(N
: Node_Id
) is
1919 Loc
: constant Source_Ptr
:= Sloc
(N
);
1931 -- If not bit packed, we have the enumeration case, which is easily
1932 -- dealt with (just adjust the subscripts of the indexed component)
1934 -- Note: this leaves the result as an indexed component, which is
1935 -- still a variable, so can be used in the assignment case, as is
1936 -- required in the enumeration case.
1938 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
1939 Setup_Enumeration_Packed_Array_Reference
(N
);
1943 -- Remaining processing is for the bit-packed case
1945 Obj
:= Relocate_Node
(Prefix
(N
));
1946 Convert_To_Actual_Subtype
(Obj
);
1947 Atyp
:= Etype
(Obj
);
1948 PAT
:= Packed_Array_Type
(Atyp
);
1949 Ctyp
:= Component_Type
(Atyp
);
1950 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1952 -- Case of component size 1,2,4 or any component size for the modular
1953 -- case. These are the cases for which we can inline the code.
1955 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1956 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1958 Setup_Inline_Packed_Array_Reference
(N
, Atyp
, Obj
, Cmask
, Shift
);
1959 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1960 Set_Print_In_Hex
(Lit
);
1962 -- We generate a shift right to position the field, followed by a
1963 -- masking operation to extract the bit field, and we finally do an
1964 -- unchecked conversion to convert the result to the required target.
1966 -- Note that the unchecked conversion automatically deals with the
1967 -- bias if we are dealing with a biased representation. What will
1968 -- happen is that we temporarily generate the biased representation,
1969 -- but almost immediately that will be converted to the original
1970 -- unbiased component type, and the bias will disappear.
1974 Left_Opnd
=> Make_Shift_Right
(Obj
, Shift
),
1977 -- We needed to analyze this before we do the unchecked convert
1978 -- below, but we need it temporarily attached to the tree for
1979 -- this analysis (hence the temporary Set_Parent call).
1981 Set_Parent
(Arg
, Parent
(N
));
1982 Analyze_And_Resolve
(Arg
);
1985 RJ_Unchecked_Convert_To
(Ctyp
, Arg
));
1987 -- All other component sizes for non-modular case
1992 -- Component_Type!(Get_nn (Arr'address, Subscr))
1994 -- where Subscr is the computed linear subscript
2001 -- Acquire proper Get entity. We use the aligned or unaligned
2002 -- case as appropriate.
2004 if Known_Aligned_Enough
(Obj
, Csiz
) then
2005 Get_nn
:= RTE
(Get_Id
(Csiz
));
2007 Get_nn
:= RTE
(GetU_Id
(Csiz
));
2010 -- Now generate the get reference
2012 Compute_Linear_Subscript
(Atyp
, N
, Subscr
);
2014 -- Below we make the assumption that Obj is at least byte
2015 -- aligned, since otherwise its address cannot be taken.
2016 -- The assumption holds since the only arrays that can be
2017 -- misaligned are small packed arrays which are implemented
2018 -- as a modular type, and that is not the case here.
2021 Unchecked_Convert_To
(Ctyp
,
2022 Make_Function_Call
(Loc
,
2023 Name
=> New_Occurrence_Of
(Get_nn
, Loc
),
2024 Parameter_Associations
=> New_List
(
2025 Make_Attribute_Reference
(Loc
,
2027 Attribute_Name
=> Name_Address
),
2032 Analyze_And_Resolve
(N
, Ctyp
, Suppress
=> All_Checks
);
2034 end Expand_Packed_Element_Reference
;
2036 ----------------------
2037 -- Expand_Packed_Eq --
2038 ----------------------
2040 -- Handles expansion of "=" on packed array types
2042 procedure Expand_Packed_Eq
(N
: Node_Id
) is
2043 Loc
: constant Source_Ptr
:= Sloc
(N
);
2044 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2045 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2055 Convert_To_Actual_Subtype
(L
);
2056 Convert_To_Actual_Subtype
(R
);
2057 Ltyp
:= Underlying_Type
(Etype
(L
));
2058 Rtyp
:= Underlying_Type
(Etype
(R
));
2060 Convert_To_PAT_Type
(L
);
2061 Convert_To_PAT_Type
(R
);
2065 Make_Op_Multiply
(Loc
,
2067 Make_Attribute_Reference
(Loc
,
2068 Prefix
=> New_Occurrence_Of
(Ltyp
, Loc
),
2069 Attribute_Name
=> Name_Length
),
2071 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
)));
2074 Make_Op_Multiply
(Loc
,
2076 Make_Attribute_Reference
(Loc
,
2077 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
),
2078 Attribute_Name
=> Name_Length
),
2080 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
)));
2082 -- For the modular case, we transform the comparison to:
2084 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2086 -- where PAT is the packed array type. This works fine, since in the
2087 -- modular case we guarantee that the unused bits are always zeroes.
2088 -- We do have to compare the lengths because we could be comparing
2089 -- two different subtypes of the same base type.
2091 if Is_Modular_Integer_Type
(PAT
) then
2096 Left_Opnd
=> LLexpr
,
2097 Right_Opnd
=> RLexpr
),
2104 -- For the non-modular case, we call a runtime routine
2106 -- System.Bit_Ops.Bit_Eq
2107 -- (L'Address, L_Length, R'Address, R_Length)
2109 -- where PAT is the packed array type, and the lengths are the lengths
2110 -- in bits of the original packed arrays. This routine takes care of
2111 -- not comparing the unused bits in the last byte.
2115 Make_Function_Call
(Loc
,
2116 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Eq
), Loc
),
2117 Parameter_Associations
=> New_List
(
2118 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2120 Attribute_Name
=> Name_Address
),
2124 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2126 Attribute_Name
=> Name_Address
),
2131 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
2132 end Expand_Packed_Eq
;
2134 -----------------------
2135 -- Expand_Packed_Not --
2136 -----------------------
2138 -- Handles expansion of "not" on packed array types
2140 procedure Expand_Packed_Not
(N
: Node_Id
) is
2141 Loc
: constant Source_Ptr
:= Sloc
(N
);
2142 Typ
: constant Entity_Id
:= Etype
(N
);
2143 Opnd
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2150 Convert_To_Actual_Subtype
(Opnd
);
2151 Rtyp
:= Etype
(Opnd
);
2153 -- Deal with silly False..False and True..True subtype case
2155 Silly_Boolean_Array_Not_Test
(N
, Rtyp
);
2157 -- Now that the silliness is taken care of, get packed array type
2159 Convert_To_PAT_Type
(Opnd
);
2160 PAT
:= Etype
(Opnd
);
2162 -- For the case where the packed array type is a modular type,
2163 -- not A expands simply into:
2165 -- rtyp!(PAT!(A) xor mask)
2167 -- where PAT is the packed array type, and mask is a mask of all
2168 -- one bits of length equal to the size of this packed type and
2169 -- rtyp is the actual subtype of the operand
2171 Lit
:= Make_Integer_Literal
(Loc
, 2 ** RM_Size
(PAT
) - 1);
2172 Set_Print_In_Hex
(Lit
);
2174 if not Is_Array_Type
(PAT
) then
2176 Unchecked_Convert_To
(Rtyp
,
2179 Right_Opnd
=> Lit
)));
2181 -- For the array case, we insert the actions
2185 -- System.Bitops.Bit_Not
2187 -- Typ'Length * Typ'Component_Size;
2190 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2191 -- argument is the length of the operand in bits. Then we replace
2192 -- the expression by a reference to Result.
2196 Result_Ent
: constant Entity_Id
:=
2197 Make_Defining_Identifier
(Loc
,
2198 Chars
=> New_Internal_Name
('T'));
2201 Insert_Actions
(N
, New_List
(
2203 Make_Object_Declaration
(Loc
,
2204 Defining_Identifier
=> Result_Ent
,
2205 Object_Definition
=> New_Occurrence_Of
(Rtyp
, Loc
)),
2207 Make_Procedure_Call_Statement
(Loc
,
2208 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Not
), Loc
),
2209 Parameter_Associations
=> New_List
(
2211 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2213 Attribute_Name
=> Name_Address
),
2215 Make_Op_Multiply
(Loc
,
2217 Make_Attribute_Reference
(Loc
,
2220 (Etype
(First_Index
(Rtyp
)), Loc
),
2221 Attribute_Name
=> Name_Range_Length
),
2224 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2226 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2227 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
2228 Attribute_Name
=> Name_Address
)))));
2231 New_Occurrence_Of
(Result_Ent
, Loc
));
2235 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2237 end Expand_Packed_Not
;
2239 -------------------------------------
2240 -- Involves_Packed_Array_Reference --
2241 -------------------------------------
2243 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean is
2245 if Nkind
(N
) = N_Indexed_Component
2246 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
2250 elsif Nkind
(N
) = N_Selected_Component
then
2251 return Involves_Packed_Array_Reference
(Prefix
(N
));
2256 end Involves_Packed_Array_Reference
;
2258 --------------------------
2259 -- Known_Aligned_Enough --
2260 --------------------------
2262 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean is
2263 Typ
: constant Entity_Id
:= Etype
(Obj
);
2265 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean;
2266 -- If the component is in a record that contains previous packed
2267 -- components, consider it unaligned because the back-end might
2268 -- choose to pack the rest of the record. Lead to less efficient code,
2269 -- but safer vis-a-vis of back-end choices.
2271 --------------------------------
2272 -- In_Partially_Packed_Record --
2273 --------------------------------
2275 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean is
2276 Rec_Type
: constant Entity_Id
:= Scope
(Comp
);
2277 Prev_Comp
: Entity_Id
;
2280 Prev_Comp
:= First_Entity
(Rec_Type
);
2281 while Present
(Prev_Comp
) loop
2282 if Is_Packed
(Etype
(Prev_Comp
)) then
2285 elsif Prev_Comp
= Comp
then
2289 Next_Entity
(Prev_Comp
);
2293 end In_Partially_Packed_Record
;
2295 -- Start of processing for Known_Aligned_Enough
2298 -- Odd bit sizes don't need alignment anyway
2300 if Csiz
mod 2 = 1 then
2303 -- If we have a specified alignment, see if it is sufficient, if not
2304 -- then we can't possibly be aligned enough in any case.
2306 elsif Known_Alignment
(Etype
(Obj
)) then
2307 -- Alignment required is 4 if size is a multiple of 4, and
2308 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2310 if Alignment
(Etype
(Obj
)) < 4 - (Csiz
mod 4) then
2315 -- OK, alignment should be sufficient, if object is aligned
2317 -- If object is strictly aligned, then it is definitely aligned
2319 if Strict_Alignment
(Typ
) then
2322 -- Case of subscripted array reference
2324 elsif Nkind
(Obj
) = N_Indexed_Component
then
2326 -- If we have a pointer to an array, then this is definitely
2327 -- aligned, because pointers always point to aligned versions.
2329 if Is_Access_Type
(Etype
(Prefix
(Obj
))) then
2332 -- Otherwise, go look at the prefix
2335 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2338 -- Case of record field
2340 elsif Nkind
(Obj
) = N_Selected_Component
then
2342 -- What is significant here is whether the record type is packed
2344 if Is_Record_Type
(Etype
(Prefix
(Obj
)))
2345 and then Is_Packed
(Etype
(Prefix
(Obj
)))
2349 -- Or the component has a component clause which might cause
2350 -- the component to become unaligned (we can't tell if the
2351 -- backend is doing alignment computations).
2353 elsif Present
(Component_Clause
(Entity
(Selector_Name
(Obj
)))) then
2356 elsif In_Partially_Packed_Record
(Entity
(Selector_Name
(Obj
))) then
2359 -- In all other cases, go look at prefix
2362 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2365 elsif Nkind
(Obj
) = N_Type_Conversion
then
2366 return Known_Aligned_Enough
(Expression
(Obj
), Csiz
);
2368 -- For a formal parameter, it is safer to assume that it is not
2369 -- aligned, because the formal may be unconstrained while the actual
2370 -- is constrained. In this situation, a small constrained packed
2371 -- array, represented in modular form, may be unaligned.
2373 elsif Is_Entity_Name
(Obj
) then
2374 return not Is_Formal
(Entity
(Obj
));
2377 -- If none of the above, must be aligned
2380 end Known_Aligned_Enough
;
2382 ---------------------
2383 -- Make_Shift_Left --
2384 ---------------------
2386 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2390 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2394 Make_Op_Shift_Left
(Sloc
(N
),
2397 Set_Shift_Count_OK
(Nod
, True);
2400 end Make_Shift_Left
;
2402 ----------------------
2403 -- Make_Shift_Right --
2404 ----------------------
2406 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2410 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2414 Make_Op_Shift_Right
(Sloc
(N
),
2417 Set_Shift_Count_OK
(Nod
, True);
2420 end Make_Shift_Right
;
2422 -----------------------------
2423 -- RJ_Unchecked_Convert_To --
2424 -----------------------------
2426 function RJ_Unchecked_Convert_To
2428 Expr
: Node_Id
) return Node_Id
2430 Source_Typ
: constant Entity_Id
:= Etype
(Expr
);
2431 Target_Typ
: constant Entity_Id
:= Typ
;
2433 Src
: Node_Id
:= Expr
;
2439 Source_Siz
:= UI_To_Int
(RM_Size
(Source_Typ
));
2440 Target_Siz
:= UI_To_Int
(RM_Size
(Target_Typ
));
2442 -- First step, if the source type is not a discrete type, then we
2443 -- first convert to a modular type of the source length, since
2444 -- otherwise, on a big-endian machine, we get left-justification.
2445 -- We do it for little-endian machines as well, because there might
2446 -- be junk bits that are not cleared if the type is not numeric.
2448 if Source_Siz
/= Target_Siz
2449 and then not Is_Discrete_Type
(Source_Typ
)
2451 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Source_Siz
)), Src
);
2454 -- In the big endian case, if the lengths of the two types differ,
2455 -- then we must worry about possible left justification in the
2456 -- conversion, and avoiding that is what this is all about.
2458 if Bytes_Big_Endian
and then Source_Siz
/= Target_Siz
then
2460 -- Next step. If the target is not a discrete type, then we first
2461 -- convert to a modular type of the target length, since
2462 -- otherwise, on a big-endian machine, we get left-justification.
2464 if not Is_Discrete_Type
(Target_Typ
) then
2465 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Target_Siz
)), Src
);
2469 -- And now we can do the final conversion to the target type
2471 return Unchecked_Convert_To
(Target_Typ
, Src
);
2472 end RJ_Unchecked_Convert_To
;
2474 ----------------------------------------------
2475 -- Setup_Enumeration_Packed_Array_Reference --
2476 ----------------------------------------------
2478 -- All we have to do here is to find the subscripts that correspond
2479 -- to the index positions that have non-standard enumeration types
2480 -- and insert a Pos attribute to get the proper subscript value.
2482 -- Finally the prefix must be uncheck converted to the corresponding
2483 -- packed array type.
2485 -- Note that the component type is unchanged, so we do not need to
2486 -- fiddle with the types (Gigi always automatically takes the packed
2487 -- array type if it is set, as it will be in this case).
2489 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
) is
2490 Pfx
: constant Node_Id
:= Prefix
(N
);
2491 Typ
: constant Entity_Id
:= Etype
(N
);
2492 Exprs
: constant List_Id
:= Expressions
(N
);
2496 -- If the array is unconstrained, then we replace the array
2497 -- reference with its actual subtype. This actual subtype will
2498 -- have a packed array type with appropriate bounds.
2500 if not Is_Constrained
(Packed_Array_Type
(Etype
(Pfx
))) then
2501 Convert_To_Actual_Subtype
(Pfx
);
2504 Expr
:= First
(Exprs
);
2505 while Present
(Expr
) loop
2507 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2508 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
2511 if Is_Enumeration_Type
(Expr_Typ
)
2512 and then Has_Non_Standard_Rep
(Expr_Typ
)
2515 Make_Attribute_Reference
(Loc
,
2516 Prefix
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
2517 Attribute_Name
=> Name_Pos
,
2518 Expressions
=> New_List
(Relocate_Node
(Expr
))));
2519 Analyze_And_Resolve
(Expr
, Standard_Natural
);
2527 Make_Indexed_Component
(Sloc
(N
),
2529 Unchecked_Convert_To
(Packed_Array_Type
(Etype
(Pfx
)), Pfx
),
2530 Expressions
=> Exprs
));
2532 Analyze_And_Resolve
(N
, Typ
);
2534 end Setup_Enumeration_Packed_Array_Reference
;
2536 -----------------------------------------
2537 -- Setup_Inline_Packed_Array_Reference --
2538 -----------------------------------------
2540 procedure Setup_Inline_Packed_Array_Reference
2543 Obj
: in out Node_Id
;
2545 Shift
: out Node_Id
)
2547 Loc
: constant Source_Ptr
:= Sloc
(N
);
2554 Csiz
:= Component_Size
(Atyp
);
2556 Convert_To_PAT_Type
(Obj
);
2559 Cmask
:= 2 ** Csiz
- 1;
2561 if Is_Array_Type
(PAT
) then
2562 Otyp
:= Component_Type
(PAT
);
2563 Osiz
:= Component_Size
(PAT
);
2568 -- In the case where the PAT is a modular type, we want the actual
2569 -- size in bits of the modular value we use. This is neither the
2570 -- Object_Size nor the Value_Size, either of which may have been
2571 -- reset to strange values, but rather the minimum size. Note that
2572 -- since this is a modular type with full range, the issue of
2573 -- biased representation does not arise.
2575 Osiz
:= UI_From_Int
(Minimum_Size
(Otyp
));
2578 Compute_Linear_Subscript
(Atyp
, N
, Shift
);
2580 -- If the component size is not 1, then the subscript must be
2581 -- multiplied by the component size to get the shift count.
2585 Make_Op_Multiply
(Loc
,
2586 Left_Opnd
=> Make_Integer_Literal
(Loc
, Csiz
),
2587 Right_Opnd
=> Shift
);
2590 -- If we have the array case, then this shift count must be broken
2591 -- down into a byte subscript, and a shift within the byte.
2593 if Is_Array_Type
(PAT
) then
2596 New_Shift
: Node_Id
;
2599 -- We must analyze shift, since we will duplicate it
2601 Set_Parent
(Shift
, N
);
2603 (Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2605 -- The shift count within the word is
2610 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2611 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
));
2613 -- The subscript to be used on the PAT array is
2617 Make_Indexed_Component
(Loc
,
2619 Expressions
=> New_List
(
2620 Make_Op_Divide
(Loc
,
2621 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2622 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
))));
2627 -- For the modular integer case, the object to be manipulated is
2628 -- the entire array, so Obj is unchanged. Note that we will reset
2629 -- its type to PAT before returning to the caller.
2635 -- The one remaining step is to modify the shift count for the
2636 -- big-endian case. Consider the following example in a byte:
2638 -- xxxxxxxx bits of byte
2639 -- vvvvvvvv bits of value
2640 -- 33221100 little-endian numbering
2641 -- 00112233 big-endian numbering
2643 -- Here we have the case of 2-bit fields
2645 -- For the little-endian case, we already have the proper shift
2646 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2648 -- For the big endian case, we have to adjust the shift count,
2649 -- computing it as (N - F) - shift, where N is the number of bits
2650 -- in an element of the array used to implement the packed array,
2651 -- F is the number of bits in a source level array element, and
2652 -- shift is the count so far computed.
2654 if Bytes_Big_Endian
then
2656 Make_Op_Subtract
(Loc
,
2657 Left_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
- Csiz
),
2658 Right_Opnd
=> Shift
);
2661 Set_Parent
(Shift
, N
);
2662 Set_Parent
(Obj
, N
);
2663 Analyze_And_Resolve
(Obj
, Otyp
, Suppress
=> All_Checks
);
2664 Analyze_And_Resolve
(Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2666 -- Make sure final type of object is the appropriate packed type
2668 Set_Etype
(Obj
, Otyp
);
2670 end Setup_Inline_Packed_Array_Reference
;