Daily bump.
[official-gcc.git] / gcc / ada / layout.adb
blob092f2f58cfb41aaabcea2bee9097c7b4ae05a078
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- L A Y O U T --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2021, Free Software Foundation, Inc. --
10 -- --
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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug; use Debug;
28 with Einfo; use Einfo;
29 with Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Errout; use Errout;
32 with Opt; use Opt;
33 with Sem_Aux; use Sem_Aux;
34 with Sem_Ch13; use Sem_Ch13;
35 with Sem_Eval; use Sem_Eval;
36 with Sem_Util; use Sem_Util;
37 with Sinfo; use Sinfo;
38 with Sinfo.Nodes; use Sinfo.Nodes;
39 with Sinfo.Utils; use Sinfo.Utils;
40 with Snames; use Snames;
41 with Ttypes; use Ttypes;
42 with Uintp; use Uintp;
44 package body Layout is
46 ------------------------
47 -- Local Declarations --
48 ------------------------
50 SSU : constant Int := Ttypes.System_Storage_Unit;
51 -- Short hand for System_Storage_Unit
53 -----------------------
54 -- Local Subprograms --
55 -----------------------
57 procedure Compute_Size_Depends_On_Discriminant (E : Entity_Id);
58 -- Given an array type or an array subtype E, compute whether its size
59 -- depends on the value of one or more discriminants and set the flag
60 -- Size_Depends_On_Discriminant accordingly. This need not be called
61 -- in front end layout mode since it does the computation on its own.
63 procedure Set_Composite_Alignment (E : Entity_Id);
64 -- This procedure is called for record types and subtypes, and also for
65 -- atomic array types and subtypes. If no alignment is set, and the size
66 -- is 2 or 4 (or 8 if the word size is 8), then the alignment is set to
67 -- match the size.
69 ----------------------------
70 -- Adjust_Esize_Alignment --
71 ----------------------------
73 procedure Adjust_Esize_Alignment (E : Entity_Id) is
74 Abits : Int;
75 Esize_Set : Boolean;
77 begin
78 -- Nothing to do if size unknown
80 if not Known_Esize (E) then
81 return;
82 end if;
84 -- Determine if size is constrained by an attribute definition clause
85 -- which must be obeyed. If so, we cannot increase the size in this
86 -- routine.
88 -- For a type, the issue is whether an object size clause has been set.
89 -- A normal size clause constrains only the value size (RM_Size)
91 if Is_Type (E) then
92 Esize_Set := Has_Object_Size_Clause (E);
94 -- For an object, the issue is whether a size clause is present
96 else
97 Esize_Set := Has_Size_Clause (E);
98 end if;
100 -- If size is known it must be a multiple of the storage unit size
102 if Esize (E) mod SSU /= 0 then
104 -- If not, and size specified, then give error
106 if Esize_Set then
107 Error_Msg_NE
108 ("size for& not a multiple of storage unit size",
109 Size_Clause (E), E);
110 return;
112 -- Otherwise bump up size to a storage unit boundary
114 else
115 Set_Esize (E, (Esize (E) + SSU - 1) / SSU * SSU);
116 end if;
117 end if;
119 -- Now we have the size set, it must be a multiple of the alignment
120 -- nothing more we can do here if the alignment is unknown here.
122 if not Known_Alignment (E) then
123 return;
124 end if;
126 -- At this point both the Esize and Alignment are known, so we need
127 -- to make sure they are consistent.
129 Abits := UI_To_Int (Alignment (E)) * SSU;
131 if Esize (E) mod Abits = 0 then
132 return;
133 end if;
135 -- Here we have a situation where the Esize is not a multiple of the
136 -- alignment. We must either increase Esize or reduce the alignment to
137 -- correct this situation.
139 -- The case in which we can decrease the alignment is where the
140 -- alignment was not set by an alignment clause, and the type in
141 -- question is a discrete type, where it is definitely safe to reduce
142 -- the alignment. For example:
144 -- t : integer range 1 .. 2;
145 -- for t'size use 8;
147 -- In this situation, the initial alignment of t is 4, copied from
148 -- the Integer base type, but it is safe to reduce it to 1 at this
149 -- stage, since we will only be loading a single storage unit.
151 if Is_Discrete_Type (Etype (E)) and then not Has_Alignment_Clause (E)
152 then
153 loop
154 Abits := Abits / 2;
155 exit when Esize (E) mod Abits = 0;
156 end loop;
158 Set_Alignment (E, UI_From_Int (Abits / SSU));
159 return;
160 end if;
162 -- Now the only possible approach left is to increase the Esize but we
163 -- can't do that if the size was set by a specific clause.
165 if Esize_Set then
166 Error_Msg_NE
167 ("size for& is not a multiple of alignment",
168 Size_Clause (E), E);
170 -- Otherwise we can indeed increase the size to a multiple of alignment
172 else
173 Set_Esize (E, ((Esize (E) + (Abits - 1)) / Abits) * Abits);
174 end if;
175 end Adjust_Esize_Alignment;
177 ------------------------------------------
178 -- Compute_Size_Depends_On_Discriminant --
179 ------------------------------------------
181 procedure Compute_Size_Depends_On_Discriminant (E : Entity_Id) is
182 Indx : Node_Id;
183 Ityp : Entity_Id;
184 Lo : Node_Id;
185 Hi : Node_Id;
186 Res : Boolean := False;
188 begin
189 -- Loop to process array indexes
191 Indx := First_Index (E);
192 while Present (Indx) loop
193 Ityp := Etype (Indx);
195 -- If an index of the array is a generic formal type then there is
196 -- no point in determining a size for the array type.
198 if Is_Generic_Type (Ityp) then
199 return;
200 end if;
202 Lo := Type_Low_Bound (Ityp);
203 Hi := Type_High_Bound (Ityp);
205 if (Nkind (Lo) = N_Identifier
206 and then Ekind (Entity (Lo)) = E_Discriminant)
207 or else
208 (Nkind (Hi) = N_Identifier
209 and then Ekind (Entity (Hi)) = E_Discriminant)
210 then
211 Res := True;
212 end if;
214 Next_Index (Indx);
215 end loop;
217 if Res then
218 Set_Size_Depends_On_Discriminant (E);
219 end if;
220 end Compute_Size_Depends_On_Discriminant;
222 -------------------
223 -- Layout_Object --
224 -------------------
226 procedure Layout_Object (E : Entity_Id) is
227 pragma Unreferenced (E);
228 begin
229 -- Nothing to do for now, assume backend does the layout
231 return;
232 end Layout_Object;
234 -----------------
235 -- Layout_Type --
236 -----------------
238 procedure Layout_Type (E : Entity_Id) is
239 Desig_Type : Entity_Id;
241 begin
242 -- For string literal types, kill the size always, because gigi does not
243 -- like or need the size to be set.
245 if Ekind (E) = E_String_Literal_Subtype then
246 Reinit_Esize (E);
247 Reinit_RM_Size (E);
248 return;
249 end if;
251 -- For access types, set size/alignment. This is system address size,
252 -- except for fat pointers (unconstrained array access types), where the
253 -- size is two times the address size, to accommodate the two pointers
254 -- that are required for a fat pointer (data and template). Note that
255 -- E_Access_Protected_Subprogram_Type is not an access type for this
256 -- purpose since it is not a pointer but is equivalent to a record. For
257 -- access subtypes, copy the size from the base type since Gigi
258 -- represents them the same way.
260 if Is_Access_Type (E) then
261 Desig_Type := Underlying_Type (Designated_Type (E));
263 -- If we only have a limited view of the type, see whether the
264 -- non-limited view is available.
266 if From_Limited_With (Designated_Type (E))
267 and then Ekind (Designated_Type (E)) = E_Incomplete_Type
268 and then Present (Non_Limited_View (Designated_Type (E)))
269 then
270 Desig_Type := Non_Limited_View (Designated_Type (E));
271 end if;
273 -- If Esize already set (e.g. by a size or value size clause), then
274 -- nothing further to be done here.
276 if Known_Esize (E) then
277 null;
279 -- Access to protected subprogram is a strange beast, and we let the
280 -- backend figure out what is needed (it may be some kind of fat
281 -- pointer, including the static link for example).
283 elsif Is_Access_Protected_Subprogram_Type (E) then
284 null;
286 -- For access subtypes, copy the size information from base type
288 elsif Ekind (E) = E_Access_Subtype then
289 Set_Size_Info (E, Base_Type (E));
290 Copy_RM_Size (To => E, From => Base_Type (E));
292 -- For other access types, we use either address size, or, if a fat
293 -- pointer is used (pointer-to-unconstrained array case), twice the
294 -- address size to accommodate a fat pointer.
296 elsif Present (Desig_Type)
297 and then Is_Array_Type (Desig_Type)
298 and then not Is_Constrained (Desig_Type)
299 and then not Has_Completion_In_Body (Desig_Type)
301 -- Debug Flag -gnatd6 says make all pointers to unconstrained thin
303 and then not Debug_Flag_6
304 then
305 Init_Size (E, 2 * System_Address_Size);
307 -- Check for bad convention set
309 if Warn_On_Export_Import
310 and then
311 (Convention (E) = Convention_C
312 or else
313 Convention (E) = Convention_CPP)
314 then
315 Error_Msg_N
316 ("?x?this access type does not correspond to C pointer", E);
317 end if;
319 -- If the designated type is a limited view it is unanalyzed. We can
320 -- examine the declaration itself to determine whether it will need a
321 -- fat pointer.
323 elsif Present (Desig_Type)
324 and then Present (Parent (Desig_Type))
325 and then Nkind (Parent (Desig_Type)) = N_Full_Type_Declaration
326 and then Nkind (Type_Definition (Parent (Desig_Type))) =
327 N_Unconstrained_Array_Definition
328 and then not Debug_Flag_6
329 then
330 Init_Size (E, 2 * System_Address_Size);
332 -- If unnesting subprograms, subprogram access types contain the
333 -- address of both the subprogram and an activation record. But if we
334 -- set that, we'll get a warning on different unchecked conversion
335 -- sizes in the RTS. So leave unset in that case.
337 elsif Unnest_Subprogram_Mode
338 and then Is_Access_Subprogram_Type (E)
339 then
340 null;
342 -- Normal case of thin pointer
344 else
345 Init_Size (E, System_Address_Size);
346 end if;
348 Set_Elem_Alignment (E);
350 -- Scalar types: set size and alignment
352 elsif Is_Scalar_Type (E) then
354 -- For discrete types, the RM_Size and Esize must be set already,
355 -- since this is part of the earlier processing and the front end is
356 -- always required to lay out the sizes of such types (since they are
357 -- available as static attributes). All we do is to check that this
358 -- rule is indeed obeyed.
360 if Is_Discrete_Type (E) then
362 -- If the RM_Size is not set, then here is where we set it
364 -- Note: an RM_Size of zero looks like not set here, but this
365 -- is a rare case, and we can simply reset it without any harm.
367 if not Known_RM_Size (E) then
368 Set_Discrete_RM_Size (E);
369 end if;
371 -- If Esize for a discrete type is not set then set it
373 if not Known_Esize (E) then
374 declare
375 S : Pos := 8;
377 begin
378 loop
379 -- If size is big enough, set it and exit
381 if S >= RM_Size (E) then
382 Set_Esize (E, UI_From_Int (S));
383 exit;
385 -- If the RM_Size is greater than System_Max_Integer_Size
386 -- (happens only when strange values are specified by the
387 -- user), then Esize is simply a copy of RM_Size, it will
388 -- be further refined later on.
390 elsif S = System_Max_Integer_Size then
391 Set_Esize (E, RM_Size (E));
392 exit;
394 -- Otherwise double possible size and keep trying
396 else
397 S := S * 2;
398 end if;
399 end loop;
400 end;
401 end if;
403 -- For non-discrete scalar types, if the RM_Size is not set, then set
404 -- it now to a copy of the Esize if the Esize is set.
406 else
407 if Known_Esize (E) and then not Known_RM_Size (E) then
408 Set_RM_Size (E, Esize (E));
409 end if;
410 end if;
412 Set_Elem_Alignment (E);
414 -- Non-elementary (composite) types
416 else
417 -- For packed arrays, take size and alignment values from the packed
418 -- array type if a packed array type has been created and the fields
419 -- are not currently set.
421 if Is_Array_Type (E)
422 and then Present (Packed_Array_Impl_Type (E))
423 then
424 declare
425 PAT : constant Entity_Id := Packed_Array_Impl_Type (E);
427 begin
428 if not Known_Esize (E) then
429 Copy_Esize (To => E, From => PAT);
430 end if;
432 if not Known_RM_Size (E) then
433 Copy_RM_Size (To => E, From => PAT);
434 end if;
436 if not Known_Alignment (E) then
437 Copy_Alignment (To => E, From => PAT);
438 end if;
439 end;
440 end if;
442 -- For array base types, set the component size if object size of the
443 -- component type is known and is a small power of 2 (8, 16, 32, 64
444 -- or 128), since this is what will always be used, except if a very
445 -- large alignment was specified and so Adjust_Esize_For_Alignment
446 -- gave up because, in this case, the object size is not a multiple
447 -- of the alignment and, therefore, cannot be the component size.
449 if Ekind (E) = E_Array_Type and then not Known_Component_Size (E) then
450 declare
451 CT : constant Entity_Id := Component_Type (E);
453 begin
454 -- For some reason, access types can cause trouble, So let's
455 -- just do this for scalar types.
457 if Present (CT)
458 and then Is_Scalar_Type (CT)
459 and then Known_Static_Esize (CT)
460 and then not (Known_Alignment (CT)
461 and then Alignment_In_Bits (CT) >
462 System_Max_Integer_Size)
463 then
464 declare
465 S : constant Uint := Esize (CT);
466 begin
467 if Addressable (S) then
468 Set_Component_Size (E, S);
469 end if;
470 end;
471 end if;
472 end;
473 end if;
475 -- For non-packed arrays set the alignment of the array to the
476 -- alignment of the component type if it is unknown. Skip this
477 -- in full access case since a larger alignment may be needed.
479 if Is_Array_Type (E)
480 and then not Is_Packed (E)
481 and then not Known_Alignment (E)
482 and then Known_Alignment (Component_Type (E))
483 and then Known_Static_Component_Size (E)
484 and then Known_Static_Esize (Component_Type (E))
485 and then Component_Size (E) = Esize (Component_Type (E))
486 and then not Is_Full_Access (E)
487 then
488 Set_Alignment (E, Alignment (Component_Type (E)));
489 end if;
491 -- If packing was requested, the one-dimensional array is constrained
492 -- with static bounds, the component size was set explicitly, and
493 -- the alignment is known, we can set (if not set explicitly) the
494 -- RM_Size and the Esize of the array type, as RM_Size is equal to
495 -- (arr'length * arr'component_size) and Esize is the same value
496 -- rounded to the next multiple of arr'alignment. This is not
497 -- applicable to packed arrays that are implemented specially
498 -- in GNAT, i.e. when Packed_Array_Impl_Type is set.
500 if Is_Array_Type (E)
501 and then Present (First_Index (E)) -- Skip types in error
502 and then Number_Dimensions (E) = 1
503 and then not Present (Packed_Array_Impl_Type (E))
504 and then Has_Pragma_Pack (E)
505 and then Is_Constrained (E)
506 and then Compile_Time_Known_Bounds (E)
507 and then Known_Component_Size (E)
508 and then Known_Alignment (E)
509 then
510 declare
511 Abits : constant Int := UI_To_Int (Alignment (E)) * SSU;
512 Lo, Hi : Node_Id;
513 Siz : Uint;
515 begin
516 Get_Index_Bounds (First_Index (E), Lo, Hi);
518 -- Even if the bounds are known at compile time, they could
519 -- have been replaced by an error node. Check each bound
520 -- explicitly.
522 if Compile_Time_Known_Value (Lo)
523 and then Compile_Time_Known_Value (Hi)
524 then
525 Siz := (Expr_Value (Hi) - Expr_Value (Lo) + 1)
526 * Component_Size (E);
528 -- Do not overwrite a different value of 'Size specified
529 -- explicitly by the user. In that case, also do not set
530 -- Esize.
532 if not Known_RM_Size (E) or else RM_Size (E) = Siz then
533 Set_RM_Size (E, Siz);
535 if not Known_Esize (E) then
536 Siz := ((Siz + (Abits - 1)) / Abits) * Abits;
537 Set_Esize (E, Siz);
538 end if;
539 end if;
540 end if;
541 end;
542 end if;
543 end if;
545 -- Even if the backend performs the layout, we still do a little in
546 -- the front end
548 -- Processing for record types
550 if Is_Record_Type (E) then
552 -- Special remaining processing for record types with a known
553 -- size of 16, 32, or 64 bits whose alignment is not yet set.
554 -- For these types, we set a corresponding alignment matching
555 -- the size if possible, or as large as possible if not.
557 if Convention (E) = Convention_Ada and then not Debug_Flag_Q then
558 Set_Composite_Alignment (E);
559 end if;
561 -- Processing for array types
563 elsif Is_Array_Type (E) then
565 -- For arrays that are required to be full access, we do the same
566 -- processing as described above for short records, since we really
567 -- need to have the alignment set for the whole array.
569 if Is_Full_Access (E) and then not Debug_Flag_Q then
570 Set_Composite_Alignment (E);
571 end if;
573 -- For unpacked array types, set an alignment of 1 if we know
574 -- that the component alignment is not greater than 1. The reason
575 -- we do this is to avoid unnecessary copying of slices of such
576 -- arrays when passed to subprogram parameters (see special test
577 -- in Exp_Ch6.Expand_Actuals).
579 if not Is_Packed (E) and then not Known_Alignment (E) then
580 if Known_Static_Component_Size (E)
581 and then Component_Size (E) = 1
582 then
583 Set_Alignment (E, Uint_1);
584 end if;
585 end if;
587 -- We need to know whether the size depends on the value of one
588 -- or more discriminants to select the return mechanism. Skip if
589 -- errors are present, to prevent cascaded messages.
591 if Serious_Errors_Detected = 0 then
592 Compute_Size_Depends_On_Discriminant (E);
593 end if;
594 end if;
596 -- Final step is to check that Esize and RM_Size are compatible
598 if Known_Static_Esize (E) and then Known_Static_RM_Size (E) then
599 if Esize (E) < RM_Size (E) then
601 -- Esize is less than RM_Size. That's not good. First we test
602 -- whether this was set deliberately with an Object_Size clause
603 -- and if so, object to the clause.
605 if Has_Object_Size_Clause (E) then
606 Error_Msg_Uint_1 := RM_Size (E);
607 Error_Msg_F
608 ("object size is too small, minimum allowed is ^",
609 Expression (Get_Attribute_Definition_Clause
610 (E, Attribute_Object_Size)));
611 end if;
613 -- Adjust Esize up to RM_Size value
615 declare
616 Size : constant Uint := RM_Size (E);
618 begin
619 Set_Esize (E, RM_Size (E));
621 -- For scalar types, increase Object_Size to power of 2, but
622 -- not less than a storage unit in any case (i.e., normally
623 -- this means it will be storage-unit addressable).
625 if Is_Scalar_Type (E) then
626 if Size <= SSU then
627 Set_Esize (E, UI_From_Int (SSU));
628 elsif Size <= 16 then
629 Set_Esize (E, Uint_16);
630 elsif Size <= 32 then
631 Set_Esize (E, Uint_32);
632 else
633 Set_Esize (E, (Size + 63) / 64 * 64);
634 end if;
636 -- Finally, make sure that alignment is consistent with
637 -- the newly assigned size.
639 while Alignment (E) * SSU < Esize (E)
640 and then Alignment (E) < Maximum_Alignment
641 loop
642 Set_Alignment (E, 2 * Alignment (E));
643 end loop;
644 end if;
645 end;
646 end if;
647 end if;
648 end Layout_Type;
650 -----------------------------
651 -- Set_Composite_Alignment --
652 -----------------------------
654 procedure Set_Composite_Alignment (E : Entity_Id) is
655 Siz : Uint;
656 Align : Nat;
658 begin
659 -- If alignment is already set, then nothing to do
661 if Known_Alignment (E) then
662 return;
663 end if;
665 -- Alignment is not known, see if we can set it, taking into account
666 -- the setting of the Optimize_Alignment mode.
668 -- If Optimize_Alignment is set to Space, then we try to give packed
669 -- records an aligmment of 1, unless there is some reason we can't.
671 if Optimize_Alignment_Space (E)
672 and then Is_Record_Type (E)
673 and then Is_Packed (E)
674 then
675 -- No effect for record with full access components
677 if Is_Full_Access (E) then
678 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
680 if Is_Atomic (E) then
681 Error_Msg_N
682 ("\pragma ignored for atomic record??", E);
683 else
684 Error_Msg_N
685 ("\pragma ignored for bolatile full access record??", E);
686 end if;
688 return;
689 end if;
691 -- No effect if independent components
693 if Has_Independent_Components (E) then
694 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
695 Error_Msg_N
696 ("\pragma ignored for record with independent components??", E);
697 return;
698 end if;
700 -- No effect if a component is full access or of a by-reference type
702 declare
703 Ent : Entity_Id;
705 begin
706 Ent := First_Component_Or_Discriminant (E);
707 while Present (Ent) loop
708 if Is_By_Reference_Type (Etype (Ent))
709 or else Is_Full_Access (Etype (Ent))
710 or else Is_Full_Access (Ent)
711 then
712 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
714 if Is_Atomic (Etype (Ent)) or else Is_Atomic (Ent) then
715 Error_Msg_N
716 ("\pragma is ignored if atomic "
717 & "components present??", E);
718 else
719 Error_Msg_N
720 ("\pragma is ignored if volatile full access "
721 & "components present??", E);
722 end if;
724 return;
725 else
726 Next_Component_Or_Discriminant (Ent);
727 end if;
728 end loop;
729 end;
731 -- Optimize_Alignment has no effect on variable length record
733 if not Size_Known_At_Compile_Time (E) then
734 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
735 Error_Msg_N ("\pragma is ignored for variable length record??", E);
736 return;
737 end if;
739 -- All tests passed, we can set alignment to 1
741 Align := 1;
743 -- Not a record, or not packed
745 else
746 -- The only other cases we worry about here are where the size is
747 -- statically known at compile time.
749 if Known_Static_Esize (E) then
750 Siz := Esize (E);
751 elsif not Known_Esize (E) and then Known_Static_RM_Size (E) then
752 Siz := RM_Size (E);
753 else
754 return;
755 end if;
757 -- Size is known, alignment is not set
759 -- Reset alignment to match size if the known size is exactly 2, 4,
760 -- or 8 storage units.
762 if Siz = 2 * SSU then
763 Align := 2;
764 elsif Siz = 4 * SSU then
765 Align := 4;
766 elsif Siz = 8 * SSU then
767 Align := 8;
769 -- If Optimize_Alignment is set to Space, then make sure the
770 -- alignment matches the size, for example, if the size is 17
771 -- bytes then we want an alignment of 1 for the type.
773 elsif Optimize_Alignment_Space (E) then
774 if Siz mod (8 * SSU) = 0 then
775 Align := 8;
776 elsif Siz mod (4 * SSU) = 0 then
777 Align := 4;
778 elsif Siz mod (2 * SSU) = 0 then
779 Align := 2;
780 else
781 Align := 1;
782 end if;
784 -- If Optimize_Alignment is set to Time, then we reset for odd
785 -- "in between sizes", for example a 17 bit record is given an
786 -- alignment of 4.
788 elsif Optimize_Alignment_Time (E)
789 and then Siz > SSU
790 and then Siz <= 8 * SSU
791 then
792 if Siz <= 2 * SSU then
793 Align := 2;
794 elsif Siz <= 4 * SSU then
795 Align := 4;
796 else -- Siz <= 8 * SSU then
797 Align := 8;
798 end if;
800 -- No special alignment fiddling needed
802 else
803 return;
804 end if;
805 end if;
807 -- Here we have Set Align to the proposed improved value. Make sure the
808 -- value set does not exceed Maximum_Alignment for the target.
810 if Align > Maximum_Alignment then
811 Align := Maximum_Alignment;
812 end if;
814 -- Further processing for record types only to reduce the alignment
815 -- set by the above processing in some specific cases. We do not
816 -- do this for full access records, since we need max alignment there,
818 if Is_Record_Type (E) and then not Is_Full_Access (E) then
820 -- For records, there is generally no point in setting alignment
821 -- higher than word size since we cannot do better than move by
822 -- words in any case. Omit this if we are optimizing for time,
823 -- since conceivably we may be able to do better.
825 if Align > System_Word_Size / SSU
826 and then not Optimize_Alignment_Time (E)
827 then
828 Align := System_Word_Size / SSU;
829 end if;
831 -- Check components. If any component requires a higher alignment,
832 -- then we set that higher alignment in any case. Don't do this if we
833 -- have Optimize_Alignment set to Space. Note that covers the case of
834 -- packed records, where we already set alignment to 1.
836 if not Optimize_Alignment_Space (E) then
837 declare
838 Comp : Entity_Id;
840 begin
841 Comp := First_Component (E);
842 while Present (Comp) loop
843 if Known_Alignment (Etype (Comp)) then
844 declare
845 Calign : constant Uint := Alignment (Etype (Comp));
847 begin
848 -- The cases to process are when the alignment of the
849 -- component type is larger than the alignment we have
850 -- so far, and either there is no component clause for
851 -- the component, or the length set by the component
852 -- clause matches the length of the component type.
854 if Calign > Align
855 and then
856 (not Known_Esize (Comp)
857 or else (Known_Static_Esize (Comp)
858 and then
859 Esize (Comp) = Calign * SSU))
860 then
861 Align := UI_To_Int (Calign);
862 end if;
863 end;
864 end if;
866 Next_Component (Comp);
867 end loop;
868 end;
869 end if;
870 end if;
872 -- Set chosen alignment, and increase Esize if necessary to match the
873 -- chosen alignment.
875 Set_Alignment (E, UI_From_Int (Align));
877 if Known_Static_Esize (E)
878 and then Esize (E) < Align * SSU
879 then
880 Set_Esize (E, UI_From_Int (Align * SSU));
881 end if;
882 end Set_Composite_Alignment;
884 --------------------------
885 -- Set_Discrete_RM_Size --
886 --------------------------
888 procedure Set_Discrete_RM_Size (Def_Id : Entity_Id) is
889 FST : constant Entity_Id := First_Subtype (Def_Id);
891 begin
892 -- All discrete types except for the base types in standard are
893 -- constrained, so indicate this by setting Is_Constrained.
895 Set_Is_Constrained (Def_Id);
897 -- Set generic types to have an unknown size, since the representation
898 -- of a generic type is irrelevant, in view of the fact that they have
899 -- nothing to do with code.
901 if Is_Generic_Type (Root_Type (FST)) then
902 Reinit_RM_Size (Def_Id);
904 -- If the subtype statically matches the first subtype, then it is
905 -- required to have exactly the same layout. This is required by
906 -- aliasing considerations.
908 elsif Def_Id /= FST and then
909 Subtypes_Statically_Match (Def_Id, FST)
910 then
911 Set_RM_Size (Def_Id, RM_Size (FST));
912 Set_Size_Info (Def_Id, FST);
914 -- In all other cases the RM_Size is set to the minimum size. Note that
915 -- this routine is never called for subtypes for which the RM_Size is
916 -- set explicitly by an attribute clause.
918 else
919 Set_RM_Size (Def_Id, UI_From_Int (Minimum_Size (Def_Id)));
920 end if;
921 end Set_Discrete_RM_Size;
923 ------------------------
924 -- Set_Elem_Alignment --
925 ------------------------
927 procedure Set_Elem_Alignment (E : Entity_Id; Align : Nat := 0) is
928 begin
929 -- Do not set alignment for packed array types, this is handled in the
930 -- backend.
932 if Is_Packed_Array_Impl_Type (E) then
933 return;
935 -- If there is an alignment clause, then we respect it
937 elsif Has_Alignment_Clause (E) then
938 return;
940 -- If the size is not set, then don't attempt to set the alignment. This
941 -- happens in the backend layout case for access-to-subprogram types.
943 elsif not Known_Static_Esize (E) then
944 return;
946 -- For access types, do not set the alignment if the size is less than
947 -- the allowed minimum size. This avoids cascaded error messages.
949 elsif Is_Access_Type (E) and then Esize (E) < System_Address_Size then
950 return;
951 end if;
953 -- We attempt to set the alignment in all the other cases
955 declare
956 S : Int;
957 A : Nat;
958 M : Nat;
960 begin
961 -- The given Esize may be larger that int'last because of a previous
962 -- error, and the call to UI_To_Int will fail, so use default.
964 if Esize (E) / SSU > Ttypes.Maximum_Alignment then
965 S := Ttypes.Maximum_Alignment;
967 -- If this is an access type and the target doesn't have strict
968 -- alignment, then cap the alignment to that of a regular access
969 -- type. This will avoid giving fat pointers twice the usual
970 -- alignment for no practical benefit since the misalignment doesn't
971 -- really matter.
973 elsif Is_Access_Type (E)
974 and then not Target_Strict_Alignment
975 then
976 S := System_Address_Size / SSU;
978 else
979 S := UI_To_Int (Esize (E)) / SSU;
980 end if;
982 -- If the default alignment of "double" floating-point types is
983 -- specifically capped, enforce the cap.
985 if Ttypes.Target_Double_Float_Alignment > 0
986 and then S = 8
987 and then Is_Floating_Point_Type (E)
988 then
989 M := Ttypes.Target_Double_Float_Alignment;
991 -- If the default alignment of "double" or larger scalar types is
992 -- specifically capped, enforce the cap.
994 elsif Ttypes.Target_Double_Scalar_Alignment > 0
995 and then S >= 8
996 and then Is_Scalar_Type (E)
997 then
998 M := Ttypes.Target_Double_Scalar_Alignment;
1000 -- Otherwise enforce the overall alignment cap
1002 else
1003 M := Ttypes.Maximum_Alignment;
1004 end if;
1006 -- We calculate the alignment as the largest power-of-two multiple
1007 -- of System.Storage_Unit that does not exceed the object size of
1008 -- the type and the maximum allowed alignment, if none was specified.
1009 -- Otherwise we only cap it to the maximum allowed alignment.
1011 if Align = 0 then
1012 A := 1;
1013 while 2 * A <= S and then 2 * A <= M loop
1014 A := 2 * A;
1015 end loop;
1016 else
1017 A := Nat'Min (Align, M);
1018 end if;
1020 -- If alignment is currently not set, then we can safely set it to
1021 -- this new calculated value.
1023 if not Known_Alignment (E) then
1024 Set_Alignment (E, UI_From_Int (A));
1026 -- Cases where we have inherited an alignment
1028 -- For constructed types, always reset the alignment, these are
1029 -- generally invisible to the user anyway, and that way we are
1030 -- sure that no constructed types have weird alignments.
1032 elsif not Comes_From_Source (E) then
1033 Set_Alignment (E, UI_From_Int (A));
1035 -- If this inherited alignment is the same as the one we computed,
1036 -- then obviously everything is fine, and we do not need to reset it.
1038 elsif Alignment (E) = A then
1039 null;
1041 else
1042 -- Now we come to the difficult cases of subtypes for which we
1043 -- have inherited an alignment different from the computed one.
1044 -- We resort to the presence of alignment and size clauses to
1045 -- guide our choices. Note that they can generally be present
1046 -- only on the first subtype (except for Object_Size) and that
1047 -- we need to look at the Rep_Item chain to correctly handle
1048 -- derived types.
1050 declare
1051 FST : constant Entity_Id := First_Subtype (E);
1053 function Has_Attribute_Clause
1054 (E : Entity_Id;
1055 Id : Attribute_Id) return Boolean;
1056 -- Wrapper around Get_Attribute_Definition_Clause which tests
1057 -- for the presence of the specified attribute clause.
1059 --------------------------
1060 -- Has_Attribute_Clause --
1061 --------------------------
1063 function Has_Attribute_Clause
1064 (E : Entity_Id;
1065 Id : Attribute_Id) return Boolean is
1066 begin
1067 return Present (Get_Attribute_Definition_Clause (E, Id));
1068 end Has_Attribute_Clause;
1070 begin
1071 -- If the alignment comes from a clause, then we respect it.
1072 -- Consider for example:
1074 -- type R is new Character;
1075 -- for R'Alignment use 1;
1076 -- for R'Size use 16;
1077 -- subtype S is R;
1079 -- Here R has a specified size of 16 and a specified alignment
1080 -- of 1, and it seems right for S to inherit both values.
1082 if Has_Attribute_Clause (FST, Attribute_Alignment) then
1083 null;
1085 -- Now we come to the cases where we have inherited alignment
1086 -- and size, and overridden the size but not the alignment.
1088 elsif Has_Attribute_Clause (FST, Attribute_Size)
1089 or else Has_Attribute_Clause (FST, Attribute_Object_Size)
1090 or else Has_Attribute_Clause (E, Attribute_Object_Size)
1091 then
1092 -- This is tricky, it might be thought that we should try to
1093 -- inherit the alignment, since that's what the RM implies,
1094 -- but that leads to complex rules and oddities. Consider
1095 -- for example:
1097 -- type R is new Character;
1098 -- for R'Size use 16;
1100 -- It seems quite bogus in this case to inherit an alignment
1101 -- of 1 from the parent type Character. Furthermore, if that
1102 -- is what the programmer really wanted for some odd reason,
1103 -- then he could specify the alignment directly.
1105 -- Moreover we really don't want to inherit the alignment in
1106 -- the case of a specified Object_Size for a subtype, since
1107 -- there would be no way of overriding to give a reasonable
1108 -- value (as we don't have an Object_Alignment attribute).
1109 -- Consider for example:
1111 -- subtype R is Character;
1112 -- for R'Object_Size use 16;
1114 -- If we inherit the alignment of 1, then it will be very
1115 -- inefficient for the subtype and this cannot be fixed.
1117 -- So we make the decision that if Size (or Object_Size) is
1118 -- given and the alignment is not specified with a clause,
1119 -- we reset the alignment to the appropriate value for the
1120 -- specified size. This is a nice simple rule to implement
1121 -- and document.
1123 -- There is a theoretical glitch, which is that a confirming
1124 -- size clause could now change the alignment, which, if we
1125 -- really think that confirming rep clauses should have no
1126 -- effect, could be seen as a no-no. However that's already
1127 -- implemented by Alignment_Check_For_Size_Change so we do
1128 -- not change the philosophy here.
1130 -- Historical note: in versions prior to Nov 6th, 2011, an
1131 -- odd distinction was made between inherited alignments
1132 -- larger than the computed alignment (where the larger
1133 -- alignment was inherited) and inherited alignments smaller
1134 -- than the computed alignment (where the smaller alignment
1135 -- was overridden). This was a dubious fix to get around an
1136 -- ACATS problem which seems to have disappeared anyway, and
1137 -- in any case, this peculiarity was never documented.
1139 Set_Alignment (E, UI_From_Int (A));
1141 -- If no Size (or Object_Size) was specified, then we have
1142 -- inherited the object size, so we should also inherit the
1143 -- alignment and not modify it.
1145 else
1146 null;
1147 end if;
1148 end;
1149 end if;
1150 end;
1151 end Set_Elem_Alignment;
1153 end Layout;