1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- GNAT was originally developed by the GNAT team at New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
27 ------------------------------------------------------------------------------
29 with Atree; use Atree;
30 with Checks; use Checks;
31 with Einfo; use Einfo;
32 with Exp_Dbug; use Exp_Dbug;
33 with Exp_Util; use Exp_Util;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
37 with Rtsfind; use Rtsfind;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Ch13; use Sem_Ch13;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Targparm; use Targparm;
48 with Tbuild; use Tbuild;
49 with Ttypes; use Ttypes;
50 with Uintp; use Uintp;
52 package body Exp_Pakd is
54 ---------------------------
55 -- Endian Considerations --
56 ---------------------------
58 -- As described in the specification, bit numbering in a packed array
59 -- is consistent with bit numbering in a record representation clause,
60 -- and hence dependent on the endianness of the machine:
62 -- For little-endian machines, element zero is at the right hand end
63 -- (low order end) of a bit field.
65 -- For big-endian machines, element zero is at the left hand end
66 -- (high order end) of a bit field.
68 -- The shifts that are used to right justify a field therefore differ
69 -- in the two cases. For the little-endian case, we can simply use the
70 -- bit number (i.e. the element number * element size) as the count for
71 -- a right shift. For the big-endian case, we have to subtract the shift
72 -- count from an appropriate constant to use in the right shift. We use
73 -- rotates instead of shifts (which is necessary in the store case to
74 -- preserve other fields), and we expect that the backend will be able
75 -- to change the right rotate into a left rotate, avoiding the subtract,
76 -- if the architecture provides such an instruction.
78 ----------------------------------------------
79 -- Entity Tables for Packed Access Routines --
80 ----------------------------------------------
82 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
83 -- library routines. This table is used to obtain the entity for the
86 type E_Array is array (Int range 01 .. 63) of RE_Id;
88 -- Array of Bits_nn entities. Note that we do not use library routines
89 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
90 -- entries from System.Unsigned, because we also use this table for
91 -- certain special unchecked conversions in the big-endian case.
93 Bits_Id : constant E_Array :=
109 16 => RE_Unsigned_16,
125 32 => RE_Unsigned_32,
158 -- Array of Get routine entities. These are used to obtain an element
159 -- from a packed array. The N'th entry is used to obtain elements from
160 -- a packed array whose component size is N. RE_Null is used as a null
161 -- entry, for the cases where a library routine is not used.
163 Get_Id : constant E_Array :=
228 -- Array of Get routine entities to be used in the case where the packed
229 -- array is itself a component of a packed structure, and therefore may
230 -- not be fully aligned. This only affects the even sizes, since for the
231 -- odd sizes, we do not get any fixed alignment in any case.
233 GetU_Id : constant E_Array :=
298 -- Array of Set routine entities. These are used to assign an element
299 -- of a packed array. The N'th entry is used to assign elements for
300 -- a packed array whose component size is N. RE_Null is used as a null
301 -- entry, for the cases where a library routine is not used.
368 -- Array of Set routine entities to be used in the case where the packed
369 -- array is itself a component of a packed structure, and therefore may
370 -- not be fully aligned. This only affects the even sizes, since for the
371 -- odd sizes, we do not get any fixed alignment in any case.
438 -----------------------
439 -- Local Subprograms --
440 -----------------------
442 procedure Compute_Linear_Subscript
445 Subscr : out Node_Id);
446 -- Given a constrained array type Atyp, and an indexed component node
447 -- N referencing an array object of this type, build an expression of
448 -- type Standard.Integer representing the zero-based linear subscript
449 -- value. This expression includes any required range checks.
451 procedure Convert_To_PAT_Type (Aexp : Node_Id);
452 -- Given an expression of a packed array type, builds a corresponding
453 -- expression whose type is the implementation type used to represent
454 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
456 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
457 -- Build a left shift node, checking for the case of a shift count of zero
459 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
460 -- Build a right shift node, checking for the case of a shift count of zero
462 function RJ_Unchecked_Convert_To
466 -- The packed array code does unchecked conversions which in some cases
467 -- may involve non-discrete types with differing sizes. The semantics of
468 -- such conversions is potentially endian dependent, and the effect we
469 -- want here for such a conversion is to do the conversion in size as
470 -- though numeric items are involved, and we extend or truncate on the
471 -- left side. This happens naturally in the little-endian case, but in
472 -- the big endian case we can get left justification, when what we want
473 -- is right justification. This routine does the unchecked conversion in
474 -- a stepwise manner to ensure that it gives the expected result. Hence
475 -- the name (RJ = Right justified). The parameters Typ and Expr are as
476 -- for the case of a normal Unchecked_Convert_To call.
478 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
479 -- This routine is called in the Get and Set case for arrays that are
480 -- packed but not bit-packed, meaning that they have at least one
481 -- subscript that is of an enumeration type with a non-standard
482 -- representation. This routine modifies the given node to properly
483 -- reference the corresponding packed array type.
485 procedure Setup_Inline_Packed_Array_Reference
488 Obj : in out Node_Id;
490 Shift : out Node_Id);
491 -- This procedure performs common processing on the N_Indexed_Component
492 -- parameter given as N, whose prefix is a reference to a packed array.
493 -- This is used for the get and set when the component size is 1,2,4
494 -- or for other component sizes when the packed array type is a modular
495 -- type (i.e. the cases that are handled with inline code).
499 -- N is the N_Indexed_Component node for the packed array reference
501 -- Atyp is the constrained array type (the actual subtype has been
502 -- computed if necessary to obtain the constraints, but this is still
503 -- the original array type, not the Packed_Array_Type value).
505 -- Obj is the object which is to be indexed. It is always of type Atyp.
509 -- Obj is the object containing the desired bit field. It is of type
510 -- Unsigned or Long_Long_Unsigned, and is either the entire value,
511 -- for the small static case, or the proper selected byte from the
512 -- array in the large or dynamic case. This node is analyzed and
513 -- resolved on return.
515 -- Shift is a node representing the shift count to be used in the
516 -- rotate right instruction that positions the field for access.
517 -- This node is analyzed and resolved on return.
519 -- Cmask is a mask corresponding to the width of the component field.
520 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
522 -- Note: in some cases the call to this routine may generate actions
523 -- (for handling multi-use references and the generation of the packed
524 -- array type on the fly). Such actions are inserted into the tree
525 -- directly using Insert_Action.
527 ------------------------------
528 -- Compute_Linear_Subcsript --
529 ------------------------------
531 procedure Compute_Linear_Subscript
534 Subscr : out Node_Id)
536 Loc : constant Source_Ptr := Sloc (N);
545 -- Loop through dimensions
547 Indx := First_Index (Atyp);
548 Oldsub := First (Expressions (N));
550 while Present (Indx) loop
551 Styp := Etype (Indx);
552 Newsub := Relocate_Node (Oldsub);
554 -- Get expression for the subscript value. First, if Do_Range_Check
555 -- is set on a subscript, then we must do a range check against the
556 -- original bounds (not the bounds of the packed array type). We do
557 -- this by introducing a subtype conversion.
559 if Do_Range_Check (Newsub)
560 and then Etype (Newsub) /= Styp
562 Newsub := Convert_To (Styp, Newsub);
565 -- Now evolve the expression for the subscript. First convert
566 -- the subscript to be zero based and of an integer type.
568 -- Case of integer type, where we just subtract to get lower bound
570 if Is_Integer_Type (Styp) then
572 -- If length of integer type is smaller than standard integer,
573 -- then we convert to integer first, then do the subtract
575 -- Integer (subscript) - Integer (Styp'First)
577 if Esize (Styp) < Esize (Standard_Integer) then
579 Make_Op_Subtract (Loc,
580 Left_Opnd => Convert_To (Standard_Integer, Newsub),
582 Convert_To (Standard_Integer,
583 Make_Attribute_Reference (Loc,
584 Prefix => New_Occurrence_Of (Styp, Loc),
585 Attribute_Name => Name_First)));
587 -- For larger integer types, subtract first, then convert to
588 -- integer, this deals with strange long long integer bounds.
590 -- Integer (subscript - Styp'First)
594 Convert_To (Standard_Integer,
595 Make_Op_Subtract (Loc,
598 Make_Attribute_Reference (Loc,
599 Prefix => New_Occurrence_Of (Styp, Loc),
600 Attribute_Name => Name_First)));
603 -- For the enumeration case, we have to use 'Pos to get the value
604 -- to work with before subtracting the lower bound.
606 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
608 -- This is not quite right for bizarre cases where the size of the
609 -- enumeration type is > Integer'Size bits due to rep clause ???
612 pragma Assert (Is_Enumeration_Type (Styp));
615 Make_Op_Subtract (Loc,
616 Left_Opnd => Convert_To (Standard_Integer,
617 Make_Attribute_Reference (Loc,
618 Prefix => New_Occurrence_Of (Styp, Loc),
619 Attribute_Name => Name_Pos,
620 Expressions => New_List (Newsub))),
623 Convert_To (Standard_Integer,
624 Make_Attribute_Reference (Loc,
625 Prefix => New_Occurrence_Of (Styp, Loc),
626 Attribute_Name => Name_Pos,
627 Expressions => New_List (
628 Make_Attribute_Reference (Loc,
629 Prefix => New_Occurrence_Of (Styp, Loc),
630 Attribute_Name => Name_First)))));
633 Set_Paren_Count (Newsub, 1);
635 -- For the first subscript, we just copy that subscript value
640 -- Otherwise, we must multiply what we already have by the current
641 -- stride and then add in the new value to the evolving subscript.
647 Make_Op_Multiply (Loc,
650 Make_Attribute_Reference (Loc,
651 Attribute_Name => Name_Range_Length,
652 Prefix => New_Occurrence_Of (Styp, Loc))),
653 Right_Opnd => Newsub);
656 -- Move to next subscript
661 end Compute_Linear_Subscript;
663 -------------------------
664 -- Convert_To_PAT_Type --
665 -------------------------
667 -- The PAT is always obtained from the actual subtype
669 procedure Convert_To_PAT_Type (Aexp : Entity_Id) is
673 Convert_To_Actual_Subtype (Aexp);
674 Act_ST := Underlying_Type (Etype (Aexp));
675 Create_Packed_Array_Type (Act_ST);
677 -- Just replace the etype with the packed array type. This works
678 -- because the expression will not be further analyzed, and Gigi
679 -- considers the two types equivalent in any case.
681 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
682 end Convert_To_PAT_Type;
684 ------------------------------
685 -- Create_Packed_Array_Type --
686 ------------------------------
688 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
689 Loc : constant Source_Ptr := Sloc (Typ);
690 Ctyp : constant Entity_Id := Component_Type (Typ);
691 Csize : constant Uint := Component_Size (Typ);
706 procedure Install_PAT;
707 -- This procedure is called with Decl set to the declaration for the
708 -- packed array type. It creates the type and installs it as required.
710 procedure Set_PB_Type;
711 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
712 -- requirements (see documentation in the spec of this package).
718 procedure Install_PAT is
719 Pushed_Scope : Boolean := False;
722 -- We do not want to put the declaration we have created in the tree
723 -- since it is often hard, and sometimes impossible to find a proper
724 -- place for it (the impossible case arises for a packed array type
725 -- with bounds depending on the discriminant, a declaration cannot
726 -- be put inside the record, and the reference to the discriminant
727 -- cannot be outside the record).
729 -- The solution is to analyze the declaration while temporarily
730 -- attached to the tree at an appropriate point, and then we install
731 -- the resulting type as an Itype in the packed array type field of
732 -- the original type, so that no explicit declaration is required.
734 -- Note: the packed type is created in the scope of its parent
735 -- type. There are at least some cases where the current scope
736 -- is deeper, and so when this is the case, we temporarily reset
737 -- the scope for the definition. This is clearly safe, since the
738 -- first use of the packed array type will be the implicit
739 -- reference from the corresponding unpacked type when it is
742 if Is_Itype (Typ) then
743 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
745 Set_Parent (Decl, Declaration_Node (Typ));
748 if Scope (Typ) /= Current_Scope then
749 New_Scope (Scope (Typ));
750 Pushed_Scope := True;
753 Set_Is_Itype (PAT, True);
754 Set_Is_Packed_Array_Type (PAT, True);
755 Analyze (Decl, Suppress => All_Checks);
761 -- Set Esize and RM_Size to the actual size of the packed object
762 -- Do not reset RM_Size if already set, as happens in the case
765 Set_Esize (PAT, Esiz);
767 if Unknown_RM_Size (PAT) then
768 Set_RM_Size (PAT, Esiz);
771 -- Set remaining fields of packed array type
773 Init_Alignment (PAT);
774 Set_Parent (PAT, Empty);
775 Set_Packed_Array_Type (Typ, PAT);
776 Set_Associated_Node_For_Itype (PAT, Typ);
778 -- We definitely do not want to delay freezing for packed array
779 -- types. This is of particular importance for the itypes that
780 -- are generated for record components depending on discriminants
781 -- where there is no place to put the freeze node.
783 Set_Has_Delayed_Freeze (PAT, False);
784 Set_Has_Delayed_Freeze (Etype (PAT), False);
791 procedure Set_PB_Type is
793 -- If the user has specified an explicit alignment for the
794 -- component, take it into account.
796 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
797 or else Component_Alignment (Typ) = Calign_Storage_Unit
799 PB_Type := RTE (RE_Packed_Bytes1);
801 elsif Csize mod 4 /= 0 then
802 PB_Type := RTE (RE_Packed_Bytes2);
805 PB_Type := RTE (RE_Packed_Bytes4);
809 -- Start of processing for Create_Packed_Array_Type
812 -- If we already have a packed array type, nothing to do
814 if Present (Packed_Array_Type (Typ)) then
818 -- If our immediate ancestor subtype is constrained, and it already
819 -- has a packed array type, then just share the same type, since the
820 -- bounds must be the same.
822 if Ekind (Typ) = E_Array_Subtype then
823 Ancest := Ancestor_Subtype (Typ);
826 and then Is_Constrained (Ancest)
827 and then Present (Packed_Array_Type (Ancest))
829 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
834 -- We preset the result type size from the size of the original array
835 -- type, since this size clearly belongs to the packed array type. The
836 -- size of the conceptual unpacked type is always set to unknown.
840 -- Case of an array where at least one index is of an enumeration
841 -- type with a non-standard representation, but the component size
842 -- is not appropriate for bit packing. This is the case where we
843 -- have Is_Packed set (we would never be in this unit otherwise),
844 -- but Is_Bit_Packed_Array is false.
846 -- Note that if the component size is appropriate for bit packing,
847 -- then the circuit for the computation of the subscript properly
848 -- deals with the non-standard enumeration type case by taking the
851 if not Is_Bit_Packed_Array (Typ) then
853 -- Here we build a declaration:
855 -- type tttP is array (index1, index2, ...) of component_type
857 -- where index1, index2, are the index types. These are the same
858 -- as the index types of the original array, except for the non-
859 -- standard representation enumeration type case, where we have
862 -- For the unconstrained array case, we use
866 -- For the constrained case, we use
868 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
869 -- Enum_Type'Pos (Enum_Type'Last);
872 Make_Defining_Identifier (Loc,
873 Chars => New_External_Name (Chars (Typ), 'P'));
875 Set_Packed_Array_Type (Typ, PAT);
878 Indexes : List_Id := New_List;
880 Indx_Typ : Entity_Id;
885 Indx := First_Index (Typ);
887 while Present (Indx) loop
888 Indx_Typ := Etype (Indx);
890 Enum_Case := Is_Enumeration_Type (Indx_Typ)
891 and then Has_Non_Standard_Rep (Indx_Typ);
893 -- Unconstrained case
895 if not Is_Constrained (Typ) then
897 Indx_Typ := Standard_Natural;
900 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
905 if not Enum_Case then
906 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
910 Make_Subtype_Indication (Loc,
912 New_Occurrence_Of (Standard_Natural, Loc),
914 Make_Range_Constraint (Loc,
918 Make_Attribute_Reference (Loc,
920 New_Occurrence_Of (Indx_Typ, Loc),
921 Attribute_Name => Name_Pos,
922 Expressions => New_List (
923 Make_Attribute_Reference (Loc,
925 New_Occurrence_Of (Indx_Typ, Loc),
926 Attribute_Name => Name_First))),
929 Make_Attribute_Reference (Loc,
931 New_Occurrence_Of (Indx_Typ, Loc),
932 Attribute_Name => Name_Pos,
933 Expressions => New_List (
934 Make_Attribute_Reference (Loc,
936 New_Occurrence_Of (Indx_Typ, Loc),
937 Attribute_Name => Name_Last)))))));
945 if not Is_Constrained (Typ) then
947 Make_Unconstrained_Array_Definition (Loc,
948 Subtype_Marks => Indexes,
949 Subtype_Indication =>
950 New_Occurrence_Of (Ctyp, Loc));
954 Make_Constrained_Array_Definition (Loc,
955 Discrete_Subtype_Definitions => Indexes,
956 Subtype_Indication =>
957 New_Occurrence_Of (Ctyp, Loc));
961 Make_Full_Type_Declaration (Loc,
962 Defining_Identifier => PAT,
963 Type_Definition => Typedef);
969 -- Case of bit-packing required for unconstrained array. We simply
970 -- use Packed_Bytes{1,2,4} as appropriate, and we do not need to
971 -- construct a special packed array type.
973 elsif not Is_Constrained (Typ) then
975 Set_Packed_Array_Type (Typ, PB_Type);
976 Set_Is_Packed_Array_Type (Packed_Array_Type (Typ), True);
979 -- Remaining code is for the case of bit-packing for constrained array
981 -- The name of the packed array subtype is
985 -- where sss is the component size in bits and ttt is the name of
986 -- the parent packed type.
990 Make_Defining_Identifier (Loc,
991 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
993 Set_Packed_Array_Type (Typ, PAT);
995 -- Build an expression for the length of the array in bits.
996 -- This is the product of the length of each of the dimensions
1002 Len_Expr := Empty; -- suppress junk warning
1006 Make_Attribute_Reference (Loc,
1007 Attribute_Name => Name_Length,
1008 Prefix => New_Occurrence_Of (Typ, Loc),
1009 Expressions => New_List (
1010 Make_Integer_Literal (Loc, J)));
1013 Len_Expr := Len_Dim;
1017 Make_Op_Multiply (Loc,
1018 Left_Opnd => Len_Expr,
1019 Right_Opnd => Len_Dim);
1023 exit when J > Number_Dimensions (Typ);
1027 -- Temporarily attach the length expression to the tree and analyze
1028 -- and resolve it, so that we can test its value. We assume that the
1029 -- total length fits in type Integer.
1031 Set_Parent (Len_Expr, Typ);
1032 Analyze_And_Resolve (Len_Expr, Standard_Integer);
1034 -- Use a modular type if possible. We can do this if we are we
1035 -- have static bounds, and the length is small enough, and the
1036 -- length is not zero. We exclude the zero length case because the
1037 -- size of things is always at least one, and the zero length object
1038 -- would have an anomous size
1040 if Compile_Time_Known_Value (Len_Expr) then
1041 Len_Bits := Expr_Value (Len_Expr) * Csize;
1043 -- We normally consider small enough to mean no larger than the
1044 -- value of System_Max_Binary_Modulus_Power, except that in
1045 -- No_Run_Time mode, we use the Word Size on machines for
1046 -- which double length shifts are not generated in line.
1050 (Len_Bits <= System_Word_Size
1051 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1052 and then (not No_Run_Time
1054 Long_Shifts_Inlined_On_Target)))
1056 -- We can use the modular type, it has the form:
1058 -- subtype tttPn is btyp
1059 -- range 0 .. 2 ** (Esize (Typ) * Csize) - 1;
1061 -- Here Siz is 1, 2 or 4, as computed above, and btyp is either
1062 -- Unsigned or Long_Long_Unsigned depending on the length.
1064 if Len_Bits <= Standard_Integer_Size then
1065 Btyp := RTE (RE_Unsigned);
1067 Btyp := RTE (RE_Long_Long_Unsigned);
1070 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1071 Set_Print_In_Hex (Lit);
1074 Make_Subtype_Declaration (Loc,
1075 Defining_Identifier => PAT,
1076 Subtype_Indication =>
1077 Make_Subtype_Indication (Loc,
1078 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1081 Make_Range_Constraint (Loc,
1085 Make_Integer_Literal (Loc, 0),
1086 High_Bound => Lit))));
1088 if Esiz = Uint_0 then
1097 -- Could not use a modular type, for all other cases, we build
1098 -- a packed array subtype:
1101 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1103 -- Bits is the length of the array in bits.
1110 Make_Op_Multiply (Loc,
1112 Make_Integer_Literal (Loc, Csize),
1113 Right_Opnd => Len_Expr),
1116 Make_Integer_Literal (Loc, 7));
1118 Set_Paren_Count (Bits_U1, 1);
1121 Make_Op_Subtract (Loc,
1123 Make_Op_Divide (Loc,
1124 Left_Opnd => Bits_U1,
1125 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1126 Right_Opnd => Make_Integer_Literal (Loc, 1));
1129 Make_Subtype_Declaration (Loc,
1130 Defining_Identifier => PAT,
1131 Subtype_Indication =>
1132 Make_Subtype_Indication (Loc,
1133 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1136 Make_Index_Or_Discriminant_Constraint (Loc,
1137 Constraints => New_List (
1140 Make_Integer_Literal (Loc, 0),
1141 High_Bound => PAT_High)))));
1145 end Create_Packed_Array_Type;
1147 -----------------------------------
1148 -- Expand_Bit_Packed_Element_Set --
1149 -----------------------------------
1151 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1152 Loc : constant Source_Ptr := Sloc (N);
1153 Lhs : constant Node_Id := Name (N);
1155 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1156 -- Used to preserve assignment OK status when assignment is rewritten
1158 Rhs : Node_Id := Expression (N);
1159 -- Initially Rhs is the right hand side value, it will be replaced
1160 -- later by an appropriate unchecked conversion for the assignment.
1173 Rhs_Val_Known : Boolean;
1175 -- If the value of the right hand side as an integer constant is
1176 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1177 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1178 -- the Rhs_Val is undefined.
1181 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1183 Obj := Relocate_Node (Prefix (Lhs));
1184 Convert_To_Actual_Subtype (Obj);
1185 Atyp := Etype (Obj);
1186 PAT := Packed_Array_Type (Atyp);
1187 Ctyp := Component_Type (Atyp);
1188 Csiz := UI_To_Int (Component_Size (Atyp));
1190 -- We convert the right hand side to the proper subtype to ensure
1191 -- that an appropriate range check is made (since the normal range
1192 -- check from assignment will be lost in the transformations). This
1193 -- conversion is analyzed immediately so that subsequent processing
1194 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1196 Rhs := Convert_To (Ctyp, Rhs);
1197 Set_Parent (Rhs, N);
1198 Analyze_And_Resolve (Rhs, Ctyp);
1200 -- Case of component size 1,2,4 or any component size for the modular
1201 -- case. These are the cases for which we can inline the code.
1203 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1204 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1206 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1208 -- The statement to be generated is:
1210 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1212 -- where mask1 is obtained by shifting Cmask left Shift bits
1213 -- and then complementing the result.
1215 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1217 -- the "or ..." is omitted if rhs is constant and all 0 bits
1219 -- rhs is converted to the appropriate type.
1221 -- The result is converted back to the array type, since
1222 -- otherwise we lose knowledge of the packed nature.
1224 -- Determine if right side is all 0 bits or all 1 bits
1226 if Compile_Time_Known_Value (Rhs) then
1227 Rhs_Val := Expr_Rep_Value (Rhs);
1228 Rhs_Val_Known := True;
1230 -- The following test catches the case of an unchecked conversion
1231 -- of an integer literal. This results from optimizing aggregates
1234 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1235 and then Compile_Time_Known_Value (Expression (Rhs))
1237 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1238 Rhs_Val_Known := True;
1242 Rhs_Val_Known := False;
1245 -- Some special checks for the case where the right hand value
1246 -- is known at compile time. Basically we have to take care of
1247 -- the implicit conversion to the subtype of the component object.
1249 if Rhs_Val_Known then
1251 -- If we have a biased component type then we must manually do
1252 -- the biasing, since we are taking responsibility in this case
1253 -- for constructing the exact bit pattern to be used.
1255 if Has_Biased_Representation (Ctyp) then
1256 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1259 -- For a negative value, we manually convert the twos complement
1260 -- value to a corresponding unsigned value, so that the proper
1261 -- field width is maintained. If we did not do this, we would
1262 -- get too many leading sign bits later on.
1265 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1269 New_Lhs := Duplicate_Subexpr (Obj, True);
1270 New_Rhs := Duplicate_Subexpr (Obj);
1272 -- First we deal with the "and"
1274 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1280 if Compile_Time_Known_Value (Shift) then
1282 Make_Integer_Literal (Loc,
1283 Modulus (Etype (Obj)) - 1 -
1284 (Cmask * (2 ** Expr_Value (Shift))));
1285 Set_Print_In_Hex (Mask1);
1288 Lit := Make_Integer_Literal (Loc, Cmask);
1289 Set_Print_In_Hex (Lit);
1292 Right_Opnd => Make_Shift_Left (Lit, Shift));
1297 Left_Opnd => New_Rhs,
1298 Right_Opnd => Mask1);
1302 -- Then deal with the "or"
1304 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1308 procedure Fixup_Rhs;
1309 -- Adjust Rhs by bias if biased representation for components
1310 -- or remove extraneous high order sign bits if signed.
1312 procedure Fixup_Rhs is
1313 Etyp : constant Entity_Id := Etype (Rhs);
1316 -- For biased case, do the required biasing by simply
1317 -- converting to the biased subtype (the conversion
1318 -- will generate the required bias).
1320 if Has_Biased_Representation (Ctyp) then
1321 Rhs := Convert_To (Ctyp, Rhs);
1323 -- For a signed integer type that is not biased, generate
1324 -- a conversion to unsigned to strip high order sign bits.
1326 elsif Is_Signed_Integer_Type (Ctyp) then
1327 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1330 -- Set Etype, since it can be referenced before the
1331 -- node is completely analyzed.
1333 Set_Etype (Rhs, Etyp);
1335 -- We now need to do an unchecked conversion of the
1336 -- result to the target type, but it is important that
1337 -- this conversion be a right justified conversion and
1338 -- not a left justified conversion.
1340 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1346 and then Compile_Time_Known_Value (Shift)
1349 Make_Integer_Literal (Loc,
1350 Rhs_Val * (2 ** Expr_Value (Shift)));
1351 Set_Print_In_Hex (Or_Rhs);
1354 -- We have to convert the right hand side to Etype (Obj).
1355 -- A special case case arises if what we have now is a Val
1356 -- attribute reference whose expression type is Etype (Obj).
1357 -- This happens for assignments of fields from the same
1358 -- array. In this case we get the required right hand side
1359 -- by simply removing the inner attribute reference.
1361 if Nkind (Rhs) = N_Attribute_Reference
1362 and then Attribute_Name (Rhs) = Name_Val
1363 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1365 Rhs := Relocate_Node (First (Expressions (Rhs)));
1368 -- If the value of the right hand side is a known integer
1369 -- value, then just replace it by an untyped constant,
1370 -- which will be properly retyped when we analyze and
1371 -- resolve the expression.
1373 elsif Rhs_Val_Known then
1375 -- Note that Rhs_Val has already been normalized to
1376 -- be an unsigned value with the proper number of bits.
1379 Make_Integer_Literal (Loc, Rhs_Val);
1381 -- Otherwise we need an unchecked conversion
1387 Or_Rhs := Make_Shift_Left (Rhs, Shift);
1390 if Nkind (New_Rhs) = N_Op_And then
1391 Set_Paren_Count (New_Rhs, 1);
1396 Left_Opnd => New_Rhs,
1397 Right_Opnd => Or_Rhs);
1401 -- Now do the rewrite
1404 Make_Assignment_Statement (Loc,
1407 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1408 Set_Assignment_OK (Name (N), Ass_OK);
1410 -- All other component sizes for non-modular case
1415 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1417 -- where Subscr is the computed linear subscript.
1420 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1426 -- Acquire proper Set entity. We use the aligned or unaligned
1427 -- case as appropriate.
1429 if Must_Be_Aligned (Obj) then
1430 Set_nn := RTE (Set_Id (Csiz));
1432 Set_nn := RTE (SetU_Id (Csiz));
1435 -- Now generate the set reference
1437 Obj := Relocate_Node (Prefix (Lhs));
1438 Convert_To_Actual_Subtype (Obj);
1439 Atyp := Etype (Obj);
1440 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1443 Make_Procedure_Call_Statement (Loc,
1444 Name => New_Occurrence_Of (Set_nn, Loc),
1445 Parameter_Associations => New_List (
1446 Make_Attribute_Reference (Loc,
1447 Attribute_Name => Name_Address,
1450 Unchecked_Convert_To (Bits_nn,
1451 Convert_To (Ctyp, Rhs)))));
1456 Analyze (N, Suppress => All_Checks);
1457 end Expand_Bit_Packed_Element_Set;
1459 -------------------------------------
1460 -- Expand_Packed_Address_Reference --
1461 -------------------------------------
1463 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1464 Loc : constant Source_Ptr := Sloc (N);
1476 -- We build up an expression serially that has the form
1478 -- outer_object'Address
1479 -- + (linear-subscript * component_size for each array reference
1480 -- + field'Bit_Position for each record field
1482 -- + ...) / Storage_Unit;
1484 -- Some additional conversions are required to deal with the addition
1485 -- operation, which is not normally visible to generated code.
1488 Ploc := Sloc (Pref);
1490 if Nkind (Pref) = N_Indexed_Component then
1491 Convert_To_Actual_Subtype (Prefix (Pref));
1492 Atyp := Etype (Prefix (Pref));
1493 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1496 Make_Op_Multiply (Ploc,
1497 Left_Opnd => Subscr,
1499 Make_Attribute_Reference (Ploc,
1500 Prefix => New_Occurrence_Of (Atyp, Ploc),
1501 Attribute_Name => Name_Component_Size));
1503 elsif Nkind (Pref) = N_Selected_Component then
1505 Make_Attribute_Reference (Ploc,
1506 Prefix => Selector_Name (Pref),
1507 Attribute_Name => Name_Bit_Position);
1513 Term := Convert_To (RTE (RE_Integer_Address), Term);
1522 Right_Opnd => Term);
1525 Pref := Prefix (Pref);
1529 Unchecked_Convert_To (RTE (RE_Address),
1532 Unchecked_Convert_To (RTE (RE_Integer_Address),
1533 Make_Attribute_Reference (Loc,
1535 Attribute_Name => Name_Address)),
1538 Make_Op_Divide (Loc,
1541 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1543 Analyze_And_Resolve (N, RTE (RE_Address));
1544 end Expand_Packed_Address_Reference;
1546 ------------------------------------
1547 -- Expand_Packed_Boolean_Operator --
1548 ------------------------------------
1550 -- This routine expands "a op b" for the packed cases
1552 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1553 Loc : constant Source_Ptr := Sloc (N);
1554 Typ : constant Entity_Id := Etype (N);
1555 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1556 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1563 Convert_To_Actual_Subtype (L);
1564 Convert_To_Actual_Subtype (R);
1566 Ensure_Defined (Etype (L), N);
1567 Ensure_Defined (Etype (R), N);
1569 Apply_Length_Check (R, Etype (L));
1574 -- First an odd and silly test. We explicitly check for the XOR
1575 -- case where the component type is True .. True, since this will
1576 -- raise constraint error. A special check is required since CE
1577 -- will not be required other wise (cf Expand_Packed_Not).
1579 -- No such check is required for AND and OR, since for both these
1580 -- cases False op False = False, and True op True = True.
1582 if Nkind (N) = N_Op_Xor then
1584 CT : constant Entity_Id := Component_Type (Rtyp);
1585 BT : constant Entity_Id := Base_Type (CT);
1589 Make_Raise_Constraint_Error (Loc,
1595 Make_Attribute_Reference (Loc,
1596 Prefix => New_Occurrence_Of (CT, Loc),
1597 Attribute_Name => Name_First),
1601 New_Occurrence_Of (Standard_True, Loc))),
1606 Make_Attribute_Reference (Loc,
1607 Prefix => New_Occurrence_Of (CT, Loc),
1608 Attribute_Name => Name_Last),
1612 New_Occurrence_Of (Standard_True, Loc))))));
1616 -- Now that that silliness is taken care of, get packed array type
1618 Convert_To_PAT_Type (L);
1619 Convert_To_PAT_Type (R);
1623 -- For the modular case, we expand a op b into
1625 -- rtyp!(pat!(a) op pat!(b))
1627 -- where rtyp is the Etype of the left operand. Note that we do not
1628 -- convert to the base type, since this would be unconstrained, and
1629 -- hence not have a corresponding packed array type set.
1631 if Is_Modular_Integer_Type (PAT) then
1636 if Nkind (N) = N_Op_And then
1637 P := Make_Op_And (Loc, L, R);
1639 elsif Nkind (N) = N_Op_Or then
1640 P := Make_Op_Or (Loc, L, R);
1642 else -- Nkind (N) = N_Op_Xor
1643 P := Make_Op_Xor (Loc, L, R);
1646 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1649 -- For the array case, we insert the actions
1653 -- System.Bitops.Bit_And/Or/Xor
1655 -- Ltype'Length * Ltype'Component_Size;
1657 -- Rtype'Length * Rtype'Component_Size
1660 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1661 -- the second argument and fourth arguments are the lengths of the
1662 -- operands in bits. Then we replace the expression by a reference
1667 Result_Ent : constant Entity_Id :=
1668 Make_Defining_Identifier (Loc,
1669 Chars => New_Internal_Name ('T'));
1674 if Nkind (N) = N_Op_And then
1677 elsif Nkind (N) = N_Op_Or then
1680 else -- Nkind (N) = N_Op_Xor
1684 Insert_Actions (N, New_List (
1686 Make_Object_Declaration (Loc,
1687 Defining_Identifier => Result_Ent,
1688 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1690 Make_Procedure_Call_Statement (Loc,
1691 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1692 Parameter_Associations => New_List (
1694 Make_Attribute_Reference (Loc,
1695 Attribute_Name => Name_Address,
1698 Make_Op_Multiply (Loc,
1700 Make_Attribute_Reference (Loc,
1703 (Etype (First_Index (Ltyp)), Loc),
1704 Attribute_Name => Name_Range_Length),
1706 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1708 Make_Attribute_Reference (Loc,
1709 Attribute_Name => Name_Address,
1712 Make_Op_Multiply (Loc,
1714 Make_Attribute_Reference (Loc,
1717 (Etype (First_Index (Rtyp)), Loc),
1718 Attribute_Name => Name_Range_Length),
1720 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1722 Make_Attribute_Reference (Loc,
1723 Attribute_Name => Name_Address,
1724 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1727 New_Occurrence_Of (Result_Ent, Loc));
1731 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1732 end Expand_Packed_Boolean_Operator;
1734 -------------------------------------
1735 -- Expand_Packed_Element_Reference --
1736 -------------------------------------
1738 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1739 Loc : constant Source_Ptr := Sloc (N);
1751 -- If not bit packed, we have the enumeration case, which is easily
1752 -- dealt with (just adjust the subscripts of the indexed component)
1754 -- Note: this leaves the result as an indexed component, which is
1755 -- still a variable, so can be used in the assignment case, as is
1756 -- required in the enumeration case.
1758 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1759 Setup_Enumeration_Packed_Array_Reference (N);
1763 -- Remaining processing is for the bit-packed case.
1765 Obj := Relocate_Node (Prefix (N));
1766 Convert_To_Actual_Subtype (Obj);
1767 Atyp := Etype (Obj);
1768 PAT := Packed_Array_Type (Atyp);
1769 Ctyp := Component_Type (Atyp);
1770 Csiz := UI_To_Int (Component_Size (Atyp));
1772 -- Case of component size 1,2,4 or any component size for the modular
1773 -- case. These are the cases for which we can inline the code.
1775 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1776 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1778 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1779 Lit := Make_Integer_Literal (Loc, Cmask);
1780 Set_Print_In_Hex (Lit);
1782 -- We generate a shift right to position the field, followed by a
1783 -- masking operation to extract the bit field, and we finally do an
1784 -- unchecked conversion to convert the result to the required target.
1786 -- Note that the unchecked conversion automatically deals with the
1787 -- bias if we are dealing with a biased representation. What will
1788 -- happen is that we temporarily generate the biased representation,
1789 -- but almost immediately that will be converted to the original
1790 -- unbiased component type, and the bias will disappear.
1794 Left_Opnd => Make_Shift_Right (Obj, Shift),
1797 Analyze_And_Resolve (Arg);
1800 RJ_Unchecked_Convert_To (Ctyp, Arg));
1802 -- All other component sizes for non-modular case
1807 -- Component_Type!(Get_nn (Arr'address, Subscr))
1809 -- where Subscr is the computed linear subscript.
1816 -- Acquire proper Get entity. We use the aligned or unaligned
1817 -- case as appropriate.
1819 if Must_Be_Aligned (Obj) then
1820 Get_nn := RTE (Get_Id (Csiz));
1822 Get_nn := RTE (GetU_Id (Csiz));
1825 -- Now generate the get reference
1827 Compute_Linear_Subscript (Atyp, N, Subscr);
1830 Unchecked_Convert_To (Ctyp,
1831 Make_Function_Call (Loc,
1832 Name => New_Occurrence_Of (Get_nn, Loc),
1833 Parameter_Associations => New_List (
1834 Make_Attribute_Reference (Loc,
1835 Attribute_Name => Name_Address,
1841 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1843 end Expand_Packed_Element_Reference;
1845 ----------------------
1846 -- Expand_Packed_Eq --
1847 ----------------------
1849 -- Handles expansion of "=" on packed array types
1851 procedure Expand_Packed_Eq (N : Node_Id) is
1852 Loc : constant Source_Ptr := Sloc (N);
1853 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1854 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1864 Convert_To_Actual_Subtype (L);
1865 Convert_To_Actual_Subtype (R);
1866 Ltyp := Underlying_Type (Etype (L));
1867 Rtyp := Underlying_Type (Etype (R));
1869 Convert_To_PAT_Type (L);
1870 Convert_To_PAT_Type (R);
1874 Make_Op_Multiply (Loc,
1876 Make_Attribute_Reference (Loc,
1877 Attribute_Name => Name_Length,
1878 Prefix => New_Occurrence_Of (Ltyp, Loc)),
1880 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1883 Make_Op_Multiply (Loc,
1885 Make_Attribute_Reference (Loc,
1886 Attribute_Name => Name_Length,
1887 Prefix => New_Occurrence_Of (Rtyp, Loc)),
1889 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1891 -- For the modular case, we transform the comparison to:
1893 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1895 -- where PAT is the packed array type. This works fine, since in the
1896 -- modular case we guarantee that the unused bits are always zeroes.
1897 -- We do have to compare the lengths because we could be comparing
1898 -- two different subtypes of the same base type.
1900 if Is_Modular_Integer_Type (PAT) then
1905 Left_Opnd => LLexpr,
1906 Right_Opnd => RLexpr),
1913 -- For the non-modular case, we call a runtime routine
1915 -- System.Bit_Ops.Bit_Eq
1916 -- (L'Address, L_Length, R'Address, R_Length)
1918 -- where PAT is the packed array type, and the lengths are the lengths
1919 -- in bits of the original packed arrays. This routine takes care of
1920 -- not comparing the unused bits in the last byte.
1924 Make_Function_Call (Loc,
1925 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1926 Parameter_Associations => New_List (
1927 Make_Attribute_Reference (Loc,
1928 Attribute_Name => Name_Address,
1933 Make_Attribute_Reference (Loc,
1934 Attribute_Name => Name_Address,
1940 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
1941 end Expand_Packed_Eq;
1943 -----------------------
1944 -- Expand_Packed_Not --
1945 -----------------------
1947 -- Handles expansion of "not" on packed array types
1949 procedure Expand_Packed_Not (N : Node_Id) is
1950 Loc : constant Source_Ptr := Sloc (N);
1951 Typ : constant Entity_Id := Etype (N);
1952 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
1959 Convert_To_Actual_Subtype (Opnd);
1960 Rtyp := Etype (Opnd);
1962 -- First an odd and silly test. We explicitly check for the case
1963 -- where the 'First of the component type is equal to the 'Last of
1964 -- this component type, and if this is the case, we make sure that
1965 -- constraint error is raised. The reason is that the NOT is bound
1966 -- to cause CE in this case, and we will not otherwise catch it.
1968 -- Believe it or not, this was reported as a bug. Note that nearly
1969 -- always, the test will evaluate statically to False, so the code
1970 -- will be statically removed, and no extra overhead caused.
1973 CT : constant Entity_Id := Component_Type (Rtyp);
1977 Make_Raise_Constraint_Error (Loc,
1981 Make_Attribute_Reference (Loc,
1982 Prefix => New_Occurrence_Of (CT, Loc),
1983 Attribute_Name => Name_First),
1986 Make_Attribute_Reference (Loc,
1987 Prefix => New_Occurrence_Of (CT, Loc),
1988 Attribute_Name => Name_Last))));
1991 -- Now that that silliness is taken care of, get packed array type
1993 Convert_To_PAT_Type (Opnd);
1994 PAT := Etype (Opnd);
1996 -- For the case where the packed array type is a modular type,
1997 -- not A expands simply into:
1999 -- rtyp!(PAT!(A) xor mask)
2001 -- where PAT is the packed array type, and mask is a mask of all
2002 -- one bits of length equal to the size of this packed type and
2003 -- rtyp is the actual subtype of the operand
2005 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2006 Set_Print_In_Hex (Lit);
2008 if not Is_Array_Type (PAT) then
2010 Unchecked_Convert_To (Rtyp,
2013 Right_Opnd => Lit)));
2015 -- For the array case, we insert the actions
2019 -- System.Bitops.Bit_Not
2021 -- Typ'Length * Typ'Component_Size;
2024 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2025 -- argument is the length of the operand in bits. Then we replace
2026 -- the expression by a reference to Result.
2030 Result_Ent : constant Entity_Id :=
2031 Make_Defining_Identifier (Loc,
2032 Chars => New_Internal_Name ('T'));
2035 Insert_Actions (N, New_List (
2037 Make_Object_Declaration (Loc,
2038 Defining_Identifier => Result_Ent,
2039 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2041 Make_Procedure_Call_Statement (Loc,
2042 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2043 Parameter_Associations => New_List (
2045 Make_Attribute_Reference (Loc,
2046 Attribute_Name => Name_Address,
2049 Make_Op_Multiply (Loc,
2051 Make_Attribute_Reference (Loc,
2054 (Etype (First_Index (Rtyp)), Loc),
2055 Attribute_Name => Name_Range_Length),
2057 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2059 Make_Attribute_Reference (Loc,
2060 Attribute_Name => Name_Address,
2061 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2064 New_Occurrence_Of (Result_Ent, Loc));
2068 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2070 end Expand_Packed_Not;
2072 -------------------------------------
2073 -- Involves_Packed_Array_Reference --
2074 -------------------------------------
2076 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2078 if Nkind (N) = N_Indexed_Component
2079 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2083 elsif Nkind (N) = N_Selected_Component then
2084 return Involves_Packed_Array_Reference (Prefix (N));
2089 end Involves_Packed_Array_Reference;
2091 ---------------------
2092 -- Make_Shift_Left --
2093 ---------------------
2095 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2099 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2103 Make_Op_Shift_Left (Sloc (N),
2106 Set_Shift_Count_OK (Nod, True);
2109 end Make_Shift_Left;
2111 ----------------------
2112 -- Make_Shift_Right --
2113 ----------------------
2115 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2119 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2123 Make_Op_Shift_Right (Sloc (N),
2126 Set_Shift_Count_OK (Nod, True);
2129 end Make_Shift_Right;
2131 -----------------------------
2132 -- RJ_Unchecked_Convert_To --
2133 -----------------------------
2135 function RJ_Unchecked_Convert_To
2140 Source_Typ : constant Entity_Id := Etype (Expr);
2141 Target_Typ : constant Entity_Id := Typ;
2143 Src : Node_Id := Expr;
2149 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2150 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2152 -- In the big endian case, if the lengths of the two types differ,
2153 -- then we must worry about possible left justification in the
2154 -- conversion, and avoiding that is what this is all about.
2156 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2158 -- First step, if the source type is not a discrete type, then we
2159 -- first convert to a modular type of the source length, since
2160 -- otherwise, on a big-endian machine, we get left-justification.
2162 if not Is_Discrete_Type (Source_Typ) then
2163 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2166 -- Next step. If the target is not a discrete type, then we first
2167 -- convert to a modular type of the target length, since
2168 -- otherwise, on a big-endian machine, we get left-justification.
2170 if not Is_Discrete_Type (Target_Typ) then
2171 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2175 -- And now we can do the final conversion to the target type
2177 return Unchecked_Convert_To (Target_Typ, Src);
2178 end RJ_Unchecked_Convert_To;
2180 ----------------------------------------------
2181 -- Setup_Enumeration_Packed_Array_Reference --
2182 ----------------------------------------------
2184 -- All we have to do here is to find the subscripts that correspond
2185 -- to the index positions that have non-standard enumeration types
2186 -- and insert a Pos attribute to get the proper subscript value.
2187 -- Finally the prefix must be uncheck converted to the corresponding
2188 -- packed array type.
2190 -- Note that the component type is unchanged, so we do not need to
2191 -- fiddle with the types (Gigi always automatically takes the packed
2192 -- array type if it is set, as it will be in this case).
2194 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2195 Pfx : constant Node_Id := Prefix (N);
2196 Typ : constant Entity_Id := Etype (N);
2197 Exprs : constant List_Id := Expressions (N);
2201 -- If the array is unconstrained, then we replace the array
2202 -- reference with its actual subtype. This actual subtype will
2203 -- have a packed array type with appropriate bounds.
2205 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2206 Convert_To_Actual_Subtype (Pfx);
2209 Expr := First (Exprs);
2210 while Present (Expr) loop
2212 Loc : constant Source_Ptr := Sloc (Expr);
2213 Expr_Typ : constant Entity_Id := Etype (Expr);
2216 if Is_Enumeration_Type (Expr_Typ)
2217 and then Has_Non_Standard_Rep (Expr_Typ)
2220 Make_Attribute_Reference (Loc,
2221 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2222 Attribute_Name => Name_Pos,
2223 Expressions => New_List (Relocate_Node (Expr))));
2224 Analyze_And_Resolve (Expr, Standard_Natural);
2232 Make_Indexed_Component (Sloc (N),
2234 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2235 Expressions => Exprs));
2237 Analyze_And_Resolve (N, Typ);
2239 end Setup_Enumeration_Packed_Array_Reference;
2241 -----------------------------------------
2242 -- Setup_Inline_Packed_Array_Reference --
2243 -----------------------------------------
2245 procedure Setup_Inline_Packed_Array_Reference
2248 Obj : in out Node_Id;
2250 Shift : out Node_Id)
2252 Loc : constant Source_Ptr := Sloc (N);
2260 Ctyp := Component_Type (Atyp);
2261 Csiz := Component_Size (Atyp);
2263 Convert_To_PAT_Type (Obj);
2266 Cmask := 2 ** Csiz - 1;
2268 if Is_Array_Type (PAT) then
2269 Otyp := Component_Type (PAT);
2270 Osiz := Esize (Otyp);
2275 -- In the case where the PAT is a modular type, we want the actual
2276 -- size in bits of the modular value we use. This is neither the
2277 -- Object_Size nor the Value_Size, either of which may have been
2278 -- reset to strange values, but rather the minimum size. Note that
2279 -- since this is a modular type with full range, the issue of
2280 -- biased representation does not arise.
2282 Osiz := UI_From_Int (Minimum_Size (Otyp));
2285 Compute_Linear_Subscript (Atyp, N, Shift);
2287 -- If the component size is not 1, then the subscript must be
2288 -- multiplied by the component size to get the shift count.
2292 Make_Op_Multiply (Loc,
2293 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2294 Right_Opnd => Shift);
2297 -- If we have the array case, then this shift count must be broken
2298 -- down into a byte subscript, and a shift within the byte.
2300 if Is_Array_Type (PAT) then
2303 New_Shift : Node_Id;
2306 -- We must analyze shift, since we will duplicate it
2308 Set_Parent (Shift, N);
2310 (Shift, Standard_Integer, Suppress => All_Checks);
2312 -- The shift count within the word is
2317 Left_Opnd => Duplicate_Subexpr (Shift),
2318 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2320 -- The subscript to be used on the PAT array is
2324 Make_Indexed_Component (Loc,
2326 Expressions => New_List (
2327 Make_Op_Divide (Loc,
2328 Left_Opnd => Duplicate_Subexpr (Shift),
2329 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2334 -- For the modular integer case, the object to be manipulated is
2335 -- the entire array, so Obj is unchanged. Note that we will reset
2336 -- its type to PAT before returning to the caller.
2342 -- The one remaining step is to modify the shift count for the
2343 -- big-endian case. Consider the following example in a byte:
2345 -- xxxxxxxx bits of byte
2346 -- vvvvvvvv bits of value
2347 -- 33221100 little-endian numbering
2348 -- 00112233 big-endian numbering
2350 -- Here we have the case of 2-bit fields
2352 -- For the little-endian case, we already have the proper shift
2353 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2355 -- For the big endian case, we have to adjust the shift count,
2356 -- computing it as (N - F) - shift, where N is the number of bits
2357 -- in an element of the array used to implement the packed array,
2358 -- F is the number of bits in a source level array element, and
2359 -- shift is the count so far computed.
2361 if Bytes_Big_Endian then
2363 Make_Op_Subtract (Loc,
2364 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2365 Right_Opnd => Shift);
2368 Set_Parent (Shift, N);
2369 Set_Parent (Obj, N);
2370 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2371 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2373 -- Make sure final type of object is the appropriate packed type
2375 Set_Etype (Obj, Otyp);
2377 end Setup_Inline_Packed_Array_Reference;