1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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
71 -- in the two cases. For the little-endian case, we can simply use the
72 -- bit number (i.e. the element number * element size) as the count for
73 -- a right shift. For the big-endian case, we have to subtract the shift
74 -- count from an appropriate constant to use in the right shift. We use
75 -- rotates instead of shifts (which is necessary in the store case to
76 -- preserve other fields), and we expect that the backend will be able
77 -- to change the right rotate into a left rotate, avoiding the subtract,
78 -- if the 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
85 -- library routines. This table is used to obtain the entity for the
88 type E_Array
is array (Int
range 01 .. 63) of RE_Id
;
90 -- Array of Bits_nn entities. Note that we do not use library routines
91 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
92 -- entries from System.Unsigned, because we also use this table for
93 -- certain special unchecked conversions in the big-endian case.
95 Bits_Id
: constant E_Array
:=
111 16 => RE_Unsigned_16
,
127 32 => RE_Unsigned_32
,
160 -- Array of Get routine entities. These are used to obtain an element
161 -- from a packed array. The N'th entry is used to obtain elements from
162 -- a packed array whose component size is N. RE_Null is used as a null
163 -- entry, for the cases where a library routine is not used.
165 Get_Id
: constant E_Array
:=
230 -- Array of Get routine entities to be used in the case where the packed
231 -- array is itself a component of a packed structure, and therefore may
232 -- not be fully aligned. This only affects the even sizes, since for the
233 -- odd sizes, we do not get any fixed alignment in any case.
235 GetU_Id
: constant E_Array
:=
300 -- Array of Set routine entities. These are used to assign an element
301 -- of a packed array. The N'th entry is used to assign elements for
302 -- a packed array whose component size is N. RE_Null is used as a null
303 -- entry, for the cases where a library routine is not used.
305 Set_Id
: constant E_Array
:=
370 -- Array of Set routine entities to be used in the case where the packed
371 -- array is itself a component of a packed structure, and therefore may
372 -- not be fully aligned. This only affects the even sizes, since for the
373 -- odd sizes, we do not get any fixed alignment in any case.
375 SetU_Id
: constant E_Array
:=
440 -----------------------
441 -- Local Subprograms --
442 -----------------------
444 procedure Compute_Linear_Subscript
447 Subscr
: out Node_Id
);
448 -- Given a constrained array type Atyp, and an indexed component node
449 -- N referencing an array object of this type, build an expression of
450 -- type Standard.Integer representing the zero-based linear subscript
451 -- value. This expression includes any required range checks.
453 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
);
454 -- Given an expression of a packed array type, builds a corresponding
455 -- expression whose type is the implementation type used to represent
456 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
458 procedure Get_Base_And_Bit_Offset
461 Offset
: out Node_Id
);
462 -- Given a node N for a name which involves a packed array reference,
463 -- return the base object of the reference and build an expression of
464 -- type Standard.Integer representing the zero-based offset in bits
465 -- from Base'Address to the first bit of the reference.
467 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean;
468 -- There are two versions of the Set routines, the ones used when the
469 -- object is known to be sufficiently well aligned given the number of
470 -- bits, and the ones used when the object is not known to be aligned.
471 -- This routine is used to determine which set to use. Obj is a reference
472 -- to the object, and Csiz is the component size of the packed array.
473 -- True is returned if the alignment of object is known to be sufficient,
474 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
477 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
478 -- Build a left shift node, checking for the case of a shift count of zero
480 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
481 -- Build a right shift node, checking for the case of a shift count of zero
483 function RJ_Unchecked_Convert_To
485 Expr
: Node_Id
) return Node_Id
;
486 -- The packed array code does unchecked conversions which in some cases
487 -- may involve non-discrete types with differing sizes. The semantics of
488 -- such conversions is potentially endian dependent, and the effect we
489 -- want here for such a conversion is to do the conversion in size as
490 -- though numeric items are involved, and we extend or truncate on the
491 -- left side. This happens naturally in the little-endian case, but in
492 -- the big endian case we can get left justification, when what we want
493 -- is right justification. This routine does the unchecked conversion in
494 -- a stepwise manner to ensure that it gives the expected result. Hence
495 -- the name (RJ = Right justified). The parameters Typ and Expr are as
496 -- for the case of a normal Unchecked_Convert_To call.
498 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
);
499 -- This routine is called in the Get and Set case for arrays that are
500 -- packed but not bit-packed, meaning that they have at least one
501 -- subscript that is of an enumeration type with a non-standard
502 -- representation. This routine modifies the given node to properly
503 -- reference the corresponding packed array type.
505 procedure Setup_Inline_Packed_Array_Reference
508 Obj
: in out Node_Id
;
510 Shift
: out Node_Id
);
511 -- This procedure performs common processing on the N_Indexed_Component
512 -- parameter given as N, whose prefix is a reference to a packed array.
513 -- This is used for the get and set when the component size is 1,2,4
514 -- or for other component sizes when the packed array type is a modular
515 -- type (i.e. the cases that are handled with inline code).
519 -- N is the N_Indexed_Component node for the packed array reference
521 -- Atyp is the constrained array type (the actual subtype has been
522 -- computed if necessary to obtain the constraints, but this is still
523 -- the original array type, not the Packed_Array_Type value).
525 -- Obj is the object which is to be indexed. It is always of type Atyp.
529 -- Obj is the object containing the desired bit field. It is of type
530 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
531 -- entire value, for the small static case, or the proper selected byte
532 -- from the array in the large or dynamic case. This node is analyzed
533 -- and resolved on return.
535 -- Shift is a node representing the shift count to be used in the
536 -- rotate right instruction that positions the field for access.
537 -- This node is analyzed and resolved on return.
539 -- Cmask is a mask corresponding to the width of the component field.
540 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
542 -- Note: in some cases the call to this routine may generate actions
543 -- (for handling multi-use references and the generation of the packed
544 -- array type on the fly). Such actions are inserted into the tree
545 -- directly using Insert_Action.
547 ------------------------------
548 -- Compute_Linear_Subscript --
549 ------------------------------
551 procedure Compute_Linear_Subscript
554 Subscr
: out Node_Id
)
556 Loc
: constant Source_Ptr
:= Sloc
(N
);
565 -- Loop through dimensions
567 Indx
:= First_Index
(Atyp
);
568 Oldsub
:= First
(Expressions
(N
));
570 while Present
(Indx
) loop
571 Styp
:= Etype
(Indx
);
572 Newsub
:= Relocate_Node
(Oldsub
);
574 -- Get expression for the subscript value. First, if Do_Range_Check
575 -- is set on a subscript, then we must do a range check against the
576 -- original bounds (not the bounds of the packed array type). We do
577 -- this by introducing a subtype conversion.
579 if Do_Range_Check
(Newsub
)
580 and then Etype
(Newsub
) /= Styp
582 Newsub
:= Convert_To
(Styp
, Newsub
);
585 -- Now evolve the expression for the subscript. First convert
586 -- the subscript to be zero based and of an integer type.
588 -- Case of integer type, where we just subtract to get lower bound
590 if Is_Integer_Type
(Styp
) then
592 -- If length of integer type is smaller than standard integer,
593 -- then we convert to integer first, then do the subtract
595 -- Integer (subscript) - Integer (Styp'First)
597 if Esize
(Styp
) < Esize
(Standard_Integer
) then
599 Make_Op_Subtract
(Loc
,
600 Left_Opnd
=> Convert_To
(Standard_Integer
, Newsub
),
602 Convert_To
(Standard_Integer
,
603 Make_Attribute_Reference
(Loc
,
604 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
605 Attribute_Name
=> Name_First
)));
607 -- For larger integer types, subtract first, then convert to
608 -- integer, this deals with strange long long integer bounds.
610 -- Integer (subscript - Styp'First)
614 Convert_To
(Standard_Integer
,
615 Make_Op_Subtract
(Loc
,
618 Make_Attribute_Reference
(Loc
,
619 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
620 Attribute_Name
=> Name_First
)));
623 -- For the enumeration case, we have to use 'Pos to get the value
624 -- to work with before subtracting the lower bound.
626 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
628 -- This is not quite right for bizarre cases where the size of the
629 -- enumeration type is > Integer'Size bits due to rep clause ???
632 pragma Assert
(Is_Enumeration_Type
(Styp
));
635 Make_Op_Subtract
(Loc
,
636 Left_Opnd
=> Convert_To
(Standard_Integer
,
637 Make_Attribute_Reference
(Loc
,
638 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
639 Attribute_Name
=> Name_Pos
,
640 Expressions
=> New_List
(Newsub
))),
643 Convert_To
(Standard_Integer
,
644 Make_Attribute_Reference
(Loc
,
645 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
646 Attribute_Name
=> Name_Pos
,
647 Expressions
=> New_List
(
648 Make_Attribute_Reference
(Loc
,
649 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
650 Attribute_Name
=> Name_First
)))));
653 Set_Paren_Count
(Newsub
, 1);
655 -- For the first subscript, we just copy that subscript value
660 -- Otherwise, we must multiply what we already have by the current
661 -- stride and then add in the new value to the evolving subscript.
667 Make_Op_Multiply
(Loc
,
670 Make_Attribute_Reference
(Loc
,
671 Attribute_Name
=> Name_Range_Length
,
672 Prefix
=> New_Occurrence_Of
(Styp
, Loc
))),
673 Right_Opnd
=> Newsub
);
676 -- Move to next subscript
681 end Compute_Linear_Subscript
;
683 -------------------------
684 -- Convert_To_PAT_Type --
685 -------------------------
687 -- The PAT is always obtained from the actual subtype
689 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
) is
693 Convert_To_Actual_Subtype
(Aexp
);
694 Act_ST
:= Underlying_Type
(Etype
(Aexp
));
695 Create_Packed_Array_Type
(Act_ST
);
697 -- Just replace the etype with the packed array type. This works because
698 -- the expression will not be further analyzed, and Gigi considers the
699 -- two types equivalent in any case.
701 -- This is not strictly the case ??? If the reference is an actual in
702 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
703 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
704 -- array reference, reanalysis can produce spurious type errors when the
705 -- PAT type is replaced again with the original type of the array. Same
706 -- for the case of a dereference. The following is correct and minimal,
707 -- but the handling of more complex packed expressions in actuals is
708 -- confused. Probably the problem only remains for actuals in calls.
710 Set_Etype
(Aexp
, Packed_Array_Type
(Act_ST
));
712 if Is_Entity_Name
(Aexp
)
714 (Nkind
(Aexp
) = N_Indexed_Component
715 and then Is_Entity_Name
(Prefix
(Aexp
)))
716 or else Nkind
(Aexp
) = N_Explicit_Dereference
720 end Convert_To_PAT_Type
;
722 ------------------------------
723 -- Create_Packed_Array_Type --
724 ------------------------------
726 procedure Create_Packed_Array_Type
(Typ
: Entity_Id
) is
727 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
728 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
729 Csize
: constant Uint
:= Component_Size
(Typ
);
744 procedure Install_PAT
;
745 -- This procedure is called with Decl set to the declaration for the
746 -- packed array type. It creates the type and installs it as required.
748 procedure Set_PB_Type
;
749 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
750 -- requirements (see documentation in the spec of this package).
756 procedure Install_PAT
is
757 Pushed_Scope
: Boolean := False;
760 -- We do not want to put the declaration we have created in the tree
761 -- since it is often hard, and sometimes impossible to find a proper
762 -- place for it (the impossible case arises for a packed array type
763 -- with bounds depending on the discriminant, a declaration cannot
764 -- be put inside the record, and the reference to the discriminant
765 -- cannot be outside the record).
767 -- The solution is to analyze the declaration while temporarily
768 -- attached to the tree at an appropriate point, and then we install
769 -- the resulting type as an Itype in the packed array type field of
770 -- the original type, so that no explicit declaration is required.
772 -- Note: the packed type is created in the scope of its parent
773 -- type. There are at least some cases where the current scope
774 -- is deeper, and so when this is the case, we temporarily reset
775 -- the scope for the definition. This is clearly safe, since the
776 -- first use of the packed array type will be the implicit
777 -- reference from the corresponding unpacked type when it is
780 if Is_Itype
(Typ
) then
781 Set_Parent
(Decl
, Associated_Node_For_Itype
(Typ
));
783 Set_Parent
(Decl
, Declaration_Node
(Typ
));
786 if Scope
(Typ
) /= Current_Scope
then
787 Push_Scope
(Scope
(Typ
));
788 Pushed_Scope
:= True;
791 Set_Is_Itype
(PAT
, True);
792 Set_Packed_Array_Type
(Typ
, PAT
);
793 Analyze
(Decl
, Suppress
=> All_Checks
);
799 -- Set Esize and RM_Size to the actual size of the packed object
800 -- Do not reset RM_Size if already set, as happens in the case of
803 if Unknown_Esize
(PAT
) then
804 Set_Esize
(PAT
, PASize
);
807 if Unknown_RM_Size
(PAT
) then
808 Set_RM_Size
(PAT
, PASize
);
811 Adjust_Esize_Alignment
(PAT
);
813 -- Set remaining fields of packed array type
815 Init_Alignment
(PAT
);
816 Set_Parent
(PAT
, Empty
);
817 Set_Associated_Node_For_Itype
(PAT
, Typ
);
818 Set_Is_Packed_Array_Type
(PAT
, True);
819 Set_Original_Array_Type
(PAT
, Typ
);
821 -- We definitely do not want to delay freezing for packed array
822 -- types. This is of particular importance for the itypes that
823 -- are generated for record components depending on discriminants
824 -- where there is no place to put the freeze node.
826 Set_Has_Delayed_Freeze
(PAT
, False);
827 Set_Has_Delayed_Freeze
(Etype
(PAT
), False);
829 -- If we did allocate a freeze node, then clear out the reference
830 -- since it is obsolete (should we delete the freeze node???)
832 Set_Freeze_Node
(PAT
, Empty
);
833 Set_Freeze_Node
(Etype
(PAT
), Empty
);
840 procedure Set_PB_Type
is
842 -- If the user has specified an explicit alignment for the
843 -- type or component, take it into account.
845 if Csize
<= 2 or else Csize
= 4 or else Csize
mod 2 /= 0
846 or else Alignment
(Typ
) = 1
847 or else Component_Alignment
(Typ
) = Calign_Storage_Unit
849 PB_Type
:= RTE
(RE_Packed_Bytes1
);
851 elsif Csize
mod 4 /= 0
852 or else Alignment
(Typ
) = 2
854 PB_Type
:= RTE
(RE_Packed_Bytes2
);
857 PB_Type
:= RTE
(RE_Packed_Bytes4
);
861 -- Start of processing for Create_Packed_Array_Type
864 -- If we already have a packed array type, nothing to do
866 if Present
(Packed_Array_Type
(Typ
)) then
870 -- If our immediate ancestor subtype is constrained, and it already
871 -- has a packed array type, then just share the same type, since the
872 -- bounds must be the same. If the ancestor is not an array type but
873 -- a private type, as can happen with multiple instantiations, create
874 -- a new packed type, to avoid privacy issues.
876 if Ekind
(Typ
) = E_Array_Subtype
then
877 Ancest
:= Ancestor_Subtype
(Typ
);
880 and then Is_Array_Type
(Ancest
)
881 and then Is_Constrained
(Ancest
)
882 and then Present
(Packed_Array_Type
(Ancest
))
884 Set_Packed_Array_Type
(Typ
, Packed_Array_Type
(Ancest
));
889 -- We preset the result type size from the size of the original array
890 -- type, since this size clearly belongs to the packed array type. The
891 -- size of the conceptual unpacked type is always set to unknown.
893 PASize
:= RM_Size
(Typ
);
895 -- Case of an array where at least one index is of an enumeration
896 -- type with a non-standard representation, but the component size
897 -- is not appropriate for bit packing. This is the case where we
898 -- have Is_Packed set (we would never be in this unit otherwise),
899 -- but Is_Bit_Packed_Array is false.
901 -- Note that if the component size is appropriate for bit packing,
902 -- then the circuit for the computation of the subscript properly
903 -- deals with the non-standard enumeration type case by taking the
906 if not Is_Bit_Packed_Array
(Typ
) then
908 -- Here we build a declaration:
910 -- type tttP is array (index1, index2, ...) of component_type
912 -- where index1, index2, are the index types. These are the same
913 -- as the index types of the original array, except for the non-
914 -- standard representation enumeration type case, where we have
917 -- For the unconstrained array case, we use
921 -- For the constrained case, we use
923 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
924 -- Enum_Type'Pos (Enum_Type'Last);
927 Make_Defining_Identifier
(Loc
,
928 Chars
=> New_External_Name
(Chars
(Typ
), 'P'));
930 Set_Packed_Array_Type
(Typ
, PAT
);
933 Indexes
: constant List_Id
:= New_List
;
935 Indx_Typ
: Entity_Id
;
940 Indx
:= First_Index
(Typ
);
942 while Present
(Indx
) loop
943 Indx_Typ
:= Etype
(Indx
);
945 Enum_Case
:= Is_Enumeration_Type
(Indx_Typ
)
946 and then Has_Non_Standard_Rep
(Indx_Typ
);
948 -- Unconstrained case
950 if not Is_Constrained
(Typ
) then
952 Indx_Typ
:= Standard_Natural
;
955 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
960 if not Enum_Case
then
961 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
965 Make_Subtype_Indication
(Loc
,
967 New_Occurrence_Of
(Standard_Natural
, Loc
),
969 Make_Range_Constraint
(Loc
,
973 Make_Attribute_Reference
(Loc
,
975 New_Occurrence_Of
(Indx_Typ
, Loc
),
976 Attribute_Name
=> Name_Pos
,
977 Expressions
=> New_List
(
978 Make_Attribute_Reference
(Loc
,
980 New_Occurrence_Of
(Indx_Typ
, Loc
),
981 Attribute_Name
=> Name_First
))),
984 Make_Attribute_Reference
(Loc
,
986 New_Occurrence_Of
(Indx_Typ
, Loc
),
987 Attribute_Name
=> Name_Pos
,
988 Expressions
=> New_List
(
989 Make_Attribute_Reference
(Loc
,
991 New_Occurrence_Of
(Indx_Typ
, Loc
),
992 Attribute_Name
=> Name_Last
)))))));
1000 if not Is_Constrained
(Typ
) then
1002 Make_Unconstrained_Array_Definition
(Loc
,
1003 Subtype_Marks
=> Indexes
,
1004 Component_Definition
=>
1005 Make_Component_Definition
(Loc
,
1006 Aliased_Present
=> False,
1007 Subtype_Indication
=>
1008 New_Occurrence_Of
(Ctyp
, Loc
)));
1012 Make_Constrained_Array_Definition
(Loc
,
1013 Discrete_Subtype_Definitions
=> Indexes
,
1014 Component_Definition
=>
1015 Make_Component_Definition
(Loc
,
1016 Aliased_Present
=> False,
1017 Subtype_Indication
=>
1018 New_Occurrence_Of
(Ctyp
, Loc
)));
1022 Make_Full_Type_Declaration
(Loc
,
1023 Defining_Identifier
=> PAT
,
1024 Type_Definition
=> Typedef
);
1027 -- Set type as packed array type and install it
1029 Set_Is_Packed_Array_Type
(PAT
);
1033 -- Case of bit-packing required for unconstrained array. We create
1034 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1036 elsif not Is_Constrained
(Typ
) then
1038 Make_Defining_Identifier
(Loc
,
1039 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1041 Set_Packed_Array_Type
(Typ
, PAT
);
1045 Make_Subtype_Declaration
(Loc
,
1046 Defining_Identifier
=> PAT
,
1047 Subtype_Indication
=> New_Occurrence_Of
(PB_Type
, Loc
));
1051 -- Remaining code is for the case of bit-packing for constrained array
1053 -- The name of the packed array subtype is
1057 -- where sss is the component size in bits and ttt is the name of
1058 -- the parent packed type.
1062 Make_Defining_Identifier
(Loc
,
1063 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1065 Set_Packed_Array_Type
(Typ
, PAT
);
1067 -- Build an expression for the length of the array in bits.
1068 -- This is the product of the length of each of the dimensions
1074 Len_Expr
:= Empty
; -- suppress junk warning
1078 Make_Attribute_Reference
(Loc
,
1079 Attribute_Name
=> Name_Length
,
1080 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1081 Expressions
=> New_List
(
1082 Make_Integer_Literal
(Loc
, J
)));
1085 Len_Expr
:= Len_Dim
;
1089 Make_Op_Multiply
(Loc
,
1090 Left_Opnd
=> Len_Expr
,
1091 Right_Opnd
=> Len_Dim
);
1095 exit when J
> Number_Dimensions
(Typ
);
1099 -- Temporarily attach the length expression to the tree and analyze
1100 -- and resolve it, so that we can test its value. We assume that the
1101 -- total length fits in type Integer. This expression may involve
1102 -- discriminants, so we treat it as a default/per-object expression.
1104 Set_Parent
(Len_Expr
, Typ
);
1105 Preanalyze_Spec_Expression
(Len_Expr
, Standard_Long_Long_Integer
);
1107 -- Use a modular type if possible. We can do this if we have
1108 -- static bounds, and the length is small enough, and the length
1109 -- is not zero. We exclude the zero length case because the size
1110 -- of things is always at least one, and the zero length object
1111 -- would have an anomalous size.
1113 if Compile_Time_Known_Value
(Len_Expr
) then
1114 Len_Bits
:= Expr_Value
(Len_Expr
) * Csize
;
1116 -- Check for size known to be too large
1119 Uint_2
** (Standard_Integer_Size
- 1) * System_Storage_Unit
1121 if System_Storage_Unit
= 8 then
1123 ("packed array size cannot exceed " &
1124 "Integer''Last bytes", Typ
);
1127 ("packed array size cannot exceed " &
1128 "Integer''Last storage units", Typ
);
1131 -- Reset length to arbitrary not too high value to continue
1133 Len_Expr
:= Make_Integer_Literal
(Loc
, 65535);
1134 Analyze_And_Resolve
(Len_Expr
, Standard_Long_Long_Integer
);
1137 -- We normally consider small enough to mean no larger than the
1138 -- value of System_Max_Binary_Modulus_Power, checking that in the
1139 -- case of values longer than word size, we have long shifts.
1143 (Len_Bits
<= System_Word_Size
1144 or else (Len_Bits
<= System_Max_Binary_Modulus_Power
1145 and then Support_Long_Shifts_On_Target
))
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
1196 -- Propagate a given alignment to the modular type. This can
1197 -- cause it to be under-aligned, but that's OK.
1199 if Present
(Alignment_Clause
(Typ
)) then
1200 Set_Alignment
(PAT
, Alignment
(Typ
));
1207 -- Could not use a modular type, for all other cases, we build
1208 -- a packed array subtype:
1211 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1213 -- Bits is the length of the array in bits
1220 Make_Op_Multiply
(Loc
,
1222 Make_Integer_Literal
(Loc
, Csize
),
1223 Right_Opnd
=> Len_Expr
),
1226 Make_Integer_Literal
(Loc
, 7));
1228 Set_Paren_Count
(Bits_U1
, 1);
1231 Make_Op_Subtract
(Loc
,
1233 Make_Op_Divide
(Loc
,
1234 Left_Opnd
=> Bits_U1
,
1235 Right_Opnd
=> Make_Integer_Literal
(Loc
, 8)),
1236 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
1239 Make_Subtype_Declaration
(Loc
,
1240 Defining_Identifier
=> PAT
,
1241 Subtype_Indication
=>
1242 Make_Subtype_Indication
(Loc
,
1243 Subtype_Mark
=> New_Occurrence_Of
(PB_Type
, Loc
),
1245 Make_Index_Or_Discriminant_Constraint
(Loc
,
1246 Constraints
=> New_List
(
1249 Make_Integer_Literal
(Loc
, 0),
1251 Convert_To
(Standard_Integer
, PAT_High
))))));
1255 -- Currently the code in this unit requires that packed arrays
1256 -- represented by non-modular arrays of bytes be on a byte
1257 -- boundary for bit sizes handled by System.Pack_nn units.
1258 -- That's because these units assume the array being accessed
1259 -- starts on a byte boundary.
1261 if Get_Id
(UI_To_Int
(Csize
)) /= RE_Null
then
1262 Set_Must_Be_On_Byte_Boundary
(Typ
);
1265 end Create_Packed_Array_Type
;
1267 -----------------------------------
1268 -- Expand_Bit_Packed_Element_Set --
1269 -----------------------------------
1271 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
) is
1272 Loc
: constant Source_Ptr
:= Sloc
(N
);
1273 Lhs
: constant Node_Id
:= Name
(N
);
1275 Ass_OK
: constant Boolean := Assignment_OK
(Lhs
);
1276 -- Used to preserve assignment OK status when assignment is rewritten
1278 Rhs
: Node_Id
:= Expression
(N
);
1279 -- Initially Rhs is the right hand side value, it will be replaced
1280 -- later by an appropriate unchecked conversion for the assignment.
1290 -- The expression for the shift value that is required
1292 Shift_Used
: Boolean := False;
1293 -- Set True if Shift has been used in the generated code at least
1294 -- once, so that it must be duplicated if used again
1299 Rhs_Val_Known
: Boolean;
1301 -- If the value of the right hand side as an integer constant is
1302 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1303 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1304 -- the Rhs_Val is undefined.
1306 function Get_Shift
return Node_Id
;
1307 -- Function used to get the value of Shift, making sure that it
1308 -- gets duplicated if the function is called more than once.
1314 function Get_Shift
return Node_Id
is
1316 -- If we used the shift value already, then duplicate it. We
1317 -- set a temporary parent in case actions have to be inserted.
1320 Set_Parent
(Shift
, N
);
1321 return Duplicate_Subexpr_No_Checks
(Shift
);
1323 -- If first time, use Shift unchanged, and set flag for first use
1331 -- Start of processing for Expand_Bit_Packed_Element_Set
1334 pragma Assert
(Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
))));
1336 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1337 Convert_To_Actual_Subtype
(Obj
);
1338 Atyp
:= Etype
(Obj
);
1339 PAT
:= Packed_Array_Type
(Atyp
);
1340 Ctyp
:= Component_Type
(Atyp
);
1341 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1343 -- We convert the right hand side to the proper subtype to ensure
1344 -- that an appropriate range check is made (since the normal range
1345 -- check from assignment will be lost in the transformations). This
1346 -- conversion is analyzed immediately so that subsequent processing
1347 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1349 -- If the right-hand side is a string literal, create a temporary for
1350 -- it, constant-folding is not ready to wrap the bit representation
1351 -- of a string literal.
1353 if Nkind
(Rhs
) = N_String_Literal
then
1358 Make_Object_Declaration
(Loc
,
1359 Defining_Identifier
=> Make_Temporary
(Loc
, 'T', Rhs
),
1360 Object_Definition
=> New_Occurrence_Of
(Ctyp
, Loc
),
1361 Expression
=> New_Copy_Tree
(Rhs
));
1363 Insert_Actions
(N
, New_List
(Decl
));
1364 Rhs
:= New_Occurrence_Of
(Defining_Identifier
(Decl
), Loc
);
1368 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1369 Set_Parent
(Rhs
, N
);
1371 -- If we are building the initialization procedure for a packed array,
1372 -- and Initialize_Scalars is enabled, each component assignment is an
1373 -- out-of-range value by design. Compile this value without checks,
1374 -- because a call to the array init_proc must not raise an exception.
1377 and then Initialize_Scalars
1379 Analyze_And_Resolve
(Rhs
, Ctyp
, Suppress
=> All_Checks
);
1381 Analyze_And_Resolve
(Rhs
, Ctyp
);
1384 -- For the AAMP target, indexing of certain packed array is passed
1385 -- through to the back end without expansion, because the expansion
1386 -- results in very inefficient code on that target. This allows the
1387 -- GNAAMP back end to generate specialized macros that support more
1388 -- efficient indexing of packed arrays with components having sizes
1389 -- that are small powers of two.
1392 and then (Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4)
1397 -- Case of component size 1,2,4 or any component size for the modular
1398 -- case. These are the cases for which we can inline the code.
1400 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1401 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1403 Setup_Inline_Packed_Array_Reference
(Lhs
, Atyp
, Obj
, Cmask
, Shift
);
1405 -- The statement to be generated is:
1407 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1409 -- where mask1 is obtained by shifting Cmask left Shift bits
1410 -- and then complementing the result.
1412 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1414 -- the "or ..." is omitted if rhs is constant and all 0 bits
1416 -- rhs is converted to the appropriate type
1418 -- The result is converted back to the array type, since
1419 -- otherwise we lose knowledge of the packed nature.
1421 -- Determine if right side is all 0 bits or all 1 bits
1423 if Compile_Time_Known_Value
(Rhs
) then
1424 Rhs_Val
:= Expr_Rep_Value
(Rhs
);
1425 Rhs_Val_Known
:= True;
1427 -- The following test catches the case of an unchecked conversion
1428 -- of an integer literal. This results from optimizing aggregates
1431 elsif Nkind
(Rhs
) = N_Unchecked_Type_Conversion
1432 and then Compile_Time_Known_Value
(Expression
(Rhs
))
1434 Rhs_Val
:= Expr_Rep_Value
(Expression
(Rhs
));
1435 Rhs_Val_Known
:= True;
1439 Rhs_Val_Known
:= False;
1442 -- Some special checks for the case where the right hand value
1443 -- is known at compile time. Basically we have to take care of
1444 -- the implicit conversion to the subtype of the component object.
1446 if Rhs_Val_Known
then
1448 -- If we have a biased component type then we must manually do
1449 -- the biasing, since we are taking responsibility in this case
1450 -- for constructing the exact bit pattern to be used.
1452 if Has_Biased_Representation
(Ctyp
) then
1453 Rhs_Val
:= Rhs_Val
- Expr_Rep_Value
(Type_Low_Bound
(Ctyp
));
1456 -- For a negative value, we manually convert the twos complement
1457 -- value to a corresponding unsigned value, so that the proper
1458 -- field width is maintained. If we did not do this, we would
1459 -- get too many leading sign bits later on.
1462 Rhs_Val
:= 2 ** UI_From_Int
(Csiz
) + Rhs_Val
;
1466 -- Now create copies removing side effects. Note that in some
1467 -- complex cases, this may cause the fact that we have already
1468 -- set a packed array type on Obj to get lost. So we save the
1469 -- type of Obj, and make sure it is reset properly.
1472 T
: constant Entity_Id
:= Etype
(Obj
);
1474 New_Lhs
:= Duplicate_Subexpr
(Obj
, True);
1475 New_Rhs
:= Duplicate_Subexpr_No_Checks
(Obj
);
1477 Set_Etype
(New_Lhs
, T
);
1478 Set_Etype
(New_Rhs
, T
);
1481 -- First we deal with the "and"
1483 if not Rhs_Val_Known
or else Rhs_Val
/= Cmask
then
1489 if Compile_Time_Known_Value
(Shift
) then
1491 Make_Integer_Literal
(Loc
,
1492 Modulus
(Etype
(Obj
)) - 1 -
1493 (Cmask
* (2 ** Expr_Value
(Get_Shift
))));
1494 Set_Print_In_Hex
(Mask1
);
1497 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1498 Set_Print_In_Hex
(Lit
);
1501 Right_Opnd
=> Make_Shift_Left
(Lit
, Get_Shift
));
1506 Left_Opnd
=> New_Rhs
,
1507 Right_Opnd
=> Mask1
);
1511 -- Then deal with the "or"
1513 if not Rhs_Val_Known
or else Rhs_Val
/= 0 then
1517 procedure Fixup_Rhs
;
1518 -- Adjust Rhs by bias if biased representation for components
1519 -- or remove extraneous high order sign bits if signed.
1521 procedure Fixup_Rhs
is
1522 Etyp
: constant Entity_Id
:= Etype
(Rhs
);
1525 -- For biased case, do the required biasing by simply
1526 -- converting to the biased subtype (the conversion
1527 -- will generate the required bias).
1529 if Has_Biased_Representation
(Ctyp
) then
1530 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1532 -- For a signed integer type that is not biased, generate
1533 -- a conversion to unsigned to strip high order sign bits.
1535 elsif Is_Signed_Integer_Type
(Ctyp
) then
1536 Rhs
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Csiz
)), Rhs
);
1539 -- Set Etype, since it can be referenced before the
1540 -- node is completely analyzed.
1542 Set_Etype
(Rhs
, Etyp
);
1544 -- We now need to do an unchecked conversion of the
1545 -- result to the target type, but it is important that
1546 -- this conversion be a right justified conversion and
1547 -- not a left justified conversion.
1549 Rhs
:= RJ_Unchecked_Convert_To
(Etype
(Obj
), Rhs
);
1555 and then Compile_Time_Known_Value
(Get_Shift
)
1558 Make_Integer_Literal
(Loc
,
1559 Rhs_Val
* (2 ** Expr_Value
(Get_Shift
)));
1560 Set_Print_In_Hex
(Or_Rhs
);
1563 -- We have to convert the right hand side to Etype (Obj).
1564 -- A special case arises if what we have now is a Val
1565 -- attribute reference whose expression type is Etype (Obj).
1566 -- This happens for assignments of fields from the same
1567 -- array. In this case we get the required right hand side
1568 -- by simply removing the inner attribute reference.
1570 if Nkind
(Rhs
) = N_Attribute_Reference
1571 and then Attribute_Name
(Rhs
) = Name_Val
1572 and then Etype
(First
(Expressions
(Rhs
))) = Etype
(Obj
)
1574 Rhs
:= Relocate_Node
(First
(Expressions
(Rhs
)));
1577 -- If the value of the right hand side is a known integer
1578 -- value, then just replace it by an untyped constant,
1579 -- which will be properly retyped when we analyze and
1580 -- resolve the expression.
1582 elsif Rhs_Val_Known
then
1584 -- Note that Rhs_Val has already been normalized to
1585 -- be an unsigned value with the proper number of bits.
1588 Make_Integer_Literal
(Loc
, Rhs_Val
);
1590 -- Otherwise we need an unchecked conversion
1596 Or_Rhs
:= Make_Shift_Left
(Rhs
, Get_Shift
);
1599 if Nkind
(New_Rhs
) = N_Op_And
then
1600 Set_Paren_Count
(New_Rhs
, 1);
1605 Left_Opnd
=> New_Rhs
,
1606 Right_Opnd
=> Or_Rhs
);
1610 -- Now do the rewrite
1613 Make_Assignment_Statement
(Loc
,
1616 Unchecked_Convert_To
(Etype
(New_Lhs
), New_Rhs
)));
1617 Set_Assignment_OK
(Name
(N
), Ass_OK
);
1619 -- All other component sizes for non-modular case
1624 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1626 -- where Subscr is the computed linear subscript
1629 Bits_nn
: constant Entity_Id
:= RTE
(Bits_Id
(Csiz
));
1635 if No
(Bits_nn
) then
1637 -- Error, most likely High_Integrity_Mode restriction
1642 -- Acquire proper Set entity. We use the aligned or unaligned
1643 -- case as appropriate.
1645 if Known_Aligned_Enough
(Obj
, Csiz
) then
1646 Set_nn
:= RTE
(Set_Id
(Csiz
));
1648 Set_nn
:= RTE
(SetU_Id
(Csiz
));
1651 -- Now generate the set reference
1653 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1654 Convert_To_Actual_Subtype
(Obj
);
1655 Atyp
:= Etype
(Obj
);
1656 Compute_Linear_Subscript
(Atyp
, Lhs
, Subscr
);
1658 -- Below we must make the assumption that Obj is
1659 -- at least byte aligned, since otherwise its address
1660 -- cannot be taken. The assumption holds since the
1661 -- only arrays that can be misaligned are small packed
1662 -- arrays which are implemented as a modular type, and
1663 -- that is not the case here.
1666 Make_Procedure_Call_Statement
(Loc
,
1667 Name
=> New_Occurrence_Of
(Set_nn
, Loc
),
1668 Parameter_Associations
=> New_List
(
1669 Make_Attribute_Reference
(Loc
,
1671 Attribute_Name
=> Name_Address
),
1673 Unchecked_Convert_To
(Bits_nn
,
1674 Convert_To
(Ctyp
, Rhs
)))));
1679 Analyze
(N
, Suppress
=> All_Checks
);
1680 end Expand_Bit_Packed_Element_Set
;
1682 -------------------------------------
1683 -- Expand_Packed_Address_Reference --
1684 -------------------------------------
1686 procedure Expand_Packed_Address_Reference
(N
: Node_Id
) is
1687 Loc
: constant Source_Ptr
:= Sloc
(N
);
1692 -- We build an expression that has the form
1694 -- outer_object'Address
1695 -- + (linear-subscript * component_size for each array reference
1696 -- + field'Bit_Position for each record field
1698 -- + ...) / Storage_Unit;
1700 Get_Base_And_Bit_Offset
(Prefix
(N
), Base
, Offset
);
1703 Unchecked_Convert_To
(RTE
(RE_Address
),
1706 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1707 Make_Attribute_Reference
(Loc
,
1709 Attribute_Name
=> Name_Address
)),
1712 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1713 Make_Op_Divide
(Loc
,
1714 Left_Opnd
=> Offset
,
1716 Make_Integer_Literal
(Loc
, System_Storage_Unit
))))));
1718 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1719 end Expand_Packed_Address_Reference
;
1721 ---------------------------------
1722 -- Expand_Packed_Bit_Reference --
1723 ---------------------------------
1725 procedure Expand_Packed_Bit_Reference
(N
: Node_Id
) is
1726 Loc
: constant Source_Ptr
:= Sloc
(N
);
1731 -- We build an expression that has the form
1733 -- (linear-subscript * component_size for each array reference
1734 -- + field'Bit_Position for each record field
1736 -- + ...) mod Storage_Unit;
1738 Get_Base_And_Bit_Offset
(Prefix
(N
), Base
, Offset
);
1741 Unchecked_Convert_To
(Universal_Integer
,
1743 Left_Opnd
=> Offset
,
1744 Right_Opnd
=> Make_Integer_Literal
(Loc
, System_Storage_Unit
))));
1746 Analyze_And_Resolve
(N
, Universal_Integer
);
1747 end Expand_Packed_Bit_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.Bit_Ops.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
:= Make_Temporary
(Loc
, 'T');
1849 if Nkind
(N
) = N_Op_And
then
1852 elsif Nkind
(N
) = N_Op_Or
then
1855 else -- Nkind (N) = N_Op_Xor
1859 Insert_Actions
(N
, New_List
(
1861 Make_Object_Declaration
(Loc
,
1862 Defining_Identifier
=> Result_Ent
,
1863 Object_Definition
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1865 Make_Procedure_Call_Statement
(Loc
,
1866 Name
=> New_Occurrence_Of
(RTE
(E_Id
), Loc
),
1867 Parameter_Associations
=> New_List
(
1869 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1871 Attribute_Name
=> Name_Address
),
1873 Make_Op_Multiply
(Loc
,
1875 Make_Attribute_Reference
(Loc
,
1878 (Etype
(First_Index
(Ltyp
)), Loc
),
1879 Attribute_Name
=> Name_Range_Length
),
1882 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
))),
1884 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1886 Attribute_Name
=> Name_Address
),
1888 Make_Op_Multiply
(Loc
,
1890 Make_Attribute_Reference
(Loc
,
1893 (Etype
(First_Index
(Rtyp
)), Loc
),
1894 Attribute_Name
=> Name_Range_Length
),
1897 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
1899 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1900 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
1901 Attribute_Name
=> Name_Address
)))));
1904 New_Occurrence_Of
(Result_Ent
, Loc
));
1908 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
1909 end Expand_Packed_Boolean_Operator
;
1911 -------------------------------------
1912 -- Expand_Packed_Element_Reference --
1913 -------------------------------------
1915 procedure Expand_Packed_Element_Reference
(N
: Node_Id
) is
1916 Loc
: constant Source_Ptr
:= Sloc
(N
);
1928 -- If not bit packed, we have the enumeration case, which is easily
1929 -- dealt with (just adjust the subscripts of the indexed component)
1931 -- Note: this leaves the result as an indexed component, which is
1932 -- still a variable, so can be used in the assignment case, as is
1933 -- required in the enumeration case.
1935 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
1936 Setup_Enumeration_Packed_Array_Reference
(N
);
1940 -- Remaining processing is for the bit-packed case
1942 Obj
:= Relocate_Node
(Prefix
(N
));
1943 Convert_To_Actual_Subtype
(Obj
);
1944 Atyp
:= Etype
(Obj
);
1945 PAT
:= Packed_Array_Type
(Atyp
);
1946 Ctyp
:= Component_Type
(Atyp
);
1947 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1949 -- For the AAMP target, indexing of certain packed array is passed
1950 -- through to the back end without expansion, because the expansion
1951 -- results in very inefficient code on that target. This allows the
1952 -- GNAAMP back end to generate specialized macros that support more
1953 -- efficient indexing of packed arrays with components having sizes
1954 -- that are small powers of two.
1957 and then (Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4)
1962 -- Case of component size 1,2,4 or any component size for the modular
1963 -- case. These are the cases for which we can inline the code.
1965 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1966 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1968 Setup_Inline_Packed_Array_Reference
(N
, Atyp
, Obj
, Cmask
, Shift
);
1969 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1970 Set_Print_In_Hex
(Lit
);
1972 -- We generate a shift right to position the field, followed by a
1973 -- masking operation to extract the bit field, and we finally do an
1974 -- unchecked conversion to convert the result to the required target.
1976 -- Note that the unchecked conversion automatically deals with the
1977 -- bias if we are dealing with a biased representation. What will
1978 -- happen is that we temporarily generate the biased representation,
1979 -- but almost immediately that will be converted to the original
1980 -- unbiased component type, and the bias will disappear.
1984 Left_Opnd
=> Make_Shift_Right
(Obj
, Shift
),
1987 -- We needed to analyze this before we do the unchecked convert
1988 -- below, but we need it temporarily attached to the tree for
1989 -- this analysis (hence the temporary Set_Parent call).
1991 Set_Parent
(Arg
, Parent
(N
));
1992 Analyze_And_Resolve
(Arg
);
1995 RJ_Unchecked_Convert_To
(Ctyp
, Arg
));
1997 -- All other component sizes for non-modular case
2002 -- Component_Type!(Get_nn (Arr'address, Subscr))
2004 -- where Subscr is the computed linear subscript
2011 -- Acquire proper Get entity. We use the aligned or unaligned
2012 -- case as appropriate.
2014 if Known_Aligned_Enough
(Obj
, Csiz
) then
2015 Get_nn
:= RTE
(Get_Id
(Csiz
));
2017 Get_nn
:= RTE
(GetU_Id
(Csiz
));
2020 -- Now generate the get reference
2022 Compute_Linear_Subscript
(Atyp
, N
, Subscr
);
2024 -- Below we make the assumption that Obj is at least byte
2025 -- aligned, since otherwise its address cannot be taken.
2026 -- The assumption holds since the only arrays that can be
2027 -- misaligned are small packed arrays which are implemented
2028 -- as a modular type, and that is not the case here.
2031 Unchecked_Convert_To
(Ctyp
,
2032 Make_Function_Call
(Loc
,
2033 Name
=> New_Occurrence_Of
(Get_nn
, Loc
),
2034 Parameter_Associations
=> New_List
(
2035 Make_Attribute_Reference
(Loc
,
2037 Attribute_Name
=> Name_Address
),
2042 Analyze_And_Resolve
(N
, Ctyp
, Suppress
=> All_Checks
);
2044 end Expand_Packed_Element_Reference
;
2046 ----------------------
2047 -- Expand_Packed_Eq --
2048 ----------------------
2050 -- Handles expansion of "=" on packed array types
2052 procedure Expand_Packed_Eq
(N
: Node_Id
) is
2053 Loc
: constant Source_Ptr
:= Sloc
(N
);
2054 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
2055 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2065 Convert_To_Actual_Subtype
(L
);
2066 Convert_To_Actual_Subtype
(R
);
2067 Ltyp
:= Underlying_Type
(Etype
(L
));
2068 Rtyp
:= Underlying_Type
(Etype
(R
));
2070 Convert_To_PAT_Type
(L
);
2071 Convert_To_PAT_Type
(R
);
2075 Make_Op_Multiply
(Loc
,
2077 Make_Attribute_Reference
(Loc
,
2078 Prefix
=> New_Occurrence_Of
(Ltyp
, Loc
),
2079 Attribute_Name
=> Name_Length
),
2081 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
)));
2084 Make_Op_Multiply
(Loc
,
2086 Make_Attribute_Reference
(Loc
,
2087 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
),
2088 Attribute_Name
=> Name_Length
),
2090 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
)));
2092 -- For the modular case, we transform the comparison to:
2094 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2096 -- where PAT is the packed array type. This works fine, since in the
2097 -- modular case we guarantee that the unused bits are always zeroes.
2098 -- We do have to compare the lengths because we could be comparing
2099 -- two different subtypes of the same base type.
2101 if Is_Modular_Integer_Type
(PAT
) then
2106 Left_Opnd
=> LLexpr
,
2107 Right_Opnd
=> RLexpr
),
2114 -- For the non-modular case, we call a runtime routine
2116 -- System.Bit_Ops.Bit_Eq
2117 -- (L'Address, L_Length, R'Address, R_Length)
2119 -- where PAT is the packed array type, and the lengths are the lengths
2120 -- in bits of the original packed arrays. This routine takes care of
2121 -- not comparing the unused bits in the last byte.
2125 Make_Function_Call
(Loc
,
2126 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Eq
), Loc
),
2127 Parameter_Associations
=> New_List
(
2128 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2130 Attribute_Name
=> Name_Address
),
2134 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2136 Attribute_Name
=> Name_Address
),
2141 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
2142 end Expand_Packed_Eq
;
2144 -----------------------
2145 -- Expand_Packed_Not --
2146 -----------------------
2148 -- Handles expansion of "not" on packed array types
2150 procedure Expand_Packed_Not
(N
: Node_Id
) is
2151 Loc
: constant Source_Ptr
:= Sloc
(N
);
2152 Typ
: constant Entity_Id
:= Etype
(N
);
2153 Opnd
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
2160 Convert_To_Actual_Subtype
(Opnd
);
2161 Rtyp
:= Etype
(Opnd
);
2163 -- Deal with silly False..False and True..True subtype case
2165 Silly_Boolean_Array_Not_Test
(N
, Rtyp
);
2167 -- Now that the silliness is taken care of, get packed array type
2169 Convert_To_PAT_Type
(Opnd
);
2170 PAT
:= Etype
(Opnd
);
2172 -- For the case where the packed array type is a modular type,
2173 -- not A expands simply into:
2175 -- rtyp!(PAT!(A) xor mask)
2177 -- where PAT is the packed array type, and mask is a mask of all
2178 -- one bits of length equal to the size of this packed type and
2179 -- rtyp is the actual subtype of the operand
2181 Lit
:= Make_Integer_Literal
(Loc
, 2 ** RM_Size
(PAT
) - 1);
2182 Set_Print_In_Hex
(Lit
);
2184 if not Is_Array_Type
(PAT
) then
2186 Unchecked_Convert_To
(Rtyp
,
2189 Right_Opnd
=> Lit
)));
2191 -- For the array case, we insert the actions
2195 -- System.Bit_Ops.Bit_Not
2197 -- Typ'Length * Typ'Component_Size;
2200 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2201 -- argument is the length of the operand in bits. Then we replace
2202 -- the expression by a reference to Result.
2206 Result_Ent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T');
2209 Insert_Actions
(N
, New_List
(
2211 Make_Object_Declaration
(Loc
,
2212 Defining_Identifier
=> Result_Ent
,
2213 Object_Definition
=> New_Occurrence_Of
(Rtyp
, Loc
)),
2215 Make_Procedure_Call_Statement
(Loc
,
2216 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Not
), Loc
),
2217 Parameter_Associations
=> New_List
(
2219 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2221 Attribute_Name
=> Name_Address
),
2223 Make_Op_Multiply
(Loc
,
2225 Make_Attribute_Reference
(Loc
,
2228 (Etype
(First_Index
(Rtyp
)), Loc
),
2229 Attribute_Name
=> Name_Range_Length
),
2232 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2234 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2235 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
),
2236 Attribute_Name
=> Name_Address
)))));
2239 New_Occurrence_Of
(Result_Ent
, Loc
));
2243 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2245 end Expand_Packed_Not
;
2247 -----------------------------
2248 -- Get_Base_And_Bit_Offset --
2249 -----------------------------
2251 procedure Get_Base_And_Bit_Offset
2254 Offset
: out Node_Id
)
2265 -- We build up an expression serially that has the form
2267 -- linear-subscript * component_size for each array reference
2268 -- + field'Bit_Position for each record field
2274 if Nkind
(Base
) = N_Indexed_Component
then
2275 Convert_To_Actual_Subtype
(Prefix
(Base
));
2276 Atyp
:= Etype
(Prefix
(Base
));
2277 Compute_Linear_Subscript
(Atyp
, Base
, Subscr
);
2280 Make_Op_Multiply
(Loc
,
2281 Left_Opnd
=> Subscr
,
2283 Make_Attribute_Reference
(Loc
,
2284 Prefix
=> New_Occurrence_Of
(Atyp
, Loc
),
2285 Attribute_Name
=> Name_Component_Size
));
2287 elsif Nkind
(Base
) = N_Selected_Component
then
2289 Make_Attribute_Reference
(Loc
,
2290 Prefix
=> Selector_Name
(Base
),
2291 Attribute_Name
=> Name_Bit_Position
);
2303 Left_Opnd
=> Offset
,
2304 Right_Opnd
=> Term
);
2307 Base
:= Prefix
(Base
);
2309 end Get_Base_And_Bit_Offset
;
2311 -------------------------------------
2312 -- Involves_Packed_Array_Reference --
2313 -------------------------------------
2315 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean is
2317 if Nkind
(N
) = N_Indexed_Component
2318 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
2322 elsif Nkind
(N
) = N_Selected_Component
then
2323 return Involves_Packed_Array_Reference
(Prefix
(N
));
2328 end Involves_Packed_Array_Reference
;
2330 --------------------------
2331 -- Known_Aligned_Enough --
2332 --------------------------
2334 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean is
2335 Typ
: constant Entity_Id
:= Etype
(Obj
);
2337 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean;
2338 -- If the component is in a record that contains previous packed
2339 -- components, consider it unaligned because the back-end might
2340 -- choose to pack the rest of the record. Lead to less efficient code,
2341 -- but safer vis-a-vis of back-end choices.
2343 --------------------------------
2344 -- In_Partially_Packed_Record --
2345 --------------------------------
2347 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean is
2348 Rec_Type
: constant Entity_Id
:= Scope
(Comp
);
2349 Prev_Comp
: Entity_Id
;
2352 Prev_Comp
:= First_Entity
(Rec_Type
);
2353 while Present
(Prev_Comp
) loop
2354 if Is_Packed
(Etype
(Prev_Comp
)) then
2357 elsif Prev_Comp
= Comp
then
2361 Next_Entity
(Prev_Comp
);
2365 end In_Partially_Packed_Record
;
2367 -- Start of processing for Known_Aligned_Enough
2370 -- Odd bit sizes don't need alignment anyway
2372 if Csiz
mod 2 = 1 then
2375 -- If we have a specified alignment, see if it is sufficient, if not
2376 -- then we can't possibly be aligned enough in any case.
2378 elsif Known_Alignment
(Etype
(Obj
)) then
2379 -- Alignment required is 4 if size is a multiple of 4, and
2380 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2382 if Alignment
(Etype
(Obj
)) < 4 - (Csiz
mod 4) then
2387 -- OK, alignment should be sufficient, if object is aligned
2389 -- If object is strictly aligned, then it is definitely aligned
2391 if Strict_Alignment
(Typ
) then
2394 -- Case of subscripted array reference
2396 elsif Nkind
(Obj
) = N_Indexed_Component
then
2398 -- If we have a pointer to an array, then this is definitely
2399 -- aligned, because pointers always point to aligned versions.
2401 if Is_Access_Type
(Etype
(Prefix
(Obj
))) then
2404 -- Otherwise, go look at the prefix
2407 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2410 -- Case of record field
2412 elsif Nkind
(Obj
) = N_Selected_Component
then
2414 -- What is significant here is whether the record type is packed
2416 if Is_Record_Type
(Etype
(Prefix
(Obj
)))
2417 and then Is_Packed
(Etype
(Prefix
(Obj
)))
2421 -- Or the component has a component clause which might cause
2422 -- the component to become unaligned (we can't tell if the
2423 -- backend is doing alignment computations).
2425 elsif Present
(Component_Clause
(Entity
(Selector_Name
(Obj
)))) then
2428 elsif In_Partially_Packed_Record
(Entity
(Selector_Name
(Obj
))) then
2431 -- In all other cases, go look at prefix
2434 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2437 elsif Nkind
(Obj
) = N_Type_Conversion
then
2438 return Known_Aligned_Enough
(Expression
(Obj
), Csiz
);
2440 -- For a formal parameter, it is safer to assume that it is not
2441 -- aligned, because the formal may be unconstrained while the actual
2442 -- is constrained. In this situation, a small constrained packed
2443 -- array, represented in modular form, may be unaligned.
2445 elsif Is_Entity_Name
(Obj
) then
2446 return not Is_Formal
(Entity
(Obj
));
2449 -- If none of the above, must be aligned
2452 end Known_Aligned_Enough
;
2454 ---------------------
2455 -- Make_Shift_Left --
2456 ---------------------
2458 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2462 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2466 Make_Op_Shift_Left
(Sloc
(N
),
2469 Set_Shift_Count_OK
(Nod
, True);
2472 end Make_Shift_Left
;
2474 ----------------------
2475 -- Make_Shift_Right --
2476 ----------------------
2478 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2482 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2486 Make_Op_Shift_Right
(Sloc
(N
),
2489 Set_Shift_Count_OK
(Nod
, True);
2492 end Make_Shift_Right
;
2494 -----------------------------
2495 -- RJ_Unchecked_Convert_To --
2496 -----------------------------
2498 function RJ_Unchecked_Convert_To
2500 Expr
: Node_Id
) return Node_Id
2502 Source_Typ
: constant Entity_Id
:= Etype
(Expr
);
2503 Target_Typ
: constant Entity_Id
:= Typ
;
2505 Src
: Node_Id
:= Expr
;
2511 Source_Siz
:= UI_To_Int
(RM_Size
(Source_Typ
));
2512 Target_Siz
:= UI_To_Int
(RM_Size
(Target_Typ
));
2514 -- First step, if the source type is not a discrete type, then we
2515 -- first convert to a modular type of the source length, since
2516 -- otherwise, on a big-endian machine, we get left-justification.
2517 -- We do it for little-endian machines as well, because there might
2518 -- be junk bits that are not cleared if the type is not numeric.
2520 if Source_Siz
/= Target_Siz
2521 and then not Is_Discrete_Type
(Source_Typ
)
2523 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Source_Siz
)), Src
);
2526 -- In the big endian case, if the lengths of the two types differ,
2527 -- then we must worry about possible left justification in the
2528 -- conversion, and avoiding that is what this is all about.
2530 if Bytes_Big_Endian
and then Source_Siz
/= Target_Siz
then
2532 -- Next step. If the target is not a discrete type, then we first
2533 -- convert to a modular type of the target length, since
2534 -- otherwise, on a big-endian machine, we get left-justification.
2536 if not Is_Discrete_Type
(Target_Typ
) then
2537 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Target_Siz
)), Src
);
2541 -- And now we can do the final conversion to the target type
2543 return Unchecked_Convert_To
(Target_Typ
, Src
);
2544 end RJ_Unchecked_Convert_To
;
2546 ----------------------------------------------
2547 -- Setup_Enumeration_Packed_Array_Reference --
2548 ----------------------------------------------
2550 -- All we have to do here is to find the subscripts that correspond
2551 -- to the index positions that have non-standard enumeration types
2552 -- and insert a Pos attribute to get the proper subscript value.
2554 -- Finally the prefix must be uncheck converted to the corresponding
2555 -- packed array type.
2557 -- Note that the component type is unchanged, so we do not need to
2558 -- fiddle with the types (Gigi always automatically takes the packed
2559 -- array type if it is set, as it will be in this case).
2561 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
) is
2562 Pfx
: constant Node_Id
:= Prefix
(N
);
2563 Typ
: constant Entity_Id
:= Etype
(N
);
2564 Exprs
: constant List_Id
:= Expressions
(N
);
2568 -- If the array is unconstrained, then we replace the array
2569 -- reference with its actual subtype. This actual subtype will
2570 -- have a packed array type with appropriate bounds.
2572 if not Is_Constrained
(Packed_Array_Type
(Etype
(Pfx
))) then
2573 Convert_To_Actual_Subtype
(Pfx
);
2576 Expr
:= First
(Exprs
);
2577 while Present
(Expr
) loop
2579 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2580 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
2583 if Is_Enumeration_Type
(Expr_Typ
)
2584 and then Has_Non_Standard_Rep
(Expr_Typ
)
2587 Make_Attribute_Reference
(Loc
,
2588 Prefix
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
2589 Attribute_Name
=> Name_Pos
,
2590 Expressions
=> New_List
(Relocate_Node
(Expr
))));
2591 Analyze_And_Resolve
(Expr
, Standard_Natural
);
2599 Make_Indexed_Component
(Sloc
(N
),
2601 Unchecked_Convert_To
(Packed_Array_Type
(Etype
(Pfx
)), Pfx
),
2602 Expressions
=> Exprs
));
2604 Analyze_And_Resolve
(N
, Typ
);
2606 end Setup_Enumeration_Packed_Array_Reference
;
2608 -----------------------------------------
2609 -- Setup_Inline_Packed_Array_Reference --
2610 -----------------------------------------
2612 procedure Setup_Inline_Packed_Array_Reference
2615 Obj
: in out Node_Id
;
2617 Shift
: out Node_Id
)
2619 Loc
: constant Source_Ptr
:= Sloc
(N
);
2626 Csiz
:= Component_Size
(Atyp
);
2628 Convert_To_PAT_Type
(Obj
);
2631 Cmask
:= 2 ** Csiz
- 1;
2633 if Is_Array_Type
(PAT
) then
2634 Otyp
:= Component_Type
(PAT
);
2635 Osiz
:= Component_Size
(PAT
);
2640 -- In the case where the PAT is a modular type, we want the actual
2641 -- size in bits of the modular value we use. This is neither the
2642 -- Object_Size nor the Value_Size, either of which may have been
2643 -- reset to strange values, but rather the minimum size. Note that
2644 -- since this is a modular type with full range, the issue of
2645 -- biased representation does not arise.
2647 Osiz
:= UI_From_Int
(Minimum_Size
(Otyp
));
2650 Compute_Linear_Subscript
(Atyp
, N
, Shift
);
2652 -- If the component size is not 1, then the subscript must be
2653 -- multiplied by the component size to get the shift count.
2657 Make_Op_Multiply
(Loc
,
2658 Left_Opnd
=> Make_Integer_Literal
(Loc
, Csiz
),
2659 Right_Opnd
=> Shift
);
2662 -- If we have the array case, then this shift count must be broken
2663 -- down into a byte subscript, and a shift within the byte.
2665 if Is_Array_Type
(PAT
) then
2668 New_Shift
: Node_Id
;
2671 -- We must analyze shift, since we will duplicate it
2673 Set_Parent
(Shift
, N
);
2675 (Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2677 -- The shift count within the word is
2682 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2683 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
));
2685 -- The subscript to be used on the PAT array is
2689 Make_Indexed_Component
(Loc
,
2691 Expressions
=> New_List
(
2692 Make_Op_Divide
(Loc
,
2693 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2694 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
))));
2699 -- For the modular integer case, the object to be manipulated is
2700 -- the entire array, so Obj is unchanged. Note that we will reset
2701 -- its type to PAT before returning to the caller.
2707 -- The one remaining step is to modify the shift count for the
2708 -- big-endian case. Consider the following example in a byte:
2710 -- xxxxxxxx bits of byte
2711 -- vvvvvvvv bits of value
2712 -- 33221100 little-endian numbering
2713 -- 00112233 big-endian numbering
2715 -- Here we have the case of 2-bit fields
2717 -- For the little-endian case, we already have the proper shift
2718 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2720 -- For the big endian case, we have to adjust the shift count,
2721 -- computing it as (N - F) - shift, where N is the number of bits
2722 -- in an element of the array used to implement the packed array,
2723 -- F is the number of bits in a source level array element, and
2724 -- shift is the count so far computed.
2726 if Bytes_Big_Endian
then
2728 Make_Op_Subtract
(Loc
,
2729 Left_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
- Csiz
),
2730 Right_Opnd
=> Shift
);
2733 Set_Parent
(Shift
, N
);
2734 Set_Parent
(Obj
, N
);
2735 Analyze_And_Resolve
(Obj
, Otyp
, Suppress
=> All_Checks
);
2736 Analyze_And_Resolve
(Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2738 -- Make sure final type of object is the appropriate packed type
2740 Set_Etype
(Obj
, Otyp
);
2742 end Setup_Inline_Packed_Array_Reference
;