Merge from trunk:
[official-gcc.git] / main / gcc / ada / exp_pakd.adb
blob6ff75278d9705758f1169545d35d8f062f7cd569
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-2014, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Einfo; use Einfo;
29 with Errout; use Errout;
30 with Exp_Dbug; use Exp_Dbug;
31 with Exp_Util; use Exp_Util;
32 with Layout; use Layout;
33 with Namet; use Namet;
34 with Nlists; use Nlists;
35 with Nmake; use Nmake;
36 with Opt; use Opt;
37 with Rtsfind; use Rtsfind;
38 with Sem; use Sem;
39 with Sem_Aux; use Sem_Aux;
40 with Sem_Ch3; use Sem_Ch3;
41 with Sem_Ch8; use Sem_Ch8;
42 with Sem_Ch13; use Sem_Ch13;
43 with Sem_Eval; use Sem_Eval;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sinfo; use Sinfo;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Targparm; use Targparm;
50 with Tbuild; use Tbuild;
51 with Ttypes; use Ttypes;
52 with Uintp; use Uintp;
54 package body Exp_Pakd is
56 ---------------------------
57 -- Endian Considerations --
58 ---------------------------
60 -- As described in the specification, bit numbering in a packed array
61 -- is consistent with bit numbering in a record representation clause,
62 -- and hence dependent on the endianness of the machine:
64 -- For little-endian machines, element zero is at the right hand end
65 -- (low order end) of a bit field.
67 -- For big-endian machines, element zero is at the left hand end
68 -- (high order end) of a bit field.
70 -- The shifts that are used to right justify a field therefore differ in
71 -- the two cases. For the little-endian case, we can simply use the bit
72 -- number (i.e. the element number * element size) as the count for a right
73 -- shift. For the big-endian case, we have to subtract the shift count from
74 -- an appropriate constant to use in the right shift. We use rotates
75 -- instead of shifts (which is necessary in the store case to preserve
76 -- other fields), and we expect that the backend will be able to change the
77 -- right rotate into a left rotate, avoiding the subtract, if the machine
78 -- architecture provides such an instruction.
80 ----------------------------------------------
81 -- Entity Tables for Packed Access Routines --
82 ----------------------------------------------
84 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call library
85 -- routines. This table provides the entity for the proper routine.
87 type E_Array is array (Int range 01 .. 63) of RE_Id;
89 -- Array of Bits_nn entities. Note that we do not use library routines
90 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
91 -- entries from System.Unsigned, because we also use this table for
92 -- certain special unchecked conversions in the big-endian case.
94 Bits_Id : constant E_Array :=
95 (01 => RE_Bits_1,
96 02 => RE_Bits_2,
97 03 => RE_Bits_03,
98 04 => RE_Bits_4,
99 05 => RE_Bits_05,
100 06 => RE_Bits_06,
101 07 => RE_Bits_07,
102 08 => RE_Unsigned_8,
103 09 => RE_Bits_09,
104 10 => RE_Bits_10,
105 11 => RE_Bits_11,
106 12 => RE_Bits_12,
107 13 => RE_Bits_13,
108 14 => RE_Bits_14,
109 15 => RE_Bits_15,
110 16 => RE_Unsigned_16,
111 17 => RE_Bits_17,
112 18 => RE_Bits_18,
113 19 => RE_Bits_19,
114 20 => RE_Bits_20,
115 21 => RE_Bits_21,
116 22 => RE_Bits_22,
117 23 => RE_Bits_23,
118 24 => RE_Bits_24,
119 25 => RE_Bits_25,
120 26 => RE_Bits_26,
121 27 => RE_Bits_27,
122 28 => RE_Bits_28,
123 29 => RE_Bits_29,
124 30 => RE_Bits_30,
125 31 => RE_Bits_31,
126 32 => RE_Unsigned_32,
127 33 => RE_Bits_33,
128 34 => RE_Bits_34,
129 35 => RE_Bits_35,
130 36 => RE_Bits_36,
131 37 => RE_Bits_37,
132 38 => RE_Bits_38,
133 39 => RE_Bits_39,
134 40 => RE_Bits_40,
135 41 => RE_Bits_41,
136 42 => RE_Bits_42,
137 43 => RE_Bits_43,
138 44 => RE_Bits_44,
139 45 => RE_Bits_45,
140 46 => RE_Bits_46,
141 47 => RE_Bits_47,
142 48 => RE_Bits_48,
143 49 => RE_Bits_49,
144 50 => RE_Bits_50,
145 51 => RE_Bits_51,
146 52 => RE_Bits_52,
147 53 => RE_Bits_53,
148 54 => RE_Bits_54,
149 55 => RE_Bits_55,
150 56 => RE_Bits_56,
151 57 => RE_Bits_57,
152 58 => RE_Bits_58,
153 59 => RE_Bits_59,
154 60 => RE_Bits_60,
155 61 => RE_Bits_61,
156 62 => RE_Bits_62,
157 63 => RE_Bits_63);
159 -- Array of Get routine entities. These are used to obtain an element from
160 -- a packed array. The N'th entry is used to obtain elements from a packed
161 -- array whose component size is N. RE_Null is used as a null entry, for
162 -- the cases where a library routine is not used.
164 Get_Id : constant E_Array :=
165 (01 => RE_Null,
166 02 => RE_Null,
167 03 => RE_Get_03,
168 04 => RE_Null,
169 05 => RE_Get_05,
170 06 => RE_Get_06,
171 07 => RE_Get_07,
172 08 => RE_Null,
173 09 => RE_Get_09,
174 10 => RE_Get_10,
175 11 => RE_Get_11,
176 12 => RE_Get_12,
177 13 => RE_Get_13,
178 14 => RE_Get_14,
179 15 => RE_Get_15,
180 16 => RE_Null,
181 17 => RE_Get_17,
182 18 => RE_Get_18,
183 19 => RE_Get_19,
184 20 => RE_Get_20,
185 21 => RE_Get_21,
186 22 => RE_Get_22,
187 23 => RE_Get_23,
188 24 => RE_Get_24,
189 25 => RE_Get_25,
190 26 => RE_Get_26,
191 27 => RE_Get_27,
192 28 => RE_Get_28,
193 29 => RE_Get_29,
194 30 => RE_Get_30,
195 31 => RE_Get_31,
196 32 => RE_Null,
197 33 => RE_Get_33,
198 34 => RE_Get_34,
199 35 => RE_Get_35,
200 36 => RE_Get_36,
201 37 => RE_Get_37,
202 38 => RE_Get_38,
203 39 => RE_Get_39,
204 40 => RE_Get_40,
205 41 => RE_Get_41,
206 42 => RE_Get_42,
207 43 => RE_Get_43,
208 44 => RE_Get_44,
209 45 => RE_Get_45,
210 46 => RE_Get_46,
211 47 => RE_Get_47,
212 48 => RE_Get_48,
213 49 => RE_Get_49,
214 50 => RE_Get_50,
215 51 => RE_Get_51,
216 52 => RE_Get_52,
217 53 => RE_Get_53,
218 54 => RE_Get_54,
219 55 => RE_Get_55,
220 56 => RE_Get_56,
221 57 => RE_Get_57,
222 58 => RE_Get_58,
223 59 => RE_Get_59,
224 60 => RE_Get_60,
225 61 => RE_Get_61,
226 62 => RE_Get_62,
227 63 => RE_Get_63);
229 -- Array of Get routine entities to be used in the case where the packed
230 -- array is itself a component of a packed structure, and therefore may not
231 -- be fully aligned. This only affects the even sizes, since for the odd
232 -- sizes, we do not get any fixed alignment in any case.
234 GetU_Id : constant E_Array :=
235 (01 => RE_Null,
236 02 => RE_Null,
237 03 => RE_Get_03,
238 04 => RE_Null,
239 05 => RE_Get_05,
240 06 => RE_GetU_06,
241 07 => RE_Get_07,
242 08 => RE_Null,
243 09 => RE_Get_09,
244 10 => RE_GetU_10,
245 11 => RE_Get_11,
246 12 => RE_GetU_12,
247 13 => RE_Get_13,
248 14 => RE_GetU_14,
249 15 => RE_Get_15,
250 16 => RE_Null,
251 17 => RE_Get_17,
252 18 => RE_GetU_18,
253 19 => RE_Get_19,
254 20 => RE_GetU_20,
255 21 => RE_Get_21,
256 22 => RE_GetU_22,
257 23 => RE_Get_23,
258 24 => RE_GetU_24,
259 25 => RE_Get_25,
260 26 => RE_GetU_26,
261 27 => RE_Get_27,
262 28 => RE_GetU_28,
263 29 => RE_Get_29,
264 30 => RE_GetU_30,
265 31 => RE_Get_31,
266 32 => RE_Null,
267 33 => RE_Get_33,
268 34 => RE_GetU_34,
269 35 => RE_Get_35,
270 36 => RE_GetU_36,
271 37 => RE_Get_37,
272 38 => RE_GetU_38,
273 39 => RE_Get_39,
274 40 => RE_GetU_40,
275 41 => RE_Get_41,
276 42 => RE_GetU_42,
277 43 => RE_Get_43,
278 44 => RE_GetU_44,
279 45 => RE_Get_45,
280 46 => RE_GetU_46,
281 47 => RE_Get_47,
282 48 => RE_GetU_48,
283 49 => RE_Get_49,
284 50 => RE_GetU_50,
285 51 => RE_Get_51,
286 52 => RE_GetU_52,
287 53 => RE_Get_53,
288 54 => RE_GetU_54,
289 55 => RE_Get_55,
290 56 => RE_GetU_56,
291 57 => RE_Get_57,
292 58 => RE_GetU_58,
293 59 => RE_Get_59,
294 60 => RE_GetU_60,
295 61 => RE_Get_61,
296 62 => RE_GetU_62,
297 63 => RE_Get_63);
299 -- Array of Set routine entities. These are used to assign an element of a
300 -- packed array. The N'th entry is used to assign elements for a packed
301 -- array whose component size is N. RE_Null is used as a null entry, for
302 -- the cases where a library routine is not used.
304 Set_Id : constant E_Array :=
305 (01 => RE_Null,
306 02 => RE_Null,
307 03 => RE_Set_03,
308 04 => RE_Null,
309 05 => RE_Set_05,
310 06 => RE_Set_06,
311 07 => RE_Set_07,
312 08 => RE_Null,
313 09 => RE_Set_09,
314 10 => RE_Set_10,
315 11 => RE_Set_11,
316 12 => RE_Set_12,
317 13 => RE_Set_13,
318 14 => RE_Set_14,
319 15 => RE_Set_15,
320 16 => RE_Null,
321 17 => RE_Set_17,
322 18 => RE_Set_18,
323 19 => RE_Set_19,
324 20 => RE_Set_20,
325 21 => RE_Set_21,
326 22 => RE_Set_22,
327 23 => RE_Set_23,
328 24 => RE_Set_24,
329 25 => RE_Set_25,
330 26 => RE_Set_26,
331 27 => RE_Set_27,
332 28 => RE_Set_28,
333 29 => RE_Set_29,
334 30 => RE_Set_30,
335 31 => RE_Set_31,
336 32 => RE_Null,
337 33 => RE_Set_33,
338 34 => RE_Set_34,
339 35 => RE_Set_35,
340 36 => RE_Set_36,
341 37 => RE_Set_37,
342 38 => RE_Set_38,
343 39 => RE_Set_39,
344 40 => RE_Set_40,
345 41 => RE_Set_41,
346 42 => RE_Set_42,
347 43 => RE_Set_43,
348 44 => RE_Set_44,
349 45 => RE_Set_45,
350 46 => RE_Set_46,
351 47 => RE_Set_47,
352 48 => RE_Set_48,
353 49 => RE_Set_49,
354 50 => RE_Set_50,
355 51 => RE_Set_51,
356 52 => RE_Set_52,
357 53 => RE_Set_53,
358 54 => RE_Set_54,
359 55 => RE_Set_55,
360 56 => RE_Set_56,
361 57 => RE_Set_57,
362 58 => RE_Set_58,
363 59 => RE_Set_59,
364 60 => RE_Set_60,
365 61 => RE_Set_61,
366 62 => RE_Set_62,
367 63 => RE_Set_63);
369 -- Array of Set routine entities to be used in the case where the packed
370 -- array is itself a component of a packed structure, and therefore may not
371 -- be fully aligned. This only affects the even sizes, since for the odd
372 -- sizes, we do not get any fixed alignment in any case.
374 SetU_Id : constant E_Array :=
375 (01 => RE_Null,
376 02 => RE_Null,
377 03 => RE_Set_03,
378 04 => RE_Null,
379 05 => RE_Set_05,
380 06 => RE_SetU_06,
381 07 => RE_Set_07,
382 08 => RE_Null,
383 09 => RE_Set_09,
384 10 => RE_SetU_10,
385 11 => RE_Set_11,
386 12 => RE_SetU_12,
387 13 => RE_Set_13,
388 14 => RE_SetU_14,
389 15 => RE_Set_15,
390 16 => RE_Null,
391 17 => RE_Set_17,
392 18 => RE_SetU_18,
393 19 => RE_Set_19,
394 20 => RE_SetU_20,
395 21 => RE_Set_21,
396 22 => RE_SetU_22,
397 23 => RE_Set_23,
398 24 => RE_SetU_24,
399 25 => RE_Set_25,
400 26 => RE_SetU_26,
401 27 => RE_Set_27,
402 28 => RE_SetU_28,
403 29 => RE_Set_29,
404 30 => RE_SetU_30,
405 31 => RE_Set_31,
406 32 => RE_Null,
407 33 => RE_Set_33,
408 34 => RE_SetU_34,
409 35 => RE_Set_35,
410 36 => RE_SetU_36,
411 37 => RE_Set_37,
412 38 => RE_SetU_38,
413 39 => RE_Set_39,
414 40 => RE_SetU_40,
415 41 => RE_Set_41,
416 42 => RE_SetU_42,
417 43 => RE_Set_43,
418 44 => RE_SetU_44,
419 45 => RE_Set_45,
420 46 => RE_SetU_46,
421 47 => RE_Set_47,
422 48 => RE_SetU_48,
423 49 => RE_Set_49,
424 50 => RE_SetU_50,
425 51 => RE_Set_51,
426 52 => RE_SetU_52,
427 53 => RE_Set_53,
428 54 => RE_SetU_54,
429 55 => RE_Set_55,
430 56 => RE_SetU_56,
431 57 => RE_Set_57,
432 58 => RE_SetU_58,
433 59 => RE_Set_59,
434 60 => RE_SetU_60,
435 61 => RE_Set_61,
436 62 => RE_SetU_62,
437 63 => RE_Set_63);
439 -----------------------
440 -- Local Subprograms --
441 -----------------------
443 procedure Compute_Linear_Subscript
444 (Atyp : Entity_Id;
445 N : Node_Id;
446 Subscr : out Node_Id);
447 -- Given a constrained array type Atyp, and an indexed component node N
448 -- referencing an array object of this type, build an expression of type
449 -- Standard.Integer representing the zero-based linear subscript value.
450 -- This expression includes any required range checks.
452 procedure Convert_To_PAT_Type (Aexp : Node_Id);
453 -- Given an expression of a packed array type, builds a corresponding
454 -- expression whose type is the implementation type used to represent
455 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
457 procedure Get_Base_And_Bit_Offset
458 (N : Node_Id;
459 Base : out Node_Id;
460 Offset : out Node_Id);
461 -- Given a node N for a name which involves a packed array reference,
462 -- return the base object of the reference and build an expression of
463 -- type Standard.Integer representing the zero-based offset in bits
464 -- from Base'Address to the first bit of the reference.
466 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
467 -- There are two versions of the Set routines, the ones used when the
468 -- object is known to be sufficiently well aligned given the number of
469 -- bits, and the ones used when the object is not known to be aligned.
470 -- This routine is used to determine which set to use. Obj is a reference
471 -- to the object, and Csiz is the component size of the packed array.
472 -- True is returned if the alignment of object is known to be sufficient,
473 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
474 -- 2 otherwise.
476 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
477 -- Build a left shift node, checking for the case of a shift count of zero
479 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
480 -- Build a right shift node, checking for the case of a shift count of zero
482 function RJ_Unchecked_Convert_To
483 (Typ : Entity_Id;
484 Expr : Node_Id) return Node_Id;
485 -- The packed array code does unchecked conversions which in some cases
486 -- may involve non-discrete types with differing sizes. The semantics of
487 -- such conversions is potentially endianness dependent, and the effect
488 -- we want here for such a conversion is to do the conversion in size as
489 -- though numeric items are involved, and we extend or truncate on the
490 -- left side. This happens naturally in the little-endian case, but in
491 -- the big endian case we can get left justification, when what we want
492 -- is right justification. This routine does the unchecked conversion in
493 -- a stepwise manner to ensure that it gives the expected result. Hence
494 -- the name (RJ = Right justified). The parameters Typ and Expr are as
495 -- for the case of a normal Unchecked_Convert_To call.
497 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
498 -- This routine is called in the Get and Set case for arrays that are
499 -- packed but not bit-packed, meaning that they have at least one
500 -- subscript that is of an enumeration type with a non-standard
501 -- representation. This routine modifies the given node to properly
502 -- reference the corresponding packed array type.
504 procedure Setup_Inline_Packed_Array_Reference
505 (N : Node_Id;
506 Atyp : Entity_Id;
507 Obj : in out Node_Id;
508 Cmask : out Uint;
509 Shift : out Node_Id);
510 -- This procedure performs common processing on the N_Indexed_Component
511 -- parameter given as N, whose prefix is a reference to a packed array.
512 -- This is used for the get and set when the component size is 1, 2, 4,
513 -- or for other component sizes when the packed array type is a modular
514 -- type (i.e. the cases that are handled with inline code).
516 -- On entry:
518 -- N is the N_Indexed_Component node for the packed array reference
520 -- Atyp is the constrained array type (the actual subtype has been
521 -- computed if necessary to obtain the constraints, but this is still
522 -- the original array type, not the Packed_Array_Impl_Type value).
524 -- Obj is the object which is to be indexed. It is always of type Atyp.
526 -- On return:
528 -- Obj is the object containing the desired bit field. It is of type
529 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
530 -- entire value, for the small static case, or the proper selected byte
531 -- from the array in the large or dynamic case. This node is analyzed
532 -- and resolved on return.
534 -- Shift is a node representing the shift count to be used in the
535 -- rotate right instruction that positions the field for access.
536 -- This node is analyzed and resolved on return.
538 -- Cmask is a mask corresponding to the width of the component field.
539 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
541 -- Note: in some cases the call to this routine may generate actions
542 -- (for handling multi-use references and the generation of the packed
543 -- array type on the fly). Such actions are inserted into the tree
544 -- directly using Insert_Action.
546 function Revert_Storage_Order (N : Node_Id) return Node_Id;
547 -- Perform appropriate justification and byte ordering adjustments for N,
548 -- an element of a packed array type, when both the component type and
549 -- the enclosing packed array type have reverse scalar storage order.
550 -- On little-endian targets, the value is left justified before byte
551 -- swapping. The Etype of the returned expression is an integer type of
552 -- an appropriate power-of-2 size.
554 --------------------------
555 -- Revert_Storage_Order --
556 --------------------------
558 function Revert_Storage_Order (N : Node_Id) return Node_Id is
559 Loc : constant Source_Ptr := Sloc (N);
560 T : constant Entity_Id := Etype (N);
561 T_Size : constant Uint := RM_Size (T);
563 Swap_RE : RE_Id;
564 Swap_F : Entity_Id;
565 Swap_T : Entity_Id;
566 -- Swapping function
568 Arg : Node_Id;
569 Adjusted : Node_Id;
570 Shift : Uint;
572 begin
573 if T_Size <= 8 then
575 -- Array component size is less than a byte: no swapping needed
577 Swap_F := Empty;
578 Swap_T := RTE (RE_Unsigned_8);
580 else
581 -- Select byte swapping function depending on array component size
583 if T_Size <= 16 then
584 Swap_RE := RE_Bswap_16;
586 elsif T_Size <= 32 then
587 Swap_RE := RE_Bswap_32;
589 else pragma Assert (T_Size <= 64);
590 Swap_RE := RE_Bswap_64;
591 end if;
593 Swap_F := RTE (Swap_RE);
594 Swap_T := Etype (Swap_F);
596 end if;
598 Shift := Esize (Swap_T) - T_Size;
600 Arg := RJ_Unchecked_Convert_To (Swap_T, N);
602 if not Bytes_Big_Endian and then Shift > Uint_0 then
603 Arg :=
604 Make_Op_Shift_Left (Loc,
605 Left_Opnd => Arg,
606 Right_Opnd => Make_Integer_Literal (Loc, Shift));
607 end if;
609 if Present (Swap_F) then
610 Adjusted :=
611 Make_Function_Call (Loc,
612 Name => New_Occurrence_Of (Swap_F, Loc),
613 Parameter_Associations => New_List (Arg));
614 else
615 Adjusted := Arg;
616 end if;
618 Set_Etype (Adjusted, Swap_T);
619 return Adjusted;
620 end Revert_Storage_Order;
622 ------------------------------
623 -- Compute_Linear_Subscript --
624 ------------------------------
626 procedure Compute_Linear_Subscript
627 (Atyp : Entity_Id;
628 N : Node_Id;
629 Subscr : out Node_Id)
631 Loc : constant Source_Ptr := Sloc (N);
632 Oldsub : Node_Id;
633 Newsub : Node_Id;
634 Indx : Node_Id;
635 Styp : Entity_Id;
637 begin
638 Subscr := Empty;
640 -- Loop through dimensions
642 Indx := First_Index (Atyp);
643 Oldsub := First (Expressions (N));
645 while Present (Indx) loop
646 Styp := Etype (Indx);
647 Newsub := Relocate_Node (Oldsub);
649 -- Get expression for the subscript value. First, if Do_Range_Check
650 -- is set on a subscript, then we must do a range check against the
651 -- original bounds (not the bounds of the packed array type). We do
652 -- this by introducing a subtype conversion.
654 if Do_Range_Check (Newsub)
655 and then Etype (Newsub) /= Styp
656 then
657 Newsub := Convert_To (Styp, Newsub);
658 end if;
660 -- Now evolve the expression for the subscript. First convert
661 -- the subscript to be zero based and of an integer type.
663 -- Case of integer type, where we just subtract to get lower bound
665 if Is_Integer_Type (Styp) then
667 -- If length of integer type is smaller than standard integer,
668 -- then we convert to integer first, then do the subtract
670 -- Integer (subscript) - Integer (Styp'First)
672 if Esize (Styp) < Esize (Standard_Integer) then
673 Newsub :=
674 Make_Op_Subtract (Loc,
675 Left_Opnd => Convert_To (Standard_Integer, Newsub),
676 Right_Opnd =>
677 Convert_To (Standard_Integer,
678 Make_Attribute_Reference (Loc,
679 Prefix => New_Occurrence_Of (Styp, Loc),
680 Attribute_Name => Name_First)));
682 -- For larger integer types, subtract first, then convert to
683 -- integer, this deals with strange long long integer bounds.
685 -- Integer (subscript - Styp'First)
687 else
688 Newsub :=
689 Convert_To (Standard_Integer,
690 Make_Op_Subtract (Loc,
691 Left_Opnd => Newsub,
692 Right_Opnd =>
693 Make_Attribute_Reference (Loc,
694 Prefix => New_Occurrence_Of (Styp, Loc),
695 Attribute_Name => Name_First)));
696 end if;
698 -- For the enumeration case, we have to use 'Pos to get the value
699 -- to work with before subtracting the lower bound.
701 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
703 -- This is not quite right for bizarre cases where the size of the
704 -- enumeration type is > Integer'Size bits due to rep clause ???
706 else
707 pragma Assert (Is_Enumeration_Type (Styp));
709 Newsub :=
710 Make_Op_Subtract (Loc,
711 Left_Opnd => Convert_To (Standard_Integer,
712 Make_Attribute_Reference (Loc,
713 Prefix => New_Occurrence_Of (Styp, Loc),
714 Attribute_Name => Name_Pos,
715 Expressions => New_List (Newsub))),
717 Right_Opnd =>
718 Convert_To (Standard_Integer,
719 Make_Attribute_Reference (Loc,
720 Prefix => New_Occurrence_Of (Styp, Loc),
721 Attribute_Name => Name_Pos,
722 Expressions => New_List (
723 Make_Attribute_Reference (Loc,
724 Prefix => New_Occurrence_Of (Styp, Loc),
725 Attribute_Name => Name_First)))));
726 end if;
728 Set_Paren_Count (Newsub, 1);
730 -- For the first subscript, we just copy that subscript value
732 if No (Subscr) then
733 Subscr := Newsub;
735 -- Otherwise, we must multiply what we already have by the current
736 -- stride and then add in the new value to the evolving subscript.
738 else
739 Subscr :=
740 Make_Op_Add (Loc,
741 Left_Opnd =>
742 Make_Op_Multiply (Loc,
743 Left_Opnd => Subscr,
744 Right_Opnd =>
745 Make_Attribute_Reference (Loc,
746 Attribute_Name => Name_Range_Length,
747 Prefix => New_Occurrence_Of (Styp, Loc))),
748 Right_Opnd => Newsub);
749 end if;
751 -- Move to next subscript
753 Next_Index (Indx);
754 Next (Oldsub);
755 end loop;
756 end Compute_Linear_Subscript;
758 -------------------------
759 -- Convert_To_PAT_Type --
760 -------------------------
762 -- The PAT is always obtained from the actual subtype
764 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
765 Act_ST : Entity_Id;
767 begin
768 Convert_To_Actual_Subtype (Aexp);
769 Act_ST := Underlying_Type (Etype (Aexp));
770 Create_Packed_Array_Impl_Type (Act_ST);
772 -- Just replace the etype with the packed array type. This works because
773 -- the expression will not be further analyzed, and Gigi considers the
774 -- two types equivalent in any case.
776 -- This is not strictly the case ??? If the reference is an actual in
777 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
778 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
779 -- array reference, reanalysis can produce spurious type errors when the
780 -- PAT type is replaced again with the original type of the array. Same
781 -- for the case of a dereference. Ditto for function calls: expansion
782 -- may introduce additional actuals which will trigger errors if call is
783 -- reanalyzed. The following is correct and minimal, but the handling of
784 -- more complex packed expressions in actuals is confused. Probably the
785 -- problem only remains for actuals in calls.
787 Set_Etype (Aexp, Packed_Array_Impl_Type (Act_ST));
789 if Is_Entity_Name (Aexp)
790 or else
791 (Nkind (Aexp) = N_Indexed_Component
792 and then Is_Entity_Name (Prefix (Aexp)))
793 or else Nkind_In (Aexp, N_Explicit_Dereference, N_Function_Call)
794 then
795 Set_Analyzed (Aexp);
796 end if;
797 end Convert_To_PAT_Type;
799 -----------------------------------
800 -- Create_Packed_Array_Impl_Type --
801 -----------------------------------
803 procedure Create_Packed_Array_Impl_Type (Typ : Entity_Id) is
804 Loc : constant Source_Ptr := Sloc (Typ);
805 Ctyp : constant Entity_Id := Component_Type (Typ);
806 Csize : constant Uint := Component_Size (Typ);
808 Ancest : Entity_Id;
809 PB_Type : Entity_Id;
810 PASize : Uint;
811 Decl : Node_Id;
812 PAT : Entity_Id;
813 Len_Dim : Node_Id;
814 Len_Expr : Node_Id;
815 Len_Bits : Uint;
816 Bits_U1 : Node_Id;
817 PAT_High : Node_Id;
818 Btyp : Entity_Id;
819 Lit : Node_Id;
821 procedure Install_PAT;
822 -- This procedure is called with Decl set to the declaration for the
823 -- packed array type. It creates the type and installs it as required.
825 procedure Set_PB_Type;
826 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
827 -- requirements (see documentation in the spec of this package).
829 -----------------
830 -- Install_PAT --
831 -----------------
833 procedure Install_PAT is
834 Pushed_Scope : Boolean := False;
836 begin
837 -- We do not want to put the declaration we have created in the tree
838 -- since it is often hard, and sometimes impossible to find a proper
839 -- place for it (the impossible case arises for a packed array type
840 -- with bounds depending on the discriminant, a declaration cannot
841 -- be put inside the record, and the reference to the discriminant
842 -- cannot be outside the record).
844 -- The solution is to analyze the declaration while temporarily
845 -- attached to the tree at an appropriate point, and then we install
846 -- the resulting type as an Itype in the packed array type field of
847 -- the original type, so that no explicit declaration is required.
849 -- Note: the packed type is created in the scope of its parent type.
850 -- There are at least some cases where the current scope is deeper,
851 -- and so when this is the case, we temporarily reset the scope
852 -- for the definition. This is clearly safe, since the first use
853 -- of the packed array type will be the implicit reference from
854 -- the corresponding unpacked type when it is elaborated.
856 if Is_Itype (Typ) then
857 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
858 else
859 Set_Parent (Decl, Declaration_Node (Typ));
860 end if;
862 if Scope (Typ) /= Current_Scope then
863 Push_Scope (Scope (Typ));
864 Pushed_Scope := True;
865 end if;
867 Set_Is_Itype (PAT, True);
868 Set_Packed_Array_Impl_Type (Typ, PAT);
869 Analyze (Decl, Suppress => All_Checks);
871 if Pushed_Scope then
872 Pop_Scope;
873 end if;
875 -- Set Esize and RM_Size to the actual size of the packed object
876 -- Do not reset RM_Size if already set, as happens in the case of
877 -- a modular type.
879 if Unknown_Esize (PAT) then
880 Set_Esize (PAT, PASize);
881 end if;
883 if Unknown_RM_Size (PAT) then
884 Set_RM_Size (PAT, PASize);
885 end if;
887 Adjust_Esize_Alignment (PAT);
889 -- Set remaining fields of packed array type
891 Init_Alignment (PAT);
892 Set_Parent (PAT, Empty);
893 Set_Associated_Node_For_Itype (PAT, Typ);
894 Set_Is_Packed_Array_Impl_Type (PAT, True);
895 Set_Original_Array_Type (PAT, Typ);
897 -- For a non-bit-packed array, propagate reverse storage order
898 -- flag from original base type to packed array base type.
900 if not Is_Bit_Packed_Array (Typ) then
901 Set_Reverse_Storage_Order
902 (Etype (PAT), Reverse_Storage_Order (Base_Type (Typ)));
903 end if;
905 -- We definitely do not want to delay freezing for packed array
906 -- types. This is of particular importance for the itypes that are
907 -- generated for record components depending on discriminants where
908 -- there is no place to put the freeze node.
910 Set_Has_Delayed_Freeze (PAT, False);
911 Set_Has_Delayed_Freeze (Etype (PAT), False);
913 -- If we did allocate a freeze node, then clear out the reference
914 -- since it is obsolete (should we delete the freeze node???)
916 Set_Freeze_Node (PAT, Empty);
917 Set_Freeze_Node (Etype (PAT), Empty);
918 end Install_PAT;
920 -----------------
921 -- Set_PB_Type --
922 -----------------
924 procedure Set_PB_Type is
925 begin
926 -- If the user has specified an explicit alignment for the
927 -- type or component, take it into account.
929 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
930 or else Alignment (Typ) = 1
931 or else Component_Alignment (Typ) = Calign_Storage_Unit
932 then
933 PB_Type := RTE (RE_Packed_Bytes1);
935 elsif Csize mod 4 /= 0
936 or else Alignment (Typ) = 2
937 then
938 PB_Type := RTE (RE_Packed_Bytes2);
940 else
941 PB_Type := RTE (RE_Packed_Bytes4);
942 end if;
943 end Set_PB_Type;
945 -- Start of processing for Create_Packed_Array_Impl_Type
947 begin
948 -- If we already have a packed array type, nothing to do
950 if Present (Packed_Array_Impl_Type (Typ)) then
951 return;
952 end if;
954 -- If our immediate ancestor subtype is constrained, and it already
955 -- has a packed array type, then just share the same type, since the
956 -- bounds must be the same. If the ancestor is not an array type but
957 -- a private type, as can happen with multiple instantiations, create
958 -- a new packed type, to avoid privacy issues.
960 if Ekind (Typ) = E_Array_Subtype then
961 Ancest := Ancestor_Subtype (Typ);
963 if Present (Ancest)
964 and then Is_Array_Type (Ancest)
965 and then Is_Constrained (Ancest)
966 and then Present (Packed_Array_Impl_Type (Ancest))
967 then
968 Set_Packed_Array_Impl_Type (Typ, Packed_Array_Impl_Type (Ancest));
969 return;
970 end if;
971 end if;
973 -- We preset the result type size from the size of the original array
974 -- type, since this size clearly belongs to the packed array type. The
975 -- size of the conceptual unpacked type is always set to unknown.
977 PASize := RM_Size (Typ);
979 -- Case of an array where at least one index is of an enumeration
980 -- type with a non-standard representation, but the component size
981 -- is not appropriate for bit packing. This is the case where we
982 -- have Is_Packed set (we would never be in this unit otherwise),
983 -- but Is_Bit_Packed_Array is false.
985 -- Note that if the component size is appropriate for bit packing,
986 -- then the circuit for the computation of the subscript properly
987 -- deals with the non-standard enumeration type case by taking the
988 -- Pos anyway.
990 if not Is_Bit_Packed_Array (Typ) then
992 -- Here we build a declaration:
994 -- type tttP is array (index1, index2, ...) of component_type
996 -- where index1, index2, are the index types. These are the same
997 -- as the index types of the original array, except for the non-
998 -- standard representation enumeration type case, where we have
999 -- two subcases.
1001 -- For the unconstrained array case, we use
1003 -- Natural range <>
1005 -- For the constrained case, we use
1007 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
1008 -- Enum_Type'Pos (Enum_Type'Last);
1010 -- Note that tttP is created even if no index subtype is a non
1011 -- standard enumeration, because we still need to remove padding
1012 -- normally inserted for component alignment.
1014 PAT :=
1015 Make_Defining_Identifier (Loc,
1016 Chars => New_External_Name (Chars (Typ), 'P'));
1018 Set_Packed_Array_Impl_Type (Typ, PAT);
1020 declare
1021 Indexes : constant List_Id := New_List;
1022 Indx : Node_Id;
1023 Indx_Typ : Entity_Id;
1024 Enum_Case : Boolean;
1025 Typedef : Node_Id;
1027 begin
1028 Indx := First_Index (Typ);
1030 while Present (Indx) loop
1031 Indx_Typ := Etype (Indx);
1033 Enum_Case := Is_Enumeration_Type (Indx_Typ)
1034 and then Has_Non_Standard_Rep (Indx_Typ);
1036 -- Unconstrained case
1038 if not Is_Constrained (Typ) then
1039 if Enum_Case then
1040 Indx_Typ := Standard_Natural;
1041 end if;
1043 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
1045 -- Constrained case
1047 else
1048 if not Enum_Case then
1049 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
1051 else
1052 Append_To (Indexes,
1053 Make_Subtype_Indication (Loc,
1054 Subtype_Mark =>
1055 New_Occurrence_Of (Standard_Natural, Loc),
1056 Constraint =>
1057 Make_Range_Constraint (Loc,
1058 Range_Expression =>
1059 Make_Range (Loc,
1060 Low_Bound =>
1061 Make_Attribute_Reference (Loc,
1062 Prefix =>
1063 New_Occurrence_Of (Indx_Typ, Loc),
1064 Attribute_Name => Name_Pos,
1065 Expressions => New_List (
1066 Make_Attribute_Reference (Loc,
1067 Prefix =>
1068 New_Occurrence_Of (Indx_Typ, Loc),
1069 Attribute_Name => Name_First))),
1071 High_Bound =>
1072 Make_Attribute_Reference (Loc,
1073 Prefix =>
1074 New_Occurrence_Of (Indx_Typ, Loc),
1075 Attribute_Name => Name_Pos,
1076 Expressions => New_List (
1077 Make_Attribute_Reference (Loc,
1078 Prefix =>
1079 New_Occurrence_Of (Indx_Typ, Loc),
1080 Attribute_Name => Name_Last)))))));
1082 end if;
1083 end if;
1085 Next_Index (Indx);
1086 end loop;
1088 if not Is_Constrained (Typ) then
1089 Typedef :=
1090 Make_Unconstrained_Array_Definition (Loc,
1091 Subtype_Marks => Indexes,
1092 Component_Definition =>
1093 Make_Component_Definition (Loc,
1094 Aliased_Present => False,
1095 Subtype_Indication =>
1096 New_Occurrence_Of (Ctyp, Loc)));
1098 else
1099 Typedef :=
1100 Make_Constrained_Array_Definition (Loc,
1101 Discrete_Subtype_Definitions => Indexes,
1102 Component_Definition =>
1103 Make_Component_Definition (Loc,
1104 Aliased_Present => False,
1105 Subtype_Indication =>
1106 New_Occurrence_Of (Ctyp, Loc)));
1107 end if;
1109 Decl :=
1110 Make_Full_Type_Declaration (Loc,
1111 Defining_Identifier => PAT,
1112 Type_Definition => Typedef);
1113 end;
1115 -- Set type as packed array type and install it
1117 Set_Is_Packed_Array_Impl_Type (PAT);
1118 Install_PAT;
1119 return;
1121 -- Case of bit-packing required for unconstrained array. We create
1122 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1124 elsif not Is_Constrained (Typ) then
1125 PAT :=
1126 Make_Defining_Identifier (Loc,
1127 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
1129 Set_Packed_Array_Impl_Type (Typ, PAT);
1130 Set_PB_Type;
1132 Decl :=
1133 Make_Subtype_Declaration (Loc,
1134 Defining_Identifier => PAT,
1135 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1136 Install_PAT;
1137 return;
1139 -- Remaining code is for the case of bit-packing for constrained array
1141 -- The name of the packed array subtype is
1143 -- ttt___XPsss
1145 -- where sss is the component size in bits and ttt is the name of
1146 -- the parent packed type.
1148 else
1149 PAT :=
1150 Make_Defining_Identifier (Loc,
1151 Chars => Make_Packed_Array_Impl_Type_Name (Typ, Csize));
1153 Set_Packed_Array_Impl_Type (Typ, PAT);
1155 -- Build an expression for the length of the array in bits.
1156 -- This is the product of the length of each of the dimensions
1158 declare
1159 J : Nat := 1;
1161 begin
1162 Len_Expr := Empty; -- suppress junk warning
1164 loop
1165 Len_Dim :=
1166 Make_Attribute_Reference (Loc,
1167 Attribute_Name => Name_Length,
1168 Prefix => New_Occurrence_Of (Typ, Loc),
1169 Expressions => New_List (
1170 Make_Integer_Literal (Loc, J)));
1172 if J = 1 then
1173 Len_Expr := Len_Dim;
1175 else
1176 Len_Expr :=
1177 Make_Op_Multiply (Loc,
1178 Left_Opnd => Len_Expr,
1179 Right_Opnd => Len_Dim);
1180 end if;
1182 J := J + 1;
1183 exit when J > Number_Dimensions (Typ);
1184 end loop;
1185 end;
1187 -- Temporarily attach the length expression to the tree and analyze
1188 -- and resolve it, so that we can test its value. We assume that the
1189 -- total length fits in type Integer. This expression may involve
1190 -- discriminants, so we treat it as a default/per-object expression.
1192 Set_Parent (Len_Expr, Typ);
1193 Preanalyze_Spec_Expression (Len_Expr, Standard_Long_Long_Integer);
1195 -- Use a modular type if possible. We can do this if we have
1196 -- static bounds, and the length is small enough, and the length
1197 -- is not zero. We exclude the zero length case because the size
1198 -- of things is always at least one, and the zero length object
1199 -- would have an anomalous size.
1201 if Compile_Time_Known_Value (Len_Expr) then
1202 Len_Bits := Expr_Value (Len_Expr) * Csize;
1204 -- Check for size known to be too large
1206 if Len_Bits >
1207 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1208 then
1209 if System_Storage_Unit = 8 then
1210 Error_Msg_N
1211 ("packed array size cannot exceed " &
1212 "Integer''Last bytes", Typ);
1213 else
1214 Error_Msg_N
1215 ("packed array size cannot exceed " &
1216 "Integer''Last storage units", Typ);
1217 end if;
1219 -- Reset length to arbitrary not too high value to continue
1221 Len_Expr := Make_Integer_Literal (Loc, 65535);
1222 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1223 end if;
1225 -- We normally consider small enough to mean no larger than the
1226 -- value of System_Max_Binary_Modulus_Power, checking that in the
1227 -- case of values longer than word size, we have long shifts.
1229 if Len_Bits > 0
1230 and then
1231 (Len_Bits <= System_Word_Size
1232 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1233 and then Support_Long_Shifts_On_Target))
1234 then
1235 -- We can use the modular type, it has the form:
1237 -- subtype tttPn is btyp
1238 -- range 0 .. 2 ** ((Typ'Length (1)
1239 -- * ... * Typ'Length (n)) * Csize) - 1;
1241 -- The bounds are statically known, and btyp is one of the
1242 -- unsigned types, depending on the length.
1244 if Len_Bits <= Standard_Short_Short_Integer_Size then
1245 Btyp := RTE (RE_Short_Short_Unsigned);
1247 elsif Len_Bits <= Standard_Short_Integer_Size then
1248 Btyp := RTE (RE_Short_Unsigned);
1250 elsif Len_Bits <= Standard_Integer_Size then
1251 Btyp := RTE (RE_Unsigned);
1253 elsif Len_Bits <= Standard_Long_Integer_Size then
1254 Btyp := RTE (RE_Long_Unsigned);
1256 else
1257 Btyp := RTE (RE_Long_Long_Unsigned);
1258 end if;
1260 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1261 Set_Print_In_Hex (Lit);
1263 Decl :=
1264 Make_Subtype_Declaration (Loc,
1265 Defining_Identifier => PAT,
1266 Subtype_Indication =>
1267 Make_Subtype_Indication (Loc,
1268 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1270 Constraint =>
1271 Make_Range_Constraint (Loc,
1272 Range_Expression =>
1273 Make_Range (Loc,
1274 Low_Bound =>
1275 Make_Integer_Literal (Loc, 0),
1276 High_Bound => Lit))));
1278 if PASize = Uint_0 then
1279 PASize := Len_Bits;
1280 end if;
1282 Install_PAT;
1284 -- Propagate a given alignment to the modular type. This can
1285 -- cause it to be under-aligned, but that's OK.
1287 if Present (Alignment_Clause (Typ)) then
1288 Set_Alignment (PAT, Alignment (Typ));
1289 end if;
1291 return;
1292 end if;
1293 end if;
1295 -- Could not use a modular type, for all other cases, we build
1296 -- a packed array subtype:
1298 -- subtype tttPn is
1299 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1301 -- Bits is the length of the array in bits
1303 Set_PB_Type;
1305 Bits_U1 :=
1306 Make_Op_Add (Loc,
1307 Left_Opnd =>
1308 Make_Op_Multiply (Loc,
1309 Left_Opnd =>
1310 Make_Integer_Literal (Loc, Csize),
1311 Right_Opnd => Len_Expr),
1313 Right_Opnd =>
1314 Make_Integer_Literal (Loc, 7));
1316 Set_Paren_Count (Bits_U1, 1);
1318 PAT_High :=
1319 Make_Op_Subtract (Loc,
1320 Left_Opnd =>
1321 Make_Op_Divide (Loc,
1322 Left_Opnd => Bits_U1,
1323 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1324 Right_Opnd => Make_Integer_Literal (Loc, 1));
1326 Decl :=
1327 Make_Subtype_Declaration (Loc,
1328 Defining_Identifier => PAT,
1329 Subtype_Indication =>
1330 Make_Subtype_Indication (Loc,
1331 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1332 Constraint =>
1333 Make_Index_Or_Discriminant_Constraint (Loc,
1334 Constraints => New_List (
1335 Make_Range (Loc,
1336 Low_Bound =>
1337 Make_Integer_Literal (Loc, 0),
1338 High_Bound =>
1339 Convert_To (Standard_Integer, PAT_High))))));
1341 Install_PAT;
1343 -- Currently the code in this unit requires that packed arrays
1344 -- represented by non-modular arrays of bytes be on a byte
1345 -- boundary for bit sizes handled by System.Pack_nn units.
1346 -- That's because these units assume the array being accessed
1347 -- starts on a byte boundary.
1349 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1350 Set_Must_Be_On_Byte_Boundary (Typ);
1351 end if;
1352 end if;
1353 end Create_Packed_Array_Impl_Type;
1355 -----------------------------------
1356 -- Expand_Bit_Packed_Element_Set --
1357 -----------------------------------
1359 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1360 Loc : constant Source_Ptr := Sloc (N);
1361 Lhs : constant Node_Id := Name (N);
1363 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1364 -- Used to preserve assignment OK status when assignment is rewritten
1366 Rhs : Node_Id := Expression (N);
1367 -- Initially Rhs is the right hand side value, it will be replaced
1368 -- later by an appropriate unchecked conversion for the assignment.
1370 Obj : Node_Id;
1371 Atyp : Entity_Id;
1372 PAT : Entity_Id;
1373 Ctyp : Entity_Id;
1374 Csiz : Int;
1375 Cmask : Uint;
1377 Shift : Node_Id;
1378 -- The expression for the shift value that is required
1380 Shift_Used : Boolean := False;
1381 -- Set True if Shift has been used in the generated code at least once,
1382 -- so that it must be duplicated if used again.
1384 New_Lhs : Node_Id;
1385 New_Rhs : Node_Id;
1387 Rhs_Val_Known : Boolean;
1388 Rhs_Val : Uint;
1389 -- If the value of the right hand side as an integer constant is
1390 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1391 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1392 -- the Rhs_Val is undefined.
1394 function Get_Shift return Node_Id;
1395 -- Function used to get the value of Shift, making sure that it
1396 -- gets duplicated if the function is called more than once.
1398 ---------------
1399 -- Get_Shift --
1400 ---------------
1402 function Get_Shift return Node_Id is
1403 begin
1404 -- If we used the shift value already, then duplicate it. We
1405 -- set a temporary parent in case actions have to be inserted.
1407 if Shift_Used then
1408 Set_Parent (Shift, N);
1409 return Duplicate_Subexpr_No_Checks (Shift);
1411 -- If first time, use Shift unchanged, and set flag for first use
1413 else
1414 Shift_Used := True;
1415 return Shift;
1416 end if;
1417 end Get_Shift;
1419 -- Start of processing for Expand_Bit_Packed_Element_Set
1421 begin
1422 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1424 Obj := Relocate_Node (Prefix (Lhs));
1425 Convert_To_Actual_Subtype (Obj);
1426 Atyp := Etype (Obj);
1427 PAT := Packed_Array_Impl_Type (Atyp);
1428 Ctyp := Component_Type (Atyp);
1429 Csiz := UI_To_Int (Component_Size (Atyp));
1431 -- We remove side effects, in case the rhs modifies the lhs, because we
1432 -- are about to transform the rhs into an expression that first READS
1433 -- the lhs, so we can do the necessary shifting and masking. Example:
1434 -- "X(2) := F(...);" where F modifies X(3). Otherwise, the side effect
1435 -- will be lost.
1437 Remove_Side_Effects (Rhs);
1439 -- We convert the right hand side to the proper subtype to ensure
1440 -- that an appropriate range check is made (since the normal range
1441 -- check from assignment will be lost in the transformations). This
1442 -- conversion is analyzed immediately so that subsequent processing
1443 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1445 -- If the right-hand side is a string literal, create a temporary for
1446 -- it, constant-folding is not ready to wrap the bit representation
1447 -- of a string literal.
1449 if Nkind (Rhs) = N_String_Literal then
1450 declare
1451 Decl : Node_Id;
1452 begin
1453 Decl :=
1454 Make_Object_Declaration (Loc,
1455 Defining_Identifier => Make_Temporary (Loc, 'T', Rhs),
1456 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1457 Expression => New_Copy_Tree (Rhs));
1459 Insert_Actions (N, New_List (Decl));
1460 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1461 end;
1462 end if;
1464 Rhs := Convert_To (Ctyp, Rhs);
1465 Set_Parent (Rhs, N);
1467 -- If we are building the initialization procedure for a packed array,
1468 -- and Initialize_Scalars is enabled, each component assignment is an
1469 -- out-of-range value by design. Compile this value without checks,
1470 -- because a call to the array init_proc must not raise an exception.
1472 -- Condition is not consistent with description above, Within_Init_Proc
1473 -- is True also when we are building the IP for a record or protected
1474 -- type that has a packed array component???
1476 if Within_Init_Proc
1477 and then Initialize_Scalars
1478 then
1479 Analyze_And_Resolve (Rhs, Ctyp, Suppress => All_Checks);
1480 else
1481 Analyze_And_Resolve (Rhs, Ctyp);
1482 end if;
1484 -- For the AAMP target, indexing of certain packed array is passed
1485 -- through to the back end without expansion, because the expansion
1486 -- results in very inefficient code on that target. This allows the
1487 -- GNAAMP back end to generate specialized macros that support more
1488 -- efficient indexing of packed arrays with components having sizes
1489 -- that are small powers of two.
1491 if AAMP_On_Target
1492 and then (Csiz = 1 or else Csiz = 2 or else Csiz = 4)
1493 then
1494 return;
1495 end if;
1497 -- Case of component size 1,2,4 or any component size for the modular
1498 -- case. These are the cases for which we can inline the code.
1500 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1501 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1502 then
1503 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1505 -- The statement to be generated is:
1507 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, Shift)))
1509 -- or in the case of a freestanding Reverse_Storage_Order object,
1511 -- Obj := Swap (atyp!((Swap (Obj) and Mask1)
1512 -- or (shift_left (rhs, Shift))))
1514 -- where Mask1 is obtained by shifting Cmask left Shift bits
1515 -- and then complementing the result.
1517 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1519 -- the "or ..." is omitted if rhs is constant and all 0 bits
1521 -- rhs is converted to the appropriate type
1523 -- The result is converted back to the array type, since
1524 -- otherwise we lose knowledge of the packed nature.
1526 -- Determine if right side is all 0 bits or all 1 bits
1528 if Compile_Time_Known_Value (Rhs) then
1529 Rhs_Val := Expr_Rep_Value (Rhs);
1530 Rhs_Val_Known := True;
1532 -- The following test catches the case of an unchecked conversion of
1533 -- an integer literal. This results from optimizing aggregates of
1534 -- packed types.
1536 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1537 and then Compile_Time_Known_Value (Expression (Rhs))
1538 then
1539 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1540 Rhs_Val_Known := True;
1542 else
1543 Rhs_Val := No_Uint;
1544 Rhs_Val_Known := False;
1545 end if;
1547 -- Some special checks for the case where the right hand value is
1548 -- known at compile time. Basically we have to take care of the
1549 -- implicit conversion to the subtype of the component object.
1551 if Rhs_Val_Known then
1553 -- If we have a biased component type then we must manually do the
1554 -- biasing, since we are taking responsibility in this case for
1555 -- constructing the exact bit pattern to be used.
1557 if Has_Biased_Representation (Ctyp) then
1558 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1559 end if;
1561 -- For a negative value, we manually convert the two's complement
1562 -- value to a corresponding unsigned value, so that the proper
1563 -- field width is maintained. If we did not do this, we would
1564 -- get too many leading sign bits later on.
1566 if Rhs_Val < 0 then
1567 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1568 end if;
1569 end if;
1571 -- Now create copies removing side effects. Note that in some complex
1572 -- cases, this may cause the fact that we have already set a packed
1573 -- array type on Obj to get lost. So we save the type of Obj, and
1574 -- make sure it is reset properly.
1576 New_Lhs := Duplicate_Subexpr (Obj, Name_Req => True);
1577 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1579 -- First we deal with the "and"
1581 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1582 declare
1583 Mask1 : Node_Id;
1584 Lit : Node_Id;
1586 begin
1587 if Compile_Time_Known_Value (Shift) then
1588 Mask1 :=
1589 Make_Integer_Literal (Loc,
1590 Modulus (Etype (Obj)) - 1 -
1591 (Cmask * (2 ** Expr_Value (Get_Shift))));
1592 Set_Print_In_Hex (Mask1);
1594 else
1595 Lit := Make_Integer_Literal (Loc, Cmask);
1596 Set_Print_In_Hex (Lit);
1597 Mask1 :=
1598 Make_Op_Not (Loc,
1599 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1600 end if;
1602 New_Rhs :=
1603 Make_Op_And (Loc,
1604 Left_Opnd => New_Rhs,
1605 Right_Opnd => Mask1);
1606 end;
1607 end if;
1609 -- Then deal with the "or"
1611 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1612 declare
1613 Or_Rhs : Node_Id;
1615 procedure Fixup_Rhs;
1616 -- Adjust Rhs by bias if biased representation for components
1617 -- or remove extraneous high order sign bits if signed.
1619 procedure Fixup_Rhs is
1620 Etyp : constant Entity_Id := Etype (Rhs);
1622 begin
1623 -- For biased case, do the required biasing by simply
1624 -- converting to the biased subtype (the conversion
1625 -- will generate the required bias).
1627 if Has_Biased_Representation (Ctyp) then
1628 Rhs := Convert_To (Ctyp, Rhs);
1630 -- For a signed integer type that is not biased, generate
1631 -- a conversion to unsigned to strip high order sign bits.
1633 elsif Is_Signed_Integer_Type (Ctyp) then
1634 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1635 end if;
1637 -- Set Etype, since it can be referenced before the node is
1638 -- completely analyzed.
1640 Set_Etype (Rhs, Etyp);
1642 -- We now need to do an unchecked conversion of the
1643 -- result to the target type, but it is important that
1644 -- this conversion be a right justified conversion and
1645 -- not a left justified conversion.
1647 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1648 end Fixup_Rhs;
1650 begin
1651 if Rhs_Val_Known
1652 and then Compile_Time_Known_Value (Get_Shift)
1653 then
1654 Or_Rhs :=
1655 Make_Integer_Literal (Loc,
1656 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1657 Set_Print_In_Hex (Or_Rhs);
1659 else
1660 -- We have to convert the right hand side to Etype (Obj).
1661 -- A special case arises if what we have now is a Val
1662 -- attribute reference whose expression type is Etype (Obj).
1663 -- This happens for assignments of fields from the same
1664 -- array. In this case we get the required right hand side
1665 -- by simply removing the inner attribute reference.
1667 if Nkind (Rhs) = N_Attribute_Reference
1668 and then Attribute_Name (Rhs) = Name_Val
1669 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1670 then
1671 Rhs := Relocate_Node (First (Expressions (Rhs)));
1672 Fixup_Rhs;
1674 -- If the value of the right hand side is a known integer
1675 -- value, then just replace it by an untyped constant,
1676 -- which will be properly retyped when we analyze and
1677 -- resolve the expression.
1679 elsif Rhs_Val_Known then
1681 -- Note that Rhs_Val has already been normalized to
1682 -- be an unsigned value with the proper number of bits.
1684 Rhs := Make_Integer_Literal (Loc, Rhs_Val);
1686 -- Otherwise we need an unchecked conversion
1688 else
1689 Fixup_Rhs;
1690 end if;
1692 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1693 end if;
1695 if Nkind (New_Rhs) = N_Op_And then
1696 Set_Paren_Count (New_Rhs, 1);
1697 Set_Etype (New_Rhs, Etype (Left_Opnd (New_Rhs)));
1698 end if;
1700 New_Rhs :=
1701 Make_Op_Or (Loc,
1702 Left_Opnd => New_Rhs,
1703 Right_Opnd => Or_Rhs);
1704 end;
1705 end if;
1707 -- Now do the rewrite
1709 Rewrite (N,
1710 Make_Assignment_Statement (Loc,
1711 Name => New_Lhs,
1712 Expression =>
1713 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1714 Set_Assignment_OK (Name (N), Ass_OK);
1716 -- All other component sizes for non-modular case
1718 else
1719 -- We generate
1721 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1723 -- where Subscr is the computed linear subscript
1725 declare
1726 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1727 Set_nn : Entity_Id;
1728 Subscr : Node_Id;
1729 Atyp : Entity_Id;
1730 Rev_SSO : Node_Id;
1732 begin
1733 if No (Bits_nn) then
1735 -- Error, most likely High_Integrity_Mode restriction
1737 return;
1738 end if;
1740 -- Acquire proper Set entity. We use the aligned or unaligned
1741 -- case as appropriate.
1743 if Known_Aligned_Enough (Obj, Csiz) then
1744 Set_nn := RTE (Set_Id (Csiz));
1745 else
1746 Set_nn := RTE (SetU_Id (Csiz));
1747 end if;
1749 -- Now generate the set reference
1751 Obj := Relocate_Node (Prefix (Lhs));
1752 Convert_To_Actual_Subtype (Obj);
1753 Atyp := Etype (Obj);
1754 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1756 -- Set indication of whether the packed array has reverse SSO
1758 Rev_SSO :=
1759 New_Occurrence_Of
1760 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
1762 -- Below we must make the assumption that Obj is
1763 -- at least byte aligned, since otherwise its address
1764 -- cannot be taken. The assumption holds since the
1765 -- only arrays that can be misaligned are small packed
1766 -- arrays which are implemented as a modular type, and
1767 -- that is not the case here.
1769 Rewrite (N,
1770 Make_Procedure_Call_Statement (Loc,
1771 Name => New_Occurrence_Of (Set_nn, Loc),
1772 Parameter_Associations => New_List (
1773 Make_Attribute_Reference (Loc,
1774 Prefix => Obj,
1775 Attribute_Name => Name_Address),
1776 Subscr,
1777 Unchecked_Convert_To (Bits_nn, Convert_To (Ctyp, Rhs)),
1778 Rev_SSO)));
1780 end;
1781 end if;
1783 Analyze (N, Suppress => All_Checks);
1784 end Expand_Bit_Packed_Element_Set;
1786 -------------------------------------
1787 -- Expand_Packed_Address_Reference --
1788 -------------------------------------
1790 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1791 Loc : constant Source_Ptr := Sloc (N);
1792 Base : Node_Id;
1793 Offset : Node_Id;
1795 begin
1796 -- We build an expression that has the form
1798 -- outer_object'Address
1799 -- + (linear-subscript * component_size for each array reference
1800 -- + field'Bit_Position for each record field
1801 -- + ...
1802 -- + ...) / Storage_Unit;
1804 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1806 Rewrite (N,
1807 Unchecked_Convert_To (RTE (RE_Address),
1808 Make_Op_Add (Loc,
1809 Left_Opnd =>
1810 Unchecked_Convert_To (RTE (RE_Integer_Address),
1811 Make_Attribute_Reference (Loc,
1812 Prefix => Base,
1813 Attribute_Name => Name_Address)),
1815 Right_Opnd =>
1816 Unchecked_Convert_To (RTE (RE_Integer_Address),
1817 Make_Op_Divide (Loc,
1818 Left_Opnd => Offset,
1819 Right_Opnd =>
1820 Make_Integer_Literal (Loc, System_Storage_Unit))))));
1822 Analyze_And_Resolve (N, RTE (RE_Address));
1823 end Expand_Packed_Address_Reference;
1825 ---------------------------------
1826 -- Expand_Packed_Bit_Reference --
1827 ---------------------------------
1829 procedure Expand_Packed_Bit_Reference (N : Node_Id) is
1830 Loc : constant Source_Ptr := Sloc (N);
1831 Base : Node_Id;
1832 Offset : Node_Id;
1834 begin
1835 -- We build an expression that has the form
1837 -- (linear-subscript * component_size for each array reference
1838 -- + field'Bit_Position for each record field
1839 -- + ...
1840 -- + ...) mod Storage_Unit;
1842 Get_Base_And_Bit_Offset (Prefix (N), Base, Offset);
1844 Rewrite (N,
1845 Unchecked_Convert_To (Universal_Integer,
1846 Make_Op_Mod (Loc,
1847 Left_Opnd => Offset,
1848 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
1850 Analyze_And_Resolve (N, Universal_Integer);
1851 end Expand_Packed_Bit_Reference;
1853 ------------------------------------
1854 -- Expand_Packed_Boolean_Operator --
1855 ------------------------------------
1857 -- This routine expands "a op b" for the packed cases
1859 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1860 Loc : constant Source_Ptr := Sloc (N);
1861 Typ : constant Entity_Id := Etype (N);
1862 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1863 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1865 Ltyp : Entity_Id;
1866 Rtyp : Entity_Id;
1867 PAT : Entity_Id;
1869 begin
1870 Convert_To_Actual_Subtype (L);
1871 Convert_To_Actual_Subtype (R);
1873 Ensure_Defined (Etype (L), N);
1874 Ensure_Defined (Etype (R), N);
1876 Apply_Length_Check (R, Etype (L));
1878 Ltyp := Etype (L);
1879 Rtyp := Etype (R);
1881 -- Deal with silly case of XOR where the subcomponent has a range
1882 -- True .. True where an exception must be raised.
1884 if Nkind (N) = N_Op_Xor then
1885 Silly_Boolean_Array_Xor_Test (N, Rtyp);
1886 end if;
1888 -- Now that that silliness is taken care of, get packed array type
1890 Convert_To_PAT_Type (L);
1891 Convert_To_PAT_Type (R);
1893 PAT := Etype (L);
1895 -- For the modular case, we expand a op b into
1897 -- rtyp!(pat!(a) op pat!(b))
1899 -- where rtyp is the Etype of the left operand. Note that we do not
1900 -- convert to the base type, since this would be unconstrained, and
1901 -- hence not have a corresponding packed array type set.
1903 -- Note that both operands must be modular for this code to be used
1905 if Is_Modular_Integer_Type (PAT)
1906 and then
1907 Is_Modular_Integer_Type (Etype (R))
1908 then
1909 declare
1910 P : Node_Id;
1912 begin
1913 if Nkind (N) = N_Op_And then
1914 P := Make_Op_And (Loc, L, R);
1916 elsif Nkind (N) = N_Op_Or then
1917 P := Make_Op_Or (Loc, L, R);
1919 else -- Nkind (N) = N_Op_Xor
1920 P := Make_Op_Xor (Loc, L, R);
1921 end if;
1923 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1924 end;
1926 -- For the array case, we insert the actions
1928 -- Result : Ltype;
1930 -- System.Bit_Ops.Bit_And/Or/Xor
1931 -- (Left'Address,
1932 -- Ltype'Length * Ltype'Component_Size;
1933 -- Right'Address,
1934 -- Rtype'Length * Rtype'Component_Size
1935 -- Result'Address);
1937 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1938 -- the second argument and fourth arguments are the lengths of the
1939 -- operands in bits. Then we replace the expression by a reference
1940 -- to Result.
1942 -- Note that if we are mixing a modular and array operand, everything
1943 -- works fine, since we ensure that the modular representation has the
1944 -- same physical layout as the array representation (that's what the
1945 -- left justified modular stuff in the big-endian case is about).
1947 else
1948 declare
1949 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
1950 E_Id : RE_Id;
1952 begin
1953 if Nkind (N) = N_Op_And then
1954 E_Id := RE_Bit_And;
1956 elsif Nkind (N) = N_Op_Or then
1957 E_Id := RE_Bit_Or;
1959 else -- Nkind (N) = N_Op_Xor
1960 E_Id := RE_Bit_Xor;
1961 end if;
1963 Insert_Actions (N, New_List (
1965 Make_Object_Declaration (Loc,
1966 Defining_Identifier => Result_Ent,
1967 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1969 Make_Procedure_Call_Statement (Loc,
1970 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1971 Parameter_Associations => New_List (
1973 Make_Byte_Aligned_Attribute_Reference (Loc,
1974 Prefix => L,
1975 Attribute_Name => Name_Address),
1977 Make_Op_Multiply (Loc,
1978 Left_Opnd =>
1979 Make_Attribute_Reference (Loc,
1980 Prefix =>
1981 New_Occurrence_Of
1982 (Etype (First_Index (Ltyp)), Loc),
1983 Attribute_Name => Name_Range_Length),
1985 Right_Opnd =>
1986 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1988 Make_Byte_Aligned_Attribute_Reference (Loc,
1989 Prefix => R,
1990 Attribute_Name => Name_Address),
1992 Make_Op_Multiply (Loc,
1993 Left_Opnd =>
1994 Make_Attribute_Reference (Loc,
1995 Prefix =>
1996 New_Occurrence_Of
1997 (Etype (First_Index (Rtyp)), Loc),
1998 Attribute_Name => Name_Range_Length),
2000 Right_Opnd =>
2001 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2003 Make_Byte_Aligned_Attribute_Reference (Loc,
2004 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2005 Attribute_Name => Name_Address)))));
2007 Rewrite (N,
2008 New_Occurrence_Of (Result_Ent, Loc));
2009 end;
2010 end if;
2012 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2013 end Expand_Packed_Boolean_Operator;
2015 -------------------------------------
2016 -- Expand_Packed_Element_Reference --
2017 -------------------------------------
2019 procedure Expand_Packed_Element_Reference (N : Node_Id) is
2020 Loc : constant Source_Ptr := Sloc (N);
2021 Obj : Node_Id;
2022 Atyp : Entity_Id;
2023 PAT : Entity_Id;
2024 Ctyp : Entity_Id;
2025 Csiz : Int;
2026 Shift : Node_Id;
2027 Cmask : Uint;
2028 Lit : Node_Id;
2029 Arg : Node_Id;
2031 begin
2032 -- If the node is an actual in a call, the prefix has not been fully
2033 -- expanded, to account for the additional expansion for in-out actuals
2034 -- (see expand_actuals for details). If the prefix itself is a packed
2035 -- reference as well, we have to recurse to complete the transformation
2036 -- of the prefix.
2038 if Nkind (Prefix (N)) = N_Indexed_Component
2039 and then not Analyzed (Prefix (N))
2040 and then Is_Bit_Packed_Array (Etype (Prefix (Prefix (N))))
2041 then
2042 Expand_Packed_Element_Reference (Prefix (N));
2043 end if;
2045 -- If not bit packed, we have the enumeration case, which is easily
2046 -- dealt with (just adjust the subscripts of the indexed component)
2048 -- Note: this leaves the result as an indexed component, which is
2049 -- still a variable, so can be used in the assignment case, as is
2050 -- required in the enumeration case.
2052 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
2053 Setup_Enumeration_Packed_Array_Reference (N);
2054 return;
2055 end if;
2057 -- Remaining processing is for the bit-packed case
2059 Obj := Relocate_Node (Prefix (N));
2060 Convert_To_Actual_Subtype (Obj);
2061 Atyp := Etype (Obj);
2062 PAT := Packed_Array_Impl_Type (Atyp);
2063 Ctyp := Component_Type (Atyp);
2064 Csiz := UI_To_Int (Component_Size (Atyp));
2066 -- For the AAMP target, indexing of certain packed array is passed
2067 -- through to the back end without expansion, because the expansion
2068 -- results in very inefficient code on that target. This allows the
2069 -- GNAAMP back end to generate specialized macros that support more
2070 -- efficient indexing of packed arrays with components having sizes
2071 -- that are small powers of two.
2073 if AAMP_On_Target
2074 and then (Csiz = 1 or else Csiz = 2 or else Csiz = 4)
2075 then
2076 return;
2077 end if;
2079 -- Case of component size 1,2,4 or any component size for the modular
2080 -- case. These are the cases for which we can inline the code.
2082 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
2083 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
2084 then
2085 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
2086 Lit := Make_Integer_Literal (Loc, Cmask);
2087 Set_Print_In_Hex (Lit);
2089 -- We generate a shift right to position the field, followed by a
2090 -- masking operation to extract the bit field, and we finally do an
2091 -- unchecked conversion to convert the result to the required target.
2093 -- Note that the unchecked conversion automatically deals with the
2094 -- bias if we are dealing with a biased representation. What will
2095 -- happen is that we temporarily generate the biased representation,
2096 -- but almost immediately that will be converted to the original
2097 -- unbiased component type, and the bias will disappear.
2099 Arg :=
2100 Make_Op_And (Loc,
2101 Left_Opnd => Make_Shift_Right (Obj, Shift),
2102 Right_Opnd => Lit);
2103 Set_Etype (Arg, Ctyp);
2105 -- Component extraction is performed on a native endianness scalar
2106 -- value: if Atyp has reverse storage order, then it has been byte
2107 -- swapped, and if the component being extracted is itself of a
2108 -- composite type with reverse storage order, then we need to swap
2109 -- it back to its expected endianness after extraction.
2111 if Reverse_Storage_Order (Atyp)
2112 and then (Is_Record_Type (Ctyp) or else Is_Array_Type (Ctyp))
2113 and then Reverse_Storage_Order (Ctyp)
2114 then
2115 Arg := Revert_Storage_Order (Arg);
2116 end if;
2118 -- We needed to analyze this before we do the unchecked convert
2119 -- below, but we need it temporarily attached to the tree for
2120 -- this analysis (hence the temporary Set_Parent call).
2122 Set_Parent (Arg, Parent (N));
2123 Analyze_And_Resolve (Arg);
2125 Rewrite (N, RJ_Unchecked_Convert_To (Ctyp, Arg));
2127 -- All other component sizes for non-modular case
2129 else
2130 -- We generate
2132 -- Component_Type!(Get_nn (Arr'address, Subscr))
2134 -- where Subscr is the computed linear subscript
2136 declare
2137 Get_nn : Entity_Id;
2138 Subscr : Node_Id;
2139 Rev_SSO : constant Node_Id :=
2140 New_Occurrence_Of
2141 (Boolean_Literals (Reverse_Storage_Order (Atyp)), Loc);
2143 begin
2144 -- Acquire proper Get entity. We use the aligned or unaligned
2145 -- case as appropriate.
2147 if Known_Aligned_Enough (Obj, Csiz) then
2148 Get_nn := RTE (Get_Id (Csiz));
2149 else
2150 Get_nn := RTE (GetU_Id (Csiz));
2151 end if;
2153 -- Now generate the get reference
2155 Compute_Linear_Subscript (Atyp, N, Subscr);
2157 -- Below we make the assumption that Obj is at least byte
2158 -- aligned, since otherwise its address cannot be taken.
2159 -- The assumption holds since the only arrays that can be
2160 -- misaligned are small packed arrays which are implemented
2161 -- as a modular type, and that is not the case here.
2163 Rewrite (N,
2164 Unchecked_Convert_To (Ctyp,
2165 Make_Function_Call (Loc,
2166 Name => New_Occurrence_Of (Get_nn, Loc),
2167 Parameter_Associations => New_List (
2168 Make_Attribute_Reference (Loc,
2169 Prefix => Obj,
2170 Attribute_Name => Name_Address),
2171 Subscr,
2172 Rev_SSO))));
2173 end;
2174 end if;
2176 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2177 end Expand_Packed_Element_Reference;
2179 ----------------------
2180 -- Expand_Packed_Eq --
2181 ----------------------
2183 -- Handles expansion of "=" on packed array types
2185 procedure Expand_Packed_Eq (N : Node_Id) is
2186 Loc : constant Source_Ptr := Sloc (N);
2187 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2188 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2190 LLexpr : Node_Id;
2191 RLexpr : Node_Id;
2193 Ltyp : Entity_Id;
2194 Rtyp : Entity_Id;
2195 PAT : Entity_Id;
2197 begin
2198 Convert_To_Actual_Subtype (L);
2199 Convert_To_Actual_Subtype (R);
2200 Ltyp := Underlying_Type (Etype (L));
2201 Rtyp := Underlying_Type (Etype (R));
2203 Convert_To_PAT_Type (L);
2204 Convert_To_PAT_Type (R);
2205 PAT := Etype (L);
2207 LLexpr :=
2208 Make_Op_Multiply (Loc,
2209 Left_Opnd =>
2210 Make_Attribute_Reference (Loc,
2211 Prefix => New_Occurrence_Of (Ltyp, Loc),
2212 Attribute_Name => Name_Length),
2213 Right_Opnd =>
2214 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2216 RLexpr :=
2217 Make_Op_Multiply (Loc,
2218 Left_Opnd =>
2219 Make_Attribute_Reference (Loc,
2220 Prefix => New_Occurrence_Of (Rtyp, Loc),
2221 Attribute_Name => Name_Length),
2222 Right_Opnd =>
2223 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2225 -- For the modular case, we transform the comparison to:
2227 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2229 -- where PAT is the packed array type. This works fine, since in the
2230 -- modular case we guarantee that the unused bits are always zeroes.
2231 -- We do have to compare the lengths because we could be comparing
2232 -- two different subtypes of the same base type.
2234 if Is_Modular_Integer_Type (PAT) then
2235 Rewrite (N,
2236 Make_And_Then (Loc,
2237 Left_Opnd =>
2238 Make_Op_Eq (Loc,
2239 Left_Opnd => LLexpr,
2240 Right_Opnd => RLexpr),
2242 Right_Opnd =>
2243 Make_Op_Eq (Loc,
2244 Left_Opnd => L,
2245 Right_Opnd => R)));
2247 -- For the non-modular case, we call a runtime routine
2249 -- System.Bit_Ops.Bit_Eq
2250 -- (L'Address, L_Length, R'Address, R_Length)
2252 -- where PAT is the packed array type, and the lengths are the lengths
2253 -- in bits of the original packed arrays. This routine takes care of
2254 -- not comparing the unused bits in the last byte.
2256 else
2257 Rewrite (N,
2258 Make_Function_Call (Loc,
2259 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2260 Parameter_Associations => New_List (
2261 Make_Byte_Aligned_Attribute_Reference (Loc,
2262 Prefix => L,
2263 Attribute_Name => Name_Address),
2265 LLexpr,
2267 Make_Byte_Aligned_Attribute_Reference (Loc,
2268 Prefix => R,
2269 Attribute_Name => Name_Address),
2271 RLexpr)));
2272 end if;
2274 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2275 end Expand_Packed_Eq;
2277 -----------------------
2278 -- Expand_Packed_Not --
2279 -----------------------
2281 -- Handles expansion of "not" on packed array types
2283 procedure Expand_Packed_Not (N : Node_Id) is
2284 Loc : constant Source_Ptr := Sloc (N);
2285 Typ : constant Entity_Id := Etype (N);
2286 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2288 Rtyp : Entity_Id;
2289 PAT : Entity_Id;
2290 Lit : Node_Id;
2292 begin
2293 Convert_To_Actual_Subtype (Opnd);
2294 Rtyp := Etype (Opnd);
2296 -- Deal with silly False..False and True..True subtype case
2298 Silly_Boolean_Array_Not_Test (N, Rtyp);
2300 -- Now that the silliness is taken care of, get packed array type
2302 Convert_To_PAT_Type (Opnd);
2303 PAT := Etype (Opnd);
2305 -- For the case where the packed array type is a modular type, "not A"
2306 -- expands simply into:
2308 -- Rtyp!(PAT!(A) xor Mask)
2310 -- where PAT is the packed array type, Mask is a mask of all 1 bits of
2311 -- length equal to the size of this packed type, and Rtyp is the actual
2312 -- actual subtype of the operand.
2314 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2315 Set_Print_In_Hex (Lit);
2317 if not Is_Array_Type (PAT) then
2318 Rewrite (N,
2319 Unchecked_Convert_To (Rtyp,
2320 Make_Op_Xor (Loc,
2321 Left_Opnd => Opnd,
2322 Right_Opnd => Lit)));
2324 -- For the array case, we insert the actions
2326 -- Result : Typ;
2328 -- System.Bit_Ops.Bit_Not
2329 -- (Opnd'Address,
2330 -- Typ'Length * Typ'Component_Size,
2331 -- Result'Address);
2333 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second argument
2334 -- is the length of the operand in bits. We then replace the expression
2335 -- with a reference to Result.
2337 else
2338 declare
2339 Result_Ent : constant Entity_Id := Make_Temporary (Loc, 'T');
2341 begin
2342 Insert_Actions (N, New_List (
2343 Make_Object_Declaration (Loc,
2344 Defining_Identifier => Result_Ent,
2345 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2347 Make_Procedure_Call_Statement (Loc,
2348 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2349 Parameter_Associations => New_List (
2350 Make_Byte_Aligned_Attribute_Reference (Loc,
2351 Prefix => Opnd,
2352 Attribute_Name => Name_Address),
2354 Make_Op_Multiply (Loc,
2355 Left_Opnd =>
2356 Make_Attribute_Reference (Loc,
2357 Prefix =>
2358 New_Occurrence_Of
2359 (Etype (First_Index (Rtyp)), Loc),
2360 Attribute_Name => Name_Range_Length),
2362 Right_Opnd =>
2363 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2365 Make_Byte_Aligned_Attribute_Reference (Loc,
2366 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2367 Attribute_Name => Name_Address)))));
2369 Rewrite (N, New_Occurrence_Of (Result_Ent, Loc));
2370 end;
2371 end if;
2373 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2374 end Expand_Packed_Not;
2376 -----------------------------
2377 -- Get_Base_And_Bit_Offset --
2378 -----------------------------
2380 procedure Get_Base_And_Bit_Offset
2381 (N : Node_Id;
2382 Base : out Node_Id;
2383 Offset : out Node_Id)
2385 Loc : Source_Ptr;
2386 Term : Node_Id;
2387 Atyp : Entity_Id;
2388 Subscr : Node_Id;
2390 begin
2391 Base := N;
2392 Offset := Empty;
2394 -- We build up an expression serially that has the form
2396 -- linear-subscript * component_size for each array reference
2397 -- + field'Bit_Position for each record field
2398 -- + ...
2400 loop
2401 Loc := Sloc (Base);
2403 if Nkind (Base) = N_Indexed_Component then
2404 Convert_To_Actual_Subtype (Prefix (Base));
2405 Atyp := Etype (Prefix (Base));
2406 Compute_Linear_Subscript (Atyp, Base, Subscr);
2408 Term :=
2409 Make_Op_Multiply (Loc,
2410 Left_Opnd => Subscr,
2411 Right_Opnd =>
2412 Make_Attribute_Reference (Loc,
2413 Prefix => New_Occurrence_Of (Atyp, Loc),
2414 Attribute_Name => Name_Component_Size));
2416 elsif Nkind (Base) = N_Selected_Component then
2417 Term :=
2418 Make_Attribute_Reference (Loc,
2419 Prefix => Selector_Name (Base),
2420 Attribute_Name => Name_Bit_Position);
2422 else
2423 return;
2424 end if;
2426 if No (Offset) then
2427 Offset := Term;
2429 else
2430 Offset :=
2431 Make_Op_Add (Loc,
2432 Left_Opnd => Offset,
2433 Right_Opnd => Term);
2434 end if;
2436 Base := Prefix (Base);
2437 end loop;
2438 end Get_Base_And_Bit_Offset;
2440 -------------------------------------
2441 -- Involves_Packed_Array_Reference --
2442 -------------------------------------
2444 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2445 begin
2446 if Nkind (N) = N_Indexed_Component
2447 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2448 then
2449 return True;
2451 elsif Nkind (N) = N_Selected_Component then
2452 return Involves_Packed_Array_Reference (Prefix (N));
2454 else
2455 return False;
2456 end if;
2457 end Involves_Packed_Array_Reference;
2459 --------------------------
2460 -- Known_Aligned_Enough --
2461 --------------------------
2463 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2464 Typ : constant Entity_Id := Etype (Obj);
2466 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2467 -- If the component is in a record that contains previous packed
2468 -- components, consider it unaligned because the back-end might
2469 -- choose to pack the rest of the record. Lead to less efficient code,
2470 -- but safer vis-a-vis of back-end choices.
2472 --------------------------------
2473 -- In_Partially_Packed_Record --
2474 --------------------------------
2476 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2477 Rec_Type : constant Entity_Id := Scope (Comp);
2478 Prev_Comp : Entity_Id;
2480 begin
2481 Prev_Comp := First_Entity (Rec_Type);
2482 while Present (Prev_Comp) loop
2483 if Is_Packed (Etype (Prev_Comp)) then
2484 return True;
2486 elsif Prev_Comp = Comp then
2487 return False;
2488 end if;
2490 Next_Entity (Prev_Comp);
2491 end loop;
2493 return False;
2494 end In_Partially_Packed_Record;
2496 -- Start of processing for Known_Aligned_Enough
2498 begin
2499 -- Odd bit sizes don't need alignment anyway
2501 if Csiz mod 2 = 1 then
2502 return True;
2504 -- If we have a specified alignment, see if it is sufficient, if not
2505 -- then we can't possibly be aligned enough in any case.
2507 elsif Known_Alignment (Etype (Obj)) then
2508 -- Alignment required is 4 if size is a multiple of 4, and
2509 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2511 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2512 return False;
2513 end if;
2514 end if;
2516 -- OK, alignment should be sufficient, if object is aligned
2518 -- If object is strictly aligned, then it is definitely aligned
2520 if Strict_Alignment (Typ) then
2521 return True;
2523 -- Case of subscripted array reference
2525 elsif Nkind (Obj) = N_Indexed_Component then
2527 -- If we have a pointer to an array, then this is definitely
2528 -- aligned, because pointers always point to aligned versions.
2530 if Is_Access_Type (Etype (Prefix (Obj))) then
2531 return True;
2533 -- Otherwise, go look at the prefix
2535 else
2536 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2537 end if;
2539 -- Case of record field
2541 elsif Nkind (Obj) = N_Selected_Component then
2543 -- What is significant here is whether the record type is packed
2545 if Is_Record_Type (Etype (Prefix (Obj)))
2546 and then Is_Packed (Etype (Prefix (Obj)))
2547 then
2548 return False;
2550 -- Or the component has a component clause which might cause
2551 -- the component to become unaligned (we can't tell if the
2552 -- backend is doing alignment computations).
2554 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2555 return False;
2557 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2558 return False;
2560 -- In all other cases, go look at prefix
2562 else
2563 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2564 end if;
2566 elsif Nkind (Obj) = N_Type_Conversion then
2567 return Known_Aligned_Enough (Expression (Obj), Csiz);
2569 -- For a formal parameter, it is safer to assume that it is not
2570 -- aligned, because the formal may be unconstrained while the actual
2571 -- is constrained. In this situation, a small constrained packed
2572 -- array, represented in modular form, may be unaligned.
2574 elsif Is_Entity_Name (Obj) then
2575 return not Is_Formal (Entity (Obj));
2576 else
2578 -- If none of the above, must be aligned
2579 return True;
2580 end if;
2581 end Known_Aligned_Enough;
2583 ---------------------
2584 -- Make_Shift_Left --
2585 ---------------------
2587 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2588 Nod : Node_Id;
2590 begin
2591 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2592 return N;
2593 else
2594 Nod :=
2595 Make_Op_Shift_Left (Sloc (N),
2596 Left_Opnd => N,
2597 Right_Opnd => S);
2598 Set_Shift_Count_OK (Nod, True);
2599 return Nod;
2600 end if;
2601 end Make_Shift_Left;
2603 ----------------------
2604 -- Make_Shift_Right --
2605 ----------------------
2607 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2608 Nod : Node_Id;
2610 begin
2611 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2612 return N;
2613 else
2614 Nod :=
2615 Make_Op_Shift_Right (Sloc (N),
2616 Left_Opnd => N,
2617 Right_Opnd => S);
2618 Set_Shift_Count_OK (Nod, True);
2619 return Nod;
2620 end if;
2621 end Make_Shift_Right;
2623 -----------------------------
2624 -- RJ_Unchecked_Convert_To --
2625 -----------------------------
2627 function RJ_Unchecked_Convert_To
2628 (Typ : Entity_Id;
2629 Expr : Node_Id) return Node_Id
2631 Source_Typ : constant Entity_Id := Etype (Expr);
2632 Target_Typ : constant Entity_Id := Typ;
2634 Src : Node_Id := Expr;
2636 Source_Siz : Nat;
2637 Target_Siz : Nat;
2639 begin
2640 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2641 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2643 -- For a little-endian target type stored byte-swapped on a
2644 -- big-endian machine, do not mask to Target_Siz bits.
2646 if Bytes_Big_Endian
2647 and then (Is_Record_Type (Target_Typ)
2648 or else
2649 Is_Array_Type (Target_Typ))
2650 and then Reverse_Storage_Order (Target_Typ)
2651 then
2652 Source_Siz := Target_Siz;
2653 end if;
2655 -- First step, if the source type is not a discrete type, then we first
2656 -- convert to a modular type of the source length, since otherwise, on
2657 -- a big-endian machine, we get left-justification. We do it for little-
2658 -- endian machines as well, because there might be junk bits that are
2659 -- not cleared if the type is not numeric.
2661 if Source_Siz /= Target_Siz
2662 and then not Is_Discrete_Type (Source_Typ)
2663 then
2664 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2665 end if;
2667 -- In the big endian case, if the lengths of the two types differ, then
2668 -- we must worry about possible left justification in the conversion,
2669 -- and avoiding that is what this is all about.
2671 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2673 -- Next step. If the target is not a discrete type, then we first
2674 -- convert to a modular type of the target length, since otherwise,
2675 -- on a big-endian machine, we get left-justification.
2677 if not Is_Discrete_Type (Target_Typ) then
2678 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2679 end if;
2680 end if;
2682 -- And now we can do the final conversion to the target type
2684 return Unchecked_Convert_To (Target_Typ, Src);
2685 end RJ_Unchecked_Convert_To;
2687 ----------------------------------------------
2688 -- Setup_Enumeration_Packed_Array_Reference --
2689 ----------------------------------------------
2691 -- All we have to do here is to find the subscripts that correspond to the
2692 -- index positions that have non-standard enumeration types and insert a
2693 -- Pos attribute to get the proper subscript value.
2695 -- Finally the prefix must be uncheck-converted to the corresponding packed
2696 -- array type.
2698 -- Note that the component type is unchanged, so we do not need to fiddle
2699 -- with the types (Gigi always automatically takes the packed array type if
2700 -- it is set, as it will be in this case).
2702 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2703 Pfx : constant Node_Id := Prefix (N);
2704 Typ : constant Entity_Id := Etype (N);
2705 Exprs : constant List_Id := Expressions (N);
2706 Expr : Node_Id;
2708 begin
2709 -- If the array is unconstrained, then we replace the array reference
2710 -- with its actual subtype. This actual subtype will have a packed array
2711 -- type with appropriate bounds.
2713 if not Is_Constrained (Packed_Array_Impl_Type (Etype (Pfx))) then
2714 Convert_To_Actual_Subtype (Pfx);
2715 end if;
2717 Expr := First (Exprs);
2718 while Present (Expr) loop
2719 declare
2720 Loc : constant Source_Ptr := Sloc (Expr);
2721 Expr_Typ : constant Entity_Id := Etype (Expr);
2723 begin
2724 if Is_Enumeration_Type (Expr_Typ)
2725 and then Has_Non_Standard_Rep (Expr_Typ)
2726 then
2727 Rewrite (Expr,
2728 Make_Attribute_Reference (Loc,
2729 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2730 Attribute_Name => Name_Pos,
2731 Expressions => New_List (Relocate_Node (Expr))));
2732 Analyze_And_Resolve (Expr, Standard_Natural);
2733 end if;
2734 end;
2736 Next (Expr);
2737 end loop;
2739 Rewrite (N,
2740 Make_Indexed_Component (Sloc (N),
2741 Prefix =>
2742 Unchecked_Convert_To (Packed_Array_Impl_Type (Etype (Pfx)), Pfx),
2743 Expressions => Exprs));
2745 Analyze_And_Resolve (N, Typ);
2746 end Setup_Enumeration_Packed_Array_Reference;
2748 -----------------------------------------
2749 -- Setup_Inline_Packed_Array_Reference --
2750 -----------------------------------------
2752 procedure Setup_Inline_Packed_Array_Reference
2753 (N : Node_Id;
2754 Atyp : Entity_Id;
2755 Obj : in out Node_Id;
2756 Cmask : out Uint;
2757 Shift : out Node_Id)
2759 Loc : constant Source_Ptr := Sloc (N);
2760 PAT : Entity_Id;
2761 Otyp : Entity_Id;
2762 Csiz : Uint;
2763 Osiz : Uint;
2765 begin
2766 Csiz := Component_Size (Atyp);
2768 Convert_To_PAT_Type (Obj);
2769 PAT := Etype (Obj);
2771 Cmask := 2 ** Csiz - 1;
2773 if Is_Array_Type (PAT) then
2774 Otyp := Component_Type (PAT);
2775 Osiz := Component_Size (PAT);
2777 else
2778 Otyp := PAT;
2780 -- In the case where the PAT is a modular type, we want the actual
2781 -- size in bits of the modular value we use. This is neither the
2782 -- Object_Size nor the Value_Size, either of which may have been
2783 -- reset to strange values, but rather the minimum size. Note that
2784 -- since this is a modular type with full range, the issue of
2785 -- biased representation does not arise.
2787 Osiz := UI_From_Int (Minimum_Size (Otyp));
2788 end if;
2790 Compute_Linear_Subscript (Atyp, N, Shift);
2792 -- If the component size is not 1, then the subscript must be multiplied
2793 -- by the component size to get the shift count.
2795 if Csiz /= 1 then
2796 Shift :=
2797 Make_Op_Multiply (Loc,
2798 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2799 Right_Opnd => Shift);
2800 end if;
2802 -- If we have the array case, then this shift count must be broken down
2803 -- into a byte subscript, and a shift within the byte.
2805 if Is_Array_Type (PAT) then
2807 declare
2808 New_Shift : Node_Id;
2810 begin
2811 -- We must analyze shift, since we will duplicate it
2813 Set_Parent (Shift, N);
2814 Analyze_And_Resolve
2815 (Shift, Standard_Integer, Suppress => All_Checks);
2817 -- The shift count within the word is
2818 -- shift mod Osiz
2820 New_Shift :=
2821 Make_Op_Mod (Loc,
2822 Left_Opnd => Duplicate_Subexpr (Shift),
2823 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2825 -- The subscript to be used on the PAT array is
2826 -- shift / Osiz
2828 Obj :=
2829 Make_Indexed_Component (Loc,
2830 Prefix => Obj,
2831 Expressions => New_List (
2832 Make_Op_Divide (Loc,
2833 Left_Opnd => Duplicate_Subexpr (Shift),
2834 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2836 Shift := New_Shift;
2837 end;
2839 -- For the modular integer case, the object to be manipulated is the
2840 -- entire array, so Obj is unchanged. Note that we will reset its type
2841 -- to PAT before returning to the caller.
2843 else
2844 null;
2845 end if;
2847 -- The one remaining step is to modify the shift count for the
2848 -- big-endian case. Consider the following example in a byte:
2850 -- xxxxxxxx bits of byte
2851 -- vvvvvvvv bits of value
2852 -- 33221100 little-endian numbering
2853 -- 00112233 big-endian numbering
2855 -- Here we have the case of 2-bit fields
2857 -- For the little-endian case, we already have the proper shift count
2858 -- set, e.g. for element 2, the shift count is 2*2 = 4.
2860 -- For the big endian case, we have to adjust the shift count, computing
2861 -- it as (N - F) - Shift, where N is the number of bits in an element of
2862 -- the array used to implement the packed array, F is the number of bits
2863 -- in a source array element, and Shift is the count so far computed.
2865 -- We also have to adjust if the storage order is reversed
2867 if Bytes_Big_Endian xor Reverse_Storage_Order (Base_Type (Atyp)) then
2868 Shift :=
2869 Make_Op_Subtract (Loc,
2870 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2871 Right_Opnd => Shift);
2872 end if;
2874 Set_Parent (Shift, N);
2875 Set_Parent (Obj, N);
2876 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2877 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2879 -- Make sure final type of object is the appropriate packed type
2881 Set_Etype (Obj, Otyp);
2883 end Setup_Inline_Packed_Array_Reference;
2885 end Exp_Pakd;