1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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 Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Rtsfind; use Rtsfind;
37 with Sem_Ch3; use Sem_Ch3;
38 with Sem_Ch8; use Sem_Ch8;
39 with Sem_Ch13; use Sem_Ch13;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Targparm; use Targparm;
47 with Tbuild; use Tbuild;
48 with Ttypes; use Ttypes;
49 with Uintp; use Uintp;
51 package body Exp_Pakd is
53 ---------------------------
54 -- Endian Considerations --
55 ---------------------------
57 -- As described in the specification, bit numbering in a packed array
58 -- is consistent with bit numbering in a record representation clause,
59 -- and hence dependent on the endianness of the machine:
61 -- For little-endian machines, element zero is at the right hand end
62 -- (low order end) of a bit field.
64 -- For big-endian machines, element zero is at the left hand end
65 -- (high order end) of a bit field.
67 -- The shifts that are used to right justify a field therefore differ
68 -- in the two cases. For the little-endian case, we can simply use the
69 -- bit number (i.e. the element number * element size) as the count for
70 -- a right shift. For the big-endian case, we have to subtract the shift
71 -- count from an appropriate constant to use in the right shift. We use
72 -- rotates instead of shifts (which is necessary in the store case to
73 -- preserve other fields), and we expect that the backend will be able
74 -- to change the right rotate into a left rotate, avoiding the subtract,
75 -- if the architecture provides such an instruction.
77 ----------------------------------------------
78 -- Entity Tables for Packed Access Routines --
79 ----------------------------------------------
81 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
82 -- library routines. This table is used to obtain the entity for the
85 type E_Array is array (Int range 01 .. 63) of RE_Id;
87 -- Array of Bits_nn entities. Note that we do not use library routines
88 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
89 -- entries from System.Unsigned, because we also use this table for
90 -- certain special unchecked conversions in the big-endian case.
92 Bits_Id : constant E_Array :=
108 16 => RE_Unsigned_16,
124 32 => RE_Unsigned_32,
157 -- Array of Get routine entities. These are used to obtain an element
158 -- from a packed array. The N'th entry is used to obtain elements from
159 -- a packed array whose component size is N. RE_Null is used as a null
160 -- entry, for the cases where a library routine is not used.
162 Get_Id : constant E_Array :=
227 -- Array of Get routine entities to be used in the case where the packed
228 -- array is itself a component of a packed structure, and therefore may
229 -- not be fully aligned. This only affects the even sizes, since for the
230 -- odd sizes, we do not get any fixed alignment in any case.
232 GetU_Id : constant E_Array :=
297 -- Array of Set routine entities. These are used to assign an element
298 -- of a packed array. The N'th entry is used to assign elements for
299 -- a packed array whose component size is N. RE_Null is used as a null
300 -- entry, for the cases where a library routine is not used.
302 Set_Id : constant E_Array :=
367 -- Array of Set routine entities to be used in the case where the packed
368 -- array is itself a component of a packed structure, and therefore may
369 -- not be fully aligned. This only affects the even sizes, since for the
370 -- odd sizes, we do not get any fixed alignment in any case.
372 SetU_Id : constant E_Array :=
437 -----------------------
438 -- Local Subprograms --
439 -----------------------
441 procedure Compute_Linear_Subscript
444 Subscr : out Node_Id);
445 -- Given a constrained array type Atyp, and an indexed component node
446 -- N referencing an array object of this type, build an expression of
447 -- type Standard.Integer representing the zero-based linear subscript
448 -- value. This expression includes any required range checks.
450 procedure Convert_To_PAT_Type (Aexp : Node_Id);
451 -- Given an expression of a packed array type, builds a corresponding
452 -- expression whose type is the implementation type used to represent
453 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
455 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
456 -- There are two versions of the Set routines, the ones used when the
457 -- object is known to be sufficiently well aligned given the number of
458 -- bits, and the ones used when the object is not known to be aligned.
459 -- This routine is used to determine which set to use. Obj is a reference
460 -- to the object, and Csiz is the component size of the packed array.
461 -- True is returned if the alignment of object is known to be sufficient,
462 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
465 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
466 -- Build a left shift node, checking for the case of a shift count of zero
468 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
469 -- Build a right shift node, checking for the case of a shift count of zero
471 function RJ_Unchecked_Convert_To
473 Expr : Node_Id) return Node_Id;
474 -- The packed array code does unchecked conversions which in some cases
475 -- may involve non-discrete types with differing sizes. The semantics of
476 -- such conversions is potentially endian dependent, and the effect we
477 -- want here for such a conversion is to do the conversion in size as
478 -- though numeric items are involved, and we extend or truncate on the
479 -- left side. This happens naturally in the little-endian case, but in
480 -- the big endian case we can get left justification, when what we want
481 -- is right justification. This routine does the unchecked conversion in
482 -- a stepwise manner to ensure that it gives the expected result. Hence
483 -- the name (RJ = Right justified). The parameters Typ and Expr are as
484 -- for the case of a normal Unchecked_Convert_To call.
486 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
487 -- This routine is called in the Get and Set case for arrays that are
488 -- packed but not bit-packed, meaning that they have at least one
489 -- subscript that is of an enumeration type with a non-standard
490 -- representation. This routine modifies the given node to properly
491 -- reference the corresponding packed array type.
493 procedure Setup_Inline_Packed_Array_Reference
496 Obj : in out Node_Id;
498 Shift : out Node_Id);
499 -- This procedure performs common processing on the N_Indexed_Component
500 -- parameter given as N, whose prefix is a reference to a packed array.
501 -- This is used for the get and set when the component size is 1,2,4
502 -- or for other component sizes when the packed array type is a modular
503 -- type (i.e. the cases that are handled with inline code).
507 -- N is the N_Indexed_Component node for the packed array reference
509 -- Atyp is the constrained array type (the actual subtype has been
510 -- computed if necessary to obtain the constraints, but this is still
511 -- the original array type, not the Packed_Array_Type value).
513 -- Obj is the object which is to be indexed. It is always of type Atyp.
517 -- Obj is the object containing the desired bit field. It is of type
518 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
519 -- entire value, for the small static case, or the proper selected byte
520 -- from the array in the large or dynamic case. This node is analyzed
521 -- and resolved on return.
523 -- Shift is a node representing the shift count to be used in the
524 -- rotate right instruction that positions the field for access.
525 -- This node is analyzed and resolved on return.
527 -- Cmask is a mask corresponding to the width of the component field.
528 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
530 -- Note: in some cases the call to this routine may generate actions
531 -- (for handling multi-use references and the generation of the packed
532 -- array type on the fly). Such actions are inserted into the tree
533 -- directly using Insert_Action.
535 ------------------------------
536 -- Compute_Linear_Subcsript --
537 ------------------------------
539 procedure Compute_Linear_Subscript
542 Subscr : out Node_Id)
544 Loc : constant Source_Ptr := Sloc (N);
553 -- Loop through dimensions
555 Indx := First_Index (Atyp);
556 Oldsub := First (Expressions (N));
558 while Present (Indx) loop
559 Styp := Etype (Indx);
560 Newsub := Relocate_Node (Oldsub);
562 -- Get expression for the subscript value. First, if Do_Range_Check
563 -- is set on a subscript, then we must do a range check against the
564 -- original bounds (not the bounds of the packed array type). We do
565 -- this by introducing a subtype conversion.
567 if Do_Range_Check (Newsub)
568 and then Etype (Newsub) /= Styp
570 Newsub := Convert_To (Styp, Newsub);
573 -- Now evolve the expression for the subscript. First convert
574 -- the subscript to be zero based and of an integer type.
576 -- Case of integer type, where we just subtract to get lower bound
578 if Is_Integer_Type (Styp) then
580 -- If length of integer type is smaller than standard integer,
581 -- then we convert to integer first, then do the subtract
583 -- Integer (subscript) - Integer (Styp'First)
585 if Esize (Styp) < Esize (Standard_Integer) then
587 Make_Op_Subtract (Loc,
588 Left_Opnd => Convert_To (Standard_Integer, Newsub),
590 Convert_To (Standard_Integer,
591 Make_Attribute_Reference (Loc,
592 Prefix => New_Occurrence_Of (Styp, Loc),
593 Attribute_Name => Name_First)));
595 -- For larger integer types, subtract first, then convert to
596 -- integer, this deals with strange long long integer bounds.
598 -- Integer (subscript - Styp'First)
602 Convert_To (Standard_Integer,
603 Make_Op_Subtract (Loc,
606 Make_Attribute_Reference (Loc,
607 Prefix => New_Occurrence_Of (Styp, Loc),
608 Attribute_Name => Name_First)));
611 -- For the enumeration case, we have to use 'Pos to get the value
612 -- to work with before subtracting the lower bound.
614 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
616 -- This is not quite right for bizarre cases where the size of the
617 -- enumeration type is > Integer'Size bits due to rep clause ???
620 pragma Assert (Is_Enumeration_Type (Styp));
623 Make_Op_Subtract (Loc,
624 Left_Opnd => Convert_To (Standard_Integer,
625 Make_Attribute_Reference (Loc,
626 Prefix => New_Occurrence_Of (Styp, Loc),
627 Attribute_Name => Name_Pos,
628 Expressions => New_List (Newsub))),
631 Convert_To (Standard_Integer,
632 Make_Attribute_Reference (Loc,
633 Prefix => New_Occurrence_Of (Styp, Loc),
634 Attribute_Name => Name_Pos,
635 Expressions => New_List (
636 Make_Attribute_Reference (Loc,
637 Prefix => New_Occurrence_Of (Styp, Loc),
638 Attribute_Name => Name_First)))));
641 Set_Paren_Count (Newsub, 1);
643 -- For the first subscript, we just copy that subscript value
648 -- Otherwise, we must multiply what we already have by the current
649 -- stride and then add in the new value to the evolving subscript.
655 Make_Op_Multiply (Loc,
658 Make_Attribute_Reference (Loc,
659 Attribute_Name => Name_Range_Length,
660 Prefix => New_Occurrence_Of (Styp, Loc))),
661 Right_Opnd => Newsub);
664 -- Move to next subscript
669 end Compute_Linear_Subscript;
671 -------------------------
672 -- Convert_To_PAT_Type --
673 -------------------------
675 -- The PAT is always obtained from the actual subtype
677 procedure Convert_To_PAT_Type (Aexp : Entity_Id) is
681 Convert_To_Actual_Subtype (Aexp);
682 Act_ST := Underlying_Type (Etype (Aexp));
683 Create_Packed_Array_Type (Act_ST);
685 -- Just replace the etype with the packed array type. This works
686 -- because the expression will not be further analyzed, and Gigi
687 -- considers the two types equivalent in any case.
689 -- This is not strictly the case ??? If the reference is an actual
690 -- in a call, the expansion of the prefix is delayed, and must be
691 -- reanalyzed, see Reset_Packed_Prefix. On the other hand, if the
692 -- prefix is a simple array reference, reanalysis can produce spurious
693 -- type errors when the PAT type is replaced again with the original
694 -- type of the array. The following is correct and minimal, but the
695 -- handling of more complex packed expressions in actuals is confused.
696 -- It is likely that the problem only remains for actuals in calls.
698 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
700 if Is_Entity_Name (Aexp)
702 (Nkind (Aexp) = N_Indexed_Component
703 and then Is_Entity_Name (Prefix (Aexp)))
707 end Convert_To_PAT_Type;
709 ------------------------------
710 -- Create_Packed_Array_Type --
711 ------------------------------
713 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
714 Loc : constant Source_Ptr := Sloc (Typ);
715 Ctyp : constant Entity_Id := Component_Type (Typ);
716 Csize : constant Uint := Component_Size (Typ);
731 procedure Install_PAT;
732 -- This procedure is called with Decl set to the declaration for the
733 -- packed array type. It creates the type and installs it as required.
735 procedure Set_PB_Type;
736 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
737 -- requirements (see documentation in the spec of this package).
743 procedure Install_PAT is
744 Pushed_Scope : Boolean := False;
747 -- We do not want to put the declaration we have created in the tree
748 -- since it is often hard, and sometimes impossible to find a proper
749 -- place for it (the impossible case arises for a packed array type
750 -- with bounds depending on the discriminant, a declaration cannot
751 -- be put inside the record, and the reference to the discriminant
752 -- cannot be outside the record).
754 -- The solution is to analyze the declaration while temporarily
755 -- attached to the tree at an appropriate point, and then we install
756 -- the resulting type as an Itype in the packed array type field of
757 -- the original type, so that no explicit declaration is required.
759 -- Note: the packed type is created in the scope of its parent
760 -- type. There are at least some cases where the current scope
761 -- is deeper, and so when this is the case, we temporarily reset
762 -- the scope for the definition. This is clearly safe, since the
763 -- first use of the packed array type will be the implicit
764 -- reference from the corresponding unpacked type when it is
767 if Is_Itype (Typ) then
768 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
770 Set_Parent (Decl, Declaration_Node (Typ));
773 if Scope (Typ) /= Current_Scope then
774 New_Scope (Scope (Typ));
775 Pushed_Scope := True;
778 Set_Is_Itype (PAT, True);
779 Set_Packed_Array_Type (Typ, PAT);
780 Analyze (Decl, Suppress => All_Checks);
786 -- Set Esize and RM_Size to the actual size of the packed object
787 -- Do not reset RM_Size if already set, as happens in the case
788 -- of a modular type.
790 Set_Esize (PAT, PASize);
792 if Unknown_RM_Size (PAT) then
793 Set_RM_Size (PAT, PASize);
796 -- Set remaining fields of packed array type
798 Init_Alignment (PAT);
799 Set_Parent (PAT, Empty);
800 Set_Associated_Node_For_Itype (PAT, Typ);
801 Set_Is_Packed_Array_Type (PAT, True);
802 Set_Original_Array_Type (PAT, Typ);
804 -- We definitely do not want to delay freezing for packed array
805 -- types. This is of particular importance for the itypes that
806 -- are generated for record components depending on discriminants
807 -- where there is no place to put the freeze node.
809 Set_Has_Delayed_Freeze (PAT, False);
810 Set_Has_Delayed_Freeze (Etype (PAT), False);
812 -- If we did allocate a freeze node, then clear out the reference
813 -- since it is obsolete (should we delete the freeze node???)
815 Set_Freeze_Node (PAT, Empty);
816 Set_Freeze_Node (Etype (PAT), Empty);
823 procedure Set_PB_Type is
825 -- If the user has specified an explicit alignment for the
826 -- type or component, take it into account.
828 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
829 or else Alignment (Typ) = 1
830 or else Component_Alignment (Typ) = Calign_Storage_Unit
832 PB_Type := RTE (RE_Packed_Bytes1);
834 elsif Csize mod 4 /= 0
835 or else Alignment (Typ) = 2
837 PB_Type := RTE (RE_Packed_Bytes2);
840 PB_Type := RTE (RE_Packed_Bytes4);
844 -- Start of processing for Create_Packed_Array_Type
847 -- If we already have a packed array type, nothing to do
849 if Present (Packed_Array_Type (Typ)) then
853 -- If our immediate ancestor subtype is constrained, and it already
854 -- has a packed array type, then just share the same type, since the
855 -- bounds must be the same. If the ancestor is not an array type but
856 -- a private type, as can happen with multiple instantiations, create
857 -- a new packed type, to avoid privacy issues.
859 if Ekind (Typ) = E_Array_Subtype then
860 Ancest := Ancestor_Subtype (Typ);
863 and then Is_Array_Type (Ancest)
864 and then Is_Constrained (Ancest)
865 and then Present (Packed_Array_Type (Ancest))
867 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
872 -- We preset the result type size from the size of the original array
873 -- type, since this size clearly belongs to the packed array type. The
874 -- size of the conceptual unpacked type is always set to unknown.
876 PASize := Esize (Typ);
878 -- Case of an array where at least one index is of an enumeration
879 -- type with a non-standard representation, but the component size
880 -- is not appropriate for bit packing. This is the case where we
881 -- have Is_Packed set (we would never be in this unit otherwise),
882 -- but Is_Bit_Packed_Array is false.
884 -- Note that if the component size is appropriate for bit packing,
885 -- then the circuit for the computation of the subscript properly
886 -- deals with the non-standard enumeration type case by taking the
889 if not Is_Bit_Packed_Array (Typ) then
891 -- Here we build a declaration:
893 -- type tttP is array (index1, index2, ...) of component_type
895 -- where index1, index2, are the index types. These are the same
896 -- as the index types of the original array, except for the non-
897 -- standard representation enumeration type case, where we have
900 -- For the unconstrained array case, we use
904 -- For the constrained case, we use
906 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
907 -- Enum_Type'Pos (Enum_Type'Last);
910 Make_Defining_Identifier (Loc,
911 Chars => New_External_Name (Chars (Typ), 'P'));
913 Set_Packed_Array_Type (Typ, PAT);
916 Indexes : constant List_Id := New_List;
918 Indx_Typ : Entity_Id;
923 Indx := First_Index (Typ);
925 while Present (Indx) loop
926 Indx_Typ := Etype (Indx);
928 Enum_Case := Is_Enumeration_Type (Indx_Typ)
929 and then Has_Non_Standard_Rep (Indx_Typ);
931 -- Unconstrained case
933 if not Is_Constrained (Typ) then
935 Indx_Typ := Standard_Natural;
938 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
943 if not Enum_Case then
944 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
948 Make_Subtype_Indication (Loc,
950 New_Occurrence_Of (Standard_Natural, Loc),
952 Make_Range_Constraint (Loc,
956 Make_Attribute_Reference (Loc,
958 New_Occurrence_Of (Indx_Typ, Loc),
959 Attribute_Name => Name_Pos,
960 Expressions => New_List (
961 Make_Attribute_Reference (Loc,
963 New_Occurrence_Of (Indx_Typ, Loc),
964 Attribute_Name => Name_First))),
967 Make_Attribute_Reference (Loc,
969 New_Occurrence_Of (Indx_Typ, Loc),
970 Attribute_Name => Name_Pos,
971 Expressions => New_List (
972 Make_Attribute_Reference (Loc,
974 New_Occurrence_Of (Indx_Typ, Loc),
975 Attribute_Name => Name_Last)))))));
983 if not Is_Constrained (Typ) then
985 Make_Unconstrained_Array_Definition (Loc,
986 Subtype_Marks => Indexes,
987 Component_Definition =>
988 Make_Component_Definition (Loc,
989 Aliased_Present => False,
990 Subtype_Indication =>
991 New_Occurrence_Of (Ctyp, Loc)));
995 Make_Constrained_Array_Definition (Loc,
996 Discrete_Subtype_Definitions => Indexes,
997 Component_Definition =>
998 Make_Component_Definition (Loc,
999 Aliased_Present => False,
1000 Subtype_Indication =>
1001 New_Occurrence_Of (Ctyp, Loc)));
1005 Make_Full_Type_Declaration (Loc,
1006 Defining_Identifier => PAT,
1007 Type_Definition => Typedef);
1010 -- Set type as packed array type and install it
1012 Set_Is_Packed_Array_Type (PAT);
1016 -- Case of bit-packing required for unconstrained array. We create
1017 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1019 elsif not Is_Constrained (Typ) then
1021 Make_Defining_Identifier (Loc,
1022 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1024 Set_Packed_Array_Type (Typ, PAT);
1028 Make_Subtype_Declaration (Loc,
1029 Defining_Identifier => PAT,
1030 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1034 -- Remaining code is for the case of bit-packing for constrained array
1036 -- The name of the packed array subtype is
1040 -- where sss is the component size in bits and ttt is the name of
1041 -- the parent packed type.
1045 Make_Defining_Identifier (Loc,
1046 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1048 Set_Packed_Array_Type (Typ, PAT);
1050 -- Build an expression for the length of the array in bits.
1051 -- This is the product of the length of each of the dimensions
1057 Len_Expr := Empty; -- suppress junk warning
1061 Make_Attribute_Reference (Loc,
1062 Attribute_Name => Name_Length,
1063 Prefix => New_Occurrence_Of (Typ, Loc),
1064 Expressions => New_List (
1065 Make_Integer_Literal (Loc, J)));
1068 Len_Expr := Len_Dim;
1072 Make_Op_Multiply (Loc,
1073 Left_Opnd => Len_Expr,
1074 Right_Opnd => Len_Dim);
1078 exit when J > Number_Dimensions (Typ);
1082 -- Temporarily attach the length expression to the tree and analyze
1083 -- and resolve it, so that we can test its value. We assume that the
1084 -- total length fits in type Integer. This expression may involve
1085 -- discriminants, so we treat it as a default/per-object expression.
1087 Set_Parent (Len_Expr, Typ);
1088 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1090 -- Use a modular type if possible. We can do this if we have
1091 -- static bounds, and the length is small enough, and the length
1092 -- is not zero. We exclude the zero length case because the size
1093 -- of things is always at least one, and the zero length object
1094 -- would have an anomalous size.
1096 if Compile_Time_Known_Value (Len_Expr) then
1097 Len_Bits := Expr_Value (Len_Expr) * Csize;
1099 -- Check for size known to be too large
1102 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1104 if System_Storage_Unit = 8 then
1106 ("packed array size cannot exceed " &
1107 "Integer''Last bytes", Typ);
1110 ("packed array size cannot exceed " &
1111 "Integer''Last storage units", Typ);
1114 -- Reset length to arbitrary not too high value to continue
1116 Len_Expr := Make_Integer_Literal (Loc, 65535);
1117 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1120 -- We normally consider small enough to mean no larger than the
1121 -- value of System_Max_Binary_Modulus_Power, checking that in the
1122 -- case of values longer than word size, we have long shifts.
1126 (Len_Bits <= System_Word_Size
1127 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1128 and then Support_Long_Shifts_On_Target))
1130 -- Also test for alignment given. If an alignment is given which
1131 -- is smaller than the natural modular alignment, force the array
1132 -- of bytes representation to accommodate the alignment.
1135 (No (Alignment_Clause (Typ))
1137 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1138 / System_Storage_Unit))
1140 -- We can use the modular type, it has the form:
1142 -- subtype tttPn is btyp
1143 -- range 0 .. 2 ** ((Typ'Length (1)
1144 -- * ... * Typ'Length (n)) * Csize) - 1;
1146 -- The bounds are statically known, and btyp is one
1147 -- of the unsigned types, depending on the length. If the
1148 -- type is its first subtype, i.e. it is a user-defined
1149 -- type, no object of the type will be larger, and it is
1150 -- worthwhile to use a small unsigned type.
1152 if Len_Bits <= Standard_Short_Integer_Size
1153 and then First_Subtype (Typ) = Typ
1155 Btyp := RTE (RE_Short_Unsigned);
1157 elsif Len_Bits <= Standard_Integer_Size then
1158 Btyp := RTE (RE_Unsigned);
1160 elsif Len_Bits <= Standard_Long_Integer_Size then
1161 Btyp := RTE (RE_Long_Unsigned);
1164 Btyp := RTE (RE_Long_Long_Unsigned);
1167 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1168 Set_Print_In_Hex (Lit);
1171 Make_Subtype_Declaration (Loc,
1172 Defining_Identifier => PAT,
1173 Subtype_Indication =>
1174 Make_Subtype_Indication (Loc,
1175 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1178 Make_Range_Constraint (Loc,
1182 Make_Integer_Literal (Loc, 0),
1183 High_Bound => Lit))));
1185 if PASize = Uint_0 then
1194 -- Could not use a modular type, for all other cases, we build
1195 -- a packed array subtype:
1198 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1200 -- Bits is the length of the array in bits
1207 Make_Op_Multiply (Loc,
1209 Make_Integer_Literal (Loc, Csize),
1210 Right_Opnd => Len_Expr),
1213 Make_Integer_Literal (Loc, 7));
1215 Set_Paren_Count (Bits_U1, 1);
1218 Make_Op_Subtract (Loc,
1220 Make_Op_Divide (Loc,
1221 Left_Opnd => Bits_U1,
1222 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1223 Right_Opnd => Make_Integer_Literal (Loc, 1));
1226 Make_Subtype_Declaration (Loc,
1227 Defining_Identifier => PAT,
1228 Subtype_Indication =>
1229 Make_Subtype_Indication (Loc,
1230 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1232 Make_Index_Or_Discriminant_Constraint (Loc,
1233 Constraints => New_List (
1236 Make_Integer_Literal (Loc, 0),
1238 Convert_To (Standard_Integer, PAT_High))))));
1242 -- Currently the code in this unit requires that packed arrays
1243 -- represented by non-modular arrays of bytes be on a byte
1244 -- boundary for bit sizes handled by System.Pack_nn units.
1245 -- That's because these units assume the array being accessed
1246 -- starts on a byte boundary.
1248 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1249 Set_Must_Be_On_Byte_Boundary (Typ);
1252 end Create_Packed_Array_Type;
1254 -----------------------------------
1255 -- Expand_Bit_Packed_Element_Set --
1256 -----------------------------------
1258 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1259 Loc : constant Source_Ptr := Sloc (N);
1260 Lhs : constant Node_Id := Name (N);
1262 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1263 -- Used to preserve assignment OK status when assignment is rewritten
1265 Rhs : Node_Id := Expression (N);
1266 -- Initially Rhs is the right hand side value, it will be replaced
1267 -- later by an appropriate unchecked conversion for the assignment.
1277 -- The expression for the shift value that is required
1279 Shift_Used : Boolean := False;
1280 -- Set True if Shift has been used in the generated code at least
1281 -- once, so that it must be duplicated if used again
1286 Rhs_Val_Known : Boolean;
1288 -- If the value of the right hand side as an integer constant is
1289 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1290 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1291 -- the Rhs_Val is undefined.
1293 function Get_Shift return Node_Id;
1294 -- Function used to get the value of Shift, making sure that it
1295 -- gets duplicated if the function is called more than once.
1301 function Get_Shift return Node_Id is
1303 -- If we used the shift value already, then duplicate it. We
1304 -- set a temporary parent in case actions have to be inserted.
1307 Set_Parent (Shift, N);
1308 return Duplicate_Subexpr_No_Checks (Shift);
1310 -- If first time, use Shift unchanged, and set flag for first use
1318 -- Start of processing for Expand_Bit_Packed_Element_Set
1321 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1323 Obj := Relocate_Node (Prefix (Lhs));
1324 Convert_To_Actual_Subtype (Obj);
1325 Atyp := Etype (Obj);
1326 PAT := Packed_Array_Type (Atyp);
1327 Ctyp := Component_Type (Atyp);
1328 Csiz := UI_To_Int (Component_Size (Atyp));
1330 -- We convert the right hand side to the proper subtype to ensure
1331 -- that an appropriate range check is made (since the normal range
1332 -- check from assignment will be lost in the transformations). This
1333 -- conversion is analyzed immediately so that subsequent processing
1334 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1336 -- If the right-hand side is a string literal, create a temporary for
1337 -- it, constant-folding is not ready to wrap the bit representation
1338 -- of a string literal.
1340 if Nkind (Rhs) = N_String_Literal then
1345 Make_Object_Declaration (Loc,
1346 Defining_Identifier =>
1347 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1348 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1349 Expression => New_Copy_Tree (Rhs));
1351 Insert_Actions (N, New_List (Decl));
1352 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1356 Rhs := Convert_To (Ctyp, Rhs);
1357 Set_Parent (Rhs, N);
1358 Analyze_And_Resolve (Rhs, Ctyp);
1360 -- Case of component size 1,2,4 or any component size for the modular
1361 -- case. These are the cases for which we can inline the code.
1363 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1364 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1366 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1368 -- The statement to be generated is:
1370 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1372 -- where mask1 is obtained by shifting Cmask left Shift bits
1373 -- and then complementing the result.
1375 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1377 -- the "or ..." is omitted if rhs is constant and all 0 bits
1379 -- rhs is converted to the appropriate type
1381 -- The result is converted back to the array type, since
1382 -- otherwise we lose knowledge of the packed nature.
1384 -- Determine if right side is all 0 bits or all 1 bits
1386 if Compile_Time_Known_Value (Rhs) then
1387 Rhs_Val := Expr_Rep_Value (Rhs);
1388 Rhs_Val_Known := True;
1390 -- The following test catches the case of an unchecked conversion
1391 -- of an integer literal. This results from optimizing aggregates
1394 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1395 and then Compile_Time_Known_Value (Expression (Rhs))
1397 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1398 Rhs_Val_Known := True;
1402 Rhs_Val_Known := False;
1405 -- Some special checks for the case where the right hand value
1406 -- is known at compile time. Basically we have to take care of
1407 -- the implicit conversion to the subtype of the component object.
1409 if Rhs_Val_Known then
1411 -- If we have a biased component type then we must manually do
1412 -- the biasing, since we are taking responsibility in this case
1413 -- for constructing the exact bit pattern to be used.
1415 if Has_Biased_Representation (Ctyp) then
1416 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1419 -- For a negative value, we manually convert the twos complement
1420 -- value to a corresponding unsigned value, so that the proper
1421 -- field width is maintained. If we did not do this, we would
1422 -- get too many leading sign bits later on.
1425 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1429 New_Lhs := Duplicate_Subexpr (Obj, True);
1430 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1432 -- First we deal with the "and"
1434 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1440 if Compile_Time_Known_Value (Shift) then
1442 Make_Integer_Literal (Loc,
1443 Modulus (Etype (Obj)) - 1 -
1444 (Cmask * (2 ** Expr_Value (Get_Shift))));
1445 Set_Print_In_Hex (Mask1);
1448 Lit := Make_Integer_Literal (Loc, Cmask);
1449 Set_Print_In_Hex (Lit);
1452 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1457 Left_Opnd => New_Rhs,
1458 Right_Opnd => Mask1);
1462 -- Then deal with the "or"
1464 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1468 procedure Fixup_Rhs;
1469 -- Adjust Rhs by bias if biased representation for components
1470 -- or remove extraneous high order sign bits if signed.
1472 procedure Fixup_Rhs is
1473 Etyp : constant Entity_Id := Etype (Rhs);
1476 -- For biased case, do the required biasing by simply
1477 -- converting to the biased subtype (the conversion
1478 -- will generate the required bias).
1480 if Has_Biased_Representation (Ctyp) then
1481 Rhs := Convert_To (Ctyp, Rhs);
1483 -- For a signed integer type that is not biased, generate
1484 -- a conversion to unsigned to strip high order sign bits.
1486 elsif Is_Signed_Integer_Type (Ctyp) then
1487 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1490 -- Set Etype, since it can be referenced before the
1491 -- node is completely analyzed.
1493 Set_Etype (Rhs, Etyp);
1495 -- We now need to do an unchecked conversion of the
1496 -- result to the target type, but it is important that
1497 -- this conversion be a right justified conversion and
1498 -- not a left justified conversion.
1500 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1506 and then Compile_Time_Known_Value (Get_Shift)
1509 Make_Integer_Literal (Loc,
1510 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1511 Set_Print_In_Hex (Or_Rhs);
1514 -- We have to convert the right hand side to Etype (Obj).
1515 -- A special case case arises if what we have now is a Val
1516 -- attribute reference whose expression type is Etype (Obj).
1517 -- This happens for assignments of fields from the same
1518 -- array. In this case we get the required right hand side
1519 -- by simply removing the inner attribute reference.
1521 if Nkind (Rhs) = N_Attribute_Reference
1522 and then Attribute_Name (Rhs) = Name_Val
1523 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1525 Rhs := Relocate_Node (First (Expressions (Rhs)));
1528 -- If the value of the right hand side is a known integer
1529 -- value, then just replace it by an untyped constant,
1530 -- which will be properly retyped when we analyze and
1531 -- resolve the expression.
1533 elsif Rhs_Val_Known then
1535 -- Note that Rhs_Val has already been normalized to
1536 -- be an unsigned value with the proper number of bits.
1539 Make_Integer_Literal (Loc, Rhs_Val);
1541 -- Otherwise we need an unchecked conversion
1547 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1550 if Nkind (New_Rhs) = N_Op_And then
1551 Set_Paren_Count (New_Rhs, 1);
1556 Left_Opnd => New_Rhs,
1557 Right_Opnd => Or_Rhs);
1561 -- Now do the rewrite
1564 Make_Assignment_Statement (Loc,
1567 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1568 Set_Assignment_OK (Name (N), Ass_OK);
1570 -- All other component sizes for non-modular case
1575 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1577 -- where Subscr is the computed linear subscript
1580 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1586 if No (Bits_nn) then
1588 -- Error, most likely High_Integrity_Mode restriction
1593 -- Acquire proper Set entity. We use the aligned or unaligned
1594 -- case as appropriate.
1596 if Known_Aligned_Enough (Obj, Csiz) then
1597 Set_nn := RTE (Set_Id (Csiz));
1599 Set_nn := RTE (SetU_Id (Csiz));
1602 -- Now generate the set reference
1604 Obj := Relocate_Node (Prefix (Lhs));
1605 Convert_To_Actual_Subtype (Obj);
1606 Atyp := Etype (Obj);
1607 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1609 -- Below we must make the assumption that Obj is
1610 -- at least byte aligned, since otherwise its address
1611 -- cannot be taken. The assumption holds since the
1612 -- only arrays that can be misaligned are small packed
1613 -- arrays which are implemented as a modular type, and
1614 -- that is not the case here.
1617 Make_Procedure_Call_Statement (Loc,
1618 Name => New_Occurrence_Of (Set_nn, Loc),
1619 Parameter_Associations => New_List (
1620 Make_Attribute_Reference (Loc,
1621 Attribute_Name => Name_Address,
1624 Unchecked_Convert_To (Bits_nn,
1625 Convert_To (Ctyp, Rhs)))));
1630 Analyze (N, Suppress => All_Checks);
1631 end Expand_Bit_Packed_Element_Set;
1633 -------------------------------------
1634 -- Expand_Packed_Address_Reference --
1635 -------------------------------------
1637 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1638 Loc : constant Source_Ptr := Sloc (N);
1650 -- We build up an expression serially that has the form
1652 -- outer_object'Address
1653 -- + (linear-subscript * component_size for each array reference
1654 -- + field'Bit_Position for each record field
1656 -- + ...) / Storage_Unit;
1658 -- Some additional conversions are required to deal with the addition
1659 -- operation, which is not normally visible to generated code.
1662 Ploc := Sloc (Pref);
1664 if Nkind (Pref) = N_Indexed_Component then
1665 Convert_To_Actual_Subtype (Prefix (Pref));
1666 Atyp := Etype (Prefix (Pref));
1667 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1670 Make_Op_Multiply (Ploc,
1671 Left_Opnd => Subscr,
1673 Make_Attribute_Reference (Ploc,
1674 Prefix => New_Occurrence_Of (Atyp, Ploc),
1675 Attribute_Name => Name_Component_Size));
1677 elsif Nkind (Pref) = N_Selected_Component then
1679 Make_Attribute_Reference (Ploc,
1680 Prefix => Selector_Name (Pref),
1681 Attribute_Name => Name_Bit_Position);
1687 Term := Convert_To (RTE (RE_Integer_Address), Term);
1696 Right_Opnd => Term);
1699 Pref := Prefix (Pref);
1703 Unchecked_Convert_To (RTE (RE_Address),
1706 Unchecked_Convert_To (RTE (RE_Integer_Address),
1707 Make_Attribute_Reference (Loc,
1709 Attribute_Name => Name_Address)),
1712 Make_Op_Divide (Loc,
1715 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1717 Analyze_And_Resolve (N, RTE (RE_Address));
1718 end Expand_Packed_Address_Reference;
1720 ------------------------------------
1721 -- Expand_Packed_Boolean_Operator --
1722 ------------------------------------
1724 -- This routine expands "a op b" for the packed cases
1726 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1727 Loc : constant Source_Ptr := Sloc (N);
1728 Typ : constant Entity_Id := Etype (N);
1729 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1730 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1737 Convert_To_Actual_Subtype (L);
1738 Convert_To_Actual_Subtype (R);
1740 Ensure_Defined (Etype (L), N);
1741 Ensure_Defined (Etype (R), N);
1743 Apply_Length_Check (R, Etype (L));
1748 -- First an odd and silly test. We explicitly check for the XOR
1749 -- case where the component type is True .. True, since this will
1750 -- raise constraint error. A special check is required since CE
1751 -- will not be required other wise (cf Expand_Packed_Not).
1753 -- No such check is required for AND and OR, since for both these
1754 -- cases False op False = False, and True op True = True.
1756 if Nkind (N) = N_Op_Xor then
1758 CT : constant Entity_Id := Component_Type (Rtyp);
1759 BT : constant Entity_Id := Base_Type (CT);
1763 Make_Raise_Constraint_Error (Loc,
1769 Make_Attribute_Reference (Loc,
1770 Prefix => New_Occurrence_Of (CT, Loc),
1771 Attribute_Name => Name_First),
1775 New_Occurrence_Of (Standard_True, Loc))),
1780 Make_Attribute_Reference (Loc,
1781 Prefix => New_Occurrence_Of (CT, Loc),
1782 Attribute_Name => Name_Last),
1786 New_Occurrence_Of (Standard_True, Loc)))),
1787 Reason => CE_Range_Check_Failed));
1791 -- Now that that silliness is taken care of, get packed array type
1793 Convert_To_PAT_Type (L);
1794 Convert_To_PAT_Type (R);
1798 -- For the modular case, we expand a op b into
1800 -- rtyp!(pat!(a) op pat!(b))
1802 -- where rtyp is the Etype of the left operand. Note that we do not
1803 -- convert to the base type, since this would be unconstrained, and
1804 -- hence not have a corresponding packed array type set.
1806 -- Note that both operands must be modular for this code to be used
1808 if Is_Modular_Integer_Type (PAT)
1810 Is_Modular_Integer_Type (Etype (R))
1816 if Nkind (N) = N_Op_And then
1817 P := Make_Op_And (Loc, L, R);
1819 elsif Nkind (N) = N_Op_Or then
1820 P := Make_Op_Or (Loc, L, R);
1822 else -- Nkind (N) = N_Op_Xor
1823 P := Make_Op_Xor (Loc, L, R);
1826 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1829 -- For the array case, we insert the actions
1833 -- System.Bitops.Bit_And/Or/Xor
1835 -- Ltype'Length * Ltype'Component_Size;
1837 -- Rtype'Length * Rtype'Component_Size
1840 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1841 -- the second argument and fourth arguments are the lengths of the
1842 -- operands in bits. Then we replace the expression by a reference
1845 -- Note that if we are mixing a modular and array operand, everything
1846 -- works fine, since we ensure that the modular representation has the
1847 -- same physical layout as the array representation (that's what the
1848 -- left justified modular stuff in the big-endian case is about).
1852 Result_Ent : constant Entity_Id :=
1853 Make_Defining_Identifier (Loc,
1854 Chars => New_Internal_Name ('T'));
1859 if Nkind (N) = N_Op_And then
1862 elsif Nkind (N) = N_Op_Or then
1865 else -- Nkind (N) = N_Op_Xor
1869 Insert_Actions (N, New_List (
1871 Make_Object_Declaration (Loc,
1872 Defining_Identifier => Result_Ent,
1873 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1875 Make_Procedure_Call_Statement (Loc,
1876 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1877 Parameter_Associations => New_List (
1879 Make_Byte_Aligned_Attribute_Reference (Loc,
1880 Attribute_Name => Name_Address,
1883 Make_Op_Multiply (Loc,
1885 Make_Attribute_Reference (Loc,
1888 (Etype (First_Index (Ltyp)), Loc),
1889 Attribute_Name => Name_Range_Length),
1891 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1893 Make_Byte_Aligned_Attribute_Reference (Loc,
1894 Attribute_Name => Name_Address,
1897 Make_Op_Multiply (Loc,
1899 Make_Attribute_Reference (Loc,
1902 (Etype (First_Index (Rtyp)), Loc),
1903 Attribute_Name => Name_Range_Length),
1905 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1907 Make_Byte_Aligned_Attribute_Reference (Loc,
1908 Attribute_Name => Name_Address,
1909 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1912 New_Occurrence_Of (Result_Ent, Loc));
1916 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1917 end Expand_Packed_Boolean_Operator;
1919 -------------------------------------
1920 -- Expand_Packed_Element_Reference --
1921 -------------------------------------
1923 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1924 Loc : constant Source_Ptr := Sloc (N);
1936 -- If not bit packed, we have the enumeration case, which is easily
1937 -- dealt with (just adjust the subscripts of the indexed component)
1939 -- Note: this leaves the result as an indexed component, which is
1940 -- still a variable, so can be used in the assignment case, as is
1941 -- required in the enumeration case.
1943 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1944 Setup_Enumeration_Packed_Array_Reference (N);
1948 -- Remaining processing is for the bit-packed case
1950 Obj := Relocate_Node (Prefix (N));
1951 Convert_To_Actual_Subtype (Obj);
1952 Atyp := Etype (Obj);
1953 PAT := Packed_Array_Type (Atyp);
1954 Ctyp := Component_Type (Atyp);
1955 Csiz := UI_To_Int (Component_Size (Atyp));
1957 -- Case of component size 1,2,4 or any component size for the modular
1958 -- case. These are the cases for which we can inline the code.
1960 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1961 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1963 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1964 Lit := Make_Integer_Literal (Loc, Cmask);
1965 Set_Print_In_Hex (Lit);
1967 -- We generate a shift right to position the field, followed by a
1968 -- masking operation to extract the bit field, and we finally do an
1969 -- unchecked conversion to convert the result to the required target.
1971 -- Note that the unchecked conversion automatically deals with the
1972 -- bias if we are dealing with a biased representation. What will
1973 -- happen is that we temporarily generate the biased representation,
1974 -- but almost immediately that will be converted to the original
1975 -- unbiased component type, and the bias will disappear.
1979 Left_Opnd => Make_Shift_Right (Obj, Shift),
1982 -- We neded to analyze this before we do the unchecked convert
1983 -- below, but we need it temporarily attached to the tree for
1984 -- this analysis (hence the temporary Set_Parent call).
1986 Set_Parent (Arg, Parent (N));
1987 Analyze_And_Resolve (Arg);
1990 RJ_Unchecked_Convert_To (Ctyp, Arg));
1992 -- All other component sizes for non-modular case
1997 -- Component_Type!(Get_nn (Arr'address, Subscr))
1999 -- where Subscr is the computed linear subscript
2006 -- Acquire proper Get entity. We use the aligned or unaligned
2007 -- case as appropriate.
2009 if Known_Aligned_Enough (Obj, Csiz) then
2010 Get_nn := RTE (Get_Id (Csiz));
2012 Get_nn := RTE (GetU_Id (Csiz));
2015 -- Now generate the get reference
2017 Compute_Linear_Subscript (Atyp, N, Subscr);
2019 -- Below we make the assumption that Obj is at least byte
2020 -- aligned, since otherwise its address cannot be taken.
2021 -- The assumption holds since the only arrays that can be
2022 -- misaligned are small packed arrays which are implemented
2023 -- as a modular type, and that is not the case here.
2026 Unchecked_Convert_To (Ctyp,
2027 Make_Function_Call (Loc,
2028 Name => New_Occurrence_Of (Get_nn, Loc),
2029 Parameter_Associations => New_List (
2030 Make_Attribute_Reference (Loc,
2031 Attribute_Name => Name_Address,
2037 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2039 end Expand_Packed_Element_Reference;
2041 ----------------------
2042 -- Expand_Packed_Eq --
2043 ----------------------
2045 -- Handles expansion of "=" on packed array types
2047 procedure Expand_Packed_Eq (N : Node_Id) is
2048 Loc : constant Source_Ptr := Sloc (N);
2049 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2050 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2060 Convert_To_Actual_Subtype (L);
2061 Convert_To_Actual_Subtype (R);
2062 Ltyp := Underlying_Type (Etype (L));
2063 Rtyp := Underlying_Type (Etype (R));
2065 Convert_To_PAT_Type (L);
2066 Convert_To_PAT_Type (R);
2070 Make_Op_Multiply (Loc,
2072 Make_Attribute_Reference (Loc,
2073 Attribute_Name => Name_Length,
2074 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2076 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2079 Make_Op_Multiply (Loc,
2081 Make_Attribute_Reference (Loc,
2082 Attribute_Name => Name_Length,
2083 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2085 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2087 -- For the modular case, we transform the comparison to:
2089 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2091 -- where PAT is the packed array type. This works fine, since in the
2092 -- modular case we guarantee that the unused bits are always zeroes.
2093 -- We do have to compare the lengths because we could be comparing
2094 -- two different subtypes of the same base type.
2096 if Is_Modular_Integer_Type (PAT) then
2101 Left_Opnd => LLexpr,
2102 Right_Opnd => RLexpr),
2109 -- For the non-modular case, we call a runtime routine
2111 -- System.Bit_Ops.Bit_Eq
2112 -- (L'Address, L_Length, R'Address, R_Length)
2114 -- where PAT is the packed array type, and the lengths are the lengths
2115 -- in bits of the original packed arrays. This routine takes care of
2116 -- not comparing the unused bits in the last byte.
2120 Make_Function_Call (Loc,
2121 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2122 Parameter_Associations => New_List (
2123 Make_Byte_Aligned_Attribute_Reference (Loc,
2124 Attribute_Name => Name_Address,
2129 Make_Byte_Aligned_Attribute_Reference (Loc,
2130 Attribute_Name => Name_Address,
2136 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2137 end Expand_Packed_Eq;
2139 -----------------------
2140 -- Expand_Packed_Not --
2141 -----------------------
2143 -- Handles expansion of "not" on packed array types
2145 procedure Expand_Packed_Not (N : Node_Id) is
2146 Loc : constant Source_Ptr := Sloc (N);
2147 Typ : constant Entity_Id := Etype (N);
2148 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2155 Convert_To_Actual_Subtype (Opnd);
2156 Rtyp := Etype (Opnd);
2158 -- First an odd and silly test. We explicitly check for the case
2159 -- where the 'First of the component type is equal to the 'Last of
2160 -- this component type, and if this is the case, we make sure that
2161 -- constraint error is raised. The reason is that the NOT is bound
2162 -- to cause CE in this case, and we will not otherwise catch it.
2164 -- Believe it or not, this was reported as a bug. Note that nearly
2165 -- always, the test will evaluate statically to False, so the code
2166 -- will be statically removed, and no extra overhead caused.
2169 CT : constant Entity_Id := Component_Type (Rtyp);
2173 Make_Raise_Constraint_Error (Loc,
2177 Make_Attribute_Reference (Loc,
2178 Prefix => New_Occurrence_Of (CT, Loc),
2179 Attribute_Name => Name_First),
2182 Make_Attribute_Reference (Loc,
2183 Prefix => New_Occurrence_Of (CT, Loc),
2184 Attribute_Name => Name_Last)),
2185 Reason => CE_Range_Check_Failed));
2188 -- Now that that silliness is taken care of, get packed array type
2190 Convert_To_PAT_Type (Opnd);
2191 PAT := Etype (Opnd);
2193 -- For the case where the packed array type is a modular type,
2194 -- not A expands simply into:
2196 -- rtyp!(PAT!(A) xor mask)
2198 -- where PAT is the packed array type, and mask is a mask of all
2199 -- one bits of length equal to the size of this packed type and
2200 -- rtyp is the actual subtype of the operand
2202 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2203 Set_Print_In_Hex (Lit);
2205 if not Is_Array_Type (PAT) then
2207 Unchecked_Convert_To (Rtyp,
2210 Right_Opnd => Lit)));
2212 -- For the array case, we insert the actions
2216 -- System.Bitops.Bit_Not
2218 -- Typ'Length * Typ'Component_Size;
2221 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2222 -- argument is the length of the operand in bits. Then we replace
2223 -- the expression by a reference to Result.
2227 Result_Ent : constant Entity_Id :=
2228 Make_Defining_Identifier (Loc,
2229 Chars => New_Internal_Name ('T'));
2232 Insert_Actions (N, New_List (
2234 Make_Object_Declaration (Loc,
2235 Defining_Identifier => Result_Ent,
2236 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2238 Make_Procedure_Call_Statement (Loc,
2239 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2240 Parameter_Associations => New_List (
2242 Make_Byte_Aligned_Attribute_Reference (Loc,
2243 Attribute_Name => Name_Address,
2246 Make_Op_Multiply (Loc,
2248 Make_Attribute_Reference (Loc,
2251 (Etype (First_Index (Rtyp)), Loc),
2252 Attribute_Name => Name_Range_Length),
2254 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2256 Make_Byte_Aligned_Attribute_Reference (Loc,
2257 Attribute_Name => Name_Address,
2258 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2261 New_Occurrence_Of (Result_Ent, Loc));
2265 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2267 end Expand_Packed_Not;
2269 -------------------------------------
2270 -- Involves_Packed_Array_Reference --
2271 -------------------------------------
2273 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2275 if Nkind (N) = N_Indexed_Component
2276 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2280 elsif Nkind (N) = N_Selected_Component then
2281 return Involves_Packed_Array_Reference (Prefix (N));
2286 end Involves_Packed_Array_Reference;
2288 --------------------------
2289 -- Known_Aligned_Enough --
2290 --------------------------
2292 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2293 Typ : constant Entity_Id := Etype (Obj);
2295 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2296 -- If the component is in a record that contains previous packed
2297 -- components, consider it unaligned because the back-end might
2298 -- choose to pack the rest of the record. Lead to less efficient code,
2299 -- but safer vis-a-vis of back-end choices.
2301 --------------------------------
2302 -- In_Partially_Packed_Record --
2303 --------------------------------
2305 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2306 Rec_Type : constant Entity_Id := Scope (Comp);
2307 Prev_Comp : Entity_Id;
2310 Prev_Comp := First_Entity (Rec_Type);
2311 while Present (Prev_Comp) loop
2312 if Is_Packed (Etype (Prev_Comp)) then
2315 elsif Prev_Comp = Comp then
2319 Next_Entity (Prev_Comp);
2323 end In_Partially_Packed_Record;
2325 -- Start of processing for Known_Aligned_Enough
2328 -- Odd bit sizes don't need alignment anyway
2330 if Csiz mod 2 = 1 then
2333 -- If we have a specified alignment, see if it is sufficient, if not
2334 -- then we can't possibly be aligned enough in any case.
2336 elsif Known_Alignment (Etype (Obj)) then
2337 -- Alignment required is 4 if size is a multiple of 4, and
2338 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2340 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2345 -- OK, alignment should be sufficient, if object is aligned
2347 -- If object is strictly aligned, then it is definitely aligned
2349 if Strict_Alignment (Typ) then
2352 -- Case of subscripted array reference
2354 elsif Nkind (Obj) = N_Indexed_Component then
2356 -- If we have a pointer to an array, then this is definitely
2357 -- aligned, because pointers always point to aligned versions.
2359 if Is_Access_Type (Etype (Prefix (Obj))) then
2362 -- Otherwise, go look at the prefix
2365 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2368 -- Case of record field
2370 elsif Nkind (Obj) = N_Selected_Component then
2372 -- What is significant here is whether the record type is packed
2374 if Is_Record_Type (Etype (Prefix (Obj)))
2375 and then Is_Packed (Etype (Prefix (Obj)))
2379 -- Or the component has a component clause which might cause
2380 -- the component to become unaligned (we can't tell if the
2381 -- backend is doing alignment computations).
2383 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2386 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2389 -- In all other cases, go look at prefix
2392 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2395 elsif Nkind (Obj) = N_Type_Conversion then
2396 return Known_Aligned_Enough (Expression (Obj), Csiz);
2398 -- For a formal parameter, it is safer to assume that it is not
2399 -- aligned, because the formal may be unconstrained while the actual
2400 -- is constrained. In this situation, a small constrained packed
2401 -- array, represented in modular form, may be unaligned.
2403 elsif Is_Entity_Name (Obj) then
2404 return not Is_Formal (Entity (Obj));
2407 -- If none of the above, must be aligned
2410 end Known_Aligned_Enough;
2412 ---------------------
2413 -- Make_Shift_Left --
2414 ---------------------
2416 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2420 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2424 Make_Op_Shift_Left (Sloc (N),
2427 Set_Shift_Count_OK (Nod, True);
2430 end Make_Shift_Left;
2432 ----------------------
2433 -- Make_Shift_Right --
2434 ----------------------
2436 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2440 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2444 Make_Op_Shift_Right (Sloc (N),
2447 Set_Shift_Count_OK (Nod, True);
2450 end Make_Shift_Right;
2452 -----------------------------
2453 -- RJ_Unchecked_Convert_To --
2454 -----------------------------
2456 function RJ_Unchecked_Convert_To
2458 Expr : Node_Id) return Node_Id
2460 Source_Typ : constant Entity_Id := Etype (Expr);
2461 Target_Typ : constant Entity_Id := Typ;
2463 Src : Node_Id := Expr;
2469 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2470 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2472 -- First step, if the source type is not a discrete type, then we
2473 -- first convert to a modular type of the source length, since
2474 -- otherwise, on a big-endian machine, we get left-justification.
2475 -- We do it for little-endian machines as well, because there might
2476 -- be junk bits that are not cleared if the type is not numeric.
2478 if Source_Siz /= Target_Siz
2479 and then not Is_Discrete_Type (Source_Typ)
2481 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2484 -- In the big endian case, if the lengths of the two types differ,
2485 -- then we must worry about possible left justification in the
2486 -- conversion, and avoiding that is what this is all about.
2488 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2490 -- Next step. If the target is not a discrete type, then we first
2491 -- convert to a modular type of the target length, since
2492 -- otherwise, on a big-endian machine, we get left-justification.
2494 if not Is_Discrete_Type (Target_Typ) then
2495 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2499 -- And now we can do the final conversion to the target type
2501 return Unchecked_Convert_To (Target_Typ, Src);
2502 end RJ_Unchecked_Convert_To;
2504 ----------------------------------------------
2505 -- Setup_Enumeration_Packed_Array_Reference --
2506 ----------------------------------------------
2508 -- All we have to do here is to find the subscripts that correspond
2509 -- to the index positions that have non-standard enumeration types
2510 -- and insert a Pos attribute to get the proper subscript value.
2512 -- Finally the prefix must be uncheck converted to the corresponding
2513 -- packed array type.
2515 -- Note that the component type is unchanged, so we do not need to
2516 -- fiddle with the types (Gigi always automatically takes the packed
2517 -- array type if it is set, as it will be in this case).
2519 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2520 Pfx : constant Node_Id := Prefix (N);
2521 Typ : constant Entity_Id := Etype (N);
2522 Exprs : constant List_Id := Expressions (N);
2526 -- If the array is unconstrained, then we replace the array
2527 -- reference with its actual subtype. This actual subtype will
2528 -- have a packed array type with appropriate bounds.
2530 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2531 Convert_To_Actual_Subtype (Pfx);
2534 Expr := First (Exprs);
2535 while Present (Expr) loop
2537 Loc : constant Source_Ptr := Sloc (Expr);
2538 Expr_Typ : constant Entity_Id := Etype (Expr);
2541 if Is_Enumeration_Type (Expr_Typ)
2542 and then Has_Non_Standard_Rep (Expr_Typ)
2545 Make_Attribute_Reference (Loc,
2546 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2547 Attribute_Name => Name_Pos,
2548 Expressions => New_List (Relocate_Node (Expr))));
2549 Analyze_And_Resolve (Expr, Standard_Natural);
2557 Make_Indexed_Component (Sloc (N),
2559 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2560 Expressions => Exprs));
2562 Analyze_And_Resolve (N, Typ);
2564 end Setup_Enumeration_Packed_Array_Reference;
2566 -----------------------------------------
2567 -- Setup_Inline_Packed_Array_Reference --
2568 -----------------------------------------
2570 procedure Setup_Inline_Packed_Array_Reference
2573 Obj : in out Node_Id;
2575 Shift : out Node_Id)
2577 Loc : constant Source_Ptr := Sloc (N);
2584 Csiz := Component_Size (Atyp);
2586 Convert_To_PAT_Type (Obj);
2589 Cmask := 2 ** Csiz - 1;
2591 if Is_Array_Type (PAT) then
2592 Otyp := Component_Type (PAT);
2593 Osiz := Component_Size (PAT);
2598 -- In the case where the PAT is a modular type, we want the actual
2599 -- size in bits of the modular value we use. This is neither the
2600 -- Object_Size nor the Value_Size, either of which may have been
2601 -- reset to strange values, but rather the minimum size. Note that
2602 -- since this is a modular type with full range, the issue of
2603 -- biased representation does not arise.
2605 Osiz := UI_From_Int (Minimum_Size (Otyp));
2608 Compute_Linear_Subscript (Atyp, N, Shift);
2610 -- If the component size is not 1, then the subscript must be
2611 -- multiplied by the component size to get the shift count.
2615 Make_Op_Multiply (Loc,
2616 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2617 Right_Opnd => Shift);
2620 -- If we have the array case, then this shift count must be broken
2621 -- down into a byte subscript, and a shift within the byte.
2623 if Is_Array_Type (PAT) then
2626 New_Shift : Node_Id;
2629 -- We must analyze shift, since we will duplicate it
2631 Set_Parent (Shift, N);
2633 (Shift, Standard_Integer, Suppress => All_Checks);
2635 -- The shift count within the word is
2640 Left_Opnd => Duplicate_Subexpr (Shift),
2641 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2643 -- The subscript to be used on the PAT array is
2647 Make_Indexed_Component (Loc,
2649 Expressions => New_List (
2650 Make_Op_Divide (Loc,
2651 Left_Opnd => Duplicate_Subexpr (Shift),
2652 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2657 -- For the modular integer case, the object to be manipulated is
2658 -- the entire array, so Obj is unchanged. Note that we will reset
2659 -- its type to PAT before returning to the caller.
2665 -- The one remaining step is to modify the shift count for the
2666 -- big-endian case. Consider the following example in a byte:
2668 -- xxxxxxxx bits of byte
2669 -- vvvvvvvv bits of value
2670 -- 33221100 little-endian numbering
2671 -- 00112233 big-endian numbering
2673 -- Here we have the case of 2-bit fields
2675 -- For the little-endian case, we already have the proper shift
2676 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2678 -- For the big endian case, we have to adjust the shift count,
2679 -- computing it as (N - F) - shift, where N is the number of bits
2680 -- in an element of the array used to implement the packed array,
2681 -- F is the number of bits in a source level array element, and
2682 -- shift is the count so far computed.
2684 if Bytes_Big_Endian then
2686 Make_Op_Subtract (Loc,
2687 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2688 Right_Opnd => Shift);
2691 Set_Parent (Shift, N);
2692 Set_Parent (Obj, N);
2693 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2694 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2696 -- Make sure final type of object is the appropriate packed type
2698 Set_Etype (Obj, Otyp);
2700 end Setup_Inline_Packed_Array_Reference;