Merge from mainline
[official-gcc.git] / gcc / ada / exp_pakd.adb
blob7c84a6d85f323ef3e84bd619540f9071123f6260
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-2005, 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Einfo; use Einfo;
30 with Errout; use Errout;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Nlists; use Nlists;
34 with Nmake; use Nmake;
35 with Rtsfind; use Rtsfind;
36 with Sem; use Sem;
37 with Sem_Ch3; use Sem_Ch3;
38 with Sem_Ch8; use Sem_Ch8;
39 with Sem_Ch13; use Sem_Ch13;
40 with Sem_Eval; use Sem_Eval;
41 with Sem_Res; use Sem_Res;
42 with Sem_Util; use Sem_Util;
43 with Sinfo; use Sinfo;
44 with Snames; use Snames;
45 with Stand; use Stand;
46 with Targparm; use Targparm;
47 with Tbuild; use Tbuild;
48 with Ttypes; use Ttypes;
49 with Uintp; use Uintp;
51 package body Exp_Pakd is
53 ---------------------------
54 -- Endian Considerations --
55 ---------------------------
57 -- As described in the specification, bit numbering in a packed array
58 -- is consistent with bit numbering in a record representation clause,
59 -- and hence dependent on the endianness of the machine:
61 -- For little-endian machines, element zero is at the right hand end
62 -- (low order end) of a bit field.
64 -- For big-endian machines, element zero is at the left hand end
65 -- (high order end) of a bit field.
67 -- The shifts that are used to right justify a field therefore differ
68 -- in the two cases. For the little-endian case, we can simply use the
69 -- bit number (i.e. the element number * element size) as the count for
70 -- a right shift. For the big-endian case, we have to subtract the shift
71 -- count from an appropriate constant to use in the right shift. We use
72 -- rotates instead of shifts (which is necessary in the store case to
73 -- preserve other fields), and we expect that the backend will be able
74 -- to change the right rotate into a left rotate, avoiding the subtract,
75 -- if the architecture provides such an instruction.
77 ----------------------------------------------
78 -- Entity Tables for Packed Access Routines --
79 ----------------------------------------------
81 -- For the cases of component size = 3,5-7,9-15,17-31,33-63 we call
82 -- library routines. This table is used to obtain the entity for the
83 -- proper routine.
85 type E_Array is array (Int range 01 .. 63) of RE_Id;
87 -- Array of Bits_nn entities. Note that we do not use library routines
88 -- for the 8-bit and 16-bit cases, but we still fill in the table, using
89 -- entries from System.Unsigned, because we also use this table for
90 -- certain special unchecked conversions in the big-endian case.
92 Bits_Id : constant E_Array :=
93 (01 => RE_Bits_1,
94 02 => RE_Bits_2,
95 03 => RE_Bits_03,
96 04 => RE_Bits_4,
97 05 => RE_Bits_05,
98 06 => RE_Bits_06,
99 07 => RE_Bits_07,
100 08 => RE_Unsigned_8,
101 09 => RE_Bits_09,
102 10 => RE_Bits_10,
103 11 => RE_Bits_11,
104 12 => RE_Bits_12,
105 13 => RE_Bits_13,
106 14 => RE_Bits_14,
107 15 => RE_Bits_15,
108 16 => RE_Unsigned_16,
109 17 => RE_Bits_17,
110 18 => RE_Bits_18,
111 19 => RE_Bits_19,
112 20 => RE_Bits_20,
113 21 => RE_Bits_21,
114 22 => RE_Bits_22,
115 23 => RE_Bits_23,
116 24 => RE_Bits_24,
117 25 => RE_Bits_25,
118 26 => RE_Bits_26,
119 27 => RE_Bits_27,
120 28 => RE_Bits_28,
121 29 => RE_Bits_29,
122 30 => RE_Bits_30,
123 31 => RE_Bits_31,
124 32 => RE_Unsigned_32,
125 33 => RE_Bits_33,
126 34 => RE_Bits_34,
127 35 => RE_Bits_35,
128 36 => RE_Bits_36,
129 37 => RE_Bits_37,
130 38 => RE_Bits_38,
131 39 => RE_Bits_39,
132 40 => RE_Bits_40,
133 41 => RE_Bits_41,
134 42 => RE_Bits_42,
135 43 => RE_Bits_43,
136 44 => RE_Bits_44,
137 45 => RE_Bits_45,
138 46 => RE_Bits_46,
139 47 => RE_Bits_47,
140 48 => RE_Bits_48,
141 49 => RE_Bits_49,
142 50 => RE_Bits_50,
143 51 => RE_Bits_51,
144 52 => RE_Bits_52,
145 53 => RE_Bits_53,
146 54 => RE_Bits_54,
147 55 => RE_Bits_55,
148 56 => RE_Bits_56,
149 57 => RE_Bits_57,
150 58 => RE_Bits_58,
151 59 => RE_Bits_59,
152 60 => RE_Bits_60,
153 61 => RE_Bits_61,
154 62 => RE_Bits_62,
155 63 => RE_Bits_63);
157 -- Array of Get routine entities. These are used to obtain an element
158 -- from a packed array. The N'th entry is used to obtain elements from
159 -- a packed array whose component size is N. RE_Null is used as a null
160 -- entry, for the cases where a library routine is not used.
162 Get_Id : constant E_Array :=
163 (01 => RE_Null,
164 02 => RE_Null,
165 03 => RE_Get_03,
166 04 => RE_Null,
167 05 => RE_Get_05,
168 06 => RE_Get_06,
169 07 => RE_Get_07,
170 08 => RE_Null,
171 09 => RE_Get_09,
172 10 => RE_Get_10,
173 11 => RE_Get_11,
174 12 => RE_Get_12,
175 13 => RE_Get_13,
176 14 => RE_Get_14,
177 15 => RE_Get_15,
178 16 => RE_Null,
179 17 => RE_Get_17,
180 18 => RE_Get_18,
181 19 => RE_Get_19,
182 20 => RE_Get_20,
183 21 => RE_Get_21,
184 22 => RE_Get_22,
185 23 => RE_Get_23,
186 24 => RE_Get_24,
187 25 => RE_Get_25,
188 26 => RE_Get_26,
189 27 => RE_Get_27,
190 28 => RE_Get_28,
191 29 => RE_Get_29,
192 30 => RE_Get_30,
193 31 => RE_Get_31,
194 32 => RE_Null,
195 33 => RE_Get_33,
196 34 => RE_Get_34,
197 35 => RE_Get_35,
198 36 => RE_Get_36,
199 37 => RE_Get_37,
200 38 => RE_Get_38,
201 39 => RE_Get_39,
202 40 => RE_Get_40,
203 41 => RE_Get_41,
204 42 => RE_Get_42,
205 43 => RE_Get_43,
206 44 => RE_Get_44,
207 45 => RE_Get_45,
208 46 => RE_Get_46,
209 47 => RE_Get_47,
210 48 => RE_Get_48,
211 49 => RE_Get_49,
212 50 => RE_Get_50,
213 51 => RE_Get_51,
214 52 => RE_Get_52,
215 53 => RE_Get_53,
216 54 => RE_Get_54,
217 55 => RE_Get_55,
218 56 => RE_Get_56,
219 57 => RE_Get_57,
220 58 => RE_Get_58,
221 59 => RE_Get_59,
222 60 => RE_Get_60,
223 61 => RE_Get_61,
224 62 => RE_Get_62,
225 63 => RE_Get_63);
227 -- Array of Get routine entities to be used in the case where the packed
228 -- array is itself a component of a packed structure, and therefore may
229 -- not be fully aligned. This only affects the even sizes, since for the
230 -- odd sizes, we do not get any fixed alignment in any case.
232 GetU_Id : constant E_Array :=
233 (01 => RE_Null,
234 02 => RE_Null,
235 03 => RE_Get_03,
236 04 => RE_Null,
237 05 => RE_Get_05,
238 06 => RE_GetU_06,
239 07 => RE_Get_07,
240 08 => RE_Null,
241 09 => RE_Get_09,
242 10 => RE_GetU_10,
243 11 => RE_Get_11,
244 12 => RE_GetU_12,
245 13 => RE_Get_13,
246 14 => RE_GetU_14,
247 15 => RE_Get_15,
248 16 => RE_Null,
249 17 => RE_Get_17,
250 18 => RE_GetU_18,
251 19 => RE_Get_19,
252 20 => RE_GetU_20,
253 21 => RE_Get_21,
254 22 => RE_GetU_22,
255 23 => RE_Get_23,
256 24 => RE_GetU_24,
257 25 => RE_Get_25,
258 26 => RE_GetU_26,
259 27 => RE_Get_27,
260 28 => RE_GetU_28,
261 29 => RE_Get_29,
262 30 => RE_GetU_30,
263 31 => RE_Get_31,
264 32 => RE_Null,
265 33 => RE_Get_33,
266 34 => RE_GetU_34,
267 35 => RE_Get_35,
268 36 => RE_GetU_36,
269 37 => RE_Get_37,
270 38 => RE_GetU_38,
271 39 => RE_Get_39,
272 40 => RE_GetU_40,
273 41 => RE_Get_41,
274 42 => RE_GetU_42,
275 43 => RE_Get_43,
276 44 => RE_GetU_44,
277 45 => RE_Get_45,
278 46 => RE_GetU_46,
279 47 => RE_Get_47,
280 48 => RE_GetU_48,
281 49 => RE_Get_49,
282 50 => RE_GetU_50,
283 51 => RE_Get_51,
284 52 => RE_GetU_52,
285 53 => RE_Get_53,
286 54 => RE_GetU_54,
287 55 => RE_Get_55,
288 56 => RE_GetU_56,
289 57 => RE_Get_57,
290 58 => RE_GetU_58,
291 59 => RE_Get_59,
292 60 => RE_GetU_60,
293 61 => RE_Get_61,
294 62 => RE_GetU_62,
295 63 => RE_Get_63);
297 -- Array of Set routine entities. These are used to assign an element
298 -- of a packed array. The N'th entry is used to assign elements for
299 -- a packed array whose component size is N. RE_Null is used as a null
300 -- entry, for the cases where a library routine is not used.
302 Set_Id : constant E_Array :=
303 (01 => RE_Null,
304 02 => RE_Null,
305 03 => RE_Set_03,
306 04 => RE_Null,
307 05 => RE_Set_05,
308 06 => RE_Set_06,
309 07 => RE_Set_07,
310 08 => RE_Null,
311 09 => RE_Set_09,
312 10 => RE_Set_10,
313 11 => RE_Set_11,
314 12 => RE_Set_12,
315 13 => RE_Set_13,
316 14 => RE_Set_14,
317 15 => RE_Set_15,
318 16 => RE_Null,
319 17 => RE_Set_17,
320 18 => RE_Set_18,
321 19 => RE_Set_19,
322 20 => RE_Set_20,
323 21 => RE_Set_21,
324 22 => RE_Set_22,
325 23 => RE_Set_23,
326 24 => RE_Set_24,
327 25 => RE_Set_25,
328 26 => RE_Set_26,
329 27 => RE_Set_27,
330 28 => RE_Set_28,
331 29 => RE_Set_29,
332 30 => RE_Set_30,
333 31 => RE_Set_31,
334 32 => RE_Null,
335 33 => RE_Set_33,
336 34 => RE_Set_34,
337 35 => RE_Set_35,
338 36 => RE_Set_36,
339 37 => RE_Set_37,
340 38 => RE_Set_38,
341 39 => RE_Set_39,
342 40 => RE_Set_40,
343 41 => RE_Set_41,
344 42 => RE_Set_42,
345 43 => RE_Set_43,
346 44 => RE_Set_44,
347 45 => RE_Set_45,
348 46 => RE_Set_46,
349 47 => RE_Set_47,
350 48 => RE_Set_48,
351 49 => RE_Set_49,
352 50 => RE_Set_50,
353 51 => RE_Set_51,
354 52 => RE_Set_52,
355 53 => RE_Set_53,
356 54 => RE_Set_54,
357 55 => RE_Set_55,
358 56 => RE_Set_56,
359 57 => RE_Set_57,
360 58 => RE_Set_58,
361 59 => RE_Set_59,
362 60 => RE_Set_60,
363 61 => RE_Set_61,
364 62 => RE_Set_62,
365 63 => RE_Set_63);
367 -- Array of Set routine entities to be used in the case where the packed
368 -- array is itself a component of a packed structure, and therefore may
369 -- not be fully aligned. This only affects the even sizes, since for the
370 -- odd sizes, we do not get any fixed alignment in any case.
372 SetU_Id : constant E_Array :=
373 (01 => RE_Null,
374 02 => RE_Null,
375 03 => RE_Set_03,
376 04 => RE_Null,
377 05 => RE_Set_05,
378 06 => RE_SetU_06,
379 07 => RE_Set_07,
380 08 => RE_Null,
381 09 => RE_Set_09,
382 10 => RE_SetU_10,
383 11 => RE_Set_11,
384 12 => RE_SetU_12,
385 13 => RE_Set_13,
386 14 => RE_SetU_14,
387 15 => RE_Set_15,
388 16 => RE_Null,
389 17 => RE_Set_17,
390 18 => RE_SetU_18,
391 19 => RE_Set_19,
392 20 => RE_SetU_20,
393 21 => RE_Set_21,
394 22 => RE_SetU_22,
395 23 => RE_Set_23,
396 24 => RE_SetU_24,
397 25 => RE_Set_25,
398 26 => RE_SetU_26,
399 27 => RE_Set_27,
400 28 => RE_SetU_28,
401 29 => RE_Set_29,
402 30 => RE_SetU_30,
403 31 => RE_Set_31,
404 32 => RE_Null,
405 33 => RE_Set_33,
406 34 => RE_SetU_34,
407 35 => RE_Set_35,
408 36 => RE_SetU_36,
409 37 => RE_Set_37,
410 38 => RE_SetU_38,
411 39 => RE_Set_39,
412 40 => RE_SetU_40,
413 41 => RE_Set_41,
414 42 => RE_SetU_42,
415 43 => RE_Set_43,
416 44 => RE_SetU_44,
417 45 => RE_Set_45,
418 46 => RE_SetU_46,
419 47 => RE_Set_47,
420 48 => RE_SetU_48,
421 49 => RE_Set_49,
422 50 => RE_SetU_50,
423 51 => RE_Set_51,
424 52 => RE_SetU_52,
425 53 => RE_Set_53,
426 54 => RE_SetU_54,
427 55 => RE_Set_55,
428 56 => RE_SetU_56,
429 57 => RE_Set_57,
430 58 => RE_SetU_58,
431 59 => RE_Set_59,
432 60 => RE_SetU_60,
433 61 => RE_Set_61,
434 62 => RE_SetU_62,
435 63 => RE_Set_63);
437 -----------------------
438 -- Local Subprograms --
439 -----------------------
441 procedure Compute_Linear_Subscript
442 (Atyp : Entity_Id;
443 N : Node_Id;
444 Subscr : out Node_Id);
445 -- Given a constrained array type Atyp, and an indexed component node
446 -- N referencing an array object of this type, build an expression of
447 -- type Standard.Integer representing the zero-based linear subscript
448 -- value. This expression includes any required range checks.
450 procedure Convert_To_PAT_Type (Aexp : Node_Id);
451 -- Given an expression of a packed array type, builds a corresponding
452 -- expression whose type is the implementation type used to represent
453 -- the packed array. Aexp is analyzed and resolved on entry and on exit.
455 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean;
456 -- There are two versions of the Set routines, the ones used when the
457 -- object is known to be sufficiently well aligned given the number of
458 -- bits, and the ones used when the object is not known to be aligned.
459 -- This routine is used to determine which set to use. Obj is a reference
460 -- to the object, and Csiz is the component size of the packed array.
461 -- True is returned if the alignment of object is known to be sufficient,
462 -- defined as 1 for odd bit sizes, 4 for bit sizes divisible by 4, and
463 -- 2 otherwise.
465 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id;
466 -- Build a left shift node, checking for the case of a shift count of zero
468 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id;
469 -- Build a right shift node, checking for the case of a shift count of zero
471 function RJ_Unchecked_Convert_To
472 (Typ : Entity_Id;
473 Expr : Node_Id) return Node_Id;
474 -- The packed array code does unchecked conversions which in some cases
475 -- may involve non-discrete types with differing sizes. The semantics of
476 -- such conversions is potentially endian dependent, and the effect we
477 -- want here for such a conversion is to do the conversion in size as
478 -- though numeric items are involved, and we extend or truncate on the
479 -- left side. This happens naturally in the little-endian case, but in
480 -- the big endian case we can get left justification, when what we want
481 -- is right justification. This routine does the unchecked conversion in
482 -- a stepwise manner to ensure that it gives the expected result. Hence
483 -- the name (RJ = Right justified). The parameters Typ and Expr are as
484 -- for the case of a normal Unchecked_Convert_To call.
486 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id);
487 -- This routine is called in the Get and Set case for arrays that are
488 -- packed but not bit-packed, meaning that they have at least one
489 -- subscript that is of an enumeration type with a non-standard
490 -- representation. This routine modifies the given node to properly
491 -- reference the corresponding packed array type.
493 procedure Setup_Inline_Packed_Array_Reference
494 (N : Node_Id;
495 Atyp : Entity_Id;
496 Obj : in out Node_Id;
497 Cmask : out Uint;
498 Shift : out Node_Id);
499 -- This procedure performs common processing on the N_Indexed_Component
500 -- parameter given as N, whose prefix is a reference to a packed array.
501 -- This is used for the get and set when the component size is 1,2,4
502 -- or for other component sizes when the packed array type is a modular
503 -- type (i.e. the cases that are handled with inline code).
505 -- On entry:
507 -- N is the N_Indexed_Component node for the packed array reference
509 -- Atyp is the constrained array type (the actual subtype has been
510 -- computed if necessary to obtain the constraints, but this is still
511 -- the original array type, not the Packed_Array_Type value).
513 -- Obj is the object which is to be indexed. It is always of type Atyp.
515 -- On return:
517 -- Obj is the object containing the desired bit field. It is of type
518 -- Unsigned, Long_Unsigned, or Long_Long_Unsigned, and is either the
519 -- entire value, for the small static case, or the proper selected byte
520 -- from the array in the large or dynamic case. This node is analyzed
521 -- and resolved on return.
523 -- Shift is a node representing the shift count to be used in the
524 -- rotate right instruction that positions the field for access.
525 -- This node is analyzed and resolved on return.
527 -- Cmask is a mask corresponding to the width of the component field.
528 -- Its value is 2 ** Csize - 1 (e.g. 2#1111# for component size of 4).
530 -- Note: in some cases the call to this routine may generate actions
531 -- (for handling multi-use references and the generation of the packed
532 -- array type on the fly). Such actions are inserted into the tree
533 -- directly using Insert_Action.
535 ------------------------------
536 -- Compute_Linear_Subcsript --
537 ------------------------------
539 procedure Compute_Linear_Subscript
540 (Atyp : Entity_Id;
541 N : Node_Id;
542 Subscr : out Node_Id)
544 Loc : constant Source_Ptr := Sloc (N);
545 Oldsub : Node_Id;
546 Newsub : Node_Id;
547 Indx : Node_Id;
548 Styp : Entity_Id;
550 begin
551 Subscr := Empty;
553 -- Loop through dimensions
555 Indx := First_Index (Atyp);
556 Oldsub := First (Expressions (N));
558 while Present (Indx) loop
559 Styp := Etype (Indx);
560 Newsub := Relocate_Node (Oldsub);
562 -- Get expression for the subscript value. First, if Do_Range_Check
563 -- is set on a subscript, then we must do a range check against the
564 -- original bounds (not the bounds of the packed array type). We do
565 -- this by introducing a subtype conversion.
567 if Do_Range_Check (Newsub)
568 and then Etype (Newsub) /= Styp
569 then
570 Newsub := Convert_To (Styp, Newsub);
571 end if;
573 -- Now evolve the expression for the subscript. First convert
574 -- the subscript to be zero based and of an integer type.
576 -- Case of integer type, where we just subtract to get lower bound
578 if Is_Integer_Type (Styp) then
580 -- If length of integer type is smaller than standard integer,
581 -- then we convert to integer first, then do the subtract
583 -- Integer (subscript) - Integer (Styp'First)
585 if Esize (Styp) < Esize (Standard_Integer) then
586 Newsub :=
587 Make_Op_Subtract (Loc,
588 Left_Opnd => Convert_To (Standard_Integer, Newsub),
589 Right_Opnd =>
590 Convert_To (Standard_Integer,
591 Make_Attribute_Reference (Loc,
592 Prefix => New_Occurrence_Of (Styp, Loc),
593 Attribute_Name => Name_First)));
595 -- For larger integer types, subtract first, then convert to
596 -- integer, this deals with strange long long integer bounds.
598 -- Integer (subscript - Styp'First)
600 else
601 Newsub :=
602 Convert_To (Standard_Integer,
603 Make_Op_Subtract (Loc,
604 Left_Opnd => Newsub,
605 Right_Opnd =>
606 Make_Attribute_Reference (Loc,
607 Prefix => New_Occurrence_Of (Styp, Loc),
608 Attribute_Name => Name_First)));
609 end if;
611 -- For the enumeration case, we have to use 'Pos to get the value
612 -- to work with before subtracting the lower bound.
614 -- Integer (Styp'Pos (subscr)) - Integer (Styp'Pos (Styp'First));
616 -- This is not quite right for bizarre cases where the size of the
617 -- enumeration type is > Integer'Size bits due to rep clause ???
619 else
620 pragma Assert (Is_Enumeration_Type (Styp));
622 Newsub :=
623 Make_Op_Subtract (Loc,
624 Left_Opnd => Convert_To (Standard_Integer,
625 Make_Attribute_Reference (Loc,
626 Prefix => New_Occurrence_Of (Styp, Loc),
627 Attribute_Name => Name_Pos,
628 Expressions => New_List (Newsub))),
630 Right_Opnd =>
631 Convert_To (Standard_Integer,
632 Make_Attribute_Reference (Loc,
633 Prefix => New_Occurrence_Of (Styp, Loc),
634 Attribute_Name => Name_Pos,
635 Expressions => New_List (
636 Make_Attribute_Reference (Loc,
637 Prefix => New_Occurrence_Of (Styp, Loc),
638 Attribute_Name => Name_First)))));
639 end if;
641 Set_Paren_Count (Newsub, 1);
643 -- For the first subscript, we just copy that subscript value
645 if No (Subscr) then
646 Subscr := Newsub;
648 -- Otherwise, we must multiply what we already have by the current
649 -- stride and then add in the new value to the evolving subscript.
651 else
652 Subscr :=
653 Make_Op_Add (Loc,
654 Left_Opnd =>
655 Make_Op_Multiply (Loc,
656 Left_Opnd => Subscr,
657 Right_Opnd =>
658 Make_Attribute_Reference (Loc,
659 Attribute_Name => Name_Range_Length,
660 Prefix => New_Occurrence_Of (Styp, Loc))),
661 Right_Opnd => Newsub);
662 end if;
664 -- Move to next subscript
666 Next_Index (Indx);
667 Next (Oldsub);
668 end loop;
669 end Compute_Linear_Subscript;
671 -------------------------
672 -- Convert_To_PAT_Type --
673 -------------------------
675 -- The PAT is always obtained from the actual subtype
677 procedure Convert_To_PAT_Type (Aexp : Node_Id) is
678 Act_ST : Entity_Id;
680 begin
681 Convert_To_Actual_Subtype (Aexp);
682 Act_ST := Underlying_Type (Etype (Aexp));
683 Create_Packed_Array_Type (Act_ST);
685 -- Just replace the eEype with the packed array type. This works
686 -- because the expression will not be further analyzed, and Gigi
687 -- considers the two types equivalent in any case.
689 -- This is not strictly the case ??? If the reference is an actual in
690 -- call, the expansion of the prefix is delayed, and must be reanalyzed,
691 -- see Reset_Packed_Prefix. On the other hand, if the prefix is a simple
692 -- array reference, reanalysis can produce spurious type errors when the
693 -- PAT type is replaced again with the original type of the array. Same
694 -- for the case of a dereference. The following is correct and minimal,
695 -- but the handling of more complex packed expressions in actuals is
696 -- confused. Probably the problem only remains for actuals in calls.
698 Set_Etype (Aexp, Packed_Array_Type (Act_ST));
700 if Is_Entity_Name (Aexp)
701 or else
702 (Nkind (Aexp) = N_Indexed_Component
703 and then Is_Entity_Name (Prefix (Aexp)))
704 or else Nkind (Aexp) = N_Explicit_Dereference
705 then
706 Set_Analyzed (Aexp);
707 end if;
708 end Convert_To_PAT_Type;
710 ------------------------------
711 -- Create_Packed_Array_Type --
712 ------------------------------
714 procedure Create_Packed_Array_Type (Typ : Entity_Id) is
715 Loc : constant Source_Ptr := Sloc (Typ);
716 Ctyp : constant Entity_Id := Component_Type (Typ);
717 Csize : constant Uint := Component_Size (Typ);
719 Ancest : Entity_Id;
720 PB_Type : Entity_Id;
721 PASize : Uint;
722 Decl : Node_Id;
723 PAT : Entity_Id;
724 Len_Dim : Node_Id;
725 Len_Expr : Node_Id;
726 Len_Bits : Uint;
727 Bits_U1 : Node_Id;
728 PAT_High : Node_Id;
729 Btyp : Entity_Id;
730 Lit : Node_Id;
732 procedure Install_PAT;
733 -- This procedure is called with Decl set to the declaration for the
734 -- packed array type. It creates the type and installs it as required.
736 procedure Set_PB_Type;
737 -- Sets PB_Type to Packed_Bytes{1,2,4} as required by the alignment
738 -- requirements (see documentation in the spec of this package).
740 -----------------
741 -- Install_PAT --
742 -----------------
744 procedure Install_PAT is
745 Pushed_Scope : Boolean := False;
747 begin
748 -- We do not want to put the declaration we have created in the tree
749 -- since it is often hard, and sometimes impossible to find a proper
750 -- place for it (the impossible case arises for a packed array type
751 -- with bounds depending on the discriminant, a declaration cannot
752 -- be put inside the record, and the reference to the discriminant
753 -- cannot be outside the record).
755 -- The solution is to analyze the declaration while temporarily
756 -- attached to the tree at an appropriate point, and then we install
757 -- the resulting type as an Itype in the packed array type field of
758 -- the original type, so that no explicit declaration is required.
760 -- Note: the packed type is created in the scope of its parent
761 -- type. There are at least some cases where the current scope
762 -- is deeper, and so when this is the case, we temporarily reset
763 -- the scope for the definition. This is clearly safe, since the
764 -- first use of the packed array type will be the implicit
765 -- reference from the corresponding unpacked type when it is
766 -- elaborated.
768 if Is_Itype (Typ) then
769 Set_Parent (Decl, Associated_Node_For_Itype (Typ));
770 else
771 Set_Parent (Decl, Declaration_Node (Typ));
772 end if;
774 if Scope (Typ) /= Current_Scope then
775 New_Scope (Scope (Typ));
776 Pushed_Scope := True;
777 end if;
779 Set_Is_Itype (PAT, True);
780 Set_Packed_Array_Type (Typ, PAT);
781 Analyze (Decl, Suppress => All_Checks);
783 if Pushed_Scope then
784 Pop_Scope;
785 end if;
787 -- Set Esize and RM_Size to the actual size of the packed object
788 -- Do not reset RM_Size if already set, as happens in the case
789 -- of a modular type.
791 Set_Esize (PAT, PASize);
793 if Unknown_RM_Size (PAT) then
794 Set_RM_Size (PAT, PASize);
795 end if;
797 -- Set remaining fields of packed array type
799 Init_Alignment (PAT);
800 Set_Parent (PAT, Empty);
801 Set_Associated_Node_For_Itype (PAT, Typ);
802 Set_Is_Packed_Array_Type (PAT, True);
803 Set_Original_Array_Type (PAT, Typ);
805 -- We definitely do not want to delay freezing for packed array
806 -- types. This is of particular importance for the itypes that
807 -- are generated for record components depending on discriminants
808 -- where there is no place to put the freeze node.
810 Set_Has_Delayed_Freeze (PAT, False);
811 Set_Has_Delayed_Freeze (Etype (PAT), False);
813 -- If we did allocate a freeze node, then clear out the reference
814 -- since it is obsolete (should we delete the freeze node???)
816 Set_Freeze_Node (PAT, Empty);
817 Set_Freeze_Node (Etype (PAT), Empty);
818 end Install_PAT;
820 -----------------
821 -- Set_PB_Type --
822 -----------------
824 procedure Set_PB_Type is
825 begin
826 -- If the user has specified an explicit alignment for the
827 -- type or component, take it into account.
829 if Csize <= 2 or else Csize = 4 or else Csize mod 2 /= 0
830 or else Alignment (Typ) = 1
831 or else Component_Alignment (Typ) = Calign_Storage_Unit
832 then
833 PB_Type := RTE (RE_Packed_Bytes1);
835 elsif Csize mod 4 /= 0
836 or else Alignment (Typ) = 2
837 then
838 PB_Type := RTE (RE_Packed_Bytes2);
840 else
841 PB_Type := RTE (RE_Packed_Bytes4);
842 end if;
843 end Set_PB_Type;
845 -- Start of processing for Create_Packed_Array_Type
847 begin
848 -- If we already have a packed array type, nothing to do
850 if Present (Packed_Array_Type (Typ)) then
851 return;
852 end if;
854 -- If our immediate ancestor subtype is constrained, and it already
855 -- has a packed array type, then just share the same type, since the
856 -- bounds must be the same. If the ancestor is not an array type but
857 -- a private type, as can happen with multiple instantiations, create
858 -- a new packed type, to avoid privacy issues.
860 if Ekind (Typ) = E_Array_Subtype then
861 Ancest := Ancestor_Subtype (Typ);
863 if Present (Ancest)
864 and then Is_Array_Type (Ancest)
865 and then Is_Constrained (Ancest)
866 and then Present (Packed_Array_Type (Ancest))
867 then
868 Set_Packed_Array_Type (Typ, Packed_Array_Type (Ancest));
869 return;
870 end if;
871 end if;
873 -- We preset the result type size from the size of the original array
874 -- type, since this size clearly belongs to the packed array type. The
875 -- size of the conceptual unpacked type is always set to unknown.
877 PASize := Esize (Typ);
879 -- Case of an array where at least one index is of an enumeration
880 -- type with a non-standard representation, but the component size
881 -- is not appropriate for bit packing. This is the case where we
882 -- have Is_Packed set (we would never be in this unit otherwise),
883 -- but Is_Bit_Packed_Array is false.
885 -- Note that if the component size is appropriate for bit packing,
886 -- then the circuit for the computation of the subscript properly
887 -- deals with the non-standard enumeration type case by taking the
888 -- Pos anyway.
890 if not Is_Bit_Packed_Array (Typ) then
892 -- Here we build a declaration:
894 -- type tttP is array (index1, index2, ...) of component_type
896 -- where index1, index2, are the index types. These are the same
897 -- as the index types of the original array, except for the non-
898 -- standard representation enumeration type case, where we have
899 -- two subcases.
901 -- For the unconstrained array case, we use
903 -- Natural range <>
905 -- For the constrained case, we use
907 -- Natural range Enum_Type'Pos (Enum_Type'First) ..
908 -- Enum_Type'Pos (Enum_Type'Last);
910 PAT :=
911 Make_Defining_Identifier (Loc,
912 Chars => New_External_Name (Chars (Typ), 'P'));
914 Set_Packed_Array_Type (Typ, PAT);
916 declare
917 Indexes : constant List_Id := New_List;
918 Indx : Node_Id;
919 Indx_Typ : Entity_Id;
920 Enum_Case : Boolean;
921 Typedef : Node_Id;
923 begin
924 Indx := First_Index (Typ);
926 while Present (Indx) loop
927 Indx_Typ := Etype (Indx);
929 Enum_Case := Is_Enumeration_Type (Indx_Typ)
930 and then Has_Non_Standard_Rep (Indx_Typ);
932 -- Unconstrained case
934 if not Is_Constrained (Typ) then
935 if Enum_Case then
936 Indx_Typ := Standard_Natural;
937 end if;
939 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
941 -- Constrained case
943 else
944 if not Enum_Case then
945 Append_To (Indexes, New_Occurrence_Of (Indx_Typ, Loc));
947 else
948 Append_To (Indexes,
949 Make_Subtype_Indication (Loc,
950 Subtype_Mark =>
951 New_Occurrence_Of (Standard_Natural, Loc),
952 Constraint =>
953 Make_Range_Constraint (Loc,
954 Range_Expression =>
955 Make_Range (Loc,
956 Low_Bound =>
957 Make_Attribute_Reference (Loc,
958 Prefix =>
959 New_Occurrence_Of (Indx_Typ, Loc),
960 Attribute_Name => Name_Pos,
961 Expressions => New_List (
962 Make_Attribute_Reference (Loc,
963 Prefix =>
964 New_Occurrence_Of (Indx_Typ, Loc),
965 Attribute_Name => Name_First))),
967 High_Bound =>
968 Make_Attribute_Reference (Loc,
969 Prefix =>
970 New_Occurrence_Of (Indx_Typ, Loc),
971 Attribute_Name => Name_Pos,
972 Expressions => New_List (
973 Make_Attribute_Reference (Loc,
974 Prefix =>
975 New_Occurrence_Of (Indx_Typ, Loc),
976 Attribute_Name => Name_Last)))))));
978 end if;
979 end if;
981 Next_Index (Indx);
982 end loop;
984 if not Is_Constrained (Typ) then
985 Typedef :=
986 Make_Unconstrained_Array_Definition (Loc,
987 Subtype_Marks => Indexes,
988 Component_Definition =>
989 Make_Component_Definition (Loc,
990 Aliased_Present => False,
991 Subtype_Indication =>
992 New_Occurrence_Of (Ctyp, Loc)));
994 else
995 Typedef :=
996 Make_Constrained_Array_Definition (Loc,
997 Discrete_Subtype_Definitions => Indexes,
998 Component_Definition =>
999 Make_Component_Definition (Loc,
1000 Aliased_Present => False,
1001 Subtype_Indication =>
1002 New_Occurrence_Of (Ctyp, Loc)));
1003 end if;
1005 Decl :=
1006 Make_Full_Type_Declaration (Loc,
1007 Defining_Identifier => PAT,
1008 Type_Definition => Typedef);
1009 end;
1011 -- Set type as packed array type and install it
1013 Set_Is_Packed_Array_Type (PAT);
1014 Install_PAT;
1015 return;
1017 -- Case of bit-packing required for unconstrained array. We create
1018 -- a subtype that is equivalent to use Packed_Bytes{1,2,4} as needed.
1020 elsif not Is_Constrained (Typ) then
1021 PAT :=
1022 Make_Defining_Identifier (Loc,
1023 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1025 Set_Packed_Array_Type (Typ, PAT);
1026 Set_PB_Type;
1028 Decl :=
1029 Make_Subtype_Declaration (Loc,
1030 Defining_Identifier => PAT,
1031 Subtype_Indication => New_Occurrence_Of (PB_Type, Loc));
1032 Install_PAT;
1033 return;
1035 -- Remaining code is for the case of bit-packing for constrained array
1037 -- The name of the packed array subtype is
1039 -- ttt___Xsss
1041 -- where sss is the component size in bits and ttt is the name of
1042 -- the parent packed type.
1044 else
1045 PAT :=
1046 Make_Defining_Identifier (Loc,
1047 Chars => Make_Packed_Array_Type_Name (Typ, Csize));
1049 Set_Packed_Array_Type (Typ, PAT);
1051 -- Build an expression for the length of the array in bits.
1052 -- This is the product of the length of each of the dimensions
1054 declare
1055 J : Nat := 1;
1057 begin
1058 Len_Expr := Empty; -- suppress junk warning
1060 loop
1061 Len_Dim :=
1062 Make_Attribute_Reference (Loc,
1063 Attribute_Name => Name_Length,
1064 Prefix => New_Occurrence_Of (Typ, Loc),
1065 Expressions => New_List (
1066 Make_Integer_Literal (Loc, J)));
1068 if J = 1 then
1069 Len_Expr := Len_Dim;
1071 else
1072 Len_Expr :=
1073 Make_Op_Multiply (Loc,
1074 Left_Opnd => Len_Expr,
1075 Right_Opnd => Len_Dim);
1076 end if;
1078 J := J + 1;
1079 exit when J > Number_Dimensions (Typ);
1080 end loop;
1081 end;
1083 -- Temporarily attach the length expression to the tree and analyze
1084 -- and resolve it, so that we can test its value. We assume that the
1085 -- total length fits in type Integer. This expression may involve
1086 -- discriminants, so we treat it as a default/per-object expression.
1088 Set_Parent (Len_Expr, Typ);
1089 Analyze_Per_Use_Expression (Len_Expr, Standard_Long_Long_Integer);
1091 -- Use a modular type if possible. We can do this if we have
1092 -- static bounds, and the length is small enough, and the length
1093 -- is not zero. We exclude the zero length case because the size
1094 -- of things is always at least one, and the zero length object
1095 -- would have an anomalous size.
1097 if Compile_Time_Known_Value (Len_Expr) then
1098 Len_Bits := Expr_Value (Len_Expr) * Csize;
1100 -- Check for size known to be too large
1102 if Len_Bits >
1103 Uint_2 ** (Standard_Integer_Size - 1) * System_Storage_Unit
1104 then
1105 if System_Storage_Unit = 8 then
1106 Error_Msg_N
1107 ("packed array size cannot exceed " &
1108 "Integer''Last bytes", Typ);
1109 else
1110 Error_Msg_N
1111 ("packed array size cannot exceed " &
1112 "Integer''Last storage units", Typ);
1113 end if;
1115 -- Reset length to arbitrary not too high value to continue
1117 Len_Expr := Make_Integer_Literal (Loc, 65535);
1118 Analyze_And_Resolve (Len_Expr, Standard_Long_Long_Integer);
1119 end if;
1121 -- We normally consider small enough to mean no larger than the
1122 -- value of System_Max_Binary_Modulus_Power, checking that in the
1123 -- case of values longer than word size, we have long shifts.
1125 if Len_Bits > 0
1126 and then
1127 (Len_Bits <= System_Word_Size
1128 or else (Len_Bits <= System_Max_Binary_Modulus_Power
1129 and then Support_Long_Shifts_On_Target))
1131 -- Also test for alignment given. If an alignment is given which
1132 -- is smaller than the natural modular alignment, force the array
1133 -- of bytes representation to accommodate the alignment.
1135 and then
1136 (No (Alignment_Clause (Typ))
1137 or else
1138 Alignment (Typ) >= ((Len_Bits + System_Storage_Unit)
1139 / System_Storage_Unit))
1140 then
1141 -- We can use the modular type, it has the form:
1143 -- subtype tttPn is btyp
1144 -- range 0 .. 2 ** ((Typ'Length (1)
1145 -- * ... * Typ'Length (n)) * Csize) - 1;
1147 -- The bounds are statically known, and btyp is one
1148 -- of the unsigned types, depending on the length. If the
1149 -- type is its first subtype, i.e. it is a user-defined
1150 -- type, no object of the type will be larger, and it is
1151 -- worthwhile to use a small unsigned type.
1153 if Len_Bits <= Standard_Short_Integer_Size
1154 and then First_Subtype (Typ) = Typ
1155 then
1156 Btyp := RTE (RE_Short_Unsigned);
1158 elsif Len_Bits <= Standard_Integer_Size then
1159 Btyp := RTE (RE_Unsigned);
1161 elsif Len_Bits <= Standard_Long_Integer_Size then
1162 Btyp := RTE (RE_Long_Unsigned);
1164 else
1165 Btyp := RTE (RE_Long_Long_Unsigned);
1166 end if;
1168 Lit := Make_Integer_Literal (Loc, 2 ** Len_Bits - 1);
1169 Set_Print_In_Hex (Lit);
1171 Decl :=
1172 Make_Subtype_Declaration (Loc,
1173 Defining_Identifier => PAT,
1174 Subtype_Indication =>
1175 Make_Subtype_Indication (Loc,
1176 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
1178 Constraint =>
1179 Make_Range_Constraint (Loc,
1180 Range_Expression =>
1181 Make_Range (Loc,
1182 Low_Bound =>
1183 Make_Integer_Literal (Loc, 0),
1184 High_Bound => Lit))));
1186 if PASize = Uint_0 then
1187 PASize := Len_Bits;
1188 end if;
1190 Install_PAT;
1191 return;
1192 end if;
1193 end if;
1195 -- Could not use a modular type, for all other cases, we build
1196 -- a packed array subtype:
1198 -- subtype tttPn is
1199 -- System.Packed_Bytes{1,2,4} (0 .. (Bits + 7) / 8 - 1);
1201 -- Bits is the length of the array in bits
1203 Set_PB_Type;
1205 Bits_U1 :=
1206 Make_Op_Add (Loc,
1207 Left_Opnd =>
1208 Make_Op_Multiply (Loc,
1209 Left_Opnd =>
1210 Make_Integer_Literal (Loc, Csize),
1211 Right_Opnd => Len_Expr),
1213 Right_Opnd =>
1214 Make_Integer_Literal (Loc, 7));
1216 Set_Paren_Count (Bits_U1, 1);
1218 PAT_High :=
1219 Make_Op_Subtract (Loc,
1220 Left_Opnd =>
1221 Make_Op_Divide (Loc,
1222 Left_Opnd => Bits_U1,
1223 Right_Opnd => Make_Integer_Literal (Loc, 8)),
1224 Right_Opnd => Make_Integer_Literal (Loc, 1));
1226 Decl :=
1227 Make_Subtype_Declaration (Loc,
1228 Defining_Identifier => PAT,
1229 Subtype_Indication =>
1230 Make_Subtype_Indication (Loc,
1231 Subtype_Mark => New_Occurrence_Of (PB_Type, Loc),
1232 Constraint =>
1233 Make_Index_Or_Discriminant_Constraint (Loc,
1234 Constraints => New_List (
1235 Make_Range (Loc,
1236 Low_Bound =>
1237 Make_Integer_Literal (Loc, 0),
1238 High_Bound =>
1239 Convert_To (Standard_Integer, PAT_High))))));
1241 Install_PAT;
1243 -- Currently the code in this unit requires that packed arrays
1244 -- represented by non-modular arrays of bytes be on a byte
1245 -- boundary for bit sizes handled by System.Pack_nn units.
1246 -- That's because these units assume the array being accessed
1247 -- starts on a byte boundary.
1249 if Get_Id (UI_To_Int (Csize)) /= RE_Null then
1250 Set_Must_Be_On_Byte_Boundary (Typ);
1251 end if;
1252 end if;
1253 end Create_Packed_Array_Type;
1255 -----------------------------------
1256 -- Expand_Bit_Packed_Element_Set --
1257 -----------------------------------
1259 procedure Expand_Bit_Packed_Element_Set (N : Node_Id) is
1260 Loc : constant Source_Ptr := Sloc (N);
1261 Lhs : constant Node_Id := Name (N);
1263 Ass_OK : constant Boolean := Assignment_OK (Lhs);
1264 -- Used to preserve assignment OK status when assignment is rewritten
1266 Rhs : Node_Id := Expression (N);
1267 -- Initially Rhs is the right hand side value, it will be replaced
1268 -- later by an appropriate unchecked conversion for the assignment.
1270 Obj : Node_Id;
1271 Atyp : Entity_Id;
1272 PAT : Entity_Id;
1273 Ctyp : Entity_Id;
1274 Csiz : Int;
1275 Cmask : Uint;
1277 Shift : Node_Id;
1278 -- The expression for the shift value that is required
1280 Shift_Used : Boolean := False;
1281 -- Set True if Shift has been used in the generated code at least
1282 -- once, so that it must be duplicated if used again
1284 New_Lhs : Node_Id;
1285 New_Rhs : Node_Id;
1287 Rhs_Val_Known : Boolean;
1288 Rhs_Val : Uint;
1289 -- If the value of the right hand side as an integer constant is
1290 -- known at compile time, Rhs_Val_Known is set True, and Rhs_Val
1291 -- contains the value. Otherwise Rhs_Val_Known is set False, and
1292 -- the Rhs_Val is undefined.
1294 function Get_Shift return Node_Id;
1295 -- Function used to get the value of Shift, making sure that it
1296 -- gets duplicated if the function is called more than once.
1298 ---------------
1299 -- Get_Shift --
1300 ---------------
1302 function Get_Shift return Node_Id is
1303 begin
1304 -- If we used the shift value already, then duplicate it. We
1305 -- set a temporary parent in case actions have to be inserted.
1307 if Shift_Used then
1308 Set_Parent (Shift, N);
1309 return Duplicate_Subexpr_No_Checks (Shift);
1311 -- If first time, use Shift unchanged, and set flag for first use
1313 else
1314 Shift_Used := True;
1315 return Shift;
1316 end if;
1317 end Get_Shift;
1319 -- Start of processing for Expand_Bit_Packed_Element_Set
1321 begin
1322 pragma Assert (Is_Bit_Packed_Array (Etype (Prefix (Lhs))));
1324 Obj := Relocate_Node (Prefix (Lhs));
1325 Convert_To_Actual_Subtype (Obj);
1326 Atyp := Etype (Obj);
1327 PAT := Packed_Array_Type (Atyp);
1328 Ctyp := Component_Type (Atyp);
1329 Csiz := UI_To_Int (Component_Size (Atyp));
1331 -- We convert the right hand side to the proper subtype to ensure
1332 -- that an appropriate range check is made (since the normal range
1333 -- check from assignment will be lost in the transformations). This
1334 -- conversion is analyzed immediately so that subsequent processing
1335 -- can work with an analyzed Rhs (and e.g. look at its Etype)
1337 -- If the right-hand side is a string literal, create a temporary for
1338 -- it, constant-folding is not ready to wrap the bit representation
1339 -- of a string literal.
1341 if Nkind (Rhs) = N_String_Literal then
1342 declare
1343 Decl : Node_Id;
1344 begin
1345 Decl :=
1346 Make_Object_Declaration (Loc,
1347 Defining_Identifier =>
1348 Make_Defining_Identifier (Loc, New_Internal_Name ('T')),
1349 Object_Definition => New_Occurrence_Of (Ctyp, Loc),
1350 Expression => New_Copy_Tree (Rhs));
1352 Insert_Actions (N, New_List (Decl));
1353 Rhs := New_Occurrence_Of (Defining_Identifier (Decl), Loc);
1354 end;
1355 end if;
1357 Rhs := Convert_To (Ctyp, Rhs);
1358 Set_Parent (Rhs, N);
1359 Analyze_And_Resolve (Rhs, Ctyp);
1361 -- Case of component size 1,2,4 or any component size for the modular
1362 -- case. These are the cases for which we can inline the code.
1364 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1365 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1366 then
1367 Setup_Inline_Packed_Array_Reference (Lhs, Atyp, Obj, Cmask, Shift);
1369 -- The statement to be generated is:
1371 -- Obj := atyp!((Obj and Mask1) or (shift_left (rhs, shift)))
1373 -- where mask1 is obtained by shifting Cmask left Shift bits
1374 -- and then complementing the result.
1376 -- the "and Mask1" is omitted if rhs is constant and all 1 bits
1378 -- the "or ..." is omitted if rhs is constant and all 0 bits
1380 -- rhs is converted to the appropriate type
1382 -- The result is converted back to the array type, since
1383 -- otherwise we lose knowledge of the packed nature.
1385 -- Determine if right side is all 0 bits or all 1 bits
1387 if Compile_Time_Known_Value (Rhs) then
1388 Rhs_Val := Expr_Rep_Value (Rhs);
1389 Rhs_Val_Known := True;
1391 -- The following test catches the case of an unchecked conversion
1392 -- of an integer literal. This results from optimizing aggregates
1393 -- of packed types.
1395 elsif Nkind (Rhs) = N_Unchecked_Type_Conversion
1396 and then Compile_Time_Known_Value (Expression (Rhs))
1397 then
1398 Rhs_Val := Expr_Rep_Value (Expression (Rhs));
1399 Rhs_Val_Known := True;
1401 else
1402 Rhs_Val := No_Uint;
1403 Rhs_Val_Known := False;
1404 end if;
1406 -- Some special checks for the case where the right hand value
1407 -- is known at compile time. Basically we have to take care of
1408 -- the implicit conversion to the subtype of the component object.
1410 if Rhs_Val_Known then
1412 -- If we have a biased component type then we must manually do
1413 -- the biasing, since we are taking responsibility in this case
1414 -- for constructing the exact bit pattern to be used.
1416 if Has_Biased_Representation (Ctyp) then
1417 Rhs_Val := Rhs_Val - Expr_Rep_Value (Type_Low_Bound (Ctyp));
1418 end if;
1420 -- For a negative value, we manually convert the twos complement
1421 -- value to a corresponding unsigned value, so that the proper
1422 -- field width is maintained. If we did not do this, we would
1423 -- get too many leading sign bits later on.
1425 if Rhs_Val < 0 then
1426 Rhs_Val := 2 ** UI_From_Int (Csiz) + Rhs_Val;
1427 end if;
1428 end if;
1430 New_Lhs := Duplicate_Subexpr (Obj, True);
1431 New_Rhs := Duplicate_Subexpr_No_Checks (Obj);
1433 -- First we deal with the "and"
1435 if not Rhs_Val_Known or else Rhs_Val /= Cmask then
1436 declare
1437 Mask1 : Node_Id;
1438 Lit : Node_Id;
1440 begin
1441 if Compile_Time_Known_Value (Shift) then
1442 Mask1 :=
1443 Make_Integer_Literal (Loc,
1444 Modulus (Etype (Obj)) - 1 -
1445 (Cmask * (2 ** Expr_Value (Get_Shift))));
1446 Set_Print_In_Hex (Mask1);
1448 else
1449 Lit := Make_Integer_Literal (Loc, Cmask);
1450 Set_Print_In_Hex (Lit);
1451 Mask1 :=
1452 Make_Op_Not (Loc,
1453 Right_Opnd => Make_Shift_Left (Lit, Get_Shift));
1454 end if;
1456 New_Rhs :=
1457 Make_Op_And (Loc,
1458 Left_Opnd => New_Rhs,
1459 Right_Opnd => Mask1);
1460 end;
1461 end if;
1463 -- Then deal with the "or"
1465 if not Rhs_Val_Known or else Rhs_Val /= 0 then
1466 declare
1467 Or_Rhs : Node_Id;
1469 procedure Fixup_Rhs;
1470 -- Adjust Rhs by bias if biased representation for components
1471 -- or remove extraneous high order sign bits if signed.
1473 procedure Fixup_Rhs is
1474 Etyp : constant Entity_Id := Etype (Rhs);
1476 begin
1477 -- For biased case, do the required biasing by simply
1478 -- converting to the biased subtype (the conversion
1479 -- will generate the required bias).
1481 if Has_Biased_Representation (Ctyp) then
1482 Rhs := Convert_To (Ctyp, Rhs);
1484 -- For a signed integer type that is not biased, generate
1485 -- a conversion to unsigned to strip high order sign bits.
1487 elsif Is_Signed_Integer_Type (Ctyp) then
1488 Rhs := Unchecked_Convert_To (RTE (Bits_Id (Csiz)), Rhs);
1489 end if;
1491 -- Set Etype, since it can be referenced before the
1492 -- node is completely analyzed.
1494 Set_Etype (Rhs, Etyp);
1496 -- We now need to do an unchecked conversion of the
1497 -- result to the target type, but it is important that
1498 -- this conversion be a right justified conversion and
1499 -- not a left justified conversion.
1501 Rhs := RJ_Unchecked_Convert_To (Etype (Obj), Rhs);
1503 end Fixup_Rhs;
1505 begin
1506 if Rhs_Val_Known
1507 and then Compile_Time_Known_Value (Get_Shift)
1508 then
1509 Or_Rhs :=
1510 Make_Integer_Literal (Loc,
1511 Rhs_Val * (2 ** Expr_Value (Get_Shift)));
1512 Set_Print_In_Hex (Or_Rhs);
1514 else
1515 -- We have to convert the right hand side to Etype (Obj).
1516 -- A special case case arises if what we have now is a Val
1517 -- attribute reference whose expression type is Etype (Obj).
1518 -- This happens for assignments of fields from the same
1519 -- array. In this case we get the required right hand side
1520 -- by simply removing the inner attribute reference.
1522 if Nkind (Rhs) = N_Attribute_Reference
1523 and then Attribute_Name (Rhs) = Name_Val
1524 and then Etype (First (Expressions (Rhs))) = Etype (Obj)
1525 then
1526 Rhs := Relocate_Node (First (Expressions (Rhs)));
1527 Fixup_Rhs;
1529 -- If the value of the right hand side is a known integer
1530 -- value, then just replace it by an untyped constant,
1531 -- which will be properly retyped when we analyze and
1532 -- resolve the expression.
1534 elsif Rhs_Val_Known then
1536 -- Note that Rhs_Val has already been normalized to
1537 -- be an unsigned value with the proper number of bits.
1539 Rhs :=
1540 Make_Integer_Literal (Loc, Rhs_Val);
1542 -- Otherwise we need an unchecked conversion
1544 else
1545 Fixup_Rhs;
1546 end if;
1548 Or_Rhs := Make_Shift_Left (Rhs, Get_Shift);
1549 end if;
1551 if Nkind (New_Rhs) = N_Op_And then
1552 Set_Paren_Count (New_Rhs, 1);
1553 end if;
1555 New_Rhs :=
1556 Make_Op_Or (Loc,
1557 Left_Opnd => New_Rhs,
1558 Right_Opnd => Or_Rhs);
1559 end;
1560 end if;
1562 -- Now do the rewrite
1564 Rewrite (N,
1565 Make_Assignment_Statement (Loc,
1566 Name => New_Lhs,
1567 Expression =>
1568 Unchecked_Convert_To (Etype (New_Lhs), New_Rhs)));
1569 Set_Assignment_OK (Name (N), Ass_OK);
1571 -- All other component sizes for non-modular case
1573 else
1574 -- We generate
1576 -- Set_nn (Arr'address, Subscr, Bits_nn!(Rhs))
1578 -- where Subscr is the computed linear subscript
1580 declare
1581 Bits_nn : constant Entity_Id := RTE (Bits_Id (Csiz));
1582 Set_nn : Entity_Id;
1583 Subscr : Node_Id;
1584 Atyp : Entity_Id;
1586 begin
1587 if No (Bits_nn) then
1589 -- Error, most likely High_Integrity_Mode restriction
1591 return;
1592 end if;
1594 -- Acquire proper Set entity. We use the aligned or unaligned
1595 -- case as appropriate.
1597 if Known_Aligned_Enough (Obj, Csiz) then
1598 Set_nn := RTE (Set_Id (Csiz));
1599 else
1600 Set_nn := RTE (SetU_Id (Csiz));
1601 end if;
1603 -- Now generate the set reference
1605 Obj := Relocate_Node (Prefix (Lhs));
1606 Convert_To_Actual_Subtype (Obj);
1607 Atyp := Etype (Obj);
1608 Compute_Linear_Subscript (Atyp, Lhs, Subscr);
1610 -- Below we must make the assumption that Obj is
1611 -- at least byte aligned, since otherwise its address
1612 -- cannot be taken. The assumption holds since the
1613 -- only arrays that can be misaligned are small packed
1614 -- arrays which are implemented as a modular type, and
1615 -- that is not the case here.
1617 Rewrite (N,
1618 Make_Procedure_Call_Statement (Loc,
1619 Name => New_Occurrence_Of (Set_nn, Loc),
1620 Parameter_Associations => New_List (
1621 Make_Attribute_Reference (Loc,
1622 Attribute_Name => Name_Address,
1623 Prefix => Obj),
1624 Subscr,
1625 Unchecked_Convert_To (Bits_nn,
1626 Convert_To (Ctyp, Rhs)))));
1628 end;
1629 end if;
1631 Analyze (N, Suppress => All_Checks);
1632 end Expand_Bit_Packed_Element_Set;
1634 -------------------------------------
1635 -- Expand_Packed_Address_Reference --
1636 -------------------------------------
1638 procedure Expand_Packed_Address_Reference (N : Node_Id) is
1639 Loc : constant Source_Ptr := Sloc (N);
1640 Ploc : Source_Ptr;
1641 Pref : Node_Id;
1642 Expr : Node_Id;
1643 Term : Node_Id;
1644 Atyp : Entity_Id;
1645 Subscr : Node_Id;
1647 begin
1648 Pref := Prefix (N);
1649 Expr := Empty;
1651 -- We build up an expression serially that has the form
1653 -- outer_object'Address
1654 -- + (linear-subscript * component_size for each array reference
1655 -- + field'Bit_Position for each record field
1656 -- + ...
1657 -- + ...) / Storage_Unit;
1659 -- Some additional conversions are required to deal with the addition
1660 -- operation, which is not normally visible to generated code.
1662 loop
1663 Ploc := Sloc (Pref);
1665 if Nkind (Pref) = N_Indexed_Component then
1666 Convert_To_Actual_Subtype (Prefix (Pref));
1667 Atyp := Etype (Prefix (Pref));
1668 Compute_Linear_Subscript (Atyp, Pref, Subscr);
1670 Term :=
1671 Make_Op_Multiply (Ploc,
1672 Left_Opnd => Subscr,
1673 Right_Opnd =>
1674 Make_Attribute_Reference (Ploc,
1675 Prefix => New_Occurrence_Of (Atyp, Ploc),
1676 Attribute_Name => Name_Component_Size));
1678 elsif Nkind (Pref) = N_Selected_Component then
1679 Term :=
1680 Make_Attribute_Reference (Ploc,
1681 Prefix => Selector_Name (Pref),
1682 Attribute_Name => Name_Bit_Position);
1684 else
1685 exit;
1686 end if;
1688 Term := Convert_To (RTE (RE_Integer_Address), Term);
1690 if No (Expr) then
1691 Expr := Term;
1693 else
1694 Expr :=
1695 Make_Op_Add (Ploc,
1696 Left_Opnd => Expr,
1697 Right_Opnd => Term);
1698 end if;
1700 Pref := Prefix (Pref);
1701 end loop;
1703 Rewrite (N,
1704 Unchecked_Convert_To (RTE (RE_Address),
1705 Make_Op_Add (Loc,
1706 Left_Opnd =>
1707 Unchecked_Convert_To (RTE (RE_Integer_Address),
1708 Make_Attribute_Reference (Loc,
1709 Prefix => Pref,
1710 Attribute_Name => Name_Address)),
1712 Right_Opnd =>
1713 Make_Op_Divide (Loc,
1714 Left_Opnd => Expr,
1715 Right_Opnd =>
1716 Make_Integer_Literal (Loc, System_Storage_Unit)))));
1718 Analyze_And_Resolve (N, RTE (RE_Address));
1719 end Expand_Packed_Address_Reference;
1721 ------------------------------------
1722 -- Expand_Packed_Boolean_Operator --
1723 ------------------------------------
1725 -- This routine expands "a op b" for the packed cases
1727 procedure Expand_Packed_Boolean_Operator (N : Node_Id) is
1728 Loc : constant Source_Ptr := Sloc (N);
1729 Typ : constant Entity_Id := Etype (N);
1730 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1731 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1733 Ltyp : Entity_Id;
1734 Rtyp : Entity_Id;
1735 PAT : Entity_Id;
1737 begin
1738 Convert_To_Actual_Subtype (L);
1739 Convert_To_Actual_Subtype (R);
1741 Ensure_Defined (Etype (L), N);
1742 Ensure_Defined (Etype (R), N);
1744 Apply_Length_Check (R, Etype (L));
1746 Ltyp := Etype (L);
1747 Rtyp := Etype (R);
1749 -- First an odd and silly test. We explicitly check for the XOR
1750 -- case where the component type is True .. True, since this will
1751 -- raise constraint error. A special check is required since CE
1752 -- will not be required other wise (cf Expand_Packed_Not).
1754 -- No such check is required for AND and OR, since for both these
1755 -- cases False op False = False, and True op True = True.
1757 if Nkind (N) = N_Op_Xor then
1758 declare
1759 CT : constant Entity_Id := Component_Type (Rtyp);
1760 BT : constant Entity_Id := Base_Type (CT);
1762 begin
1763 Insert_Action (N,
1764 Make_Raise_Constraint_Error (Loc,
1765 Condition =>
1766 Make_Op_And (Loc,
1767 Left_Opnd =>
1768 Make_Op_Eq (Loc,
1769 Left_Opnd =>
1770 Make_Attribute_Reference (Loc,
1771 Prefix => New_Occurrence_Of (CT, Loc),
1772 Attribute_Name => Name_First),
1774 Right_Opnd =>
1775 Convert_To (BT,
1776 New_Occurrence_Of (Standard_True, Loc))),
1778 Right_Opnd =>
1779 Make_Op_Eq (Loc,
1780 Left_Opnd =>
1781 Make_Attribute_Reference (Loc,
1782 Prefix => New_Occurrence_Of (CT, Loc),
1783 Attribute_Name => Name_Last),
1785 Right_Opnd =>
1786 Convert_To (BT,
1787 New_Occurrence_Of (Standard_True, Loc)))),
1788 Reason => CE_Range_Check_Failed));
1789 end;
1790 end if;
1792 -- Now that that silliness is taken care of, get packed array type
1794 Convert_To_PAT_Type (L);
1795 Convert_To_PAT_Type (R);
1797 PAT := Etype (L);
1799 -- For the modular case, we expand a op b into
1801 -- rtyp!(pat!(a) op pat!(b))
1803 -- where rtyp is the Etype of the left operand. Note that we do not
1804 -- convert to the base type, since this would be unconstrained, and
1805 -- hence not have a corresponding packed array type set.
1807 -- Note that both operands must be modular for this code to be used
1809 if Is_Modular_Integer_Type (PAT)
1810 and then
1811 Is_Modular_Integer_Type (Etype (R))
1812 then
1813 declare
1814 P : Node_Id;
1816 begin
1817 if Nkind (N) = N_Op_And then
1818 P := Make_Op_And (Loc, L, R);
1820 elsif Nkind (N) = N_Op_Or then
1821 P := Make_Op_Or (Loc, L, R);
1823 else -- Nkind (N) = N_Op_Xor
1824 P := Make_Op_Xor (Loc, L, R);
1825 end if;
1827 Rewrite (N, Unchecked_Convert_To (Rtyp, P));
1828 end;
1830 -- For the array case, we insert the actions
1832 -- Result : Ltype;
1834 -- System.Bitops.Bit_And/Or/Xor
1835 -- (Left'Address,
1836 -- Ltype'Length * Ltype'Component_Size;
1837 -- Right'Address,
1838 -- Rtype'Length * Rtype'Component_Size
1839 -- Result'Address);
1841 -- where Left and Right are the Packed_Bytes{1,2,4} operands and
1842 -- the second argument and fourth arguments are the lengths of the
1843 -- operands in bits. Then we replace the expression by a reference
1844 -- to Result.
1846 -- Note that if we are mixing a modular and array operand, everything
1847 -- works fine, since we ensure that the modular representation has the
1848 -- same physical layout as the array representation (that's what the
1849 -- left justified modular stuff in the big-endian case is about).
1851 else
1852 declare
1853 Result_Ent : constant Entity_Id :=
1854 Make_Defining_Identifier (Loc,
1855 Chars => New_Internal_Name ('T'));
1857 E_Id : RE_Id;
1859 begin
1860 if Nkind (N) = N_Op_And then
1861 E_Id := RE_Bit_And;
1863 elsif Nkind (N) = N_Op_Or then
1864 E_Id := RE_Bit_Or;
1866 else -- Nkind (N) = N_Op_Xor
1867 E_Id := RE_Bit_Xor;
1868 end if;
1870 Insert_Actions (N, New_List (
1872 Make_Object_Declaration (Loc,
1873 Defining_Identifier => Result_Ent,
1874 Object_Definition => New_Occurrence_Of (Ltyp, Loc)),
1876 Make_Procedure_Call_Statement (Loc,
1877 Name => New_Occurrence_Of (RTE (E_Id), Loc),
1878 Parameter_Associations => New_List (
1880 Make_Byte_Aligned_Attribute_Reference (Loc,
1881 Attribute_Name => Name_Address,
1882 Prefix => L),
1884 Make_Op_Multiply (Loc,
1885 Left_Opnd =>
1886 Make_Attribute_Reference (Loc,
1887 Prefix =>
1888 New_Occurrence_Of
1889 (Etype (First_Index (Ltyp)), Loc),
1890 Attribute_Name => Name_Range_Length),
1891 Right_Opnd =>
1892 Make_Integer_Literal (Loc, Component_Size (Ltyp))),
1894 Make_Byte_Aligned_Attribute_Reference (Loc,
1895 Attribute_Name => Name_Address,
1896 Prefix => R),
1898 Make_Op_Multiply (Loc,
1899 Left_Opnd =>
1900 Make_Attribute_Reference (Loc,
1901 Prefix =>
1902 New_Occurrence_Of
1903 (Etype (First_Index (Rtyp)), Loc),
1904 Attribute_Name => Name_Range_Length),
1905 Right_Opnd =>
1906 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
1908 Make_Byte_Aligned_Attribute_Reference (Loc,
1909 Attribute_Name => Name_Address,
1910 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
1912 Rewrite (N,
1913 New_Occurrence_Of (Result_Ent, Loc));
1914 end;
1915 end if;
1917 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
1918 end Expand_Packed_Boolean_Operator;
1920 -------------------------------------
1921 -- Expand_Packed_Element_Reference --
1922 -------------------------------------
1924 procedure Expand_Packed_Element_Reference (N : Node_Id) is
1925 Loc : constant Source_Ptr := Sloc (N);
1926 Obj : Node_Id;
1927 Atyp : Entity_Id;
1928 PAT : Entity_Id;
1929 Ctyp : Entity_Id;
1930 Csiz : Int;
1931 Shift : Node_Id;
1932 Cmask : Uint;
1933 Lit : Node_Id;
1934 Arg : Node_Id;
1936 begin
1937 -- If not bit packed, we have the enumeration case, which is easily
1938 -- dealt with (just adjust the subscripts of the indexed component)
1940 -- Note: this leaves the result as an indexed component, which is
1941 -- still a variable, so can be used in the assignment case, as is
1942 -- required in the enumeration case.
1944 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
1945 Setup_Enumeration_Packed_Array_Reference (N);
1946 return;
1947 end if;
1949 -- Remaining processing is for the bit-packed case
1951 Obj := Relocate_Node (Prefix (N));
1952 Convert_To_Actual_Subtype (Obj);
1953 Atyp := Etype (Obj);
1954 PAT := Packed_Array_Type (Atyp);
1955 Ctyp := Component_Type (Atyp);
1956 Csiz := UI_To_Int (Component_Size (Atyp));
1958 -- Case of component size 1,2,4 or any component size for the modular
1959 -- case. These are the cases for which we can inline the code.
1961 if Csiz = 1 or else Csiz = 2 or else Csiz = 4
1962 or else (Present (PAT) and then Is_Modular_Integer_Type (PAT))
1963 then
1964 Setup_Inline_Packed_Array_Reference (N, Atyp, Obj, Cmask, Shift);
1965 Lit := Make_Integer_Literal (Loc, Cmask);
1966 Set_Print_In_Hex (Lit);
1968 -- We generate a shift right to position the field, followed by a
1969 -- masking operation to extract the bit field, and we finally do an
1970 -- unchecked conversion to convert the result to the required target.
1972 -- Note that the unchecked conversion automatically deals with the
1973 -- bias if we are dealing with a biased representation. What will
1974 -- happen is that we temporarily generate the biased representation,
1975 -- but almost immediately that will be converted to the original
1976 -- unbiased component type, and the bias will disappear.
1978 Arg :=
1979 Make_Op_And (Loc,
1980 Left_Opnd => Make_Shift_Right (Obj, Shift),
1981 Right_Opnd => Lit);
1983 -- We neded to analyze this before we do the unchecked convert
1984 -- below, but we need it temporarily attached to the tree for
1985 -- this analysis (hence the temporary Set_Parent call).
1987 Set_Parent (Arg, Parent (N));
1988 Analyze_And_Resolve (Arg);
1990 Rewrite (N,
1991 RJ_Unchecked_Convert_To (Ctyp, Arg));
1993 -- All other component sizes for non-modular case
1995 else
1996 -- We generate
1998 -- Component_Type!(Get_nn (Arr'address, Subscr))
2000 -- where Subscr is the computed linear subscript
2002 declare
2003 Get_nn : Entity_Id;
2004 Subscr : Node_Id;
2006 begin
2007 -- Acquire proper Get entity. We use the aligned or unaligned
2008 -- case as appropriate.
2010 if Known_Aligned_Enough (Obj, Csiz) then
2011 Get_nn := RTE (Get_Id (Csiz));
2012 else
2013 Get_nn := RTE (GetU_Id (Csiz));
2014 end if;
2016 -- Now generate the get reference
2018 Compute_Linear_Subscript (Atyp, N, Subscr);
2020 -- Below we make the assumption that Obj is at least byte
2021 -- aligned, since otherwise its address cannot be taken.
2022 -- The assumption holds since the only arrays that can be
2023 -- misaligned are small packed arrays which are implemented
2024 -- as a modular type, and that is not the case here.
2026 Rewrite (N,
2027 Unchecked_Convert_To (Ctyp,
2028 Make_Function_Call (Loc,
2029 Name => New_Occurrence_Of (Get_nn, Loc),
2030 Parameter_Associations => New_List (
2031 Make_Attribute_Reference (Loc,
2032 Attribute_Name => Name_Address,
2033 Prefix => Obj),
2034 Subscr))));
2035 end;
2036 end if;
2038 Analyze_And_Resolve (N, Ctyp, Suppress => All_Checks);
2040 end Expand_Packed_Element_Reference;
2042 ----------------------
2043 -- Expand_Packed_Eq --
2044 ----------------------
2046 -- Handles expansion of "=" on packed array types
2048 procedure Expand_Packed_Eq (N : Node_Id) is
2049 Loc : constant Source_Ptr := Sloc (N);
2050 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2051 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2053 LLexpr : Node_Id;
2054 RLexpr : Node_Id;
2056 Ltyp : Entity_Id;
2057 Rtyp : Entity_Id;
2058 PAT : Entity_Id;
2060 begin
2061 Convert_To_Actual_Subtype (L);
2062 Convert_To_Actual_Subtype (R);
2063 Ltyp := Underlying_Type (Etype (L));
2064 Rtyp := Underlying_Type (Etype (R));
2066 Convert_To_PAT_Type (L);
2067 Convert_To_PAT_Type (R);
2068 PAT := Etype (L);
2070 LLexpr :=
2071 Make_Op_Multiply (Loc,
2072 Left_Opnd =>
2073 Make_Attribute_Reference (Loc,
2074 Attribute_Name => Name_Length,
2075 Prefix => New_Occurrence_Of (Ltyp, Loc)),
2076 Right_Opnd =>
2077 Make_Integer_Literal (Loc, Component_Size (Ltyp)));
2079 RLexpr :=
2080 Make_Op_Multiply (Loc,
2081 Left_Opnd =>
2082 Make_Attribute_Reference (Loc,
2083 Attribute_Name => Name_Length,
2084 Prefix => New_Occurrence_Of (Rtyp, Loc)),
2085 Right_Opnd =>
2086 Make_Integer_Literal (Loc, Component_Size (Rtyp)));
2088 -- For the modular case, we transform the comparison to:
2090 -- Ltyp'Length = Rtyp'Length and then PAT!(L) = PAT!(R)
2092 -- where PAT is the packed array type. This works fine, since in the
2093 -- modular case we guarantee that the unused bits are always zeroes.
2094 -- We do have to compare the lengths because we could be comparing
2095 -- two different subtypes of the same base type.
2097 if Is_Modular_Integer_Type (PAT) then
2098 Rewrite (N,
2099 Make_And_Then (Loc,
2100 Left_Opnd =>
2101 Make_Op_Eq (Loc,
2102 Left_Opnd => LLexpr,
2103 Right_Opnd => RLexpr),
2105 Right_Opnd =>
2106 Make_Op_Eq (Loc,
2107 Left_Opnd => L,
2108 Right_Opnd => R)));
2110 -- For the non-modular case, we call a runtime routine
2112 -- System.Bit_Ops.Bit_Eq
2113 -- (L'Address, L_Length, R'Address, R_Length)
2115 -- where PAT is the packed array type, and the lengths are the lengths
2116 -- in bits of the original packed arrays. This routine takes care of
2117 -- not comparing the unused bits in the last byte.
2119 else
2120 Rewrite (N,
2121 Make_Function_Call (Loc,
2122 Name => New_Occurrence_Of (RTE (RE_Bit_Eq), Loc),
2123 Parameter_Associations => New_List (
2124 Make_Byte_Aligned_Attribute_Reference (Loc,
2125 Attribute_Name => Name_Address,
2126 Prefix => L),
2128 LLexpr,
2130 Make_Byte_Aligned_Attribute_Reference (Loc,
2131 Attribute_Name => Name_Address,
2132 Prefix => R),
2134 RLexpr)));
2135 end if;
2137 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
2138 end Expand_Packed_Eq;
2140 -----------------------
2141 -- Expand_Packed_Not --
2142 -----------------------
2144 -- Handles expansion of "not" on packed array types
2146 procedure Expand_Packed_Not (N : Node_Id) is
2147 Loc : constant Source_Ptr := Sloc (N);
2148 Typ : constant Entity_Id := Etype (N);
2149 Opnd : constant Node_Id := Relocate_Node (Right_Opnd (N));
2151 Rtyp : Entity_Id;
2152 PAT : Entity_Id;
2153 Lit : Node_Id;
2155 begin
2156 Convert_To_Actual_Subtype (Opnd);
2157 Rtyp := Etype (Opnd);
2159 -- First an odd and silly test. We explicitly check for the case
2160 -- where the 'First of the component type is equal to the 'Last of
2161 -- this component type, and if this is the case, we make sure that
2162 -- constraint error is raised. The reason is that the NOT is bound
2163 -- to cause CE in this case, and we will not otherwise catch it.
2165 -- Believe it or not, this was reported as a bug. Note that nearly
2166 -- always, the test will evaluate statically to False, so the code
2167 -- will be statically removed, and no extra overhead caused.
2169 declare
2170 CT : constant Entity_Id := Component_Type (Rtyp);
2172 begin
2173 Insert_Action (N,
2174 Make_Raise_Constraint_Error (Loc,
2175 Condition =>
2176 Make_Op_Eq (Loc,
2177 Left_Opnd =>
2178 Make_Attribute_Reference (Loc,
2179 Prefix => New_Occurrence_Of (CT, Loc),
2180 Attribute_Name => Name_First),
2182 Right_Opnd =>
2183 Make_Attribute_Reference (Loc,
2184 Prefix => New_Occurrence_Of (CT, Loc),
2185 Attribute_Name => Name_Last)),
2186 Reason => CE_Range_Check_Failed));
2187 end;
2189 -- Now that that silliness is taken care of, get packed array type
2191 Convert_To_PAT_Type (Opnd);
2192 PAT := Etype (Opnd);
2194 -- For the case where the packed array type is a modular type,
2195 -- not A expands simply into:
2197 -- rtyp!(PAT!(A) xor mask)
2199 -- where PAT is the packed array type, and mask is a mask of all
2200 -- one bits of length equal to the size of this packed type and
2201 -- rtyp is the actual subtype of the operand
2203 Lit := Make_Integer_Literal (Loc, 2 ** Esize (PAT) - 1);
2204 Set_Print_In_Hex (Lit);
2206 if not Is_Array_Type (PAT) then
2207 Rewrite (N,
2208 Unchecked_Convert_To (Rtyp,
2209 Make_Op_Xor (Loc,
2210 Left_Opnd => Opnd,
2211 Right_Opnd => Lit)));
2213 -- For the array case, we insert the actions
2215 -- Result : Typ;
2217 -- System.Bitops.Bit_Not
2218 -- (Opnd'Address,
2219 -- Typ'Length * Typ'Component_Size;
2220 -- Result'Address);
2222 -- where Opnd is the Packed_Bytes{1,2,4} operand and the second
2223 -- argument is the length of the operand in bits. Then we replace
2224 -- the expression by a reference to Result.
2226 else
2227 declare
2228 Result_Ent : constant Entity_Id :=
2229 Make_Defining_Identifier (Loc,
2230 Chars => New_Internal_Name ('T'));
2232 begin
2233 Insert_Actions (N, New_List (
2235 Make_Object_Declaration (Loc,
2236 Defining_Identifier => Result_Ent,
2237 Object_Definition => New_Occurrence_Of (Rtyp, Loc)),
2239 Make_Procedure_Call_Statement (Loc,
2240 Name => New_Occurrence_Of (RTE (RE_Bit_Not), Loc),
2241 Parameter_Associations => New_List (
2243 Make_Byte_Aligned_Attribute_Reference (Loc,
2244 Attribute_Name => Name_Address,
2245 Prefix => Opnd),
2247 Make_Op_Multiply (Loc,
2248 Left_Opnd =>
2249 Make_Attribute_Reference (Loc,
2250 Prefix =>
2251 New_Occurrence_Of
2252 (Etype (First_Index (Rtyp)), Loc),
2253 Attribute_Name => Name_Range_Length),
2254 Right_Opnd =>
2255 Make_Integer_Literal (Loc, Component_Size (Rtyp))),
2257 Make_Byte_Aligned_Attribute_Reference (Loc,
2258 Attribute_Name => Name_Address,
2259 Prefix => New_Occurrence_Of (Result_Ent, Loc))))));
2261 Rewrite (N,
2262 New_Occurrence_Of (Result_Ent, Loc));
2263 end;
2264 end if;
2266 Analyze_And_Resolve (N, Typ, Suppress => All_Checks);
2268 end Expand_Packed_Not;
2270 -------------------------------------
2271 -- Involves_Packed_Array_Reference --
2272 -------------------------------------
2274 function Involves_Packed_Array_Reference (N : Node_Id) return Boolean is
2275 begin
2276 if Nkind (N) = N_Indexed_Component
2277 and then Is_Bit_Packed_Array (Etype (Prefix (N)))
2278 then
2279 return True;
2281 elsif Nkind (N) = N_Selected_Component then
2282 return Involves_Packed_Array_Reference (Prefix (N));
2284 else
2285 return False;
2286 end if;
2287 end Involves_Packed_Array_Reference;
2289 --------------------------
2290 -- Known_Aligned_Enough --
2291 --------------------------
2293 function Known_Aligned_Enough (Obj : Node_Id; Csiz : Nat) return Boolean is
2294 Typ : constant Entity_Id := Etype (Obj);
2296 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean;
2297 -- If the component is in a record that contains previous packed
2298 -- components, consider it unaligned because the back-end might
2299 -- choose to pack the rest of the record. Lead to less efficient code,
2300 -- but safer vis-a-vis of back-end choices.
2302 --------------------------------
2303 -- In_Partially_Packed_Record --
2304 --------------------------------
2306 function In_Partially_Packed_Record (Comp : Entity_Id) return Boolean is
2307 Rec_Type : constant Entity_Id := Scope (Comp);
2308 Prev_Comp : Entity_Id;
2310 begin
2311 Prev_Comp := First_Entity (Rec_Type);
2312 while Present (Prev_Comp) loop
2313 if Is_Packed (Etype (Prev_Comp)) then
2314 return True;
2316 elsif Prev_Comp = Comp then
2317 return False;
2318 end if;
2320 Next_Entity (Prev_Comp);
2321 end loop;
2323 return False;
2324 end In_Partially_Packed_Record;
2326 -- Start of processing for Known_Aligned_Enough
2328 begin
2329 -- Odd bit sizes don't need alignment anyway
2331 if Csiz mod 2 = 1 then
2332 return True;
2334 -- If we have a specified alignment, see if it is sufficient, if not
2335 -- then we can't possibly be aligned enough in any case.
2337 elsif Known_Alignment (Etype (Obj)) then
2338 -- Alignment required is 4 if size is a multiple of 4, and
2339 -- 2 otherwise (e.g. 12 bits requires 4, 10 bits requires 2)
2341 if Alignment (Etype (Obj)) < 4 - (Csiz mod 4) then
2342 return False;
2343 end if;
2344 end if;
2346 -- OK, alignment should be sufficient, if object is aligned
2348 -- If object is strictly aligned, then it is definitely aligned
2350 if Strict_Alignment (Typ) then
2351 return True;
2353 -- Case of subscripted array reference
2355 elsif Nkind (Obj) = N_Indexed_Component then
2357 -- If we have a pointer to an array, then this is definitely
2358 -- aligned, because pointers always point to aligned versions.
2360 if Is_Access_Type (Etype (Prefix (Obj))) then
2361 return True;
2363 -- Otherwise, go look at the prefix
2365 else
2366 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2367 end if;
2369 -- Case of record field
2371 elsif Nkind (Obj) = N_Selected_Component then
2373 -- What is significant here is whether the record type is packed
2375 if Is_Record_Type (Etype (Prefix (Obj)))
2376 and then Is_Packed (Etype (Prefix (Obj)))
2377 then
2378 return False;
2380 -- Or the component has a component clause which might cause
2381 -- the component to become unaligned (we can't tell if the
2382 -- backend is doing alignment computations).
2384 elsif Present (Component_Clause (Entity (Selector_Name (Obj)))) then
2385 return False;
2387 elsif In_Partially_Packed_Record (Entity (Selector_Name (Obj))) then
2388 return False;
2390 -- In all other cases, go look at prefix
2392 else
2393 return Known_Aligned_Enough (Prefix (Obj), Csiz);
2394 end if;
2396 elsif Nkind (Obj) = N_Type_Conversion then
2397 return Known_Aligned_Enough (Expression (Obj), Csiz);
2399 -- For a formal parameter, it is safer to assume that it is not
2400 -- aligned, because the formal may be unconstrained while the actual
2401 -- is constrained. In this situation, a small constrained packed
2402 -- array, represented in modular form, may be unaligned.
2404 elsif Is_Entity_Name (Obj) then
2405 return not Is_Formal (Entity (Obj));
2406 else
2408 -- If none of the above, must be aligned
2409 return True;
2410 end if;
2411 end Known_Aligned_Enough;
2413 ---------------------
2414 -- Make_Shift_Left --
2415 ---------------------
2417 function Make_Shift_Left (N : Node_Id; S : Node_Id) return Node_Id is
2418 Nod : Node_Id;
2420 begin
2421 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2422 return N;
2423 else
2424 Nod :=
2425 Make_Op_Shift_Left (Sloc (N),
2426 Left_Opnd => N,
2427 Right_Opnd => S);
2428 Set_Shift_Count_OK (Nod, True);
2429 return Nod;
2430 end if;
2431 end Make_Shift_Left;
2433 ----------------------
2434 -- Make_Shift_Right --
2435 ----------------------
2437 function Make_Shift_Right (N : Node_Id; S : Node_Id) return Node_Id is
2438 Nod : Node_Id;
2440 begin
2441 if Compile_Time_Known_Value (S) and then Expr_Value (S) = 0 then
2442 return N;
2443 else
2444 Nod :=
2445 Make_Op_Shift_Right (Sloc (N),
2446 Left_Opnd => N,
2447 Right_Opnd => S);
2448 Set_Shift_Count_OK (Nod, True);
2449 return Nod;
2450 end if;
2451 end Make_Shift_Right;
2453 -----------------------------
2454 -- RJ_Unchecked_Convert_To --
2455 -----------------------------
2457 function RJ_Unchecked_Convert_To
2458 (Typ : Entity_Id;
2459 Expr : Node_Id) return Node_Id
2461 Source_Typ : constant Entity_Id := Etype (Expr);
2462 Target_Typ : constant Entity_Id := Typ;
2464 Src : Node_Id := Expr;
2466 Source_Siz : Nat;
2467 Target_Siz : Nat;
2469 begin
2470 Source_Siz := UI_To_Int (RM_Size (Source_Typ));
2471 Target_Siz := UI_To_Int (RM_Size (Target_Typ));
2473 -- First step, if the source type is not a discrete type, then we
2474 -- first convert to a modular type of the source length, since
2475 -- otherwise, on a big-endian machine, we get left-justification.
2476 -- We do it for little-endian machines as well, because there might
2477 -- be junk bits that are not cleared if the type is not numeric.
2479 if Source_Siz /= Target_Siz
2480 and then not Is_Discrete_Type (Source_Typ)
2481 then
2482 Src := Unchecked_Convert_To (RTE (Bits_Id (Source_Siz)), Src);
2483 end if;
2485 -- In the big endian case, if the lengths of the two types differ,
2486 -- then we must worry about possible left justification in the
2487 -- conversion, and avoiding that is what this is all about.
2489 if Bytes_Big_Endian and then Source_Siz /= Target_Siz then
2491 -- Next step. If the target is not a discrete type, then we first
2492 -- convert to a modular type of the target length, since
2493 -- otherwise, on a big-endian machine, we get left-justification.
2495 if not Is_Discrete_Type (Target_Typ) then
2496 Src := Unchecked_Convert_To (RTE (Bits_Id (Target_Siz)), Src);
2497 end if;
2498 end if;
2500 -- And now we can do the final conversion to the target type
2502 return Unchecked_Convert_To (Target_Typ, Src);
2503 end RJ_Unchecked_Convert_To;
2505 ----------------------------------------------
2506 -- Setup_Enumeration_Packed_Array_Reference --
2507 ----------------------------------------------
2509 -- All we have to do here is to find the subscripts that correspond
2510 -- to the index positions that have non-standard enumeration types
2511 -- and insert a Pos attribute to get the proper subscript value.
2513 -- Finally the prefix must be uncheck converted to the corresponding
2514 -- packed array type.
2516 -- Note that the component type is unchanged, so we do not need to
2517 -- fiddle with the types (Gigi always automatically takes the packed
2518 -- array type if it is set, as it will be in this case).
2520 procedure Setup_Enumeration_Packed_Array_Reference (N : Node_Id) is
2521 Pfx : constant Node_Id := Prefix (N);
2522 Typ : constant Entity_Id := Etype (N);
2523 Exprs : constant List_Id := Expressions (N);
2524 Expr : Node_Id;
2526 begin
2527 -- If the array is unconstrained, then we replace the array
2528 -- reference with its actual subtype. This actual subtype will
2529 -- have a packed array type with appropriate bounds.
2531 if not Is_Constrained (Packed_Array_Type (Etype (Pfx))) then
2532 Convert_To_Actual_Subtype (Pfx);
2533 end if;
2535 Expr := First (Exprs);
2536 while Present (Expr) loop
2537 declare
2538 Loc : constant Source_Ptr := Sloc (Expr);
2539 Expr_Typ : constant Entity_Id := Etype (Expr);
2541 begin
2542 if Is_Enumeration_Type (Expr_Typ)
2543 and then Has_Non_Standard_Rep (Expr_Typ)
2544 then
2545 Rewrite (Expr,
2546 Make_Attribute_Reference (Loc,
2547 Prefix => New_Occurrence_Of (Expr_Typ, Loc),
2548 Attribute_Name => Name_Pos,
2549 Expressions => New_List (Relocate_Node (Expr))));
2550 Analyze_And_Resolve (Expr, Standard_Natural);
2551 end if;
2552 end;
2554 Next (Expr);
2555 end loop;
2557 Rewrite (N,
2558 Make_Indexed_Component (Sloc (N),
2559 Prefix =>
2560 Unchecked_Convert_To (Packed_Array_Type (Etype (Pfx)), Pfx),
2561 Expressions => Exprs));
2563 Analyze_And_Resolve (N, Typ);
2565 end Setup_Enumeration_Packed_Array_Reference;
2567 -----------------------------------------
2568 -- Setup_Inline_Packed_Array_Reference --
2569 -----------------------------------------
2571 procedure Setup_Inline_Packed_Array_Reference
2572 (N : Node_Id;
2573 Atyp : Entity_Id;
2574 Obj : in out Node_Id;
2575 Cmask : out Uint;
2576 Shift : out Node_Id)
2578 Loc : constant Source_Ptr := Sloc (N);
2579 PAT : Entity_Id;
2580 Otyp : Entity_Id;
2581 Csiz : Uint;
2582 Osiz : Uint;
2584 begin
2585 Csiz := Component_Size (Atyp);
2587 Convert_To_PAT_Type (Obj);
2588 PAT := Etype (Obj);
2590 Cmask := 2 ** Csiz - 1;
2592 if Is_Array_Type (PAT) then
2593 Otyp := Component_Type (PAT);
2594 Osiz := Component_Size (PAT);
2596 else
2597 Otyp := PAT;
2599 -- In the case where the PAT is a modular type, we want the actual
2600 -- size in bits of the modular value we use. This is neither the
2601 -- Object_Size nor the Value_Size, either of which may have been
2602 -- reset to strange values, but rather the minimum size. Note that
2603 -- since this is a modular type with full range, the issue of
2604 -- biased representation does not arise.
2606 Osiz := UI_From_Int (Minimum_Size (Otyp));
2607 end if;
2609 Compute_Linear_Subscript (Atyp, N, Shift);
2611 -- If the component size is not 1, then the subscript must be
2612 -- multiplied by the component size to get the shift count.
2614 if Csiz /= 1 then
2615 Shift :=
2616 Make_Op_Multiply (Loc,
2617 Left_Opnd => Make_Integer_Literal (Loc, Csiz),
2618 Right_Opnd => Shift);
2619 end if;
2621 -- If we have the array case, then this shift count must be broken
2622 -- down into a byte subscript, and a shift within the byte.
2624 if Is_Array_Type (PAT) then
2626 declare
2627 New_Shift : Node_Id;
2629 begin
2630 -- We must analyze shift, since we will duplicate it
2632 Set_Parent (Shift, N);
2633 Analyze_And_Resolve
2634 (Shift, Standard_Integer, Suppress => All_Checks);
2636 -- The shift count within the word is
2637 -- shift mod Osiz
2639 New_Shift :=
2640 Make_Op_Mod (Loc,
2641 Left_Opnd => Duplicate_Subexpr (Shift),
2642 Right_Opnd => Make_Integer_Literal (Loc, Osiz));
2644 -- The subscript to be used on the PAT array is
2645 -- shift / Osiz
2647 Obj :=
2648 Make_Indexed_Component (Loc,
2649 Prefix => Obj,
2650 Expressions => New_List (
2651 Make_Op_Divide (Loc,
2652 Left_Opnd => Duplicate_Subexpr (Shift),
2653 Right_Opnd => Make_Integer_Literal (Loc, Osiz))));
2655 Shift := New_Shift;
2656 end;
2658 -- For the modular integer case, the object to be manipulated is
2659 -- the entire array, so Obj is unchanged. Note that we will reset
2660 -- its type to PAT before returning to the caller.
2662 else
2663 null;
2664 end if;
2666 -- The one remaining step is to modify the shift count for the
2667 -- big-endian case. Consider the following example in a byte:
2669 -- xxxxxxxx bits of byte
2670 -- vvvvvvvv bits of value
2671 -- 33221100 little-endian numbering
2672 -- 00112233 big-endian numbering
2674 -- Here we have the case of 2-bit fields
2676 -- For the little-endian case, we already have the proper shift
2677 -- count set, e.g. for element 2, the shift count is 2*2 = 4.
2679 -- For the big endian case, we have to adjust the shift count,
2680 -- computing it as (N - F) - shift, where N is the number of bits
2681 -- in an element of the array used to implement the packed array,
2682 -- F is the number of bits in a source level array element, and
2683 -- shift is the count so far computed.
2685 if Bytes_Big_Endian then
2686 Shift :=
2687 Make_Op_Subtract (Loc,
2688 Left_Opnd => Make_Integer_Literal (Loc, Osiz - Csiz),
2689 Right_Opnd => Shift);
2690 end if;
2692 Set_Parent (Shift, N);
2693 Set_Parent (Obj, N);
2694 Analyze_And_Resolve (Obj, Otyp, Suppress => All_Checks);
2695 Analyze_And_Resolve (Shift, Standard_Integer, Suppress => All_Checks);
2697 -- Make sure final type of object is the appropriate packed type
2699 Set_Etype (Obj, Otyp);
2701 end Setup_Inline_Packed_Array_Reference;
2703 end Exp_Pakd;