hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / ada / layout.adb
blobce56ef1ebae70a08daa1305c3077665557e1027d
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-2023, 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;
43 with Warnsw; use Warnsw;
45 package body Layout is
47 ------------------------
48 -- Local Declarations --
49 ------------------------
51 SSU : constant Int := Ttypes.System_Storage_Unit;
52 -- Short hand for System_Storage_Unit
54 -----------------------
55 -- Local Subprograms --
56 -----------------------
58 procedure Compute_Size_Depends_On_Discriminant (E : Entity_Id);
59 -- Given an array type or an array subtype E, compute whether its size
60 -- depends on the value of one or more discriminants and set the flag
61 -- Size_Depends_On_Discriminant accordingly. This need not be called
62 -- in front end layout mode since it does the computation on its own.
64 procedure Set_Composite_Alignment (E : Entity_Id);
65 -- This procedure is called for record types and subtypes, and also for
66 -- atomic array types and subtypes. If no alignment is set, and the size
67 -- is 2 or 4 (or 8 if the word size is 8), then the alignment is set to
68 -- match the size.
70 ----------------------------
71 -- Adjust_Esize_Alignment --
72 ----------------------------
74 procedure Adjust_Esize_Alignment (E : Entity_Id) is
75 Abits : Int;
76 Esize_Set : Boolean;
78 begin
79 -- Nothing to do if size unknown
81 if not Known_Esize (E) then
82 return;
83 end if;
85 -- Determine if size is constrained by an attribute definition clause
86 -- which must be obeyed. If so, we cannot increase the size in this
87 -- routine.
89 -- For a type, the issue is whether an object size clause has been set.
90 -- A normal size clause constrains only the value size (RM_Size)
92 if Is_Type (E) then
93 Esize_Set := Has_Object_Size_Clause (E);
95 -- For an object, the issue is whether a size clause is present
97 else
98 Esize_Set := Has_Size_Clause (E);
99 end if;
101 -- If size is known it must be a multiple of the storage unit size
103 if Esize (E) mod SSU /= 0 then
105 -- If not, and size specified, then give error
107 if Esize_Set then
108 Error_Msg_NE
109 ("size for& not a multiple of storage unit size",
110 Size_Clause (E), E);
111 return;
113 -- Otherwise bump up size to a storage unit boundary
115 else
116 Set_Esize (E, (Esize (E) + SSU - 1) / SSU * SSU);
117 end if;
118 end if;
120 -- Now we have the size set, it must be a multiple of the alignment
121 -- nothing more we can do here if the alignment is unknown here.
123 if not Known_Alignment (E) then
124 return;
125 end if;
127 -- At this point both the Esize and Alignment are known, so we need
128 -- to make sure they are consistent.
130 Abits := UI_To_Int (Alignment (E)) * SSU;
132 if Esize (E) mod Abits = 0 then
133 return;
134 end if;
136 -- Here we have a situation where the Esize is not a multiple of the
137 -- alignment. We must either increase Esize or reduce the alignment to
138 -- correct this situation.
140 -- The case in which we can decrease the alignment is where the
141 -- alignment was not set by an alignment clause, and the type in
142 -- question is a discrete type, where it is definitely safe to reduce
143 -- the alignment. For example:
145 -- t : integer range 1 .. 2;
146 -- for t'size use 8;
148 -- In this situation, the initial alignment of t is 4, copied from
149 -- the Integer base type, but it is safe to reduce it to 1 at this
150 -- stage, since we will only be loading a single storage unit.
152 if Is_Discrete_Type (Etype (E)) and then not Has_Alignment_Clause (E)
153 then
154 loop
155 Abits := Abits / 2;
156 exit when Esize (E) mod Abits = 0;
157 end loop;
159 Set_Alignment (E, UI_From_Int (Abits / SSU));
160 return;
161 end if;
163 -- Now the only possible approach left is to increase the Esize but we
164 -- can't do that if the size was set by a specific clause.
166 if Esize_Set then
167 Error_Msg_NE
168 ("size for& is not a multiple of alignment",
169 Size_Clause (E), E);
171 -- Otherwise we can indeed increase the size to a multiple of alignment
173 else
174 Set_Esize (E, ((Esize (E) + (Abits - 1)) / Abits) * Abits);
175 end if;
176 end Adjust_Esize_Alignment;
178 ------------------------------------------
179 -- Compute_Size_Depends_On_Discriminant --
180 ------------------------------------------
182 procedure Compute_Size_Depends_On_Discriminant (E : Entity_Id) is
183 Indx : Node_Id;
184 Ityp : Entity_Id;
185 Lo : Node_Id;
186 Hi : Node_Id;
187 Res : Boolean := False;
189 begin
190 -- Loop to process array indexes
192 Indx := First_Index (E);
193 while Present (Indx) loop
194 Ityp := Etype (Indx);
196 -- If an index of the array is a generic formal type then there is
197 -- no point in determining a size for the array type.
199 if Is_Generic_Type (Ityp) then
200 return;
201 end if;
203 Lo := Type_Low_Bound (Ityp);
204 Hi := Type_High_Bound (Ityp);
206 if (Nkind (Lo) = N_Identifier
207 and then Ekind (Entity (Lo)) = E_Discriminant)
208 or else
209 (Nkind (Hi) = N_Identifier
210 and then Ekind (Entity (Hi)) = E_Discriminant)
211 then
212 Res := True;
213 end if;
215 Next_Index (Indx);
216 end loop;
218 if Res then
219 Set_Size_Depends_On_Discriminant (E);
220 end if;
221 end Compute_Size_Depends_On_Discriminant;
223 -------------------
224 -- Layout_Object --
225 -------------------
227 procedure Layout_Object (E : Entity_Id) is
228 pragma Unreferenced (E);
229 begin
230 -- Nothing to do for now, assume backend does the layout
232 return;
233 end Layout_Object;
235 -----------------
236 -- Layout_Type --
237 -----------------
239 procedure Layout_Type (E : Entity_Id) is
240 Desig_Type : Entity_Id;
242 begin
243 -- For string literal types, kill the size always, because gigi does not
244 -- like or need the size to be set.
246 if Ekind (E) = E_String_Literal_Subtype then
247 Reinit_Esize (E);
248 Reinit_RM_Size (E);
249 return;
250 end if;
252 -- For access types, set size/alignment. This is system address size,
253 -- except for fat pointers (unconstrained array access types), where the
254 -- size is two times the address size, to accommodate the two pointers
255 -- that are required for a fat pointer (data and template). Note that
256 -- E_Access_Protected_Subprogram_Type is not an access type for this
257 -- purpose since it is not a pointer but is equivalent to a record. For
258 -- access subtypes, copy the size from the base type since Gigi
259 -- represents them the same way.
261 if Is_Access_Type (E) then
262 Desig_Type := Underlying_Type (Designated_Type (E));
264 -- If we only have a limited view of the type, see whether the
265 -- non-limited view is available.
267 if From_Limited_With (Designated_Type (E))
268 and then Ekind (Designated_Type (E)) = E_Incomplete_Type
269 and then Present (Non_Limited_View (Designated_Type (E)))
270 then
271 Desig_Type := Non_Limited_View (Designated_Type (E));
272 end if;
274 -- If Esize already set (e.g. by a size or value size clause), then
275 -- nothing further to be done here.
277 if Known_Esize (E) then
278 null;
280 -- Access to protected subprogram is a strange beast, and we let the
281 -- backend figure out what is needed (it may be some kind of fat
282 -- pointer, including the static link for example).
284 elsif Is_Access_Protected_Subprogram_Type (E) then
285 null;
287 -- For access subtypes, copy the size information from base type
289 elsif Ekind (E) = E_Access_Subtype then
290 Set_Size_Info (E, Base_Type (E));
291 Copy_RM_Size (To => E, From => Base_Type (E));
293 -- For other access types, we use either address size, or, if a fat
294 -- pointer is used (pointer-to-unconstrained array case), twice the
295 -- address size to accommodate a fat pointer.
297 elsif Present (Desig_Type)
298 and then Is_Array_Type (Desig_Type)
299 and then not Is_Constrained (Desig_Type)
300 and then not Has_Completion_In_Body (Desig_Type)
302 -- Debug Flag -gnatd6 says make all pointers to unconstrained thin
304 and then not Debug_Flag_6
305 then
306 Init_Size (E, 2 * System_Address_Size);
308 -- Check for bad convention set
310 if Warn_On_Export_Import
311 and then
312 (Convention (E) = Convention_C
313 or else
314 Convention (E) = Convention_CPP)
315 then
316 Error_Msg_N
317 ("?x?this access type does not correspond to C pointer", E);
318 end if;
320 -- If the designated type is a limited view it is unanalyzed. We can
321 -- examine the declaration itself to determine whether it will need a
322 -- fat pointer.
324 elsif Present (Desig_Type)
325 and then Present (Parent (Desig_Type))
326 and then Nkind (Parent (Desig_Type)) = N_Full_Type_Declaration
327 and then Nkind (Type_Definition (Parent (Desig_Type))) =
328 N_Unconstrained_Array_Definition
329 and then not Debug_Flag_6
330 then
331 Init_Size (E, 2 * System_Address_Size);
333 -- If unnesting subprograms, subprogram access types contain the
334 -- address of both the subprogram and an activation record. But if we
335 -- set that, we'll get a warning on different unchecked conversion
336 -- sizes in the RTS. So leave unset in that case.
338 elsif Unnest_Subprogram_Mode
339 and then Is_Access_Subprogram_Type (E)
340 then
341 null;
343 -- Normal case of thin pointer
345 else
346 Init_Size (E, System_Address_Size);
347 end if;
349 Set_Elem_Alignment (E);
351 -- Scalar types: set size and alignment
353 elsif Is_Scalar_Type (E) then
355 -- For discrete types, the RM_Size and Esize must be set already,
356 -- since this is part of the earlier processing and the front end is
357 -- always required to lay out the sizes of such types (since they are
358 -- available as static attributes). All we do is to check that this
359 -- rule is indeed obeyed.
361 if Is_Discrete_Type (E) then
363 -- If the RM_Size is not set, then here is where we set it
365 -- Note: an RM_Size of zero looks like not set here, but this
366 -- is a rare case, and we can simply reset it without any harm.
368 if not Known_RM_Size (E) then
369 Set_Discrete_RM_Size (E);
370 end if;
372 -- If Esize for a discrete type is not set then set it
374 if not Known_Esize (E) then
375 declare
376 S : Pos := 8;
378 begin
379 loop
380 -- If size is big enough, set it and exit
382 if S >= RM_Size (E) then
383 Set_Esize (E, UI_From_Int (S));
384 exit;
386 -- If the RM_Size is greater than System_Max_Integer_Size
387 -- (happens only when strange values are specified by the
388 -- user), then Esize is simply a copy of RM_Size, it will
389 -- be further refined later on.
391 elsif S = System_Max_Integer_Size then
392 Set_Esize (E, RM_Size (E));
393 exit;
395 -- Otherwise double possible size and keep trying
397 else
398 S := S * 2;
399 end if;
400 end loop;
401 end;
402 end if;
404 -- For non-discrete scalar types, if the RM_Size is not set, then set
405 -- it now to a copy of the Esize if the Esize is set.
407 else
408 if Known_Esize (E) and then not Known_RM_Size (E) then
409 Set_RM_Size (E, Esize (E));
410 end if;
411 end if;
413 Set_Elem_Alignment (E);
415 -- Non-elementary (composite) types
417 else
418 -- For packed arrays, take size and alignment values from the packed
419 -- array type if a packed array type has been created and the fields
420 -- are not currently set.
422 if Is_Array_Type (E)
423 and then Present (Packed_Array_Impl_Type (E))
424 then
425 declare
426 PAT : constant Entity_Id := Packed_Array_Impl_Type (E);
428 begin
429 if not Known_Esize (E) then
430 Copy_Esize (To => E, From => PAT);
431 end if;
433 if not Known_RM_Size (E) then
434 Copy_RM_Size (To => E, From => PAT);
435 end if;
437 if not Known_Alignment (E) then
438 Copy_Alignment (To => E, From => PAT);
439 end if;
440 end;
441 end if;
443 -- For array base types, set the component size if object size of the
444 -- component type is known and is a small power of 2 (8, 16, 32, 64
445 -- or 128), since this is what will always be used, except if a very
446 -- large alignment was specified and so Adjust_Esize_For_Alignment
447 -- gave up because, in this case, the object size is not a multiple
448 -- of the alignment and, therefore, cannot be the component size.
450 if Ekind (E) = E_Array_Type and then not Known_Component_Size (E) then
451 declare
452 CT : constant Entity_Id := Component_Type (E);
454 begin
455 -- For some reason, access types can cause trouble, So let's
456 -- just do this for scalar types.
458 if Present (CT)
459 and then Is_Scalar_Type (CT)
460 and then Known_Static_Esize (CT)
461 and then not (Known_Alignment (CT)
462 and then Alignment_In_Bits (CT) >
463 System_Max_Integer_Size)
464 then
465 declare
466 S : constant Uint := Esize (CT);
467 begin
468 if Addressable (S) then
469 Set_Component_Size (E, S);
470 end if;
471 end;
472 end if;
473 end;
474 end if;
476 -- For non-packed arrays set the alignment of the array to the
477 -- alignment of the component type if it is unknown. Skip this
478 -- in full access case since a larger alignment may be needed.
480 if Is_Array_Type (E)
481 and then not Is_Packed (E)
482 and then not Known_Alignment (E)
483 and then Known_Alignment (Component_Type (E))
484 and then Known_Static_Component_Size (E)
485 and then Known_Static_Esize (Component_Type (E))
486 and then Component_Size (E) = Esize (Component_Type (E))
487 and then not Is_Full_Access (E)
488 then
489 Set_Alignment (E, Alignment (Component_Type (E)));
490 end if;
492 -- If packing was requested, the one-dimensional array is constrained
493 -- with static bounds, the component size was set explicitly, and
494 -- the alignment is known, we can set (if not set explicitly) the
495 -- RM_Size and the Esize of the array type, as RM_Size is equal to
496 -- (arr'length * arr'component_size) and Esize is the same value
497 -- rounded to the next multiple of arr'alignment. This is not
498 -- applicable to packed arrays that are implemented specially
499 -- in GNAT, i.e. when Packed_Array_Impl_Type is set.
501 if Is_Array_Type (E)
502 and then Present (First_Index (E)) -- Skip types in error
503 and then Number_Dimensions (E) = 1
504 and then No (Packed_Array_Impl_Type (E))
505 and then Has_Pragma_Pack (E)
506 and then Is_Constrained (E)
507 and then Compile_Time_Known_Bounds (E)
508 and then Known_Component_Size (E)
509 and then Known_Alignment (E)
510 then
511 declare
512 Abits : constant Int := UI_To_Int (Alignment (E)) * SSU;
513 Lo, Hi : Node_Id;
514 Siz : Uint;
516 begin
517 Get_Index_Bounds (First_Index (E), Lo, Hi);
519 -- Even if the bounds are known at compile time, they could
520 -- have been replaced by an error node. Check each bound
521 -- explicitly.
523 if Compile_Time_Known_Value (Lo)
524 and then Compile_Time_Known_Value (Hi)
525 then
526 Siz := (Expr_Value (Hi) - Expr_Value (Lo) + 1)
527 * Component_Size (E);
529 -- Do not overwrite a different value of 'Size specified
530 -- explicitly by the user. In that case, also do not set
531 -- Esize.
533 if not Known_RM_Size (E) or else RM_Size (E) = Siz then
534 Set_RM_Size (E, Siz);
536 if not Known_Esize (E) then
537 Siz := ((Siz + (Abits - 1)) / Abits) * Abits;
538 Set_Esize (E, Siz);
539 end if;
540 end if;
541 end if;
542 end;
543 end if;
544 end if;
546 -- Even if the backend performs the layout, we still do a little in
547 -- the front end
549 -- Processing for record types
551 if Is_Record_Type (E) then
553 -- Special remaining processing for record types with a known
554 -- size of 16, 32, or 64 bits whose alignment is not yet set.
555 -- For these types, we set a corresponding alignment matching
556 -- the size if possible, or as large as possible if not.
558 if Convention (E) = Convention_Ada and then not Debug_Flag_Q then
559 Set_Composite_Alignment (E);
560 end if;
562 -- Processing for array types
564 elsif Is_Array_Type (E) then
566 -- For arrays that are required to be full access, we do the same
567 -- processing as described above for short records, since we really
568 -- need to have the alignment set for the whole array.
570 if Is_Full_Access (E) and then not Debug_Flag_Q then
571 Set_Composite_Alignment (E);
572 end if;
574 -- For unpacked array types, set an alignment of 1 if we know
575 -- that the component alignment is not greater than 1. The reason
576 -- we do this is to avoid unnecessary copying of slices of such
577 -- arrays when passed to subprogram parameters (see special test
578 -- in Exp_Ch6.Expand_Actuals).
580 if not Is_Packed (E) and then not Known_Alignment (E) then
581 if Known_Static_Component_Size (E)
582 and then Component_Size (E) = 1
583 then
584 Set_Alignment (E, Uint_1);
585 end if;
586 end if;
588 -- We need to know whether the size depends on the value of one
589 -- or more discriminants to select the return mechanism. Skip if
590 -- errors are present, to prevent cascaded messages.
592 if Serious_Errors_Detected = 0 then
593 Compute_Size_Depends_On_Discriminant (E);
594 end if;
595 end if;
597 -- Final step is to check that Esize and RM_Size are compatible
599 if Known_Static_Esize (E) and then Known_Static_RM_Size (E) then
600 if Esize (E) < RM_Size (E) then
602 -- Esize is less than RM_Size. That's not good. First we test
603 -- whether this was set deliberately with an Object_Size clause
604 -- and if so, object to the clause.
606 if Has_Object_Size_Clause (E) then
607 Error_Msg_Uint_1 := RM_Size (E);
608 Error_Msg_F
609 ("object size is too small, minimum allowed is ^",
610 Expression (Get_Attribute_Definition_Clause
611 (E, Attribute_Object_Size)));
612 end if;
614 -- Adjust Esize up to RM_Size value
616 declare
617 Size : constant Uint := RM_Size (E);
619 begin
620 Set_Esize (E, Size);
622 -- For scalar types, increase Object_Size to power of 2, but
623 -- not less than a storage unit in any case (i.e., normally
624 -- this means it will be storage-unit addressable).
626 if Is_Scalar_Type (E) then
627 if Size <= SSU then
628 Set_Esize (E, UI_From_Int (SSU));
629 elsif Size <= 16 then
630 Set_Esize (E, Uint_16);
631 elsif Size <= 32 then
632 Set_Esize (E, Uint_32);
633 else
634 Set_Esize (E, (Size + 63) / 64 * 64);
635 end if;
637 -- Finally, make sure that alignment is consistent with
638 -- the newly assigned size.
640 while Alignment (E) * SSU < Esize (E)
641 and then Alignment (E) < Maximum_Alignment
642 loop
643 Set_Alignment (E, 2 * Alignment (E));
644 end loop;
646 -- For the other types, apply standard adjustments
648 else
649 Adjust_Esize_Alignment (E);
650 end if;
651 end;
652 end if;
653 end if;
654 end Layout_Type;
656 -----------------------------
657 -- Set_Composite_Alignment --
658 -----------------------------
660 procedure Set_Composite_Alignment (E : Entity_Id) is
661 Siz : Uint;
662 Align : Nat;
664 begin
665 -- If alignment is already set, then nothing to do
667 if Known_Alignment (E) then
668 return;
669 end if;
671 -- Alignment is not known, see if we can set it, taking into account
672 -- the setting of the Optimize_Alignment mode.
674 -- If Optimize_Alignment is set to Space, then we try to give packed
675 -- records an aligmment of 1, unless there is some reason we can't.
677 if Optimize_Alignment_Space (E)
678 and then Is_Record_Type (E)
679 and then Is_Packed (E)
680 then
681 -- No effect for record with full access components
683 if Is_Full_Access (E) then
684 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
686 if Is_Atomic (E) then
687 Error_Msg_N
688 ("\pragma ignored for atomic record??", E);
689 else
690 Error_Msg_N
691 ("\pragma ignored for bolatile full access record??", E);
692 end if;
694 return;
695 end if;
697 -- No effect if independent components
699 if Has_Independent_Components (E) then
700 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
701 Error_Msg_N
702 ("\pragma ignored for record with independent components??", E);
703 return;
704 end if;
706 -- No effect if a component is full access or of a by-reference type
708 declare
709 Ent : Entity_Id;
711 begin
712 Ent := First_Component_Or_Discriminant (E);
713 while Present (Ent) loop
714 if Is_By_Reference_Type (Etype (Ent))
715 or else Is_Full_Access (Etype (Ent))
716 or else Is_Full_Access (Ent)
717 then
718 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
720 if Is_Atomic (Etype (Ent)) or else Is_Atomic (Ent) then
721 Error_Msg_N
722 ("\pragma is ignored if atomic "
723 & "components present??", E);
724 else
725 Error_Msg_N
726 ("\pragma is ignored if volatile full access "
727 & "components present??", E);
728 end if;
730 return;
731 else
732 Next_Component_Or_Discriminant (Ent);
733 end if;
734 end loop;
735 end;
737 -- Optimize_Alignment has no effect on variable length record
739 if not Size_Known_At_Compile_Time (E) then
740 Error_Msg_N ("Optimize_Alignment has no effect for &??", E);
741 Error_Msg_N ("\pragma is ignored for variable length record??", E);
742 return;
743 end if;
745 -- All tests passed, we can set alignment to 1
747 Align := 1;
749 -- Not a record, or not packed
751 else
752 -- The only other cases we worry about here are where the size is
753 -- statically known at compile time.
755 if Known_Static_Esize (E) then
756 Siz := Esize (E);
757 elsif not Known_Esize (E) and then Known_Static_RM_Size (E) then
758 Siz := RM_Size (E);
759 else
760 return;
761 end if;
763 -- Size is known, alignment is not set
765 -- Reset alignment to match size if the known size is exactly 2, 4,
766 -- or 8 storage units.
768 if Siz = 2 * SSU then
769 Align := 2;
770 elsif Siz = 4 * SSU then
771 Align := 4;
772 elsif Siz = 8 * SSU then
773 Align := 8;
775 -- If Optimize_Alignment is set to Space, then make sure the
776 -- alignment matches the size, for example, if the size is 17
777 -- bytes then we want an alignment of 1 for the type.
779 elsif Optimize_Alignment_Space (E) then
780 if Siz mod (8 * SSU) = 0 then
781 Align := 8;
782 elsif Siz mod (4 * SSU) = 0 then
783 Align := 4;
784 elsif Siz mod (2 * SSU) = 0 then
785 Align := 2;
786 else
787 Align := 1;
788 end if;
790 -- If Optimize_Alignment is set to Time, then we reset for odd
791 -- "in between sizes", for example a 17 bit record is given an
792 -- alignment of 4.
794 elsif Optimize_Alignment_Time (E)
795 and then Siz > SSU
796 and then Siz <= 8 * SSU
797 then
798 if Siz <= 2 * SSU then
799 Align := 2;
800 elsif Siz <= 4 * SSU then
801 Align := 4;
802 else -- Siz <= 8 * SSU then
803 Align := 8;
804 end if;
806 -- No special alignment fiddling needed
808 else
809 return;
810 end if;
811 end if;
813 -- Here we have Set Align to the proposed improved value. Make sure the
814 -- value set does not exceed Maximum_Alignment for the target.
816 if Align > Maximum_Alignment then
817 Align := Maximum_Alignment;
818 end if;
820 -- Further processing for record types only to reduce the alignment
821 -- set by the above processing in some specific cases. We do not
822 -- do this for full access records, since we need max alignment there,
824 if Is_Record_Type (E) and then not Is_Full_Access (E) then
826 -- For records, there is generally no point in setting alignment
827 -- higher than word size since we cannot do better than move by
828 -- words in any case. Omit this if we are optimizing for time,
829 -- since conceivably we may be able to do better.
831 if Align > System_Word_Size / SSU
832 and then not Optimize_Alignment_Time (E)
833 then
834 Align := System_Word_Size / SSU;
835 end if;
837 -- Check components. If any component requires a higher alignment,
838 -- then we set that higher alignment in any case. Don't do this if we
839 -- have Optimize_Alignment set to Space. Note that covers the case of
840 -- packed records, where we already set alignment to 1.
842 if not Optimize_Alignment_Space (E) then
843 declare
844 Comp : Entity_Id;
846 begin
847 Comp := First_Component (E);
848 while Present (Comp) loop
849 if Known_Alignment (Etype (Comp)) then
850 declare
851 Calign : constant Uint := Alignment (Etype (Comp));
853 begin
854 -- The cases to process are when the alignment of the
855 -- component type is larger than the alignment we have
856 -- so far, and either there is no component clause for
857 -- the component, or the length set by the component
858 -- clause matches the length of the component type.
860 if Calign > Align
861 and then
862 (not Known_Esize (Comp)
863 or else (Known_Static_Esize (Comp)
864 and then
865 Esize (Comp) = Calign * SSU))
866 then
867 Align := UI_To_Int (Calign);
868 end if;
869 end;
870 end if;
872 Next_Component (Comp);
873 end loop;
874 end;
875 end if;
876 end if;
878 -- Set chosen alignment, and increase Esize if necessary to match the
879 -- chosen alignment.
881 Set_Alignment (E, UI_From_Int (Align));
883 if Known_Static_Esize (E)
884 and then Esize (E) < Align * SSU
885 then
886 Set_Esize (E, UI_From_Int (Align * SSU));
887 end if;
888 end Set_Composite_Alignment;
890 --------------------------
891 -- Set_Discrete_RM_Size --
892 --------------------------
894 procedure Set_Discrete_RM_Size (Def_Id : Entity_Id) is
895 FST : constant Entity_Id := First_Subtype (Def_Id);
897 begin
898 -- All discrete types except for the base types in standard are
899 -- constrained, so indicate this by setting Is_Constrained.
901 Set_Is_Constrained (Def_Id);
903 -- Set generic types to have an unknown size, since the representation
904 -- of a generic type is irrelevant, in view of the fact that they have
905 -- nothing to do with code.
907 if Is_Generic_Type (Root_Type (FST)) then
908 Reinit_RM_Size (Def_Id);
910 -- If the subtype statically matches the first subtype, then it is
911 -- required to have exactly the same layout. This is required by
912 -- aliasing considerations.
914 elsif Def_Id /= FST and then
915 Subtypes_Statically_Match (Def_Id, FST)
916 then
917 Set_RM_Size (Def_Id, RM_Size (FST));
918 Set_Size_Info (Def_Id, FST);
920 -- In all other cases the RM_Size is set to the minimum size. Note that
921 -- this routine is never called for subtypes for which the RM_Size is
922 -- set explicitly by an attribute clause.
924 else
925 Set_RM_Size (Def_Id, UI_From_Int (Minimum_Size (Def_Id)));
926 end if;
927 end Set_Discrete_RM_Size;
929 ------------------------
930 -- Set_Elem_Alignment --
931 ------------------------
933 procedure Set_Elem_Alignment (E : Entity_Id; Align : Nat := 0) is
934 begin
935 -- Do not set alignment for packed array types, this is handled in the
936 -- backend.
938 if Is_Packed_Array_Impl_Type (E) then
939 return;
941 -- If there is an alignment clause, then we respect it
943 elsif Has_Alignment_Clause (E) then
944 return;
946 -- If the size is not set, then don't attempt to set the alignment. This
947 -- happens in the backend layout case for access-to-subprogram types.
949 elsif not Known_Static_Esize (E) then
950 return;
952 -- For access types, do not set the alignment if the size is less than
953 -- the allowed minimum size. This avoids cascaded error messages.
955 elsif Is_Access_Type (E) and then Esize (E) < System_Address_Size then
956 return;
957 end if;
959 -- We attempt to set the alignment in all the other cases
961 declare
962 S : Int;
963 A : Nat;
964 M : Nat;
966 begin
967 -- The given Esize may be larger that int'last because of a previous
968 -- error, and the call to UI_To_Int will fail, so use default.
970 if Esize (E) / SSU > Ttypes.Maximum_Alignment then
971 S := Ttypes.Maximum_Alignment;
973 -- If this is an access type and the target doesn't have strict
974 -- alignment, then cap the alignment to that of a regular access
975 -- type. This will avoid giving fat pointers twice the usual
976 -- alignment for no practical benefit since the misalignment doesn't
977 -- really matter.
979 elsif Is_Access_Type (E)
980 and then not Target_Strict_Alignment
981 then
982 S := System_Address_Size / SSU;
984 else
985 S := UI_To_Int (Esize (E)) / SSU;
986 end if;
988 -- If the default alignment of "double" floating-point types is
989 -- specifically capped, enforce the cap.
991 if Ttypes.Target_Double_Float_Alignment > 0
992 and then S = 8
993 and then Is_Floating_Point_Type (E)
994 then
995 M := Ttypes.Target_Double_Float_Alignment;
997 -- If the default alignment of "double" or larger scalar types is
998 -- specifically capped, enforce the cap.
1000 elsif Ttypes.Target_Double_Scalar_Alignment > 0
1001 and then S >= 8
1002 and then Is_Scalar_Type (E)
1003 then
1004 M := Ttypes.Target_Double_Scalar_Alignment;
1006 -- Otherwise enforce the overall alignment cap
1008 else
1009 M := Ttypes.Maximum_Alignment;
1010 end if;
1012 -- We calculate the alignment as the largest power-of-two multiple
1013 -- of System.Storage_Unit that does not exceed the object size of
1014 -- the type and the maximum allowed alignment, if none was specified.
1015 -- Otherwise we only cap it to the maximum allowed alignment.
1017 if Align = 0 then
1018 A := 1;
1019 while 2 * A <= S and then 2 * A <= M loop
1020 A := 2 * A;
1021 end loop;
1022 else
1023 A := Nat'Min (Align, M);
1024 end if;
1026 -- If alignment is currently not set, then we can safely set it to
1027 -- this new calculated value.
1029 if not Known_Alignment (E) then
1030 Set_Alignment (E, UI_From_Int (A));
1032 -- Cases where we have inherited an alignment
1034 -- For constructed types, always reset the alignment, these are
1035 -- generally invisible to the user anyway, and that way we are
1036 -- sure that no constructed types have weird alignments.
1038 elsif not Comes_From_Source (E) then
1039 Set_Alignment (E, UI_From_Int (A));
1041 -- If this inherited alignment is the same as the one we computed,
1042 -- then obviously everything is fine, and we do not need to reset it.
1044 elsif Alignment (E) = A then
1045 null;
1047 else
1048 -- Now we come to the difficult cases of subtypes for which we
1049 -- have inherited an alignment different from the computed one.
1050 -- We resort to the presence of alignment and size clauses to
1051 -- guide our choices. Note that they can generally be present
1052 -- only on the first subtype (except for Object_Size) and that
1053 -- we need to look at the Rep_Item chain to correctly handle
1054 -- derived types.
1056 declare
1057 function Has_Attribute_Clause
1058 (E : Entity_Id;
1059 Id : Attribute_Id) return Boolean;
1060 -- Wrapper around Get_Attribute_Definition_Clause which tests
1061 -- for the presence of the specified attribute clause.
1063 --------------------------
1064 -- Has_Attribute_Clause --
1065 --------------------------
1067 function Has_Attribute_Clause
1068 (E : Entity_Id;
1069 Id : Attribute_Id) return Boolean is
1070 begin
1071 return Present (Get_Attribute_Definition_Clause (E, Id));
1072 end Has_Attribute_Clause;
1074 FST : Entity_Id;
1076 begin
1077 FST := First_Subtype (E);
1079 -- Deal with private types
1081 if Is_Private_Type (FST) then
1082 FST := Full_View (FST);
1083 end if;
1085 -- If the alignment comes from a clause, then we respect it.
1086 -- Consider for example:
1088 -- type R is new Character;
1089 -- for R'Alignment use 1;
1090 -- for R'Size use 16;
1091 -- subtype S is R;
1093 -- Here R has a specified size of 16 and a specified alignment
1094 -- of 1, and it seems right for S to inherit both values.
1096 if Has_Attribute_Clause (FST, Attribute_Alignment) then
1097 null;
1099 -- Now we come to the cases where we have inherited alignment
1100 -- and size, and overridden the size but not the alignment.
1102 elsif Has_Attribute_Clause (FST, Attribute_Size)
1103 or else Has_Attribute_Clause (FST, Attribute_Object_Size)
1104 or else Has_Attribute_Clause (E, Attribute_Object_Size)
1105 then
1106 -- This is tricky, it might be thought that we should try to
1107 -- inherit the alignment, since that's what the RM implies,
1108 -- but that leads to complex rules and oddities. Consider
1109 -- for example:
1111 -- type R is new Character;
1112 -- for R'Size use 16;
1114 -- It seems quite bogus in this case to inherit an alignment
1115 -- of 1 from the parent type Character. Furthermore, if that
1116 -- is what the programmer really wanted for some odd reason,
1117 -- then he could specify the alignment directly.
1119 -- Moreover we really don't want to inherit the alignment in
1120 -- the case of a specified Object_Size for a subtype, since
1121 -- there would be no way of overriding to give a reasonable
1122 -- value (as we don't have an Object_Alignment attribute).
1123 -- Consider for example:
1125 -- subtype R is Character;
1126 -- for R'Object_Size use 16;
1128 -- If we inherit the alignment of 1, then it will be very
1129 -- inefficient for the subtype and this cannot be fixed.
1131 -- So we make the decision that if Size (or Object_Size) is
1132 -- given and the alignment is not specified with a clause,
1133 -- we reset the alignment to the appropriate value for the
1134 -- specified size. This is a nice simple rule to implement
1135 -- and document.
1137 -- There is a theoretical glitch, which is that a confirming
1138 -- size clause could now change the alignment, which, if we
1139 -- really think that confirming rep clauses should have no
1140 -- effect, could be seen as a no-no. However that's already
1141 -- implemented by Alignment_Check_For_Size_Change so we do
1142 -- not change the philosophy here.
1144 -- Historical note: in versions prior to Nov 6th, 2011, an
1145 -- odd distinction was made between inherited alignments
1146 -- larger than the computed alignment (where the larger
1147 -- alignment was inherited) and inherited alignments smaller
1148 -- than the computed alignment (where the smaller alignment
1149 -- was overridden). This was a dubious fix to get around an
1150 -- ACATS problem which seems to have disappeared anyway, and
1151 -- in any case, this peculiarity was never documented.
1153 Set_Alignment (E, UI_From_Int (A));
1155 -- If no Size (or Object_Size) was specified, then we have
1156 -- inherited the object size, so we should also inherit the
1157 -- alignment and not modify it.
1159 else
1160 null;
1161 end if;
1162 end;
1163 end if;
1164 end;
1165 end Set_Elem_Alignment;
1167 end Layout;