1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Layout; use Layout;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
37 with Rtsfind; use Rtsfind;
39 with Sem_Aux; use Sem_Aux;
40 with Sem_Ch3; use Sem_Ch3;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Ch13; use Sem_Ch13;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sinfo; use Sinfo;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Targparm; use Targparm;
50 with Tbuild; use Tbuild;
51 with Ttypes; use Ttypes;
52 with Uintp; use Uintp;
54 package body Exp_Pakd is
56 ---------------------------
57 -- Endian Considerations --
58 ---------------------------
60 -- As described in the specification, bit numbering in a packed array
61 -- is consistent with bit numbering in a record representation clause,
62 -- and hence dependent on the endianness of the machine:
64 -- For little-endian machines, element zero is at the right hand end
65 -- (low order end) of a bit field.
67 -- For big-endian machines, element zero is at the left hand end
68 -- (high order end) of a bit field.
70 -- The shifts that are used to right justify a field therefore differ
71 -- in the two cases. For the little-endian case, we can simply use the
72 -- bit number (i.e. the element number * element size) as the count for
73 -- a right shift. For the big-endian case, we have to subtract the shift
74 -- count from an appropriate constant to use in the right shift. We use
75 -- rotates instead of shifts (which is necessary in the store case to
76 -- preserve other fields), and we expect that the backend will be able
77 -- to change the right rotate into a left rotate, avoiding the subtract,
78 -- if the architecture provides such an instruction.
80 ----------------------------------------------
81 -- Entity Tables for Packed Access Routines --
82 ----------------------------------------------
84 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
85 -- library routines. This table is used to obtain the entity for the
88 type E_Array is array (Int range 01 .. 63) of RE_Id;
90 -- Array of Bits_nn entities. Note that we do not use library routines
91 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
92 -- entries from System.Unsigned, because we also use this table for
93 -- certain special unchecked conversions in the big-endian case.
95 Bits_Id : constant E_Array :=
111 16 => RE_Unsigned_16,
127 32 => RE_Unsigned_32,
160 -- Array of Get routine entities. These are used to obtain an element
161 -- from a packed array. The N'th entry is used to obtain elements from
162 -- a packed array whose component size is N. RE_Null is used as a null
163 -- entry, for the cases where a library routine is not used.
165 Get_Id : constant E_Array :=
230 -- Array of Get routine entities to be used in the case where the packed
231 -- array is itself a component of a packed structure, and therefore may
232 -- not be fully aligned. This only affects the even sizes, since for the
233 -- odd sizes, we do not get any fixed alignment in any case.
235 GetU_Id : constant E_Array :=
300 -- Array of Set routine entities. These are used to assign an element
301 -- of a packed array. The N'th entry is used to assign elements for
302 -- a packed array whose component size is N. RE_Null is used as a null
303 -- entry, for the cases where a library routine is not used.
305 Set_Id : constant E_Array :=
370 -- Array of Set routine entities to be used in the case where the packed
371 -- array is itself a component of a packed structure, and therefore may
372 -- not be fully aligned. This only affects the even sizes, since for the
373 -- odd sizes, we do not get any fixed alignment in any case.
375 SetU_Id : constant E_Array :=
440 -----------------------
441 -- Local Subprograms --
442 -----------------------
444 procedure Compute_Linear_Subscript
447 Subscr : out Node_Id);
448 -- Given a constrained array type Atyp, and an indexed component node
449 -- N referencing an array object of this type, build an expression of
450 -- type Standard.Integer representing the zero-based linear subscript
451 -- value. This expression includes any required range checks.
453 procedure Convert_To_PAT_Type (Aexp : Node_Id);
454 -- Given an expression of a packed array type, builds a corresponding
455 -- expression whose type is the implementation type used to represent
456 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
458 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
459 -- There are two versions of the Set routines, the ones used when the
460 -- object is known to be sufficiently well aligned given the number of
461 -- bits, and the ones used when the object is not known to be aligned.
462 -- This routine is used to determine which set to use. Obj is a reference
463 -- to the object, and Csiz is the component size of the packed array.
464 -- True is returned if the alignment of object is known to be sufficient,
465 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
468 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
469 -- Build a left shift node, checking for the case of a shift count of zero
471 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
472 -- Build a right shift node, checking for the case of a shift count of zero
474 function RJ_Unchecked_Convert_To
476 Expr : Node_Id) return Node_Id;
477 -- The packed array code does unchecked conversions which in some cases
478 -- may involve non-discrete types with differing sizes. The semantics of
479 -- such conversions is potentially endian dependent, and the effect we
480 -- want here for such a conversion is to do the conversion in size as
481 -- though numeric items are involved, and we extend or truncate on the
482 -- left side. This happens naturally in the little-endian case, but in
483 -- the big endian case we can get left justification, when what we want
484 -- is right justification. This routine does the unchecked conversion in
485 -- a stepwise manner to ensure that it gives the expected result. Hence
486 -- the name (RJ = Right justified). The parameters Typ and Expr are as
487 -- for the case of a normal Unchecked_Convert_To call.
489 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
490 -- This routine is called in the Get and Set case for arrays that are
491 -- packed but not bit-packed, meaning that they have at least one
492 -- subscript that is of an enumeration type with a non-standard
493 -- representation. This routine modifies the given node to properly
494 -- reference the corresponding packed array type.
496 procedure Setup_Inline_Packed_Array_Reference
499 Obj : in out Node_Id;
501 Shift : out Node_Id);
502 -- This procedure performs common processing on the N_Indexed_Component
503 -- parameter given as N, whose prefix is a reference to a packed array.
504 -- This is used for the get and set when the component size is 1,2,4
505 -- or for other component sizes when the packed array type is a modular
506 -- type (i.e. the cases that are handled with inline code).
510 -- N is the N_Indexed_Component node for the packed array reference
512 -- Atyp is the constrained array type (the actual subtype has been
513 -- computed if necessary to obtain the constraints, but this is still
514 -- the original array type, not the Packed_Array_Type value).
516 -- Obj is the object which is to be indexed. It is always of type Atyp.
520 -- Obj is the object containing the desired bit field. It is of type
521 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
522 -- entire value, for the small static case, or the proper selected byte
523 -- from the array in the large or dynamic case. This node is analyzed
524 -- and resolved on return.
526 -- Shift is a node representing the shift count to be used in the
527 -- rotate right instruction that positions the field for access.
528 -- This node is analyzed and resolved on return.
530 -- Cmask is a mask corresponding to the width of the component field.
531 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
533 -- Note: in some cases the call to this routine may generate actions
534 -- (for handling multi-use references and the generation of the packed
535 -- array type on the fly). Such actions are inserted into the tree
536 -- directly using Insert_Action.
538 ------------------------------
539 -- Compute_Linear_Subscript --
540 ------------------------------
542 procedure Compute_Linear_Subscript
545 Subscr : out Node_Id)
547 Loc : constant Source_Ptr := Sloc (N);
556 -- Loop through dimensions
558 Indx := First_Index (Atyp);
559 Oldsub := First (Expressions (N));
561 while Present (Indx) loop
562 Styp := Etype (Indx);
563 Newsub := Relocate_Node (Oldsub);
565 -- Get expression for the subscript value. First, if Do_Range_Check
566 -- is set on a subscript, then we must do a range check against the
567 -- original bounds (not the bounds of the packed array type). We do
568 -- this by introducing a subtype conversion.
570 if Do_Range_Check (Newsub)
571 and then Etype (Newsub) /= Styp
573 Newsub := Convert_To (Styp, Newsub);
576 -- Now evolve the expression for the subscript. First convert
577 -- the subscript to be zero based and of an integer type.
579 -- Case of integer type, where we just subtract to get lower bound
581 if Is_Integer_Type (Styp) then
583 -- If length of integer type is smaller than standard integer,
584 -- then we convert to integer first, then do the subtract
586 -- Integer (subscript) - Integer (Styp'First)
588 if Esize (Styp) < Esize (Standard_Integer) then
590 Make_Op_Subtract (Loc,
591 Left_Opnd => Convert_To (Standard_Integer, Newsub),
593 Convert_To (Standard_Integer,
594 Make_Attribute_Reference (Loc,
595 Prefix => New_Occurrence_Of (Styp, Loc),
596 Attribute_Name => Name_First)));
598 -- For larger integer types, subtract first, then convert to
599 -- integer, this deals with strange long long integer bounds.
601 -- Integer (subscript - Styp'First)
605 Convert_To (Standard_Integer,
606 Make_Op_Subtract (Loc,
609 Make_Attribute_Reference (Loc,
610 Prefix => New_Occurrence_Of (Styp, Loc),
611 Attribute_Name => Name_First)));
614 -- For the enumeration case, we have to use 'Pos to get the value
615 -- to work with before subtracting the lower bound.
617 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
619 -- This is not quite right for bizarre cases where the size of the
620 -- enumeration type is > Integer'Size bits due to rep clause ???
623 pragma Assert (Is_Enumeration_Type (Styp));
626 Make_Op_Subtract (Loc,
627 Left_Opnd => Convert_To (Standard_Integer,
628 Make_Attribute_Reference (Loc,
629 Prefix => New_Occurrence_Of (Styp, Loc),
630 Attribute_Name => Name_Pos,
631 Expressions => New_List (Newsub))),
634 Convert_To (Standard_Integer,
635 Make_Attribute_Reference (Loc,
636 Prefix => New_Occurrence_Of (Styp, Loc),
637 Attribute_Name => Name_Pos,
638 Expressions => New_List (
639 Make_Attribute_Reference (Loc,
640 Prefix => New_Occurrence_Of (Styp, Loc),
641 Attribute_Name => Name_First)))));
644 Set_Paren_Count (Newsub, 1);
646 -- For the first subscript, we just copy that subscript value
651 -- Otherwise, we must multiply what we already have by the current
652 -- stride and then add in the new value to the evolving subscript.
658 Make_Op_Multiply (Loc,
661 Make_Attribute_Reference (Loc,
662 Attribute_Name => Name_Range_Length,
663 Prefix => New_Occurrence_Of (Styp, Loc))),
664 Right_Opnd => Newsub);
667 -- Move to next subscript
672 end Compute_Linear_Subscript;
674 -------------------------
675 -- Convert_To_PAT_Type --
676 -------------------------
678 -- The PAT is always obtained from the actual subtype
680 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
684 Convert_To_Actual_Subtype (Aexp);
685 Act_ST := Underlying_Type (Etype (Aexp));
686 Create_Packed_Array_Type (Act_ST);
688 -- Just replace the etype with the packed array type. This works because
689 -- the expression will not be further analyzed, and Gigi considers the
690 -- two types equivalent in any case.
692 -- This is not strictly the case ??? If the reference is an actual in
693 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
694 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
695 -- array reference, reanalysis can produce spurious type errors when the
696 -- PAT type is replaced again with the original type of the array. Same
697 -- for the case of a dereference. The following is correct and minimal,
698 -- but the handling of more complex packed expressions in actuals is
699 -- confused. Probably the problem only remains for actuals in calls.
701 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
703 if Is_Entity_Name (Aexp)
705 (Nkind (Aexp) = N_Indexed_Component
706 and then Is_Entity_Name (Prefix (Aexp)))
707 or else Nkind (Aexp) = N_Explicit_Dereference
711 end Convert_To_PAT_Type;
713 ------------------------------
714 -- Create_Packed_Array_Type --
715 ------------------------------
717 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
718 Loc : constant Source_Ptr := Sloc (Typ);
719 Ctyp : constant Entity_Id := Component_Type (Typ);
720 Csize : constant Uint := Component_Size (Typ);
735 procedure Install_PAT;
736 -- This procedure is called with Decl set to the declaration for the
737 -- packed array type. It creates the type and installs it as required.
739 procedure Set_PB_Type;
740 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
741 -- requirements (see documentation in the spec of this package).
747 procedure Install_PAT is
748 Pushed_Scope : Boolean := False;
751 -- We do not want to put the declaration we have created in the tree
752 -- since it is often hard, and sometimes impossible to find a proper
753 -- place for it (the impossible case arises for a packed array type
754 -- with bounds depending on the discriminant, a declaration cannot
755 -- be put inside the record, and the reference to the discriminant
756 -- cannot be outside the record).
758 -- The solution is to analyze the declaration while temporarily
759 -- attached to the tree at an appropriate point, and then we install
760 -- the resulting type as an Itype in the packed array type field of
761 -- the original type, so that no explicit declaration is required.
763 -- Note: the packed type is created in the scope of its parent
764 -- type. There are at least some cases where the current scope
765 -- is deeper, and so when this is the case, we temporarily reset
766 -- the scope for the definition. This is clearly safe, since the
767 -- first use of the packed array type will be the implicit
768 -- reference from the corresponding unpacked type when it is
771 if Is_Itype (Typ) then
772 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
774 Set_Parent (Decl, Declaration_Node (Typ));
777 if Scope (Typ) /= Current_Scope then
778 Push_Scope (Scope (Typ));
779 Pushed_Scope := True;
782 Set_Is_Itype (PAT, True);
783 Set_Packed_Array_Type (Typ, PAT);
784 Analyze (Decl, Suppress => All_Checks);
790 -- Set Esize and RM_Size to the actual size of the packed object
791 -- Do not reset RM_Size if already set, as happens in the case of
794 if Unknown_Esize (PAT) then
795 Set_Esize (PAT, PASize);
798 if Unknown_RM_Size (PAT) then
799 Set_RM_Size (PAT, PASize);
802 Adjust_Esize_Alignment (PAT);
804 -- Set remaining fields of packed array type
806 Init_Alignment (PAT);
807 Set_Parent (PAT, Empty);
808 Set_Associated_Node_For_Itype (PAT, Typ);
809 Set_Is_Packed_Array_Type (PAT, True);
810 Set_Original_Array_Type (PAT, Typ);
812 -- We definitely do not want to delay freezing for packed array
813 -- types. This is of particular importance for the itypes that
814 -- are generated for record components depending on discriminants
815 -- where there is no place to put the freeze node.
817 Set_Has_Delayed_Freeze (PAT, False);
818 Set_Has_Delayed_Freeze (Etype (PAT), False);
820 -- If we did allocate a freeze node, then clear out the reference
821 -- since it is obsolete (should we delete the freeze node???)
823 Set_Freeze_Node (PAT, Empty);
824 Set_Freeze_Node (Etype (PAT), Empty);
831 procedure Set_PB_Type is
833 -- If the user has specified an explicit alignment for the
834 -- type or component, take it into account.
836 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
837 or else Alignment (Typ) = 1
838 or else Component_Alignment (Typ) = Calign_Storage_Unit
840 PB_Type := RTE (RE_Packed_Bytes1);
842 elsif Csize mod 4 /= 0
843 or else Alignment (Typ) = 2
845 PB_Type := RTE (RE_Packed_Bytes2);
848 PB_Type := RTE (RE_Packed_Bytes4);
852 -- Start of processing for Create_Packed_Array_Type
855 -- If we already have a packed array type, nothing to do
857 if Present (Packed_Array_Type (Typ)) then
861 -- If our immediate ancestor subtype is constrained, and it already
862 -- has a packed array type, then just share the same type, since the
863 -- bounds must be the same. If the ancestor is not an array type but
864 -- a private type, as can happen with multiple instantiations, create
865 -- a new packed type, to avoid privacy issues.
867 if Ekind (Typ) = E_Array_Subtype then
868 Ancest := Ancestor_Subtype (Typ);
871 and then Is_Array_Type (Ancest)
872 and then Is_Constrained (Ancest)
873 and then Present (Packed_Array_Type (Ancest))
875 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
880 -- We preset the result type size from the size of the original array
881 -- type, since this size clearly belongs to the packed array type. The
882 -- size of the conceptual unpacked type is always set to unknown.
884 PASize := RM_Size (Typ);
886 -- Case of an array where at least one index is of an enumeration
887 -- type with a non-standard representation, but the component size
888 -- is not appropriate for bit packing. This is the case where we
889 -- have Is_Packed set (we would never be in this unit otherwise),
890 -- but Is_Bit_Packed_Array is false.
892 -- Note that if the component size is appropriate for bit packing,
893 -- then the circuit for the computation of the subscript properly
894 -- deals with the non-standard enumeration type case by taking the
897 if not Is_Bit_Packed_Array (Typ) then
899 -- Here we build a declaration:
901 -- type tttP is array (index1, index2, ...) of component_type
903 -- where index1, index2, are the index types. These are the same
904 -- as the index types of the original array, except for the non-
905 -- standard representation enumeration type case, where we have
908 -- For the unconstrained array case, we use
912 -- For the constrained case, we use
914 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
915 -- Enum_Type'Pos (Enum_Type'Last);
918 Make_Defining_Identifier (Loc,
919 Chars => New_External_Name (Chars (Typ), 'P'));
921 Set_Packed_Array_Type (Typ, PAT);
924 Indexes : constant List_Id := New_List;
926 Indx_Typ : Entity_Id;
931 Indx := First_Index (Typ);
933 while Present (Indx) loop
934 Indx_Typ := Etype (Indx);
936 Enum_Case := Is_Enumeration_Type (Indx_Typ)
937 and then Has_Non_Standard_Rep (Indx_Typ);
939 -- Unconstrained case
941 if not Is_Constrained (Typ) then
943 Indx_Typ := Standard_Natural;
946 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
951 if not Enum_Case then
952 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
956 Make_Subtype_Indication (Loc,
958 New_Occurrence_Of (Standard_Natural, Loc),
960 Make_Range_Constraint (Loc,
964 Make_Attribute_Reference (Loc,
966 New_Occurrence_Of (Indx_Typ, Loc),
967 Attribute_Name => Name_Pos,
968 Expressions => New_List (
969 Make_Attribute_Reference (Loc,
971 New_Occurrence_Of (Indx_Typ, Loc),
972 Attribute_Name => Name_First))),
975 Make_Attribute_Reference (Loc,
977 New_Occurrence_Of (Indx_Typ, Loc),
978 Attribute_Name => Name_Pos,
979 Expressions => New_List (
980 Make_Attribute_Reference (Loc,
982 New_Occurrence_Of (Indx_Typ, Loc),
983 Attribute_Name => Name_Last)))))));
991 if not Is_Constrained (Typ) then
993 Make_Unconstrained_Array_Definition (Loc,
994 Subtype_Marks => Indexes,
995 Component_Definition =>
996 Make_Component_Definition (Loc,
997 Aliased_Present => False,
998 Subtype_Indication =>
999 New_Occurrence_Of (Ctyp, Loc)));
1003 Make_Constrained_Array_Definition (Loc,
1004 Discrete_Subtype_Definitions => Indexes,
1005 Component_Definition =>
1006 Make_Component_Definition (Loc,
1007 Aliased_Present => False,
1008 Subtype_Indication =>
1009 New_Occurrence_Of (Ctyp, Loc)));
1013 Make_Full_Type_Declaration (Loc,
1014 Defining_Identifier => PAT,
1015 Type_Definition => Typedef);
1018 -- Set type as packed array type and install it
1020 Set_Is_Packed_Array_Type (PAT);
1024 -- Case of bit-packing required for unconstrained array. We create
1025 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1027 elsif not Is_Constrained (Typ) then
1029 Make_Defining_Identifier (Loc,
1030 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1032 Set_Packed_Array_Type (Typ, PAT);
1036 Make_Subtype_Declaration (Loc,
1037 Defining_Identifier => PAT,
1038 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1042 -- Remaining code is for the case of bit-packing for constrained array
1044 -- The name of the packed array subtype is
1048 -- where sss is the component size in bits and ttt is the name of
1049 -- the parent packed type.
1053 Make_Defining_Identifier (Loc,
1054 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1056 Set_Packed_Array_Type (Typ, PAT);
1058 -- Build an expression for the length of the array in bits.
1059 -- This is the product of the length of each of the dimensions
1065 Len_Expr := Empty; -- suppress junk warning
1069 Make_Attribute_Reference (Loc,
1070 Attribute_Name => Name_Length,
1071 Prefix => New_Occurrence_Of (Typ, Loc),
1072 Expressions => New_List (
1073 Make_Integer_Literal (Loc, J)));
1076 Len_Expr := Len_Dim;
1080 Make_Op_Multiply (Loc,
1081 Left_Opnd => Len_Expr,
1082 Right_Opnd => Len_Dim);
1086 exit when J > Number_Dimensions (Typ);
1090 -- Temporarily attach the length expression to the tree and analyze
1091 -- and resolve it, so that we can test its value. We assume that the
1092 -- total length fits in type Integer. This expression may involve
1093 -- discriminants, so we treat it as a default/per-object expression.
1095 Set_Parent (Len_Expr, Typ);
1096 Preanalyze_Spec_Expression (Len_Expr, Standard_Long_Long_Integer);
1098 -- Use a modular type if possible. We can do this if we have
1099 -- static bounds, and the length is small enough, and the length
1100 -- is not zero. We exclude the zero length case because the size
1101 -- of things is always at least one, and the zero length object
1102 -- would have an anomalous size.
1104 if Compile_Time_Known_Value (Len_Expr) then
1105 Len_Bits := Expr_Value (Len_Expr) * Csize;
1107 -- Check for size known to be too large
1110 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1112 if System_Storage_Unit = 8 then
1114 ("packed array size cannot exceed " &
1115 "Integer''Last bytes", Typ);
1118 ("packed array size cannot exceed " &
1119 "Integer''Last storage units", Typ);
1122 -- Reset length to arbitrary not too high value to continue
1124 Len_Expr := Make_Integer_Literal (Loc, 65535);
1125 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1128 -- We normally consider small enough to mean no larger than the
1129 -- value of System_Max_Binary_Modulus_Power, checking that in the
1130 -- case of values longer than word size, we have long shifts.
1134 (Len_Bits <= System_Word_Size
1135 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1136 and then Support_Long_Shifts_On_Target))
1138 -- We can use the modular type, it has the form:
1140 -- subtype tttPn is btyp
1141 -- range 0 .. 2 ** ((Typ'Length (1)
1142 -- * ... * Typ'Length (n)) * Csize) - 1;
1144 -- The bounds are statically known, and btyp is one of the
1145 -- unsigned types, depending on the length.
1147 if Len_Bits <= Standard_Short_Short_Integer_Size then
1148 Btyp := RTE (RE_Short_Short_Unsigned);
1150 elsif Len_Bits <= Standard_Short_Integer_Size then
1151 Btyp := RTE (RE_Short_Unsigned);
1153 elsif Len_Bits <= Standard_Integer_Size then
1154 Btyp := RTE (RE_Unsigned);
1156 elsif Len_Bits <= Standard_Long_Integer_Size then
1157 Btyp := RTE (RE_Long_Unsigned);
1160 Btyp := RTE (RE_Long_Long_Unsigned);
1163 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1164 Set_Print_In_Hex (Lit);
1167 Make_Subtype_Declaration (Loc,
1168 Defining_Identifier => PAT,
1169 Subtype_Indication =>
1170 Make_Subtype_Indication (Loc,
1171 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1174 Make_Range_Constraint (Loc,
1178 Make_Integer_Literal (Loc, 0),
1179 High_Bound => Lit))));
1181 if PASize = Uint_0 then
1187 -- Propagate a given alignment to the modular type. This can
1188 -- cause it to be under-aligned, but that's OK.
1190 if Present (Alignment_Clause (Typ)) then
1191 Set_Alignment (PAT, Alignment (Typ));
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);
1363 -- If we are building the initialization procedure for a packed array,
1364 -- and Initialize_Scalars is enabled, each component assignment is an
1365 -- out-of-range value by design. Compile this value without checks,
1366 -- because a call to the array init_proc must not raise an exception.
1369 and then Initialize_Scalars
1371 Analyze_And_Resolve (Rhs, Ctyp, Suppress => All_Checks);
1373 Analyze_And_Resolve (Rhs, Ctyp);
1376 -- Case of component size 1,2,4 or any component size for the modular
1377 -- case. These are the cases for which we can inline the code.
1379 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1380 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1382 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1384 -- The statement to be generated is:
1386 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1388 -- where mask1 is obtained by shifting Cmask left Shift bits
1389 -- and then complementing the result.
1391 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1393 -- the "or ..." is omitted if rhs is constant and all 0 bits
1395 -- rhs is converted to the appropriate type
1397 -- The result is converted back to the array type, since
1398 -- otherwise we lose knowledge of the packed nature.
1400 -- Determine if right side is all 0 bits or all 1 bits
1402 if Compile_Time_Known_Value (Rhs) then
1403 Rhs_Val := Expr_Rep_Value (Rhs);
1404 Rhs_Val_Known := True;
1406 -- The following test catches the case of an unchecked conversion
1407 -- of an integer literal. This results from optimizing aggregates
1410 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1411 and then Compile_Time_Known_Value (Expression (Rhs))
1413 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1414 Rhs_Val_Known := True;
1418 Rhs_Val_Known := False;
1421 -- Some special checks for the case where the right hand value
1422 -- is known at compile time. Basically we have to take care of
1423 -- the implicit conversion to the subtype of the component object.
1425 if Rhs_Val_Known then
1427 -- If we have a biased component type then we must manually do
1428 -- the biasing, since we are taking responsibility in this case
1429 -- for constructing the exact bit pattern to be used.
1431 if Has_Biased_Representation (Ctyp) then
1432 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1435 -- For a negative value, we manually convert the twos complement
1436 -- value to a corresponding unsigned value, so that the proper
1437 -- field width is maintained. If we did not do this, we would
1438 -- get too many leading sign bits later on.
1441 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1445 -- Now create copies removing side effects. Note that in some
1446 -- complex cases, this may cause the fact that we have already
1447 -- set a packed array type on Obj to get lost. So we save the
1448 -- type of Obj, and make sure it is reset properly.
1451 T : constant Entity_Id := Etype (Obj);
1453 New_Lhs := Duplicate_Subexpr (Obj, True);
1454 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1456 Set_Etype (New_Lhs, T);
1457 Set_Etype (New_Rhs, T);
1460 -- First we deal with the "and"
1462 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1468 if Compile_Time_Known_Value (Shift) then
1470 Make_Integer_Literal (Loc,
1471 Modulus (Etype (Obj)) - 1 -
1472 (Cmask * (2 ** Expr_Value (Get_Shift))));
1473 Set_Print_In_Hex (Mask1);
1476 Lit := Make_Integer_Literal (Loc, Cmask);
1477 Set_Print_In_Hex (Lit);
1480 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1485 Left_Opnd => New_Rhs,
1486 Right_Opnd => Mask1);
1490 -- Then deal with the "or"
1492 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1496 procedure Fixup_Rhs;
1497 -- Adjust Rhs by bias if biased representation for components
1498 -- or remove extraneous high order sign bits if signed.
1500 procedure Fixup_Rhs is
1501 Etyp : constant Entity_Id := Etype (Rhs);
1504 -- For biased case, do the required biasing by simply
1505 -- converting to the biased subtype (the conversion
1506 -- will generate the required bias).
1508 if Has_Biased_Representation (Ctyp) then
1509 Rhs := Convert_To (Ctyp, Rhs);
1511 -- For a signed integer type that is not biased, generate
1512 -- a conversion to unsigned to strip high order sign bits.
1514 elsif Is_Signed_Integer_Type (Ctyp) then
1515 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1518 -- Set Etype, since it can be referenced before the
1519 -- node is completely analyzed.
1521 Set_Etype (Rhs, Etyp);
1523 -- We now need to do an unchecked conversion of the
1524 -- result to the target type, but it is important that
1525 -- this conversion be a right justified conversion and
1526 -- not a left justified conversion.
1528 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1534 and then Compile_Time_Known_Value (Get_Shift)
1537 Make_Integer_Literal (Loc,
1538 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1539 Set_Print_In_Hex (Or_Rhs);
1542 -- We have to convert the right hand side to Etype (Obj).
1543 -- A special case arises if what we have now is a Val
1544 -- attribute reference whose expression type is Etype (Obj).
1545 -- This happens for assignments of fields from the same
1546 -- array. In this case we get the required right hand side
1547 -- by simply removing the inner attribute reference.
1549 if Nkind (Rhs) = N_Attribute_Reference
1550 and then Attribute_Name (Rhs) = Name_Val
1551 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1553 Rhs := Relocate_Node (First (Expressions (Rhs)));
1556 -- If the value of the right hand side is a known integer
1557 -- value, then just replace it by an untyped constant,
1558 -- which will be properly retyped when we analyze and
1559 -- resolve the expression.
1561 elsif Rhs_Val_Known then
1563 -- Note that Rhs_Val has already been normalized to
1564 -- be an unsigned value with the proper number of bits.
1567 Make_Integer_Literal (Loc, Rhs_Val);
1569 -- Otherwise we need an unchecked conversion
1575 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1578 if Nkind (New_Rhs) = N_Op_And then
1579 Set_Paren_Count (New_Rhs, 1);
1584 Left_Opnd => New_Rhs,
1585 Right_Opnd => Or_Rhs);
1589 -- Now do the rewrite
1592 Make_Assignment_Statement (Loc,
1595 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1596 Set_Assignment_OK (Name (N), Ass_OK);
1598 -- All other component sizes for non-modular case
1603 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1605 -- where Subscr is the computed linear subscript
1608 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1614 if No (Bits_nn) then
1616 -- Error, most likely High_Integrity_Mode restriction
1621 -- Acquire proper Set entity. We use the aligned or unaligned
1622 -- case as appropriate.
1624 if Known_Aligned_Enough (Obj, Csiz) then
1625 Set_nn := RTE (Set_Id (Csiz));
1627 Set_nn := RTE (SetU_Id (Csiz));
1630 -- Now generate the set reference
1632 Obj := Relocate_Node (Prefix (Lhs));
1633 Convert_To_Actual_Subtype (Obj);
1634 Atyp := Etype (Obj);
1635 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1637 -- Below we must make the assumption that Obj is
1638 -- at least byte aligned, since otherwise its address
1639 -- cannot be taken. The assumption holds since the
1640 -- only arrays that can be misaligned are small packed
1641 -- arrays which are implemented as a modular type, and
1642 -- that is not the case here.
1645 Make_Procedure_Call_Statement (Loc,
1646 Name => New_Occurrence_Of (Set_nn, Loc),
1647 Parameter_Associations => New_List (
1648 Make_Attribute_Reference (Loc,
1650 Attribute_Name => Name_Address),
1652 Unchecked_Convert_To (Bits_nn,
1653 Convert_To (Ctyp, Rhs)))));
1658 Analyze (N, Suppress => All_Checks);
1659 end Expand_Bit_Packed_Element_Set;
1661 -------------------------------------
1662 -- Expand_Packed_Address_Reference --
1663 -------------------------------------
1665 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1666 Loc : constant Source_Ptr := Sloc (N);
1678 -- We build up an expression serially that has the form
1680 -- outer_object'Address
1681 -- + (linear-subscript * component_size for each array reference
1682 -- + field'Bit_Position for each record field
1684 -- + ...) / Storage_Unit;
1686 -- Some additional conversions are required to deal with the addition
1687 -- operation, which is not normally visible to generated code.
1690 Ploc := Sloc (Pref);
1692 if Nkind (Pref) = N_Indexed_Component then
1693 Convert_To_Actual_Subtype (Prefix (Pref));
1694 Atyp := Etype (Prefix (Pref));
1695 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1698 Make_Op_Multiply (Ploc,
1699 Left_Opnd => Subscr,
1701 Make_Attribute_Reference (Ploc,
1702 Prefix => New_Occurrence_Of (Atyp, Ploc),
1703 Attribute_Name => Name_Component_Size));
1705 elsif Nkind (Pref) = N_Selected_Component then
1707 Make_Attribute_Reference (Ploc,
1708 Prefix => Selector_Name (Pref),
1709 Attribute_Name => Name_Bit_Position);
1715 Term := Convert_To (RTE (RE_Integer_Address), Term);
1724 Right_Opnd => Term);
1727 Pref := Prefix (Pref);
1731 Unchecked_Convert_To (RTE (RE_Address),
1734 Unchecked_Convert_To (RTE (RE_Integer_Address),
1735 Make_Attribute_Reference (Loc,
1737 Attribute_Name => Name_Address)),
1740 Make_Op_Divide (Loc,
1743 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1745 Analyze_And_Resolve (N, RTE (RE_Address));
1746 end Expand_Packed_Address_Reference;
1748 ------------------------------------
1749 -- Expand_Packed_Boolean_Operator --
1750 ------------------------------------
1752 -- This routine expands "a op b" for the packed cases
1754 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1755 Loc : constant Source_Ptr := Sloc (N);
1756 Typ : constant Entity_Id := Etype (N);
1757 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1758 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1765 Convert_To_Actual_Subtype (L);
1766 Convert_To_Actual_Subtype (R);
1768 Ensure_Defined (Etype (L), N);
1769 Ensure_Defined (Etype (R), N);
1771 Apply_Length_Check (R, Etype (L));
1776 -- Deal with silly case of XOR where the subcomponent has a range
1777 -- True .. True where an exception must be raised.
1779 if Nkind (N) = N_Op_Xor then
1780 Silly_Boolean_Array_Xor_Test (N, Rtyp);
1783 -- Now that that silliness is taken care of, get packed array type
1785 Convert_To_PAT_Type (L);
1786 Convert_To_PAT_Type (R);
1790 -- For the modular case, we expand a op b into
1792 -- rtyp!(pat!(a) op pat!(b))
1794 -- where rtyp is the Etype of the left operand. Note that we do not
1795 -- convert to the base type, since this would be unconstrained, and
1796 -- hence not have a corresponding packed array type set.
1798 -- Note that both operands must be modular for this code to be used
1800 if Is_Modular_Integer_Type (PAT)
1802 Is_Modular_Integer_Type (Etype (R))
1808 if Nkind (N) = N_Op_And then
1809 P := Make_Op_And (Loc, L, R);
1811 elsif Nkind (N) = N_Op_Or then
1812 P := Make_Op_Or (Loc, L, R);
1814 else -- Nkind (N) = N_Op_Xor
1815 P := Make_Op_Xor (Loc, L, R);
1818 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1821 -- For the array case, we insert the actions
1825 -- System.Bit_Ops.Bit_And/Or/Xor
1827 -- Ltype'Length * Ltype'Component_Size;
1829 -- Rtype'Length * Rtype'Component_Size
1832 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1833 -- the second argument and fourth arguments are the lengths of the
1834 -- operands in bits. Then we replace the expression by a reference
1837 -- Note that if we are mixing a modular and array operand, everything
1838 -- works fine, since we ensure that the modular representation has the
1839 -- same physical layout as the array representation (that's what the
1840 -- left justified modular stuff in the big-endian case is about).
1844 Result_Ent : constant Entity_Id :=
1845 Make_Defining_Identifier (Loc,
1846 Chars => New_Internal_Name ('T'));
1851 if Nkind (N) = N_Op_And then
1854 elsif Nkind (N) = N_Op_Or then
1857 else -- Nkind (N) = N_Op_Xor
1861 Insert_Actions (N, New_List (
1863 Make_Object_Declaration (Loc,
1864 Defining_Identifier => Result_Ent,
1865 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1867 Make_Procedure_Call_Statement (Loc,
1868 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1869 Parameter_Associations => New_List (
1871 Make_Byte_Aligned_Attribute_Reference (Loc,
1873 Attribute_Name => Name_Address),
1875 Make_Op_Multiply (Loc,
1877 Make_Attribute_Reference (Loc,
1880 (Etype (First_Index (Ltyp)), Loc),
1881 Attribute_Name => Name_Range_Length),
1884 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1886 Make_Byte_Aligned_Attribute_Reference (Loc,
1888 Attribute_Name => Name_Address),
1890 Make_Op_Multiply (Loc,
1892 Make_Attribute_Reference (Loc,
1895 (Etype (First_Index (Rtyp)), Loc),
1896 Attribute_Name => Name_Range_Length),
1899 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1901 Make_Byte_Aligned_Attribute_Reference (Loc,
1902 Prefix => New_Occurrence_Of (Result_Ent, Loc),
1903 Attribute_Name => Name_Address)))));
1906 New_Occurrence_Of (Result_Ent, Loc));
1910 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1911 end Expand_Packed_Boolean_Operator;
1913 -------------------------------------
1914 -- Expand_Packed_Element_Reference --
1915 -------------------------------------
1917 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1918 Loc : constant Source_Ptr := Sloc (N);
1930 -- If not bit packed, we have the enumeration case, which is easily
1931 -- dealt with (just adjust the subscripts of the indexed component)
1933 -- Note: this leaves the result as an indexed component, which is
1934 -- still a variable, so can be used in the assignment case, as is
1935 -- required in the enumeration case.
1937 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1938 Setup_Enumeration_Packed_Array_Reference (N);
1942 -- Remaining processing is for the bit-packed case
1944 Obj := Relocate_Node (Prefix (N));
1945 Convert_To_Actual_Subtype (Obj);
1946 Atyp := Etype (Obj);
1947 PAT := Packed_Array_Type (Atyp);
1948 Ctyp := Component_Type (Atyp);
1949 Csiz := UI_To_Int (Component_Size (Atyp));
1951 -- Case of component size 1,2,4 or any component size for the modular
1952 -- case. These are the cases for which we can inline the code.
1954 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1955 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1957 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1958 Lit := Make_Integer_Literal (Loc, Cmask);
1959 Set_Print_In_Hex (Lit);
1961 -- We generate a shift right to position the field, followed by a
1962 -- masking operation to extract the bit field, and we finally do an
1963 -- unchecked conversion to convert the result to the required target.
1965 -- Note that the unchecked conversion automatically deals with the
1966 -- bias if we are dealing with a biased representation. What will
1967 -- happen is that we temporarily generate the biased representation,
1968 -- but almost immediately that will be converted to the original
1969 -- unbiased component type, and the bias will disappear.
1973 Left_Opnd => Make_Shift_Right (Obj, Shift),
1976 -- We needed to analyze this before we do the unchecked convert
1977 -- below, but we need it temporarily attached to the tree for
1978 -- this analysis (hence the temporary Set_Parent call).
1980 Set_Parent (Arg, Parent (N));
1981 Analyze_And_Resolve (Arg);
1984 RJ_Unchecked_Convert_To (Ctyp, Arg));
1986 -- All other component sizes for non-modular case
1991 -- Component_Type!(Get_nn (Arr'address, Subscr))
1993 -- where Subscr is the computed linear subscript
2000 -- Acquire proper Get entity. We use the aligned or unaligned
2001 -- case as appropriate.
2003 if Known_Aligned_Enough (Obj, Csiz) then
2004 Get_nn := RTE (Get_Id (Csiz));
2006 Get_nn := RTE (GetU_Id (Csiz));
2009 -- Now generate the get reference
2011 Compute_Linear_Subscript (Atyp, N, Subscr);
2013 -- Below we make the assumption that Obj is at least byte
2014 -- aligned, since otherwise its address cannot be taken.
2015 -- The assumption holds since the only arrays that can be
2016 -- misaligned are small packed arrays which are implemented
2017 -- as a modular type, and that is not the case here.
2020 Unchecked_Convert_To (Ctyp,
2021 Make_Function_Call (Loc,
2022 Name => New_Occurrence_Of (Get_nn, Loc),
2023 Parameter_Associations => New_List (
2024 Make_Attribute_Reference (Loc,
2026 Attribute_Name => Name_Address),
2031 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2033 end Expand_Packed_Element_Reference;
2035 ----------------------
2036 -- Expand_Packed_Eq --
2037 ----------------------
2039 -- Handles expansion of "=" on packed array types
2041 procedure Expand_Packed_Eq (N : Node_Id) is
2042 Loc : constant Source_Ptr := Sloc (N);
2043 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2044 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2054 Convert_To_Actual_Subtype (L);
2055 Convert_To_Actual_Subtype (R);
2056 Ltyp := Underlying_Type (Etype (L));
2057 Rtyp := Underlying_Type (Etype (R));
2059 Convert_To_PAT_Type (L);
2060 Convert_To_PAT_Type (R);
2064 Make_Op_Multiply (Loc,
2066 Make_Attribute_Reference (Loc,
2067 Prefix => New_Occurrence_Of (Ltyp, Loc),
2068 Attribute_Name => Name_Length),
2070 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2073 Make_Op_Multiply (Loc,
2075 Make_Attribute_Reference (Loc,
2076 Prefix => New_Occurrence_Of (Rtyp, Loc),
2077 Attribute_Name => Name_Length),
2079 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2081 -- For the modular case, we transform the comparison to:
2083 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2085 -- where PAT is the packed array type. This works fine, since in the
2086 -- modular case we guarantee that the unused bits are always zeroes.
2087 -- We do have to compare the lengths because we could be comparing
2088 -- two different subtypes of the same base type.
2090 if Is_Modular_Integer_Type (PAT) then
2095 Left_Opnd => LLexpr,
2096 Right_Opnd => RLexpr),
2103 -- For the non-modular case, we call a runtime routine
2105 -- System.Bit_Ops.Bit_Eq
2106 -- (L'Address, L_Length, R'Address, R_Length)
2108 -- where PAT is the packed array type, and the lengths are the lengths
2109 -- in bits of the original packed arrays. This routine takes care of
2110 -- not comparing the unused bits in the last byte.
2114 Make_Function_Call (Loc,
2115 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2116 Parameter_Associations => New_List (
2117 Make_Byte_Aligned_Attribute_Reference (Loc,
2119 Attribute_Name => Name_Address),
2123 Make_Byte_Aligned_Attribute_Reference (Loc,
2125 Attribute_Name => Name_Address),
2130 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2131 end Expand_Packed_Eq;
2133 -----------------------
2134 -- Expand_Packed_Not --
2135 -----------------------
2137 -- Handles expansion of "not" on packed array types
2139 procedure Expand_Packed_Not (N : Node_Id) is
2140 Loc : constant Source_Ptr := Sloc (N);
2141 Typ : constant Entity_Id := Etype (N);
2142 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2149 Convert_To_Actual_Subtype (Opnd);
2150 Rtyp := Etype (Opnd);
2152 -- Deal with silly False..False and True..True subtype case
2154 Silly_Boolean_Array_Not_Test (N, Rtyp);
2156 -- Now that the silliness is taken care of, get packed array type
2158 Convert_To_PAT_Type (Opnd);
2159 PAT := Etype (Opnd);
2161 -- For the case where the packed array type is a modular type,
2162 -- not A expands simply into:
2164 -- rtyp!(PAT!(A) xor mask)
2166 -- where PAT is the packed array type, and mask is a mask of all
2167 -- one bits of length equal to the size of this packed type and
2168 -- rtyp is the actual subtype of the operand
2170 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2171 Set_Print_In_Hex (Lit);
2173 if not Is_Array_Type (PAT) then
2175 Unchecked_Convert_To (Rtyp,
2178 Right_Opnd => Lit)));
2180 -- For the array case, we insert the actions
2184 -- System.Bit_Ops.Bit_Not
2186 -- Typ'Length * Typ'Component_Size;
2189 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2190 -- argument is the length of the operand in bits. Then we replace
2191 -- the expression by a reference to Result.
2195 Result_Ent : constant Entity_Id :=
2196 Make_Defining_Identifier (Loc,
2197 Chars => New_Internal_Name ('T'));
2200 Insert_Actions (N, New_List (
2202 Make_Object_Declaration (Loc,
2203 Defining_Identifier => Result_Ent,
2204 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2206 Make_Procedure_Call_Statement (Loc,
2207 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2208 Parameter_Associations => New_List (
2210 Make_Byte_Aligned_Attribute_Reference (Loc,
2212 Attribute_Name => Name_Address),
2214 Make_Op_Multiply (Loc,
2216 Make_Attribute_Reference (Loc,
2219 (Etype (First_Index (Rtyp)), Loc),
2220 Attribute_Name => Name_Range_Length),
2223 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2225 Make_Byte_Aligned_Attribute_Reference (Loc,
2226 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2227 Attribute_Name => Name_Address)))));
2230 New_Occurrence_Of (Result_Ent, Loc));
2234 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2236 end Expand_Packed_Not;
2238 -------------------------------------
2239 -- Involves_Packed_Array_Reference --
2240 -------------------------------------
2242 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2244 if Nkind (N) = N_Indexed_Component
2245 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2249 elsif Nkind (N) = N_Selected_Component then
2250 return Involves_Packed_Array_Reference (Prefix (N));
2255 end Involves_Packed_Array_Reference;
2257 --------------------------
2258 -- Known_Aligned_Enough --
2259 --------------------------
2261 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2262 Typ : constant Entity_Id := Etype (Obj);
2264 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2265 -- If the component is in a record that contains previous packed
2266 -- components, consider it unaligned because the back-end might
2267 -- choose to pack the rest of the record. Lead to less efficient code,
2268 -- but safer vis-a-vis of back-end choices.
2270 --------------------------------
2271 -- In_Partially_Packed_Record --
2272 --------------------------------
2274 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2275 Rec_Type : constant Entity_Id := Scope (Comp);
2276 Prev_Comp : Entity_Id;
2279 Prev_Comp := First_Entity (Rec_Type);
2280 while Present (Prev_Comp) loop
2281 if Is_Packed (Etype (Prev_Comp)) then
2284 elsif Prev_Comp = Comp then
2288 Next_Entity (Prev_Comp);
2292 end In_Partially_Packed_Record;
2294 -- Start of processing for Known_Aligned_Enough
2297 -- Odd bit sizes don't need alignment anyway
2299 if Csiz mod 2 = 1 then
2302 -- If we have a specified alignment, see if it is sufficient, if not
2303 -- then we can't possibly be aligned enough in any case.
2305 elsif Known_Alignment (Etype (Obj)) then
2306 -- Alignment required is 4 if size is a multiple of 4, and
2307 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2309 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2314 -- OK, alignment should be sufficient, if object is aligned
2316 -- If object is strictly aligned, then it is definitely aligned
2318 if Strict_Alignment (Typ) then
2321 -- Case of subscripted array reference
2323 elsif Nkind (Obj) = N_Indexed_Component then
2325 -- If we have a pointer to an array, then this is definitely
2326 -- aligned, because pointers always point to aligned versions.
2328 if Is_Access_Type (Etype (Prefix (Obj))) then
2331 -- Otherwise, go look at the prefix
2334 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2337 -- Case of record field
2339 elsif Nkind (Obj) = N_Selected_Component then
2341 -- What is significant here is whether the record type is packed
2343 if Is_Record_Type (Etype (Prefix (Obj)))
2344 and then Is_Packed (Etype (Prefix (Obj)))
2348 -- Or the component has a component clause which might cause
2349 -- the component to become unaligned (we can't tell if the
2350 -- backend is doing alignment computations).
2352 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2355 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2358 -- In all other cases, go look at prefix
2361 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2364 elsif Nkind (Obj) = N_Type_Conversion then
2365 return Known_Aligned_Enough (Expression (Obj), Csiz);
2367 -- For a formal parameter, it is safer to assume that it is not
2368 -- aligned, because the formal may be unconstrained while the actual
2369 -- is constrained. In this situation, a small constrained packed
2370 -- array, represented in modular form, may be unaligned.
2372 elsif Is_Entity_Name (Obj) then
2373 return not Is_Formal (Entity (Obj));
2376 -- If none of the above, must be aligned
2379 end Known_Aligned_Enough;
2381 ---------------------
2382 -- Make_Shift_Left --
2383 ---------------------
2385 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2389 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2393 Make_Op_Shift_Left (Sloc (N),
2396 Set_Shift_Count_OK (Nod, True);
2399 end Make_Shift_Left;
2401 ----------------------
2402 -- Make_Shift_Right --
2403 ----------------------
2405 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2409 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2413 Make_Op_Shift_Right (Sloc (N),
2416 Set_Shift_Count_OK (Nod, True);
2419 end Make_Shift_Right;
2421 -----------------------------
2422 -- RJ_Unchecked_Convert_To --
2423 -----------------------------
2425 function RJ_Unchecked_Convert_To
2427 Expr : Node_Id) return Node_Id
2429 Source_Typ : constant Entity_Id := Etype (Expr);
2430 Target_Typ : constant Entity_Id := Typ;
2432 Src : Node_Id := Expr;
2438 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2439 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2441 -- First step, if the source type is not a discrete type, then we
2442 -- first convert to a modular type of the source length, since
2443 -- otherwise, on a big-endian machine, we get left-justification.
2444 -- We do it for little-endian machines as well, because there might
2445 -- be junk bits that are not cleared if the type is not numeric.
2447 if Source_Siz /= Target_Siz
2448 and then not Is_Discrete_Type (Source_Typ)
2450 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2453 -- In the big endian case, if the lengths of the two types differ,
2454 -- then we must worry about possible left justification in the
2455 -- conversion, and avoiding that is what this is all about.
2457 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2459 -- Next step. If the target is not a discrete type, then we first
2460 -- convert to a modular type of the target length, since
2461 -- otherwise, on a big-endian machine, we get left-justification.
2463 if not Is_Discrete_Type (Target_Typ) then
2464 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2468 -- And now we can do the final conversion to the target type
2470 return Unchecked_Convert_To (Target_Typ, Src);
2471 end RJ_Unchecked_Convert_To;
2473 ----------------------------------------------
2474 -- Setup_Enumeration_Packed_Array_Reference --
2475 ----------------------------------------------
2477 -- All we have to do here is to find the subscripts that correspond
2478 -- to the index positions that have non-standard enumeration types
2479 -- and insert a Pos attribute to get the proper subscript value.
2481 -- Finally the prefix must be uncheck converted to the corresponding
2482 -- packed array type.
2484 -- Note that the component type is unchanged, so we do not need to
2485 -- fiddle with the types (Gigi always automatically takes the packed
2486 -- array type if it is set, as it will be in this case).
2488 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2489 Pfx : constant Node_Id := Prefix (N);
2490 Typ : constant Entity_Id := Etype (N);
2491 Exprs : constant List_Id := Expressions (N);
2495 -- If the array is unconstrained, then we replace the array
2496 -- reference with its actual subtype. This actual subtype will
2497 -- have a packed array type with appropriate bounds.
2499 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2500 Convert_To_Actual_Subtype (Pfx);
2503 Expr := First (Exprs);
2504 while Present (Expr) loop
2506 Loc : constant Source_Ptr := Sloc (Expr);
2507 Expr_Typ : constant Entity_Id := Etype (Expr);
2510 if Is_Enumeration_Type (Expr_Typ)
2511 and then Has_Non_Standard_Rep (Expr_Typ)
2514 Make_Attribute_Reference (Loc,
2515 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2516 Attribute_Name => Name_Pos,
2517 Expressions => New_List (Relocate_Node (Expr))));
2518 Analyze_And_Resolve (Expr, Standard_Natural);
2526 Make_Indexed_Component (Sloc (N),
2528 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2529 Expressions => Exprs));
2531 Analyze_And_Resolve (N, Typ);
2533 end Setup_Enumeration_Packed_Array_Reference;
2535 -----------------------------------------
2536 -- Setup_Inline_Packed_Array_Reference --
2537 -----------------------------------------
2539 procedure Setup_Inline_Packed_Array_Reference
2542 Obj : in out Node_Id;
2544 Shift : out Node_Id)
2546 Loc : constant Source_Ptr := Sloc (N);
2553 Csiz := Component_Size (Atyp);
2555 Convert_To_PAT_Type (Obj);
2558 Cmask := 2 ** Csiz - 1;
2560 if Is_Array_Type (PAT) then
2561 Otyp := Component_Type (PAT);
2562 Osiz := Component_Size (PAT);
2567 -- In the case where the PAT is a modular type, we want the actual
2568 -- size in bits of the modular value we use. This is neither the
2569 -- Object_Size nor the Value_Size, either of which may have been
2570 -- reset to strange values, but rather the minimum size. Note that
2571 -- since this is a modular type with full range, the issue of
2572 -- biased representation does not arise.
2574 Osiz := UI_From_Int (Minimum_Size (Otyp));
2577 Compute_Linear_Subscript (Atyp, N, Shift);
2579 -- If the component size is not 1, then the subscript must be
2580 -- multiplied by the component size to get the shift count.
2584 Make_Op_Multiply (Loc,
2585 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2586 Right_Opnd => Shift);
2589 -- If we have the array case, then this shift count must be broken
2590 -- down into a byte subscript, and a shift within the byte.
2592 if Is_Array_Type (PAT) then
2595 New_Shift : Node_Id;
2598 -- We must analyze shift, since we will duplicate it
2600 Set_Parent (Shift, N);
2602 (Shift, Standard_Integer, Suppress => All_Checks);
2604 -- The shift count within the word is
2609 Left_Opnd => Duplicate_Subexpr (Shift),
2610 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2612 -- The subscript to be used on the PAT array is
2616 Make_Indexed_Component (Loc,
2618 Expressions => New_List (
2619 Make_Op_Divide (Loc,
2620 Left_Opnd => Duplicate_Subexpr (Shift),
2621 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2626 -- For the modular integer case, the object to be manipulated is
2627 -- the entire array, so Obj is unchanged. Note that we will reset
2628 -- its type to PAT before returning to the caller.
2634 -- The one remaining step is to modify the shift count for the
2635 -- big-endian case. Consider the following example in a byte:
2637 -- xxxxxxxx bits of byte
2638 -- vvvvvvvv bits of value
2639 -- 33221100 little-endian numbering
2640 -- 00112233 big-endian numbering
2642 -- Here we have the case of 2-bit fields
2644 -- For the little-endian case, we already have the proper shift
2645 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2647 -- For the big endian case, we have to adjust the shift count,
2648 -- computing it as (N - F) - shift, where N is the number of bits
2649 -- in an element of the array used to implement the packed array,
2650 -- F is the number of bits in a source level array element, and
2651 -- shift is the count so far computed.
2653 if Bytes_Big_Endian then
2655 Make_Op_Subtract (Loc,
2656 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2657 Right_Opnd => Shift);
2660 Set_Parent (Shift, N);
2661 Set_Parent (Obj, N);
2662 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2663 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2665 -- Make sure final type of object is the appropriate packed type
2667 Set_Etype (Obj, Otyp);
2669 end Setup_Inline_Packed_Array_Reference;