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