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 Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
457 -- There are two versions of the Set routines, the ones used when the
458 -- object is known to be sufficiently well aligned given the number of
459 -- bits, and the ones used when the object is not known to be aligned.
460 -- This routine is used to determine which set to use. Obj is a reference
461 -- to the object, and Csiz is the component size of the packed array.
462 -- True is returned if the alignment of object is known to be sufficient,
463 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
466 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
467 -- Build a left shift node, checking for the case of a shift count of zero
469 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
470 -- Build a right shift node, checking for the case of a shift count of zero
472 function RJ_Unchecked_Convert_To
476 -- The packed array code does unchecked conversions which in some cases
477 -- may involve non-discrete types with differing sizes. The semantics of
478 -- such conversions is potentially endian dependent, and the effect we
479 -- want here for such a conversion is to do the conversion in size as
480 -- though numeric items are involved, and we extend or truncate on the
481 -- left side. This happens naturally in the little-endian case, but in
482 -- the big endian case we can get left justification, when what we want
483 -- is right justification. This routine does the unchecked conversion in
484 -- a stepwise manner to ensure that it gives the expected result. Hence
485 -- the name (RJ = Right justified). The parameters Typ and Expr are as
486 -- for the case of a normal Unchecked_Convert_To call.
488 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
489 -- This routine is called in the Get and Set case for arrays that are
490 -- packed but not bit-packed, meaning that they have at least one
491 -- subscript that is of an enumeration type with a non-standard
492 -- representation. This routine modifies the given node to properly
493 -- reference the corresponding packed array type.
495 procedure Setup_Inline_Packed_Array_Reference
498 Obj : in out Node_Id;
500 Shift : out Node_Id);
501 -- This procedure performs common processing on the N_Indexed_Component
502 -- parameter given as N, whose prefix is a reference to a packed array.
503 -- This is used for the get and set when the component size is 1,2,4
504 -- or for other component sizes when the packed array type is a modular
505 -- type (i.e. the cases that are handled with inline code).
509 -- N is the N_Indexed_Component node for the packed array reference
511 -- Atyp is the constrained array type (the actual subtype has been
512 -- computed if necessary to obtain the constraints, but this is still
513 -- the original array type, not the Packed_Array_Type value).
515 -- Obj is the object which is to be indexed. It is always of type Atyp.
519 -- Obj is the object containing the desired bit field. It is of type
520 -- Unsigned or Long_Long_Unsigned, and is either the entire value,
521 -- for the small static case, or the proper selected byte from the
522 -- array in the large or dynamic case. This node is analyzed and
523 -- resolved on return.
525 -- Shift is a node representing the shift count to be used in the
526 -- rotate right instruction that positions the field for access.
527 -- This node is analyzed and resolved on return.
529 -- Cmask is a mask corresponding to the width of the component field.
530 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
532 -- Note: in some cases the call to this routine may generate actions
533 -- (for handling multi-use references and the generation of the packed
534 -- array type on the fly). Such actions are inserted into the tree
535 -- directly using Insert_Action.
537 ------------------------------
538 -- Compute_Linear_Subcsript --
539 ------------------------------
541 procedure Compute_Linear_Subscript
544 Subscr : out Node_Id)
546 Loc : constant Source_Ptr := Sloc (N);
555 -- Loop through dimensions
557 Indx := First_Index (Atyp);
558 Oldsub := First (Expressions (N));
560 while Present (Indx) loop
561 Styp := Etype (Indx);
562 Newsub := Relocate_Node (Oldsub);
564 -- Get expression for the subscript value. First, if Do_Range_Check
565 -- is set on a subscript, then we must do a range check against the
566 -- original bounds (not the bounds of the packed array type). We do
567 -- this by introducing a subtype conversion.
569 if Do_Range_Check (Newsub)
570 and then Etype (Newsub) /= Styp
572 Newsub := Convert_To (Styp, Newsub);
575 -- Now evolve the expression for the subscript. First convert
576 -- the subscript to be zero based and of an integer type.
578 -- Case of integer type, where we just subtract to get lower bound
580 if Is_Integer_Type (Styp) then
582 -- If length of integer type is smaller than standard integer,
583 -- then we convert to integer first, then do the subtract
585 -- Integer (subscript) - Integer (Styp'First)
587 if Esize (Styp) < Esize (Standard_Integer) then
589 Make_Op_Subtract (Loc,
590 Left_Opnd => Convert_To (Standard_Integer, Newsub),
592 Convert_To (Standard_Integer,
593 Make_Attribute_Reference (Loc,
594 Prefix => New_Occurrence_Of (Styp, Loc),
595 Attribute_Name => Name_First)));
597 -- For larger integer types, subtract first, then convert to
598 -- integer, this deals with strange long long integer bounds.
600 -- Integer (subscript - Styp'First)
604 Convert_To (Standard_Integer,
605 Make_Op_Subtract (Loc,
608 Make_Attribute_Reference (Loc,
609 Prefix => New_Occurrence_Of (Styp, Loc),
610 Attribute_Name => Name_First)));
613 -- For the enumeration case, we have to use 'Pos to get the value
614 -- to work with before subtracting the lower bound.
616 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
618 -- This is not quite right for bizarre cases where the size of the
619 -- enumeration type is > Integer'Size bits due to rep clause ???
622 pragma Assert (Is_Enumeration_Type (Styp));
625 Make_Op_Subtract (Loc,
626 Left_Opnd => Convert_To (Standard_Integer,
627 Make_Attribute_Reference (Loc,
628 Prefix => New_Occurrence_Of (Styp, Loc),
629 Attribute_Name => Name_Pos,
630 Expressions => New_List (Newsub))),
633 Convert_To (Standard_Integer,
634 Make_Attribute_Reference (Loc,
635 Prefix => New_Occurrence_Of (Styp, Loc),
636 Attribute_Name => Name_Pos,
637 Expressions => New_List (
638 Make_Attribute_Reference (Loc,
639 Prefix => New_Occurrence_Of (Styp, Loc),
640 Attribute_Name => Name_First)))));
643 Set_Paren_Count (Newsub, 1);
645 -- For the first subscript, we just copy that subscript value
650 -- Otherwise, we must multiply what we already have by the current
651 -- stride and then add in the new value to the evolving subscript.
657 Make_Op_Multiply (Loc,
660 Make_Attribute_Reference (Loc,
661 Attribute_Name => Name_Range_Length,
662 Prefix => New_Occurrence_Of (Styp, Loc))),
663 Right_Opnd => Newsub);
666 -- Move to next subscript
671 end Compute_Linear_Subscript;
673 -------------------------
674 -- Convert_To_PAT_Type --
675 -------------------------
677 -- The PAT is always obtained from the actual subtype
679 procedure Convert_To_PAT_Type (Aexp : Entity_Id) is
683 Convert_To_Actual_Subtype (Aexp);
684 Act_ST := Underlying_Type (Etype (Aexp));
685 Create_Packed_Array_Type (Act_ST);
687 -- Just replace the etype with the packed array type. This works
688 -- because the expression will not be further analyzed, and Gigi
689 -- considers the two types equivalent in any case.
691 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
692 end Convert_To_PAT_Type;
694 ------------------------------
695 -- Create_Packed_Array_Type --
696 ------------------------------
698 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
699 Loc : constant Source_Ptr := Sloc (Typ);
700 Ctyp : constant Entity_Id := Component_Type (Typ);
701 Csize : constant Uint := Component_Size (Typ);
716 procedure Install_PAT;
717 -- This procedure is called with Decl set to the declaration for the
718 -- packed array type. It creates the type and installs it as required.
720 procedure Set_PB_Type;
721 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
722 -- requirements (see documentation in the spec of this package).
728 procedure Install_PAT is
729 Pushed_Scope : Boolean := False;
732 -- We do not want to put the declaration we have created in the tree
733 -- since it is often hard, and sometimes impossible to find a proper
734 -- place for it (the impossible case arises for a packed array type
735 -- with bounds depending on the discriminant, a declaration cannot
736 -- be put inside the record, and the reference to the discriminant
737 -- cannot be outside the record).
739 -- The solution is to analyze the declaration while temporarily
740 -- attached to the tree at an appropriate point, and then we install
741 -- the resulting type as an Itype in the packed array type field of
742 -- the original type, so that no explicit declaration is required.
744 -- Note: the packed type is created in the scope of its parent
745 -- type. There are at least some cases where the current scope
746 -- is deeper, and so when this is the case, we temporarily reset
747 -- the scope for the definition. This is clearly safe, since the
748 -- first use of the packed array type will be the implicit
749 -- reference from the corresponding unpacked type when it is
752 if Is_Itype (Typ) then
753 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
755 Set_Parent (Decl, Declaration_Node (Typ));
758 if Scope (Typ) /= Current_Scope then
759 New_Scope (Scope (Typ));
760 Pushed_Scope := True;
763 Set_Is_Itype (PAT, True);
764 Set_Is_Packed_Array_Type (PAT, True);
765 Analyze (Decl, Suppress => All_Checks);
771 -- Set Esize and RM_Size to the actual size of the packed object
772 -- Do not reset RM_Size if already set, as happens in the case
775 Set_Esize (PAT, Esiz);
777 if Unknown_RM_Size (PAT) then
778 Set_RM_Size (PAT, Esiz);
781 -- Set remaining fields of packed array type
783 Init_Alignment (PAT);
784 Set_Parent (PAT, Empty);
785 Set_Packed_Array_Type (Typ, PAT);
786 Set_Associated_Node_For_Itype (PAT, Typ);
788 -- We definitely do not want to delay freezing for packed array
789 -- types. This is of particular importance for the itypes that
790 -- are generated for record components depending on discriminants
791 -- where there is no place to put the freeze node.
793 Set_Has_Delayed_Freeze (PAT, False);
794 Set_Has_Delayed_Freeze (Etype (PAT), False);
801 procedure Set_PB_Type is
803 -- If the user has specified an explicit alignment for the
804 -- component, take it into account.
806 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
807 or else Component_Alignment (Typ) = Calign_Storage_Unit
809 PB_Type := RTE (RE_Packed_Bytes1);
811 elsif Csize mod 4 /= 0 then
812 PB_Type := RTE (RE_Packed_Bytes2);
815 PB_Type := RTE (RE_Packed_Bytes4);
819 -- Start of processing for Create_Packed_Array_Type
822 -- If we already have a packed array type, nothing to do
824 if Present (Packed_Array_Type (Typ)) then
828 -- If our immediate ancestor subtype is constrained, and it already
829 -- has a packed array type, then just share the same type, since the
830 -- bounds must be the same.
832 if Ekind (Typ) = E_Array_Subtype then
833 Ancest := Ancestor_Subtype (Typ);
836 and then Is_Constrained (Ancest)
837 and then Present (Packed_Array_Type (Ancest))
839 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
844 -- We preset the result type size from the size of the original array
845 -- type, since this size clearly belongs to the packed array type. The
846 -- size of the conceptual unpacked type is always set to unknown.
850 -- Case of an array where at least one index is of an enumeration
851 -- type with a non-standard representation, but the component size
852 -- is not appropriate for bit packing. This is the case where we
853 -- have Is_Packed set (we would never be in this unit otherwise),
854 -- but Is_Bit_Packed_Array is false.
856 -- Note that if the component size is appropriate for bit packing,
857 -- then the circuit for the computation of the subscript properly
858 -- deals with the non-standard enumeration type case by taking the
861 if not Is_Bit_Packed_Array (Typ) then
863 -- Here we build a declaration:
865 -- type tttP is array (index1, index2, ...) of component_type
867 -- where index1, index2, are the index types. These are the same
868 -- as the index types of the original array, except for the non-
869 -- standard representation enumeration type case, where we have
872 -- For the unconstrained array case, we use
876 -- For the constrained case, we use
878 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
879 -- Enum_Type'Pos (Enum_Type'Last);
882 Make_Defining_Identifier (Loc,
883 Chars => New_External_Name (Chars (Typ), 'P'));
885 Set_Packed_Array_Type (Typ, PAT);
888 Indexes : List_Id := New_List;
890 Indx_Typ : Entity_Id;
895 Indx := First_Index (Typ);
897 while Present (Indx) loop
898 Indx_Typ := Etype (Indx);
900 Enum_Case := Is_Enumeration_Type (Indx_Typ)
901 and then Has_Non_Standard_Rep (Indx_Typ);
903 -- Unconstrained case
905 if not Is_Constrained (Typ) then
907 Indx_Typ := Standard_Natural;
910 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
915 if not Enum_Case then
916 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
920 Make_Subtype_Indication (Loc,
922 New_Occurrence_Of (Standard_Natural, Loc),
924 Make_Range_Constraint (Loc,
928 Make_Attribute_Reference (Loc,
930 New_Occurrence_Of (Indx_Typ, Loc),
931 Attribute_Name => Name_Pos,
932 Expressions => New_List (
933 Make_Attribute_Reference (Loc,
935 New_Occurrence_Of (Indx_Typ, Loc),
936 Attribute_Name => Name_First))),
939 Make_Attribute_Reference (Loc,
941 New_Occurrence_Of (Indx_Typ, Loc),
942 Attribute_Name => Name_Pos,
943 Expressions => New_List (
944 Make_Attribute_Reference (Loc,
946 New_Occurrence_Of (Indx_Typ, Loc),
947 Attribute_Name => Name_Last)))))));
955 if not Is_Constrained (Typ) then
957 Make_Unconstrained_Array_Definition (Loc,
958 Subtype_Marks => Indexes,
959 Subtype_Indication =>
960 New_Occurrence_Of (Ctyp, Loc));
964 Make_Constrained_Array_Definition (Loc,
965 Discrete_Subtype_Definitions => Indexes,
966 Subtype_Indication =>
967 New_Occurrence_Of (Ctyp, Loc));
971 Make_Full_Type_Declaration (Loc,
972 Defining_Identifier => PAT,
973 Type_Definition => Typedef);
979 -- Case of bit-packing required for unconstrained array. We simply
980 -- use Packed_Bytes{1,2,4} as appropriate, and we do not need to
981 -- construct a special packed array type.
983 elsif not Is_Constrained (Typ) then
985 Set_Packed_Array_Type (Typ, PB_Type);
986 Set_Is_Packed_Array_Type (Packed_Array_Type (Typ), True);
989 -- Remaining code is for the case of bit-packing for constrained array
991 -- The name of the packed array subtype is
995 -- where sss is the component size in bits and ttt is the name of
996 -- the parent packed type.
1000 Make_Defining_Identifier (Loc,
1001 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1003 Set_Packed_Array_Type (Typ, PAT);
1005 -- Build an expression for the length of the array in bits.
1006 -- This is the product of the length of each of the dimensions
1012 Len_Expr := Empty; -- suppress junk warning
1016 Make_Attribute_Reference (Loc,
1017 Attribute_Name => Name_Length,
1018 Prefix => New_Occurrence_Of (Typ, Loc),
1019 Expressions => New_List (
1020 Make_Integer_Literal (Loc, J)));
1023 Len_Expr := Len_Dim;
1027 Make_Op_Multiply (Loc,
1028 Left_Opnd => Len_Expr,
1029 Right_Opnd => Len_Dim);
1033 exit when J > Number_Dimensions (Typ);
1037 -- Temporarily attach the length expression to the tree and analyze
1038 -- and resolve it, so that we can test its value. We assume that the
1039 -- total length fits in type Integer.
1041 Set_Parent (Len_Expr, Typ);
1042 Analyze_And_Resolve (Len_Expr, Standard_Integer);
1044 -- Use a modular type if possible. We can do this if we are we
1045 -- have static bounds, and the length is small enough, and the
1046 -- length is not zero. We exclude the zero length case because the
1047 -- size of things is always at least one, and the zero length object
1048 -- would have an anomous size
1050 if Compile_Time_Known_Value (Len_Expr) then
1051 Len_Bits := Expr_Value (Len_Expr) * Csize;
1053 -- We normally consider small enough to mean no larger than the
1054 -- value of System_Max_Binary_Modulus_Power, except that in
1055 -- No_Run_Time mode, we use the Word Size on machines for
1056 -- which double length shifts are not generated in line.
1060 (Len_Bits <= System_Word_Size
1061 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1062 and then (not No_Run_Time
1064 Long_Shifts_Inlined_On_Target)))
1066 -- We can use the modular type, it has the form:
1068 -- subtype tttPn is btyp
1069 -- range 0 .. 2 ** (Esize (Typ) * Csize) - 1;
1071 -- Here Siz is 1, 2 or 4, as computed above, and btyp is either
1072 -- Unsigned or Long_Long_Unsigned depending on the length.
1074 if Len_Bits <= Standard_Integer_Size then
1075 Btyp := RTE (RE_Unsigned);
1077 Btyp := RTE (RE_Long_Long_Unsigned);
1080 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1081 Set_Print_In_Hex (Lit);
1084 Make_Subtype_Declaration (Loc,
1085 Defining_Identifier => PAT,
1086 Subtype_Indication =>
1087 Make_Subtype_Indication (Loc,
1088 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1091 Make_Range_Constraint (Loc,
1095 Make_Integer_Literal (Loc, 0),
1096 High_Bound => Lit))));
1098 if Esiz = Uint_0 then
1107 -- Could not use a modular type, for all other cases, we build
1108 -- a packed array subtype:
1111 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1113 -- Bits is the length of the array in bits.
1120 Make_Op_Multiply (Loc,
1122 Make_Integer_Literal (Loc, Csize),
1123 Right_Opnd => Len_Expr),
1126 Make_Integer_Literal (Loc, 7));
1128 Set_Paren_Count (Bits_U1, 1);
1131 Make_Op_Subtract (Loc,
1133 Make_Op_Divide (Loc,
1134 Left_Opnd => Bits_U1,
1135 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1136 Right_Opnd => Make_Integer_Literal (Loc, 1));
1139 Make_Subtype_Declaration (Loc,
1140 Defining_Identifier => PAT,
1141 Subtype_Indication =>
1142 Make_Subtype_Indication (Loc,
1143 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1146 Make_Index_Or_Discriminant_Constraint (Loc,
1147 Constraints => New_List (
1150 Make_Integer_Literal (Loc, 0),
1151 High_Bound => PAT_High)))));
1155 end Create_Packed_Array_Type;
1157 -----------------------------------
1158 -- Expand_Bit_Packed_Element_Set --
1159 -----------------------------------
1161 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1162 Loc : constant Source_Ptr := Sloc (N);
1163 Lhs : constant Node_Id := Name (N);
1165 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1166 -- Used to preserve assignment OK status when assignment is rewritten
1168 Rhs : Node_Id := Expression (N);
1169 -- Initially Rhs is the right hand side value, it will be replaced
1170 -- later by an appropriate unchecked conversion for the assignment.
1183 Rhs_Val_Known : Boolean;
1185 -- If the value of the right hand side as an integer constant is
1186 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1187 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1188 -- the Rhs_Val is undefined.
1191 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1193 Obj := Relocate_Node (Prefix (Lhs));
1194 Convert_To_Actual_Subtype (Obj);
1195 Atyp := Etype (Obj);
1196 PAT := Packed_Array_Type (Atyp);
1197 Ctyp := Component_Type (Atyp);
1198 Csiz := UI_To_Int (Component_Size (Atyp));
1200 -- We convert the right hand side to the proper subtype to ensure
1201 -- that an appropriate range check is made (since the normal range
1202 -- check from assignment will be lost in the transformations). This
1203 -- conversion is analyzed immediately so that subsequent processing
1204 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1206 Rhs := Convert_To (Ctyp, Rhs);
1207 Set_Parent (Rhs, N);
1208 Analyze_And_Resolve (Rhs, Ctyp);
1210 -- Case of component size 1,2,4 or any component size for the modular
1211 -- case. These are the cases for which we can inline the code.
1213 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1214 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1216 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1218 -- The statement to be generated is:
1220 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1222 -- where mask1 is obtained by shifting Cmask left Shift bits
1223 -- and then complementing the result.
1225 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1227 -- the "or ..." is omitted if rhs is constant and all 0 bits
1229 -- rhs is converted to the appropriate type.
1231 -- The result is converted back to the array type, since
1232 -- otherwise we lose knowledge of the packed nature.
1234 -- Determine if right side is all 0 bits or all 1 bits
1236 if Compile_Time_Known_Value (Rhs) then
1237 Rhs_Val := Expr_Rep_Value (Rhs);
1238 Rhs_Val_Known := True;
1240 -- The following test catches the case of an unchecked conversion
1241 -- of an integer literal. This results from optimizing aggregates
1244 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1245 and then Compile_Time_Known_Value (Expression (Rhs))
1247 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1248 Rhs_Val_Known := True;
1252 Rhs_Val_Known := False;
1255 -- Some special checks for the case where the right hand value
1256 -- is known at compile time. Basically we have to take care of
1257 -- the implicit conversion to the subtype of the component object.
1259 if Rhs_Val_Known then
1261 -- If we have a biased component type then we must manually do
1262 -- the biasing, since we are taking responsibility in this case
1263 -- for constructing the exact bit pattern to be used.
1265 if Has_Biased_Representation (Ctyp) then
1266 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1269 -- For a negative value, we manually convert the twos complement
1270 -- value to a corresponding unsigned value, so that the proper
1271 -- field width is maintained. If we did not do this, we would
1272 -- get too many leading sign bits later on.
1275 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1279 New_Lhs := Duplicate_Subexpr (Obj, True);
1280 New_Rhs := Duplicate_Subexpr (Obj);
1282 -- First we deal with the "and"
1284 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1290 if Compile_Time_Known_Value (Shift) then
1292 Make_Integer_Literal (Loc,
1293 Modulus (Etype (Obj)) - 1 -
1294 (Cmask * (2 ** Expr_Value (Shift))));
1295 Set_Print_In_Hex (Mask1);
1298 Lit := Make_Integer_Literal (Loc, Cmask);
1299 Set_Print_In_Hex (Lit);
1302 Right_Opnd => Make_Shift_Left (Lit, Shift));
1307 Left_Opnd => New_Rhs,
1308 Right_Opnd => Mask1);
1312 -- Then deal with the "or"
1314 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1318 procedure Fixup_Rhs;
1319 -- Adjust Rhs by bias if biased representation for components
1320 -- or remove extraneous high order sign bits if signed.
1322 procedure Fixup_Rhs is
1323 Etyp : constant Entity_Id := Etype (Rhs);
1326 -- For biased case, do the required biasing by simply
1327 -- converting to the biased subtype (the conversion
1328 -- will generate the required bias).
1330 if Has_Biased_Representation (Ctyp) then
1331 Rhs := Convert_To (Ctyp, Rhs);
1333 -- For a signed integer type that is not biased, generate
1334 -- a conversion to unsigned to strip high order sign bits.
1336 elsif Is_Signed_Integer_Type (Ctyp) then
1337 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1340 -- Set Etype, since it can be referenced before the
1341 -- node is completely analyzed.
1343 Set_Etype (Rhs, Etyp);
1345 -- We now need to do an unchecked conversion of the
1346 -- result to the target type, but it is important that
1347 -- this conversion be a right justified conversion and
1348 -- not a left justified conversion.
1350 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1356 and then Compile_Time_Known_Value (Shift)
1359 Make_Integer_Literal (Loc,
1360 Rhs_Val * (2 ** Expr_Value (Shift)));
1361 Set_Print_In_Hex (Or_Rhs);
1364 -- We have to convert the right hand side to Etype (Obj).
1365 -- A special case case arises if what we have now is a Val
1366 -- attribute reference whose expression type is Etype (Obj).
1367 -- This happens for assignments of fields from the same
1368 -- array. In this case we get the required right hand side
1369 -- by simply removing the inner attribute reference.
1371 if Nkind (Rhs) = N_Attribute_Reference
1372 and then Attribute_Name (Rhs) = Name_Val
1373 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1375 Rhs := Relocate_Node (First (Expressions (Rhs)));
1378 -- If the value of the right hand side is a known integer
1379 -- value, then just replace it by an untyped constant,
1380 -- which will be properly retyped when we analyze and
1381 -- resolve the expression.
1383 elsif Rhs_Val_Known then
1385 -- Note that Rhs_Val has already been normalized to
1386 -- be an unsigned value with the proper number of bits.
1389 Make_Integer_Literal (Loc, Rhs_Val);
1391 -- Otherwise we need an unchecked conversion
1397 Or_Rhs := Make_Shift_Left (Rhs, Shift);
1400 if Nkind (New_Rhs) = N_Op_And then
1401 Set_Paren_Count (New_Rhs, 1);
1406 Left_Opnd => New_Rhs,
1407 Right_Opnd => Or_Rhs);
1411 -- Now do the rewrite
1414 Make_Assignment_Statement (Loc,
1417 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1418 Set_Assignment_OK (Name (N), Ass_OK);
1420 -- All other component sizes for non-modular case
1425 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1427 -- where Subscr is the computed linear subscript.
1430 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1436 -- Acquire proper Set entity. We use the aligned or unaligned
1437 -- case as appropriate.
1439 if Known_Aligned_Enough (Obj, Csiz) then
1440 Set_nn := RTE (Set_Id (Csiz));
1442 Set_nn := RTE (SetU_Id (Csiz));
1445 -- Now generate the set reference
1447 Obj := Relocate_Node (Prefix (Lhs));
1448 Convert_To_Actual_Subtype (Obj);
1449 Atyp := Etype (Obj);
1450 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1453 Make_Procedure_Call_Statement (Loc,
1454 Name => New_Occurrence_Of (Set_nn, Loc),
1455 Parameter_Associations => New_List (
1456 Make_Attribute_Reference (Loc,
1457 Attribute_Name => Name_Address,
1460 Unchecked_Convert_To (Bits_nn,
1461 Convert_To (Ctyp, Rhs)))));
1466 Analyze (N, Suppress => All_Checks);
1467 end Expand_Bit_Packed_Element_Set;
1469 -------------------------------------
1470 -- Expand_Packed_Address_Reference --
1471 -------------------------------------
1473 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1474 Loc : constant Source_Ptr := Sloc (N);
1486 -- We build up an expression serially that has the form
1488 -- outer_object'Address
1489 -- + (linear-subscript * component_size for each array reference
1490 -- + field'Bit_Position for each record field
1492 -- + ...) / Storage_Unit;
1494 -- Some additional conversions are required to deal with the addition
1495 -- operation, which is not normally visible to generated code.
1498 Ploc := Sloc (Pref);
1500 if Nkind (Pref) = N_Indexed_Component then
1501 Convert_To_Actual_Subtype (Prefix (Pref));
1502 Atyp := Etype (Prefix (Pref));
1503 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1506 Make_Op_Multiply (Ploc,
1507 Left_Opnd => Subscr,
1509 Make_Attribute_Reference (Ploc,
1510 Prefix => New_Occurrence_Of (Atyp, Ploc),
1511 Attribute_Name => Name_Component_Size));
1513 elsif Nkind (Pref) = N_Selected_Component then
1515 Make_Attribute_Reference (Ploc,
1516 Prefix => Selector_Name (Pref),
1517 Attribute_Name => Name_Bit_Position);
1523 Term := Convert_To (RTE (RE_Integer_Address), Term);
1532 Right_Opnd => Term);
1535 Pref := Prefix (Pref);
1539 Unchecked_Convert_To (RTE (RE_Address),
1542 Unchecked_Convert_To (RTE (RE_Integer_Address),
1543 Make_Attribute_Reference (Loc,
1545 Attribute_Name => Name_Address)),
1548 Make_Op_Divide (Loc,
1551 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1553 Analyze_And_Resolve (N, RTE (RE_Address));
1554 end Expand_Packed_Address_Reference;
1556 ------------------------------------
1557 -- Expand_Packed_Boolean_Operator --
1558 ------------------------------------
1560 -- This routine expands "a op b" for the packed cases
1562 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1563 Loc : constant Source_Ptr := Sloc (N);
1564 Typ : constant Entity_Id := Etype (N);
1565 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1566 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1573 Convert_To_Actual_Subtype (L);
1574 Convert_To_Actual_Subtype (R);
1576 Ensure_Defined (Etype (L), N);
1577 Ensure_Defined (Etype (R), N);
1579 Apply_Length_Check (R, Etype (L));
1584 -- First an odd and silly test. We explicitly check for the XOR
1585 -- case where the component type is True .. True, since this will
1586 -- raise constraint error. A special check is required since CE
1587 -- will not be required other wise (cf Expand_Packed_Not).
1589 -- No such check is required for AND and OR, since for both these
1590 -- cases False op False = False, and True op True = True.
1592 if Nkind (N) = N_Op_Xor then
1594 CT : constant Entity_Id := Component_Type (Rtyp);
1595 BT : constant Entity_Id := Base_Type (CT);
1599 Make_Raise_Constraint_Error (Loc,
1605 Make_Attribute_Reference (Loc,
1606 Prefix => New_Occurrence_Of (CT, Loc),
1607 Attribute_Name => Name_First),
1611 New_Occurrence_Of (Standard_True, Loc))),
1616 Make_Attribute_Reference (Loc,
1617 Prefix => New_Occurrence_Of (CT, Loc),
1618 Attribute_Name => Name_Last),
1622 New_Occurrence_Of (Standard_True, Loc))))));
1626 -- Now that that silliness is taken care of, get packed array type
1628 Convert_To_PAT_Type (L);
1629 Convert_To_PAT_Type (R);
1633 -- For the modular case, we expand a op b into
1635 -- rtyp!(pat!(a) op pat!(b))
1637 -- where rtyp is the Etype of the left operand. Note that we do not
1638 -- convert to the base type, since this would be unconstrained, and
1639 -- hence not have a corresponding packed array type set.
1641 if Is_Modular_Integer_Type (PAT) then
1646 if Nkind (N) = N_Op_And then
1647 P := Make_Op_And (Loc, L, R);
1649 elsif Nkind (N) = N_Op_Or then
1650 P := Make_Op_Or (Loc, L, R);
1652 else -- Nkind (N) = N_Op_Xor
1653 P := Make_Op_Xor (Loc, L, R);
1656 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1659 -- For the array case, we insert the actions
1663 -- System.Bitops.Bit_And/Or/Xor
1665 -- Ltype'Length * Ltype'Component_Size;
1667 -- Rtype'Length * Rtype'Component_Size
1670 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1671 -- the second argument and fourth arguments are the lengths of the
1672 -- operands in bits. Then we replace the expression by a reference
1677 Result_Ent : constant Entity_Id :=
1678 Make_Defining_Identifier (Loc,
1679 Chars => New_Internal_Name ('T'));
1684 if Nkind (N) = N_Op_And then
1687 elsif Nkind (N) = N_Op_Or then
1690 else -- Nkind (N) = N_Op_Xor
1694 Insert_Actions (N, New_List (
1696 Make_Object_Declaration (Loc,
1697 Defining_Identifier => Result_Ent,
1698 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1700 Make_Procedure_Call_Statement (Loc,
1701 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1702 Parameter_Associations => New_List (
1704 Make_Attribute_Reference (Loc,
1705 Attribute_Name => Name_Address,
1708 Make_Op_Multiply (Loc,
1710 Make_Attribute_Reference (Loc,
1713 (Etype (First_Index (Ltyp)), Loc),
1714 Attribute_Name => Name_Range_Length),
1716 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1718 Make_Attribute_Reference (Loc,
1719 Attribute_Name => Name_Address,
1722 Make_Op_Multiply (Loc,
1724 Make_Attribute_Reference (Loc,
1727 (Etype (First_Index (Rtyp)), Loc),
1728 Attribute_Name => Name_Range_Length),
1730 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1732 Make_Attribute_Reference (Loc,
1733 Attribute_Name => Name_Address,
1734 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1737 New_Occurrence_Of (Result_Ent, Loc));
1741 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1742 end Expand_Packed_Boolean_Operator;
1744 -------------------------------------
1745 -- Expand_Packed_Element_Reference --
1746 -------------------------------------
1748 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1749 Loc : constant Source_Ptr := Sloc (N);
1761 -- If not bit packed, we have the enumeration case, which is easily
1762 -- dealt with (just adjust the subscripts of the indexed component)
1764 -- Note: this leaves the result as an indexed component, which is
1765 -- still a variable, so can be used in the assignment case, as is
1766 -- required in the enumeration case.
1768 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1769 Setup_Enumeration_Packed_Array_Reference (N);
1773 -- Remaining processing is for the bit-packed case.
1775 Obj := Relocate_Node (Prefix (N));
1776 Convert_To_Actual_Subtype (Obj);
1777 Atyp := Etype (Obj);
1778 PAT := Packed_Array_Type (Atyp);
1779 Ctyp := Component_Type (Atyp);
1780 Csiz := UI_To_Int (Component_Size (Atyp));
1782 -- Case of component size 1,2,4 or any component size for the modular
1783 -- case. These are the cases for which we can inline the code.
1785 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1786 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1788 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1789 Lit := Make_Integer_Literal (Loc, Cmask);
1790 Set_Print_In_Hex (Lit);
1792 -- We generate a shift right to position the field, followed by a
1793 -- masking operation to extract the bit field, and we finally do an
1794 -- unchecked conversion to convert the result to the required target.
1796 -- Note that the unchecked conversion automatically deals with the
1797 -- bias if we are dealing with a biased representation. What will
1798 -- happen is that we temporarily generate the biased representation,
1799 -- but almost immediately that will be converted to the original
1800 -- unbiased component type, and the bias will disappear.
1804 Left_Opnd => Make_Shift_Right (Obj, Shift),
1807 Analyze_And_Resolve (Arg);
1810 RJ_Unchecked_Convert_To (Ctyp, Arg));
1812 -- All other component sizes for non-modular case
1817 -- Component_Type!(Get_nn (Arr'address, Subscr))
1819 -- where Subscr is the computed linear subscript.
1826 -- Acquire proper Get entity. We use the aligned or unaligned
1827 -- case as appropriate.
1829 if Known_Aligned_Enough (Obj, Csiz) then
1830 Get_nn := RTE (Get_Id (Csiz));
1832 Get_nn := RTE (GetU_Id (Csiz));
1835 -- Now generate the get reference
1837 Compute_Linear_Subscript (Atyp, N, Subscr);
1840 Unchecked_Convert_To (Ctyp,
1841 Make_Function_Call (Loc,
1842 Name => New_Occurrence_Of (Get_nn, Loc),
1843 Parameter_Associations => New_List (
1844 Make_Attribute_Reference (Loc,
1845 Attribute_Name => Name_Address,
1851 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1853 end Expand_Packed_Element_Reference;
1855 ----------------------
1856 -- Expand_Packed_Eq --
1857 ----------------------
1859 -- Handles expansion of "=" on packed array types
1861 procedure Expand_Packed_Eq (N : Node_Id) is
1862 Loc : constant Source_Ptr := Sloc (N);
1863 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1864 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1874 Convert_To_Actual_Subtype (L);
1875 Convert_To_Actual_Subtype (R);
1876 Ltyp := Underlying_Type (Etype (L));
1877 Rtyp := Underlying_Type (Etype (R));
1879 Convert_To_PAT_Type (L);
1880 Convert_To_PAT_Type (R);
1884 Make_Op_Multiply (Loc,
1886 Make_Attribute_Reference (Loc,
1887 Attribute_Name => Name_Length,
1888 Prefix => New_Occurrence_Of (Ltyp, Loc)),
1890 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1893 Make_Op_Multiply (Loc,
1895 Make_Attribute_Reference (Loc,
1896 Attribute_Name => Name_Length,
1897 Prefix => New_Occurrence_Of (Rtyp, Loc)),
1899 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1901 -- For the modular case, we transform the comparison to:
1903 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1905 -- where PAT is the packed array type. This works fine, since in the
1906 -- modular case we guarantee that the unused bits are always zeroes.
1907 -- We do have to compare the lengths because we could be comparing
1908 -- two different subtypes of the same base type.
1910 if Is_Modular_Integer_Type (PAT) then
1915 Left_Opnd => LLexpr,
1916 Right_Opnd => RLexpr),
1923 -- For the non-modular case, we call a runtime routine
1925 -- System.Bit_Ops.Bit_Eq
1926 -- (L'Address, L_Length, R'Address, R_Length)
1928 -- where PAT is the packed array type, and the lengths are the lengths
1929 -- in bits of the original packed arrays. This routine takes care of
1930 -- not comparing the unused bits in the last byte.
1934 Make_Function_Call (Loc,
1935 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1936 Parameter_Associations => New_List (
1937 Make_Attribute_Reference (Loc,
1938 Attribute_Name => Name_Address,
1943 Make_Attribute_Reference (Loc,
1944 Attribute_Name => Name_Address,
1950 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
1951 end Expand_Packed_Eq;
1953 -----------------------
1954 -- Expand_Packed_Not --
1955 -----------------------
1957 -- Handles expansion of "not" on packed array types
1959 procedure Expand_Packed_Not (N : Node_Id) is
1960 Loc : constant Source_Ptr := Sloc (N);
1961 Typ : constant Entity_Id := Etype (N);
1962 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
1969 Convert_To_Actual_Subtype (Opnd);
1970 Rtyp := Etype (Opnd);
1972 -- First an odd and silly test. We explicitly check for the case
1973 -- where the 'First of the component type is equal to the 'Last of
1974 -- this component type, and if this is the case, we make sure that
1975 -- constraint error is raised. The reason is that the NOT is bound
1976 -- to cause CE in this case, and we will not otherwise catch it.
1978 -- Believe it or not, this was reported as a bug. Note that nearly
1979 -- always, the test will evaluate statically to False, so the code
1980 -- will be statically removed, and no extra overhead caused.
1983 CT : constant Entity_Id := Component_Type (Rtyp);
1987 Make_Raise_Constraint_Error (Loc,
1991 Make_Attribute_Reference (Loc,
1992 Prefix => New_Occurrence_Of (CT, Loc),
1993 Attribute_Name => Name_First),
1996 Make_Attribute_Reference (Loc,
1997 Prefix => New_Occurrence_Of (CT, Loc),
1998 Attribute_Name => Name_Last))));
2001 -- Now that that silliness is taken care of, get packed array type
2003 Convert_To_PAT_Type (Opnd);
2004 PAT := Etype (Opnd);
2006 -- For the case where the packed array type is a modular type,
2007 -- not A expands simply into:
2009 -- rtyp!(PAT!(A) xor mask)
2011 -- where PAT is the packed array type, and mask is a mask of all
2012 -- one bits of length equal to the size of this packed type and
2013 -- rtyp is the actual subtype of the operand
2015 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2016 Set_Print_In_Hex (Lit);
2018 if not Is_Array_Type (PAT) then
2020 Unchecked_Convert_To (Rtyp,
2023 Right_Opnd => Lit)));
2025 -- For the array case, we insert the actions
2029 -- System.Bitops.Bit_Not
2031 -- Typ'Length * Typ'Component_Size;
2034 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2035 -- argument is the length of the operand in bits. Then we replace
2036 -- the expression by a reference to Result.
2040 Result_Ent : constant Entity_Id :=
2041 Make_Defining_Identifier (Loc,
2042 Chars => New_Internal_Name ('T'));
2045 Insert_Actions (N, New_List (
2047 Make_Object_Declaration (Loc,
2048 Defining_Identifier => Result_Ent,
2049 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2051 Make_Procedure_Call_Statement (Loc,
2052 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2053 Parameter_Associations => New_List (
2055 Make_Attribute_Reference (Loc,
2056 Attribute_Name => Name_Address,
2059 Make_Op_Multiply (Loc,
2061 Make_Attribute_Reference (Loc,
2064 (Etype (First_Index (Rtyp)), Loc),
2065 Attribute_Name => Name_Range_Length),
2067 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2069 Make_Attribute_Reference (Loc,
2070 Attribute_Name => Name_Address,
2071 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2074 New_Occurrence_Of (Result_Ent, Loc));
2078 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2080 end Expand_Packed_Not;
2082 -------------------------------------
2083 -- Involves_Packed_Array_Reference --
2084 -------------------------------------
2086 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2088 if Nkind (N) = N_Indexed_Component
2089 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2093 elsif Nkind (N) = N_Selected_Component then
2094 return Involves_Packed_Array_Reference (Prefix (N));
2099 end Involves_Packed_Array_Reference;
2101 --------------------------
2102 -- Known_Aligned_Enough --
2103 --------------------------
2105 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2106 Typ : constant Entity_Id := Etype (Obj);
2108 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2109 -- If the component is in a record that contains previous packed
2110 -- components, consider it unaligned because the back-end might
2111 -- choose to pack the rest of the record. Lead to less efficient code,
2112 -- but safer vis-a-vis of back-end choices.
2114 --------------------------------
2115 -- In_Partially_Packed_Record --
2116 --------------------------------
2118 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2119 Rec_Type : constant Entity_Id := Scope (Comp);
2120 Prev_Comp : Entity_Id;
2123 Prev_Comp := First_Entity (Rec_Type);
2124 while Present (Prev_Comp) loop
2125 if Is_Packed (Etype (Prev_Comp)) then
2128 elsif Prev_Comp = Comp then
2132 Next_Entity (Prev_Comp);
2136 end In_Partially_Packed_Record;
2138 -- Start of processing for Known_Aligned_Enough
2141 -- Odd bit sizes don't need alignment anyway
2143 if Csiz mod 2 = 1 then
2146 -- If we have a specified alignment, see if it is sufficient, if not
2147 -- then we can't possibly be aligned enough in any case.
2149 elsif Is_Entity_Name (Obj)
2150 and then Known_Alignment (Entity (Obj))
2152 -- Alignment required is 4 if size is a multiple of 4, and
2153 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2155 if Alignment (Entity (Obj)) < 4 - (Csiz mod 4) then
2160 -- OK, alignment should be sufficient, if object is aligned
2162 -- If object is strictly aligned, then it is definitely aligned
2164 if Strict_Alignment (Typ) then
2167 -- Case of subscripted array reference
2169 elsif Nkind (Obj) = N_Indexed_Component then
2171 -- If we have a pointer to an array, then this is definitely
2172 -- aligned, because pointers always point to aligned versions.
2174 if Is_Access_Type (Etype (Prefix (Obj))) then
2177 -- Otherwise, go look at the prefix
2180 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2183 -- Case of record field
2185 elsif Nkind (Obj) = N_Selected_Component then
2187 -- What is significant here is whether the record type is packed
2189 if Is_Record_Type (Etype (Prefix (Obj)))
2190 and then Is_Packed (Etype (Prefix (Obj)))
2194 -- Or the component has a component clause which might cause
2195 -- the component to become unaligned (we can't tell if the
2196 -- backend is doing alignment computations).
2198 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2201 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2204 -- In all other cases, go look at prefix
2207 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2210 -- If not selected or indexed component, must be aligned
2215 end Known_Aligned_Enough;
2217 ---------------------
2218 -- Make_Shift_Left --
2219 ---------------------
2221 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2225 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2229 Make_Op_Shift_Left (Sloc (N),
2232 Set_Shift_Count_OK (Nod, True);
2235 end Make_Shift_Left;
2237 ----------------------
2238 -- Make_Shift_Right --
2239 ----------------------
2241 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2245 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2249 Make_Op_Shift_Right (Sloc (N),
2252 Set_Shift_Count_OK (Nod, True);
2255 end Make_Shift_Right;
2257 -----------------------------
2258 -- RJ_Unchecked_Convert_To --
2259 -----------------------------
2261 function RJ_Unchecked_Convert_To
2266 Source_Typ : constant Entity_Id := Etype (Expr);
2267 Target_Typ : constant Entity_Id := Typ;
2269 Src : Node_Id := Expr;
2275 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2276 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2278 -- In the big endian case, if the lengths of the two types differ,
2279 -- then we must worry about possible left justification in the
2280 -- conversion, and avoiding that is what this is all about.
2282 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2284 -- First step, if the source type is not a discrete type, then we
2285 -- first convert to a modular type of the source length, since
2286 -- otherwise, on a big-endian machine, we get left-justification.
2288 if not Is_Discrete_Type (Source_Typ) then
2289 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2292 -- Next step. If the target is not a discrete type, then we first
2293 -- convert to a modular type of the target length, since
2294 -- otherwise, on a big-endian machine, we get left-justification.
2296 if not Is_Discrete_Type (Target_Typ) then
2297 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2301 -- And now we can do the final conversion to the target type
2303 return Unchecked_Convert_To (Target_Typ, Src);
2304 end RJ_Unchecked_Convert_To;
2306 ----------------------------------------------
2307 -- Setup_Enumeration_Packed_Array_Reference --
2308 ----------------------------------------------
2310 -- All we have to do here is to find the subscripts that correspond
2311 -- to the index positions that have non-standard enumeration types
2312 -- and insert a Pos attribute to get the proper subscript value.
2314 -- Finally the prefix must be uncheck converted to the corresponding
2315 -- packed array type.
2317 -- Note that the component type is unchanged, so we do not need to
2318 -- fiddle with the types (Gigi always automatically takes the packed
2319 -- array type if it is set, as it will be in this case).
2321 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2322 Pfx : constant Node_Id := Prefix (N);
2323 Typ : constant Entity_Id := Etype (N);
2324 Exprs : constant List_Id := Expressions (N);
2328 -- If the array is unconstrained, then we replace the array
2329 -- reference with its actual subtype. This actual subtype will
2330 -- have a packed array type with appropriate bounds.
2332 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2333 Convert_To_Actual_Subtype (Pfx);
2336 Expr := First (Exprs);
2337 while Present (Expr) loop
2339 Loc : constant Source_Ptr := Sloc (Expr);
2340 Expr_Typ : constant Entity_Id := Etype (Expr);
2343 if Is_Enumeration_Type (Expr_Typ)
2344 and then Has_Non_Standard_Rep (Expr_Typ)
2347 Make_Attribute_Reference (Loc,
2348 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2349 Attribute_Name => Name_Pos,
2350 Expressions => New_List (Relocate_Node (Expr))));
2351 Analyze_And_Resolve (Expr, Standard_Natural);
2359 Make_Indexed_Component (Sloc (N),
2361 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2362 Expressions => Exprs));
2364 Analyze_And_Resolve (N, Typ);
2366 end Setup_Enumeration_Packed_Array_Reference;
2368 -----------------------------------------
2369 -- Setup_Inline_Packed_Array_Reference --
2370 -----------------------------------------
2372 procedure Setup_Inline_Packed_Array_Reference
2375 Obj : in out Node_Id;
2377 Shift : out Node_Id)
2379 Loc : constant Source_Ptr := Sloc (N);
2387 Ctyp := Component_Type (Atyp);
2388 Csiz := Component_Size (Atyp);
2390 Convert_To_PAT_Type (Obj);
2393 Cmask := 2 ** Csiz - 1;
2395 if Is_Array_Type (PAT) then
2396 Otyp := Component_Type (PAT);
2397 Osiz := Esize (Otyp);
2402 -- In the case where the PAT is a modular type, we want the actual
2403 -- size in bits of the modular value we use. This is neither the
2404 -- Object_Size nor the Value_Size, either of which may have been
2405 -- reset to strange values, but rather the minimum size. Note that
2406 -- since this is a modular type with full range, the issue of
2407 -- biased representation does not arise.
2409 Osiz := UI_From_Int (Minimum_Size (Otyp));
2412 Compute_Linear_Subscript (Atyp, N, Shift);
2414 -- If the component size is not 1, then the subscript must be
2415 -- multiplied by the component size to get the shift count.
2419 Make_Op_Multiply (Loc,
2420 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2421 Right_Opnd => Shift);
2424 -- If we have the array case, then this shift count must be broken
2425 -- down into a byte subscript, and a shift within the byte.
2427 if Is_Array_Type (PAT) then
2430 New_Shift : Node_Id;
2433 -- We must analyze shift, since we will duplicate it
2435 Set_Parent (Shift, N);
2437 (Shift, Standard_Integer, Suppress => All_Checks);
2439 -- The shift count within the word is
2444 Left_Opnd => Duplicate_Subexpr (Shift),
2445 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2447 -- The subscript to be used on the PAT array is
2451 Make_Indexed_Component (Loc,
2453 Expressions => New_List (
2454 Make_Op_Divide (Loc,
2455 Left_Opnd => Duplicate_Subexpr (Shift),
2456 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2461 -- For the modular integer case, the object to be manipulated is
2462 -- the entire array, so Obj is unchanged. Note that we will reset
2463 -- its type to PAT before returning to the caller.
2469 -- The one remaining step is to modify the shift count for the
2470 -- big-endian case. Consider the following example in a byte:
2472 -- xxxxxxxx bits of byte
2473 -- vvvvvvvv bits of value
2474 -- 33221100 little-endian numbering
2475 -- 00112233 big-endian numbering
2477 -- Here we have the case of 2-bit fields
2479 -- For the little-endian case, we already have the proper shift
2480 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2482 -- For the big endian case, we have to adjust the shift count,
2483 -- computing it as (N - F) - shift, where N is the number of bits
2484 -- in an element of the array used to implement the packed array,
2485 -- F is the number of bits in a source level array element, and
2486 -- shift is the count so far computed.
2488 if Bytes_Big_Endian then
2490 Make_Op_Subtract (Loc,
2491 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2492 Right_Opnd => Shift);
2495 Set_Parent (Shift, N);
2496 Set_Parent (Obj, N);
2497 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2498 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2500 -- Make sure final type of object is the appropriate packed type
2502 Set_Etype (Obj, Otyp);
2504 end Setup_Inline_Packed_Array_Reference;