Daily bump.
[official-gcc.git] / gcc / ada / exp_pakd.adb
blobf3ead191c965b80f0c05a2e900f6d437d0e72b7d
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-2007, 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 Rtsfind; use Rtsfind;
37 with Sem; use Sem;
38 with Sem_Ch3; use Sem_Ch3;
39 with Sem_Ch8; use Sem_Ch8;
40 with Sem_Ch13; use Sem_Ch13;
41 with Sem_Eval; use Sem_Eval;
42 with Sem_Res; use Sem_Res;
43 with Sem_Util; use Sem_Util;
44 with Sinfo; use Sinfo;
45 with Snames; use Snames;
46 with Stand; use Stand;
47 with Targparm; use Targparm;
48 with Tbuild; use Tbuild;
49 with Ttypes; use Ttypes;
50 with Uintp; use Uintp;
52 package body Exp_Pakd is
54 ---------------------------
55 -- Endian Considerations --
56 ---------------------------
58 -- As described in the specification, bit numbering in a packed array
59 -- is consistent with bit numbering in a record representation clause,
60 -- and hence dependent on the endianness of the machine:
62 -- For little-endian machines, element zero is at the right hand end
63 -- (low order end) of a bit field.
65 -- For big-endian machines, element zero is at the left hand end
66 -- (high order end) of a bit field.
68 -- The shifts that are used to right justify a field therefore differ
69 -- in the two cases. For the little-endian case, we can simply use the
70 -- bit number (i.e. the element number * element size) as the count for
71 -- a right shift. For the big-endian case, we have to subtract the shift
72 -- count from an appropriate constant to use in the right shift. We use
73 -- rotates instead of shifts (which is necessary in the store case to
74 -- preserve other fields), and we expect that the backend will be able
75 -- to change the right rotate into a left rotate, avoiding the subtract,
76 -- if the architecture provides such an instruction.
78 ----------------------------------------------
79 -- Entity Tables for Packed Access Routines --
80 ----------------------------------------------
82 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
83 -- library routines. This table is used to obtain the entity for the
84 -- proper routine.
86 type E_Array is array (Int range 01 .. 63) of RE_Id;
88 -- Array of Bits_nn entities. Note that we do not use library routines
89 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
90 -- entries from System.Unsigned, because we also use this table for
91 -- certain special unchecked conversions in the big-endian case.
93 Bits_Id : constant E_Array :=
94 (01 => RE_Bits_1,
95 02 => RE_Bits_2,
96 03 => RE_Bits_03,
97 04 => RE_Bits_4,
98 05 => RE_Bits_05,
99 06 => RE_Bits_06,
100 07 => RE_Bits_07,
101 08 => RE_Unsigned_8,
102 09 => RE_Bits_09,
103 10 => RE_Bits_10,
104 11 => RE_Bits_11,
105 12 => RE_Bits_12,
106 13 => RE_Bits_13,
107 14 => RE_Bits_14,
108 15 => RE_Bits_15,
109 16 => RE_Unsigned_16,
110 17 => RE_Bits_17,
111 18 => RE_Bits_18,
112 19 => RE_Bits_19,
113 20 => RE_Bits_20,
114 21 => RE_Bits_21,
115 22 => RE_Bits_22,
116 23 => RE_Bits_23,
117 24 => RE_Bits_24,
118 25 => RE_Bits_25,
119 26 => RE_Bits_26,
120 27 => RE_Bits_27,
121 28 => RE_Bits_28,
122 29 => RE_Bits_29,
123 30 => RE_Bits_30,
124 31 => RE_Bits_31,
125 32 => RE_Unsigned_32,
126 33 => RE_Bits_33,
127 34 => RE_Bits_34,
128 35 => RE_Bits_35,
129 36 => RE_Bits_36,
130 37 => RE_Bits_37,
131 38 => RE_Bits_38,
132 39 => RE_Bits_39,
133 40 => RE_Bits_40,
134 41 => RE_Bits_41,
135 42 => RE_Bits_42,
136 43 => RE_Bits_43,
137 44 => RE_Bits_44,
138 45 => RE_Bits_45,
139 46 => RE_Bits_46,
140 47 => RE_Bits_47,
141 48 => RE_Bits_48,
142 49 => RE_Bits_49,
143 50 => RE_Bits_50,
144 51 => RE_Bits_51,
145 52 => RE_Bits_52,
146 53 => RE_Bits_53,
147 54 => RE_Bits_54,
148 55 => RE_Bits_55,
149 56 => RE_Bits_56,
150 57 => RE_Bits_57,
151 58 => RE_Bits_58,
152 59 => RE_Bits_59,
153 60 => RE_Bits_60,
154 61 => RE_Bits_61,
155 62 => RE_Bits_62,
156 63 => RE_Bits_63);
158 -- Array of Get routine entities. These are used to obtain an element
159 -- from a packed array. The N'th entry is used to obtain elements from
160 -- a packed array whose component size is N. RE_Null is used as a null
161 -- entry, for the cases where a library routine is not used.
163 Get_Id : constant E_Array :=
164 (01 => RE_Null,
165 02 => RE_Null,
166 03 => RE_Get_03,
167 04 => RE_Null,
168 05 => RE_Get_05,
169 06 => RE_Get_06,
170 07 => RE_Get_07,
171 08 => RE_Null,
172 09 => RE_Get_09,
173 10 => RE_Get_10,
174 11 => RE_Get_11,
175 12 => RE_Get_12,
176 13 => RE_Get_13,
177 14 => RE_Get_14,
178 15 => RE_Get_15,
179 16 => RE_Null,
180 17 => RE_Get_17,
181 18 => RE_Get_18,
182 19 => RE_Get_19,
183 20 => RE_Get_20,
184 21 => RE_Get_21,
185 22 => RE_Get_22,
186 23 => RE_Get_23,
187 24 => RE_Get_24,
188 25 => RE_Get_25,
189 26 => RE_Get_26,
190 27 => RE_Get_27,
191 28 => RE_Get_28,
192 29 => RE_Get_29,
193 30 => RE_Get_30,
194 31 => RE_Get_31,
195 32 => RE_Null,
196 33 => RE_Get_33,
197 34 => RE_Get_34,
198 35 => RE_Get_35,
199 36 => RE_Get_36,
200 37 => RE_Get_37,
201 38 => RE_Get_38,
202 39 => RE_Get_39,
203 40 => RE_Get_40,
204 41 => RE_Get_41,
205 42 => RE_Get_42,
206 43 => RE_Get_43,
207 44 => RE_Get_44,
208 45 => RE_Get_45,
209 46 => RE_Get_46,
210 47 => RE_Get_47,
211 48 => RE_Get_48,
212 49 => RE_Get_49,
213 50 => RE_Get_50,
214 51 => RE_Get_51,
215 52 => RE_Get_52,
216 53 => RE_Get_53,
217 54 => RE_Get_54,
218 55 => RE_Get_55,
219 56 => RE_Get_56,
220 57 => RE_Get_57,
221 58 => RE_Get_58,
222 59 => RE_Get_59,
223 60 => RE_Get_60,
224 61 => RE_Get_61,
225 62 => RE_Get_62,
226 63 => RE_Get_63);
228 -- Array of Get routine entities to be used in the case where the packed
229 -- array is itself a component of a packed structure, and therefore may
230 -- not be fully aligned. This only affects the even sizes, since for the
231 -- odd sizes, we do not get any fixed alignment in any case.
233 GetU_Id : constant E_Array :=
234 (01 => RE_Null,
235 02 => RE_Null,
236 03 => RE_Get_03,
237 04 => RE_Null,
238 05 => RE_Get_05,
239 06 => RE_GetU_06,
240 07 => RE_Get_07,
241 08 => RE_Null,
242 09 => RE_Get_09,
243 10 => RE_GetU_10,
244 11 => RE_Get_11,
245 12 => RE_GetU_12,
246 13 => RE_Get_13,
247 14 => RE_GetU_14,
248 15 => RE_Get_15,
249 16 => RE_Null,
250 17 => RE_Get_17,
251 18 => RE_GetU_18,
252 19 => RE_Get_19,
253 20 => RE_GetU_20,
254 21 => RE_Get_21,
255 22 => RE_GetU_22,
256 23 => RE_Get_23,
257 24 => RE_GetU_24,
258 25 => RE_Get_25,
259 26 => RE_GetU_26,
260 27 => RE_Get_27,
261 28 => RE_GetU_28,
262 29 => RE_Get_29,
263 30 => RE_GetU_30,
264 31 => RE_Get_31,
265 32 => RE_Null,
266 33 => RE_Get_33,
267 34 => RE_GetU_34,
268 35 => RE_Get_35,
269 36 => RE_GetU_36,
270 37 => RE_Get_37,
271 38 => RE_GetU_38,
272 39 => RE_Get_39,
273 40 => RE_GetU_40,
274 41 => RE_Get_41,
275 42 => RE_GetU_42,
276 43 => RE_Get_43,
277 44 => RE_GetU_44,
278 45 => RE_Get_45,
279 46 => RE_GetU_46,
280 47 => RE_Get_47,
281 48 => RE_GetU_48,
282 49 => RE_Get_49,
283 50 => RE_GetU_50,
284 51 => RE_Get_51,
285 52 => RE_GetU_52,
286 53 => RE_Get_53,
287 54 => RE_GetU_54,
288 55 => RE_Get_55,
289 56 => RE_GetU_56,
290 57 => RE_Get_57,
291 58 => RE_GetU_58,
292 59 => RE_Get_59,
293 60 => RE_GetU_60,
294 61 => RE_Get_61,
295 62 => RE_GetU_62,
296 63 => RE_Get_63);
298 -- Array of Set routine entities. These are used to assign an element
299 -- of a packed array. The N'th entry is used to assign elements for
300 -- a packed array whose component size is N. RE_Null is used as a null
301 -- entry, for the cases where a library routine is not used.
303 Set_Id : constant E_Array :=
304 (01 => RE_Null,
305 02 => RE_Null,
306 03 => RE_Set_03,
307 04 => RE_Null,
308 05 => RE_Set_05,
309 06 => RE_Set_06,
310 07 => RE_Set_07,
311 08 => RE_Null,
312 09 => RE_Set_09,
313 10 => RE_Set_10,
314 11 => RE_Set_11,
315 12 => RE_Set_12,
316 13 => RE_Set_13,
317 14 => RE_Set_14,
318 15 => RE_Set_15,
319 16 => RE_Null,
320 17 => RE_Set_17,
321 18 => RE_Set_18,
322 19 => RE_Set_19,
323 20 => RE_Set_20,
324 21 => RE_Set_21,
325 22 => RE_Set_22,
326 23 => RE_Set_23,
327 24 => RE_Set_24,
328 25 => RE_Set_25,
329 26 => RE_Set_26,
330 27 => RE_Set_27,
331 28 => RE_Set_28,
332 29 => RE_Set_29,
333 30 => RE_Set_30,
334 31 => RE_Set_31,
335 32 => RE_Null,
336 33 => RE_Set_33,
337 34 => RE_Set_34,
338 35 => RE_Set_35,
339 36 => RE_Set_36,
340 37 => RE_Set_37,
341 38 => RE_Set_38,
342 39 => RE_Set_39,
343 40 => RE_Set_40,
344 41 => RE_Set_41,
345 42 => RE_Set_42,
346 43 => RE_Set_43,
347 44 => RE_Set_44,
348 45 => RE_Set_45,
349 46 => RE_Set_46,
350 47 => RE_Set_47,
351 48 => RE_Set_48,
352 49 => RE_Set_49,
353 50 => RE_Set_50,
354 51 => RE_Set_51,
355 52 => RE_Set_52,
356 53 => RE_Set_53,
357 54 => RE_Set_54,
358 55 => RE_Set_55,
359 56 => RE_Set_56,
360 57 => RE_Set_57,
361 58 => RE_Set_58,
362 59 => RE_Set_59,
363 60 => RE_Set_60,
364 61 => RE_Set_61,
365 62 => RE_Set_62,
366 63 => RE_Set_63);
368 -- Array of Set routine entities to be used in the case where the packed
369 -- array is itself a component of a packed structure, and therefore may
370 -- not be fully aligned. This only affects the even sizes, since for the
371 -- odd sizes, we do not get any fixed alignment in any case.
373 SetU_Id : constant E_Array :=
374 (01 => RE_Null,
375 02 => RE_Null,
376 03 => RE_Set_03,
377 04 => RE_Null,
378 05 => RE_Set_05,
379 06 => RE_SetU_06,
380 07 => RE_Set_07,
381 08 => RE_Null,
382 09 => RE_Set_09,
383 10 => RE_SetU_10,
384 11 => RE_Set_11,
385 12 => RE_SetU_12,
386 13 => RE_Set_13,
387 14 => RE_SetU_14,
388 15 => RE_Set_15,
389 16 => RE_Null,
390 17 => RE_Set_17,
391 18 => RE_SetU_18,
392 19 => RE_Set_19,
393 20 => RE_SetU_20,
394 21 => RE_Set_21,
395 22 => RE_SetU_22,
396 23 => RE_Set_23,
397 24 => RE_SetU_24,
398 25 => RE_Set_25,
399 26 => RE_SetU_26,
400 27 => RE_Set_27,
401 28 => RE_SetU_28,
402 29 => RE_Set_29,
403 30 => RE_SetU_30,
404 31 => RE_Set_31,
405 32 => RE_Null,
406 33 => RE_Set_33,
407 34 => RE_SetU_34,
408 35 => RE_Set_35,
409 36 => RE_SetU_36,
410 37 => RE_Set_37,
411 38 => RE_SetU_38,
412 39 => RE_Set_39,
413 40 => RE_SetU_40,
414 41 => RE_Set_41,
415 42 => RE_SetU_42,
416 43 => RE_Set_43,
417 44 => RE_SetU_44,
418 45 => RE_Set_45,
419 46 => RE_SetU_46,
420 47 => RE_Set_47,
421 48 => RE_SetU_48,
422 49 => RE_Set_49,
423 50 => RE_SetU_50,
424 51 => RE_Set_51,
425 52 => RE_SetU_52,
426 53 => RE_Set_53,
427 54 => RE_SetU_54,
428 55 => RE_Set_55,
429 56 => RE_SetU_56,
430 57 => RE_Set_57,
431 58 => RE_SetU_58,
432 59 => RE_Set_59,
433 60 => RE_SetU_60,
434 61 => RE_Set_61,
435 62 => RE_SetU_62,
436 63 => RE_Set_63);
438 -----------------------
439 -- Local Subprograms --
440 -----------------------
442 procedure Compute_Linear_Subscript
443 (Atyp : Entity_Id;
444 N : Node_Id;
445 Subscr : out Node_Id);
446 -- Given a constrained array type Atyp, and an indexed component node
447 -- N referencing an array object of this type, build an expression of
448 -- type Standard.Integer representing the zero-based linear subscript
449 -- value. This expression includes any required range checks.
451 procedure Convert_To_PAT_Type (Aexp : Node_Id);
452 -- Given an expression of a packed array type, builds a corresponding
453 -- expression whose type is the implementation type used to represent
454 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
456 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
457 -- There are two versions of the Set routines, the ones used when the
458 -- object is known to be sufficiently well aligned given the number of
459 -- bits, and the ones used when the object is not known to be aligned.
460 -- This routine is used to determine which set to use. Obj is a reference
461 -- to the object, and Csiz is the component size of the packed array.
462 -- True is returned if the alignment of object is known to be sufficient,
463 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
464 -- 2 otherwise.
466 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
467 -- Build a left shift node, checking for the case of a shift count of zero
469 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
470 -- Build a right shift node, checking for the case of a shift count of zero
472 function RJ_Unchecked_Convert_To
473 (Typ : Entity_Id;
474 Expr : Node_Id) return Node_Id;
475 -- The packed array code does unchecked conversions which in some cases
476 -- may involve non-discrete types with differing sizes. The semantics of
477 -- such conversions is potentially endian dependent, and the effect we
478 -- want here for such a conversion is to do the conversion in size as
479 -- though numeric items are involved, and we extend or truncate on the
480 -- left side. This happens naturally in the little-endian case, but in
481 -- the big endian case we can get left justification, when what we want
482 -- is right justification. This routine does the unchecked conversion in
483 -- a stepwise manner to ensure that it gives the expected result. Hence
484 -- the name (RJ = Right justified). The parameters Typ and Expr are as
485 -- for the case of a normal Unchecked_Convert_To call.
487 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
488 -- This routine is called in the Get and Set case for arrays that are
489 -- packed but not bit-packed, meaning that they have at least one
490 -- subscript that is of an enumeration type with a non-standard
491 -- representation. This routine modifies the given node to properly
492 -- reference the corresponding packed array type.
494 procedure Setup_Inline_Packed_Array_Reference
495 (N : Node_Id;
496 Atyp : Entity_Id;
497 Obj : in out Node_Id;
498 Cmask : out Uint;
499 Shift : out Node_Id);
500 -- This procedure performs common processing on the N_Indexed_Component
501 -- parameter given as N, whose prefix is a reference to a packed array.
502 -- This is used for the get and set when the component size is 1,2,4
503 -- or for other component sizes when the packed array type is a modular
504 -- type (i.e. the cases that are handled with inline code).
506 -- On entry:
508 -- N is the N_Indexed_Component node for the packed array reference
510 -- Atyp is the constrained array type (the actual subtype has been
511 -- computed if necessary to obtain the constraints, but this is still
512 -- the original array type, not the Packed_Array_Type value).
514 -- Obj is the object which is to be indexed. It is always of type Atyp.
516 -- On return:
518 -- Obj is the object containing the desired bit field. It is of type
519 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
520 -- entire value, for the small static case, or the proper selected byte
521 -- from the array in the large or dynamic case. This node is analyzed
522 -- and resolved on return.
524 -- Shift is a node representing the shift count to be used in the
525 -- rotate right instruction that positions the field for access.
526 -- This node is analyzed and resolved on return.
528 -- Cmask is a mask corresponding to the width of the component field.
529 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
531 -- Note: in some cases the call to this routine may generate actions
532 -- (for handling multi-use references and the generation of the packed
533 -- array type on the fly). Such actions are inserted into the tree
534 -- directly using Insert_Action.
536 ------------------------------
537 -- Compute_Linear_Subcsript --
538 ------------------------------
540 procedure Compute_Linear_Subscript
541 (Atyp : Entity_Id;
542 N : Node_Id;
543 Subscr : out Node_Id)
545 Loc : constant Source_Ptr := Sloc (N);
546 Oldsub : Node_Id;
547 Newsub : Node_Id;
548 Indx : Node_Id;
549 Styp : Entity_Id;
551 begin
552 Subscr := Empty;
554 -- Loop through dimensions
556 Indx := First_Index (Atyp);
557 Oldsub := First (Expressions (N));
559 while Present (Indx) loop
560 Styp := Etype (Indx);
561 Newsub := Relocate_Node (Oldsub);
563 -- Get expression for the subscript value. First, if Do_Range_Check
564 -- is set on a subscript, then we must do a range check against the
565 -- original bounds (not the bounds of the packed array type). We do
566 -- this by introducing a subtype conversion.
568 if Do_Range_Check (Newsub)
569 and then Etype (Newsub) /= Styp
570 then
571 Newsub := Convert_To (Styp, Newsub);
572 end if;
574 -- Now evolve the expression for the subscript. First convert
575 -- the subscript to be zero based and of an integer type.
577 -- Case of integer type, where we just subtract to get lower bound
579 if Is_Integer_Type (Styp) then
581 -- If length of integer type is smaller than standard integer,
582 -- then we convert to integer first, then do the subtract
584 -- Integer (subscript) - Integer (Styp'First)
586 if Esize (Styp) < Esize (Standard_Integer) then
587 Newsub :=
588 Make_Op_Subtract (Loc,
589 Left_Opnd => Convert_To (Standard_Integer, Newsub),
590 Right_Opnd =>
591 Convert_To (Standard_Integer,
592 Make_Attribute_Reference (Loc,
593 Prefix => New_Occurrence_Of (Styp, Loc),
594 Attribute_Name => Name_First)));
596 -- For larger integer types, subtract first, then convert to
597 -- integer, this deals with strange long long integer bounds.
599 -- Integer (subscript - Styp'First)
601 else
602 Newsub :=
603 Convert_To (Standard_Integer,
604 Make_Op_Subtract (Loc,
605 Left_Opnd => Newsub,
606 Right_Opnd =>
607 Make_Attribute_Reference (Loc,
608 Prefix => New_Occurrence_Of (Styp, Loc),
609 Attribute_Name => Name_First)));
610 end if;
612 -- For the enumeration case, we have to use 'Pos to get the value
613 -- to work with before subtracting the lower bound.
615 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
617 -- This is not quite right for bizarre cases where the size of the
618 -- enumeration type is > Integer'Size bits due to rep clause ???
620 else
621 pragma Assert (Is_Enumeration_Type (Styp));
623 Newsub :=
624 Make_Op_Subtract (Loc,
625 Left_Opnd => Convert_To (Standard_Integer,
626 Make_Attribute_Reference (Loc,
627 Prefix => New_Occurrence_Of (Styp, Loc),
628 Attribute_Name => Name_Pos,
629 Expressions => New_List (Newsub))),
631 Right_Opnd =>
632 Convert_To (Standard_Integer,
633 Make_Attribute_Reference (Loc,
634 Prefix => New_Occurrence_Of (Styp, Loc),
635 Attribute_Name => Name_Pos,
636 Expressions => New_List (
637 Make_Attribute_Reference (Loc,
638 Prefix => New_Occurrence_Of (Styp, Loc),
639 Attribute_Name => Name_First)))));
640 end if;
642 Set_Paren_Count (Newsub, 1);
644 -- For the first subscript, we just copy that subscript value
646 if No (Subscr) then
647 Subscr := Newsub;
649 -- Otherwise, we must multiply what we already have by the current
650 -- stride and then add in the new value to the evolving subscript.
652 else
653 Subscr :=
654 Make_Op_Add (Loc,
655 Left_Opnd =>
656 Make_Op_Multiply (Loc,
657 Left_Opnd => Subscr,
658 Right_Opnd =>
659 Make_Attribute_Reference (Loc,
660 Attribute_Name => Name_Range_Length,
661 Prefix => New_Occurrence_Of (Styp, Loc))),
662 Right_Opnd => Newsub);
663 end if;
665 -- Move to next subscript
667 Next_Index (Indx);
668 Next (Oldsub);
669 end loop;
670 end Compute_Linear_Subscript;
672 -------------------------
673 -- Convert_To_PAT_Type --
674 -------------------------
676 -- The PAT is always obtained from the actual subtype
678 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
679 Act_ST : Entity_Id;
681 begin
682 Convert_To_Actual_Subtype (Aexp);
683 Act_ST := Underlying_Type (Etype (Aexp));
684 Create_Packed_Array_Type (Act_ST);
686 -- Just replace the etype with the packed array type. This works because
687 -- the expression will not be further analyzed, and Gigi considers the
688 -- two types equivalent in any case.
690 -- This is not strictly the case ??? If the reference is an actual in
691 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
692 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
693 -- array reference, reanalysis can produce spurious type errors when the
694 -- PAT type is replaced again with the original type of the array. Same
695 -- for the case of a dereference. The following is correct and minimal,
696 -- but the handling of more complex packed expressions in actuals is
697 -- confused. Probably the problem only remains for actuals in calls.
699 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
701 if Is_Entity_Name (Aexp)
702 or else
703 (Nkind (Aexp) = N_Indexed_Component
704 and then Is_Entity_Name (Prefix (Aexp)))
705 or else Nkind (Aexp) = N_Explicit_Dereference
706 then
707 Set_Analyzed (Aexp);
708 end if;
709 end Convert_To_PAT_Type;
711 ------------------------------
712 -- Create_Packed_Array_Type --
713 ------------------------------
715 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
716 Loc : constant Source_Ptr := Sloc (Typ);
717 Ctyp : constant Entity_Id := Component_Type (Typ);
718 Csize : constant Uint := Component_Size (Typ);
720 Ancest : Entity_Id;
721 PB_Type : Entity_Id;
722 PASize : Uint;
723 Decl : Node_Id;
724 PAT : Entity_Id;
725 Len_Dim : Node_Id;
726 Len_Expr : Node_Id;
727 Len_Bits : Uint;
728 Bits_U1 : Node_Id;
729 PAT_High : Node_Id;
730 Btyp : Entity_Id;
731 Lit : Node_Id;
733 procedure Install_PAT;
734 -- This procedure is called with Decl set to the declaration for the
735 -- packed array type. It creates the type and installs it as required.
737 procedure Set_PB_Type;
738 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
739 -- requirements (see documentation in the spec of this package).
741 -----------------
742 -- Install_PAT --
743 -----------------
745 procedure Install_PAT is
746 Pushed_Scope : Boolean := False;
748 begin
749 -- We do not want to put the declaration we have created in the tree
750 -- since it is often hard, and sometimes impossible to find a proper
751 -- place for it (the impossible case arises for a packed array type
752 -- with bounds depending on the discriminant, a declaration cannot
753 -- be put inside the record, and the reference to the discriminant
754 -- cannot be outside the record).
756 -- The solution is to analyze the declaration while temporarily
757 -- attached to the tree at an appropriate point, and then we install
758 -- the resulting type as an Itype in the packed array type field of
759 -- the original type, so that no explicit declaration is required.
761 -- Note: the packed type is created in the scope of its parent
762 -- type. There are at least some cases where the current scope
763 -- is deeper, and so when this is the case, we temporarily reset
764 -- the scope for the definition. This is clearly safe, since the
765 -- first use of the packed array type will be the implicit
766 -- reference from the corresponding unpacked type when it is
767 -- elaborated.
769 if Is_Itype (Typ) then
770 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
771 else
772 Set_Parent (Decl, Declaration_Node (Typ));
773 end if;
775 if Scope (Typ) /= Current_Scope then
776 Push_Scope (Scope (Typ));
777 Pushed_Scope := True;
778 end if;
780 Set_Is_Itype (PAT, True);
781 Set_Packed_Array_Type (Typ, PAT);
782 Analyze (Decl, Suppress => All_Checks);
784 if Pushed_Scope then
785 Pop_Scope;
786 end if;
788 -- Set Esize and RM_Size to the actual size of the packed object
789 -- Do not reset RM_Size if already set, as happens in the case of
790 -- a modular type.
792 if Unknown_Esize (PAT) then
793 Set_Esize (PAT, PASize);
794 end if;
796 if Unknown_RM_Size (PAT) then
797 Set_RM_Size (PAT, PASize);
798 end if;
800 Adjust_Esize_Alignment (PAT);
802 -- Set remaining fields of packed array type
804 Init_Alignment (PAT);
805 Set_Parent (PAT, Empty);
806 Set_Associated_Node_For_Itype (PAT, Typ);
807 Set_Is_Packed_Array_Type (PAT, True);
808 Set_Original_Array_Type (PAT, Typ);
810 -- We definitely do not want to delay freezing for packed array
811 -- types. This is of particular importance for the itypes that
812 -- are generated for record components depending on discriminants
813 -- where there is no place to put the freeze node.
815 Set_Has_Delayed_Freeze (PAT, False);
816 Set_Has_Delayed_Freeze (Etype (PAT), False);
818 -- If we did allocate a freeze node, then clear out the reference
819 -- since it is obsolete (should we delete the freeze node???)
821 Set_Freeze_Node (PAT, Empty);
822 Set_Freeze_Node (Etype (PAT), Empty);
823 end Install_PAT;
825 -----------------
826 -- Set_PB_Type --
827 -----------------
829 procedure Set_PB_Type is
830 begin
831 -- If the user has specified an explicit alignment for the
832 -- type or component, take it into account.
834 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
835 or else Alignment (Typ) = 1
836 or else Component_Alignment (Typ) = Calign_Storage_Unit
837 then
838 PB_Type := RTE (RE_Packed_Bytes1);
840 elsif Csize mod 4 /= 0
841 or else Alignment (Typ) = 2
842 then
843 PB_Type := RTE (RE_Packed_Bytes2);
845 else
846 PB_Type := RTE (RE_Packed_Bytes4);
847 end if;
848 end Set_PB_Type;
850 -- Start of processing for Create_Packed_Array_Type
852 begin
853 -- If we already have a packed array type, nothing to do
855 if Present (Packed_Array_Type (Typ)) then
856 return;
857 end if;
859 -- If our immediate ancestor subtype is constrained, and it already
860 -- has a packed array type, then just share the same type, since the
861 -- bounds must be the same. If the ancestor is not an array type but
862 -- a private type, as can happen with multiple instantiations, create
863 -- a new packed type, to avoid privacy issues.
865 if Ekind (Typ) = E_Array_Subtype then
866 Ancest := Ancestor_Subtype (Typ);
868 if Present (Ancest)
869 and then Is_Array_Type (Ancest)
870 and then Is_Constrained (Ancest)
871 and then Present (Packed_Array_Type (Ancest))
872 then
873 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
874 return;
875 end if;
876 end if;
878 -- We preset the result type size from the size of the original array
879 -- type, since this size clearly belongs to the packed array type. The
880 -- size of the conceptual unpacked type is always set to unknown.
882 PASize := RM_Size (Typ);
884 -- Case of an array where at least one index is of an enumeration
885 -- type with a non-standard representation, but the component size
886 -- is not appropriate for bit packing. This is the case where we
887 -- have Is_Packed set (we would never be in this unit otherwise),
888 -- but Is_Bit_Packed_Array is false.
890 -- Note that if the component size is appropriate for bit packing,
891 -- then the circuit for the computation of the subscript properly
892 -- deals with the non-standard enumeration type case by taking the
893 -- Pos anyway.
895 if not Is_Bit_Packed_Array (Typ) then
897 -- Here we build a declaration:
899 -- type tttP is array (index1, index2, ...) of component_type
901 -- where index1, index2, are the index types. These are the same
902 -- as the index types of the original array, except for the non-
903 -- standard representation enumeration type case, where we have
904 -- two subcases.
906 -- For the unconstrained array case, we use
908 -- Natural range <>
910 -- For the constrained case, we use
912 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
913 -- Enum_Type'Pos (Enum_Type'Last);
915 PAT :=
916 Make_Defining_Identifier (Loc,
917 Chars => New_External_Name (Chars (Typ), 'P'));
919 Set_Packed_Array_Type (Typ, PAT);
921 declare
922 Indexes : constant List_Id := New_List;
923 Indx : Node_Id;
924 Indx_Typ : Entity_Id;
925 Enum_Case : Boolean;
926 Typedef : Node_Id;
928 begin
929 Indx := First_Index (Typ);
931 while Present (Indx) loop
932 Indx_Typ := Etype (Indx);
934 Enum_Case := Is_Enumeration_Type (Indx_Typ)
935 and then Has_Non_Standard_Rep (Indx_Typ);
937 -- Unconstrained case
939 if not Is_Constrained (Typ) then
940 if Enum_Case then
941 Indx_Typ := Standard_Natural;
942 end if;
944 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
946 -- Constrained case
948 else
949 if not Enum_Case then
950 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
952 else
953 Append_To (Indexes,
954 Make_Subtype_Indication (Loc,
955 Subtype_Mark =>
956 New_Occurrence_Of (Standard_Natural, Loc),
957 Constraint =>
958 Make_Range_Constraint (Loc,
959 Range_Expression =>
960 Make_Range (Loc,
961 Low_Bound =>
962 Make_Attribute_Reference (Loc,
963 Prefix =>
964 New_Occurrence_Of (Indx_Typ, Loc),
965 Attribute_Name => Name_Pos,
966 Expressions => New_List (
967 Make_Attribute_Reference (Loc,
968 Prefix =>
969 New_Occurrence_Of (Indx_Typ, Loc),
970 Attribute_Name => Name_First))),
972 High_Bound =>
973 Make_Attribute_Reference (Loc,
974 Prefix =>
975 New_Occurrence_Of (Indx_Typ, Loc),
976 Attribute_Name => Name_Pos,
977 Expressions => New_List (
978 Make_Attribute_Reference (Loc,
979 Prefix =>
980 New_Occurrence_Of (Indx_Typ, Loc),
981 Attribute_Name => Name_Last)))))));
983 end if;
984 end if;
986 Next_Index (Indx);
987 end loop;
989 if not Is_Constrained (Typ) then
990 Typedef :=
991 Make_Unconstrained_Array_Definition (Loc,
992 Subtype_Marks => Indexes,
993 Component_Definition =>
994 Make_Component_Definition (Loc,
995 Aliased_Present => False,
996 Subtype_Indication =>
997 New_Occurrence_Of (Ctyp, Loc)));
999 else
1000 Typedef :=
1001 Make_Constrained_Array_Definition (Loc,
1002 Discrete_Subtype_Definitions => Indexes,
1003 Component_Definition =>
1004 Make_Component_Definition (Loc,
1005 Aliased_Present => False,
1006 Subtype_Indication =>
1007 New_Occurrence_Of (Ctyp, Loc)));
1008 end if;
1010 Decl :=
1011 Make_Full_Type_Declaration (Loc,
1012 Defining_Identifier => PAT,
1013 Type_Definition => Typedef);
1014 end;
1016 -- Set type as packed array type and install it
1018 Set_Is_Packed_Array_Type (PAT);
1019 Install_PAT;
1020 return;
1022 -- Case of bit-packing required for unconstrained array. We create
1023 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1025 elsif not Is_Constrained (Typ) then
1026 PAT :=
1027 Make_Defining_Identifier (Loc,
1028 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1030 Set_Packed_Array_Type (Typ, PAT);
1031 Set_PB_Type;
1033 Decl :=
1034 Make_Subtype_Declaration (Loc,
1035 Defining_Identifier => PAT,
1036 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1037 Install_PAT;
1038 return;
1040 -- Remaining code is for the case of bit-packing for constrained array
1042 -- The name of the packed array subtype is
1044 -- ttt___Xsss
1046 -- where sss is the component size in bits and ttt is the name of
1047 -- the parent packed type.
1049 else
1050 PAT :=
1051 Make_Defining_Identifier (Loc,
1052 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1054 Set_Packed_Array_Type (Typ, PAT);
1056 -- Build an expression for the length of the array in bits.
1057 -- This is the product of the length of each of the dimensions
1059 declare
1060 J : Nat := 1;
1062 begin
1063 Len_Expr := Empty; -- suppress junk warning
1065 loop
1066 Len_Dim :=
1067 Make_Attribute_Reference (Loc,
1068 Attribute_Name => Name_Length,
1069 Prefix => New_Occurrence_Of (Typ, Loc),
1070 Expressions => New_List (
1071 Make_Integer_Literal (Loc, J)));
1073 if J = 1 then
1074 Len_Expr := Len_Dim;
1076 else
1077 Len_Expr :=
1078 Make_Op_Multiply (Loc,
1079 Left_Opnd => Len_Expr,
1080 Right_Opnd => Len_Dim);
1081 end if;
1083 J := J + 1;
1084 exit when J > Number_Dimensions (Typ);
1085 end loop;
1086 end;
1088 -- Temporarily attach the length expression to the tree and analyze
1089 -- and resolve it, so that we can test its value. We assume that the
1090 -- total length fits in type Integer. This expression may involve
1091 -- discriminants, so we treat it as a default/per-object expression.
1093 Set_Parent (Len_Expr, Typ);
1094 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1096 -- Use a modular type if possible. We can do this if we have
1097 -- static bounds, and the length is small enough, and the length
1098 -- is not zero. We exclude the zero length case because the size
1099 -- of things is always at least one, and the zero length object
1100 -- would have an anomalous size.
1102 if Compile_Time_Known_Value (Len_Expr) then
1103 Len_Bits := Expr_Value (Len_Expr) * Csize;
1105 -- Check for size known to be too large
1107 if Len_Bits >
1108 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1109 then
1110 if System_Storage_Unit = 8 then
1111 Error_Msg_N
1112 ("packed array size cannot exceed " &
1113 "Integer''Last bytes", Typ);
1114 else
1115 Error_Msg_N
1116 ("packed array size cannot exceed " &
1117 "Integer''Last storage units", Typ);
1118 end if;
1120 -- Reset length to arbitrary not too high value to continue
1122 Len_Expr := Make_Integer_Literal (Loc, 65535);
1123 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1124 end if;
1126 -- We normally consider small enough to mean no larger than the
1127 -- value of System_Max_Binary_Modulus_Power, checking that in the
1128 -- case of values longer than word size, we have long shifts.
1130 if Len_Bits > 0
1131 and then
1132 (Len_Bits <= System_Word_Size
1133 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1134 and then Support_Long_Shifts_On_Target))
1136 -- Also test for alignment given. If an alignment is given which
1137 -- is smaller than the natural modular alignment, force the array
1138 -- of bytes representation to accommodate the alignment.
1140 and then
1141 (No (Alignment_Clause (Typ))
1142 or else
1143 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1144 / System_Storage_Unit))
1145 then
1146 -- We can use the modular type, it has the form:
1148 -- subtype tttPn is btyp
1149 -- range 0 .. 2 ** ((Typ'Length (1)
1150 -- * ... * Typ'Length (n)) * Csize) - 1;
1152 -- The bounds are statically known, and btyp is one of the
1153 -- unsigned types, depending on the length.
1155 if Len_Bits <= Standard_Short_Short_Integer_Size then
1156 Btyp := RTE (RE_Short_Short_Unsigned);
1158 elsif Len_Bits <= Standard_Short_Integer_Size then
1159 Btyp := RTE (RE_Short_Unsigned);
1161 elsif Len_Bits <= Standard_Integer_Size then
1162 Btyp := RTE (RE_Unsigned);
1164 elsif Len_Bits <= Standard_Long_Integer_Size then
1165 Btyp := RTE (RE_Long_Unsigned);
1167 else
1168 Btyp := RTE (RE_Long_Long_Unsigned);
1169 end if;
1171 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1172 Set_Print_In_Hex (Lit);
1174 Decl :=
1175 Make_Subtype_Declaration (Loc,
1176 Defining_Identifier => PAT,
1177 Subtype_Indication =>
1178 Make_Subtype_Indication (Loc,
1179 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1181 Constraint =>
1182 Make_Range_Constraint (Loc,
1183 Range_Expression =>
1184 Make_Range (Loc,
1185 Low_Bound =>
1186 Make_Integer_Literal (Loc, 0),
1187 High_Bound => Lit))));
1189 if PASize = Uint_0 then
1190 PASize := Len_Bits;
1191 end if;
1193 Install_PAT;
1194 return;
1195 end if;
1196 end if;
1198 -- Could not use a modular type, for all other cases, we build
1199 -- a packed array subtype:
1201 -- subtype tttPn is
1202 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1204 -- Bits is the length of the array in bits
1206 Set_PB_Type;
1208 Bits_U1 :=
1209 Make_Op_Add (Loc,
1210 Left_Opnd =>
1211 Make_Op_Multiply (Loc,
1212 Left_Opnd =>
1213 Make_Integer_Literal (Loc, Csize),
1214 Right_Opnd => Len_Expr),
1216 Right_Opnd =>
1217 Make_Integer_Literal (Loc, 7));
1219 Set_Paren_Count (Bits_U1, 1);
1221 PAT_High :=
1222 Make_Op_Subtract (Loc,
1223 Left_Opnd =>
1224 Make_Op_Divide (Loc,
1225 Left_Opnd => Bits_U1,
1226 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1227 Right_Opnd => Make_Integer_Literal (Loc, 1));
1229 Decl :=
1230 Make_Subtype_Declaration (Loc,
1231 Defining_Identifier => PAT,
1232 Subtype_Indication =>
1233 Make_Subtype_Indication (Loc,
1234 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1235 Constraint =>
1236 Make_Index_Or_Discriminant_Constraint (Loc,
1237 Constraints => New_List (
1238 Make_Range (Loc,
1239 Low_Bound =>
1240 Make_Integer_Literal (Loc, 0),
1241 High_Bound =>
1242 Convert_To (Standard_Integer, PAT_High))))));
1244 Install_PAT;
1246 -- Currently the code in this unit requires that packed arrays
1247 -- represented by non-modular arrays of bytes be on a byte
1248 -- boundary for bit sizes handled by System.Pack_nn units.
1249 -- That's because these units assume the array being accessed
1250 -- starts on a byte boundary.
1252 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1253 Set_Must_Be_On_Byte_Boundary (Typ);
1254 end if;
1255 end if;
1256 end Create_Packed_Array_Type;
1258 -----------------------------------
1259 -- Expand_Bit_Packed_Element_Set --
1260 -----------------------------------
1262 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1263 Loc : constant Source_Ptr := Sloc (N);
1264 Lhs : constant Node_Id := Name (N);
1266 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1267 -- Used to preserve assignment OK status when assignment is rewritten
1269 Rhs : Node_Id := Expression (N);
1270 -- Initially Rhs is the right hand side value, it will be replaced
1271 -- later by an appropriate unchecked conversion for the assignment.
1273 Obj : Node_Id;
1274 Atyp : Entity_Id;
1275 PAT : Entity_Id;
1276 Ctyp : Entity_Id;
1277 Csiz : Int;
1278 Cmask : Uint;
1280 Shift : Node_Id;
1281 -- The expression for the shift value that is required
1283 Shift_Used : Boolean := False;
1284 -- Set True if Shift has been used in the generated code at least
1285 -- once, so that it must be duplicated if used again
1287 New_Lhs : Node_Id;
1288 New_Rhs : Node_Id;
1290 Rhs_Val_Known : Boolean;
1291 Rhs_Val : Uint;
1292 -- If the value of the right hand side as an integer constant is
1293 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1294 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1295 -- the Rhs_Val is undefined.
1297 function Get_Shift return Node_Id;
1298 -- Function used to get the value of Shift, making sure that it
1299 -- gets duplicated if the function is called more than once.
1301 ---------------
1302 -- Get_Shift --
1303 ---------------
1305 function Get_Shift return Node_Id is
1306 begin
1307 -- If we used the shift value already, then duplicate it. We
1308 -- set a temporary parent in case actions have to be inserted.
1310 if Shift_Used then
1311 Set_Parent (Shift, N);
1312 return Duplicate_Subexpr_No_Checks (Shift);
1314 -- If first time, use Shift unchanged, and set flag for first use
1316 else
1317 Shift_Used := True;
1318 return Shift;
1319 end if;
1320 end Get_Shift;
1322 -- Start of processing for Expand_Bit_Packed_Element_Set
1324 begin
1325 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1327 Obj := Relocate_Node (Prefix (Lhs));
1328 Convert_To_Actual_Subtype (Obj);
1329 Atyp := Etype (Obj);
1330 PAT := Packed_Array_Type (Atyp);
1331 Ctyp := Component_Type (Atyp);
1332 Csiz := UI_To_Int (Component_Size (Atyp));
1334 -- We convert the right hand side to the proper subtype to ensure
1335 -- that an appropriate range check is made (since the normal range
1336 -- check from assignment will be lost in the transformations). This
1337 -- conversion is analyzed immediately so that subsequent processing
1338 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1340 -- If the right-hand side is a string literal, create a temporary for
1341 -- it, constant-folding is not ready to wrap the bit representation
1342 -- of a string literal.
1344 if Nkind (Rhs) = N_String_Literal then
1345 declare
1346 Decl : Node_Id;
1347 begin
1348 Decl :=
1349 Make_Object_Declaration (Loc,
1350 Defining_Identifier =>
1351 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1352 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1353 Expression => New_Copy_Tree (Rhs));
1355 Insert_Actions (N, New_List (Decl));
1356 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1357 end;
1358 end if;
1360 Rhs := Convert_To (Ctyp, Rhs);
1361 Set_Parent (Rhs, N);
1362 Analyze_And_Resolve (Rhs, Ctyp);
1364 -- Case of component size 1,2,4 or any component size for the modular
1365 -- case. These are the cases for which we can inline the code.
1367 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1368 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1369 then
1370 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1372 -- The statement to be generated is:
1374 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1376 -- where mask1 is obtained by shifting Cmask left Shift bits
1377 -- and then complementing the result.
1379 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1381 -- the "or ..." is omitted if rhs is constant and all 0 bits
1383 -- rhs is converted to the appropriate type
1385 -- The result is converted back to the array type, since
1386 -- otherwise we lose knowledge of the packed nature.
1388 -- Determine if right side is all 0 bits or all 1 bits
1390 if Compile_Time_Known_Value (Rhs) then
1391 Rhs_Val := Expr_Rep_Value (Rhs);
1392 Rhs_Val_Known := True;
1394 -- The following test catches the case of an unchecked conversion
1395 -- of an integer literal. This results from optimizing aggregates
1396 -- of packed types.
1398 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1399 and then Compile_Time_Known_Value (Expression (Rhs))
1400 then
1401 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1402 Rhs_Val_Known := True;
1404 else
1405 Rhs_Val := No_Uint;
1406 Rhs_Val_Known := False;
1407 end if;
1409 -- Some special checks for the case where the right hand value
1410 -- is known at compile time. Basically we have to take care of
1411 -- the implicit conversion to the subtype of the component object.
1413 if Rhs_Val_Known then
1415 -- If we have a biased component type then we must manually do
1416 -- the biasing, since we are taking responsibility in this case
1417 -- for constructing the exact bit pattern to be used.
1419 if Has_Biased_Representation (Ctyp) then
1420 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1421 end if;
1423 -- For a negative value, we manually convert the twos complement
1424 -- value to a corresponding unsigned value, so that the proper
1425 -- field width is maintained. If we did not do this, we would
1426 -- get too many leading sign bits later on.
1428 if Rhs_Val < 0 then
1429 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1430 end if;
1431 end if;
1433 -- Now create copies removing side effects. Note that in some
1434 -- complex cases, this may cause the fact that we have already
1435 -- set a packed array type on Obj to get lost. So we save the
1436 -- type of Obj, and make sure it is reset properly.
1438 declare
1439 T : constant Entity_Id := Etype (Obj);
1440 begin
1441 New_Lhs := Duplicate_Subexpr (Obj, True);
1442 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1443 Set_Etype (Obj, T);
1444 Set_Etype (New_Lhs, T);
1445 Set_Etype (New_Rhs, T);
1446 end;
1448 -- First we deal with the "and"
1450 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1451 declare
1452 Mask1 : Node_Id;
1453 Lit : Node_Id;
1455 begin
1456 if Compile_Time_Known_Value (Shift) then
1457 Mask1 :=
1458 Make_Integer_Literal (Loc,
1459 Modulus (Etype (Obj)) - 1 -
1460 (Cmask * (2 ** Expr_Value (Get_Shift))));
1461 Set_Print_In_Hex (Mask1);
1463 else
1464 Lit := Make_Integer_Literal (Loc, Cmask);
1465 Set_Print_In_Hex (Lit);
1466 Mask1 :=
1467 Make_Op_Not (Loc,
1468 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1469 end if;
1471 New_Rhs :=
1472 Make_Op_And (Loc,
1473 Left_Opnd => New_Rhs,
1474 Right_Opnd => Mask1);
1475 end;
1476 end if;
1478 -- Then deal with the "or"
1480 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1481 declare
1482 Or_Rhs : Node_Id;
1484 procedure Fixup_Rhs;
1485 -- Adjust Rhs by bias if biased representation for components
1486 -- or remove extraneous high order sign bits if signed.
1488 procedure Fixup_Rhs is
1489 Etyp : constant Entity_Id := Etype (Rhs);
1491 begin
1492 -- For biased case, do the required biasing by simply
1493 -- converting to the biased subtype (the conversion
1494 -- will generate the required bias).
1496 if Has_Biased_Representation (Ctyp) then
1497 Rhs := Convert_To (Ctyp, Rhs);
1499 -- For a signed integer type that is not biased, generate
1500 -- a conversion to unsigned to strip high order sign bits.
1502 elsif Is_Signed_Integer_Type (Ctyp) then
1503 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1504 end if;
1506 -- Set Etype, since it can be referenced before the
1507 -- node is completely analyzed.
1509 Set_Etype (Rhs, Etyp);
1511 -- We now need to do an unchecked conversion of the
1512 -- result to the target type, but it is important that
1513 -- this conversion be a right justified conversion and
1514 -- not a left justified conversion.
1516 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1518 end Fixup_Rhs;
1520 begin
1521 if Rhs_Val_Known
1522 and then Compile_Time_Known_Value (Get_Shift)
1523 then
1524 Or_Rhs :=
1525 Make_Integer_Literal (Loc,
1526 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1527 Set_Print_In_Hex (Or_Rhs);
1529 else
1530 -- We have to convert the right hand side to Etype (Obj).
1531 -- A special case case arises if what we have now is a Val
1532 -- attribute reference whose expression type is Etype (Obj).
1533 -- This happens for assignments of fields from the same
1534 -- array. In this case we get the required right hand side
1535 -- by simply removing the inner attribute reference.
1537 if Nkind (Rhs) = N_Attribute_Reference
1538 and then Attribute_Name (Rhs) = Name_Val
1539 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1540 then
1541 Rhs := Relocate_Node (First (Expressions (Rhs)));
1542 Fixup_Rhs;
1544 -- If the value of the right hand side is a known integer
1545 -- value, then just replace it by an untyped constant,
1546 -- which will be properly retyped when we analyze and
1547 -- resolve the expression.
1549 elsif Rhs_Val_Known then
1551 -- Note that Rhs_Val has already been normalized to
1552 -- be an unsigned value with the proper number of bits.
1554 Rhs :=
1555 Make_Integer_Literal (Loc, Rhs_Val);
1557 -- Otherwise we need an unchecked conversion
1559 else
1560 Fixup_Rhs;
1561 end if;
1563 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1564 end if;
1566 if Nkind (New_Rhs) = N_Op_And then
1567 Set_Paren_Count (New_Rhs, 1);
1568 end if;
1570 New_Rhs :=
1571 Make_Op_Or (Loc,
1572 Left_Opnd => New_Rhs,
1573 Right_Opnd => Or_Rhs);
1574 end;
1575 end if;
1577 -- Now do the rewrite
1579 Rewrite (N,
1580 Make_Assignment_Statement (Loc,
1581 Name => New_Lhs,
1582 Expression =>
1583 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1584 Set_Assignment_OK (Name (N), Ass_OK);
1586 -- All other component sizes for non-modular case
1588 else
1589 -- We generate
1591 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1593 -- where Subscr is the computed linear subscript
1595 declare
1596 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1597 Set_nn : Entity_Id;
1598 Subscr : Node_Id;
1599 Atyp : Entity_Id;
1601 begin
1602 if No (Bits_nn) then
1604 -- Error, most likely High_Integrity_Mode restriction
1606 return;
1607 end if;
1609 -- Acquire proper Set entity. We use the aligned or unaligned
1610 -- case as appropriate.
1612 if Known_Aligned_Enough (Obj, Csiz) then
1613 Set_nn := RTE (Set_Id (Csiz));
1614 else
1615 Set_nn := RTE (SetU_Id (Csiz));
1616 end if;
1618 -- Now generate the set reference
1620 Obj := Relocate_Node (Prefix (Lhs));
1621 Convert_To_Actual_Subtype (Obj);
1622 Atyp := Etype (Obj);
1623 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1625 -- Below we must make the assumption that Obj is
1626 -- at least byte aligned, since otherwise its address
1627 -- cannot be taken. The assumption holds since the
1628 -- only arrays that can be misaligned are small packed
1629 -- arrays which are implemented as a modular type, and
1630 -- that is not the case here.
1632 Rewrite (N,
1633 Make_Procedure_Call_Statement (Loc,
1634 Name => New_Occurrence_Of (Set_nn, Loc),
1635 Parameter_Associations => New_List (
1636 Make_Attribute_Reference (Loc,
1637 Prefix => Obj,
1638 Attribute_Name => Name_Address),
1639 Subscr,
1640 Unchecked_Convert_To (Bits_nn,
1641 Convert_To (Ctyp, Rhs)))));
1643 end;
1644 end if;
1646 Analyze (N, Suppress => All_Checks);
1647 end Expand_Bit_Packed_Element_Set;
1649 -------------------------------------
1650 -- Expand_Packed_Address_Reference --
1651 -------------------------------------
1653 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1654 Loc : constant Source_Ptr := Sloc (N);
1655 Ploc : Source_Ptr;
1656 Pref : Node_Id;
1657 Expr : Node_Id;
1658 Term : Node_Id;
1659 Atyp : Entity_Id;
1660 Subscr : Node_Id;
1662 begin
1663 Pref := Prefix (N);
1664 Expr := Empty;
1666 -- We build up an expression serially that has the form
1668 -- outer_object'Address
1669 -- + (linear-subscript * component_size for each array reference
1670 -- + field'Bit_Position for each record field
1671 -- + ...
1672 -- + ...) / Storage_Unit;
1674 -- Some additional conversions are required to deal with the addition
1675 -- operation, which is not normally visible to generated code.
1677 loop
1678 Ploc := Sloc (Pref);
1680 if Nkind (Pref) = N_Indexed_Component then
1681 Convert_To_Actual_Subtype (Prefix (Pref));
1682 Atyp := Etype (Prefix (Pref));
1683 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1685 Term :=
1686 Make_Op_Multiply (Ploc,
1687 Left_Opnd => Subscr,
1688 Right_Opnd =>
1689 Make_Attribute_Reference (Ploc,
1690 Prefix => New_Occurrence_Of (Atyp, Ploc),
1691 Attribute_Name => Name_Component_Size));
1693 elsif Nkind (Pref) = N_Selected_Component then
1694 Term :=
1695 Make_Attribute_Reference (Ploc,
1696 Prefix => Selector_Name (Pref),
1697 Attribute_Name => Name_Bit_Position);
1699 else
1700 exit;
1701 end if;
1703 Term := Convert_To (RTE (RE_Integer_Address), Term);
1705 if No (Expr) then
1706 Expr := Term;
1708 else
1709 Expr :=
1710 Make_Op_Add (Ploc,
1711 Left_Opnd => Expr,
1712 Right_Opnd => Term);
1713 end if;
1715 Pref := Prefix (Pref);
1716 end loop;
1718 Rewrite (N,
1719 Unchecked_Convert_To (RTE (RE_Address),
1720 Make_Op_Add (Loc,
1721 Left_Opnd =>
1722 Unchecked_Convert_To (RTE (RE_Integer_Address),
1723 Make_Attribute_Reference (Loc,
1724 Prefix => Pref,
1725 Attribute_Name => Name_Address)),
1727 Right_Opnd =>
1728 Make_Op_Divide (Loc,
1729 Left_Opnd => Expr,
1730 Right_Opnd =>
1731 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1733 Analyze_And_Resolve (N, RTE (RE_Address));
1734 end Expand_Packed_Address_Reference;
1736 ------------------------------------
1737 -- Expand_Packed_Boolean_Operator --
1738 ------------------------------------
1740 -- This routine expands "a op b" for the packed cases
1742 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1743 Loc : constant Source_Ptr := Sloc (N);
1744 Typ : constant Entity_Id := Etype (N);
1745 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1746 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1748 Ltyp : Entity_Id;
1749 Rtyp : Entity_Id;
1750 PAT : Entity_Id;
1752 begin
1753 Convert_To_Actual_Subtype (L);
1754 Convert_To_Actual_Subtype (R);
1756 Ensure_Defined (Etype (L), N);
1757 Ensure_Defined (Etype (R), N);
1759 Apply_Length_Check (R, Etype (L));
1761 Ltyp := Etype (L);
1762 Rtyp := Etype (R);
1764 -- First an odd and silly test. We explicitly check for the XOR
1765 -- case where the component type is True .. True, since this will
1766 -- raise constraint error. A special check is required since CE
1767 -- will not be required other wise (cf Expand_Packed_Not).
1769 -- No such check is required for AND and OR, since for both these
1770 -- cases False op False = False, and True op True = True.
1772 if Nkind (N) = N_Op_Xor then
1773 declare
1774 CT : constant Entity_Id := Component_Type (Rtyp);
1775 BT : constant Entity_Id := Base_Type (CT);
1777 begin
1778 Insert_Action (N,
1779 Make_Raise_Constraint_Error (Loc,
1780 Condition =>
1781 Make_Op_And (Loc,
1782 Left_Opnd =>
1783 Make_Op_Eq (Loc,
1784 Left_Opnd =>
1785 Make_Attribute_Reference (Loc,
1786 Prefix => New_Occurrence_Of (CT, Loc),
1787 Attribute_Name => Name_First),
1789 Right_Opnd =>
1790 Convert_To (BT,
1791 New_Occurrence_Of (Standard_True, Loc))),
1793 Right_Opnd =>
1794 Make_Op_Eq (Loc,
1795 Left_Opnd =>
1796 Make_Attribute_Reference (Loc,
1797 Prefix => New_Occurrence_Of (CT, Loc),
1798 Attribute_Name => Name_Last),
1800 Right_Opnd =>
1801 Convert_To (BT,
1802 New_Occurrence_Of (Standard_True, Loc)))),
1803 Reason => CE_Range_Check_Failed));
1804 end;
1805 end if;
1807 -- Now that that silliness is taken care of, get packed array type
1809 Convert_To_PAT_Type (L);
1810 Convert_To_PAT_Type (R);
1812 PAT := Etype (L);
1814 -- For the modular case, we expand a op b into
1816 -- rtyp!(pat!(a) op pat!(b))
1818 -- where rtyp is the Etype of the left operand. Note that we do not
1819 -- convert to the base type, since this would be unconstrained, and
1820 -- hence not have a corresponding packed array type set.
1822 -- Note that both operands must be modular for this code to be used
1824 if Is_Modular_Integer_Type (PAT)
1825 and then
1826 Is_Modular_Integer_Type (Etype (R))
1827 then
1828 declare
1829 P : Node_Id;
1831 begin
1832 if Nkind (N) = N_Op_And then
1833 P := Make_Op_And (Loc, L, R);
1835 elsif Nkind (N) = N_Op_Or then
1836 P := Make_Op_Or (Loc, L, R);
1838 else -- Nkind (N) = N_Op_Xor
1839 P := Make_Op_Xor (Loc, L, R);
1840 end if;
1842 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1843 end;
1845 -- For the array case, we insert the actions
1847 -- Result : Ltype;
1849 -- System.Bitops.Bit_And/Or/Xor
1850 -- (Left'Address,
1851 -- Ltype'Length * Ltype'Component_Size;
1852 -- Right'Address,
1853 -- Rtype'Length * Rtype'Component_Size
1854 -- Result'Address);
1856 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1857 -- the second argument and fourth arguments are the lengths of the
1858 -- operands in bits. Then we replace the expression by a reference
1859 -- to Result.
1861 -- Note that if we are mixing a modular and array operand, everything
1862 -- works fine, since we ensure that the modular representation has the
1863 -- same physical layout as the array representation (that's what the
1864 -- left justified modular stuff in the big-endian case is about).
1866 else
1867 declare
1868 Result_Ent : constant Entity_Id :=
1869 Make_Defining_Identifier (Loc,
1870 Chars => New_Internal_Name ('T'));
1872 E_Id : RE_Id;
1874 begin
1875 if Nkind (N) = N_Op_And then
1876 E_Id := RE_Bit_And;
1878 elsif Nkind (N) = N_Op_Or then
1879 E_Id := RE_Bit_Or;
1881 else -- Nkind (N) = N_Op_Xor
1882 E_Id := RE_Bit_Xor;
1883 end if;
1885 Insert_Actions (N, New_List (
1887 Make_Object_Declaration (Loc,
1888 Defining_Identifier => Result_Ent,
1889 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1891 Make_Procedure_Call_Statement (Loc,
1892 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1893 Parameter_Associations => New_List (
1895 Make_Byte_Aligned_Attribute_Reference (Loc,
1896 Prefix => L,
1897 Attribute_Name => Name_Address),
1899 Make_Op_Multiply (Loc,
1900 Left_Opnd =>
1901 Make_Attribute_Reference (Loc,
1902 Prefix =>
1903 New_Occurrence_Of
1904 (Etype (First_Index (Ltyp)), Loc),
1905 Attribute_Name => Name_Range_Length),
1907 Right_Opnd =>
1908 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1910 Make_Byte_Aligned_Attribute_Reference (Loc,
1911 Prefix => R,
1912 Attribute_Name => Name_Address),
1914 Make_Op_Multiply (Loc,
1915 Left_Opnd =>
1916 Make_Attribute_Reference (Loc,
1917 Prefix =>
1918 New_Occurrence_Of
1919 (Etype (First_Index (Rtyp)), Loc),
1920 Attribute_Name => Name_Range_Length),
1922 Right_Opnd =>
1923 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1925 Make_Byte_Aligned_Attribute_Reference (Loc,
1926 Prefix => New_Occurrence_Of (Result_Ent, Loc),
1927 Attribute_Name => Name_Address)))));
1929 Rewrite (N,
1930 New_Occurrence_Of (Result_Ent, Loc));
1931 end;
1932 end if;
1934 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1935 end Expand_Packed_Boolean_Operator;
1937 -------------------------------------
1938 -- Expand_Packed_Element_Reference --
1939 -------------------------------------
1941 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1942 Loc : constant Source_Ptr := Sloc (N);
1943 Obj : Node_Id;
1944 Atyp : Entity_Id;
1945 PAT : Entity_Id;
1946 Ctyp : Entity_Id;
1947 Csiz : Int;
1948 Shift : Node_Id;
1949 Cmask : Uint;
1950 Lit : Node_Id;
1951 Arg : Node_Id;
1953 begin
1954 -- If not bit packed, we have the enumeration case, which is easily
1955 -- dealt with (just adjust the subscripts of the indexed component)
1957 -- Note: this leaves the result as an indexed component, which is
1958 -- still a variable, so can be used in the assignment case, as is
1959 -- required in the enumeration case.
1961 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1962 Setup_Enumeration_Packed_Array_Reference (N);
1963 return;
1964 end if;
1966 -- Remaining processing is for the bit-packed case
1968 Obj := Relocate_Node (Prefix (N));
1969 Convert_To_Actual_Subtype (Obj);
1970 Atyp := Etype (Obj);
1971 PAT := Packed_Array_Type (Atyp);
1972 Ctyp := Component_Type (Atyp);
1973 Csiz := UI_To_Int (Component_Size (Atyp));
1975 -- Case of component size 1,2,4 or any component size for the modular
1976 -- case. These are the cases for which we can inline the code.
1978 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1979 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1980 then
1981 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1982 Lit := Make_Integer_Literal (Loc, Cmask);
1983 Set_Print_In_Hex (Lit);
1985 -- We generate a shift right to position the field, followed by a
1986 -- masking operation to extract the bit field, and we finally do an
1987 -- unchecked conversion to convert the result to the required target.
1989 -- Note that the unchecked conversion automatically deals with the
1990 -- bias if we are dealing with a biased representation. What will
1991 -- happen is that we temporarily generate the biased representation,
1992 -- but almost immediately that will be converted to the original
1993 -- unbiased component type, and the bias will disappear.
1995 Arg :=
1996 Make_Op_And (Loc,
1997 Left_Opnd => Make_Shift_Right (Obj, Shift),
1998 Right_Opnd => Lit);
2000 -- We neded to analyze this before we do the unchecked convert
2001 -- below, but we need it temporarily attached to the tree for
2002 -- this analysis (hence the temporary Set_Parent call).
2004 Set_Parent (Arg, Parent (N));
2005 Analyze_And_Resolve (Arg);
2007 Rewrite (N,
2008 RJ_Unchecked_Convert_To (Ctyp, Arg));
2010 -- All other component sizes for non-modular case
2012 else
2013 -- We generate
2015 -- Component_Type!(Get_nn (Arr'address, Subscr))
2017 -- where Subscr is the computed linear subscript
2019 declare
2020 Get_nn : Entity_Id;
2021 Subscr : Node_Id;
2023 begin
2024 -- Acquire proper Get entity. We use the aligned or unaligned
2025 -- case as appropriate.
2027 if Known_Aligned_Enough (Obj, Csiz) then
2028 Get_nn := RTE (Get_Id (Csiz));
2029 else
2030 Get_nn := RTE (GetU_Id (Csiz));
2031 end if;
2033 -- Now generate the get reference
2035 Compute_Linear_Subscript (Atyp, N, Subscr);
2037 -- Below we make the assumption that Obj is at least byte
2038 -- aligned, since otherwise its address cannot be taken.
2039 -- The assumption holds since the only arrays that can be
2040 -- misaligned are small packed arrays which are implemented
2041 -- as a modular type, and that is not the case here.
2043 Rewrite (N,
2044 Unchecked_Convert_To (Ctyp,
2045 Make_Function_Call (Loc,
2046 Name => New_Occurrence_Of (Get_nn, Loc),
2047 Parameter_Associations => New_List (
2048 Make_Attribute_Reference (Loc,
2049 Prefix => Obj,
2050 Attribute_Name => Name_Address),
2051 Subscr))));
2052 end;
2053 end if;
2055 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2057 end Expand_Packed_Element_Reference;
2059 ----------------------
2060 -- Expand_Packed_Eq --
2061 ----------------------
2063 -- Handles expansion of "=" on packed array types
2065 procedure Expand_Packed_Eq (N : Node_Id) is
2066 Loc : constant Source_Ptr := Sloc (N);
2067 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2068 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2070 LLexpr : Node_Id;
2071 RLexpr : Node_Id;
2073 Ltyp : Entity_Id;
2074 Rtyp : Entity_Id;
2075 PAT : Entity_Id;
2077 begin
2078 Convert_To_Actual_Subtype (L);
2079 Convert_To_Actual_Subtype (R);
2080 Ltyp := Underlying_Type (Etype (L));
2081 Rtyp := Underlying_Type (Etype (R));
2083 Convert_To_PAT_Type (L);
2084 Convert_To_PAT_Type (R);
2085 PAT := Etype (L);
2087 LLexpr :=
2088 Make_Op_Multiply (Loc,
2089 Left_Opnd =>
2090 Make_Attribute_Reference (Loc,
2091 Prefix => New_Occurrence_Of (Ltyp, Loc),
2092 Attribute_Name => Name_Length),
2093 Right_Opnd =>
2094 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2096 RLexpr :=
2097 Make_Op_Multiply (Loc,
2098 Left_Opnd =>
2099 Make_Attribute_Reference (Loc,
2100 Prefix => New_Occurrence_Of (Rtyp, Loc),
2101 Attribute_Name => Name_Length),
2102 Right_Opnd =>
2103 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2105 -- For the modular case, we transform the comparison to:
2107 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2109 -- where PAT is the packed array type. This works fine, since in the
2110 -- modular case we guarantee that the unused bits are always zeroes.
2111 -- We do have to compare the lengths because we could be comparing
2112 -- two different subtypes of the same base type.
2114 if Is_Modular_Integer_Type (PAT) then
2115 Rewrite (N,
2116 Make_And_Then (Loc,
2117 Left_Opnd =>
2118 Make_Op_Eq (Loc,
2119 Left_Opnd => LLexpr,
2120 Right_Opnd => RLexpr),
2122 Right_Opnd =>
2123 Make_Op_Eq (Loc,
2124 Left_Opnd => L,
2125 Right_Opnd => R)));
2127 -- For the non-modular case, we call a runtime routine
2129 -- System.Bit_Ops.Bit_Eq
2130 -- (L'Address, L_Length, R'Address, R_Length)
2132 -- where PAT is the packed array type, and the lengths are the lengths
2133 -- in bits of the original packed arrays. This routine takes care of
2134 -- not comparing the unused bits in the last byte.
2136 else
2137 Rewrite (N,
2138 Make_Function_Call (Loc,
2139 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2140 Parameter_Associations => New_List (
2141 Make_Byte_Aligned_Attribute_Reference (Loc,
2142 Prefix => L,
2143 Attribute_Name => Name_Address),
2145 LLexpr,
2147 Make_Byte_Aligned_Attribute_Reference (Loc,
2148 Prefix => R,
2149 Attribute_Name => Name_Address),
2151 RLexpr)));
2152 end if;
2154 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2155 end Expand_Packed_Eq;
2157 -----------------------
2158 -- Expand_Packed_Not --
2159 -----------------------
2161 -- Handles expansion of "not" on packed array types
2163 procedure Expand_Packed_Not (N : Node_Id) is
2164 Loc : constant Source_Ptr := Sloc (N);
2165 Typ : constant Entity_Id := Etype (N);
2166 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2168 Rtyp : Entity_Id;
2169 PAT : Entity_Id;
2170 Lit : Node_Id;
2172 begin
2173 Convert_To_Actual_Subtype (Opnd);
2174 Rtyp := Etype (Opnd);
2176 -- First an odd and silly test. We explicitly check for the case
2177 -- where the 'First of the component type is equal to the 'Last of
2178 -- this component type, and if this is the case, we make sure that
2179 -- constraint error is raised. The reason is that the NOT is bound
2180 -- to cause CE in this case, and we will not otherwise catch it.
2182 -- Believe it or not, this was reported as a bug. Note that nearly
2183 -- always, the test will evaluate statically to False, so the code
2184 -- will be statically removed, and no extra overhead caused.
2186 declare
2187 CT : constant Entity_Id := Component_Type (Rtyp);
2189 begin
2190 Insert_Action (N,
2191 Make_Raise_Constraint_Error (Loc,
2192 Condition =>
2193 Make_Op_Eq (Loc,
2194 Left_Opnd =>
2195 Make_Attribute_Reference (Loc,
2196 Prefix => New_Occurrence_Of (CT, Loc),
2197 Attribute_Name => Name_First),
2199 Right_Opnd =>
2200 Make_Attribute_Reference (Loc,
2201 Prefix => New_Occurrence_Of (CT, Loc),
2202 Attribute_Name => Name_Last)),
2203 Reason => CE_Range_Check_Failed));
2204 end;
2206 -- Now that that silliness is taken care of, get packed array type
2208 Convert_To_PAT_Type (Opnd);
2209 PAT := Etype (Opnd);
2211 -- For the case where the packed array type is a modular type,
2212 -- not A expands simply into:
2214 -- rtyp!(PAT!(A) xor mask)
2216 -- where PAT is the packed array type, and mask is a mask of all
2217 -- one bits of length equal to the size of this packed type and
2218 -- rtyp is the actual subtype of the operand
2220 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2221 Set_Print_In_Hex (Lit);
2223 if not Is_Array_Type (PAT) then
2224 Rewrite (N,
2225 Unchecked_Convert_To (Rtyp,
2226 Make_Op_Xor (Loc,
2227 Left_Opnd => Opnd,
2228 Right_Opnd => Lit)));
2230 -- For the array case, we insert the actions
2232 -- Result : Typ;
2234 -- System.Bitops.Bit_Not
2235 -- (Opnd'Address,
2236 -- Typ'Length * Typ'Component_Size;
2237 -- Result'Address);
2239 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2240 -- argument is the length of the operand in bits. Then we replace
2241 -- the expression by a reference to Result.
2243 else
2244 declare
2245 Result_Ent : constant Entity_Id :=
2246 Make_Defining_Identifier (Loc,
2247 Chars => New_Internal_Name ('T'));
2249 begin
2250 Insert_Actions (N, New_List (
2252 Make_Object_Declaration (Loc,
2253 Defining_Identifier => Result_Ent,
2254 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2256 Make_Procedure_Call_Statement (Loc,
2257 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2258 Parameter_Associations => New_List (
2260 Make_Byte_Aligned_Attribute_Reference (Loc,
2261 Prefix => Opnd,
2262 Attribute_Name => Name_Address),
2264 Make_Op_Multiply (Loc,
2265 Left_Opnd =>
2266 Make_Attribute_Reference (Loc,
2267 Prefix =>
2268 New_Occurrence_Of
2269 (Etype (First_Index (Rtyp)), Loc),
2270 Attribute_Name => Name_Range_Length),
2272 Right_Opnd =>
2273 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2275 Make_Byte_Aligned_Attribute_Reference (Loc,
2276 Prefix => New_Occurrence_Of (Result_Ent, Loc),
2277 Attribute_Name => Name_Address)))));
2279 Rewrite (N,
2280 New_Occurrence_Of (Result_Ent, Loc));
2281 end;
2282 end if;
2284 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2286 end Expand_Packed_Not;
2288 -------------------------------------
2289 -- Involves_Packed_Array_Reference --
2290 -------------------------------------
2292 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2293 begin
2294 if Nkind (N) = N_Indexed_Component
2295 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2296 then
2297 return True;
2299 elsif Nkind (N) = N_Selected_Component then
2300 return Involves_Packed_Array_Reference (Prefix (N));
2302 else
2303 return False;
2304 end if;
2305 end Involves_Packed_Array_Reference;
2307 --------------------------
2308 -- Known_Aligned_Enough --
2309 --------------------------
2311 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2312 Typ : constant Entity_Id := Etype (Obj);
2314 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2315 -- If the component is in a record that contains previous packed
2316 -- components, consider it unaligned because the back-end might
2317 -- choose to pack the rest of the record. Lead to less efficient code,
2318 -- but safer vis-a-vis of back-end choices.
2320 --------------------------------
2321 -- In_Partially_Packed_Record --
2322 --------------------------------
2324 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2325 Rec_Type : constant Entity_Id := Scope (Comp);
2326 Prev_Comp : Entity_Id;
2328 begin
2329 Prev_Comp := First_Entity (Rec_Type);
2330 while Present (Prev_Comp) loop
2331 if Is_Packed (Etype (Prev_Comp)) then
2332 return True;
2334 elsif Prev_Comp = Comp then
2335 return False;
2336 end if;
2338 Next_Entity (Prev_Comp);
2339 end loop;
2341 return False;
2342 end In_Partially_Packed_Record;
2344 -- Start of processing for Known_Aligned_Enough
2346 begin
2347 -- Odd bit sizes don't need alignment anyway
2349 if Csiz mod 2 = 1 then
2350 return True;
2352 -- If we have a specified alignment, see if it is sufficient, if not
2353 -- then we can't possibly be aligned enough in any case.
2355 elsif Known_Alignment (Etype (Obj)) then
2356 -- Alignment required is 4 if size is a multiple of 4, and
2357 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2359 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2360 return False;
2361 end if;
2362 end if;
2364 -- OK, alignment should be sufficient, if object is aligned
2366 -- If object is strictly aligned, then it is definitely aligned
2368 if Strict_Alignment (Typ) then
2369 return True;
2371 -- Case of subscripted array reference
2373 elsif Nkind (Obj) = N_Indexed_Component then
2375 -- If we have a pointer to an array, then this is definitely
2376 -- aligned, because pointers always point to aligned versions.
2378 if Is_Access_Type (Etype (Prefix (Obj))) then
2379 return True;
2381 -- Otherwise, go look at the prefix
2383 else
2384 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2385 end if;
2387 -- Case of record field
2389 elsif Nkind (Obj) = N_Selected_Component then
2391 -- What is significant here is whether the record type is packed
2393 if Is_Record_Type (Etype (Prefix (Obj)))
2394 and then Is_Packed (Etype (Prefix (Obj)))
2395 then
2396 return False;
2398 -- Or the component has a component clause which might cause
2399 -- the component to become unaligned (we can't tell if the
2400 -- backend is doing alignment computations).
2402 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2403 return False;
2405 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2406 return False;
2408 -- In all other cases, go look at prefix
2410 else
2411 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2412 end if;
2414 elsif Nkind (Obj) = N_Type_Conversion then
2415 return Known_Aligned_Enough (Expression (Obj), Csiz);
2417 -- For a formal parameter, it is safer to assume that it is not
2418 -- aligned, because the formal may be unconstrained while the actual
2419 -- is constrained. In this situation, a small constrained packed
2420 -- array, represented in modular form, may be unaligned.
2422 elsif Is_Entity_Name (Obj) then
2423 return not Is_Formal (Entity (Obj));
2424 else
2426 -- If none of the above, must be aligned
2427 return True;
2428 end if;
2429 end Known_Aligned_Enough;
2431 ---------------------
2432 -- Make_Shift_Left --
2433 ---------------------
2435 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2436 Nod : Node_Id;
2438 begin
2439 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2440 return N;
2441 else
2442 Nod :=
2443 Make_Op_Shift_Left (Sloc (N),
2444 Left_Opnd => N,
2445 Right_Opnd => S);
2446 Set_Shift_Count_OK (Nod, True);
2447 return Nod;
2448 end if;
2449 end Make_Shift_Left;
2451 ----------------------
2452 -- Make_Shift_Right --
2453 ----------------------
2455 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2456 Nod : Node_Id;
2458 begin
2459 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2460 return N;
2461 else
2462 Nod :=
2463 Make_Op_Shift_Right (Sloc (N),
2464 Left_Opnd => N,
2465 Right_Opnd => S);
2466 Set_Shift_Count_OK (Nod, True);
2467 return Nod;
2468 end if;
2469 end Make_Shift_Right;
2471 -----------------------------
2472 -- RJ_Unchecked_Convert_To --
2473 -----------------------------
2475 function RJ_Unchecked_Convert_To
2476 (Typ : Entity_Id;
2477 Expr : Node_Id) return Node_Id
2479 Source_Typ : constant Entity_Id := Etype (Expr);
2480 Target_Typ : constant Entity_Id := Typ;
2482 Src : Node_Id := Expr;
2484 Source_Siz : Nat;
2485 Target_Siz : Nat;
2487 begin
2488 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2489 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2491 -- First step, if the source type is not a discrete type, then we
2492 -- first convert to a modular type of the source length, since
2493 -- otherwise, on a big-endian machine, we get left-justification.
2494 -- We do it for little-endian machines as well, because there might
2495 -- be junk bits that are not cleared if the type is not numeric.
2497 if Source_Siz /= Target_Siz
2498 and then not Is_Discrete_Type (Source_Typ)
2499 then
2500 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2501 end if;
2503 -- In the big endian case, if the lengths of the two types differ,
2504 -- then we must worry about possible left justification in the
2505 -- conversion, and avoiding that is what this is all about.
2507 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2509 -- Next step. If the target is not a discrete type, then we first
2510 -- convert to a modular type of the target length, since
2511 -- otherwise, on a big-endian machine, we get left-justification.
2513 if not Is_Discrete_Type (Target_Typ) then
2514 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2515 end if;
2516 end if;
2518 -- And now we can do the final conversion to the target type
2520 return Unchecked_Convert_To (Target_Typ, Src);
2521 end RJ_Unchecked_Convert_To;
2523 ----------------------------------------------
2524 -- Setup_Enumeration_Packed_Array_Reference --
2525 ----------------------------------------------
2527 -- All we have to do here is to find the subscripts that correspond
2528 -- to the index positions that have non-standard enumeration types
2529 -- and insert a Pos attribute to get the proper subscript value.
2531 -- Finally the prefix must be uncheck converted to the corresponding
2532 -- packed array type.
2534 -- Note that the component type is unchanged, so we do not need to
2535 -- fiddle with the types (Gigi always automatically takes the packed
2536 -- array type if it is set, as it will be in this case).
2538 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2539 Pfx : constant Node_Id := Prefix (N);
2540 Typ : constant Entity_Id := Etype (N);
2541 Exprs : constant List_Id := Expressions (N);
2542 Expr : Node_Id;
2544 begin
2545 -- If the array is unconstrained, then we replace the array
2546 -- reference with its actual subtype. This actual subtype will
2547 -- have a packed array type with appropriate bounds.
2549 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2550 Convert_To_Actual_Subtype (Pfx);
2551 end if;
2553 Expr := First (Exprs);
2554 while Present (Expr) loop
2555 declare
2556 Loc : constant Source_Ptr := Sloc (Expr);
2557 Expr_Typ : constant Entity_Id := Etype (Expr);
2559 begin
2560 if Is_Enumeration_Type (Expr_Typ)
2561 and then Has_Non_Standard_Rep (Expr_Typ)
2562 then
2563 Rewrite (Expr,
2564 Make_Attribute_Reference (Loc,
2565 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2566 Attribute_Name => Name_Pos,
2567 Expressions => New_List (Relocate_Node (Expr))));
2568 Analyze_And_Resolve (Expr, Standard_Natural);
2569 end if;
2570 end;
2572 Next (Expr);
2573 end loop;
2575 Rewrite (N,
2576 Make_Indexed_Component (Sloc (N),
2577 Prefix =>
2578 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2579 Expressions => Exprs));
2581 Analyze_And_Resolve (N, Typ);
2583 end Setup_Enumeration_Packed_Array_Reference;
2585 -----------------------------------------
2586 -- Setup_Inline_Packed_Array_Reference --
2587 -----------------------------------------
2589 procedure Setup_Inline_Packed_Array_Reference
2590 (N : Node_Id;
2591 Atyp : Entity_Id;
2592 Obj : in out Node_Id;
2593 Cmask : out Uint;
2594 Shift : out Node_Id)
2596 Loc : constant Source_Ptr := Sloc (N);
2597 PAT : Entity_Id;
2598 Otyp : Entity_Id;
2599 Csiz : Uint;
2600 Osiz : Uint;
2602 begin
2603 Csiz := Component_Size (Atyp);
2605 Convert_To_PAT_Type (Obj);
2606 PAT := Etype (Obj);
2608 Cmask := 2 ** Csiz - 1;
2610 if Is_Array_Type (PAT) then
2611 Otyp := Component_Type (PAT);
2612 Osiz := Component_Size (PAT);
2614 else
2615 Otyp := PAT;
2617 -- In the case where the PAT is a modular type, we want the actual
2618 -- size in bits of the modular value we use. This is neither the
2619 -- Object_Size nor the Value_Size, either of which may have been
2620 -- reset to strange values, but rather the minimum size. Note that
2621 -- since this is a modular type with full range, the issue of
2622 -- biased representation does not arise.
2624 Osiz := UI_From_Int (Minimum_Size (Otyp));
2625 end if;
2627 Compute_Linear_Subscript (Atyp, N, Shift);
2629 -- If the component size is not 1, then the subscript must be
2630 -- multiplied by the component size to get the shift count.
2632 if Csiz /= 1 then
2633 Shift :=
2634 Make_Op_Multiply (Loc,
2635 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2636 Right_Opnd => Shift);
2637 end if;
2639 -- If we have the array case, then this shift count must be broken
2640 -- down into a byte subscript, and a shift within the byte.
2642 if Is_Array_Type (PAT) then
2644 declare
2645 New_Shift : Node_Id;
2647 begin
2648 -- We must analyze shift, since we will duplicate it
2650 Set_Parent (Shift, N);
2651 Analyze_And_Resolve
2652 (Shift, Standard_Integer, Suppress => All_Checks);
2654 -- The shift count within the word is
2655 -- shift mod Osiz
2657 New_Shift :=
2658 Make_Op_Mod (Loc,
2659 Left_Opnd => Duplicate_Subexpr (Shift),
2660 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2662 -- The subscript to be used on the PAT array is
2663 -- shift / Osiz
2665 Obj :=
2666 Make_Indexed_Component (Loc,
2667 Prefix => Obj,
2668 Expressions => New_List (
2669 Make_Op_Divide (Loc,
2670 Left_Opnd => Duplicate_Subexpr (Shift),
2671 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2673 Shift := New_Shift;
2674 end;
2676 -- For the modular integer case, the object to be manipulated is
2677 -- the entire array, so Obj is unchanged. Note that we will reset
2678 -- its type to PAT before returning to the caller.
2680 else
2681 null;
2682 end if;
2684 -- The one remaining step is to modify the shift count for the
2685 -- big-endian case. Consider the following example in a byte:
2687 -- xxxxxxxx bits of byte
2688 -- vvvvvvvv bits of value
2689 -- 33221100 little-endian numbering
2690 -- 00112233 big-endian numbering
2692 -- Here we have the case of 2-bit fields
2694 -- For the little-endian case, we already have the proper shift
2695 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2697 -- For the big endian case, we have to adjust the shift count,
2698 -- computing it as (N - F) - shift, where N is the number of bits
2699 -- in an element of the array used to implement the packed array,
2700 -- F is the number of bits in a source level array element, and
2701 -- shift is the count so far computed.
2703 if Bytes_Big_Endian then
2704 Shift :=
2705 Make_Op_Subtract (Loc,
2706 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2707 Right_Opnd => Shift);
2708 end if;
2710 Set_Parent (Shift, N);
2711 Set_Parent (Obj, N);
2712 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2713 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2715 -- Make sure final type of object is the appropriate packed type
2717 Set_Etype (Obj, Otyp);
2719 end Setup_Inline_Packed_Array_Reference;
2721 end Exp_Pakd;