1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Nlists; use Nlists;
33 with Nmake; use Nmake;
34 with Rtsfind; use Rtsfind;
36 with Sem_Ch3; use Sem_Ch3;
37 with Sem_Ch8; use Sem_Ch8;
38 with Sem_Ch13; use Sem_Ch13;
39 with Sem_Eval; use Sem_Eval;
40 with Sem_Res; use Sem_Res;
41 with Sem_Util; use Sem_Util;
42 with Sinfo; use Sinfo;
43 with Snames; use Snames;
44 with Stand; use Stand;
45 with Targparm; use Targparm;
46 with Tbuild; use Tbuild;
47 with Ttypes; use Ttypes;
48 with Uintp; use Uintp;
50 package body Exp_Pakd is
52 ---------------------------
53 -- Endian Considerations --
54 ---------------------------
56 -- As described in the specification, bit numbering in a packed array
57 -- is consistent with bit numbering in a record representation clause,
58 -- and hence dependent on the endianness of the machine:
60 -- For little-endian machines, element zero is at the right hand end
61 -- (low order end) of a bit field.
63 -- For big-endian machines, element zero is at the left hand end
64 -- (high order end) of a bit field.
66 -- The shifts that are used to right justify a field therefore differ
67 -- in the two cases. For the little-endian case, we can simply use the
68 -- bit number (i.e. the element number * element size) as the count for
69 -- a right shift. For the big-endian case, we have to subtract the shift
70 -- count from an appropriate constant to use in the right shift. We use
71 -- rotates instead of shifts (which is necessary in the store case to
72 -- preserve other fields), and we expect that the backend will be able
73 -- to change the right rotate into a left rotate, avoiding the subtract,
74 -- if the architecture provides such an instruction.
76 ----------------------------------------------
77 -- Entity Tables for Packed Access Routines --
78 ----------------------------------------------
80 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
81 -- library routines. This table is used to obtain the entity for the
84 type E_Array is array (Int range 01 .. 63) of RE_Id;
86 -- Array of Bits_nn entities. Note that we do not use library routines
87 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
88 -- entries from System.Unsigned, because we also use this table for
89 -- certain special unchecked conversions in the big-endian case.
91 Bits_Id : constant E_Array :=
107 16 => RE_Unsigned_16,
123 32 => RE_Unsigned_32,
156 -- Array of Get routine entities. These are used to obtain an element
157 -- from a packed array. The N'th entry is used to obtain elements from
158 -- a packed array whose component size is N. RE_Null is used as a null
159 -- entry, for the cases where a library routine is not used.
161 Get_Id : constant E_Array :=
226 -- Array of Get routine entities to be used in the case where the packed
227 -- array is itself a component of a packed structure, and therefore may
228 -- not be fully aligned. This only affects the even sizes, since for the
229 -- odd sizes, we do not get any fixed alignment in any case.
231 GetU_Id : constant E_Array :=
296 -- Array of Set routine entities. These are used to assign an element
297 -- of a packed array. The N'th entry is used to assign elements for
298 -- a packed array whose component size is N. RE_Null is used as a null
299 -- entry, for the cases where a library routine is not used.
301 Set_Id : constant E_Array :=
366 -- Array of Set routine entities to be used in the case where the packed
367 -- array is itself a component of a packed structure, and therefore may
368 -- not be fully aligned. This only affects the even sizes, since for the
369 -- odd sizes, we do not get any fixed alignment in any case.
371 SetU_Id : constant E_Array :=
436 -----------------------
437 -- Local Subprograms --
438 -----------------------
440 procedure Compute_Linear_Subscript
443 Subscr : out Node_Id);
444 -- Given a constrained array type Atyp, and an indexed component node
445 -- N referencing an array object of this type, build an expression of
446 -- type Standard.Integer representing the zero-based linear subscript
447 -- value. This expression includes any required range checks.
449 procedure Convert_To_PAT_Type (Aexp : Node_Id);
450 -- Given an expression of a packed array type, builds a corresponding
451 -- expression whose type is the implementation type used to represent
452 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
454 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
455 -- There are two versions of the Set routines, the ones used when the
456 -- object is known to be sufficiently well aligned given the number of
457 -- bits, and the ones used when the object is not known to be aligned.
458 -- This routine is used to determine which set to use. Obj is a reference
459 -- to the object, and Csiz is the component size of the packed array.
460 -- True is returned if the alignment of object is known to be sufficient,
461 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
464 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
465 -- Build a left shift node, checking for the case of a shift count of zero
467 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
468 -- Build a right shift node, checking for the case of a shift count of zero
470 function RJ_Unchecked_Convert_To
474 -- The packed array code does unchecked conversions which in some cases
475 -- may involve non-discrete types with differing sizes. The semantics of
476 -- such conversions is potentially endian dependent, and the effect we
477 -- want here for such a conversion is to do the conversion in size as
478 -- though numeric items are involved, and we extend or truncate on the
479 -- left side. This happens naturally in the little-endian case, but in
480 -- the big endian case we can get left justification, when what we want
481 -- is right justification. This routine does the unchecked conversion in
482 -- a stepwise manner to ensure that it gives the expected result. Hence
483 -- the name (RJ = Right justified). The parameters Typ and Expr are as
484 -- for the case of a normal Unchecked_Convert_To call.
486 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
487 -- This routine is called in the Get and Set case for arrays that are
488 -- packed but not bit-packed, meaning that they have at least one
489 -- subscript that is of an enumeration type with a non-standard
490 -- representation. This routine modifies the given node to properly
491 -- reference the corresponding packed array type.
493 procedure Setup_Inline_Packed_Array_Reference
496 Obj : in out Node_Id;
498 Shift : out Node_Id);
499 -- This procedure performs common processing on the N_Indexed_Component
500 -- parameter given as N, whose prefix is a reference to a packed array.
501 -- This is used for the get and set when the component size is 1,2,4
502 -- or for other component sizes when the packed array type is a modular
503 -- type (i.e. the cases that are handled with inline code).
507 -- N is the N_Indexed_Component node for the packed array reference
509 -- Atyp is the constrained array type (the actual subtype has been
510 -- computed if necessary to obtain the constraints, but this is still
511 -- the original array type, not the Packed_Array_Type value).
513 -- Obj is the object which is to be indexed. It is always of type Atyp.
517 -- Obj is the object containing the desired bit field. It is of type
518 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
519 -- entire value, for the small static case, or the proper selected byte
520 -- from the array in the large or dynamic case. This node is analyzed
521 -- and resolved on return.
523 -- Shift is a node representing the shift count to be used in the
524 -- rotate right instruction that positions the field for access.
525 -- This node is analyzed and resolved on return.
527 -- Cmask is a mask corresponding to the width of the component field.
528 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
530 -- Note: in some cases the call to this routine may generate actions
531 -- (for handling multi-use references and the generation of the packed
532 -- array type on the fly). Such actions are inserted into the tree
533 -- directly using Insert_Action.
535 ------------------------------
536 -- Compute_Linear_Subcsript --
537 ------------------------------
539 procedure Compute_Linear_Subscript
542 Subscr : out Node_Id)
544 Loc : constant Source_Ptr := Sloc (N);
553 -- Loop through dimensions
555 Indx := First_Index (Atyp);
556 Oldsub := First (Expressions (N));
558 while Present (Indx) loop
559 Styp := Etype (Indx);
560 Newsub := Relocate_Node (Oldsub);
562 -- Get expression for the subscript value. First, if Do_Range_Check
563 -- is set on a subscript, then we must do a range check against the
564 -- original bounds (not the bounds of the packed array type). We do
565 -- this by introducing a subtype conversion.
567 if Do_Range_Check (Newsub)
568 and then Etype (Newsub) /= Styp
570 Newsub := Convert_To (Styp, Newsub);
573 -- Now evolve the expression for the subscript. First convert
574 -- the subscript to be zero based and of an integer type.
576 -- Case of integer type, where we just subtract to get lower bound
578 if Is_Integer_Type (Styp) then
580 -- If length of integer type is smaller than standard integer,
581 -- then we convert to integer first, then do the subtract
583 -- Integer (subscript) - Integer (Styp'First)
585 if Esize (Styp) < Esize (Standard_Integer) then
587 Make_Op_Subtract (Loc,
588 Left_Opnd => Convert_To (Standard_Integer, Newsub),
590 Convert_To (Standard_Integer,
591 Make_Attribute_Reference (Loc,
592 Prefix => New_Occurrence_Of (Styp, Loc),
593 Attribute_Name => Name_First)));
595 -- For larger integer types, subtract first, then convert to
596 -- integer, this deals with strange long long integer bounds.
598 -- Integer (subscript - Styp'First)
602 Convert_To (Standard_Integer,
603 Make_Op_Subtract (Loc,
606 Make_Attribute_Reference (Loc,
607 Prefix => New_Occurrence_Of (Styp, Loc),
608 Attribute_Name => Name_First)));
611 -- For the enumeration case, we have to use 'Pos to get the value
612 -- to work with before subtracting the lower bound.
614 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
616 -- This is not quite right for bizarre cases where the size of the
617 -- enumeration type is > Integer'Size bits due to rep clause ???
620 pragma Assert (Is_Enumeration_Type (Styp));
623 Make_Op_Subtract (Loc,
624 Left_Opnd => Convert_To (Standard_Integer,
625 Make_Attribute_Reference (Loc,
626 Prefix => New_Occurrence_Of (Styp, Loc),
627 Attribute_Name => Name_Pos,
628 Expressions => New_List (Newsub))),
631 Convert_To (Standard_Integer,
632 Make_Attribute_Reference (Loc,
633 Prefix => New_Occurrence_Of (Styp, Loc),
634 Attribute_Name => Name_Pos,
635 Expressions => New_List (
636 Make_Attribute_Reference (Loc,
637 Prefix => New_Occurrence_Of (Styp, Loc),
638 Attribute_Name => Name_First)))));
641 Set_Paren_Count (Newsub, 1);
643 -- For the first subscript, we just copy that subscript value
648 -- Otherwise, we must multiply what we already have by the current
649 -- stride and then add in the new value to the evolving subscript.
655 Make_Op_Multiply (Loc,
658 Make_Attribute_Reference (Loc,
659 Attribute_Name => Name_Range_Length,
660 Prefix => New_Occurrence_Of (Styp, Loc))),
661 Right_Opnd => Newsub);
664 -- Move to next subscript
669 end Compute_Linear_Subscript;
671 -------------------------
672 -- Convert_To_PAT_Type --
673 -------------------------
675 -- The PAT is always obtained from the actual subtype
677 procedure Convert_To_PAT_Type (Aexp : Entity_Id) is
681 Convert_To_Actual_Subtype (Aexp);
682 Act_ST := Underlying_Type (Etype (Aexp));
683 Create_Packed_Array_Type (Act_ST);
685 -- Just replace the etype with the packed array type. This works
686 -- because the expression will not be further analyzed, and Gigi
687 -- considers the two types equivalent in any case.
689 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
690 end Convert_To_PAT_Type;
692 ------------------------------
693 -- Create_Packed_Array_Type --
694 ------------------------------
696 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
697 Loc : constant Source_Ptr := Sloc (Typ);
698 Ctyp : constant Entity_Id := Component_Type (Typ);
699 Csize : constant Uint := Component_Size (Typ);
714 procedure Install_PAT;
715 -- This procedure is called with Decl set to the declaration for the
716 -- packed array type. It creates the type and installs it as required.
718 procedure Set_PB_Type;
719 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
720 -- requirements (see documentation in the spec of this package).
726 procedure Install_PAT is
727 Pushed_Scope : Boolean := False;
730 -- We do not want to put the declaration we have created in the tree
731 -- since it is often hard, and sometimes impossible to find a proper
732 -- place for it (the impossible case arises for a packed array type
733 -- with bounds depending on the discriminant, a declaration cannot
734 -- be put inside the record, and the reference to the discriminant
735 -- cannot be outside the record).
737 -- The solution is to analyze the declaration while temporarily
738 -- attached to the tree at an appropriate point, and then we install
739 -- the resulting type as an Itype in the packed array type field of
740 -- the original type, so that no explicit declaration is required.
742 -- Note: the packed type is created in the scope of its parent
743 -- type. There are at least some cases where the current scope
744 -- is deeper, and so when this is the case, we temporarily reset
745 -- the scope for the definition. This is clearly safe, since the
746 -- first use of the packed array type will be the implicit
747 -- reference from the corresponding unpacked type when it is
750 if Is_Itype (Typ) then
751 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
753 Set_Parent (Decl, Declaration_Node (Typ));
756 if Scope (Typ) /= Current_Scope then
757 New_Scope (Scope (Typ));
758 Pushed_Scope := True;
761 Set_Is_Itype (PAT, True);
762 Set_Packed_Array_Type (Typ, PAT);
763 Analyze (Decl, Suppress => All_Checks);
769 -- Set Esize and RM_Size to the actual size of the packed object
770 -- Do not reset RM_Size if already set, as happens in the case
771 -- of a modular type.
773 Set_Esize (PAT, Esiz);
775 if Unknown_RM_Size (PAT) then
776 Set_RM_Size (PAT, Esiz);
779 -- Set remaining fields of packed array type
781 Init_Alignment (PAT);
782 Set_Parent (PAT, Empty);
783 Set_Associated_Node_For_Itype (PAT, Typ);
784 Set_Is_Packed_Array_Type (PAT, True);
785 Set_Original_Array_Type (PAT, Typ);
787 -- We definitely do not want to delay freezing for packed array
788 -- types. This is of particular importance for the itypes that
789 -- are generated for record components depending on discriminants
790 -- where there is no place to put the freeze node.
792 Set_Has_Delayed_Freeze (PAT, False);
793 Set_Has_Delayed_Freeze (Etype (PAT), False);
800 procedure Set_PB_Type is
802 -- If the user has specified an explicit alignment for the
803 -- type or component, take it into account.
805 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
806 or else Alignment (Typ) = 1
807 or else Component_Alignment (Typ) = Calign_Storage_Unit
809 PB_Type := RTE (RE_Packed_Bytes1);
811 elsif Csize mod 4 /= 0
812 or else Alignment (Typ) = 2
814 PB_Type := RTE (RE_Packed_Bytes2);
817 PB_Type := RTE (RE_Packed_Bytes4);
821 -- Start of processing for Create_Packed_Array_Type
824 -- If we already have a packed array type, nothing to do
826 if Present (Packed_Array_Type (Typ)) then
830 -- If our immediate ancestor subtype is constrained, and it already
831 -- has a packed array type, then just share the same type, since the
832 -- bounds must be the same.
834 if Ekind (Typ) = E_Array_Subtype then
835 Ancest := Ancestor_Subtype (Typ);
838 and then Is_Constrained (Ancest)
839 and then Present (Packed_Array_Type (Ancest))
841 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
846 -- We preset the result type size from the size of the original array
847 -- type, since this size clearly belongs to the packed array type. The
848 -- size of the conceptual unpacked type is always set to unknown.
852 -- Case of an array where at least one index is of an enumeration
853 -- type with a non-standard representation, but the component size
854 -- is not appropriate for bit packing. This is the case where we
855 -- have Is_Packed set (we would never be in this unit otherwise),
856 -- but Is_Bit_Packed_Array is false.
858 -- Note that if the component size is appropriate for bit packing,
859 -- then the circuit for the computation of the subscript properly
860 -- deals with the non-standard enumeration type case by taking the
863 if not Is_Bit_Packed_Array (Typ) then
865 -- Here we build a declaration:
867 -- type tttP is array (index1, index2, ...) of component_type
869 -- where index1, index2, are the index types. These are the same
870 -- as the index types of the original array, except for the non-
871 -- standard representation enumeration type case, where we have
874 -- For the unconstrained array case, we use
878 -- For the constrained case, we use
880 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
881 -- Enum_Type'Pos (Enum_Type'Last);
884 Make_Defining_Identifier (Loc,
885 Chars => New_External_Name (Chars (Typ), 'P'));
887 Set_Packed_Array_Type (Typ, PAT);
890 Indexes : constant List_Id := New_List;
892 Indx_Typ : Entity_Id;
897 Indx := First_Index (Typ);
899 while Present (Indx) loop
900 Indx_Typ := Etype (Indx);
902 Enum_Case := Is_Enumeration_Type (Indx_Typ)
903 and then Has_Non_Standard_Rep (Indx_Typ);
905 -- Unconstrained case
907 if not Is_Constrained (Typ) then
909 Indx_Typ := Standard_Natural;
912 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
917 if not Enum_Case then
918 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
922 Make_Subtype_Indication (Loc,
924 New_Occurrence_Of (Standard_Natural, Loc),
926 Make_Range_Constraint (Loc,
930 Make_Attribute_Reference (Loc,
932 New_Occurrence_Of (Indx_Typ, Loc),
933 Attribute_Name => Name_Pos,
934 Expressions => New_List (
935 Make_Attribute_Reference (Loc,
937 New_Occurrence_Of (Indx_Typ, Loc),
938 Attribute_Name => Name_First))),
941 Make_Attribute_Reference (Loc,
943 New_Occurrence_Of (Indx_Typ, Loc),
944 Attribute_Name => Name_Pos,
945 Expressions => New_List (
946 Make_Attribute_Reference (Loc,
948 New_Occurrence_Of (Indx_Typ, Loc),
949 Attribute_Name => Name_Last)))))));
957 if not Is_Constrained (Typ) then
959 Make_Unconstrained_Array_Definition (Loc,
960 Subtype_Marks => Indexes,
961 Subtype_Indication =>
962 New_Occurrence_Of (Ctyp, Loc));
966 Make_Constrained_Array_Definition (Loc,
967 Discrete_Subtype_Definitions => Indexes,
968 Subtype_Indication =>
969 New_Occurrence_Of (Ctyp, Loc));
973 Make_Full_Type_Declaration (Loc,
974 Defining_Identifier => PAT,
975 Type_Definition => Typedef);
978 -- Set type as packed array type and install it
980 Set_Is_Packed_Array_Type (PAT);
984 -- Case of bit-packing required for unconstrained array. We create
985 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
987 elsif not Is_Constrained (Typ) then
989 Make_Defining_Identifier (Loc,
990 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
992 Set_Packed_Array_Type (Typ, PAT);
996 Make_Subtype_Declaration (Loc,
997 Defining_Identifier => PAT,
998 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1002 -- Remaining code is for the case of bit-packing for constrained array
1004 -- The name of the packed array subtype is
1008 -- where sss is the component size in bits and ttt is the name of
1009 -- the parent packed type.
1013 Make_Defining_Identifier (Loc,
1014 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1016 Set_Packed_Array_Type (Typ, PAT);
1018 -- Build an expression for the length of the array in bits.
1019 -- This is the product of the length of each of the dimensions
1025 Len_Expr := Empty; -- suppress junk warning
1029 Make_Attribute_Reference (Loc,
1030 Attribute_Name => Name_Length,
1031 Prefix => New_Occurrence_Of (Typ, Loc),
1032 Expressions => New_List (
1033 Make_Integer_Literal (Loc, J)));
1036 Len_Expr := Len_Dim;
1040 Make_Op_Multiply (Loc,
1041 Left_Opnd => Len_Expr,
1042 Right_Opnd => Len_Dim);
1046 exit when J > Number_Dimensions (Typ);
1050 -- Temporarily attach the length expression to the tree and analyze
1051 -- and resolve it, so that we can test its value. We assume that the
1052 -- total length fits in type Integer. This expression may involve
1053 -- discriminants, so we treat it as a default/per-object expression.
1055 Set_Parent (Len_Expr, Typ);
1056 Analyze_Per_Use_Expression (Len_Expr, Standard_Integer);
1058 -- Use a modular type if possible. We can do this if we are we
1059 -- have static bounds, and the length is small enough, and the
1060 -- length is not zero. We exclude the zero length case because the
1061 -- size of things is always at least one, and the zero length object
1062 -- would have an anomous size.
1064 if Compile_Time_Known_Value (Len_Expr) then
1065 Len_Bits := Expr_Value (Len_Expr) * Csize;
1067 -- We normally consider small enough to mean no larger than the
1068 -- value of System_Max_Binary_Modulus_Power, checking that in the
1069 -- case of values longer than word size, we have long shifts.
1073 (Len_Bits <= System_Word_Size
1074 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1075 and then Support_Long_Shifts_On_Target))
1077 -- Also test for alignment given. If an alignment is given which
1078 -- is smaller than the natural modular alignment, force the array
1079 -- of bytes representation to accommodate the alignment.
1082 (No (Alignment_Clause (Typ))
1084 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1085 / System_Storage_Unit))
1087 -- We can use the modular type, it has the form:
1089 -- subtype tttPn is btyp
1090 -- range 0 .. 2 ** (Esize (Typ) * Csize) - 1;
1092 -- The bounds are statically known, and btyp is one
1093 -- of the unsigned types, depending on the length. If the
1094 -- type is its first subtype, i.e. it is a user-defined
1095 -- type, no object of the type will be larger, and it is
1096 -- worthwhile to use a small unsigned type.
1098 if Len_Bits <= Standard_Short_Integer_Size
1099 and then First_Subtype (Typ) = Typ
1101 Btyp := RTE (RE_Short_Unsigned);
1103 elsif Len_Bits <= Standard_Integer_Size then
1104 Btyp := RTE (RE_Unsigned);
1106 elsif Len_Bits <= Standard_Long_Integer_Size then
1107 Btyp := RTE (RE_Long_Unsigned);
1110 Btyp := RTE (RE_Long_Long_Unsigned);
1113 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1114 Set_Print_In_Hex (Lit);
1117 Make_Subtype_Declaration (Loc,
1118 Defining_Identifier => PAT,
1119 Subtype_Indication =>
1120 Make_Subtype_Indication (Loc,
1121 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1124 Make_Range_Constraint (Loc,
1128 Make_Integer_Literal (Loc, 0),
1129 High_Bound => Lit))));
1131 if Esiz = Uint_0 then
1140 -- Could not use a modular type, for all other cases, we build
1141 -- a packed array subtype:
1144 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1146 -- Bits is the length of the array in bits.
1153 Make_Op_Multiply (Loc,
1155 Make_Integer_Literal (Loc, Csize),
1156 Right_Opnd => Len_Expr),
1159 Make_Integer_Literal (Loc, 7));
1161 Set_Paren_Count (Bits_U1, 1);
1164 Make_Op_Subtract (Loc,
1166 Make_Op_Divide (Loc,
1167 Left_Opnd => Bits_U1,
1168 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1169 Right_Opnd => Make_Integer_Literal (Loc, 1));
1172 Make_Subtype_Declaration (Loc,
1173 Defining_Identifier => PAT,
1174 Subtype_Indication =>
1175 Make_Subtype_Indication (Loc,
1176 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1179 Make_Index_Or_Discriminant_Constraint (Loc,
1180 Constraints => New_List (
1183 Make_Integer_Literal (Loc, 0),
1184 High_Bound => PAT_High)))));
1188 end Create_Packed_Array_Type;
1190 -----------------------------------
1191 -- Expand_Bit_Packed_Element_Set --
1192 -----------------------------------
1194 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1195 Loc : constant Source_Ptr := Sloc (N);
1196 Lhs : constant Node_Id := Name (N);
1198 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1199 -- Used to preserve assignment OK status when assignment is rewritten
1201 Rhs : Node_Id := Expression (N);
1202 -- Initially Rhs is the right hand side value, it will be replaced
1203 -- later by an appropriate unchecked conversion for the assignment.
1213 -- The expression for the shift value that is required
1215 Shift_Used : Boolean := False;
1216 -- Set True if Shift has been used in the generated code at least
1217 -- once, so that it must be duplicated if used again
1222 Rhs_Val_Known : Boolean;
1224 -- If the value of the right hand side as an integer constant is
1225 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1226 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1227 -- the Rhs_Val is undefined.
1229 function Get_Shift return Node_Id;
1230 -- Function used to get the value of Shift, making sure that it
1231 -- gets duplicated if the function is called more than once.
1237 function Get_Shift return Node_Id is
1239 -- If we used the shift value already, then duplicate it. We
1240 -- set a temporary parent in case actions have to be inserted.
1243 Set_Parent (Shift, N);
1244 return Duplicate_Subexpr_No_Checks (Shift);
1246 -- If first time, use Shift unchanged, and set flag for first use
1254 -- Start of processing for Expand_Bit_Packed_Element_Set
1257 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1259 Obj := Relocate_Node (Prefix (Lhs));
1260 Convert_To_Actual_Subtype (Obj);
1261 Atyp := Etype (Obj);
1262 PAT := Packed_Array_Type (Atyp);
1263 Ctyp := Component_Type (Atyp);
1264 Csiz := UI_To_Int (Component_Size (Atyp));
1266 -- We convert the right hand side to the proper subtype to ensure
1267 -- that an appropriate range check is made (since the normal range
1268 -- check from assignment will be lost in the transformations). This
1269 -- conversion is analyzed immediately so that subsequent processing
1270 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1272 Rhs := Convert_To (Ctyp, Rhs);
1273 Set_Parent (Rhs, N);
1274 Analyze_And_Resolve (Rhs, Ctyp);
1276 -- Case of component size 1,2,4 or any component size for the modular
1277 -- case. These are the cases for which we can inline the code.
1279 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1280 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1282 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1284 -- The statement to be generated is:
1286 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1288 -- where mask1 is obtained by shifting Cmask left Shift bits
1289 -- and then complementing the result.
1291 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1293 -- the "or ..." is omitted if rhs is constant and all 0 bits
1295 -- rhs is converted to the appropriate type.
1297 -- The result is converted back to the array type, since
1298 -- otherwise we lose knowledge of the packed nature.
1300 -- Determine if right side is all 0 bits or all 1 bits
1302 if Compile_Time_Known_Value (Rhs) then
1303 Rhs_Val := Expr_Rep_Value (Rhs);
1304 Rhs_Val_Known := True;
1306 -- The following test catches the case of an unchecked conversion
1307 -- of an integer literal. This results from optimizing aggregates
1310 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1311 and then Compile_Time_Known_Value (Expression (Rhs))
1313 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1314 Rhs_Val_Known := True;
1318 Rhs_Val_Known := False;
1321 -- Some special checks for the case where the right hand value
1322 -- is known at compile time. Basically we have to take care of
1323 -- the implicit conversion to the subtype of the component object.
1325 if Rhs_Val_Known then
1327 -- If we have a biased component type then we must manually do
1328 -- the biasing, since we are taking responsibility in this case
1329 -- for constructing the exact bit pattern to be used.
1331 if Has_Biased_Representation (Ctyp) then
1332 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1335 -- For a negative value, we manually convert the twos complement
1336 -- value to a corresponding unsigned value, so that the proper
1337 -- field width is maintained. If we did not do this, we would
1338 -- get too many leading sign bits later on.
1341 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1345 New_Lhs := Duplicate_Subexpr (Obj, True);
1346 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1348 -- First we deal with the "and"
1350 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1356 if Compile_Time_Known_Value (Shift) then
1358 Make_Integer_Literal (Loc,
1359 Modulus (Etype (Obj)) - 1 -
1360 (Cmask * (2 ** Expr_Value (Get_Shift))));
1361 Set_Print_In_Hex (Mask1);
1364 Lit := Make_Integer_Literal (Loc, Cmask);
1365 Set_Print_In_Hex (Lit);
1368 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1373 Left_Opnd => New_Rhs,
1374 Right_Opnd => Mask1);
1378 -- Then deal with the "or"
1380 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1384 procedure Fixup_Rhs;
1385 -- Adjust Rhs by bias if biased representation for components
1386 -- or remove extraneous high order sign bits if signed.
1388 procedure Fixup_Rhs is
1389 Etyp : constant Entity_Id := Etype (Rhs);
1392 -- For biased case, do the required biasing by simply
1393 -- converting to the biased subtype (the conversion
1394 -- will generate the required bias).
1396 if Has_Biased_Representation (Ctyp) then
1397 Rhs := Convert_To (Ctyp, Rhs);
1399 -- For a signed integer type that is not biased, generate
1400 -- a conversion to unsigned to strip high order sign bits.
1402 elsif Is_Signed_Integer_Type (Ctyp) then
1403 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1406 -- Set Etype, since it can be referenced before the
1407 -- node is completely analyzed.
1409 Set_Etype (Rhs, Etyp);
1411 -- We now need to do an unchecked conversion of the
1412 -- result to the target type, but it is important that
1413 -- this conversion be a right justified conversion and
1414 -- not a left justified conversion.
1416 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1422 and then Compile_Time_Known_Value (Get_Shift)
1425 Make_Integer_Literal (Loc,
1426 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1427 Set_Print_In_Hex (Or_Rhs);
1430 -- We have to convert the right hand side to Etype (Obj).
1431 -- A special case case arises if what we have now is a Val
1432 -- attribute reference whose expression type is Etype (Obj).
1433 -- This happens for assignments of fields from the same
1434 -- array. In this case we get the required right hand side
1435 -- by simply removing the inner attribute reference.
1437 if Nkind (Rhs) = N_Attribute_Reference
1438 and then Attribute_Name (Rhs) = Name_Val
1439 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1441 Rhs := Relocate_Node (First (Expressions (Rhs)));
1444 -- If the value of the right hand side is a known integer
1445 -- value, then just replace it by an untyped constant,
1446 -- which will be properly retyped when we analyze and
1447 -- resolve the expression.
1449 elsif Rhs_Val_Known then
1451 -- Note that Rhs_Val has already been normalized to
1452 -- be an unsigned value with the proper number of bits.
1455 Make_Integer_Literal (Loc, Rhs_Val);
1457 -- Otherwise we need an unchecked conversion
1463 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1466 if Nkind (New_Rhs) = N_Op_And then
1467 Set_Paren_Count (New_Rhs, 1);
1472 Left_Opnd => New_Rhs,
1473 Right_Opnd => Or_Rhs);
1477 -- Now do the rewrite
1480 Make_Assignment_Statement (Loc,
1483 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1484 Set_Assignment_OK (Name (N), Ass_OK);
1486 -- All other component sizes for non-modular case
1491 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1493 -- where Subscr is the computed linear subscript.
1496 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1502 if No (Bits_nn) then
1504 -- Error, most likely High_Integrity_Mode restriction.
1509 -- Acquire proper Set entity. We use the aligned or unaligned
1510 -- case as appropriate.
1512 if Known_Aligned_Enough (Obj, Csiz) then
1513 Set_nn := RTE (Set_Id (Csiz));
1515 Set_nn := RTE (SetU_Id (Csiz));
1518 -- Now generate the set reference
1520 Obj := Relocate_Node (Prefix (Lhs));
1521 Convert_To_Actual_Subtype (Obj);
1522 Atyp := Etype (Obj);
1523 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1525 -- Below we must make the assumption that Obj is
1526 -- at least byte aligned, since otherwise its address
1527 -- cannot be taken. The assumption holds since the
1528 -- only arrays that can be misaligned are small packed
1529 -- arrays which are implemented as a modular type, and
1530 -- that is not the case here.
1533 Make_Procedure_Call_Statement (Loc,
1534 Name => New_Occurrence_Of (Set_nn, Loc),
1535 Parameter_Associations => New_List (
1536 Make_Attribute_Reference (Loc,
1537 Attribute_Name => Name_Address,
1540 Unchecked_Convert_To (Bits_nn,
1541 Convert_To (Ctyp, Rhs)))));
1546 Analyze (N, Suppress => All_Checks);
1547 end Expand_Bit_Packed_Element_Set;
1549 -------------------------------------
1550 -- Expand_Packed_Address_Reference --
1551 -------------------------------------
1553 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1554 Loc : constant Source_Ptr := Sloc (N);
1566 -- We build up an expression serially that has the form
1568 -- outer_object'Address
1569 -- + (linear-subscript * component_size for each array reference
1570 -- + field'Bit_Position for each record field
1572 -- + ...) / Storage_Unit;
1574 -- Some additional conversions are required to deal with the addition
1575 -- operation, which is not normally visible to generated code.
1578 Ploc := Sloc (Pref);
1580 if Nkind (Pref) = N_Indexed_Component then
1581 Convert_To_Actual_Subtype (Prefix (Pref));
1582 Atyp := Etype (Prefix (Pref));
1583 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1586 Make_Op_Multiply (Ploc,
1587 Left_Opnd => Subscr,
1589 Make_Attribute_Reference (Ploc,
1590 Prefix => New_Occurrence_Of (Atyp, Ploc),
1591 Attribute_Name => Name_Component_Size));
1593 elsif Nkind (Pref) = N_Selected_Component then
1595 Make_Attribute_Reference (Ploc,
1596 Prefix => Selector_Name (Pref),
1597 Attribute_Name => Name_Bit_Position);
1603 Term := Convert_To (RTE (RE_Integer_Address), Term);
1612 Right_Opnd => Term);
1615 Pref := Prefix (Pref);
1619 Unchecked_Convert_To (RTE (RE_Address),
1622 Unchecked_Convert_To (RTE (RE_Integer_Address),
1623 Make_Attribute_Reference (Loc,
1625 Attribute_Name => Name_Address)),
1628 Make_Op_Divide (Loc,
1631 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1633 Analyze_And_Resolve (N, RTE (RE_Address));
1634 end Expand_Packed_Address_Reference;
1636 ------------------------------------
1637 -- Expand_Packed_Boolean_Operator --
1638 ------------------------------------
1640 -- This routine expands "a op b" for the packed cases
1642 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1643 Loc : constant Source_Ptr := Sloc (N);
1644 Typ : constant Entity_Id := Etype (N);
1645 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1646 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1653 Convert_To_Actual_Subtype (L);
1654 Convert_To_Actual_Subtype (R);
1656 Ensure_Defined (Etype (L), N);
1657 Ensure_Defined (Etype (R), N);
1659 Apply_Length_Check (R, Etype (L));
1664 -- First an odd and silly test. We explicitly check for the XOR
1665 -- case where the component type is True .. True, since this will
1666 -- raise constraint error. A special check is required since CE
1667 -- will not be required other wise (cf Expand_Packed_Not).
1669 -- No such check is required for AND and OR, since for both these
1670 -- cases False op False = False, and True op True = True.
1672 if Nkind (N) = N_Op_Xor then
1674 CT : constant Entity_Id := Component_Type (Rtyp);
1675 BT : constant Entity_Id := Base_Type (CT);
1679 Make_Raise_Constraint_Error (Loc,
1685 Make_Attribute_Reference (Loc,
1686 Prefix => New_Occurrence_Of (CT, Loc),
1687 Attribute_Name => Name_First),
1691 New_Occurrence_Of (Standard_True, Loc))),
1696 Make_Attribute_Reference (Loc,
1697 Prefix => New_Occurrence_Of (CT, Loc),
1698 Attribute_Name => Name_Last),
1702 New_Occurrence_Of (Standard_True, Loc)))),
1703 Reason => CE_Range_Check_Failed));
1707 -- Now that that silliness is taken care of, get packed array type
1709 Convert_To_PAT_Type (L);
1710 Convert_To_PAT_Type (R);
1714 -- For the modular case, we expand a op b into
1716 -- rtyp!(pat!(a) op pat!(b))
1718 -- where rtyp is the Etype of the left operand. Note that we do not
1719 -- convert to the base type, since this would be unconstrained, and
1720 -- hence not have a corresponding packed array type set.
1722 -- Note that both operands must be modular for this code to be used.
1724 if Is_Modular_Integer_Type (PAT)
1726 Is_Modular_Integer_Type (Etype (R))
1732 if Nkind (N) = N_Op_And then
1733 P := Make_Op_And (Loc, L, R);
1735 elsif Nkind (N) = N_Op_Or then
1736 P := Make_Op_Or (Loc, L, R);
1738 else -- Nkind (N) = N_Op_Xor
1739 P := Make_Op_Xor (Loc, L, R);
1742 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1745 -- For the array case, we insert the actions
1749 -- System.Bitops.Bit_And/Or/Xor
1751 -- Ltype'Length * Ltype'Component_Size;
1753 -- Rtype'Length * Rtype'Component_Size
1756 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1757 -- the second argument and fourth arguments are the lengths of the
1758 -- operands in bits. Then we replace the expression by a reference
1761 -- Note that if we are mixing a modular and array operand, everything
1762 -- works fine, since we ensure that the modular representation has the
1763 -- same physical layout as the array representation (that's what the
1764 -- left justified modular stuff in the big-endian case is about).
1768 Result_Ent : constant Entity_Id :=
1769 Make_Defining_Identifier (Loc,
1770 Chars => New_Internal_Name ('T'));
1775 if Nkind (N) = N_Op_And then
1778 elsif Nkind (N) = N_Op_Or then
1781 else -- Nkind (N) = N_Op_Xor
1785 Insert_Actions (N, New_List (
1787 Make_Object_Declaration (Loc,
1788 Defining_Identifier => Result_Ent,
1789 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1791 Make_Procedure_Call_Statement (Loc,
1792 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1793 Parameter_Associations => New_List (
1795 Make_Byte_Aligned_Attribute_Reference (Loc,
1796 Attribute_Name => Name_Address,
1799 Make_Op_Multiply (Loc,
1801 Make_Attribute_Reference (Loc,
1804 (Etype (First_Index (Ltyp)), Loc),
1805 Attribute_Name => Name_Range_Length),
1807 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1809 Make_Byte_Aligned_Attribute_Reference (Loc,
1810 Attribute_Name => Name_Address,
1813 Make_Op_Multiply (Loc,
1815 Make_Attribute_Reference (Loc,
1818 (Etype (First_Index (Rtyp)), Loc),
1819 Attribute_Name => Name_Range_Length),
1821 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1823 Make_Byte_Aligned_Attribute_Reference (Loc,
1824 Attribute_Name => Name_Address,
1825 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1828 New_Occurrence_Of (Result_Ent, Loc));
1832 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1833 end Expand_Packed_Boolean_Operator;
1835 -------------------------------------
1836 -- Expand_Packed_Element_Reference --
1837 -------------------------------------
1839 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1840 Loc : constant Source_Ptr := Sloc (N);
1852 -- If not bit packed, we have the enumeration case, which is easily
1853 -- dealt with (just adjust the subscripts of the indexed component)
1855 -- Note: this leaves the result as an indexed component, which is
1856 -- still a variable, so can be used in the assignment case, as is
1857 -- required in the enumeration case.
1859 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1860 Setup_Enumeration_Packed_Array_Reference (N);
1864 -- Remaining processing is for the bit-packed case.
1866 Obj := Relocate_Node (Prefix (N));
1867 Convert_To_Actual_Subtype (Obj);
1868 Atyp := Etype (Obj);
1869 PAT := Packed_Array_Type (Atyp);
1870 Ctyp := Component_Type (Atyp);
1871 Csiz := UI_To_Int (Component_Size (Atyp));
1873 -- Case of component size 1,2,4 or any component size for the modular
1874 -- case. These are the cases for which we can inline the code.
1876 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1877 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1879 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1880 Lit := Make_Integer_Literal (Loc, Cmask);
1881 Set_Print_In_Hex (Lit);
1883 -- We generate a shift right to position the field, followed by a
1884 -- masking operation to extract the bit field, and we finally do an
1885 -- unchecked conversion to convert the result to the required target.
1887 -- Note that the unchecked conversion automatically deals with the
1888 -- bias if we are dealing with a biased representation. What will
1889 -- happen is that we temporarily generate the biased representation,
1890 -- but almost immediately that will be converted to the original
1891 -- unbiased component type, and the bias will disappear.
1895 Left_Opnd => Make_Shift_Right (Obj, Shift),
1898 -- We neded to analyze this before we do the unchecked convert
1899 -- below, but we need it temporarily attached to the tree for
1900 -- this analysis (hence the temporary Set_Parent call).
1902 Set_Parent (Arg, Parent (N));
1903 Analyze_And_Resolve (Arg);
1906 RJ_Unchecked_Convert_To (Ctyp, Arg));
1908 -- All other component sizes for non-modular case
1913 -- Component_Type!(Get_nn (Arr'address, Subscr))
1915 -- where Subscr is the computed linear subscript.
1922 -- Acquire proper Get entity. We use the aligned or unaligned
1923 -- case as appropriate.
1925 if Known_Aligned_Enough (Obj, Csiz) then
1926 Get_nn := RTE (Get_Id (Csiz));
1928 Get_nn := RTE (GetU_Id (Csiz));
1931 -- Now generate the get reference
1933 Compute_Linear_Subscript (Atyp, N, Subscr);
1935 -- Below we make the assumption that Obj is at least byte
1936 -- aligned, since otherwise its address cannot be taken.
1937 -- The assumption holds since the only arrays that can be
1938 -- misaligned are small packed arrays which are implemented
1939 -- as a modular type, and that is not the case here.
1942 Unchecked_Convert_To (Ctyp,
1943 Make_Function_Call (Loc,
1944 Name => New_Occurrence_Of (Get_nn, Loc),
1945 Parameter_Associations => New_List (
1946 Make_Attribute_Reference (Loc,
1947 Attribute_Name => Name_Address,
1953 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1955 end Expand_Packed_Element_Reference;
1957 ----------------------
1958 -- Expand_Packed_Eq --
1959 ----------------------
1961 -- Handles expansion of "=" on packed array types
1963 procedure Expand_Packed_Eq (N : Node_Id) is
1964 Loc : constant Source_Ptr := Sloc (N);
1965 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1966 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1976 Convert_To_Actual_Subtype (L);
1977 Convert_To_Actual_Subtype (R);
1978 Ltyp := Underlying_Type (Etype (L));
1979 Rtyp := Underlying_Type (Etype (R));
1981 Convert_To_PAT_Type (L);
1982 Convert_To_PAT_Type (R);
1986 Make_Op_Multiply (Loc,
1988 Make_Attribute_Reference (Loc,
1989 Attribute_Name => Name_Length,
1990 Prefix => New_Occurrence_Of (Ltyp, Loc)),
1992 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1995 Make_Op_Multiply (Loc,
1997 Make_Attribute_Reference (Loc,
1998 Attribute_Name => Name_Length,
1999 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2001 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2003 -- For the modular case, we transform the comparison to:
2005 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2007 -- where PAT is the packed array type. This works fine, since in the
2008 -- modular case we guarantee that the unused bits are always zeroes.
2009 -- We do have to compare the lengths because we could be comparing
2010 -- two different subtypes of the same base type.
2012 if Is_Modular_Integer_Type (PAT) then
2017 Left_Opnd => LLexpr,
2018 Right_Opnd => RLexpr),
2025 -- For the non-modular case, we call a runtime routine
2027 -- System.Bit_Ops.Bit_Eq
2028 -- (L'Address, L_Length, R'Address, R_Length)
2030 -- where PAT is the packed array type, and the lengths are the lengths
2031 -- in bits of the original packed arrays. This routine takes care of
2032 -- not comparing the unused bits in the last byte.
2036 Make_Function_Call (Loc,
2037 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2038 Parameter_Associations => New_List (
2039 Make_Byte_Aligned_Attribute_Reference (Loc,
2040 Attribute_Name => Name_Address,
2045 Make_Byte_Aligned_Attribute_Reference (Loc,
2046 Attribute_Name => Name_Address,
2052 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2053 end Expand_Packed_Eq;
2055 -----------------------
2056 -- Expand_Packed_Not --
2057 -----------------------
2059 -- Handles expansion of "not" on packed array types
2061 procedure Expand_Packed_Not (N : Node_Id) is
2062 Loc : constant Source_Ptr := Sloc (N);
2063 Typ : constant Entity_Id := Etype (N);
2064 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2071 Convert_To_Actual_Subtype (Opnd);
2072 Rtyp := Etype (Opnd);
2074 -- First an odd and silly test. We explicitly check for the case
2075 -- where the 'First of the component type is equal to the 'Last of
2076 -- this component type, and if this is the case, we make sure that
2077 -- constraint error is raised. The reason is that the NOT is bound
2078 -- to cause CE in this case, and we will not otherwise catch it.
2080 -- Believe it or not, this was reported as a bug. Note that nearly
2081 -- always, the test will evaluate statically to False, so the code
2082 -- will be statically removed, and no extra overhead caused.
2085 CT : constant Entity_Id := Component_Type (Rtyp);
2089 Make_Raise_Constraint_Error (Loc,
2093 Make_Attribute_Reference (Loc,
2094 Prefix => New_Occurrence_Of (CT, Loc),
2095 Attribute_Name => Name_First),
2098 Make_Attribute_Reference (Loc,
2099 Prefix => New_Occurrence_Of (CT, Loc),
2100 Attribute_Name => Name_Last)),
2101 Reason => CE_Range_Check_Failed));
2104 -- Now that that silliness is taken care of, get packed array type
2106 Convert_To_PAT_Type (Opnd);
2107 PAT := Etype (Opnd);
2109 -- For the case where the packed array type is a modular type,
2110 -- not A expands simply into:
2112 -- rtyp!(PAT!(A) xor mask)
2114 -- where PAT is the packed array type, and mask is a mask of all
2115 -- one bits of length equal to the size of this packed type and
2116 -- rtyp is the actual subtype of the operand
2118 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2119 Set_Print_In_Hex (Lit);
2121 if not Is_Array_Type (PAT) then
2123 Unchecked_Convert_To (Rtyp,
2126 Right_Opnd => Lit)));
2128 -- For the array case, we insert the actions
2132 -- System.Bitops.Bit_Not
2134 -- Typ'Length * Typ'Component_Size;
2137 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2138 -- argument is the length of the operand in bits. Then we replace
2139 -- the expression by a reference to Result.
2143 Result_Ent : constant Entity_Id :=
2144 Make_Defining_Identifier (Loc,
2145 Chars => New_Internal_Name ('T'));
2148 Insert_Actions (N, New_List (
2150 Make_Object_Declaration (Loc,
2151 Defining_Identifier => Result_Ent,
2152 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2154 Make_Procedure_Call_Statement (Loc,
2155 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2156 Parameter_Associations => New_List (
2158 Make_Byte_Aligned_Attribute_Reference (Loc,
2159 Attribute_Name => Name_Address,
2162 Make_Op_Multiply (Loc,
2164 Make_Attribute_Reference (Loc,
2167 (Etype (First_Index (Rtyp)), Loc),
2168 Attribute_Name => Name_Range_Length),
2170 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2172 Make_Byte_Aligned_Attribute_Reference (Loc,
2173 Attribute_Name => Name_Address,
2174 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2177 New_Occurrence_Of (Result_Ent, Loc));
2181 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2183 end Expand_Packed_Not;
2185 -------------------------------------
2186 -- Involves_Packed_Array_Reference --
2187 -------------------------------------
2189 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2191 if Nkind (N) = N_Indexed_Component
2192 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2196 elsif Nkind (N) = N_Selected_Component then
2197 return Involves_Packed_Array_Reference (Prefix (N));
2202 end Involves_Packed_Array_Reference;
2204 --------------------------
2205 -- Known_Aligned_Enough --
2206 --------------------------
2208 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2209 Typ : constant Entity_Id := Etype (Obj);
2211 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2212 -- If the component is in a record that contains previous packed
2213 -- components, consider it unaligned because the back-end might
2214 -- choose to pack the rest of the record. Lead to less efficient code,
2215 -- but safer vis-a-vis of back-end choices.
2217 --------------------------------
2218 -- In_Partially_Packed_Record --
2219 --------------------------------
2221 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2222 Rec_Type : constant Entity_Id := Scope (Comp);
2223 Prev_Comp : Entity_Id;
2226 Prev_Comp := First_Entity (Rec_Type);
2227 while Present (Prev_Comp) loop
2228 if Is_Packed (Etype (Prev_Comp)) then
2231 elsif Prev_Comp = Comp then
2235 Next_Entity (Prev_Comp);
2239 end In_Partially_Packed_Record;
2241 -- Start of processing for Known_Aligned_Enough
2244 -- Odd bit sizes don't need alignment anyway
2246 if Csiz mod 2 = 1 then
2249 -- If we have a specified alignment, see if it is sufficient, if not
2250 -- then we can't possibly be aligned enough in any case.
2252 elsif Known_Alignment (Etype (Obj)) then
2253 -- Alignment required is 4 if size is a multiple of 4, and
2254 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2256 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2261 -- OK, alignment should be sufficient, if object is aligned
2263 -- If object is strictly aligned, then it is definitely aligned
2265 if Strict_Alignment (Typ) then
2268 -- Case of subscripted array reference
2270 elsif Nkind (Obj) = N_Indexed_Component then
2272 -- If we have a pointer to an array, then this is definitely
2273 -- aligned, because pointers always point to aligned versions.
2275 if Is_Access_Type (Etype (Prefix (Obj))) then
2278 -- Otherwise, go look at the prefix
2281 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2284 -- Case of record field
2286 elsif Nkind (Obj) = N_Selected_Component then
2288 -- What is significant here is whether the record type is packed
2290 if Is_Record_Type (Etype (Prefix (Obj)))
2291 and then Is_Packed (Etype (Prefix (Obj)))
2295 -- Or the component has a component clause which might cause
2296 -- the component to become unaligned (we can't tell if the
2297 -- backend is doing alignment computations).
2299 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2302 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2305 -- In all other cases, go look at prefix
2308 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2311 elsif Nkind (Obj) = N_Type_Conversion then
2312 return Known_Aligned_Enough (Expression (Obj), Csiz);
2314 -- For a formal parameter, it is safer to assume that it is not
2315 -- aligned, because the formal may be unconstrained while the actual
2316 -- is constrained. In this situation, a small constrained packed
2317 -- array, represented in modular form, may be unaligned.
2319 elsif Is_Entity_Name (Obj) then
2320 return not Is_Formal (Entity (Obj));
2323 -- If none of the above, must be aligned
2326 end Known_Aligned_Enough;
2328 ---------------------
2329 -- Make_Shift_Left --
2330 ---------------------
2332 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2336 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2340 Make_Op_Shift_Left (Sloc (N),
2343 Set_Shift_Count_OK (Nod, True);
2346 end Make_Shift_Left;
2348 ----------------------
2349 -- Make_Shift_Right --
2350 ----------------------
2352 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2356 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2360 Make_Op_Shift_Right (Sloc (N),
2363 Set_Shift_Count_OK (Nod, True);
2366 end Make_Shift_Right;
2368 -----------------------------
2369 -- RJ_Unchecked_Convert_To --
2370 -----------------------------
2372 function RJ_Unchecked_Convert_To
2377 Source_Typ : constant Entity_Id := Etype (Expr);
2378 Target_Typ : constant Entity_Id := Typ;
2380 Src : Node_Id := Expr;
2386 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2387 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2389 -- First step, if the source type is not a discrete type, then we
2390 -- first convert to a modular type of the source length, since
2391 -- otherwise, on a big-endian machine, we get left-justification.
2392 -- We do it for little-endian machines as well, because there might
2393 -- be junk bits that are not cleared if the type is not numeric.
2395 if Source_Siz /= Target_Siz
2396 and then not Is_Discrete_Type (Source_Typ)
2398 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2401 -- In the big endian case, if the lengths of the two types differ,
2402 -- then we must worry about possible left justification in the
2403 -- conversion, and avoiding that is what this is all about.
2405 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2407 -- Next step. If the target is not a discrete type, then we first
2408 -- convert to a modular type of the target length, since
2409 -- otherwise, on a big-endian machine, we get left-justification.
2411 if not Is_Discrete_Type (Target_Typ) then
2412 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2416 -- And now we can do the final conversion to the target type
2418 return Unchecked_Convert_To (Target_Typ, Src);
2419 end RJ_Unchecked_Convert_To;
2421 ----------------------------------------------
2422 -- Setup_Enumeration_Packed_Array_Reference --
2423 ----------------------------------------------
2425 -- All we have to do here is to find the subscripts that correspond
2426 -- to the index positions that have non-standard enumeration types
2427 -- and insert a Pos attribute to get the proper subscript value.
2429 -- Finally the prefix must be uncheck converted to the corresponding
2430 -- packed array type.
2432 -- Note that the component type is unchanged, so we do not need to
2433 -- fiddle with the types (Gigi always automatically takes the packed
2434 -- array type if it is set, as it will be in this case).
2436 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2437 Pfx : constant Node_Id := Prefix (N);
2438 Typ : constant Entity_Id := Etype (N);
2439 Exprs : constant List_Id := Expressions (N);
2443 -- If the array is unconstrained, then we replace the array
2444 -- reference with its actual subtype. This actual subtype will
2445 -- have a packed array type with appropriate bounds.
2447 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2448 Convert_To_Actual_Subtype (Pfx);
2451 Expr := First (Exprs);
2452 while Present (Expr) loop
2454 Loc : constant Source_Ptr := Sloc (Expr);
2455 Expr_Typ : constant Entity_Id := Etype (Expr);
2458 if Is_Enumeration_Type (Expr_Typ)
2459 and then Has_Non_Standard_Rep (Expr_Typ)
2462 Make_Attribute_Reference (Loc,
2463 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2464 Attribute_Name => Name_Pos,
2465 Expressions => New_List (Relocate_Node (Expr))));
2466 Analyze_And_Resolve (Expr, Standard_Natural);
2474 Make_Indexed_Component (Sloc (N),
2476 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2477 Expressions => Exprs));
2479 Analyze_And_Resolve (N, Typ);
2481 end Setup_Enumeration_Packed_Array_Reference;
2483 -----------------------------------------
2484 -- Setup_Inline_Packed_Array_Reference --
2485 -----------------------------------------
2487 procedure Setup_Inline_Packed_Array_Reference
2490 Obj : in out Node_Id;
2492 Shift : out Node_Id)
2494 Loc : constant Source_Ptr := Sloc (N);
2501 Csiz := Component_Size (Atyp);
2503 Convert_To_PAT_Type (Obj);
2506 Cmask := 2 ** Csiz - 1;
2508 if Is_Array_Type (PAT) then
2509 Otyp := Component_Type (PAT);
2510 Osiz := Component_Size (PAT);
2515 -- In the case where the PAT is a modular type, we want the actual
2516 -- size in bits of the modular value we use. This is neither the
2517 -- Object_Size nor the Value_Size, either of which may have been
2518 -- reset to strange values, but rather the minimum size. Note that
2519 -- since this is a modular type with full range, the issue of
2520 -- biased representation does not arise.
2522 Osiz := UI_From_Int (Minimum_Size (Otyp));
2525 Compute_Linear_Subscript (Atyp, N, Shift);
2527 -- If the component size is not 1, then the subscript must be
2528 -- multiplied by the component size to get the shift count.
2532 Make_Op_Multiply (Loc,
2533 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2534 Right_Opnd => Shift);
2537 -- If we have the array case, then this shift count must be broken
2538 -- down into a byte subscript, and a shift within the byte.
2540 if Is_Array_Type (PAT) then
2543 New_Shift : Node_Id;
2546 -- We must analyze shift, since we will duplicate it
2548 Set_Parent (Shift, N);
2550 (Shift, Standard_Integer, Suppress => All_Checks);
2552 -- The shift count within the word is
2557 Left_Opnd => Duplicate_Subexpr (Shift),
2558 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2560 -- The subscript to be used on the PAT array is
2564 Make_Indexed_Component (Loc,
2566 Expressions => New_List (
2567 Make_Op_Divide (Loc,
2568 Left_Opnd => Duplicate_Subexpr (Shift),
2569 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2574 -- For the modular integer case, the object to be manipulated is
2575 -- the entire array, so Obj is unchanged. Note that we will reset
2576 -- its type to PAT before returning to the caller.
2582 -- The one remaining step is to modify the shift count for the
2583 -- big-endian case. Consider the following example in a byte:
2585 -- xxxxxxxx bits of byte
2586 -- vvvvvvvv bits of value
2587 -- 33221100 little-endian numbering
2588 -- 00112233 big-endian numbering
2590 -- Here we have the case of 2-bit fields
2592 -- For the little-endian case, we already have the proper shift
2593 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2595 -- For the big endian case, we have to adjust the shift count,
2596 -- computing it as (N - F) - shift, where N is the number of bits
2597 -- in an element of the array used to implement the packed array,
2598 -- F is the number of bits in a source level array element, and
2599 -- shift is the count so far computed.
2601 if Bytes_Big_Endian then
2603 Make_Op_Subtract (Loc,
2604 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2605 Right_Opnd => Shift);
2608 Set_Parent (Shift, N);
2609 Set_Parent (Obj, N);
2610 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2611 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2613 -- Make sure final type of object is the appropriate packed type
2615 Set_Etype (Obj, Otyp);
2617 end Setup_Inline_Packed_Array_Reference;