1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 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, PASize);
775 if Unknown_RM_Size (PAT) then
776 Set_RM_Size (PAT, PASize);
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);
795 -- If we did allocate a freeze node, then clear out the reference
796 -- since it is obsolete (should we delete the freeze node???)
798 Set_Freeze_Node (PAT, Empty);
799 Set_Freeze_Node (Etype (PAT), Empty);
806 procedure Set_PB_Type is
808 -- If the user has specified an explicit alignment for the
809 -- type or component, take it into account.
811 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
812 or else Alignment (Typ) = 1
813 or else Component_Alignment (Typ) = Calign_Storage_Unit
815 PB_Type := RTE (RE_Packed_Bytes1);
817 elsif Csize mod 4 /= 0
818 or else Alignment (Typ) = 2
820 PB_Type := RTE (RE_Packed_Bytes2);
823 PB_Type := RTE (RE_Packed_Bytes4);
827 -- Start of processing for Create_Packed_Array_Type
830 -- If we already have a packed array type, nothing to do
832 if Present (Packed_Array_Type (Typ)) then
836 -- If our immediate ancestor subtype is constrained, and it already
837 -- has a packed array type, then just share the same type, since the
838 -- bounds must be the same.
840 if Ekind (Typ) = E_Array_Subtype then
841 Ancest := Ancestor_Subtype (Typ);
844 and then Is_Constrained (Ancest)
845 and then Present (Packed_Array_Type (Ancest))
847 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
852 -- We preset the result type size from the size of the original array
853 -- type, since this size clearly belongs to the packed array type. The
854 -- size of the conceptual unpacked type is always set to unknown.
856 PASize := Esize (Typ);
858 -- Case of an array where at least one index is of an enumeration
859 -- type with a non-standard representation, but the component size
860 -- is not appropriate for bit packing. This is the case where we
861 -- have Is_Packed set (we would never be in this unit otherwise),
862 -- but Is_Bit_Packed_Array is false.
864 -- Note that if the component size is appropriate for bit packing,
865 -- then the circuit for the computation of the subscript properly
866 -- deals with the non-standard enumeration type case by taking the
869 if not Is_Bit_Packed_Array (Typ) then
871 -- Here we build a declaration:
873 -- type tttP is array (index1, index2, ...) of component_type
875 -- where index1, index2, are the index types. These are the same
876 -- as the index types of the original array, except for the non-
877 -- standard representation enumeration type case, where we have
880 -- For the unconstrained array case, we use
884 -- For the constrained case, we use
886 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
887 -- Enum_Type'Pos (Enum_Type'Last);
890 Make_Defining_Identifier (Loc,
891 Chars => New_External_Name (Chars (Typ), 'P'));
893 Set_Packed_Array_Type (Typ, PAT);
896 Indexes : constant List_Id := New_List;
898 Indx_Typ : Entity_Id;
903 Indx := First_Index (Typ);
905 while Present (Indx) loop
906 Indx_Typ := Etype (Indx);
908 Enum_Case := Is_Enumeration_Type (Indx_Typ)
909 and then Has_Non_Standard_Rep (Indx_Typ);
911 -- Unconstrained case
913 if not Is_Constrained (Typ) then
915 Indx_Typ := Standard_Natural;
918 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
923 if not Enum_Case then
924 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
928 Make_Subtype_Indication (Loc,
930 New_Occurrence_Of (Standard_Natural, Loc),
932 Make_Range_Constraint (Loc,
936 Make_Attribute_Reference (Loc,
938 New_Occurrence_Of (Indx_Typ, Loc),
939 Attribute_Name => Name_Pos,
940 Expressions => New_List (
941 Make_Attribute_Reference (Loc,
943 New_Occurrence_Of (Indx_Typ, Loc),
944 Attribute_Name => Name_First))),
947 Make_Attribute_Reference (Loc,
949 New_Occurrence_Of (Indx_Typ, Loc),
950 Attribute_Name => Name_Pos,
951 Expressions => New_List (
952 Make_Attribute_Reference (Loc,
954 New_Occurrence_Of (Indx_Typ, Loc),
955 Attribute_Name => Name_Last)))))));
963 if not Is_Constrained (Typ) then
965 Make_Unconstrained_Array_Definition (Loc,
966 Subtype_Marks => Indexes,
967 Component_Definition =>
968 Make_Component_Definition (Loc,
969 Aliased_Present => False,
970 Subtype_Indication =>
971 New_Occurrence_Of (Ctyp, Loc)));
975 Make_Constrained_Array_Definition (Loc,
976 Discrete_Subtype_Definitions => Indexes,
977 Component_Definition =>
978 Make_Component_Definition (Loc,
979 Aliased_Present => False,
980 Subtype_Indication =>
981 New_Occurrence_Of (Ctyp, Loc)));
985 Make_Full_Type_Declaration (Loc,
986 Defining_Identifier => PAT,
987 Type_Definition => Typedef);
990 -- Set type as packed array type and install it
992 Set_Is_Packed_Array_Type (PAT);
996 -- Case of bit-packing required for unconstrained array. We create
997 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
999 elsif not Is_Constrained (Typ) then
1001 Make_Defining_Identifier (Loc,
1002 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1004 Set_Packed_Array_Type (Typ, PAT);
1008 Make_Subtype_Declaration (Loc,
1009 Defining_Identifier => PAT,
1010 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1014 -- Remaining code is for the case of bit-packing for constrained array
1016 -- The name of the packed array subtype is
1020 -- where sss is the component size in bits and ttt is the name of
1021 -- the parent packed type.
1025 Make_Defining_Identifier (Loc,
1026 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1028 Set_Packed_Array_Type (Typ, PAT);
1030 -- Build an expression for the length of the array in bits.
1031 -- This is the product of the length of each of the dimensions
1037 Len_Expr := Empty; -- suppress junk warning
1041 Make_Attribute_Reference (Loc,
1042 Attribute_Name => Name_Length,
1043 Prefix => New_Occurrence_Of (Typ, Loc),
1044 Expressions => New_List (
1045 Make_Integer_Literal (Loc, J)));
1048 Len_Expr := Len_Dim;
1052 Make_Op_Multiply (Loc,
1053 Left_Opnd => Len_Expr,
1054 Right_Opnd => Len_Dim);
1058 exit when J > Number_Dimensions (Typ);
1062 -- Temporarily attach the length expression to the tree and analyze
1063 -- and resolve it, so that we can test its value. We assume that the
1064 -- total length fits in type Integer. This expression may involve
1065 -- discriminants, so we treat it as a default/per-object expression.
1067 Set_Parent (Len_Expr, Typ);
1068 Analyze_Per_Use_Expression (Len_Expr, Standard_Integer);
1070 -- Use a modular type if possible. We can do this if we have
1071 -- static bounds, and the length is small enough, and the length
1072 -- is not zero. We exclude the zero length case because the size
1073 -- of things is always at least one, and the zero length object
1074 -- would have an anomalous size.
1076 if Compile_Time_Known_Value (Len_Expr) then
1077 Len_Bits := Expr_Value (Len_Expr) * Csize;
1079 -- We normally consider small enough to mean no larger than the
1080 -- value of System_Max_Binary_Modulus_Power, checking that in the
1081 -- case of values longer than word size, we have long shifts.
1085 (Len_Bits <= System_Word_Size
1086 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1087 and then Support_Long_Shifts_On_Target))
1089 -- Also test for alignment given. If an alignment is given which
1090 -- is smaller than the natural modular alignment, force the array
1091 -- of bytes representation to accommodate the alignment.
1094 (No (Alignment_Clause (Typ))
1096 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1097 / System_Storage_Unit))
1099 -- We can use the modular type, it has the form:
1101 -- subtype tttPn is btyp
1102 -- range 0 .. 2 ** ((Typ'Length (1)
1103 -- * ... * Typ'Length (n)) * Csize) - 1;
1105 -- The bounds are statically known, and btyp is one
1106 -- of the unsigned types, depending on the length. If the
1107 -- type is its first subtype, i.e. it is a user-defined
1108 -- type, no object of the type will be larger, and it is
1109 -- worthwhile to use a small unsigned type.
1111 if Len_Bits <= Standard_Short_Integer_Size
1112 and then First_Subtype (Typ) = Typ
1114 Btyp := RTE (RE_Short_Unsigned);
1116 elsif Len_Bits <= Standard_Integer_Size then
1117 Btyp := RTE (RE_Unsigned);
1119 elsif Len_Bits <= Standard_Long_Integer_Size then
1120 Btyp := RTE (RE_Long_Unsigned);
1123 Btyp := RTE (RE_Long_Long_Unsigned);
1126 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1127 Set_Print_In_Hex (Lit);
1130 Make_Subtype_Declaration (Loc,
1131 Defining_Identifier => PAT,
1132 Subtype_Indication =>
1133 Make_Subtype_Indication (Loc,
1134 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1137 Make_Range_Constraint (Loc,
1141 Make_Integer_Literal (Loc, 0),
1142 High_Bound => Lit))));
1144 if PASize = Uint_0 then
1153 -- Could not use a modular type, for all other cases, we build
1154 -- a packed array subtype:
1157 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1159 -- Bits is the length of the array in bits
1166 Make_Op_Multiply (Loc,
1168 Make_Integer_Literal (Loc, Csize),
1169 Right_Opnd => Len_Expr),
1172 Make_Integer_Literal (Loc, 7));
1174 Set_Paren_Count (Bits_U1, 1);
1177 Make_Op_Subtract (Loc,
1179 Make_Op_Divide (Loc,
1180 Left_Opnd => Bits_U1,
1181 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1182 Right_Opnd => Make_Integer_Literal (Loc, 1));
1185 Make_Subtype_Declaration (Loc,
1186 Defining_Identifier => PAT,
1187 Subtype_Indication =>
1188 Make_Subtype_Indication (Loc,
1189 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1192 Make_Index_Or_Discriminant_Constraint (Loc,
1193 Constraints => New_List (
1196 Make_Integer_Literal (Loc, 0),
1197 High_Bound => PAT_High)))));
1201 -- Currently the code in this unit requires that packed arrays
1202 -- represented by non-modular arrays of bytes be on a byte
1205 Set_Must_Be_On_Byte_Boundary (Typ);
1207 end Create_Packed_Array_Type;
1209 -----------------------------------
1210 -- Expand_Bit_Packed_Element_Set --
1211 -----------------------------------
1213 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1214 Loc : constant Source_Ptr := Sloc (N);
1215 Lhs : constant Node_Id := Name (N);
1217 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1218 -- Used to preserve assignment OK status when assignment is rewritten
1220 Rhs : Node_Id := Expression (N);
1221 -- Initially Rhs is the right hand side value, it will be replaced
1222 -- later by an appropriate unchecked conversion for the assignment.
1232 -- The expression for the shift value that is required
1234 Shift_Used : Boolean := False;
1235 -- Set True if Shift has been used in the generated code at least
1236 -- once, so that it must be duplicated if used again
1241 Rhs_Val_Known : Boolean;
1243 -- If the value of the right hand side as an integer constant is
1244 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1245 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1246 -- the Rhs_Val is undefined.
1248 function Get_Shift return Node_Id;
1249 -- Function used to get the value of Shift, making sure that it
1250 -- gets duplicated if the function is called more than once.
1256 function Get_Shift return Node_Id is
1258 -- If we used the shift value already, then duplicate it. We
1259 -- set a temporary parent in case actions have to be inserted.
1262 Set_Parent (Shift, N);
1263 return Duplicate_Subexpr_No_Checks (Shift);
1265 -- If first time, use Shift unchanged, and set flag for first use
1273 -- Start of processing for Expand_Bit_Packed_Element_Set
1276 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1278 Obj := Relocate_Node (Prefix (Lhs));
1279 Convert_To_Actual_Subtype (Obj);
1280 Atyp := Etype (Obj);
1281 PAT := Packed_Array_Type (Atyp);
1282 Ctyp := Component_Type (Atyp);
1283 Csiz := UI_To_Int (Component_Size (Atyp));
1285 -- We convert the right hand side to the proper subtype to ensure
1286 -- that an appropriate range check is made (since the normal range
1287 -- check from assignment will be lost in the transformations). This
1288 -- conversion is analyzed immediately so that subsequent processing
1289 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1291 -- If the right-hand side is a string literal, create a temporary for
1292 -- it, constant-folding is not ready to wrap the bit representation
1293 -- of a string literal.
1295 if Nkind (Rhs) = N_String_Literal then
1300 Make_Object_Declaration (Loc,
1301 Defining_Identifier =>
1302 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1303 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1304 Expression => New_Copy_Tree (Rhs));
1306 Insert_Actions (N, New_List (Decl));
1307 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1311 Rhs := Convert_To (Ctyp, Rhs);
1312 Set_Parent (Rhs, N);
1313 Analyze_And_Resolve (Rhs, Ctyp);
1315 -- Case of component size 1,2,4 or any component size for the modular
1316 -- case. These are the cases for which we can inline the code.
1318 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1319 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1321 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1323 -- The statement to be generated is:
1325 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1327 -- where mask1 is obtained by shifting Cmask left Shift bits
1328 -- and then complementing the result.
1330 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1332 -- the "or ..." is omitted if rhs is constant and all 0 bits
1334 -- rhs is converted to the appropriate type.
1336 -- The result is converted back to the array type, since
1337 -- otherwise we lose knowledge of the packed nature.
1339 -- Determine if right side is all 0 bits or all 1 bits
1341 if Compile_Time_Known_Value (Rhs) then
1342 Rhs_Val := Expr_Rep_Value (Rhs);
1343 Rhs_Val_Known := True;
1345 -- The following test catches the case of an unchecked conversion
1346 -- of an integer literal. This results from optimizing aggregates
1349 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1350 and then Compile_Time_Known_Value (Expression (Rhs))
1352 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1353 Rhs_Val_Known := True;
1357 Rhs_Val_Known := False;
1360 -- Some special checks for the case where the right hand value
1361 -- is known at compile time. Basically we have to take care of
1362 -- the implicit conversion to the subtype of the component object.
1364 if Rhs_Val_Known then
1366 -- If we have a biased component type then we must manually do
1367 -- the biasing, since we are taking responsibility in this case
1368 -- for constructing the exact bit pattern to be used.
1370 if Has_Biased_Representation (Ctyp) then
1371 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1374 -- For a negative value, we manually convert the twos complement
1375 -- value to a corresponding unsigned value, so that the proper
1376 -- field width is maintained. If we did not do this, we would
1377 -- get too many leading sign bits later on.
1380 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1384 New_Lhs := Duplicate_Subexpr (Obj, True);
1385 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1387 -- First we deal with the "and"
1389 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1395 if Compile_Time_Known_Value (Shift) then
1397 Make_Integer_Literal (Loc,
1398 Modulus (Etype (Obj)) - 1 -
1399 (Cmask * (2 ** Expr_Value (Get_Shift))));
1400 Set_Print_In_Hex (Mask1);
1403 Lit := Make_Integer_Literal (Loc, Cmask);
1404 Set_Print_In_Hex (Lit);
1407 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1412 Left_Opnd => New_Rhs,
1413 Right_Opnd => Mask1);
1417 -- Then deal with the "or"
1419 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1423 procedure Fixup_Rhs;
1424 -- Adjust Rhs by bias if biased representation for components
1425 -- or remove extraneous high order sign bits if signed.
1427 procedure Fixup_Rhs is
1428 Etyp : constant Entity_Id := Etype (Rhs);
1431 -- For biased case, do the required biasing by simply
1432 -- converting to the biased subtype (the conversion
1433 -- will generate the required bias).
1435 if Has_Biased_Representation (Ctyp) then
1436 Rhs := Convert_To (Ctyp, Rhs);
1438 -- For a signed integer type that is not biased, generate
1439 -- a conversion to unsigned to strip high order sign bits.
1441 elsif Is_Signed_Integer_Type (Ctyp) then
1442 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1445 -- Set Etype, since it can be referenced before the
1446 -- node is completely analyzed.
1448 Set_Etype (Rhs, Etyp);
1450 -- We now need to do an unchecked conversion of the
1451 -- result to the target type, but it is important that
1452 -- this conversion be a right justified conversion and
1453 -- not a left justified conversion.
1455 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1461 and then Compile_Time_Known_Value (Get_Shift)
1464 Make_Integer_Literal (Loc,
1465 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1466 Set_Print_In_Hex (Or_Rhs);
1469 -- We have to convert the right hand side to Etype (Obj).
1470 -- A special case case arises if what we have now is a Val
1471 -- attribute reference whose expression type is Etype (Obj).
1472 -- This happens for assignments of fields from the same
1473 -- array. In this case we get the required right hand side
1474 -- by simply removing the inner attribute reference.
1476 if Nkind (Rhs) = N_Attribute_Reference
1477 and then Attribute_Name (Rhs) = Name_Val
1478 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1480 Rhs := Relocate_Node (First (Expressions (Rhs)));
1483 -- If the value of the right hand side is a known integer
1484 -- value, then just replace it by an untyped constant,
1485 -- which will be properly retyped when we analyze and
1486 -- resolve the expression.
1488 elsif Rhs_Val_Known then
1490 -- Note that Rhs_Val has already been normalized to
1491 -- be an unsigned value with the proper number of bits.
1494 Make_Integer_Literal (Loc, Rhs_Val);
1496 -- Otherwise we need an unchecked conversion
1502 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1505 if Nkind (New_Rhs) = N_Op_And then
1506 Set_Paren_Count (New_Rhs, 1);
1511 Left_Opnd => New_Rhs,
1512 Right_Opnd => Or_Rhs);
1516 -- Now do the rewrite
1519 Make_Assignment_Statement (Loc,
1522 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1523 Set_Assignment_OK (Name (N), Ass_OK);
1525 -- All other component sizes for non-modular case
1530 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1532 -- where Subscr is the computed linear subscript.
1535 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1541 if No (Bits_nn) then
1543 -- Error, most likely High_Integrity_Mode restriction.
1548 -- Acquire proper Set entity. We use the aligned or unaligned
1549 -- case as appropriate.
1551 if Known_Aligned_Enough (Obj, Csiz) then
1552 Set_nn := RTE (Set_Id (Csiz));
1554 Set_nn := RTE (SetU_Id (Csiz));
1557 -- Now generate the set reference
1559 Obj := Relocate_Node (Prefix (Lhs));
1560 Convert_To_Actual_Subtype (Obj);
1561 Atyp := Etype (Obj);
1562 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1564 -- Below we must make the assumption that Obj is
1565 -- at least byte aligned, since otherwise its address
1566 -- cannot be taken. The assumption holds since the
1567 -- only arrays that can be misaligned are small packed
1568 -- arrays which are implemented as a modular type, and
1569 -- that is not the case here.
1572 Make_Procedure_Call_Statement (Loc,
1573 Name => New_Occurrence_Of (Set_nn, Loc),
1574 Parameter_Associations => New_List (
1575 Make_Attribute_Reference (Loc,
1576 Attribute_Name => Name_Address,
1579 Unchecked_Convert_To (Bits_nn,
1580 Convert_To (Ctyp, Rhs)))));
1585 Analyze (N, Suppress => All_Checks);
1586 end Expand_Bit_Packed_Element_Set;
1588 -------------------------------------
1589 -- Expand_Packed_Address_Reference --
1590 -------------------------------------
1592 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1593 Loc : constant Source_Ptr := Sloc (N);
1605 -- We build up an expression serially that has the form
1607 -- outer_object'Address
1608 -- + (linear-subscript * component_size for each array reference
1609 -- + field'Bit_Position for each record field
1611 -- + ...) / Storage_Unit;
1613 -- Some additional conversions are required to deal with the addition
1614 -- operation, which is not normally visible to generated code.
1617 Ploc := Sloc (Pref);
1619 if Nkind (Pref) = N_Indexed_Component then
1620 Convert_To_Actual_Subtype (Prefix (Pref));
1621 Atyp := Etype (Prefix (Pref));
1622 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1625 Make_Op_Multiply (Ploc,
1626 Left_Opnd => Subscr,
1628 Make_Attribute_Reference (Ploc,
1629 Prefix => New_Occurrence_Of (Atyp, Ploc),
1630 Attribute_Name => Name_Component_Size));
1632 elsif Nkind (Pref) = N_Selected_Component then
1634 Make_Attribute_Reference (Ploc,
1635 Prefix => Selector_Name (Pref),
1636 Attribute_Name => Name_Bit_Position);
1642 Term := Convert_To (RTE (RE_Integer_Address), Term);
1651 Right_Opnd => Term);
1654 Pref := Prefix (Pref);
1658 Unchecked_Convert_To (RTE (RE_Address),
1661 Unchecked_Convert_To (RTE (RE_Integer_Address),
1662 Make_Attribute_Reference (Loc,
1664 Attribute_Name => Name_Address)),
1667 Make_Op_Divide (Loc,
1670 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1672 Analyze_And_Resolve (N, RTE (RE_Address));
1673 end Expand_Packed_Address_Reference;
1675 ------------------------------------
1676 -- Expand_Packed_Boolean_Operator --
1677 ------------------------------------
1679 -- This routine expands "a op b" for the packed cases
1681 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1682 Loc : constant Source_Ptr := Sloc (N);
1683 Typ : constant Entity_Id := Etype (N);
1684 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1685 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1692 Convert_To_Actual_Subtype (L);
1693 Convert_To_Actual_Subtype (R);
1695 Ensure_Defined (Etype (L), N);
1696 Ensure_Defined (Etype (R), N);
1698 Apply_Length_Check (R, Etype (L));
1703 -- First an odd and silly test. We explicitly check for the XOR
1704 -- case where the component type is True .. True, since this will
1705 -- raise constraint error. A special check is required since CE
1706 -- will not be required other wise (cf Expand_Packed_Not).
1708 -- No such check is required for AND and OR, since for both these
1709 -- cases False op False = False, and True op True = True.
1711 if Nkind (N) = N_Op_Xor then
1713 CT : constant Entity_Id := Component_Type (Rtyp);
1714 BT : constant Entity_Id := Base_Type (CT);
1718 Make_Raise_Constraint_Error (Loc,
1724 Make_Attribute_Reference (Loc,
1725 Prefix => New_Occurrence_Of (CT, Loc),
1726 Attribute_Name => Name_First),
1730 New_Occurrence_Of (Standard_True, Loc))),
1735 Make_Attribute_Reference (Loc,
1736 Prefix => New_Occurrence_Of (CT, Loc),
1737 Attribute_Name => Name_Last),
1741 New_Occurrence_Of (Standard_True, Loc)))),
1742 Reason => CE_Range_Check_Failed));
1746 -- Now that that silliness is taken care of, get packed array type
1748 Convert_To_PAT_Type (L);
1749 Convert_To_PAT_Type (R);
1753 -- For the modular case, we expand a op b into
1755 -- rtyp!(pat!(a) op pat!(b))
1757 -- where rtyp is the Etype of the left operand. Note that we do not
1758 -- convert to the base type, since this would be unconstrained, and
1759 -- hence not have a corresponding packed array type set.
1761 -- Note that both operands must be modular for this code to be used.
1763 if Is_Modular_Integer_Type (PAT)
1765 Is_Modular_Integer_Type (Etype (R))
1771 if Nkind (N) = N_Op_And then
1772 P := Make_Op_And (Loc, L, R);
1774 elsif Nkind (N) = N_Op_Or then
1775 P := Make_Op_Or (Loc, L, R);
1777 else -- Nkind (N) = N_Op_Xor
1778 P := Make_Op_Xor (Loc, L, R);
1781 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1784 -- For the array case, we insert the actions
1788 -- System.Bitops.Bit_And/Or/Xor
1790 -- Ltype'Length * Ltype'Component_Size;
1792 -- Rtype'Length * Rtype'Component_Size
1795 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1796 -- the second argument and fourth arguments are the lengths of the
1797 -- operands in bits. Then we replace the expression by a reference
1800 -- Note that if we are mixing a modular and array operand, everything
1801 -- works fine, since we ensure that the modular representation has the
1802 -- same physical layout as the array representation (that's what the
1803 -- left justified modular stuff in the big-endian case is about).
1807 Result_Ent : constant Entity_Id :=
1808 Make_Defining_Identifier (Loc,
1809 Chars => New_Internal_Name ('T'));
1814 if Nkind (N) = N_Op_And then
1817 elsif Nkind (N) = N_Op_Or then
1820 else -- Nkind (N) = N_Op_Xor
1824 Insert_Actions (N, New_List (
1826 Make_Object_Declaration (Loc,
1827 Defining_Identifier => Result_Ent,
1828 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1830 Make_Procedure_Call_Statement (Loc,
1831 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1832 Parameter_Associations => New_List (
1834 Make_Byte_Aligned_Attribute_Reference (Loc,
1835 Attribute_Name => Name_Address,
1838 Make_Op_Multiply (Loc,
1840 Make_Attribute_Reference (Loc,
1843 (Etype (First_Index (Ltyp)), Loc),
1844 Attribute_Name => Name_Range_Length),
1846 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1848 Make_Byte_Aligned_Attribute_Reference (Loc,
1849 Attribute_Name => Name_Address,
1852 Make_Op_Multiply (Loc,
1854 Make_Attribute_Reference (Loc,
1857 (Etype (First_Index (Rtyp)), Loc),
1858 Attribute_Name => Name_Range_Length),
1860 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1862 Make_Byte_Aligned_Attribute_Reference (Loc,
1863 Attribute_Name => Name_Address,
1864 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1867 New_Occurrence_Of (Result_Ent, Loc));
1871 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1872 end Expand_Packed_Boolean_Operator;
1874 -------------------------------------
1875 -- Expand_Packed_Element_Reference --
1876 -------------------------------------
1878 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1879 Loc : constant Source_Ptr := Sloc (N);
1891 -- If not bit packed, we have the enumeration case, which is easily
1892 -- dealt with (just adjust the subscripts of the indexed component)
1894 -- Note: this leaves the result as an indexed component, which is
1895 -- still a variable, so can be used in the assignment case, as is
1896 -- required in the enumeration case.
1898 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1899 Setup_Enumeration_Packed_Array_Reference (N);
1903 -- Remaining processing is for the bit-packed case.
1905 Obj := Relocate_Node (Prefix (N));
1906 Convert_To_Actual_Subtype (Obj);
1907 Atyp := Etype (Obj);
1908 PAT := Packed_Array_Type (Atyp);
1909 Ctyp := Component_Type (Atyp);
1910 Csiz := UI_To_Int (Component_Size (Atyp));
1912 -- Case of component size 1,2,4 or any component size for the modular
1913 -- case. These are the cases for which we can inline the code.
1915 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1916 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1918 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1919 Lit := Make_Integer_Literal (Loc, Cmask);
1920 Set_Print_In_Hex (Lit);
1922 -- We generate a shift right to position the field, followed by a
1923 -- masking operation to extract the bit field, and we finally do an
1924 -- unchecked conversion to convert the result to the required target.
1926 -- Note that the unchecked conversion automatically deals with the
1927 -- bias if we are dealing with a biased representation. What will
1928 -- happen is that we temporarily generate the biased representation,
1929 -- but almost immediately that will be converted to the original
1930 -- unbiased component type, and the bias will disappear.
1934 Left_Opnd => Make_Shift_Right (Obj, Shift),
1937 -- We neded to analyze this before we do the unchecked convert
1938 -- below, but we need it temporarily attached to the tree for
1939 -- this analysis (hence the temporary Set_Parent call).
1941 Set_Parent (Arg, Parent (N));
1942 Analyze_And_Resolve (Arg);
1945 RJ_Unchecked_Convert_To (Ctyp, Arg));
1947 -- All other component sizes for non-modular case
1952 -- Component_Type!(Get_nn (Arr'address, Subscr))
1954 -- where Subscr is the computed linear subscript.
1961 -- Acquire proper Get entity. We use the aligned or unaligned
1962 -- case as appropriate.
1964 if Known_Aligned_Enough (Obj, Csiz) then
1965 Get_nn := RTE (Get_Id (Csiz));
1967 Get_nn := RTE (GetU_Id (Csiz));
1970 -- Now generate the get reference
1972 Compute_Linear_Subscript (Atyp, N, Subscr);
1974 -- Below we make the assumption that Obj is at least byte
1975 -- aligned, since otherwise its address cannot be taken.
1976 -- The assumption holds since the only arrays that can be
1977 -- misaligned are small packed arrays which are implemented
1978 -- as a modular type, and that is not the case here.
1981 Unchecked_Convert_To (Ctyp,
1982 Make_Function_Call (Loc,
1983 Name => New_Occurrence_Of (Get_nn, Loc),
1984 Parameter_Associations => New_List (
1985 Make_Attribute_Reference (Loc,
1986 Attribute_Name => Name_Address,
1992 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1994 end Expand_Packed_Element_Reference;
1996 ----------------------
1997 -- Expand_Packed_Eq --
1998 ----------------------
2000 -- Handles expansion of "=" on packed array types
2002 procedure Expand_Packed_Eq (N : Node_Id) is
2003 Loc : constant Source_Ptr := Sloc (N);
2004 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2005 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2015 Convert_To_Actual_Subtype (L);
2016 Convert_To_Actual_Subtype (R);
2017 Ltyp := Underlying_Type (Etype (L));
2018 Rtyp := Underlying_Type (Etype (R));
2020 Convert_To_PAT_Type (L);
2021 Convert_To_PAT_Type (R);
2025 Make_Op_Multiply (Loc,
2027 Make_Attribute_Reference (Loc,
2028 Attribute_Name => Name_Length,
2029 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2031 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2034 Make_Op_Multiply (Loc,
2036 Make_Attribute_Reference (Loc,
2037 Attribute_Name => Name_Length,
2038 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2040 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2042 -- For the modular case, we transform the comparison to:
2044 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2046 -- where PAT is the packed array type. This works fine, since in the
2047 -- modular case we guarantee that the unused bits are always zeroes.
2048 -- We do have to compare the lengths because we could be comparing
2049 -- two different subtypes of the same base type.
2051 if Is_Modular_Integer_Type (PAT) then
2056 Left_Opnd => LLexpr,
2057 Right_Opnd => RLexpr),
2064 -- For the non-modular case, we call a runtime routine
2066 -- System.Bit_Ops.Bit_Eq
2067 -- (L'Address, L_Length, R'Address, R_Length)
2069 -- where PAT is the packed array type, and the lengths are the lengths
2070 -- in bits of the original packed arrays. This routine takes care of
2071 -- not comparing the unused bits in the last byte.
2075 Make_Function_Call (Loc,
2076 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2077 Parameter_Associations => New_List (
2078 Make_Byte_Aligned_Attribute_Reference (Loc,
2079 Attribute_Name => Name_Address,
2084 Make_Byte_Aligned_Attribute_Reference (Loc,
2085 Attribute_Name => Name_Address,
2091 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2092 end Expand_Packed_Eq;
2094 -----------------------
2095 -- Expand_Packed_Not --
2096 -----------------------
2098 -- Handles expansion of "not" on packed array types
2100 procedure Expand_Packed_Not (N : Node_Id) is
2101 Loc : constant Source_Ptr := Sloc (N);
2102 Typ : constant Entity_Id := Etype (N);
2103 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2110 Convert_To_Actual_Subtype (Opnd);
2111 Rtyp := Etype (Opnd);
2113 -- First an odd and silly test. We explicitly check for the case
2114 -- where the 'First of the component type is equal to the 'Last of
2115 -- this component type, and if this is the case, we make sure that
2116 -- constraint error is raised. The reason is that the NOT is bound
2117 -- to cause CE in this case, and we will not otherwise catch it.
2119 -- Believe it or not, this was reported as a bug. Note that nearly
2120 -- always, the test will evaluate statically to False, so the code
2121 -- will be statically removed, and no extra overhead caused.
2124 CT : constant Entity_Id := Component_Type (Rtyp);
2128 Make_Raise_Constraint_Error (Loc,
2132 Make_Attribute_Reference (Loc,
2133 Prefix => New_Occurrence_Of (CT, Loc),
2134 Attribute_Name => Name_First),
2137 Make_Attribute_Reference (Loc,
2138 Prefix => New_Occurrence_Of (CT, Loc),
2139 Attribute_Name => Name_Last)),
2140 Reason => CE_Range_Check_Failed));
2143 -- Now that that silliness is taken care of, get packed array type
2145 Convert_To_PAT_Type (Opnd);
2146 PAT := Etype (Opnd);
2148 -- For the case where the packed array type is a modular type,
2149 -- not A expands simply into:
2151 -- rtyp!(PAT!(A) xor mask)
2153 -- where PAT is the packed array type, and mask is a mask of all
2154 -- one bits of length equal to the size of this packed type and
2155 -- rtyp is the actual subtype of the operand
2157 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2158 Set_Print_In_Hex (Lit);
2160 if not Is_Array_Type (PAT) then
2162 Unchecked_Convert_To (Rtyp,
2165 Right_Opnd => Lit)));
2167 -- For the array case, we insert the actions
2171 -- System.Bitops.Bit_Not
2173 -- Typ'Length * Typ'Component_Size;
2176 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2177 -- argument is the length of the operand in bits. Then we replace
2178 -- the expression by a reference to Result.
2182 Result_Ent : constant Entity_Id :=
2183 Make_Defining_Identifier (Loc,
2184 Chars => New_Internal_Name ('T'));
2187 Insert_Actions (N, New_List (
2189 Make_Object_Declaration (Loc,
2190 Defining_Identifier => Result_Ent,
2191 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2193 Make_Procedure_Call_Statement (Loc,
2194 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2195 Parameter_Associations => New_List (
2197 Make_Byte_Aligned_Attribute_Reference (Loc,
2198 Attribute_Name => Name_Address,
2201 Make_Op_Multiply (Loc,
2203 Make_Attribute_Reference (Loc,
2206 (Etype (First_Index (Rtyp)), Loc),
2207 Attribute_Name => Name_Range_Length),
2209 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2211 Make_Byte_Aligned_Attribute_Reference (Loc,
2212 Attribute_Name => Name_Address,
2213 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2216 New_Occurrence_Of (Result_Ent, Loc));
2220 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2222 end Expand_Packed_Not;
2224 -------------------------------------
2225 -- Involves_Packed_Array_Reference --
2226 -------------------------------------
2228 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2230 if Nkind (N) = N_Indexed_Component
2231 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2235 elsif Nkind (N) = N_Selected_Component then
2236 return Involves_Packed_Array_Reference (Prefix (N));
2241 end Involves_Packed_Array_Reference;
2243 --------------------------
2244 -- Known_Aligned_Enough --
2245 --------------------------
2247 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2248 Typ : constant Entity_Id := Etype (Obj);
2250 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2251 -- If the component is in a record that contains previous packed
2252 -- components, consider it unaligned because the back-end might
2253 -- choose to pack the rest of the record. Lead to less efficient code,
2254 -- but safer vis-a-vis of back-end choices.
2256 --------------------------------
2257 -- In_Partially_Packed_Record --
2258 --------------------------------
2260 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2261 Rec_Type : constant Entity_Id := Scope (Comp);
2262 Prev_Comp : Entity_Id;
2265 Prev_Comp := First_Entity (Rec_Type);
2266 while Present (Prev_Comp) loop
2267 if Is_Packed (Etype (Prev_Comp)) then
2270 elsif Prev_Comp = Comp then
2274 Next_Entity (Prev_Comp);
2278 end In_Partially_Packed_Record;
2280 -- Start of processing for Known_Aligned_Enough
2283 -- Odd bit sizes don't need alignment anyway
2285 if Csiz mod 2 = 1 then
2288 -- If we have a specified alignment, see if it is sufficient, if not
2289 -- then we can't possibly be aligned enough in any case.
2291 elsif Known_Alignment (Etype (Obj)) then
2292 -- Alignment required is 4 if size is a multiple of 4, and
2293 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2295 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2300 -- OK, alignment should be sufficient, if object is aligned
2302 -- If object is strictly aligned, then it is definitely aligned
2304 if Strict_Alignment (Typ) then
2307 -- Case of subscripted array reference
2309 elsif Nkind (Obj) = N_Indexed_Component then
2311 -- If we have a pointer to an array, then this is definitely
2312 -- aligned, because pointers always point to aligned versions.
2314 if Is_Access_Type (Etype (Prefix (Obj))) then
2317 -- Otherwise, go look at the prefix
2320 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2323 -- Case of record field
2325 elsif Nkind (Obj) = N_Selected_Component then
2327 -- What is significant here is whether the record type is packed
2329 if Is_Record_Type (Etype (Prefix (Obj)))
2330 and then Is_Packed (Etype (Prefix (Obj)))
2334 -- Or the component has a component clause which might cause
2335 -- the component to become unaligned (we can't tell if the
2336 -- backend is doing alignment computations).
2338 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2341 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2344 -- In all other cases, go look at prefix
2347 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2350 elsif Nkind (Obj) = N_Type_Conversion then
2351 return Known_Aligned_Enough (Expression (Obj), Csiz);
2353 -- For a formal parameter, it is safer to assume that it is not
2354 -- aligned, because the formal may be unconstrained while the actual
2355 -- is constrained. In this situation, a small constrained packed
2356 -- array, represented in modular form, may be unaligned.
2358 elsif Is_Entity_Name (Obj) then
2359 return not Is_Formal (Entity (Obj));
2362 -- If none of the above, must be aligned
2365 end Known_Aligned_Enough;
2367 ---------------------
2368 -- Make_Shift_Left --
2369 ---------------------
2371 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2375 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2379 Make_Op_Shift_Left (Sloc (N),
2382 Set_Shift_Count_OK (Nod, True);
2385 end Make_Shift_Left;
2387 ----------------------
2388 -- Make_Shift_Right --
2389 ----------------------
2391 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2395 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2399 Make_Op_Shift_Right (Sloc (N),
2402 Set_Shift_Count_OK (Nod, True);
2405 end Make_Shift_Right;
2407 -----------------------------
2408 -- RJ_Unchecked_Convert_To --
2409 -----------------------------
2411 function RJ_Unchecked_Convert_To
2416 Source_Typ : constant Entity_Id := Etype (Expr);
2417 Target_Typ : constant Entity_Id := Typ;
2419 Src : Node_Id := Expr;
2425 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2426 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2428 -- First step, if the source type is not a discrete type, then we
2429 -- first convert to a modular type of the source length, since
2430 -- otherwise, on a big-endian machine, we get left-justification.
2431 -- We do it for little-endian machines as well, because there might
2432 -- be junk bits that are not cleared if the type is not numeric.
2434 if Source_Siz /= Target_Siz
2435 and then not Is_Discrete_Type (Source_Typ)
2437 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2440 -- In the big endian case, if the lengths of the two types differ,
2441 -- then we must worry about possible left justification in the
2442 -- conversion, and avoiding that is what this is all about.
2444 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2446 -- Next step. If the target is not a discrete type, then we first
2447 -- convert to a modular type of the target length, since
2448 -- otherwise, on a big-endian machine, we get left-justification.
2450 if not Is_Discrete_Type (Target_Typ) then
2451 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2455 -- And now we can do the final conversion to the target type
2457 return Unchecked_Convert_To (Target_Typ, Src);
2458 end RJ_Unchecked_Convert_To;
2460 ----------------------------------------------
2461 -- Setup_Enumeration_Packed_Array_Reference --
2462 ----------------------------------------------
2464 -- All we have to do here is to find the subscripts that correspond
2465 -- to the index positions that have non-standard enumeration types
2466 -- and insert a Pos attribute to get the proper subscript value.
2468 -- Finally the prefix must be uncheck converted to the corresponding
2469 -- packed array type.
2471 -- Note that the component type is unchanged, so we do not need to
2472 -- fiddle with the types (Gigi always automatically takes the packed
2473 -- array type if it is set, as it will be in this case).
2475 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2476 Pfx : constant Node_Id := Prefix (N);
2477 Typ : constant Entity_Id := Etype (N);
2478 Exprs : constant List_Id := Expressions (N);
2482 -- If the array is unconstrained, then we replace the array
2483 -- reference with its actual subtype. This actual subtype will
2484 -- have a packed array type with appropriate bounds.
2486 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2487 Convert_To_Actual_Subtype (Pfx);
2490 Expr := First (Exprs);
2491 while Present (Expr) loop
2493 Loc : constant Source_Ptr := Sloc (Expr);
2494 Expr_Typ : constant Entity_Id := Etype (Expr);
2497 if Is_Enumeration_Type (Expr_Typ)
2498 and then Has_Non_Standard_Rep (Expr_Typ)
2501 Make_Attribute_Reference (Loc,
2502 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2503 Attribute_Name => Name_Pos,
2504 Expressions => New_List (Relocate_Node (Expr))));
2505 Analyze_And_Resolve (Expr, Standard_Natural);
2513 Make_Indexed_Component (Sloc (N),
2515 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2516 Expressions => Exprs));
2518 Analyze_And_Resolve (N, Typ);
2520 end Setup_Enumeration_Packed_Array_Reference;
2522 -----------------------------------------
2523 -- Setup_Inline_Packed_Array_Reference --
2524 -----------------------------------------
2526 procedure Setup_Inline_Packed_Array_Reference
2529 Obj : in out Node_Id;
2531 Shift : out Node_Id)
2533 Loc : constant Source_Ptr := Sloc (N);
2540 Csiz := Component_Size (Atyp);
2542 Convert_To_PAT_Type (Obj);
2545 Cmask := 2 ** Csiz - 1;
2547 if Is_Array_Type (PAT) then
2548 Otyp := Component_Type (PAT);
2549 Osiz := Component_Size (PAT);
2554 -- In the case where the PAT is a modular type, we want the actual
2555 -- size in bits of the modular value we use. This is neither the
2556 -- Object_Size nor the Value_Size, either of which may have been
2557 -- reset to strange values, but rather the minimum size. Note that
2558 -- since this is a modular type with full range, the issue of
2559 -- biased representation does not arise.
2561 Osiz := UI_From_Int (Minimum_Size (Otyp));
2564 Compute_Linear_Subscript (Atyp, N, Shift);
2566 -- If the component size is not 1, then the subscript must be
2567 -- multiplied by the component size to get the shift count.
2571 Make_Op_Multiply (Loc,
2572 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2573 Right_Opnd => Shift);
2576 -- If we have the array case, then this shift count must be broken
2577 -- down into a byte subscript, and a shift within the byte.
2579 if Is_Array_Type (PAT) then
2582 New_Shift : Node_Id;
2585 -- We must analyze shift, since we will duplicate it
2587 Set_Parent (Shift, N);
2589 (Shift, Standard_Integer, Suppress => All_Checks);
2591 -- The shift count within the word is
2596 Left_Opnd => Duplicate_Subexpr (Shift),
2597 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2599 -- The subscript to be used on the PAT array is
2603 Make_Indexed_Component (Loc,
2605 Expressions => New_List (
2606 Make_Op_Divide (Loc,
2607 Left_Opnd => Duplicate_Subexpr (Shift),
2608 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2613 -- For the modular integer case, the object to be manipulated is
2614 -- the entire array, so Obj is unchanged. Note that we will reset
2615 -- its type to PAT before returning to the caller.
2621 -- The one remaining step is to modify the shift count for the
2622 -- big-endian case. Consider the following example in a byte:
2624 -- xxxxxxxx bits of byte
2625 -- vvvvvvvv bits of value
2626 -- 33221100 little-endian numbering
2627 -- 00112233 big-endian numbering
2629 -- Here we have the case of 2-bit fields
2631 -- For the little-endian case, we already have the proper shift
2632 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2634 -- For the big endian case, we have to adjust the shift count,
2635 -- computing it as (N - F) - shift, where N is the number of bits
2636 -- in an element of the array used to implement the packed array,
2637 -- F is the number of bits in a source level array element, and
2638 -- shift is the count so far computed.
2640 if Bytes_Big_Endian then
2642 Make_Op_Subtract (Loc,
2643 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2644 Right_Opnd => Shift);
2647 Set_Parent (Shift, N);
2648 Set_Parent (Obj, N);
2649 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2650 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2652 -- Make sure final type of object is the appropriate packed type
2654 Set_Etype (Obj, Otyp);
2656 end Setup_Inline_Packed_Array_Reference;