Debian Codegraph @

gcc/ada/exp_pakd.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ P A K D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, 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 ------------------------------------------------------------------------------
25
26 with Atree; use Atree;
27 with Checks; use Checks;
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 Exp_Dbug; use Exp_Dbug;
33 with Exp_Util; use Exp_Util;
34 with Layout; use Layout;
35 with Lib.Xref; use Lib.Xref;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Sem; use Sem;
41 with Sem_Aux; use Sem_Aux;
42 with Sem_Ch3; use Sem_Ch3;
43 with Sem_Ch8; use Sem_Ch8;
44 with Sem_Ch13; use Sem_Ch13;
45 with Sem_Eval; use Sem_Eval;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sinfo; use Sinfo;
49 with Sinfo.Nodes; use Sinfo.Nodes;
50 with Sinfo.Utils; use Sinfo.Utils;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Ttypes; use Ttypes;
56 with Uintp; use Uintp;
57
58 package body Exp_Pakd is
59
60 ---------------------------
61 -- Endian Considerations --
62 ---------------------------
63
64 -- As described in the specification, bit numbering in a packed array
65 -- is consistent with bit numbering in a record representation clause,
66 -- and hence dependent on the endianness of the machine:
67
68 -- For little-endian machines, element zero is at the right hand end
69 -- (low order end) of a bit field.
70
71 -- For big-endian machines, element zero is at the left hand end
72 -- (high order end) of a bit field.
73
74 -- The shifts that are used to right justify a field therefore differ in
75 -- the two cases. For the little-endian case, we can simply use the bit
76 -- number (i.e. the element number * element size) as the count for a right
77 -- shift. For the big-endian case, we have to subtract the shift count from
78 -- an appropriate constant to use in the right shift. We use rotates
79 -- instead of shifts (which is necessary in the store case to preserve
80 -- other fields), and we expect that the backend will be able to change the
81 -- right rotate into a left rotate, avoiding the subtract, if the machine
82 -- architecture provides such an instruction.
83
84 -----------------------
85 -- Local Subprograms --
86 -----------------------
87
88 procedure Compute_Linear_Subscript
89 (Atyp : Entity_Id;
90 N : Node_Id;
91 Subscr : out Node_Id);
92 -- Given a constrained array type Atyp, and an indexed component node N
93 -- referencing an array object of this type, build an expression of type
94 -- Standard.Integer representing the zero-based linear subscript value.
95 -- This expression includes any required range checks.
96
97 function Compute_Number_Components
98 (N : Node_Id;
99 Typ : Entity_Id) return Node_Id;
100 -- Build an expression that multiplies the length of the dimensions of the
101 -- array, used to control array equality checks.
102
103 procedure Convert_To_PAT_Type (Aexp : Node_Id);
104 -- Given an expression of a packed array type, builds a corresponding
105 -- expression whose type is the implementation type used to represent
106 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
107
108 procedure Get_Base_And_Bit_Offset
109 (N : Node_Id;
110 Base : out Node_Id;
111 Offset : out Node_Id);
112 -- Given a node N for a name which involves a packed array reference,
113 -- return the base object of the reference and build an expression of
114 -- type Standard.Integer representing the zero-based offset in bits
115 -- from Base'Address to the first bit of the reference.
116
117 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
118 -- There are two versions of the Set routines, the ones used when the
119 -- object is known to be sufficiently well aligned given the number of
120 -- bits, and the ones used when the object is not known to be aligned.
121 -- This routine is used to determine which set to use. Obj is a reference
122 -- to the object, and Csiz is the component size of the packed array.
123 -- True is returned if the alignment of object is known to be sufficient,
124 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
125 -- 2 otherwise.
126
127 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
128 -- Build a left shift node, checking for the case of a shift count of zero
129
130 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
131 -- Build a right shift node, checking for the case of a shift count of zero
132
133 function RJ_Unchecked_Convert_To
134 (Typ : Entity_Id;
135 Expr : Node_Id) return Node_Id;
136 -- The packed array code does unchecked conversions which in some cases
137 -- may involve non-discrete types with differing sizes. The semantics of
138 -- such conversions is potentially endianness dependent, and the effect
139 -- we want here for such a conversion is to do the conversion in size as
140 -- though numeric items are involved, and we extend or truncate on the
141 -- left side. This happens naturally in the little-endian case, but in
142 -- the big endian case we can get left justification, when what we want
143 -- is right justification. This routine does the unchecked conversion in
144 -- a stepwise manner to ensure that it gives the expected result. Hence
145 -- the name (RJ = Right justified). The parameters Typ and Expr are as
146 -- for the case of a normal Unchecked_Convert_To call.
147
148 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
149 -- This routine is called in the Get and Set case for arrays that are
150 -- packed but not bit-packed, meaning that they have at least one
151 -- subscript that is of an enumeration type with a non-standard
152 -- representation. This routine modifies the given node to properly
153 -- reference the corresponding packed array type.
154
155 procedure Setup_Inline_Packed_Array_Reference
156 (N : Node_Id;
157 Atyp : Entity_Id;
158 Obj : in out Node_Id;
159 Cmask : out Uint;
160 Shift : out Node_Id);
161 -- This procedure performs common processing on the N_Indexed_Component
162 -- parameter given as N, whose prefix is a reference to a packed array.
163 -- This is used for the get and set when the component size is 1, 2, 4,
164 -- or for other component sizes when the packed array type is a modular
165 -- type (i.e. the cases that are handled with inline code).
166 --
167 -- On entry:
168 --
169 -- N is the N_Indexed_Component node for the packed array reference
170 --
171 -- Atyp is the constrained array type (the actual subtype has been
172 -- computed if necessary to obtain the constraints, but this is still
173 -- the original array type, not the Packed_Array_Impl_Type value).
174 --
175 -- Obj is the object which is to be indexed. It is always of type Atyp.
176 --
177 -- On return:
178 --
179 -- Obj is the object containing the desired bit field. It is of type
180 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
181 -- entire value, for the small static case, or the proper selected byte
182 -- from the array in the large or dynamic case. This node is analyzed
183 -- and resolved on return.
184 --
185 -- Shift is a node representing the shift count to be used in the
186 -- rotate right instruction that positions the field for access.
187 -- This node is analyzed and resolved on return.
188 --
189 -- Cmask is a mask corresponding to the width of the component field.
190 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
191 --
192 -- Note: in some cases the call to this routine may generate actions
193 -- (for handling multi-use references and the generation of the packed
194 -- array type on the fly). Such actions are inserted into the tree
195 -- directly using Insert_Action.
196
197 function Revert_Storage_Order (N : Node_Id) return Node_Id;
198 -- Perform appropriate justification and byte ordering adjustments for N,
199 -- an element of a packed array type, when both the component type and
200 -- the enclosing packed array type have reverse scalar storage order.
201 -- On little-endian targets, the value is left justified before byte
202 -- swapping. The Etype of the returned expression is an integer type of
203 -- an appropriate power-of-2 size.
204
205 --------------------------
206 -- Revert_Storage_Order --
207 --------------------------
208
209 function Revert_Storage_Order (N : Node_Id) return Node_Id is
210 Loc : constant Source_Ptr := Sloc (N);
211 T : constant Entity_Id := Etype (N);
212 T_Size : constant Uint := RM_Size (T);
213
214 Swap_RE : RE_Id;
215 Swap_F : Entity_Id;
216 Swap_T : Entity_Id;
217 -- Swapping function
218
219 Arg : Node_Id;
220 Adjusted : Node_Id;
221 Shift : Uint;
222
223 begin
224 if T_Size <= 8 then
225
226 -- Array component size is less than a byte: no swapping needed
227
228 Swap_F := Empty;
229 Swap_T := RTE (RE_Unsigned_8);
230
231 else
232 -- Select byte swapping function depending on array component size
233
234 if T_Size <= 16 then
235 Swap_RE := RE_Bswap_16;
236
237 elsif T_Size <= 32 then
238 Swap_RE := RE_Bswap_32;
239
240 elsif T_Size <= 64 then
241 Swap_RE := RE_Bswap_64;
242
243 else pragma Assert (T_Size <= 128);
244 Swap_RE := RE_Bswap_128;
245 end if;
246
247 Swap_F := RTE (Swap_RE);
248 Swap_T := Etype (Swap_F);
249
250 end if;
251
252 Shift := Esize (Swap_T) - T_Size;
253
254 Arg := RJ_Unchecked_Convert_To (Swap_T, N);
255
256 if not Bytes_Big_Endian and then Shift > Uint_0 then
257 Arg :=
258 Make_Op_Shift_Left (Loc,
259 Left_Opnd => Arg,
260 Right_Opnd => Make_Integer_Literal (Loc, Shift));
261 end if;
262
263 if Present (Swap_F) then
264 Adjusted :=
265 Make_Function_Call (Loc,
266 Name => New_Occurrence_Of (Swap_F, Loc),
267 Parameter_Associations => New_List (Arg));
268 else
269 Adjusted := Arg;
270 end if;
271
272 Set_Etype (Adjusted, Swap_T);
273 return Adjusted;
274 end Revert_Storage_Order;
275
276 ------------------------------
277 -- Compute_Linear_Subscript --
278 ------------------------------
279
280 procedure Compute_Linear_Subscript
281 (Atyp : Entity_Id;
282 N : Node_Id;
283 Subscr : out Node_Id)
284 is
285 Loc : constant Source_Ptr := Sloc (N);
286 Oldsub : Node_Id;
287 Newsub : Node_Id;
288 Indx : Node_Id;
289 Styp : Entity_Id;
290
291 begin
292 Subscr := Empty;
293
294 -- Loop through dimensions
295
296 Indx := First_Index (Atyp);
297 Oldsub := First (Expressions (N));
298
299 while Present (Indx) loop
300 Styp := Etype (Indx);
301 Newsub := Relocate_Node (Oldsub);
302
303 -- Get expression for the subscript value. First, if Do_Range_Check
304 -- is set on a subscript, then we must do a range check against the
305 -- original bounds (not the bounds of the packed array type). We do
306 -- this by introducing a subtype conversion.
307
308 if Do_Range_Check (Newsub)
309 and then Etype (Newsub) /= Styp
310 then
311 Newsub := Convert_To (Styp, Newsub);
312 end if;
313
314 -- Now evolve the expression for the subscript. First convert
315 -- the subscript to be zero based and of an integer type.
316
317 -- Case of integer type, where we just subtract to get lower bound
318
319 if Is_Integer_Type (Styp) then
320
321 -- If length of integer type is smaller than standard integer,
322 -- then we convert to integer first, then do the subtract
323
324 -- Integer (subscript) - Integer (Styp'First)
325
326 if Esize (Styp) < Standard_Integer_Size then
327 Newsub :=
328 Make_Op_Subtract (Loc,
329 Left_Opnd => Convert_To (Standard_Integer, Newsub),
330 Right_Opnd =>
331 Convert_To (Standard_Integer,
332 Make_Attribute_Reference (Loc,
333 Prefix => New_Occurrence_Of (Styp, Loc),
334 Attribute_Name => Name_First)));
335
336 -- For larger integer types, subtract first, then convert to
337 -- integer, this deals with strange long long integer bounds.
338
339 -- Integer (subscript - Styp'First)
340
341 else
342 Newsub :=
343 Convert_To (Standard_Integer,
344 Make_Op_Subtract (Loc,
345 Left_Opnd => Newsub,
346 Right_Opnd =>
347 Make_Attribute_Reference (Loc,
348 Prefix => New_Occurrence_Of (Styp, Loc),
349 Attribute_Name => Name_First)));
350 end if;
351
352 -- For the enumeration case, we have to use 'Pos to get the value
353 -- to work with before subtracting the lower bound.
354
355 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
356
357 -- This is not quite right for bizarre cases where the size of the
358 -- enumeration type is > Integer'Size bits due to rep clause ???
359
360 else
361 pragma Assert (Is_Enumeration_Type (Styp));
362
363 Newsub :=
364 Make_Op_Subtract (Loc,
365 Left_Opnd => Convert_To (Standard_Integer,
366 Make_Attribute_Reference (Loc,
367 Prefix => New_Occurrence_Of (Styp, Loc),
368 Attribute_Name => Name_Pos,
369 Expressions => New_List (Newsub))),
370
371 Right_Opnd =>
372 Convert_To (Standard_Integer,
373 Make_Attribute_Reference (Loc,
374 Prefix => New_Occurrence_Of (Styp, Loc),
375 Attribute_Name => Name_Pos,
376 Expressions => New_List (
377 Make_Attribute_Reference (Loc,
378 Prefix => New_Occurrence_Of (Styp, Loc),
379 Attribute_Name => Name_First)))));
380 end if;
381
382 Set_Paren_Count (Newsub, 1);
383
384 -- For the first subscript, we just copy that subscript value
385
386 if No (Subscr) then
387 Subscr := Newsub;
388
389 -- Otherwise, we must multiply what we already have by the current
390 -- stride and then add in the new value to the evolving subscript.
391
392 else
393 Subscr :=
394 Make_Op_Add (Loc,
395 Left_Opnd =>
396 Make_Op_Multiply (Loc,
397 Left_Opnd => Subscr,
398 Right_Opnd =>
399 Make_Attribute_Reference (Loc,
400 Attribute_Name => Name_Range_Length,
401 Prefix => New_Occurrence_Of (Styp, Loc))),
402 Right_Opnd => Newsub);
403 end if;
404
405 -- Move to next subscript
406
407 Next_Index (Indx);
408 Next (Oldsub);
409 end loop;
410 end Compute_Linear_Subscript;
411
412 -------------------------------
413 -- Compute_Number_Components --
414 -------------------------------
415
416 function Compute_Number_Components
417 (N : Node_Id;
418 Typ : Entity_Id) return Node_Id
419 is
420 Loc : constant Source_Ptr := Sloc (N);
421 Len_Expr : Node_Id;
422
423 begin
424 Len_Expr :=
425 Make_Attribute_Reference (Loc,
426 Attribute_Name => Name_Length,
427 Prefix => New_Occurrence_Of (Typ, Loc),
428 Expressions => New_List (Make_Integer_Literal (Loc, 1)));
429
430 for J in 2 .. Number_Dimensions (Typ) loop
431 Len_Expr :=
432 Make_Op_Multiply (Loc,
433 Left_Opnd => Len_Expr,
434 Right_Opnd =>
435 Make_Attribute_Reference (Loc,
436 Attribute_Name => Name_Length,
437 Prefix => New_Occurrence_Of (Typ, Loc),
438 Expressions => New_List (Make_Integer_Literal (Loc, J))));
439 end loop;
440
441 return Len_Expr;
442 end Compute_Number_Components;
443
444 -------------------------
445 -- Convert_To_PAT_Type --
446 -------------------------
447
448 -- The PAT is always obtained from the actual subtype
449
450 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
451 Act_ST : Entity_Id;
452
453 begin
454 Convert_To_Actual_Subtype (Aexp);
455 Act_ST := Underlying_Type (Etype (Aexp));
456 Create_Packed_Array_Impl_Type (Act_ST);
457
458 -- Just replace the etype with the packed array type. This works because
459 -- the expression will not be further analyzed, and Gigi considers the
460 -- two types equivalent in any case.
461
462 -- This is not strictly the case ??? If the reference is an actual in
463 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
464 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
465 -- array reference, reanalysis can produce spurious type errors when the
466 -- PAT type is replaced again with the original type of the array. Same
467 -- for the case of a dereference. Ditto for function calls: expansion
468 -- may introduce additional actuals which will trigger errors if call is
469 -- reanalyzed. The following is correct and minimal, but the handling of
470 -- more complex packed expressions in actuals is confused. Probably the
471 -- problem only remains for actuals in calls.
472
473 Set_Etype (Aexp, Packed_Array_Impl_Type (Act_ST));
474
475 if Is_Entity_Name (Aexp)
476 or else
477 (Nkind (Aexp) = N_Indexed_Component
478 and then Is_Entity_Name (Prefix (Aexp)))
479 or else Nkind (Aexp) in N_Explicit_Dereference | N_Function_Call
480 then
481 Set_Analyzed (Aexp);
482 end if;
483 end Convert_To_PAT_Type;
484
485 -----------------------------------
486 -- Create_Packed_Array_Impl_Type --
487 -----------------------------------
488
489 procedure Create_Packed_Array_Impl_Type (Typ : Entity_Id) is
490 Loc : constant Source_Ptr := Sloc (Typ);
491 Ctyp : constant Entity_Id := Component_Type (Typ);
492 Csize : constant Uint := Component_Size (Typ);
493
494 Ancest : Entity_Id;
495 PB_Type : Entity_Id;
496 PASize : Uint := No_Uint;
497 Decl : Node_Id;
498 PAT : Entity_Id;
499 Len_Expr : Node_Id;
500 Len_Bits : Uint;
501 Bits_U1 : Node_Id;
502 PAT_High : Node_Id;
503 Btyp : Entity_Id;
504 Lit : Node_Id;
505
506 procedure Install_PAT;
507 -- This procedure is called with Decl set to the declaration for the
508 -- packed array type. It creates the type and installs it as required.
509
510 procedure Set_PB_Type;
511 -- Set PB_Type to [Rev_]Packed_Bytes{1,2,4} as required by the alignment
512 -- and the scalar storage order requirements (see documentation in the
513 -- spec of this package).
514
515 -----------------
516 -- Install_PAT --
517 -----------------
518
519 procedure Install_PAT is
520 Pushed_Scope : Boolean := False;
521
522 begin
523 -- We do not want to put the declaration we have created in the tree
524 -- since it is often hard, and sometimes impossible to find a proper
525 -- place for it (the impossible case arises for a packed array type
526 -- with bounds depending on the discriminant, a declaration cannot
527 -- be put inside the record, and the reference to the discriminant
528 -- cannot be outside the record).
529
530 -- The solution is to analyze the declaration while temporarily
531 -- attached to the tree at an appropriate point, and then we install
532 -- the resulting type as an Itype in the packed array type field of
533 -- the original type, so that no explicit declaration is required.
534
535 -- Note: the packed type is created in the scope of its parent type.
536 -- There are at least some cases where the current scope is deeper,
537 -- and so when this is the case, we temporarily reset the scope
538 -- for the definition. This is clearly safe, since the first use
539 -- of the packed array type will be the implicit reference from
540 -- the corresponding unpacked type when it is elaborated.
541
542 if Is_Itype (Typ) then
543 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
544 Set_Associated_Node_For_Itype
545 (PAT, Associated_Node_For_Itype (Typ));
546 else
547 Set_Parent (Decl, Declaration_Node (Typ));
548 Set_Associated_Node_For_Itype
549 (PAT, Declaration_Node (Typ));
550 end if;
551
552 if Scope (Typ) /= Current_Scope then
553 Push_Scope (Scope (Typ));
554 Pushed_Scope := True;
555 end if;
556
557 Set_Is_Itype (PAT, True);
558 Set_Is_Packed_Array_Impl_Type (PAT, True);
559 Set_Packed_Array_Impl_Type (Typ, PAT);
560 Analyze (Decl, Suppress => All_Checks);
561
562 if Pushed_Scope then
563 Pop_Scope;
564 end if;
565
566 -- Set Esize and RM_Size to the actual size of the packed object
567 -- Do not reset RM_Size if already set, as happens in the case of
568 -- a modular type.
569
570 if Present (PASize) then
571 if not Known_Esize (PAT) then
572 Set_Esize (PAT, PASize);
573 end if;
574
575 if not Known_RM_Size (PAT) then
576 Set_RM_Size (PAT, PASize);
577 end if;
578 end if;
579
580 -- In the case of a modular type, make sure the alignment is
581 -- consistent with the Esize.
582
583 if Is_Scalar_Type (PAT) then
584 while Alignment (PAT) * System_Storage_Unit < Esize (PAT)
585 and then Alignment (PAT) < Maximum_Alignment
586 loop
587 Set_Alignment (PAT, 2 * Alignment (PAT));
588 end loop;
589 end if;
590
591 -- Then, in all cases, make sure the opposite is also true
592
593 Adjust_Esize_Alignment (PAT);
594
595 -- Set remaining fields of packed array type
596
597 Set_Parent (PAT, Empty);
598 Set_Associated_Node_For_Itype (PAT, Typ);
599 Set_Original_Array_Type (PAT, Typ);
600
601 -- In the case of a constrained array type, also set fields on the
602 -- implicit base type built during the analysis of its declaration.
603
604 if Ekind (PAT) = E_Array_Subtype then
605 Set_Is_Packed_Array_Impl_Type (Etype (PAT), True);
606 Set_Original_Array_Type (Etype (PAT), Base_Type (Typ));
607 end if;
608
609 -- Propagate representation aspects
610
611 Set_Is_Atomic (PAT, Is_Atomic (Typ));
612 Set_Is_Independent (PAT, Is_Independent (Typ));
613 Set_Is_Volatile (PAT, Is_Volatile (Typ));
614 Set_Is_Volatile_Full_Access (PAT, Is_Volatile_Full_Access (Typ));
615 Set_Treat_As_Volatile (PAT, Treat_As_Volatile (Typ));
616
617 -- We definitely do not want to delay freezing for packed array
618 -- types. This is of particular importance for the itypes that are
619 -- generated for record components depending on discriminants where
620 -- there is no place to put the freeze node.
621
622 Set_Has_Delayed_Freeze (PAT, False);
623 Set_Has_Delayed_Freeze (Etype (PAT), False);
624
625 -- If we did allocate a freeze node, then clear out the reference
626 -- since it is obsolete (should we delete the freeze node???)
627
628 Set_Freeze_Node (PAT, Empty);
629 Set_Freeze_Node (Etype (PAT), Empty);
630 end Install_PAT;
631
632 -----------------
633 -- Set_PB_Type --
634 -----------------
635
636 procedure Set_PB_Type is
637 begin
638 -- If the user has specified an explicit alignment for the
639 -- type or component, take it into account.
640
641 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
642 or else (Known_Alignment (Typ) and then Alignment (Typ) = 1)
643 or else Component_Alignment (Typ) = Calign_Storage_Unit
644 then
645 if Reverse_Storage_Order (Typ) then
646 PB_Type := RTE (RE_Rev_Packed_Bytes1);
647 else
648 PB_Type := RTE (RE_Packed_Bytes1);
649 end if;
650
651 elsif Csize mod 4 /= 0
652 or else (Known_Alignment (Typ) and then Alignment (Typ) = 2)
653 then
654 if Reverse_Storage_Order (Typ) then
655 PB_Type := RTE (RE_Rev_Packed_Bytes2);
656 else
657 PB_Type := RTE (RE_Packed_Bytes2);
658 end if;
659
660 else
661 if Reverse_Storage_Order (Typ) then
662 PB_Type := RTE (RE_Rev_Packed_Bytes4);
663 else
664 PB_Type := RTE (RE_Packed_Bytes4);
665 end if;
666 end if;
667
668 -- The Rev_Packed_Bytes{1,2,4} types cannot be directly declared with
669 -- the reverse scalar storage order in System.Unsigned_Types because
670 -- their component type is aliased and the combination would then be
671 -- flagged as illegal by the compiler. Moreover changing the compiler
672 -- would not address the bootstrap path issue with earlier versions.
673
674 Set_Reverse_Storage_Order (PB_Type, Reverse_Storage_Order (Typ));
675 end Set_PB_Type;
676
677 -- Start of processing for Create_Packed_Array_Impl_Type
678
679 begin
680 -- If we already have a packed array type, nothing to do
681
682 if Present (Packed_Array_Impl_Type (Typ)) then
683 return;
684 end if;
685
686 -- If our immediate ancestor subtype is constrained, and it already has
687 -- a packed array type, and it has the same size, then just share the
688 -- same type, since the bounds must be the same. If the ancestor is not
689 -- an array type but a private type, as can happen with multiple
690 -- instantiations, create a new packed type, to avoid privacy issues.
691
692 if Ekind (Typ) = E_Array_Subtype then
693 Ancest := Ancestor_Subtype (Typ);
694
695 if Present (Ancest)
696 and then Is_Array_Type (Ancest)
697 and then Is_Constrained (Ancest)
698 and then Present (Packed_Array_Impl_Type (Ancest))
699 and then Known_Esize (Typ)
700 and then Known_Esize (Ancest)
701 and then Esize (Typ) = Esize (Ancest)
702 then
703 Set_Packed_Array_Impl_Type (Typ, Packed_Array_Impl_Type (Ancest));
704 return;
705 end if;
706 end if;
707
708 -- We preset the result type size from the size of the original array
709 -- type, since this size clearly belongs to the packed array type. The
710 -- size of the conceptual unpacked type is always set to unknown.
711
712 if Known_RM_Size (Typ) then
713 PASize := RM_Size (Typ);
714 end if;
715
716 -- Case of an array where at least one index is of an enumeration
717 -- type with a non-standard representation, but the component size
718 -- is not appropriate for bit packing. This is the case where we
719 -- have Is_Packed set (we would never be in this unit otherwise),
720 -- but Is_Bit_Packed_Array is false.
721
722 -- Note that if the component size is appropriate for bit packing,
723 -- then the circuit for the computation of the subscript properly
724 -- deals with the non-standard enumeration type case by taking the
725 -- Pos anyway.
726
727 if not Is_Bit_Packed_Array (Typ) then
728
729 -- Here we build a declaration:
730
731 -- type tttP is array (index1, index2, ...) of component_type
732
733 -- where index1, index2, are the index types. These are the same
734 -- as the index types of the original array, except for the non-
735 -- standard representation enumeration type case, where we have
736 -- two subcases.
737
738 -- For the unconstrained array case, we use
739
740 -- Natural range <>
741
742 -- For the constrained case, we use
743
744 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
745 -- Enum_Type'Pos (Enum_Type'Last);
746
747 -- Note that tttP is created even if no index subtype is a non
748 -- standard enumeration, because we still need to remove padding
749 -- normally inserted for component alignment.
750
751 PAT :=
752 Make_Defining_Identifier (Loc,
753 Chars => New_External_Name (Chars (Typ), 'P'));
754
755 declare
756 Indexes : constant List_Id := New_List;
757 Indx : Node_Id;
758 Indx_Typ : Entity_Id;
759 Enum_Case : Boolean;
760 Typedef : Node_Id;
761
762 begin
763 Indx := First_Index (Typ);
764
765 while Present (Indx) loop
766 Indx_Typ := Etype (Indx);
767
768 Enum_Case := Is_Enumeration_Type (Indx_Typ)
769 and then Has_Non_Standard_Rep (Indx_Typ);
770
771 -- Unconstrained case
772
773 if not Is_Constrained (Typ) then
774 if Enum_Case then
775 Indx_Typ := Standard_Natural;
776 end if;
777
778 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
779
780 -- Constrained case
781
782 else
783 if not Enum_Case then
784 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
785
786 else
787 Append_To (Indexes,
788 Make_Subtype_Indication (Loc,
789 Subtype_Mark =>
790 New_Occurrence_Of (Standard_Natural, Loc),
791 Constraint =>
792 Make_Range_Constraint (Loc,
793 Range_Expression =>
794 Make_Range (Loc,
795 Low_Bound =>
796 Make_Attribute_Reference (Loc,
797 Prefix =>
798 New_Occurrence_Of (Indx_Typ, Loc),
799 Attribute_Name => Name_Pos,
800 Expressions => New_List (
801 Make_Attribute_Reference (Loc,
802 Prefix =>
803 New_Occurrence_Of (Indx_Typ, Loc),
804 Attribute_Name => Name_First))),
805
806 High_Bound =>
807 Make_Attribute_Reference (Loc,
808 Prefix =>
809 New_Occurrence_Of (Indx_Typ, Loc),
810 Attribute_Name => Name_Pos,
811 Expressions => New_List (
812 Make_Attribute_Reference (Loc,
813 Prefix =>
814 New_Occurrence_Of (Indx_Typ, Loc),
815 Attribute_Name => Name_Last)))))));
816
817 end if;
818 end if;
819
820 Next_Index (Indx);
821 end loop;
822
823 if not Is_Constrained (Typ) then
824 Typedef :=
825 Make_Unconstrained_Array_Definition (Loc,
826 Subtype_Marks => Indexes,
827 Component_Definition =>
828 Make_Component_Definition (Loc,
829 Aliased_Present => Has_Aliased_Components (Typ),
830 Subtype_Indication =>
831 New_Occurrence_Of (Ctyp, Loc)));
832
833 else
834 Typedef :=
835 Make_Constrained_Array_Definition (Loc,
836 Discrete_Subtype_Definitions => Indexes,
837 Component_Definition =>
838 Make_Component_Definition (Loc,
839 Aliased_Present => Has_Aliased_Components (Typ),
840 Subtype_Indication =>
841 New_Occurrence_Of (Ctyp, Loc)));
842 end if;
843
844 Decl :=
845 Make_Full_Type_Declaration (Loc,
846 Defining_Identifier => PAT,
847 Type_Definition => Typedef);
848 end;
849
850 Install_PAT;
851
852 -- Propagate the reverse storage order flag to the base type
853
854 Set_Reverse_Storage_Order (Etype (PAT), Reverse_Storage_Order (Typ));
855 return;
856
857 -- Case of bit-packing required for unconstrained array. We create
858 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
859
860 elsif not Is_Constrained (Typ) then
861
862 -- When generating standard DWARF (i.e when GNAT_Encodings is not
863 -- DWARF_GNAT_Encodings_All), the ___XP suffix will be stripped
864 -- by the back-end but generate it anyway to ease compiler debugging.
865 -- This will help to distinguish implementation types from original
866 -- packed arrays.
867
868 PAT :=
869 Make_Defining_Identifier (Loc,
870 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
871
872 Set_PB_Type;
873
874 Decl :=
875 Make_Subtype_Declaration (Loc,
876 Defining_Identifier => PAT,
877 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
878
879 Install_PAT;
880 return;
881
882 -- Remaining code is for the case of bit-packing for constrained array
883
884 -- The name of the packed array subtype is
885
886 -- ttt___XPsss
887
888 -- where sss is the component size in bits and ttt is the name of
889 -- the parent packed type.
890
891 else
892 PAT :=
893 Make_Defining_Identifier (Loc,
894 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
895
896 -- Build an expression for the length of the array in bits.
897 -- This is the product of the length of each of the dimensions
898
899 Len_Expr := Compute_Number_Components (Typ, Typ);
900
901 -- Temporarily attach the length expression to the tree and analyze
902 -- and resolve it, so that we can test its value. We assume that the
903 -- total length fits in type Integer. This expression may involve
904 -- discriminants, so we treat it as a default/per-object expression.
905
906 Set_Parent (Len_Expr, Typ);
907 Preanalyze_Spec_Expression (Len_Expr, Standard_Long_Long_Integer);
908
909 -- Use a modular type if possible. We can do this if we have
910 -- static bounds, and the length is small enough, and the length
911 -- is not zero. We exclude the zero length case because the size
912 -- of things is always at least one, and the zero length object
913 -- would have an anomalous size.
914
915 if Compile_Time_Known_Value (Len_Expr) then
916 Len_Bits := Expr_Value (Len_Expr) * Csize;
917
918 -- Check for size known to be too large
919
920 if Len_Bits >
921 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
922 then
923 if System_Storage_Unit = 8 then
924 Error_Msg_N
925 ("packed array size cannot exceed " &
926 "Integer''Last bytes", Typ);
927 else
928 Error_Msg_N
929 ("packed array size cannot exceed " &
930 "Integer''Last storage units", Typ);
931 end if;
932
933 -- Reset length to arbitrary not too high value to continue
934
935 Len_Expr := Make_Integer_Literal (Loc, 65535);
936 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
937 end if;
938
939 -- We normally consider small enough to mean no larger than the
940 -- value of System_Max_Binary_Modulus_Power, checking that in the
941 -- case of values longer than word size, we have long shifts.
942
943 if Len_Bits > 0
944 and then
945 (Len_Bits <= System_Word_Size
946 or else (Len_Bits <= System_Max_Binary_Modulus_Power
947 and then Support_Long_Shifts_On_Target))
948 then
949 -- We can use the modular type, it has the form:
950
951 -- subtype tttPn is btyp
952 -- range 0 .. 2 ** ((Typ'Length (1)
953 -- * ... * Typ'Length (n)) * Csize) - 1;
954
955 -- The bounds are statically known, and btyp is one of the
956 -- unsigned types, depending on the length.
957
958 Btyp := Small_Integer_Type_For (Len_Bits, Uns => True);
959 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
960 Set_Print_In_Hex (Lit);
961
962 Decl :=
963 Make_Subtype_Declaration (Loc,
964 Defining_Identifier => PAT,
965 Subtype_Indication =>
966 Make_Subtype_Indication (Loc,
967 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
968
969 Constraint =>
970 Make_Range_Constraint (Loc,
971 Range_Expression =>
972 Make_Range (Loc,
973 Low_Bound =>
974 Make_Integer_Literal (Loc, 0),
975 High_Bound => Lit))));
976
977 if Present (PASize) then
978 PASize := Len_Bits;
979 end if;
980
981 Install_PAT;
982
983 -- Propagate a given alignment to the modular type. This can
984 -- cause it to be under-aligned, but that's OK.
985
986 if Present (Alignment_Clause (Typ)) then
987 Set_Alignment (PAT, Alignment (Typ));
988 end if;
989
990 return;
991 end if;
992 end if;
993
994 -- Could not use a modular type, for all other cases, we build
995 -- a packed array subtype:
996
997 -- subtype tttPn is
998 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
999
1000 -- Bits is the length of the array in bits
1001
1002 Set_PB_Type;
1003
1004 Bits_U1 :=
1005 Make_Op_Add (Loc,
1006 Left_Opnd =>
1007 Make_Op_Multiply (Loc,
1008 Left_Opnd =>
1009 Make_Integer_Literal (Loc, Csize),
1010 Right_Opnd => Len_Expr),
1011
1012 Right_Opnd =>
1013 Make_Integer_Literal (Loc, 7));
1014
1015 Set_Paren_Count (Bits_U1, 1);
1016
1017 PAT_High :=
1018 Make_Op_Subtract (Loc,
1019 Left_Opnd =>
1020 Make_Op_Divide (Loc,
1021 Left_Opnd => Bits_U1,
1022 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1023 Right_Opnd => Make_Integer_Literal (Loc, 1));
1024
1025 Decl :=
1026 Make_Subtype_Declaration (Loc,
1027 Defining_Identifier => PAT,
1028 Subtype_Indication =>
1029 Make_Subtype_Indication (Loc,
1030 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1031 Constraint =>
1032 Make_Index_Or_Discriminant_Constraint (Loc,
1033 Constraints => New_List (
1034 Make_Range (Loc,
1035 Low_Bound =>
1036 Make_Integer_Literal (Loc, 0),
1037 High_Bound =>
1038 Convert_To (Standard_Integer, PAT_High))))));
1039
1040 Install_PAT;
1041
1042 -- Currently the code in this unit requires that packed arrays
1043 -- represented by non-modular arrays of bytes be on a byte
1044 -- boundary for bit sizes handled by System.Pack_nn units.
1045 -- That's because these units assume the array being accessed
1046 -- starts on a byte boundary.
1047
1048 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1049 Set_Must_Be_On_Byte_Boundary (Typ);
1050 end if;
1051 end if;
1052 end Create_Packed_Array_Impl_Type;
1053
1054 -----------------------------------
1055 -- Expand_Bit_Packed_Element_Set --
1056 -----------------------------------
1057
1058 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1059 Loc : constant Source_Ptr := Sloc (N);
1060 Lhs : constant Node_Id := Name (N);
1061
1062 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1063 -- Used to preserve assignment OK status when assignment is rewritten
1064
1065 Expr : Node_Id;
1066
1067 Rhs : Node_Id := Expression (N);
1068 -- Initially Rhs is the right hand side value, it will be replaced
1069 -- later by an appropriate unchecked conversion for the assignment.
1070
1071 Obj : Node_Id;
1072 Atyp : Entity_Id;
1073 PAT : Entity_Id;
1074 Cmask : Uint;
1075
1076 Arr_Typ : constant Entity_Id := Etype (Prefix (Lhs));
1077 Ctyp : constant Entity_Id := Component_Type (Arr_Typ);
1078 Csiz : constant Int := UI_To_Int (Component_Size (Arr_Typ));
1079
1080 Shift : Node_Id;
1081 -- The expression for the shift value that is required
1082
1083 Shift_Used : Boolean := False;
1084 -- Set True if Shift has been used in the generated code at least once,
1085 -- so that it must be duplicated if used again.
1086
1087 New_Lhs : Node_Id;
1088 New_Rhs : Node_Id;
1089
1090 Rhs_Val : Uint;
1091 -- If the value of the right hand side as an integer constant is
1092 -- known at compile time, Rhs_Val contains the value.
1093
1094 function Get_Shift return Node_Id;
1095 -- Function used to get the value of Shift, making sure that it
1096 -- gets duplicated if the function is called more than once.
1097
1098 ---------------
1099 -- Get_Shift --
1100 ---------------
1101
1102 function Get_Shift return Node_Id is
1103 begin
1104 -- If we used the shift value already, then duplicate it. We
1105 -- set a temporary parent in case actions have to be inserted.
1106
1107 if Shift_Used then
1108 Set_Parent (Shift, N);
1109 return Duplicate_Subexpr_No_Checks (Shift);
1110
1111 -- If first time, use Shift unchanged, and set flag for first use
1112
1113 else
1114 Shift_Used := True;
1115 return Shift;
1116 end if;
1117 end Get_Shift;
1118
1119 -- Start of processing for Expand_Bit_Packed_Element_Set
1120
1121 begin
1122 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1123
1124 if CodePeer_Mode then
1125 return;
1126 end if;
1127
1128 Obj := Relocate_Node (Prefix (Lhs));
1129 Convert_To_Actual_Subtype (Obj);
1130 Atyp := Etype (Obj);
1131 PAT := Packed_Array_Impl_Type (Atyp);
1132
1133 -- We remove side effects, in case the rhs modifies the lhs, because we
1134 -- are about to transform the rhs into an expression that first READS
1135 -- the lhs, so we can do the necessary shifting and masking. Example:
1136 -- "X(2) := F(...);" where F modifies X(3). Otherwise, the side effect
1137 -- will be lost.
1138
1139 Remove_Side_Effects (Rhs);
1140
1141 -- We convert the right hand side to the proper subtype to ensure
1142 -- that an appropriate range check is made (since the normal range
1143 -- check from assignment will be lost in the transformations). This
1144 -- conversion is analyzed immediately so that subsequent processing
1145 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1146
1147 -- If the right-hand side is a string literal, create a temporary for
1148 -- it, constant-folding is not ready to wrap the bit representation
1149 -- of a string literal.
1150
1151 if Nkind (Rhs) = N_String_Literal then
1152 declare
1153 Decl : constant Node_Id :=
1154 Make_Object_Declaration (Loc,
1155 Defining_Identifier => Make_Temporary (Loc, 'T', Rhs),
1156 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1157 Expression => New_Copy_Tree (Rhs));
1158 begin
1159 Insert_Actions (N, New_List (Decl));
1160 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1161 end;
1162 else
1163 Rhs := Convert_To (Ctyp, Rhs);
1164 end if;
1165
1166 Set_Parent (Rhs, N);
1167
1168 -- If we are building the initialization procedure for a packed array,
1169 -- and Initialize_Scalars is enabled, each component assignment is an
1170 -- out-of-range value by design. Compile this value without checks,
1171 -- because a call to the array init_proc must not raise an exception.
1172
1173 -- Condition is not consistent with description above, Within_Init_Proc
1174 -- is True also when we are building the IP for a record or protected
1175 -- type that has a packed array component???
1176
1177 if Within_Init_Proc
1178 and then Initialize_Scalars
1179 then
1180 Analyze_And_Resolve (Rhs, Ctyp, Suppress => All_Checks);
1181 else
1182 Analyze_And_Resolve (Rhs, Ctyp);
1183 end if;
1184
1185 -- If any of the indices has a nonstandard representation, introduce
1186 -- the proper Rep_To_Pos conversion, which in turn will generate index
1187 -- checks when needed. We do this on a copy of the index expression,
1188 -- rather that rewriting the LHS altogether.
1189
1190 Expr := First (Expressions (Lhs));
1191 while Present (Expr) loop
1192 declare
1193 Expr_Typ : constant Entity_Id := Etype (Expr);
1194 Loc : constant Source_Ptr := Sloc (Expr);
1195
1196 Expr_Copy : Node_Id;
1197
1198 begin
1199 if Is_Enumeration_Type (Expr_Typ)
1200 and then Has_Non_Standard_Rep (Expr_Typ)
1201 then
1202 Expr_Copy :=
1203 Make_Attribute_Reference (Loc,
1204 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
1205 Attribute_Name => Name_Pos,
1206 Expressions => New_List (Relocate_Node (Expr)));
1207 Set_Parent (Expr_Copy, N);
1208 Analyze_And_Resolve (Expr_Copy, Standard_Natural);
1209 end if;
1210 end;
1211
1212 Next (Expr);
1213 end loop;
1214
1215 -- Case of component size 1,2,4 or any component size for the modular
1216 -- case. These are the cases for which we can inline the code.
1217
1218 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1219 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1220 then
1221 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1222
1223 -- The statement to be generated is:
1224
1225 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, Shift)))
1226
1227 -- or in the case of a freestanding Reverse_Storage_Order object,
1228
1229 -- Obj := Swap (atyp!((Swap (Obj) and Mask1)
1230 -- or (shift_left (rhs, Shift))))
1231
1232 -- where Mask1 is obtained by shifting Cmask left Shift bits
1233 -- and then complementing the result.
1234
1235 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1236
1237 -- the "or ..." is omitted if rhs is constant and all 0 bits
1238
1239 -- rhs is converted to the appropriate type
1240
1241 -- The result is converted back to the array type, since
1242 -- otherwise we lose knowledge of the packed nature.
1243
1244 -- Determine if right side is all 0 bits or all 1 bits
1245
1246 if Compile_Time_Known_Value (Rhs) then
1247 Rhs_Val := Expr_Rep_Value (Rhs);
1248
1249 -- The following test catches the case of an unchecked conversion of
1250 -- an integer literal. This results from optimizing aggregates of
1251 -- packed types.
1252
1253 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1254 and then Compile_Time_Known_Value (Expression (Rhs))
1255 then
1256 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1257
1258 else
1259 Rhs_Val := No_Uint;
1260 end if;
1261
1262 -- Some special checks for the case where the right hand value is
1263 -- known at compile time. Basically we have to take care of the
1264 -- implicit conversion to the subtype of the component object.
1265
1266 if Present (Rhs_Val) then
1267
1268 -- If we have a biased component type then we must manually do the
1269 -- biasing, since we are taking responsibility in this case for
1270 -- constructing the exact bit pattern to be used.
1271
1272 if Has_Biased_Representation (Ctyp) then
1273 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1274 end if;
1275
1276 -- For a negative value, we manually convert the two's complement
1277 -- value to a corresponding unsigned value, so that the proper
1278 -- field width is maintained. If we did not do this, we would
1279 -- get too many leading sign bits later on.
1280
1281 if Rhs_Val < 0 then
1282 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1283 end if;
1284 end if;
1285
1286 -- Now create copies removing side effects. Note that in some complex
1287 -- cases, this may cause the fact that we have already set a packed
1288 -- array type on Obj to get lost. So we save the type of Obj, and
1289 -- make sure it is reset properly.
1290
1291 declare
1292 T : constant Entity_Id := Etype (Obj);
1293 begin
1294 New_Lhs := Duplicate_Subexpr (Obj, Name_Req => True);
1295 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1296 Set_Etype (Obj, T);
1297 Set_Etype (New_Lhs, T);
1298 Set_Etype (New_Rhs, T);
1299 end;
1300
1301 -- First we deal with the "and"
1302
1303 if No (Rhs_Val) or else Rhs_Val /= Cmask then
1304 declare
1305 Mask1 : Node_Id;
1306 Lit : Node_Id;
1307
1308 begin
1309 if Compile_Time_Known_Value (Shift) then
1310 Mask1 :=
1311 Make_Integer_Literal (Loc,
1312 Modulus (Etype (Obj)) - 1 -
1313 (Cmask * (2 ** Expr_Value (Get_Shift))));
1314 Set_Print_In_Hex (Mask1);
1315
1316 else
1317 Lit := Make_Integer_Literal (Loc, Cmask);
1318 Set_Print_In_Hex (Lit);
1319 Mask1 :=
1320 Make_Op_Not (Loc,
1321 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1322 end if;
1323
1324 New_Rhs :=
1325 Make_Op_And (Loc,
1326 Left_Opnd => New_Rhs,
1327 Right_Opnd => Mask1);
1328 end;
1329 end if;
1330
1331 -- Then deal with the "or"
1332
1333 if No (Rhs_Val) or else Rhs_Val /= 0 then
1334 declare
1335 Or_Rhs : Node_Id;
1336
1337 procedure Fixup_Rhs;
1338 -- Adjust Rhs by bias if biased representation for components
1339 -- or remove extraneous high order sign bits if signed.
1340
1341 procedure Fixup_Rhs is
1342 Etyp : constant Entity_Id := Etype (Rhs);
1343
1344 begin
1345 -- For biased case, do the required biasing by simply
1346 -- converting to the biased subtype (the conversion
1347 -- will generate the required bias).
1348
1349 if Has_Biased_Representation (Ctyp) then
1350 Rhs := Convert_To (Ctyp, Rhs);
1351
1352 -- For a signed integer type that is not biased, generate
1353 -- a conversion to unsigned to strip high order sign bits.
1354
1355 elsif Is_Signed_Integer_Type (Ctyp) then
1356 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1357 end if;
1358
1359 -- Set Etype, since it can be referenced before the node is
1360 -- completely analyzed.
1361
1362 Set_Etype (Rhs, Etyp);
1363
1364 -- We now need to do an unchecked conversion of the
1365 -- result to the target type, but it is important that
1366 -- this conversion be a right justified conversion and
1367 -- not a left justified conversion.
1368
1369 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1370 end Fixup_Rhs;
1371
1372 begin
1373 if Present (Rhs_Val)
1374 and then Compile_Time_Known_Value (Get_Shift)
1375 then
1376 Or_Rhs :=
1377 Make_Integer_Literal (Loc,
1378 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1379 Set_Print_In_Hex (Or_Rhs);
1380
1381 else
1382 -- We have to convert the right hand side to Etype (Obj).
1383 -- A special case arises if what we have now is a Val
1384 -- attribute reference whose expression type is Etype (Obj).
1385 -- This happens for assignments of fields from the same
1386 -- array. In this case we get the required right hand side
1387 -- by simply removing the inner attribute reference.
1388
1389 if Nkind (Rhs) = N_Attribute_Reference
1390 and then Attribute_Name (Rhs) = Name_Val
1391 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1392 then
1393 Rhs := Relocate_Node (First (Expressions (Rhs)));
1394 Fixup_Rhs;
1395
1396 -- If the value of the right hand side is a known integer
1397 -- value, then just replace it by an untyped constant,
1398 -- which will be properly retyped when we analyze and
1399 -- resolve the expression.
1400
1401 elsif Present (Rhs_Val) then
1402
1403 -- Note that Rhs_Val has already been normalized to
1404 -- be an unsigned value with the proper number of bits.
1405
1406 Rhs := Make_Integer_Literal (Loc, Rhs_Val);
1407
1408 -- Otherwise we need an unchecked conversion
1409
1410 else
1411 Fixup_Rhs;
1412 end if;
1413
1414 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1415 end if;
1416
1417 if Nkind (New_Rhs) = N_Op_And then
1418 Set_Paren_Count (New_Rhs, 1);
1419 Set_Etype (New_Rhs, Etype (Left_Opnd (New_Rhs)));
1420 end if;
1421
1422 New_Rhs :=
1423 Make_Op_Or (Loc,
1424 Left_Opnd => New_Rhs,
1425 Right_Opnd => Or_Rhs);
1426 end;
1427 end if;
1428
1429 -- Now do the rewrite
1430
1431 Rewrite (N,
1432 Make_Assignment_Statement (Loc,
1433 Name => New_Lhs,
1434 Expression =>
1435 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1436 Set_Assignment_OK (Name (N), Ass_OK);
1437
1438 -- All other component sizes for non-modular case
1439
1440 else
1441 -- We generate
1442
1443 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1444
1445 -- where Subscr is the computed linear subscript
1446
1447 declare
1448 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1449 Set_nn : Entity_Id;
1450 Subscr : Node_Id;
1451 Rev_SSO : Node_Id;
1452
1453 begin
1454 if No (Bits_nn) then
1455
1456 -- Error, most likely High_Integrity_Mode restriction
1457
1458 return;
1459 end if;
1460
1461 -- Acquire proper Set entity. We use the aligned or unaligned
1462 -- case as appropriate.
1463
1464 if Known_Aligned_Enough (Obj, Csiz) then
1465 Set_nn := RTE (Set_Id (Csiz));
1466 else
1467 Set_nn := RTE (SetU_Id (Csiz));
1468 end if;
1469
1470 -- Now generate the set reference
1471
1472 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1473
1474 -- Set indication of whether the packed array has reverse SSO
1475
1476 Rev_SSO :=
1477 New_Occurrence_Of
1478 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1479
1480 -- Below we must make the assumption that Obj is
1481 -- at least byte aligned, since otherwise its address
1482 -- cannot be taken. The assumption holds since the
1483 -- only arrays that can be misaligned are small packed
1484 -- arrays which are implemented as a modular type, and
1485 -- that is not the case here.
1486
1487 Rewrite (N,
1488 Make_Procedure_Call_Statement (Loc,
1489 Name => New_Occurrence_Of (Set_nn, Loc),
1490 Parameter_Associations => New_List (
1491 Make_Attribute_Reference (Loc,
1492 Prefix => Obj,
1493 Attribute_Name => Name_Address),
1494 Subscr,
1495 Unchecked_Convert_To (Bits_nn, Convert_To (Ctyp, Rhs)),
1496 Rev_SSO)));
1497
1498 end;
1499 end if;
1500
1501 Analyze (N, Suppress => All_Checks);
1502 end Expand_Bit_Packed_Element_Set;
1503
1504 -------------------------------------
1505 -- Expand_Packed_Address_Reference --
1506 -------------------------------------
1507
1508 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1509 Loc : constant Source_Ptr := Sloc (N);
1510 Base : Node_Id;
1511 Offset : Node_Id;
1512
1513 begin
1514 if CodePeer_Mode then
1515 return;
1516 end if;
1517
1518 -- We build an expression that has the form
1519
1520 -- outer_object'Address
1521 -- + (linear-subscript * component_size for each array reference
1522 -- + field'Bit_Position for each record field
1523 -- + ...
1524 -- + ...) / Storage_Unit;
1525
1526 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1527
1528 Rewrite (N,
1529 Unchecked_Convert_To (RTE (RE_Address),
1530 Make_Op_Add (Loc,
1531 Left_Opnd =>
1532 Unchecked_Convert_To (RTE (RE_Integer_Address),
1533 Make_Attribute_Reference (Loc,
1534 Prefix => Base,
1535 Attribute_Name => Name_Address)),
1536
1537 Right_Opnd =>
1538 Unchecked_Convert_To (RTE (RE_Integer_Address),
1539 Make_Op_Divide (Loc,
1540 Left_Opnd => Offset,
1541 Right_Opnd =>
1542 Make_Integer_Literal (Loc, System_Storage_Unit))))));
1543
1544 Analyze_And_Resolve (N, RTE (RE_Address));
1545 end Expand_Packed_Address_Reference;
1546
1547 ---------------------------------
1548 -- Expand_Packed_Bit_Reference --
1549 ---------------------------------
1550
1551 procedure Expand_Packed_Bit_Reference (N : Node_Id) is
1552 Loc : constant Source_Ptr := Sloc (N);
1553 Base : Node_Id;
1554 Offset : Node_Id;
1555
1556 begin
1557 if CodePeer_Mode then
1558 return;
1559 end if;
1560
1561 -- We build an expression that has the form
1562
1563 -- (linear-subscript * component_size for each array reference
1564 -- + field'Bit_Position for each record field
1565 -- + ...
1566 -- + ...) mod Storage_Unit;
1567
1568 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1569
1570 Rewrite (N,
1571 Unchecked_Convert_To (Standard_Natural,
1572 Make_Op_Mod (Loc,
1573 Left_Opnd => Offset,
1574 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
1575
1576 Analyze_And_Resolve (N, Standard_Natural);
1577 end Expand_Packed_Bit_Reference;
1578
1579 ------------------------------------
1580 -- Expand_Packed_Boolean_Operator --
1581 ------------------------------------
1582
1583 -- This routine expands "a op b" for the packed cases
1584
1585 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1586 Loc : constant Source_Ptr := Sloc (N);
1587 Typ : constant Entity_Id := Etype (N);
1588 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1589 R : Node_Id := Relocate_Node (Right_Opnd (N));
1590
1591 Ltyp : Entity_Id;
1592 Rtyp : Entity_Id;
1593 PAT : Entity_Id;
1594
1595 begin
1596 if CodePeer_Mode then
1597 return;
1598 end if;
1599
1600 Convert_To_Actual_Subtype (L);
1601 Convert_To_Actual_Subtype (R);
1602
1603 Ensure_Defined (Etype (L), N);
1604 Ensure_Defined (Etype (R), N);
1605
1606 Apply_Length_Check (R, Etype (L));
1607
1608 Ltyp := Etype (L);
1609 Rtyp := Etype (R);
1610
1611 -- Deal with silly case of XOR where the subcomponent has a range
1612 -- True .. True where an exception must be raised.
1613
1614 if Nkind (N) = N_Op_Xor then
1615 R := Duplicate_Subexpr (R);
1616 Silly_Boolean_Array_Xor_Test (N, R, Rtyp);
1617 end if;
1618
1619 -- Now that silliness is taken care of, get packed array type
1620
1621 Convert_To_PAT_Type (L);
1622 Convert_To_PAT_Type (R);
1623
1624 PAT := Etype (L);
1625
1626 -- For the modular case, we expand a op b into
1627
1628 -- rtyp!(pat!(a) op pat!(b))
1629
1630 -- where rtyp is the Etype of the left operand. Note that we do not
1631 -- convert to the base type, since this would be unconstrained, and
1632 -- hence not have a corresponding packed array type set.
1633
1634 -- Note that both operands must be modular for this code to be used
1635
1636 if Is_Modular_Integer_Type (PAT)
1637 and then
1638 Is_Modular_Integer_Type (Etype (R))
1639 then
1640 declare
1641 P : Node_Id;
1642
1643 begin
1644 if Nkind (N) = N_Op_And then
1645 P := Make_Op_And (Loc, L, R);
1646
1647 elsif Nkind (N) = N_Op_Or then
1648 P := Make_Op_Or (Loc, L, R);
1649
1650 else -- Nkind (N) = N_Op_Xor
1651 P := Make_Op_Xor (Loc, L, R);
1652 end if;
1653
1654 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1655 end;
1656
1657 -- For the array case, we insert the actions
1658
1659 -- Result : Ltype;
1660
1661 -- System.Bit_Ops.Bit_And/Or/Xor
1662 -- (Left'Address,
1663 -- Ltype'Length * Ltype'Component_Size;
1664 -- Right'Address,
1665 -- Rtype'Length * Rtype'Component_Size
1666 -- Result'Address);
1667
1668 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1669 -- the second argument and fourth arguments are the lengths of the
1670 -- operands in bits. Then we replace the expression by a reference
1671 -- to Result.
1672
1673 -- Note that if we are mixing a modular and array operand, everything
1674 -- works fine, since we ensure that the modular representation has the
1675 -- same physical layout as the array representation (that's what the
1676 -- left justified modular stuff in the big-endian case is about).
1677
1678 else
1679 declare
1680 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
1681 E_Id : RE_Id;
1682
1683 begin
1684 if Nkind (N) = N_Op_And then
1685 E_Id := RE_Bit_And;
1686
1687 elsif Nkind (N) = N_Op_Or then
1688 E_Id := RE_Bit_Or;
1689
1690 else -- Nkind (N) = N_Op_Xor
1691 E_Id := RE_Bit_Xor;
1692 end if;
1693
1694 Insert_Actions (N, New_List (
1695
1696 Make_Object_Declaration (Loc,
1697 Defining_Identifier => Result_Ent,
1698 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1699
1700 Make_Procedure_Call_Statement (Loc,
1701 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1702 Parameter_Associations => New_List (
1703
1704 Make_Byte_Aligned_Attribute_Reference (Loc,
1705 Prefix => L,
1706 Attribute_Name => Name_Address),
1707
1708 Make_Op_Multiply (Loc,
1709 Left_Opnd =>
1710 Make_Attribute_Reference (Loc,
1711 Prefix =>
1712 New_Occurrence_Of
1713 (Etype (First_Index (Ltyp)), Loc),
1714 Attribute_Name => Name_Range_Length),
1715
1716 Right_Opnd =>
1717 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1718
1719 Make_Byte_Aligned_Attribute_Reference (Loc,
1720 Prefix => R,
1721 Attribute_Name => Name_Address),
1722
1723 Make_Op_Multiply (Loc,
1724 Left_Opnd =>
1725 Make_Attribute_Reference (Loc,
1726 Prefix =>
1727 New_Occurrence_Of
1728 (Etype (First_Index (Rtyp)), Loc),
1729 Attribute_Name => Name_Range_Length),
1730
1731 Right_Opnd =>
1732 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1733
1734 Make_Byte_Aligned_Attribute_Reference (Loc,
1735 Prefix => New_Occurrence_Of (Result_Ent, Loc),
1736 Attribute_Name => Name_Address)))));
1737
1738 Rewrite (N,
1739 New_Occurrence_Of (Result_Ent, Loc));
1740 end;
1741 end if;
1742
1743 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1744 end Expand_Packed_Boolean_Operator;
1745
1746 -------------------------------------
1747 -- Expand_Packed_Element_Reference --
1748 -------------------------------------
1749
1750 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1751 Loc : constant Source_Ptr := Sloc (N);
1752 Obj : Node_Id;
1753 Atyp : Entity_Id;
1754 PAT : Entity_Id;
1755 Ctyp : Entity_Id;
1756 Csiz : Int;
1757 Shift : Node_Id;
1758 Cmask : Uint;
1759 Lit : Node_Id;
1760 Arg : Node_Id;
1761
1762 begin
1763 if CodePeer_Mode then
1764 return;
1765 end if;
1766
1767 -- If the node is an actual in a call, the prefix has not been fully
1768 -- expanded, to account for the additional expansion for in-out actuals
1769 -- (see expand_actuals for details). If the prefix itself is a packed
1770 -- reference as well, we have to recurse to complete the transformation
1771 -- of the prefix.
1772
1773 if Nkind (Prefix (N)) = N_Indexed_Component
1774 and then not Analyzed (Prefix (N))
1775 and then Is_Bit_Packed_Array (Etype (Prefix (Prefix (N))))
1776 then
1777 Expand_Packed_Element_Reference (Prefix (N));
1778 end if;
1779
1780 -- The prefix may be rewritten below as a conversion. If it is a source
1781 -- entity generate reference to it now, to prevent spurious warnings
1782 -- about unused entities.
1783
1784 if Is_Entity_Name (Prefix (N))
1785 and then Comes_From_Source (Prefix (N))
1786 then
1787 Generate_Reference (Entity (Prefix (N)), Prefix (N), 'r');
1788 end if;
1789
1790 -- If not bit packed, we have the enumeration case, which is easily
1791 -- dealt with (just adjust the subscripts of the indexed component)
1792
1793 -- Note: this leaves the result as an indexed component, which is
1794 -- still a variable, so can be used in the assignment case, as is
1795 -- required in the enumeration case.
1796
1797 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1798 Setup_Enumeration_Packed_Array_Reference (N);
1799 return;
1800 end if;
1801
1802 -- Remaining processing is for the bit-packed case
1803
1804 Obj := Relocate_Node (Prefix (N));
1805 Convert_To_Actual_Subtype (Obj);
1806 Atyp := Etype (Obj);
1807 PAT := Packed_Array_Impl_Type (Atyp);
1808 Ctyp := Component_Type (Atyp);
1809 Csiz := UI_To_Int (Component_Size (Atyp));
1810
1811 -- Case of component size 1,2,4 or any component size for the modular
1812 -- case. These are the cases for which we can inline the code.
1813
1814 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1815 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1816 then
1817 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1818 Lit := Make_Integer_Literal (Loc, Cmask);
1819 Set_Print_In_Hex (Lit);
1820
1821 -- We generate a shift right to position the field, followed by a
1822 -- masking operation to extract the bit field, and we finally do an
1823 -- unchecked conversion to convert the result to the required target.
1824
1825 -- Note that the unchecked conversion automatically deals with the
1826 -- bias if we are dealing with a biased representation. What will
1827 -- happen is that we temporarily generate the biased representation,
1828 -- but almost immediately that will be converted to the original
1829 -- unbiased component type, and the bias will disappear.
1830
1831 Arg :=
1832 Make_Op_And (Loc,
1833 Left_Opnd => Make_Shift_Right (Obj, Shift),
1834 Right_Opnd => Lit);
1835 Set_Etype (Arg, Ctyp);
1836
1837 -- Component extraction is performed on a native endianness scalar
1838 -- value: if Atyp has reverse storage order, then it has been byte
1839 -- swapped, and if the component being extracted is itself of a
1840 -- composite type with reverse storage order, then we need to swap
1841 -- it back to its expected endianness after extraction.
1842
1843 if Reverse_Storage_Order (Atyp)
1844 and then (Is_Record_Type (Ctyp) or else Is_Array_Type (Ctyp))
1845 and then Reverse_Storage_Order (Ctyp)
1846 then
1847 Arg := Revert_Storage_Order (Arg);
1848 end if;
1849
1850 -- We needed to analyze this before we do the unchecked convert
1851 -- below, but we need it temporarily attached to the tree for
1852 -- this analysis (hence the temporary Set_Parent call).
1853
1854 Set_Parent (Arg, Parent (N));
1855 Analyze_And_Resolve (Arg);
1856
1857 Rewrite (N, RJ_Unchecked_Convert_To (Ctyp, Arg));
1858
1859 -- All other component sizes for non-modular case
1860
1861 else
1862 -- We generate
1863
1864 -- Component_Type!(Get_nn (Arr'address, Subscr))
1865
1866 -- where Subscr is the computed linear subscript
1867
1868 declare
1869 Get_nn : Entity_Id;
1870 Subscr : Node_Id;
1871 Rev_SSO : constant Node_Id :=
1872 New_Occurrence_Of
1873 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1874
1875 begin
1876 -- Acquire proper Get entity. We use the aligned or unaligned
1877 -- case as appropriate.
1878
1879 if Known_Aligned_Enough (Obj, Csiz) then
1880 Get_nn := RTE (Get_Id (Csiz));
1881 else
1882 Get_nn := RTE (GetU_Id (Csiz));
1883 end if;
1884
1885 -- Now generate the get reference
1886
1887 Compute_Linear_Subscript (Atyp, N, Subscr);
1888
1889 -- Below we make the assumption that Obj is at least byte
1890 -- aligned, since otherwise its address cannot be taken.
1891 -- The assumption holds since the only arrays that can be
1892 -- misaligned are small packed arrays which are implemented
1893 -- as a modular type, and that is not the case here.
1894
1895 Rewrite (N,
1896 Unchecked_Convert_To (Ctyp,
1897 Make_Function_Call (Loc,
1898 Name => New_Occurrence_Of (Get_nn, Loc),
1899 Parameter_Associations => New_List (
1900 Make_Attribute_Reference (Loc,
1901 Prefix => Obj,
1902 Attribute_Name => Name_Address),
1903 Subscr,
1904 Rev_SSO))));
1905 end;
1906 end if;
1907
1908 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
1909 end Expand_Packed_Element_Reference;
1910
1911 ----------------------
1912 -- Expand_Packed_Eq --
1913 ----------------------
1914
1915 -- Handles expansion of "=" on packed array types
1916
1917 procedure Expand_Packed_Eq (N : Node_Id) is
1918 Loc : constant Source_Ptr := Sloc (N);
1919 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1920 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1921
1922 LLexpr : Node_Id;
1923 RLexpr : Node_Id;
1924
1925 Ltyp : Entity_Id;
1926 Rtyp : Entity_Id;
1927 PAT : Entity_Id;
1928
1929 begin
1930 if CodePeer_Mode then
1931 return;
1932 end if;
1933
1934 Convert_To_Actual_Subtype (L);
1935 Convert_To_Actual_Subtype (R);
1936 Ltyp := Underlying_Type (Etype (L));
1937 Rtyp := Underlying_Type (Etype (R));
1938
1939 Convert_To_PAT_Type (L);
1940 Convert_To_PAT_Type (R);
1941 PAT := Etype (L);
1942
1943 LLexpr :=
1944 Make_Op_Multiply (Loc,
1945 Left_Opnd => Compute_Number_Components (N, Ltyp),
1946 Right_Opnd => Make_Integer_Literal (Loc, Component_Size (Ltyp)));
1947
1948 RLexpr :=
1949 Make_Op_Multiply (Loc,
1950 Left_Opnd => Compute_Number_Components (N, Rtyp),
1951 Right_Opnd => Make_Integer_Literal (Loc, Component_Size (Rtyp)));
1952
1953 -- For the modular case, we transform the comparison to:
1954
1955 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
1956
1957 -- where PAT is the packed array type. This works fine, since in the
1958 -- modular case we guarantee that the unused bits are always zeroes.
1959 -- We do have to compare the lengths because we could be comparing
1960 -- two different subtypes of the same base type. We can only do this
1961 -- if the PATs on both sides are modular (in which case they are
1962 -- necessarily structurally the same -- same Modulus and so on);
1963 -- otherwise, we have a case where the right operand is not of
1964 -- compile time known size.
1965
1966 if Is_Modular_Integer_Type (PAT)
1967 and then Is_Modular_Integer_Type (Etype (R))
1968 then
1969 pragma Assert (RM_Size (Etype (R)) = RM_Size (PAT));
1970 pragma Assert (Modulus (Etype (R)) = Modulus (PAT));
1971
1972 Rewrite (N,
1973 Make_And_Then (Loc,
1974 Left_Opnd =>
1975 Make_Op_Eq (Loc,
1976 Left_Opnd => LLexpr,
1977 Right_Opnd => RLexpr),
1978
1979 Right_Opnd =>
1980 Make_Op_Eq (Loc,
1981 Left_Opnd => L,
1982 Right_Opnd => R)));
1983
1984 -- For the non-modular case, we call a runtime routine
1985
1986 -- System.Bit_Ops.Bit_Eq
1987 -- (L'Address, L_Length, R'Address, R_Length)
1988
1989 -- where PAT is the packed array type, and the lengths are the lengths
1990 -- in bits of the original packed arrays. This routine takes care of
1991 -- not comparing the unused bits in the last byte.
1992
1993 else
1994 Rewrite (N,
1995 Make_Function_Call (Loc,
1996 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
1997 Parameter_Associations => New_List (
1998 Make_Byte_Aligned_Attribute_Reference (Loc,
1999 Prefix => L,
2000 Attribute_Name => Name_Address),
2001
2002 LLexpr,
2003
2004 Make_Byte_Aligned_Attribute_Reference (Loc,
2005 Prefix => R,
2006 Attribute_Name => Name_Address),
2007
2008 RLexpr)));
2009 end if;
2010
2011 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2012 end Expand_Packed_Eq;
2013
2014 -----------------------
2015 -- Expand_Packed_Not --
2016 -----------------------
2017
2018 -- Handles expansion of "not" on packed array types
2019
2020 procedure Expand_Packed_Not (N : Node_Id) is
2021 Loc : constant Source_Ptr := Sloc (N);
2022 Typ : constant Entity_Id := Etype (N);
2023 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2024
2025 Rtyp : Entity_Id;
2026 PAT : Entity_Id;
2027 Lit : Node_Id;
2028 Size : Unat;
2029
2030 begin
2031 if CodePeer_Mode then
2032 return;
2033 end if;
2034
2035 Convert_To_Actual_Subtype (Opnd);
2036 Rtyp := Etype (Opnd);
2037
2038 -- Deal with silly False..False and True..True subtype case
2039
2040 Silly_Boolean_Array_Not_Test (N, Rtyp);
2041
2042 -- Now that the silliness is taken care of, get packed array type
2043
2044 Convert_To_PAT_Type (Opnd);
2045 PAT := Etype (Opnd);
2046
2047 -- For the case where the packed array type is a modular type, "not A"
2048 -- expands simply into:
2049
2050 -- Rtyp!(PAT!(A) xor Mask)
2051
2052 -- where PAT is the packed array type, Mask is a mask of all 1 bits of
2053 -- length equal to the size of this packed type, and Rtyp is the actual
2054 -- actual subtype of the operand. Preserve old behavior in case size is
2055 -- not set.
2056
2057 if Known_RM_Size (PAT) then
2058 Size := RM_Size (PAT);
2059 else
2060 Size := Uint_0;
2061 end if;
2062 Lit := Make_Integer_Literal (Loc, 2 ** Size - 1);
2063 Set_Print_In_Hex (Lit);
2064
2065 if not Is_Array_Type (PAT) then
2066 Rewrite (N,
2067 Unchecked_Convert_To (Rtyp,
2068 Make_Op_Xor (Loc,
2069 Left_Opnd => Opnd,
2070 Right_Opnd => Lit)));
2071
2072 -- For the array case, we insert the actions
2073
2074 -- Result : Typ;
2075
2076 -- System.Bit_Ops.Bit_Not
2077 -- (Opnd'Address,
2078 -- Typ'Length * Typ'Component_Size,
2079 -- Result'Address);
2080
2081 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second argument
2082 -- is the length of the operand in bits. We then replace the expression
2083 -- with a reference to Result.
2084
2085 else
2086 declare
2087 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
2088
2089 begin
2090 Insert_Actions (N, New_List (
2091 Make_Object_Declaration (Loc,
2092 Defining_Identifier => Result_Ent,
2093 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2094
2095 Make_Procedure_Call_Statement (Loc,
2096 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2097 Parameter_Associations => New_List (
2098 Make_Byte_Aligned_Attribute_Reference (Loc,
2099 Prefix => Opnd,
2100 Attribute_Name => Name_Address),
2101
2102 Make_Op_Multiply (Loc,
2103 Left_Opnd =>
2104 Make_Attribute_Reference (Loc,
2105 Prefix =>
2106 New_Occurrence_Of
2107 (Etype (First_Index (Rtyp)), Loc),
2108 Attribute_Name => Name_Range_Length),
2109
2110 Right_Opnd =>
2111 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2112
2113 Make_Byte_Aligned_Attribute_Reference (Loc,
2114 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2115 Attribute_Name => Name_Address)))));
2116
2117 Rewrite (N, New_Occurrence_Of (Result_Ent, Loc));
2118 end;
2119 end if;
2120
2121 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2122 end Expand_Packed_Not;
2123
2124 -----------------------------
2125 -- Get_Base_And_Bit_Offset --
2126 -----------------------------
2127
2128 procedure Get_Base_And_Bit_Offset
2129 (N : Node_Id;
2130 Base : out Node_Id;
2131 Offset : out Node_Id)
2132 is
2133 Loc : Source_Ptr;
2134 Term : Node_Id;
2135 Atyp : Entity_Id;
2136 Subscr : Node_Id;
2137
2138 begin
2139 Base := N;
2140 Offset := Empty;
2141
2142 -- We build up an expression serially that has the form
2143
2144 -- linear-subscript * component_size for each array component ref
2145 -- + pref.component'Bit_Position for each record component ref
2146 -- + ...
2147
2148 loop
2149 Loc := Sloc (Base);
2150
2151 if Nkind (Base) = N_Indexed_Component then
2152 Convert_To_Actual_Subtype (Prefix (Base));
2153 Atyp := Etype (Prefix (Base));
2154 Compute_Linear_Subscript (Atyp, Base, Subscr);
2155
2156 Term :=
2157 Make_Op_Multiply (Loc,
2158 Left_Opnd => Subscr,
2159 Right_Opnd =>
2160 Make_Attribute_Reference (Loc,
2161 Prefix => New_Occurrence_Of (Atyp, Loc),
2162 Attribute_Name => Name_Component_Size));
2163
2164 elsif Nkind (Base) = N_Selected_Component then
2165 Term :=
2166 Make_Attribute_Reference (Loc,
2167 Prefix => Base,
2168 Attribute_Name => Name_Bit_Position);
2169
2170 else
2171 return;
2172 end if;
2173
2174 if No (Offset) then
2175 Offset := Term;
2176
2177 else
2178 Offset :=
2179 Make_Op_Add (Loc,
2180 Left_Opnd => Offset,
2181 Right_Opnd => Term);
2182 end if;
2183
2184 Base := Prefix (Base);
2185 end loop;
2186 end Get_Base_And_Bit_Offset;
2187
2188 -------------------------------------
2189 -- Involves_Packed_Array_Reference --
2190 -------------------------------------
2191
2192 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2193 begin
2194 if Nkind (N) = N_Indexed_Component
2195 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2196 then
2197 return True;
2198
2199 elsif Nkind (N) = N_Selected_Component then
2200 return Involves_Packed_Array_Reference (Prefix (N));
2201
2202 else
2203 return False;
2204 end if;
2205 end Involves_Packed_Array_Reference;
2206
2207 --------------------------
2208 -- Known_Aligned_Enough --
2209 --------------------------
2210
2211 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2212 Typ : constant Entity_Id := Etype (Obj);
2213
2214 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2215 -- If the component is in a record that contains previous packed
2216 -- components, consider it unaligned because the back-end might
2217 -- choose to pack the rest of the record. Lead to less efficient code,
2218 -- but safer vis-a-vis of back-end choices.
2219
2220 --------------------------------
2221 -- In_Partially_Packed_Record --
2222 --------------------------------
2223
2224 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2225 Rec_Type : constant Entity_Id := Scope (Comp);
2226 Prev_Comp : Entity_Id;
2227
2228 begin
2229 Prev_Comp := First_Entity (Rec_Type);
2230 while Present (Prev_Comp) loop
2231 if Is_Packed (Etype (Prev_Comp)) then
2232 return True;
2233
2234 elsif Prev_Comp = Comp then
2235 return False;
2236 end if;
2237
2238 Next_Entity (Prev_Comp);
2239 end loop;
2240
2241 return False;
2242 end In_Partially_Packed_Record;
2243
2244 -- Start of processing for Known_Aligned_Enough
2245
2246 begin
2247 -- Odd bit sizes don't need alignment anyway
2248
2249 if Csiz mod 2 = 1 then
2250 return True;
2251
2252 -- If we have a specified alignment, see if it is sufficient, if not
2253 -- then we can't possibly be aligned enough in any case.
2254
2255 elsif Known_Alignment (Etype (Obj)) then
2256 -- Alignment required is 4 if size is a multiple of 4, and
2257 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2258
2259 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2260 return False;
2261 end if;
2262 end if;
2263
2264 -- OK, alignment should be sufficient, if object is aligned
2265
2266 -- If object is strictly aligned, then it is definitely aligned
2267
2268 if Strict_Alignment (Typ) then
2269 return True;
2270
2271 -- Case of subscripted array reference
2272
2273 elsif Nkind (Obj) = N_Indexed_Component then
2274
2275 -- If we have a pointer to an array, then this is definitely
2276 -- aligned, because pointers always point to aligned versions.
2277
2278 if Is_Access_Type (Etype (Prefix (Obj))) then
2279 return True;
2280
2281 -- Otherwise, go look at the prefix
2282
2283 else
2284 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2285 end if;
2286
2287 -- Case of record field
2288
2289 elsif Nkind (Obj) = N_Selected_Component then
2290
2291 -- What is significant here is whether the record type is packed
2292
2293 if Is_Record_Type (Etype (Prefix (Obj)))
2294 and then Is_Packed (Etype (Prefix (Obj)))
2295 then
2296 return False;
2297
2298 -- Or the component has a component clause which might cause
2299 -- the component to become unaligned (we can't tell if the
2300 -- backend is doing alignment computations).
2301
2302 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2303 return False;
2304
2305 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2306 return False;
2307
2308 -- In all other cases, go look at prefix
2309
2310 else
2311 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2312 end if;
2313
2314 elsif Nkind (Obj) = N_Type_Conversion then
2315 return Known_Aligned_Enough (Expression (Obj), Csiz);
2316
2317 -- For a formal parameter, it is safer to assume that it is not
2318 -- aligned, because the formal may be unconstrained while the actual
2319 -- is constrained. In this situation, a small constrained packed
2320 -- array, represented in modular form, may be unaligned.
2321
2322 elsif Is_Entity_Name (Obj) then
2323 return not Is_Formal (Entity (Obj));
2324 else
2325
2326 -- If none of the above, must be aligned
2327 return True;
2328 end if;
2329 end Known_Aligned_Enough;
2330
2331 ---------------------
2332 -- Make_Shift_Left --
2333 ---------------------
2334
2335 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2336 Nod : Node_Id;
2337
2338 begin
2339 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2340 return N;
2341 else
2342 Nod :=
2343 Make_Op_Shift_Left (Sloc (N),
2344 Left_Opnd => N,
2345 Right_Opnd => S);
2346 Set_Shift_Count_OK (Nod, True);
2347 return Nod;
2348 end if;
2349 end Make_Shift_Left;
2350
2351 ----------------------
2352 -- Make_Shift_Right --
2353 ----------------------
2354
2355 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2356 Nod : Node_Id;
2357
2358 begin
2359 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2360 return N;
2361 else
2362 Nod :=
2363 Make_Op_Shift_Right (Sloc (N),
2364 Left_Opnd => N,
2365 Right_Opnd => S);
2366 Set_Shift_Count_OK (Nod, True);
2367 return Nod;
2368 end if;
2369 end Make_Shift_Right;
2370
2371 -----------------------------
2372 -- RJ_Unchecked_Convert_To --
2373 -----------------------------
2374
2375 function RJ_Unchecked_Convert_To
2376 (Typ : Entity_Id;
2377 Expr : Node_Id) return Node_Id
2378 is
2379 Source_Typ : constant Entity_Id := Etype (Expr);
2380 Target_Typ : constant Entity_Id := Typ;
2381
2382 Src : Node_Id := Expr;
2383
2384 Source_Siz : Nat;
2385 Target_Siz : Nat;
2386
2387 begin
2388 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2389 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2390
2391 -- For a little-endian target type stored byte-swapped on a
2392 -- big-endian machine, do not mask to Target_Siz bits.
2393
2394 if Bytes_Big_Endian
2395 and then (Is_Record_Type (Target_Typ)
2396 or else
2397 Is_Array_Type (Target_Typ))
2398 and then Reverse_Storage_Order (Target_Typ)
2399 then
2400 Source_Siz := Target_Siz;
2401 end if;
2402
2403 -- First step, if the source type is not a discrete type, then we first
2404 -- convert to a modular type of the source length, since otherwise, on
2405 -- a big-endian machine, we get left-justification. We do it for little-
2406 -- endian machines as well, because there might be junk bits that are
2407 -- not cleared if the type is not numeric. This can be done only if the
2408 -- source siz is different from 0 (i.e. known), otherwise we must trust
2409 -- the type declarations (case of non-discrete components).
2410
2411 if Source_Siz /= 0
2412 and then Source_Siz /= Target_Siz
2413 and then not Is_Discrete_Type (Source_Typ)
2414 then
2415 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2416 end if;
2417
2418 -- In the big endian case, if the lengths of the two types differ, then
2419 -- we must worry about possible left justification in the conversion,
2420 -- and avoiding that is what this is all about.
2421
2422 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2423
2424 -- Next step. If the target is not a discrete type, then we first
2425 -- convert to a modular type of the target length, since otherwise,
2426 -- on a big-endian machine, we get left-justification.
2427
2428 if not Is_Discrete_Type (Target_Typ) then
2429 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2430 end if;
2431 end if;
2432
2433 -- And now we can do the final conversion to the target type
2434
2435 return Unchecked_Convert_To (Target_Typ, Src);
2436 end RJ_Unchecked_Convert_To;
2437
2438 ----------------------------------------------
2439 -- Setup_Enumeration_Packed_Array_Reference --
2440 ----------------------------------------------
2441
2442 -- All we have to do here is to find the subscripts that correspond to the
2443 -- index positions that have non-standard enumeration types and insert a
2444 -- Pos attribute to get the proper subscript value.
2445
2446 -- Finally the prefix must be uncheck-converted to the corresponding packed
2447 -- array type.
2448
2449 -- Note that the component type is unchanged, so we do not need to fiddle
2450 -- with the types (Gigi always automatically takes the packed array type if
2451 -- it is set, as it will be in this case).
2452
2453 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2454 Pfx : constant Node_Id := Prefix (N);
2455 Typ : constant Entity_Id := Etype (N);
2456 Exprs : constant List_Id := Expressions (N);
2457 Expr : Node_Id;
2458
2459 begin
2460 -- If the array is unconstrained, then we replace the array reference
2461 -- with its actual subtype. This actual subtype will have a packed array
2462 -- type with appropriate bounds.
2463
2464 if not Is_Constrained (Packed_Array_Impl_Type (Etype (Pfx))) then
2465 Convert_To_Actual_Subtype (Pfx);
2466 end if;
2467
2468 Expr := First (Exprs);
2469 while Present (Expr) loop
2470 declare
2471 Loc : constant Source_Ptr := Sloc (Expr);
2472 Expr_Typ : constant Entity_Id := Etype (Expr);
2473
2474 begin
2475 if Is_Enumeration_Type (Expr_Typ)
2476 and then Has_Non_Standard_Rep (Expr_Typ)
2477 then
2478 Rewrite (Expr,
2479 Make_Attribute_Reference (Loc,
2480 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2481 Attribute_Name => Name_Pos,
2482 Expressions => New_List (Relocate_Node (Expr))));
2483 Analyze_And_Resolve (Expr, Standard_Natural);
2484 end if;
2485 end;
2486
2487 Next (Expr);
2488 end loop;
2489
2490 Rewrite (N,
2491 Make_Indexed_Component (Sloc (N),
2492 Prefix =>
2493 Unchecked_Convert_To (Packed_Array_Impl_Type (Etype (Pfx)), Pfx),
2494 Expressions => Exprs));
2495
2496 Analyze_And_Resolve (N, Typ);
2497 end Setup_Enumeration_Packed_Array_Reference;
2498
2499 -----------------------------------------
2500 -- Setup_Inline_Packed_Array_Reference --
2501 -----------------------------------------
2502
2503 procedure Setup_Inline_Packed_Array_Reference
2504 (N : Node_Id;
2505 Atyp : Entity_Id;
2506 Obj : in out Node_Id;
2507 Cmask : out Uint;
2508 Shift : out Node_Id)
2509 is
2510 Loc : constant Source_Ptr := Sloc (N);
2511 PAT : Entity_Id;
2512 Otyp : Entity_Id;
2513 Csiz : constant Uint := Component_Size (Atyp);
2514 Osiz : Uint;
2515
2516 begin
2517 Convert_To_PAT_Type (Obj);
2518 PAT := Etype (Obj);
2519
2520 Cmask := 2 ** Csiz - 1;
2521
2522 if Is_Array_Type (PAT) then
2523 Otyp := Component_Type (PAT);
2524 Osiz := Component_Size (PAT);
2525
2526 else
2527 Otyp := PAT;
2528
2529 -- In the case where the PAT is a modular type, we want the actual
2530 -- size in bits of the modular value we use. This is neither the
2531 -- Object_Size nor the Value_Size, either of which may have been
2532 -- reset to strange values, but rather the minimum size. Note that
2533 -- since this is a modular type with full range, the issue of
2534 -- biased representation does not arise.
2535
2536 Osiz := UI_From_Int (Minimum_Size (Otyp));
2537 end if;
2538
2539 Compute_Linear_Subscript (Atyp, N, Shift);
2540
2541 -- If the component size is not 1, then the subscript must be multiplied
2542 -- by the component size to get the shift count.
2543
2544 if Csiz /= 1 then
2545 Shift :=
2546 Make_Op_Multiply (Loc,
2547 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2548 Right_Opnd => Shift);
2549 end if;
2550
2551 -- If we have the array case, then this shift count must be broken down
2552 -- into a byte subscript, and a shift within the byte.
2553
2554 if Is_Array_Type (PAT) then
2555
2556 declare
2557 New_Shift : Node_Id;
2558
2559 begin
2560 -- We must analyze shift, since we will duplicate it
2561
2562 Set_Parent (Shift, N);
2563 Analyze_And_Resolve
2564 (Shift, Standard_Integer, Suppress => All_Checks);
2565
2566 -- The shift count within the word is
2567 -- shift mod Osiz
2568
2569 New_Shift :=
2570 Make_Op_Mod (Loc,
2571 Left_Opnd => Duplicate_Subexpr (Shift),
2572 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2573
2574 -- The subscript to be used on the PAT array is
2575 -- shift / Osiz
2576
2577 Obj :=
2578 Make_Indexed_Component (Loc,
2579 Prefix => Obj,
2580 Expressions => New_List (
2581 Make_Op_Divide (Loc,
2582 Left_Opnd => Duplicate_Subexpr (Shift),
2583 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2584
2585 Shift := New_Shift;
2586 end;
2587
2588 -- For the modular integer case, the object to be manipulated is the
2589 -- entire array, so Obj is unchanged. Note that we will reset its type
2590 -- to PAT before returning to the caller.
2591
2592 else
2593 null;
2594 end if;
2595
2596 -- The one remaining step is to modify the shift count for the
2597 -- big-endian case. Consider the following example in a byte:
2598
2599 -- xxxxxxxx bits of byte
2600 -- vvvvvvvv bits of value
2601 -- 33221100 little-endian numbering
2602 -- 00112233 big-endian numbering
2603
2604 -- Here we have the case of 2-bit fields
2605
2606 -- For the little-endian case, we already have the proper shift count
2607 -- set, e.g. for element 2, the shift count is 2*2 = 4.
2608
2609 -- For the big endian case, we have to adjust the shift count, computing
2610 -- it as (N - F) - Shift, where N is the number of bits in an element of
2611 -- the array used to implement the packed array, F is the number of bits
2612 -- in a source array element, and Shift is the count so far computed.
2613
2614 -- We also have to adjust if the storage order is reversed
2615
2616 if Bytes_Big_Endian xor Reverse_Storage_Order (Base_Type (Atyp)) then
2617 Shift :=
2618 Make_Op_Subtract (Loc,
2619 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2620 Right_Opnd => Shift);
2621 end if;
2622
2623 Set_Parent (Shift, N);
2624 Set_Parent (Obj, N);
2625 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2626 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2627
2628 -- Make sure final type of object is the appropriate packed type
2629
2630 Set_Etype (Obj, Otyp);
2631
2632 end Setup_Inline_Packed_Array_Reference;
2633
2634 end Exp_Pakd;