Merged with mainline at revision 128810.
[official-gcc.git] / gcc / ada / exp_pakd.adb
blobfd280163f5ede63206015bf532e5a47228062332
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 New_Lhs := Duplicate_Subexpr (Obj, True);
1434 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1436 -- First we deal with the "and"
1438 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1439 declare
1440 Mask1 : Node_Id;
1441 Lit : Node_Id;
1443 begin
1444 if Compile_Time_Known_Value (Shift) then
1445 Mask1 :=
1446 Make_Integer_Literal (Loc,
1447 Modulus (Etype (Obj)) - 1 -
1448 (Cmask * (2 ** Expr_Value (Get_Shift))));
1449 Set_Print_In_Hex (Mask1);
1451 else
1452 Lit := Make_Integer_Literal (Loc, Cmask);
1453 Set_Print_In_Hex (Lit);
1454 Mask1 :=
1455 Make_Op_Not (Loc,
1456 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1457 end if;
1459 New_Rhs :=
1460 Make_Op_And (Loc,
1461 Left_Opnd => New_Rhs,
1462 Right_Opnd => Mask1);
1463 end;
1464 end if;
1466 -- Then deal with the "or"
1468 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1469 declare
1470 Or_Rhs : Node_Id;
1472 procedure Fixup_Rhs;
1473 -- Adjust Rhs by bias if biased representation for components
1474 -- or remove extraneous high order sign bits if signed.
1476 procedure Fixup_Rhs is
1477 Etyp : constant Entity_Id := Etype (Rhs);
1479 begin
1480 -- For biased case, do the required biasing by simply
1481 -- converting to the biased subtype (the conversion
1482 -- will generate the required bias).
1484 if Has_Biased_Representation (Ctyp) then
1485 Rhs := Convert_To (Ctyp, Rhs);
1487 -- For a signed integer type that is not biased, generate
1488 -- a conversion to unsigned to strip high order sign bits.
1490 elsif Is_Signed_Integer_Type (Ctyp) then
1491 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1492 end if;
1494 -- Set Etype, since it can be referenced before the
1495 -- node is completely analyzed.
1497 Set_Etype (Rhs, Etyp);
1499 -- We now need to do an unchecked conversion of the
1500 -- result to the target type, but it is important that
1501 -- this conversion be a right justified conversion and
1502 -- not a left justified conversion.
1504 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1506 end Fixup_Rhs;
1508 begin
1509 if Rhs_Val_Known
1510 and then Compile_Time_Known_Value (Get_Shift)
1511 then
1512 Or_Rhs :=
1513 Make_Integer_Literal (Loc,
1514 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1515 Set_Print_In_Hex (Or_Rhs);
1517 else
1518 -- We have to convert the right hand side to Etype (Obj).
1519 -- A special case case arises if what we have now is a Val
1520 -- attribute reference whose expression type is Etype (Obj).
1521 -- This happens for assignments of fields from the same
1522 -- array. In this case we get the required right hand side
1523 -- by simply removing the inner attribute reference.
1525 if Nkind (Rhs) = N_Attribute_Reference
1526 and then Attribute_Name (Rhs) = Name_Val
1527 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1528 then
1529 Rhs := Relocate_Node (First (Expressions (Rhs)));
1530 Fixup_Rhs;
1532 -- If the value of the right hand side is a known integer
1533 -- value, then just replace it by an untyped constant,
1534 -- which will be properly retyped when we analyze and
1535 -- resolve the expression.
1537 elsif Rhs_Val_Known then
1539 -- Note that Rhs_Val has already been normalized to
1540 -- be an unsigned value with the proper number of bits.
1542 Rhs :=
1543 Make_Integer_Literal (Loc, Rhs_Val);
1545 -- Otherwise we need an unchecked conversion
1547 else
1548 Fixup_Rhs;
1549 end if;
1551 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1552 end if;
1554 if Nkind (New_Rhs) = N_Op_And then
1555 Set_Paren_Count (New_Rhs, 1);
1556 end if;
1558 New_Rhs :=
1559 Make_Op_Or (Loc,
1560 Left_Opnd => New_Rhs,
1561 Right_Opnd => Or_Rhs);
1562 end;
1563 end if;
1565 -- Now do the rewrite
1567 Rewrite (N,
1568 Make_Assignment_Statement (Loc,
1569 Name => New_Lhs,
1570 Expression =>
1571 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1572 Set_Assignment_OK (Name (N), Ass_OK);
1574 -- All other component sizes for non-modular case
1576 else
1577 -- We generate
1579 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1581 -- where Subscr is the computed linear subscript
1583 declare
1584 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1585 Set_nn : Entity_Id;
1586 Subscr : Node_Id;
1587 Atyp : Entity_Id;
1589 begin
1590 if No (Bits_nn) then
1592 -- Error, most likely High_Integrity_Mode restriction
1594 return;
1595 end if;
1597 -- Acquire proper Set entity. We use the aligned or unaligned
1598 -- case as appropriate.
1600 if Known_Aligned_Enough (Obj, Csiz) then
1601 Set_nn := RTE (Set_Id (Csiz));
1602 else
1603 Set_nn := RTE (SetU_Id (Csiz));
1604 end if;
1606 -- Now generate the set reference
1608 Obj := Relocate_Node (Prefix (Lhs));
1609 Convert_To_Actual_Subtype (Obj);
1610 Atyp := Etype (Obj);
1611 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1613 -- Below we must make the assumption that Obj is
1614 -- at least byte aligned, since otherwise its address
1615 -- cannot be taken. The assumption holds since the
1616 -- only arrays that can be misaligned are small packed
1617 -- arrays which are implemented as a modular type, and
1618 -- that is not the case here.
1620 Rewrite (N,
1621 Make_Procedure_Call_Statement (Loc,
1622 Name => New_Occurrence_Of (Set_nn, Loc),
1623 Parameter_Associations => New_List (
1624 Make_Attribute_Reference (Loc,
1625 Attribute_Name => Name_Address,
1626 Prefix => Obj),
1627 Subscr,
1628 Unchecked_Convert_To (Bits_nn,
1629 Convert_To (Ctyp, Rhs)))));
1631 end;
1632 end if;
1634 Analyze (N, Suppress => All_Checks);
1635 end Expand_Bit_Packed_Element_Set;
1637 -------------------------------------
1638 -- Expand_Packed_Address_Reference --
1639 -------------------------------------
1641 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1642 Loc : constant Source_Ptr := Sloc (N);
1643 Ploc : Source_Ptr;
1644 Pref : Node_Id;
1645 Expr : Node_Id;
1646 Term : Node_Id;
1647 Atyp : Entity_Id;
1648 Subscr : Node_Id;
1650 begin
1651 Pref := Prefix (N);
1652 Expr := Empty;
1654 -- We build up an expression serially that has the form
1656 -- outer_object'Address
1657 -- + (linear-subscript * component_size for each array reference
1658 -- + field'Bit_Position for each record field
1659 -- + ...
1660 -- + ...) / Storage_Unit;
1662 -- Some additional conversions are required to deal with the addition
1663 -- operation, which is not normally visible to generated code.
1665 loop
1666 Ploc := Sloc (Pref);
1668 if Nkind (Pref) = N_Indexed_Component then
1669 Convert_To_Actual_Subtype (Prefix (Pref));
1670 Atyp := Etype (Prefix (Pref));
1671 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1673 Term :=
1674 Make_Op_Multiply (Ploc,
1675 Left_Opnd => Subscr,
1676 Right_Opnd =>
1677 Make_Attribute_Reference (Ploc,
1678 Prefix => New_Occurrence_Of (Atyp, Ploc),
1679 Attribute_Name => Name_Component_Size));
1681 elsif Nkind (Pref) = N_Selected_Component then
1682 Term :=
1683 Make_Attribute_Reference (Ploc,
1684 Prefix => Selector_Name (Pref),
1685 Attribute_Name => Name_Bit_Position);
1687 else
1688 exit;
1689 end if;
1691 Term := Convert_To (RTE (RE_Integer_Address), Term);
1693 if No (Expr) then
1694 Expr := Term;
1696 else
1697 Expr :=
1698 Make_Op_Add (Ploc,
1699 Left_Opnd => Expr,
1700 Right_Opnd => Term);
1701 end if;
1703 Pref := Prefix (Pref);
1704 end loop;
1706 Rewrite (N,
1707 Unchecked_Convert_To (RTE (RE_Address),
1708 Make_Op_Add (Loc,
1709 Left_Opnd =>
1710 Unchecked_Convert_To (RTE (RE_Integer_Address),
1711 Make_Attribute_Reference (Loc,
1712 Prefix => Pref,
1713 Attribute_Name => Name_Address)),
1715 Right_Opnd =>
1716 Make_Op_Divide (Loc,
1717 Left_Opnd => Expr,
1718 Right_Opnd =>
1719 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1721 Analyze_And_Resolve (N, RTE (RE_Address));
1722 end Expand_Packed_Address_Reference;
1724 ------------------------------------
1725 -- Expand_Packed_Boolean_Operator --
1726 ------------------------------------
1728 -- This routine expands "a op b" for the packed cases
1730 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1731 Loc : constant Source_Ptr := Sloc (N);
1732 Typ : constant Entity_Id := Etype (N);
1733 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1734 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1736 Ltyp : Entity_Id;
1737 Rtyp : Entity_Id;
1738 PAT : Entity_Id;
1740 begin
1741 Convert_To_Actual_Subtype (L);
1742 Convert_To_Actual_Subtype (R);
1744 Ensure_Defined (Etype (L), N);
1745 Ensure_Defined (Etype (R), N);
1747 Apply_Length_Check (R, Etype (L));
1749 Ltyp := Etype (L);
1750 Rtyp := Etype (R);
1752 -- First an odd and silly test. We explicitly check for the XOR
1753 -- case where the component type is True .. True, since this will
1754 -- raise constraint error. A special check is required since CE
1755 -- will not be required other wise (cf Expand_Packed_Not).
1757 -- No such check is required for AND and OR, since for both these
1758 -- cases False op False = False, and True op True = True.
1760 if Nkind (N) = N_Op_Xor then
1761 declare
1762 CT : constant Entity_Id := Component_Type (Rtyp);
1763 BT : constant Entity_Id := Base_Type (CT);
1765 begin
1766 Insert_Action (N,
1767 Make_Raise_Constraint_Error (Loc,
1768 Condition =>
1769 Make_Op_And (Loc,
1770 Left_Opnd =>
1771 Make_Op_Eq (Loc,
1772 Left_Opnd =>
1773 Make_Attribute_Reference (Loc,
1774 Prefix => New_Occurrence_Of (CT, Loc),
1775 Attribute_Name => Name_First),
1777 Right_Opnd =>
1778 Convert_To (BT,
1779 New_Occurrence_Of (Standard_True, Loc))),
1781 Right_Opnd =>
1782 Make_Op_Eq (Loc,
1783 Left_Opnd =>
1784 Make_Attribute_Reference (Loc,
1785 Prefix => New_Occurrence_Of (CT, Loc),
1786 Attribute_Name => Name_Last),
1788 Right_Opnd =>
1789 Convert_To (BT,
1790 New_Occurrence_Of (Standard_True, Loc)))),
1791 Reason => CE_Range_Check_Failed));
1792 end;
1793 end if;
1795 -- Now that that silliness is taken care of, get packed array type
1797 Convert_To_PAT_Type (L);
1798 Convert_To_PAT_Type (R);
1800 PAT := Etype (L);
1802 -- For the modular case, we expand a op b into
1804 -- rtyp!(pat!(a) op pat!(b))
1806 -- where rtyp is the Etype of the left operand. Note that we do not
1807 -- convert to the base type, since this would be unconstrained, and
1808 -- hence not have a corresponding packed array type set.
1810 -- Note that both operands must be modular for this code to be used
1812 if Is_Modular_Integer_Type (PAT)
1813 and then
1814 Is_Modular_Integer_Type (Etype (R))
1815 then
1816 declare
1817 P : Node_Id;
1819 begin
1820 if Nkind (N) = N_Op_And then
1821 P := Make_Op_And (Loc, L, R);
1823 elsif Nkind (N) = N_Op_Or then
1824 P := Make_Op_Or (Loc, L, R);
1826 else -- Nkind (N) = N_Op_Xor
1827 P := Make_Op_Xor (Loc, L, R);
1828 end if;
1830 Rewrite (N, Unchecked_Convert_To (Ltyp, P));
1831 end;
1833 -- For the array case, we insert the actions
1835 -- Result : Ltype;
1837 -- System.Bitops.Bit_And/Or/Xor
1838 -- (Left'Address,
1839 -- Ltype'Length * Ltype'Component_Size;
1840 -- Right'Address,
1841 -- Rtype'Length * Rtype'Component_Size
1842 -- Result'Address);
1844 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1845 -- the second argument and fourth arguments are the lengths of the
1846 -- operands in bits. Then we replace the expression by a reference
1847 -- to Result.
1849 -- Note that if we are mixing a modular and array operand, everything
1850 -- works fine, since we ensure that the modular representation has the
1851 -- same physical layout as the array representation (that's what the
1852 -- left justified modular stuff in the big-endian case is about).
1854 else
1855 declare
1856 Result_Ent : constant Entity_Id :=
1857 Make_Defining_Identifier (Loc,
1858 Chars => New_Internal_Name ('T'));
1860 E_Id : RE_Id;
1862 begin
1863 if Nkind (N) = N_Op_And then
1864 E_Id := RE_Bit_And;
1866 elsif Nkind (N) = N_Op_Or then
1867 E_Id := RE_Bit_Or;
1869 else -- Nkind (N) = N_Op_Xor
1870 E_Id := RE_Bit_Xor;
1871 end if;
1873 Insert_Actions (N, New_List (
1875 Make_Object_Declaration (Loc,
1876 Defining_Identifier => Result_Ent,
1877 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1879 Make_Procedure_Call_Statement (Loc,
1880 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1881 Parameter_Associations => New_List (
1883 Make_Byte_Aligned_Attribute_Reference (Loc,
1884 Attribute_Name => Name_Address,
1885 Prefix => L),
1887 Make_Op_Multiply (Loc,
1888 Left_Opnd =>
1889 Make_Attribute_Reference (Loc,
1890 Prefix =>
1891 New_Occurrence_Of
1892 (Etype (First_Index (Ltyp)), Loc),
1893 Attribute_Name => Name_Range_Length),
1894 Right_Opnd =>
1895 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1897 Make_Byte_Aligned_Attribute_Reference (Loc,
1898 Attribute_Name => Name_Address,
1899 Prefix => R),
1901 Make_Op_Multiply (Loc,
1902 Left_Opnd =>
1903 Make_Attribute_Reference (Loc,
1904 Prefix =>
1905 New_Occurrence_Of
1906 (Etype (First_Index (Rtyp)), Loc),
1907 Attribute_Name => Name_Range_Length),
1908 Right_Opnd =>
1909 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1911 Make_Byte_Aligned_Attribute_Reference (Loc,
1912 Attribute_Name => Name_Address,
1913 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1915 Rewrite (N,
1916 New_Occurrence_Of (Result_Ent, Loc));
1917 end;
1918 end if;
1920 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1921 end Expand_Packed_Boolean_Operator;
1923 -------------------------------------
1924 -- Expand_Packed_Element_Reference --
1925 -------------------------------------
1927 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1928 Loc : constant Source_Ptr := Sloc (N);
1929 Obj : Node_Id;
1930 Atyp : Entity_Id;
1931 PAT : Entity_Id;
1932 Ctyp : Entity_Id;
1933 Csiz : Int;
1934 Shift : Node_Id;
1935 Cmask : Uint;
1936 Lit : Node_Id;
1937 Arg : Node_Id;
1939 begin
1940 -- If not bit packed, we have the enumeration case, which is easily
1941 -- dealt with (just adjust the subscripts of the indexed component)
1943 -- Note: this leaves the result as an indexed component, which is
1944 -- still a variable, so can be used in the assignment case, as is
1945 -- required in the enumeration case.
1947 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1948 Setup_Enumeration_Packed_Array_Reference (N);
1949 return;
1950 end if;
1952 -- Remaining processing is for the bit-packed case
1954 Obj := Relocate_Node (Prefix (N));
1955 Convert_To_Actual_Subtype (Obj);
1956 Atyp := Etype (Obj);
1957 PAT := Packed_Array_Type (Atyp);
1958 Ctyp := Component_Type (Atyp);
1959 Csiz := UI_To_Int (Component_Size (Atyp));
1961 -- Case of component size 1,2,4 or any component size for the modular
1962 -- case. These are the cases for which we can inline the code.
1964 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1965 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1966 then
1967 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1968 Lit := Make_Integer_Literal (Loc, Cmask);
1969 Set_Print_In_Hex (Lit);
1971 -- We generate a shift right to position the field, followed by a
1972 -- masking operation to extract the bit field, and we finally do an
1973 -- unchecked conversion to convert the result to the required target.
1975 -- Note that the unchecked conversion automatically deals with the
1976 -- bias if we are dealing with a biased representation. What will
1977 -- happen is that we temporarily generate the biased representation,
1978 -- but almost immediately that will be converted to the original
1979 -- unbiased component type, and the bias will disappear.
1981 Arg :=
1982 Make_Op_And (Loc,
1983 Left_Opnd => Make_Shift_Right (Obj, Shift),
1984 Right_Opnd => Lit);
1986 -- We neded to analyze this before we do the unchecked convert
1987 -- below, but we need it temporarily attached to the tree for
1988 -- this analysis (hence the temporary Set_Parent call).
1990 Set_Parent (Arg, Parent (N));
1991 Analyze_And_Resolve (Arg);
1993 Rewrite (N,
1994 RJ_Unchecked_Convert_To (Ctyp, Arg));
1996 -- All other component sizes for non-modular case
1998 else
1999 -- We generate
2001 -- Component_Type!(Get_nn (Arr'address, Subscr))
2003 -- where Subscr is the computed linear subscript
2005 declare
2006 Get_nn : Entity_Id;
2007 Subscr : Node_Id;
2009 begin
2010 -- Acquire proper Get entity. We use the aligned or unaligned
2011 -- case as appropriate.
2013 if Known_Aligned_Enough (Obj, Csiz) then
2014 Get_nn := RTE (Get_Id (Csiz));
2015 else
2016 Get_nn := RTE (GetU_Id (Csiz));
2017 end if;
2019 -- Now generate the get reference
2021 Compute_Linear_Subscript (Atyp, N, Subscr);
2023 -- Below we make the assumption that Obj is at least byte
2024 -- aligned, since otherwise its address cannot be taken.
2025 -- The assumption holds since the only arrays that can be
2026 -- misaligned are small packed arrays which are implemented
2027 -- as a modular type, and that is not the case here.
2029 Rewrite (N,
2030 Unchecked_Convert_To (Ctyp,
2031 Make_Function_Call (Loc,
2032 Name => New_Occurrence_Of (Get_nn, Loc),
2033 Parameter_Associations => New_List (
2034 Make_Attribute_Reference (Loc,
2035 Attribute_Name => Name_Address,
2036 Prefix => Obj),
2037 Subscr))));
2038 end;
2039 end if;
2041 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2043 end Expand_Packed_Element_Reference;
2045 ----------------------
2046 -- Expand_Packed_Eq --
2047 ----------------------
2049 -- Handles expansion of "=" on packed array types
2051 procedure Expand_Packed_Eq (N : Node_Id) is
2052 Loc : constant Source_Ptr := Sloc (N);
2053 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2054 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2056 LLexpr : Node_Id;
2057 RLexpr : Node_Id;
2059 Ltyp : Entity_Id;
2060 Rtyp : Entity_Id;
2061 PAT : Entity_Id;
2063 begin
2064 Convert_To_Actual_Subtype (L);
2065 Convert_To_Actual_Subtype (R);
2066 Ltyp := Underlying_Type (Etype (L));
2067 Rtyp := Underlying_Type (Etype (R));
2069 Convert_To_PAT_Type (L);
2070 Convert_To_PAT_Type (R);
2071 PAT := Etype (L);
2073 LLexpr :=
2074 Make_Op_Multiply (Loc,
2075 Left_Opnd =>
2076 Make_Attribute_Reference (Loc,
2077 Attribute_Name => Name_Length,
2078 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2079 Right_Opnd =>
2080 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2082 RLexpr :=
2083 Make_Op_Multiply (Loc,
2084 Left_Opnd =>
2085 Make_Attribute_Reference (Loc,
2086 Attribute_Name => Name_Length,
2087 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2088 Right_Opnd =>
2089 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2091 -- For the modular case, we transform the comparison to:
2093 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2095 -- where PAT is the packed array type. This works fine, since in the
2096 -- modular case we guarantee that the unused bits are always zeroes.
2097 -- We do have to compare the lengths because we could be comparing
2098 -- two different subtypes of the same base type.
2100 if Is_Modular_Integer_Type (PAT) then
2101 Rewrite (N,
2102 Make_And_Then (Loc,
2103 Left_Opnd =>
2104 Make_Op_Eq (Loc,
2105 Left_Opnd => LLexpr,
2106 Right_Opnd => RLexpr),
2108 Right_Opnd =>
2109 Make_Op_Eq (Loc,
2110 Left_Opnd => L,
2111 Right_Opnd => R)));
2113 -- For the non-modular case, we call a runtime routine
2115 -- System.Bit_Ops.Bit_Eq
2116 -- (L'Address, L_Length, R'Address, R_Length)
2118 -- where PAT is the packed array type, and the lengths are the lengths
2119 -- in bits of the original packed arrays. This routine takes care of
2120 -- not comparing the unused bits in the last byte.
2122 else
2123 Rewrite (N,
2124 Make_Function_Call (Loc,
2125 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2126 Parameter_Associations => New_List (
2127 Make_Byte_Aligned_Attribute_Reference (Loc,
2128 Attribute_Name => Name_Address,
2129 Prefix => L),
2131 LLexpr,
2133 Make_Byte_Aligned_Attribute_Reference (Loc,
2134 Attribute_Name => Name_Address,
2135 Prefix => R),
2137 RLexpr)));
2138 end if;
2140 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2141 end Expand_Packed_Eq;
2143 -----------------------
2144 -- Expand_Packed_Not --
2145 -----------------------
2147 -- Handles expansion of "not" on packed array types
2149 procedure Expand_Packed_Not (N : Node_Id) is
2150 Loc : constant Source_Ptr := Sloc (N);
2151 Typ : constant Entity_Id := Etype (N);
2152 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2154 Rtyp : Entity_Id;
2155 PAT : Entity_Id;
2156 Lit : Node_Id;
2158 begin
2159 Convert_To_Actual_Subtype (Opnd);
2160 Rtyp := Etype (Opnd);
2162 -- First an odd and silly test. We explicitly check for the case
2163 -- where the 'First of the component type is equal to the 'Last of
2164 -- this component type, and if this is the case, we make sure that
2165 -- constraint error is raised. The reason is that the NOT is bound
2166 -- to cause CE in this case, and we will not otherwise catch it.
2168 -- Believe it or not, this was reported as a bug. Note that nearly
2169 -- always, the test will evaluate statically to False, so the code
2170 -- will be statically removed, and no extra overhead caused.
2172 declare
2173 CT : constant Entity_Id := Component_Type (Rtyp);
2175 begin
2176 Insert_Action (N,
2177 Make_Raise_Constraint_Error (Loc,
2178 Condition =>
2179 Make_Op_Eq (Loc,
2180 Left_Opnd =>
2181 Make_Attribute_Reference (Loc,
2182 Prefix => New_Occurrence_Of (CT, Loc),
2183 Attribute_Name => Name_First),
2185 Right_Opnd =>
2186 Make_Attribute_Reference (Loc,
2187 Prefix => New_Occurrence_Of (CT, Loc),
2188 Attribute_Name => Name_Last)),
2189 Reason => CE_Range_Check_Failed));
2190 end;
2192 -- Now that that silliness is taken care of, get packed array type
2194 Convert_To_PAT_Type (Opnd);
2195 PAT := Etype (Opnd);
2197 -- For the case where the packed array type is a modular type,
2198 -- not A expands simply into:
2200 -- rtyp!(PAT!(A) xor mask)
2202 -- where PAT is the packed array type, and mask is a mask of all
2203 -- one bits of length equal to the size of this packed type and
2204 -- rtyp is the actual subtype of the operand
2206 Lit := Make_Integer_Literal (Loc, 2 ** RM_Size (PAT) - 1);
2207 Set_Print_In_Hex (Lit);
2209 if not Is_Array_Type (PAT) then
2210 Rewrite (N,
2211 Unchecked_Convert_To (Rtyp,
2212 Make_Op_Xor (Loc,
2213 Left_Opnd => Opnd,
2214 Right_Opnd => Lit)));
2216 -- For the array case, we insert the actions
2218 -- Result : Typ;
2220 -- System.Bitops.Bit_Not
2221 -- (Opnd'Address,
2222 -- Typ'Length * Typ'Component_Size;
2223 -- Result'Address);
2225 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2226 -- argument is the length of the operand in bits. Then we replace
2227 -- the expression by a reference to Result.
2229 else
2230 declare
2231 Result_Ent : constant Entity_Id :=
2232 Make_Defining_Identifier (Loc,
2233 Chars => New_Internal_Name ('T'));
2235 begin
2236 Insert_Actions (N, New_List (
2238 Make_Object_Declaration (Loc,
2239 Defining_Identifier => Result_Ent,
2240 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2242 Make_Procedure_Call_Statement (Loc,
2243 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2244 Parameter_Associations => New_List (
2246 Make_Byte_Aligned_Attribute_Reference (Loc,
2247 Attribute_Name => Name_Address,
2248 Prefix => Opnd),
2250 Make_Op_Multiply (Loc,
2251 Left_Opnd =>
2252 Make_Attribute_Reference (Loc,
2253 Prefix =>
2254 New_Occurrence_Of
2255 (Etype (First_Index (Rtyp)), Loc),
2256 Attribute_Name => Name_Range_Length),
2257 Right_Opnd =>
2258 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2260 Make_Byte_Aligned_Attribute_Reference (Loc,
2261 Attribute_Name => Name_Address,
2262 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2264 Rewrite (N,
2265 New_Occurrence_Of (Result_Ent, Loc));
2266 end;
2267 end if;
2269 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2271 end Expand_Packed_Not;
2273 -------------------------------------
2274 -- Involves_Packed_Array_Reference --
2275 -------------------------------------
2277 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2278 begin
2279 if Nkind (N) = N_Indexed_Component
2280 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2281 then
2282 return True;
2284 elsif Nkind (N) = N_Selected_Component then
2285 return Involves_Packed_Array_Reference (Prefix (N));
2287 else
2288 return False;
2289 end if;
2290 end Involves_Packed_Array_Reference;
2292 --------------------------
2293 -- Known_Aligned_Enough --
2294 --------------------------
2296 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2297 Typ : constant Entity_Id := Etype (Obj);
2299 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2300 -- If the component is in a record that contains previous packed
2301 -- components, consider it unaligned because the back-end might
2302 -- choose to pack the rest of the record. Lead to less efficient code,
2303 -- but safer vis-a-vis of back-end choices.
2305 --------------------------------
2306 -- In_Partially_Packed_Record --
2307 --------------------------------
2309 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2310 Rec_Type : constant Entity_Id := Scope (Comp);
2311 Prev_Comp : Entity_Id;
2313 begin
2314 Prev_Comp := First_Entity (Rec_Type);
2315 while Present (Prev_Comp) loop
2316 if Is_Packed (Etype (Prev_Comp)) then
2317 return True;
2319 elsif Prev_Comp = Comp then
2320 return False;
2321 end if;
2323 Next_Entity (Prev_Comp);
2324 end loop;
2326 return False;
2327 end In_Partially_Packed_Record;
2329 -- Start of processing for Known_Aligned_Enough
2331 begin
2332 -- Odd bit sizes don't need alignment anyway
2334 if Csiz mod 2 = 1 then
2335 return True;
2337 -- If we have a specified alignment, see if it is sufficient, if not
2338 -- then we can't possibly be aligned enough in any case.
2340 elsif Known_Alignment (Etype (Obj)) then
2341 -- Alignment required is 4 if size is a multiple of 4, and
2342 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2344 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2345 return False;
2346 end if;
2347 end if;
2349 -- OK, alignment should be sufficient, if object is aligned
2351 -- If object is strictly aligned, then it is definitely aligned
2353 if Strict_Alignment (Typ) then
2354 return True;
2356 -- Case of subscripted array reference
2358 elsif Nkind (Obj) = N_Indexed_Component then
2360 -- If we have a pointer to an array, then this is definitely
2361 -- aligned, because pointers always point to aligned versions.
2363 if Is_Access_Type (Etype (Prefix (Obj))) then
2364 return True;
2366 -- Otherwise, go look at the prefix
2368 else
2369 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2370 end if;
2372 -- Case of record field
2374 elsif Nkind (Obj) = N_Selected_Component then
2376 -- What is significant here is whether the record type is packed
2378 if Is_Record_Type (Etype (Prefix (Obj)))
2379 and then Is_Packed (Etype (Prefix (Obj)))
2380 then
2381 return False;
2383 -- Or the component has a component clause which might cause
2384 -- the component to become unaligned (we can't tell if the
2385 -- backend is doing alignment computations).
2387 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2388 return False;
2390 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2391 return False;
2393 -- In all other cases, go look at prefix
2395 else
2396 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2397 end if;
2399 elsif Nkind (Obj) = N_Type_Conversion then
2400 return Known_Aligned_Enough (Expression (Obj), Csiz);
2402 -- For a formal parameter, it is safer to assume that it is not
2403 -- aligned, because the formal may be unconstrained while the actual
2404 -- is constrained. In this situation, a small constrained packed
2405 -- array, represented in modular form, may be unaligned.
2407 elsif Is_Entity_Name (Obj) then
2408 return not Is_Formal (Entity (Obj));
2409 else
2411 -- If none of the above, must be aligned
2412 return True;
2413 end if;
2414 end Known_Aligned_Enough;
2416 ---------------------
2417 -- Make_Shift_Left --
2418 ---------------------
2420 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2421 Nod : Node_Id;
2423 begin
2424 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2425 return N;
2426 else
2427 Nod :=
2428 Make_Op_Shift_Left (Sloc (N),
2429 Left_Opnd => N,
2430 Right_Opnd => S);
2431 Set_Shift_Count_OK (Nod, True);
2432 return Nod;
2433 end if;
2434 end Make_Shift_Left;
2436 ----------------------
2437 -- Make_Shift_Right --
2438 ----------------------
2440 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2441 Nod : Node_Id;
2443 begin
2444 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2445 return N;
2446 else
2447 Nod :=
2448 Make_Op_Shift_Right (Sloc (N),
2449 Left_Opnd => N,
2450 Right_Opnd => S);
2451 Set_Shift_Count_OK (Nod, True);
2452 return Nod;
2453 end if;
2454 end Make_Shift_Right;
2456 -----------------------------
2457 -- RJ_Unchecked_Convert_To --
2458 -----------------------------
2460 function RJ_Unchecked_Convert_To
2461 (Typ : Entity_Id;
2462 Expr : Node_Id) return Node_Id
2464 Source_Typ : constant Entity_Id := Etype (Expr);
2465 Target_Typ : constant Entity_Id := Typ;
2467 Src : Node_Id := Expr;
2469 Source_Siz : Nat;
2470 Target_Siz : Nat;
2472 begin
2473 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2474 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2476 -- First step, if the source type is not a discrete type, then we
2477 -- first convert to a modular type of the source length, since
2478 -- otherwise, on a big-endian machine, we get left-justification.
2479 -- We do it for little-endian machines as well, because there might
2480 -- be junk bits that are not cleared if the type is not numeric.
2482 if Source_Siz /= Target_Siz
2483 and then not Is_Discrete_Type (Source_Typ)
2484 then
2485 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2486 end if;
2488 -- In the big endian case, if the lengths of the two types differ,
2489 -- then we must worry about possible left justification in the
2490 -- conversion, and avoiding that is what this is all about.
2492 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2494 -- Next step. If the target is not a discrete type, then we first
2495 -- convert to a modular type of the target length, since
2496 -- otherwise, on a big-endian machine, we get left-justification.
2498 if not Is_Discrete_Type (Target_Typ) then
2499 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2500 end if;
2501 end if;
2503 -- And now we can do the final conversion to the target type
2505 return Unchecked_Convert_To (Target_Typ, Src);
2506 end RJ_Unchecked_Convert_To;
2508 ----------------------------------------------
2509 -- Setup_Enumeration_Packed_Array_Reference --
2510 ----------------------------------------------
2512 -- All we have to do here is to find the subscripts that correspond
2513 -- to the index positions that have non-standard enumeration types
2514 -- and insert a Pos attribute to get the proper subscript value.
2516 -- Finally the prefix must be uncheck converted to the corresponding
2517 -- packed array type.
2519 -- Note that the component type is unchanged, so we do not need to
2520 -- fiddle with the types (Gigi always automatically takes the packed
2521 -- array type if it is set, as it will be in this case).
2523 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2524 Pfx : constant Node_Id := Prefix (N);
2525 Typ : constant Entity_Id := Etype (N);
2526 Exprs : constant List_Id := Expressions (N);
2527 Expr : Node_Id;
2529 begin
2530 -- If the array is unconstrained, then we replace the array
2531 -- reference with its actual subtype. This actual subtype will
2532 -- have a packed array type with appropriate bounds.
2534 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2535 Convert_To_Actual_Subtype (Pfx);
2536 end if;
2538 Expr := First (Exprs);
2539 while Present (Expr) loop
2540 declare
2541 Loc : constant Source_Ptr := Sloc (Expr);
2542 Expr_Typ : constant Entity_Id := Etype (Expr);
2544 begin
2545 if Is_Enumeration_Type (Expr_Typ)
2546 and then Has_Non_Standard_Rep (Expr_Typ)
2547 then
2548 Rewrite (Expr,
2549 Make_Attribute_Reference (Loc,
2550 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2551 Attribute_Name => Name_Pos,
2552 Expressions => New_List (Relocate_Node (Expr))));
2553 Analyze_And_Resolve (Expr, Standard_Natural);
2554 end if;
2555 end;
2557 Next (Expr);
2558 end loop;
2560 Rewrite (N,
2561 Make_Indexed_Component (Sloc (N),
2562 Prefix =>
2563 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2564 Expressions => Exprs));
2566 Analyze_And_Resolve (N, Typ);
2568 end Setup_Enumeration_Packed_Array_Reference;
2570 -----------------------------------------
2571 -- Setup_Inline_Packed_Array_Reference --
2572 -----------------------------------------
2574 procedure Setup_Inline_Packed_Array_Reference
2575 (N : Node_Id;
2576 Atyp : Entity_Id;
2577 Obj : in out Node_Id;
2578 Cmask : out Uint;
2579 Shift : out Node_Id)
2581 Loc : constant Source_Ptr := Sloc (N);
2582 PAT : Entity_Id;
2583 Otyp : Entity_Id;
2584 Csiz : Uint;
2585 Osiz : Uint;
2587 begin
2588 Csiz := Component_Size (Atyp);
2590 Convert_To_PAT_Type (Obj);
2591 PAT := Etype (Obj);
2593 Cmask := 2 ** Csiz - 1;
2595 if Is_Array_Type (PAT) then
2596 Otyp := Component_Type (PAT);
2597 Osiz := Component_Size (PAT);
2599 else
2600 Otyp := PAT;
2602 -- In the case where the PAT is a modular type, we want the actual
2603 -- size in bits of the modular value we use. This is neither the
2604 -- Object_Size nor the Value_Size, either of which may have been
2605 -- reset to strange values, but rather the minimum size. Note that
2606 -- since this is a modular type with full range, the issue of
2607 -- biased representation does not arise.
2609 Osiz := UI_From_Int (Minimum_Size (Otyp));
2610 end if;
2612 Compute_Linear_Subscript (Atyp, N, Shift);
2614 -- If the component size is not 1, then the subscript must be
2615 -- multiplied by the component size to get the shift count.
2617 if Csiz /= 1 then
2618 Shift :=
2619 Make_Op_Multiply (Loc,
2620 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2621 Right_Opnd => Shift);
2622 end if;
2624 -- If we have the array case, then this shift count must be broken
2625 -- down into a byte subscript, and a shift within the byte.
2627 if Is_Array_Type (PAT) then
2629 declare
2630 New_Shift : Node_Id;
2632 begin
2633 -- We must analyze shift, since we will duplicate it
2635 Set_Parent (Shift, N);
2636 Analyze_And_Resolve
2637 (Shift, Standard_Integer, Suppress => All_Checks);
2639 -- The shift count within the word is
2640 -- shift mod Osiz
2642 New_Shift :=
2643 Make_Op_Mod (Loc,
2644 Left_Opnd => Duplicate_Subexpr (Shift),
2645 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2647 -- The subscript to be used on the PAT array is
2648 -- shift / Osiz
2650 Obj :=
2651 Make_Indexed_Component (Loc,
2652 Prefix => Obj,
2653 Expressions => New_List (
2654 Make_Op_Divide (Loc,
2655 Left_Opnd => Duplicate_Subexpr (Shift),
2656 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2658 Shift := New_Shift;
2659 end;
2661 -- For the modular integer case, the object to be manipulated is
2662 -- the entire array, so Obj is unchanged. Note that we will reset
2663 -- its type to PAT before returning to the caller.
2665 else
2666 null;
2667 end if;
2669 -- The one remaining step is to modify the shift count for the
2670 -- big-endian case. Consider the following example in a byte:
2672 -- xxxxxxxx bits of byte
2673 -- vvvvvvvv bits of value
2674 -- 33221100 little-endian numbering
2675 -- 00112233 big-endian numbering
2677 -- Here we have the case of 2-bit fields
2679 -- For the little-endian case, we already have the proper shift
2680 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2682 -- For the big endian case, we have to adjust the shift count,
2683 -- computing it as (N - F) - shift, where N is the number of bits
2684 -- in an element of the array used to implement the packed array,
2685 -- F is the number of bits in a source level array element, and
2686 -- shift is the count so far computed.
2688 if Bytes_Big_Endian then
2689 Shift :=
2690 Make_Op_Subtract (Loc,
2691 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2692 Right_Opnd => Shift);
2693 end if;
2695 Set_Parent (Shift, N);
2696 Set_Parent (Obj, N);
2697 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2698 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2700 -- Make sure final type of object is the appropriate packed type
2702 Set_Etype (Obj, Otyp);
2704 end Setup_Inline_Packed_Array_Reference;
2706 end Exp_Pakd;