1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-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 Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Exp_Ch3
; use Exp_Ch3
;
33 with Exp_Util
; use Exp_Util
;
34 with Nlists
; use Nlists
;
35 with Nmake
; use Nmake
;
37 with Repinfo
; use Repinfo
;
39 with Sem_Ch13
; use Sem_Ch13
;
40 with Sem_Eval
; use Sem_Eval
;
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 Layout
is
52 ------------------------
53 -- Local Declarations --
54 ------------------------
56 SSU
: constant Int
:= Ttypes
.System_Storage_Unit
;
57 -- Short hand for System_Storage_Unit
59 Vname
: constant Name_Id
:= Name_uV
;
60 -- Formal parameter name used for functions generated for size offset
61 -- values that depend on the discriminant. All such functions have the
64 -- function xxx (V : vtyp) return Unsigned is
66 -- return ... expression involving V.discrim
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Adjust_Esize_Alignment
(E
: Entity_Id
);
74 -- E is the entity for a type or object. This procedure checks that the
75 -- size and alignment are compatible, and if not either gives an error
76 -- message if they cannot be adjusted or else adjusts them appropriately.
83 -- This is like Make_Op_Add except that it optimizes some cases knowing
84 -- that associative rearrangement is allowed for constant folding if one
85 -- of the operands is a compile time known value.
87 function Assoc_Multiply
92 -- This is like Make_Op_Multiply except that it optimizes some cases
93 -- knowing that associative rearrangement is allowed for constant
94 -- folding if one of the operands is a compile time known value
96 function Assoc_Subtract
101 -- This is like Make_Op_Subtract except that it optimizes some cases
102 -- knowing that associative rearrangement is allowed for constant
103 -- folding if one of the operands is a compile time known value
105 function Bits_To_SU
(N
: Node_Id
) return Node_Id
;
106 -- This is used when we cross the boundary from static sizes in bits to
107 -- dynamic sizes in storage units. If the argument N is anything other
108 -- than an integer literal, it is returned unchanged, but if it is an
109 -- integer literal, then it is taken as a size in bits, and is replaced
110 -- by the corresponding size in bytes.
112 function Compute_Length
(Lo
: Node_Id
; Hi
: Node_Id
) return Node_Id
;
113 -- Given expressions for the low bound (Lo) and the high bound (Hi),
114 -- Build an expression for the value hi-lo+1, converted to type
115 -- Standard.Unsigned. Takes care of the case where the operands
116 -- are of an enumeration type (so that the subtraction cannot be
117 -- done directly) by applying the Pos operator to Hi/Lo first.
119 function Expr_From_SO_Ref
122 Comp
: Entity_Id
:= Empty
)
124 -- Given a value D from a size or offset field, return an expression
125 -- representing the value stored. If the value is known at compile time,
126 -- then an N_Integer_Literal is returned with the appropriate value. If
127 -- the value references a constant entity, then an N_Identifier node
128 -- referencing this entity is returned. If the value denotes a size
129 -- function, then returns a call node denoting the given function, with
130 -- a single actual parameter that either refers to the parameter V of
131 -- an enclosing size function (if Comp is Empty or its type doesn't match
132 -- the function's formal), or else is a selected component V.c when Comp
133 -- denotes a component c whose type matches that of the function formal.
134 -- The Loc value is used for the Sloc value of constructed notes.
136 function SO_Ref_From_Expr
138 Ins_Type
: Entity_Id
;
139 Vtype
: Entity_Id
:= Empty
;
140 Make_Func
: Boolean := False)
141 return Dynamic_SO_Ref
;
142 -- This routine is used in the case where a size/offset value is dynamic
143 -- and is represented by the expression Expr. SO_Ref_From_Expr checks if
144 -- the Expr contains a reference to the identifier V, and if so builds
145 -- a function depending on discriminants of the formal parameter V which
146 -- is of type Vtype. Otherwise, if the parameter Make_Func is True, then
147 -- Expr will be encapsulated in a parameterless function; if Make_Func is
148 -- False, then a constant entity with the value Expr is built. The result
149 -- is a Dynamic_SO_Ref to the created entity. Note that Vtype can be
150 -- omitted if Expr does not contain any reference to V, the created entity.
151 -- The declaration created is inserted in the freeze actions of Ins_Type,
152 -- which also supplies the Sloc for created nodes. This function also takes
153 -- care of making sure that the expression is properly analyzed and
154 -- resolved (which may not be the case yet if we build the expression
157 function Get_Max_Size
(E
: Entity_Id
) return Node_Id
;
158 -- E is an array type or subtype that has at least one index bound that
159 -- is the value of a record discriminant. For such an array, the function
160 -- computes an expression that yields the maximum possible size of the
161 -- array in storage units. The result is not defined for any other type,
162 -- or for arrays that do not depend on discriminants, and it is a fatal
163 -- error to call this unless Size_Depends_On_Discriminant (E) is True.
165 procedure Layout_Array_Type
(E
: Entity_Id
);
166 -- Front-end layout of non-bit-packed array type or subtype
168 procedure Layout_Record_Type
(E
: Entity_Id
);
169 -- Front-end layout of record type
171 procedure Rewrite_Integer
(N
: Node_Id
; V
: Uint
);
172 -- Rewrite node N with an integer literal whose value is V. The Sloc
173 -- for the new node is taken from N, and the type of the literal is
174 -- set to a copy of the type of N on entry.
176 procedure Set_And_Check_Static_Size
180 -- This procedure is called to check explicit given sizes (possibly
181 -- stored in the Esize and RM_Size fields of E) against computed
182 -- Object_Size (Esiz) and Value_Size (RM_Siz) values. Appropriate
183 -- errors and warnings are posted if specified sizes are inconsistent
184 -- with specified sizes. On return, the Esize and RM_Size fields of
185 -- E are set (either from previously given values, or from the newly
186 -- computed values, as appropriate).
188 procedure Set_Composite_Alignment
(E
: Entity_Id
);
189 -- This procedure is called for record types and subtypes, and also for
190 -- atomic array types and subtypes. If no alignment is set, and the size
191 -- is 2 or 4 (or 8 if the word size is 8), then the alignment is set to
194 ----------------------------
195 -- Adjust_Esize_Alignment --
196 ----------------------------
198 procedure Adjust_Esize_Alignment
(E
: Entity_Id
) is
203 -- Nothing to do if size unknown
205 if Unknown_Esize
(E
) then
209 -- Determine if size is constrained by an attribute definition clause
210 -- which must be obeyed. If so, we cannot increase the size in this
213 -- For a type, the issue is whether an object size clause has been
214 -- set. A normal size clause constrains only the value size (RM_Size)
217 Esize_Set
:= Has_Object_Size_Clause
(E
);
219 -- For an object, the issue is whether a size clause is present
222 Esize_Set
:= Has_Size_Clause
(E
);
225 -- If size is known it must be a multiple of the byte size
227 if Esize
(E
) mod SSU
/= 0 then
229 -- If not, and size specified, then give error
233 ("size for& not a multiple of byte size", Size_Clause
(E
), E
);
236 -- Otherwise bump up size to a byte boundary
239 Set_Esize
(E
, (Esize
(E
) + SSU
- 1) / SSU
* SSU
);
243 -- Now we have the size set, it must be a multiple of the alignment
244 -- nothing more we can do here if the alignment is unknown here.
246 if Unknown_Alignment
(E
) then
250 -- At this point both the Esize and Alignment are known, so we need
251 -- to make sure they are consistent.
253 Abits
:= UI_To_Int
(Alignment
(E
)) * SSU
;
255 if Esize
(E
) mod Abits
= 0 then
259 -- Here we have a situation where the Esize is not a multiple of
260 -- the alignment. We must either increase Esize or reduce the
261 -- alignment to correct this situation.
263 -- The case in which we can decrease the alignment is where the
264 -- alignment was not set by an alignment clause, and the type in
265 -- question is a discrete type, where it is definitely safe to
266 -- reduce the alignment. For example:
268 -- t : integer range 1 .. 2;
271 -- In this situation, the initial alignment of t is 4, copied from
272 -- the Integer base type, but it is safe to reduce it to 1 at this
273 -- stage, since we will only be loading a single byte.
275 if Is_Discrete_Type
(Etype
(E
))
276 and then not Has_Alignment_Clause
(E
)
280 exit when Esize
(E
) mod Abits
= 0;
283 Init_Alignment
(E
, Abits
/ SSU
);
287 -- Now the only possible approach left is to increase the Esize
288 -- but we can't do that if the size was set by a specific clause.
292 ("size for& is not a multiple of alignment",
295 -- Otherwise we can indeed increase the size to a multiple of alignment
298 Set_Esize
(E
, ((Esize
(E
) + (Abits
- 1)) / Abits
) * Abits
);
300 end Adjust_Esize_Alignment
;
309 Right_Opnd
: Node_Id
)
316 -- Case of right operand is a constant
318 if Compile_Time_Known_Value
(Right_Opnd
) then
320 R
:= Expr_Value
(Right_Opnd
);
322 -- Case of left operand is a constant
324 elsif Compile_Time_Known_Value
(Left_Opnd
) then
326 R
:= Expr_Value
(Left_Opnd
);
328 -- Neither operand is a constant, do the addition with no optimization
331 return Make_Op_Add
(Loc
, Left_Opnd
, Right_Opnd
);
334 -- Case of left operand is an addition
336 if Nkind
(L
) = N_Op_Add
then
338 -- (C1 + E) + C2 = (C1 + C2) + E
340 if Compile_Time_Known_Value
(Sinfo
.Left_Opnd
(L
)) then
342 (Sinfo
.Left_Opnd
(L
),
343 Expr_Value
(Sinfo
.Left_Opnd
(L
)) + R
);
346 -- (E + C1) + C2 = E + (C1 + C2)
348 elsif Compile_Time_Known_Value
(Sinfo
.Right_Opnd
(L
)) then
350 (Sinfo
.Right_Opnd
(L
),
351 Expr_Value
(Sinfo
.Right_Opnd
(L
)) + R
);
355 -- Case of left operand is a subtraction
357 elsif Nkind
(L
) = N_Op_Subtract
then
359 -- (C1 - E) + C2 = (C1 + C2) + E
361 if Compile_Time_Known_Value
(Sinfo
.Left_Opnd
(L
)) then
363 (Sinfo
.Left_Opnd
(L
),
364 Expr_Value
(Sinfo
.Left_Opnd
(L
)) + R
);
367 -- (E - C1) + C2 = E - (C1 - C2)
369 elsif Compile_Time_Known_Value
(Sinfo
.Right_Opnd
(L
)) then
371 (Sinfo
.Right_Opnd
(L
),
372 Expr_Value
(Sinfo
.Right_Opnd
(L
)) - R
);
377 -- Not optimizable, do the addition
379 return Make_Op_Add
(Loc
, Left_Opnd
, Right_Opnd
);
386 function Assoc_Multiply
389 Right_Opnd
: Node_Id
)
396 -- Case of right operand is a constant
398 if Compile_Time_Known_Value
(Right_Opnd
) then
400 R
:= Expr_Value
(Right_Opnd
);
402 -- Case of left operand is a constant
404 elsif Compile_Time_Known_Value
(Left_Opnd
) then
406 R
:= Expr_Value
(Left_Opnd
);
408 -- Neither operand is a constant, do the multiply with no optimization
411 return Make_Op_Multiply
(Loc
, Left_Opnd
, Right_Opnd
);
414 -- Case of left operand is an multiplication
416 if Nkind
(L
) = N_Op_Multiply
then
418 -- (C1 * E) * C2 = (C1 * C2) + E
420 if Compile_Time_Known_Value
(Sinfo
.Left_Opnd
(L
)) then
422 (Sinfo
.Left_Opnd
(L
),
423 Expr_Value
(Sinfo
.Left_Opnd
(L
)) * R
);
426 -- (E * C1) * C2 = E * (C1 * C2)
428 elsif Compile_Time_Known_Value
(Sinfo
.Right_Opnd
(L
)) then
430 (Sinfo
.Right_Opnd
(L
),
431 Expr_Value
(Sinfo
.Right_Opnd
(L
)) * R
);
436 -- Not optimizable, do the multiplication
438 return Make_Op_Multiply
(Loc
, Left_Opnd
, Right_Opnd
);
445 function Assoc_Subtract
448 Right_Opnd
: Node_Id
)
455 -- Case of right operand is a constant
457 if Compile_Time_Known_Value
(Right_Opnd
) then
459 R
:= Expr_Value
(Right_Opnd
);
461 -- Right operand is a constant, do the subtract with no optimization
464 return Make_Op_Subtract
(Loc
, Left_Opnd
, Right_Opnd
);
467 -- Case of left operand is an addition
469 if Nkind
(L
) = N_Op_Add
then
471 -- (C1 + E) - C2 = (C1 - C2) + E
473 if Compile_Time_Known_Value
(Sinfo
.Left_Opnd
(L
)) then
475 (Sinfo
.Left_Opnd
(L
),
476 Expr_Value
(Sinfo
.Left_Opnd
(L
)) - R
);
479 -- (E + C1) - C2 = E + (C1 - C2)
481 elsif Compile_Time_Known_Value
(Sinfo
.Right_Opnd
(L
)) then
483 (Sinfo
.Right_Opnd
(L
),
484 Expr_Value
(Sinfo
.Right_Opnd
(L
)) - R
);
488 -- Case of left operand is a subtraction
490 elsif Nkind
(L
) = N_Op_Subtract
then
492 -- (C1 - E) - C2 = (C1 - C2) + E
494 if Compile_Time_Known_Value
(Sinfo
.Left_Opnd
(L
)) then
496 (Sinfo
.Left_Opnd
(L
),
497 Expr_Value
(Sinfo
.Left_Opnd
(L
)) + R
);
500 -- (E - C1) - C2 = E - (C1 + C2)
502 elsif Compile_Time_Known_Value
(Sinfo
.Right_Opnd
(L
)) then
504 (Sinfo
.Right_Opnd
(L
),
505 Expr_Value
(Sinfo
.Right_Opnd
(L
)) + R
);
510 -- Not optimizable, do the subtraction
512 return Make_Op_Subtract
(Loc
, Left_Opnd
, Right_Opnd
);
519 function Bits_To_SU
(N
: Node_Id
) return Node_Id
is
521 if Nkind
(N
) = N_Integer_Literal
then
522 Set_Intval
(N
, (Intval
(N
) + (SSU
- 1)) / SSU
);
532 function Compute_Length
(Lo
: Node_Id
; Hi
: Node_Id
) return Node_Id
is
533 Loc
: constant Source_Ptr
:= Sloc
(Lo
);
534 Typ
: constant Entity_Id
:= Etype
(Lo
);
541 -- If the bounds are First and Last attributes for the same dimension
542 -- and both have prefixes that denotes the same entity, then we create
543 -- and return a Length attribute. This may allow the back end to
544 -- generate better code in cases where it already has the length.
546 if Nkind
(Lo
) = N_Attribute_Reference
547 and then Attribute_Name
(Lo
) = Name_First
548 and then Nkind
(Hi
) = N_Attribute_Reference
549 and then Attribute_Name
(Hi
) = Name_Last
550 and then Is_Entity_Name
(Prefix
(Lo
))
551 and then Is_Entity_Name
(Prefix
(Hi
))
552 and then Entity
(Prefix
(Lo
)) = Entity
(Prefix
(Hi
))
557 if Present
(First
(Expressions
(Lo
))) then
558 Lo_Dim
:= Expr_Value
(First
(Expressions
(Lo
)));
561 if Present
(First
(Expressions
(Hi
))) then
562 Hi_Dim
:= Expr_Value
(First
(Expressions
(Hi
)));
565 if Lo_Dim
= Hi_Dim
then
567 Make_Attribute_Reference
(Loc
,
568 Prefix
=> New_Occurrence_Of
569 (Entity
(Prefix
(Lo
)), Loc
),
570 Attribute_Name
=> Name_Length
,
571 Expressions
=> New_List
572 (Make_Integer_Literal
(Loc
, Lo_Dim
)));
576 Lo_Op
:= New_Copy_Tree
(Lo
);
577 Hi_Op
:= New_Copy_Tree
(Hi
);
579 -- If type is enumeration type, then use Pos attribute to convert
580 -- to integer type for which subtraction is a permitted operation.
582 if Is_Enumeration_Type
(Typ
) then
584 Make_Attribute_Reference
(Loc
,
585 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
586 Attribute_Name
=> Name_Pos
,
587 Expressions
=> New_List
(Lo_Op
));
590 Make_Attribute_Reference
(Loc
,
591 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
592 Attribute_Name
=> Name_Pos
,
593 Expressions
=> New_List
(Hi_Op
));
601 Right_Opnd
=> Lo_Op
),
602 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
605 ----------------------
606 -- Expr_From_SO_Ref --
607 ----------------------
609 function Expr_From_SO_Ref
612 Comp
: Entity_Id
:= Empty
)
618 if Is_Dynamic_SO_Ref
(D
) then
619 Ent
:= Get_Dynamic_SO_Entity
(D
);
621 if Is_Discrim_SO_Function
(Ent
) then
622 -- If a component is passed in whose type matches the type
623 -- of the function formal, then select that component from
624 -- the "V" parameter rather than passing "V" directly.
627 and then Base_Type
(Etype
(Comp
))
628 = Base_Type
(Etype
(First_Formal
(Ent
)))
631 Make_Function_Call
(Loc
,
632 Name
=> New_Occurrence_Of
(Ent
, Loc
),
633 Parameter_Associations
=> New_List
(
634 Make_Selected_Component
(Loc
,
635 Prefix
=> Make_Identifier
(Loc
, Chars
=> Vname
),
636 Selector_Name
=> New_Occurrence_Of
(Comp
, Loc
))));
640 Make_Function_Call
(Loc
,
641 Name
=> New_Occurrence_Of
(Ent
, Loc
),
642 Parameter_Associations
=> New_List
(
643 Make_Identifier
(Loc
, Chars
=> Vname
)));
647 return New_Occurrence_Of
(Ent
, Loc
);
651 return Make_Integer_Literal
(Loc
, D
);
653 end Expr_From_SO_Ref
;
659 function Get_Max_Size
(E
: Entity_Id
) return Node_Id
is
660 Loc
: constant Source_Ptr
:= Sloc
(E
);
668 type Val_Status_Type
is (Const
, Dynamic
);
670 type Val_Type
(Status
: Val_Status_Type
:= Const
) is
673 when Const
=> Val
: Uint
;
674 when Dynamic
=> Nod
: Node_Id
;
677 -- Shows the status of the value so far. Const means that the value
678 -- is constant, and Val is the current constant value. Dynamic means
679 -- that the value is dynamic, and in this case Nod is the Node_Id of
680 -- the expression to compute the value.
683 -- Calculated value so far if Size.Status = Const,
684 -- or expression value so far if Size.Status = Dynamic.
686 SU_Convert_Required
: Boolean := False;
687 -- This is set to True if the final result must be converted from
688 -- bits to storage units (rounding up to a storage unit boundary).
690 -----------------------
691 -- Local Subprograms --
692 -----------------------
694 procedure Max_Discrim
(N
: in out Node_Id
);
695 -- If the node N represents a discriminant, replace it by the maximum
696 -- value of the discriminant.
698 procedure Min_Discrim
(N
: in out Node_Id
);
699 -- If the node N represents a discriminant, replace it by the minimum
700 -- value of the discriminant.
706 procedure Max_Discrim
(N
: in out Node_Id
) is
708 if Nkind
(N
) = N_Identifier
709 and then Ekind
(Entity
(N
)) = E_Discriminant
711 N
:= Type_High_Bound
(Etype
(N
));
719 procedure Min_Discrim
(N
: in out Node_Id
) is
721 if Nkind
(N
) = N_Identifier
722 and then Ekind
(Entity
(N
)) = E_Discriminant
724 N
:= Type_Low_Bound
(Etype
(N
));
728 -- Start of processing for Get_Max_Size
731 pragma Assert
(Size_Depends_On_Discriminant
(E
));
733 -- Initialize status from component size
735 if Known_Static_Component_Size
(E
) then
736 Size
:= (Const
, Component_Size
(E
));
739 Size
:= (Dynamic
, Expr_From_SO_Ref
(Loc
, Component_Size
(E
)));
742 -- Loop through indices
744 Indx
:= First_Index
(E
);
745 while Present
(Indx
) loop
746 Ityp
:= Etype
(Indx
);
747 Lo
:= Type_Low_Bound
(Ityp
);
748 Hi
:= Type_High_Bound
(Ityp
);
753 -- Value of the current subscript range is statically known
755 if Compile_Time_Known_Value
(Lo
)
756 and then Compile_Time_Known_Value
(Hi
)
758 S
:= Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1;
760 -- If known flat bound, entire size of array is zero!
763 return Make_Integer_Literal
(Loc
, 0);
766 -- Current value is constant, evolve value
768 if Size
.Status
= Const
then
769 Size
.Val
:= Size
.Val
* S
;
771 -- Current value is dynamic
774 -- An interesting little optimization, if we have a pending
775 -- conversion from bits to storage units, and the current
776 -- length is a multiple of the storage unit size, then we
777 -- can take the factor out here statically, avoiding some
778 -- extra dynamic computations at the end.
780 if SU_Convert_Required
and then S
mod SSU
= 0 then
782 SU_Convert_Required
:= False;
787 Left_Opnd
=> Size
.Nod
,
789 Make_Integer_Literal
(Loc
, Intval
=> S
));
792 -- Value of the current subscript range is dynamic
795 -- If the current size value is constant, then here is where we
796 -- make a transition to dynamic values, which are always stored
797 -- in storage units, However, we do not want to convert to SU's
798 -- too soon, consider the case of a packed array of single bits,
799 -- we want to do the SU conversion after computing the size in
802 if Size
.Status
= Const
then
804 -- If the current value is a multiple of the storage unit,
805 -- then most certainly we can do the conversion now, simply
806 -- by dividing the current value by the storage unit value.
807 -- If this works, we set SU_Convert_Required to False.
809 if Size
.Val
mod SSU
= 0 then
812 (Dynamic
, Make_Integer_Literal
(Loc
, Size
.Val
/ SSU
));
813 SU_Convert_Required
:= False;
815 -- Otherwise, we go ahead and convert the value in bits,
816 -- and set SU_Convert_Required to True to ensure that the
817 -- final value is indeed properly converted.
820 Size
:= (Dynamic
, Make_Integer_Literal
(Loc
, Size
.Val
));
821 SU_Convert_Required
:= True;
827 Len
:= Compute_Length
(Lo
, Hi
);
829 -- Check possible range of Len
838 Determine_Range
(Len
, OK
, LLo
, LHi
);
840 Len
:= Convert_To
(Standard_Unsigned
, Len
);
842 -- If we cannot verify that range cannot be super-flat,
843 -- we need a max with zero, since length must be non-neg.
845 if not OK
or else LLo
< 0 then
847 Make_Attribute_Reference
(Loc
,
849 New_Occurrence_Of
(Standard_Unsigned
, Loc
),
850 Attribute_Name
=> Name_Max
,
851 Expressions
=> New_List
(
852 Make_Integer_Literal
(Loc
, 0),
861 -- Here after processing all bounds to set sizes. If the value is
862 -- a constant, then it is bits, and we just return the value.
864 if Size
.Status
= Const
then
865 return Make_Integer_Literal
(Loc
, Size
.Val
);
867 -- Case where the value is dynamic
870 -- Do convert from bits to SU's if needed
872 if SU_Convert_Required
then
874 -- The expression required is (Size.Nod + SU - 1) / SU
880 Left_Opnd
=> Size
.Nod
,
881 Right_Opnd
=> Make_Integer_Literal
(Loc
, SSU
- 1)),
882 Right_Opnd
=> Make_Integer_Literal
(Loc
, SSU
));
889 -----------------------
890 -- Layout_Array_Type --
891 -----------------------
893 procedure Layout_Array_Type
(E
: Entity_Id
) is
894 Loc
: constant Source_Ptr
:= Sloc
(E
);
895 Ctyp
: constant Entity_Id
:= Component_Type
(E
);
903 Insert_Typ
: Entity_Id
;
904 -- This is the type with which any generated constants or functions
905 -- will be associated (i.e. inserted into the freeze actions). This
906 -- is normally the type being laid out. The exception occurs when
907 -- we are laying out Itype's which are local to a record type, and
908 -- whose scope is this record type. Such types do not have freeze
909 -- nodes (because we have no place to put them).
911 ------------------------------------
912 -- How An Array Type is Laid Out --
913 ------------------------------------
915 -- Here is what goes on. We need to multiply the component size of
916 -- the array (which has already been set) by the length of each of
917 -- the indexes. If all these values are known at compile time, then
918 -- the resulting size of the array is the appropriate constant value.
920 -- If the component size or at least one bound is dynamic (but no
921 -- discriminants are present), then the size will be computed as an
922 -- expression that calculates the proper size.
924 -- If there is at least one discriminant bound, then the size is also
925 -- computed as an expression, but this expression contains discriminant
926 -- values which are obtained by selecting from a function parameter, and
927 -- the size is given by a function that is passed the variant record in
928 -- question, and whose body is the expression.
930 type Val_Status_Type
is (Const
, Dynamic
, Discrim
);
932 type Val_Type
(Status
: Val_Status_Type
:= Const
) is
937 -- Calculated value so far if Val_Status = Const
939 when Dynamic | Discrim
=>
941 -- Expression value so far if Val_Status /= Const
945 -- Records the value or expression computed so far. Const means that
946 -- the value is constant, and Val is the current constant value.
947 -- Dynamic means that the value is dynamic, and in this case Nod is
948 -- the Node_Id of the expression to compute the value, and Discrim
949 -- means that at least one bound is a discriminant, in which case Nod
950 -- is the expression so far (which will be the body of the function).
953 -- Value of size computed so far. See comments above.
955 Vtyp
: Entity_Id
:= Empty
;
956 -- Variant record type for the formal parameter of the
957 -- discriminant function V if Status = Discrim.
959 SU_Convert_Required
: Boolean := False;
960 -- This is set to True if the final result must be converted from
961 -- bits to storage units (rounding up to a storage unit boundary).
963 Storage_Divisor
: Uint
:= UI_From_Int
(SSU
);
964 -- This is the amount that a nonstatic computed size will be divided
965 -- by to convert it from bits to storage units. This is normally
966 -- equal to SSU, but can be reduced in the case of packed components
967 -- that fit evenly into a storage unit.
969 Make_Size_Function
: Boolean := False;
970 -- Indicates whether to request that SO_Ref_From_Expr should
971 -- encapsulate the array size expresion in a function.
973 procedure Discrimify
(N
: in out Node_Id
);
974 -- If N represents a discriminant, then the Size.Status is set to
975 -- Discrim, and Vtyp is set. The parameter N is replaced with the
976 -- proper expression to extract the discriminant value from V.
982 procedure Discrimify
(N
: in out Node_Id
) is
987 if Nkind
(N
) = N_Identifier
988 and then Ekind
(Entity
(N
)) = E_Discriminant
990 Set_Size_Depends_On_Discriminant
(E
);
992 if Size
.Status
/= Discrim
then
993 Decl
:= Parent
(Parent
(Entity
(N
)));
994 Size
:= (Discrim
, Size
.Nod
);
995 Vtyp
:= Defining_Identifier
(Decl
);
997 -- Ensure that we get a private type's full type
999 if Present
(Underlying_Type
(Vtyp
)) then
1000 Vtyp
:= Underlying_Type
(Vtyp
);
1007 Make_Selected_Component
(Loc
,
1008 Prefix
=> Make_Identifier
(Loc
, Chars
=> Vname
),
1009 Selector_Name
=> New_Occurrence_Of
(Entity
(N
), Loc
));
1011 -- Set the Etype attributes of the selected name and its prefix.
1012 -- Analyze_And_Resolve can't be called here because the Vname
1013 -- entity denoted by the prefix will not yet exist (it's created
1014 -- by SO_Ref_From_Expr, called at the end of Layout_Array_Type).
1016 Set_Etype
(Prefix
(N
), Vtyp
);
1021 -- Start of processing for Layout_Array_Type
1024 -- Default alignment is component alignment
1026 if Unknown_Alignment
(E
) then
1027 Set_Alignment
(E
, Alignment
(Ctyp
));
1030 -- Calculate proper type for insertions
1032 if Is_Record_Type
(Scope
(E
)) then
1033 Insert_Typ
:= Scope
(E
);
1038 -- If the component type is a generic formal type then there's no point
1039 -- in determining a size for the array type.
1041 if Is_Generic_Type
(Ctyp
) then
1045 -- Deal with component size if base type
1047 if Ekind
(E
) = E_Array_Type
then
1049 -- Cannot do anything if Esize of component type unknown
1051 if Unknown_Esize
(Ctyp
) then
1055 -- Set component size if not set already
1057 if Unknown_Component_Size
(E
) then
1058 Set_Component_Size
(E
, Esize
(Ctyp
));
1062 -- (RM 13.3 (48)) says that the size of an unconstrained array
1063 -- is implementation defined. We choose to leave it as Unknown
1064 -- here, and the actual behavior is determined by the back end.
1066 if not Is_Constrained
(E
) then
1070 -- Initialize status from component size
1072 if Known_Static_Component_Size
(E
) then
1073 Size
:= (Const
, Component_Size
(E
));
1076 Size
:= (Dynamic
, Expr_From_SO_Ref
(Loc
, Component_Size
(E
)));
1079 -- Loop to process array indices
1081 Indx
:= First_Index
(E
);
1082 while Present
(Indx
) loop
1083 Ityp
:= Etype
(Indx
);
1085 -- If an index of the array is a generic formal type then there's
1086 -- no point in determining a size for the array type.
1088 if Is_Generic_Type
(Ityp
) then
1092 Lo
:= Type_Low_Bound
(Ityp
);
1093 Hi
:= Type_High_Bound
(Ityp
);
1095 -- Value of the current subscript range is statically known
1097 if Compile_Time_Known_Value
(Lo
)
1098 and then Compile_Time_Known_Value
(Hi
)
1100 S
:= Expr_Value
(Hi
) - Expr_Value
(Lo
) + 1;
1102 -- If known flat bound, entire size of array is zero!
1105 Set_Esize
(E
, Uint_0
);
1106 Set_RM_Size
(E
, Uint_0
);
1110 -- If constant, evolve value
1112 if Size
.Status
= Const
then
1113 Size
.Val
:= Size
.Val
* S
;
1115 -- Current value is dynamic
1118 -- An interesting little optimization, if we have a pending
1119 -- conversion from bits to storage units, and the current
1120 -- length is a multiple of the storage unit size, then we
1121 -- can take the factor out here statically, avoiding some
1122 -- extra dynamic computations at the end.
1124 if SU_Convert_Required
and then S
mod SSU
= 0 then
1126 SU_Convert_Required
:= False;
1129 -- Now go ahead and evolve the expression
1132 Assoc_Multiply
(Loc
,
1133 Left_Opnd
=> Size
.Nod
,
1135 Make_Integer_Literal
(Loc
, Intval
=> S
));
1138 -- Value of the current subscript range is dynamic
1141 -- If the current size value is constant, then here is where we
1142 -- make a transition to dynamic values, which are always stored
1143 -- in storage units, However, we do not want to convert to SU's
1144 -- too soon, consider the case of a packed array of single bits,
1145 -- we want to do the SU conversion after computing the size in
1148 if Size
.Status
= Const
then
1150 -- If the current value is a multiple of the storage unit,
1151 -- then most certainly we can do the conversion now, simply
1152 -- by dividing the current value by the storage unit value.
1153 -- If this works, we set SU_Convert_Required to False.
1155 if Size
.Val
mod SSU
= 0 then
1157 (Dynamic
, Make_Integer_Literal
(Loc
, Size
.Val
/ SSU
));
1158 SU_Convert_Required
:= False;
1160 -- If the current value is a factor of the storage unit,
1161 -- then we can use a value of one for the size and reduce
1162 -- the strength of the later division.
1164 elsif SSU
mod Size
.Val
= 0 then
1165 Storage_Divisor
:= SSU
/ Size
.Val
;
1166 Size
:= (Dynamic
, Make_Integer_Literal
(Loc
, Uint_1
));
1167 SU_Convert_Required
:= True;
1169 -- Otherwise, we go ahead and convert the value in bits,
1170 -- and set SU_Convert_Required to True to ensure that the
1171 -- final value is indeed properly converted.
1174 Size
:= (Dynamic
, Make_Integer_Literal
(Loc
, Size
.Val
));
1175 SU_Convert_Required
:= True;
1182 -- Length is hi-lo+1
1184 Len
:= Compute_Length
(Lo
, Hi
);
1186 -- If Len isn't a Length attribute, then its range needs to
1187 -- be checked a possible Max with zero needs to be computed.
1189 if Nkind
(Len
) /= N_Attribute_Reference
1190 or else Attribute_Name
(Len
) /= Name_Length
1198 -- Check possible range of Len
1200 Set_Parent
(Len
, E
);
1201 Determine_Range
(Len
, OK
, LLo
, LHi
);
1203 Len
:= Convert_To
(Standard_Unsigned
, Len
);
1205 -- If range definitely flat or superflat,
1206 -- result size is zero
1208 if OK
and then LHi
<= 0 then
1209 Set_Esize
(E
, Uint_0
);
1210 Set_RM_Size
(E
, Uint_0
);
1214 -- If we cannot verify that range cannot be super-flat,
1215 -- we need a maximum with zero, since length cannot be
1218 if not OK
or else LLo
< 0 then
1220 Make_Attribute_Reference
(Loc
,
1222 New_Occurrence_Of
(Standard_Unsigned
, Loc
),
1223 Attribute_Name
=> Name_Max
,
1224 Expressions
=> New_List
(
1225 Make_Integer_Literal
(Loc
, 0),
1231 -- At this stage, Len has the expression for the length
1234 Assoc_Multiply
(Loc
,
1235 Left_Opnd
=> Size
.Nod
,
1242 -- Here after processing all bounds to set sizes. If the value is
1243 -- a constant, then it is bits, and the only thing we need to do
1244 -- is to check against explicit given size and do alignment adjust.
1246 if Size
.Status
= Const
then
1247 Set_And_Check_Static_Size
(E
, Size
.Val
, Size
.Val
);
1248 Adjust_Esize_Alignment
(E
);
1250 -- Case where the value is dynamic
1253 -- Do convert from bits to SU's if needed
1255 if SU_Convert_Required
then
1257 -- The expression required is:
1258 -- (Size.Nod + Storage_Divisor - 1) / Storage_Divisor
1261 Make_Op_Divide
(Loc
,
1264 Left_Opnd
=> Size
.Nod
,
1265 Right_Opnd
=> Make_Integer_Literal
1266 (Loc
, Storage_Divisor
- 1)),
1267 Right_Opnd
=> Make_Integer_Literal
(Loc
, Storage_Divisor
));
1270 -- If the array entity is not declared at the library level and its
1271 -- not nested within a subprogram that is marked for inlining, then
1272 -- we request that the size expression be encapsulated in a function.
1273 -- Since this expression is not needed in most cases, we prefer not
1274 -- to incur the overhead of the computation on calls to the enclosing
1275 -- subprogram except for subprograms that require the size.
1277 if not Is_Library_Level_Entity
(E
) then
1278 Make_Size_Function
:= True;
1281 Parent_Subp
: Entity_Id
:= Enclosing_Subprogram
(E
);
1284 while Present
(Parent_Subp
) loop
1285 if Is_Inlined
(Parent_Subp
) then
1286 Make_Size_Function
:= False;
1290 Parent_Subp
:= Enclosing_Subprogram
(Parent_Subp
);
1295 -- Now set the dynamic size (the Value_Size is always the same
1296 -- as the Object_Size for arrays whose length is dynamic).
1298 -- ??? If Size.Status = Dynamic, Vtyp will not have been set.
1299 -- The added initialization sets it to Empty now, but is this
1305 (Size
.Nod
, Insert_Typ
, Vtyp
, Make_Func
=> Make_Size_Function
));
1306 Set_RM_Size
(E
, Esize
(E
));
1308 end Layout_Array_Type
;
1314 procedure Layout_Object
(E
: Entity_Id
) is
1315 T
: constant Entity_Id
:= Etype
(E
);
1318 -- Nothing to do if backend does layout
1320 if not Frontend_Layout_On_Target
then
1324 -- Set size if not set for object and known for type. Use the
1325 -- RM_Size if that is known for the type and Esize is not.
1327 if Unknown_Esize
(E
) then
1328 if Known_Esize
(T
) then
1329 Set_Esize
(E
, Esize
(T
));
1331 elsif Known_RM_Size
(T
) then
1332 Set_Esize
(E
, RM_Size
(T
));
1336 -- Set alignment from type if unknown and type alignment known
1338 if Unknown_Alignment
(E
) and then Known_Alignment
(T
) then
1339 Set_Alignment
(E
, Alignment
(T
));
1342 -- Make sure size and alignment are consistent
1344 Adjust_Esize_Alignment
(E
);
1346 -- Final adjustment, if we don't know the alignment, and the Esize
1347 -- was not set by an explicit Object_Size attribute clause, then
1348 -- we reset the Esize to unknown, since we really don't know it.
1350 if Unknown_Alignment
(E
)
1351 and then not Has_Size_Clause
(E
)
1353 Set_Esize
(E
, Uint_0
);
1357 ------------------------
1358 -- Layout_Record_Type --
1359 ------------------------
1361 procedure Layout_Record_Type
(E
: Entity_Id
) is
1362 Loc
: constant Source_Ptr
:= Sloc
(E
);
1366 -- Current component being laid out
1368 Prev_Comp
: Entity_Id
;
1369 -- Previous laid out component
1371 procedure Get_Next_Component_Location
1372 (Prev_Comp
: Entity_Id
;
1374 New_Npos
: out SO_Ref
;
1375 New_Fbit
: out SO_Ref
;
1376 New_NPMax
: out SO_Ref
;
1377 Force_SU
: Boolean);
1378 -- Given the previous component in Prev_Comp, which is already laid
1379 -- out, and the alignment of the following component, lays out the
1380 -- following component, and returns its starting position in New_Npos
1381 -- (Normalized_Position value), New_Fbit (Normalized_First_Bit value),
1382 -- and New_NPMax (Normalized_Position_Max value). If Prev_Comp is empty
1383 -- (no previous component is present), then New_Npos, New_Fbit and
1384 -- New_NPMax are all set to zero on return. This procedure is also
1385 -- used to compute the size of a record or variant by giving it the
1386 -- last component, and the record alignment. Force_SU is used to force
1387 -- the new component location to be aligned on a storage unit boundary,
1388 -- even in a packed record, False means that the new position does not
1389 -- need to be bumped to a storage unit boundary, True means a storage
1390 -- unit boundary is always required.
1392 procedure Layout_Component
(Comp
: Entity_Id
; Prev_Comp
: Entity_Id
);
1393 -- Lays out component Comp, given Prev_Comp, the previously laid-out
1394 -- component (Prev_Comp = Empty if no components laid out yet). The
1395 -- alignment of the record itself is also updated if needed. Both
1396 -- Comp and Prev_Comp can be either components or discriminants.
1398 procedure Layout_Components
1402 RM_Siz
: out SO_Ref
);
1403 -- This procedure lays out the components of the given component list
1404 -- which contains the components starting with From and ending with To.
1405 -- The Next_Entity chain is used to traverse the components. On entry,
1406 -- Prev_Comp is set to the component preceding the list, so that the
1407 -- list is laid out after this component. Prev_Comp is set to Empty if
1408 -- the component list is to be laid out starting at the start of the
1409 -- record. On return, the components are all laid out, and Prev_Comp is
1410 -- set to the last laid out component. On return, Esiz is set to the
1411 -- resulting Object_Size value, which is the length of the record up
1412 -- to and including the last laid out entity. For Esiz, the value is
1413 -- adjusted to match the alignment of the record. RM_Siz is similarly
1414 -- set to the resulting Value_Size value, which is the same length, but
1415 -- not adjusted to meet the alignment. Note that in the case of variant
1416 -- records, Esiz represents the maximum size.
1418 procedure Layout_Non_Variant_Record
;
1419 -- Procedure called to lay out a non-variant record type or subtype
1421 procedure Layout_Variant_Record
;
1422 -- Procedure called to lay out a variant record type. Decl is set to the
1423 -- full type declaration for the variant record.
1425 ---------------------------------
1426 -- Get_Next_Component_Location --
1427 ---------------------------------
1429 procedure Get_Next_Component_Location
1430 (Prev_Comp
: Entity_Id
;
1432 New_Npos
: out SO_Ref
;
1433 New_Fbit
: out SO_Ref
;
1434 New_NPMax
: out SO_Ref
;
1438 -- No previous component, return zero position
1440 if No
(Prev_Comp
) then
1443 New_NPMax
:= Uint_0
;
1447 -- Here we have a previous component
1450 Loc
: constant Source_Ptr
:= Sloc
(Prev_Comp
);
1452 Old_Npos
: constant SO_Ref
:= Normalized_Position
(Prev_Comp
);
1453 Old_Fbit
: constant SO_Ref
:= Normalized_First_Bit
(Prev_Comp
);
1454 Old_NPMax
: constant SO_Ref
:= Normalized_Position_Max
(Prev_Comp
);
1455 Old_Esiz
: constant SO_Ref
:= Esize
(Prev_Comp
);
1457 Old_Maxsz
: Node_Id
;
1458 -- Expression representing maximum size of previous component
1461 -- Case where previous field had a dynamic size
1463 if Is_Dynamic_SO_Ref
(Esize
(Prev_Comp
)) then
1465 -- If the previous field had a dynamic length, then it is
1466 -- required to occupy an integral number of storage units,
1467 -- and start on a storage unit boundary. This means that
1468 -- the Normalized_First_Bit value is zero in the previous
1469 -- component, and the new value is also set to zero.
1473 -- In this case, the new position is given by an expression
1474 -- that is the sum of old normalized position and old size.
1480 Expr_From_SO_Ref
(Loc
, Old_Npos
),
1482 Expr_From_SO_Ref
(Loc
, Old_Esiz
, Prev_Comp
)),
1486 -- Get maximum size of previous component
1488 if Size_Depends_On_Discriminant
(Etype
(Prev_Comp
)) then
1489 Old_Maxsz
:= Get_Max_Size
(Etype
(Prev_Comp
));
1491 Old_Maxsz
:= Expr_From_SO_Ref
(Loc
, Old_Esiz
, Prev_Comp
);
1494 -- Now we can compute the new max position. If the max size
1495 -- is static and the old position is static, then we can
1496 -- compute the new position statically.
1498 if Nkind
(Old_Maxsz
) = N_Integer_Literal
1499 and then Known_Static_Normalized_Position_Max
(Prev_Comp
)
1501 New_NPMax
:= Old_NPMax
+ Intval
(Old_Maxsz
);
1503 -- Otherwise new max position is dynamic
1509 Left_Opnd
=> Expr_From_SO_Ref
(Loc
, Old_NPMax
),
1510 Right_Opnd
=> Old_Maxsz
),
1515 -- Previous field has known static Esize
1518 New_Fbit
:= Old_Fbit
+ Old_Esiz
;
1520 -- Bump New_Fbit to storage unit boundary if required
1522 if New_Fbit
/= 0 and then Force_SU
then
1523 New_Fbit
:= (New_Fbit
+ SSU
- 1) / SSU
* SSU
;
1526 -- If old normalized position is static, we can go ahead
1527 -- and compute the new normalized position directly.
1529 if Known_Static_Normalized_Position
(Prev_Comp
) then
1530 New_Npos
:= Old_Npos
;
1532 if New_Fbit
>= SSU
then
1533 New_Npos
:= New_Npos
+ New_Fbit
/ SSU
;
1534 New_Fbit
:= New_Fbit
mod SSU
;
1537 -- Bump alignment if stricter than prev
1539 if Align
> Alignment
(Etype
(Prev_Comp
)) then
1540 New_Npos
:= (New_Npos
+ Align
- 1) / Align
* Align
;
1543 -- The max position is always equal to the position if
1544 -- the latter is static, since arrays depending on the
1545 -- values of discriminants never have static sizes.
1547 New_NPMax
:= New_Npos
;
1550 -- Case of old normalized position is dynamic
1553 -- If new bit position is within the current storage unit,
1554 -- we can just copy the old position as the result position
1555 -- (we have already set the new first bit value).
1557 if New_Fbit
< SSU
then
1558 New_Npos
:= Old_Npos
;
1559 New_NPMax
:= Old_NPMax
;
1561 -- If new bit position is past the current storage unit, we
1562 -- need to generate a new dynamic value for the position
1563 -- ??? need to deal with alignment
1569 Left_Opnd
=> Expr_From_SO_Ref
(Loc
, Old_Npos
),
1571 Make_Integer_Literal
(Loc
,
1572 Intval
=> New_Fbit
/ SSU
)),
1579 Left_Opnd
=> Expr_From_SO_Ref
(Loc
, Old_NPMax
),
1581 Make_Integer_Literal
(Loc
,
1582 Intval
=> New_Fbit
/ SSU
)),
1585 New_Fbit
:= New_Fbit
mod SSU
;
1590 end Get_Next_Component_Location
;
1592 ----------------------
1593 -- Layout_Component --
1594 ----------------------
1596 procedure Layout_Component
(Comp
: Entity_Id
; Prev_Comp
: Entity_Id
) is
1597 Ctyp
: constant Entity_Id
:= Etype
(Comp
);
1604 -- Parent field is always at start of record, this will overlap
1605 -- the actual fields that are part of the parent, and that's fine
1607 if Chars
(Comp
) = Name_uParent
then
1608 Set_Normalized_Position
(Comp
, Uint_0
);
1609 Set_Normalized_First_Bit
(Comp
, Uint_0
);
1610 Set_Normalized_Position_Max
(Comp
, Uint_0
);
1611 Set_Component_Bit_Offset
(Comp
, Uint_0
);
1612 Set_Esize
(Comp
, Esize
(Ctyp
));
1616 -- Check case of type of component has a scope of the record we
1617 -- are laying out. When this happens, the type in question is an
1618 -- Itype that has not yet been laid out (that's because such
1619 -- types do not get frozen in the normal manner, because there
1620 -- is no place for the freeze nodes).
1622 if Scope
(Ctyp
) = E
then
1626 -- Increase alignment of record if necessary. Note that we do not
1627 -- do this for packed records, which have an alignment of one by
1628 -- default, or for records for which an explicit alignment was
1629 -- specified with an alignment clause.
1631 if not Is_Packed
(E
)
1632 and then not Has_Alignment_Clause
(E
)
1633 and then Alignment
(Ctyp
) > Alignment
(E
)
1635 Set_Alignment
(E
, Alignment
(Ctyp
));
1638 -- If component already laid out, then we are done
1640 if Known_Normalized_Position
(Comp
) then
1644 -- Set size of component from type. We use the Esize except in a
1645 -- packed record, where we use the RM_Size (since that is exactly
1646 -- what the RM_Size value, as distinct from the Object_Size is
1649 if Is_Packed
(E
) then
1650 Set_Esize
(Comp
, RM_Size
(Ctyp
));
1652 Set_Esize
(Comp
, Esize
(Ctyp
));
1655 -- Compute the component position from the previous one. See if
1656 -- current component requires being on a storage unit boundary.
1658 -- If record is not packed, we always go to a storage unit boundary
1660 if not Is_Packed
(E
) then
1666 -- Elementary types do not need SU boundary in packed record
1668 if Is_Elementary_Type
(Ctyp
) then
1671 -- Packed array types with a modular packed array type do not
1672 -- force a storage unit boundary (since the code generation
1673 -- treats these as equivalent to the underlying modular type),
1675 elsif Is_Array_Type
(Ctyp
)
1676 and then Is_Bit_Packed_Array
(Ctyp
)
1677 and then Is_Modular_Integer_Type
(Packed_Array_Type
(Ctyp
))
1681 -- Record types with known length less than or equal to the length
1682 -- of long long integer can also be unaligned, since they can be
1683 -- treated as scalars.
1685 elsif Is_Record_Type
(Ctyp
)
1686 and then not Is_Dynamic_SO_Ref
(Esize
(Ctyp
))
1687 and then Esize
(Ctyp
) <= Esize
(Standard_Long_Long_Integer
)
1691 -- All other cases force a storage unit boundary, even when packed
1698 -- Now get the next component location
1700 Get_Next_Component_Location
1701 (Prev_Comp
, Alignment
(Ctyp
), Npos
, Fbit
, NPMax
, Forc
);
1702 Set_Normalized_Position
(Comp
, Npos
);
1703 Set_Normalized_First_Bit
(Comp
, Fbit
);
1704 Set_Normalized_Position_Max
(Comp
, NPMax
);
1706 -- Set Component_Bit_Offset in the static case
1708 if Known_Static_Normalized_Position
(Comp
)
1709 and then Known_Normalized_First_Bit
(Comp
)
1711 Set_Component_Bit_Offset
(Comp
, SSU
* Npos
+ Fbit
);
1713 end Layout_Component
;
1715 -----------------------
1716 -- Layout_Components --
1717 -----------------------
1719 procedure Layout_Components
1723 RM_Siz
: out SO_Ref
)
1730 -- Only lay out components if there are some to lay out!
1732 if Present
(From
) then
1734 -- Lay out components with no component clauses
1738 if Ekind
(Comp
) = E_Component
1739 or else Ekind
(Comp
) = E_Discriminant
1741 -- The compatibility of component clauses with composite
1742 -- types isn't checked in Sem_Ch13, so we check it here.
1744 if Present
(Component_Clause
(Comp
)) then
1745 if Is_Composite_Type
(Etype
(Comp
))
1746 and then Esize
(Comp
) < RM_Size
(Etype
(Comp
))
1748 Error_Msg_Uint_1
:= RM_Size
(Etype
(Comp
));
1750 ("size for & too small, minimum allowed is ^",
1751 Component_Clause
(Comp
),
1756 Layout_Component
(Comp
, Prev_Comp
);
1761 exit when Comp
= To
;
1766 -- Set size fields, both are zero if no components
1768 if No
(Prev_Comp
) then
1773 -- First the object size, for which we align past the last
1774 -- field to the alignment of the record (the object size
1775 -- is required to be a multiple of the alignment).
1777 Get_Next_Component_Location
1785 -- If the resulting normalized position is a dynamic reference,
1786 -- then the size is dynamic, and is stored in storage units.
1787 -- In this case, we set the RM_Size to the same value, it is
1788 -- simply not worth distinguishing Esize and RM_Size values in
1789 -- the dynamic case, since the RM has nothing to say about them.
1791 -- Note that a size cannot have been given in this case, since
1792 -- size specifications cannot be given for variable length types.
1795 Align
: constant Uint
:= Alignment
(E
);
1798 if Is_Dynamic_SO_Ref
(End_Npos
) then
1801 -- Set the Object_Size allowing for alignment. In the
1802 -- dynamic case, we have to actually do the runtime
1803 -- computation. We can skip this in the non-packed
1804 -- record case if the last component has a smaller
1805 -- alignment than the overall record alignment.
1807 if Is_Dynamic_SO_Ref
(End_NPMax
) then
1811 or else Alignment
(Etype
(Prev_Comp
)) < Align
1813 -- The expression we build is
1814 -- (expr + align - 1) / align * align
1819 Make_Op_Multiply
(Loc
,
1821 Make_Op_Divide
(Loc
,
1825 Expr_From_SO_Ref
(Loc
, Esiz
),
1827 Make_Integer_Literal
(Loc
,
1828 Intval
=> Align
- 1)),
1830 Make_Integer_Literal
(Loc
, Align
)),
1832 Make_Integer_Literal
(Loc
, Align
)),
1837 -- Here Esiz is static, so we can adjust the alignment
1838 -- directly go give the required aligned value.
1841 Esiz
:= (End_NPMax
+ Align
- 1) / Align
* Align
* SSU
;
1844 -- Case where computed size is static
1847 -- The ending size was computed in Npos in storage units,
1848 -- but the actual size is stored in bits, so adjust
1849 -- accordingly. We also adjust the size to match the
1852 Esiz
:= (End_NPMax
+ Align
- 1) / Align
* Align
* SSU
;
1854 -- Compute the resulting Value_Size (RM_Size). For this
1855 -- purpose we do not force alignment of the record or
1856 -- storage size alignment of the result.
1858 Get_Next_Component_Location
1866 RM_Siz
:= End_Npos
* SSU
+ End_Fbit
;
1867 Set_And_Check_Static_Size
(E
, Esiz
, RM_Siz
);
1871 end Layout_Components
;
1873 -------------------------------
1874 -- Layout_Non_Variant_Record --
1875 -------------------------------
1877 procedure Layout_Non_Variant_Record
is
1882 Layout_Components
(First_Entity
(E
), Last_Entity
(E
), Esiz
, RM_Siz
);
1883 Set_Esize
(E
, Esiz
);
1884 Set_RM_Size
(E
, RM_Siz
);
1885 end Layout_Non_Variant_Record
;
1887 ---------------------------
1888 -- Layout_Variant_Record --
1889 ---------------------------
1891 procedure Layout_Variant_Record
is
1892 Tdef
: constant Node_Id
:= Type_Definition
(Decl
);
1893 Dlist
: constant List_Id
:= Discriminant_Specifications
(Decl
);
1897 RM_Siz_Expr
: Node_Id
:= Empty
;
1898 -- Expression for the evolving RM_Siz value. This is typically a
1899 -- conditional expression which involves tests of discriminant
1900 -- values that are formed as references to the entity V. At
1901 -- the end of scanning all the components, a suitable function
1902 -- is constructed in which V is the parameter.
1904 -----------------------
1905 -- Local Subprograms --
1906 -----------------------
1908 procedure Layout_Component_List
1911 RM_Siz_Expr
: out Node_Id
);
1912 -- Recursive procedure, called to lay out one component list
1913 -- Esiz and RM_Siz_Expr are set to the Object_Size and Value_Size
1914 -- values respectively representing the record size up to and
1915 -- including the last component in the component list (including
1916 -- any variants in this component list). RM_Siz_Expr is returned
1917 -- as an expression which may in the general case involve some
1918 -- references to the discriminants of the current record value,
1919 -- referenced by selecting from the entity V.
1921 ---------------------------
1922 -- Layout_Component_List --
1923 ---------------------------
1925 procedure Layout_Component_List
1928 RM_Siz_Expr
: out Node_Id
)
1930 Citems
: constant List_Id
:= Component_Items
(Clist
);
1931 Vpart
: constant Node_Id
:= Variant_Part
(Clist
);
1935 RMS_Ent
: Entity_Id
;
1938 if Is_Non_Empty_List
(Citems
) then
1940 (From
=> Defining_Identifier
(First
(Citems
)),
1941 To
=> Defining_Identifier
(Last
(Citems
)),
1945 Layout_Components
(Empty
, Empty
, Esiz
, RM_Siz
);
1948 -- Case where no variants are present in the component list
1952 -- The Esiz value has been correctly set by the call to
1953 -- Layout_Components, so there is nothing more to be done.
1955 -- For RM_Siz, we have an SO_Ref value, which we must convert
1956 -- to an appropriate expression.
1958 if Is_Static_SO_Ref
(RM_Siz
) then
1960 Make_Integer_Literal
(Loc
,
1964 RMS_Ent
:= Get_Dynamic_SO_Entity
(RM_Siz
);
1966 -- If the size is represented by a function, then we
1967 -- create an appropriate function call using V as
1968 -- the parameter to the call.
1970 if Is_Discrim_SO_Function
(RMS_Ent
) then
1972 Make_Function_Call
(Loc
,
1973 Name
=> New_Occurrence_Of
(RMS_Ent
, Loc
),
1974 Parameter_Associations
=> New_List
(
1975 Make_Identifier
(Loc
, Chars
=> Vname
)));
1977 -- If the size is represented by a constant, then the
1978 -- expression we want is a reference to this constant
1981 RM_Siz_Expr
:= New_Occurrence_Of
(RMS_Ent
, Loc
);
1985 -- Case where variants are present in this component list
1995 D_Entity
: Entity_Id
;
1998 RM_Siz_Expr
:= Empty
;
2001 Var
:= Last
(Variants
(Vpart
));
2002 while Present
(Var
) loop
2004 Layout_Component_List
2005 (Component_List
(Var
), EsizV
, RM_SizV
);
2007 -- Set the Object_Size. If this is the first variant,
2008 -- we just set the size of this first variant.
2010 if Var
= Last
(Variants
(Vpart
)) then
2013 -- Otherwise the Object_Size is formed as a maximum
2014 -- of Esiz so far from previous variants, and the new
2015 -- Esiz value from the variant we just processed.
2017 -- If both values are static, we can just compute the
2018 -- maximum directly to save building junk nodes.
2020 elsif not Is_Dynamic_SO_Ref
(Esiz
)
2021 and then not Is_Dynamic_SO_Ref
(EsizV
)
2023 Esiz
:= UI_Max
(Esiz
, EsizV
);
2025 -- If either value is dynamic, then we have to generate
2026 -- an appropriate Standard_Unsigned'Max attribute call.
2031 (Make_Attribute_Reference
(Loc
,
2032 Attribute_Name
=> Name_Max
,
2034 New_Occurrence_Of
(Standard_Unsigned
, Loc
),
2035 Expressions
=> New_List
(
2036 Expr_From_SO_Ref
(Loc
, Esiz
),
2037 Expr_From_SO_Ref
(Loc
, EsizV
))),
2042 -- Now deal with Value_Size (RM_Siz). We are aiming at
2043 -- an expression that looks like:
2045 -- if xxDx (V.disc) then rmsiz1
2046 -- else if xxDx (V.disc) then rmsiz2
2049 -- Where rmsiz1, rmsiz2... are the RM_Siz values for the
2050 -- individual variants, and xxDx are the discriminant
2051 -- checking functions generated for the variant type.
2053 -- If this is the first variant, we simply set the
2054 -- result as the expression. Note that this takes
2055 -- care of the others case.
2057 if No
(RM_Siz_Expr
) then
2058 RM_Siz_Expr
:= Bits_To_SU
(RM_SizV
);
2060 -- Otherwise construct the appropriate test
2063 -- The test to be used in general is a call to the
2064 -- discriminant checking function. However, it is
2065 -- definitely worth special casing the very common
2066 -- case where a single value is involved.
2068 Dchoice
:= First
(Discrete_Choices
(Var
));
2070 if No
(Next
(Dchoice
))
2071 and then Nkind
(Dchoice
) /= N_Range
2073 -- Discriminant to be tested
2076 Make_Selected_Component
(Loc
,
2078 Make_Identifier
(Loc
, Chars
=> Vname
),
2081 (Entity
(Name
(Vpart
)), Loc
));
2085 Left_Opnd
=> Discrim
,
2086 Right_Opnd
=> New_Copy
(Dchoice
));
2088 -- Generate a call to the discriminant-checking
2089 -- function for the variant. Note that the result
2090 -- has to be complemented since the function returns
2091 -- False when the passed discriminant value matches.
2094 -- The checking function takes all of the type's
2095 -- discriminants as parameters, so a list of all
2096 -- the selected discriminants must be constructed.
2099 D_Entity
:= First_Discriminant
(E
);
2100 while Present
(D_Entity
) loop
2102 Make_Selected_Component
(Loc
,
2104 Make_Identifier
(Loc
, Chars
=> Vname
),
2110 D_Entity
:= Next_Discriminant
(D_Entity
);
2116 Make_Function_Call
(Loc
,
2119 (Dcheck_Function
(Var
), Loc
),
2120 Parameter_Associations
=>
2125 Make_Conditional_Expression
(Loc
,
2128 (Dtest
, Bits_To_SU
(RM_SizV
), RM_Siz_Expr
));
2135 end Layout_Component_List
;
2137 -- Start of processing for Layout_Variant_Record
2140 -- We need the discriminant checking functions, since we generate
2141 -- calls to these functions for the RM_Size expression, so make
2142 -- sure that these functions have been constructed in time.
2144 Build_Discr_Checking_Funcs
(Decl
);
2146 -- Lay out the discriminants
2149 (From
=> Defining_Identifier
(First
(Dlist
)),
2150 To
=> Defining_Identifier
(Last
(Dlist
)),
2154 -- Lay out the main component list (this will make recursive calls
2155 -- to lay out all component lists nested within variants).
2157 Layout_Component_List
(Component_List
(Tdef
), Esiz
, RM_Siz_Expr
);
2158 Set_Esize
(E
, Esiz
);
2160 -- If the RM_Size is a literal, set its value
2162 if Nkind
(RM_Siz_Expr
) = N_Integer_Literal
then
2163 Set_RM_Size
(E
, Intval
(RM_Siz_Expr
));
2165 -- Otherwise we construct a dynamic SO_Ref
2174 end Layout_Variant_Record
;
2176 -- Start of processing for Layout_Record_Type
2179 -- If this is a cloned subtype, just copy the size fields from the
2180 -- original, nothing else needs to be done in this case, since the
2181 -- components themselves are all shared.
2183 if (Ekind
(E
) = E_Record_Subtype
2184 or else Ekind
(E
) = E_Class_Wide_Subtype
)
2185 and then Present
(Cloned_Subtype
(E
))
2187 Set_Esize
(E
, Esize
(Cloned_Subtype
(E
)));
2188 Set_RM_Size
(E
, RM_Size
(Cloned_Subtype
(E
)));
2189 Set_Alignment
(E
, Alignment
(Cloned_Subtype
(E
)));
2191 -- Another special case, class-wide types. The RM says that the size
2192 -- of such types is implementation defined (RM 13.3(48)). What we do
2193 -- here is to leave the fields set as unknown values, and the backend
2194 -- determines the actual behavior.
2196 elsif Ekind
(E
) = E_Class_Wide_Type
then
2202 -- Initialize alignment conservatively to 1. This value will
2203 -- be increased as necessary during processing of the record.
2205 if Unknown_Alignment
(E
) then
2206 Set_Alignment
(E
, Uint_1
);
2209 -- Initialize previous component. This is Empty unless there
2210 -- are components which have already been laid out by component
2211 -- clauses. If there are such components, we start our lay out of
2212 -- the remaining components following the last such component.
2216 Comp
:= First_Entity
(E
);
2217 while Present
(Comp
) loop
2218 if (Ekind
(Comp
) = E_Component
2219 or else Ekind
(Comp
) = E_Discriminant
)
2220 and then Present
(Component_Clause
(Comp
))
2224 Component_Bit_Offset
(Comp
) >
2225 Component_Bit_Offset
(Prev_Comp
)
2234 -- We have two separate circuits, one for non-variant records and
2235 -- one for variant records. For non-variant records, we simply go
2236 -- through the list of components. This handles all the non-variant
2237 -- cases including those cases of subtypes where there is no full
2238 -- type declaration, so the tree cannot be used to drive the layout.
2239 -- For variant records, we have to drive the layout from the tree
2240 -- since we need to understand the variant structure in this case.
2242 if Present
(Full_View
(E
)) then
2243 Decl
:= Declaration_Node
(Full_View
(E
));
2245 Decl
:= Declaration_Node
(E
);
2248 -- Scan all the components
2250 if Nkind
(Decl
) = N_Full_Type_Declaration
2251 and then Has_Discriminants
(E
)
2252 and then Nkind
(Type_Definition
(Decl
)) = N_Record_Definition
2253 and then Present
(Component_List
(Type_Definition
(Decl
)))
2255 Present
(Variant_Part
(Component_List
(Type_Definition
(Decl
))))
2257 Layout_Variant_Record
;
2259 Layout_Non_Variant_Record
;
2262 end Layout_Record_Type
;
2268 procedure Layout_Type
(E
: Entity_Id
) is
2270 -- For string literal types, for now, kill the size always, this
2271 -- is because gigi does not like or need the size to be set ???
2273 if Ekind
(E
) = E_String_Literal_Subtype
then
2274 Set_Esize
(E
, Uint_0
);
2275 Set_RM_Size
(E
, Uint_0
);
2279 -- For access types, set size/alignment. This is system address
2280 -- size, except for fat pointers (unconstrained array access types),
2281 -- where the size is two times the address size, to accommodate the
2282 -- two pointers that are required for a fat pointer (data and
2283 -- template). Note that E_Access_Protected_Subprogram_Type is not
2284 -- an access type for this purpose since it is not a pointer but is
2285 -- equivalent to a record. For access subtypes, copy the size from
2286 -- the base type since Gigi represents them the same way.
2288 if Is_Access_Type
(E
) then
2290 -- If Esize already set (e.g. by a size clause), then nothing
2291 -- further to be done here.
2293 if Known_Esize
(E
) then
2296 -- Access to subprogram is a strange beast, and we let the
2297 -- backend figure out what is needed (it may be some kind
2298 -- of fat pointer, including the static link for example.
2300 elsif Ekind
(E
) = E_Access_Protected_Subprogram_Type
then
2303 -- For access subtypes, copy the size information from base type
2305 elsif Ekind
(E
) = E_Access_Subtype
then
2306 Set_Size_Info
(E
, Base_Type
(E
));
2307 Set_RM_Size
(E
, RM_Size
(Base_Type
(E
)));
2309 -- For other access types, we use either address size, or, if
2310 -- a fat pointer is used (pointer-to-unconstrained array case),
2311 -- twice the address size to accommodate a fat pointer.
2315 Desig
: Entity_Id
:= Designated_Type
(E
);
2318 if Is_Private_Type
(Desig
)
2319 and then Present
(Full_View
(Desig
))
2321 Desig
:= Full_View
(Desig
);
2324 if Is_Array_Type
(Desig
)
2325 and then not Is_Constrained
(Desig
)
2326 and then not Has_Completion_In_Body
(Desig
)
2327 and then not Debug_Flag_6
2329 Init_Size
(E
, 2 * System_Address_Size
);
2331 -- Check for bad convention set
2333 if Warn_On_Export_Import
2335 (Convention
(E
) = Convention_C
2337 Convention
(E
) = Convention_CPP
)
2340 ("?this access type does not " &
2341 "correspond to C pointer", E
);
2345 Init_Size
(E
, System_Address_Size
);
2350 Set_Elem_Alignment
(E
);
2352 -- Scalar types: set size and alignment
2354 elsif Is_Scalar_Type
(E
) then
2356 -- For discrete types, the RM_Size and Esize must be set
2357 -- already, since this is part of the earlier processing
2358 -- and the front end is always required to lay out the
2359 -- sizes of such types (since they are available as static
2360 -- attributes). All we do is to check that this rule is
2363 if Is_Discrete_Type
(E
) then
2365 -- If the RM_Size is not set, then here is where we set it.
2367 -- Note: an RM_Size of zero looks like not set here, but this
2368 -- is a rare case, and we can simply reset it without any harm.
2370 if not Known_RM_Size
(E
) then
2371 Set_Discrete_RM_Size
(E
);
2374 -- If Esize for a discrete type is not set then set it
2376 if not Known_Esize
(E
) then
2382 -- If size is big enough, set it and exit
2384 if S
>= RM_Size
(E
) then
2388 -- If the RM_Size is greater than 64 (happens only
2389 -- when strange values are specified by the user,
2390 -- then Esize is simply a copy of RM_Size, it will
2391 -- be further refined later on)
2394 Set_Esize
(E
, RM_Size
(E
));
2397 -- Otherwise double possible size and keep trying
2406 -- For non-discrete sclar types, if the RM_Size is not set,
2407 -- then set it now to a copy of the Esize if the Esize is set.
2410 if Known_Esize
(E
) and then Unknown_RM_Size
(E
) then
2411 Set_RM_Size
(E
, Esize
(E
));
2415 Set_Elem_Alignment
(E
);
2417 -- Non-elementary (composite) types
2420 -- If RM_Size is known, set Esize if not known
2422 if Known_RM_Size
(E
) and then Unknown_Esize
(E
) then
2424 -- If the alignment is known, we bump the Esize up to the
2425 -- next alignment boundary if it is not already on one.
2427 if Known_Alignment
(E
) then
2429 A
: constant Uint
:= Alignment_In_Bits
(E
);
2430 S
: constant SO_Ref
:= RM_Size
(E
);
2433 Set_Esize
(E
, (S
* A
+ A
- 1) / A
);
2437 -- If Esize is set, and RM_Size is not, RM_Size is copied from
2438 -- Esize at least for now this seems reasonable, and is in any
2439 -- case needed for compatibility with old versions of gigi.
2440 -- look to be unknown.
2442 elsif Known_Esize
(E
) and then Unknown_RM_Size
(E
) then
2443 Set_RM_Size
(E
, Esize
(E
));
2446 -- For array base types, set component size if object size of
2447 -- the component type is known and is a small power of 2 (8,
2448 -- 16, 32, 64), since this is what will always be used.
2450 if Ekind
(E
) = E_Array_Type
2451 and then Unknown_Component_Size
(E
)
2454 CT
: constant Entity_Id
:= Component_Type
(E
);
2457 -- For some reasons, access types can cause trouble,
2458 -- So let's just do this for discrete types ???
2461 and then Is_Discrete_Type
(CT
)
2462 and then Known_Static_Esize
(CT
)
2465 S
: constant Uint
:= Esize
(CT
);
2473 Set_Component_Size
(E
, Esize
(CT
));
2481 -- Lay out array and record types if front end layout set
2483 if Frontend_Layout_On_Target
then
2484 if Is_Array_Type
(E
) and then not Is_Bit_Packed_Array
(E
) then
2485 Layout_Array_Type
(E
);
2486 elsif Is_Record_Type
(E
) then
2487 Layout_Record_Type
(E
);
2490 -- Case of backend layout, we still do a little in the front end
2493 -- Processing for record types
2495 if Is_Record_Type
(E
) then
2497 -- Special remaining processing for record types with a known
2498 -- size of 16, 32, or 64 bits whose alignment is not yet set.
2499 -- For these types, we set a corresponding alignment matching
2500 -- the size if possible, or as large as possible if not.
2502 if Convention
(E
) = Convention_Ada
2503 and then not Debug_Flag_Q
2505 Set_Composite_Alignment
(E
);
2508 -- Procressing for array types
2510 elsif Is_Array_Type
(E
) then
2512 -- For arrays that are required to be atomic, we do the same
2513 -- processing as described above for short records, since we
2514 -- really need to have the alignment set for the whole array.
2516 if Is_Atomic
(E
) and then not Debug_Flag_Q
then
2517 Set_Composite_Alignment
(E
);
2520 -- For unpacked array types, set an alignment of 1 if we know
2521 -- that the component alignment is not greater than 1. The reason
2522 -- we do this is to avoid unnecessary copying of slices of such
2523 -- arrays when passed to subprogram parameters (see special test
2524 -- in Exp_Ch6.Expand_Actuals).
2526 if not Is_Packed
(E
)
2527 and then Unknown_Alignment
(E
)
2529 if Known_Static_Component_Size
(E
)
2530 and then Component_Size
(E
) = 1
2532 Set_Alignment
(E
, Uint_1
);
2538 -- Final step is to check that Esize and RM_Size are compatible
2540 if Known_Static_Esize
(E
) and then Known_Static_RM_Size
(E
) then
2541 if Esize
(E
) < RM_Size
(E
) then
2543 -- Esize is less than RM_Size. That's not good. First we test
2544 -- whether this was set deliberately with an Object_Size clause
2545 -- and if so, object to the clause.
2547 if Has_Object_Size_Clause
(E
) then
2548 Error_Msg_Uint_1
:= RM_Size
(E
);
2550 ("object size is too small, minimum is ^",
2551 Expression
(Get_Attribute_Definition_Clause
2552 (E
, Attribute_Object_Size
)));
2555 -- Adjust Esize up to RM_Size value
2558 Size
: constant Uint
:= RM_Size
(E
);
2561 Set_Esize
(E
, RM_Size
(E
));
2563 -- For scalar types, increase Object_Size to power of 2,
2564 -- but not less than a storage unit in any case (i.e.,
2565 -- normally this means it will be byte addressable).
2567 if Is_Scalar_Type
(E
) then
2568 if Size
<= System_Storage_Unit
then
2569 Init_Esize
(E
, System_Storage_Unit
);
2570 elsif Size
<= 16 then
2572 elsif Size
<= 32 then
2575 Set_Esize
(E
, (Size
+ 63) / 64 * 64);
2578 -- Finally, make sure that alignment is consistent with
2579 -- the newly assigned size.
2581 while Alignment
(E
) * System_Storage_Unit
< Esize
(E
)
2582 and then Alignment
(E
) < Maximum_Alignment
2584 Set_Alignment
(E
, 2 * Alignment
(E
));
2592 ---------------------
2593 -- Rewrite_Integer --
2594 ---------------------
2596 procedure Rewrite_Integer
(N
: Node_Id
; V
: Uint
) is
2597 Loc
: constant Source_Ptr
:= Sloc
(N
);
2598 Typ
: constant Entity_Id
:= Etype
(N
);
2601 Rewrite
(N
, Make_Integer_Literal
(Loc
, Intval
=> V
));
2603 end Rewrite_Integer
;
2605 -------------------------------
2606 -- Set_And_Check_Static_Size --
2607 -------------------------------
2609 procedure Set_And_Check_Static_Size
2616 procedure Check_Size_Too_Small
(Spec
: Uint
; Min
: Uint
);
2617 -- Spec is the number of bit specified in the size clause, and
2618 -- Min is the minimum computed size. An error is given that the
2619 -- specified size is too small if Spec < Min, and in this case
2620 -- both Esize and RM_Size are set to unknown in E. The error
2621 -- message is posted on node SC.
2623 procedure Check_Unused_Bits
(Spec
: Uint
; Max
: Uint
);
2624 -- Spec is the number of bits specified in the size clause, and
2625 -- Max is the maximum computed size. A warning is given about
2626 -- unused bits if Spec > Max. This warning is posted on node SC.
2628 --------------------------
2629 -- Check_Size_Too_Small --
2630 --------------------------
2632 procedure Check_Size_Too_Small
(Spec
: Uint
; Min
: Uint
) is
2635 Error_Msg_Uint_1
:= Min
;
2637 ("size for & too small, minimum allowed is ^", SC
, E
);
2641 end Check_Size_Too_Small
;
2643 -----------------------
2644 -- Check_Unused_Bits --
2645 -----------------------
2647 procedure Check_Unused_Bits
(Spec
: Uint
; Max
: Uint
) is
2650 Error_Msg_Uint_1
:= Spec
- Max
;
2651 Error_Msg_NE
("?^ bits of & unused", SC
, E
);
2653 end Check_Unused_Bits
;
2655 -- Start of processing for Set_And_Check_Static_Size
2658 -- Case where Object_Size (Esize) is already set by a size clause
2660 if Known_Static_Esize
(E
) then
2661 SC
:= Size_Clause
(E
);
2664 SC
:= Get_Attribute_Definition_Clause
(E
, Attribute_Object_Size
);
2667 -- Perform checks on specified size against computed sizes
2669 if Present
(SC
) then
2670 Check_Unused_Bits
(Esize
(E
), Esiz
);
2671 Check_Size_Too_Small
(Esize
(E
), RM_Siz
);
2675 -- Case where Value_Size (RM_Size) is set by specific Value_Size
2676 -- clause (we do not need to worry about Value_Size being set by
2677 -- a Size clause, since that will have set Esize as well, and we
2678 -- already took care of that case).
2680 if Known_Static_RM_Size
(E
) then
2681 SC
:= Get_Attribute_Definition_Clause
(E
, Attribute_Value_Size
);
2683 -- Perform checks on specified size against computed sizes
2685 if Present
(SC
) then
2686 Check_Unused_Bits
(RM_Size
(E
), Esiz
);
2687 Check_Size_Too_Small
(RM_Size
(E
), RM_Siz
);
2691 -- Set sizes if unknown
2693 if Unknown_Esize
(E
) then
2694 Set_Esize
(E
, Esiz
);
2697 if Unknown_RM_Size
(E
) then
2698 Set_RM_Size
(E
, RM_Siz
);
2700 end Set_And_Check_Static_Size
;
2702 -----------------------------
2703 -- Set_Composite_Alignment --
2704 -----------------------------
2706 procedure Set_Composite_Alignment
(E
: Entity_Id
) is
2711 if Unknown_Alignment
(E
) then
2712 if Known_Static_Esize
(E
) then
2715 elsif Unknown_Esize
(E
)
2716 and then Known_Static_RM_Size
(E
)
2724 -- Size is known, alignment is not set
2726 -- Reset alignment to match size if size is exactly 2, 4, or 8 bytes
2728 if Siz
= 2 * System_Storage_Unit
then
2730 elsif Siz
= 4 * System_Storage_Unit
then
2732 elsif Siz
= 8 * System_Storage_Unit
then
2735 -- On VMS, also reset for odd "in between" sizes, e.g. a 17-bit
2736 -- record is given an alignment of 4. This is more consistent with
2737 -- what DEC Ada does.
2739 elsif OpenVMS_On_Target
and then Siz
> System_Storage_Unit
then
2741 if Siz
<= 2 * System_Storage_Unit
then
2743 elsif Siz
<= 4 * System_Storage_Unit
then
2745 elsif Siz
<= 8 * System_Storage_Unit
then
2751 -- No special alignment fiddling needed
2757 -- Here Align is set to the proposed improved alignment
2759 if Align
> Maximum_Alignment
then
2760 Align
:= Maximum_Alignment
;
2763 -- Further processing for record types only to reduce the alignment
2764 -- set by the above processing in some specific cases. We do not
2765 -- do this for atomic records, since we need max alignment there.
2767 if Is_Record_Type
(E
) then
2769 -- For records, there is generally no point in setting alignment
2770 -- higher than word size since we cannot do better than move by
2771 -- words in any case
2773 if Align
> System_Word_Size
/ System_Storage_Unit
then
2774 Align
:= System_Word_Size
/ System_Storage_Unit
;
2777 -- Check components. If any component requires a higher
2778 -- alignment, then we set that higher alignment in any case.
2784 Comp
:= First_Component
(E
);
2785 while Present
(Comp
) loop
2786 if Known_Alignment
(Etype
(Comp
)) then
2788 Calign
: constant Uint
:= Alignment
(Etype
(Comp
));
2791 -- The cases to worry about are when the alignment
2792 -- of the component type is larger than the alignment
2793 -- we have so far, and either there is no component
2794 -- clause for the alignment, or the length set by
2795 -- the component clause matches the alignment set.
2799 (Unknown_Esize
(Comp
)
2800 or else (Known_Static_Esize
(Comp
)
2803 Calign
* System_Storage_Unit
))
2805 Align
:= UI_To_Int
(Calign
);
2810 Next_Component
(Comp
);
2815 -- Set chosen alignment
2817 Set_Alignment
(E
, UI_From_Int
(Align
));
2819 if Known_Static_Esize
(E
)
2820 and then Esize
(E
) < Align
* System_Storage_Unit
2822 Set_Esize
(E
, UI_From_Int
(Align
* System_Storage_Unit
));
2825 end Set_Composite_Alignment
;
2827 --------------------------
2828 -- Set_Discrete_RM_Size --
2829 --------------------------
2831 procedure Set_Discrete_RM_Size
(Def_Id
: Entity_Id
) is
2832 FST
: constant Entity_Id
:= First_Subtype
(Def_Id
);
2835 -- All discrete types except for the base types in standard
2836 -- are constrained, so indicate this by setting Is_Constrained.
2838 Set_Is_Constrained
(Def_Id
);
2840 -- We set generic types to have an unknown size, since the
2841 -- representation of a generic type is irrelevant, in view
2842 -- of the fact that they have nothing to do with code.
2844 if Is_Generic_Type
(Root_Type
(FST
)) then
2845 Set_RM_Size
(Def_Id
, Uint_0
);
2847 -- If the subtype statically matches the first subtype, then
2848 -- it is required to have exactly the same layout. This is
2849 -- required by aliasing considerations.
2851 elsif Def_Id
/= FST
and then
2852 Subtypes_Statically_Match
(Def_Id
, FST
)
2854 Set_RM_Size
(Def_Id
, RM_Size
(FST
));
2855 Set_Size_Info
(Def_Id
, FST
);
2857 -- In all other cases the RM_Size is set to the minimum size.
2858 -- Note that this routine is never called for subtypes for which
2859 -- the RM_Size is set explicitly by an attribute clause.
2862 Set_RM_Size
(Def_Id
, UI_From_Int
(Minimum_Size
(Def_Id
)));
2864 end Set_Discrete_RM_Size
;
2866 ------------------------
2867 -- Set_Elem_Alignment --
2868 ------------------------
2870 procedure Set_Elem_Alignment
(E
: Entity_Id
) is
2872 -- Do not set alignment for packed array types, unless we are doing
2873 -- front end layout, because otherwise this is always handled in the
2876 if Is_Packed_Array_Type
(E
) and then not Frontend_Layout_On_Target
then
2879 -- If there is an alignment clause, then we respect it
2881 elsif Has_Alignment_Clause
(E
) then
2884 -- If the size is not set, then don't attempt to set the alignment. This
2885 -- happens in the backend layout case for access-to-subprogram types.
2887 elsif not Known_Static_Esize
(E
) then
2890 -- For access types, do not set the alignment if the size is less than
2891 -- the allowed minimum size. This avoids cascaded error messages.
2893 elsif Is_Access_Type
(E
)
2894 and then Esize
(E
) < System_Address_Size
2899 -- Here we calculate the alignment as the largest power of two
2900 -- multiple of System.Storage_Unit that does not exceed either
2901 -- the actual size of the type, or the maximum allowed alignment.
2905 UI_To_Int
(Esize
(E
)) / SSU
;
2910 while 2 * A
<= Ttypes
.Maximum_Alignment
2916 -- Now we think we should set the alignment to A, but we
2917 -- skip this if an alignment is already set to a value
2918 -- greater than A (happens for derived types).
2920 -- However, if the alignment is known and too small it
2921 -- must be increased, this happens in a case like:
2923 -- type R is new Character;
2924 -- for R'Size use 16;
2926 -- Here the alignment inherited from Character is 1, but
2927 -- it must be increased to 2 to reflect the increased size.
2929 if Unknown_Alignment
(E
) or else Alignment
(E
) < A
then
2930 Init_Alignment
(E
, A
);
2933 end Set_Elem_Alignment
;
2935 ----------------------
2936 -- SO_Ref_From_Expr --
2937 ----------------------
2939 function SO_Ref_From_Expr
2941 Ins_Type
: Entity_Id
;
2942 Vtype
: Entity_Id
:= Empty
;
2943 Make_Func
: Boolean := False)
2944 return Dynamic_SO_Ref
2946 Loc
: constant Source_Ptr
:= Sloc
(Ins_Type
);
2948 K
: constant Entity_Id
:=
2949 Make_Defining_Identifier
(Loc
,
2950 Chars
=> New_Internal_Name
('K'));
2954 function Check_Node_V_Ref
(N
: Node_Id
) return Traverse_Result
;
2955 -- Function used to check one node for reference to V
2957 function Has_V_Ref
is new Traverse_Func
(Check_Node_V_Ref
);
2958 -- Function used to traverse tree to check for reference to V
2960 ----------------------
2961 -- Check_Node_V_Ref --
2962 ----------------------
2964 function Check_Node_V_Ref
(N
: Node_Id
) return Traverse_Result
is
2966 if Nkind
(N
) = N_Identifier
then
2967 if Chars
(N
) = Vname
then
2976 end Check_Node_V_Ref
;
2978 -- Start of processing for SO_Ref_From_Expr
2981 -- Case of expression is an integer literal, in this case we just
2982 -- return the value (which must always be non-negative, since size
2983 -- and offset values can never be negative).
2985 if Nkind
(Expr
) = N_Integer_Literal
then
2986 pragma Assert
(Intval
(Expr
) >= 0);
2987 return Intval
(Expr
);
2990 -- Case where there is a reference to V, create function
2992 if Has_V_Ref
(Expr
) = Abandon
then
2994 pragma Assert
(Present
(Vtype
));
2995 Set_Is_Discrim_SO_Function
(K
);
2998 Make_Subprogram_Body
(Loc
,
3001 Make_Function_Specification
(Loc
,
3002 Defining_Unit_Name
=> K
,
3003 Parameter_Specifications
=> New_List
(
3004 Make_Parameter_Specification
(Loc
,
3005 Defining_Identifier
=>
3006 Make_Defining_Identifier
(Loc
, Chars
=> Vname
),
3008 New_Occurrence_Of
(Vtype
, Loc
))),
3010 New_Occurrence_Of
(Standard_Unsigned
, Loc
)),
3012 Declarations
=> Empty_List
,
3014 Handled_Statement_Sequence
=>
3015 Make_Handled_Sequence_Of_Statements
(Loc
,
3016 Statements
=> New_List
(
3017 Make_Return_Statement
(Loc
,
3018 Expression
=> Expr
))));
3020 -- The caller requests that the expression be encapsulated in
3021 -- a parameterless function.
3023 elsif Make_Func
then
3025 Make_Subprogram_Body
(Loc
,
3028 Make_Function_Specification
(Loc
,
3029 Defining_Unit_Name
=> K
,
3030 Parameter_Specifications
=> Empty_List
,
3031 Subtype_Mark
=> New_Occurrence_Of
(Standard_Unsigned
, Loc
)),
3033 Declarations
=> Empty_List
,
3035 Handled_Statement_Sequence
=>
3036 Make_Handled_Sequence_Of_Statements
(Loc
,
3037 Statements
=> New_List
(
3038 Make_Return_Statement
(Loc
, Expression
=> Expr
))));
3040 -- No reference to V and function not requested, so create a constant
3044 Make_Object_Declaration
(Loc
,
3045 Defining_Identifier
=> K
,
3046 Object_Definition
=>
3047 New_Occurrence_Of
(Standard_Unsigned
, Loc
),
3048 Constant_Present
=> True,
3049 Expression
=> Expr
);
3052 Append_Freeze_Action
(Ins_Type
, Decl
);
3054 return Create_Dynamic_SO_Ref
(K
);
3055 end SO_Ref_From_Expr
;