1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Layout; use Layout;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Rtsfind; use Rtsfind;
38 with Sem_Ch3; use Sem_Ch3;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Ch13; use Sem_Ch13;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Targparm; use Targparm;
48 with Tbuild; use Tbuild;
49 with Ttypes; use Ttypes;
50 with Uintp; use Uintp;
52 package body Exp_Pakd is
54 ---------------------------
55 -- Endian Considerations --
56 ---------------------------
58 -- As described in the specification, bit numbering in a packed array
59 -- is consistent with bit numbering in a record representation clause,
60 -- and hence dependent on the endianness of the machine:
62 -- For little-endian machines, element zero is at the right hand end
63 -- (low order end) of a bit field.
65 -- For big-endian machines, element zero is at the left hand end
66 -- (high order end) of a bit field.
68 -- The shifts that are used to right justify a field therefore differ
69 -- in the two cases. For the little-endian case, we can simply use the
70 -- bit number (i.e. the element number * element size) as the count for
71 -- a right shift. For the big-endian case, we have to subtract the shift
72 -- count from an appropriate constant to use in the right shift. We use
73 -- rotates instead of shifts (which is necessary in the store case to
74 -- preserve other fields), and we expect that the backend will be able
75 -- to change the right rotate into a left rotate, avoiding the subtract,
76 -- if the architecture provides such an instruction.
78 ----------------------------------------------
79 -- Entity Tables for Packed Access Routines --
80 ----------------------------------------------
82 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
83 -- library routines. This table is used to obtain the entity for the
86 type E_Array is array (Int range 01 .. 63) of RE_Id;
88 -- Array of Bits_nn entities. Note that we do not use library routines
89 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
90 -- entries from System.Unsigned, because we also use this table for
91 -- certain special unchecked conversions in the big-endian case.
93 Bits_Id : constant E_Array :=
109 16 => RE_Unsigned_16,
125 32 => RE_Unsigned_32,
158 -- Array of Get routine entities. These are used to obtain an element
159 -- from a packed array. The N'th entry is used to obtain elements from
160 -- a packed array whose component size is N. RE_Null is used as a null
161 -- entry, for the cases where a library routine is not used.
163 Get_Id : constant E_Array :=
228 -- Array of Get routine entities to be used in the case where the packed
229 -- array is itself a component of a packed structure, and therefore may
230 -- not be fully aligned. This only affects the even sizes, since for the
231 -- odd sizes, we do not get any fixed alignment in any case.
233 GetU_Id : constant E_Array :=
298 -- Array of Set routine entities. These are used to assign an element
299 -- of a packed array. The N'th entry is used to assign elements for
300 -- a packed array whose component size is N. RE_Null is used as a null
301 -- entry, for the cases where a library routine is not used.
303 Set_Id : constant E_Array :=
368 -- Array of Set routine entities to be used in the case where the packed
369 -- array is itself a component of a packed structure, and therefore may
370 -- not be fully aligned. This only affects the even sizes, since for the
371 -- odd sizes, we do not get any fixed alignment in any case.
373 SetU_Id : constant E_Array :=
438 -----------------------
439 -- Local Subprograms --
440 -----------------------
442 procedure Compute_Linear_Subscript
445 Subscr : out Node_Id);
446 -- Given a constrained array type Atyp, and an indexed component node
447 -- N referencing an array object of this type, build an expression of
448 -- type Standard.Integer representing the zero-based linear subscript
449 -- value. This expression includes any required range checks.
451 procedure Convert_To_PAT_Type (Aexp : Node_Id);
452 -- Given an expression of a packed array type, builds a corresponding
453 -- expression whose type is the implementation type used to represent
454 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
456 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
457 -- There are two versions of the Set routines, the ones used when the
458 -- object is known to be sufficiently well aligned given the number of
459 -- bits, and the ones used when the object is not known to be aligned.
460 -- This routine is used to determine which set to use. Obj is a reference
461 -- to the object, and Csiz is the component size of the packed array.
462 -- True is returned if the alignment of object is known to be sufficient,
463 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
466 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
467 -- Build a left shift node, checking for the case of a shift count of zero
469 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
470 -- Build a right shift node, checking for the case of a shift count of zero
472 function RJ_Unchecked_Convert_To
474 Expr : Node_Id) return Node_Id;
475 -- The packed array code does unchecked conversions which in some cases
476 -- may involve non-discrete types with differing sizes. The semantics of
477 -- such conversions is potentially endian dependent, and the effect we
478 -- want here for such a conversion is to do the conversion in size as
479 -- though numeric items are involved, and we extend or truncate on the
480 -- left side. This happens naturally in the little-endian case, but in
481 -- the big endian case we can get left justification, when what we want
482 -- is right justification. This routine does the unchecked conversion in
483 -- a stepwise manner to ensure that it gives the expected result. Hence
484 -- the name (RJ = Right justified). The parameters Typ and Expr are as
485 -- for the case of a normal Unchecked_Convert_To call.
487 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
488 -- This routine is called in the Get and Set case for arrays that are
489 -- packed but not bit-packed, meaning that they have at least one
490 -- subscript that is of an enumeration type with a non-standard
491 -- representation. This routine modifies the given node to properly
492 -- reference the corresponding packed array type.
494 procedure Setup_Inline_Packed_Array_Reference
497 Obj : in out Node_Id;
499 Shift : out Node_Id);
500 -- This procedure performs common processing on the N_Indexed_Component
501 -- parameter given as N, whose prefix is a reference to a packed array.
502 -- This is used for the get and set when the component size is 1,2,4
503 -- or for other component sizes when the packed array type is a modular
504 -- type (i.e. the cases that are handled with inline code).
508 -- N is the N_Indexed_Component node for the packed array reference
510 -- Atyp is the constrained array type (the actual subtype has been
511 -- computed if necessary to obtain the constraints, but this is still
512 -- the original array type, not the Packed_Array_Type value).
514 -- Obj is the object which is to be indexed. It is always of type Atyp.
518 -- Obj is the object containing the desired bit field. It is of type
519 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
520 -- entire value, for the small static case, or the proper selected byte
521 -- from the array in the large or dynamic case. This node is analyzed
522 -- and resolved on return.
524 -- Shift is a node representing the shift count to be used in the
525 -- rotate right instruction that positions the field for access.
526 -- This node is analyzed and resolved on return.
528 -- Cmask is a mask corresponding to the width of the component field.
529 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
531 -- Note: in some cases the call to this routine may generate actions
532 -- (for handling multi-use references and the generation of the packed
533 -- array type on the fly). Such actions are inserted into the tree
534 -- directly using Insert_Action.
536 ------------------------------
537 -- Compute_Linear_Subcsript --
538 ------------------------------
540 procedure Compute_Linear_Subscript
543 Subscr : out Node_Id)
545 Loc : constant Source_Ptr := Sloc (N);
554 -- Loop through dimensions
556 Indx := First_Index (Atyp);
557 Oldsub := First (Expressions (N));
559 while Present (Indx) loop
560 Styp := Etype (Indx);
561 Newsub := Relocate_Node (Oldsub);
563 -- Get expression for the subscript value. First, if Do_Range_Check
564 -- is set on a subscript, then we must do a range check against the
565 -- original bounds (not the bounds of the packed array type). We do
566 -- this by introducing a subtype conversion.
568 if Do_Range_Check (Newsub)
569 and then Etype (Newsub) /= Styp
571 Newsub := Convert_To (Styp, Newsub);
574 -- Now evolve the expression for the subscript. First convert
575 -- the subscript to be zero based and of an integer type.
577 -- Case of integer type, where we just subtract to get lower bound
579 if Is_Integer_Type (Styp) then
581 -- If length of integer type is smaller than standard integer,
582 -- then we convert to integer first, then do the subtract
584 -- Integer (subscript) - Integer (Styp'First)
586 if Esize (Styp) < Esize (Standard_Integer) then
588 Make_Op_Subtract (Loc,
589 Left_Opnd => Convert_To (Standard_Integer, Newsub),
591 Convert_To (Standard_Integer,
592 Make_Attribute_Reference (Loc,
593 Prefix => New_Occurrence_Of (Styp, Loc),
594 Attribute_Name => Name_First)));
596 -- For larger integer types, subtract first, then convert to
597 -- integer, this deals with strange long long integer bounds.
599 -- Integer (subscript - Styp'First)
603 Convert_To (Standard_Integer,
604 Make_Op_Subtract (Loc,
607 Make_Attribute_Reference (Loc,
608 Prefix => New_Occurrence_Of (Styp, Loc),
609 Attribute_Name => Name_First)));
612 -- For the enumeration case, we have to use 'Pos to get the value
613 -- to work with before subtracting the lower bound.
615 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
617 -- This is not quite right for bizarre cases where the size of the
618 -- enumeration type is > Integer'Size bits due to rep clause ???
621 pragma Assert (Is_Enumeration_Type (Styp));
624 Make_Op_Subtract (Loc,
625 Left_Opnd => Convert_To (Standard_Integer,
626 Make_Attribute_Reference (Loc,
627 Prefix => New_Occurrence_Of (Styp, Loc),
628 Attribute_Name => Name_Pos,
629 Expressions => New_List (Newsub))),
632 Convert_To (Standard_Integer,
633 Make_Attribute_Reference (Loc,
634 Prefix => New_Occurrence_Of (Styp, Loc),
635 Attribute_Name => Name_Pos,
636 Expressions => New_List (
637 Make_Attribute_Reference (Loc,
638 Prefix => New_Occurrence_Of (Styp, Loc),
639 Attribute_Name => Name_First)))));
642 Set_Paren_Count (Newsub, 1);
644 -- For the first subscript, we just copy that subscript value
649 -- Otherwise, we must multiply what we already have by the current
650 -- stride and then add in the new value to the evolving subscript.
656 Make_Op_Multiply (Loc,
659 Make_Attribute_Reference (Loc,
660 Attribute_Name => Name_Range_Length,
661 Prefix => New_Occurrence_Of (Styp, Loc))),
662 Right_Opnd => Newsub);
665 -- Move to next subscript
670 end Compute_Linear_Subscript;
672 -------------------------
673 -- Convert_To_PAT_Type --
674 -------------------------
676 -- The PAT is always obtained from the actual subtype
678 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
682 Convert_To_Actual_Subtype (Aexp);
683 Act_ST := Underlying_Type (Etype (Aexp));
684 Create_Packed_Array_Type (Act_ST);
686 -- Just replace the etype with the packed array type. This works because
687 -- the expression will not be further analyzed, and Gigi considers the
688 -- two types equivalent in any case.
690 -- This is not strictly the case ??? If the reference is an actual in
691 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
692 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
693 -- array reference, reanalysis can produce spurious type errors when the
694 -- PAT type is replaced again with the original type of the array. Same
695 -- for the case of a dereference. The following is correct and minimal,
696 -- but the handling of more complex packed expressions in actuals is
697 -- confused. Probably the problem only remains for actuals in calls.
699 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
701 if Is_Entity_Name (Aexp)
703 (Nkind (Aexp) = N_Indexed_Component
704 and then Is_Entity_Name (Prefix (Aexp)))
705 or else Nkind (Aexp) = N_Explicit_Dereference
709 end Convert_To_PAT_Type;
711 ------------------------------
712 -- Create_Packed_Array_Type --
713 ------------------------------
715 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
716 Loc : constant Source_Ptr := Sloc (Typ);
717 Ctyp : constant Entity_Id := Component_Type (Typ);
718 Csize : constant Uint := Component_Size (Typ);
733 procedure Install_PAT;
734 -- This procedure is called with Decl set to the declaration for the
735 -- packed array type. It creates the type and installs it as required.
737 procedure Set_PB_Type;
738 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
739 -- requirements (see documentation in the spec of this package).
745 procedure Install_PAT is
746 Pushed_Scope : Boolean := False;
749 -- We do not want to put the declaration we have created in the tree
750 -- since it is often hard, and sometimes impossible to find a proper
751 -- place for it (the impossible case arises for a packed array type
752 -- with bounds depending on the discriminant, a declaration cannot
753 -- be put inside the record, and the reference to the discriminant
754 -- cannot be outside the record).
756 -- The solution is to analyze the declaration while temporarily
757 -- attached to the tree at an appropriate point, and then we install
758 -- the resulting type as an Itype in the packed array type field of
759 -- the original type, so that no explicit declaration is required.
761 -- Note: the packed type is created in the scope of its parent
762 -- type. There are at least some cases where the current scope
763 -- is deeper, and so when this is the case, we temporarily reset
764 -- the scope for the definition. This is clearly safe, since the
765 -- first use of the packed array type will be the implicit
766 -- reference from the corresponding unpacked type when it is
769 if Is_Itype (Typ) then
770 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
772 Set_Parent (Decl, Declaration_Node (Typ));
775 if Scope (Typ) /= Current_Scope then
776 Push_Scope (Scope (Typ));
777 Pushed_Scope := True;
780 Set_Is_Itype (PAT, True);
781 Set_Packed_Array_Type (Typ, PAT);
782 Analyze (Decl, Suppress => All_Checks);
788 -- Set Esize and RM_Size to the actual size of the packed object
789 -- Do not reset RM_Size if already set, as happens in the case of
792 if Unknown_Esize (PAT) then
793 Set_Esize (PAT, PASize);
796 if Unknown_RM_Size (PAT) then
797 Set_RM_Size (PAT, PASize);
800 Adjust_Esize_Alignment (PAT);
802 -- Set remaining fields of packed array type
804 Init_Alignment (PAT);
805 Set_Parent (PAT, Empty);
806 Set_Associated_Node_For_Itype (PAT, Typ);
807 Set_Is_Packed_Array_Type (PAT, True);
808 Set_Original_Array_Type (PAT, Typ);
810 -- We definitely do not want to delay freezing for packed array
811 -- types. This is of particular importance for the itypes that
812 -- are generated for record components depending on discriminants
813 -- where there is no place to put the freeze node.
815 Set_Has_Delayed_Freeze (PAT, False);
816 Set_Has_Delayed_Freeze (Etype (PAT), False);
818 -- If we did allocate a freeze node, then clear out the reference
819 -- since it is obsolete (should we delete the freeze node???)
821 Set_Freeze_Node (PAT, Empty);
822 Set_Freeze_Node (Etype (PAT), Empty);
829 procedure Set_PB_Type is
831 -- If the user has specified an explicit alignment for the
832 -- type or component, take it into account.
834 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
835 or else Alignment (Typ) = 1
836 or else Component_Alignment (Typ) = Calign_Storage_Unit
838 PB_Type := RTE (RE_Packed_Bytes1);
840 elsif Csize mod 4 /= 0
841 or else Alignment (Typ) = 2
843 PB_Type := RTE (RE_Packed_Bytes2);
846 PB_Type := RTE (RE_Packed_Bytes4);
850 -- Start of processing for Create_Packed_Array_Type
853 -- If we already have a packed array type, nothing to do
855 if Present (Packed_Array_Type (Typ)) then
859 -- If our immediate ancestor subtype is constrained, and it already
860 -- has a packed array type, then just share the same type, since the
861 -- bounds must be the same. If the ancestor is not an array type but
862 -- a private type, as can happen with multiple instantiations, create
863 -- a new packed type, to avoid privacy issues.
865 if Ekind (Typ) = E_Array_Subtype then
866 Ancest := Ancestor_Subtype (Typ);
869 and then Is_Array_Type (Ancest)
870 and then Is_Constrained (Ancest)
871 and then Present (Packed_Array_Type (Ancest))
873 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
878 -- We preset the result type size from the size of the original array
879 -- type, since this size clearly belongs to the packed array type. The
880 -- size of the conceptual unpacked type is always set to unknown.
882 PASize := RM_Size (Typ);
884 -- Case of an array where at least one index is of an enumeration
885 -- type with a non-standard representation, but the component size
886 -- is not appropriate for bit packing. This is the case where we
887 -- have Is_Packed set (we would never be in this unit otherwise),
888 -- but Is_Bit_Packed_Array is false.
890 -- Note that if the component size is appropriate for bit packing,
891 -- then the circuit for the computation of the subscript properly
892 -- deals with the non-standard enumeration type case by taking the
895 if not Is_Bit_Packed_Array (Typ) then
897 -- Here we build a declaration:
899 -- type tttP is array (index1, index2, ...) of component_type
901 -- where index1, index2, are the index types. These are the same
902 -- as the index types of the original array, except for the non-
903 -- standard representation enumeration type case, where we have
906 -- For the unconstrained array case, we use
910 -- For the constrained case, we use
912 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
913 -- Enum_Type'Pos (Enum_Type'Last);
916 Make_Defining_Identifier (Loc,
917 Chars => New_External_Name (Chars (Typ), 'P'));
919 Set_Packed_Array_Type (Typ, PAT);
922 Indexes : constant List_Id := New_List;
924 Indx_Typ : Entity_Id;
929 Indx := First_Index (Typ);
931 while Present (Indx) loop
932 Indx_Typ := Etype (Indx);
934 Enum_Case := Is_Enumeration_Type (Indx_Typ)
935 and then Has_Non_Standard_Rep (Indx_Typ);
937 -- Unconstrained case
939 if not Is_Constrained (Typ) then
941 Indx_Typ := Standard_Natural;
944 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
949 if not Enum_Case then
950 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
954 Make_Subtype_Indication (Loc,
956 New_Occurrence_Of (Standard_Natural, Loc),
958 Make_Range_Constraint (Loc,
962 Make_Attribute_Reference (Loc,
964 New_Occurrence_Of (Indx_Typ, Loc),
965 Attribute_Name => Name_Pos,
966 Expressions => New_List (
967 Make_Attribute_Reference (Loc,
969 New_Occurrence_Of (Indx_Typ, Loc),
970 Attribute_Name => Name_First))),
973 Make_Attribute_Reference (Loc,
975 New_Occurrence_Of (Indx_Typ, Loc),
976 Attribute_Name => Name_Pos,
977 Expressions => New_List (
978 Make_Attribute_Reference (Loc,
980 New_Occurrence_Of (Indx_Typ, Loc),
981 Attribute_Name => Name_Last)))))));
989 if not Is_Constrained (Typ) then
991 Make_Unconstrained_Array_Definition (Loc,
992 Subtype_Marks => Indexes,
993 Component_Definition =>
994 Make_Component_Definition (Loc,
995 Aliased_Present => False,
996 Subtype_Indication =>
997 New_Occurrence_Of (Ctyp, Loc)));
1001 Make_Constrained_Array_Definition (Loc,
1002 Discrete_Subtype_Definitions => Indexes,
1003 Component_Definition =>
1004 Make_Component_Definition (Loc,
1005 Aliased_Present => False,
1006 Subtype_Indication =>
1007 New_Occurrence_Of (Ctyp, Loc)));
1011 Make_Full_Type_Declaration (Loc,
1012 Defining_Identifier => PAT,
1013 Type_Definition => Typedef);
1016 -- Set type as packed array type and install it
1018 Set_Is_Packed_Array_Type (PAT);
1022 -- Case of bit-packing required for unconstrained array. We create
1023 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1025 elsif not Is_Constrained (Typ) then
1027 Make_Defining_Identifier (Loc,
1028 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1030 Set_Packed_Array_Type (Typ, PAT);
1034 Make_Subtype_Declaration (Loc,
1035 Defining_Identifier => PAT,
1036 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1040 -- Remaining code is for the case of bit-packing for constrained array
1042 -- The name of the packed array subtype is
1046 -- where sss is the component size in bits and ttt is the name of
1047 -- the parent packed type.
1051 Make_Defining_Identifier (Loc,
1052 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1054 Set_Packed_Array_Type (Typ, PAT);
1056 -- Build an expression for the length of the array in bits.
1057 -- This is the product of the length of each of the dimensions
1063 Len_Expr := Empty; -- suppress junk warning
1067 Make_Attribute_Reference (Loc,
1068 Attribute_Name => Name_Length,
1069 Prefix => New_Occurrence_Of (Typ, Loc),
1070 Expressions => New_List (
1071 Make_Integer_Literal (Loc, J)));
1074 Len_Expr := Len_Dim;
1078 Make_Op_Multiply (Loc,
1079 Left_Opnd => Len_Expr,
1080 Right_Opnd => Len_Dim);
1084 exit when J > Number_Dimensions (Typ);
1088 -- Temporarily attach the length expression to the tree and analyze
1089 -- and resolve it, so that we can test its value. We assume that the
1090 -- total length fits in type Integer. This expression may involve
1091 -- discriminants, so we treat it as a default/per-object expression.
1093 Set_Parent (Len_Expr, Typ);
1094 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1096 -- Use a modular type if possible. We can do this if we have
1097 -- static bounds, and the length is small enough, and the length
1098 -- is not zero. We exclude the zero length case because the size
1099 -- of things is always at least one, and the zero length object
1100 -- would have an anomalous size.
1102 if Compile_Time_Known_Value (Len_Expr) then
1103 Len_Bits := Expr_Value (Len_Expr) * Csize;
1105 -- Check for size known to be too large
1108 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1110 if System_Storage_Unit = 8 then
1112 ("packed array size cannot exceed " &
1113 "Integer''Last bytes", Typ);
1116 ("packed array size cannot exceed " &
1117 "Integer''Last storage units", Typ);
1120 -- Reset length to arbitrary not too high value to continue
1122 Len_Expr := Make_Integer_Literal (Loc, 65535);
1123 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1126 -- We normally consider small enough to mean no larger than the
1127 -- value of System_Max_Binary_Modulus_Power, checking that in the
1128 -- case of values longer than word size, we have long shifts.
1132 (Len_Bits <= System_Word_Size
1133 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1134 and then Support_Long_Shifts_On_Target))
1136 -- Also test for alignment given. If an alignment is given which
1137 -- is smaller than the natural modular alignment, force the array
1138 -- of bytes representation to accommodate the alignment.
1141 (No (Alignment_Clause (Typ))
1143 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1144 / System_Storage_Unit))
1146 -- We can use the modular type, it has the form:
1148 -- subtype tttPn is btyp
1149 -- range 0 .. 2 ** ((Typ'Length (1)
1150 -- * ... * Typ'Length (n)) * Csize) - 1;
1152 -- The bounds are statically known, and btyp is one of the
1153 -- unsigned types, depending on the length.
1155 if Len_Bits <= Standard_Short_Short_Integer_Size then
1156 Btyp := RTE (RE_Short_Short_Unsigned);
1158 elsif Len_Bits <= Standard_Short_Integer_Size then
1159 Btyp := RTE (RE_Short_Unsigned);
1161 elsif Len_Bits <= Standard_Integer_Size then
1162 Btyp := RTE (RE_Unsigned);
1164 elsif Len_Bits <= Standard_Long_Integer_Size then
1165 Btyp := RTE (RE_Long_Unsigned);
1168 Btyp := RTE (RE_Long_Long_Unsigned);
1171 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1172 Set_Print_In_Hex (Lit);
1175 Make_Subtype_Declaration (Loc,
1176 Defining_Identifier => PAT,
1177 Subtype_Indication =>
1178 Make_Subtype_Indication (Loc,
1179 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1182 Make_Range_Constraint (Loc,
1186 Make_Integer_Literal (Loc, 0),
1187 High_Bound => Lit))));
1189 if PASize = Uint_0 then
1198 -- Could not use a modular type, for all other cases, we build
1199 -- a packed array subtype:
1202 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1204 -- Bits is the length of the array in bits
1211 Make_Op_Multiply (Loc,
1213 Make_Integer_Literal (Loc, Csize),
1214 Right_Opnd => Len_Expr),
1217 Make_Integer_Literal (Loc, 7));
1219 Set_Paren_Count (Bits_U1, 1);
1222 Make_Op_Subtract (Loc,
1224 Make_Op_Divide (Loc,
1225 Left_Opnd => Bits_U1,
1226 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1227 Right_Opnd => Make_Integer_Literal (Loc, 1));
1230 Make_Subtype_Declaration (Loc,
1231 Defining_Identifier => PAT,
1232 Subtype_Indication =>
1233 Make_Subtype_Indication (Loc,
1234 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1236 Make_Index_Or_Discriminant_Constraint (Loc,
1237 Constraints => New_List (
1240 Make_Integer_Literal (Loc, 0),
1242 Convert_To (Standard_Integer, PAT_High))))));
1246 -- Currently the code in this unit requires that packed arrays
1247 -- represented by non-modular arrays of bytes be on a byte
1248 -- boundary for bit sizes handled by System.Pack_nn units.
1249 -- That's because these units assume the array being accessed
1250 -- starts on a byte boundary.
1252 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1253 Set_Must_Be_On_Byte_Boundary (Typ);
1256 end Create_Packed_Array_Type;
1258 -----------------------------------
1259 -- Expand_Bit_Packed_Element_Set --
1260 -----------------------------------
1262 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1263 Loc : constant Source_Ptr := Sloc (N);
1264 Lhs : constant Node_Id := Name (N);
1266 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1267 -- Used to preserve assignment OK status when assignment is rewritten
1269 Rhs : Node_Id := Expression (N);
1270 -- Initially Rhs is the right hand side value, it will be replaced
1271 -- later by an appropriate unchecked conversion for the assignment.
1281 -- The expression for the shift value that is required
1283 Shift_Used : Boolean := False;
1284 -- Set True if Shift has been used in the generated code at least
1285 -- once, so that it must be duplicated if used again
1290 Rhs_Val_Known : Boolean;
1292 -- If the value of the right hand side as an integer constant is
1293 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1294 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1295 -- the Rhs_Val is undefined.
1297 function Get_Shift return Node_Id;
1298 -- Function used to get the value of Shift, making sure that it
1299 -- gets duplicated if the function is called more than once.
1305 function Get_Shift return Node_Id is
1307 -- If we used the shift value already, then duplicate it. We
1308 -- set a temporary parent in case actions have to be inserted.
1311 Set_Parent (Shift, N);
1312 return Duplicate_Subexpr_No_Checks (Shift);
1314 -- If first time, use Shift unchanged, and set flag for first use
1322 -- Start of processing for Expand_Bit_Packed_Element_Set
1325 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1327 Obj := Relocate_Node (Prefix (Lhs));
1328 Convert_To_Actual_Subtype (Obj);
1329 Atyp := Etype (Obj);
1330 PAT := Packed_Array_Type (Atyp);
1331 Ctyp := Component_Type (Atyp);
1332 Csiz := UI_To_Int (Component_Size (Atyp));
1334 -- We convert the right hand side to the proper subtype to ensure
1335 -- that an appropriate range check is made (since the normal range
1336 -- check from assignment will be lost in the transformations). This
1337 -- conversion is analyzed immediately so that subsequent processing
1338 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1340 -- If the right-hand side is a string literal, create a temporary for
1341 -- it, constant-folding is not ready to wrap the bit representation
1342 -- of a string literal.
1344 if Nkind (Rhs) = N_String_Literal then
1349 Make_Object_Declaration (Loc,
1350 Defining_Identifier =>
1351 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1352 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1353 Expression => New_Copy_Tree (Rhs));
1355 Insert_Actions (N, New_List (Decl));
1356 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1360 Rhs := Convert_To (Ctyp, Rhs);
1361 Set_Parent (Rhs, N);
1362 Analyze_And_Resolve (Rhs, Ctyp);
1364 -- Case of component size 1,2,4 or any component size for the modular
1365 -- case. These are the cases for which we can inline the code.
1367 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1368 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1370 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1372 -- The statement to be generated is:
1374 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1376 -- where mask1 is obtained by shifting Cmask left Shift bits
1377 -- and then complementing the result.
1379 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1381 -- the "or ..." is omitted if rhs is constant and all 0 bits
1383 -- rhs is converted to the appropriate type
1385 -- The result is converted back to the array type, since
1386 -- otherwise we lose knowledge of the packed nature.
1388 -- Determine if right side is all 0 bits or all 1 bits
1390 if Compile_Time_Known_Value (Rhs) then
1391 Rhs_Val := Expr_Rep_Value (Rhs);
1392 Rhs_Val_Known := True;
1394 -- The following test catches the case of an unchecked conversion
1395 -- of an integer literal. This results from optimizing aggregates
1398 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1399 and then Compile_Time_Known_Value (Expression (Rhs))
1401 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1402 Rhs_Val_Known := True;
1406 Rhs_Val_Known := False;
1409 -- Some special checks for the case where the right hand value
1410 -- is known at compile time. Basically we have to take care of
1411 -- the implicit conversion to the subtype of the component object.
1413 if Rhs_Val_Known then
1415 -- If we have a biased component type then we must manually do
1416 -- the biasing, since we are taking responsibility in this case
1417 -- for constructing the exact bit pattern to be used.
1419 if Has_Biased_Representation (Ctyp) then
1420 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1423 -- For a negative value, we manually convert the twos complement
1424 -- value to a corresponding unsigned value, so that the proper
1425 -- field width is maintained. If we did not do this, we would
1426 -- get too many leading sign bits later on.
1429 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1433 New_Lhs := Duplicate_Subexpr (Obj, True);
1434 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1436 -- First we deal with the "and"
1438 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1444 if Compile_Time_Known_Value (Shift) then
1446 Make_Integer_Literal (Loc,
1447 Modulus (Etype (Obj)) - 1 -
1448 (Cmask * (2 ** Expr_Value (Get_Shift))));
1449 Set_Print_In_Hex (Mask1);
1452 Lit := Make_Integer_Literal (Loc, Cmask);
1453 Set_Print_In_Hex (Lit);
1456 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1461 Left_Opnd => New_Rhs,
1462 Right_Opnd => Mask1);
1466 -- Then deal with the "or"
1468 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1472 procedure Fixup_Rhs;
1473 -- Adjust Rhs by bias if biased representation for components
1474 -- or remove extraneous high order sign bits if signed.
1476 procedure Fixup_Rhs is
1477 Etyp : constant Entity_Id := Etype (Rhs);
1480 -- For biased case, do the required biasing by simply
1481 -- converting to the biased subtype (the conversion
1482 -- will generate the required bias).
1484 if Has_Biased_Representation (Ctyp) then
1485 Rhs := Convert_To (Ctyp, Rhs);
1487 -- For a signed integer type that is not biased, generate
1488 -- a conversion to unsigned to strip high order sign bits.
1490 elsif Is_Signed_Integer_Type (Ctyp) then
1491 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1494 -- Set Etype, since it can be referenced before the
1495 -- node is completely analyzed.
1497 Set_Etype (Rhs, Etyp);
1499 -- We now need to do an unchecked conversion of the
1500 -- result to the target type, but it is important that
1501 -- this conversion be a right justified conversion and
1502 -- not a left justified conversion.
1504 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1510 and then Compile_Time_Known_Value (Get_Shift)
1513 Make_Integer_Literal (Loc,
1514 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1515 Set_Print_In_Hex (Or_Rhs);
1518 -- We have to convert the right hand side to Etype (Obj).
1519 -- A special case case arises if what we have now is a Val
1520 -- attribute reference whose expression type is Etype (Obj).
1521 -- This happens for assignments of fields from the same
1522 -- array. In this case we get the required right hand side
1523 -- by simply removing the inner attribute reference.
1525 if Nkind (Rhs) = N_Attribute_Reference
1526 and then Attribute_Name (Rhs) = Name_Val
1527 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1529 Rhs := Relocate_Node (First (Expressions (Rhs)));
1532 -- If the value of the right hand side is a known integer
1533 -- value, then just replace it by an untyped constant,
1534 -- which will be properly retyped when we analyze and
1535 -- resolve the expression.
1537 elsif Rhs_Val_Known then
1539 -- Note that Rhs_Val has already been normalized to
1540 -- be an unsigned value with the proper number of bits.
1543 Make_Integer_Literal (Loc, Rhs_Val);
1545 -- Otherwise we need an unchecked conversion
1551 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1554 if Nkind (New_Rhs) = N_Op_And then
1555 Set_Paren_Count (New_Rhs, 1);
1560 Left_Opnd => New_Rhs,
1561 Right_Opnd => Or_Rhs);
1565 -- Now do the rewrite
1568 Make_Assignment_Statement (Loc,
1571 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1572 Set_Assignment_OK (Name (N), Ass_OK);
1574 -- All other component sizes for non-modular case
1579 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1581 -- where Subscr is the computed linear subscript
1584 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1590 if No (Bits_nn) then
1592 -- Error, most likely High_Integrity_Mode restriction
1597 -- Acquire proper Set entity. We use the aligned or unaligned
1598 -- case as appropriate.
1600 if Known_Aligned_Enough (Obj, Csiz) then
1601 Set_nn := RTE (Set_Id (Csiz));
1603 Set_nn := RTE (SetU_Id (Csiz));
1606 -- Now generate the set reference
1608 Obj := Relocate_Node (Prefix (Lhs));
1609 Convert_To_Actual_Subtype (Obj);
1610 Atyp := Etype (Obj);
1611 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1613 -- Below we must make the assumption that Obj is
1614 -- at least byte aligned, since otherwise its address
1615 -- cannot be taken. The assumption holds since the
1616 -- only arrays that can be misaligned are small packed
1617 -- arrays which are implemented as a modular type, and
1618 -- that is not the case here.
1621 Make_Procedure_Call_Statement (Loc,
1622 Name => New_Occurrence_Of (Set_nn, Loc),
1623 Parameter_Associations => New_List (
1624 Make_Attribute_Reference (Loc,
1625 Attribute_Name => Name_Address,
1628 Unchecked_Convert_To (Bits_nn,
1629 Convert_To (Ctyp, Rhs)))));
1634 Analyze (N, Suppress => All_Checks);
1635 end Expand_Bit_Packed_Element_Set;
1637 -------------------------------------
1638 -- Expand_Packed_Address_Reference --
1639 -------------------------------------
1641 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1642 Loc : constant Source_Ptr := Sloc (N);
1654 -- We build up an expression serially that has the form
1656 -- outer_object'Address
1657 -- + (linear-subscript * component_size for each array reference
1658 -- + field'Bit_Position for each record field
1660 -- + ...) / Storage_Unit;
1662 -- Some additional conversions are required to deal with the addition
1663 -- operation, which is not normally visible to generated code.
1666 Ploc := Sloc (Pref);
1668 if Nkind (Pref) = N_Indexed_Component then
1669 Convert_To_Actual_Subtype (Prefix (Pref));
1670 Atyp := Etype (Prefix (Pref));
1671 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1674 Make_Op_Multiply (Ploc,
1675 Left_Opnd => Subscr,
1677 Make_Attribute_Reference (Ploc,
1678 Prefix => New_Occurrence_Of (Atyp, Ploc),
1679 Attribute_Name => Name_Component_Size));
1681 elsif Nkind (Pref) = N_Selected_Component then
1683 Make_Attribute_Reference (Ploc,
1684 Prefix => Selector_Name (Pref),
1685 Attribute_Name => Name_Bit_Position);
1691 Term := Convert_To (RTE (RE_Integer_Address), Term);
1700 Right_Opnd => Term);
1703 Pref := Prefix (Pref);
1707 Unchecked_Convert_To (RTE (RE_Address),
1710 Unchecked_Convert_To (RTE (RE_Integer_Address),
1711 Make_Attribute_Reference (Loc,
1713 Attribute_Name => Name_Address)),
1716 Make_Op_Divide (Loc,
1719 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1721 Analyze_And_Resolve (N, RTE (RE_Address));
1722 end Expand_Packed_Address_Reference;
1724 ------------------------------------
1725 -- Expand_Packed_Boolean_Operator --
1726 ------------------------------------
1728 -- This routine expands "a op b" for the packed cases
1730 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1731 Loc : constant Source_Ptr := Sloc (N);
1732 Typ : constant Entity_Id := Etype (N);
1733 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1734 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1741 Convert_To_Actual_Subtype (L);
1742 Convert_To_Actual_Subtype (R);
1744 Ensure_Defined (Etype (L), N);
1745 Ensure_Defined (Etype (R), N);
1747 Apply_Length_Check (R, Etype (L));
1752 -- First an odd and silly test. We explicitly check for the XOR
1753 -- case where the component type is True .. True, since this will
1754 -- raise constraint error. A special check is required since CE
1755 -- will not be required other wise (cf Expand_Packed_Not).
1757 -- No such check is required for AND and OR, since for both these
1758 -- cases False op False = False, and True op True = True.
1760 if Nkind (N) = N_Op_Xor then
1762 CT : constant Entity_Id := Component_Type (Rtyp);
1763 BT : constant Entity_Id := Base_Type (CT);
1767 Make_Raise_Constraint_Error (Loc,
1773 Make_Attribute_Reference (Loc,
1774 Prefix => New_Occurrence_Of (CT, Loc),
1775 Attribute_Name => Name_First),
1779 New_Occurrence_Of (Standard_True, Loc))),
1784 Make_Attribute_Reference (Loc,
1785 Prefix => New_Occurrence_Of (CT, Loc),
1786 Attribute_Name => Name_Last),
1790 New_Occurrence_Of (Standard_True, Loc)))),
1791 Reason => CE_Range_Check_Failed));
1795 -- Now that that silliness is taken care of, get packed array type
1797 Convert_To_PAT_Type (L);
1798 Convert_To_PAT_Type (R);
1802 -- For the modular case, we expand a op b into
1804 -- rtyp!(pat!(a) op pat!(b))
1806 -- where rtyp is the Etype of the left operand. Note that we do not
1807 -- convert to the base type, since this would be unconstrained, and
1808 -- hence not have a corresponding packed array type set.
1810 -- Note that both operands must be modular for this code to be used
1812 if Is_Modular_Integer_Type (PAT)
1814 Is_Modular_Integer_Type (Etype (R))
1820 if Nkind (N) = N_Op_And then
1821 P := Make_Op_And (Loc, L, R);
1823 elsif Nkind (N) = N_Op_Or then
1824 P := Make_Op_Or (Loc, L, R);
1826 else -- Nkind (N) = N_Op_Xor
1827 P := Make_Op_Xor (Loc, L, R);
1830 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1833 -- For the array case, we insert the actions
1837 -- System.Bitops.Bit_And/Or/Xor
1839 -- Ltype'Length * Ltype'Component_Size;
1841 -- Rtype'Length * Rtype'Component_Size
1844 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1845 -- the second argument and fourth arguments are the lengths of the
1846 -- operands in bits. Then we replace the expression by a reference
1849 -- Note that if we are mixing a modular and array operand, everything
1850 -- works fine, since we ensure that the modular representation has the
1851 -- same physical layout as the array representation (that's what the
1852 -- left justified modular stuff in the big-endian case is about).
1856 Result_Ent : constant Entity_Id :=
1857 Make_Defining_Identifier (Loc,
1858 Chars => New_Internal_Name ('T'));
1863 if Nkind (N) = N_Op_And then
1866 elsif Nkind (N) = N_Op_Or then
1869 else -- Nkind (N) = N_Op_Xor
1873 Insert_Actions (N, New_List (
1875 Make_Object_Declaration (Loc,
1876 Defining_Identifier => Result_Ent,
1877 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1879 Make_Procedure_Call_Statement (Loc,
1880 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1881 Parameter_Associations => New_List (
1883 Make_Byte_Aligned_Attribute_Reference (Loc,
1884 Attribute_Name => Name_Address,
1887 Make_Op_Multiply (Loc,
1889 Make_Attribute_Reference (Loc,
1892 (Etype (First_Index (Ltyp)), Loc),
1893 Attribute_Name => Name_Range_Length),
1895 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1897 Make_Byte_Aligned_Attribute_Reference (Loc,
1898 Attribute_Name => Name_Address,
1901 Make_Op_Multiply (Loc,
1903 Make_Attribute_Reference (Loc,
1906 (Etype (First_Index (Rtyp)), Loc),
1907 Attribute_Name => Name_Range_Length),
1909 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1911 Make_Byte_Aligned_Attribute_Reference (Loc,
1912 Attribute_Name => Name_Address,
1913 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1916 New_Occurrence_Of (Result_Ent, Loc));
1920 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1921 end Expand_Packed_Boolean_Operator;
1923 -------------------------------------
1924 -- Expand_Packed_Element_Reference --
1925 -------------------------------------
1927 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1928 Loc : constant Source_Ptr := Sloc (N);
1940 -- If not bit packed, we have the enumeration case, which is easily
1941 -- dealt with (just adjust the subscripts of the indexed component)
1943 -- Note: this leaves the result as an indexed component, which is
1944 -- still a variable, so can be used in the assignment case, as is
1945 -- required in the enumeration case.
1947 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1948 Setup_Enumeration_Packed_Array_Reference (N);
1952 -- Remaining processing is for the bit-packed case
1954 Obj := Relocate_Node (Prefix (N));
1955 Convert_To_Actual_Subtype (Obj);
1956 Atyp := Etype (Obj);
1957 PAT := Packed_Array_Type (Atyp);
1958 Ctyp := Component_Type (Atyp);
1959 Csiz := UI_To_Int (Component_Size (Atyp));
1961 -- Case of component size 1,2,4 or any component size for the modular
1962 -- case. These are the cases for which we can inline the code.
1964 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1965 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1967 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1968 Lit := Make_Integer_Literal (Loc, Cmask);
1969 Set_Print_In_Hex (Lit);
1971 -- We generate a shift right to position the field, followed by a
1972 -- masking operation to extract the bit field, and we finally do an
1973 -- unchecked conversion to convert the result to the required target.
1975 -- Note that the unchecked conversion automatically deals with the
1976 -- bias if we are dealing with a biased representation. What will
1977 -- happen is that we temporarily generate the biased representation,
1978 -- but almost immediately that will be converted to the original
1979 -- unbiased component type, and the bias will disappear.
1983 Left_Opnd => Make_Shift_Right (Obj, Shift),
1986 -- We neded to analyze this before we do the unchecked convert
1987 -- below, but we need it temporarily attached to the tree for
1988 -- this analysis (hence the temporary Set_Parent call).
1990 Set_Parent (Arg, Parent (N));
1991 Analyze_And_Resolve (Arg);
1994 RJ_Unchecked_Convert_To (Ctyp, Arg));
1996 -- All other component sizes for non-modular case
2001 -- Component_Type!(Get_nn (Arr'address, Subscr))
2003 -- where Subscr is the computed linear subscript
2010 -- Acquire proper Get entity. We use the aligned or unaligned
2011 -- case as appropriate.
2013 if Known_Aligned_Enough (Obj, Csiz) then
2014 Get_nn := RTE (Get_Id (Csiz));
2016 Get_nn := RTE (GetU_Id (Csiz));
2019 -- Now generate the get reference
2021 Compute_Linear_Subscript (Atyp, N, Subscr);
2023 -- Below we make the assumption that Obj is at least byte
2024 -- aligned, since otherwise its address cannot be taken.
2025 -- The assumption holds since the only arrays that can be
2026 -- misaligned are small packed arrays which are implemented
2027 -- as a modular type, and that is not the case here.
2030 Unchecked_Convert_To (Ctyp,
2031 Make_Function_Call (Loc,
2032 Name => New_Occurrence_Of (Get_nn, Loc),
2033 Parameter_Associations => New_List (
2034 Make_Attribute_Reference (Loc,
2035 Attribute_Name => Name_Address,
2041 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2043 end Expand_Packed_Element_Reference;
2045 ----------------------
2046 -- Expand_Packed_Eq --
2047 ----------------------
2049 -- Handles expansion of "=" on packed array types
2051 procedure Expand_Packed_Eq (N : Node_Id) is
2052 Loc : constant Source_Ptr := Sloc (N);
2053 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2054 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2064 Convert_To_Actual_Subtype (L);
2065 Convert_To_Actual_Subtype (R);
2066 Ltyp := Underlying_Type (Etype (L));
2067 Rtyp := Underlying_Type (Etype (R));
2069 Convert_To_PAT_Type (L);
2070 Convert_To_PAT_Type (R);
2074 Make_Op_Multiply (Loc,
2076 Make_Attribute_Reference (Loc,
2077 Attribute_Name => Name_Length,
2078 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2080 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2083 Make_Op_Multiply (Loc,
2085 Make_Attribute_Reference (Loc,
2086 Attribute_Name => Name_Length,
2087 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2089 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2091 -- For the modular case, we transform the comparison to:
2093 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2095 -- where PAT is the packed array type. This works fine, since in the
2096 -- modular case we guarantee that the unused bits are always zeroes.
2097 -- We do have to compare the lengths because we could be comparing
2098 -- two different subtypes of the same base type.
2100 if Is_Modular_Integer_Type (PAT) then
2105 Left_Opnd => LLexpr,
2106 Right_Opnd => RLexpr),
2113 -- For the non-modular case, we call a runtime routine
2115 -- System.Bit_Ops.Bit_Eq
2116 -- (L'Address, L_Length, R'Address, R_Length)
2118 -- where PAT is the packed array type, and the lengths are the lengths
2119 -- in bits of the original packed arrays. This routine takes care of
2120 -- not comparing the unused bits in the last byte.
2124 Make_Function_Call (Loc,
2125 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2126 Parameter_Associations => New_List (
2127 Make_Byte_Aligned_Attribute_Reference (Loc,
2128 Attribute_Name => Name_Address,
2133 Make_Byte_Aligned_Attribute_Reference (Loc,
2134 Attribute_Name => Name_Address,
2140 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2141 end Expand_Packed_Eq;
2143 -----------------------
2144 -- Expand_Packed_Not --
2145 -----------------------
2147 -- Handles expansion of "not" on packed array types
2149 procedure Expand_Packed_Not (N : Node_Id) is
2150 Loc : constant Source_Ptr := Sloc (N);
2151 Typ : constant Entity_Id := Etype (N);
2152 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2159 Convert_To_Actual_Subtype (Opnd);
2160 Rtyp := Etype (Opnd);
2162 -- First an odd and silly test. We explicitly check for the case
2163 -- where the 'First of the component type is equal to the 'Last of
2164 -- this component type, and if this is the case, we make sure that
2165 -- constraint error is raised. The reason is that the NOT is bound
2166 -- to cause CE in this case, and we will not otherwise catch it.
2168 -- Believe it or not, this was reported as a bug. Note that nearly
2169 -- always, the test will evaluate statically to False, so the code
2170 -- will be statically removed, and no extra overhead caused.
2173 CT : constant Entity_Id := Component_Type (Rtyp);
2177 Make_Raise_Constraint_Error (Loc,
2181 Make_Attribute_Reference (Loc,
2182 Prefix => New_Occurrence_Of (CT, Loc),
2183 Attribute_Name => Name_First),
2186 Make_Attribute_Reference (Loc,
2187 Prefix => New_Occurrence_Of (CT, Loc),
2188 Attribute_Name => Name_Last)),
2189 Reason => CE_Range_Check_Failed));
2192 -- Now that that silliness is taken care of, get packed array type
2194 Convert_To_PAT_Type (Opnd);
2195 PAT := Etype (Opnd);
2197 -- For the case where the packed array type is a modular type,
2198 -- not A expands simply into:
2200 -- rtyp!(PAT!(A) xor mask)
2202 -- where PAT is the packed array type, and mask is a mask of all
2203 -- one bits of length equal to the size of this packed type and
2204 -- rtyp is the actual subtype of the operand
2206 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2207 Set_Print_In_Hex (Lit);
2209 if not Is_Array_Type (PAT) then
2211 Unchecked_Convert_To (Rtyp,
2214 Right_Opnd => Lit)));
2216 -- For the array case, we insert the actions
2220 -- System.Bitops.Bit_Not
2222 -- Typ'Length * Typ'Component_Size;
2225 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2226 -- argument is the length of the operand in bits. Then we replace
2227 -- the expression by a reference to Result.
2231 Result_Ent : constant Entity_Id :=
2232 Make_Defining_Identifier (Loc,
2233 Chars => New_Internal_Name ('T'));
2236 Insert_Actions (N, New_List (
2238 Make_Object_Declaration (Loc,
2239 Defining_Identifier => Result_Ent,
2240 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2242 Make_Procedure_Call_Statement (Loc,
2243 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2244 Parameter_Associations => New_List (
2246 Make_Byte_Aligned_Attribute_Reference (Loc,
2247 Attribute_Name => Name_Address,
2250 Make_Op_Multiply (Loc,
2252 Make_Attribute_Reference (Loc,
2255 (Etype (First_Index (Rtyp)), Loc),
2256 Attribute_Name => Name_Range_Length),
2258 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2260 Make_Byte_Aligned_Attribute_Reference (Loc,
2261 Attribute_Name => Name_Address,
2262 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2265 New_Occurrence_Of (Result_Ent, Loc));
2269 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2271 end Expand_Packed_Not;
2273 -------------------------------------
2274 -- Involves_Packed_Array_Reference --
2275 -------------------------------------
2277 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2279 if Nkind (N) = N_Indexed_Component
2280 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2284 elsif Nkind (N) = N_Selected_Component then
2285 return Involves_Packed_Array_Reference (Prefix (N));
2290 end Involves_Packed_Array_Reference;
2292 --------------------------
2293 -- Known_Aligned_Enough --
2294 --------------------------
2296 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2297 Typ : constant Entity_Id := Etype (Obj);
2299 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2300 -- If the component is in a record that contains previous packed
2301 -- components, consider it unaligned because the back-end might
2302 -- choose to pack the rest of the record. Lead to less efficient code,
2303 -- but safer vis-a-vis of back-end choices.
2305 --------------------------------
2306 -- In_Partially_Packed_Record --
2307 --------------------------------
2309 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2310 Rec_Type : constant Entity_Id := Scope (Comp);
2311 Prev_Comp : Entity_Id;
2314 Prev_Comp := First_Entity (Rec_Type);
2315 while Present (Prev_Comp) loop
2316 if Is_Packed (Etype (Prev_Comp)) then
2319 elsif Prev_Comp = Comp then
2323 Next_Entity (Prev_Comp);
2327 end In_Partially_Packed_Record;
2329 -- Start of processing for Known_Aligned_Enough
2332 -- Odd bit sizes don't need alignment anyway
2334 if Csiz mod 2 = 1 then
2337 -- If we have a specified alignment, see if it is sufficient, if not
2338 -- then we can't possibly be aligned enough in any case.
2340 elsif Known_Alignment (Etype (Obj)) then
2341 -- Alignment required is 4 if size is a multiple of 4, and
2342 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2344 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2349 -- OK, alignment should be sufficient, if object is aligned
2351 -- If object is strictly aligned, then it is definitely aligned
2353 if Strict_Alignment (Typ) then
2356 -- Case of subscripted array reference
2358 elsif Nkind (Obj) = N_Indexed_Component then
2360 -- If we have a pointer to an array, then this is definitely
2361 -- aligned, because pointers always point to aligned versions.
2363 if Is_Access_Type (Etype (Prefix (Obj))) then
2366 -- Otherwise, go look at the prefix
2369 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2372 -- Case of record field
2374 elsif Nkind (Obj) = N_Selected_Component then
2376 -- What is significant here is whether the record type is packed
2378 if Is_Record_Type (Etype (Prefix (Obj)))
2379 and then Is_Packed (Etype (Prefix (Obj)))
2383 -- Or the component has a component clause which might cause
2384 -- the component to become unaligned (we can't tell if the
2385 -- backend is doing alignment computations).
2387 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2390 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2393 -- In all other cases, go look at prefix
2396 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2399 elsif Nkind (Obj) = N_Type_Conversion then
2400 return Known_Aligned_Enough (Expression (Obj), Csiz);
2402 -- For a formal parameter, it is safer to assume that it is not
2403 -- aligned, because the formal may be unconstrained while the actual
2404 -- is constrained. In this situation, a small constrained packed
2405 -- array, represented in modular form, may be unaligned.
2407 elsif Is_Entity_Name (Obj) then
2408 return not Is_Formal (Entity (Obj));
2411 -- If none of the above, must be aligned
2414 end Known_Aligned_Enough;
2416 ---------------------
2417 -- Make_Shift_Left --
2418 ---------------------
2420 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2424 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2428 Make_Op_Shift_Left (Sloc (N),
2431 Set_Shift_Count_OK (Nod, True);
2434 end Make_Shift_Left;
2436 ----------------------
2437 -- Make_Shift_Right --
2438 ----------------------
2440 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2444 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2448 Make_Op_Shift_Right (Sloc (N),
2451 Set_Shift_Count_OK (Nod, True);
2454 end Make_Shift_Right;
2456 -----------------------------
2457 -- RJ_Unchecked_Convert_To --
2458 -----------------------------
2460 function RJ_Unchecked_Convert_To
2462 Expr : Node_Id) return Node_Id
2464 Source_Typ : constant Entity_Id := Etype (Expr);
2465 Target_Typ : constant Entity_Id := Typ;
2467 Src : Node_Id := Expr;
2473 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2474 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2476 -- First step, if the source type is not a discrete type, then we
2477 -- first convert to a modular type of the source length, since
2478 -- otherwise, on a big-endian machine, we get left-justification.
2479 -- We do it for little-endian machines as well, because there might
2480 -- be junk bits that are not cleared if the type is not numeric.
2482 if Source_Siz /= Target_Siz
2483 and then not Is_Discrete_Type (Source_Typ)
2485 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2488 -- In the big endian case, if the lengths of the two types differ,
2489 -- then we must worry about possible left justification in the
2490 -- conversion, and avoiding that is what this is all about.
2492 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2494 -- Next step. If the target is not a discrete type, then we first
2495 -- convert to a modular type of the target length, since
2496 -- otherwise, on a big-endian machine, we get left-justification.
2498 if not Is_Discrete_Type (Target_Typ) then
2499 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2503 -- And now we can do the final conversion to the target type
2505 return Unchecked_Convert_To (Target_Typ, Src);
2506 end RJ_Unchecked_Convert_To;
2508 ----------------------------------------------
2509 -- Setup_Enumeration_Packed_Array_Reference --
2510 ----------------------------------------------
2512 -- All we have to do here is to find the subscripts that correspond
2513 -- to the index positions that have non-standard enumeration types
2514 -- and insert a Pos attribute to get the proper subscript value.
2516 -- Finally the prefix must be uncheck converted to the corresponding
2517 -- packed array type.
2519 -- Note that the component type is unchanged, so we do not need to
2520 -- fiddle with the types (Gigi always automatically takes the packed
2521 -- array type if it is set, as it will be in this case).
2523 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2524 Pfx : constant Node_Id := Prefix (N);
2525 Typ : constant Entity_Id := Etype (N);
2526 Exprs : constant List_Id := Expressions (N);
2530 -- If the array is unconstrained, then we replace the array
2531 -- reference with its actual subtype. This actual subtype will
2532 -- have a packed array type with appropriate bounds.
2534 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2535 Convert_To_Actual_Subtype (Pfx);
2538 Expr := First (Exprs);
2539 while Present (Expr) loop
2541 Loc : constant Source_Ptr := Sloc (Expr);
2542 Expr_Typ : constant Entity_Id := Etype (Expr);
2545 if Is_Enumeration_Type (Expr_Typ)
2546 and then Has_Non_Standard_Rep (Expr_Typ)
2549 Make_Attribute_Reference (Loc,
2550 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2551 Attribute_Name => Name_Pos,
2552 Expressions => New_List (Relocate_Node (Expr))));
2553 Analyze_And_Resolve (Expr, Standard_Natural);
2561 Make_Indexed_Component (Sloc (N),
2563 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2564 Expressions => Exprs));
2566 Analyze_And_Resolve (N, Typ);
2568 end Setup_Enumeration_Packed_Array_Reference;
2570 -----------------------------------------
2571 -- Setup_Inline_Packed_Array_Reference --
2572 -----------------------------------------
2574 procedure Setup_Inline_Packed_Array_Reference
2577 Obj : in out Node_Id;
2579 Shift : out Node_Id)
2581 Loc : constant Source_Ptr := Sloc (N);
2588 Csiz := Component_Size (Atyp);
2590 Convert_To_PAT_Type (Obj);
2593 Cmask := 2 ** Csiz - 1;
2595 if Is_Array_Type (PAT) then
2596 Otyp := Component_Type (PAT);
2597 Osiz := Component_Size (PAT);
2602 -- In the case where the PAT is a modular type, we want the actual
2603 -- size in bits of the modular value we use. This is neither the
2604 -- Object_Size nor the Value_Size, either of which may have been
2605 -- reset to strange values, but rather the minimum size. Note that
2606 -- since this is a modular type with full range, the issue of
2607 -- biased representation does not arise.
2609 Osiz := UI_From_Int (Minimum_Size (Otyp));
2612 Compute_Linear_Subscript (Atyp, N, Shift);
2614 -- If the component size is not 1, then the subscript must be
2615 -- multiplied by the component size to get the shift count.
2619 Make_Op_Multiply (Loc,
2620 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2621 Right_Opnd => Shift);
2624 -- If we have the array case, then this shift count must be broken
2625 -- down into a byte subscript, and a shift within the byte.
2627 if Is_Array_Type (PAT) then
2630 New_Shift : Node_Id;
2633 -- We must analyze shift, since we will duplicate it
2635 Set_Parent (Shift, N);
2637 (Shift, Standard_Integer, Suppress => All_Checks);
2639 -- The shift count within the word is
2644 Left_Opnd => Duplicate_Subexpr (Shift),
2645 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2647 -- The subscript to be used on the PAT array is
2651 Make_Indexed_Component (Loc,
2653 Expressions => New_List (
2654 Make_Op_Divide (Loc,
2655 Left_Opnd => Duplicate_Subexpr (Shift),
2656 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2661 -- For the modular integer case, the object to be manipulated is
2662 -- the entire array, so Obj is unchanged. Note that we will reset
2663 -- its type to PAT before returning to the caller.
2669 -- The one remaining step is to modify the shift count for the
2670 -- big-endian case. Consider the following example in a byte:
2672 -- xxxxxxxx bits of byte
2673 -- vvvvvvvv bits of value
2674 -- 33221100 little-endian numbering
2675 -- 00112233 big-endian numbering
2677 -- Here we have the case of 2-bit fields
2679 -- For the little-endian case, we already have the proper shift
2680 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2682 -- For the big endian case, we have to adjust the shift count,
2683 -- computing it as (N - F) - shift, where N is the number of bits
2684 -- in an element of the array used to implement the packed array,
2685 -- F is the number of bits in a source level array element, and
2686 -- shift is the count so far computed.
2688 if Bytes_Big_Endian then
2690 Make_Op_Subtract (Loc,
2691 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2692 Right_Opnd => Shift);
2695 Set_Parent (Shift, N);
2696 Set_Parent (Obj, N);
2697 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2698 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2700 -- Make sure final type of object is the appropriate packed type
2702 Set_Etype (Obj, Otyp);
2704 end Setup_Inline_Packed_Array_Reference;