1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2002 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Einfo
; use Einfo
;
30 with Exp_Dbug
; use Exp_Dbug
;
31 with Exp_Util
; use Exp_Util
;
32 with Nlists
; use Nlists
;
33 with Nmake
; use Nmake
;
35 with Rtsfind
; use Rtsfind
;
37 with Sem_Ch8
; use Sem_Ch8
;
38 with Sem_Ch13
; use Sem_Ch13
;
39 with Sem_Eval
; use Sem_Eval
;
40 with Sem_Res
; use Sem_Res
;
41 with Sem_Util
; use Sem_Util
;
42 with Sinfo
; use Sinfo
;
43 with Snames
; use Snames
;
44 with Stand
; use Stand
;
45 with Targparm
; use Targparm
;
46 with Tbuild
; use Tbuild
;
47 with Ttypes
; use Ttypes
;
48 with Uintp
; use Uintp
;
50 package body Exp_Pakd
is
52 ---------------------------
53 -- Endian Considerations --
54 ---------------------------
56 -- As described in the specification, bit numbering in a packed array
57 -- is consistent with bit numbering in a record representation clause,
58 -- and hence dependent on the endianness of the machine:
60 -- For little-endian machines, element zero is at the right hand end
61 -- (low order end) of a bit field.
63 -- For big-endian machines, element zero is at the left hand end
64 -- (high order end) of a bit field.
66 -- The shifts that are used to right justify a field therefore differ
67 -- in the two cases. For the little-endian case, we can simply use the
68 -- bit number (i.e. the element number * element size) as the count for
69 -- a right shift. For the big-endian case, we have to subtract the shift
70 -- count from an appropriate constant to use in the right shift. We use
71 -- rotates instead of shifts (which is necessary in the store case to
72 -- preserve other fields), and we expect that the backend will be able
73 -- to change the right rotate into a left rotate, avoiding the subtract,
74 -- if the architecture provides such an instruction.
76 ----------------------------------------------
77 -- Entity Tables for Packed Access Routines --
78 ----------------------------------------------
80 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
81 -- library routines. This table is used to obtain the entity for the
84 type E_Array
is array (Int
range 01 .. 63) of RE_Id
;
86 -- Array of Bits_nn entities. Note that we do not use library routines
87 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
88 -- entries from System.Unsigned, because we also use this table for
89 -- certain special unchecked conversions in the big-endian case.
91 Bits_Id
: constant E_Array
:=
107 16 => RE_Unsigned_16
,
123 32 => RE_Unsigned_32
,
156 -- Array of Get routine entities. These are used to obtain an element
157 -- from a packed array. The N'th entry is used to obtain elements from
158 -- a packed array whose component size is N. RE_Null is used as a null
159 -- entry, for the cases where a library routine is not used.
161 Get_Id
: constant E_Array
:=
226 -- Array of Get routine entities to be used in the case where the packed
227 -- array is itself a component of a packed structure, and therefore may
228 -- not be fully aligned. This only affects the even sizes, since for the
229 -- odd sizes, we do not get any fixed alignment in any case.
231 GetU_Id
: constant E_Array
:=
296 -- Array of Set routine entities. These are used to assign an element
297 -- of a packed array. The N'th entry is used to assign elements for
298 -- a packed array whose component size is N. RE_Null is used as a null
299 -- entry, for the cases where a library routine is not used.
366 -- Array of Set routine entities to be used in the case where the packed
367 -- array is itself a component of a packed structure, and therefore may
368 -- not be fully aligned. This only affects the even sizes, since for the
369 -- odd sizes, we do not get any fixed alignment in any case.
436 -----------------------
437 -- Local Subprograms --
438 -----------------------
440 procedure Compute_Linear_Subscript
443 Subscr
: out Node_Id
);
444 -- Given a constrained array type Atyp, and an indexed component node
445 -- N referencing an array object of this type, build an expression of
446 -- type Standard.Integer representing the zero-based linear subscript
447 -- value. This expression includes any required range checks.
449 procedure Convert_To_PAT_Type
(Aexp
: Node_Id
);
450 -- Given an expression of a packed array type, builds a corresponding
451 -- expression whose type is the implementation type used to represent
452 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
454 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean;
455 -- There are two versions of the Set routines, the ones used when the
456 -- object is known to be sufficiently well aligned given the number of
457 -- bits, and the ones used when the object is not known to be aligned.
458 -- This routine is used to determine which set to use. Obj is a reference
459 -- to the object, and Csiz is the component size of the packed array.
460 -- True is returned if the alignment of object is known to be sufficient,
461 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
464 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
465 -- Build a left shift node, checking for the case of a shift count of zero
467 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
;
468 -- Build a right shift node, checking for the case of a shift count of zero
470 function RJ_Unchecked_Convert_To
474 -- The packed array code does unchecked conversions which in some cases
475 -- may involve non-discrete types with differing sizes. The semantics of
476 -- such conversions is potentially endian dependent, and the effect we
477 -- want here for such a conversion is to do the conversion in size as
478 -- though numeric items are involved, and we extend or truncate on the
479 -- left side. This happens naturally in the little-endian case, but in
480 -- the big endian case we can get left justification, when what we want
481 -- is right justification. This routine does the unchecked conversion in
482 -- a stepwise manner to ensure that it gives the expected result. Hence
483 -- the name (RJ = Right justified). The parameters Typ and Expr are as
484 -- for the case of a normal Unchecked_Convert_To call.
486 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
);
487 -- This routine is called in the Get and Set case for arrays that are
488 -- packed but not bit-packed, meaning that they have at least one
489 -- subscript that is of an enumeration type with a non-standard
490 -- representation. This routine modifies the given node to properly
491 -- reference the corresponding packed array type.
493 procedure Setup_Inline_Packed_Array_Reference
496 Obj
: in out Node_Id
;
498 Shift
: out Node_Id
);
499 -- This procedure performs common processing on the N_Indexed_Component
500 -- parameter given as N, whose prefix is a reference to a packed array.
501 -- This is used for the get and set when the component size is 1,2,4
502 -- or for other component sizes when the packed array type is a modular
503 -- type (i.e. the cases that are handled with inline code).
507 -- N is the N_Indexed_Component node for the packed array reference
509 -- Atyp is the constrained array type (the actual subtype has been
510 -- computed if necessary to obtain the constraints, but this is still
511 -- the original array type, not the Packed_Array_Type value).
513 -- Obj is the object which is to be indexed. It is always of type Atyp.
517 -- Obj is the object containing the desired bit field. It is of type
518 -- Unsigned or Long_Long_Unsigned, and is either the entire value,
519 -- for the small static case, or the proper selected byte from the
520 -- array in the large or dynamic case. This node is analyzed and
521 -- resolved on return.
523 -- Shift is a node representing the shift count to be used in the
524 -- rotate right instruction that positions the field for access.
525 -- This node is analyzed and resolved on return.
527 -- Cmask is a mask corresponding to the width of the component field.
528 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
530 -- Note: in some cases the call to this routine may generate actions
531 -- (for handling multi-use references and the generation of the packed
532 -- array type on the fly). Such actions are inserted into the tree
533 -- directly using Insert_Action.
535 ------------------------------
536 -- Compute_Linear_Subcsript --
537 ------------------------------
539 procedure Compute_Linear_Subscript
542 Subscr
: out Node_Id
)
544 Loc
: constant Source_Ptr
:= Sloc
(N
);
553 -- Loop through dimensions
555 Indx
:= First_Index
(Atyp
);
556 Oldsub
:= First
(Expressions
(N
));
558 while Present
(Indx
) loop
559 Styp
:= Etype
(Indx
);
560 Newsub
:= Relocate_Node
(Oldsub
);
562 -- Get expression for the subscript value. First, if Do_Range_Check
563 -- is set on a subscript, then we must do a range check against the
564 -- original bounds (not the bounds of the packed array type). We do
565 -- this by introducing a subtype conversion.
567 if Do_Range_Check
(Newsub
)
568 and then Etype
(Newsub
) /= Styp
570 Newsub
:= Convert_To
(Styp
, Newsub
);
573 -- Now evolve the expression for the subscript. First convert
574 -- the subscript to be zero based and of an integer type.
576 -- Case of integer type, where we just subtract to get lower bound
578 if Is_Integer_Type
(Styp
) then
580 -- If length of integer type is smaller than standard integer,
581 -- then we convert to integer first, then do the subtract
583 -- Integer (subscript) - Integer (Styp'First)
585 if Esize
(Styp
) < Esize
(Standard_Integer
) then
587 Make_Op_Subtract
(Loc
,
588 Left_Opnd
=> Convert_To
(Standard_Integer
, Newsub
),
590 Convert_To
(Standard_Integer
,
591 Make_Attribute_Reference
(Loc
,
592 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
593 Attribute_Name
=> Name_First
)));
595 -- For larger integer types, subtract first, then convert to
596 -- integer, this deals with strange long long integer bounds.
598 -- Integer (subscript - Styp'First)
602 Convert_To
(Standard_Integer
,
603 Make_Op_Subtract
(Loc
,
606 Make_Attribute_Reference
(Loc
,
607 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
608 Attribute_Name
=> Name_First
)));
611 -- For the enumeration case, we have to use 'Pos to get the value
612 -- to work with before subtracting the lower bound.
614 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
616 -- This is not quite right for bizarre cases where the size of the
617 -- enumeration type is > Integer'Size bits due to rep clause ???
620 pragma Assert
(Is_Enumeration_Type
(Styp
));
623 Make_Op_Subtract
(Loc
,
624 Left_Opnd
=> Convert_To
(Standard_Integer
,
625 Make_Attribute_Reference
(Loc
,
626 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
627 Attribute_Name
=> Name_Pos
,
628 Expressions
=> New_List
(Newsub
))),
631 Convert_To
(Standard_Integer
,
632 Make_Attribute_Reference
(Loc
,
633 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
634 Attribute_Name
=> Name_Pos
,
635 Expressions
=> New_List
(
636 Make_Attribute_Reference
(Loc
,
637 Prefix
=> New_Occurrence_Of
(Styp
, Loc
),
638 Attribute_Name
=> Name_First
)))));
641 Set_Paren_Count
(Newsub
, 1);
643 -- For the first subscript, we just copy that subscript value
648 -- Otherwise, we must multiply what we already have by the current
649 -- stride and then add in the new value to the evolving subscript.
655 Make_Op_Multiply
(Loc
,
658 Make_Attribute_Reference
(Loc
,
659 Attribute_Name
=> Name_Range_Length
,
660 Prefix
=> New_Occurrence_Of
(Styp
, Loc
))),
661 Right_Opnd
=> Newsub
);
664 -- Move to next subscript
669 end Compute_Linear_Subscript
;
671 -------------------------
672 -- Convert_To_PAT_Type --
673 -------------------------
675 -- The PAT is always obtained from the actual subtype
677 procedure Convert_To_PAT_Type
(Aexp
: Entity_Id
) is
681 Convert_To_Actual_Subtype
(Aexp
);
682 Act_ST
:= Underlying_Type
(Etype
(Aexp
));
683 Create_Packed_Array_Type
(Act_ST
);
685 -- Just replace the etype with the packed array type. This works
686 -- because the expression will not be further analyzed, and Gigi
687 -- considers the two types equivalent in any case.
689 Set_Etype
(Aexp
, Packed_Array_Type
(Act_ST
));
690 end Convert_To_PAT_Type
;
692 ------------------------------
693 -- Create_Packed_Array_Type --
694 ------------------------------
696 procedure Create_Packed_Array_Type
(Typ
: Entity_Id
) is
697 Loc
: constant Source_Ptr
:= Sloc
(Typ
);
698 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
699 Csize
: constant Uint
:= Component_Size
(Typ
);
714 procedure Install_PAT
;
715 -- This procedure is called with Decl set to the declaration for the
716 -- packed array type. It creates the type and installs it as required.
718 procedure Set_PB_Type
;
719 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
720 -- requirements (see documentation in the spec of this package).
726 procedure Install_PAT
is
727 Pushed_Scope
: Boolean := False;
730 -- We do not want to put the declaration we have created in the tree
731 -- since it is often hard, and sometimes impossible to find a proper
732 -- place for it (the impossible case arises for a packed array type
733 -- with bounds depending on the discriminant, a declaration cannot
734 -- be put inside the record, and the reference to the discriminant
735 -- cannot be outside the record).
737 -- The solution is to analyze the declaration while temporarily
738 -- attached to the tree at an appropriate point, and then we install
739 -- the resulting type as an Itype in the packed array type field of
740 -- the original type, so that no explicit declaration is required.
742 -- Note: the packed type is created in the scope of its parent
743 -- type. There are at least some cases where the current scope
744 -- is deeper, and so when this is the case, we temporarily reset
745 -- the scope for the definition. This is clearly safe, since the
746 -- first use of the packed array type will be the implicit
747 -- reference from the corresponding unpacked type when it is
750 if Is_Itype
(Typ
) then
751 Set_Parent
(Decl
, Associated_Node_For_Itype
(Typ
));
753 Set_Parent
(Decl
, Declaration_Node
(Typ
));
756 if Scope
(Typ
) /= Current_Scope
then
757 New_Scope
(Scope
(Typ
));
758 Pushed_Scope
:= True;
761 Set_Is_Itype
(PAT
, True);
762 Set_Packed_Array_Type
(Typ
, PAT
);
763 Analyze
(Decl
, Suppress
=> All_Checks
);
769 -- Set Esize and RM_Size to the actual size of the packed object
770 -- Do not reset RM_Size if already set, as happens in the case
773 Set_Esize
(PAT
, Esiz
);
775 if Unknown_RM_Size
(PAT
) then
776 Set_RM_Size
(PAT
, Esiz
);
779 -- Set remaining fields of packed array type
781 Init_Alignment
(PAT
);
782 Set_Parent
(PAT
, Empty
);
783 Set_Associated_Node_For_Itype
(PAT
, Typ
);
784 Set_Is_Packed_Array_Type
(PAT
, True);
785 Set_Original_Array_Type
(PAT
, Typ
);
787 -- We definitely do not want to delay freezing for packed array
788 -- types. This is of particular importance for the itypes that
789 -- are generated for record components depending on discriminants
790 -- where there is no place to put the freeze node.
792 Set_Has_Delayed_Freeze
(PAT
, False);
793 Set_Has_Delayed_Freeze
(Etype
(PAT
), False);
800 procedure Set_PB_Type
is
802 -- If the user has specified an explicit alignment for the
803 -- type or component, take it into account.
805 if Csize
<= 2 or else Csize
= 4 or else Csize
mod 2 /= 0
806 or else Alignment
(Typ
) = 1
807 or else Component_Alignment
(Typ
) = Calign_Storage_Unit
809 PB_Type
:= RTE
(RE_Packed_Bytes1
);
811 elsif Csize
mod 4 /= 0
812 or else Alignment
(Typ
) = 2
814 PB_Type
:= RTE
(RE_Packed_Bytes2
);
817 PB_Type
:= RTE
(RE_Packed_Bytes4
);
821 -- Start of processing for Create_Packed_Array_Type
824 -- If we already have a packed array type, nothing to do
826 if Present
(Packed_Array_Type
(Typ
)) then
830 -- If our immediate ancestor subtype is constrained, and it already
831 -- has a packed array type, then just share the same type, since the
832 -- bounds must be the same.
834 if Ekind
(Typ
) = E_Array_Subtype
then
835 Ancest
:= Ancestor_Subtype
(Typ
);
838 and then Is_Constrained
(Ancest
)
839 and then Present
(Packed_Array_Type
(Ancest
))
841 Set_Packed_Array_Type
(Typ
, Packed_Array_Type
(Ancest
));
846 -- We preset the result type size from the size of the original array
847 -- type, since this size clearly belongs to the packed array type. The
848 -- size of the conceptual unpacked type is always set to unknown.
852 -- Case of an array where at least one index is of an enumeration
853 -- type with a non-standard representation, but the component size
854 -- is not appropriate for bit packing. This is the case where we
855 -- have Is_Packed set (we would never be in this unit otherwise),
856 -- but Is_Bit_Packed_Array is false.
858 -- Note that if the component size is appropriate for bit packing,
859 -- then the circuit for the computation of the subscript properly
860 -- deals with the non-standard enumeration type case by taking the
863 if not Is_Bit_Packed_Array
(Typ
) then
865 -- Here we build a declaration:
867 -- type tttP is array (index1, index2, ...) of component_type
869 -- where index1, index2, are the index types. These are the same
870 -- as the index types of the original array, except for the non-
871 -- standard representation enumeration type case, where we have
874 -- For the unconstrained array case, we use
878 -- For the constrained case, we use
880 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
881 -- Enum_Type'Pos (Enum_Type'Last);
884 Make_Defining_Identifier
(Loc
,
885 Chars
=> New_External_Name
(Chars
(Typ
), 'P'));
887 Set_Packed_Array_Type
(Typ
, PAT
);
890 Indexes
: List_Id
:= New_List
;
892 Indx_Typ
: Entity_Id
;
897 Indx
:= First_Index
(Typ
);
899 while Present
(Indx
) loop
900 Indx_Typ
:= Etype
(Indx
);
902 Enum_Case
:= Is_Enumeration_Type
(Indx_Typ
)
903 and then Has_Non_Standard_Rep
(Indx_Typ
);
905 -- Unconstrained case
907 if not Is_Constrained
(Typ
) then
909 Indx_Typ
:= Standard_Natural
;
912 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
917 if not Enum_Case
then
918 Append_To
(Indexes
, New_Occurrence_Of
(Indx_Typ
, Loc
));
922 Make_Subtype_Indication
(Loc
,
924 New_Occurrence_Of
(Standard_Natural
, Loc
),
926 Make_Range_Constraint
(Loc
,
930 Make_Attribute_Reference
(Loc
,
932 New_Occurrence_Of
(Indx_Typ
, Loc
),
933 Attribute_Name
=> Name_Pos
,
934 Expressions
=> New_List
(
935 Make_Attribute_Reference
(Loc
,
937 New_Occurrence_Of
(Indx_Typ
, Loc
),
938 Attribute_Name
=> Name_First
))),
941 Make_Attribute_Reference
(Loc
,
943 New_Occurrence_Of
(Indx_Typ
, Loc
),
944 Attribute_Name
=> Name_Pos
,
945 Expressions
=> New_List
(
946 Make_Attribute_Reference
(Loc
,
948 New_Occurrence_Of
(Indx_Typ
, Loc
),
949 Attribute_Name
=> Name_Last
)))))));
957 if not Is_Constrained
(Typ
) then
959 Make_Unconstrained_Array_Definition
(Loc
,
960 Subtype_Marks
=> Indexes
,
961 Subtype_Indication
=>
962 New_Occurrence_Of
(Ctyp
, Loc
));
966 Make_Constrained_Array_Definition
(Loc
,
967 Discrete_Subtype_Definitions
=> Indexes
,
968 Subtype_Indication
=>
969 New_Occurrence_Of
(Ctyp
, Loc
));
973 Make_Full_Type_Declaration
(Loc
,
974 Defining_Identifier
=> PAT
,
975 Type_Definition
=> Typedef
);
978 -- Set type as packed array type and install it
980 Set_Is_Packed_Array_Type
(PAT
);
984 -- Case of bit-packing required for unconstrained array. We create
985 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
987 elsif not Is_Constrained
(Typ
) then
989 Make_Defining_Identifier
(Loc
,
990 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
992 Set_Packed_Array_Type
(Typ
, PAT
);
996 Make_Subtype_Declaration
(Loc
,
997 Defining_Identifier
=> PAT
,
998 Subtype_Indication
=> New_Occurrence_Of
(PB_Type
, Loc
));
1002 -- Remaining code is for the case of bit-packing for constrained array
1004 -- The name of the packed array subtype is
1008 -- where sss is the component size in bits and ttt is the name of
1009 -- the parent packed type.
1013 Make_Defining_Identifier
(Loc
,
1014 Chars
=> Make_Packed_Array_Type_Name
(Typ
, Csize
));
1016 Set_Packed_Array_Type
(Typ
, PAT
);
1018 -- Build an expression for the length of the array in bits.
1019 -- This is the product of the length of each of the dimensions
1025 Len_Expr
:= Empty
; -- suppress junk warning
1029 Make_Attribute_Reference
(Loc
,
1030 Attribute_Name
=> Name_Length
,
1031 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
1032 Expressions
=> New_List
(
1033 Make_Integer_Literal
(Loc
, J
)));
1036 Len_Expr
:= Len_Dim
;
1040 Make_Op_Multiply
(Loc
,
1041 Left_Opnd
=> Len_Expr
,
1042 Right_Opnd
=> Len_Dim
);
1046 exit when J
> Number_Dimensions
(Typ
);
1050 -- Temporarily attach the length expression to the tree and analyze
1051 -- and resolve it, so that we can test its value. We assume that the
1052 -- total length fits in type Integer.
1054 Set_Parent
(Len_Expr
, Typ
);
1055 Analyze_And_Resolve
(Len_Expr
, Standard_Integer
);
1057 -- Use a modular type if possible. We can do this if we are we
1058 -- have static bounds, and the length is small enough, and the
1059 -- length is not zero. We exclude the zero length case because the
1060 -- size of things is always at least one, and the zero length object
1061 -- would have an anomous size
1063 if Compile_Time_Known_Value
(Len_Expr
) then
1064 Len_Bits
:= Expr_Value
(Len_Expr
) * Csize
;
1066 -- We normally consider small enough to mean no larger than the
1067 -- value of System_Max_Binary_Modulus_Power, except that in
1068 -- No_Run_Time mode, we use the Word Size on machines for
1069 -- which double length shifts are not generated in line.
1073 (Len_Bits
<= System_Word_Size
1074 or else (Len_Bits
<= System_Max_Binary_Modulus_Power
1075 and then (not No_Run_Time
1077 Long_Shifts_Inlined_On_Target
)))
1079 -- We can use the modular type, it has the form:
1081 -- subtype tttPn is btyp
1082 -- range 0 .. 2 ** (Esize (Typ) * Csize) - 1;
1084 -- Here Siz is 1, 2 or 4, as computed above, and btyp is either
1085 -- Unsigned or Long_Long_Unsigned depending on the length.
1087 if Len_Bits
<= Standard_Integer_Size
then
1088 Btyp
:= RTE
(RE_Unsigned
);
1090 Btyp
:= RTE
(RE_Long_Long_Unsigned
);
1093 Lit
:= Make_Integer_Literal
(Loc
, 2 ** Len_Bits
- 1);
1094 Set_Print_In_Hex
(Lit
);
1097 Make_Subtype_Declaration
(Loc
,
1098 Defining_Identifier
=> PAT
,
1099 Subtype_Indication
=>
1100 Make_Subtype_Indication
(Loc
,
1101 Subtype_Mark
=> New_Occurrence_Of
(Btyp
, Loc
),
1104 Make_Range_Constraint
(Loc
,
1108 Make_Integer_Literal
(Loc
, 0),
1109 High_Bound
=> Lit
))));
1111 if Esiz
= Uint_0
then
1120 -- Could not use a modular type, for all other cases, we build
1121 -- a packed array subtype:
1124 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1126 -- Bits is the length of the array in bits.
1133 Make_Op_Multiply
(Loc
,
1135 Make_Integer_Literal
(Loc
, Csize
),
1136 Right_Opnd
=> Len_Expr
),
1139 Make_Integer_Literal
(Loc
, 7));
1141 Set_Paren_Count
(Bits_U1
, 1);
1144 Make_Op_Subtract
(Loc
,
1146 Make_Op_Divide
(Loc
,
1147 Left_Opnd
=> Bits_U1
,
1148 Right_Opnd
=> Make_Integer_Literal
(Loc
, 8)),
1149 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
1152 Make_Subtype_Declaration
(Loc
,
1153 Defining_Identifier
=> PAT
,
1154 Subtype_Indication
=>
1155 Make_Subtype_Indication
(Loc
,
1156 Subtype_Mark
=> New_Occurrence_Of
(PB_Type
, Loc
),
1159 Make_Index_Or_Discriminant_Constraint
(Loc
,
1160 Constraints
=> New_List
(
1163 Make_Integer_Literal
(Loc
, 0),
1164 High_Bound
=> PAT_High
)))));
1168 end Create_Packed_Array_Type
;
1170 -----------------------------------
1171 -- Expand_Bit_Packed_Element_Set --
1172 -----------------------------------
1174 procedure Expand_Bit_Packed_Element_Set
(N
: Node_Id
) is
1175 Loc
: constant Source_Ptr
:= Sloc
(N
);
1176 Lhs
: constant Node_Id
:= Name
(N
);
1178 Ass_OK
: constant Boolean := Assignment_OK
(Lhs
);
1179 -- Used to preserve assignment OK status when assignment is rewritten
1181 Rhs
: Node_Id
:= Expression
(N
);
1182 -- Initially Rhs is the right hand side value, it will be replaced
1183 -- later by an appropriate unchecked conversion for the assignment.
1196 Rhs_Val_Known
: Boolean;
1198 -- If the value of the right hand side as an integer constant is
1199 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1200 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1201 -- the Rhs_Val is undefined.
1204 pragma Assert
(Is_Bit_Packed_Array
(Etype
(Prefix
(Lhs
))));
1206 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1207 Convert_To_Actual_Subtype
(Obj
);
1208 Atyp
:= Etype
(Obj
);
1209 PAT
:= Packed_Array_Type
(Atyp
);
1210 Ctyp
:= Component_Type
(Atyp
);
1211 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1213 -- We convert the right hand side to the proper subtype to ensure
1214 -- that an appropriate range check is made (since the normal range
1215 -- check from assignment will be lost in the transformations). This
1216 -- conversion is analyzed immediately so that subsequent processing
1217 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1219 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1220 Set_Parent
(Rhs
, N
);
1221 Analyze_And_Resolve
(Rhs
, Ctyp
);
1223 -- Case of component size 1,2,4 or any component size for the modular
1224 -- case. These are the cases for which we can inline the code.
1226 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1227 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1229 Setup_Inline_Packed_Array_Reference
(Lhs
, Atyp
, Obj
, Cmask
, Shift
);
1231 -- The statement to be generated is:
1233 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1235 -- where mask1 is obtained by shifting Cmask left Shift bits
1236 -- and then complementing the result.
1238 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1240 -- the "or ..." is omitted if rhs is constant and all 0 bits
1242 -- rhs is converted to the appropriate type.
1244 -- The result is converted back to the array type, since
1245 -- otherwise we lose knowledge of the packed nature.
1247 -- Determine if right side is all 0 bits or all 1 bits
1249 if Compile_Time_Known_Value
(Rhs
) then
1250 Rhs_Val
:= Expr_Rep_Value
(Rhs
);
1251 Rhs_Val_Known
:= True;
1253 -- The following test catches the case of an unchecked conversion
1254 -- of an integer literal. This results from optimizing aggregates
1257 elsif Nkind
(Rhs
) = N_Unchecked_Type_Conversion
1258 and then Compile_Time_Known_Value
(Expression
(Rhs
))
1260 Rhs_Val
:= Expr_Rep_Value
(Expression
(Rhs
));
1261 Rhs_Val_Known
:= True;
1265 Rhs_Val_Known
:= False;
1268 -- Some special checks for the case where the right hand value
1269 -- is known at compile time. Basically we have to take care of
1270 -- the implicit conversion to the subtype of the component object.
1272 if Rhs_Val_Known
then
1274 -- If we have a biased component type then we must manually do
1275 -- the biasing, since we are taking responsibility in this case
1276 -- for constructing the exact bit pattern to be used.
1278 if Has_Biased_Representation
(Ctyp
) then
1279 Rhs_Val
:= Rhs_Val
- Expr_Rep_Value
(Type_Low_Bound
(Ctyp
));
1282 -- For a negative value, we manually convert the twos complement
1283 -- value to a corresponding unsigned value, so that the proper
1284 -- field width is maintained. If we did not do this, we would
1285 -- get too many leading sign bits later on.
1288 Rhs_Val
:= 2 ** UI_From_Int
(Csiz
) + Rhs_Val
;
1292 New_Lhs
:= Duplicate_Subexpr
(Obj
, True);
1293 New_Rhs
:= Duplicate_Subexpr
(Obj
);
1295 -- First we deal with the "and"
1297 if not Rhs_Val_Known
or else Rhs_Val
/= Cmask
then
1303 if Compile_Time_Known_Value
(Shift
) then
1305 Make_Integer_Literal
(Loc
,
1306 Modulus
(Etype
(Obj
)) - 1 -
1307 (Cmask
* (2 ** Expr_Value
(Shift
))));
1308 Set_Print_In_Hex
(Mask1
);
1311 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1312 Set_Print_In_Hex
(Lit
);
1315 Right_Opnd
=> Make_Shift_Left
(Lit
, Shift
));
1320 Left_Opnd
=> New_Rhs
,
1321 Right_Opnd
=> Mask1
);
1325 -- Then deal with the "or"
1327 if not Rhs_Val_Known
or else Rhs_Val
/= 0 then
1331 procedure Fixup_Rhs
;
1332 -- Adjust Rhs by bias if biased representation for components
1333 -- or remove extraneous high order sign bits if signed.
1335 procedure Fixup_Rhs
is
1336 Etyp
: constant Entity_Id
:= Etype
(Rhs
);
1339 -- For biased case, do the required biasing by simply
1340 -- converting to the biased subtype (the conversion
1341 -- will generate the required bias).
1343 if Has_Biased_Representation
(Ctyp
) then
1344 Rhs
:= Convert_To
(Ctyp
, Rhs
);
1346 -- For a signed integer type that is not biased, generate
1347 -- a conversion to unsigned to strip high order sign bits.
1349 elsif Is_Signed_Integer_Type
(Ctyp
) then
1350 Rhs
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Csiz
)), Rhs
);
1353 -- Set Etype, since it can be referenced before the
1354 -- node is completely analyzed.
1356 Set_Etype
(Rhs
, Etyp
);
1358 -- We now need to do an unchecked conversion of the
1359 -- result to the target type, but it is important that
1360 -- this conversion be a right justified conversion and
1361 -- not a left justified conversion.
1363 Rhs
:= RJ_Unchecked_Convert_To
(Etype
(Obj
), Rhs
);
1369 and then Compile_Time_Known_Value
(Shift
)
1372 Make_Integer_Literal
(Loc
,
1373 Rhs_Val
* (2 ** Expr_Value
(Shift
)));
1374 Set_Print_In_Hex
(Or_Rhs
);
1377 -- We have to convert the right hand side to Etype (Obj).
1378 -- A special case case arises if what we have now is a Val
1379 -- attribute reference whose expression type is Etype (Obj).
1380 -- This happens for assignments of fields from the same
1381 -- array. In this case we get the required right hand side
1382 -- by simply removing the inner attribute reference.
1384 if Nkind
(Rhs
) = N_Attribute_Reference
1385 and then Attribute_Name
(Rhs
) = Name_Val
1386 and then Etype
(First
(Expressions
(Rhs
))) = Etype
(Obj
)
1388 Rhs
:= Relocate_Node
(First
(Expressions
(Rhs
)));
1391 -- If the value of the right hand side is a known integer
1392 -- value, then just replace it by an untyped constant,
1393 -- which will be properly retyped when we analyze and
1394 -- resolve the expression.
1396 elsif Rhs_Val_Known
then
1398 -- Note that Rhs_Val has already been normalized to
1399 -- be an unsigned value with the proper number of bits.
1402 Make_Integer_Literal
(Loc
, Rhs_Val
);
1404 -- Otherwise we need an unchecked conversion
1410 Or_Rhs
:= Make_Shift_Left
(Rhs
, Shift
);
1413 if Nkind
(New_Rhs
) = N_Op_And
then
1414 Set_Paren_Count
(New_Rhs
, 1);
1419 Left_Opnd
=> New_Rhs
,
1420 Right_Opnd
=> Or_Rhs
);
1424 -- Now do the rewrite
1427 Make_Assignment_Statement
(Loc
,
1430 Unchecked_Convert_To
(Etype
(New_Lhs
), New_Rhs
)));
1431 Set_Assignment_OK
(Name
(N
), Ass_OK
);
1433 -- All other component sizes for non-modular case
1438 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1440 -- where Subscr is the computed linear subscript.
1443 Bits_nn
: constant Entity_Id
:= RTE
(Bits_Id
(Csiz
));
1449 -- Acquire proper Set entity. We use the aligned or unaligned
1450 -- case as appropriate.
1452 if Known_Aligned_Enough
(Obj
, Csiz
) then
1453 Set_nn
:= RTE
(Set_Id
(Csiz
));
1455 Set_nn
:= RTE
(SetU_Id
(Csiz
));
1458 -- Now generate the set reference
1460 Obj
:= Relocate_Node
(Prefix
(Lhs
));
1461 Convert_To_Actual_Subtype
(Obj
);
1462 Atyp
:= Etype
(Obj
);
1463 Compute_Linear_Subscript
(Atyp
, Lhs
, Subscr
);
1466 Make_Procedure_Call_Statement
(Loc
,
1467 Name
=> New_Occurrence_Of
(Set_nn
, Loc
),
1468 Parameter_Associations
=> New_List
(
1469 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1470 Attribute_Name
=> Name_Address
,
1473 Unchecked_Convert_To
(Bits_nn
,
1474 Convert_To
(Ctyp
, Rhs
)))));
1479 Analyze
(N
, Suppress
=> All_Checks
);
1480 end Expand_Bit_Packed_Element_Set
;
1482 -------------------------------------
1483 -- Expand_Packed_Address_Reference --
1484 -------------------------------------
1486 procedure Expand_Packed_Address_Reference
(N
: Node_Id
) is
1487 Loc
: constant Source_Ptr
:= Sloc
(N
);
1499 -- We build up an expression serially that has the form
1501 -- outer_object'Address
1502 -- + (linear-subscript * component_size for each array reference
1503 -- + field'Bit_Position for each record field
1505 -- + ...) / Storage_Unit;
1507 -- Some additional conversions are required to deal with the addition
1508 -- operation, which is not normally visible to generated code.
1511 Ploc
:= Sloc
(Pref
);
1513 if Nkind
(Pref
) = N_Indexed_Component
then
1514 Convert_To_Actual_Subtype
(Prefix
(Pref
));
1515 Atyp
:= Etype
(Prefix
(Pref
));
1516 Compute_Linear_Subscript
(Atyp
, Pref
, Subscr
);
1519 Make_Op_Multiply
(Ploc
,
1520 Left_Opnd
=> Subscr
,
1522 Make_Attribute_Reference
(Ploc
,
1523 Prefix
=> New_Occurrence_Of
(Atyp
, Ploc
),
1524 Attribute_Name
=> Name_Component_Size
));
1526 elsif Nkind
(Pref
) = N_Selected_Component
then
1528 Make_Attribute_Reference
(Ploc
,
1529 Prefix
=> Selector_Name
(Pref
),
1530 Attribute_Name
=> Name_Bit_Position
);
1536 Term
:= Convert_To
(RTE
(RE_Integer_Address
), Term
);
1545 Right_Opnd
=> Term
);
1548 Pref
:= Prefix
(Pref
);
1552 Unchecked_Convert_To
(RTE
(RE_Address
),
1555 Unchecked_Convert_To
(RTE
(RE_Integer_Address
),
1556 Make_Attribute_Reference
(Loc
,
1558 Attribute_Name
=> Name_Address
)),
1561 Make_Op_Divide
(Loc
,
1564 Make_Integer_Literal
(Loc
, System_Storage_Unit
)))));
1566 Analyze_And_Resolve
(N
, RTE
(RE_Address
));
1567 end Expand_Packed_Address_Reference
;
1569 ------------------------------------
1570 -- Expand_Packed_Boolean_Operator --
1571 ------------------------------------
1573 -- This routine expands "a op b" for the packed cases
1575 procedure Expand_Packed_Boolean_Operator
(N
: Node_Id
) is
1576 Loc
: constant Source_Ptr
:= Sloc
(N
);
1577 Typ
: constant Entity_Id
:= Etype
(N
);
1578 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1579 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1586 Convert_To_Actual_Subtype
(L
);
1587 Convert_To_Actual_Subtype
(R
);
1589 Ensure_Defined
(Etype
(L
), N
);
1590 Ensure_Defined
(Etype
(R
), N
);
1592 Apply_Length_Check
(R
, Etype
(L
));
1597 -- First an odd and silly test. We explicitly check for the XOR
1598 -- case where the component type is True .. True, since this will
1599 -- raise constraint error. A special check is required since CE
1600 -- will not be required other wise (cf Expand_Packed_Not).
1602 -- No such check is required for AND and OR, since for both these
1603 -- cases False op False = False, and True op True = True.
1605 if Nkind
(N
) = N_Op_Xor
then
1607 CT
: constant Entity_Id
:= Component_Type
(Rtyp
);
1608 BT
: constant Entity_Id
:= Base_Type
(CT
);
1612 Make_Raise_Constraint_Error
(Loc
,
1618 Make_Attribute_Reference
(Loc
,
1619 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
1620 Attribute_Name
=> Name_First
),
1624 New_Occurrence_Of
(Standard_True
, Loc
))),
1629 Make_Attribute_Reference
(Loc
,
1630 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
1631 Attribute_Name
=> Name_Last
),
1635 New_Occurrence_Of
(Standard_True
, Loc
)))),
1636 Reason
=> CE_Range_Check_Failed
));
1640 -- Now that that silliness is taken care of, get packed array type
1642 Convert_To_PAT_Type
(L
);
1643 Convert_To_PAT_Type
(R
);
1647 -- For the modular case, we expand a op b into
1649 -- rtyp!(pat!(a) op pat!(b))
1651 -- where rtyp is the Etype of the left operand. Note that we do not
1652 -- convert to the base type, since this would be unconstrained, and
1653 -- hence not have a corresponding packed array type set.
1655 if Is_Modular_Integer_Type
(PAT
) then
1660 if Nkind
(N
) = N_Op_And
then
1661 P
:= Make_Op_And
(Loc
, L
, R
);
1663 elsif Nkind
(N
) = N_Op_Or
then
1664 P
:= Make_Op_Or
(Loc
, L
, R
);
1666 else -- Nkind (N) = N_Op_Xor
1667 P
:= Make_Op_Xor
(Loc
, L
, R
);
1670 Rewrite
(N
, Unchecked_Convert_To
(Rtyp
, P
));
1673 -- For the array case, we insert the actions
1677 -- System.Bitops.Bit_And/Or/Xor
1679 -- Ltype'Length * Ltype'Component_Size;
1681 -- Rtype'Length * Rtype'Component_Size
1684 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1685 -- the second argument and fourth arguments are the lengths of the
1686 -- operands in bits. Then we replace the expression by a reference
1691 Result_Ent
: constant Entity_Id
:=
1692 Make_Defining_Identifier
(Loc
,
1693 Chars
=> New_Internal_Name
('T'));
1698 if Nkind
(N
) = N_Op_And
then
1701 elsif Nkind
(N
) = N_Op_Or
then
1704 else -- Nkind (N) = N_Op_Xor
1708 Insert_Actions
(N
, New_List
(
1710 Make_Object_Declaration
(Loc
,
1711 Defining_Identifier
=> Result_Ent
,
1712 Object_Definition
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1714 Make_Procedure_Call_Statement
(Loc
,
1715 Name
=> New_Occurrence_Of
(RTE
(E_Id
), Loc
),
1716 Parameter_Associations
=> New_List
(
1718 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1719 Attribute_Name
=> Name_Address
,
1722 Make_Op_Multiply
(Loc
,
1724 Make_Attribute_Reference
(Loc
,
1727 (Etype
(First_Index
(Ltyp
)), Loc
),
1728 Attribute_Name
=> Name_Range_Length
),
1730 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
))),
1732 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1733 Attribute_Name
=> Name_Address
,
1736 Make_Op_Multiply
(Loc
,
1738 Make_Attribute_Reference
(Loc
,
1741 (Etype
(First_Index
(Rtyp
)), Loc
),
1742 Attribute_Name
=> Name_Range_Length
),
1744 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
1746 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1747 Attribute_Name
=> Name_Address
,
1748 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
))))));
1751 New_Occurrence_Of
(Result_Ent
, Loc
));
1755 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
1756 end Expand_Packed_Boolean_Operator
;
1758 -------------------------------------
1759 -- Expand_Packed_Element_Reference --
1760 -------------------------------------
1762 procedure Expand_Packed_Element_Reference
(N
: Node_Id
) is
1763 Loc
: constant Source_Ptr
:= Sloc
(N
);
1775 -- If not bit packed, we have the enumeration case, which is easily
1776 -- dealt with (just adjust the subscripts of the indexed component)
1778 -- Note: this leaves the result as an indexed component, which is
1779 -- still a variable, so can be used in the assignment case, as is
1780 -- required in the enumeration case.
1782 if not Is_Bit_Packed_Array
(Etype
(Prefix
(N
))) then
1783 Setup_Enumeration_Packed_Array_Reference
(N
);
1787 -- Remaining processing is for the bit-packed case.
1789 Obj
:= Relocate_Node
(Prefix
(N
));
1790 Convert_To_Actual_Subtype
(Obj
);
1791 Atyp
:= Etype
(Obj
);
1792 PAT
:= Packed_Array_Type
(Atyp
);
1793 Ctyp
:= Component_Type
(Atyp
);
1794 Csiz
:= UI_To_Int
(Component_Size
(Atyp
));
1796 -- Case of component size 1,2,4 or any component size for the modular
1797 -- case. These are the cases for which we can inline the code.
1799 if Csiz
= 1 or else Csiz
= 2 or else Csiz
= 4
1800 or else (Present
(PAT
) and then Is_Modular_Integer_Type
(PAT
))
1802 Setup_Inline_Packed_Array_Reference
(N
, Atyp
, Obj
, Cmask
, Shift
);
1803 Lit
:= Make_Integer_Literal
(Loc
, Cmask
);
1804 Set_Print_In_Hex
(Lit
);
1806 -- We generate a shift right to position the field, followed by a
1807 -- masking operation to extract the bit field, and we finally do an
1808 -- unchecked conversion to convert the result to the required target.
1810 -- Note that the unchecked conversion automatically deals with the
1811 -- bias if we are dealing with a biased representation. What will
1812 -- happen is that we temporarily generate the biased representation,
1813 -- but almost immediately that will be converted to the original
1814 -- unbiased component type, and the bias will disappear.
1818 Left_Opnd
=> Make_Shift_Right
(Obj
, Shift
),
1821 Analyze_And_Resolve
(Arg
);
1824 RJ_Unchecked_Convert_To
(Ctyp
, Arg
));
1826 -- All other component sizes for non-modular case
1831 -- Component_Type!(Get_nn (Arr'address, Subscr))
1833 -- where Subscr is the computed linear subscript.
1840 -- Acquire proper Get entity. We use the aligned or unaligned
1841 -- case as appropriate.
1843 if Known_Aligned_Enough
(Obj
, Csiz
) then
1844 Get_nn
:= RTE
(Get_Id
(Csiz
));
1846 Get_nn
:= RTE
(GetU_Id
(Csiz
));
1849 -- Now generate the get reference
1851 Compute_Linear_Subscript
(Atyp
, N
, Subscr
);
1854 Unchecked_Convert_To
(Ctyp
,
1855 Make_Function_Call
(Loc
,
1856 Name
=> New_Occurrence_Of
(Get_nn
, Loc
),
1857 Parameter_Associations
=> New_List
(
1858 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1859 Attribute_Name
=> Name_Address
,
1865 Analyze_And_Resolve
(N
, Ctyp
, Suppress
=> All_Checks
);
1867 end Expand_Packed_Element_Reference
;
1869 ----------------------
1870 -- Expand_Packed_Eq --
1871 ----------------------
1873 -- Handles expansion of "=" on packed array types
1875 procedure Expand_Packed_Eq
(N
: Node_Id
) is
1876 Loc
: constant Source_Ptr
:= Sloc
(N
);
1877 L
: constant Node_Id
:= Relocate_Node
(Left_Opnd
(N
));
1878 R
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1888 Convert_To_Actual_Subtype
(L
);
1889 Convert_To_Actual_Subtype
(R
);
1890 Ltyp
:= Underlying_Type
(Etype
(L
));
1891 Rtyp
:= Underlying_Type
(Etype
(R
));
1893 Convert_To_PAT_Type
(L
);
1894 Convert_To_PAT_Type
(R
);
1898 Make_Op_Multiply
(Loc
,
1900 Make_Attribute_Reference
(Loc
,
1901 Attribute_Name
=> Name_Length
,
1902 Prefix
=> New_Occurrence_Of
(Ltyp
, Loc
)),
1904 Make_Integer_Literal
(Loc
, Component_Size
(Ltyp
)));
1907 Make_Op_Multiply
(Loc
,
1909 Make_Attribute_Reference
(Loc
,
1910 Attribute_Name
=> Name_Length
,
1911 Prefix
=> New_Occurrence_Of
(Rtyp
, Loc
)),
1913 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
)));
1915 -- For the modular case, we transform the comparison to:
1917 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1919 -- where PAT is the packed array type. This works fine, since in the
1920 -- modular case we guarantee that the unused bits are always zeroes.
1921 -- We do have to compare the lengths because we could be comparing
1922 -- two different subtypes of the same base type.
1924 if Is_Modular_Integer_Type
(PAT
) then
1929 Left_Opnd
=> LLexpr
,
1930 Right_Opnd
=> RLexpr
),
1937 -- For the non-modular case, we call a runtime routine
1939 -- System.Bit_Ops.Bit_Eq
1940 -- (L'Address, L_Length, R'Address, R_Length)
1942 -- where PAT is the packed array type, and the lengths are the lengths
1943 -- in bits of the original packed arrays. This routine takes care of
1944 -- not comparing the unused bits in the last byte.
1948 Make_Function_Call
(Loc
,
1949 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Eq
), Loc
),
1950 Parameter_Associations
=> New_List
(
1951 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1952 Attribute_Name
=> Name_Address
,
1957 Make_Byte_Aligned_Attribute_Reference
(Loc
,
1958 Attribute_Name
=> Name_Address
,
1964 Analyze_And_Resolve
(N
, Standard_Boolean
, Suppress
=> All_Checks
);
1965 end Expand_Packed_Eq
;
1967 -----------------------
1968 -- Expand_Packed_Not --
1969 -----------------------
1971 -- Handles expansion of "not" on packed array types
1973 procedure Expand_Packed_Not
(N
: Node_Id
) is
1974 Loc
: constant Source_Ptr
:= Sloc
(N
);
1975 Typ
: constant Entity_Id
:= Etype
(N
);
1976 Opnd
: constant Node_Id
:= Relocate_Node
(Right_Opnd
(N
));
1983 Convert_To_Actual_Subtype
(Opnd
);
1984 Rtyp
:= Etype
(Opnd
);
1986 -- First an odd and silly test. We explicitly check for the case
1987 -- where the 'First of the component type is equal to the 'Last of
1988 -- this component type, and if this is the case, we make sure that
1989 -- constraint error is raised. The reason is that the NOT is bound
1990 -- to cause CE in this case, and we will not otherwise catch it.
1992 -- Believe it or not, this was reported as a bug. Note that nearly
1993 -- always, the test will evaluate statically to False, so the code
1994 -- will be statically removed, and no extra overhead caused.
1997 CT
: constant Entity_Id
:= Component_Type
(Rtyp
);
2001 Make_Raise_Constraint_Error
(Loc
,
2005 Make_Attribute_Reference
(Loc
,
2006 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
2007 Attribute_Name
=> Name_First
),
2010 Make_Attribute_Reference
(Loc
,
2011 Prefix
=> New_Occurrence_Of
(CT
, Loc
),
2012 Attribute_Name
=> Name_Last
)),
2013 Reason
=> CE_Range_Check_Failed
));
2016 -- Now that that silliness is taken care of, get packed array type
2018 Convert_To_PAT_Type
(Opnd
);
2019 PAT
:= Etype
(Opnd
);
2021 -- For the case where the packed array type is a modular type,
2022 -- not A expands simply into:
2024 -- rtyp!(PAT!(A) xor mask)
2026 -- where PAT is the packed array type, and mask is a mask of all
2027 -- one bits of length equal to the size of this packed type and
2028 -- rtyp is the actual subtype of the operand
2030 Lit
:= Make_Integer_Literal
(Loc
, 2 ** Esize
(PAT
) - 1);
2031 Set_Print_In_Hex
(Lit
);
2033 if not Is_Array_Type
(PAT
) then
2035 Unchecked_Convert_To
(Rtyp
,
2038 Right_Opnd
=> Lit
)));
2040 -- For the array case, we insert the actions
2044 -- System.Bitops.Bit_Not
2046 -- Typ'Length * Typ'Component_Size;
2049 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2050 -- argument is the length of the operand in bits. Then we replace
2051 -- the expression by a reference to Result.
2055 Result_Ent
: constant Entity_Id
:=
2056 Make_Defining_Identifier
(Loc
,
2057 Chars
=> New_Internal_Name
('T'));
2060 Insert_Actions
(N
, New_List
(
2062 Make_Object_Declaration
(Loc
,
2063 Defining_Identifier
=> Result_Ent
,
2064 Object_Definition
=> New_Occurrence_Of
(Rtyp
, Loc
)),
2066 Make_Procedure_Call_Statement
(Loc
,
2067 Name
=> New_Occurrence_Of
(RTE
(RE_Bit_Not
), Loc
),
2068 Parameter_Associations
=> New_List
(
2070 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2071 Attribute_Name
=> Name_Address
,
2074 Make_Op_Multiply
(Loc
,
2076 Make_Attribute_Reference
(Loc
,
2079 (Etype
(First_Index
(Rtyp
)), Loc
),
2080 Attribute_Name
=> Name_Range_Length
),
2082 Make_Integer_Literal
(Loc
, Component_Size
(Rtyp
))),
2084 Make_Byte_Aligned_Attribute_Reference
(Loc
,
2085 Attribute_Name
=> Name_Address
,
2086 Prefix
=> New_Occurrence_Of
(Result_Ent
, Loc
))))));
2089 New_Occurrence_Of
(Result_Ent
, Loc
));
2093 Analyze_And_Resolve
(N
, Typ
, Suppress
=> All_Checks
);
2095 end Expand_Packed_Not
;
2097 -------------------------------------
2098 -- Involves_Packed_Array_Reference --
2099 -------------------------------------
2101 function Involves_Packed_Array_Reference
(N
: Node_Id
) return Boolean is
2103 if Nkind
(N
) = N_Indexed_Component
2104 and then Is_Bit_Packed_Array
(Etype
(Prefix
(N
)))
2108 elsif Nkind
(N
) = N_Selected_Component
then
2109 return Involves_Packed_Array_Reference
(Prefix
(N
));
2114 end Involves_Packed_Array_Reference
;
2116 --------------------------
2117 -- Known_Aligned_Enough --
2118 --------------------------
2120 function Known_Aligned_Enough
(Obj
: Node_Id
; Csiz
: Nat
) return Boolean is
2121 Typ
: constant Entity_Id
:= Etype
(Obj
);
2123 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean;
2124 -- If the component is in a record that contains previous packed
2125 -- components, consider it unaligned because the back-end might
2126 -- choose to pack the rest of the record. Lead to less efficient code,
2127 -- but safer vis-a-vis of back-end choices.
2129 --------------------------------
2130 -- In_Partially_Packed_Record --
2131 --------------------------------
2133 function In_Partially_Packed_Record
(Comp
: Entity_Id
) return Boolean is
2134 Rec_Type
: constant Entity_Id
:= Scope
(Comp
);
2135 Prev_Comp
: Entity_Id
;
2138 Prev_Comp
:= First_Entity
(Rec_Type
);
2139 while Present
(Prev_Comp
) loop
2140 if Is_Packed
(Etype
(Prev_Comp
)) then
2143 elsif Prev_Comp
= Comp
then
2147 Next_Entity
(Prev_Comp
);
2151 end In_Partially_Packed_Record
;
2153 -- Start of processing for Known_Aligned_Enough
2156 -- Odd bit sizes don't need alignment anyway
2158 if Csiz
mod 2 = 1 then
2161 -- If we have a specified alignment, see if it is sufficient, if not
2162 -- then we can't possibly be aligned enough in any case.
2164 elsif Known_Alignment
(Etype
(Obj
)) then
2165 -- Alignment required is 4 if size is a multiple of 4, and
2166 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2168 if Alignment
(Etype
(Obj
)) < 4 - (Csiz
mod 4) then
2173 -- OK, alignment should be sufficient, if object is aligned
2175 -- If object is strictly aligned, then it is definitely aligned
2177 if Strict_Alignment
(Typ
) then
2180 -- Case of subscripted array reference
2182 elsif Nkind
(Obj
) = N_Indexed_Component
then
2184 -- If we have a pointer to an array, then this is definitely
2185 -- aligned, because pointers always point to aligned versions.
2187 if Is_Access_Type
(Etype
(Prefix
(Obj
))) then
2190 -- Otherwise, go look at the prefix
2193 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2196 -- Case of record field
2198 elsif Nkind
(Obj
) = N_Selected_Component
then
2200 -- What is significant here is whether the record type is packed
2202 if Is_Record_Type
(Etype
(Prefix
(Obj
)))
2203 and then Is_Packed
(Etype
(Prefix
(Obj
)))
2207 -- Or the component has a component clause which might cause
2208 -- the component to become unaligned (we can't tell if the
2209 -- backend is doing alignment computations).
2211 elsif Present
(Component_Clause
(Entity
(Selector_Name
(Obj
)))) then
2214 elsif In_Partially_Packed_Record
(Entity
(Selector_Name
(Obj
))) then
2217 -- In all other cases, go look at prefix
2220 return Known_Aligned_Enough
(Prefix
(Obj
), Csiz
);
2223 -- If not selected or indexed component, must be aligned
2228 end Known_Aligned_Enough
;
2230 ---------------------
2231 -- Make_Shift_Left --
2232 ---------------------
2234 function Make_Shift_Left
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2238 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2242 Make_Op_Shift_Left
(Sloc
(N
),
2245 Set_Shift_Count_OK
(Nod
, True);
2248 end Make_Shift_Left
;
2250 ----------------------
2251 -- Make_Shift_Right --
2252 ----------------------
2254 function Make_Shift_Right
(N
: Node_Id
; S
: Node_Id
) return Node_Id
is
2258 if Compile_Time_Known_Value
(S
) and then Expr_Value
(S
) = 0 then
2262 Make_Op_Shift_Right
(Sloc
(N
),
2265 Set_Shift_Count_OK
(Nod
, True);
2268 end Make_Shift_Right
;
2270 -----------------------------
2271 -- RJ_Unchecked_Convert_To --
2272 -----------------------------
2274 function RJ_Unchecked_Convert_To
2279 Source_Typ
: constant Entity_Id
:= Etype
(Expr
);
2280 Target_Typ
: constant Entity_Id
:= Typ
;
2282 Src
: Node_Id
:= Expr
;
2288 Source_Siz
:= UI_To_Int
(RM_Size
(Source_Typ
));
2289 Target_Siz
:= UI_To_Int
(RM_Size
(Target_Typ
));
2291 -- In the big endian case, if the lengths of the two types differ,
2292 -- then we must worry about possible left justification in the
2293 -- conversion, and avoiding that is what this is all about.
2295 if Bytes_Big_Endian
and then Source_Siz
/= Target_Siz
then
2297 -- First step, if the source type is not a discrete type, then we
2298 -- first convert to a modular type of the source length, since
2299 -- otherwise, on a big-endian machine, we get left-justification.
2301 if not Is_Discrete_Type
(Source_Typ
) then
2302 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Source_Siz
)), Src
);
2305 -- Next step. If the target is not a discrete type, then we first
2306 -- convert to a modular type of the target length, since
2307 -- otherwise, on a big-endian machine, we get left-justification.
2309 if not Is_Discrete_Type
(Target_Typ
) then
2310 Src
:= Unchecked_Convert_To
(RTE
(Bits_Id
(Target_Siz
)), Src
);
2314 -- And now we can do the final conversion to the target type
2316 return Unchecked_Convert_To
(Target_Typ
, Src
);
2317 end RJ_Unchecked_Convert_To
;
2319 ----------------------------------------------
2320 -- Setup_Enumeration_Packed_Array_Reference --
2321 ----------------------------------------------
2323 -- All we have to do here is to find the subscripts that correspond
2324 -- to the index positions that have non-standard enumeration types
2325 -- and insert a Pos attribute to get the proper subscript value.
2327 -- Finally the prefix must be uncheck converted to the corresponding
2328 -- packed array type.
2330 -- Note that the component type is unchanged, so we do not need to
2331 -- fiddle with the types (Gigi always automatically takes the packed
2332 -- array type if it is set, as it will be in this case).
2334 procedure Setup_Enumeration_Packed_Array_Reference
(N
: Node_Id
) is
2335 Pfx
: constant Node_Id
:= Prefix
(N
);
2336 Typ
: constant Entity_Id
:= Etype
(N
);
2337 Exprs
: constant List_Id
:= Expressions
(N
);
2341 -- If the array is unconstrained, then we replace the array
2342 -- reference with its actual subtype. This actual subtype will
2343 -- have a packed array type with appropriate bounds.
2345 if not Is_Constrained
(Packed_Array_Type
(Etype
(Pfx
))) then
2346 Convert_To_Actual_Subtype
(Pfx
);
2349 Expr
:= First
(Exprs
);
2350 while Present
(Expr
) loop
2352 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2353 Expr_Typ
: constant Entity_Id
:= Etype
(Expr
);
2356 if Is_Enumeration_Type
(Expr_Typ
)
2357 and then Has_Non_Standard_Rep
(Expr_Typ
)
2360 Make_Attribute_Reference
(Loc
,
2361 Prefix
=> New_Occurrence_Of
(Expr_Typ
, Loc
),
2362 Attribute_Name
=> Name_Pos
,
2363 Expressions
=> New_List
(Relocate_Node
(Expr
))));
2364 Analyze_And_Resolve
(Expr
, Standard_Natural
);
2372 Make_Indexed_Component
(Sloc
(N
),
2374 Unchecked_Convert_To
(Packed_Array_Type
(Etype
(Pfx
)), Pfx
),
2375 Expressions
=> Exprs
));
2377 Analyze_And_Resolve
(N
, Typ
);
2379 end Setup_Enumeration_Packed_Array_Reference
;
2381 -----------------------------------------
2382 -- Setup_Inline_Packed_Array_Reference --
2383 -----------------------------------------
2385 procedure Setup_Inline_Packed_Array_Reference
2388 Obj
: in out Node_Id
;
2390 Shift
: out Node_Id
)
2392 Loc
: constant Source_Ptr
:= Sloc
(N
);
2400 Ctyp
:= Component_Type
(Atyp
);
2401 Csiz
:= Component_Size
(Atyp
);
2403 Convert_To_PAT_Type
(Obj
);
2406 Cmask
:= 2 ** Csiz
- 1;
2408 if Is_Array_Type
(PAT
) then
2409 Otyp
:= Component_Type
(PAT
);
2410 Osiz
:= Esize
(Otyp
);
2415 -- In the case where the PAT is a modular type, we want the actual
2416 -- size in bits of the modular value we use. This is neither the
2417 -- Object_Size nor the Value_Size, either of which may have been
2418 -- reset to strange values, but rather the minimum size. Note that
2419 -- since this is a modular type with full range, the issue of
2420 -- biased representation does not arise.
2422 Osiz
:= UI_From_Int
(Minimum_Size
(Otyp
));
2425 Compute_Linear_Subscript
(Atyp
, N
, Shift
);
2427 -- If the component size is not 1, then the subscript must be
2428 -- multiplied by the component size to get the shift count.
2432 Make_Op_Multiply
(Loc
,
2433 Left_Opnd
=> Make_Integer_Literal
(Loc
, Csiz
),
2434 Right_Opnd
=> Shift
);
2437 -- If we have the array case, then this shift count must be broken
2438 -- down into a byte subscript, and a shift within the byte.
2440 if Is_Array_Type
(PAT
) then
2443 New_Shift
: Node_Id
;
2446 -- We must analyze shift, since we will duplicate it
2448 Set_Parent
(Shift
, N
);
2450 (Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2452 -- The shift count within the word is
2457 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2458 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
));
2460 -- The subscript to be used on the PAT array is
2464 Make_Indexed_Component
(Loc
,
2466 Expressions
=> New_List
(
2467 Make_Op_Divide
(Loc
,
2468 Left_Opnd
=> Duplicate_Subexpr
(Shift
),
2469 Right_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
))));
2474 -- For the modular integer case, the object to be manipulated is
2475 -- the entire array, so Obj is unchanged. Note that we will reset
2476 -- its type to PAT before returning to the caller.
2482 -- The one remaining step is to modify the shift count for the
2483 -- big-endian case. Consider the following example in a byte:
2485 -- xxxxxxxx bits of byte
2486 -- vvvvvvvv bits of value
2487 -- 33221100 little-endian numbering
2488 -- 00112233 big-endian numbering
2490 -- Here we have the case of 2-bit fields
2492 -- For the little-endian case, we already have the proper shift
2493 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2495 -- For the big endian case, we have to adjust the shift count,
2496 -- computing it as (N - F) - shift, where N is the number of bits
2497 -- in an element of the array used to implement the packed array,
2498 -- F is the number of bits in a source level array element, and
2499 -- shift is the count so far computed.
2501 if Bytes_Big_Endian
then
2503 Make_Op_Subtract
(Loc
,
2504 Left_Opnd
=> Make_Integer_Literal
(Loc
, Osiz
- Csiz
),
2505 Right_Opnd
=> Shift
);
2508 Set_Parent
(Shift
, N
);
2509 Set_Parent
(Obj
, N
);
2510 Analyze_And_Resolve
(Obj
, Otyp
, Suppress
=> All_Checks
);
2511 Analyze_And_Resolve
(Shift
, Standard_Integer
, Suppress
=> All_Checks
);
2513 -- Make sure final type of object is the appropriate packed type
2515 Set_Etype
(Obj
, Otyp
);
2517 end Setup_Inline_Packed_Array_Reference
;