1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2006, 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 : Node_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 because
686 -- the expression will not be further analyzed, and Gigi considers the
687 -- two types equivalent in any case.
689 -- This is not strictly the case ??? If the reference is an actual in
690 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
691 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
692 -- array reference, reanalysis can produce spurious type errors when the
693 -- PAT type is replaced again with the original type of the array. Same
694 -- for the case of a dereference. The following is correct and minimal,
695 -- but the handling of more complex packed expressions in actuals is
696 -- confused. Probably 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)))
704 or else Nkind (Aexp) = N_Explicit_Dereference
708 end Convert_To_PAT_Type;
710 ------------------------------
711 -- Create_Packed_Array_Type --
712 ------------------------------
714 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
715 Loc : constant Source_Ptr := Sloc (Typ);
716 Ctyp : constant Entity_Id := Component_Type (Typ);
717 Csize : constant Uint := Component_Size (Typ);
732 procedure Install_PAT;
733 -- This procedure is called with Decl set to the declaration for the
734 -- packed array type. It creates the type and installs it as required.
736 procedure Set_PB_Type;
737 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
738 -- requirements (see documentation in the spec of this package).
744 procedure Install_PAT is
745 Pushed_Scope : Boolean := False;
748 -- We do not want to put the declaration we have created in the tree
749 -- since it is often hard, and sometimes impossible to find a proper
750 -- place for it (the impossible case arises for a packed array type
751 -- with bounds depending on the discriminant, a declaration cannot
752 -- be put inside the record, and the reference to the discriminant
753 -- cannot be outside the record).
755 -- The solution is to analyze the declaration while temporarily
756 -- attached to the tree at an appropriate point, and then we install
757 -- the resulting type as an Itype in the packed array type field of
758 -- the original type, so that no explicit declaration is required.
760 -- Note: the packed type is created in the scope of its parent
761 -- type. There are at least some cases where the current scope
762 -- is deeper, and so when this is the case, we temporarily reset
763 -- the scope for the definition. This is clearly safe, since the
764 -- first use of the packed array type will be the implicit
765 -- reference from the corresponding unpacked type when it is
768 if Is_Itype (Typ) then
769 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
771 Set_Parent (Decl, Declaration_Node (Typ));
774 if Scope (Typ) /= Current_Scope then
775 New_Scope (Scope (Typ));
776 Pushed_Scope := True;
779 Set_Is_Itype (PAT, True);
780 Set_Packed_Array_Type (Typ, PAT);
781 Analyze (Decl, Suppress => All_Checks);
787 -- Set Esize and RM_Size to the actual size of the packed object
788 -- Do not reset RM_Size if already set, as happens in the case
789 -- of a modular type.
791 Set_Esize (PAT, PASize);
793 if Unknown_RM_Size (PAT) then
794 Set_RM_Size (PAT, PASize);
797 -- Set remaining fields of packed array type
799 Init_Alignment (PAT);
800 Set_Parent (PAT, Empty);
801 Set_Associated_Node_For_Itype (PAT, Typ);
802 Set_Is_Packed_Array_Type (PAT, True);
803 Set_Original_Array_Type (PAT, Typ);
805 -- We definitely do not want to delay freezing for packed array
806 -- types. This is of particular importance for the itypes that
807 -- are generated for record components depending on discriminants
808 -- where there is no place to put the freeze node.
810 Set_Has_Delayed_Freeze (PAT, False);
811 Set_Has_Delayed_Freeze (Etype (PAT), False);
813 -- If we did allocate a freeze node, then clear out the reference
814 -- since it is obsolete (should we delete the freeze node???)
816 Set_Freeze_Node (PAT, Empty);
817 Set_Freeze_Node (Etype (PAT), Empty);
824 procedure Set_PB_Type is
826 -- If the user has specified an explicit alignment for the
827 -- type or component, take it into account.
829 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
830 or else Alignment (Typ) = 1
831 or else Component_Alignment (Typ) = Calign_Storage_Unit
833 PB_Type := RTE (RE_Packed_Bytes1);
835 elsif Csize mod 4 /= 0
836 or else Alignment (Typ) = 2
838 PB_Type := RTE (RE_Packed_Bytes2);
841 PB_Type := RTE (RE_Packed_Bytes4);
845 -- Start of processing for Create_Packed_Array_Type
848 -- If we already have a packed array type, nothing to do
850 if Present (Packed_Array_Type (Typ)) then
854 -- If our immediate ancestor subtype is constrained, and it already
855 -- has a packed array type, then just share the same type, since the
856 -- bounds must be the same. If the ancestor is not an array type but
857 -- a private type, as can happen with multiple instantiations, create
858 -- a new packed type, to avoid privacy issues.
860 if Ekind (Typ) = E_Array_Subtype then
861 Ancest := Ancestor_Subtype (Typ);
864 and then Is_Array_Type (Ancest)
865 and then Is_Constrained (Ancest)
866 and then Present (Packed_Array_Type (Ancest))
868 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
873 -- We preset the result type size from the size of the original array
874 -- type, since this size clearly belongs to the packed array type. The
875 -- size of the conceptual unpacked type is always set to unknown.
877 PASize := Esize (Typ);
879 -- Case of an array where at least one index is of an enumeration
880 -- type with a non-standard representation, but the component size
881 -- is not appropriate for bit packing. This is the case where we
882 -- have Is_Packed set (we would never be in this unit otherwise),
883 -- but Is_Bit_Packed_Array is false.
885 -- Note that if the component size is appropriate for bit packing,
886 -- then the circuit for the computation of the subscript properly
887 -- deals with the non-standard enumeration type case by taking the
890 if not Is_Bit_Packed_Array (Typ) then
892 -- Here we build a declaration:
894 -- type tttP is array (index1, index2, ...) of component_type
896 -- where index1, index2, are the index types. These are the same
897 -- as the index types of the original array, except for the non-
898 -- standard representation enumeration type case, where we have
901 -- For the unconstrained array case, we use
905 -- For the constrained case, we use
907 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
908 -- Enum_Type'Pos (Enum_Type'Last);
911 Make_Defining_Identifier (Loc,
912 Chars => New_External_Name (Chars (Typ), 'P'));
914 Set_Packed_Array_Type (Typ, PAT);
917 Indexes : constant List_Id := New_List;
919 Indx_Typ : Entity_Id;
924 Indx := First_Index (Typ);
926 while Present (Indx) loop
927 Indx_Typ := Etype (Indx);
929 Enum_Case := Is_Enumeration_Type (Indx_Typ)
930 and then Has_Non_Standard_Rep (Indx_Typ);
932 -- Unconstrained case
934 if not Is_Constrained (Typ) then
936 Indx_Typ := Standard_Natural;
939 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
944 if not Enum_Case then
945 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
949 Make_Subtype_Indication (Loc,
951 New_Occurrence_Of (Standard_Natural, Loc),
953 Make_Range_Constraint (Loc,
957 Make_Attribute_Reference (Loc,
959 New_Occurrence_Of (Indx_Typ, Loc),
960 Attribute_Name => Name_Pos,
961 Expressions => New_List (
962 Make_Attribute_Reference (Loc,
964 New_Occurrence_Of (Indx_Typ, Loc),
965 Attribute_Name => Name_First))),
968 Make_Attribute_Reference (Loc,
970 New_Occurrence_Of (Indx_Typ, Loc),
971 Attribute_Name => Name_Pos,
972 Expressions => New_List (
973 Make_Attribute_Reference (Loc,
975 New_Occurrence_Of (Indx_Typ, Loc),
976 Attribute_Name => Name_Last)))))));
984 if not Is_Constrained (Typ) then
986 Make_Unconstrained_Array_Definition (Loc,
987 Subtype_Marks => Indexes,
988 Component_Definition =>
989 Make_Component_Definition (Loc,
990 Aliased_Present => False,
991 Subtype_Indication =>
992 New_Occurrence_Of (Ctyp, Loc)));
996 Make_Constrained_Array_Definition (Loc,
997 Discrete_Subtype_Definitions => Indexes,
998 Component_Definition =>
999 Make_Component_Definition (Loc,
1000 Aliased_Present => False,
1001 Subtype_Indication =>
1002 New_Occurrence_Of (Ctyp, Loc)));
1006 Make_Full_Type_Declaration (Loc,
1007 Defining_Identifier => PAT,
1008 Type_Definition => Typedef);
1011 -- Set type as packed array type and install it
1013 Set_Is_Packed_Array_Type (PAT);
1017 -- Case of bit-packing required for unconstrained array. We create
1018 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1020 elsif not Is_Constrained (Typ) then
1022 Make_Defining_Identifier (Loc,
1023 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1025 Set_Packed_Array_Type (Typ, PAT);
1029 Make_Subtype_Declaration (Loc,
1030 Defining_Identifier => PAT,
1031 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1035 -- Remaining code is for the case of bit-packing for constrained array
1037 -- The name of the packed array subtype is
1041 -- where sss is the component size in bits and ttt is the name of
1042 -- the parent packed type.
1046 Make_Defining_Identifier (Loc,
1047 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1049 Set_Packed_Array_Type (Typ, PAT);
1051 -- Build an expression for the length of the array in bits.
1052 -- This is the product of the length of each of the dimensions
1058 Len_Expr := Empty; -- suppress junk warning
1062 Make_Attribute_Reference (Loc,
1063 Attribute_Name => Name_Length,
1064 Prefix => New_Occurrence_Of (Typ, Loc),
1065 Expressions => New_List (
1066 Make_Integer_Literal (Loc, J)));
1069 Len_Expr := Len_Dim;
1073 Make_Op_Multiply (Loc,
1074 Left_Opnd => Len_Expr,
1075 Right_Opnd => Len_Dim);
1079 exit when J > Number_Dimensions (Typ);
1083 -- Temporarily attach the length expression to the tree and analyze
1084 -- and resolve it, so that we can test its value. We assume that the
1085 -- total length fits in type Integer. This expression may involve
1086 -- discriminants, so we treat it as a default/per-object expression.
1088 Set_Parent (Len_Expr, Typ);
1089 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1091 -- Use a modular type if possible. We can do this if we have
1092 -- static bounds, and the length is small enough, and the length
1093 -- is not zero. We exclude the zero length case because the size
1094 -- of things is always at least one, and the zero length object
1095 -- would have an anomalous size.
1097 if Compile_Time_Known_Value (Len_Expr) then
1098 Len_Bits := Expr_Value (Len_Expr) * Csize;
1100 -- Check for size known to be too large
1103 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1105 if System_Storage_Unit = 8 then
1107 ("packed array size cannot exceed " &
1108 "Integer''Last bytes", Typ);
1111 ("packed array size cannot exceed " &
1112 "Integer''Last storage units", Typ);
1115 -- Reset length to arbitrary not too high value to continue
1117 Len_Expr := Make_Integer_Literal (Loc, 65535);
1118 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1121 -- We normally consider small enough to mean no larger than the
1122 -- value of System_Max_Binary_Modulus_Power, checking that in the
1123 -- case of values longer than word size, we have long shifts.
1127 (Len_Bits <= System_Word_Size
1128 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1129 and then Support_Long_Shifts_On_Target))
1131 -- Also test for alignment given. If an alignment is given which
1132 -- is smaller than the natural modular alignment, force the array
1133 -- of bytes representation to accommodate the alignment.
1136 (No (Alignment_Clause (Typ))
1138 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1139 / System_Storage_Unit))
1141 -- We can use the modular type, it has the form:
1143 -- subtype tttPn is btyp
1144 -- range 0 .. 2 ** ((Typ'Length (1)
1145 -- * ... * Typ'Length (n)) * Csize) - 1;
1147 -- The bounds are statically known, and btyp is one
1148 -- of the unsigned types, depending on the length. If the
1149 -- type is its first subtype, i.e. it is a user-defined
1150 -- type, no object of the type will be larger, and it is
1151 -- worthwhile to use a small unsigned type.
1153 if Len_Bits <= Standard_Short_Integer_Size
1154 and then First_Subtype (Typ) = Typ
1156 Btyp := RTE (RE_Short_Unsigned);
1158 elsif Len_Bits <= Standard_Integer_Size then
1159 Btyp := RTE (RE_Unsigned);
1161 elsif Len_Bits <= Standard_Long_Integer_Size then
1162 Btyp := RTE (RE_Long_Unsigned);
1165 Btyp := RTE (RE_Long_Long_Unsigned);
1168 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1169 Set_Print_In_Hex (Lit);
1172 Make_Subtype_Declaration (Loc,
1173 Defining_Identifier => PAT,
1174 Subtype_Indication =>
1175 Make_Subtype_Indication (Loc,
1176 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1179 Make_Range_Constraint (Loc,
1183 Make_Integer_Literal (Loc, 0),
1184 High_Bound => Lit))));
1186 if PASize = Uint_0 then
1195 -- Could not use a modular type, for all other cases, we build
1196 -- a packed array subtype:
1199 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1201 -- Bits is the length of the array in bits
1208 Make_Op_Multiply (Loc,
1210 Make_Integer_Literal (Loc, Csize),
1211 Right_Opnd => Len_Expr),
1214 Make_Integer_Literal (Loc, 7));
1216 Set_Paren_Count (Bits_U1, 1);
1219 Make_Op_Subtract (Loc,
1221 Make_Op_Divide (Loc,
1222 Left_Opnd => Bits_U1,
1223 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1224 Right_Opnd => Make_Integer_Literal (Loc, 1));
1227 Make_Subtype_Declaration (Loc,
1228 Defining_Identifier => PAT,
1229 Subtype_Indication =>
1230 Make_Subtype_Indication (Loc,
1231 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1233 Make_Index_Or_Discriminant_Constraint (Loc,
1234 Constraints => New_List (
1237 Make_Integer_Literal (Loc, 0),
1239 Convert_To (Standard_Integer, PAT_High))))));
1243 -- Currently the code in this unit requires that packed arrays
1244 -- represented by non-modular arrays of bytes be on a byte
1245 -- boundary for bit sizes handled by System.Pack_nn units.
1246 -- That's because these units assume the array being accessed
1247 -- starts on a byte boundary.
1249 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1250 Set_Must_Be_On_Byte_Boundary (Typ);
1253 end Create_Packed_Array_Type;
1255 -----------------------------------
1256 -- Expand_Bit_Packed_Element_Set --
1257 -----------------------------------
1259 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1260 Loc : constant Source_Ptr := Sloc (N);
1261 Lhs : constant Node_Id := Name (N);
1263 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1264 -- Used to preserve assignment OK status when assignment is rewritten
1266 Rhs : Node_Id := Expression (N);
1267 -- Initially Rhs is the right hand side value, it will be replaced
1268 -- later by an appropriate unchecked conversion for the assignment.
1278 -- The expression for the shift value that is required
1280 Shift_Used : Boolean := False;
1281 -- Set True if Shift has been used in the generated code at least
1282 -- once, so that it must be duplicated if used again
1287 Rhs_Val_Known : Boolean;
1289 -- If the value of the right hand side as an integer constant is
1290 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1291 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1292 -- the Rhs_Val is undefined.
1294 function Get_Shift return Node_Id;
1295 -- Function used to get the value of Shift, making sure that it
1296 -- gets duplicated if the function is called more than once.
1302 function Get_Shift return Node_Id is
1304 -- If we used the shift value already, then duplicate it. We
1305 -- set a temporary parent in case actions have to be inserted.
1308 Set_Parent (Shift, N);
1309 return Duplicate_Subexpr_No_Checks (Shift);
1311 -- If first time, use Shift unchanged, and set flag for first use
1319 -- Start of processing for Expand_Bit_Packed_Element_Set
1322 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1324 Obj := Relocate_Node (Prefix (Lhs));
1325 Convert_To_Actual_Subtype (Obj);
1326 Atyp := Etype (Obj);
1327 PAT := Packed_Array_Type (Atyp);
1328 Ctyp := Component_Type (Atyp);
1329 Csiz := UI_To_Int (Component_Size (Atyp));
1331 -- We convert the right hand side to the proper subtype to ensure
1332 -- that an appropriate range check is made (since the normal range
1333 -- check from assignment will be lost in the transformations). This
1334 -- conversion is analyzed immediately so that subsequent processing
1335 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1337 -- If the right-hand side is a string literal, create a temporary for
1338 -- it, constant-folding is not ready to wrap the bit representation
1339 -- of a string literal.
1341 if Nkind (Rhs) = N_String_Literal then
1346 Make_Object_Declaration (Loc,
1347 Defining_Identifier =>
1348 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1349 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1350 Expression => New_Copy_Tree (Rhs));
1352 Insert_Actions (N, New_List (Decl));
1353 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1357 Rhs := Convert_To (Ctyp, Rhs);
1358 Set_Parent (Rhs, N);
1359 Analyze_And_Resolve (Rhs, Ctyp);
1361 -- Case of component size 1,2,4 or any component size for the modular
1362 -- case. These are the cases for which we can inline the code.
1364 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1365 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1367 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1369 -- The statement to be generated is:
1371 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1373 -- where mask1 is obtained by shifting Cmask left Shift bits
1374 -- and then complementing the result.
1376 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1378 -- the "or ..." is omitted if rhs is constant and all 0 bits
1380 -- rhs is converted to the appropriate type
1382 -- The result is converted back to the array type, since
1383 -- otherwise we lose knowledge of the packed nature.
1385 -- Determine if right side is all 0 bits or all 1 bits
1387 if Compile_Time_Known_Value (Rhs) then
1388 Rhs_Val := Expr_Rep_Value (Rhs);
1389 Rhs_Val_Known := True;
1391 -- The following test catches the case of an unchecked conversion
1392 -- of an integer literal. This results from optimizing aggregates
1395 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1396 and then Compile_Time_Known_Value (Expression (Rhs))
1398 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1399 Rhs_Val_Known := True;
1403 Rhs_Val_Known := False;
1406 -- Some special checks for the case where the right hand value
1407 -- is known at compile time. Basically we have to take care of
1408 -- the implicit conversion to the subtype of the component object.
1410 if Rhs_Val_Known then
1412 -- If we have a biased component type then we must manually do
1413 -- the biasing, since we are taking responsibility in this case
1414 -- for constructing the exact bit pattern to be used.
1416 if Has_Biased_Representation (Ctyp) then
1417 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1420 -- For a negative value, we manually convert the twos complement
1421 -- value to a corresponding unsigned value, so that the proper
1422 -- field width is maintained. If we did not do this, we would
1423 -- get too many leading sign bits later on.
1426 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1430 New_Lhs := Duplicate_Subexpr (Obj, True);
1431 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1433 -- First we deal with the "and"
1435 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1441 if Compile_Time_Known_Value (Shift) then
1443 Make_Integer_Literal (Loc,
1444 Modulus (Etype (Obj)) - 1 -
1445 (Cmask * (2 ** Expr_Value (Get_Shift))));
1446 Set_Print_In_Hex (Mask1);
1449 Lit := Make_Integer_Literal (Loc, Cmask);
1450 Set_Print_In_Hex (Lit);
1453 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1458 Left_Opnd => New_Rhs,
1459 Right_Opnd => Mask1);
1463 -- Then deal with the "or"
1465 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1469 procedure Fixup_Rhs;
1470 -- Adjust Rhs by bias if biased representation for components
1471 -- or remove extraneous high order sign bits if signed.
1473 procedure Fixup_Rhs is
1474 Etyp : constant Entity_Id := Etype (Rhs);
1477 -- For biased case, do the required biasing by simply
1478 -- converting to the biased subtype (the conversion
1479 -- will generate the required bias).
1481 if Has_Biased_Representation (Ctyp) then
1482 Rhs := Convert_To (Ctyp, Rhs);
1484 -- For a signed integer type that is not biased, generate
1485 -- a conversion to unsigned to strip high order sign bits.
1487 elsif Is_Signed_Integer_Type (Ctyp) then
1488 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1491 -- Set Etype, since it can be referenced before the
1492 -- node is completely analyzed.
1494 Set_Etype (Rhs, Etyp);
1496 -- We now need to do an unchecked conversion of the
1497 -- result to the target type, but it is important that
1498 -- this conversion be a right justified conversion and
1499 -- not a left justified conversion.
1501 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1507 and then Compile_Time_Known_Value (Get_Shift)
1510 Make_Integer_Literal (Loc,
1511 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1512 Set_Print_In_Hex (Or_Rhs);
1515 -- We have to convert the right hand side to Etype (Obj).
1516 -- A special case case arises if what we have now is a Val
1517 -- attribute reference whose expression type is Etype (Obj).
1518 -- This happens for assignments of fields from the same
1519 -- array. In this case we get the required right hand side
1520 -- by simply removing the inner attribute reference.
1522 if Nkind (Rhs) = N_Attribute_Reference
1523 and then Attribute_Name (Rhs) = Name_Val
1524 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1526 Rhs := Relocate_Node (First (Expressions (Rhs)));
1529 -- If the value of the right hand side is a known integer
1530 -- value, then just replace it by an untyped constant,
1531 -- which will be properly retyped when we analyze and
1532 -- resolve the expression.
1534 elsif Rhs_Val_Known then
1536 -- Note that Rhs_Val has already been normalized to
1537 -- be an unsigned value with the proper number of bits.
1540 Make_Integer_Literal (Loc, Rhs_Val);
1542 -- Otherwise we need an unchecked conversion
1548 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1551 if Nkind (New_Rhs) = N_Op_And then
1552 Set_Paren_Count (New_Rhs, 1);
1557 Left_Opnd => New_Rhs,
1558 Right_Opnd => Or_Rhs);
1562 -- Now do the rewrite
1565 Make_Assignment_Statement (Loc,
1568 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1569 Set_Assignment_OK (Name (N), Ass_OK);
1571 -- All other component sizes for non-modular case
1576 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1578 -- where Subscr is the computed linear subscript
1581 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1587 if No (Bits_nn) then
1589 -- Error, most likely High_Integrity_Mode restriction
1594 -- Acquire proper Set entity. We use the aligned or unaligned
1595 -- case as appropriate.
1597 if Known_Aligned_Enough (Obj, Csiz) then
1598 Set_nn := RTE (Set_Id (Csiz));
1600 Set_nn := RTE (SetU_Id (Csiz));
1603 -- Now generate the set reference
1605 Obj := Relocate_Node (Prefix (Lhs));
1606 Convert_To_Actual_Subtype (Obj);
1607 Atyp := Etype (Obj);
1608 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1610 -- Below we must make the assumption that Obj is
1611 -- at least byte aligned, since otherwise its address
1612 -- cannot be taken. The assumption holds since the
1613 -- only arrays that can be misaligned are small packed
1614 -- arrays which are implemented as a modular type, and
1615 -- that is not the case here.
1618 Make_Procedure_Call_Statement (Loc,
1619 Name => New_Occurrence_Of (Set_nn, Loc),
1620 Parameter_Associations => New_List (
1621 Make_Attribute_Reference (Loc,
1622 Attribute_Name => Name_Address,
1625 Unchecked_Convert_To (Bits_nn,
1626 Convert_To (Ctyp, Rhs)))));
1631 Analyze (N, Suppress => All_Checks);
1632 end Expand_Bit_Packed_Element_Set;
1634 -------------------------------------
1635 -- Expand_Packed_Address_Reference --
1636 -------------------------------------
1638 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1639 Loc : constant Source_Ptr := Sloc (N);
1651 -- We build up an expression serially that has the form
1653 -- outer_object'Address
1654 -- + (linear-subscript * component_size for each array reference
1655 -- + field'Bit_Position for each record field
1657 -- + ...) / Storage_Unit;
1659 -- Some additional conversions are required to deal with the addition
1660 -- operation, which is not normally visible to generated code.
1663 Ploc := Sloc (Pref);
1665 if Nkind (Pref) = N_Indexed_Component then
1666 Convert_To_Actual_Subtype (Prefix (Pref));
1667 Atyp := Etype (Prefix (Pref));
1668 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1671 Make_Op_Multiply (Ploc,
1672 Left_Opnd => Subscr,
1674 Make_Attribute_Reference (Ploc,
1675 Prefix => New_Occurrence_Of (Atyp, Ploc),
1676 Attribute_Name => Name_Component_Size));
1678 elsif Nkind (Pref) = N_Selected_Component then
1680 Make_Attribute_Reference (Ploc,
1681 Prefix => Selector_Name (Pref),
1682 Attribute_Name => Name_Bit_Position);
1688 Term := Convert_To (RTE (RE_Integer_Address), Term);
1697 Right_Opnd => Term);
1700 Pref := Prefix (Pref);
1704 Unchecked_Convert_To (RTE (RE_Address),
1707 Unchecked_Convert_To (RTE (RE_Integer_Address),
1708 Make_Attribute_Reference (Loc,
1710 Attribute_Name => Name_Address)),
1713 Make_Op_Divide (Loc,
1716 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1718 Analyze_And_Resolve (N, RTE (RE_Address));
1719 end Expand_Packed_Address_Reference;
1721 ------------------------------------
1722 -- Expand_Packed_Boolean_Operator --
1723 ------------------------------------
1725 -- This routine expands "a op b" for the packed cases
1727 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1728 Loc : constant Source_Ptr := Sloc (N);
1729 Typ : constant Entity_Id := Etype (N);
1730 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1731 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1738 Convert_To_Actual_Subtype (L);
1739 Convert_To_Actual_Subtype (R);
1741 Ensure_Defined (Etype (L), N);
1742 Ensure_Defined (Etype (R), N);
1744 Apply_Length_Check (R, Etype (L));
1749 -- First an odd and silly test. We explicitly check for the XOR
1750 -- case where the component type is True .. True, since this will
1751 -- raise constraint error. A special check is required since CE
1752 -- will not be required other wise (cf Expand_Packed_Not).
1754 -- No such check is required for AND and OR, since for both these
1755 -- cases False op False = False, and True op True = True.
1757 if Nkind (N) = N_Op_Xor then
1759 CT : constant Entity_Id := Component_Type (Rtyp);
1760 BT : constant Entity_Id := Base_Type (CT);
1764 Make_Raise_Constraint_Error (Loc,
1770 Make_Attribute_Reference (Loc,
1771 Prefix => New_Occurrence_Of (CT, Loc),
1772 Attribute_Name => Name_First),
1776 New_Occurrence_Of (Standard_True, Loc))),
1781 Make_Attribute_Reference (Loc,
1782 Prefix => New_Occurrence_Of (CT, Loc),
1783 Attribute_Name => Name_Last),
1787 New_Occurrence_Of (Standard_True, Loc)))),
1788 Reason => CE_Range_Check_Failed));
1792 -- Now that that silliness is taken care of, get packed array type
1794 Convert_To_PAT_Type (L);
1795 Convert_To_PAT_Type (R);
1799 -- For the modular case, we expand a op b into
1801 -- rtyp!(pat!(a) op pat!(b))
1803 -- where rtyp is the Etype of the left operand. Note that we do not
1804 -- convert to the base type, since this would be unconstrained, and
1805 -- hence not have a corresponding packed array type set.
1807 -- Note that both operands must be modular for this code to be used
1809 if Is_Modular_Integer_Type (PAT)
1811 Is_Modular_Integer_Type (Etype (R))
1817 if Nkind (N) = N_Op_And then
1818 P := Make_Op_And (Loc, L, R);
1820 elsif Nkind (N) = N_Op_Or then
1821 P := Make_Op_Or (Loc, L, R);
1823 else -- Nkind (N) = N_Op_Xor
1824 P := Make_Op_Xor (Loc, L, R);
1827 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1830 -- For the array case, we insert the actions
1834 -- System.Bitops.Bit_And/Or/Xor
1836 -- Ltype'Length * Ltype'Component_Size;
1838 -- Rtype'Length * Rtype'Component_Size
1841 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1842 -- the second argument and fourth arguments are the lengths of the
1843 -- operands in bits. Then we replace the expression by a reference
1846 -- Note that if we are mixing a modular and array operand, everything
1847 -- works fine, since we ensure that the modular representation has the
1848 -- same physical layout as the array representation (that's what the
1849 -- left justified modular stuff in the big-endian case is about).
1853 Result_Ent : constant Entity_Id :=
1854 Make_Defining_Identifier (Loc,
1855 Chars => New_Internal_Name ('T'));
1860 if Nkind (N) = N_Op_And then
1863 elsif Nkind (N) = N_Op_Or then
1866 else -- Nkind (N) = N_Op_Xor
1870 Insert_Actions (N, New_List (
1872 Make_Object_Declaration (Loc,
1873 Defining_Identifier => Result_Ent,
1874 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1876 Make_Procedure_Call_Statement (Loc,
1877 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1878 Parameter_Associations => New_List (
1880 Make_Byte_Aligned_Attribute_Reference (Loc,
1881 Attribute_Name => Name_Address,
1884 Make_Op_Multiply (Loc,
1886 Make_Attribute_Reference (Loc,
1889 (Etype (First_Index (Ltyp)), Loc),
1890 Attribute_Name => Name_Range_Length),
1892 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1894 Make_Byte_Aligned_Attribute_Reference (Loc,
1895 Attribute_Name => Name_Address,
1898 Make_Op_Multiply (Loc,
1900 Make_Attribute_Reference (Loc,
1903 (Etype (First_Index (Rtyp)), Loc),
1904 Attribute_Name => Name_Range_Length),
1906 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1908 Make_Byte_Aligned_Attribute_Reference (Loc,
1909 Attribute_Name => Name_Address,
1910 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1913 New_Occurrence_Of (Result_Ent, Loc));
1917 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1918 end Expand_Packed_Boolean_Operator;
1920 -------------------------------------
1921 -- Expand_Packed_Element_Reference --
1922 -------------------------------------
1924 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1925 Loc : constant Source_Ptr := Sloc (N);
1937 -- If not bit packed, we have the enumeration case, which is easily
1938 -- dealt with (just adjust the subscripts of the indexed component)
1940 -- Note: this leaves the result as an indexed component, which is
1941 -- still a variable, so can be used in the assignment case, as is
1942 -- required in the enumeration case.
1944 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1945 Setup_Enumeration_Packed_Array_Reference (N);
1949 -- Remaining processing is for the bit-packed case
1951 Obj := Relocate_Node (Prefix (N));
1952 Convert_To_Actual_Subtype (Obj);
1953 Atyp := Etype (Obj);
1954 PAT := Packed_Array_Type (Atyp);
1955 Ctyp := Component_Type (Atyp);
1956 Csiz := UI_To_Int (Component_Size (Atyp));
1958 -- Case of component size 1,2,4 or any component size for the modular
1959 -- case. These are the cases for which we can inline the code.
1961 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1962 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1964 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1965 Lit := Make_Integer_Literal (Loc, Cmask);
1966 Set_Print_In_Hex (Lit);
1968 -- We generate a shift right to position the field, followed by a
1969 -- masking operation to extract the bit field, and we finally do an
1970 -- unchecked conversion to convert the result to the required target.
1972 -- Note that the unchecked conversion automatically deals with the
1973 -- bias if we are dealing with a biased representation. What will
1974 -- happen is that we temporarily generate the biased representation,
1975 -- but almost immediately that will be converted to the original
1976 -- unbiased component type, and the bias will disappear.
1980 Left_Opnd => Make_Shift_Right (Obj, Shift),
1983 -- We neded to analyze this before we do the unchecked convert
1984 -- below, but we need it temporarily attached to the tree for
1985 -- this analysis (hence the temporary Set_Parent call).
1987 Set_Parent (Arg, Parent (N));
1988 Analyze_And_Resolve (Arg);
1991 RJ_Unchecked_Convert_To (Ctyp, Arg));
1993 -- All other component sizes for non-modular case
1998 -- Component_Type!(Get_nn (Arr'address, Subscr))
2000 -- where Subscr is the computed linear subscript
2007 -- Acquire proper Get entity. We use the aligned or unaligned
2008 -- case as appropriate.
2010 if Known_Aligned_Enough (Obj, Csiz) then
2011 Get_nn := RTE (Get_Id (Csiz));
2013 Get_nn := RTE (GetU_Id (Csiz));
2016 -- Now generate the get reference
2018 Compute_Linear_Subscript (Atyp, N, Subscr);
2020 -- Below we make the assumption that Obj is at least byte
2021 -- aligned, since otherwise its address cannot be taken.
2022 -- The assumption holds since the only arrays that can be
2023 -- misaligned are small packed arrays which are implemented
2024 -- as a modular type, and that is not the case here.
2027 Unchecked_Convert_To (Ctyp,
2028 Make_Function_Call (Loc,
2029 Name => New_Occurrence_Of (Get_nn, Loc),
2030 Parameter_Associations => New_List (
2031 Make_Attribute_Reference (Loc,
2032 Attribute_Name => Name_Address,
2038 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2040 end Expand_Packed_Element_Reference;
2042 ----------------------
2043 -- Expand_Packed_Eq --
2044 ----------------------
2046 -- Handles expansion of "=" on packed array types
2048 procedure Expand_Packed_Eq (N : Node_Id) is
2049 Loc : constant Source_Ptr := Sloc (N);
2050 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2051 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2061 Convert_To_Actual_Subtype (L);
2062 Convert_To_Actual_Subtype (R);
2063 Ltyp := Underlying_Type (Etype (L));
2064 Rtyp := Underlying_Type (Etype (R));
2066 Convert_To_PAT_Type (L);
2067 Convert_To_PAT_Type (R);
2071 Make_Op_Multiply (Loc,
2073 Make_Attribute_Reference (Loc,
2074 Attribute_Name => Name_Length,
2075 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2077 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2080 Make_Op_Multiply (Loc,
2082 Make_Attribute_Reference (Loc,
2083 Attribute_Name => Name_Length,
2084 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2086 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2088 -- For the modular case, we transform the comparison to:
2090 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2092 -- where PAT is the packed array type. This works fine, since in the
2093 -- modular case we guarantee that the unused bits are always zeroes.
2094 -- We do have to compare the lengths because we could be comparing
2095 -- two different subtypes of the same base type.
2097 if Is_Modular_Integer_Type (PAT) then
2102 Left_Opnd => LLexpr,
2103 Right_Opnd => RLexpr),
2110 -- For the non-modular case, we call a runtime routine
2112 -- System.Bit_Ops.Bit_Eq
2113 -- (L'Address, L_Length, R'Address, R_Length)
2115 -- where PAT is the packed array type, and the lengths are the lengths
2116 -- in bits of the original packed arrays. This routine takes care of
2117 -- not comparing the unused bits in the last byte.
2121 Make_Function_Call (Loc,
2122 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2123 Parameter_Associations => New_List (
2124 Make_Byte_Aligned_Attribute_Reference (Loc,
2125 Attribute_Name => Name_Address,
2130 Make_Byte_Aligned_Attribute_Reference (Loc,
2131 Attribute_Name => Name_Address,
2137 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2138 end Expand_Packed_Eq;
2140 -----------------------
2141 -- Expand_Packed_Not --
2142 -----------------------
2144 -- Handles expansion of "not" on packed array types
2146 procedure Expand_Packed_Not (N : Node_Id) is
2147 Loc : constant Source_Ptr := Sloc (N);
2148 Typ : constant Entity_Id := Etype (N);
2149 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2156 Convert_To_Actual_Subtype (Opnd);
2157 Rtyp := Etype (Opnd);
2159 -- First an odd and silly test. We explicitly check for the case
2160 -- where the 'First of the component type is equal to the 'Last of
2161 -- this component type, and if this is the case, we make sure that
2162 -- constraint error is raised. The reason is that the NOT is bound
2163 -- to cause CE in this case, and we will not otherwise catch it.
2165 -- Believe it or not, this was reported as a bug. Note that nearly
2166 -- always, the test will evaluate statically to False, so the code
2167 -- will be statically removed, and no extra overhead caused.
2170 CT : constant Entity_Id := Component_Type (Rtyp);
2174 Make_Raise_Constraint_Error (Loc,
2178 Make_Attribute_Reference (Loc,
2179 Prefix => New_Occurrence_Of (CT, Loc),
2180 Attribute_Name => Name_First),
2183 Make_Attribute_Reference (Loc,
2184 Prefix => New_Occurrence_Of (CT, Loc),
2185 Attribute_Name => Name_Last)),
2186 Reason => CE_Range_Check_Failed));
2189 -- Now that that silliness is taken care of, get packed array type
2191 Convert_To_PAT_Type (Opnd);
2192 PAT := Etype (Opnd);
2194 -- For the case where the packed array type is a modular type,
2195 -- not A expands simply into:
2197 -- rtyp!(PAT!(A) xor mask)
2199 -- where PAT is the packed array type, and mask is a mask of all
2200 -- one bits of length equal to the size of this packed type and
2201 -- rtyp is the actual subtype of the operand
2203 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2204 Set_Print_In_Hex (Lit);
2206 if not Is_Array_Type (PAT) then
2208 Unchecked_Convert_To (Rtyp,
2211 Right_Opnd => Lit)));
2213 -- For the array case, we insert the actions
2217 -- System.Bitops.Bit_Not
2219 -- Typ'Length * Typ'Component_Size;
2222 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2223 -- argument is the length of the operand in bits. Then we replace
2224 -- the expression by a reference to Result.
2228 Result_Ent : constant Entity_Id :=
2229 Make_Defining_Identifier (Loc,
2230 Chars => New_Internal_Name ('T'));
2233 Insert_Actions (N, New_List (
2235 Make_Object_Declaration (Loc,
2236 Defining_Identifier => Result_Ent,
2237 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2239 Make_Procedure_Call_Statement (Loc,
2240 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2241 Parameter_Associations => New_List (
2243 Make_Byte_Aligned_Attribute_Reference (Loc,
2244 Attribute_Name => Name_Address,
2247 Make_Op_Multiply (Loc,
2249 Make_Attribute_Reference (Loc,
2252 (Etype (First_Index (Rtyp)), Loc),
2253 Attribute_Name => Name_Range_Length),
2255 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2257 Make_Byte_Aligned_Attribute_Reference (Loc,
2258 Attribute_Name => Name_Address,
2259 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2262 New_Occurrence_Of (Result_Ent, Loc));
2266 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2268 end Expand_Packed_Not;
2270 -------------------------------------
2271 -- Involves_Packed_Array_Reference --
2272 -------------------------------------
2274 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2276 if Nkind (N) = N_Indexed_Component
2277 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2281 elsif Nkind (N) = N_Selected_Component then
2282 return Involves_Packed_Array_Reference (Prefix (N));
2287 end Involves_Packed_Array_Reference;
2289 --------------------------
2290 -- Known_Aligned_Enough --
2291 --------------------------
2293 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2294 Typ : constant Entity_Id := Etype (Obj);
2296 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2297 -- If the component is in a record that contains previous packed
2298 -- components, consider it unaligned because the back-end might
2299 -- choose to pack the rest of the record. Lead to less efficient code,
2300 -- but safer vis-a-vis of back-end choices.
2302 --------------------------------
2303 -- In_Partially_Packed_Record --
2304 --------------------------------
2306 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2307 Rec_Type : constant Entity_Id := Scope (Comp);
2308 Prev_Comp : Entity_Id;
2311 Prev_Comp := First_Entity (Rec_Type);
2312 while Present (Prev_Comp) loop
2313 if Is_Packed (Etype (Prev_Comp)) then
2316 elsif Prev_Comp = Comp then
2320 Next_Entity (Prev_Comp);
2324 end In_Partially_Packed_Record;
2326 -- Start of processing for Known_Aligned_Enough
2329 -- Odd bit sizes don't need alignment anyway
2331 if Csiz mod 2 = 1 then
2334 -- If we have a specified alignment, see if it is sufficient, if not
2335 -- then we can't possibly be aligned enough in any case.
2337 elsif Known_Alignment (Etype (Obj)) then
2338 -- Alignment required is 4 if size is a multiple of 4, and
2339 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2341 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2346 -- OK, alignment should be sufficient, if object is aligned
2348 -- If object is strictly aligned, then it is definitely aligned
2350 if Strict_Alignment (Typ) then
2353 -- Case of subscripted array reference
2355 elsif Nkind (Obj) = N_Indexed_Component then
2357 -- If we have a pointer to an array, then this is definitely
2358 -- aligned, because pointers always point to aligned versions.
2360 if Is_Access_Type (Etype (Prefix (Obj))) then
2363 -- Otherwise, go look at the prefix
2366 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2369 -- Case of record field
2371 elsif Nkind (Obj) = N_Selected_Component then
2373 -- What is significant here is whether the record type is packed
2375 if Is_Record_Type (Etype (Prefix (Obj)))
2376 and then Is_Packed (Etype (Prefix (Obj)))
2380 -- Or the component has a component clause which might cause
2381 -- the component to become unaligned (we can't tell if the
2382 -- backend is doing alignment computations).
2384 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2387 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2390 -- In all other cases, go look at prefix
2393 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2396 elsif Nkind (Obj) = N_Type_Conversion then
2397 return Known_Aligned_Enough (Expression (Obj), Csiz);
2399 -- For a formal parameter, it is safer to assume that it is not
2400 -- aligned, because the formal may be unconstrained while the actual
2401 -- is constrained. In this situation, a small constrained packed
2402 -- array, represented in modular form, may be unaligned.
2404 elsif Is_Entity_Name (Obj) then
2405 return not Is_Formal (Entity (Obj));
2408 -- If none of the above, must be aligned
2411 end Known_Aligned_Enough;
2413 ---------------------
2414 -- Make_Shift_Left --
2415 ---------------------
2417 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2421 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2425 Make_Op_Shift_Left (Sloc (N),
2428 Set_Shift_Count_OK (Nod, True);
2431 end Make_Shift_Left;
2433 ----------------------
2434 -- Make_Shift_Right --
2435 ----------------------
2437 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2441 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2445 Make_Op_Shift_Right (Sloc (N),
2448 Set_Shift_Count_OK (Nod, True);
2451 end Make_Shift_Right;
2453 -----------------------------
2454 -- RJ_Unchecked_Convert_To --
2455 -----------------------------
2457 function RJ_Unchecked_Convert_To
2459 Expr : Node_Id) return Node_Id
2461 Source_Typ : constant Entity_Id := Etype (Expr);
2462 Target_Typ : constant Entity_Id := Typ;
2464 Src : Node_Id := Expr;
2470 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2471 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2473 -- First step, if the source type is not a discrete type, then we
2474 -- first convert to a modular type of the source length, since
2475 -- otherwise, on a big-endian machine, we get left-justification.
2476 -- We do it for little-endian machines as well, because there might
2477 -- be junk bits that are not cleared if the type is not numeric.
2479 if Source_Siz /= Target_Siz
2480 and then not Is_Discrete_Type (Source_Typ)
2482 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2485 -- In the big endian case, if the lengths of the two types differ,
2486 -- then we must worry about possible left justification in the
2487 -- conversion, and avoiding that is what this is all about.
2489 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2491 -- Next step. If the target is not a discrete type, then we first
2492 -- convert to a modular type of the target length, since
2493 -- otherwise, on a big-endian machine, we get left-justification.
2495 if not Is_Discrete_Type (Target_Typ) then
2496 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2500 -- And now we can do the final conversion to the target type
2502 return Unchecked_Convert_To (Target_Typ, Src);
2503 end RJ_Unchecked_Convert_To;
2505 ----------------------------------------------
2506 -- Setup_Enumeration_Packed_Array_Reference --
2507 ----------------------------------------------
2509 -- All we have to do here is to find the subscripts that correspond
2510 -- to the index positions that have non-standard enumeration types
2511 -- and insert a Pos attribute to get the proper subscript value.
2513 -- Finally the prefix must be uncheck converted to the corresponding
2514 -- packed array type.
2516 -- Note that the component type is unchanged, so we do not need to
2517 -- fiddle with the types (Gigi always automatically takes the packed
2518 -- array type if it is set, as it will be in this case).
2520 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2521 Pfx : constant Node_Id := Prefix (N);
2522 Typ : constant Entity_Id := Etype (N);
2523 Exprs : constant List_Id := Expressions (N);
2527 -- If the array is unconstrained, then we replace the array
2528 -- reference with its actual subtype. This actual subtype will
2529 -- have a packed array type with appropriate bounds.
2531 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2532 Convert_To_Actual_Subtype (Pfx);
2535 Expr := First (Exprs);
2536 while Present (Expr) loop
2538 Loc : constant Source_Ptr := Sloc (Expr);
2539 Expr_Typ : constant Entity_Id := Etype (Expr);
2542 if Is_Enumeration_Type (Expr_Typ)
2543 and then Has_Non_Standard_Rep (Expr_Typ)
2546 Make_Attribute_Reference (Loc,
2547 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2548 Attribute_Name => Name_Pos,
2549 Expressions => New_List (Relocate_Node (Expr))));
2550 Analyze_And_Resolve (Expr, Standard_Natural);
2558 Make_Indexed_Component (Sloc (N),
2560 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2561 Expressions => Exprs));
2563 Analyze_And_Resolve (N, Typ);
2565 end Setup_Enumeration_Packed_Array_Reference;
2567 -----------------------------------------
2568 -- Setup_Inline_Packed_Array_Reference --
2569 -----------------------------------------
2571 procedure Setup_Inline_Packed_Array_Reference
2574 Obj : in out Node_Id;
2576 Shift : out Node_Id)
2578 Loc : constant Source_Ptr := Sloc (N);
2585 Csiz := Component_Size (Atyp);
2587 Convert_To_PAT_Type (Obj);
2590 Cmask := 2 ** Csiz - 1;
2592 if Is_Array_Type (PAT) then
2593 Otyp := Component_Type (PAT);
2594 Osiz := Component_Size (PAT);
2599 -- In the case where the PAT is a modular type, we want the actual
2600 -- size in bits of the modular value we use. This is neither the
2601 -- Object_Size nor the Value_Size, either of which may have been
2602 -- reset to strange values, but rather the minimum size. Note that
2603 -- since this is a modular type with full range, the issue of
2604 -- biased representation does not arise.
2606 Osiz := UI_From_Int (Minimum_Size (Otyp));
2609 Compute_Linear_Subscript (Atyp, N, Shift);
2611 -- If the component size is not 1, then the subscript must be
2612 -- multiplied by the component size to get the shift count.
2616 Make_Op_Multiply (Loc,
2617 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2618 Right_Opnd => Shift);
2621 -- If we have the array case, then this shift count must be broken
2622 -- down into a byte subscript, and a shift within the byte.
2624 if Is_Array_Type (PAT) then
2627 New_Shift : Node_Id;
2630 -- We must analyze shift, since we will duplicate it
2632 Set_Parent (Shift, N);
2634 (Shift, Standard_Integer, Suppress => All_Checks);
2636 -- The shift count within the word is
2641 Left_Opnd => Duplicate_Subexpr (Shift),
2642 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2644 -- The subscript to be used on the PAT array is
2648 Make_Indexed_Component (Loc,
2650 Expressions => New_List (
2651 Make_Op_Divide (Loc,
2652 Left_Opnd => Duplicate_Subexpr (Shift),
2653 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2658 -- For the modular integer case, the object to be manipulated is
2659 -- the entire array, so Obj is unchanged. Note that we will reset
2660 -- its type to PAT before returning to the caller.
2666 -- The one remaining step is to modify the shift count for the
2667 -- big-endian case. Consider the following example in a byte:
2669 -- xxxxxxxx bits of byte
2670 -- vvvvvvvv bits of value
2671 -- 33221100 little-endian numbering
2672 -- 00112233 big-endian numbering
2674 -- Here we have the case of 2-bit fields
2676 -- For the little-endian case, we already have the proper shift
2677 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2679 -- For the big endian case, we have to adjust the shift count,
2680 -- computing it as (N - F) - shift, where N is the number of bits
2681 -- in an element of the array used to implement the packed array,
2682 -- F is the number of bits in a source level array element, and
2683 -- shift is the count so far computed.
2685 if Bytes_Big_Endian then
2687 Make_Op_Subtract (Loc,
2688 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2689 Right_Opnd => Shift);
2692 Set_Parent (Shift, N);
2693 Set_Parent (Obj, N);
2694 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2695 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2697 -- Make sure final type of object is the appropriate packed type
2699 Set_Etype (Obj, Otyp);
2701 end Setup_Inline_Packed_Array_Reference;