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 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Layout; use Layout;
34 with Namet; use Namet;
35 with Nlists; use Nlists;
36 with Nmake; use Nmake;
37 with Rtsfind; use Rtsfind;
39 with Sem_Ch3; use Sem_Ch3;
40 with Sem_Ch8; use Sem_Ch8;
41 with Sem_Ch13; use Sem_Ch13;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sinfo; use Sinfo;
46 with Snames; use Snames;
47 with Stand; use Stand;
48 with Targparm; use Targparm;
49 with Tbuild; use Tbuild;
50 with Ttypes; use Ttypes;
51 with Uintp; use Uintp;
53 package body Exp_Pakd is
55 ---------------------------
56 -- Endian Considerations --
57 ---------------------------
59 -- As described in the specification, bit numbering in a packed array
60 -- is consistent with bit numbering in a record representation clause,
61 -- and hence dependent on the endianness of the machine:
63 -- For little-endian machines, element zero is at the right hand end
64 -- (low order end) of a bit field.
66 -- For big-endian machines, element zero is at the left hand end
67 -- (high order end) of a bit field.
69 -- The shifts that are used to right justify a field therefore differ
70 -- in the two cases. For the little-endian case, we can simply use the
71 -- bit number (i.e. the element number * element size) as the count for
72 -- a right shift. For the big-endian case, we have to subtract the shift
73 -- count from an appropriate constant to use in the right shift. We use
74 -- rotates instead of shifts (which is necessary in the store case to
75 -- preserve other fields), and we expect that the backend will be able
76 -- to change the right rotate into a left rotate, avoiding the subtract,
77 -- if the architecture provides such an instruction.
79 ----------------------------------------------
80 -- Entity Tables for Packed Access Routines --
81 ----------------------------------------------
83 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
84 -- library routines. This table is used to obtain the entity for the
87 type E_Array is array (Int range 01 .. 63) of RE_Id;
89 -- Array of Bits_nn entities. Note that we do not use library routines
90 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
91 -- entries from System.Unsigned, because we also use this table for
92 -- certain special unchecked conversions in the big-endian case.
94 Bits_Id : constant E_Array :=
110 16 => RE_Unsigned_16,
126 32 => RE_Unsigned_32,
159 -- Array of Get routine entities. These are used to obtain an element
160 -- from a packed array. The N'th entry is used to obtain elements from
161 -- a packed array whose component size is N. RE_Null is used as a null
162 -- entry, for the cases where a library routine is not used.
164 Get_Id : constant E_Array :=
229 -- Array of Get routine entities to be used in the case where the packed
230 -- array is itself a component of a packed structure, and therefore may
231 -- not be fully aligned. This only affects the even sizes, since for the
232 -- odd sizes, we do not get any fixed alignment in any case.
234 GetU_Id : constant E_Array :=
299 -- Array of Set routine entities. These are used to assign an element
300 -- of a packed array. The N'th entry is used to assign elements for
301 -- a packed array whose component size is N. RE_Null is used as a null
302 -- entry, for the cases where a library routine is not used.
304 Set_Id : constant E_Array :=
369 -- Array of Set routine entities to be used in the case where the packed
370 -- array is itself a component of a packed structure, and therefore may
371 -- not be fully aligned. This only affects the even sizes, since for the
372 -- odd sizes, we do not get any fixed alignment in any case.
374 SetU_Id : constant E_Array :=
439 -----------------------
440 -- Local Subprograms --
441 -----------------------
443 procedure Compute_Linear_Subscript
446 Subscr : out Node_Id);
447 -- Given a constrained array type Atyp, and an indexed component node
448 -- N referencing an array object of this type, build an expression of
449 -- type Standard.Integer representing the zero-based linear subscript
450 -- value. This expression includes any required range checks.
452 procedure Convert_To_PAT_Type (Aexp : Node_Id);
453 -- Given an expression of a packed array type, builds a corresponding
454 -- expression whose type is the implementation type used to represent
455 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
457 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
458 -- There are two versions of the Set routines, the ones used when the
459 -- object is known to be sufficiently well aligned given the number of
460 -- bits, and the ones used when the object is not known to be aligned.
461 -- This routine is used to determine which set to use. Obj is a reference
462 -- to the object, and Csiz is the component size of the packed array.
463 -- True is returned if the alignment of object is known to be sufficient,
464 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
467 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
468 -- Build a left shift node, checking for the case of a shift count of zero
470 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
471 -- Build a right shift node, checking for the case of a shift count of zero
473 function RJ_Unchecked_Convert_To
475 Expr : Node_Id) return Node_Id;
476 -- The packed array code does unchecked conversions which in some cases
477 -- may involve non-discrete types with differing sizes. The semantics of
478 -- such conversions is potentially endian dependent, and the effect we
479 -- want here for such a conversion is to do the conversion in size as
480 -- though numeric items are involved, and we extend or truncate on the
481 -- left side. This happens naturally in the little-endian case, but in
482 -- the big endian case we can get left justification, when what we want
483 -- is right justification. This routine does the unchecked conversion in
484 -- a stepwise manner to ensure that it gives the expected result. Hence
485 -- the name (RJ = Right justified). The parameters Typ and Expr are as
486 -- for the case of a normal Unchecked_Convert_To call.
488 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
489 -- This routine is called in the Get and Set case for arrays that are
490 -- packed but not bit-packed, meaning that they have at least one
491 -- subscript that is of an enumeration type with a non-standard
492 -- representation. This routine modifies the given node to properly
493 -- reference the corresponding packed array type.
495 procedure Setup_Inline_Packed_Array_Reference
498 Obj : in out Node_Id;
500 Shift : out Node_Id);
501 -- This procedure performs common processing on the N_Indexed_Component
502 -- parameter given as N, whose prefix is a reference to a packed array.
503 -- This is used for the get and set when the component size is 1,2,4
504 -- or for other component sizes when the packed array type is a modular
505 -- type (i.e. the cases that are handled with inline code).
509 -- N is the N_Indexed_Component node for the packed array reference
511 -- Atyp is the constrained array type (the actual subtype has been
512 -- computed if necessary to obtain the constraints, but this is still
513 -- the original array type, not the Packed_Array_Type value).
515 -- Obj is the object which is to be indexed. It is always of type Atyp.
519 -- Obj is the object containing the desired bit field. It is of type
520 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
521 -- entire value, for the small static case, or the proper selected byte
522 -- from the array in the large or dynamic case. This node is analyzed
523 -- and resolved on return.
525 -- Shift is a node representing the shift count to be used in the
526 -- rotate right instruction that positions the field for access.
527 -- This node is analyzed and resolved on return.
529 -- Cmask is a mask corresponding to the width of the component field.
530 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
532 -- Note: in some cases the call to this routine may generate actions
533 -- (for handling multi-use references and the generation of the packed
534 -- array type on the fly). Such actions are inserted into the tree
535 -- directly using Insert_Action.
537 ------------------------------
538 -- Compute_Linear_Subcsript --
539 ------------------------------
541 procedure Compute_Linear_Subscript
544 Subscr : out Node_Id)
546 Loc : constant Source_Ptr := Sloc (N);
555 -- Loop through dimensions
557 Indx := First_Index (Atyp);
558 Oldsub := First (Expressions (N));
560 while Present (Indx) loop
561 Styp := Etype (Indx);
562 Newsub := Relocate_Node (Oldsub);
564 -- Get expression for the subscript value. First, if Do_Range_Check
565 -- is set on a subscript, then we must do a range check against the
566 -- original bounds (not the bounds of the packed array type). We do
567 -- this by introducing a subtype conversion.
569 if Do_Range_Check (Newsub)
570 and then Etype (Newsub) /= Styp
572 Newsub := Convert_To (Styp, Newsub);
575 -- Now evolve the expression for the subscript. First convert
576 -- the subscript to be zero based and of an integer type.
578 -- Case of integer type, where we just subtract to get lower bound
580 if Is_Integer_Type (Styp) then
582 -- If length of integer type is smaller than standard integer,
583 -- then we convert to integer first, then do the subtract
585 -- Integer (subscript) - Integer (Styp'First)
587 if Esize (Styp) < Esize (Standard_Integer) then
589 Make_Op_Subtract (Loc,
590 Left_Opnd => Convert_To (Standard_Integer, Newsub),
592 Convert_To (Standard_Integer,
593 Make_Attribute_Reference (Loc,
594 Prefix => New_Occurrence_Of (Styp, Loc),
595 Attribute_Name => Name_First)));
597 -- For larger integer types, subtract first, then convert to
598 -- integer, this deals with strange long long integer bounds.
600 -- Integer (subscript - Styp'First)
604 Convert_To (Standard_Integer,
605 Make_Op_Subtract (Loc,
608 Make_Attribute_Reference (Loc,
609 Prefix => New_Occurrence_Of (Styp, Loc),
610 Attribute_Name => Name_First)));
613 -- For the enumeration case, we have to use 'Pos to get the value
614 -- to work with before subtracting the lower bound.
616 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
618 -- This is not quite right for bizarre cases where the size of the
619 -- enumeration type is > Integer'Size bits due to rep clause ???
622 pragma Assert (Is_Enumeration_Type (Styp));
625 Make_Op_Subtract (Loc,
626 Left_Opnd => Convert_To (Standard_Integer,
627 Make_Attribute_Reference (Loc,
628 Prefix => New_Occurrence_Of (Styp, Loc),
629 Attribute_Name => Name_Pos,
630 Expressions => New_List (Newsub))),
633 Convert_To (Standard_Integer,
634 Make_Attribute_Reference (Loc,
635 Prefix => New_Occurrence_Of (Styp, Loc),
636 Attribute_Name => Name_Pos,
637 Expressions => New_List (
638 Make_Attribute_Reference (Loc,
639 Prefix => New_Occurrence_Of (Styp, Loc),
640 Attribute_Name => Name_First)))));
643 Set_Paren_Count (Newsub, 1);
645 -- For the first subscript, we just copy that subscript value
650 -- Otherwise, we must multiply what we already have by the current
651 -- stride and then add in the new value to the evolving subscript.
657 Make_Op_Multiply (Loc,
660 Make_Attribute_Reference (Loc,
661 Attribute_Name => Name_Range_Length,
662 Prefix => New_Occurrence_Of (Styp, Loc))),
663 Right_Opnd => Newsub);
666 -- Move to next subscript
671 end Compute_Linear_Subscript;
673 -------------------------
674 -- Convert_To_PAT_Type --
675 -------------------------
677 -- The PAT is always obtained from the actual subtype
679 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
683 Convert_To_Actual_Subtype (Aexp);
684 Act_ST := Underlying_Type (Etype (Aexp));
685 Create_Packed_Array_Type (Act_ST);
687 -- Just replace the etype with the packed array type. This works because
688 -- the expression will not be further analyzed, and Gigi considers the
689 -- two types equivalent in any case.
691 -- This is not strictly the case ??? If the reference is an actual in
692 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
693 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
694 -- array reference, reanalysis can produce spurious type errors when the
695 -- PAT type is replaced again with the original type of the array. Same
696 -- for the case of a dereference. The following is correct and minimal,
697 -- but the handling of more complex packed expressions in actuals is
698 -- confused. Probably the problem only remains for actuals in calls.
700 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
702 if Is_Entity_Name (Aexp)
704 (Nkind (Aexp) = N_Indexed_Component
705 and then Is_Entity_Name (Prefix (Aexp)))
706 or else Nkind (Aexp) = N_Explicit_Dereference
710 end Convert_To_PAT_Type;
712 ------------------------------
713 -- Create_Packed_Array_Type --
714 ------------------------------
716 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
717 Loc : constant Source_Ptr := Sloc (Typ);
718 Ctyp : constant Entity_Id := Component_Type (Typ);
719 Csize : constant Uint := Component_Size (Typ);
734 procedure Install_PAT;
735 -- This procedure is called with Decl set to the declaration for the
736 -- packed array type. It creates the type and installs it as required.
738 procedure Set_PB_Type;
739 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
740 -- requirements (see documentation in the spec of this package).
746 procedure Install_PAT is
747 Pushed_Scope : Boolean := False;
750 -- We do not want to put the declaration we have created in the tree
751 -- since it is often hard, and sometimes impossible to find a proper
752 -- place for it (the impossible case arises for a packed array type
753 -- with bounds depending on the discriminant, a declaration cannot
754 -- be put inside the record, and the reference to the discriminant
755 -- cannot be outside the record).
757 -- The solution is to analyze the declaration while temporarily
758 -- attached to the tree at an appropriate point, and then we install
759 -- the resulting type as an Itype in the packed array type field of
760 -- the original type, so that no explicit declaration is required.
762 -- Note: the packed type is created in the scope of its parent
763 -- type. There are at least some cases where the current scope
764 -- is deeper, and so when this is the case, we temporarily reset
765 -- the scope for the definition. This is clearly safe, since the
766 -- first use of the packed array type will be the implicit
767 -- reference from the corresponding unpacked type when it is
770 if Is_Itype (Typ) then
771 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
773 Set_Parent (Decl, Declaration_Node (Typ));
776 if Scope (Typ) /= Current_Scope then
777 Push_Scope (Scope (Typ));
778 Pushed_Scope := True;
781 Set_Is_Itype (PAT, True);
782 Set_Packed_Array_Type (Typ, PAT);
783 Analyze (Decl, Suppress => All_Checks);
789 -- Set Esize and RM_Size to the actual size of the packed object
790 -- Do not reset RM_Size if already set, as happens in the case of
793 if Unknown_Esize (PAT) then
794 Set_Esize (PAT, PASize);
797 if Unknown_RM_Size (PAT) then
798 Set_RM_Size (PAT, PASize);
801 Adjust_Esize_Alignment (PAT);
803 -- Set remaining fields of packed array type
805 Init_Alignment (PAT);
806 Set_Parent (PAT, Empty);
807 Set_Associated_Node_For_Itype (PAT, Typ);
808 Set_Is_Packed_Array_Type (PAT, True);
809 Set_Original_Array_Type (PAT, Typ);
811 -- We definitely do not want to delay freezing for packed array
812 -- types. This is of particular importance for the itypes that
813 -- are generated for record components depending on discriminants
814 -- where there is no place to put the freeze node.
816 Set_Has_Delayed_Freeze (PAT, False);
817 Set_Has_Delayed_Freeze (Etype (PAT), False);
819 -- If we did allocate a freeze node, then clear out the reference
820 -- since it is obsolete (should we delete the freeze node???)
822 Set_Freeze_Node (PAT, Empty);
823 Set_Freeze_Node (Etype (PAT), Empty);
830 procedure Set_PB_Type is
832 -- If the user has specified an explicit alignment for the
833 -- type or component, take it into account.
835 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
836 or else Alignment (Typ) = 1
837 or else Component_Alignment (Typ) = Calign_Storage_Unit
839 PB_Type := RTE (RE_Packed_Bytes1);
841 elsif Csize mod 4 /= 0
842 or else Alignment (Typ) = 2
844 PB_Type := RTE (RE_Packed_Bytes2);
847 PB_Type := RTE (RE_Packed_Bytes4);
851 -- Start of processing for Create_Packed_Array_Type
854 -- If we already have a packed array type, nothing to do
856 if Present (Packed_Array_Type (Typ)) then
860 -- If our immediate ancestor subtype is constrained, and it already
861 -- has a packed array type, then just share the same type, since the
862 -- bounds must be the same. If the ancestor is not an array type but
863 -- a private type, as can happen with multiple instantiations, create
864 -- a new packed type, to avoid privacy issues.
866 if Ekind (Typ) = E_Array_Subtype then
867 Ancest := Ancestor_Subtype (Typ);
870 and then Is_Array_Type (Ancest)
871 and then Is_Constrained (Ancest)
872 and then Present (Packed_Array_Type (Ancest))
874 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
879 -- We preset the result type size from the size of the original array
880 -- type, since this size clearly belongs to the packed array type. The
881 -- size of the conceptual unpacked type is always set to unknown.
883 PASize := RM_Size (Typ);
885 -- Case of an array where at least one index is of an enumeration
886 -- type with a non-standard representation, but the component size
887 -- is not appropriate for bit packing. This is the case where we
888 -- have Is_Packed set (we would never be in this unit otherwise),
889 -- but Is_Bit_Packed_Array is false.
891 -- Note that if the component size is appropriate for bit packing,
892 -- then the circuit for the computation of the subscript properly
893 -- deals with the non-standard enumeration type case by taking the
896 if not Is_Bit_Packed_Array (Typ) then
898 -- Here we build a declaration:
900 -- type tttP is array (index1, index2, ...) of component_type
902 -- where index1, index2, are the index types. These are the same
903 -- as the index types of the original array, except for the non-
904 -- standard representation enumeration type case, where we have
907 -- For the unconstrained array case, we use
911 -- For the constrained case, we use
913 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
914 -- Enum_Type'Pos (Enum_Type'Last);
917 Make_Defining_Identifier (Loc,
918 Chars => New_External_Name (Chars (Typ), 'P'));
920 Set_Packed_Array_Type (Typ, PAT);
923 Indexes : constant List_Id := New_List;
925 Indx_Typ : Entity_Id;
930 Indx := First_Index (Typ);
932 while Present (Indx) loop
933 Indx_Typ := Etype (Indx);
935 Enum_Case := Is_Enumeration_Type (Indx_Typ)
936 and then Has_Non_Standard_Rep (Indx_Typ);
938 -- Unconstrained case
940 if not Is_Constrained (Typ) then
942 Indx_Typ := Standard_Natural;
945 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
950 if not Enum_Case then
951 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
955 Make_Subtype_Indication (Loc,
957 New_Occurrence_Of (Standard_Natural, Loc),
959 Make_Range_Constraint (Loc,
963 Make_Attribute_Reference (Loc,
965 New_Occurrence_Of (Indx_Typ, Loc),
966 Attribute_Name => Name_Pos,
967 Expressions => New_List (
968 Make_Attribute_Reference (Loc,
970 New_Occurrence_Of (Indx_Typ, Loc),
971 Attribute_Name => Name_First))),
974 Make_Attribute_Reference (Loc,
976 New_Occurrence_Of (Indx_Typ, Loc),
977 Attribute_Name => Name_Pos,
978 Expressions => New_List (
979 Make_Attribute_Reference (Loc,
981 New_Occurrence_Of (Indx_Typ, Loc),
982 Attribute_Name => Name_Last)))))));
990 if not Is_Constrained (Typ) then
992 Make_Unconstrained_Array_Definition (Loc,
993 Subtype_Marks => Indexes,
994 Component_Definition =>
995 Make_Component_Definition (Loc,
996 Aliased_Present => False,
997 Subtype_Indication =>
998 New_Occurrence_Of (Ctyp, Loc)));
1002 Make_Constrained_Array_Definition (Loc,
1003 Discrete_Subtype_Definitions => Indexes,
1004 Component_Definition =>
1005 Make_Component_Definition (Loc,
1006 Aliased_Present => False,
1007 Subtype_Indication =>
1008 New_Occurrence_Of (Ctyp, Loc)));
1012 Make_Full_Type_Declaration (Loc,
1013 Defining_Identifier => PAT,
1014 Type_Definition => Typedef);
1017 -- Set type as packed array type and install it
1019 Set_Is_Packed_Array_Type (PAT);
1023 -- Case of bit-packing required for unconstrained array. We create
1024 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1026 elsif not Is_Constrained (Typ) then
1028 Make_Defining_Identifier (Loc,
1029 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1031 Set_Packed_Array_Type (Typ, PAT);
1035 Make_Subtype_Declaration (Loc,
1036 Defining_Identifier => PAT,
1037 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1041 -- Remaining code is for the case of bit-packing for constrained array
1043 -- The name of the packed array subtype is
1047 -- where sss is the component size in bits and ttt is the name of
1048 -- the parent packed type.
1052 Make_Defining_Identifier (Loc,
1053 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1055 Set_Packed_Array_Type (Typ, PAT);
1057 -- Build an expression for the length of the array in bits.
1058 -- This is the product of the length of each of the dimensions
1064 Len_Expr := Empty; -- suppress junk warning
1068 Make_Attribute_Reference (Loc,
1069 Attribute_Name => Name_Length,
1070 Prefix => New_Occurrence_Of (Typ, Loc),
1071 Expressions => New_List (
1072 Make_Integer_Literal (Loc, J)));
1075 Len_Expr := Len_Dim;
1079 Make_Op_Multiply (Loc,
1080 Left_Opnd => Len_Expr,
1081 Right_Opnd => Len_Dim);
1085 exit when J > Number_Dimensions (Typ);
1089 -- Temporarily attach the length expression to the tree and analyze
1090 -- and resolve it, so that we can test its value. We assume that the
1091 -- total length fits in type Integer. This expression may involve
1092 -- discriminants, so we treat it as a default/per-object expression.
1094 Set_Parent (Len_Expr, Typ);
1095 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1097 -- Use a modular type if possible. We can do this if we have
1098 -- static bounds, and the length is small enough, and the length
1099 -- is not zero. We exclude the zero length case because the size
1100 -- of things is always at least one, and the zero length object
1101 -- would have an anomalous size.
1103 if Compile_Time_Known_Value (Len_Expr) then
1104 Len_Bits := Expr_Value (Len_Expr) * Csize;
1106 -- Check for size known to be too large
1109 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1111 if System_Storage_Unit = 8 then
1113 ("packed array size cannot exceed " &
1114 "Integer''Last bytes", Typ);
1117 ("packed array size cannot exceed " &
1118 "Integer''Last storage units", Typ);
1121 -- Reset length to arbitrary not too high value to continue
1123 Len_Expr := Make_Integer_Literal (Loc, 65535);
1124 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1127 -- We normally consider small enough to mean no larger than the
1128 -- value of System_Max_Binary_Modulus_Power, checking that in the
1129 -- case of values longer than word size, we have long shifts.
1133 (Len_Bits <= System_Word_Size
1134 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1135 and then Support_Long_Shifts_On_Target))
1137 -- Also test for alignment given. If an alignment is given which
1138 -- is smaller than the natural modular alignment, force the array
1139 -- of bytes representation to accommodate the alignment.
1142 (No (Alignment_Clause (Typ))
1144 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1145 / System_Storage_Unit))
1147 -- We can use the modular type, it has the form:
1149 -- subtype tttPn is btyp
1150 -- range 0 .. 2 ** ((Typ'Length (1)
1151 -- * ... * Typ'Length (n)) * Csize) - 1;
1153 -- The bounds are statically known, and btyp is one of the
1154 -- unsigned types, depending on the length.
1156 if Len_Bits <= Standard_Short_Short_Integer_Size then
1157 Btyp := RTE (RE_Short_Short_Unsigned);
1159 elsif Len_Bits <= Standard_Short_Integer_Size then
1160 Btyp := RTE (RE_Short_Unsigned);
1162 elsif Len_Bits <= Standard_Integer_Size then
1163 Btyp := RTE (RE_Unsigned);
1165 elsif Len_Bits <= Standard_Long_Integer_Size then
1166 Btyp := RTE (RE_Long_Unsigned);
1169 Btyp := RTE (RE_Long_Long_Unsigned);
1172 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1173 Set_Print_In_Hex (Lit);
1176 Make_Subtype_Declaration (Loc,
1177 Defining_Identifier => PAT,
1178 Subtype_Indication =>
1179 Make_Subtype_Indication (Loc,
1180 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1183 Make_Range_Constraint (Loc,
1187 Make_Integer_Literal (Loc, 0),
1188 High_Bound => Lit))));
1190 if PASize = Uint_0 then
1199 -- Could not use a modular type, for all other cases, we build
1200 -- a packed array subtype:
1203 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1205 -- Bits is the length of the array in bits
1212 Make_Op_Multiply (Loc,
1214 Make_Integer_Literal (Loc, Csize),
1215 Right_Opnd => Len_Expr),
1218 Make_Integer_Literal (Loc, 7));
1220 Set_Paren_Count (Bits_U1, 1);
1223 Make_Op_Subtract (Loc,
1225 Make_Op_Divide (Loc,
1226 Left_Opnd => Bits_U1,
1227 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1228 Right_Opnd => Make_Integer_Literal (Loc, 1));
1231 Make_Subtype_Declaration (Loc,
1232 Defining_Identifier => PAT,
1233 Subtype_Indication =>
1234 Make_Subtype_Indication (Loc,
1235 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1237 Make_Index_Or_Discriminant_Constraint (Loc,
1238 Constraints => New_List (
1241 Make_Integer_Literal (Loc, 0),
1243 Convert_To (Standard_Integer, PAT_High))))));
1247 -- Currently the code in this unit requires that packed arrays
1248 -- represented by non-modular arrays of bytes be on a byte
1249 -- boundary for bit sizes handled by System.Pack_nn units.
1250 -- That's because these units assume the array being accessed
1251 -- starts on a byte boundary.
1253 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1254 Set_Must_Be_On_Byte_Boundary (Typ);
1257 end Create_Packed_Array_Type;
1259 -----------------------------------
1260 -- Expand_Bit_Packed_Element_Set --
1261 -----------------------------------
1263 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1264 Loc : constant Source_Ptr := Sloc (N);
1265 Lhs : constant Node_Id := Name (N);
1267 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1268 -- Used to preserve assignment OK status when assignment is rewritten
1270 Rhs : Node_Id := Expression (N);
1271 -- Initially Rhs is the right hand side value, it will be replaced
1272 -- later by an appropriate unchecked conversion for the assignment.
1282 -- The expression for the shift value that is required
1284 Shift_Used : Boolean := False;
1285 -- Set True if Shift has been used in the generated code at least
1286 -- once, so that it must be duplicated if used again
1291 Rhs_Val_Known : Boolean;
1293 -- If the value of the right hand side as an integer constant is
1294 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1295 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1296 -- the Rhs_Val is undefined.
1298 function Get_Shift return Node_Id;
1299 -- Function used to get the value of Shift, making sure that it
1300 -- gets duplicated if the function is called more than once.
1306 function Get_Shift return Node_Id is
1308 -- If we used the shift value already, then duplicate it. We
1309 -- set a temporary parent in case actions have to be inserted.
1312 Set_Parent (Shift, N);
1313 return Duplicate_Subexpr_No_Checks (Shift);
1315 -- If first time, use Shift unchanged, and set flag for first use
1323 -- Start of processing for Expand_Bit_Packed_Element_Set
1326 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1328 Obj := Relocate_Node (Prefix (Lhs));
1329 Convert_To_Actual_Subtype (Obj);
1330 Atyp := Etype (Obj);
1331 PAT := Packed_Array_Type (Atyp);
1332 Ctyp := Component_Type (Atyp);
1333 Csiz := UI_To_Int (Component_Size (Atyp));
1335 -- We convert the right hand side to the proper subtype to ensure
1336 -- that an appropriate range check is made (since the normal range
1337 -- check from assignment will be lost in the transformations). This
1338 -- conversion is analyzed immediately so that subsequent processing
1339 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1341 -- If the right-hand side is a string literal, create a temporary for
1342 -- it, constant-folding is not ready to wrap the bit representation
1343 -- of a string literal.
1345 if Nkind (Rhs) = N_String_Literal then
1350 Make_Object_Declaration (Loc,
1351 Defining_Identifier =>
1352 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1353 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1354 Expression => New_Copy_Tree (Rhs));
1356 Insert_Actions (N, New_List (Decl));
1357 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1361 Rhs := Convert_To (Ctyp, Rhs);
1362 Set_Parent (Rhs, N);
1363 Analyze_And_Resolve (Rhs, Ctyp);
1365 -- Case of component size 1,2,4 or any component size for the modular
1366 -- case. These are the cases for which we can inline the code.
1368 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1369 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1371 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1373 -- The statement to be generated is:
1375 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1377 -- where mask1 is obtained by shifting Cmask left Shift bits
1378 -- and then complementing the result.
1380 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1382 -- the "or ..." is omitted if rhs is constant and all 0 bits
1384 -- rhs is converted to the appropriate type
1386 -- The result is converted back to the array type, since
1387 -- otherwise we lose knowledge of the packed nature.
1389 -- Determine if right side is all 0 bits or all 1 bits
1391 if Compile_Time_Known_Value (Rhs) then
1392 Rhs_Val := Expr_Rep_Value (Rhs);
1393 Rhs_Val_Known := True;
1395 -- The following test catches the case of an unchecked conversion
1396 -- of an integer literal. This results from optimizing aggregates
1399 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1400 and then Compile_Time_Known_Value (Expression (Rhs))
1402 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1403 Rhs_Val_Known := True;
1407 Rhs_Val_Known := False;
1410 -- Some special checks for the case where the right hand value
1411 -- is known at compile time. Basically we have to take care of
1412 -- the implicit conversion to the subtype of the component object.
1414 if Rhs_Val_Known then
1416 -- If we have a biased component type then we must manually do
1417 -- the biasing, since we are taking responsibility in this case
1418 -- for constructing the exact bit pattern to be used.
1420 if Has_Biased_Representation (Ctyp) then
1421 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1424 -- For a negative value, we manually convert the twos complement
1425 -- value to a corresponding unsigned value, so that the proper
1426 -- field width is maintained. If we did not do this, we would
1427 -- get too many leading sign bits later on.
1430 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1434 New_Lhs := Duplicate_Subexpr (Obj, True);
1435 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1437 -- First we deal with the "and"
1439 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1445 if Compile_Time_Known_Value (Shift) then
1447 Make_Integer_Literal (Loc,
1448 Modulus (Etype (Obj)) - 1 -
1449 (Cmask * (2 ** Expr_Value (Get_Shift))));
1450 Set_Print_In_Hex (Mask1);
1453 Lit := Make_Integer_Literal (Loc, Cmask);
1454 Set_Print_In_Hex (Lit);
1457 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1462 Left_Opnd => New_Rhs,
1463 Right_Opnd => Mask1);
1467 -- Then deal with the "or"
1469 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1473 procedure Fixup_Rhs;
1474 -- Adjust Rhs by bias if biased representation for components
1475 -- or remove extraneous high order sign bits if signed.
1477 procedure Fixup_Rhs is
1478 Etyp : constant Entity_Id := Etype (Rhs);
1481 -- For biased case, do the required biasing by simply
1482 -- converting to the biased subtype (the conversion
1483 -- will generate the required bias).
1485 if Has_Biased_Representation (Ctyp) then
1486 Rhs := Convert_To (Ctyp, Rhs);
1488 -- For a signed integer type that is not biased, generate
1489 -- a conversion to unsigned to strip high order sign bits.
1491 elsif Is_Signed_Integer_Type (Ctyp) then
1492 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1495 -- Set Etype, since it can be referenced before the
1496 -- node is completely analyzed.
1498 Set_Etype (Rhs, Etyp);
1500 -- We now need to do an unchecked conversion of the
1501 -- result to the target type, but it is important that
1502 -- this conversion be a right justified conversion and
1503 -- not a left justified conversion.
1505 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1511 and then Compile_Time_Known_Value (Get_Shift)
1514 Make_Integer_Literal (Loc,
1515 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1516 Set_Print_In_Hex (Or_Rhs);
1519 -- We have to convert the right hand side to Etype (Obj).
1520 -- A special case case arises if what we have now is a Val
1521 -- attribute reference whose expression type is Etype (Obj).
1522 -- This happens for assignments of fields from the same
1523 -- array. In this case we get the required right hand side
1524 -- by simply removing the inner attribute reference.
1526 if Nkind (Rhs) = N_Attribute_Reference
1527 and then Attribute_Name (Rhs) = Name_Val
1528 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1530 Rhs := Relocate_Node (First (Expressions (Rhs)));
1533 -- If the value of the right hand side is a known integer
1534 -- value, then just replace it by an untyped constant,
1535 -- which will be properly retyped when we analyze and
1536 -- resolve the expression.
1538 elsif Rhs_Val_Known then
1540 -- Note that Rhs_Val has already been normalized to
1541 -- be an unsigned value with the proper number of bits.
1544 Make_Integer_Literal (Loc, Rhs_Val);
1546 -- Otherwise we need an unchecked conversion
1552 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1555 if Nkind (New_Rhs) = N_Op_And then
1556 Set_Paren_Count (New_Rhs, 1);
1561 Left_Opnd => New_Rhs,
1562 Right_Opnd => Or_Rhs);
1566 -- Now do the rewrite
1569 Make_Assignment_Statement (Loc,
1572 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1573 Set_Assignment_OK (Name (N), Ass_OK);
1575 -- All other component sizes for non-modular case
1580 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1582 -- where Subscr is the computed linear subscript
1585 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1591 if No (Bits_nn) then
1593 -- Error, most likely High_Integrity_Mode restriction
1598 -- Acquire proper Set entity. We use the aligned or unaligned
1599 -- case as appropriate.
1601 if Known_Aligned_Enough (Obj, Csiz) then
1602 Set_nn := RTE (Set_Id (Csiz));
1604 Set_nn := RTE (SetU_Id (Csiz));
1607 -- Now generate the set reference
1609 Obj := Relocate_Node (Prefix (Lhs));
1610 Convert_To_Actual_Subtype (Obj);
1611 Atyp := Etype (Obj);
1612 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1614 -- Below we must make the assumption that Obj is
1615 -- at least byte aligned, since otherwise its address
1616 -- cannot be taken. The assumption holds since the
1617 -- only arrays that can be misaligned are small packed
1618 -- arrays which are implemented as a modular type, and
1619 -- that is not the case here.
1622 Make_Procedure_Call_Statement (Loc,
1623 Name => New_Occurrence_Of (Set_nn, Loc),
1624 Parameter_Associations => New_List (
1625 Make_Attribute_Reference (Loc,
1626 Attribute_Name => Name_Address,
1629 Unchecked_Convert_To (Bits_nn,
1630 Convert_To (Ctyp, Rhs)))));
1635 Analyze (N, Suppress => All_Checks);
1636 end Expand_Bit_Packed_Element_Set;
1638 -------------------------------------
1639 -- Expand_Packed_Address_Reference --
1640 -------------------------------------
1642 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1643 Loc : constant Source_Ptr := Sloc (N);
1655 -- We build up an expression serially that has the form
1657 -- outer_object'Address
1658 -- + (linear-subscript * component_size for each array reference
1659 -- + field'Bit_Position for each record field
1661 -- + ...) / Storage_Unit;
1663 -- Some additional conversions are required to deal with the addition
1664 -- operation, which is not normally visible to generated code.
1667 Ploc := Sloc (Pref);
1669 if Nkind (Pref) = N_Indexed_Component then
1670 Convert_To_Actual_Subtype (Prefix (Pref));
1671 Atyp := Etype (Prefix (Pref));
1672 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1675 Make_Op_Multiply (Ploc,
1676 Left_Opnd => Subscr,
1678 Make_Attribute_Reference (Ploc,
1679 Prefix => New_Occurrence_Of (Atyp, Ploc),
1680 Attribute_Name => Name_Component_Size));
1682 elsif Nkind (Pref) = N_Selected_Component then
1684 Make_Attribute_Reference (Ploc,
1685 Prefix => Selector_Name (Pref),
1686 Attribute_Name => Name_Bit_Position);
1692 Term := Convert_To (RTE (RE_Integer_Address), Term);
1701 Right_Opnd => Term);
1704 Pref := Prefix (Pref);
1708 Unchecked_Convert_To (RTE (RE_Address),
1711 Unchecked_Convert_To (RTE (RE_Integer_Address),
1712 Make_Attribute_Reference (Loc,
1714 Attribute_Name => Name_Address)),
1717 Make_Op_Divide (Loc,
1720 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1722 Analyze_And_Resolve (N, RTE (RE_Address));
1723 end Expand_Packed_Address_Reference;
1725 ------------------------------------
1726 -- Expand_Packed_Boolean_Operator --
1727 ------------------------------------
1729 -- This routine expands "a op b" for the packed cases
1731 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1732 Loc : constant Source_Ptr := Sloc (N);
1733 Typ : constant Entity_Id := Etype (N);
1734 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1735 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1742 Convert_To_Actual_Subtype (L);
1743 Convert_To_Actual_Subtype (R);
1745 Ensure_Defined (Etype (L), N);
1746 Ensure_Defined (Etype (R), N);
1748 Apply_Length_Check (R, Etype (L));
1753 -- First an odd and silly test. We explicitly check for the XOR
1754 -- case where the component type is True .. True, since this will
1755 -- raise constraint error. A special check is required since CE
1756 -- will not be required other wise (cf Expand_Packed_Not).
1758 -- No such check is required for AND and OR, since for both these
1759 -- cases False op False = False, and True op True = True.
1761 if Nkind (N) = N_Op_Xor then
1763 CT : constant Entity_Id := Component_Type (Rtyp);
1764 BT : constant Entity_Id := Base_Type (CT);
1768 Make_Raise_Constraint_Error (Loc,
1774 Make_Attribute_Reference (Loc,
1775 Prefix => New_Occurrence_Of (CT, Loc),
1776 Attribute_Name => Name_First),
1780 New_Occurrence_Of (Standard_True, Loc))),
1785 Make_Attribute_Reference (Loc,
1786 Prefix => New_Occurrence_Of (CT, Loc),
1787 Attribute_Name => Name_Last),
1791 New_Occurrence_Of (Standard_True, Loc)))),
1792 Reason => CE_Range_Check_Failed));
1796 -- Now that that silliness is taken care of, get packed array type
1798 Convert_To_PAT_Type (L);
1799 Convert_To_PAT_Type (R);
1803 -- For the modular case, we expand a op b into
1805 -- rtyp!(pat!(a) op pat!(b))
1807 -- where rtyp is the Etype of the left operand. Note that we do not
1808 -- convert to the base type, since this would be unconstrained, and
1809 -- hence not have a corresponding packed array type set.
1811 -- Note that both operands must be modular for this code to be used
1813 if Is_Modular_Integer_Type (PAT)
1815 Is_Modular_Integer_Type (Etype (R))
1821 if Nkind (N) = N_Op_And then
1822 P := Make_Op_And (Loc, L, R);
1824 elsif Nkind (N) = N_Op_Or then
1825 P := Make_Op_Or (Loc, L, R);
1827 else -- Nkind (N) = N_Op_Xor
1828 P := Make_Op_Xor (Loc, L, R);
1831 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1834 -- For the array case, we insert the actions
1838 -- System.Bitops.Bit_And/Or/Xor
1840 -- Ltype'Length * Ltype'Component_Size;
1842 -- Rtype'Length * Rtype'Component_Size
1845 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1846 -- the second argument and fourth arguments are the lengths of the
1847 -- operands in bits. Then we replace the expression by a reference
1850 -- Note that if we are mixing a modular and array operand, everything
1851 -- works fine, since we ensure that the modular representation has the
1852 -- same physical layout as the array representation (that's what the
1853 -- left justified modular stuff in the big-endian case is about).
1857 Result_Ent : constant Entity_Id :=
1858 Make_Defining_Identifier (Loc,
1859 Chars => New_Internal_Name ('T'));
1864 if Nkind (N) = N_Op_And then
1867 elsif Nkind (N) = N_Op_Or then
1870 else -- Nkind (N) = N_Op_Xor
1874 Insert_Actions (N, New_List (
1876 Make_Object_Declaration (Loc,
1877 Defining_Identifier => Result_Ent,
1878 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1880 Make_Procedure_Call_Statement (Loc,
1881 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1882 Parameter_Associations => New_List (
1884 Make_Byte_Aligned_Attribute_Reference (Loc,
1885 Attribute_Name => Name_Address,
1888 Make_Op_Multiply (Loc,
1890 Make_Attribute_Reference (Loc,
1893 (Etype (First_Index (Ltyp)), Loc),
1894 Attribute_Name => Name_Range_Length),
1896 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1898 Make_Byte_Aligned_Attribute_Reference (Loc,
1899 Attribute_Name => Name_Address,
1902 Make_Op_Multiply (Loc,
1904 Make_Attribute_Reference (Loc,
1907 (Etype (First_Index (Rtyp)), Loc),
1908 Attribute_Name => Name_Range_Length),
1910 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1912 Make_Byte_Aligned_Attribute_Reference (Loc,
1913 Attribute_Name => Name_Address,
1914 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1917 New_Occurrence_Of (Result_Ent, Loc));
1921 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1922 end Expand_Packed_Boolean_Operator;
1924 -------------------------------------
1925 -- Expand_Packed_Element_Reference --
1926 -------------------------------------
1928 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1929 Loc : constant Source_Ptr := Sloc (N);
1941 -- If not bit packed, we have the enumeration case, which is easily
1942 -- dealt with (just adjust the subscripts of the indexed component)
1944 -- Note: this leaves the result as an indexed component, which is
1945 -- still a variable, so can be used in the assignment case, as is
1946 -- required in the enumeration case.
1948 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1949 Setup_Enumeration_Packed_Array_Reference (N);
1953 -- Remaining processing is for the bit-packed case
1955 Obj := Relocate_Node (Prefix (N));
1956 Convert_To_Actual_Subtype (Obj);
1957 Atyp := Etype (Obj);
1958 PAT := Packed_Array_Type (Atyp);
1959 Ctyp := Component_Type (Atyp);
1960 Csiz := UI_To_Int (Component_Size (Atyp));
1962 -- Case of component size 1,2,4 or any component size for the modular
1963 -- case. These are the cases for which we can inline the code.
1965 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1966 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1968 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1969 Lit := Make_Integer_Literal (Loc, Cmask);
1970 Set_Print_In_Hex (Lit);
1972 -- We generate a shift right to position the field, followed by a
1973 -- masking operation to extract the bit field, and we finally do an
1974 -- unchecked conversion to convert the result to the required target.
1976 -- Note that the unchecked conversion automatically deals with the
1977 -- bias if we are dealing with a biased representation. What will
1978 -- happen is that we temporarily generate the biased representation,
1979 -- but almost immediately that will be converted to the original
1980 -- unbiased component type, and the bias will disappear.
1984 Left_Opnd => Make_Shift_Right (Obj, Shift),
1987 -- We neded to analyze this before we do the unchecked convert
1988 -- below, but we need it temporarily attached to the tree for
1989 -- this analysis (hence the temporary Set_Parent call).
1991 Set_Parent (Arg, Parent (N));
1992 Analyze_And_Resolve (Arg);
1995 RJ_Unchecked_Convert_To (Ctyp, Arg));
1997 -- All other component sizes for non-modular case
2002 -- Component_Type!(Get_nn (Arr'address, Subscr))
2004 -- where Subscr is the computed linear subscript
2011 -- Acquire proper Get entity. We use the aligned or unaligned
2012 -- case as appropriate.
2014 if Known_Aligned_Enough (Obj, Csiz) then
2015 Get_nn := RTE (Get_Id (Csiz));
2017 Get_nn := RTE (GetU_Id (Csiz));
2020 -- Now generate the get reference
2022 Compute_Linear_Subscript (Atyp, N, Subscr);
2024 -- Below we make the assumption that Obj is at least byte
2025 -- aligned, since otherwise its address cannot be taken.
2026 -- The assumption holds since the only arrays that can be
2027 -- misaligned are small packed arrays which are implemented
2028 -- as a modular type, and that is not the case here.
2031 Unchecked_Convert_To (Ctyp,
2032 Make_Function_Call (Loc,
2033 Name => New_Occurrence_Of (Get_nn, Loc),
2034 Parameter_Associations => New_List (
2035 Make_Attribute_Reference (Loc,
2036 Attribute_Name => Name_Address,
2042 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2044 end Expand_Packed_Element_Reference;
2046 ----------------------
2047 -- Expand_Packed_Eq --
2048 ----------------------
2050 -- Handles expansion of "=" on packed array types
2052 procedure Expand_Packed_Eq (N : Node_Id) is
2053 Loc : constant Source_Ptr := Sloc (N);
2054 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2055 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2065 Convert_To_Actual_Subtype (L);
2066 Convert_To_Actual_Subtype (R);
2067 Ltyp := Underlying_Type (Etype (L));
2068 Rtyp := Underlying_Type (Etype (R));
2070 Convert_To_PAT_Type (L);
2071 Convert_To_PAT_Type (R);
2075 Make_Op_Multiply (Loc,
2077 Make_Attribute_Reference (Loc,
2078 Attribute_Name => Name_Length,
2079 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2081 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2084 Make_Op_Multiply (Loc,
2086 Make_Attribute_Reference (Loc,
2087 Attribute_Name => Name_Length,
2088 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2090 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2092 -- For the modular case, we transform the comparison to:
2094 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2096 -- where PAT is the packed array type. This works fine, since in the
2097 -- modular case we guarantee that the unused bits are always zeroes.
2098 -- We do have to compare the lengths because we could be comparing
2099 -- two different subtypes of the same base type.
2101 if Is_Modular_Integer_Type (PAT) then
2106 Left_Opnd => LLexpr,
2107 Right_Opnd => RLexpr),
2114 -- For the non-modular case, we call a runtime routine
2116 -- System.Bit_Ops.Bit_Eq
2117 -- (L'Address, L_Length, R'Address, R_Length)
2119 -- where PAT is the packed array type, and the lengths are the lengths
2120 -- in bits of the original packed arrays. This routine takes care of
2121 -- not comparing the unused bits in the last byte.
2125 Make_Function_Call (Loc,
2126 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2127 Parameter_Associations => New_List (
2128 Make_Byte_Aligned_Attribute_Reference (Loc,
2129 Attribute_Name => Name_Address,
2134 Make_Byte_Aligned_Attribute_Reference (Loc,
2135 Attribute_Name => Name_Address,
2141 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2142 end Expand_Packed_Eq;
2144 -----------------------
2145 -- Expand_Packed_Not --
2146 -----------------------
2148 -- Handles expansion of "not" on packed array types
2150 procedure Expand_Packed_Not (N : Node_Id) is
2151 Loc : constant Source_Ptr := Sloc (N);
2152 Typ : constant Entity_Id := Etype (N);
2153 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2160 Convert_To_Actual_Subtype (Opnd);
2161 Rtyp := Etype (Opnd);
2163 -- First an odd and silly test. We explicitly check for the case
2164 -- where the 'First of the component type is equal to the 'Last of
2165 -- this component type, and if this is the case, we make sure that
2166 -- constraint error is raised. The reason is that the NOT is bound
2167 -- to cause CE in this case, and we will not otherwise catch it.
2169 -- Believe it or not, this was reported as a bug. Note that nearly
2170 -- always, the test will evaluate statically to False, so the code
2171 -- will be statically removed, and no extra overhead caused.
2174 CT : constant Entity_Id := Component_Type (Rtyp);
2178 Make_Raise_Constraint_Error (Loc,
2182 Make_Attribute_Reference (Loc,
2183 Prefix => New_Occurrence_Of (CT, Loc),
2184 Attribute_Name => Name_First),
2187 Make_Attribute_Reference (Loc,
2188 Prefix => New_Occurrence_Of (CT, Loc),
2189 Attribute_Name => Name_Last)),
2190 Reason => CE_Range_Check_Failed));
2193 -- Now that that silliness is taken care of, get packed array type
2195 Convert_To_PAT_Type (Opnd);
2196 PAT := Etype (Opnd);
2198 -- For the case where the packed array type is a modular type,
2199 -- not A expands simply into:
2201 -- rtyp!(PAT!(A) xor mask)
2203 -- where PAT is the packed array type, and mask is a mask of all
2204 -- one bits of length equal to the size of this packed type and
2205 -- rtyp is the actual subtype of the operand
2207 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2208 Set_Print_In_Hex (Lit);
2210 if not Is_Array_Type (PAT) then
2212 Unchecked_Convert_To (Rtyp,
2215 Right_Opnd => Lit)));
2217 -- For the array case, we insert the actions
2221 -- System.Bitops.Bit_Not
2223 -- Typ'Length * Typ'Component_Size;
2226 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2227 -- argument is the length of the operand in bits. Then we replace
2228 -- the expression by a reference to Result.
2232 Result_Ent : constant Entity_Id :=
2233 Make_Defining_Identifier (Loc,
2234 Chars => New_Internal_Name ('T'));
2237 Insert_Actions (N, New_List (
2239 Make_Object_Declaration (Loc,
2240 Defining_Identifier => Result_Ent,
2241 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2243 Make_Procedure_Call_Statement (Loc,
2244 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2245 Parameter_Associations => New_List (
2247 Make_Byte_Aligned_Attribute_Reference (Loc,
2248 Attribute_Name => Name_Address,
2251 Make_Op_Multiply (Loc,
2253 Make_Attribute_Reference (Loc,
2256 (Etype (First_Index (Rtyp)), Loc),
2257 Attribute_Name => Name_Range_Length),
2259 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2261 Make_Byte_Aligned_Attribute_Reference (Loc,
2262 Attribute_Name => Name_Address,
2263 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2266 New_Occurrence_Of (Result_Ent, Loc));
2270 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2272 end Expand_Packed_Not;
2274 -------------------------------------
2275 -- Involves_Packed_Array_Reference --
2276 -------------------------------------
2278 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2280 if Nkind (N) = N_Indexed_Component
2281 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2285 elsif Nkind (N) = N_Selected_Component then
2286 return Involves_Packed_Array_Reference (Prefix (N));
2291 end Involves_Packed_Array_Reference;
2293 --------------------------
2294 -- Known_Aligned_Enough --
2295 --------------------------
2297 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2298 Typ : constant Entity_Id := Etype (Obj);
2300 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2301 -- If the component is in a record that contains previous packed
2302 -- components, consider it unaligned because the back-end might
2303 -- choose to pack the rest of the record. Lead to less efficient code,
2304 -- but safer vis-a-vis of back-end choices.
2306 --------------------------------
2307 -- In_Partially_Packed_Record --
2308 --------------------------------
2310 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2311 Rec_Type : constant Entity_Id := Scope (Comp);
2312 Prev_Comp : Entity_Id;
2315 Prev_Comp := First_Entity (Rec_Type);
2316 while Present (Prev_Comp) loop
2317 if Is_Packed (Etype (Prev_Comp)) then
2320 elsif Prev_Comp = Comp then
2324 Next_Entity (Prev_Comp);
2328 end In_Partially_Packed_Record;
2330 -- Start of processing for Known_Aligned_Enough
2333 -- Odd bit sizes don't need alignment anyway
2335 if Csiz mod 2 = 1 then
2338 -- If we have a specified alignment, see if it is sufficient, if not
2339 -- then we can't possibly be aligned enough in any case.
2341 elsif Known_Alignment (Etype (Obj)) then
2342 -- Alignment required is 4 if size is a multiple of 4, and
2343 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2345 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2350 -- OK, alignment should be sufficient, if object is aligned
2352 -- If object is strictly aligned, then it is definitely aligned
2354 if Strict_Alignment (Typ) then
2357 -- Case of subscripted array reference
2359 elsif Nkind (Obj) = N_Indexed_Component then
2361 -- If we have a pointer to an array, then this is definitely
2362 -- aligned, because pointers always point to aligned versions.
2364 if Is_Access_Type (Etype (Prefix (Obj))) then
2367 -- Otherwise, go look at the prefix
2370 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2373 -- Case of record field
2375 elsif Nkind (Obj) = N_Selected_Component then
2377 -- What is significant here is whether the record type is packed
2379 if Is_Record_Type (Etype (Prefix (Obj)))
2380 and then Is_Packed (Etype (Prefix (Obj)))
2384 -- Or the component has a component clause which might cause
2385 -- the component to become unaligned (we can't tell if the
2386 -- backend is doing alignment computations).
2388 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2391 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2394 -- In all other cases, go look at prefix
2397 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2400 elsif Nkind (Obj) = N_Type_Conversion then
2401 return Known_Aligned_Enough (Expression (Obj), Csiz);
2403 -- For a formal parameter, it is safer to assume that it is not
2404 -- aligned, because the formal may be unconstrained while the actual
2405 -- is constrained. In this situation, a small constrained packed
2406 -- array, represented in modular form, may be unaligned.
2408 elsif Is_Entity_Name (Obj) then
2409 return not Is_Formal (Entity (Obj));
2412 -- If none of the above, must be aligned
2415 end Known_Aligned_Enough;
2417 ---------------------
2418 -- Make_Shift_Left --
2419 ---------------------
2421 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2425 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2429 Make_Op_Shift_Left (Sloc (N),
2432 Set_Shift_Count_OK (Nod, True);
2435 end Make_Shift_Left;
2437 ----------------------
2438 -- Make_Shift_Right --
2439 ----------------------
2441 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2445 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2449 Make_Op_Shift_Right (Sloc (N),
2452 Set_Shift_Count_OK (Nod, True);
2455 end Make_Shift_Right;
2457 -----------------------------
2458 -- RJ_Unchecked_Convert_To --
2459 -----------------------------
2461 function RJ_Unchecked_Convert_To
2463 Expr : Node_Id) return Node_Id
2465 Source_Typ : constant Entity_Id := Etype (Expr);
2466 Target_Typ : constant Entity_Id := Typ;
2468 Src : Node_Id := Expr;
2474 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2475 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2477 -- First step, if the source type is not a discrete type, then we
2478 -- first convert to a modular type of the source length, since
2479 -- otherwise, on a big-endian machine, we get left-justification.
2480 -- We do it for little-endian machines as well, because there might
2481 -- be junk bits that are not cleared if the type is not numeric.
2483 if Source_Siz /= Target_Siz
2484 and then not Is_Discrete_Type (Source_Typ)
2486 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2489 -- In the big endian case, if the lengths of the two types differ,
2490 -- then we must worry about possible left justification in the
2491 -- conversion, and avoiding that is what this is all about.
2493 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2495 -- Next step. If the target is not a discrete type, then we first
2496 -- convert to a modular type of the target length, since
2497 -- otherwise, on a big-endian machine, we get left-justification.
2499 if not Is_Discrete_Type (Target_Typ) then
2500 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2504 -- And now we can do the final conversion to the target type
2506 return Unchecked_Convert_To (Target_Typ, Src);
2507 end RJ_Unchecked_Convert_To;
2509 ----------------------------------------------
2510 -- Setup_Enumeration_Packed_Array_Reference --
2511 ----------------------------------------------
2513 -- All we have to do here is to find the subscripts that correspond
2514 -- to the index positions that have non-standard enumeration types
2515 -- and insert a Pos attribute to get the proper subscript value.
2517 -- Finally the prefix must be uncheck converted to the corresponding
2518 -- packed array type.
2520 -- Note that the component type is unchanged, so we do not need to
2521 -- fiddle with the types (Gigi always automatically takes the packed
2522 -- array type if it is set, as it will be in this case).
2524 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2525 Pfx : constant Node_Id := Prefix (N);
2526 Typ : constant Entity_Id := Etype (N);
2527 Exprs : constant List_Id := Expressions (N);
2531 -- If the array is unconstrained, then we replace the array
2532 -- reference with its actual subtype. This actual subtype will
2533 -- have a packed array type with appropriate bounds.
2535 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2536 Convert_To_Actual_Subtype (Pfx);
2539 Expr := First (Exprs);
2540 while Present (Expr) loop
2542 Loc : constant Source_Ptr := Sloc (Expr);
2543 Expr_Typ : constant Entity_Id := Etype (Expr);
2546 if Is_Enumeration_Type (Expr_Typ)
2547 and then Has_Non_Standard_Rep (Expr_Typ)
2550 Make_Attribute_Reference (Loc,
2551 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2552 Attribute_Name => Name_Pos,
2553 Expressions => New_List (Relocate_Node (Expr))));
2554 Analyze_And_Resolve (Expr, Standard_Natural);
2562 Make_Indexed_Component (Sloc (N),
2564 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2565 Expressions => Exprs));
2567 Analyze_And_Resolve (N, Typ);
2569 end Setup_Enumeration_Packed_Array_Reference;
2571 -----------------------------------------
2572 -- Setup_Inline_Packed_Array_Reference --
2573 -----------------------------------------
2575 procedure Setup_Inline_Packed_Array_Reference
2578 Obj : in out Node_Id;
2580 Shift : out Node_Id)
2582 Loc : constant Source_Ptr := Sloc (N);
2589 Csiz := Component_Size (Atyp);
2591 Convert_To_PAT_Type (Obj);
2594 Cmask := 2 ** Csiz - 1;
2596 if Is_Array_Type (PAT) then
2597 Otyp := Component_Type (PAT);
2598 Osiz := Component_Size (PAT);
2603 -- In the case where the PAT is a modular type, we want the actual
2604 -- size in bits of the modular value we use. This is neither the
2605 -- Object_Size nor the Value_Size, either of which may have been
2606 -- reset to strange values, but rather the minimum size. Note that
2607 -- since this is a modular type with full range, the issue of
2608 -- biased representation does not arise.
2610 Osiz := UI_From_Int (Minimum_Size (Otyp));
2613 Compute_Linear_Subscript (Atyp, N, Shift);
2615 -- If the component size is not 1, then the subscript must be
2616 -- multiplied by the component size to get the shift count.
2620 Make_Op_Multiply (Loc,
2621 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2622 Right_Opnd => Shift);
2625 -- If we have the array case, then this shift count must be broken
2626 -- down into a byte subscript, and a shift within the byte.
2628 if Is_Array_Type (PAT) then
2631 New_Shift : Node_Id;
2634 -- We must analyze shift, since we will duplicate it
2636 Set_Parent (Shift, N);
2638 (Shift, Standard_Integer, Suppress => All_Checks);
2640 -- The shift count within the word is
2645 Left_Opnd => Duplicate_Subexpr (Shift),
2646 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2648 -- The subscript to be used on the PAT array is
2652 Make_Indexed_Component (Loc,
2654 Expressions => New_List (
2655 Make_Op_Divide (Loc,
2656 Left_Opnd => Duplicate_Subexpr (Shift),
2657 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2662 -- For the modular integer case, the object to be manipulated is
2663 -- the entire array, so Obj is unchanged. Note that we will reset
2664 -- its type to PAT before returning to the caller.
2670 -- The one remaining step is to modify the shift count for the
2671 -- big-endian case. Consider the following example in a byte:
2673 -- xxxxxxxx bits of byte
2674 -- vvvvvvvv bits of value
2675 -- 33221100 little-endian numbering
2676 -- 00112233 big-endian numbering
2678 -- Here we have the case of 2-bit fields
2680 -- For the little-endian case, we already have the proper shift
2681 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2683 -- For the big endian case, we have to adjust the shift count,
2684 -- computing it as (N - F) - shift, where N is the number of bits
2685 -- in an element of the array used to implement the packed array,
2686 -- F is the number of bits in a source level array element, and
2687 -- shift is the count so far computed.
2689 if Bytes_Big_Endian then
2691 Make_Op_Subtract (Loc,
2692 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2693 Right_Opnd => Shift);
2696 Set_Parent (Shift, N);
2697 Set_Parent (Obj, N);
2698 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2699 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2701 -- Make sure final type of object is the appropriate packed type
2703 Set_Etype (Obj, Otyp);
2705 end Setup_Inline_Packed_Array_Reference;